blob: da0ffc1498004fe30972cdbd89fe775e318ec6f3 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010020#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010022#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020023static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
29/*
30 * nv_*(): functions called to handle Normal and Visual mode commands.
31 * n_*(): functions called to handle Normal mode commands.
32 * v_*(): functions called to handle Visual mode commands.
33 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void nv_ignore(cmdarg_T *cap);
35static void nv_nop(cmdarg_T *cap);
36static void nv_error(cmdarg_T *cap);
37static void nv_help(cmdarg_T *cap);
38static void nv_addsub(cmdarg_T *cap);
39static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_ver_scrollbar(cmdarg_T *cap);
43static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000045#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void nv_tabline(cmdarg_T *cap);
47static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000048#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static void nv_exmode(cmdarg_T *cap);
50static void nv_colon(cmdarg_T *cap);
51static void nv_ctrlg(cmdarg_T *cap);
52static void nv_ctrlh(cmdarg_T *cap);
53static void nv_clear(cmdarg_T *cap);
54static void nv_ctrlo(cmdarg_T *cap);
55static void nv_hat(cmdarg_T *cap);
56static void nv_Zet(cmdarg_T *cap);
57static void nv_ident(cmdarg_T *cap);
58static void nv_tagpop(cmdarg_T *cap);
59static void nv_scroll(cmdarg_T *cap);
60static void nv_right(cmdarg_T *cap);
61static void nv_left(cmdarg_T *cap);
62static void nv_up(cmdarg_T *cap);
63static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void nv_end(cmdarg_T *cap);
65static void nv_dollar(cmdarg_T *cap);
66static void nv_search(cmdarg_T *cap);
67static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020068static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_csearch(cmdarg_T *cap);
70static void nv_brackets(cmdarg_T *cap);
71static void nv_percent(cmdarg_T *cap);
72static void nv_brace(cmdarg_T *cap);
73static void nv_mark(cmdarg_T *cap);
74static void nv_findpar(cmdarg_T *cap);
75static void nv_undo(cmdarg_T *cap);
76static void nv_kundo(cmdarg_T *cap);
77static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
80static void v_visop(cmdarg_T *cap);
81static void nv_subst(cmdarg_T *cap);
82static void nv_abbrev(cmdarg_T *cap);
83static void nv_optrans(cmdarg_T *cap);
84static void nv_gomark(cmdarg_T *cap);
85static void nv_pcmark(cmdarg_T *cap);
86static void nv_regname(cmdarg_T *cap);
87static void nv_visual(cmdarg_T *cap);
88static void n_start_visual_mode(int c);
89static void nv_window(cmdarg_T *cap);
90static void nv_suspend(cmdarg_T *cap);
91static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_dot(cmdarg_T *cap);
93static void nv_redo(cmdarg_T *cap);
94static void nv_Undo(cmdarg_T *cap);
95static void nv_tilde(cmdarg_T *cap);
96static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_lineop(cmdarg_T *cap);
101static void nv_home(cmdarg_T *cap);
102static void nv_pipe(cmdarg_T *cap);
103static void nv_bck_word(cmdarg_T *cap);
104static void nv_wordcmd(cmdarg_T *cap);
105static void nv_beginline(cmdarg_T *cap);
106static void adjust_cursor(oparg_T *oap);
107static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100108static void nv_select(cmdarg_T *cap);
109static void nv_goto(cmdarg_T *cap);
110static void nv_normal(cmdarg_T *cap);
111static void nv_esc(cmdarg_T *oap);
112static void nv_edit(cmdarg_T *cap);
113static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_record(cmdarg_T *cap);
118static void nv_at(cmdarg_T *cap);
119static void nv_halfpage(cmdarg_T *cap);
120static void nv_join(cmdarg_T *cap);
121static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200122static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100130static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000132static char *e_noident = N_("E349: No identifier under cursor");
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
135 * Function to be called for a Normal or Visual mode command.
136 * The argument is a cmdarg_T.
137 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100138typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100140// Values for cmd_flags.
141#define NV_NCH 0x01 // may need to get a second char
142#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
143#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
144#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100146#define NV_SS 0x10 // may start selection
147#define NV_SSS 0x20 // may start selection with shift modifier
148#define NV_STS 0x40 // may stop selection without shift modif.
149#define NV_RL 0x80 // 'rightleft' modifies command
150#define NV_KEEPREG 0x100 // don't clear regname
151#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153/*
154 * Generally speaking, every Normal mode command should either clear any
155 * pending operator (with *clearop*()), or set the motion type variable
156 * oap->motion_type.
157 *
158 * When a cursor motion command is made, it is marked as being a character or
159 * line oriented motion. Then, if an operator is in effect, the operation
160 * becomes character or line oriented accordingly.
161 */
162
163/*
164 * This table contains one entry for every Normal or Visual mode command.
165 * The order doesn't matter, init_normal_cmds() will create a sorted index.
166 * It is faster when all keys from zero to '~' are present.
167 */
168static const struct nv_cmd
169{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100170 int cmd_char; // (first) command character
171 nv_func_T cmd_func; // function for this command
172 short_u cmd_flags; // NV_ flags
173 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174} nv_cmds[] =
175{
176 {NUL, nv_error, 0, 0},
177 {Ctrl_A, nv_addsub, 0, 0},
178 {Ctrl_B, nv_page, NV_STS, BACKWARD},
179 {Ctrl_C, nv_esc, 0, TRUE},
180 {Ctrl_D, nv_halfpage, 0, 0},
181 {Ctrl_E, nv_scroll_line, 0, TRUE},
182 {Ctrl_F, nv_page, NV_STS, FORWARD},
183 {Ctrl_G, nv_ctrlg, 0, 0},
184 {Ctrl_H, nv_ctrlh, 0, 0},
185 {Ctrl_I, nv_pcmark, 0, 0},
186 {NL, nv_down, 0, FALSE},
187 {Ctrl_K, nv_error, 0, 0},
188 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100189 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {Ctrl_N, nv_down, NV_STS, FALSE},
191 {Ctrl_O, nv_ctrlo, 0, 0},
192 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000193 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {Ctrl_R, nv_redo, 0, 0},
195 {Ctrl_S, nv_ignore, 0, 0},
196 {Ctrl_T, nv_tagpop, NV_NCW, 0},
197 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_V, nv_visual, 0, FALSE},
199 {'V', nv_visual, 0, FALSE},
200 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {Ctrl_W, nv_window, 0, 0},
202 {Ctrl_X, nv_addsub, 0, 0},
203 {Ctrl_Y, nv_scroll_line, 0, FALSE},
204 {Ctrl_Z, nv_suspend, 0, 0},
205 {ESC, nv_esc, 0, FALSE},
206 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
207 {Ctrl_RSB, nv_ident, NV_NCW, 0},
208 {Ctrl_HAT, nv_hat, NV_NCW, 0},
209 {Ctrl__, nv_error, 0, 0},
210 {' ', nv_right, 0, 0},
211 {'!', nv_operator, 0, 0},
212 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
213 {'#', nv_ident, 0, 0},
214 {'$', nv_dollar, 0, 0},
215 {'%', nv_percent, 0, 0},
216 {'&', nv_optrans, 0, 0},
217 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
218 {'(', nv_brace, 0, BACKWARD},
219 {')', nv_brace, 0, FORWARD},
220 {'*', nv_ident, 0, 0},
221 {'+', nv_down, 0, TRUE},
222 {',', nv_csearch, 0, TRUE},
223 {'-', nv_up, 0, TRUE},
224 {'.', nv_dot, NV_KEEPREG, 0},
225 {'/', nv_search, 0, FALSE},
226 {'0', nv_beginline, 0, 0},
227 {'1', nv_ignore, 0, 0},
228 {'2', nv_ignore, 0, 0},
229 {'3', nv_ignore, 0, 0},
230 {'4', nv_ignore, 0, 0},
231 {'5', nv_ignore, 0, 0},
232 {'6', nv_ignore, 0, 0},
233 {'7', nv_ignore, 0, 0},
234 {'8', nv_ignore, 0, 0},
235 {'9', nv_ignore, 0, 0},
236 {':', nv_colon, 0, 0},
237 {';', nv_csearch, 0, FALSE},
238 {'<', nv_operator, NV_RL, 0},
239 {'=', nv_operator, 0, 0},
240 {'>', nv_operator, NV_RL, 0},
241 {'?', nv_search, 0, FALSE},
242 {'@', nv_at, NV_NCH_NOP, FALSE},
243 {'A', nv_edit, 0, 0},
244 {'B', nv_bck_word, 0, 1},
245 {'C', nv_abbrev, NV_KEEPREG, 0},
246 {'D', nv_abbrev, NV_KEEPREG, 0},
247 {'E', nv_wordcmd, 0, TRUE},
248 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
249 {'G', nv_goto, 0, TRUE},
250 {'H', nv_scroll, 0, 0},
251 {'I', nv_edit, 0, 0},
252 {'J', nv_join, 0, 0},
253 {'K', nv_ident, 0, 0},
254 {'L', nv_scroll, 0, 0},
255 {'M', nv_scroll, 0, 0},
256 {'N', nv_next, 0, SEARCH_REV},
257 {'O', nv_open, 0, 0},
258 {'P', nv_put, 0, 0},
259 {'Q', nv_exmode, NV_NCW, 0},
260 {'R', nv_Replace, 0, FALSE},
261 {'S', nv_subst, NV_KEEPREG, 0},
262 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
263 {'U', nv_Undo, 0, 0},
264 {'W', nv_wordcmd, 0, TRUE},
265 {'X', nv_abbrev, NV_KEEPREG, 0},
266 {'Y', nv_abbrev, NV_KEEPREG, 0},
267 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
268 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
269 {'\\', nv_error, 0, 0},
270 {']', nv_brackets, NV_NCH_ALW, FORWARD},
271 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
272 {'_', nv_lineop, 0, 0},
273 {'`', nv_gomark, NV_NCH_ALW, FALSE},
274 {'a', nv_edit, NV_NCH, 0},
275 {'b', nv_bck_word, 0, 0},
276 {'c', nv_operator, 0, 0},
277 {'d', nv_operator, 0, 0},
278 {'e', nv_wordcmd, 0, FALSE},
279 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
280 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
281 {'h', nv_left, NV_RL, 0},
282 {'i', nv_edit, NV_NCH, 0},
283 {'j', nv_down, 0, FALSE},
284 {'k', nv_up, 0, FALSE},
285 {'l', nv_right, NV_RL, 0},
286 {'m', nv_mark, NV_NCH_NOP, 0},
287 {'n', nv_next, 0, 0},
288 {'o', nv_open, 0, 0},
289 {'p', nv_put, 0, 0},
290 {'q', nv_record, NV_NCH, 0},
291 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
292 {'s', nv_subst, NV_KEEPREG, 0},
293 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
294 {'u', nv_undo, 0, 0},
295 {'w', nv_wordcmd, 0, FALSE},
296 {'x', nv_abbrev, NV_KEEPREG, 0},
297 {'y', nv_operator, 0, 0},
298 {'z', nv_zet, NV_NCH_ALW, 0},
299 {'{', nv_findpar, 0, BACKWARD},
300 {'|', nv_pipe, 0, 0},
301 {'}', nv_findpar, 0, FORWARD},
302 {'~', nv_tilde, 0, 0},
303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100304 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200306 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
307 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
308 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
309 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 {K_LEFTMOUSE, nv_mouse, 0, 0},
311 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
312 {K_LEFTDRAG, nv_mouse, 0, 0},
313 {K_LEFTRELEASE, nv_mouse, 0, 0},
314 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100315 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
317 {K_MIDDLEDRAG, nv_mouse, 0, 0},
318 {K_MIDDLERELEASE, nv_mouse, 0, 0},
319 {K_RIGHTMOUSE, nv_mouse, 0, 0},
320 {K_RIGHTDRAG, nv_mouse, 0, 0},
321 {K_RIGHTRELEASE, nv_mouse, 0, 0},
322 {K_X1MOUSE, nv_mouse, 0, 0},
323 {K_X1DRAG, nv_mouse, 0, 0},
324 {K_X1RELEASE, nv_mouse, 0, 0},
325 {K_X2MOUSE, nv_mouse, 0, 0},
326 {K_X2DRAG, nv_mouse, 0, 0},
327 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000328 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000329 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {K_INS, nv_edit, 0, 0},
331 {K_KINS, nv_edit, 0, 0},
332 {K_BS, nv_ctrlh, 0, 0},
333 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
334 {K_S_UP, nv_page, NV_SS, BACKWARD},
335 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
336 {K_S_DOWN, nv_page, NV_SS, FORWARD},
337 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
339 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
340 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
341 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
342 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
343 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
344 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
345 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
346 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
347 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
348 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_S_END, nv_end, NV_SS, FALSE},
350 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
351 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
352 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {K_S_HOME, nv_home, NV_SS, 0},
354 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
355 {K_DEL, nv_abbrev, 0, 0},
356 {K_KDEL, nv_abbrev, 0, 0},
357 {K_UNDO, nv_kundo, 0, 0},
358 {K_HELP, nv_help, NV_NCW, 0},
359 {K_F1, nv_help, NV_NCW, 0},
360 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifdef FEAT_GUI
363 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
364 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
365#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#ifdef FEAT_GUI_TABLINE
367 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000368 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#ifdef FEAT_NETBEANS_INTG
371 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
372#endif
373#ifdef FEAT_DND
374 {K_DROP, nv_drop, NV_STS, 0},
375#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100377 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100378 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379};
380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100381// Number of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100384// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static short nv_cmd_idx[NV_CMDS_SIZE];
386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387// The highest index for which
388// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static int nv_max_linear;
390
391/*
392 * Compare functions for qsort() below, that checks the command character
393 * through the index in nv_cmd_idx[].
394 */
395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100396nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
398 int c1, c2;
399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100400 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 c1 = nv_cmds[*(const short *)s1].cmd_char;
402 c2 = nv_cmds[*(const short *)s2].cmd_char;
403 if (c1 < 0)
404 c1 = -c1;
405 if (c2 < 0)
406 c2 = -c2;
407 return c1 - c2;
408}
409
410/*
411 * Initialize the nv_cmd_idx[] table.
412 */
413 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100414init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
416 int i;
417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000419 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 nv_cmd_idx[i] = i;
421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100425 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000426 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
428 break;
429 nv_max_linear = i - 1;
430}
431
432/*
433 * Search for a command in the commands table.
434 * Returns -1 for invalid command.
435 */
436 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100437find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 int i;
440 int idx;
441 int top, bot;
442 int c;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 if (cmdchar >= 0x100)
446 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100448 // We use the absolute value of the character. Special keys have a
449 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (cmdchar < 0)
451 cmdchar = -cmdchar;
452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100453 // If the character is in the first part: The character is the index into
454 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (cmdchar <= nv_max_linear)
456 return nv_cmd_idx[cmdchar];
457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100458 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 bot = nv_max_linear + 1;
460 top = NV_CMDS_SIZE - 1;
461 idx = -1;
462 while (bot <= top)
463 {
464 i = (top + bot) / 2;
465 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
466 if (c < 0)
467 c = -c;
468 if (cmdchar == c)
469 {
470 idx = nv_cmd_idx[i];
471 break;
472 }
473 if (cmdchar > c)
474 bot = i + 1;
475 else
476 top = i - 1;
477 }
478 return idx;
479}
480
481/*
482 * Execute a command in Normal mode.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100485normal_cmd(
486 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100489 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 int old_col = curwin->w_curswant;
493#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 static int old_mapped_len = 0;
499 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000500#ifdef FEAT_EVAL
501 int set_prevcount = FALSE;
502#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200503 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaara80faa82020-04-12 19:37:17 +0200505 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 // Use a count remembered from before entering an operator. After typing
509 // "3d" we return from normal_cmd() and come back here, the "3" is
510 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 ca.opcount = opcount;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 /*
514 * If there is an operator pending, then the command we take this time
515 * will terminate it. Finish_op tells us to finish the operation before
516 * returning this time (unless the operation was cancelled).
517 */
518#ifdef CURSOR_SHAPE
519 c = finish_op;
520#endif
521 finish_op = (oap->op_type != OP_NOP);
522#ifdef CURSOR_SHAPE
523 if (finish_op != c)
524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100525 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# ifdef FEAT_MOUSESHAPE
527 update_mouseshape(-1);
528# endif
529 }
530#endif
531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 // When not finishing an operator and no register name typed, reset the
533 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000537#ifdef FEAT_EVAL
538 set_prevcount = TRUE;
539#endif
540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 // Restore counts from before receiving K_CURSORHOLD. This means after
543 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
544 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000545 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
546 {
547 ca.opcount = oap->prev_opcount;
548 ca.count0 = oap->prev_count0;
549 oap->prev_opcount = 0;
550 oap->prev_count0 = 0;
551 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 State = NORMAL_BUSY;
556#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100560#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 // Set v:count here, when called from main() and not a stuffed
562 // command, so that v:count can be used in an expression mapping
563 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100564 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100565 set_vcount_ca(&ca, &set_prevcount);
566#endif
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /*
569 * Get the command character from the user.
570 */
571 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100572 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 /*
575 * If a mapping was started in Visual or Select mode, remember the length
576 * of the mapping. This is used below to not return to Insert mode for as
577 * long as the mapping is being executed.
578 */
579 if (restart_edit == 0)
580 old_mapped_len = 0;
581 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000582 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 old_mapped_len = typebuf_maplen();
584
585 if (c == NUL)
586 c = K_ZERO;
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 /*
589 * In Select mode, typed text replaces the selection.
590 */
591 if (VIsual_active
592 && VIsual_select
593 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100595 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
596 // 'insertmode' is set) fake a "d"elete command, Insert mode will
597 // restart automatically.
598 // Insert the typed character in the typeahead buffer, so that it can
599 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200600 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000601 if (restart_edit != 0)
602 c = 'd';
603 else
604 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 msg_nowait = TRUE; // don't delay going to insert mode
606 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609#ifdef FEAT_CMDL_INFO
610 need_flushbuf = add_to_showcmd(c);
611#endif
612
613getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 /*
617 * Handle a count before a command and compute ca.count0.
618 * Note that '0' is a command and not the start of a count, but it's
619 * part of a count after other digits.
620 */
621 while ( (c >= '1' && c <= '9')
622 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
623 {
624 if (c == K_DEL || c == K_KDEL)
625 {
626 ca.count0 /= 10;
627#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#endif
630 }
631 else
632 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100633 if (ca.count0 < 0) // got too large!
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;
704 }
705
706 /*
707 * Always remember the count. It will be set to zero (on the next call,
708 * above) when there is no pending operator.
709 * When called from main(), save the count for use by the "count" built-in
710 * variable.
711 */
712 ca.opcount = ca.count0;
713 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
714
715#ifdef FEAT_EVAL
716 /*
717 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100718 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100720 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000721 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#endif
723
724 /*
725 * Find the command character in the table of commands.
726 * For CTRL-W we already got nchar when looking for a count.
727 */
728 if (ctrl_w)
729 {
730 ca.nchar = c;
731 ca.cmdchar = Ctrl_W;
732 }
733 else
734 ca.cmdchar = c;
735 idx = find_command(ca.cmdchar);
736 if (idx < 0)
737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100738 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 clearopbeep(oap);
740 goto normal_end;
741 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000742
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000743 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100745 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000747 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 goto normal_end;
749 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000750 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
751 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 /*
754 * In Visual/Select mode, a few keys are handled in a special way.
755 */
756 if (VIsual_active)
757 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100758 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 if (km_stopsel
760 && (nv_cmds[idx].cmd_flags & NV_STS)
761 && !(mod_mask & MOD_MASK_SHIFT))
762 {
763 end_visual_mode();
764 redraw_curbuf_later(INVERTED);
765 }
766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100767 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (km_startsel)
769 {
770 if (nv_cmds[idx].cmd_flags & NV_SS)
771 {
772 unshift_special(&ca);
773 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000774 if (idx < 0)
775 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100776 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000777 clearopbeep(oap);
778 goto normal_end;
779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
782 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787#ifdef FEAT_RIGHTLEFT
788 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
789 && (nv_cmds[idx].cmd_flags & NV_RL))
790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // Invert horizontal movements and operations. Only when typed by the
792 // user directly, not when the result of a mapping or "x" translated
793 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 switch (ca.cmdchar)
795 {
796 case 'l': ca.cmdchar = 'h'; break;
797 case K_RIGHT: ca.cmdchar = K_LEFT; break;
798 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
799 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
800 case 'h': ca.cmdchar = 'l'; break;
801 case K_LEFT: ca.cmdchar = K_RIGHT; break;
802 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
803 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
804 case '>': ca.cmdchar = '<'; break;
805 case '<': ca.cmdchar = '>'; break;
806 }
807 idx = find_command(ca.cmdchar);
808 }
809#endif
810
811 /*
812 * Get an additional character if we need one.
813 */
814 if ((nv_cmds[idx].cmd_flags & NV_NCH)
815 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
816 && oap->op_type == OP_NOP)
817 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
818 || (ca.cmdchar == 'q'
819 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200820 && reg_recording == 0
821 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100823 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {
825 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100826 int repl = FALSE; // get character for replace mode
827 int lit = FALSE; // get extra character literally
828 int langmap_active = FALSE; // using :lmap mappings
829 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100830#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100831 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#endif
833
834 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100835 ++allow_keys; // no mapping for nchar, but allow key codes
836 // Don't generate a CursorHold event here, most commands can't handle
837 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000838 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (ca.cmdchar == 'g')
840 {
841 /*
842 * For 'g' get the next character now, so that we can check for
843 * "gr", "g'" and "g`".
844 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000845 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifdef FEAT_CMDL_INFO
848 need_flushbuf |= add_to_showcmd(ca.nchar);
849#endif
850 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100851 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100855 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100860 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
862 else
863 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 repl = TRUE;
866 cp = &ca.nchar;
867 }
868 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
869
870 /*
871 * Get a second or third character.
872 */
873 if (cp != NULL)
874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (repl)
876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100877 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100878#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100879 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
883 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100884 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 --no_mapping;
886 --allow_keys;
887 if (repl)
888 State = LREPLACE;
889 else
890 State = LANGMAP;
891 langmap_active = TRUE;
892 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100893#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100895 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
897 im_set_active(TRUE);
898#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200899 if ((State & INSERT) && !p_ek)
900 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200901#ifdef FEAT_JOB_CHANNEL
902 ch_log_output = TRUE;
903#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200904 // Disable bracketed paste and modifyOtherKeys here, we won't
905 // recognize the escape sequences with 'esckeys' off.
906 out_str(T_BD);
907 out_str(T_CTE);
908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000910 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaarca774f62020-08-30 20:46:38 +0200912 if ((State & INSERT) && !p_ek)
913 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200914#ifdef FEAT_JOB_CHANNEL
915 ch_log_output = TRUE;
916#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200917 // Re-enable bracketed paste mode and modifyOtherKeys
918 out_str(T_BE);
919 out_str(T_CTI);
920 }
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (langmap_active)
923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100924 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 ++no_mapping;
926 ++allow_keys;
927 State = NORMAL_BUSY;
928 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100929#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (lang)
931 {
932 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
933 im_save_status(&curbuf->b_p_iminsert);
934 im_set_active(FALSE);
935 }
936 p_smd = save_smd;
937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#ifdef FEAT_CMDL_INFO
940 need_flushbuf |= add_to_showcmd(*cp);
941#endif
942
943 if (!lit)
944 {
945#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100946 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (*cp == Ctrl_K
948 && ((nv_cmds[idx].cmd_flags & NV_LANG)
949 || cp == &ca.extra_char)
950 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
951 {
952 c = get_digraph(FALSE);
953 if (c > 0)
954 {
955 *cp = c;
956# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100957 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 del_from_showcmd(3);
959 need_flushbuf |= add_to_showcmd(*cp);
960# endif
961 }
962 }
963#endif
964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100965 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100968 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (p_hkmap && lang && KeyTyped)
970 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#endif
972 }
973
974 /*
975 * When the next character is CTRL-\ a following CTRL-N means the
976 * command is aborted and we go to Normal mode.
977 */
978 if (cp == &ca.extra_char
979 && ca.nchar == Ctrl_BSL
980 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
981 {
982 ca.cmdchar = Ctrl_BSL;
983 ca.nchar = ca.extra_char;
984 idx = find_command(ca.cmdchar);
985 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200986 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200987 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 else if (*cp == Ctrl_BSL)
989 {
990 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100992 // There is a busy wait here when typing "f<C-\>" and then
993 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 while ((c = vpeekc()) <= 0 && towait > 0L)
995 {
996 do_sleep(towait > 50L ? 50L : towait);
997 towait -= 50L;
998 }
999 if (c > 0)
1000 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001001 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (c != Ctrl_N && c != Ctrl_G)
1003 vungetc(c);
1004 else
1005 {
1006 ca.cmdchar = Ctrl_BSL;
1007 ca.nchar = c;
1008 idx = find_command(ca.cmdchar);
1009 }
1010 }
1011 }
1012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001013 // When getting a text character and the next character is a
1014 // multi-byte character, it could be a composing character.
1015 // However, don't wait for it to arrive. Also, do enable mapping,
1016 // because if it's put back with vungetc() it's too late to apply
1017 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001018 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 while (enc_utf8 && lang && (c = vpeekc()) > 0
1020 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1021 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001022 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (!utf_iscomposing(c))
1024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001025 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 break;
1027 }
1028 else if (ca.ncharC1 == 0)
1029 ca.ncharC1 = c;
1030 else
1031 ca.ncharC2 = c;
1032 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001033 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035 --no_mapping;
1036 --allow_keys;
1037 }
1038
1039#ifdef FEAT_CMDL_INFO
1040 /*
1041 * Flush the showcmd characters onto the screen so we can see them while
1042 * the command is being executed. Only do this when the shown command was
1043 * actually displayed, otherwise this will slow down a lot when executing
1044 * mappings.
1045 */
1046 if (need_flushbuf)
1047 out_flush();
1048#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001049 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001050 {
1051 if (ex_normal_busy)
1052 did_cursorhold = save_did_cursorhold;
1053 else
1054 did_cursorhold = FALSE;
1055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
1057 State = NORMAL;
1058
1059 if (ca.nchar == ESC)
1060 {
1061 clearop(oap);
1062 if (restart_edit == 0 && goto_im())
1063 restart_edit = 'a';
1064 goto normal_end;
1065 }
1066
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001067 if (ca.cmdchar != K_IGNORE)
1068 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001069 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001070 msg_col = 0;
1071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001073 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001075 // When 'keymodel' contains "startsel" some keys start Select/Visual
1076 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (!VIsual_active && km_startsel)
1078 {
1079 if (nv_cmds[idx].cmd_flags & NV_SS)
1080 {
1081 start_selection();
1082 unshift_special(&ca);
1083 idx = find_command(ca.cmdchar);
1084 }
1085 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1086 && (mod_mask & MOD_MASK_SHIFT))
1087 {
1088 start_selection();
1089 mod_mask &= ~MOD_MASK_SHIFT;
1090 }
1091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
1093 /*
1094 * Execute the command!
1095 * Call the command function found in the commands table.
1096 */
1097 ca.arg = nv_cmds[idx].cmd_arg;
1098 (nv_cmds[idx].cmd_func)(&ca);
1099
1100 /*
1101 * If we didn't start or finish an operator, reset oap->regname, unless we
1102 * need it later.
1103 */
1104 if (!finish_op
1105 && !oap->op_type
1106 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1107 {
1108 clearop(oap);
1109#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001110 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#endif
1112 }
1113
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001114 // Get the length of mapped chars again after typing a count, second
1115 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001116 if (old_mapped_len > 0)
1117 old_mapped_len = typebuf_maplen();
1118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 /*
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001120 * If an operation is pending, handle it. But not for K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001122 if (ca.cmdchar != K_IGNORE)
1123 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125 /*
1126 * Wait for a moment when a message is displayed that will be overwritten
1127 * by the mode message.
1128 * In Visual mode and with "^O" in Insert mode, a short message will be
1129 * overwritten by the mode message. Wait a bit, until a key is hit.
1130 * In Visual mode, it's more important to keep the Visual area updated
1131 * than keeping a message (e.g. from a /pat search).
1132 * Only do this if the command was typed, not from a mapping.
1133 * Don't wait when emsg_silent is non-zero.
1134 * Also wait a bit after an error message, e.g. for "^O:".
1135 * Don't redraw the screen, it would remove the message.
1136 */
1137 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001138 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 || (VIsual_active
1141 && old_pos.lnum == curwin->w_cursor.lnum
1142 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 )
1144 && (clear_cmdline
1145 || redraw_cmdline)
1146 && (msg_didout || (msg_didany && msg_scroll))
1147 && !msg_nowait
1148 && KeyTyped)
1149 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 && (msg_scroll
1152 || emsg_on_display)))
1153 && oap->regname == 0
1154 && !(ca.retval & CA_COMMAND_BUSY)
1155 && stuff_empty()
1156 && typebuf_typed()
1157 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001158 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 && !did_wait_return
1160 && oap->op_type == OP_NOP)
1161 {
1162 int save_State = State;
1163
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001164 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (restart_edit != 0)
1166 State = INSERT;
1167
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001168 // If need to redraw, and there is a "keep_msg", redraw before the
1169 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1171 {
1172 char_u *kmsg;
1173
1174 kmsg = keep_msg;
1175 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001176 // Showmode() will clear keep_msg, but we want to use it anyway.
1177 // First update w_topline.
1178 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001180 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001182
1183 kmsg = vim_strsave(keep_msg);
1184 if (kmsg != NULL)
1185 {
1186 msg_attr((char *)kmsg, keep_msg_attr);
1187 vim_free(kmsg);
1188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001191#ifdef CURSOR_SHAPE
1192 ui_cursor_shape(); // may show different cursor shape
1193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 cursor_on();
1195 out_flush();
1196 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001197 ui_delay(1003L, TRUE); // wait at least one second
1198 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 State = save_State;
1200
1201 msg_scroll = FALSE;
1202 emsg_on_display = FALSE;
1203 }
1204
1205 /*
1206 * Finish up after executing a Normal mode command.
1207 */
1208normal_end:
1209
1210 msg_nowait = FALSE;
1211
Bram Moolenaarcc613032020-06-07 21:31:18 +02001212#ifdef FEAT_EVAL
1213 if (finish_op)
1214 reset_reg_var();
1215#endif
1216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#ifdef CURSOR_SHAPE
1219 c = finish_op;
1220#endif
1221 finish_op = FALSE;
1222#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001223 // Redraw the cursor with another shape, if we were in Operator-pending
1224 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (c || ca.cmdchar == 'r')
1226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228# ifdef FEAT_MOUSESHAPE
1229 update_mouseshape(-1);
1230# endif
1231 }
1232#endif
1233
1234#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001235 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001236 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 clear_showcmd();
1238#endif
1239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 vim_free(ca.searchbuf);
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (has_mbyte)
1244 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (curwin->w_p_scb && toplevel)
1247 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001248 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 do_check_scrollbind(TRUE);
1250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaar860cae12010-06-05 23:22:07 +02001252 if (curwin->w_p_crb && toplevel)
1253 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 do_check_cursorbind();
1256 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001257
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001258#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001259 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001260 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001261 restart_edit = 0;
1262#endif
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /*
1265 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1266 * if still inside a mapping that started in Visual mode).
1267 * May switch from Visual to Select mode after CTRL-O command.
1268 */
1269 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1271 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && !(ca.retval & CA_COMMAND_BUSY)
1273 && stuff_empty()
1274 && oap->regname == 0)
1275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (restart_VIsual_select == 1)
1277 {
1278 VIsual_select = TRUE;
1279 showmode();
1280 restart_VIsual_select = 0;
1281 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001282 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 (void)edit(restart_edit, FALSE, 1L);
1284 }
1285
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (restart_VIsual_select == 2)
1287 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001289 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 opcount = ca.opcount;
1291}
1292
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001293#ifdef FEAT_EVAL
1294/*
1295 * Set v:count and v:count1 according to "cap".
1296 * Set v:prevcount only when "set_prevcount" is TRUE.
1297 */
1298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001299set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001300{
1301 long count = cap->count0;
1302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001303 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001304 if (cap->opcount != 0)
1305 count = cap->opcount * (count == 0 ? 1 : count);
1306 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001307 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001308}
1309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001312 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * if not.
1314 */
1315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001316check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
1318 static int did_check = FALSE;
1319
1320 if (full_screen)
1321 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001322 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001323 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 did_check = TRUE;
1325 }
1326}
1327
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001328#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1329/*
1330 * Call yank_do_autocmd() for "regname".
1331 */
1332 static void
1333call_yank_do_autocmd(int regname)
1334{
1335 oparg_T oa;
1336 yankreg_T *reg;
1337
1338 clear_oparg(&oa);
1339 oa.regname = regname;
1340 oa.op_type = OP_YANK;
1341 oa.is_VIsual = TRUE;
1342 reg = get_register(regname, TRUE);
1343 yank_do_autocmd(&oa, reg);
1344 free_register(reg);
1345}
1346#endif
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001349 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * This function should ALWAYS be called to end Visual mode, except from
1351 * do_pending_operator().
1352 */
1353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001354end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356#ifdef FEAT_CLIPBOARD
1357 /*
1358 * If we are using the clipboard, then remember what was selected in case
1359 * we need to paste it somewhere while we still own the selection.
1360 * Only do this when the clipboard is already owned. Don't want to grab
1361 * the selection when hitting ESC.
1362 */
1363 if (clip_star.available && clip_star.owned)
1364 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001365
1366# if defined(FEAT_EVAL)
1367 // Emit a TextYankPost for the automatic copy of the selection into the
1368 // star and/or plus register.
1369 if (has_textyankpost())
1370 {
1371 if (clip_isautosel_star())
1372 call_yank_do_autocmd('*');
1373 if (clip_isautosel_plus())
1374 call_yank_do_autocmd('+');
1375 }
1376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377#endif
1378
1379 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 setmouse();
1381 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001383 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001384 curbuf->b_visual.vi_mode = VIsual_mode;
1385 curbuf->b_visual.vi_start = VIsual;
1386 curbuf->b_visual.vi_end = curwin->w_cursor;
1387 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388#ifdef FEAT_EVAL
1389 curbuf->b_visual_mode_eval = VIsual_mode;
1390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (!virtual_active())
1392 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001393 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001395 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396}
1397
1398/*
1399 * Reset VIsual_active and VIsual_reselect.
1400 */
1401 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001402reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
1404 if (VIsual_active)
1405 {
1406 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001407 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 VIsual_reselect = FALSE;
1410}
1411
1412/*
1413 * Reset VIsual_active and VIsual_reselect if it's set.
1414 */
1415 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001416reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
1418 if (VIsual_active)
1419 {
1420 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001421 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 VIsual_reselect = FALSE;
1423 }
1424}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001426 void
1427restore_visual_mode(void)
1428{
1429 if (VIsual_mode_orig != NUL)
1430 {
1431 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1432 VIsual_mode_orig = NUL;
1433 }
1434}
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436/*
1437 * Check for a balloon-eval special item to include when searching for an
1438 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1439 * Returns TRUE if the character at "*ptr" should be included.
1440 * "dir" is FORWARD or BACKWARD, the direction of searching.
1441 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1442 * "bnp" points to a counter for square brackets.
1443 */
1444 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001445find_is_eval_item(
1446 char_u *ptr,
1447 int *colp,
1448 int *bnp,
1449 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001451 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1453 ++*bnp;
1454 if (*bnp > 0)
1455 {
1456 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1457 --*bnp;
1458 return TRUE;
1459 }
1460
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001461 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (*ptr == '.')
1463 return TRUE;
1464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001465 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1467 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1468 {
1469 *colp += dir;
1470 return TRUE;
1471 }
1472 return FALSE;
1473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
1475/*
1476 * Find the identifier under or to the right of the cursor.
1477 * "find_type" can have one of three values:
1478 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001479 * FIND_STRING: find any non-white text
1480 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001481 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 *
1483 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001484 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001486 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 * This doesn't match the real Vi but I like it a little better and it
1488 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001489 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 * When FIND_IDENT isn't defined, we backup until a blank.
1491 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001492 * Returns the length of the text, or zero if no text is found.
1493 * If text is found, a pointer to the text is put in "*text". This
1494 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001497find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001500 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501}
1502
1503/*
1504 * Like find_ident_under_cursor(), but for any window and any position.
1505 * However: Uses 'iskeyword' from the current window!.
1506 */
1507 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001508find_ident_at_pos(
1509 win_T *wp,
1510 linenr_T lnum,
1511 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001512 char_u **text,
1513 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001514 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001517 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 int this_class = 0;
1520 int prev_class;
1521 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001522 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 /*
1525 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001526 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 */
1528 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1529 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1530 {
1531 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001532 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 */
1534 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 if (has_mbyte)
1536 {
1537 while (ptr[col] != NUL)
1538 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001539 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1541 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 this_class = mb_get_class(ptr + col);
1543 if (this_class != 0 && (i == 1 || this_class != 1))
1544 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001545 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547 }
1548 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001550 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 )
1553 ++col;
1554
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001555 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
1558 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001559 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (has_mbyte)
1562 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001563 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1565 this_class = mb_get_class((char_u *)"a");
1566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001568 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1571 prev_class = mb_get_class(ptr + prevcol);
1572 if (this_class != prev_class
1573 && (i == 0
1574 || prev_class == 0
1575 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 && (!(find_type & FIND_EVAL)
1577 || prevcol == 0
1578 || !find_is_eval_item(ptr + prevcol, &prevcol,
1579 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 )
1581 break;
1582 col = prevcol;
1583 }
1584
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001585 // If we don't want just any old text, or we've found an
1586 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (this_class > 2)
1588 this_class = 2;
1589 if (!(find_type & FIND_STRING) || this_class == 2)
1590 break;
1591 }
1592 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594 while (col > 0
1595 && ((i == 0
1596 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001597 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 && (!(find_type & FIND_IDENT)
1599 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 || ((find_type & FIND_EVAL)
1601 && col > 1
1602 && find_is_eval_item(ptr + col - 1, &col,
1603 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 ))
1605 --col;
1606
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001607 // If we don't want just any old text, or we've found an
1608 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1610 break;
1611 }
1612 }
1613
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001614 if (ptr[col] == NUL || (i == 0
1615 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001617 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001618 if ((find_type & FIND_NOERROR) == 0)
1619 {
1620 if (find_type & FIND_STRING)
1621 emsg(_("E348: No string under cursor"));
1622 else
1623 emsg(_(e_noident));
1624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 return 0;
1626 }
1627 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001628 *text = ptr;
1629 if (textcol != NULL)
1630 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001633 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 bn = 0;
1636 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (has_mbyte)
1639 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001640 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 this_class = mb_get_class(ptr);
1642 while (ptr[col] != NUL
1643 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1644 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 || ((find_type & FIND_EVAL)
1646 && col <= (int)startcol
1647 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001649 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
1651 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001653 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 || ((find_type & FIND_EVAL)
1655 && col <= (int)startcol
1656 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 return col;
1661}
1662
1663/*
1664 * Prepare for redo of a normal command.
1665 */
1666 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001667prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
1669 prep_redo(cap->oap->regname, cap->count0,
1670 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1671}
1672
1673/*
1674 * Prepare for redo of any command.
1675 * Note that only the last argument can be a multi-byte char.
1676 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001677 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001678prep_redo(
1679 int regname,
1680 long num,
1681 int cmd1,
1682 int cmd2,
1683 int cmd3,
1684 int cmd4,
1685 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001688 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 AppendCharToRedobuff('"');
1691 AppendCharToRedobuff(regname);
1692 }
1693 if (num)
1694 AppendNumberToRedobuff(num);
1695
1696 if (cmd1 != NUL)
1697 AppendCharToRedobuff(cmd1);
1698 if (cmd2 != NUL)
1699 AppendCharToRedobuff(cmd2);
1700 if (cmd3 != NUL)
1701 AppendCharToRedobuff(cmd3);
1702 if (cmd4 != NUL)
1703 AppendCharToRedobuff(cmd4);
1704 if (cmd5 != NUL)
1705 AppendCharToRedobuff(cmd5);
1706}
1707
1708/*
1709 * check for operator active and clear it
1710 *
1711 * return TRUE if operator was active
1712 */
1713 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001714checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 if (oap->op_type == OP_NOP)
1717 return FALSE;
1718 clearopbeep(oap);
1719 return TRUE;
1720}
1721
1722/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001723 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001725 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 */
1727 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001728checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001730 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 return FALSE;
1732 clearopbeep(oap);
1733 return TRUE;
1734}
1735
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001736 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001737clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 oap->op_type = OP_NOP;
1740 oap->regname = 0;
1741 oap->motion_force = NUL;
1742 oap->use_reg_one = FALSE;
1743}
1744
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001745 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001746clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 clearop(oap);
1749 beep_flush();
1750}
1751
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752/*
1753 * Remove the shift modifier from a special key.
1754 */
1755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001756unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 switch (cap->cmdchar)
1759 {
1760 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1761 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1762 case K_S_UP: cap->cmdchar = K_UP; break;
1763 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1764 case K_S_HOME: cap->cmdchar = K_HOME; break;
1765 case K_S_END: cap->cmdchar = K_END; break;
1766 }
1767 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001770/*
1771 * If the mode is currently displayed clear the command line or update the
1772 * command displayed.
1773 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001776{
1777 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001778 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001779#ifdef FEAT_CMDL_INFO
1780 else
1781 clear_showcmd();
1782#endif
1783}
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1786/*
1787 * Routines for displaying a partly typed command
1788 */
1789
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001790#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001792static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793static int showcmd_is_clear = TRUE;
1794static int showcmd_visual = FALSE;
1795
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001796static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001799clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 if (!p_sc)
1802 return;
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (VIsual_active && !char_avail())
1805 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001806 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 long lines;
1808 colnr_T leftcol, rightcol;
1809 linenr_T top, bot;
1810
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001811 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001812 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
1814 top = VIsual.lnum;
1815 bot = curwin->w_cursor.lnum;
1816 }
1817 else
1818 {
1819 top = curwin->w_cursor.lnum;
1820 bot = VIsual.lnum;
1821 }
1822# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001823 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001824 (void)hasFolding(top, &top, NULL);
1825 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826# endif
1827 lines = bot - top + 1;
1828
1829 if (VIsual_mode == Ctrl_V)
1830 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001831# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001832 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001833 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001835 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001836 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001837 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001840# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001841 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001842 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1845 (long)(rightcol - leftcol + 1));
1846 }
1847 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1848 sprintf((char *)showcmd_buf, "%ld", lines);
1849 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001850 {
1851 char_u *s, *e;
1852 int l;
1853 int bytes = 0;
1854 int chars = 0;
1855
1856 if (cursor_bot)
1857 {
1858 s = ml_get_pos(&VIsual);
1859 e = ml_get_cursor();
1860 }
1861 else
1862 {
1863 s = ml_get_cursor();
1864 e = ml_get_pos(&VIsual);
1865 }
1866 while ((*p_sel != 'e') ? s <= e : s < e)
1867 {
1868 l = (*mb_ptr2len)(s);
1869 if (l == 0)
1870 {
1871 ++bytes;
1872 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001873 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001874 }
1875 bytes += l;
1876 ++chars;
1877 s += l;
1878 }
1879 if (bytes == chars)
1880 sprintf((char *)showcmd_buf, "%d", chars);
1881 else
1882 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1883 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001884 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 showcmd_visual = TRUE;
1886 }
1887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
1889 showcmd_buf[0] = NUL;
1890 showcmd_visual = FALSE;
1891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001892 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (showcmd_is_clear)
1894 return;
1895 }
1896
1897 display_showcmd();
1898}
1899
1900/*
1901 * Add 'c' to string of shown command chars.
1902 * Return TRUE if output has been written (and setcursor() has been called).
1903 */
1904 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001905add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 char_u *p;
1908 int old_len;
1909 int extra_len;
1910 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 int i;
1912 static int ignore[] =
1913 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001914#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1916 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001917#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001918 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001919 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1921 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001922 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001924 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 0
1926 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaar09df3122006-01-23 22:23:09 +00001928 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 return FALSE;
1930
1931 if (showcmd_visual)
1932 {
1933 showcmd_buf[0] = NUL;
1934 showcmd_visual = FALSE;
1935 }
1936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001937 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (IS_SPECIAL(c))
1939 for (i = 0; ignore[i] != 0; ++i)
1940 if (ignore[i] == c)
1941 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
1943 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001944 if (*p == ' ')
1945 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 old_len = (int)STRLEN(showcmd_buf);
1947 extra_len = (int)STRLEN(p);
1948 overflow = old_len + extra_len - SHOWCMD_COLS;
1949 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001950 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1951 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 STRCAT(showcmd_buf, p);
1953
1954 if (char_avail())
1955 return FALSE;
1956
1957 display_showcmd();
1958
1959 return TRUE;
1960}
1961
1962 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001963add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964{
1965 if (!add_to_showcmd(c))
1966 setcursor();
1967}
1968
1969/*
1970 * Delete 'len' characters from the end of the shown command.
1971 */
1972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001973del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 int old_len;
1976
1977 if (!p_sc)
1978 return;
1979
1980 old_len = (int)STRLEN(showcmd_buf);
1981 if (len > old_len)
1982 len = old_len;
1983 showcmd_buf[old_len - len] = NUL;
1984
1985 if (!char_avail())
1986 display_showcmd();
1987}
1988
1989/*
1990 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1991 * something and there is a partial mapping.
1992 */
1993 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001994push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 if (p_sc)
1997 STRCPY(old_showcmd_buf, showcmd_buf);
1998}
1999
2000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002001pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
2003 if (!p_sc)
2004 return;
2005
2006 STRCPY(showcmd_buf, old_showcmd_buf);
2007
2008 display_showcmd();
2009}
2010
2011 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002012display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013{
2014 int len;
2015
2016 cursor_off();
2017
2018 len = (int)STRLEN(showcmd_buf);
2019 if (len == 0)
2020 showcmd_is_clear = TRUE;
2021 else
2022 {
2023 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2024 showcmd_is_clear = FALSE;
2025 }
2026
2027 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002028 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2029 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 */
2031 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002033 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034}
2035#endif
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037/*
2038 * When "check" is FALSE, prepare for commands that scroll the window.
2039 * When "check" is TRUE, take care of scroll-binding after the window has
2040 * scrolled. Called from normal_cmd() and edit().
2041 */
2042 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002043do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
2045 static win_T *old_curwin = NULL;
2046 static linenr_T old_topline = 0;
2047#ifdef FEAT_DIFF
2048 static int old_topfill = 0;
2049#endif
2050 static buf_T *old_buf = NULL;
2051 static colnr_T old_leftcol = 0;
2052
2053 if (check && curwin->w_p_scb)
2054 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002055 // If a ":syncbind" command was just used, don't scroll, only reset
2056 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (did_syncbind)
2058 did_syncbind = FALSE;
2059 else if (curwin == old_curwin)
2060 {
2061 /*
2062 * Synchronize other windows, as necessary according to
2063 * 'scrollbind'. Don't do this after an ":edit" command, except
2064 * when 'diff' is set.
2065 */
2066 if ((curwin->w_buffer == old_buf
2067#ifdef FEAT_DIFF
2068 || curwin->w_p_diff
2069#endif
2070 )
2071 && (curwin->w_topline != old_topline
2072#ifdef FEAT_DIFF
2073 || curwin->w_topfill != old_topfill
2074#endif
2075 || curwin->w_leftcol != old_leftcol))
2076 {
2077 check_scrollbind(curwin->w_topline - old_topline,
2078 (long)(curwin->w_leftcol - old_leftcol));
2079 }
2080 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002081 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
2083 /*
2084 * When switching between windows, make sure that the relative
2085 * vertical offset is valid for the new window. The relative
2086 * offset is invalid whenever another 'scrollbind' window has
2087 * scrolled to a point that would force the current window to
2088 * scroll past the beginning or end of its buffer. When the
2089 * resync is performed, some of the other 'scrollbind' windows may
2090 * need to jump so that the current window's relative position is
2091 * visible on-screen.
2092 */
2093 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2094 }
2095 curwin->w_scbind_pos = curwin->w_topline;
2096 }
2097
2098 old_curwin = curwin;
2099 old_topline = curwin->w_topline;
2100#ifdef FEAT_DIFF
2101 old_topfill = curwin->w_topfill;
2102#endif
2103 old_buf = curwin->w_buffer;
2104 old_leftcol = curwin->w_leftcol;
2105}
2106
2107/*
2108 * Synchronize any windows that have "scrollbind" set, based on the
2109 * number of rows by which the current window has changed
2110 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2111 */
2112 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002113check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
2115 int want_ver;
2116 int want_hor;
2117 win_T *old_curwin = curwin;
2118 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 int old_VIsual_select = VIsual_select;
2120 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 colnr_T tgt_leftcol = curwin->w_leftcol;
2122 long topline;
2123 long y;
2124
2125 /*
2126 * check 'scrollopt' string for vertical and horizontal scroll options
2127 */
2128 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2129#ifdef FEAT_DIFF
2130 want_ver |= old_curwin->w_p_diff;
2131#endif
2132 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2133
2134 /*
2135 * loop through the scrollbound windows and scroll accordingly
2136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002138 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002141 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (curwin != old_curwin && curwin->w_p_scb)
2143 {
2144 /*
2145 * do the vertical scroll
2146 */
2147 if (want_ver)
2148 {
2149#ifdef FEAT_DIFF
2150 if (old_curwin->w_p_diff && curwin->w_p_diff)
2151 {
2152 diff_set_topline(old_curwin, curwin);
2153 }
2154 else
2155#endif
2156 {
2157 curwin->w_scbind_pos += topline_diff;
2158 topline = curwin->w_scbind_pos;
2159 if (topline > curbuf->b_ml.ml_line_count)
2160 topline = curbuf->b_ml.ml_line_count;
2161 if (topline < 1)
2162 topline = 1;
2163
2164 y = topline - curwin->w_topline;
2165 if (y > 0)
2166 scrollup(y, FALSE);
2167 else
2168 scrolldown(-y, FALSE);
2169 }
2170
2171 redraw_later(VALID);
2172 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175
2176 /*
2177 * do the horizontal scroll
2178 */
2179 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2180 {
2181 curwin->w_leftcol = tgt_leftcol;
2182 leftcol_changed();
2183 }
2184 }
2185 }
2186
2187 /*
2188 * reset current-window
2189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 VIsual_select = old_VIsual_select;
2191 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 curwin = old_curwin;
2193 curbuf = old_curbuf;
2194}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196/*
2197 * Command character that's ignored.
2198 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002199 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002202nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205}
2206
2207/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002208 * Command character that doesn't do anything, but unlike nv_ignore() does
2209 * start edit(). Used for "startinsert" executed while starting up.
2210 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002212nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002213{
2214}
2215
2216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 * Command character doesn't exist.
2218 */
2219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002220nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222 clearopbeep(cap->oap);
2223}
2224
2225/*
2226 * <Help> and <F1> commands.
2227 */
2228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002229nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 if (!checkclearopq(cap->oap))
2232 ex_help(NULL);
2233}
2234
2235/*
2236 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2237 */
2238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002239nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002241#ifdef FEAT_JOB_CHANNEL
2242 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2243 clearopbeep(cap->oap);
2244 else
2245#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002246 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002247 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002248 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002249 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2250 op_addsub(cap->oap, cap->count1, cap->arg);
2251 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002252 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002253 else if (VIsual_active)
2254 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002255 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002256 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257}
2258
2259/*
2260 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2261 */
2262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002263nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002266 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267 if (mod_mask & MOD_MASK_CTRL)
2268 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002269 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270 if (cap->arg == BACKWARD)
2271 goto_tabpage(-(int)cap->count1);
2272 else
2273 goto_tabpage((int)cap->count0);
2274 }
2275 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002276 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278}
2279
2280/*
2281 * Implementation of "gd" and "gD" command.
2282 */
2283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002284nv_gd(
2285 oparg_T *oap,
2286 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002287 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 char_u *ptr;
2291
Bram Moolenaard9d30582005-05-18 22:10:28 +00002292 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002293 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2294 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002296#ifdef FEAT_FOLDING
2297 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2298 foldOpenCursor();
2299#endif
2300}
2301
2302/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002303 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2304 * otherwise.
2305 */
2306 static int
2307is_ident(char_u *line, int offset)
2308{
2309 int i;
2310 int incomment = FALSE;
2311 int instring = 0;
2312 int prev = 0;
2313
2314 for (i = 0; i < offset && line[i] != NUL; i++)
2315 {
2316 if (instring != 0)
2317 {
2318 if (prev != '\\' && line[i] == instring)
2319 instring = 0;
2320 }
2321 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2322 {
2323 instring = line[i];
2324 }
2325 else
2326 {
2327 if (incomment)
2328 {
2329 if (prev == '*' && line[i] == '/')
2330 incomment = FALSE;
2331 }
2332 else if (prev == '/' && line[i] == '*')
2333 {
2334 incomment = TRUE;
2335 }
2336 else if (prev == '/' && line[i] == '/')
2337 {
2338 return FALSE;
2339 }
2340 }
2341
2342 prev = line[i];
2343 }
2344
2345 return incomment == FALSE && instring == 0;
2346}
2347
2348/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002349 * Search for variable declaration of "ptr[len]".
2350 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2351 * current file ("gD").
2352 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002353 * Return FAIL when not found.
2354 */
2355 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002356find_decl(
2357 char_u *ptr,
2358 int len,
2359 int locally,
2360 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002361 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002362{
2363 char_u *pat;
2364 pos_T old_pos;
2365 pos_T par_pos;
2366 pos_T found_pos;
2367 int t;
2368 int save_p_ws;
2369 int save_p_scs;
2370 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002371 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002372 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002373 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002374
2375 if ((pat = alloc(len + 7)) == NULL)
2376 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002377
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002378 // Put "\V" before the pattern to avoid that the special meaning of "."
2379 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002380 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2381 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 old_pos = curwin->w_cursor;
2383 save_p_ws = p_ws;
2384 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002385 p_ws = FALSE; // don't wrap around end of file now
2386 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 /*
2389 * With "gD" go to line 1.
2390 * With "gd" Search back for the start of the current function, then go
2391 * back until a blank line. If this fails go to line 1.
2392 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002393 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002397 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
2399 else
2400 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002401 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2403 --curwin->w_cursor.lnum;
2404 }
2405 curwin->w_cursor.col = 0;
2406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002407 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002408 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002409 for (;;)
2410 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002411 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002412 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002413 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002414 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002415
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002416 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002417 {
2418 pos_T *pos;
2419
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002420 // Check that the block the match is in doesn't end before the
2421 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002422 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2423 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2424 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002425 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002426 // There can't be a useful match before the end of this block.
2427 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002428 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002429 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002430 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002431 }
2432
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002433 if (t == FAIL)
2434 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002435 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002436 if (found_pos.lnum != 0)
2437 {
2438 curwin->w_cursor = found_pos;
2439 t = OK;
2440 }
2441 break;
2442 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002443 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002446 ++curwin->w_cursor.lnum;
2447 curwin->w_cursor.col = 0;
2448 continue;
2449 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002450 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002452 // If the current position is not a valid identifier and a previous
2453 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002454 if (!valid && found_pos.lnum != 0)
2455 {
2456 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002457 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002458 }
2459
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002460 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002461 if (valid && !locally)
2462 break;
2463 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002464 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002465 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002466 if (found_pos.lnum != 0)
2467 curwin->w_cursor = found_pos;
2468 break;
2469 }
2470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002471 // For finding a local variable and the match is before the "{" or
2472 // inside a comment, continue searching. For K&R style function
2473 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002474 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002475 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002476 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002477 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002478 // Remove SEARCH_START from flags to avoid getting stuck at one
2479 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002480 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002482
2483 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002485 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 curwin->w_cursor = old_pos;
2487 }
2488 else
2489 {
2490 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002491 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 reset_search_dir();
2493 }
2494
2495 vim_free(pat);
2496 p_ws = save_p_ws;
2497 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002498
2499 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501
2502/*
2503 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2504 * lines rather than lines in the file.
2505 * 'dist' must be positive.
2506 *
2507 * Return OK if able to move cursor, FAIL otherwise.
2508 */
2509 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002510nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 int linelen = linetabsize(ml_get_curline());
2513 int retval = OK;
2514 int atend = FALSE;
2515 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002516 int col_off1; // margin offset for first screen line
2517 int col_off2; // margin offset for wrapped screen line
2518 int width1; // text width for first screen line
2519 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
2521 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002522 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
2524 col_off1 = curwin_col_off();
2525 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002526 width1 = curwin->w_width - col_off1;
2527 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002528 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002529 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 /*
2534 * Instead of sticking at the last character of the buffer line we
2535 * try to stick in the last column of the screen.
2536 */
2537 if (curwin->w_curswant == MAXCOL)
2538 {
2539 atend = TRUE;
2540 validate_virtcol();
2541 if (width1 <= 0)
2542 curwin->w_curswant = 0;
2543 else
2544 {
2545 curwin->w_curswant = width1 - 1;
2546 if (curwin->w_virtcol > curwin->w_curswant)
2547 curwin->w_curswant += ((curwin->w_virtcol
2548 - curwin->w_curswant - 1) / width2 + 1) * width2;
2549 }
2550 }
2551 else
2552 {
2553 if (linelen > width1)
2554 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2555 else
2556 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002557 if (curwin->w_curswant >= (colnr_T)n)
2558 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560
2561 while (dist--)
2562 {
2563 if (dir == BACKWARD)
2564 {
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002565 if ((long)curwin->w_curswant >= width1)
2566 // Move back within the line. This can give a negative value
2567 // for w_curswant if width1 < width2 (with cpoptions+=n),
2568 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 curwin->w_curswant -= width2;
2570 else
2571 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002572 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (curwin->w_cursor.lnum == 1)
2574 {
2575 retval = FAIL;
2576 break;
2577 }
2578 --curwin->w_cursor.lnum;
2579#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002580 // Move to the start of a closed fold. Don't do that when
2581 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (!(fdo_flags & FDO_ALL))
2583 (void)hasFolding(curwin->w_cursor.lnum,
2584 &curwin->w_cursor.lnum, NULL);
2585#endif
2586 linelen = linetabsize(ml_get_curline());
2587 if (linelen > width1)
2588 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2589 + 1) * width2;
2590 }
2591 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002592 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 if (linelen > width1)
2595 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2596 else
2597 n = width1;
2598 if (curwin->w_curswant + width2 < (colnr_T)n)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002599 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 curwin->w_curswant += width2;
2601 else
2602 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002603 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002605 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2607 &curwin->w_cursor.lnum);
2608#endif
2609 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2610 {
2611 retval = FAIL;
2612 break;
2613 }
2614 curwin->w_cursor.lnum++;
2615 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002616 // Check if the cursor has moved below the number display
2617 // when width1 < width2 (with cpoptions+=n). Subtract width2
2618 // to get a negative value for w_curswant, which will get
2619 // clipped to column 0.
2620 if (curwin->w_curswant >= width1)
2621 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002622 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624 }
2625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002628 if (virtual_active() && atend)
2629 coladvance(MAXCOL);
2630 else
2631 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2634 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002635 colnr_T virtcol;
2636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 /*
2638 * Check for landing on a character that got split at the end of the
2639 * last line. We want to advance a screenline, not end up in the same
2640 * screenline or move two screenlines.
2641 */
2642 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002643 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002644#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002645 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2646 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002647#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002648
2649 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 && (curwin->w_curswant < (colnr_T)width1
2651 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2652 : ((curwin->w_curswant - width1) % width2
2653 > (colnr_T)width2 / 2)))
2654 --curwin->w_cursor.col;
2655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002658 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 return retval;
2661}
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663/*
2664 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2665 * cap->arg must be TRUE for CTRL-E.
2666 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002668nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
2670 if (!checkclearop(cap->oap))
2671 scroll_redraw(cap->arg, cap->count1);
2672}
2673
2674/*
2675 * Scroll "count" lines up or down, and redraw.
2676 */
2677 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002678scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680 linenr_T prev_topline = curwin->w_topline;
2681#ifdef FEAT_DIFF
2682 int prev_topfill = curwin->w_topfill;
2683#endif
2684 linenr_T prev_lnum = curwin->w_cursor.lnum;
2685
2686 if (up)
2687 scrollup(count, TRUE);
2688 else
2689 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002690 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002692 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2693 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 cursor_correct();
2695 check_cursor_moved(curwin);
2696 curwin->w_valid |= VALID_TOPLINE;
2697
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 // If moved back to where we were, at least move the cursor, otherwise
2699 // we get stuck at one position. Don't move the cursor up if the
2700 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 while (curwin->w_topline == prev_topline
2702#ifdef FEAT_DIFF
2703 && curwin->w_topfill == prev_topfill
2704#endif
2705 )
2706 {
2707 if (up)
2708 {
2709 if (curwin->w_cursor.lnum > prev_lnum
2710 || cursor_down(1L, FALSE) == FAIL)
2711 break;
2712 }
2713 else
2714 {
2715 if (curwin->w_cursor.lnum < prev_lnum
2716 || prev_topline == 1L
2717 || cursor_up(1L, FALSE) == FAIL)
2718 break;
2719 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 // Mark w_topline as valid, otherwise the screen jumps back at the
2721 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 check_cursor_moved(curwin);
2723 curwin->w_valid |= VALID_TOPLINE;
2724 }
2725 }
2726 if (curwin->w_cursor.lnum != prev_lnum)
2727 coladvance(curwin->w_curswant);
2728 redraw_later(VALID);
2729}
2730
2731/*
2732 * Commands that start with "z".
2733 */
2734 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002735nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
2737 long n;
2738 colnr_T col;
2739 int nchar = cap->nchar;
2740#ifdef FEAT_FOLDING
2741 long old_fdl = curwin->w_p_fdl;
2742 int old_fen = curwin->w_p_fen;
2743#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002744#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002745 int undo = FALSE;
2746#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002747 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749 if (VIM_ISDIGIT(nchar))
2750 {
2751 /*
2752 * "z123{nchar}": edit the count before obtaining {nchar}
2753 */
2754 if (checkclearop(cap->oap))
2755 return;
2756 n = nchar - '0';
2757 for (;;)
2758 {
2759#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002760 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761#endif
2762 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002763 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002764 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 --no_mapping;
2767 --allow_keys;
2768#ifdef FEAT_CMDL_INFO
2769 (void)add_to_showcmd(nchar);
2770#endif
2771 if (nchar == K_DEL || nchar == K_KDEL)
2772 n /= 10;
2773 else if (VIM_ISDIGIT(nchar))
2774 n = n * 10 + (nchar - '0');
2775 else if (nchar == CAR)
2776 {
2777#ifdef FEAT_GUI
2778 need_mouse_correct = TRUE;
2779#endif
2780 win_setheight((int)n);
2781 break;
2782 }
2783 else if (nchar == 'l'
2784 || nchar == 'h'
2785 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002786 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
2788 cap->count1 = n ? n * cap->count1 : cap->count1;
2789 goto dozet;
2790 }
2791 else
2792 {
2793 clearopbeep(cap->oap);
2794 break;
2795 }
2796 }
2797 cap->oap->op_type = OP_NOP;
2798 return;
2799 }
2800
2801dozet:
2802 if (
2803#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2805 // and "zC" only in Visual mode. "zj" and "zk" are motion
2806 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 cap->nchar != 'f' && cap->nchar != 'F'
2808 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2809 && cap->nchar != 'j' && cap->nchar != 'k'
2810 &&
2811#endif
2812 checkclearop(cap->oap))
2813 return;
2814
2815 /*
2816 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2817 * If line number given, set cursor.
2818 */
2819 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2820 && cap->count0
2821 && cap->count0 != curwin->w_cursor.lnum)
2822 {
2823 setpcmark();
2824 if (cap->count0 > curbuf->b_ml.ml_line_count)
2825 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2826 else
2827 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002828 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 }
2830
2831 switch (nchar)
2832 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 case '+':
2835 if (cap->count0 == 0)
2836 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 // No count given: put cursor at the line below screen
2838 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2840 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2841 else
2842 curwin->w_cursor.lnum = curwin->w_botline;
2843 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002844 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 case NL:
2846 case CAR:
2847 case K_KENTER:
2848 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 case 't': scroll_cursor_top(0, TRUE);
2852 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002853 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 break;
2855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
2860 case 'z': scroll_cursor_halfway(TRUE);
2861 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002862 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 break;
2864
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002865 // "z^", "z-" and "zb": put cursor at bottom of screen
2866 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2867 // when <count> is at bottom of window, and puts that one at
2868 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 if (cap->count0 != 0)
2870 {
2871 scroll_cursor_bot(0, TRUE);
2872 curwin->w_cursor.lnum = curwin->w_topline;
2873 }
2874 else if (curwin->w_topline == 1)
2875 curwin->w_cursor.lnum = 1;
2876 else
2877 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 case '-':
2880 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
2883 case 'b': scroll_cursor_bot(0, TRUE);
2884 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002885 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 break;
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002890 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002893 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 case 'h':
2895 case K_LEFT:
2896 if (!curwin->w_p_wrap)
2897 {
2898 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2899 curwin->w_leftcol = 0;
2900 else
2901 curwin->w_leftcol -= (colnr_T)cap->count1;
2902 leftcol_changed();
2903 }
2904 break;
2905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002906 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002907 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 case 'l':
2912 case K_RIGHT:
2913 if (!curwin->w_p_wrap)
2914 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 curwin->w_leftcol += (colnr_T)cap->count1;
2917 leftcol_changed();
2918 }
2919 break;
2920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 case 's': if (!curwin->w_p_wrap)
2923 {
2924#ifdef FEAT_FOLDING
2925 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 else
2928#endif
2929 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002930 if ((long)col > siso)
2931 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 else
2933 col = 0;
2934 if (curwin->w_leftcol != col)
2935 {
2936 curwin->w_leftcol = col;
2937 redraw_later(NOT_VALID);
2938 }
2939 }
2940 break;
2941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 case 'e': if (!curwin->w_p_wrap)
2944 {
2945#ifdef FEAT_FOLDING
2946 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002947 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 else
2949#endif
2950 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002951 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002952 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 col = 0;
2954 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002955 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (curwin->w_leftcol != col)
2957 {
2958 curwin->w_leftcol = col;
2959 redraw_later(NOT_VALID);
2960 }
2961 }
2962 break;
2963
2964#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002965 // "zF": create fold command
2966 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 case 'F':
2968 case 'f': if (foldManualAllowed(TRUE))
2969 {
2970 cap->nchar = 'f';
2971 nv_operator(cap);
2972 curwin->w_p_fen = TRUE;
2973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2976 {
2977 nv_operator(cap);
2978 finish_op = TRUE;
2979 }
2980 }
2981 else
2982 clearopbeep(cap->oap);
2983 break;
2984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 // "zd": delete fold at cursor
2986 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 case 'd':
2988 case 'D': if (foldManualAllowed(FALSE))
2989 {
2990 if (VIsual_active)
2991 nv_operator(cap);
2992 else
2993 deleteFold(curwin->w_cursor.lnum,
2994 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2995 }
2996 break;
2997
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 case 'E': if (foldmethodIsManual(curwin))
3000 {
3001 clearFolding(curwin);
3002 changed_window_setting();
3003 }
3004 else if (foldmethodIsMarker(curwin))
3005 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3006 TRUE, FALSE);
3007 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 break;
3010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003011 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 case 'n': curwin->w_p_fen = FALSE;
3013 break;
3014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003015 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 case 'N': curwin->w_p_fen = TRUE;
3017 break;
3018
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003019 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3021 break;
3022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3025 openFold(curwin->w_cursor.lnum, cap->count1);
3026 else
3027 {
3028 closeFold(curwin->w_cursor.lnum, cap->count1);
3029 curwin->w_p_fen = TRUE;
3030 }
3031 break;
3032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3035 openFoldRecurse(curwin->w_cursor.lnum);
3036 else
3037 {
3038 closeFoldRecurse(curwin->w_cursor.lnum);
3039 curwin->w_p_fen = TRUE;
3040 }
3041 break;
3042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003043 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 case 'o': if (VIsual_active)
3045 nv_operator(cap);
3046 else
3047 openFold(curwin->w_cursor.lnum, cap->count1);
3048 break;
3049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003050 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 case 'O': if (VIsual_active)
3052 nv_operator(cap);
3053 else
3054 openFoldRecurse(curwin->w_cursor.lnum);
3055 break;
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 case 'c': if (VIsual_active)
3059 nv_operator(cap);
3060 else
3061 closeFold(curwin->w_cursor.lnum, cap->count1);
3062 curwin->w_p_fen = TRUE;
3063 break;
3064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003065 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 case 'C': if (VIsual_active)
3067 nv_operator(cap);
3068 else
3069 closeFoldRecurse(curwin->w_cursor.lnum);
3070 curwin->w_p_fen = TRUE;
3071 break;
3072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003073 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 case 'v': foldOpenCursor();
3075 break;
3076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003077 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003079 curwin->w_foldinvalid = TRUE; // recompute folds
3080 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 foldOpenCursor();
3082 break;
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003086 curwin->w_foldinvalid = TRUE; // recompute folds
3087 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 break;
3089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003092 {
3093 curwin->w_p_fdl -= cap->count1;
3094 if (curwin->w_p_fdl < 0)
3095 curwin->w_p_fdl = 0;
3096 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 curwin->w_p_fen = TRUE;
3099 break;
3100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003103 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 curwin->w_p_fen = TRUE;
3105 break;
3106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003107 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003108 case 'r': curwin->w_p_fdl += cap->count1;
3109 {
3110 int d = getDeepestNesting();
3111
3112 if (curwin->w_p_fdl >= d)
3113 curwin->w_p_fdl = d;
3114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 break;
3116
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003117 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003119 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 break;
3121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003122 case 'j': // "zj" move to next fold downwards
3123 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3125 cap->count1) == FAIL)
3126 clearopbeep(cap->oap);
3127 break;
3128
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003129#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003131#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003132 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003133 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003135 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003136 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003137 --no_mapping;
3138 --allow_keys;
3139#ifdef FEAT_CMDL_INFO
3140 (void)add_to_showcmd(nchar);
3141#endif
3142 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3143 {
3144 clearopbeep(cap->oap);
3145 break;
3146 }
3147 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003148 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003149
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003150 case 'g': // "zg": add good word to word list
3151 case 'w': // "zw": add wrong word to word list
3152 case 'G': // "zG": add good word to temp word list
3153 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003154 {
3155 char_u *ptr = NULL;
3156 int len;
3157
3158 if (checkclearop(cap->oap))
3159 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003160 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3161 == FAIL)
3162 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003163 if (ptr == NULL)
3164 {
3165 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 // Find bad word under the cursor. When 'spell' is
3168 // off this fails and find_ident_under_cursor() is
3169 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003170 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003171 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003172 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003173 if (len != 0 && curwin->w_cursor.col <= pos.col)
3174 ptr = ml_get_pos(&curwin->w_cursor);
3175 curwin->w_cursor = pos;
3176 }
3177
Bram Moolenaarb765d632005-06-07 21:00:02 +00003178 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3179 FIND_IDENT)) == 0)
3180 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003181 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3182 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003183 (nchar == 'G' || nchar == 'W')
3184 ? 0 : (int)cap->count1,
3185 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003186 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003187 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003189 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003190 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003191 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003192 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003193#endif
3194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 default: clearopbeep(cap->oap);
3196 }
3197
3198#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003199 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (old_fen != curwin->w_p_fen)
3201 {
3202# ifdef FEAT_DIFF
3203 win_T *wp;
3204
3205 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3206 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003207 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 FOR_ALL_WINDOWS(wp)
3209 {
3210 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3211 {
3212 wp->w_p_fen = curwin->w_p_fen;
3213 changed_window_setting_win(wp);
3214 }
3215 }
3216 }
3217# endif
3218 changed_window_setting();
3219 }
3220
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003221 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (old_fdl != curwin->w_p_fdl)
3223 newFoldLevel();
3224#endif
3225}
3226
3227#ifdef FEAT_GUI
3228/*
3229 * Vertical scrollbar movement.
3230 */
3231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003232nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
3234 if (cap->oap->op_type != OP_NOP)
3235 clearopbeep(cap->oap);
3236
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003237 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 gui_do_scroll();
3239}
3240
3241/*
3242 * Horizontal scrollbar movement.
3243 */
3244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003245nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 if (cap->oap->op_type != OP_NOP)
3248 clearopbeep(cap->oap);
3249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003250 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003251 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252}
3253#endif
3254
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003255#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003256/*
3257 * Click in GUI tab.
3258 */
3259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003260nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003261{
3262 if (cap->oap->op_type != OP_NOP)
3263 clearopbeep(cap->oap);
3264
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003265 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003266 goto_tabpage(current_tab);
3267}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003268
3269/*
3270 * Selected item in tab line menu.
3271 */
3272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003273nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003274{
3275 if (cap->oap->op_type != OP_NOP)
3276 clearopbeep(cap->oap);
3277
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003278 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003279 handle_tabmenu();
3280}
3281
3282/*
3283 * Handle selecting an item of the GUI tab line menu.
3284 * Used in Normal and Insert mode.
3285 */
3286 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003287handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003288{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003289 switch (current_tabmenu)
3290 {
3291 case TABLINE_MENU_CLOSE:
3292 if (current_tab == 0)
3293 do_cmdline_cmd((char_u *)"tabclose");
3294 else
3295 {
3296 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3297 current_tab);
3298 do_cmdline_cmd(IObuff);
3299 }
3300 break;
3301
3302 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003303 if (current_tab == 0)
3304 do_cmdline_cmd((char_u *)"$tabnew");
3305 else
3306 {
3307 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3308 current_tab - 1);
3309 do_cmdline_cmd(IObuff);
3310 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003311 break;
3312
3313 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003314 if (current_tab == 0)
3315 do_cmdline_cmd((char_u *)"browse $tabnew");
3316 else
3317 {
3318 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3319 current_tab - 1);
3320 do_cmdline_cmd(IObuff);
3321 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003322 break;
3323 }
3324}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003325#endif
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327/*
3328 * "Q" command.
3329 */
3330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003331nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 /*
3334 * Ignore 'Q' in Visual mode, just give a beep.
3335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003337 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003338 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 do_exmode(FALSE);
3340}
3341
3342/*
3343 * Handle a ":" command.
3344 */
3345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003346nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003348 int old_p_im;
3349 int cmd_result;
3350 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
Bram Moolenaar957cf672020-11-12 14:21:06 +01003352 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 nv_operator(cap);
3354 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
3356 if (cap->oap->op_type != OP_NOP)
3357 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003358 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 cap->oap->motion_type = MCHAR;
3360 cap->oap->inclusive = FALSE;
3361 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003362 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003364 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 stuffcharReadbuff('.');
3366 if (cap->count0 > 1)
3367 {
3368 stuffReadbuff((char_u *)",.+");
3369 stuffnumReadbuff((long)cap->count0 - 1L);
3370 }
3371 }
3372
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003373 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (KeyTyped)
3375 compute_cmdrow();
3376
3377 old_p_im = p_im;
3378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003379 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003380 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003383 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 if (p_im != old_p_im)
3385 {
3386 if (p_im)
3387 restart_edit = 'i';
3388 else
3389 restart_edit = 0;
3390 }
3391
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003392 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003393 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003394 clearop(cap->oap);
3395 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3397 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003398 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3399 || did_emsg
3400 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003401 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 clearopbeep(cap->oap);
3403 }
3404}
3405
3406/*
3407 * Handle CTRL-G command.
3408 */
3409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003410nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003412 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
3414 VIsual_select = !VIsual_select;
3415 showmode();
3416 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003417 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003418 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 fileinfo((int)cap->count0, FALSE, TRUE);
3420}
3421
3422/*
3423 * Handle CTRL-H <Backspace> command.
3424 */
3425 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003426nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if (VIsual_active && VIsual_select)
3429 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003430 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 v_visop(cap);
3432 }
3433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 nv_left(cap);
3435}
3436
3437/*
3438 * CTRL-L: clear screen and redraw.
3439 */
3440 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003441nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 if (!checkclearop(cap->oap))
3444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003446 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003447 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003448# ifdef FEAT_RELTIME
3449 {
3450 win_T *wp;
3451
3452 FOR_ALL_WINDOWS(wp)
3453 wp->w_s->b_syn_slow = FALSE;
3454 }
3455# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456#endif
3457 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003458#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3459# ifdef VIMDLL
3460 if (!gui.in_use)
3461# endif
3462 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465}
3466
3467/*
3468 * CTRL-O: In Select mode: switch to Visual mode for one command.
3469 * Otherwise: Go to older pcmark.
3470 */
3471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003472nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 if (VIsual_active && VIsual_select)
3475 {
3476 VIsual_select = FALSE;
3477 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003478 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
3482 cap->count1 = -cap->count1;
3483 nv_pcmark(cap);
3484 }
3485}
3486
3487/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003488 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3489 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 */
3491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003492nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 if (!checkclearopq(cap->oap))
3495 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3496 GETF_SETMARK|GETF_ALT, FALSE);
3497}
3498
3499/*
3500 * "Z" commands.
3501 */
3502 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003503nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 if (!checkclearopq(cap->oap))
3506 {
3507 switch (cap->nchar)
3508 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003509 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 case 'Z': do_cmdline_cmd((char_u *)"x");
3511 break;
3512
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003513 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 case 'Q': do_cmdline_cmd((char_u *)"q!");
3515 break;
3516
3517 default: clearopbeep(cap->oap);
3518 }
3519 }
3520}
3521
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522/*
3523 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3524 */
3525 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003526do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
3528 oparg_T oa;
3529 cmdarg_T ca;
3530
3531 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003532 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 ca.oap = &oa;
3534 ca.cmdchar = c1;
3535 ca.nchar = c2;
3536 nv_ident(&ca);
3537}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
3539/*
3540 * Handle the commands that use the word under the cursor.
3541 * [g] CTRL-] :ta to current identifier
3542 * [g] 'K' run program for current identifier
3543 * [g] '*' / to current identifier or string
3544 * [g] '#' ? to current identifier or string
3545 * g ']' :tselect for current identifier
3546 */
3547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003548nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
3550 char_u *ptr = NULL;
3551 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003552 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003553 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003555 char_u *kp; // value of 'keywordprg'
3556 int kp_help; // 'keywordprg' is ":he"
3557 int kp_ex; // 'keywordprg' starts with ":"
3558 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003560 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003561 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 char_u *aux_ptr;
3563 int isman;
3564 int isman_s;
3565
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003566 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568 cmdchar = cap->nchar;
3569 g_cmd = TRUE;
3570 }
3571 else
3572 {
3573 cmdchar = cap->cmdchar;
3574 g_cmd = FALSE;
3575 }
3576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003577 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 cmdchar = '#';
3579
3580 /*
3581 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3582 */
3583 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3586 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (checkclearopq(cap->oap))
3588 return;
3589 }
3590
3591 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3592 (cmdchar == '*' || cmdchar == '#')
3593 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3594 {
3595 clearop(cap->oap);
3596 return;
3597 }
3598
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003599 // Allocate buffer to put the command in. Inserting backslashes can
3600 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3601 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3603 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3604 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003605 if (kp_help && *skipwhite(ptr) == NUL)
3606 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003607 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003608 return;
3609 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003610 kp_ex = (*kp == ':');
3611 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3612 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (buf == NULL)
3614 return;
3615 buf[0] = NUL;
3616
3617 switch (cmdchar)
3618 {
3619 case '*':
3620 case '#':
3621 /*
3622 * Put cursor at start of word, makes search skip the word
3623 * under the cursor.
3624 * Call setpcmark() first, so "*``" puts the cursor back where
3625 * it was.
3626 */
3627 setpcmark();
3628 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3629
3630 if (!g_cmd && vim_iswordp(ptr))
3631 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003632 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 break;
3634
3635 case 'K':
3636 if (kp_help)
3637 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003638 else if (kp_ex)
3639 {
3640 if (cap->count0 != 0)
3641 vim_snprintf((char *)buf, buflen, "%s %ld",
3642 kp, cap->count0);
3643 else
3644 STRCPY(buf, kp);
3645 STRCAT(buf, " ");
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 else
3648 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003649 // An external command will probably use an argument starting
3650 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003651 while (*ptr == '-' && n > 0)
3652 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003653 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003654 --n;
3655 }
3656 if (n == 0)
3657 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003658 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003659 vim_free(buf);
3660 return;
3661 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003662
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003663 // When a count is given, turn it into a range. Is this
3664 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 isman = (STRCMP(kp, "man") == 0);
3666 isman_s = (STRCMP(kp, "man -s") == 0);
3667 if (cap->count0 != 0 && !(isman || isman_s))
3668 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3669
3670 STRCAT(buf, "! ");
3671 if (cap->count0 == 0 && isman_s)
3672 STRCAT(buf, "man");
3673 else
3674 STRCAT(buf, kp);
3675 STRCAT(buf, " ");
3676 if (cap->count0 != 0 && (isman || isman_s))
3677 {
3678 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3679 STRCAT(buf, " ");
3680 }
3681 }
3682 break;
3683
3684 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003685 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686#ifdef FEAT_CSCOPE
3687 if (p_cst)
3688 STRCPY(buf, "cstag ");
3689 else
3690#endif
3691 STRCPY(buf, "ts ");
3692 break;
3693
3694 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003695 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (curbuf->b_help)
3697 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003699 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003700 if (g_cmd)
3701 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003702 else if (cap->count0 == 0)
3703 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003704 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003705 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708
3709 /*
3710 * Now grab the chars in the identifier
3711 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003712 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003714 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003715 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003716 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003717 p = vim_strsave_fnameescape(ptr, FALSE);
3718 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003719 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003720 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003721 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003722 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003724 vim_free(buf);
3725 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003727 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003728 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003729 {
3730 vim_free(buf);
3731 vim_free(p);
3732 return;
3733 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003734 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003735 STRCAT(buf, p);
3736 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003738 else
3739 {
3740 if (cmdchar == '*')
3741 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
3742 else if (cmdchar == '#')
3743 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003744 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003745 {
3746 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003747 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003748 aux_ptr = (char_u *)"";
3749 else
3750 aux_ptr = (char_u *)"\\|\"\n[";
3751 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003752 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003753 aux_ptr = (char_u *)"\\|\"\n*?[";
3754
3755 p = buf + STRLEN(buf);
3756 while (n-- > 0)
3757 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003758 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003759 if (vim_strchr(aux_ptr, *ptr) != NULL)
3760 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003761 // When current byte is a part of multibyte character, copy all
3762 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003763 if (has_mbyte)
3764 {
3765 int i;
3766 int len = (*mb_ptr2len)(ptr) - 1;
3767
3768 for (i = 0; i < len && n >= 1; ++i, --n)
3769 *p++ = *ptr++;
3770 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003771 *p++ = *ptr++;
3772 }
3773 *p = NUL;
3774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
3776 /*
3777 * Execute the command.
3778 */
3779 if (cmdchar == '*' || cmdchar == '#')
3780 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003781 if (!g_cmd && (has_mbyte
3782 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3783 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003785
3786 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003787 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003789
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003790 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003793 {
3794 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003796 g_tag_at_cursor = FALSE;
3797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 vim_free(buf);
3800}
3801
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802/*
3803 * Get visually selected text, within one line only.
3804 * Returns FAIL if more than one line selected.
3805 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003806 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003807get_visual_text(
3808 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003809 char_u **pp, // return: start of selected text
3810 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812 if (VIsual_mode != 'V')
3813 unadjust_for_sel();
3814 if (VIsual.lnum != curwin->w_cursor.lnum)
3815 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003816 if (cap != NULL)
3817 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 return FAIL;
3819 }
3820 if (VIsual_mode == 'V')
3821 {
3822 *pp = ml_get_curline();
3823 *lenp = (int)STRLEN(*pp);
3824 }
3825 else
3826 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003827 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 {
3829 *pp = ml_get_pos(&curwin->w_cursor);
3830 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3831 }
3832 else
3833 {
3834 *pp = ml_get_pos(&VIsual);
3835 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003838 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003839 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841 reset_VIsual_and_resel();
3842 return OK;
3843}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
3845/*
3846 * CTRL-T: backwards in tag stack
3847 */
3848 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003849nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
3851 if (!checkclearopq(cap->oap))
3852 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3853}
3854
3855/*
3856 * Handle scrolling command 'H', 'L' and 'M'.
3857 */
3858 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003859nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
3861 int used = 0;
3862 long n;
3863#ifdef FEAT_FOLDING
3864 linenr_T lnum;
3865#endif
3866 int half;
3867
3868 cap->oap->motion_type = MLINE;
3869 setpcmark();
3870
3871 if (cap->cmdchar == 'L')
3872 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003873 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 curwin->w_cursor.lnum = curwin->w_botline - 1;
3875 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3876 curwin->w_cursor.lnum = 1;
3877 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003878 {
3879#ifdef FEAT_FOLDING
3880 if (hasAnyFolding(curwin))
3881 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003882 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003883 for (n = cap->count1 - 1; n > 0
3884 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3885 {
3886 (void)hasFolding(curwin->w_cursor.lnum,
3887 &curwin->w_cursor.lnum, NULL);
3888 --curwin->w_cursor.lnum;
3889 }
3890 }
3891 else
3892#endif
3893 curwin->w_cursor.lnum -= cap->count1 - 1;
3894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 }
3896 else
3897 {
3898 if (cap->cmdchar == 'M')
3899 {
3900#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003901 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 used -= diff_check_fill(curwin, curwin->w_topline)
3903 - curwin->w_topfill;
3904#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003905 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3907 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3908 {
3909#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003910 // Count half he number of filler lines to be "below this
3911 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3913 + n) / 2 >= half)
3914 {
3915 --n;
3916 break;
3917 }
3918#endif
3919 used += plines(curwin->w_topline + n);
3920 if (used >= half)
3921 break;
3922#ifdef FEAT_FOLDING
3923 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3924 n = lnum - curwin->w_topline;
3925#endif
3926 }
3927 if (n > 0 && used > curwin->w_height)
3928 --n;
3929 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003930 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003933#ifdef FEAT_FOLDING
3934 if (hasAnyFolding(curwin))
3935 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003936 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003937 lnum = curwin->w_topline;
3938 while (n-- > 0 && lnum < curwin->w_botline - 1)
3939 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003940 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003941 ++lnum;
3942 }
3943 n = lnum - curwin->w_topline;
3944 }
3945#endif
3946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 curwin->w_cursor.lnum = curwin->w_topline + n;
3948 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3949 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3950 }
3951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003952 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003953 if (cap->oap->op_type == OP_NOP)
3954 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 beginline(BL_SOL | BL_FIX);
3956}
3957
3958/*
3959 * Cursor right commands.
3960 */
3961 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003962nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963{
3964 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003965 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003967 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3968 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003969 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003970 if (mod_mask & MOD_MASK_CTRL)
3971 cap->arg = TRUE;
3972 nv_wordcmd(cap);
3973 return;
3974 }
3975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 cap->oap->motion_type = MCHAR;
3977 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003978 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003981 * In virtual edit mode, there's no such thing as "past_line", as lines
3982 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 */
3984 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003985 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
3987 for (n = cap->count1; n > 0; --n)
3988 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003989 if ((!past_line && oneright() == FAIL)
3990 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003991 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 {
3993 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003994 * <Space> wraps to next line if 'whichwrap' has 's'.
3995 * 'l' wraps to next line if 'whichwrap' has 'l'.
3996 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 */
3998 if ( ((cap->cmdchar == ' '
3999 && vim_strchr(p_ww, 's') != NULL)
4000 || (cap->cmdchar == 'l'
4001 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004002 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 && vim_strchr(p_ww, '>') != NULL))
4004 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4005 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004006 // When deleting we also count the NL as a character.
4007 // Set cap->oap->inclusive when last char in the line is
4008 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004009 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004011 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 cap->oap->inclusive = TRUE;
4013 else
4014 {
4015 ++curwin->w_cursor.lnum;
4016 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 curwin->w_set_curswant = TRUE;
4019 cap->oap->inclusive = FALSE;
4020 }
4021 continue;
4022 }
4023 if (cap->oap->op_type == OP_NOP)
4024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004025 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 if (n == cap->count1)
4027 beep_flush();
4028 }
4029 else
4030 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004031 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 cap->oap->inclusive = TRUE;
4033 }
4034 break;
4035 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004036 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
4038 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (virtual_active())
4040 oneright();
4041 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004044 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 ++curwin->w_cursor.col;
4047 }
4048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050#ifdef FEAT_FOLDING
4051 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4052 && cap->oap->op_type == OP_NOP)
4053 foldOpenCursor();
4054#endif
4055}
4056
4057/*
4058 * Cursor left commands.
4059 *
4060 * Returns TRUE when operator end should not be adjusted.
4061 */
4062 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004063nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064{
4065 long n;
4066
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004067 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4068 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004069 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004070 if (mod_mask & MOD_MASK_CTRL)
4071 cap->arg = 1;
4072 nv_bck_word(cap);
4073 return;
4074 }
4075
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 cap->oap->motion_type = MCHAR;
4077 cap->oap->inclusive = FALSE;
4078 for (n = cap->count1; n > 0; --n)
4079 {
4080 if (oneleft() == FAIL)
4081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004082 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4083 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4084 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 if ( (((cap->cmdchar == K_BS
4086 || cap->cmdchar == Ctrl_H)
4087 && vim_strchr(p_ww, 'b') != NULL)
4088 || (cap->cmdchar == 'h'
4089 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004090 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 && vim_strchr(p_ww, '<') != NULL))
4092 && curwin->w_cursor.lnum > 1)
4093 {
4094 --(curwin->w_cursor.lnum);
4095 coladvance((colnr_T)MAXCOL);
4096 curwin->w_set_curswant = TRUE;
4097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004098 // When the NL before the first char has to be deleted we
4099 // put the cursor on the NUL after the previous line.
4100 // This is a very special case, be careful!
4101 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if ( (cap->oap->op_type == OP_DELETE
4103 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004104 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004106 char_u *cp = ml_get_cursor();
4107
4108 if (*cp != NUL)
4109 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004110 if (has_mbyte)
4111 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4112 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004113 ++curwin->w_cursor.col;
4114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 cap->retval |= CA_NO_ADJ_OP_END;
4116 }
4117 continue;
4118 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004119 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4121 beep_flush();
4122 break;
4123 }
4124 }
4125#ifdef FEAT_FOLDING
4126 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4127 && cap->oap->op_type == OP_NOP)
4128 foldOpenCursor();
4129#endif
4130}
4131
4132/*
4133 * Cursor up commands.
4134 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4135 */
4136 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004137nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004139 if (mod_mask & MOD_MASK_SHIFT)
4140 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004141 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004142 cap->arg = BACKWARD;
4143 nv_page(cap);
4144 }
4145 else
4146 {
4147 cap->oap->motion_type = MLINE;
4148 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4149 clearopbeep(cap->oap);
4150 else if (cap->arg)
4151 beginline(BL_WHITE | BL_FIX);
4152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153}
4154
4155/*
4156 * Cursor down commands.
4157 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4158 */
4159 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004160nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004162 if (mod_mask & MOD_MASK_SHIFT)
4163 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004164 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004165 cap->arg = FORWARD;
4166 nv_page(cap);
4167 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004168#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004169 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004170 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4171 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004176 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004177 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 cmdwin_result = CAR;
4179 else
4180#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004181#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004182 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004183 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4184 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4185 {
4186 invoke_prompt_callback();
4187 if (restart_edit == 0)
4188 restart_edit = 'a';
4189 }
4190 else
4191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
4193 cap->oap->motion_type = MLINE;
4194 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4195 clearopbeep(cap->oap);
4196 else if (cap->arg)
4197 beginline(BL_WHITE | BL_FIX);
4198 }
4199 }
4200}
4201
4202#ifdef FEAT_SEARCHPATH
4203/*
4204 * Grab the file name under the cursor and edit it.
4205 */
4206 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004207nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004210 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004212 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
4214 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004215 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return;
4217 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004218 if (curbuf_locked())
4219 {
4220 clearop(cap->oap);
4221 return;
4222 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004223#ifdef FEAT_PROP_POPUP
4224 if (ERROR_IF_TERM_POPUP_WINDOW)
4225 return;
4226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004228 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230 if (ptr != NULL)
4231 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004232 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004233 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004234 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004236 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004237 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004238 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004239 {
4240 curwin->w_cursor.lnum = lnum;
4241 check_cursor_lnum();
4242 beginline(BL_SOL | BL_FIX);
4243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 vim_free(ptr);
4245 }
4246 else
4247 clearop(cap->oap);
4248}
4249#endif
4250
4251/*
4252 * <End> command: to end of current line or last line.
4253 */
4254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004255nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004257 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004259 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004261 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 nv_dollar(cap);
4264}
4265
4266/*
4267 * Handle the "$" command.
4268 */
4269 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004270nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271{
4272 cap->oap->motion_type = MCHAR;
4273 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004274 // In virtual mode when off the edge of a line and an operator
4275 // is pending (whew!) keep the cursor where it is.
4276 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if (!virtual_active() || gchar_cursor() != NUL
4278 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004279 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 if (cursor_down((long)(cap->count1 - 1),
4281 cap->oap->op_type == OP_NOP) == FAIL)
4282 clearopbeep(cap->oap);
4283#ifdef FEAT_FOLDING
4284 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4285 foldOpenCursor();
4286#endif
4287}
4288
4289/*
4290 * Implementation of '?' and '/' commands.
4291 * If cap->arg is TRUE don't set PC mark.
4292 */
4293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004294nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
4296 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004297 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4300 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004301 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 cap->cmdchar = 'g';
4303 cap->nchar = '?';
4304 nv_operator(cap);
4305 return;
4306 }
4307
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004308 // When using 'incsearch' the cursor may be moved to set a different search
4309 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004310 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 if (cap->searchbuf == NULL)
4313 {
4314 clearop(oap);
4315 return;
4316 }
4317
Bram Moolenaar46539112015-02-17 15:43:57 +01004318 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004319 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004320 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321}
4322
4323/*
4324 * Handle "N" and "n" commands.
4325 * cap->arg is SEARCH_REV for "N", 0 for "n".
4326 */
4327 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004328nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004330 pos_T old = curwin->w_cursor;
4331 int wrapped = FALSE;
4332 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004333
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004334 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004336 // Avoid getting stuck on the current cursor position, which can
4337 // happen when an offset is given and the cursor is on the last char
4338 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004339 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004340 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004341 cap->count1 -= 1;
4342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343}
4344
4345/*
4346 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4347 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004348 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004350 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004351normal_search(
4352 cmdarg_T *cap,
4353 int dir,
4354 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004355 int opt, // extra flags for do_search()
4356 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
4358 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004359 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 cap->oap->motion_type = MCHAR;
4362 cap->oap->inclusive = FALSE;
4363 cap->oap->use_reg_one = TRUE;
4364 curwin->w_set_curswant = TRUE;
4365
Bram Moolenaara80faa82020-04-12 19:37:17 +02004366 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004367 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004368 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4369 if (wrapped != NULL)
4370 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 if (i == 0)
4372 clearop(cap->oap);
4373 else
4374 {
4375 if (i == 2)
4376 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378#ifdef FEAT_FOLDING
4379 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4380 foldOpenCursor();
4381#endif
4382 }
4383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004384 // "/$" will put the cursor after the end of the line, may need to
4385 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004387 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388}
4389
4390/*
4391 * Character search commands.
4392 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4393 * ',' and FALSE for ';'.
4394 * cap->nchar is NUL for ',' and ';' (repeat the search)
4395 */
4396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004397nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
4399 int t_cmd;
4400
4401 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4402 t_cmd = TRUE;
4403 else
4404 t_cmd = FALSE;
4405
4406 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4408 clearopbeep(cap->oap);
4409 else
4410 {
4411 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004412 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4414 && (t_cmd || cap->oap->op_type != OP_NOP))
4415 {
4416 colnr_T scol, ecol;
4417
4418 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4419 curwin->w_cursor.coladd = ecol - scol;
4420 }
4421 else
4422 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424#ifdef FEAT_FOLDING
4425 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4426 foldOpenCursor();
4427#endif
4428 }
4429}
4430
4431/*
4432 * "[" and "]" commands.
4433 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4434 */
4435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004436nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004438 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004440 pos_T *pos = NULL; // init for GCC
4441 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 int flag;
4443 long n;
4444 int findc;
4445 int c;
4446
4447 cap->oap->motion_type = MCHAR;
4448 cap->oap->inclusive = FALSE;
4449 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004450 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
4452#ifdef FEAT_SEARCHPATH
4453 /*
4454 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4455 */
4456 if (cap->nchar == 'f')
4457 nv_gotofile(cap);
4458 else
4459#endif
4460
4461#ifdef FEAT_FIND_ID
4462 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004463 * Find the occurrence(s) of the identifier or define under cursor
4464 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 *
4466 * search list jump
4467 * fwd bwd fwd bwd fwd bwd
4468 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4469 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4470 */
4471 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004472# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004474# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 cap->nchar) != NULL)
4478 {
4479 char_u *ptr;
4480 int len;
4481
4482 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4483 clearop(cap->oap);
4484 else
4485 {
4486 find_pattern_in_path(ptr, 0, len, TRUE,
4487 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4488 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4489 cap->count1,
4490 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4491 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4492 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4493 (linenr_T)MAXLNUM);
4494 curwin->w_set_curswant = TRUE;
4495 }
4496 }
4497 else
4498#endif
4499
4500 /*
4501 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4502 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4503 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4504 * "[m" or "]m" search for prev/next start of (Java) method.
4505 * "[M" or "]M" search for prev/next end of (Java) method.
4506 */
4507 if ( (cap->cmdchar == '['
4508 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4509 || (cap->cmdchar == ']'
4510 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4511 {
4512 if (cap->nchar == '*')
4513 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 prev_pos.lnum = 0;
4515 if (cap->nchar == 'm' || cap->nchar == 'M')
4516 {
4517 if (cap->cmdchar == '[')
4518 findc = '{';
4519 else
4520 findc = '}';
4521 n = 9999;
4522 }
4523 else
4524 {
4525 findc = cap->nchar;
4526 n = cap->count1;
4527 }
4528 for ( ; n > 0; --n)
4529 {
4530 if ((pos = findmatchlimit(cap->oap, findc,
4531 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4532 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004533 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
4535 if (cap->nchar != 'm' && cap->nchar != 'M')
4536 clearopbeep(cap->oap);
4537 }
4538 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004539 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 break;
4541 }
4542 prev_pos = new_pos;
4543 curwin->w_cursor = *pos;
4544 new_pos = *pos;
4545 }
4546 curwin->w_cursor = old_pos;
4547
4548 /*
4549 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4550 * brought us to the match for "[m" and "]M" when inside a method.
4551 * Try finding the '{' or '}' we want to be at.
4552 * Also repeat for the given count.
4553 */
4554 if (cap->nchar == 'm' || cap->nchar == 'M')
4555 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004556 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 int norm = ((findc == '{') == (cap->nchar == 'm'));
4558
4559 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004560 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 if (prev_pos.lnum != 0)
4562 {
4563 pos = &prev_pos;
4564 curwin->w_cursor = prev_pos;
4565 if (norm)
4566 --n;
4567 }
4568 else
4569 pos = NULL;
4570 while (n > 0)
4571 {
4572 for (;;)
4573 {
4574 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4575 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004576 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (pos == NULL)
4578 clearopbeep(cap->oap);
4579 n = 0;
4580 break;
4581 }
4582 c = gchar_cursor();
4583 if (c == '{' || c == '}')
4584 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004585 // Must have found end/start of class: use it.
4586 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 if ((c == findc && norm) || (n == 1 && !norm))
4588 {
4589 new_pos = curwin->w_cursor;
4590 pos = &new_pos;
4591 n = 0;
4592 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004593 // if no match found at all, we started outside of the
4594 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 else if (new_pos.lnum == 0)
4596 {
4597 new_pos = curwin->w_cursor;
4598 pos = &new_pos;
4599 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004600 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 else if ((pos = findmatchlimit(cap->oap, findc,
4602 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4603 0)) == NULL)
4604 n = 0;
4605 else
4606 curwin->w_cursor = *pos;
4607 break;
4608 }
4609 }
4610 --n;
4611 }
4612 curwin->w_cursor = old_pos;
4613 if (pos == NULL && new_pos.lnum != 0)
4614 clearopbeep(cap->oap);
4615 }
4616 if (pos != NULL)
4617 {
4618 setpcmark();
4619 curwin->w_cursor = *pos;
4620 curwin->w_set_curswant = TRUE;
4621#ifdef FEAT_FOLDING
4622 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4623 && cap->oap->op_type == OP_NOP)
4624 foldOpenCursor();
4625#endif
4626 }
4627 }
4628
4629 /*
4630 * "[[", "[]", "]]" and "][": move to start or end of function
4631 */
4632 else if (cap->nchar == '[' || cap->nchar == ']')
4633 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004634 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 flag = '{';
4636 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004637 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
4639 curwin->w_set_curswant = TRUE;
4640 /*
4641 * Imitate strange Vi behaviour: When using "]]" with an operator
4642 * we also stop at '}'.
4643 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004644 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 (cap->oap->op_type != OP_NOP
4646 && cap->arg == FORWARD && flag == '{')))
4647 clearopbeep(cap->oap);
4648 else
4649 {
4650 if (cap->oap->op_type == OP_NOP)
4651 beginline(BL_WHITE | BL_FIX);
4652#ifdef FEAT_FOLDING
4653 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4654 foldOpenCursor();
4655#endif
4656 }
4657 }
4658
4659 /*
4660 * "[p", "[P", "]P" and "]p": put with indent adjustment
4661 */
4662 else if (cap->nchar == 'p' || cap->nchar == 'P')
4663 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004664 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
4666
4667 /*
4668 * "['", "[`", "]'" and "]`": jump to next mark
4669 */
4670 else if (cap->nchar == '\'' || cap->nchar == '`')
4671 {
4672 pos = &curwin->w_cursor;
4673 for (n = cap->count1; n > 0; --n)
4674 {
4675 prev_pos = *pos;
4676 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4677 cap->nchar == '\'');
4678 if (pos == NULL)
4679 break;
4680 }
4681 if (pos == NULL)
4682 pos = &prev_pos;
4683 nv_cursormark(cap, cap->nchar == '\'', pos);
4684 }
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 /*
4687 * [ or ] followed by a middle mouse click: put selected text with
4688 * indent adjustment. Any other button just does as usual.
4689 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004690 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
4692 (void)do_mouse(cap->oap, cap->nchar,
4693 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4694 cap->count1, PUT_FIXINDENT);
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
4697#ifdef FEAT_FOLDING
4698 /*
4699 * "[z" and "]z": move to start or end of open fold.
4700 */
4701 else if (cap->nchar == 'z')
4702 {
4703 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4704 cap->count1) == FAIL)
4705 clearopbeep(cap->oap);
4706 }
4707#endif
4708
4709#ifdef FEAT_DIFF
4710 /*
4711 * "[c" and "]c": move to next or previous diff-change.
4712 */
4713 else if (cap->nchar == 'c')
4714 {
4715 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4716 cap->count1) == FAIL)
4717 clearopbeep(cap->oap);
4718 }
4719#endif
4720
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004721#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004722 /*
4723 * "[s", "[S", "]s" and "]S": move to next spell error.
4724 */
4725 else if (cap->nchar == 's' || cap->nchar == 'S')
4726 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004727 setpcmark();
4728 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004729 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4730 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004731 {
4732 clearopbeep(cap->oap);
4733 break;
4734 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004735 else
4736 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004737# ifdef FEAT_FOLDING
4738 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4739 foldOpenCursor();
4740# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004741 }
4742#endif
4743
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004744 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 else
4746 clearopbeep(cap->oap);
4747}
4748
4749/*
4750 * Handle Normal mode "%" command.
4751 */
4752 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004753nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004756#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 linenr_T lnum = curwin->w_cursor.lnum;
4758#endif
4759
4760 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004761 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
4763 if (cap->count0 > 100)
4764 clearopbeep(cap->oap);
4765 else
4766 {
4767 cap->oap->motion_type = MLINE;
4768 setpcmark();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004769 // Round up, so CTRL-G will give same value. Watch out for a
4770 // large line count, the line number must not go negative!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (curbuf->b_ml.ml_line_count > 1000000)
4772 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4773 / 100L * cap->count0;
4774 else
4775 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4776 cap->count0 + 99L) / 100L;
4777 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4778 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4779 beginline(BL_SOL | BL_FIX);
4780 }
4781 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004782 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
4784 cap->oap->motion_type = MCHAR;
4785 cap->oap->use_reg_one = TRUE;
4786 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4787 clearopbeep(cap->oap);
4788 else
4789 {
4790 setpcmark();
4791 curwin->w_cursor = *pos;
4792 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796 }
4797#ifdef FEAT_FOLDING
4798 if (cap->oap->op_type == OP_NOP
4799 && lnum != curwin->w_cursor.lnum
4800 && (fdo_flags & FDO_PERCENT)
4801 && KeyTyped)
4802 foldOpenCursor();
4803#endif
4804}
4805
4806/*
4807 * Handle "(" and ")" commands.
4808 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4809 */
4810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004811nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
4813 cap->oap->motion_type = MCHAR;
4814 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004815 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004816 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 curwin->w_set_curswant = TRUE;
4818
4819 if (findsent(cap->arg, cap->count1) == FAIL)
4820 clearopbeep(cap->oap);
4821 else
4822 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004823 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004824 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826#ifdef FEAT_FOLDING
4827 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4828 foldOpenCursor();
4829#endif
4830 }
4831}
4832
4833/*
4834 * "m" command: Mark a position.
4835 */
4836 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004837nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
4839 if (!checkclearop(cap->oap))
4840 {
4841 if (setmark(cap->nchar) == FAIL)
4842 clearopbeep(cap->oap);
4843 }
4844}
4845
4846/*
4847 * "{" and "}" commands.
4848 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4849 */
4850 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004851nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852{
4853 cap->oap->motion_type = MCHAR;
4854 cap->oap->inclusive = FALSE;
4855 cap->oap->use_reg_one = TRUE;
4856 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004857 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 clearopbeep(cap->oap);
4859 else
4860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#ifdef FEAT_FOLDING
4863 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4864 foldOpenCursor();
4865#endif
4866 }
4867}
4868
4869/*
4870 * "u" command: Undo or make lower case.
4871 */
4872 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004873nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004875 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004877 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 cap->cmdchar = 'g';
4879 cap->nchar = 'u';
4880 nv_operator(cap);
4881 }
4882 else
4883 nv_kundo(cap);
4884}
4885
4886/*
4887 * <Undo> command.
4888 */
4889 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004890nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
4892 if (!checkclearopq(cap->oap))
4893 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004894#ifdef FEAT_JOB_CHANNEL
4895 if (bt_prompt(curbuf))
4896 {
4897 clearopbeep(cap->oap);
4898 return;
4899 }
4900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 u_undo((int)cap->count1);
4902 curwin->w_set_curswant = TRUE;
4903 }
4904}
4905
4906/*
4907 * Handle the "r" command.
4908 */
4909 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004910nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911{
4912 char_u *ptr;
4913 int had_ctrl_v;
4914 long n;
4915
4916 if (checkclearop(cap->oap))
4917 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004918#ifdef FEAT_JOB_CHANNEL
4919 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4920 {
4921 clearopbeep(cap->oap);
4922 return;
4923 }
4924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004926 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (cap->nchar == Ctrl_V)
4928 {
4929 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004930 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004931 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (cap->nchar > DEL)
4933 had_ctrl_v = NUL;
4934 }
4935 else
4936 had_ctrl_v = NUL;
4937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004938 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004939 if (IS_SPECIAL(cap->nchar))
4940 {
4941 clearopbeep(cap->oap);
4942 return;
4943 }
4944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004945 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 if (VIsual_active)
4947 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004948 if (got_int)
4949 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004950 if (had_ctrl_v)
4951 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 // Use a special (negative) number to make a difference between a
4953 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004954 if (cap->nchar == CAR)
4955 cap->nchar = REPLACE_CR_NCHAR;
4956 else if (cap->nchar == NL)
4957 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 nv_operator(cap);
4960 return;
4961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004963 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 if (virtual_active())
4965 {
4966 if (u_save_cursor() == FAIL)
4967 return;
4968 if (gchar_cursor() == NUL)
4969 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004970 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 coladvance_force((colnr_T)(getviscol() + cap->count1));
4972 curwin->w_cursor.col -= cap->count1;
4973 }
4974 else if (gchar_cursor() == TAB)
4975 coladvance_force(getviscol());
4976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004978 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004980 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004981 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 clearopbeep(cap->oap);
4984 return;
4985 }
4986
4987 /*
4988 * Replacing with a TAB is done by edit() when it is complicated because
4989 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4990 * Other characters are done below to avoid problems with things like
4991 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
4992 */
4993 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4994 {
4995 stuffnumReadbuff(cap->count1);
4996 stuffcharReadbuff('R');
4997 stuffcharReadbuff('\t');
4998 stuffcharReadbuff(ESC);
4999 return;
5000 }
5001
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005002 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (u_save_cursor() == FAIL)
5004 return;
5005
5006 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5007 {
5008 /*
5009 * Replace character(s) by a single newline.
5010 * Strange vi behaviour: Only one newline is inserted.
5011 * Delete the characters here.
5012 * Insert the newline with an insert command, takes care of
5013 * autoindent. The insert command depends on being on the last
5014 * character of a line or not.
5015 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005016 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 stuffcharReadbuff('\r');
5018 stuffcharReadbuff(ESC);
5019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005020 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 invoke_edit(cap, TRUE, 'r', FALSE);
5022 }
5023 else
5024 {
5025 prep_redo(cap->oap->regname, cap->count1,
5026 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5027
5028 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (has_mbyte)
5030 {
5031 int old_State = State;
5032
5033 if (cap->ncharC1 != 0)
5034 AppendCharToRedobuff(cap->ncharC1);
5035 if (cap->ncharC2 != 0)
5036 AppendCharToRedobuff(cap->ncharC2);
5037
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005038 // This is slow, but it handles replacing a single-byte with a
5039 // multi-byte and the other way around. Also handles adding
5040 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 for (n = cap->count1; n > 0; --n)
5042 {
5043 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005044 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5045 {
5046 int c = ins_copychar(curwin->w_cursor.lnum
5047 + (cap->nchar == Ctrl_Y ? -1 : 1));
5048 if (c != NUL)
5049 ins_char(c);
5050 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005051 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005052 ++curwin->w_cursor.col;
5053 }
5054 else
5055 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 State = old_State;
5057 if (cap->ncharC1 != 0)
5058 ins_char(cap->ncharC1);
5059 if (cap->ncharC2 != 0)
5060 ins_char(cap->ncharC2);
5061 }
5062 }
5063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 /*
5066 * Replace the characters within one line.
5067 */
5068 for (n = cap->count1; n > 0; --n)
5069 {
5070 /*
5071 * Get ptr again, because u_save and/or showmatch() will have
5072 * released the line. At the same time we let know that the
5073 * line will be changed.
5074 */
5075 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005076 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5077 {
5078 int c = ins_copychar(curwin->w_cursor.lnum
5079 + (cap->nchar == Ctrl_Y ? -1 : 1));
5080 if (c != NUL)
5081 ptr[curwin->w_cursor.col] = c;
5082 }
5083 else
5084 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (p_sm && msg_silent == 0)
5086 showmatch(cap->nchar);
5087 ++curwin->w_cursor.col;
5088 }
5089#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005090 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005092 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093
Bram Moolenaar009b2592004-10-24 19:18:58 +00005094 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005095 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005097 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099#endif
5100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005101 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 changed_bytes(curwin->w_cursor.lnum,
5103 (colnr_T)(curwin->w_cursor.col - cap->count1));
5104 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005105 --curwin->w_cursor.col; // cursor on the last replaced char
5106 // if the character on the left of the current cursor is a multi-byte
5107 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (has_mbyte)
5109 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 curbuf->b_op_end = curwin->w_cursor;
5111 curwin->w_set_curswant = TRUE;
5112 set_last_insert(cap->nchar);
5113 }
5114}
5115
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116/*
5117 * 'o': Exchange start and end of Visual area.
5118 * 'O': same, but in block mode exchange left and right corners.
5119 */
5120 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005121v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122{
5123 pos_T old_cursor;
5124 colnr_T left, right;
5125
5126 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5127 {
5128 old_cursor = curwin->w_cursor;
5129 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5130 curwin->w_cursor.lnum = VIsual.lnum;
5131 coladvance(left);
5132 VIsual = curwin->w_cursor;
5133
5134 curwin->w_cursor.lnum = old_cursor.lnum;
5135 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005136 // 'selection "exclusive" and cursor at right-bottom corner: move it
5137 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5139 ++curwin->w_curswant;
5140 coladvance(curwin->w_curswant);
5141 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005143 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 curwin->w_cursor.lnum = VIsual.lnum;
5146 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5147 ++right;
5148 coladvance(right);
5149 VIsual = curwin->w_cursor;
5150
5151 curwin->w_cursor.lnum = old_cursor.lnum;
5152 coladvance(left);
5153 curwin->w_curswant = left;
5154 }
5155 }
5156 else
5157 {
5158 old_cursor = curwin->w_cursor;
5159 curwin->w_cursor = VIsual;
5160 VIsual = old_cursor;
5161 curwin->w_set_curswant = TRUE;
5162 }
5163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
5165/*
5166 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5167 */
5168 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005169nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005171 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
5173 cap->cmdchar = 'c';
5174 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005175 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 VIsual_mode = 'V';
5177 nv_operator(cap);
5178 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005179 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
5181 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005182 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 else
5184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (virtual_active())
5186 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5188 }
5189 }
5190}
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192/*
5193 * "gr".
5194 */
5195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005196nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (VIsual_active)
5199 {
5200 cap->cmdchar = 'r';
5201 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005202 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005204 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
5206 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005207 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 else
5209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005210 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005211 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 stuffcharReadbuff(cap->extra_char);
5213 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (virtual_active())
5215 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 invoke_edit(cap, TRUE, 'v', FALSE);
5217 }
5218 }
5219}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
5221/*
5222 * Swap case for "~" command, when it does not work like an operator.
5223 */
5224 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005225n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226{
5227 long n;
5228 pos_T startpos;
5229 int did_change = 0;
5230#ifdef FEAT_NETBEANS_INTG
5231 pos_T pos;
5232 char_u *ptr;
5233 int count;
5234#endif
5235
5236 if (checkclearopq(cap->oap))
5237 return;
5238
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005239 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 clearopbeep(cap->oap);
5242 return;
5243 }
5244
5245 prep_redo_cmd(cap);
5246
5247 if (u_save_cursor() == FAIL)
5248 return;
5249
5250 startpos = curwin->w_cursor;
5251#ifdef FEAT_NETBEANS_INTG
5252 pos = startpos;
5253#endif
5254 for (n = cap->count1; n > 0; --n)
5255 {
5256 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5257 inc_cursor();
5258 if (gchar_cursor() == NUL)
5259 {
5260 if (vim_strchr(p_ww, '~') != NULL
5261 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5262 {
5263#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005264 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
5266 if (did_change)
5267 {
5268 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005269 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005270 netbeans_removed(curbuf, pos.lnum, pos.col,
5271 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005273 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275 pos.col = 0;
5276 pos.lnum++;
5277 }
5278#endif
5279 ++curwin->w_cursor.lnum;
5280 curwin->w_cursor.col = 0;
5281 if (n > 1)
5282 {
5283 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5284 break;
5285 u_clearline();
5286 }
5287 }
5288 else
5289 break;
5290 }
5291 }
5292#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005293 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
5295 ptr = ml_get(pos.lnum);
5296 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005297 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5298 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300#endif
5301
5302
5303 check_cursor();
5304 curwin->w_set_curswant = TRUE;
5305 if (did_change)
5306 {
5307 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5308 0L);
5309 curbuf->b_op_start = startpos;
5310 curbuf->b_op_end = curwin->w_cursor;
5311 if (curbuf->b_op_end.col > 0)
5312 --curbuf->b_op_end.col;
5313 }
5314}
5315
5316/*
5317 * Move cursor to mark.
5318 */
5319 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005320nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
5322 if (check_mark(pos) == FAIL)
5323 clearop(cap->oap);
5324 else
5325 {
5326 if (cap->cmdchar == '\''
5327 || cap->cmdchar == '`'
5328 || cap->cmdchar == '['
5329 || cap->cmdchar == ']')
5330 setpcmark();
5331 curwin->w_cursor = *pos;
5332 if (flag)
5333 beginline(BL_WHITE | BL_FIX);
5334 else
5335 check_cursor();
5336 }
5337 cap->oap->motion_type = flag ? MLINE : MCHAR;
5338 if (cap->cmdchar == '`')
5339 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005340 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 curwin->w_set_curswant = TRUE;
5342}
5343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344/*
5345 * Handle commands that are operators in Visual mode.
5346 */
5347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005348v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5351
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005352 // Uppercase means linewise, except in block mode, then "D" deletes till
5353 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (isupper(cap->cmdchar))
5355 {
5356 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005357 {
5358 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5362 curwin->w_curswant = MAXCOL;
5363 }
5364 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5365 nv_operator(cap);
5366}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367
5368/*
5369 * "s" and "S" commands.
5370 */
5371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005372nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005374#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005375 // When showing output of term_dumpdiff() swap the top and botom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005376 if (term_swap_diff() == OK)
5377 return;
5378#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005379#ifdef FEAT_JOB_CHANNEL
5380 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5381 {
5382 clearopbeep(cap->oap);
5383 return;
5384 }
5385#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005386 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
5388 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005389 {
5390 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 cap->cmdchar = 'c';
5394 nv_operator(cap);
5395 }
5396 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 nv_optrans(cap);
5398}
5399
5400/*
5401 * Abbreviated commands.
5402 */
5403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005404nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005407 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005409 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 if (VIsual_active)
5411 v_visop(cap);
5412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 nv_optrans(cap);
5414}
5415
5416/*
5417 * Translate a command into another command.
5418 */
5419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005420nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
5422 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5423 (char_u *)"d$", (char_u *)"c$",
5424 (char_u *)"cl", (char_u *)"cc",
5425 (char_u *)"yy", (char_u *)":s\r"};
5426 static char_u *str = (char_u *)"xXDCsSY&";
5427
5428 if (!checkclearopq(cap->oap))
5429 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005430 // In Vi "2D" doesn't delete the next line. Can't translate it
5431 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005432 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5433 {
5434 cap->oap->start = curwin->w_cursor;
5435 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005436#ifdef FEAT_EVAL
5437 set_op_var(OP_DELETE);
5438#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005439 cap->count1 = 1;
5440 nv_dollar(cap);
5441 finish_op = TRUE;
5442 ResetRedobuff();
5443 AppendCharToRedobuff('D');
5444 }
5445 else
5446 {
5447 if (cap->count0)
5448 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005449 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
5452 cap->opcount = 0;
5453}
5454
5455/*
5456 * "'" and "`" commands. Also for "g'" and "g`".
5457 * cap->arg is TRUE for "'" and "g'".
5458 */
5459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005460nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
5462 pos_T *pos;
5463 int c;
5464#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005465 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005466 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467#endif
5468
5469 if (cap->cmdchar == 'g')
5470 c = cap->extra_char;
5471 else
5472 c = cap->nchar;
5473 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005474 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
5476 if (cap->arg)
5477 {
5478 check_cursor_lnum();
5479 beginline(BL_WHITE | BL_FIX);
5480 }
5481 else
5482 check_cursor();
5483 }
5484 else
5485 nv_cursormark(cap, cap->arg, pos);
5486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005487 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 if (!virtual_active())
5489 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005490 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491#ifdef FEAT_FOLDING
5492 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005493 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005494 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 && (fdo_flags & FDO_MARK)
5496 && old_KeyTyped)
5497 foldOpenCursor();
5498#endif
5499}
5500
5501/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005502 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 */
5504 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005505nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507#ifdef FEAT_JUMPLIST
5508 pos_T *pos;
5509# ifdef FEAT_FOLDING
5510 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005511 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512# endif
5513
5514 if (!checkclearopq(cap->oap))
5515 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005516 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5517 {
5518 if (goto_tabpage_lastused() == FAIL)
5519 clearopbeep(cap->oap);
5520 return;
5521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 if (cap->cmdchar == 'g')
5523 pos = movechangelist((int)cap->count1);
5524 else
5525 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005526 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 {
5528 curwin->w_set_curswant = TRUE;
5529 check_cursor();
5530 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005531 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 nv_cursormark(cap, FALSE, pos);
5533 else if (cap->cmdchar == 'g')
5534 {
5535 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005536 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005538 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005540 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542 else
5543 clearopbeep(cap->oap);
5544# ifdef FEAT_FOLDING
5545 if (cap->oap->op_type == OP_NOP
5546 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5547 && (fdo_flags & FDO_MARK)
5548 && old_KeyTyped)
5549 foldOpenCursor();
5550# endif
5551 }
5552#else
5553 clearopbeep(cap->oap);
5554#endif
5555}
5556
5557/*
5558 * Handle '"' command.
5559 */
5560 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005561nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563 if (checkclearop(cap->oap))
5564 return;
5565#ifdef FEAT_EVAL
5566 if (cap->nchar == '=')
5567 cap->nchar = get_expr_register();
5568#endif
5569 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5570 {
5571 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005572 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573#ifdef FEAT_EVAL
5574 set_reg_var(cap->oap->regname);
5575#endif
5576 }
5577 else
5578 clearopbeep(cap->oap);
5579}
5580
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581/*
5582 * Handle "v", "V" and "CTRL-V" commands.
5583 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5584 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005585 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 */
5587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005588nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005590 if (cap->cmdchar == Ctrl_Q)
5591 cap->cmdchar = Ctrl_V;
5592
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005593 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5594 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 if (cap->oap->op_type != OP_NOP)
5596 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005597 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005598 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 return;
5600 }
5601
5602 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005605 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005607 else // toggle char/block mode
5608 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 VIsual_mode = cap->cmdchar;
5610 showmode();
5611 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005612 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005614 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 {
5616 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005617 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005619 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 VIsual = curwin->w_cursor;
5621
5622 VIsual_active = TRUE;
5623 VIsual_reselect = TRUE;
5624 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005625 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005628 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005629 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 /*
5631 * For V and ^V, we multiply the number of lines even if there
5632 * was only one -- webb
5633 */
5634 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5635 {
5636 curwin->w_cursor.lnum +=
5637 resel_VIsual_line_count * cap->count0 - 1;
5638 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5639 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5640 }
5641 VIsual_mode = resel_VIsual_mode;
5642 if (VIsual_mode == 'v')
5643 {
5644 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005645 {
5646 validate_virtcol();
5647 curwin->w_curswant = curwin->w_virtcol
5648 + resel_VIsual_vcol * cap->count0 - 1;
5649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005651 curwin->w_curswant = resel_VIsual_vcol;
5652 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005654 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 curwin->w_curswant = MAXCOL;
5657 coladvance((colnr_T)MAXCOL);
5658 }
5659 else if (VIsual_mode == Ctrl_V)
5660 {
5661 validate_virtcol();
5662 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005663 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 coladvance(curwin->w_curswant);
5665 }
5666 else
5667 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005668 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670 else
5671 {
5672 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005673 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 may_start_select('c');
5675 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005676 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005677 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005678 if (cap->count0 > 0 && --cap->count1 > 0)
5679 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005680 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005681 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5682 nv_right(cap);
5683 else if (VIsual_mode == 'V')
5684 nv_down(cap);
5685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 }
5688}
5689
5690/*
5691 * Start selection for Shift-movement keys.
5692 */
5693 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005694start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005696 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 may_start_select('k');
5698 n_start_visual_mode('v');
5699}
5700
5701/*
5702 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5703 */
5704 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005705may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
5707 VIsual_select = (stuff_empty() && typebuf_typed()
5708 && (vim_strchr(p_slm, c) != NULL));
5709}
5710
5711/*
5712 * Start Visual mode "c".
5713 * Should set VIsual_select before calling this.
5714 */
5715 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005716n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005718#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005719 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005720 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005721#endif
5722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 VIsual_mode = c;
5724 VIsual_active = TRUE;
5725 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005726
5727 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005728 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005730 {
5731 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 VIsual = curwin->w_cursor;
5735
5736#ifdef FEAT_FOLDING
5737 foldAdjustVisual();
5738#endif
5739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005741#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005742 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005743 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005744#endif
5745
Bram Moolenaar09df3122006-01-23 22:23:09 +00005746 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005747 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005749 // Make sure the clipboard gets updated. Needed because start and
5750 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 clip_star.vmode = NUL;
5752#endif
5753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005754 // Only need to redraw this line, unless still need to redraw an old
5755 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (curwin->w_redr_type < INVERTED)
5757 {
5758 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5759 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5760 }
5761}
5762
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
5764/*
5765 * CTRL-W: Window commands
5766 */
5767 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005768nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005770 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005772 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005773 cap->cmdchar = ':';
5774 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005775 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005776 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005777 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005778 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779}
5780
5781/*
5782 * CTRL-Z: Suspend
5783 */
5784 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005785nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005789 end_visual_mode(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 do_cmdline_cmd((char_u *)"st");
5791}
5792
5793/*
5794 * Commands starting with "g".
5795 */
5796 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005797nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 int i;
5802 int flag = FALSE;
5803
5804 switch (cap->nchar)
5805 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005806 case Ctrl_A:
5807 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808#ifdef MEM_PROFILE
5809 /*
5810 * "g^A": dump log of used memory.
5811 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005812 if (!VIsual_active && cap->nchar == Ctrl_A)
5813 vim_mem_profile_dump();
5814 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005816 /*
5817 * "g^A/g^X": sequentially increment visually selected region
5818 */
5819 if (VIsual_active)
5820 {
5821 cap->arg = TRUE;
5822 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005823 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005824 nv_addsub(cap);
5825 }
5826 else
5827 clearopbeep(oap);
5828 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 /*
5831 * "gR": Enter virtual replace mode.
5832 */
5833 case 'R':
5834 cap->arg = TRUE;
5835 nv_Replace(cap);
5836 break;
5837
5838 case 'r':
5839 nv_vreplace(cap);
5840 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842 case '&':
5843 do_cmdline_cmd((char_u *)"%s//~/&");
5844 break;
5845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 /*
5847 * "gv": Reselect the previous Visual area. If Visual already active,
5848 * exchange previous and current Visual area.
5849 */
5850 case 'v':
5851 if (checkclearop(oap))
5852 break;
5853
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005854 if ( curbuf->b_visual.vi_start.lnum == 0
5855 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5856 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 beep_flush();
5858 else
5859 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005860 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 if (VIsual_active)
5862 {
5863 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005864 VIsual_mode = curbuf->b_visual.vi_mode;
5865 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866# ifdef FEAT_EVAL
5867 curbuf->b_visual_mode_eval = i;
5868# endif
5869 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005870 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5871 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005873 tpos = curbuf->b_visual.vi_end;
5874 curbuf->b_visual.vi_end = curwin->w_cursor;
5875 curwin->w_cursor = curbuf->b_visual.vi_start;
5876 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 }
5878 else
5879 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005880 VIsual_mode = curbuf->b_visual.vi_mode;
5881 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5882 tpos = curbuf->b_visual.vi_end;
5883 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 }
5885
5886 VIsual_active = TRUE;
5887 VIsual_reselect = TRUE;
5888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005889 // Set Visual to the start and w_cursor to the end of the Visual
5890 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 check_cursor();
5892 VIsual = curwin->w_cursor;
5893 curwin->w_cursor = tpos;
5894 check_cursor();
5895 update_topline();
5896 /*
5897 * When called from normal "g" command: start Select mode when
5898 * 'selectmode' contains "cmd". When called for K_SELECT, always
5899 * start Select mode.
5900 */
5901 if (cap->arg)
5902 VIsual_select = TRUE;
5903 else
5904 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005907 // Make sure the clipboard gets updated. Needed because start and
5908 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 clip_star.vmode = NUL;
5910#endif
5911 redraw_curbuf_later(INVERTED);
5912 showmode();
5913 }
5914 break;
5915 /*
5916 * "gV": Don't reselect the previous Visual area after a Select mode
5917 * mapping of menu.
5918 */
5919 case 'V':
5920 VIsual_reselect = FALSE;
5921 break;
5922
5923 /*
5924 * "gh": start Select mode.
5925 * "gH": start Select line mode.
5926 * "g^H": start Select block mode.
5927 */
5928 case K_BS:
5929 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005930 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 case 'h':
5932 case 'H':
5933 case Ctrl_H:
5934# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005935 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 if (cap->nchar == Ctrl_H)
5937 cap->cmdchar = Ctrl_V;
5938 else
5939# endif
5940 cap->cmdchar = cap->nchar + ('v' - 'h');
5941 cap->arg = TRUE;
5942 nv_visual(cap);
5943 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005945 // "gn", "gN" visually select next/previous search match
5946 // "gn" selects next match
5947 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005948 case 'N':
5949 case 'n':
5950 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005951 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005952 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
5954 /*
5955 * "gj" and "gk" two new funny movement keys -- up and down
5956 * movement based on *screen* line rather than *file* line.
5957 */
5958 case 'j':
5959 case K_DOWN:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005960 // with 'nowrap' it works just like the normal "j" command; also when
5961 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 if (!curwin->w_p_wrap
5963#ifdef FEAT_FOLDING
5964 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5965#endif
5966 )
5967 {
5968 oap->motion_type = MLINE;
5969 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5970 }
5971 else
5972 i = nv_screengo(oap, FORWARD, cap->count1);
5973 if (i == FAIL)
5974 clearopbeep(oap);
5975 break;
5976
5977 case 'k':
5978 case K_UP:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005979 // with 'nowrap' it works just like the normal "k" command; also when
5980 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 if (!curwin->w_p_wrap
5982#ifdef FEAT_FOLDING
5983 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5984#endif
5985 )
5986 {
5987 oap->motion_type = MLINE;
5988 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5989 }
5990 else
5991 i = nv_screengo(oap, BACKWARD, cap->count1);
5992 if (i == FAIL)
5993 clearopbeep(oap);
5994 break;
5995
5996 /*
5997 * "gJ": join two lines without inserting a space.
5998 */
5999 case 'J':
6000 nv_join(cap);
6001 break;
6002
6003 /*
6004 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6005 * "gm": middle of "g0" and "g$".
6006 */
6007 case '^':
6008 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006009 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
6011 case '0':
6012 case 'm':
6013 case K_HOME:
6014 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 oap->motion_type = MCHAR;
6016 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006017 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006019 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 int width2 = width1 + curwin_col_off2();
6021
6022 validate_virtcol();
6023 i = 0;
6024 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6025 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6026 }
6027 else
6028 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006029 // Go to the middle of the screen line. When 'number' or
6030 // 'relativenumber' is on and lines are wrapping the middle can be more
6031 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006033 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 + ((curwin->w_p_wrap && i > 0)
6035 ? curwin_col_off2() : 0)) / 2;
6036 coladvance((colnr_T)i);
6037 if (flag)
6038 {
6039 do
6040 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006041 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006042 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 }
6044 curwin->w_set_curswant = TRUE;
6045 break;
6046
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006047 case 'M':
6048 {
6049 char_u *ptr = ml_get_curline();
6050
6051 oap->motion_type = MCHAR;
6052 oap->inclusive = FALSE;
6053 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006054 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006055 else
6056 i = (int)STRLEN(ptr);
6057 if (cap->count0 > 0 && cap->count0 <= 100)
6058 coladvance((colnr_T)(i * cap->count0 / 100));
6059 else
6060 coladvance((colnr_T)(i / 2));
6061 curwin->w_set_curswant = TRUE;
6062 }
6063 break;
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006066 // "g_": to the last non-blank character in the line or <count> lines
6067 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 cap->oap->motion_type = MCHAR;
6069 cap->oap->inclusive = TRUE;
6070 curwin->w_curswant = MAXCOL;
6071 if (cursor_down((long)(cap->count1 - 1),
6072 cap->oap->op_type == OP_NOP) == FAIL)
6073 clearopbeep(cap->oap);
6074 else
6075 {
6076 char_u *ptr = ml_get_curline();
6077
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006078 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6080 --curwin->w_cursor.col;
6081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006082 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006084 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 --curwin->w_cursor.col;
6086 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006087 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 }
6089 break;
6090
6091 case '$':
6092 case K_END:
6093 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 {
6095 int col_off = curwin_col_off();
6096
6097 oap->motion_type = MCHAR;
6098 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006099 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006101 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 if (cap->count1 == 1)
6103 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006104 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 int width2 = width1 + curwin_col_off2();
6106
6107 validate_virtcol();
6108 i = width1 - 1;
6109 if (curwin->w_virtcol >= (colnr_T)width1)
6110 i += ((curwin->w_virtcol - width1) / width2 + 1)
6111 * width2;
6112 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006113
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006114 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006115 validate_virtcol();
6116 curwin->w_curswant = curwin->w_virtcol;
6117 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6119 {
6120 /*
6121 * Check for landing on a character that got split at
6122 * the end of the line. We do not want to advance to
6123 * the next screen line.
6124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 if (curwin->w_virtcol > (colnr_T)i)
6126 --curwin->w_cursor.col;
6127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 }
6129 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6130 clearopbeep(oap);
6131 }
6132 else
6133 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006134 if (cap->count1 > 1)
6135 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006136 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006137
Bram Moolenaar02631462017-09-22 15:20:32 +02006138 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006140
Bram Moolenaard5c82342019-07-27 18:44:57 +02006141 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006142 validate_virtcol();
6143 curwin->w_curswant = curwin->w_virtcol;
6144 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 }
6146 }
6147 break;
6148
6149 /*
6150 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6151 */
6152 case '*':
6153 case '#':
6154#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006155 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006157 case Ctrl_RSB: // :tag or :tselect for current identifier
6158 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 nv_ident(cap);
6160 break;
6161
6162 /*
6163 * ge and gE: go back to end of word
6164 */
6165 case 'e':
6166 case 'E':
6167 oap->motion_type = MCHAR;
6168 curwin->w_set_curswant = TRUE;
6169 oap->inclusive = TRUE;
6170 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6171 clearopbeep(oap);
6172 break;
6173
6174 /*
6175 * "g CTRL-G": display info about cursor position
6176 */
6177 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006178 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 break;
6180
6181 /*
6182 * "gi": start Insert at the last position.
6183 */
6184 case 'i':
6185 if (curbuf->b_last_insert.lnum != 0)
6186 {
6187 curwin->w_cursor = curbuf->b_last_insert;
6188 check_cursor_lnum();
6189 i = (int)STRLEN(ml_get_curline());
6190 if (curwin->w_cursor.col > (colnr_T)i)
6191 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 if (virtual_active())
6193 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 curwin->w_cursor.col = i;
6195 }
6196 }
6197 cap->cmdchar = 'i';
6198 nv_edit(cap);
6199 break;
6200
6201 /*
6202 * "gI": Start insert in column 1.
6203 */
6204 case 'I':
6205 beginline(0);
6206 if (!checkclearopq(oap))
6207 invoke_edit(cap, FALSE, 'g', FALSE);
6208 break;
6209
6210#ifdef FEAT_SEARCHPATH
6211 /*
6212 * "gf": goto file, edit file under cursor
6213 * "]f" and "[f": can also be used.
6214 */
6215 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006216 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 nv_gotofile(cap);
6218 break;
6219#endif
6220
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006221 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 case '\'':
6223 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006224 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 case '`':
6226 nv_gomark(cap);
6227 break;
6228
6229 /*
6230 * "gs": Goto sleep.
6231 */
6232 case 's':
6233 do_sleep(cap->count1 * 1000L);
6234 break;
6235
6236 /*
6237 * "ga": Display the ascii value of the character under the
6238 * cursor. It is displayed in decimal, hex, and octal. -- webb
6239 */
6240 case 'a':
6241 do_ascii(NULL);
6242 break;
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 /*
6245 * "g8": Display the bytes used for the UTF-8 character under the
6246 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006247 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 */
6249 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006250 if (cap->count0 == 8)
6251 utf_find_illegal();
6252 else
6253 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006256 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006257 case '<':
6258 show_sb_text();
6259 break;
6260
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 /*
6262 * "gg": Goto the first line in file. With a count it goes to
6263 * that line number like for "G". -- webb
6264 */
6265 case 'g':
6266 cap->arg = FALSE;
6267 nv_goto(cap);
6268 break;
6269
6270 /*
6271 * Two-character operators:
6272 * "gq" Format text
6273 * "gw" Format text and keep cursor position
6274 * "g~" Toggle the case of the text.
6275 * "gu" Change text to lower case.
6276 * "gU" Change text to upper case.
6277 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006278 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 */
6280 case 'q':
6281 case 'w':
6282 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006283 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 case '~':
6285 case 'u':
6286 case 'U':
6287 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006288 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 nv_operator(cap);
6290 break;
6291
6292 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006293 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 * current function
6295 * "gD": idem, but in the current file.
6296 */
6297 case 'd':
6298 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006299 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 break;
6301
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 /*
6303 * g<*Mouse> : <C-*mouse>
6304 */
6305 case K_MIDDLEMOUSE:
6306 case K_MIDDLEDRAG:
6307 case K_MIDDLERELEASE:
6308 case K_LEFTMOUSE:
6309 case K_LEFTDRAG:
6310 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006311 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 case K_RIGHTMOUSE:
6313 case K_RIGHTDRAG:
6314 case K_RIGHTRELEASE:
6315 case K_X1MOUSE:
6316 case K_X1DRAG:
6317 case K_X1RELEASE:
6318 case K_X2MOUSE:
6319 case K_X2DRAG:
6320 case K_X2RELEASE:
6321 mod_mask = MOD_MASK_CTRL;
6322 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6323 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324
6325 case K_IGNORE:
6326 break;
6327
6328 /*
6329 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6330 */
6331 case 'p':
6332 case 'P':
6333 nv_put(cap);
6334 break;
6335
6336#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006337 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 case 'o':
6339 goto_byte(cap->count0);
6340 break;
6341#endif
6342
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006343 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006345 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 {
6347 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006348 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 break;
6350 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006351
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 if (!checkclearopq(oap))
6353 do_exmode(TRUE);
6354 break;
6355
6356#ifdef FEAT_JUMPLIST
6357 case ',':
6358 nv_pcmark(cap);
6359 break;
6360
6361 case ';':
6362 cap->count1 = -cap->count1;
6363 nv_pcmark(cap);
6364 break;
6365#endif
6366
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006367 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006368 if (!checkclearop(oap))
6369 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006370 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006371 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006372 if (!checkclearop(oap))
6373 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006374 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006375
Bram Moolenaar62a23252020-08-09 14:04:42 +02006376 case TAB:
6377 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6378 clearopbeep(oap);
6379 break;
6380
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006381 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006382 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006383 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006384 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006385 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006386 break;
6387
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 default:
6389 clearopbeep(oap);
6390 break;
6391 }
6392}
6393
6394/*
6395 * Handle "o" and "O" commands.
6396 */
6397 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006398n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006400#ifdef FEAT_CONCEAL
6401 linenr_T oldline = curwin->w_cursor.lnum;
6402#endif
6403
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (!checkclearopq(cap->oap))
6405 {
6406#ifdef FEAT_FOLDING
6407 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006408 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 (void)hasFolding(curwin->w_cursor.lnum,
6410 &curwin->w_cursor.lnum, NULL);
6411 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006412 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 (void)hasFolding(curwin->w_cursor.lnum,
6414 NULL, &curwin->w_cursor.lnum);
6415#endif
6416 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6417 (cap->cmdchar == 'O' ? 1 : 0)),
6418 (linenr_T)(curwin->w_cursor.lnum +
6419 (cap->cmdchar == 'o' ? 1 : 0))
6420 ) == OK
6421 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006422 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6423 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006425#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006426 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006427 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006428#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006429#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006430 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006431 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006432 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006433#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006434 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006435 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6436 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6438 }
6439 }
6440}
6441
6442/*
6443 * "." command: redo last change.
6444 */
6445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006446nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447{
6448 if (!checkclearopq(cap->oap))
6449 {
6450 /*
6451 * If "restart_edit" is TRUE, the last but one command is repeated
6452 * instead of the last command (inserting text). This is used for
6453 * CTRL-O <.> in insert mode.
6454 */
6455 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6456 clearopbeep(cap->oap);
6457 }
6458}
6459
6460/*
6461 * CTRL-R: undo undo
6462 */
6463 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006464nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
6466 if (!checkclearopq(cap->oap))
6467 {
6468 u_redo((int)cap->count1);
6469 curwin->w_set_curswant = TRUE;
6470 }
6471}
6472
6473/*
6474 * Handle "U" command.
6475 */
6476 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006477nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006479 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006480 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006482 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 cap->cmdchar = 'g';
6484 cap->nchar = 'U';
6485 nv_operator(cap);
6486 }
6487 else if (!checkclearopq(cap->oap))
6488 {
6489 u_undoline();
6490 curwin->w_set_curswant = TRUE;
6491 }
6492}
6493
6494/*
6495 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6496 * single character.
6497 */
6498 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006499nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006501 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006502 {
6503#ifdef FEAT_JOB_CHANNEL
6504 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6505 {
6506 clearopbeep(cap->oap);
6507 return;
6508 }
6509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 else
6513 nv_operator(cap);
6514}
6515
6516/*
6517 * Handle an operator command.
6518 * The actual work is done by do_pending_operator().
6519 */
6520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006521nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
6523 int op_type;
6524
6525 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006526#ifdef FEAT_JOB_CHANNEL
6527 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6528 {
6529 clearopbeep(cap->oap);
6530 return;
6531 }
6532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006534 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 nv_lineop(cap);
6536 else if (!checkclearop(cap->oap))
6537 {
6538 cap->oap->start = curwin->w_cursor;
6539 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006540#ifdef FEAT_EVAL
6541 set_op_var(op_type);
6542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
6544}
6545
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006546#ifdef FEAT_EVAL
6547/*
6548 * Set v:operator to the characters for "optype".
6549 */
6550 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006551set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006552{
6553 char_u opchars[3];
6554
6555 if (optype == OP_NOP)
6556 set_vim_var_string(VV_OP, NULL, 0);
6557 else
6558 {
6559 opchars[0] = get_op_char(optype);
6560 opchars[1] = get_extra_op_char(optype);
6561 opchars[2] = NUL;
6562 set_vim_var_string(VV_OP, opchars, -1);
6563 }
6564}
6565#endif
6566
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567/*
6568 * Handle linewise operator "dd", "yy", etc.
6569 *
6570 * "_" is is a strange motion command that helps make operators more logical.
6571 * It is actually implemented, but not documented in the real Vi. This motion
6572 * command actually refers to "the current line". Commands like "dd" and "yy"
6573 * are really an alternate form of "d_" and "y_". It does accept a count, so
6574 * "d3_" works to delete 3 lines.
6575 */
6576 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006577nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578{
6579 cap->oap->motion_type = MLINE;
6580 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6581 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006582 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006583 && cap->oap->motion_force != 'v'
6584 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 || cap->oap->op_type == OP_LSHIFT
6586 || cap->oap->op_type == OP_RSHIFT)
6587 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006588 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 beginline(BL_WHITE | BL_FIX);
6590}
6591
6592/*
6593 * <Home> command.
6594 */
6595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006596nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006598 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006599 if (mod_mask & MOD_MASK_CTRL)
6600 nv_goto(cap);
6601 else
6602 {
6603 cap->count0 = 1;
6604 nv_pipe(cap);
6605 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006606 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6607 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608}
6609
6610/*
6611 * "|" command.
6612 */
6613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006614nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 cap->oap->motion_type = MCHAR;
6617 cap->oap->inclusive = FALSE;
6618 beginline(0);
6619 if (cap->count0 > 0)
6620 {
6621 coladvance((colnr_T)(cap->count0 - 1));
6622 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6623 }
6624 else
6625 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006626 // keep curswant at the column where we wanted to go, not where
6627 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 curwin->w_set_curswant = FALSE;
6629}
6630
6631/*
6632 * Handle back-word command "b" and "B".
6633 * cap->arg is 1 for "B"
6634 */
6635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006636nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 cap->oap->motion_type = MCHAR;
6639 cap->oap->inclusive = FALSE;
6640 curwin->w_set_curswant = TRUE;
6641 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6642 clearopbeep(cap->oap);
6643#ifdef FEAT_FOLDING
6644 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6645 foldOpenCursor();
6646#endif
6647}
6648
6649/*
6650 * Handle word motion commands "e", "E", "w" and "W".
6651 * cap->arg is TRUE for "E" and "W".
6652 */
6653 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006654nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 int n;
6657 int word_end;
6658 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006659 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660
6661 /*
6662 * Set inclusive for the "E" and "e" command.
6663 */
6664 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6665 word_end = TRUE;
6666 else
6667 word_end = FALSE;
6668 cap->oap->inclusive = word_end;
6669
6670 /*
6671 * "cw" and "cW" are a special case.
6672 */
6673 if (!word_end && cap->oap->op_type == OP_CHANGE)
6674 {
6675 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006676 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006678 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
6680 /*
6681 * Reproduce a funny Vi behaviour: "cw" on a blank only
6682 * changes one character, not all blanks until the start of
6683 * the next word. Only do this when the 'w' flag is included
6684 * in 'cpoptions'.
6685 */
6686 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6687 {
6688 cap->oap->inclusive = TRUE;
6689 cap->oap->motion_type = MCHAR;
6690 return;
6691 }
6692 }
6693 else
6694 {
6695 /*
6696 * This is a little strange. To match what the real Vi does,
6697 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6698 * that we are not on a space or a TAB. This seems impolite
6699 * at first, but it's really more what we mean when we say
6700 * 'cw'.
6701 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006702 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 * will change only one character! This is done by setting
6704 * flag.
6705 */
6706 cap->oap->inclusive = TRUE;
6707 word_end = TRUE;
6708 flag = TRUE;
6709 }
6710 }
6711 }
6712
6713 cap->oap->motion_type = MCHAR;
6714 curwin->w_set_curswant = TRUE;
6715 if (word_end)
6716 n = end_word(cap->count1, cap->arg, flag, FALSE);
6717 else
6718 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006720 // Don't leave the cursor on the NUL past the end of line. Unless we
6721 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006722 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006723 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
6725 if (n == FAIL && cap->oap->op_type == OP_NOP)
6726 clearopbeep(cap->oap);
6727 else
6728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730#ifdef FEAT_FOLDING
6731 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6732 foldOpenCursor();
6733#endif
6734 }
6735}
6736
6737/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006738 * Used after a movement command: If the cursor ends up on the NUL after the
6739 * end of the line, may move it back to the last character and make the motion
6740 * inclusive.
6741 */
6742 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006743adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006744{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006745 // The cursor cannot remain on the NUL when:
6746 // - the column is > 0
6747 // - not in Visual mode or 'selection' is "o"
6748 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006749 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006750 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006751 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006752 {
6753 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006754 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006755 if (has_mbyte)
6756 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006757 oap->inclusive = TRUE;
6758 }
6759}
6760
6761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 * "0" and "^" commands.
6763 * cap->arg is the argument for beginline().
6764 */
6765 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006766nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
6768 cap->oap->motion_type = MCHAR;
6769 cap->oap->inclusive = FALSE;
6770 beginline(cap->arg);
6771#ifdef FEAT_FOLDING
6772 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6773 foldOpenCursor();
6774#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006775 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6776 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777}
6778
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779/*
6780 * In exclusive Visual mode, may include the last character.
6781 */
6782 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006783adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006786 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (has_mbyte)
6789 inc_cursor();
6790 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 ++curwin->w_cursor.col;
6792 cap->oap->inclusive = FALSE;
6793 }
6794}
6795
6796/*
6797 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6798 * Should check VIsual_mode before calling this.
6799 * Returns TRUE when backed up to the previous line.
6800 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006801 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006802unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803{
6804 pos_T *pp;
6805
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006806 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006808 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 pp = &curwin->w_cursor;
6810 else
6811 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (pp->coladd > 0)
6813 --pp->coladd;
6814 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (pp->col > 0)
6816 {
6817 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006818 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 }
6820 else if (pp->lnum > 1)
6821 {
6822 --pp->lnum;
6823 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6824 return TRUE;
6825 }
6826 }
6827 return FALSE;
6828}
6829
6830/*
6831 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6832 */
6833 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006834nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
6836 if (VIsual_active)
6837 VIsual_select = TRUE;
6838 else if (VIsual_reselect)
6839 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006840 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 cap->arg = TRUE;
6842 nv_g_cmd(cap);
6843 }
6844}
6845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846
6847/*
6848 * "G", "gg", CTRL-END, CTRL-HOME.
6849 * cap->arg is TRUE for "G".
6850 */
6851 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006852nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853{
6854 linenr_T lnum;
6855
6856 if (cap->arg)
6857 lnum = curbuf->b_ml.ml_line_count;
6858 else
6859 lnum = 1L;
6860 cap->oap->motion_type = MLINE;
6861 setpcmark();
6862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006863 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 if (cap->count0 != 0)
6865 lnum = cap->count0;
6866 if (lnum < 1L)
6867 lnum = 1L;
6868 else if (lnum > curbuf->b_ml.ml_line_count)
6869 lnum = curbuf->b_ml.ml_line_count;
6870 curwin->w_cursor.lnum = lnum;
6871 beginline(BL_SOL | BL_FIX);
6872#ifdef FEAT_FOLDING
6873 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6874 foldOpenCursor();
6875#endif
6876}
6877
6878/*
6879 * CTRL-\ in Normal mode.
6880 */
6881 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006882nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883{
6884 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6885 {
6886 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006887 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006888 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 restart_edit = 0;
6890#ifdef FEAT_CMDWIN
6891 if (cmdwin_type != 0)
6892 cmdwin_result = Ctrl_C;
6893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 if (VIsual_active)
6895 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006896 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 redraw_curbuf_later(INVERTED);
6898 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006899 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 if (cap->nchar == Ctrl_G && p_im)
6901 restart_edit = 'a';
6902 }
6903 else
6904 clearopbeep(cap->oap);
6905}
6906
6907/*
6908 * ESC in Normal mode: beep, but don't flush buffers.
6909 * Don't even beep if we are canceling a command.
6910 */
6911 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006912nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 int no_reason;
6915
6916 no_reason = (cap->oap->op_type == OP_NOP
6917 && cap->opcount == 0
6918 && cap->count0 == 0
6919 && cap->oap->regname == 0
6920 && !p_im);
6921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006922 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {
6924 if (restart_edit == 0
6925#ifdef FEAT_CMDWIN
6926 && cmdwin_type == 0
6927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006930 {
6931 if (anyBufIsChanged())
6932 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6933 else
6934 msg(_("Type :qa and press <Enter> to exit Vim"));
6935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006937 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6938 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 if (!p_im)
6940 restart_edit = 0;
6941#ifdef FEAT_CMDWIN
6942 if (cmdwin_type != 0)
6943 {
6944 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006945 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 return;
6947 }
6948#endif
6949 }
6950
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 if (VIsual_active)
6952 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006953 end_visual_mode(); // stop Visual
6954 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 curwin->w_set_curswant = TRUE;
6956 redraw_curbuf_later(INVERTED);
6957 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006958 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006959 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 clearop(cap->oap);
6961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006962 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6963 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006964 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 restart_edit = 'a';
6966}
6967
6968/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006969 * Move the cursor for the "A" command.
6970 */
6971 void
6972set_cursor_for_append_to_line(void)
6973{
6974 curwin->w_set_curswant = TRUE;
6975 if (ve_flags == VE_ALL)
6976 {
6977 int save_State = State;
6978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006979 // Pretend Insert mode here to allow the cursor on the
6980 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006981 State = INSERT;
6982 coladvance((colnr_T)MAXCOL);
6983 State = save_State;
6984 }
6985 else
6986 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6987}
6988
6989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006991 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 */
6993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006994nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006996 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6998 cap->cmdchar = 'i';
6999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007000 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007002 {
7003#ifdef FEAT_TERMINAL
7004 if (term_in_normal_mode())
7005 {
7006 end_visual_mode();
7007 clearop(cap->oap);
7008 term_enter_job_mode();
7009 return;
7010 }
7011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007015 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007016 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7017 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
7019#ifdef FEAT_TEXTOBJ
7020 nv_object(cap);
7021#else
7022 clearopbeep(cap->oap);
7023#endif
7024 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007025#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007026 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007027 {
7028 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007029 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007030 return;
7031 }
7032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 else if (!curbuf->b_p_ma && !p_im)
7034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007035 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007036 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007038 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007039 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007040 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007042 else if (cap->cmdchar == K_PS && VIsual_active)
7043 {
7044 pos_T old_pos = curwin->w_cursor;
7045 pos_T old_visual = VIsual;
7046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007047 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007048 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7049 {
7050 shift_delete_registers();
7051 cap->oap->regname = '1';
7052 }
7053 else
7054 cap->oap->regname = '-';
7055 cap->cmdchar = 'd';
7056 cap->nchar = NUL;
7057 nv_operator(cap);
7058 do_pending_operator(cap, 0, FALSE);
7059 cap->cmdchar = K_PS;
7060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007061 // When the last char in the line was deleted then append. Detect this
7062 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007063 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7064 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007065 inc_cursor();
7066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007067 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007068 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 else if (!checkclearopq(cap->oap))
7071 {
7072 switch (cap->cmdchar)
7073 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007074 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007075 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 break;
7077
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007078 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007079 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7080 beginline(BL_WHITE);
7081 else
7082 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 break;
7084
Bram Moolenaara1891842017-02-04 21:34:31 +01007085 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 // Bracketed paste works like "a"ppend, unless the cursor is in
7087 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007088 if (curwin->w_cursor.col == 0)
7089 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007090 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007091
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007092 case 'a': // "a"ppend is like "i"nsert on the next character.
7093 // increment coladd when in virtual space, increment the
7094 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 if (virtual_active()
7096 && (curwin->w_cursor.coladd > 0
7097 || *ml_get_cursor() == NUL
7098 || *ml_get_cursor() == TAB))
7099 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007100 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 inc_cursor();
7102 break;
7103 }
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7106 {
7107 int save_State = State;
7108
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109 // Pretend Insert mode here to allow the cursor on the
7110 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 State = INSERT;
7112 coladvance(getviscol());
7113 State = save_State;
7114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
7116 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7117 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007118 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007119 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007120 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121}
7122
7123/*
7124 * Invoke edit() and take care of "restart_edit" and the return value.
7125 */
7126 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007127invoke_edit(
7128 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007129 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007130 int cmd,
7131 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
7133 int restart_edit_save = 0;
7134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007135 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7136 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7137 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (repl || !stuff_empty())
7139 restart_edit_save = restart_edit;
7140 else
7141 restart_edit_save = 0;
7142
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007143 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 restart_edit = 0;
7145
7146 if (edit(cmd, startln, cap->count1))
7147 cap->retval |= CA_COMMAND_BUSY;
7148
7149 if (restart_edit == 0)
7150 restart_edit = restart_edit_save;
7151}
7152
7153#ifdef FEAT_TEXTOBJ
7154/*
7155 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7156 */
7157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007158nv_object(
7159 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161 int flag;
7162 int include;
7163 char_u *mps_save;
7164
7165 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007166 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007168 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007170 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 mps_save = curbuf->b_p_mps;
7172 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7173
7174 switch (cap->nchar)
7175 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 flag = current_word(cap->oap, cap->count1, include, FALSE);
7178 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007179 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 flag = current_word(cap->oap, cap->count1, include, TRUE);
7181 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007182 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 case '(':
7184 case ')':
7185 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7186 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007187 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 case '{':
7189 case '}':
7190 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7191 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007192 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 case ']':
7194 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7195 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007196 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 case '>':
7198 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7199 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007200 case 't': // "at" = a tag block (xml and html)
7201 // Do not adjust oap->end in do_pending_operator()
7202 // otherwise there are different results for 'dit'
7203 // (note leading whitespace in last line):
7204 // 1) <b> 2) <b>
7205 // foobar foobar
7206 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007207 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007208 flag = current_tagblock(cap->oap, cap->count1, include);
7209 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007210 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 flag = current_par(cap->oap, cap->count1, include, 'p');
7212 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007213 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 flag = current_sent(cap->oap, cap->count1, include);
7215 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007216 case '"': // "a"" = a double quoted string
7217 case '\'': // "a'" = a single quoted string
7218 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007219 flag = current_quote(cap->oap, cap->count1, include,
7220 cap->nchar);
7221 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007222#if 0 // TODO
7223 case 'S': // "aS" = a section
7224 case 'f': // "af" = a filename
7225 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226#endif
7227 default:
7228 flag = FAIL;
7229 break;
7230 }
7231
7232 curbuf->b_p_mps = mps_save;
7233 if (flag == FAIL)
7234 clearopbeep(cap->oap);
7235 adjust_cursor_col();
7236 curwin->w_set_curswant = TRUE;
7237}
7238#endif
7239
7240/*
7241 * "q" command: Start/stop recording.
7242 * "q:", "q/", "q?": edit command-line in command-line window.
7243 */
7244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007245nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
7247 if (cap->oap->op_type == OP_FORMAT)
7248 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007249 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 cap->cmdchar = 'g';
7251 cap->nchar = 'q';
7252 nv_operator(cap);
7253 }
7254 else if (!checkclearop(cap->oap))
7255 {
7256#ifdef FEAT_CMDWIN
7257 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7258 {
7259 stuffcharReadbuff(cap->nchar);
7260 stuffcharReadbuff(K_CMDWIN);
7261 }
7262 else
7263#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007264 // (stop) recording into a named register, unless executing a
7265 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007266 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 clearopbeep(cap->oap);
7268 }
7269}
7270
7271/*
7272 * Handle the "@r" command.
7273 */
7274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007275nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
7277 if (checkclearop(cap->oap))
7278 return;
7279#ifdef FEAT_EVAL
7280 if (cap->nchar == '=')
7281 {
7282 if (get_expr_register() == NUL)
7283 return;
7284 }
7285#endif
7286 while (cap->count1-- && !got_int)
7287 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007288 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
7290 clearopbeep(cap->oap);
7291 break;
7292 }
7293 line_breakcheck();
7294 }
7295}
7296
7297/*
7298 * Handle the CTRL-U and CTRL-D commands.
7299 */
7300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007301nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7304 || (cap->cmdchar == Ctrl_D
7305 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7306 clearopbeep(cap->oap);
7307 else if (!checkclearop(cap->oap))
7308 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7309}
7310
7311/*
7312 * Handle "J" or "gJ" command.
7313 */
7314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007315nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007317 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007319 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 {
7321 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007322 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7324 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007326 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007327 if (cap->count0 <= 2)
7328 {
7329 clearopbeep(cap->oap);
7330 return;
7331 }
7332 cap->count0 = curbuf->b_ml.ml_line_count
7333 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007335
7336 prep_redo(cap->oap->regname, cap->count0,
7337 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7338 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 }
7340}
7341
7342/*
7343 * "P", "gP", "p" and "gp" commands.
7344 */
7345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007346nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007348 nv_put_opt(cap, FALSE);
7349}
7350
7351/*
7352 * "P", "gP", "p" and "gp" commands.
7353 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7354 */
7355 static void
7356nv_put_opt(cmdarg_T *cap, int fix_indent)
7357{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 int regname = 0;
7359 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007360 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007361 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 int dir;
7363 int flags = 0;
7364
7365 if (cap->oap->op_type != OP_NOP)
7366 {
7367#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007368 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7370 {
7371 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007372 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 }
7374 else
7375#endif
7376 clearopbeep(cap->oap);
7377 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007378#ifdef FEAT_JOB_CHANNEL
7379 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7380 {
7381 clearopbeep(cap->oap);
7382 }
7383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 else
7385 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007386 if (fix_indent)
7387 {
7388 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7389 ? FORWARD : BACKWARD;
7390 flags |= PUT_FIXINDENT;
7391 }
7392 else
7393 dir = (cap->cmdchar == 'P'
7394 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 ? BACKWARD : FORWARD;
7396 prep_redo_cmd(cap);
7397 if (cap->cmdchar == 'g')
7398 flags |= PUT_CURSEND;
7399
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (VIsual_active)
7401 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007402 // Putting in Visual mode: The put text replaces the selected
7403 // text. First delete the selected text, then put the new text.
7404 // Need to save and restore the registers that the delete
7405 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007406 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007408#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007410#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007411 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007412 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007413#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416
7417 )
7418 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007419 // The delete is going to overwrite the register we want to
7420 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 reg1 = get_register(regname, TRUE);
7422 }
7423
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007424 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 cap->cmdchar = 'd';
7426 cap->nchar = NUL;
7427 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007428 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 nv_operator(cap);
7430 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007431 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007432 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007434 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 cap->oap->regname = regname;
7436
7437 if (reg1 != NULL)
7438 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007439 // Delete probably changed the register we want to put, save
7440 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 reg2 = get_register(regname, FALSE);
7442 put_register(regname, reg1);
7443 }
7444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007445 // When deleted a linewise Visual area, put the register as
7446 // lines to avoid it joined with the next line. When deletion was
7447 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 if (VIsual_mode == 'V')
7449 flags |= PUT_LINE;
7450 else if (VIsual_mode == 'v')
7451 flags |= PUT_LINE_SPLIT;
7452 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7453 flags |= PUT_LINE_FORWARD;
7454 dir = BACKWARD;
7455 if ((VIsual_mode != 'V'
7456 && curwin->w_cursor.col < curbuf->b_op_start.col)
7457 || (VIsual_mode == 'V'
7458 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007459 // cursor is at the end of the line or end of file, put
7460 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007462 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007463 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007465 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007467 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (reg2 != NULL)
7469 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007471 // What to reselect with "gv"? Selecting the just put text seems to
7472 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007473 if (was_visual)
7474 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007475 curbuf->b_visual.vi_start = curbuf->b_op_start;
7476 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007477 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007478 if (*p_sel == 'e')
7479 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007480 }
7481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007482 // When all lines were selected and deleted do_put() leaves an empty
7483 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007484 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007485 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007486 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007487 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007488
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007489 // If the cursor was in that line, move it to the end of the last
7490 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007491 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7492 {
7493 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7494 coladvance((colnr_T)MAXCOL);
7495 }
7496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 auto_format(FALSE, TRUE);
7498 }
7499}
7500
7501/*
7502 * "o" and "O" commands.
7503 */
7504 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007505nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
7507#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007508 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7510 {
7511 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007512 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
7514 else
7515#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007516 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007518#ifdef FEAT_JOB_CHANNEL
7519 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007520 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 n_opencmd(cap);
7524}
7525
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526#ifdef FEAT_NETBEANS_INTG
7527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007528nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 netbeans_keycommand(cap->nchar);
7531}
7532#endif
7533
7534#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007536nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007538 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539}
7540#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007541
Bram Moolenaar3918c952005-03-15 22:34:55 +00007542/*
7543 * Trigger CursorHold event.
7544 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7545 * input buffer. "did_cursorhold" is set to avoid retriggering.
7546 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007548nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007549{
7550 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7551 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007552 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007553}