blob: f8ec8e9ce32b8be6cea6bb6df35bf0425fd44ea6 [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 Moolenaar071d4272004-06-13 20:20:40 +00001353 * This function should ALWAYS be called to end Visual mode, except from
1354 * do_pending_operator().
1355 */
1356 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001357end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358{
1359#ifdef FEAT_CLIPBOARD
1360 /*
1361 * If we are using the clipboard, then remember what was selected in case
1362 * we need to paste it somewhere while we still own the selection.
1363 * Only do this when the clipboard is already owned. Don't want to grab
1364 * the selection when hitting ESC.
1365 */
1366 if (clip_star.available && clip_star.owned)
1367 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001368
1369# if defined(FEAT_EVAL)
1370 // Emit a TextYankPost for the automatic copy of the selection into the
1371 // star and/or plus register.
1372 if (has_textyankpost())
1373 {
1374 if (clip_isautosel_star())
1375 call_yank_do_autocmd('*');
1376 if (clip_isautosel_plus())
1377 call_yank_do_autocmd('+');
1378 }
1379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380#endif
1381
1382 VIsual_active = FALSE;
Bram Moolenaar85eee5b2021-06-03 20:34:57 +02001383 reset_held_button();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 setmouse();
1385 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001387 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001388 curbuf->b_visual.vi_mode = VIsual_mode;
1389 curbuf->b_visual.vi_start = VIsual;
1390 curbuf->b_visual.vi_end = curwin->w_cursor;
1391 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#ifdef FEAT_EVAL
1393 curbuf->b_visual_mode_eval = VIsual_mode;
1394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (!virtual_active())
1396 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001397 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001399 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400}
1401
1402/*
1403 * Reset VIsual_active and VIsual_reselect.
1404 */
1405 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001406reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
1408 if (VIsual_active)
1409 {
1410 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001411 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 }
1413 VIsual_reselect = FALSE;
1414}
1415
1416/*
1417 * Reset VIsual_active and VIsual_reselect if it's set.
1418 */
1419 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001420reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 if (VIsual_active)
1423 {
1424 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001425 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 VIsual_reselect = FALSE;
1427 }
1428}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001430 void
1431restore_visual_mode(void)
1432{
1433 if (VIsual_mode_orig != NUL)
1434 {
1435 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1436 VIsual_mode_orig = NUL;
1437 }
1438}
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440/*
1441 * Check for a balloon-eval special item to include when searching for an
1442 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1443 * Returns TRUE if the character at "*ptr" should be included.
1444 * "dir" is FORWARD or BACKWARD, the direction of searching.
1445 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1446 * "bnp" points to a counter for square brackets.
1447 */
1448 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001449find_is_eval_item(
1450 char_u *ptr,
1451 int *colp,
1452 int *bnp,
1453 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001455 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1457 ++*bnp;
1458 if (*bnp > 0)
1459 {
1460 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1461 --*bnp;
1462 return TRUE;
1463 }
1464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001465 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (*ptr == '.')
1467 return TRUE;
1468
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001469 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1471 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1472 {
1473 *colp += dir;
1474 return TRUE;
1475 }
1476 return FALSE;
1477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
1479/*
1480 * Find the identifier under or to the right of the cursor.
1481 * "find_type" can have one of three values:
1482 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001483 * FIND_STRING: find any non-white text
1484 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001485 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 *
1487 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001488 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001490 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 * This doesn't match the real Vi but I like it a little better and it
1492 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001493 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 * When FIND_IDENT isn't defined, we backup until a blank.
1495 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001496 * Returns the length of the text, or zero if no text is found.
1497 * If text is found, a pointer to the text is put in "*text". This
1498 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 */
1500 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001501find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
1503 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001504 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505}
1506
1507/*
1508 * Like find_ident_under_cursor(), but for any window and any position.
1509 * However: Uses 'iskeyword' from the current window!.
1510 */
1511 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001512find_ident_at_pos(
1513 win_T *wp,
1514 linenr_T lnum,
1515 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001516 char_u **text,
1517 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001518 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
1520 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001521 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 int this_class = 0;
1524 int prev_class;
1525 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001526 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
1528 /*
1529 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001530 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 */
1532 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1533 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1534 {
1535 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001536 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 */
1538 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (has_mbyte)
1540 {
1541 while (ptr[col] != NUL)
1542 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001543 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1545 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 this_class = mb_get_class(ptr + col);
1547 if (this_class != 0 && (i == 1 || this_class != 1))
1548 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001549 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 }
1551 }
1552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001554 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 )
1557 ++col;
1558
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001559 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
1562 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001563 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (has_mbyte)
1566 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001567 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1569 this_class = mb_get_class((char_u *)"a");
1570 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001572 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 {
1574 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1575 prev_class = mb_get_class(ptr + prevcol);
1576 if (this_class != prev_class
1577 && (i == 0
1578 || prev_class == 0
1579 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 && (!(find_type & FIND_EVAL)
1581 || prevcol == 0
1582 || !find_is_eval_item(ptr + prevcol, &prevcol,
1583 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 )
1585 break;
1586 col = prevcol;
1587 }
1588
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001589 // If we don't want just any old text, or we've found an
1590 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (this_class > 2)
1592 this_class = 2;
1593 if (!(find_type & FIND_STRING) || this_class == 2)
1594 break;
1595 }
1596 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
1598 while (col > 0
1599 && ((i == 0
1600 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001601 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 && (!(find_type & FIND_IDENT)
1603 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 || ((find_type & FIND_EVAL)
1605 && col > 1
1606 && find_is_eval_item(ptr + col - 1, &col,
1607 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 ))
1609 --col;
1610
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001611 // If we don't want just any old text, or we've found an
1612 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1614 break;
1615 }
1616 }
1617
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001618 if (ptr[col] == NUL || (i == 0
1619 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001621 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001622 if ((find_type & FIND_NOERROR) == 0)
1623 {
1624 if (find_type & FIND_STRING)
1625 emsg(_("E348: No string under cursor"));
1626 else
1627 emsg(_(e_noident));
1628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 return 0;
1630 }
1631 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001632 *text = ptr;
1633 if (textcol != NULL)
1634 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001637 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 bn = 0;
1640 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 if (has_mbyte)
1643 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001644 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 this_class = mb_get_class(ptr);
1646 while (ptr[col] != NUL
1647 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1648 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 || ((find_type & FIND_EVAL)
1650 && col <= (int)startcol
1651 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001653 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
1655 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001657 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 || ((find_type & FIND_EVAL)
1659 && col <= (int)startcol
1660 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
1664 return col;
1665}
1666
1667/*
1668 * Prepare for redo of a normal command.
1669 */
1670 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001671prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 prep_redo(cap->oap->regname, cap->count0,
1674 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1675}
1676
1677/*
1678 * Prepare for redo of any command.
1679 * Note that only the last argument can be a multi-byte char.
1680 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001682prep_redo(
1683 int regname,
1684 long num,
1685 int cmd1,
1686 int cmd2,
1687 int cmd3,
1688 int cmd4,
1689 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
1691 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001692 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 AppendCharToRedobuff('"');
1695 AppendCharToRedobuff(regname);
1696 }
1697 if (num)
1698 AppendNumberToRedobuff(num);
1699
1700 if (cmd1 != NUL)
1701 AppendCharToRedobuff(cmd1);
1702 if (cmd2 != NUL)
1703 AppendCharToRedobuff(cmd2);
1704 if (cmd3 != NUL)
1705 AppendCharToRedobuff(cmd3);
1706 if (cmd4 != NUL)
1707 AppendCharToRedobuff(cmd4);
1708 if (cmd5 != NUL)
1709 AppendCharToRedobuff(cmd5);
1710}
1711
1712/*
1713 * check for operator active and clear it
1714 *
1715 * return TRUE if operator was active
1716 */
1717 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001718checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 if (oap->op_type == OP_NOP)
1721 return FALSE;
1722 clearopbeep(oap);
1723 return TRUE;
1724}
1725
1726/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001727 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001729 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 */
1731 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001732checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001734 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 return FALSE;
1736 clearopbeep(oap);
1737 return TRUE;
1738}
1739
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001741clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
1743 oap->op_type = OP_NOP;
1744 oap->regname = 0;
1745 oap->motion_force = NUL;
1746 oap->use_reg_one = FALSE;
1747}
1748
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001750clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 clearop(oap);
1753 beep_flush();
1754}
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756/*
1757 * Remove the shift modifier from a special key.
1758 */
1759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001760unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761{
1762 switch (cap->cmdchar)
1763 {
1764 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1765 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1766 case K_S_UP: cap->cmdchar = K_UP; break;
1767 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1768 case K_S_HOME: cap->cmdchar = K_HOME; break;
1769 case K_S_END: cap->cmdchar = K_END; break;
1770 }
1771 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001774/*
1775 * If the mode is currently displayed clear the command line or update the
1776 * command displayed.
1777 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001778 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001779may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001780{
1781 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001782 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001783#ifdef FEAT_CMDL_INFO
1784 else
1785 clear_showcmd();
1786#endif
1787}
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1790/*
1791 * Routines for displaying a partly typed command
1792 */
1793
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001794#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001796static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797static int showcmd_is_clear = TRUE;
1798static int showcmd_visual = FALSE;
1799
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001800static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001803clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
1805 if (!p_sc)
1806 return;
1807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (VIsual_active && !char_avail())
1809 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001810 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 long lines;
1812 colnr_T leftcol, rightcol;
1813 linenr_T top, bot;
1814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001815 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001816 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
1818 top = VIsual.lnum;
1819 bot = curwin->w_cursor.lnum;
1820 }
1821 else
1822 {
1823 top = curwin->w_cursor.lnum;
1824 bot = VIsual.lnum;
1825 }
1826# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001827 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001828 (void)hasFolding(top, &top, NULL);
1829 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830# endif
1831 lines = bot - top + 1;
1832
1833 if (VIsual_mode == Ctrl_V)
1834 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001835# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001836 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001837 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001839 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001840 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001841 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001844# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001845 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001846 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1849 (long)(rightcol - leftcol + 1));
1850 }
1851 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1852 sprintf((char *)showcmd_buf, "%ld", lines);
1853 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001854 {
1855 char_u *s, *e;
1856 int l;
1857 int bytes = 0;
1858 int chars = 0;
1859
1860 if (cursor_bot)
1861 {
1862 s = ml_get_pos(&VIsual);
1863 e = ml_get_cursor();
1864 }
1865 else
1866 {
1867 s = ml_get_cursor();
1868 e = ml_get_pos(&VIsual);
1869 }
1870 while ((*p_sel != 'e') ? s <= e : s < e)
1871 {
1872 l = (*mb_ptr2len)(s);
1873 if (l == 0)
1874 {
1875 ++bytes;
1876 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001877 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001878 }
1879 bytes += l;
1880 ++chars;
1881 s += l;
1882 }
1883 if (bytes == chars)
1884 sprintf((char *)showcmd_buf, "%d", chars);
1885 else
1886 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1887 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001888 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 showcmd_visual = TRUE;
1890 }
1891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
1893 showcmd_buf[0] = NUL;
1894 showcmd_visual = FALSE;
1895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001896 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (showcmd_is_clear)
1898 return;
1899 }
1900
1901 display_showcmd();
1902}
1903
1904/*
1905 * Add 'c' to string of shown command chars.
1906 * Return TRUE if output has been written (and setcursor() has been called).
1907 */
1908 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001909add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910{
1911 char_u *p;
1912 int old_len;
1913 int extra_len;
1914 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 int i;
1916 static int ignore[] =
1917 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001918#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1920 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001921#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001922 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001923 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1925 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001926 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001928 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 0
1930 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
Bram Moolenaar09df3122006-01-23 22:23:09 +00001932 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 return FALSE;
1934
1935 if (showcmd_visual)
1936 {
1937 showcmd_buf[0] = NUL;
1938 showcmd_visual = FALSE;
1939 }
1940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001941 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (IS_SPECIAL(c))
1943 for (i = 0; ignore[i] != 0; ++i)
1944 if (ignore[i] == c)
1945 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001948 if (*p == ' ')
1949 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 old_len = (int)STRLEN(showcmd_buf);
1951 extra_len = (int)STRLEN(p);
1952 overflow = old_len + extra_len - SHOWCMD_COLS;
1953 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001954 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1955 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 STRCAT(showcmd_buf, p);
1957
1958 if (char_avail())
1959 return FALSE;
1960
1961 display_showcmd();
1962
1963 return TRUE;
1964}
1965
1966 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001967add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968{
1969 if (!add_to_showcmd(c))
1970 setcursor();
1971}
1972
1973/*
1974 * Delete 'len' characters from the end of the shown command.
1975 */
1976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001977del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978{
1979 int old_len;
1980
1981 if (!p_sc)
1982 return;
1983
1984 old_len = (int)STRLEN(showcmd_buf);
1985 if (len > old_len)
1986 len = old_len;
1987 showcmd_buf[old_len - len] = NUL;
1988
1989 if (!char_avail())
1990 display_showcmd();
1991}
1992
1993/*
1994 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1995 * something and there is a partial mapping.
1996 */
1997 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001998push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 if (p_sc)
2001 STRCPY(old_showcmd_buf, showcmd_buf);
2002}
2003
2004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002005pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 if (!p_sc)
2008 return;
2009
2010 STRCPY(showcmd_buf, old_showcmd_buf);
2011
2012 display_showcmd();
2013}
2014
2015 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002016display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 int len;
2019
2020 cursor_off();
2021
2022 len = (int)STRLEN(showcmd_buf);
2023 if (len == 0)
2024 showcmd_is_clear = TRUE;
2025 else
2026 {
2027 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2028 showcmd_is_clear = FALSE;
2029 }
2030
2031 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002032 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2033 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 */
2035 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002037 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038}
2039#endif
2040
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041/*
2042 * When "check" is FALSE, prepare for commands that scroll the window.
2043 * When "check" is TRUE, take care of scroll-binding after the window has
2044 * scrolled. Called from normal_cmd() and edit().
2045 */
2046 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002047do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
2049 static win_T *old_curwin = NULL;
2050 static linenr_T old_topline = 0;
2051#ifdef FEAT_DIFF
2052 static int old_topfill = 0;
2053#endif
2054 static buf_T *old_buf = NULL;
2055 static colnr_T old_leftcol = 0;
2056
2057 if (check && curwin->w_p_scb)
2058 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002059 // If a ":syncbind" command was just used, don't scroll, only reset
2060 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (did_syncbind)
2062 did_syncbind = FALSE;
2063 else if (curwin == old_curwin)
2064 {
2065 /*
2066 * Synchronize other windows, as necessary according to
2067 * 'scrollbind'. Don't do this after an ":edit" command, except
2068 * when 'diff' is set.
2069 */
2070 if ((curwin->w_buffer == old_buf
2071#ifdef FEAT_DIFF
2072 || curwin->w_p_diff
2073#endif
2074 )
2075 && (curwin->w_topline != old_topline
2076#ifdef FEAT_DIFF
2077 || curwin->w_topfill != old_topfill
2078#endif
2079 || curwin->w_leftcol != old_leftcol))
2080 {
2081 check_scrollbind(curwin->w_topline - old_topline,
2082 (long)(curwin->w_leftcol - old_leftcol));
2083 }
2084 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002085 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
2087 /*
2088 * When switching between windows, make sure that the relative
2089 * vertical offset is valid for the new window. The relative
2090 * offset is invalid whenever another 'scrollbind' window has
2091 * scrolled to a point that would force the current window to
2092 * scroll past the beginning or end of its buffer. When the
2093 * resync is performed, some of the other 'scrollbind' windows may
2094 * need to jump so that the current window's relative position is
2095 * visible on-screen.
2096 */
2097 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2098 }
2099 curwin->w_scbind_pos = curwin->w_topline;
2100 }
2101
2102 old_curwin = curwin;
2103 old_topline = curwin->w_topline;
2104#ifdef FEAT_DIFF
2105 old_topfill = curwin->w_topfill;
2106#endif
2107 old_buf = curwin->w_buffer;
2108 old_leftcol = curwin->w_leftcol;
2109}
2110
2111/*
2112 * Synchronize any windows that have "scrollbind" set, based on the
2113 * number of rows by which the current window has changed
2114 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2115 */
2116 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002117check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
2119 int want_ver;
2120 int want_hor;
2121 win_T *old_curwin = curwin;
2122 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 int old_VIsual_select = VIsual_select;
2124 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 colnr_T tgt_leftcol = curwin->w_leftcol;
2126 long topline;
2127 long y;
2128
2129 /*
2130 * check 'scrollopt' string for vertical and horizontal scroll options
2131 */
2132 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2133#ifdef FEAT_DIFF
2134 want_ver |= old_curwin->w_p_diff;
2135#endif
2136 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2137
2138 /*
2139 * loop through the scrollbound windows and scroll accordingly
2140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002142 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
2144 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002145 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (curwin != old_curwin && curwin->w_p_scb)
2147 {
2148 /*
2149 * do the vertical scroll
2150 */
2151 if (want_ver)
2152 {
2153#ifdef FEAT_DIFF
2154 if (old_curwin->w_p_diff && curwin->w_p_diff)
2155 {
2156 diff_set_topline(old_curwin, curwin);
2157 }
2158 else
2159#endif
2160 {
2161 curwin->w_scbind_pos += topline_diff;
2162 topline = curwin->w_scbind_pos;
2163 if (topline > curbuf->b_ml.ml_line_count)
2164 topline = curbuf->b_ml.ml_line_count;
2165 if (topline < 1)
2166 topline = 1;
2167
2168 y = topline - curwin->w_topline;
2169 if (y > 0)
2170 scrollup(y, FALSE);
2171 else
2172 scrolldown(-y, FALSE);
2173 }
2174
2175 redraw_later(VALID);
2176 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179
2180 /*
2181 * do the horizontal scroll
2182 */
2183 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2184 {
2185 curwin->w_leftcol = tgt_leftcol;
2186 leftcol_changed();
2187 }
2188 }
2189 }
2190
2191 /*
2192 * reset current-window
2193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 VIsual_select = old_VIsual_select;
2195 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 curwin = old_curwin;
2197 curbuf = old_curbuf;
2198}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200/*
2201 * Command character that's ignored.
2202 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002203 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002206nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002208 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209}
2210
2211/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002212 * Command character that doesn't do anything, but unlike nv_ignore() does
2213 * start edit(). Used for "startinsert" executed while starting up.
2214 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002215 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002216nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002217{
2218}
2219
2220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * Command character doesn't exist.
2222 */
2223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002224nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226 clearopbeep(cap->oap);
2227}
2228
2229/*
2230 * <Help> and <F1> commands.
2231 */
2232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002233nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235 if (!checkclearopq(cap->oap))
2236 ex_help(NULL);
2237}
2238
2239/*
2240 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2241 */
2242 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002243nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002245#ifdef FEAT_JOB_CHANNEL
2246 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2247 clearopbeep(cap->oap);
2248 else
2249#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002250 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002251 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002252 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002253 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2254 op_addsub(cap->oap, cap->count1, cap->arg);
2255 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002256 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002257 else if (VIsual_active)
2258 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002259 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002260 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261}
2262
2263/*
2264 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2265 */
2266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002267nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002271 if (mod_mask & MOD_MASK_CTRL)
2272 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002273 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002274 if (cap->arg == BACKWARD)
2275 goto_tabpage(-(int)cap->count1);
2276 else
2277 goto_tabpage((int)cap->count0);
2278 }
2279 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002280 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282}
2283
2284/*
2285 * Implementation of "gd" and "gD" command.
2286 */
2287 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002288nv_gd(
2289 oparg_T *oap,
2290 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002291 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
2293 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 char_u *ptr;
2295
Bram Moolenaard9d30582005-05-18 22:10:28 +00002296 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002297 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2298 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002300#ifdef FEAT_FOLDING
2301 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2302 foldOpenCursor();
2303#endif
2304}
2305
2306/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002307 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2308 * otherwise.
2309 */
2310 static int
2311is_ident(char_u *line, int offset)
2312{
2313 int i;
2314 int incomment = FALSE;
2315 int instring = 0;
2316 int prev = 0;
2317
2318 for (i = 0; i < offset && line[i] != NUL; i++)
2319 {
2320 if (instring != 0)
2321 {
2322 if (prev != '\\' && line[i] == instring)
2323 instring = 0;
2324 }
2325 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2326 {
2327 instring = line[i];
2328 }
2329 else
2330 {
2331 if (incomment)
2332 {
2333 if (prev == '*' && line[i] == '/')
2334 incomment = FALSE;
2335 }
2336 else if (prev == '/' && line[i] == '*')
2337 {
2338 incomment = TRUE;
2339 }
2340 else if (prev == '/' && line[i] == '/')
2341 {
2342 return FALSE;
2343 }
2344 }
2345
2346 prev = line[i];
2347 }
2348
2349 return incomment == FALSE && instring == 0;
2350}
2351
2352/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002353 * Search for variable declaration of "ptr[len]".
2354 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2355 * current file ("gD").
2356 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002357 * Return FAIL when not found.
2358 */
2359 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002360find_decl(
2361 char_u *ptr,
2362 int len,
2363 int locally,
2364 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002365 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002366{
2367 char_u *pat;
2368 pos_T old_pos;
2369 pos_T par_pos;
2370 pos_T found_pos;
2371 int t;
2372 int save_p_ws;
2373 int save_p_scs;
2374 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002375 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002376 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002377 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002378
2379 if ((pat = alloc(len + 7)) == NULL)
2380 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002381
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002382 // Put "\V" before the pattern to avoid that the special meaning of "."
2383 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002384 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2385 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 old_pos = curwin->w_cursor;
2387 save_p_ws = p_ws;
2388 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002389 p_ws = FALSE; // don't wrap around end of file now
2390 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392 /*
2393 * With "gD" go to line 1.
2394 * With "gd" Search back for the start of the current function, then go
2395 * back until a blank line. If this fails go to line 1.
2396 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002397 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002399 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002401 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 else
2404 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002405 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2407 --curwin->w_cursor.lnum;
2408 }
2409 curwin->w_cursor.col = 0;
2410
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002411 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002412 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002413 for (;;)
2414 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002415 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002416 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002417 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002418 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002419
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002420 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002421 {
2422 pos_T *pos;
2423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002424 // Check that the block the match is in doesn't end before the
2425 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002426 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2427 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2428 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002429 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002430 // There can't be a useful match before the end of this block.
2431 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002432 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002433 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002434 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002435 }
2436
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002437 if (t == FAIL)
2438 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002439 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002440 if (found_pos.lnum != 0)
2441 {
2442 curwin->w_cursor = found_pos;
2443 t = OK;
2444 }
2445 break;
2446 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002447 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002448 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002449 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002450 ++curwin->w_cursor.lnum;
2451 curwin->w_cursor.col = 0;
2452 continue;
2453 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002454 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2455
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002456 // If the current position is not a valid identifier and a previous
2457 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002458 if (!valid && found_pos.lnum != 0)
2459 {
2460 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002461 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002462 }
2463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002464 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002465 if (valid && !locally)
2466 break;
2467 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002468 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002469 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002470 if (found_pos.lnum != 0)
2471 curwin->w_cursor = found_pos;
2472 break;
2473 }
2474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002475 // For finding a local variable and the match is before the "{" or
2476 // inside a comment, continue searching. For K&R style function
2477 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002478 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002479 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002480 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002481 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002482 // Remove SEARCH_START from flags to avoid getting stuck at one
2483 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002484 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002486
2487 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002489 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 curwin->w_cursor = old_pos;
2491 }
2492 else
2493 {
2494 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002495 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 reset_search_dir();
2497 }
2498
2499 vim_free(pat);
2500 p_ws = save_p_ws;
2501 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002502
2503 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504}
2505
2506/*
2507 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2508 * lines rather than lines in the file.
2509 * 'dist' must be positive.
2510 *
2511 * Return OK if able to move cursor, FAIL otherwise.
2512 */
2513 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002514nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 int linelen = linetabsize(ml_get_curline());
2517 int retval = OK;
2518 int atend = FALSE;
2519 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002520 int col_off1; // margin offset for first screen line
2521 int col_off2; // margin offset for wrapped screen line
2522 int width1; // text width for first screen line
2523 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524
2525 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002526 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528 col_off1 = curwin_col_off();
2529 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002530 width1 = curwin->w_width - col_off1;
2531 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002532 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002533 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 /*
2538 * Instead of sticking at the last character of the buffer line we
2539 * try to stick in the last column of the screen.
2540 */
2541 if (curwin->w_curswant == MAXCOL)
2542 {
2543 atend = TRUE;
2544 validate_virtcol();
2545 if (width1 <= 0)
2546 curwin->w_curswant = 0;
2547 else
2548 {
2549 curwin->w_curswant = width1 - 1;
2550 if (curwin->w_virtcol > curwin->w_curswant)
2551 curwin->w_curswant += ((curwin->w_virtcol
2552 - curwin->w_curswant - 1) / width2 + 1) * width2;
2553 }
2554 }
2555 else
2556 {
2557 if (linelen > width1)
2558 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2559 else
2560 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002561 if (curwin->w_curswant >= (colnr_T)n)
2562 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564
2565 while (dist--)
2566 {
2567 if (dir == BACKWARD)
2568 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002569 if ((long)curwin->w_curswant >= width1
2570#ifdef FEAT_FOLDING
2571 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2572#endif
2573 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002574 // Move back within the line. This can give a negative value
2575 // for w_curswant if width1 < width2 (with cpoptions+=n),
2576 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 curwin->w_curswant -= width2;
2578 else
2579 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002580 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002582 // Move to the start of a closed fold. Don't do that when
2583 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (!(fdo_flags & FDO_ALL))
2585 (void)hasFolding(curwin->w_cursor.lnum,
2586 &curwin->w_cursor.lnum, NULL);
2587#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002588 if (curwin->w_cursor.lnum == 1)
2589 {
2590 retval = FAIL;
2591 break;
2592 }
2593 --curwin->w_cursor.lnum;
2594
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 linelen = linetabsize(ml_get_curline());
2596 if (linelen > width1)
2597 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2598 + 1) * width2;
2599 }
2600 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002601 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
2603 if (linelen > width1)
2604 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2605 else
2606 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002607 if (curwin->w_curswant + width2 < (colnr_T)n
2608#ifdef FEAT_FOLDING
2609 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2610#endif
2611 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002612 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 curwin->w_curswant += width2;
2614 else
2615 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002616 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002618 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2620 &curwin->w_cursor.lnum);
2621#endif
2622 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2623 {
2624 retval = FAIL;
2625 break;
2626 }
2627 curwin->w_cursor.lnum++;
2628 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002629 // Check if the cursor has moved below the number display
2630 // when width1 < width2 (with cpoptions+=n). Subtract width2
2631 // to get a negative value for w_curswant, which will get
2632 // clipped to column 0.
2633 if (curwin->w_curswant >= width1)
2634 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002635 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637 }
2638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002641 if (virtual_active() && atend)
2642 coladvance(MAXCOL);
2643 else
2644 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2647 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002648 colnr_T virtcol;
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 /*
2651 * Check for landing on a character that got split at the end of the
2652 * last line. We want to advance a screenline, not end up in the same
2653 * screenline or move two screenlines.
2654 */
2655 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002656 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002657#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002658 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2659 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002660#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002661
2662 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 && (curwin->w_curswant < (colnr_T)width1
2664 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2665 : ((curwin->w_curswant - width1) % width2
2666 > (colnr_T)width2 / 2)))
2667 --curwin->w_cursor.col;
2668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
2670 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002671 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
2673 return retval;
2674}
2675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676/*
2677 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2678 * cap->arg must be TRUE for CTRL-E.
2679 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002680 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002681nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683 if (!checkclearop(cap->oap))
2684 scroll_redraw(cap->arg, cap->count1);
2685}
2686
2687/*
2688 * Scroll "count" lines up or down, and redraw.
2689 */
2690 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002691scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692{
2693 linenr_T prev_topline = curwin->w_topline;
2694#ifdef FEAT_DIFF
2695 int prev_topfill = curwin->w_topfill;
2696#endif
2697 linenr_T prev_lnum = curwin->w_cursor.lnum;
2698
2699 if (up)
2700 scrollup(count, TRUE);
2701 else
2702 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002703 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002705 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2706 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 cursor_correct();
2708 check_cursor_moved(curwin);
2709 curwin->w_valid |= VALID_TOPLINE;
2710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // If moved back to where we were, at least move the cursor, otherwise
2712 // we get stuck at one position. Don't move the cursor up if the
2713 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 while (curwin->w_topline == prev_topline
2715#ifdef FEAT_DIFF
2716 && curwin->w_topfill == prev_topfill
2717#endif
2718 )
2719 {
2720 if (up)
2721 {
2722 if (curwin->w_cursor.lnum > prev_lnum
2723 || cursor_down(1L, FALSE) == FAIL)
2724 break;
2725 }
2726 else
2727 {
2728 if (curwin->w_cursor.lnum < prev_lnum
2729 || prev_topline == 1L
2730 || cursor_up(1L, FALSE) == FAIL)
2731 break;
2732 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002733 // Mark w_topline as valid, otherwise the screen jumps back at the
2734 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 check_cursor_moved(curwin);
2736 curwin->w_valid |= VALID_TOPLINE;
2737 }
2738 }
2739 if (curwin->w_cursor.lnum != prev_lnum)
2740 coladvance(curwin->w_curswant);
2741 redraw_later(VALID);
2742}
2743
2744/*
2745 * Commands that start with "z".
2746 */
2747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002748nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749{
2750 long n;
2751 colnr_T col;
2752 int nchar = cap->nchar;
2753#ifdef FEAT_FOLDING
2754 long old_fdl = curwin->w_p_fdl;
2755 int old_fen = curwin->w_p_fen;
2756#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002757#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002758 int undo = FALSE;
2759#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002760 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
2762 if (VIM_ISDIGIT(nchar))
2763 {
2764 /*
2765 * "z123{nchar}": edit the count before obtaining {nchar}
2766 */
2767 if (checkclearop(cap->oap))
2768 return;
2769 n = nchar - '0';
2770 for (;;)
2771 {
2772#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002773 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774#endif
2775 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002776 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002777 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 --no_mapping;
2780 --allow_keys;
2781#ifdef FEAT_CMDL_INFO
2782 (void)add_to_showcmd(nchar);
2783#endif
2784 if (nchar == K_DEL || nchar == K_KDEL)
2785 n /= 10;
2786 else if (VIM_ISDIGIT(nchar))
2787 n = n * 10 + (nchar - '0');
2788 else if (nchar == CAR)
2789 {
2790#ifdef FEAT_GUI
2791 need_mouse_correct = TRUE;
2792#endif
2793 win_setheight((int)n);
2794 break;
2795 }
2796 else if (nchar == 'l'
2797 || nchar == 'h'
2798 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002799 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
2801 cap->count1 = n ? n * cap->count1 : cap->count1;
2802 goto dozet;
2803 }
2804 else
2805 {
2806 clearopbeep(cap->oap);
2807 break;
2808 }
2809 }
2810 cap->oap->op_type = OP_NOP;
2811 return;
2812 }
2813
2814dozet:
2815 if (
2816#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002817 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2818 // and "zC" only in Visual mode. "zj" and "zk" are motion
2819 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 cap->nchar != 'f' && cap->nchar != 'F'
2821 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2822 && cap->nchar != 'j' && cap->nchar != 'k'
2823 &&
2824#endif
2825 checkclearop(cap->oap))
2826 return;
2827
2828 /*
2829 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2830 * If line number given, set cursor.
2831 */
2832 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2833 && cap->count0
2834 && cap->count0 != curwin->w_cursor.lnum)
2835 {
2836 setpcmark();
2837 if (cap->count0 > curbuf->b_ml.ml_line_count)
2838 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2839 else
2840 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002841 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 }
2843
2844 switch (nchar)
2845 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 case '+':
2848 if (cap->count0 == 0)
2849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // No count given: put cursor at the line below screen
2851 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2853 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2854 else
2855 curwin->w_cursor.lnum = curwin->w_botline;
2856 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 case NL:
2859 case CAR:
2860 case K_KENTER:
2861 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002862 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
2864 case 't': scroll_cursor_top(0, TRUE);
2865 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002866 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 break;
2868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002871 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
2873 case 'z': scroll_cursor_halfway(TRUE);
2874 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002875 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
2877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // "z^", "z-" and "zb": put cursor at bottom of screen
2879 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2880 // when <count> is at bottom of window, and puts that one at
2881 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 if (cap->count0 != 0)
2883 {
2884 scroll_cursor_bot(0, TRUE);
2885 curwin->w_cursor.lnum = curwin->w_topline;
2886 }
2887 else if (curwin->w_topline == 1)
2888 curwin->w_cursor.lnum = 1;
2889 else
2890 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 case '-':
2893 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002894 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
2896 case 'b': scroll_cursor_bot(0, TRUE);
2897 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002898 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 break;
2900
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002903 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002904 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002906 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 case 'h':
2908 case K_LEFT:
2909 if (!curwin->w_p_wrap)
2910 {
2911 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2912 curwin->w_leftcol = 0;
2913 else
2914 curwin->w_leftcol -= (colnr_T)cap->count1;
2915 leftcol_changed();
2916 }
2917 break;
2918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002919 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002920 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 case 'l':
2925 case K_RIGHT:
2926 if (!curwin->w_p_wrap)
2927 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 curwin->w_leftcol += (colnr_T)cap->count1;
2930 leftcol_changed();
2931 }
2932 break;
2933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 case 's': if (!curwin->w_p_wrap)
2936 {
2937#ifdef FEAT_FOLDING
2938 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 else
2941#endif
2942 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002943 if ((long)col > siso)
2944 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 else
2946 col = 0;
2947 if (curwin->w_leftcol != col)
2948 {
2949 curwin->w_leftcol = col;
2950 redraw_later(NOT_VALID);
2951 }
2952 }
2953 break;
2954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 case 'e': if (!curwin->w_p_wrap)
2957 {
2958#ifdef FEAT_FOLDING
2959 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 else
2962#endif
2963 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002964 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002965 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 col = 0;
2967 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002968 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 if (curwin->w_leftcol != col)
2970 {
2971 curwin->w_leftcol = col;
2972 redraw_later(NOT_VALID);
2973 }
2974 }
2975 break;
2976
Christian Brabandt2fa93842021-05-30 22:17:25 +02002977 // "zp", "zP" in block mode put without addind trailing spaces
2978 case 'P':
2979 case 'p': nv_put(cap);
2980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // "zF": create fold command
2983 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 case 'F':
2985 case 'f': if (foldManualAllowed(TRUE))
2986 {
2987 cap->nchar = 'f';
2988 nv_operator(cap);
2989 curwin->w_p_fen = TRUE;
2990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2993 {
2994 nv_operator(cap);
2995 finish_op = TRUE;
2996 }
2997 }
2998 else
2999 clearopbeep(cap->oap);
3000 break;
3001
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003002 // "zd": delete fold at cursor
3003 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 case 'd':
3005 case 'D': if (foldManualAllowed(FALSE))
3006 {
3007 if (VIsual_active)
3008 nv_operator(cap);
3009 else
3010 deleteFold(curwin->w_cursor.lnum,
3011 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3012 }
3013 break;
3014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003015 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 case 'E': if (foldmethodIsManual(curwin))
3017 {
3018 clearFolding(curwin);
3019 changed_window_setting();
3020 }
3021 else if (foldmethodIsMarker(curwin))
3022 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3023 TRUE, FALSE);
3024 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 break;
3027
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003028 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 case 'n': curwin->w_p_fen = FALSE;
3030 break;
3031
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003032 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 case 'N': curwin->w_p_fen = TRUE;
3034 break;
3035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3038 break;
3039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003040 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3042 openFold(curwin->w_cursor.lnum, cap->count1);
3043 else
3044 {
3045 closeFold(curwin->w_cursor.lnum, cap->count1);
3046 curwin->w_p_fen = TRUE;
3047 }
3048 break;
3049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003050 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3052 openFoldRecurse(curwin->w_cursor.lnum);
3053 else
3054 {
3055 closeFoldRecurse(curwin->w_cursor.lnum);
3056 curwin->w_p_fen = TRUE;
3057 }
3058 break;
3059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003060 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 case 'o': if (VIsual_active)
3062 nv_operator(cap);
3063 else
3064 openFold(curwin->w_cursor.lnum, cap->count1);
3065 break;
3066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003067 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 case 'O': if (VIsual_active)
3069 nv_operator(cap);
3070 else
3071 openFoldRecurse(curwin->w_cursor.lnum);
3072 break;
3073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003074 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 case 'c': if (VIsual_active)
3076 nv_operator(cap);
3077 else
3078 closeFold(curwin->w_cursor.lnum, cap->count1);
3079 curwin->w_p_fen = TRUE;
3080 break;
3081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 case 'C': if (VIsual_active)
3084 nv_operator(cap);
3085 else
3086 closeFoldRecurse(curwin->w_cursor.lnum);
3087 curwin->w_p_fen = TRUE;
3088 break;
3089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 case 'v': foldOpenCursor();
3092 break;
3093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096 curwin->w_foldinvalid = TRUE; // recompute folds
3097 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 foldOpenCursor();
3099 break;
3100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003103 curwin->w_foldinvalid = TRUE; // recompute folds
3104 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 break;
3106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003107 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003109 {
3110 curwin->w_p_fdl -= cap->count1;
3111 if (curwin->w_p_fdl < 0)
3112 curwin->w_p_fdl = 0;
3113 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003114 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 curwin->w_p_fen = TRUE;
3116 break;
3117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003118 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 case 'M': curwin->w_p_fdl = 0;
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 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003125 case 'r': curwin->w_p_fdl += cap->count1;
3126 {
3127 int d = getDeepestNesting();
3128
3129 if (curwin->w_p_fdl >= d)
3130 curwin->w_p_fdl = d;
3131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 break;
3133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003136 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 break;
3138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003139 case 'j': // "zj" move to next fold downwards
3140 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3142 cap->count1) == FAIL)
3143 clearopbeep(cap->oap);
3144 break;
3145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003148#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003150 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003151 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003152 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003153 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003154 --no_mapping;
3155 --allow_keys;
3156#ifdef FEAT_CMDL_INFO
3157 (void)add_to_showcmd(nchar);
3158#endif
3159 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3160 {
3161 clearopbeep(cap->oap);
3162 break;
3163 }
3164 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 case 'g': // "zg": add good word to word list
3168 case 'w': // "zw": add wrong word to word list
3169 case 'G': // "zG": add good word to temp word list
3170 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003171 {
3172 char_u *ptr = NULL;
3173 int len;
3174
3175 if (checkclearop(cap->oap))
3176 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003177 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3178 == FAIL)
3179 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003180 if (ptr == NULL)
3181 {
3182 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003184 // Find bad word under the cursor. When 'spell' is
3185 // off this fails and find_ident_under_cursor() is
3186 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003187 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003188 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003189 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003190 if (len != 0 && curwin->w_cursor.col <= pos.col)
3191 ptr = ml_get_pos(&curwin->w_cursor);
3192 curwin->w_cursor = pos;
3193 }
3194
Bram Moolenaarb765d632005-06-07 21:00:02 +00003195 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3196 FIND_IDENT)) == 0)
3197 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003198 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3199 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003200 (nchar == 'G' || nchar == 'W')
3201 ? 0 : (int)cap->count1,
3202 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003203 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003204 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003206 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003207 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003208 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003209 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003210#endif
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 default: clearopbeep(cap->oap);
3213 }
3214
3215#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003216 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (old_fen != curwin->w_p_fen)
3218 {
3219# ifdef FEAT_DIFF
3220 win_T *wp;
3221
3222 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3223 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003224 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 FOR_ALL_WINDOWS(wp)
3226 {
3227 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3228 {
3229 wp->w_p_fen = curwin->w_p_fen;
3230 changed_window_setting_win(wp);
3231 }
3232 }
3233 }
3234# endif
3235 changed_window_setting();
3236 }
3237
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003238 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (old_fdl != curwin->w_p_fdl)
3240 newFoldLevel();
3241#endif
3242}
3243
3244#ifdef FEAT_GUI
3245/*
3246 * Vertical scrollbar movement.
3247 */
3248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003249nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 if (cap->oap->op_type != OP_NOP)
3252 clearopbeep(cap->oap);
3253
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003254 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 gui_do_scroll();
3256}
3257
3258/*
3259 * Horizontal scrollbar movement.
3260 */
3261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003262nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 if (cap->oap->op_type != OP_NOP)
3265 clearopbeep(cap->oap);
3266
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003267 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003268 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269}
3270#endif
3271
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003272#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003273/*
3274 * Click in GUI tab.
3275 */
3276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003277nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003278{
3279 if (cap->oap->op_type != OP_NOP)
3280 clearopbeep(cap->oap);
3281
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003283 goto_tabpage(current_tab);
3284}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003285
3286/*
3287 * Selected item in tab line menu.
3288 */
3289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003290nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003291{
3292 if (cap->oap->op_type != OP_NOP)
3293 clearopbeep(cap->oap);
3294
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003295 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003296 handle_tabmenu();
3297}
3298
3299/*
3300 * Handle selecting an item of the GUI tab line menu.
3301 * Used in Normal and Insert mode.
3302 */
3303 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003304handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003305{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003306 switch (current_tabmenu)
3307 {
3308 case TABLINE_MENU_CLOSE:
3309 if (current_tab == 0)
3310 do_cmdline_cmd((char_u *)"tabclose");
3311 else
3312 {
3313 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3314 current_tab);
3315 do_cmdline_cmd(IObuff);
3316 }
3317 break;
3318
3319 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003320 if (current_tab == 0)
3321 do_cmdline_cmd((char_u *)"$tabnew");
3322 else
3323 {
3324 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3325 current_tab - 1);
3326 do_cmdline_cmd(IObuff);
3327 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003328 break;
3329
3330 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003331 if (current_tab == 0)
3332 do_cmdline_cmd((char_u *)"browse $tabnew");
3333 else
3334 {
3335 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3336 current_tab - 1);
3337 do_cmdline_cmd(IObuff);
3338 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003339 break;
3340 }
3341}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003342#endif
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344/*
3345 * "Q" command.
3346 */
3347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003348nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 /*
3351 * Ignore 'Q' in Visual mode, just give a beep.
3352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003354 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003355 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 do_exmode(FALSE);
3357}
3358
3359/*
3360 * Handle a ":" command.
3361 */
3362 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003363nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003365 int old_p_im;
3366 int cmd_result;
3367 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
Bram Moolenaar957cf672020-11-12 14:21:06 +01003369 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 nv_operator(cap);
3371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
3373 if (cap->oap->op_type != OP_NOP)
3374 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003375 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 cap->oap->motion_type = MCHAR;
3377 cap->oap->inclusive = FALSE;
3378 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003379 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003381 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 stuffcharReadbuff('.');
3383 if (cap->count0 > 1)
3384 {
3385 stuffReadbuff((char_u *)",.+");
3386 stuffnumReadbuff((long)cap->count0 - 1L);
3387 }
3388 }
3389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003390 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 if (KeyTyped)
3392 compute_cmdrow();
3393
3394 old_p_im = p_im;
3395
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003396 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003397 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003400 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (p_im != old_p_im)
3402 {
3403 if (p_im)
3404 restart_edit = 'i';
3405 else
3406 restart_edit = 0;
3407 }
3408
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003409 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003410 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003411 clearop(cap->oap);
3412 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3414 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003415 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3416 || did_emsg
3417 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003418 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 clearopbeep(cap->oap);
3420 }
3421}
3422
3423/*
3424 * Handle CTRL-G command.
3425 */
3426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003427nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003429 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
3431 VIsual_select = !VIsual_select;
3432 showmode();
3433 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003434 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003435 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 fileinfo((int)cap->count0, FALSE, TRUE);
3437}
3438
3439/*
3440 * Handle CTRL-H <Backspace> command.
3441 */
3442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003443nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (VIsual_active && VIsual_select)
3446 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003447 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 v_visop(cap);
3449 }
3450 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 nv_left(cap);
3452}
3453
3454/*
3455 * CTRL-L: clear screen and redraw.
3456 */
3457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003458nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 if (!checkclearop(cap->oap))
3461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003463 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003464 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003465# ifdef FEAT_RELTIME
3466 {
3467 win_T *wp;
3468
3469 FOR_ALL_WINDOWS(wp)
3470 wp->w_s->b_syn_slow = FALSE;
3471 }
3472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#endif
3474 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003475#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3476# ifdef VIMDLL
3477 if (!gui.in_use)
3478# endif
3479 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
3482}
3483
3484/*
3485 * CTRL-O: In Select mode: switch to Visual mode for one command.
3486 * Otherwise: Go to older pcmark.
3487 */
3488 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003489nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 if (VIsual_active && VIsual_select)
3492 {
3493 VIsual_select = FALSE;
3494 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003495 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 cap->count1 = -cap->count1;
3500 nv_pcmark(cap);
3501 }
3502}
3503
3504/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003505 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3506 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 */
3508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003509nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
3511 if (!checkclearopq(cap->oap))
3512 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3513 GETF_SETMARK|GETF_ALT, FALSE);
3514}
3515
3516/*
3517 * "Z" commands.
3518 */
3519 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003520nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 if (!checkclearopq(cap->oap))
3523 {
3524 switch (cap->nchar)
3525 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003526 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 case 'Z': do_cmdline_cmd((char_u *)"x");
3528 break;
3529
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003530 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 case 'Q': do_cmdline_cmd((char_u *)"q!");
3532 break;
3533
3534 default: clearopbeep(cap->oap);
3535 }
3536 }
3537}
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539/*
3540 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3541 */
3542 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003543do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544{
3545 oparg_T oa;
3546 cmdarg_T ca;
3547
3548 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003549 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 ca.oap = &oa;
3551 ca.cmdchar = c1;
3552 ca.nchar = c2;
3553 nv_ident(&ca);
3554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555
3556/*
3557 * Handle the commands that use the word under the cursor.
3558 * [g] CTRL-] :ta to current identifier
3559 * [g] 'K' run program for current identifier
3560 * [g] '*' / to current identifier or string
3561 * [g] '#' ? to current identifier or string
3562 * g ']' :tselect for current identifier
3563 */
3564 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003565nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566{
3567 char_u *ptr = NULL;
3568 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003569 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003570 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003572 char_u *kp; // value of 'keywordprg'
3573 int kp_help; // 'keywordprg' is ":he"
3574 int kp_ex; // 'keywordprg' starts with ":"
3575 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003577 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003578 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 char_u *aux_ptr;
3580 int isman;
3581 int isman_s;
3582
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 cmdchar = cap->nchar;
3586 g_cmd = TRUE;
3587 }
3588 else
3589 {
3590 cmdchar = cap->cmdchar;
3591 g_cmd = FALSE;
3592 }
3593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003594 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 cmdchar = '#';
3596
3597 /*
3598 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3599 */
3600 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3603 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (checkclearopq(cap->oap))
3605 return;
3606 }
3607
3608 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3609 (cmdchar == '*' || cmdchar == '#')
3610 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3611 {
3612 clearop(cap->oap);
3613 return;
3614 }
3615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003616 // Allocate buffer to put the command in. Inserting backslashes can
3617 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3618 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3620 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3621 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003622 if (kp_help && *skipwhite(ptr) == NUL)
3623 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003624 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003625 return;
3626 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003627 kp_ex = (*kp == ':');
3628 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3629 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (buf == NULL)
3631 return;
3632 buf[0] = NUL;
3633
3634 switch (cmdchar)
3635 {
3636 case '*':
3637 case '#':
3638 /*
3639 * Put cursor at start of word, makes search skip the word
3640 * under the cursor.
3641 * Call setpcmark() first, so "*``" puts the cursor back where
3642 * it was.
3643 */
3644 setpcmark();
3645 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3646
3647 if (!g_cmd && vim_iswordp(ptr))
3648 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003649 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 break;
3651
3652 case 'K':
3653 if (kp_help)
3654 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003655 else if (kp_ex)
3656 {
3657 if (cap->count0 != 0)
3658 vim_snprintf((char *)buf, buflen, "%s %ld",
3659 kp, cap->count0);
3660 else
3661 STRCPY(buf, kp);
3662 STRCAT(buf, " ");
3663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 else
3665 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003666 // An external command will probably use an argument starting
3667 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003668 while (*ptr == '-' && n > 0)
3669 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003670 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003671 --n;
3672 }
3673 if (n == 0)
3674 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003675 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003676 vim_free(buf);
3677 return;
3678 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003679
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003680 // When a count is given, turn it into a range. Is this
3681 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 isman = (STRCMP(kp, "man") == 0);
3683 isman_s = (STRCMP(kp, "man -s") == 0);
3684 if (cap->count0 != 0 && !(isman || isman_s))
3685 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3686
3687 STRCAT(buf, "! ");
3688 if (cap->count0 == 0 && isman_s)
3689 STRCAT(buf, "man");
3690 else
3691 STRCAT(buf, kp);
3692 STRCAT(buf, " ");
3693 if (cap->count0 != 0 && (isman || isman_s))
3694 {
3695 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3696 STRCAT(buf, " ");
3697 }
3698 }
3699 break;
3700
3701 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003702 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#ifdef FEAT_CSCOPE
3704 if (p_cst)
3705 STRCPY(buf, "cstag ");
3706 else
3707#endif
3708 STRCPY(buf, "ts ");
3709 break;
3710
3711 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003712 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 if (curbuf->b_help)
3714 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003716 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003717 if (g_cmd)
3718 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003719 else if (cap->count0 == 0)
3720 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003721 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003722 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 }
3725
3726 /*
3727 * Now grab the chars in the identifier
3728 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003729 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003731 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003732 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003733 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003734 p = vim_strsave_fnameescape(ptr, FALSE);
3735 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003736 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003737 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003738 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003739 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003741 vim_free(buf);
3742 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003744 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003745 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003746 {
3747 vim_free(buf);
3748 vim_free(p);
3749 return;
3750 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003751 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003752 STRCAT(buf, p);
3753 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003755 else
3756 {
3757 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003758 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003759 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003760 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003761 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003762 {
3763 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003764 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003765 aux_ptr = (char_u *)"";
3766 else
3767 aux_ptr = (char_u *)"\\|\"\n[";
3768 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003769 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003770 aux_ptr = (char_u *)"\\|\"\n*?[";
3771
3772 p = buf + STRLEN(buf);
3773 while (n-- > 0)
3774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003775 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003776 if (vim_strchr(aux_ptr, *ptr) != NULL)
3777 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003778 // When current byte is a part of multibyte character, copy all
3779 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003780 if (has_mbyte)
3781 {
3782 int i;
3783 int len = (*mb_ptr2len)(ptr) - 1;
3784
3785 for (i = 0; i < len && n >= 1; ++i, --n)
3786 *p++ = *ptr++;
3787 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003788 *p++ = *ptr++;
3789 }
3790 *p = NUL;
3791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
3793 /*
3794 * Execute the command.
3795 */
3796 if (cmdchar == '*' || cmdchar == '#')
3797 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003798 if (!g_cmd && (has_mbyte
3799 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3800 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003802
3803 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003804 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003806
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003807 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003810 {
3811 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003813 g_tag_at_cursor = FALSE;
3814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
3816 vim_free(buf);
3817}
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819/*
3820 * Get visually selected text, within one line only.
3821 * Returns FAIL if more than one line selected.
3822 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003823 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003824get_visual_text(
3825 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003826 char_u **pp, // return: start of selected text
3827 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 if (VIsual_mode != 'V')
3830 unadjust_for_sel();
3831 if (VIsual.lnum != curwin->w_cursor.lnum)
3832 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003833 if (cap != NULL)
3834 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 return FAIL;
3836 }
3837 if (VIsual_mode == 'V')
3838 {
3839 *pp = ml_get_curline();
3840 *lenp = (int)STRLEN(*pp);
3841 }
3842 else
3843 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003844 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 *pp = ml_get_pos(&curwin->w_cursor);
3847 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3848 }
3849 else
3850 {
3851 *pp = ml_get_pos(&VIsual);
3852 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003855 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003856 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858 reset_VIsual_and_resel();
3859 return OK;
3860}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862/*
3863 * CTRL-T: backwards in tag stack
3864 */
3865 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003866nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867{
3868 if (!checkclearopq(cap->oap))
3869 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3870}
3871
3872/*
3873 * Handle scrolling command 'H', 'L' and 'M'.
3874 */
3875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003876nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 int used = 0;
3879 long n;
3880#ifdef FEAT_FOLDING
3881 linenr_T lnum;
3882#endif
3883 int half;
3884
3885 cap->oap->motion_type = MLINE;
3886 setpcmark();
3887
3888 if (cap->cmdchar == 'L')
3889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003890 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 curwin->w_cursor.lnum = curwin->w_botline - 1;
3892 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3893 curwin->w_cursor.lnum = 1;
3894 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003895 {
3896#ifdef FEAT_FOLDING
3897 if (hasAnyFolding(curwin))
3898 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003899 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003900 for (n = cap->count1 - 1; n > 0
3901 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3902 {
3903 (void)hasFolding(curwin->w_cursor.lnum,
3904 &curwin->w_cursor.lnum, NULL);
3905 --curwin->w_cursor.lnum;
3906 }
3907 }
3908 else
3909#endif
3910 curwin->w_cursor.lnum -= cap->count1 - 1;
3911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913 else
3914 {
3915 if (cap->cmdchar == 'M')
3916 {
3917#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003918 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 used -= diff_check_fill(curwin, curwin->w_topline)
3920 - curwin->w_topfill;
3921#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003922 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3924 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3925 {
3926#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003927 // Count half he number of filler lines to be "below this
3928 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3930 + n) / 2 >= half)
3931 {
3932 --n;
3933 break;
3934 }
3935#endif
3936 used += plines(curwin->w_topline + n);
3937 if (used >= half)
3938 break;
3939#ifdef FEAT_FOLDING
3940 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3941 n = lnum - curwin->w_topline;
3942#endif
3943 }
3944 if (n > 0 && used > curwin->w_height)
3945 --n;
3946 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003947 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003950#ifdef FEAT_FOLDING
3951 if (hasAnyFolding(curwin))
3952 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003953 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003954 lnum = curwin->w_topline;
3955 while (n-- > 0 && lnum < curwin->w_botline - 1)
3956 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003957 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003958 ++lnum;
3959 }
3960 n = lnum - curwin->w_topline;
3961 }
3962#endif
3963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 curwin->w_cursor.lnum = curwin->w_topline + n;
3965 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3966 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3967 }
3968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003969 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003970 if (cap->oap->op_type == OP_NOP)
3971 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 beginline(BL_SOL | BL_FIX);
3973}
3974
3975/*
3976 * Cursor right commands.
3977 */
3978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003979nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
3981 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003982 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003984 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3985 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003986 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003987 if (mod_mask & MOD_MASK_CTRL)
3988 cap->arg = TRUE;
3989 nv_wordcmd(cap);
3990 return;
3991 }
3992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 cap->oap->motion_type = MCHAR;
3994 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003995 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003998 * In virtual edit mode, there's no such thing as "past_line", as lines
3999 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 */
4001 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004002 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
4004 for (n = cap->count1; n > 0; --n)
4005 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004006 if ((!past_line && oneright() == FAIL)
4007 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004008 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
4010 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004011 * <Space> wraps to next line if 'whichwrap' has 's'.
4012 * 'l' wraps to next line if 'whichwrap' has 'l'.
4013 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 */
4015 if ( ((cap->cmdchar == ' '
4016 && vim_strchr(p_ww, 's') != NULL)
4017 || (cap->cmdchar == 'l'
4018 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004019 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 && vim_strchr(p_ww, '>') != NULL))
4021 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4022 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004023 // When deleting we also count the NL as a character.
4024 // Set cap->oap->inclusive when last char in the line is
4025 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004026 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004028 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 cap->oap->inclusive = TRUE;
4030 else
4031 {
4032 ++curwin->w_cursor.lnum;
4033 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 curwin->w_set_curswant = TRUE;
4036 cap->oap->inclusive = FALSE;
4037 }
4038 continue;
4039 }
4040 if (cap->oap->op_type == OP_NOP)
4041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004042 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (n == cap->count1)
4044 beep_flush();
4045 }
4046 else
4047 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004048 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 cap->oap->inclusive = TRUE;
4050 }
4051 break;
4052 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004053 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
4055 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 if (virtual_active())
4057 oneright();
4058 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004061 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 ++curwin->w_cursor.col;
4064 }
4065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067#ifdef FEAT_FOLDING
4068 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4069 && cap->oap->op_type == OP_NOP)
4070 foldOpenCursor();
4071#endif
4072}
4073
4074/*
4075 * Cursor left commands.
4076 *
4077 * Returns TRUE when operator end should not be adjusted.
4078 */
4079 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004080nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081{
4082 long n;
4083
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004084 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4085 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004086 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004087 if (mod_mask & MOD_MASK_CTRL)
4088 cap->arg = 1;
4089 nv_bck_word(cap);
4090 return;
4091 }
4092
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 cap->oap->motion_type = MCHAR;
4094 cap->oap->inclusive = FALSE;
4095 for (n = cap->count1; n > 0; --n)
4096 {
4097 if (oneleft() == FAIL)
4098 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004099 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4100 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4101 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if ( (((cap->cmdchar == K_BS
4103 || cap->cmdchar == Ctrl_H)
4104 && vim_strchr(p_ww, 'b') != NULL)
4105 || (cap->cmdchar == 'h'
4106 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004107 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 && vim_strchr(p_ww, '<') != NULL))
4109 && curwin->w_cursor.lnum > 1)
4110 {
4111 --(curwin->w_cursor.lnum);
4112 coladvance((colnr_T)MAXCOL);
4113 curwin->w_set_curswant = TRUE;
4114
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004115 // When the NL before the first char has to be deleted we
4116 // put the cursor on the NUL after the previous line.
4117 // This is a very special case, be careful!
4118 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 if ( (cap->oap->op_type == OP_DELETE
4120 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004121 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004123 char_u *cp = ml_get_cursor();
4124
4125 if (*cp != NUL)
4126 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004127 if (has_mbyte)
4128 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4129 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004130 ++curwin->w_cursor.col;
4131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 cap->retval |= CA_NO_ADJ_OP_END;
4133 }
4134 continue;
4135 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004136 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4138 beep_flush();
4139 break;
4140 }
4141 }
4142#ifdef FEAT_FOLDING
4143 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4144 && cap->oap->op_type == OP_NOP)
4145 foldOpenCursor();
4146#endif
4147}
4148
4149/*
4150 * Cursor up commands.
4151 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4152 */
4153 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004154nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004156 if (mod_mask & MOD_MASK_SHIFT)
4157 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004158 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004159 cap->arg = BACKWARD;
4160 nv_page(cap);
4161 }
4162 else
4163 {
4164 cap->oap->motion_type = MLINE;
4165 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4166 clearopbeep(cap->oap);
4167 else if (cap->arg)
4168 beginline(BL_WHITE | BL_FIX);
4169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170}
4171
4172/*
4173 * Cursor down commands.
4174 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4175 */
4176 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004177nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004179 if (mod_mask & MOD_MASK_SHIFT)
4180 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004181 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004182 cap->arg = FORWARD;
4183 nv_page(cap);
4184 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004185#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004186 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004187 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4188 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004190 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
4192#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004193 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004194 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 cmdwin_result = CAR;
4196 else
4197#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004198#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004199 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004200 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4201 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4202 {
4203 invoke_prompt_callback();
4204 if (restart_edit == 0)
4205 restart_edit = 'a';
4206 }
4207 else
4208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 cap->oap->motion_type = MLINE;
4211 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4212 clearopbeep(cap->oap);
4213 else if (cap->arg)
4214 beginline(BL_WHITE | BL_FIX);
4215 }
4216 }
4217}
4218
4219#ifdef FEAT_SEARCHPATH
4220/*
4221 * Grab the file name under the cursor and edit it.
4222 */
4223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004224nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004227 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004229 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
4231 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004232 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 return;
4234 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004235 if (curbuf_locked())
4236 {
4237 clearop(cap->oap);
4238 return;
4239 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004240#ifdef FEAT_PROP_POPUP
4241 if (ERROR_IF_TERM_POPUP_WINDOW)
4242 return;
4243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004245 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
4247 if (ptr != NULL)
4248 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004249 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004250 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004251 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004253 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004254 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004255 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004256 {
4257 curwin->w_cursor.lnum = lnum;
4258 check_cursor_lnum();
4259 beginline(BL_SOL | BL_FIX);
4260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 vim_free(ptr);
4262 }
4263 else
4264 clearop(cap->oap);
4265}
4266#endif
4267
4268/*
4269 * <End> command: to end of current line or last line.
4270 */
4271 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004272nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004274 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004276 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004278 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 nv_dollar(cap);
4281}
4282
4283/*
4284 * Handle the "$" command.
4285 */
4286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004287nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
4289 cap->oap->motion_type = MCHAR;
4290 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004291 // In virtual mode when off the edge of a line and an operator
4292 // is pending (whew!) keep the cursor where it is.
4293 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 if (!virtual_active() || gchar_cursor() != NUL
4295 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004296 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 if (cursor_down((long)(cap->count1 - 1),
4298 cap->oap->op_type == OP_NOP) == FAIL)
4299 clearopbeep(cap->oap);
4300#ifdef FEAT_FOLDING
4301 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4302 foldOpenCursor();
4303#endif
4304}
4305
4306/*
4307 * Implementation of '?' and '/' commands.
4308 * If cap->arg is TRUE don't set PC mark.
4309 */
4310 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004311nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312{
4313 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004314 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4317 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004318 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 cap->cmdchar = 'g';
4320 cap->nchar = '?';
4321 nv_operator(cap);
4322 return;
4323 }
4324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004325 // When using 'incsearch' the cursor may be moved to set a different search
4326 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004327 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 if (cap->searchbuf == NULL)
4330 {
4331 clearop(oap);
4332 return;
4333 }
4334
Bram Moolenaar46539112015-02-17 15:43:57 +01004335 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004336 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004337 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338}
4339
4340/*
4341 * Handle "N" and "n" commands.
4342 * cap->arg is SEARCH_REV for "N", 0 for "n".
4343 */
4344 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004345nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004347 pos_T old = curwin->w_cursor;
4348 int wrapped = FALSE;
4349 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004350
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004351 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004352 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004353 // Avoid getting stuck on the current cursor position, which can
4354 // happen when an offset is given and the cursor is on the last char
4355 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004356 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004357 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004358 cap->count1 -= 1;
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360}
4361
4362/*
4363 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4364 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004365 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004367 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004368normal_search(
4369 cmdarg_T *cap,
4370 int dir,
4371 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004372 int opt, // extra flags for do_search()
4373 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374{
4375 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004376 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378 cap->oap->motion_type = MCHAR;
4379 cap->oap->inclusive = FALSE;
4380 cap->oap->use_reg_one = TRUE;
4381 curwin->w_set_curswant = TRUE;
4382
Bram Moolenaara80faa82020-04-12 19:37:17 +02004383 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004384 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004385 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4386 if (wrapped != NULL)
4387 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 if (i == 0)
4389 clearop(cap->oap);
4390 else
4391 {
4392 if (i == 2)
4393 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#ifdef FEAT_FOLDING
4396 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4397 foldOpenCursor();
4398#endif
4399 }
4400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004401 // "/$" will put the cursor after the end of the line, may need to
4402 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004404 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405}
4406
4407/*
4408 * Character search commands.
4409 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4410 * ',' and FALSE for ';'.
4411 * cap->nchar is NUL for ',' and ';' (repeat the search)
4412 */
4413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004414nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
4416 int t_cmd;
4417
4418 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4419 t_cmd = TRUE;
4420 else
4421 t_cmd = FALSE;
4422
4423 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4425 clearopbeep(cap->oap);
4426 else
4427 {
4428 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004429 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4431 && (t_cmd || cap->oap->op_type != OP_NOP))
4432 {
4433 colnr_T scol, ecol;
4434
4435 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4436 curwin->w_cursor.coladd = ecol - scol;
4437 }
4438 else
4439 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441#ifdef FEAT_FOLDING
4442 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4443 foldOpenCursor();
4444#endif
4445 }
4446}
4447
4448/*
4449 * "[" and "]" commands.
4450 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4451 */
4452 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004453nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004455 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004457 pos_T *pos = NULL; // init for GCC
4458 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 int flag;
4460 long n;
4461 int findc;
4462 int c;
4463
4464 cap->oap->motion_type = MCHAR;
4465 cap->oap->inclusive = FALSE;
4466 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004467 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469#ifdef FEAT_SEARCHPATH
4470 /*
4471 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4472 */
4473 if (cap->nchar == 'f')
4474 nv_gotofile(cap);
4475 else
4476#endif
4477
4478#ifdef FEAT_FIND_ID
4479 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004480 * Find the occurrence(s) of the identifier or define under cursor
4481 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 *
4483 * search list jump
4484 * fwd bwd fwd bwd fwd bwd
4485 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4486 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4487 */
4488 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004489# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004491# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 cap->nchar) != NULL)
4495 {
4496 char_u *ptr;
4497 int len;
4498
4499 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4500 clearop(cap->oap);
4501 else
4502 {
4503 find_pattern_in_path(ptr, 0, len, TRUE,
4504 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4505 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4506 cap->count1,
4507 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4508 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4509 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4510 (linenr_T)MAXLNUM);
4511 curwin->w_set_curswant = TRUE;
4512 }
4513 }
4514 else
4515#endif
4516
4517 /*
4518 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4519 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4520 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4521 * "[m" or "]m" search for prev/next start of (Java) method.
4522 * "[M" or "]M" search for prev/next end of (Java) method.
4523 */
4524 if ( (cap->cmdchar == '['
4525 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4526 || (cap->cmdchar == ']'
4527 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4528 {
4529 if (cap->nchar == '*')
4530 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 prev_pos.lnum = 0;
4532 if (cap->nchar == 'm' || cap->nchar == 'M')
4533 {
4534 if (cap->cmdchar == '[')
4535 findc = '{';
4536 else
4537 findc = '}';
4538 n = 9999;
4539 }
4540 else
4541 {
4542 findc = cap->nchar;
4543 n = cap->count1;
4544 }
4545 for ( ; n > 0; --n)
4546 {
4547 if ((pos = findmatchlimit(cap->oap, findc,
4548 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4549 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004550 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
4552 if (cap->nchar != 'm' && cap->nchar != 'M')
4553 clearopbeep(cap->oap);
4554 }
4555 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004556 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 break;
4558 }
4559 prev_pos = new_pos;
4560 curwin->w_cursor = *pos;
4561 new_pos = *pos;
4562 }
4563 curwin->w_cursor = old_pos;
4564
4565 /*
4566 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4567 * brought us to the match for "[m" and "]M" when inside a method.
4568 * Try finding the '{' or '}' we want to be at.
4569 * Also repeat for the given count.
4570 */
4571 if (cap->nchar == 'm' || cap->nchar == 'M')
4572 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004573 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 int norm = ((findc == '{') == (cap->nchar == 'm'));
4575
4576 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004577 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (prev_pos.lnum != 0)
4579 {
4580 pos = &prev_pos;
4581 curwin->w_cursor = prev_pos;
4582 if (norm)
4583 --n;
4584 }
4585 else
4586 pos = NULL;
4587 while (n > 0)
4588 {
4589 for (;;)
4590 {
4591 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4592 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004593 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (pos == NULL)
4595 clearopbeep(cap->oap);
4596 n = 0;
4597 break;
4598 }
4599 c = gchar_cursor();
4600 if (c == '{' || c == '}')
4601 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004602 // Must have found end/start of class: use it.
4603 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if ((c == findc && norm) || (n == 1 && !norm))
4605 {
4606 new_pos = curwin->w_cursor;
4607 pos = &new_pos;
4608 n = 0;
4609 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004610 // if no match found at all, we started outside of the
4611 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 else if (new_pos.lnum == 0)
4613 {
4614 new_pos = curwin->w_cursor;
4615 pos = &new_pos;
4616 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004617 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 else if ((pos = findmatchlimit(cap->oap, findc,
4619 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4620 0)) == NULL)
4621 n = 0;
4622 else
4623 curwin->w_cursor = *pos;
4624 break;
4625 }
4626 }
4627 --n;
4628 }
4629 curwin->w_cursor = old_pos;
4630 if (pos == NULL && new_pos.lnum != 0)
4631 clearopbeep(cap->oap);
4632 }
4633 if (pos != NULL)
4634 {
4635 setpcmark();
4636 curwin->w_cursor = *pos;
4637 curwin->w_set_curswant = TRUE;
4638#ifdef FEAT_FOLDING
4639 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4640 && cap->oap->op_type == OP_NOP)
4641 foldOpenCursor();
4642#endif
4643 }
4644 }
4645
4646 /*
4647 * "[[", "[]", "]]" and "][": move to start or end of function
4648 */
4649 else if (cap->nchar == '[' || cap->nchar == ']')
4650 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004651 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 flag = '{';
4653 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004654 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
4656 curwin->w_set_curswant = TRUE;
4657 /*
4658 * Imitate strange Vi behaviour: When using "]]" with an operator
4659 * we also stop at '}'.
4660 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004661 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 (cap->oap->op_type != OP_NOP
4663 && cap->arg == FORWARD && flag == '{')))
4664 clearopbeep(cap->oap);
4665 else
4666 {
4667 if (cap->oap->op_type == OP_NOP)
4668 beginline(BL_WHITE | BL_FIX);
4669#ifdef FEAT_FOLDING
4670 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4671 foldOpenCursor();
4672#endif
4673 }
4674 }
4675
4676 /*
4677 * "[p", "[P", "]P" and "]p": put with indent adjustment
4678 */
4679 else if (cap->nchar == 'p' || cap->nchar == 'P')
4680 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004681 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683
4684 /*
4685 * "['", "[`", "]'" and "]`": jump to next mark
4686 */
4687 else if (cap->nchar == '\'' || cap->nchar == '`')
4688 {
4689 pos = &curwin->w_cursor;
4690 for (n = cap->count1; n > 0; --n)
4691 {
4692 prev_pos = *pos;
4693 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4694 cap->nchar == '\'');
4695 if (pos == NULL)
4696 break;
4697 }
4698 if (pos == NULL)
4699 pos = &prev_pos;
4700 nv_cursormark(cap, cap->nchar == '\'', pos);
4701 }
4702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 /*
4704 * [ or ] followed by a middle mouse click: put selected text with
4705 * indent adjustment. Any other button just does as usual.
4706 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004707 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709 (void)do_mouse(cap->oap, cap->nchar,
4710 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4711 cap->count1, PUT_FIXINDENT);
4712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713
4714#ifdef FEAT_FOLDING
4715 /*
4716 * "[z" and "]z": move to start or end of open fold.
4717 */
4718 else if (cap->nchar == 'z')
4719 {
4720 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4721 cap->count1) == FAIL)
4722 clearopbeep(cap->oap);
4723 }
4724#endif
4725
4726#ifdef FEAT_DIFF
4727 /*
4728 * "[c" and "]c": move to next or previous diff-change.
4729 */
4730 else if (cap->nchar == 'c')
4731 {
4732 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4733 cap->count1) == FAIL)
4734 clearopbeep(cap->oap);
4735 }
4736#endif
4737
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004738#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004739 /*
4740 * "[s", "[S", "]s" and "]S": move to next spell error.
4741 */
4742 else if (cap->nchar == 's' || cap->nchar == 'S')
4743 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004744 setpcmark();
4745 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004746 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4747 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004748 {
4749 clearopbeep(cap->oap);
4750 break;
4751 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004752 else
4753 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004754# ifdef FEAT_FOLDING
4755 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4756 foldOpenCursor();
4757# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004758 }
4759#endif
4760
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004761 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 else
4763 clearopbeep(cap->oap);
4764}
4765
4766/*
4767 * Handle Normal mode "%" command.
4768 */
4769 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004770nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
4772 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004773#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 linenr_T lnum = curwin->w_cursor.lnum;
4775#endif
4776
4777 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004778 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
4780 if (cap->count0 > 100)
4781 clearopbeep(cap->oap);
4782 else
4783 {
4784 cap->oap->motion_type = MLINE;
4785 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004786 // Round up, so 'normal 100%' always jumps at the line line.
4787 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4788 // overflow on 32-bits, so use a formula with less accuracy
4789 // to avoid overflows.
4790 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4792 / 100L * cap->count0;
4793 else
4794 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4795 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004796 if (curwin->w_cursor.lnum < 1)
4797 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4799 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4800 beginline(BL_SOL | BL_FIX);
4801 }
4802 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004803 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
4805 cap->oap->motion_type = MCHAR;
4806 cap->oap->use_reg_one = TRUE;
4807 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4808 clearopbeep(cap->oap);
4809 else
4810 {
4811 setpcmark();
4812 curwin->w_cursor = *pos;
4813 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
4817 }
4818#ifdef FEAT_FOLDING
4819 if (cap->oap->op_type == OP_NOP
4820 && lnum != curwin->w_cursor.lnum
4821 && (fdo_flags & FDO_PERCENT)
4822 && KeyTyped)
4823 foldOpenCursor();
4824#endif
4825}
4826
4827/*
4828 * Handle "(" and ")" commands.
4829 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4830 */
4831 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004832nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
4834 cap->oap->motion_type = MCHAR;
4835 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004836 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004837 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 curwin->w_set_curswant = TRUE;
4839
4840 if (findsent(cap->arg, cap->count1) == FAIL)
4841 clearopbeep(cap->oap);
4842 else
4843 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004844 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004845 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#ifdef FEAT_FOLDING
4848 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4849 foldOpenCursor();
4850#endif
4851 }
4852}
4853
4854/*
4855 * "m" command: Mark a position.
4856 */
4857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004858nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
4860 if (!checkclearop(cap->oap))
4861 {
4862 if (setmark(cap->nchar) == FAIL)
4863 clearopbeep(cap->oap);
4864 }
4865}
4866
4867/*
4868 * "{" and "}" commands.
4869 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4870 */
4871 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004872nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873{
4874 cap->oap->motion_type = MCHAR;
4875 cap->oap->inclusive = FALSE;
4876 cap->oap->use_reg_one = TRUE;
4877 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004878 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 clearopbeep(cap->oap);
4880 else
4881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883#ifdef FEAT_FOLDING
4884 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4885 foldOpenCursor();
4886#endif
4887 }
4888}
4889
4890/*
4891 * "u" command: Undo or make lower case.
4892 */
4893 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004894nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004896 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004898 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 cap->cmdchar = 'g';
4900 cap->nchar = 'u';
4901 nv_operator(cap);
4902 }
4903 else
4904 nv_kundo(cap);
4905}
4906
4907/*
4908 * <Undo> command.
4909 */
4910 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004911nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
4913 if (!checkclearopq(cap->oap))
4914 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004915#ifdef FEAT_JOB_CHANNEL
4916 if (bt_prompt(curbuf))
4917 {
4918 clearopbeep(cap->oap);
4919 return;
4920 }
4921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 u_undo((int)cap->count1);
4923 curwin->w_set_curswant = TRUE;
4924 }
4925}
4926
4927/*
4928 * Handle the "r" command.
4929 */
4930 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004931nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932{
4933 char_u *ptr;
4934 int had_ctrl_v;
4935 long n;
4936
4937 if (checkclearop(cap->oap))
4938 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004939#ifdef FEAT_JOB_CHANNEL
4940 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4941 {
4942 clearopbeep(cap->oap);
4943 return;
4944 }
4945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004947 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (cap->nchar == Ctrl_V)
4949 {
4950 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004951 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 if (cap->nchar > DEL)
4954 had_ctrl_v = NUL;
4955 }
4956 else
4957 had_ctrl_v = NUL;
4958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004959 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004960 if (IS_SPECIAL(cap->nchar))
4961 {
4962 clearopbeep(cap->oap);
4963 return;
4964 }
4965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004966 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (VIsual_active)
4968 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004969 if (got_int)
4970 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004971 if (had_ctrl_v)
4972 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004973 // Use a special (negative) number to make a difference between a
4974 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004975 if (cap->nchar == CAR)
4976 cap->nchar = REPLACE_CR_NCHAR;
4977 else if (cap->nchar == NL)
4978 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 nv_operator(cap);
4981 return;
4982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004984 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (virtual_active())
4986 {
4987 if (u_save_cursor() == FAIL)
4988 return;
4989 if (gchar_cursor() == NUL)
4990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004991 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 coladvance_force((colnr_T)(getviscol() + cap->count1));
4993 curwin->w_cursor.col -= cap->count1;
4994 }
4995 else if (gchar_cursor() == TAB)
4996 coladvance_force(getviscol());
4997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004999 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005001 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005002 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 clearopbeep(cap->oap);
5005 return;
5006 }
5007
5008 /*
5009 * Replacing with a TAB is done by edit() when it is complicated because
5010 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5011 * Other characters are done below to avoid problems with things like
5012 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5013 */
5014 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5015 {
5016 stuffnumReadbuff(cap->count1);
5017 stuffcharReadbuff('R');
5018 stuffcharReadbuff('\t');
5019 stuffcharReadbuff(ESC);
5020 return;
5021 }
5022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005023 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (u_save_cursor() == FAIL)
5025 return;
5026
5027 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5028 {
5029 /*
5030 * Replace character(s) by a single newline.
5031 * Strange vi behaviour: Only one newline is inserted.
5032 * Delete the characters here.
5033 * Insert the newline with an insert command, takes care of
5034 * autoindent. The insert command depends on being on the last
5035 * character of a line or not.
5036 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005037 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 stuffcharReadbuff('\r');
5039 stuffcharReadbuff(ESC);
5040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005041 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 invoke_edit(cap, TRUE, 'r', FALSE);
5043 }
5044 else
5045 {
5046 prep_redo(cap->oap->regname, cap->count1,
5047 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5048
5049 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (has_mbyte)
5051 {
5052 int old_State = State;
5053
5054 if (cap->ncharC1 != 0)
5055 AppendCharToRedobuff(cap->ncharC1);
5056 if (cap->ncharC2 != 0)
5057 AppendCharToRedobuff(cap->ncharC2);
5058
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005059 // This is slow, but it handles replacing a single-byte with a
5060 // multi-byte and the other way around. Also handles adding
5061 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 for (n = cap->count1; n > 0; --n)
5063 {
5064 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005065 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5066 {
5067 int c = ins_copychar(curwin->w_cursor.lnum
5068 + (cap->nchar == Ctrl_Y ? -1 : 1));
5069 if (c != NUL)
5070 ins_char(c);
5071 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005072 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005073 ++curwin->w_cursor.col;
5074 }
5075 else
5076 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 State = old_State;
5078 if (cap->ncharC1 != 0)
5079 ins_char(cap->ncharC1);
5080 if (cap->ncharC2 != 0)
5081 ins_char(cap->ncharC2);
5082 }
5083 }
5084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
5086 /*
5087 * Replace the characters within one line.
5088 */
5089 for (n = cap->count1; n > 0; --n)
5090 {
5091 /*
5092 * Get ptr again, because u_save and/or showmatch() will have
5093 * released the line. At the same time we let know that the
5094 * line will be changed.
5095 */
5096 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005097 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5098 {
5099 int c = ins_copychar(curwin->w_cursor.lnum
5100 + (cap->nchar == Ctrl_Y ? -1 : 1));
5101 if (c != NUL)
5102 ptr[curwin->w_cursor.col] = c;
5103 }
5104 else
5105 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (p_sm && msg_silent == 0)
5107 showmatch(cap->nchar);
5108 ++curwin->w_cursor.col;
5109 }
5110#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005111 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005113 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
Bram Moolenaar009b2592004-10-24 19:18:58 +00005115 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005116 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005118 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120#endif
5121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005122 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 changed_bytes(curwin->w_cursor.lnum,
5124 (colnr_T)(curwin->w_cursor.col - cap->count1));
5125 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005126 --curwin->w_cursor.col; // cursor on the last replaced char
5127 // if the character on the left of the current cursor is a multi-byte
5128 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (has_mbyte)
5130 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 curbuf->b_op_end = curwin->w_cursor;
5132 curwin->w_set_curswant = TRUE;
5133 set_last_insert(cap->nchar);
5134 }
5135}
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137/*
5138 * 'o': Exchange start and end of Visual area.
5139 * 'O': same, but in block mode exchange left and right corners.
5140 */
5141 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005142v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143{
5144 pos_T old_cursor;
5145 colnr_T left, right;
5146
5147 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5148 {
5149 old_cursor = curwin->w_cursor;
5150 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5151 curwin->w_cursor.lnum = VIsual.lnum;
5152 coladvance(left);
5153 VIsual = curwin->w_cursor;
5154
5155 curwin->w_cursor.lnum = old_cursor.lnum;
5156 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005157 // 'selection "exclusive" and cursor at right-bottom corner: move it
5158 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5160 ++curwin->w_curswant;
5161 coladvance(curwin->w_curswant);
5162 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005164 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 curwin->w_cursor.lnum = VIsual.lnum;
5167 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5168 ++right;
5169 coladvance(right);
5170 VIsual = curwin->w_cursor;
5171
5172 curwin->w_cursor.lnum = old_cursor.lnum;
5173 coladvance(left);
5174 curwin->w_curswant = left;
5175 }
5176 }
5177 else
5178 {
5179 old_cursor = curwin->w_cursor;
5180 curwin->w_cursor = VIsual;
5181 VIsual = old_cursor;
5182 curwin->w_set_curswant = TRUE;
5183 }
5184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186/*
5187 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5188 */
5189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005190nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005192 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
5194 cap->cmdchar = 'c';
5195 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005196 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 VIsual_mode = 'V';
5198 nv_operator(cap);
5199 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005200 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
5202 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005203 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 else
5205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (virtual_active())
5207 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5209 }
5210 }
5211}
5212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213/*
5214 * "gr".
5215 */
5216 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005217nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (VIsual_active)
5220 {
5221 cap->cmdchar = 'r';
5222 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005223 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005225 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005228 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 else
5230 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005231 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005232 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 stuffcharReadbuff(cap->extra_char);
5234 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (virtual_active())
5236 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 invoke_edit(cap, TRUE, 'v', FALSE);
5238 }
5239 }
5240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
5242/*
5243 * Swap case for "~" command, when it does not work like an operator.
5244 */
5245 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005246n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247{
5248 long n;
5249 pos_T startpos;
5250 int did_change = 0;
5251#ifdef FEAT_NETBEANS_INTG
5252 pos_T pos;
5253 char_u *ptr;
5254 int count;
5255#endif
5256
5257 if (checkclearopq(cap->oap))
5258 return;
5259
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005260 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
5262 clearopbeep(cap->oap);
5263 return;
5264 }
5265
5266 prep_redo_cmd(cap);
5267
5268 if (u_save_cursor() == FAIL)
5269 return;
5270
5271 startpos = curwin->w_cursor;
5272#ifdef FEAT_NETBEANS_INTG
5273 pos = startpos;
5274#endif
5275 for (n = cap->count1; n > 0; --n)
5276 {
5277 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5278 inc_cursor();
5279 if (gchar_cursor() == NUL)
5280 {
5281 if (vim_strchr(p_ww, '~') != NULL
5282 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5283 {
5284#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005285 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
5287 if (did_change)
5288 {
5289 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005290 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005291 netbeans_removed(curbuf, pos.lnum, pos.col,
5292 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005294 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296 pos.col = 0;
5297 pos.lnum++;
5298 }
5299#endif
5300 ++curwin->w_cursor.lnum;
5301 curwin->w_cursor.col = 0;
5302 if (n > 1)
5303 {
5304 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5305 break;
5306 u_clearline();
5307 }
5308 }
5309 else
5310 break;
5311 }
5312 }
5313#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005314 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 {
5316 ptr = ml_get(pos.lnum);
5317 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005318 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5319 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321#endif
5322
5323
5324 check_cursor();
5325 curwin->w_set_curswant = TRUE;
5326 if (did_change)
5327 {
5328 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5329 0L);
5330 curbuf->b_op_start = startpos;
5331 curbuf->b_op_end = curwin->w_cursor;
5332 if (curbuf->b_op_end.col > 0)
5333 --curbuf->b_op_end.col;
5334 }
5335}
5336
5337/*
5338 * Move cursor to mark.
5339 */
5340 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005341nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
5343 if (check_mark(pos) == FAIL)
5344 clearop(cap->oap);
5345 else
5346 {
5347 if (cap->cmdchar == '\''
5348 || cap->cmdchar == '`'
5349 || cap->cmdchar == '['
5350 || cap->cmdchar == ']')
5351 setpcmark();
5352 curwin->w_cursor = *pos;
5353 if (flag)
5354 beginline(BL_WHITE | BL_FIX);
5355 else
5356 check_cursor();
5357 }
5358 cap->oap->motion_type = flag ? MLINE : MCHAR;
5359 if (cap->cmdchar == '`')
5360 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005361 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 curwin->w_set_curswant = TRUE;
5363}
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365/*
5366 * Handle commands that are operators in Visual mode.
5367 */
5368 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005369v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
5371 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5372
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005373 // Uppercase means linewise, except in block mode, then "D" deletes till
5374 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (isupper(cap->cmdchar))
5376 {
5377 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005378 {
5379 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5383 curwin->w_curswant = MAXCOL;
5384 }
5385 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5386 nv_operator(cap);
5387}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
5389/*
5390 * "s" and "S" commands.
5391 */
5392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005393nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005395#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005396 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005397 if (term_swap_diff() == OK)
5398 return;
5399#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005400#ifdef FEAT_JOB_CHANNEL
5401 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5402 {
5403 clearopbeep(cap->oap);
5404 return;
5405 }
5406#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005407 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005410 {
5411 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 cap->cmdchar = 'c';
5415 nv_operator(cap);
5416 }
5417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 nv_optrans(cap);
5419}
5420
5421/*
5422 * Abbreviated commands.
5423 */
5424 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005425nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
5427 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005428 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005430 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 if (VIsual_active)
5432 v_visop(cap);
5433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 nv_optrans(cap);
5435}
5436
5437/*
5438 * Translate a command into another command.
5439 */
5440 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005441nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5444 (char_u *)"d$", (char_u *)"c$",
5445 (char_u *)"cl", (char_u *)"cc",
5446 (char_u *)"yy", (char_u *)":s\r"};
5447 static char_u *str = (char_u *)"xXDCsSY&";
5448
5449 if (!checkclearopq(cap->oap))
5450 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005451 // In Vi "2D" doesn't delete the next line. Can't translate it
5452 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005453 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5454 {
5455 cap->oap->start = curwin->w_cursor;
5456 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005457#ifdef FEAT_EVAL
5458 set_op_var(OP_DELETE);
5459#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005460 cap->count1 = 1;
5461 nv_dollar(cap);
5462 finish_op = TRUE;
5463 ResetRedobuff();
5464 AppendCharToRedobuff('D');
5465 }
5466 else
5467 {
5468 if (cap->count0)
5469 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005470 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473 cap->opcount = 0;
5474}
5475
5476/*
5477 * "'" and "`" commands. Also for "g'" and "g`".
5478 * cap->arg is TRUE for "'" and "g'".
5479 */
5480 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005481nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483 pos_T *pos;
5484 int c;
5485#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005486 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005487 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488#endif
5489
5490 if (cap->cmdchar == 'g')
5491 c = cap->extra_char;
5492 else
5493 c = cap->nchar;
5494 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005495 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
5497 if (cap->arg)
5498 {
5499 check_cursor_lnum();
5500 beginline(BL_WHITE | BL_FIX);
5501 }
5502 else
5503 check_cursor();
5504 }
5505 else
5506 nv_cursormark(cap, cap->arg, pos);
5507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005508 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (!virtual_active())
5510 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005511 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512#ifdef FEAT_FOLDING
5513 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005514 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005515 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 && (fdo_flags & FDO_MARK)
5517 && old_KeyTyped)
5518 foldOpenCursor();
5519#endif
5520}
5521
5522/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005523 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 */
5525 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005526nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528#ifdef FEAT_JUMPLIST
5529 pos_T *pos;
5530# ifdef FEAT_FOLDING
5531 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005532 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533# endif
5534
5535 if (!checkclearopq(cap->oap))
5536 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005537 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5538 {
5539 if (goto_tabpage_lastused() == FAIL)
5540 clearopbeep(cap->oap);
5541 return;
5542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 if (cap->cmdchar == 'g')
5544 pos = movechangelist((int)cap->count1);
5545 else
5546 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005547 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 {
5549 curwin->w_set_curswant = TRUE;
5550 check_cursor();
5551 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005552 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 nv_cursormark(cap, FALSE, pos);
5554 else if (cap->cmdchar == 'g')
5555 {
5556 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005557 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005559 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005561 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 }
5563 else
5564 clearopbeep(cap->oap);
5565# ifdef FEAT_FOLDING
5566 if (cap->oap->op_type == OP_NOP
5567 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5568 && (fdo_flags & FDO_MARK)
5569 && old_KeyTyped)
5570 foldOpenCursor();
5571# endif
5572 }
5573#else
5574 clearopbeep(cap->oap);
5575#endif
5576}
5577
5578/*
5579 * Handle '"' command.
5580 */
5581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005582nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 if (checkclearop(cap->oap))
5585 return;
5586#ifdef FEAT_EVAL
5587 if (cap->nchar == '=')
5588 cap->nchar = get_expr_register();
5589#endif
5590 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5591 {
5592 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005593 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594#ifdef FEAT_EVAL
5595 set_reg_var(cap->oap->regname);
5596#endif
5597 }
5598 else
5599 clearopbeep(cap->oap);
5600}
5601
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602/*
5603 * Handle "v", "V" and "CTRL-V" commands.
5604 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5605 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005606 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 */
5608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005609nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005611 if (cap->cmdchar == Ctrl_Q)
5612 cap->cmdchar = Ctrl_V;
5613
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005614 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5615 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 if (cap->oap->op_type != OP_NOP)
5617 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005618 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005619 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 return;
5621 }
5622
5623 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005624 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005626 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005628 else // toggle char/block mode
5629 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 VIsual_mode = cap->cmdchar;
5631 showmode();
5632 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005633 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005635 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
5637 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005638 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005640 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 VIsual = curwin->w_cursor;
5642
5643 VIsual_active = TRUE;
5644 VIsual_reselect = TRUE;
5645 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005646 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005649 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005650 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 /*
5652 * For V and ^V, we multiply the number of lines even if there
5653 * was only one -- webb
5654 */
5655 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5656 {
5657 curwin->w_cursor.lnum +=
5658 resel_VIsual_line_count * cap->count0 - 1;
5659 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5660 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5661 }
5662 VIsual_mode = resel_VIsual_mode;
5663 if (VIsual_mode == 'v')
5664 {
5665 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005666 {
5667 validate_virtcol();
5668 curwin->w_curswant = curwin->w_virtcol
5669 + resel_VIsual_vcol * cap->count0 - 1;
5670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005672 curwin->w_curswant = resel_VIsual_vcol;
5673 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005675 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
5677 curwin->w_curswant = MAXCOL;
5678 coladvance((colnr_T)MAXCOL);
5679 }
5680 else if (VIsual_mode == Ctrl_V)
5681 {
5682 validate_virtcol();
5683 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005684 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 coladvance(curwin->w_curswant);
5686 }
5687 else
5688 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005689 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 else
5692 {
5693 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005694 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 may_start_select('c');
5696 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005697 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005698 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005699 if (cap->count0 > 0 && --cap->count1 > 0)
5700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005701 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005702 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5703 nv_right(cap);
5704 else if (VIsual_mode == 'V')
5705 nv_down(cap);
5706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708 }
5709}
5710
5711/*
5712 * Start selection for Shift-movement keys.
5713 */
5714 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005715start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005717 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 may_start_select('k');
5719 n_start_visual_mode('v');
5720}
5721
5722/*
5723 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5724 */
5725 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005726may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 VIsual_select = (stuff_empty() && typebuf_typed()
5729 && (vim_strchr(p_slm, c) != NULL));
5730}
5731
5732/*
5733 * Start Visual mode "c".
5734 * Should set VIsual_select before calling this.
5735 */
5736 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005737n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005739#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005740 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005741 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005742#endif
5743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 VIsual_mode = c;
5745 VIsual_active = TRUE;
5746 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005747
5748 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005749 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005751 {
5752 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 VIsual = curwin->w_cursor;
5756
5757#ifdef FEAT_FOLDING
5758 foldAdjustVisual();
5759#endif
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005762#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005763 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005764 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005765#endif
5766
Bram Moolenaar09df3122006-01-23 22:23:09 +00005767 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005768 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005770 // Make sure the clipboard gets updated. Needed because start and
5771 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 clip_star.vmode = NUL;
5773#endif
5774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005775 // Only need to redraw this line, unless still need to redraw an old
5776 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 if (curwin->w_redr_type < INVERTED)
5778 {
5779 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5780 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5781 }
5782}
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785/*
5786 * CTRL-W: Window commands
5787 */
5788 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005789nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005791 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005793 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005794 cap->cmdchar = ':';
5795 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005796 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005797 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005798 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005799 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800}
5801
5802/*
5803 * CTRL-Z: Suspend
5804 */
5805 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005806nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
5808 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005810 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005811 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812}
5813
5814/*
5815 * Commands starting with "g".
5816 */
5817 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005818nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
5820 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 int i;
5823 int flag = FALSE;
5824
5825 switch (cap->nchar)
5826 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005827 case Ctrl_A:
5828 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829#ifdef MEM_PROFILE
5830 /*
5831 * "g^A": dump log of used memory.
5832 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005833 if (!VIsual_active && cap->nchar == Ctrl_A)
5834 vim_mem_profile_dump();
5835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005837 /*
5838 * "g^A/g^X": sequentially increment visually selected region
5839 */
5840 if (VIsual_active)
5841 {
5842 cap->arg = TRUE;
5843 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005844 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005845 nv_addsub(cap);
5846 }
5847 else
5848 clearopbeep(oap);
5849 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 /*
5852 * "gR": Enter virtual replace mode.
5853 */
5854 case 'R':
5855 cap->arg = TRUE;
5856 nv_Replace(cap);
5857 break;
5858
5859 case 'r':
5860 nv_vreplace(cap);
5861 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863 case '&':
5864 do_cmdline_cmd((char_u *)"%s//~/&");
5865 break;
5866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 /*
5868 * "gv": Reselect the previous Visual area. If Visual already active,
5869 * exchange previous and current Visual area.
5870 */
5871 case 'v':
5872 if (checkclearop(oap))
5873 break;
5874
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005875 if ( curbuf->b_visual.vi_start.lnum == 0
5876 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5877 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 beep_flush();
5879 else
5880 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005881 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 if (VIsual_active)
5883 {
5884 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005885 VIsual_mode = curbuf->b_visual.vi_mode;
5886 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887# ifdef FEAT_EVAL
5888 curbuf->b_visual_mode_eval = i;
5889# endif
5890 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005891 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5892 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005894 tpos = curbuf->b_visual.vi_end;
5895 curbuf->b_visual.vi_end = curwin->w_cursor;
5896 curwin->w_cursor = curbuf->b_visual.vi_start;
5897 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 }
5899 else
5900 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005901 VIsual_mode = curbuf->b_visual.vi_mode;
5902 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5903 tpos = curbuf->b_visual.vi_end;
5904 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 }
5906
5907 VIsual_active = TRUE;
5908 VIsual_reselect = TRUE;
5909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005910 // Set Visual to the start and w_cursor to the end of the Visual
5911 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 check_cursor();
5913 VIsual = curwin->w_cursor;
5914 curwin->w_cursor = tpos;
5915 check_cursor();
5916 update_topline();
5917 /*
5918 * When called from normal "g" command: start Select mode when
5919 * 'selectmode' contains "cmd". When called for K_SELECT, always
5920 * start Select mode.
5921 */
5922 if (cap->arg)
5923 VIsual_select = TRUE;
5924 else
5925 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005928 // Make sure the clipboard gets updated. Needed because start and
5929 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 clip_star.vmode = NUL;
5931#endif
5932 redraw_curbuf_later(INVERTED);
5933 showmode();
5934 }
5935 break;
5936 /*
5937 * "gV": Don't reselect the previous Visual area after a Select mode
5938 * mapping of menu.
5939 */
5940 case 'V':
5941 VIsual_reselect = FALSE;
5942 break;
5943
5944 /*
5945 * "gh": start Select mode.
5946 * "gH": start Select line mode.
5947 * "g^H": start Select block mode.
5948 */
5949 case K_BS:
5950 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005951 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 case 'h':
5953 case 'H':
5954 case Ctrl_H:
5955# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005956 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (cap->nchar == Ctrl_H)
5958 cap->cmdchar = Ctrl_V;
5959 else
5960# endif
5961 cap->cmdchar = cap->nchar + ('v' - 'h');
5962 cap->arg = TRUE;
5963 nv_visual(cap);
5964 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005966 // "gn", "gN" visually select next/previous search match
5967 // "gn" selects next match
5968 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005969 case 'N':
5970 case 'n':
5971 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005972 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005973 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974
5975 /*
5976 * "gj" and "gk" two new funny movement keys -- up and down
5977 * movement based on *screen* line rather than *file* line.
5978 */
5979 case 'j':
5980 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005981 // with 'nowrap' it works just like the normal "j" command.
5982 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 {
5984 oap->motion_type = MLINE;
5985 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5986 }
5987 else
5988 i = nv_screengo(oap, FORWARD, cap->count1);
5989 if (i == FAIL)
5990 clearopbeep(oap);
5991 break;
5992
5993 case 'k':
5994 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005995 // with 'nowrap' it works just like the normal "k" command.
5996 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 {
5998 oap->motion_type = MLINE;
5999 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6000 }
6001 else
6002 i = nv_screengo(oap, BACKWARD, cap->count1);
6003 if (i == FAIL)
6004 clearopbeep(oap);
6005 break;
6006
6007 /*
6008 * "gJ": join two lines without inserting a space.
6009 */
6010 case 'J':
6011 nv_join(cap);
6012 break;
6013
6014 /*
6015 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6016 * "gm": middle of "g0" and "g$".
6017 */
6018 case '^':
6019 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006020 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
6022 case '0':
6023 case 'm':
6024 case K_HOME:
6025 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 oap->motion_type = MCHAR;
6027 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006028 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006030 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 int width2 = width1 + curwin_col_off2();
6032
6033 validate_virtcol();
6034 i = 0;
6035 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6036 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6037 }
6038 else
6039 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006040 // Go to the middle of the screen line. When 'number' or
6041 // 'relativenumber' is on and lines are wrapping the middle can be more
6042 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006044 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 + ((curwin->w_p_wrap && i > 0)
6046 ? curwin_col_off2() : 0)) / 2;
6047 coladvance((colnr_T)i);
6048 if (flag)
6049 {
6050 do
6051 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006052 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006053 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 }
6055 curwin->w_set_curswant = TRUE;
6056 break;
6057
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006058 case 'M':
6059 {
6060 char_u *ptr = ml_get_curline();
6061
6062 oap->motion_type = MCHAR;
6063 oap->inclusive = FALSE;
6064 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006065 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006066 else
6067 i = (int)STRLEN(ptr);
6068 if (cap->count0 > 0 && cap->count0 <= 100)
6069 coladvance((colnr_T)(i * cap->count0 / 100));
6070 else
6071 coladvance((colnr_T)(i / 2));
6072 curwin->w_set_curswant = TRUE;
6073 }
6074 break;
6075
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006077 // "g_": to the last non-blank character in the line or <count> lines
6078 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 cap->oap->motion_type = MCHAR;
6080 cap->oap->inclusive = TRUE;
6081 curwin->w_curswant = MAXCOL;
6082 if (cursor_down((long)(cap->count1 - 1),
6083 cap->oap->op_type == OP_NOP) == FAIL)
6084 clearopbeep(cap->oap);
6085 else
6086 {
6087 char_u *ptr = ml_get_curline();
6088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006089 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6091 --curwin->w_cursor.col;
6092
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006093 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006095 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 --curwin->w_cursor.col;
6097 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006098 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 }
6100 break;
6101
6102 case '$':
6103 case K_END:
6104 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 {
6106 int col_off = curwin_col_off();
6107
6108 oap->motion_type = MCHAR;
6109 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006110 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006112 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (cap->count1 == 1)
6114 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006115 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 int width2 = width1 + curwin_col_off2();
6117
6118 validate_virtcol();
6119 i = width1 - 1;
6120 if (curwin->w_virtcol >= (colnr_T)width1)
6121 i += ((curwin->w_virtcol - width1) / width2 + 1)
6122 * width2;
6123 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006124
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006125 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006126 validate_virtcol();
6127 curwin->w_curswant = curwin->w_virtcol;
6128 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6130 {
6131 /*
6132 * Check for landing on a character that got split at
6133 * the end of the line. We do not want to advance to
6134 * the next screen line.
6135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (curwin->w_virtcol > (colnr_T)i)
6137 --curwin->w_cursor.col;
6138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 }
6140 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6141 clearopbeep(oap);
6142 }
6143 else
6144 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006145 if (cap->count1 > 1)
6146 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006147 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006148
Bram Moolenaar02631462017-09-22 15:20:32 +02006149 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006151
Bram Moolenaar74ede802021-05-29 19:18:01 +02006152 // if the character doesn't fit move one back
6153 if (curwin->w_cursor.col > 0
6154 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6155 {
6156 colnr_T vcol;
6157
6158 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6159 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6160 --curwin->w_cursor.col;
6161 }
6162
Bram Moolenaard5c82342019-07-27 18:44:57 +02006163 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006164 validate_virtcol();
6165 curwin->w_curswant = curwin->w_virtcol;
6166 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 }
6168 }
6169 break;
6170
6171 /*
6172 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6173 */
6174 case '*':
6175 case '#':
6176#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006177 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006179 case Ctrl_RSB: // :tag or :tselect for current identifier
6180 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 nv_ident(cap);
6182 break;
6183
6184 /*
6185 * ge and gE: go back to end of word
6186 */
6187 case 'e':
6188 case 'E':
6189 oap->motion_type = MCHAR;
6190 curwin->w_set_curswant = TRUE;
6191 oap->inclusive = TRUE;
6192 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6193 clearopbeep(oap);
6194 break;
6195
6196 /*
6197 * "g CTRL-G": display info about cursor position
6198 */
6199 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006200 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 break;
6202
6203 /*
6204 * "gi": start Insert at the last position.
6205 */
6206 case 'i':
6207 if (curbuf->b_last_insert.lnum != 0)
6208 {
6209 curwin->w_cursor = curbuf->b_last_insert;
6210 check_cursor_lnum();
6211 i = (int)STRLEN(ml_get_curline());
6212 if (curwin->w_cursor.col > (colnr_T)i)
6213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 if (virtual_active())
6215 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 curwin->w_cursor.col = i;
6217 }
6218 }
6219 cap->cmdchar = 'i';
6220 nv_edit(cap);
6221 break;
6222
6223 /*
6224 * "gI": Start insert in column 1.
6225 */
6226 case 'I':
6227 beginline(0);
6228 if (!checkclearopq(oap))
6229 invoke_edit(cap, FALSE, 'g', FALSE);
6230 break;
6231
6232#ifdef FEAT_SEARCHPATH
6233 /*
6234 * "gf": goto file, edit file under cursor
6235 * "]f" and "[f": can also be used.
6236 */
6237 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006238 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 nv_gotofile(cap);
6240 break;
6241#endif
6242
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006243 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 case '\'':
6245 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006246 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 case '`':
6248 nv_gomark(cap);
6249 break;
6250
6251 /*
6252 * "gs": Goto sleep.
6253 */
6254 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006255 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 break;
6257
6258 /*
6259 * "ga": Display the ascii value of the character under the
6260 * cursor. It is displayed in decimal, hex, and octal. -- webb
6261 */
6262 case 'a':
6263 do_ascii(NULL);
6264 break;
6265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 /*
6267 * "g8": Display the bytes used for the UTF-8 character under the
6268 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006269 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 */
6271 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006272 if (cap->count0 == 8)
6273 utf_find_illegal();
6274 else
6275 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006278 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006279 case '<':
6280 show_sb_text();
6281 break;
6282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 /*
6284 * "gg": Goto the first line in file. With a count it goes to
6285 * that line number like for "G". -- webb
6286 */
6287 case 'g':
6288 cap->arg = FALSE;
6289 nv_goto(cap);
6290 break;
6291
6292 /*
6293 * Two-character operators:
6294 * "gq" Format text
6295 * "gw" Format text and keep cursor position
6296 * "g~" Toggle the case of the text.
6297 * "gu" Change text to lower case.
6298 * "gU" Change text to upper case.
6299 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006300 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 */
6302 case 'q':
6303 case 'w':
6304 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006305 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 case '~':
6307 case 'u':
6308 case 'U':
6309 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006310 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 nv_operator(cap);
6312 break;
6313
6314 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006315 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 * current function
6317 * "gD": idem, but in the current file.
6318 */
6319 case 'd':
6320 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006321 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 break;
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 /*
6325 * g<*Mouse> : <C-*mouse>
6326 */
6327 case K_MIDDLEMOUSE:
6328 case K_MIDDLEDRAG:
6329 case K_MIDDLERELEASE:
6330 case K_LEFTMOUSE:
6331 case K_LEFTDRAG:
6332 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006333 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 case K_RIGHTMOUSE:
6335 case K_RIGHTDRAG:
6336 case K_RIGHTRELEASE:
6337 case K_X1MOUSE:
6338 case K_X1DRAG:
6339 case K_X1RELEASE:
6340 case K_X2MOUSE:
6341 case K_X2DRAG:
6342 case K_X2RELEASE:
6343 mod_mask = MOD_MASK_CTRL;
6344 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6345 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
6347 case K_IGNORE:
6348 break;
6349
6350 /*
6351 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6352 */
6353 case 'p':
6354 case 'P':
6355 nv_put(cap);
6356 break;
6357
6358#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006359 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 case 'o':
6361 goto_byte(cap->count0);
6362 break;
6363#endif
6364
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006365 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006367 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 {
6369 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006370 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 break;
6372 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006373
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if (!checkclearopq(oap))
6375 do_exmode(TRUE);
6376 break;
6377
6378#ifdef FEAT_JUMPLIST
6379 case ',':
6380 nv_pcmark(cap);
6381 break;
6382
6383 case ';':
6384 cap->count1 = -cap->count1;
6385 nv_pcmark(cap);
6386 break;
6387#endif
6388
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006389 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006390 if (!checkclearop(oap))
6391 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006392 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006393 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006394 if (!checkclearop(oap))
6395 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006396 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006397
Bram Moolenaar62a23252020-08-09 14:04:42 +02006398 case TAB:
6399 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6400 clearopbeep(oap);
6401 break;
6402
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006403 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006404 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006405 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006406 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006407 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006408 break;
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 default:
6411 clearopbeep(oap);
6412 break;
6413 }
6414}
6415
6416/*
6417 * Handle "o" and "O" commands.
6418 */
6419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006420n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006422#ifdef FEAT_CONCEAL
6423 linenr_T oldline = curwin->w_cursor.lnum;
6424#endif
6425
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (!checkclearopq(cap->oap))
6427 {
6428#ifdef FEAT_FOLDING
6429 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006430 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 (void)hasFolding(curwin->w_cursor.lnum,
6432 &curwin->w_cursor.lnum, NULL);
6433 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006434 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 (void)hasFolding(curwin->w_cursor.lnum,
6436 NULL, &curwin->w_cursor.lnum);
6437#endif
6438 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6439 (cap->cmdchar == 'O' ? 1 : 0)),
6440 (linenr_T)(curwin->w_cursor.lnum +
6441 (cap->cmdchar == 'o' ? 1 : 0))
6442 ) == OK
6443 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006444 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6445 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006447#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006448 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006449 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006450#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006451#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006452 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006453 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006454 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006455#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006456 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006457 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6458 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6460 }
6461 }
6462}
6463
6464/*
6465 * "." command: redo last change.
6466 */
6467 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006468nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 if (!checkclearopq(cap->oap))
6471 {
6472 /*
6473 * If "restart_edit" is TRUE, the last but one command is repeated
6474 * instead of the last command (inserting text). This is used for
6475 * CTRL-O <.> in insert mode.
6476 */
6477 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6478 clearopbeep(cap->oap);
6479 }
6480}
6481
6482/*
6483 * CTRL-R: undo undo
6484 */
6485 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006486nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
6488 if (!checkclearopq(cap->oap))
6489 {
6490 u_redo((int)cap->count1);
6491 curwin->w_set_curswant = TRUE;
6492 }
6493}
6494
6495/*
6496 * Handle "U" command.
6497 */
6498 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006499nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006501 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006502 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006504 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 cap->cmdchar = 'g';
6506 cap->nchar = 'U';
6507 nv_operator(cap);
6508 }
6509 else if (!checkclearopq(cap->oap))
6510 {
6511 u_undoline();
6512 curwin->w_set_curswant = TRUE;
6513 }
6514}
6515
6516/*
6517 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6518 * single character.
6519 */
6520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006521nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006523 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006524 {
6525#ifdef FEAT_JOB_CHANNEL
6526 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6527 {
6528 clearopbeep(cap->oap);
6529 return;
6530 }
6531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 else
6535 nv_operator(cap);
6536}
6537
6538/*
6539 * Handle an operator command.
6540 * The actual work is done by do_pending_operator().
6541 */
6542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006543nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
6545 int op_type;
6546
6547 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006548#ifdef FEAT_JOB_CHANNEL
6549 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6550 {
6551 clearopbeep(cap->oap);
6552 return;
6553 }
6554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006556 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 nv_lineop(cap);
6558 else if (!checkclearop(cap->oap))
6559 {
6560 cap->oap->start = curwin->w_cursor;
6561 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006562#ifdef FEAT_EVAL
6563 set_op_var(op_type);
6564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 }
6566}
6567
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006568#ifdef FEAT_EVAL
6569/*
6570 * Set v:operator to the characters for "optype".
6571 */
6572 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006573set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006574{
6575 char_u opchars[3];
6576
6577 if (optype == OP_NOP)
6578 set_vim_var_string(VV_OP, NULL, 0);
6579 else
6580 {
6581 opchars[0] = get_op_char(optype);
6582 opchars[1] = get_extra_op_char(optype);
6583 opchars[2] = NUL;
6584 set_vim_var_string(VV_OP, opchars, -1);
6585 }
6586}
6587#endif
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589/*
6590 * Handle linewise operator "dd", "yy", etc.
6591 *
6592 * "_" is is a strange motion command that helps make operators more logical.
6593 * It is actually implemented, but not documented in the real Vi. This motion
6594 * command actually refers to "the current line". Commands like "dd" and "yy"
6595 * are really an alternate form of "d_" and "y_". It does accept a count, so
6596 * "d3_" works to delete 3 lines.
6597 */
6598 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006599nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
6601 cap->oap->motion_type = MLINE;
6602 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6603 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006604 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006605 && cap->oap->motion_force != 'v'
6606 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 || cap->oap->op_type == OP_LSHIFT
6608 || cap->oap->op_type == OP_RSHIFT)
6609 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006610 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 beginline(BL_WHITE | BL_FIX);
6612}
6613
6614/*
6615 * <Home> command.
6616 */
6617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006618nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006620 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006621 if (mod_mask & MOD_MASK_CTRL)
6622 nv_goto(cap);
6623 else
6624 {
6625 cap->count0 = 1;
6626 nv_pipe(cap);
6627 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006628 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6629 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630}
6631
6632/*
6633 * "|" command.
6634 */
6635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006636nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 cap->oap->motion_type = MCHAR;
6639 cap->oap->inclusive = FALSE;
6640 beginline(0);
6641 if (cap->count0 > 0)
6642 {
6643 coladvance((colnr_T)(cap->count0 - 1));
6644 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6645 }
6646 else
6647 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006648 // keep curswant at the column where we wanted to go, not where
6649 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 curwin->w_set_curswant = FALSE;
6651}
6652
6653/*
6654 * Handle back-word command "b" and "B".
6655 * cap->arg is 1 for "B"
6656 */
6657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006658nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 cap->oap->motion_type = MCHAR;
6661 cap->oap->inclusive = FALSE;
6662 curwin->w_set_curswant = TRUE;
6663 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6664 clearopbeep(cap->oap);
6665#ifdef FEAT_FOLDING
6666 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6667 foldOpenCursor();
6668#endif
6669}
6670
6671/*
6672 * Handle word motion commands "e", "E", "w" and "W".
6673 * cap->arg is TRUE for "E" and "W".
6674 */
6675 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006676nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 int n;
6679 int word_end;
6680 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006681 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683 /*
6684 * Set inclusive for the "E" and "e" command.
6685 */
6686 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6687 word_end = TRUE;
6688 else
6689 word_end = FALSE;
6690 cap->oap->inclusive = word_end;
6691
6692 /*
6693 * "cw" and "cW" are a special case.
6694 */
6695 if (!word_end && cap->oap->op_type == OP_CHANGE)
6696 {
6697 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006698 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006700 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 {
6702 /*
6703 * Reproduce a funny Vi behaviour: "cw" on a blank only
6704 * changes one character, not all blanks until the start of
6705 * the next word. Only do this when the 'w' flag is included
6706 * in 'cpoptions'.
6707 */
6708 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6709 {
6710 cap->oap->inclusive = TRUE;
6711 cap->oap->motion_type = MCHAR;
6712 return;
6713 }
6714 }
6715 else
6716 {
6717 /*
6718 * This is a little strange. To match what the real Vi does,
6719 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6720 * that we are not on a space or a TAB. This seems impolite
6721 * at first, but it's really more what we mean when we say
6722 * 'cw'.
6723 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006724 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 * will change only one character! This is done by setting
6726 * flag.
6727 */
6728 cap->oap->inclusive = TRUE;
6729 word_end = TRUE;
6730 flag = TRUE;
6731 }
6732 }
6733 }
6734
6735 cap->oap->motion_type = MCHAR;
6736 curwin->w_set_curswant = TRUE;
6737 if (word_end)
6738 n = end_word(cap->count1, cap->arg, flag, FALSE);
6739 else
6740 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6741
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006742 // Don't leave the cursor on the NUL past the end of line. Unless we
6743 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006744 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006745 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746
6747 if (n == FAIL && cap->oap->op_type == OP_NOP)
6748 clearopbeep(cap->oap);
6749 else
6750 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752#ifdef FEAT_FOLDING
6753 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6754 foldOpenCursor();
6755#endif
6756 }
6757}
6758
6759/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006760 * Used after a movement command: If the cursor ends up on the NUL after the
6761 * end of the line, may move it back to the last character and make the motion
6762 * inclusive.
6763 */
6764 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006765adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006766{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006767 // The cursor cannot remain on the NUL when:
6768 // - the column is > 0
6769 // - not in Visual mode or 'selection' is "o"
6770 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006771 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006772 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006773 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006774 {
6775 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006776 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006777 if (has_mbyte)
6778 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006779 oap->inclusive = TRUE;
6780 }
6781}
6782
6783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 * "0" and "^" commands.
6785 * cap->arg is the argument for beginline().
6786 */
6787 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006788nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
6790 cap->oap->motion_type = MCHAR;
6791 cap->oap->inclusive = FALSE;
6792 beginline(cap->arg);
6793#ifdef FEAT_FOLDING
6794 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6795 foldOpenCursor();
6796#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006797 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6798 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799}
6800
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801/*
6802 * In exclusive Visual mode, may include the last character.
6803 */
6804 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006805adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006808 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 if (has_mbyte)
6811 inc_cursor();
6812 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 ++curwin->w_cursor.col;
6814 cap->oap->inclusive = FALSE;
6815 }
6816}
6817
6818/*
6819 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6820 * Should check VIsual_mode before calling this.
6821 * Returns TRUE when backed up to the previous line.
6822 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006823 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006824unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
6826 pos_T *pp;
6827
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006828 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006830 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 pp = &curwin->w_cursor;
6832 else
6833 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 if (pp->coladd > 0)
6835 --pp->coladd;
6836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 if (pp->col > 0)
6838 {
6839 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006840 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 }
6842 else if (pp->lnum > 1)
6843 {
6844 --pp->lnum;
6845 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6846 return TRUE;
6847 }
6848 }
6849 return FALSE;
6850}
6851
6852/*
6853 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6854 */
6855 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006856nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 if (VIsual_active)
6859 VIsual_select = TRUE;
6860 else if (VIsual_reselect)
6861 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006862 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 cap->arg = TRUE;
6864 nv_g_cmd(cap);
6865 }
6866}
6867
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868
6869/*
6870 * "G", "gg", CTRL-END, CTRL-HOME.
6871 * cap->arg is TRUE for "G".
6872 */
6873 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006874nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
6876 linenr_T lnum;
6877
6878 if (cap->arg)
6879 lnum = curbuf->b_ml.ml_line_count;
6880 else
6881 lnum = 1L;
6882 cap->oap->motion_type = MLINE;
6883 setpcmark();
6884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006885 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 if (cap->count0 != 0)
6887 lnum = cap->count0;
6888 if (lnum < 1L)
6889 lnum = 1L;
6890 else if (lnum > curbuf->b_ml.ml_line_count)
6891 lnum = curbuf->b_ml.ml_line_count;
6892 curwin->w_cursor.lnum = lnum;
6893 beginline(BL_SOL | BL_FIX);
6894#ifdef FEAT_FOLDING
6895 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6896 foldOpenCursor();
6897#endif
6898}
6899
6900/*
6901 * CTRL-\ in Normal mode.
6902 */
6903 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006904nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905{
6906 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6907 {
6908 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006909 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006910 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 restart_edit = 0;
6912#ifdef FEAT_CMDWIN
6913 if (cmdwin_type != 0)
6914 cmdwin_result = Ctrl_C;
6915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (VIsual_active)
6917 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006918 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 redraw_curbuf_later(INVERTED);
6920 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006921 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 if (cap->nchar == Ctrl_G && p_im)
6923 restart_edit = 'a';
6924 }
6925 else
6926 clearopbeep(cap->oap);
6927}
6928
6929/*
6930 * ESC in Normal mode: beep, but don't flush buffers.
6931 * Don't even beep if we are canceling a command.
6932 */
6933 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006934nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935{
6936 int no_reason;
6937
6938 no_reason = (cap->oap->op_type == OP_NOP
6939 && cap->opcount == 0
6940 && cap->count0 == 0
6941 && cap->oap->regname == 0
6942 && !p_im);
6943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006944 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 {
6946 if (restart_edit == 0
6947#ifdef FEAT_CMDWIN
6948 && cmdwin_type == 0
6949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006952 {
6953 if (anyBufIsChanged())
6954 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6955 else
6956 msg(_("Type :qa and press <Enter> to exit Vim"));
6957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006959 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6960 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (!p_im)
6962 restart_edit = 0;
6963#ifdef FEAT_CMDWIN
6964 if (cmdwin_type != 0)
6965 {
6966 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 return;
6969 }
6970#endif
6971 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006972#ifdef FEAT_CMDWIN
6973 else if (cmdwin_type != 0 && ex_normal_busy)
6974 {
6975 // When :normal runs out of characters while in the command line window
6976 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6977 // loop.
6978 cmdwin_result = K_IGNORE;
6979 return;
6980 }
6981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (VIsual_active)
6984 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006985 end_visual_mode(); // stop Visual
6986 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 curwin->w_set_curswant = TRUE;
6988 redraw_curbuf_later(INVERTED);
6989 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006990 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006991 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 clearop(cap->oap);
6993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006994 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6995 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006996 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 restart_edit = 'a';
6998}
6999
7000/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007001 * Move the cursor for the "A" command.
7002 */
7003 void
7004set_cursor_for_append_to_line(void)
7005{
7006 curwin->w_set_curswant = TRUE;
7007 if (ve_flags == VE_ALL)
7008 {
7009 int save_State = State;
7010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 // Pretend Insert mode here to allow the cursor on the
7012 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007013 State = INSERT;
7014 coladvance((colnr_T)MAXCOL);
7015 State = save_State;
7016 }
7017 else
7018 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7019}
7020
7021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007023 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 */
7025 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007026nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7030 cap->cmdchar = 'i';
7031
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007032 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007034 {
7035#ifdef FEAT_TERMINAL
7036 if (term_in_normal_mode())
7037 {
7038 end_visual_mode();
7039 clearop(cap->oap);
7040 term_enter_job_mode();
7041 return;
7042 }
7043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007047 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007048 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7049 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
7051#ifdef FEAT_TEXTOBJ
7052 nv_object(cap);
7053#else
7054 clearopbeep(cap->oap);
7055#endif
7056 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007057#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007058 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007059 {
7060 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007061 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007062 return;
7063 }
7064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 else if (!curbuf->b_p_ma && !p_im)
7066 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007068 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007070 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007071 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007072 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007074 else if (cap->cmdchar == K_PS && VIsual_active)
7075 {
7076 pos_T old_pos = curwin->w_cursor;
7077 pos_T old_visual = VIsual;
7078
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007079 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007080 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7081 {
7082 shift_delete_registers();
7083 cap->oap->regname = '1';
7084 }
7085 else
7086 cap->oap->regname = '-';
7087 cap->cmdchar = 'd';
7088 cap->nchar = NUL;
7089 nv_operator(cap);
7090 do_pending_operator(cap, 0, FALSE);
7091 cap->cmdchar = K_PS;
7092
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007093 // When the last char in the line was deleted then append. Detect this
7094 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007095 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7096 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007097 inc_cursor();
7098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007099 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007100 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 else if (!checkclearopq(cap->oap))
7103 {
7104 switch (cap->cmdchar)
7105 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007106 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007107 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 break;
7109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007110 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007111 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7112 beginline(BL_WHITE);
7113 else
7114 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 break;
7116
Bram Moolenaara1891842017-02-04 21:34:31 +01007117 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007118 // Bracketed paste works like "a"ppend, unless the cursor is in
7119 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007120 if (curwin->w_cursor.col == 0)
7121 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007122 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007124 case 'a': // "a"ppend is like "i"nsert on the next character.
7125 // increment coladd when in virtual space, increment the
7126 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 if (virtual_active()
7128 && (curwin->w_cursor.coladd > 0
7129 || *ml_get_cursor() == NUL
7130 || *ml_get_cursor() == TAB))
7131 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007132 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 inc_cursor();
7134 break;
7135 }
7136
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7138 {
7139 int save_State = State;
7140
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007141 // Pretend Insert mode here to allow the cursor on the
7142 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 State = INSERT;
7144 coladvance(getviscol());
7145 State = save_State;
7146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7149 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007150 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007151 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007152 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153}
7154
7155/*
7156 * Invoke edit() and take care of "restart_edit" and the return value.
7157 */
7158 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007159invoke_edit(
7160 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007161 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007162 int cmd,
7163 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
7165 int restart_edit_save = 0;
7166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007167 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7168 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7169 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 if (repl || !stuff_empty())
7171 restart_edit_save = restart_edit;
7172 else
7173 restart_edit_save = 0;
7174
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007175 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 restart_edit = 0;
7177
7178 if (edit(cmd, startln, cap->count1))
7179 cap->retval |= CA_COMMAND_BUSY;
7180
7181 if (restart_edit == 0)
7182 restart_edit = restart_edit_save;
7183}
7184
7185#ifdef FEAT_TEXTOBJ
7186/*
7187 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7188 */
7189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007190nv_object(
7191 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192{
7193 int flag;
7194 int include;
7195 char_u *mps_save;
7196
7197 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007198 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007200 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007202 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 mps_save = curbuf->b_p_mps;
7204 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7205
7206 switch (cap->nchar)
7207 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007208 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 flag = current_word(cap->oap, cap->count1, include, FALSE);
7210 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007211 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 flag = current_word(cap->oap, cap->count1, include, TRUE);
7213 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007214 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 case '(':
7216 case ')':
7217 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7218 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007219 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 case '{':
7221 case '}':
7222 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7223 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007224 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 case ']':
7226 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7227 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007228 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 case '>':
7230 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7231 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007232 case 't': // "at" = a tag block (xml and html)
7233 // Do not adjust oap->end in do_pending_operator()
7234 // otherwise there are different results for 'dit'
7235 // (note leading whitespace in last line):
7236 // 1) <b> 2) <b>
7237 // foobar foobar
7238 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007239 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007240 flag = current_tagblock(cap->oap, cap->count1, include);
7241 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007242 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 flag = current_par(cap->oap, cap->count1, include, 'p');
7244 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007245 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 flag = current_sent(cap->oap, cap->count1, include);
7247 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007248 case '"': // "a"" = a double quoted string
7249 case '\'': // "a'" = a single quoted string
7250 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007251 flag = current_quote(cap->oap, cap->count1, include,
7252 cap->nchar);
7253 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007254#if 0 // TODO
7255 case 'S': // "aS" = a section
7256 case 'f': // "af" = a filename
7257 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258#endif
7259 default:
7260 flag = FAIL;
7261 break;
7262 }
7263
7264 curbuf->b_p_mps = mps_save;
7265 if (flag == FAIL)
7266 clearopbeep(cap->oap);
7267 adjust_cursor_col();
7268 curwin->w_set_curswant = TRUE;
7269}
7270#endif
7271
7272/*
7273 * "q" command: Start/stop recording.
7274 * "q:", "q/", "q?": edit command-line in command-line window.
7275 */
7276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007277nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278{
7279 if (cap->oap->op_type == OP_FORMAT)
7280 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007281 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 cap->cmdchar = 'g';
7283 cap->nchar = 'q';
7284 nv_operator(cap);
7285 }
7286 else if (!checkclearop(cap->oap))
7287 {
7288#ifdef FEAT_CMDWIN
7289 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7290 {
7291 stuffcharReadbuff(cap->nchar);
7292 stuffcharReadbuff(K_CMDWIN);
7293 }
7294 else
7295#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007296 // (stop) recording into a named register, unless executing a
7297 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007298 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 clearopbeep(cap->oap);
7300 }
7301}
7302
7303/*
7304 * Handle the "@r" command.
7305 */
7306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007307nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
7309 if (checkclearop(cap->oap))
7310 return;
7311#ifdef FEAT_EVAL
7312 if (cap->nchar == '=')
7313 {
7314 if (get_expr_register() == NUL)
7315 return;
7316 }
7317#endif
7318 while (cap->count1-- && !got_int)
7319 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007320 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 {
7322 clearopbeep(cap->oap);
7323 break;
7324 }
7325 line_breakcheck();
7326 }
7327}
7328
7329/*
7330 * Handle the CTRL-U and CTRL-D commands.
7331 */
7332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007333nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
7335 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7336 || (cap->cmdchar == Ctrl_D
7337 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7338 clearopbeep(cap->oap);
7339 else if (!checkclearop(cap->oap))
7340 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7341}
7342
7343/*
7344 * Handle "J" or "gJ" command.
7345 */
7346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007347nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007349 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007351 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 {
7353 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007354 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7356 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007358 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007359 if (cap->count0 <= 2)
7360 {
7361 clearopbeep(cap->oap);
7362 return;
7363 }
7364 cap->count0 = curbuf->b_ml.ml_line_count
7365 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007367
7368 prep_redo(cap->oap->regname, cap->count0,
7369 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7370 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 }
7372}
7373
7374/*
7375 * "P", "gP", "p" and "gp" commands.
7376 */
7377 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007378nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007380 nv_put_opt(cap, FALSE);
7381}
7382
7383/*
7384 * "P", "gP", "p" and "gp" commands.
7385 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7386 */
7387 static void
7388nv_put_opt(cmdarg_T *cap, int fix_indent)
7389{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 int regname = 0;
7391 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007392 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007393 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 int dir;
7395 int flags = 0;
7396
7397 if (cap->oap->op_type != OP_NOP)
7398 {
7399#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007400 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7402 {
7403 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007404 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406 else
7407#endif
7408 clearopbeep(cap->oap);
7409 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007410#ifdef FEAT_JOB_CHANNEL
7411 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7412 {
7413 clearopbeep(cap->oap);
7414 }
7415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 else
7417 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007418 if (fix_indent)
7419 {
7420 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7421 ? FORWARD : BACKWARD;
7422 flags |= PUT_FIXINDENT;
7423 }
7424 else
7425 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007426 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7427 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 prep_redo_cmd(cap);
7429 if (cap->cmdchar == 'g')
7430 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007431 else if (cap->cmdchar == 'z')
7432 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if (VIsual_active)
7435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007436 // Putting in Visual mode: The put text replaces the selected
7437 // text. First delete the selected text, then put the new text.
7438 // Need to save and restore the registers that the delete
7439 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007440 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007442#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007444#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007445 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007446 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007447#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
7451 )
7452 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007453 // The delete is going to overwrite the register we want to
7454 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 reg1 = get_register(regname, TRUE);
7456 }
7457
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007458 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 cap->cmdchar = 'd';
7460 cap->nchar = NUL;
7461 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007462 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 nv_operator(cap);
7464 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007465 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007466 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007468 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 cap->oap->regname = regname;
7470
7471 if (reg1 != NULL)
7472 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007473 // Delete probably changed the register we want to put, save
7474 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 reg2 = get_register(regname, FALSE);
7476 put_register(regname, reg1);
7477 }
7478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007479 // When deleted a linewise Visual area, put the register as
7480 // lines to avoid it joined with the next line. When deletion was
7481 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 if (VIsual_mode == 'V')
7483 flags |= PUT_LINE;
7484 else if (VIsual_mode == 'v')
7485 flags |= PUT_LINE_SPLIT;
7486 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7487 flags |= PUT_LINE_FORWARD;
7488 dir = BACKWARD;
7489 if ((VIsual_mode != 'V'
7490 && curwin->w_cursor.col < curbuf->b_op_start.col)
7491 || (VIsual_mode == 'V'
7492 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007493 // cursor is at the end of the line or end of file, put
7494 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007496 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007497 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007499 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007501 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 if (reg2 != NULL)
7503 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007504
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007505 // What to reselect with "gv"? Selecting the just put text seems to
7506 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007507 if (was_visual)
7508 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007509 curbuf->b_visual.vi_start = curbuf->b_op_start;
7510 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007511 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007512 if (*p_sel == 'e')
7513 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007514 }
7515
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007516 // When all lines were selected and deleted do_put() leaves an empty
7517 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007518 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007519 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007520 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007521 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007522
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007523 // If the cursor was in that line, move it to the end of the last
7524 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007525 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7526 {
7527 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7528 coladvance((colnr_T)MAXCOL);
7529 }
7530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 auto_format(FALSE, TRUE);
7532 }
7533}
7534
7535/*
7536 * "o" and "O" commands.
7537 */
7538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007539nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
7541#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007542 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7544 {
7545 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007546 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 }
7548 else
7549#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007550 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007552#ifdef FEAT_JOB_CHANNEL
7553 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007554 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 n_opencmd(cap);
7558}
7559
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560#ifdef FEAT_NETBEANS_INTG
7561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007562nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563{
7564 netbeans_keycommand(cap->nchar);
7565}
7566#endif
7567
7568#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007570nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007572 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573}
7574#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007575
Bram Moolenaar3918c952005-03-15 22:34:55 +00007576/*
7577 * Trigger CursorHold event.
7578 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7579 * input buffer. "did_cursorhold" is set to avoid retriggering.
7580 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007582nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007583{
7584 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7585 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007586 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007587}