blob: e3523546b91826685521147d841cc8df4094d8cf [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 Moolenaar071d4272004-06-13 20:20:40 +0000378};
379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100380// Number of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static short nv_cmd_idx[NV_CMDS_SIZE];
385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386// The highest index for which
387// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static int nv_max_linear;
389
390/*
391 * Compare functions for qsort() below, that checks the command character
392 * through the index in nv_cmd_idx[].
393 */
394 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100395nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396{
397 int c1, c2;
398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100399 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 c1 = nv_cmds[*(const short *)s1].cmd_char;
401 c2 = nv_cmds[*(const short *)s2].cmd_char;
402 if (c1 < 0)
403 c1 = -c1;
404 if (c2 < 0)
405 c2 = -c2;
406 return c1 - c2;
407}
408
409/*
410 * Initialize the nv_cmd_idx[] table.
411 */
412 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100413init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
415 int i;
416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100417 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000418 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 nv_cmd_idx[i] = i;
420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100421 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100424 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000425 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
427 break;
428 nv_max_linear = i - 1;
429}
430
431/*
432 * Search for a command in the commands table.
433 * Returns -1 for invalid command.
434 */
435 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 int i;
439 int idx;
440 int top, bot;
441 int c;
442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100443 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (cmdchar >= 0x100)
445 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100447 // We use the absolute value of the character. Special keys have a
448 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (cmdchar < 0)
450 cmdchar = -cmdchar;
451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // If the character is in the first part: The character is the index into
453 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (cmdchar <= nv_max_linear)
455 return nv_cmd_idx[cmdchar];
456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100457 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 bot = nv_max_linear + 1;
459 top = NV_CMDS_SIZE - 1;
460 idx = -1;
461 while (bot <= top)
462 {
463 i = (top + bot) / 2;
464 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
465 if (c < 0)
466 c = -c;
467 if (cmdchar == c)
468 {
469 idx = nv_cmd_idx[i];
470 break;
471 }
472 if (cmdchar > c)
473 bot = i + 1;
474 else
475 top = i - 1;
476 }
477 return idx;
478}
479
480/*
481 * Execute a command in Normal mode.
482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100484normal_cmd(
485 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100486 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100490 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 int old_col = curwin->w_curswant;
492#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100493 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100495 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 static int old_mapped_len = 0;
498 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000499#ifdef FEAT_EVAL
500 int set_prevcount = FALSE;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502
Bram Moolenaara80faa82020-04-12 19:37:17 +0200503 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000505
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100506 // Use a count remembered from before entering an operator. After typing
507 // "3d" we return from normal_cmd() and come back here, the "3" is
508 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 ca.opcount = opcount;
510
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 /*
512 * If there is an operator pending, then the command we take this time
513 * will terminate it. Finish_op tells us to finish the operation before
514 * returning this time (unless the operation was cancelled).
515 */
516#ifdef CURSOR_SHAPE
517 c = finish_op;
518#endif
519 finish_op = (oap->op_type != OP_NOP);
520#ifdef CURSOR_SHAPE
521 if (finish_op != c)
522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100523 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524# ifdef FEAT_MOUSESHAPE
525 update_mouseshape(-1);
526# endif
527 }
528#endif
529
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100530 // When not finishing an operator and no register name typed, reset the
531 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000533 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000535#ifdef FEAT_EVAL
536 set_prevcount = TRUE;
537#endif
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100540 // Restore counts from before receiving K_CURSORHOLD. This means after
541 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
542 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000543 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
544 {
545 ca.opcount = oap->prev_opcount;
546 ca.count0 = oap->prev_count0;
547 oap->prev_opcount = 0;
548 oap->prev_count0 = 0;
549 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000550
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
553 State = NORMAL_BUSY;
554#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100555 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#endif
557
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100558#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100559 // Set v:count here, when called from main() and not a stuffed
560 // command, so that v:count can be used in an expression mapping
561 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100562 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100563 set_vcount_ca(&ca, &set_prevcount);
564#endif
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 /*
567 * Get the command character from the user.
568 */
569 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100570 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571
572 /*
573 * If a mapping was started in Visual or Select mode, remember the length
574 * of the mapping. This is used below to not return to Insert mode for as
575 * long as the mapping is being executed.
576 */
577 if (restart_edit == 0)
578 old_mapped_len = 0;
579 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000580 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 old_mapped_len = typebuf_maplen();
582
583 if (c == NUL)
584 c = K_ZERO;
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 /*
587 * In Select mode, typed text replaces the selection.
588 */
589 if (VIsual_active
590 && VIsual_select
591 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
592 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100593 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
594 // 'insertmode' is set) fake a "d"elete command, Insert mode will
595 // restart automatically.
596 // Insert the typed character in the typeahead buffer, so that it can
597 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200598 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000599 if (restart_edit != 0)
600 c = 'd';
601 else
602 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100603 msg_nowait = TRUE; // don't delay going to insert mode
604 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
607#ifdef FEAT_CMDL_INFO
608 need_flushbuf = add_to_showcmd(c);
609#endif
610
611getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {
614 /*
615 * Handle a count before a command and compute ca.count0.
616 * Note that '0' is a command and not the start of a count, but it's
617 * part of a count after other digits.
618 */
619 while ( (c >= '1' && c <= '9')
620 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
621 {
622 if (c == K_DEL || c == K_KDEL)
623 {
624 ca.count0 /= 10;
625#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100626 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627#endif
628 }
629 else
630 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100631 if (ca.count0 < 0) // got too large!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000633#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100634 // Set v:count here, when called from main() and not a stuffed
635 // command, so that v:count can be used in an expression mapping
636 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100637 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100638 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 if (ctrl_w)
641 {
642 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100643 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100645 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000646 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 --no_zero_mapping;
649 if (ctrl_w)
650 {
651 --no_mapping;
652 --allow_keys;
653 }
654#ifdef FEAT_CMDL_INFO
655 need_flushbuf |= add_to_showcmd(c);
656#endif
657 }
658
659 /*
660 * If we got CTRL-W there may be a/another count
661 */
662 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
663 {
664 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100665 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 ca.count0 = 0;
667 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100668 ++allow_keys; // no mapping for nchar, but keys
669 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 --no_mapping;
672 --allow_keys;
673#ifdef FEAT_CMDL_INFO
674 need_flushbuf |= add_to_showcmd(c);
675#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100676 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
678 }
679
Bram Moolenaara983fe92008-07-31 20:04:27 +0000680 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100682 // Save the count values so that ca.opcount and ca.count0 are exactly
683 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000684 oap->prev_opcount = ca.opcount;
685 oap->prev_count0 = ca.count0;
686 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100687 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000688 {
689 /*
690 * If we're in the middle of an operator (including after entering a
691 * yank buffer with '"') AND we had a count before the operator, then
692 * that count overrides the current value of ca.count0.
693 * What this means effectively, is that commands like "3dw" get turned
694 * into "d3w" which makes things fall into place pretty neatly.
695 * If you give a count before AND after the operator, they are
696 * multiplied.
697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (ca.count0)
699 ca.count0 *= ca.opcount;
700 else
701 ca.count0 = ca.opcount;
702 }
703
704 /*
705 * Always remember the count. It will be set to zero (on the next call,
706 * above) when there is no pending operator.
707 * When called from main(), save the count for use by the "count" built-in
708 * variable.
709 */
710 ca.opcount = ca.count0;
711 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
712
713#ifdef FEAT_EVAL
714 /*
715 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100716 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100718 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000719 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720#endif
721
722 /*
723 * Find the command character in the table of commands.
724 * For CTRL-W we already got nchar when looking for a count.
725 */
726 if (ctrl_w)
727 {
728 ca.nchar = c;
729 ca.cmdchar = Ctrl_W;
730 }
731 else
732 ca.cmdchar = c;
733 idx = find_command(ca.cmdchar);
734 if (idx < 0)
735 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100736 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 clearopbeep(oap);
738 goto normal_end;
739 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000740
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000741 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100743 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000745 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 goto normal_end;
747 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000748 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
749 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 /*
752 * In Visual/Select mode, a few keys are handled in a special way.
753 */
754 if (VIsual_active)
755 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100756 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (km_stopsel
758 && (nv_cmds[idx].cmd_flags & NV_STS)
759 && !(mod_mask & MOD_MASK_SHIFT))
760 {
761 end_visual_mode();
762 redraw_curbuf_later(INVERTED);
763 }
764
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100765 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (km_startsel)
767 {
768 if (nv_cmds[idx].cmd_flags & NV_SS)
769 {
770 unshift_special(&ca);
771 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000772 if (idx < 0)
773 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100774 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000775 clearopbeep(oap);
776 goto normal_end;
777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
779 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
780 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
785#ifdef FEAT_RIGHTLEFT
786 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
787 && (nv_cmds[idx].cmd_flags & NV_RL))
788 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 // Invert horizontal movements and operations. Only when typed by the
790 // user directly, not when the result of a mapping or "x" translated
791 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 switch (ca.cmdchar)
793 {
794 case 'l': ca.cmdchar = 'h'; break;
795 case K_RIGHT: ca.cmdchar = K_LEFT; break;
796 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
797 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
798 case 'h': ca.cmdchar = 'l'; break;
799 case K_LEFT: ca.cmdchar = K_RIGHT; break;
800 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
801 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
802 case '>': ca.cmdchar = '<'; break;
803 case '<': ca.cmdchar = '>'; break;
804 }
805 idx = find_command(ca.cmdchar);
806 }
807#endif
808
809 /*
810 * Get an additional character if we need one.
811 */
812 if ((nv_cmds[idx].cmd_flags & NV_NCH)
813 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
814 && oap->op_type == OP_NOP)
815 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
816 || (ca.cmdchar == 'q'
817 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200818 && reg_recording == 0
819 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100821 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 {
823 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100824 int repl = FALSE; // get character for replace mode
825 int lit = FALSE; // get extra character literally
826 int langmap_active = FALSE; // using :lmap mappings
827 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100828#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100829 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#endif
831
832 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100833 ++allow_keys; // no mapping for nchar, but allow key codes
834 // Don't generate a CursorHold event here, most commands can't handle
835 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000836 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (ca.cmdchar == 'g')
838 {
839 /*
840 * For 'g' get the next character now, so that we can check for
841 * "gr", "g'" and "g`".
842 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000843 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#ifdef FEAT_CMDL_INFO
846 need_flushbuf |= add_to_showcmd(ca.nchar);
847#endif
848 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100849 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100851 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100855 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100858 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860 else
861 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100862 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 repl = TRUE;
864 cp = &ca.nchar;
865 }
866 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
867
868 /*
869 * Get a second or third character.
870 */
871 if (cp != NULL)
872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (repl)
874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100875 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100876#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100877 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
881 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100882 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 --no_mapping;
884 --allow_keys;
885 if (repl)
886 State = LREPLACE;
887 else
888 State = LANGMAP;
889 langmap_active = TRUE;
890 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100891#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100893 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
895 im_set_active(TRUE);
896#endif
897
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000898 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
900 if (langmap_active)
901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100902 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 ++no_mapping;
904 ++allow_keys;
905 State = NORMAL_BUSY;
906 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100907#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (lang)
909 {
910 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
911 im_save_status(&curbuf->b_p_iminsert);
912 im_set_active(FALSE);
913 }
914 p_smd = save_smd;
915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917#ifdef FEAT_CMDL_INFO
918 need_flushbuf |= add_to_showcmd(*cp);
919#endif
920
921 if (!lit)
922 {
923#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100924 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (*cp == Ctrl_K
926 && ((nv_cmds[idx].cmd_flags & NV_LANG)
927 || cp == &ca.extra_char)
928 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
929 {
930 c = get_digraph(FALSE);
931 if (c > 0)
932 {
933 *cp = c;
934# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100935 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 del_from_showcmd(3);
937 need_flushbuf |= add_to_showcmd(*cp);
938# endif
939 }
940 }
941#endif
942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100943 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100946 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (p_hkmap && lang && KeyTyped)
948 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#endif
950 }
951
952 /*
953 * When the next character is CTRL-\ a following CTRL-N means the
954 * command is aborted and we go to Normal mode.
955 */
956 if (cp == &ca.extra_char
957 && ca.nchar == Ctrl_BSL
958 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
959 {
960 ca.cmdchar = Ctrl_BSL;
961 ca.nchar = ca.extra_char;
962 idx = find_command(ca.cmdchar);
963 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200964 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200965 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 else if (*cp == Ctrl_BSL)
967 {
968 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100970 // There is a busy wait here when typing "f<C-\>" and then
971 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 while ((c = vpeekc()) <= 0 && towait > 0L)
973 {
974 do_sleep(towait > 50L ? 50L : towait);
975 towait -= 50L;
976 }
977 if (c > 0)
978 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000979 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (c != Ctrl_N && c != Ctrl_G)
981 vungetc(c);
982 else
983 {
984 ca.cmdchar = Ctrl_BSL;
985 ca.nchar = c;
986 idx = find_command(ca.cmdchar);
987 }
988 }
989 }
990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100991 // When getting a text character and the next character is a
992 // multi-byte character, it could be a composing character.
993 // However, don't wait for it to arrive. Also, do enable mapping,
994 // because if it's put back with vungetc() it's too late to apply
995 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +0200996 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 while (enc_utf8 && lang && (c = vpeekc()) > 0
998 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
999 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001000 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 if (!utf_iscomposing(c))
1002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001003 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 break;
1005 }
1006 else if (ca.ncharC1 == 0)
1007 ca.ncharC1 = c;
1008 else
1009 ca.ncharC2 = c;
1010 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001011 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 --no_mapping;
1014 --allow_keys;
1015 }
1016
1017#ifdef FEAT_CMDL_INFO
1018 /*
1019 * Flush the showcmd characters onto the screen so we can see them while
1020 * the command is being executed. Only do this when the shown command was
1021 * actually displayed, otherwise this will slow down a lot when executing
1022 * mappings.
1023 */
1024 if (need_flushbuf)
1025 out_flush();
1026#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001027 if (ca.cmdchar != K_IGNORE)
1028 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029
1030 State = NORMAL;
1031
1032 if (ca.nchar == ESC)
1033 {
1034 clearop(oap);
1035 if (restart_edit == 0 && goto_im())
1036 restart_edit = 'a';
1037 goto normal_end;
1038 }
1039
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001040 if (ca.cmdchar != K_IGNORE)
1041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001042 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001043 msg_col = 0;
1044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001046 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001048 // When 'keymodel' contains "startsel" some keys start Select/Visual
1049 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (!VIsual_active && km_startsel)
1051 {
1052 if (nv_cmds[idx].cmd_flags & NV_SS)
1053 {
1054 start_selection();
1055 unshift_special(&ca);
1056 idx = find_command(ca.cmdchar);
1057 }
1058 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1059 && (mod_mask & MOD_MASK_SHIFT))
1060 {
1061 start_selection();
1062 mod_mask &= ~MOD_MASK_SHIFT;
1063 }
1064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
1066 /*
1067 * Execute the command!
1068 * Call the command function found in the commands table.
1069 */
1070 ca.arg = nv_cmds[idx].cmd_arg;
1071 (nv_cmds[idx].cmd_func)(&ca);
1072
1073 /*
1074 * If we didn't start or finish an operator, reset oap->regname, unless we
1075 * need it later.
1076 */
1077 if (!finish_op
1078 && !oap->op_type
1079 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1080 {
1081 clearop(oap);
1082#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001083 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084#endif
1085 }
1086
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001087 // Get the length of mapped chars again after typing a count, second
1088 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001089 if (old_mapped_len > 0)
1090 old_mapped_len = typebuf_maplen();
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 /*
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001093 * If an operation is pending, handle it. But not for K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001095 if (ca.cmdchar != K_IGNORE)
1096 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
1098 /*
1099 * Wait for a moment when a message is displayed that will be overwritten
1100 * by the mode message.
1101 * In Visual mode and with "^O" in Insert mode, a short message will be
1102 * overwritten by the mode message. Wait a bit, until a key is hit.
1103 * In Visual mode, it's more important to keep the Visual area updated
1104 * than keeping a message (e.g. from a /pat search).
1105 * Only do this if the command was typed, not from a mapping.
1106 * Don't wait when emsg_silent is non-zero.
1107 * Also wait a bit after an error message, e.g. for "^O:".
1108 * Don't redraw the screen, it would remove the message.
1109 */
1110 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001111 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 || (VIsual_active
1114 && old_pos.lnum == curwin->w_cursor.lnum
1115 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 )
1117 && (clear_cmdline
1118 || redraw_cmdline)
1119 && (msg_didout || (msg_didany && msg_scroll))
1120 && !msg_nowait
1121 && KeyTyped)
1122 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 && (msg_scroll
1125 || emsg_on_display)))
1126 && oap->regname == 0
1127 && !(ca.retval & CA_COMMAND_BUSY)
1128 && stuff_empty()
1129 && typebuf_typed()
1130 && emsg_silent == 0
1131 && !did_wait_return
1132 && oap->op_type == OP_NOP)
1133 {
1134 int save_State = State;
1135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001136 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (restart_edit != 0)
1138 State = INSERT;
1139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001140 // If need to redraw, and there is a "keep_msg", redraw before the
1141 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1143 {
1144 char_u *kmsg;
1145
1146 kmsg = keep_msg;
1147 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001148 // Showmode() will clear keep_msg, but we want to use it anyway.
1149 // First update w_topline.
1150 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001152 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001154
1155 kmsg = vim_strsave(keep_msg);
1156 if (kmsg != NULL)
1157 {
1158 msg_attr((char *)kmsg, keep_msg_attr);
1159 vim_free(kmsg);
1160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001163#ifdef CURSOR_SHAPE
1164 ui_cursor_shape(); // may show different cursor shape
1165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 cursor_on();
1167 out_flush();
1168 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001169 ui_delay(1003L, TRUE); // wait at least one second
1170 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 State = save_State;
1172
1173 msg_scroll = FALSE;
1174 emsg_on_display = FALSE;
1175 }
1176
1177 /*
1178 * Finish up after executing a Normal mode command.
1179 */
1180normal_end:
1181
1182 msg_nowait = FALSE;
1183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001184 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#ifdef CURSOR_SHAPE
1186 c = finish_op;
1187#endif
1188 finish_op = FALSE;
1189#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001190 // Redraw the cursor with another shape, if we were in Operator-pending
1191 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 if (c || ca.cmdchar == 'r')
1193 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001194 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195# ifdef FEAT_MOUSESHAPE
1196 update_mouseshape(-1);
1197# endif
1198 }
1199#endif
1200
1201#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001202 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001203 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 clear_showcmd();
1205#endif
1206
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001207 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 vim_free(ca.searchbuf);
1209
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 if (has_mbyte)
1211 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 if (curwin->w_p_scb && toplevel)
1214 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001215 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 do_check_scrollbind(TRUE);
1217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaar860cae12010-06-05 23:22:07 +02001219 if (curwin->w_p_crb && toplevel)
1220 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001221 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001222 do_check_cursorbind();
1223 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001224
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001225#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001226 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001227 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001228 restart_edit = 0;
1229#endif
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 /*
1232 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1233 * if still inside a mapping that started in Visual mode).
1234 * May switch from Visual to Select mode after CTRL-O command.
1235 */
1236 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1238 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 && !(ca.retval & CA_COMMAND_BUSY)
1240 && stuff_empty()
1241 && oap->regname == 0)
1242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (restart_VIsual_select == 1)
1244 {
1245 VIsual_select = TRUE;
1246 showmode();
1247 restart_VIsual_select = 0;
1248 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001249 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 (void)edit(restart_edit, FALSE, 1L);
1251 }
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 if (restart_VIsual_select == 2)
1254 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001256 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 opcount = ca.opcount;
1258}
1259
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001260#ifdef FEAT_EVAL
1261/*
1262 * Set v:count and v:count1 according to "cap".
1263 * Set v:prevcount only when "set_prevcount" is TRUE.
1264 */
1265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001266set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001267{
1268 long count = cap->count0;
1269
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001270 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001271 if (cap->opcount != 0)
1272 count = cap->opcount * (count == 0 ? 1 : count);
1273 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001274 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001275}
1276#endif
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001279 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 * if not.
1281 */
1282 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001283check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 static int did_check = FALSE;
1286
1287 if (full_screen)
1288 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001289 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001290 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 did_check = TRUE;
1292 }
1293}
1294
1295/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001296 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 * This function should ALWAYS be called to end Visual mode, except from
1298 * do_pending_operator().
1299 */
1300 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001301end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
1303#ifdef FEAT_CLIPBOARD
1304 /*
1305 * If we are using the clipboard, then remember what was selected in case
1306 * we need to paste it somewhere while we still own the selection.
1307 * Only do this when the clipboard is already owned. Don't want to grab
1308 * the selection when hitting ESC.
1309 */
1310 if (clip_star.available && clip_star.owned)
1311 clip_auto_select();
1312#endif
1313
1314 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 setmouse();
1316 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001318 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001319 curbuf->b_visual.vi_mode = VIsual_mode;
1320 curbuf->b_visual.vi_start = VIsual;
1321 curbuf->b_visual.vi_end = curwin->w_cursor;
1322 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#ifdef FEAT_EVAL
1324 curbuf->b_visual_mode_eval = VIsual_mode;
1325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (!virtual_active())
1327 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001328 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001330 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331}
1332
1333/*
1334 * Reset VIsual_active and VIsual_reselect.
1335 */
1336 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001337reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
1339 if (VIsual_active)
1340 {
1341 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001342 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344 VIsual_reselect = FALSE;
1345}
1346
1347/*
1348 * Reset VIsual_active and VIsual_reselect if it's set.
1349 */
1350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001351reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 if (VIsual_active)
1354 {
1355 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001356 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 VIsual_reselect = FALSE;
1358 }
1359}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001361 void
1362restore_visual_mode(void)
1363{
1364 if (VIsual_mode_orig != NUL)
1365 {
1366 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1367 VIsual_mode_orig = NUL;
1368 }
1369}
1370
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371/*
1372 * Check for a balloon-eval special item to include when searching for an
1373 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1374 * Returns TRUE if the character at "*ptr" should be included.
1375 * "dir" is FORWARD or BACKWARD, the direction of searching.
1376 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1377 * "bnp" points to a counter for square brackets.
1378 */
1379 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001380find_is_eval_item(
1381 char_u *ptr,
1382 int *colp,
1383 int *bnp,
1384 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001386 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1388 ++*bnp;
1389 if (*bnp > 0)
1390 {
1391 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1392 --*bnp;
1393 return TRUE;
1394 }
1395
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001396 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 if (*ptr == '.')
1398 return TRUE;
1399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001400 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1402 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1403 {
1404 *colp += dir;
1405 return TRUE;
1406 }
1407 return FALSE;
1408}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410/*
1411 * Find the identifier under or to the right of the cursor.
1412 * "find_type" can have one of three values:
1413 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001414 * FIND_STRING: find any non-white text
1415 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001416 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 *
1418 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001419 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001421 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 * This doesn't match the real Vi but I like it a little better and it
1423 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001424 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 * When FIND_IDENT isn't defined, we backup until a blank.
1426 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001427 * Returns the length of the text, or zero if no text is found.
1428 * If text is found, a pointer to the text is put in "*text". This
1429 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 */
1431 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001432find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
1434 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001435 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436}
1437
1438/*
1439 * Like find_ident_under_cursor(), but for any window and any position.
1440 * However: Uses 'iskeyword' from the current window!.
1441 */
1442 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001443find_ident_at_pos(
1444 win_T *wp,
1445 linenr_T lnum,
1446 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001447 char_u **text,
1448 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001449 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001452 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 int this_class = 0;
1455 int prev_class;
1456 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001457 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
1459 /*
1460 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001461 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 */
1463 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1464 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1465 {
1466 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001467 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 */
1469 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (has_mbyte)
1471 {
1472 while (ptr[col] != NUL)
1473 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001474 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 this_class = mb_get_class(ptr + col);
1478 if (this_class != 0 && (i == 1 || this_class != 1))
1479 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001480 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 }
1482 }
1483 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001485 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 )
1488 ++col;
1489
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001490 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001494 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (has_mbyte)
1497 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001498 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1500 this_class = mb_get_class((char_u *)"a");
1501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001503 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 {
1505 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1506 prev_class = mb_get_class(ptr + prevcol);
1507 if (this_class != prev_class
1508 && (i == 0
1509 || prev_class == 0
1510 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 && (!(find_type & FIND_EVAL)
1512 || prevcol == 0
1513 || !find_is_eval_item(ptr + prevcol, &prevcol,
1514 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 )
1516 break;
1517 col = prevcol;
1518 }
1519
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001520 // If we don't want just any old text, or we've found an
1521 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (this_class > 2)
1523 this_class = 2;
1524 if (!(find_type & FIND_STRING) || this_class == 2)
1525 break;
1526 }
1527 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 while (col > 0
1530 && ((i == 0
1531 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001532 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 && (!(find_type & FIND_IDENT)
1534 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 || ((find_type & FIND_EVAL)
1536 && col > 1
1537 && find_is_eval_item(ptr + col - 1, &col,
1538 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 ))
1540 --col;
1541
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001542 // If we don't want just any old text, or we've found an
1543 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1545 break;
1546 }
1547 }
1548
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001549 if (ptr[col] == NUL || (i == 0
1550 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001552 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001553 if ((find_type & FIND_NOERROR) == 0)
1554 {
1555 if (find_type & FIND_STRING)
1556 emsg(_("E348: No string under cursor"));
1557 else
1558 emsg(_(e_noident));
1559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 return 0;
1561 }
1562 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001563 *text = ptr;
1564 if (textcol != NULL)
1565 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001568 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 bn = 0;
1571 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (has_mbyte)
1574 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001575 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 this_class = mb_get_class(ptr);
1577 while (ptr[col] != NUL
1578 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1579 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 || ((find_type & FIND_EVAL)
1581 && col <= (int)startcol
1582 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001584 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001588 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 || ((find_type & FIND_EVAL)
1590 && col <= (int)startcol
1591 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
1595 return col;
1596}
1597
1598/*
1599 * Prepare for redo of a normal command.
1600 */
1601 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603{
1604 prep_redo(cap->oap->regname, cap->count0,
1605 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1606}
1607
1608/*
1609 * Prepare for redo of any command.
1610 * Note that only the last argument can be a multi-byte char.
1611 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001613prep_redo(
1614 int regname,
1615 long num,
1616 int cmd1,
1617 int cmd2,
1618 int cmd3,
1619 int cmd4,
1620 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001623 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
1625 AppendCharToRedobuff('"');
1626 AppendCharToRedobuff(regname);
1627 }
1628 if (num)
1629 AppendNumberToRedobuff(num);
1630
1631 if (cmd1 != NUL)
1632 AppendCharToRedobuff(cmd1);
1633 if (cmd2 != NUL)
1634 AppendCharToRedobuff(cmd2);
1635 if (cmd3 != NUL)
1636 AppendCharToRedobuff(cmd3);
1637 if (cmd4 != NUL)
1638 AppendCharToRedobuff(cmd4);
1639 if (cmd5 != NUL)
1640 AppendCharToRedobuff(cmd5);
1641}
1642
1643/*
1644 * check for operator active and clear it
1645 *
1646 * return TRUE if operator was active
1647 */
1648 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001649checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
1651 if (oap->op_type == OP_NOP)
1652 return FALSE;
1653 clearopbeep(oap);
1654 return TRUE;
1655}
1656
1657/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001658 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001660 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 */
1662 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001663checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001665 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 return FALSE;
1667 clearopbeep(oap);
1668 return TRUE;
1669}
1670
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001671 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001672clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674 oap->op_type = OP_NOP;
1675 oap->regname = 0;
1676 oap->motion_force = NUL;
1677 oap->use_reg_one = FALSE;
1678}
1679
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001680 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001681clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 clearop(oap);
1684 beep_flush();
1685}
1686
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687/*
1688 * Remove the shift modifier from a special key.
1689 */
1690 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001691unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
1693 switch (cap->cmdchar)
1694 {
1695 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1696 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1697 case K_S_UP: cap->cmdchar = K_UP; break;
1698 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1699 case K_S_HOME: cap->cmdchar = K_HOME; break;
1700 case K_S_END: cap->cmdchar = K_END; break;
1701 }
1702 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001705/*
1706 * If the mode is currently displayed clear the command line or update the
1707 * command displayed.
1708 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001710may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001711{
1712 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001713 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001714#ifdef FEAT_CMDL_INFO
1715 else
1716 clear_showcmd();
1717#endif
1718}
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1721/*
1722 * Routines for displaying a partly typed command
1723 */
1724
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001725#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001727static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728static int showcmd_is_clear = TRUE;
1729static int showcmd_visual = FALSE;
1730
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001731static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001734clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
1736 if (!p_sc)
1737 return;
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (VIsual_active && !char_avail())
1740 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001741 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 long lines;
1743 colnr_T leftcol, rightcol;
1744 linenr_T top, bot;
1745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001746 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001747 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 {
1749 top = VIsual.lnum;
1750 bot = curwin->w_cursor.lnum;
1751 }
1752 else
1753 {
1754 top = curwin->w_cursor.lnum;
1755 bot = VIsual.lnum;
1756 }
1757# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001758 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001759 (void)hasFolding(top, &top, NULL);
1760 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761# endif
1762 lines = bot - top + 1;
1763
1764 if (VIsual_mode == Ctrl_V)
1765 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001766# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001767 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001768 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001770 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001771 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001772 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001775# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001776 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001777 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1780 (long)(rightcol - leftcol + 1));
1781 }
1782 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1783 sprintf((char *)showcmd_buf, "%ld", lines);
1784 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001785 {
1786 char_u *s, *e;
1787 int l;
1788 int bytes = 0;
1789 int chars = 0;
1790
1791 if (cursor_bot)
1792 {
1793 s = ml_get_pos(&VIsual);
1794 e = ml_get_cursor();
1795 }
1796 else
1797 {
1798 s = ml_get_cursor();
1799 e = ml_get_pos(&VIsual);
1800 }
1801 while ((*p_sel != 'e') ? s <= e : s < e)
1802 {
1803 l = (*mb_ptr2len)(s);
1804 if (l == 0)
1805 {
1806 ++bytes;
1807 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001808 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001809 }
1810 bytes += l;
1811 ++chars;
1812 s += l;
1813 }
1814 if (bytes == chars)
1815 sprintf((char *)showcmd_buf, "%d", chars);
1816 else
1817 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1818 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001819 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 showcmd_visual = TRUE;
1821 }
1822 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 showcmd_buf[0] = NUL;
1825 showcmd_visual = FALSE;
1826
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001827 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (showcmd_is_clear)
1829 return;
1830 }
1831
1832 display_showcmd();
1833}
1834
1835/*
1836 * Add 'c' to string of shown command chars.
1837 * Return TRUE if output has been written (and setcursor() has been called).
1838 */
1839 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001840add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 char_u *p;
1843 int old_len;
1844 int extra_len;
1845 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int i;
1847 static int ignore[] =
1848 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001849#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1851 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001852#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001853 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001854 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1856 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001857 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001859 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 0
1861 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
Bram Moolenaar09df3122006-01-23 22:23:09 +00001863 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 return FALSE;
1865
1866 if (showcmd_visual)
1867 {
1868 showcmd_buf[0] = NUL;
1869 showcmd_visual = FALSE;
1870 }
1871
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001872 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (IS_SPECIAL(c))
1874 for (i = 0; ignore[i] != 0; ++i)
1875 if (ignore[i] == c)
1876 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001879 if (*p == ' ')
1880 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 old_len = (int)STRLEN(showcmd_buf);
1882 extra_len = (int)STRLEN(p);
1883 overflow = old_len + extra_len - SHOWCMD_COLS;
1884 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001885 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1886 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 STRCAT(showcmd_buf, p);
1888
1889 if (char_avail())
1890 return FALSE;
1891
1892 display_showcmd();
1893
1894 return TRUE;
1895}
1896
1897 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001898add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899{
1900 if (!add_to_showcmd(c))
1901 setcursor();
1902}
1903
1904/*
1905 * Delete 'len' characters from the end of the shown command.
1906 */
1907 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001908del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909{
1910 int old_len;
1911
1912 if (!p_sc)
1913 return;
1914
1915 old_len = (int)STRLEN(showcmd_buf);
1916 if (len > old_len)
1917 len = old_len;
1918 showcmd_buf[old_len - len] = NUL;
1919
1920 if (!char_avail())
1921 display_showcmd();
1922}
1923
1924/*
1925 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1926 * something and there is a partial mapping.
1927 */
1928 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001929push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
1931 if (p_sc)
1932 STRCPY(old_showcmd_buf, showcmd_buf);
1933}
1934
1935 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001936pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937{
1938 if (!p_sc)
1939 return;
1940
1941 STRCPY(showcmd_buf, old_showcmd_buf);
1942
1943 display_showcmd();
1944}
1945
1946 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001947display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 int len;
1950
1951 cursor_off();
1952
1953 len = (int)STRLEN(showcmd_buf);
1954 if (len == 0)
1955 showcmd_is_clear = TRUE;
1956 else
1957 {
1958 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1959 showcmd_is_clear = FALSE;
1960 }
1961
1962 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001963 * clear the rest of an old message by outputting up to SHOWCMD_COLS
1964 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 */
1966 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001968 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969}
1970#endif
1971
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972/*
1973 * When "check" is FALSE, prepare for commands that scroll the window.
1974 * When "check" is TRUE, take care of scroll-binding after the window has
1975 * scrolled. Called from normal_cmd() and edit().
1976 */
1977 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001978do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979{
1980 static win_T *old_curwin = NULL;
1981 static linenr_T old_topline = 0;
1982#ifdef FEAT_DIFF
1983 static int old_topfill = 0;
1984#endif
1985 static buf_T *old_buf = NULL;
1986 static colnr_T old_leftcol = 0;
1987
1988 if (check && curwin->w_p_scb)
1989 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001990 // If a ":syncbind" command was just used, don't scroll, only reset
1991 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (did_syncbind)
1993 did_syncbind = FALSE;
1994 else if (curwin == old_curwin)
1995 {
1996 /*
1997 * Synchronize other windows, as necessary according to
1998 * 'scrollbind'. Don't do this after an ":edit" command, except
1999 * when 'diff' is set.
2000 */
2001 if ((curwin->w_buffer == old_buf
2002#ifdef FEAT_DIFF
2003 || curwin->w_p_diff
2004#endif
2005 )
2006 && (curwin->w_topline != old_topline
2007#ifdef FEAT_DIFF
2008 || curwin->w_topfill != old_topfill
2009#endif
2010 || curwin->w_leftcol != old_leftcol))
2011 {
2012 check_scrollbind(curwin->w_topline - old_topline,
2013 (long)(curwin->w_leftcol - old_leftcol));
2014 }
2015 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002016 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
2018 /*
2019 * When switching between windows, make sure that the relative
2020 * vertical offset is valid for the new window. The relative
2021 * offset is invalid whenever another 'scrollbind' window has
2022 * scrolled to a point that would force the current window to
2023 * scroll past the beginning or end of its buffer. When the
2024 * resync is performed, some of the other 'scrollbind' windows may
2025 * need to jump so that the current window's relative position is
2026 * visible on-screen.
2027 */
2028 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2029 }
2030 curwin->w_scbind_pos = curwin->w_topline;
2031 }
2032
2033 old_curwin = curwin;
2034 old_topline = curwin->w_topline;
2035#ifdef FEAT_DIFF
2036 old_topfill = curwin->w_topfill;
2037#endif
2038 old_buf = curwin->w_buffer;
2039 old_leftcol = curwin->w_leftcol;
2040}
2041
2042/*
2043 * Synchronize any windows that have "scrollbind" set, based on the
2044 * number of rows by which the current window has changed
2045 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2046 */
2047 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002048check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050 int want_ver;
2051 int want_hor;
2052 win_T *old_curwin = curwin;
2053 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 int old_VIsual_select = VIsual_select;
2055 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 colnr_T tgt_leftcol = curwin->w_leftcol;
2057 long topline;
2058 long y;
2059
2060 /*
2061 * check 'scrollopt' string for vertical and horizontal scroll options
2062 */
2063 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2064#ifdef FEAT_DIFF
2065 want_ver |= old_curwin->w_p_diff;
2066#endif
2067 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2068
2069 /*
2070 * loop through the scrollbound windows and scroll accordingly
2071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002073 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 {
2075 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002076 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 if (curwin != old_curwin && curwin->w_p_scb)
2078 {
2079 /*
2080 * do the vertical scroll
2081 */
2082 if (want_ver)
2083 {
2084#ifdef FEAT_DIFF
2085 if (old_curwin->w_p_diff && curwin->w_p_diff)
2086 {
2087 diff_set_topline(old_curwin, curwin);
2088 }
2089 else
2090#endif
2091 {
2092 curwin->w_scbind_pos += topline_diff;
2093 topline = curwin->w_scbind_pos;
2094 if (topline > curbuf->b_ml.ml_line_count)
2095 topline = curbuf->b_ml.ml_line_count;
2096 if (topline < 1)
2097 topline = 1;
2098
2099 y = topline - curwin->w_topline;
2100 if (y > 0)
2101 scrollup(y, FALSE);
2102 else
2103 scrolldown(-y, FALSE);
2104 }
2105
2106 redraw_later(VALID);
2107 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110
2111 /*
2112 * do the horizontal scroll
2113 */
2114 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2115 {
2116 curwin->w_leftcol = tgt_leftcol;
2117 leftcol_changed();
2118 }
2119 }
2120 }
2121
2122 /*
2123 * reset current-window
2124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 VIsual_select = old_VIsual_select;
2126 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 curwin = old_curwin;
2128 curbuf = old_curbuf;
2129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
2131/*
2132 * Command character that's ignored.
2133 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002134 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002137nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002139 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140}
2141
2142/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002143 * Command character that doesn't do anything, but unlike nv_ignore() does
2144 * start edit(). Used for "startinsert" executed while starting up.
2145 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002146 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002148{
2149}
2150
2151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 * Command character doesn't exist.
2153 */
2154 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002155nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157 clearopbeep(cap->oap);
2158}
2159
2160/*
2161 * <Help> and <F1> commands.
2162 */
2163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002164nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 if (!checkclearopq(cap->oap))
2167 ex_help(NULL);
2168}
2169
2170/*
2171 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2172 */
2173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002174nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002176#ifdef FEAT_JOB_CHANNEL
2177 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2178 clearopbeep(cap->oap);
2179 else
2180#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002181 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002182 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002183 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002184 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2185 op_addsub(cap->oap, cap->count1, cap->arg);
2186 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002187 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002188 else if (VIsual_active)
2189 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002190 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002191 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192}
2193
2194/*
2195 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2196 */
2197 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002198nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002202 if (mod_mask & MOD_MASK_CTRL)
2203 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002205 if (cap->arg == BACKWARD)
2206 goto_tabpage(-(int)cap->count1);
2207 else
2208 goto_tabpage((int)cap->count0);
2209 }
2210 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002211 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213}
2214
2215/*
2216 * Implementation of "gd" and "gD" command.
2217 */
2218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002219nv_gd(
2220 oparg_T *oap,
2221 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002222 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 char_u *ptr;
2226
Bram Moolenaard9d30582005-05-18 22:10:28 +00002227 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002228 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2229 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002231#ifdef FEAT_FOLDING
2232 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2233 foldOpenCursor();
2234#endif
2235}
2236
2237/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002238 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2239 * otherwise.
2240 */
2241 static int
2242is_ident(char_u *line, int offset)
2243{
2244 int i;
2245 int incomment = FALSE;
2246 int instring = 0;
2247 int prev = 0;
2248
2249 for (i = 0; i < offset && line[i] != NUL; i++)
2250 {
2251 if (instring != 0)
2252 {
2253 if (prev != '\\' && line[i] == instring)
2254 instring = 0;
2255 }
2256 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2257 {
2258 instring = line[i];
2259 }
2260 else
2261 {
2262 if (incomment)
2263 {
2264 if (prev == '*' && line[i] == '/')
2265 incomment = FALSE;
2266 }
2267 else if (prev == '/' && line[i] == '*')
2268 {
2269 incomment = TRUE;
2270 }
2271 else if (prev == '/' && line[i] == '/')
2272 {
2273 return FALSE;
2274 }
2275 }
2276
2277 prev = line[i];
2278 }
2279
2280 return incomment == FALSE && instring == 0;
2281}
2282
2283/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002284 * Search for variable declaration of "ptr[len]".
2285 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2286 * current file ("gD").
2287 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002288 * Return FAIL when not found.
2289 */
2290 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291find_decl(
2292 char_u *ptr,
2293 int len,
2294 int locally,
2295 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002296 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002297{
2298 char_u *pat;
2299 pos_T old_pos;
2300 pos_T par_pos;
2301 pos_T found_pos;
2302 int t;
2303 int save_p_ws;
2304 int save_p_scs;
2305 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002306 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002307 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002308 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002309
2310 if ((pat = alloc(len + 7)) == NULL)
2311 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002312
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002313 // Put "\V" before the pattern to avoid that the special meaning of "."
2314 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002315 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2316 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 old_pos = curwin->w_cursor;
2318 save_p_ws = p_ws;
2319 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002320 p_ws = FALSE; // don't wrap around end of file now
2321 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 /*
2324 * With "gD" go to line 1.
2325 * With "gd" Search back for the start of the current function, then go
2326 * back until a blank line. If this fails go to line 1.
2327 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002328 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002330 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002332 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334 else
2335 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002336 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2338 --curwin->w_cursor.lnum;
2339 }
2340 curwin->w_cursor.col = 0;
2341
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002342 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002343 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002344 for (;;)
2345 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002346 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002347 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002348 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002349 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002350
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002351 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002352 {
2353 pos_T *pos;
2354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002355 // Check that the block the match is in doesn't end before the
2356 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002357 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2358 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2359 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002360 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002361 // There can't be a useful match before the end of this block.
2362 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002363 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002364 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002365 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002366 }
2367
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002368 if (t == FAIL)
2369 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002370 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002371 if (found_pos.lnum != 0)
2372 {
2373 curwin->w_cursor = found_pos;
2374 t = OK;
2375 }
2376 break;
2377 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002378 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002379 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002380 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002381 ++curwin->w_cursor.lnum;
2382 curwin->w_cursor.col = 0;
2383 continue;
2384 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002385 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002387 // If the current position is not a valid identifier and a previous
2388 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002389 if (!valid && found_pos.lnum != 0)
2390 {
2391 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002392 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002393 }
2394
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002396 if (valid && !locally)
2397 break;
2398 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002399 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002400 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002401 if (found_pos.lnum != 0)
2402 curwin->w_cursor = found_pos;
2403 break;
2404 }
2405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002406 // For finding a local variable and the match is before the "{" or
2407 // inside a comment, continue searching. For K&R style function
2408 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002409 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002410 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002411 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002412 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002413 // Remove SEARCH_START from flags to avoid getting stuck at one
2414 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002415 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002417
2418 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002420 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 curwin->w_cursor = old_pos;
2422 }
2423 else
2424 {
2425 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002426 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 reset_search_dir();
2428 }
2429
2430 vim_free(pat);
2431 p_ws = save_p_ws;
2432 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002433
2434 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435}
2436
2437/*
2438 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2439 * lines rather than lines in the file.
2440 * 'dist' must be positive.
2441 *
2442 * Return OK if able to move cursor, FAIL otherwise.
2443 */
2444 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002445nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446{
2447 int linelen = linetabsize(ml_get_curline());
2448 int retval = OK;
2449 int atend = FALSE;
2450 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002451 int col_off1; // margin offset for first screen line
2452 int col_off2; // margin offset for wrapped screen line
2453 int width1; // text width for first screen line
2454 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
2456 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002457 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 col_off1 = curwin_col_off();
2460 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002461 width1 = curwin->w_width - col_off1;
2462 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002463 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002464 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 /*
2469 * Instead of sticking at the last character of the buffer line we
2470 * try to stick in the last column of the screen.
2471 */
2472 if (curwin->w_curswant == MAXCOL)
2473 {
2474 atend = TRUE;
2475 validate_virtcol();
2476 if (width1 <= 0)
2477 curwin->w_curswant = 0;
2478 else
2479 {
2480 curwin->w_curswant = width1 - 1;
2481 if (curwin->w_virtcol > curwin->w_curswant)
2482 curwin->w_curswant += ((curwin->w_virtcol
2483 - curwin->w_curswant - 1) / width2 + 1) * width2;
2484 }
2485 }
2486 else
2487 {
2488 if (linelen > width1)
2489 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2490 else
2491 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002492 if (curwin->w_curswant >= (colnr_T)n)
2493 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495
2496 while (dist--)
2497 {
2498 if (dir == BACKWARD)
2499 {
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002500 if ((long)curwin->w_curswant >= width1)
2501 // Move back within the line. This can give a negative value
2502 // for w_curswant if width1 < width2 (with cpoptions+=n),
2503 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 curwin->w_curswant -= width2;
2505 else
2506 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002507 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 if (curwin->w_cursor.lnum == 1)
2509 {
2510 retval = FAIL;
2511 break;
2512 }
2513 --curwin->w_cursor.lnum;
2514#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002515 // Move to the start of a closed fold. Don't do that when
2516 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (!(fdo_flags & FDO_ALL))
2518 (void)hasFolding(curwin->w_cursor.lnum,
2519 &curwin->w_cursor.lnum, NULL);
2520#endif
2521 linelen = linetabsize(ml_get_curline());
2522 if (linelen > width1)
2523 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2524 + 1) * width2;
2525 }
2526 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002527 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
2529 if (linelen > width1)
2530 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2531 else
2532 n = width1;
2533 if (curwin->w_curswant + width2 < (colnr_T)n)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002534 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 curwin->w_curswant += width2;
2536 else
2537 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002538 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002540 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2542 &curwin->w_cursor.lnum);
2543#endif
2544 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2545 {
2546 retval = FAIL;
2547 break;
2548 }
2549 curwin->w_cursor.lnum++;
2550 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002551 // Check if the cursor has moved below the number display
2552 // when width1 < width2 (with cpoptions+=n). Subtract width2
2553 // to get a negative value for w_curswant, which will get
2554 // clipped to column 0.
2555 if (curwin->w_curswant >= width1)
2556 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002557 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
2559 }
2560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002563 if (virtual_active() && atend)
2564 coladvance(MAXCOL);
2565 else
2566 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2569 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002570 colnr_T virtcol;
2571
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 /*
2573 * Check for landing on a character that got split at the end of the
2574 * last line. We want to advance a screenline, not end up in the same
2575 * screenline or move two screenlines.
2576 */
2577 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002578 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002579#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002580 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2581 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002582#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002583
2584 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 && (curwin->w_curswant < (colnr_T)width1
2586 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2587 : ((curwin->w_curswant - width1) % width2
2588 > (colnr_T)width2 / 2)))
2589 --curwin->w_cursor.col;
2590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002593 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
2595 return retval;
2596}
2597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598/*
2599 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2600 * cap->arg must be TRUE for CTRL-E.
2601 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002603nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604{
2605 if (!checkclearop(cap->oap))
2606 scroll_redraw(cap->arg, cap->count1);
2607}
2608
2609/*
2610 * Scroll "count" lines up or down, and redraw.
2611 */
2612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002613scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 linenr_T prev_topline = curwin->w_topline;
2616#ifdef FEAT_DIFF
2617 int prev_topfill = curwin->w_topfill;
2618#endif
2619 linenr_T prev_lnum = curwin->w_cursor.lnum;
2620
2621 if (up)
2622 scrollup(count, TRUE);
2623 else
2624 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002625 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002627 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2628 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 cursor_correct();
2630 check_cursor_moved(curwin);
2631 curwin->w_valid |= VALID_TOPLINE;
2632
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002633 // If moved back to where we were, at least move the cursor, otherwise
2634 // we get stuck at one position. Don't move the cursor up if the
2635 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 while (curwin->w_topline == prev_topline
2637#ifdef FEAT_DIFF
2638 && curwin->w_topfill == prev_topfill
2639#endif
2640 )
2641 {
2642 if (up)
2643 {
2644 if (curwin->w_cursor.lnum > prev_lnum
2645 || cursor_down(1L, FALSE) == FAIL)
2646 break;
2647 }
2648 else
2649 {
2650 if (curwin->w_cursor.lnum < prev_lnum
2651 || prev_topline == 1L
2652 || cursor_up(1L, FALSE) == FAIL)
2653 break;
2654 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002655 // Mark w_topline as valid, otherwise the screen jumps back at the
2656 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 check_cursor_moved(curwin);
2658 curwin->w_valid |= VALID_TOPLINE;
2659 }
2660 }
2661 if (curwin->w_cursor.lnum != prev_lnum)
2662 coladvance(curwin->w_curswant);
2663 redraw_later(VALID);
2664}
2665
2666/*
2667 * Commands that start with "z".
2668 */
2669 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002670nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671{
2672 long n;
2673 colnr_T col;
2674 int nchar = cap->nchar;
2675#ifdef FEAT_FOLDING
2676 long old_fdl = curwin->w_p_fdl;
2677 int old_fen = curwin->w_p_fen;
2678#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002679#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002680 int undo = FALSE;
2681#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002682 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
2684 if (VIM_ISDIGIT(nchar))
2685 {
2686 /*
2687 * "z123{nchar}": edit the count before obtaining {nchar}
2688 */
2689 if (checkclearop(cap->oap))
2690 return;
2691 n = nchar - '0';
2692 for (;;)
2693 {
2694#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002695 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696#endif
2697 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002698 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002699 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 --no_mapping;
2702 --allow_keys;
2703#ifdef FEAT_CMDL_INFO
2704 (void)add_to_showcmd(nchar);
2705#endif
2706 if (nchar == K_DEL || nchar == K_KDEL)
2707 n /= 10;
2708 else if (VIM_ISDIGIT(nchar))
2709 n = n * 10 + (nchar - '0');
2710 else if (nchar == CAR)
2711 {
2712#ifdef FEAT_GUI
2713 need_mouse_correct = TRUE;
2714#endif
2715 win_setheight((int)n);
2716 break;
2717 }
2718 else if (nchar == 'l'
2719 || nchar == 'h'
2720 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002721 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 {
2723 cap->count1 = n ? n * cap->count1 : cap->count1;
2724 goto dozet;
2725 }
2726 else
2727 {
2728 clearopbeep(cap->oap);
2729 break;
2730 }
2731 }
2732 cap->oap->op_type = OP_NOP;
2733 return;
2734 }
2735
2736dozet:
2737 if (
2738#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002739 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2740 // and "zC" only in Visual mode. "zj" and "zk" are motion
2741 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 cap->nchar != 'f' && cap->nchar != 'F'
2743 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2744 && cap->nchar != 'j' && cap->nchar != 'k'
2745 &&
2746#endif
2747 checkclearop(cap->oap))
2748 return;
2749
2750 /*
2751 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2752 * If line number given, set cursor.
2753 */
2754 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2755 && cap->count0
2756 && cap->count0 != curwin->w_cursor.lnum)
2757 {
2758 setpcmark();
2759 if (cap->count0 > curbuf->b_ml.ml_line_count)
2760 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2761 else
2762 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002763 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
2765
2766 switch (nchar)
2767 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002768 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 case '+':
2770 if (cap->count0 == 0)
2771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // No count given: put cursor at the line below screen
2773 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2775 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2776 else
2777 curwin->w_cursor.lnum = curwin->w_botline;
2778 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 case NL:
2781 case CAR:
2782 case K_KENTER:
2783 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002784 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786 case 't': scroll_cursor_top(0, TRUE);
2787 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002788 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 break;
2790
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002791 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 case 'z': scroll_cursor_halfway(TRUE);
2796 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002797 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 break;
2799
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002800 // "z^", "z-" and "zb": put cursor at bottom of screen
2801 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2802 // when <count> is at bottom of window, and puts that one at
2803 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (cap->count0 != 0)
2805 {
2806 scroll_cursor_bot(0, TRUE);
2807 curwin->w_cursor.lnum = curwin->w_topline;
2808 }
2809 else if (curwin->w_topline == 1)
2810 curwin->w_cursor.lnum = 1;
2811 else
2812 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002813 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 case '-':
2815 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002816 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
2818 case 'b': scroll_cursor_bot(0, TRUE);
2819 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002820 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 break;
2822
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002825 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002826 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 case 'h':
2830 case K_LEFT:
2831 if (!curwin->w_p_wrap)
2832 {
2833 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2834 curwin->w_leftcol = 0;
2835 else
2836 curwin->w_leftcol -= (colnr_T)cap->count1;
2837 leftcol_changed();
2838 }
2839 break;
2840
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002842 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002843 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 case 'l':
2847 case K_RIGHT:
2848 if (!curwin->w_p_wrap)
2849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 curwin->w_leftcol += (colnr_T)cap->count1;
2852 leftcol_changed();
2853 }
2854 break;
2855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 case 's': if (!curwin->w_p_wrap)
2858 {
2859#ifdef FEAT_FOLDING
2860 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 else
2863#endif
2864 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002865 if ((long)col > siso)
2866 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 else
2868 col = 0;
2869 if (curwin->w_leftcol != col)
2870 {
2871 curwin->w_leftcol = col;
2872 redraw_later(NOT_VALID);
2873 }
2874 }
2875 break;
2876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 case 'e': if (!curwin->w_p_wrap)
2879 {
2880#ifdef FEAT_FOLDING
2881 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 else
2884#endif
2885 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002886 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002887 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 col = 0;
2889 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002890 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 if (curwin->w_leftcol != col)
2892 {
2893 curwin->w_leftcol = col;
2894 redraw_later(NOT_VALID);
2895 }
2896 }
2897 break;
2898
2899#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 // "zF": create fold command
2901 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 case 'F':
2903 case 'f': if (foldManualAllowed(TRUE))
2904 {
2905 cap->nchar = 'f';
2906 nv_operator(cap);
2907 curwin->w_p_fen = TRUE;
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2911 {
2912 nv_operator(cap);
2913 finish_op = TRUE;
2914 }
2915 }
2916 else
2917 clearopbeep(cap->oap);
2918 break;
2919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // "zd": delete fold at cursor
2921 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 case 'd':
2923 case 'D': if (foldManualAllowed(FALSE))
2924 {
2925 if (VIsual_active)
2926 nv_operator(cap);
2927 else
2928 deleteFold(curwin->w_cursor.lnum,
2929 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2930 }
2931 break;
2932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 case 'E': if (foldmethodIsManual(curwin))
2935 {
2936 clearFolding(curwin);
2937 changed_window_setting();
2938 }
2939 else if (foldmethodIsMarker(curwin))
2940 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2941 TRUE, FALSE);
2942 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002943 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 break;
2945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 case 'n': curwin->w_p_fen = FALSE;
2948 break;
2949
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002950 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 case 'N': curwin->w_p_fen = TRUE;
2952 break;
2953
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2956 break;
2957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2960 openFold(curwin->w_cursor.lnum, cap->count1);
2961 else
2962 {
2963 closeFold(curwin->w_cursor.lnum, cap->count1);
2964 curwin->w_p_fen = TRUE;
2965 }
2966 break;
2967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2970 openFoldRecurse(curwin->w_cursor.lnum);
2971 else
2972 {
2973 closeFoldRecurse(curwin->w_cursor.lnum);
2974 curwin->w_p_fen = TRUE;
2975 }
2976 break;
2977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 case 'o': if (VIsual_active)
2980 nv_operator(cap);
2981 else
2982 openFold(curwin->w_cursor.lnum, cap->count1);
2983 break;
2984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 case 'O': if (VIsual_active)
2987 nv_operator(cap);
2988 else
2989 openFoldRecurse(curwin->w_cursor.lnum);
2990 break;
2991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002992 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 case 'c': if (VIsual_active)
2994 nv_operator(cap);
2995 else
2996 closeFold(curwin->w_cursor.lnum, cap->count1);
2997 curwin->w_p_fen = TRUE;
2998 break;
2999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 case 'C': if (VIsual_active)
3002 nv_operator(cap);
3003 else
3004 closeFoldRecurse(curwin->w_cursor.lnum);
3005 curwin->w_p_fen = TRUE;
3006 break;
3007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003008 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 case 'v': foldOpenCursor();
3010 break;
3011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003014 curwin->w_foldinvalid = TRUE; // recompute folds
3015 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 foldOpenCursor();
3017 break;
3018
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003019 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003021 curwin->w_foldinvalid = TRUE; // recompute folds
3022 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 break;
3024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003027 {
3028 curwin->w_p_fdl -= cap->count1;
3029 if (curwin->w_p_fdl < 0)
3030 curwin->w_p_fdl = 0;
3031 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003032 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 curwin->w_p_fen = TRUE;
3034 break;
3035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003038 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 curwin->w_p_fen = TRUE;
3040 break;
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003043 case 'r': curwin->w_p_fdl += cap->count1;
3044 {
3045 int d = getDeepestNesting();
3046
3047 if (curwin->w_p_fdl >= d)
3048 curwin->w_p_fdl = d;
3049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 break;
3051
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003052 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003054 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 break;
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 case 'j': // "zj" move to next fold downwards
3058 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3060 cap->count1) == FAIL)
3061 clearopbeep(cap->oap);
3062 break;
3063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003064#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003066#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003067 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003068 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003070 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003071 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003072 --no_mapping;
3073 --allow_keys;
3074#ifdef FEAT_CMDL_INFO
3075 (void)add_to_showcmd(nchar);
3076#endif
3077 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3078 {
3079 clearopbeep(cap->oap);
3080 break;
3081 }
3082 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 case 'g': // "zg": add good word to word list
3086 case 'w': // "zw": add wrong word to word list
3087 case 'G': // "zG": add good word to temp word list
3088 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003089 {
3090 char_u *ptr = NULL;
3091 int len;
3092
3093 if (checkclearop(cap->oap))
3094 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003095 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3096 == FAIL)
3097 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003098 if (ptr == NULL)
3099 {
3100 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 // Find bad word under the cursor. When 'spell' is
3103 // off this fails and find_ident_under_cursor() is
3104 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003105 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003106 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003107 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003108 if (len != 0 && curwin->w_cursor.col <= pos.col)
3109 ptr = ml_get_pos(&curwin->w_cursor);
3110 curwin->w_cursor = pos;
3111 }
3112
Bram Moolenaarb765d632005-06-07 21:00:02 +00003113 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3114 FIND_IDENT)) == 0)
3115 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003116 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3117 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003118 (nchar == 'G' || nchar == 'W')
3119 ? 0 : (int)cap->count1,
3120 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003121 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003122 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003124 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003125 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003126 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003127 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003128#endif
3129
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 default: clearopbeep(cap->oap);
3131 }
3132
3133#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 if (old_fen != curwin->w_p_fen)
3136 {
3137# ifdef FEAT_DIFF
3138 win_T *wp;
3139
3140 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003142 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 FOR_ALL_WINDOWS(wp)
3144 {
3145 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3146 {
3147 wp->w_p_fen = curwin->w_p_fen;
3148 changed_window_setting_win(wp);
3149 }
3150 }
3151 }
3152# endif
3153 changed_window_setting();
3154 }
3155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003156 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (old_fdl != curwin->w_p_fdl)
3158 newFoldLevel();
3159#endif
3160}
3161
3162#ifdef FEAT_GUI
3163/*
3164 * Vertical scrollbar movement.
3165 */
3166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003167nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168{
3169 if (cap->oap->op_type != OP_NOP)
3170 clearopbeep(cap->oap);
3171
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003172 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 gui_do_scroll();
3174}
3175
3176/*
3177 * Horizontal scrollbar movement.
3178 */
3179 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003180nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 if (cap->oap->op_type != OP_NOP)
3183 clearopbeep(cap->oap);
3184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003185 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003186 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187}
3188#endif
3189
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003190#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003191/*
3192 * Click in GUI tab.
3193 */
3194 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003195nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003196{
3197 if (cap->oap->op_type != OP_NOP)
3198 clearopbeep(cap->oap);
3199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003200 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003201 goto_tabpage(current_tab);
3202}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003203
3204/*
3205 * Selected item in tab line menu.
3206 */
3207 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003208nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003209{
3210 if (cap->oap->op_type != OP_NOP)
3211 clearopbeep(cap->oap);
3212
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003213 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003214 handle_tabmenu();
3215}
3216
3217/*
3218 * Handle selecting an item of the GUI tab line menu.
3219 * Used in Normal and Insert mode.
3220 */
3221 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003222handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003223{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003224 switch (current_tabmenu)
3225 {
3226 case TABLINE_MENU_CLOSE:
3227 if (current_tab == 0)
3228 do_cmdline_cmd((char_u *)"tabclose");
3229 else
3230 {
3231 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3232 current_tab);
3233 do_cmdline_cmd(IObuff);
3234 }
3235 break;
3236
3237 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003238 if (current_tab == 0)
3239 do_cmdline_cmd((char_u *)"$tabnew");
3240 else
3241 {
3242 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3243 current_tab - 1);
3244 do_cmdline_cmd(IObuff);
3245 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003246 break;
3247
3248 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003249 if (current_tab == 0)
3250 do_cmdline_cmd((char_u *)"browse $tabnew");
3251 else
3252 {
3253 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3254 current_tab - 1);
3255 do_cmdline_cmd(IObuff);
3256 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003257 break;
3258 }
3259}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003260#endif
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262/*
3263 * "Q" command.
3264 */
3265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003266nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
3268 /*
3269 * Ignore 'Q' in Visual mode, just give a beep.
3270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003272 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003273 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 do_exmode(FALSE);
3275}
3276
3277/*
3278 * Handle a ":" command.
3279 */
3280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003281nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003284 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (VIsual_active)
3287 nv_operator(cap);
3288 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 if (cap->oap->op_type != OP_NOP)
3291 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003292 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 cap->oap->motion_type = MCHAR;
3294 cap->oap->inclusive = FALSE;
3295 }
3296 else if (cap->count0)
3297 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003298 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 stuffcharReadbuff('.');
3300 if (cap->count0 > 1)
3301 {
3302 stuffReadbuff((char_u *)",.+");
3303 stuffnumReadbuff((long)cap->count0 - 1L);
3304 }
3305 }
3306
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003307 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (KeyTyped)
3309 compute_cmdrow();
3310
3311 old_p_im = p_im;
3312
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003313 // get a command line and execute it
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003314 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3316
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003317 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (p_im != old_p_im)
3319 {
3320 if (p_im)
3321 restart_edit = 'i';
3322 else
3323 restart_edit = 0;
3324 }
3325
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003326 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003327 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003328 clearop(cap->oap);
3329 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3331 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003332 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3333 || did_emsg
3334 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003335 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 clearopbeep(cap->oap);
3337 }
3338}
3339
3340/*
3341 * Handle CTRL-G command.
3342 */
3343 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003344nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003346 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 {
3348 VIsual_select = !VIsual_select;
3349 showmode();
3350 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003351 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003352 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 fileinfo((int)cap->count0, FALSE, TRUE);
3354}
3355
3356/*
3357 * Handle CTRL-H <Backspace> command.
3358 */
3359 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003360nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (VIsual_active && VIsual_select)
3363 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003364 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 v_visop(cap);
3366 }
3367 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 nv_left(cap);
3369}
3370
3371/*
3372 * CTRL-L: clear screen and redraw.
3373 */
3374 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003375nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
3377 if (!checkclearop(cap->oap))
3378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003380 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003381 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003382# ifdef FEAT_RELTIME
3383 {
3384 win_T *wp;
3385
3386 FOR_ALL_WINDOWS(wp)
3387 wp->w_s->b_syn_slow = FALSE;
3388 }
3389# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390#endif
3391 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003392#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3393# ifdef VIMDLL
3394 if (!gui.in_use)
3395# endif
3396 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 }
3399}
3400
3401/*
3402 * CTRL-O: In Select mode: switch to Visual mode for one command.
3403 * Otherwise: Go to older pcmark.
3404 */
3405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003406nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (VIsual_active && VIsual_select)
3409 {
3410 VIsual_select = FALSE;
3411 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003412 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 {
3416 cap->count1 = -cap->count1;
3417 nv_pcmark(cap);
3418 }
3419}
3420
3421/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003422 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3423 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 */
3425 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003426nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
3428 if (!checkclearopq(cap->oap))
3429 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3430 GETF_SETMARK|GETF_ALT, FALSE);
3431}
3432
3433/*
3434 * "Z" commands.
3435 */
3436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003437nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 if (!checkclearopq(cap->oap))
3440 {
3441 switch (cap->nchar)
3442 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 case 'Z': do_cmdline_cmd((char_u *)"x");
3445 break;
3446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003447 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 case 'Q': do_cmdline_cmd((char_u *)"q!");
3449 break;
3450
3451 default: clearopbeep(cap->oap);
3452 }
3453 }
3454}
3455
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456/*
3457 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3458 */
3459 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003460do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 oparg_T oa;
3463 cmdarg_T ca;
3464
3465 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003466 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 ca.oap = &oa;
3468 ca.cmdchar = c1;
3469 ca.nchar = c2;
3470 nv_ident(&ca);
3471}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473/*
3474 * Handle the commands that use the word under the cursor.
3475 * [g] CTRL-] :ta to current identifier
3476 * [g] 'K' run program for current identifier
3477 * [g] '*' / to current identifier or string
3478 * [g] '#' ? to current identifier or string
3479 * g ']' :tselect for current identifier
3480 */
3481 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003482nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 char_u *ptr = NULL;
3485 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003486 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003487 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003489 char_u *kp; // value of 'keywordprg'
3490 int kp_help; // 'keywordprg' is ":he"
3491 int kp_ex; // 'keywordprg' starts with ":"
3492 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003495 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 char_u *aux_ptr;
3497 int isman;
3498 int isman_s;
3499
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003500 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 cmdchar = cap->nchar;
3503 g_cmd = TRUE;
3504 }
3505 else
3506 {
3507 cmdchar = cap->cmdchar;
3508 g_cmd = FALSE;
3509 }
3510
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003511 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 cmdchar = '#';
3513
3514 /*
3515 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3516 */
3517 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (checkclearopq(cap->oap))
3522 return;
3523 }
3524
3525 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3526 (cmdchar == '*' || cmdchar == '#')
3527 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3528 {
3529 clearop(cap->oap);
3530 return;
3531 }
3532
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003533 // Allocate buffer to put the command in. Inserting backslashes can
3534 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3535 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3537 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3538 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003539 if (kp_help && *skipwhite(ptr) == NUL)
3540 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003541 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003542 return;
3543 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003544 kp_ex = (*kp == ':');
3545 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3546 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (buf == NULL)
3548 return;
3549 buf[0] = NUL;
3550
3551 switch (cmdchar)
3552 {
3553 case '*':
3554 case '#':
3555 /*
3556 * Put cursor at start of word, makes search skip the word
3557 * under the cursor.
3558 * Call setpcmark() first, so "*``" puts the cursor back where
3559 * it was.
3560 */
3561 setpcmark();
3562 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3563
3564 if (!g_cmd && vim_iswordp(ptr))
3565 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003566 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 break;
3568
3569 case 'K':
3570 if (kp_help)
3571 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003572 else if (kp_ex)
3573 {
3574 if (cap->count0 != 0)
3575 vim_snprintf((char *)buf, buflen, "%s %ld",
3576 kp, cap->count0);
3577 else
3578 STRCPY(buf, kp);
3579 STRCAT(buf, " ");
3580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 else
3582 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003583 // An external command will probably use an argument starting
3584 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003585 while (*ptr == '-' && n > 0)
3586 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003587 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003588 --n;
3589 }
3590 if (n == 0)
3591 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003592 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003593 vim_free(buf);
3594 return;
3595 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003596
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003597 // When a count is given, turn it into a range. Is this
3598 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 isman = (STRCMP(kp, "man") == 0);
3600 isman_s = (STRCMP(kp, "man -s") == 0);
3601 if (cap->count0 != 0 && !(isman || isman_s))
3602 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3603
3604 STRCAT(buf, "! ");
3605 if (cap->count0 == 0 && isman_s)
3606 STRCAT(buf, "man");
3607 else
3608 STRCAT(buf, kp);
3609 STRCAT(buf, " ");
3610 if (cap->count0 != 0 && (isman || isman_s))
3611 {
3612 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3613 STRCAT(buf, " ");
3614 }
3615 }
3616 break;
3617
3618 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003619 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620#ifdef FEAT_CSCOPE
3621 if (p_cst)
3622 STRCPY(buf, "cstag ");
3623 else
3624#endif
3625 STRCPY(buf, "ts ");
3626 break;
3627
3628 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003629 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (curbuf->b_help)
3631 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003633 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003634 if (g_cmd)
3635 STRCPY(buf, "tj ");
3636 else
3637 sprintf((char *)buf, "%ldta ", cap->count0);
3638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640
3641 /*
3642 * Now grab the chars in the identifier
3643 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003644 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003646 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003647 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003648 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003649 p = vim_strsave_fnameescape(ptr, FALSE);
3650 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003651 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003652 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003653 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003654 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003656 vim_free(buf);
3657 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003659 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003660 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003661 {
3662 vim_free(buf);
3663 vim_free(p);
3664 return;
3665 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003666 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003667 STRCAT(buf, p);
3668 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003670 else
3671 {
3672 if (cmdchar == '*')
3673 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
3674 else if (cmdchar == '#')
3675 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003676 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003677 {
3678 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003679 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003680 aux_ptr = (char_u *)"";
3681 else
3682 aux_ptr = (char_u *)"\\|\"\n[";
3683 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003684 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003685 aux_ptr = (char_u *)"\\|\"\n*?[";
3686
3687 p = buf + STRLEN(buf);
3688 while (n-- > 0)
3689 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003690 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003691 if (vim_strchr(aux_ptr, *ptr) != NULL)
3692 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003693 // When current byte is a part of multibyte character, copy all
3694 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003695 if (has_mbyte)
3696 {
3697 int i;
3698 int len = (*mb_ptr2len)(ptr) - 1;
3699
3700 for (i = 0; i < len && n >= 1; ++i, --n)
3701 *p++ = *ptr++;
3702 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003703 *p++ = *ptr++;
3704 }
3705 *p = NUL;
3706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 /*
3709 * Execute the command.
3710 */
3711 if (cmdchar == '*' || cmdchar == '#')
3712 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003713 if (!g_cmd && (has_mbyte
3714 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3715 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003717
3718 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003719 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003721
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003722 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 }
3724 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003725 {
3726 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003728 g_tag_at_cursor = FALSE;
3729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
3731 vim_free(buf);
3732}
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734/*
3735 * Get visually selected text, within one line only.
3736 * Returns FAIL if more than one line selected.
3737 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003738 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003739get_visual_text(
3740 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003741 char_u **pp, // return: start of selected text
3742 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
3744 if (VIsual_mode != 'V')
3745 unadjust_for_sel();
3746 if (VIsual.lnum != curwin->w_cursor.lnum)
3747 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003748 if (cap != NULL)
3749 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 return FAIL;
3751 }
3752 if (VIsual_mode == 'V')
3753 {
3754 *pp = ml_get_curline();
3755 *lenp = (int)STRLEN(*pp);
3756 }
3757 else
3758 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003759 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 {
3761 *pp = ml_get_pos(&curwin->w_cursor);
3762 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3763 }
3764 else
3765 {
3766 *pp = ml_get_pos(&VIsual);
3767 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003770 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003771 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 reset_VIsual_and_resel();
3774 return OK;
3775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777/*
3778 * CTRL-T: backwards in tag stack
3779 */
3780 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003781nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 if (!checkclearopq(cap->oap))
3784 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3785}
3786
3787/*
3788 * Handle scrolling command 'H', 'L' and 'M'.
3789 */
3790 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003791nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792{
3793 int used = 0;
3794 long n;
3795#ifdef FEAT_FOLDING
3796 linenr_T lnum;
3797#endif
3798 int half;
3799
3800 cap->oap->motion_type = MLINE;
3801 setpcmark();
3802
3803 if (cap->cmdchar == 'L')
3804 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003805 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 curwin->w_cursor.lnum = curwin->w_botline - 1;
3807 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3808 curwin->w_cursor.lnum = 1;
3809 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003810 {
3811#ifdef FEAT_FOLDING
3812 if (hasAnyFolding(curwin))
3813 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003814 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003815 for (n = cap->count1 - 1; n > 0
3816 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3817 {
3818 (void)hasFolding(curwin->w_cursor.lnum,
3819 &curwin->w_cursor.lnum, NULL);
3820 --curwin->w_cursor.lnum;
3821 }
3822 }
3823 else
3824#endif
3825 curwin->w_cursor.lnum -= cap->count1 - 1;
3826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828 else
3829 {
3830 if (cap->cmdchar == 'M')
3831 {
3832#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003833 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 used -= diff_check_fill(curwin, curwin->w_topline)
3835 - curwin->w_topfill;
3836#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003837 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3839 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3840 {
3841#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003842 // Count half he number of filler lines to be "below this
3843 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3845 + n) / 2 >= half)
3846 {
3847 --n;
3848 break;
3849 }
3850#endif
3851 used += plines(curwin->w_topline + n);
3852 if (used >= half)
3853 break;
3854#ifdef FEAT_FOLDING
3855 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3856 n = lnum - curwin->w_topline;
3857#endif
3858 }
3859 if (n > 0 && used > curwin->w_height)
3860 --n;
3861 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003862 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003865#ifdef FEAT_FOLDING
3866 if (hasAnyFolding(curwin))
3867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003868 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003869 lnum = curwin->w_topline;
3870 while (n-- > 0 && lnum < curwin->w_botline - 1)
3871 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003872 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003873 ++lnum;
3874 }
3875 n = lnum - curwin->w_topline;
3876 }
3877#endif
3878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 curwin->w_cursor.lnum = curwin->w_topline + n;
3880 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3881 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3882 }
3883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003884 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003885 if (cap->oap->op_type == OP_NOP)
3886 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 beginline(BL_SOL | BL_FIX);
3888}
3889
3890/*
3891 * Cursor right commands.
3892 */
3893 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003894nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895{
3896 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003897 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003899 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003901 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003902 if (mod_mask & MOD_MASK_CTRL)
3903 cap->arg = TRUE;
3904 nv_wordcmd(cap);
3905 return;
3906 }
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 cap->oap->motion_type = MCHAR;
3909 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003910 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003913 * In virtual edit mode, there's no such thing as "past_line", as lines
3914 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 */
3916 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003917 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918
3919 for (n = cap->count1; n > 0; --n)
3920 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003921 if ((!past_line && oneright() == FAIL)
3922 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003923 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 {
3925 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003926 * <Space> wraps to next line if 'whichwrap' has 's'.
3927 * 'l' wraps to next line if 'whichwrap' has 'l'.
3928 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 */
3930 if ( ((cap->cmdchar == ' '
3931 && vim_strchr(p_ww, 's') != NULL)
3932 || (cap->cmdchar == 'l'
3933 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003934 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 && vim_strchr(p_ww, '>') != NULL))
3936 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003938 // When deleting we also count the NL as a character.
3939 // Set cap->oap->inclusive when last char in the line is
3940 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003941 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003943 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 cap->oap->inclusive = TRUE;
3945 else
3946 {
3947 ++curwin->w_cursor.lnum;
3948 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 curwin->w_set_curswant = TRUE;
3951 cap->oap->inclusive = FALSE;
3952 }
3953 continue;
3954 }
3955 if (cap->oap->op_type == OP_NOP)
3956 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003957 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 if (n == cap->count1)
3959 beep_flush();
3960 }
3961 else
3962 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003963 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 cap->oap->inclusive = TRUE;
3965 }
3966 break;
3967 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003968 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
3970 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 if (virtual_active())
3972 oneright();
3973 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003976 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 ++curwin->w_cursor.col;
3979 }
3980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982#ifdef FEAT_FOLDING
3983 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3984 && cap->oap->op_type == OP_NOP)
3985 foldOpenCursor();
3986#endif
3987}
3988
3989/*
3990 * Cursor left commands.
3991 *
3992 * Returns TRUE when operator end should not be adjusted.
3993 */
3994 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003995nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
3997 long n;
3998
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003999 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4000 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004001 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004002 if (mod_mask & MOD_MASK_CTRL)
4003 cap->arg = 1;
4004 nv_bck_word(cap);
4005 return;
4006 }
4007
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 cap->oap->motion_type = MCHAR;
4009 cap->oap->inclusive = FALSE;
4010 for (n = cap->count1; n > 0; --n)
4011 {
4012 if (oneleft() == FAIL)
4013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004014 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4015 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4016 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 if ( (((cap->cmdchar == K_BS
4018 || cap->cmdchar == Ctrl_H)
4019 && vim_strchr(p_ww, 'b') != NULL)
4020 || (cap->cmdchar == 'h'
4021 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004022 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 && vim_strchr(p_ww, '<') != NULL))
4024 && curwin->w_cursor.lnum > 1)
4025 {
4026 --(curwin->w_cursor.lnum);
4027 coladvance((colnr_T)MAXCOL);
4028 curwin->w_set_curswant = TRUE;
4029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004030 // When the NL before the first char has to be deleted we
4031 // put the cursor on the NUL after the previous line.
4032 // This is a very special case, be careful!
4033 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 if ( (cap->oap->op_type == OP_DELETE
4035 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004036 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004038 char_u *cp = ml_get_cursor();
4039
4040 if (*cp != NUL)
4041 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004042 if (has_mbyte)
4043 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4044 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004045 ++curwin->w_cursor.col;
4046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 cap->retval |= CA_NO_ADJ_OP_END;
4048 }
4049 continue;
4050 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004051 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4053 beep_flush();
4054 break;
4055 }
4056 }
4057#ifdef FEAT_FOLDING
4058 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4059 && cap->oap->op_type == OP_NOP)
4060 foldOpenCursor();
4061#endif
4062}
4063
4064/*
4065 * Cursor up commands.
4066 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4067 */
4068 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004069nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004071 if (mod_mask & MOD_MASK_SHIFT)
4072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004073 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004074 cap->arg = BACKWARD;
4075 nv_page(cap);
4076 }
4077 else
4078 {
4079 cap->oap->motion_type = MLINE;
4080 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4081 clearopbeep(cap->oap);
4082 else if (cap->arg)
4083 beginline(BL_WHITE | BL_FIX);
4084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085}
4086
4087/*
4088 * Cursor down commands.
4089 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4090 */
4091 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004092nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004094 if (mod_mask & MOD_MASK_SHIFT)
4095 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004096 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004097 cap->arg = FORWARD;
4098 nv_page(cap);
4099 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004100#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004101 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004102 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4103 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
4107#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004108 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004109 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 cmdwin_result = CAR;
4111 else
4112#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004113#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004114 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004115 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4116 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4117 {
4118 invoke_prompt_callback();
4119 if (restart_edit == 0)
4120 restart_edit = 'a';
4121 }
4122 else
4123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
4125 cap->oap->motion_type = MLINE;
4126 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4127 clearopbeep(cap->oap);
4128 else if (cap->arg)
4129 beginline(BL_WHITE | BL_FIX);
4130 }
4131 }
4132}
4133
4134#ifdef FEAT_SEARCHPATH
4135/*
4136 * Grab the file name under the cursor and edit it.
4137 */
4138 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004139nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140{
4141 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004142 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004144 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004147 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 return;
4149 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004150 if (curbuf_locked())
4151 {
4152 clearop(cap->oap);
4153 return;
4154 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004155#ifdef FEAT_PROP_POPUP
4156 if (ERROR_IF_TERM_POPUP_WINDOW)
4157 return;
4158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004160 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
4162 if (ptr != NULL)
4163 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004164 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004165 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004166 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004168 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004169 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004170 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004171 {
4172 curwin->w_cursor.lnum = lnum;
4173 check_cursor_lnum();
4174 beginline(BL_SOL | BL_FIX);
4175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 vim_free(ptr);
4177 }
4178 else
4179 clearop(cap->oap);
4180}
4181#endif
4182
4183/*
4184 * <End> command: to end of current line or last line.
4185 */
4186 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004187nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004189 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004191 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004193 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 nv_dollar(cap);
4196}
4197
4198/*
4199 * Handle the "$" command.
4200 */
4201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004202nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
4204 cap->oap->motion_type = MCHAR;
4205 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004206 // In virtual mode when off the edge of a line and an operator
4207 // is pending (whew!) keep the cursor where it is.
4208 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 if (!virtual_active() || gchar_cursor() != NUL
4210 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004211 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 if (cursor_down((long)(cap->count1 - 1),
4213 cap->oap->op_type == OP_NOP) == FAIL)
4214 clearopbeep(cap->oap);
4215#ifdef FEAT_FOLDING
4216 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4217 foldOpenCursor();
4218#endif
4219}
4220
4221/*
4222 * Implementation of '?' and '/' commands.
4223 * If cap->arg is TRUE don't set PC mark.
4224 */
4225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004226nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
4228 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004229 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4232 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004233 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 cap->cmdchar = 'g';
4235 cap->nchar = '?';
4236 nv_operator(cap);
4237 return;
4238 }
4239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004240 // When using 'incsearch' the cursor may be moved to set a different search
4241 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004242 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 if (cap->searchbuf == NULL)
4245 {
4246 clearop(oap);
4247 return;
4248 }
4249
Bram Moolenaar46539112015-02-17 15:43:57 +01004250 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004251 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004252 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253}
4254
4255/*
4256 * Handle "N" and "n" commands.
4257 * cap->arg is SEARCH_REV for "N", 0 for "n".
4258 */
4259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004260nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004262 pos_T old = curwin->w_cursor;
4263 int wrapped = FALSE;
4264 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004265
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004266 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004267 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004268 // Avoid getting stuck on the current cursor position, which can
4269 // happen when an offset is given and the cursor is on the last char
4270 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004271 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004272 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004273 cap->count1 -= 1;
4274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275}
4276
4277/*
4278 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4279 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004280 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004282 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004283normal_search(
4284 cmdarg_T *cap,
4285 int dir,
4286 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004287 int opt, // extra flags for do_search()
4288 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289{
4290 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004291 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
4293 cap->oap->motion_type = MCHAR;
4294 cap->oap->inclusive = FALSE;
4295 cap->oap->use_reg_one = TRUE;
4296 curwin->w_set_curswant = TRUE;
4297
Bram Moolenaara80faa82020-04-12 19:37:17 +02004298 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004299 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004300 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4301 if (wrapped != NULL)
4302 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (i == 0)
4304 clearop(cap->oap);
4305 else
4306 {
4307 if (i == 2)
4308 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310#ifdef FEAT_FOLDING
4311 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4312 foldOpenCursor();
4313#endif
4314 }
4315
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004316 // "/$" will put the cursor after the end of the line, may need to
4317 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004319 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320}
4321
4322/*
4323 * Character search commands.
4324 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4325 * ',' and FALSE for ';'.
4326 * cap->nchar is NUL for ',' and ';' (repeat the search)
4327 */
4328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004329nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330{
4331 int t_cmd;
4332
4333 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4334 t_cmd = TRUE;
4335 else
4336 t_cmd = FALSE;
4337
4338 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4340 clearopbeep(cap->oap);
4341 else
4342 {
4343 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004344 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4346 && (t_cmd || cap->oap->op_type != OP_NOP))
4347 {
4348 colnr_T scol, ecol;
4349
4350 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4351 curwin->w_cursor.coladd = ecol - scol;
4352 }
4353 else
4354 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356#ifdef FEAT_FOLDING
4357 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4358 foldOpenCursor();
4359#endif
4360 }
4361}
4362
4363/*
4364 * "[" and "]" commands.
4365 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4366 */
4367 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004368nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004370 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004372 pos_T *pos = NULL; // init for GCC
4373 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 int flag;
4375 long n;
4376 int findc;
4377 int c;
4378
4379 cap->oap->motion_type = MCHAR;
4380 cap->oap->inclusive = FALSE;
4381 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004382 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
4384#ifdef FEAT_SEARCHPATH
4385 /*
4386 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4387 */
4388 if (cap->nchar == 'f')
4389 nv_gotofile(cap);
4390 else
4391#endif
4392
4393#ifdef FEAT_FIND_ID
4394 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004395 * Find the occurrence(s) of the identifier or define under cursor
4396 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 *
4398 * search list jump
4399 * fwd bwd fwd bwd fwd bwd
4400 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4401 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4402 */
4403 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004404# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004406# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 cap->nchar) != NULL)
4410 {
4411 char_u *ptr;
4412 int len;
4413
4414 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4415 clearop(cap->oap);
4416 else
4417 {
4418 find_pattern_in_path(ptr, 0, len, TRUE,
4419 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4420 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4421 cap->count1,
4422 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4423 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4424 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4425 (linenr_T)MAXLNUM);
4426 curwin->w_set_curswant = TRUE;
4427 }
4428 }
4429 else
4430#endif
4431
4432 /*
4433 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4434 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4435 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4436 * "[m" or "]m" search for prev/next start of (Java) method.
4437 * "[M" or "]M" search for prev/next end of (Java) method.
4438 */
4439 if ( (cap->cmdchar == '['
4440 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4441 || (cap->cmdchar == ']'
4442 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4443 {
4444 if (cap->nchar == '*')
4445 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 prev_pos.lnum = 0;
4447 if (cap->nchar == 'm' || cap->nchar == 'M')
4448 {
4449 if (cap->cmdchar == '[')
4450 findc = '{';
4451 else
4452 findc = '}';
4453 n = 9999;
4454 }
4455 else
4456 {
4457 findc = cap->nchar;
4458 n = cap->count1;
4459 }
4460 for ( ; n > 0; --n)
4461 {
4462 if ((pos = findmatchlimit(cap->oap, findc,
4463 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4464 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004465 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
4467 if (cap->nchar != 'm' && cap->nchar != 'M')
4468 clearopbeep(cap->oap);
4469 }
4470 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004471 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 break;
4473 }
4474 prev_pos = new_pos;
4475 curwin->w_cursor = *pos;
4476 new_pos = *pos;
4477 }
4478 curwin->w_cursor = old_pos;
4479
4480 /*
4481 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4482 * brought us to the match for "[m" and "]M" when inside a method.
4483 * Try finding the '{' or '}' we want to be at.
4484 * Also repeat for the given count.
4485 */
4486 if (cap->nchar == 'm' || cap->nchar == 'M')
4487 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004488 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 int norm = ((findc == '{') == (cap->nchar == 'm'));
4490
4491 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004492 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (prev_pos.lnum != 0)
4494 {
4495 pos = &prev_pos;
4496 curwin->w_cursor = prev_pos;
4497 if (norm)
4498 --n;
4499 }
4500 else
4501 pos = NULL;
4502 while (n > 0)
4503 {
4504 for (;;)
4505 {
4506 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4507 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004508 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 if (pos == NULL)
4510 clearopbeep(cap->oap);
4511 n = 0;
4512 break;
4513 }
4514 c = gchar_cursor();
4515 if (c == '{' || c == '}')
4516 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004517 // Must have found end/start of class: use it.
4518 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if ((c == findc && norm) || (n == 1 && !norm))
4520 {
4521 new_pos = curwin->w_cursor;
4522 pos = &new_pos;
4523 n = 0;
4524 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004525 // if no match found at all, we started outside of the
4526 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 else if (new_pos.lnum == 0)
4528 {
4529 new_pos = curwin->w_cursor;
4530 pos = &new_pos;
4531 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004532 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 else if ((pos = findmatchlimit(cap->oap, findc,
4534 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4535 0)) == NULL)
4536 n = 0;
4537 else
4538 curwin->w_cursor = *pos;
4539 break;
4540 }
4541 }
4542 --n;
4543 }
4544 curwin->w_cursor = old_pos;
4545 if (pos == NULL && new_pos.lnum != 0)
4546 clearopbeep(cap->oap);
4547 }
4548 if (pos != NULL)
4549 {
4550 setpcmark();
4551 curwin->w_cursor = *pos;
4552 curwin->w_set_curswant = TRUE;
4553#ifdef FEAT_FOLDING
4554 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4555 && cap->oap->op_type == OP_NOP)
4556 foldOpenCursor();
4557#endif
4558 }
4559 }
4560
4561 /*
4562 * "[[", "[]", "]]" and "][": move to start or end of function
4563 */
4564 else if (cap->nchar == '[' || cap->nchar == ']')
4565 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004566 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 flag = '{';
4568 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004569 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
4571 curwin->w_set_curswant = TRUE;
4572 /*
4573 * Imitate strange Vi behaviour: When using "]]" with an operator
4574 * we also stop at '}'.
4575 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004576 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 (cap->oap->op_type != OP_NOP
4578 && cap->arg == FORWARD && flag == '{')))
4579 clearopbeep(cap->oap);
4580 else
4581 {
4582 if (cap->oap->op_type == OP_NOP)
4583 beginline(BL_WHITE | BL_FIX);
4584#ifdef FEAT_FOLDING
4585 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4586 foldOpenCursor();
4587#endif
4588 }
4589 }
4590
4591 /*
4592 * "[p", "[P", "]P" and "]p": put with indent adjustment
4593 */
4594 else if (cap->nchar == 'p' || cap->nchar == 'P')
4595 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004596 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 }
4598
4599 /*
4600 * "['", "[`", "]'" and "]`": jump to next mark
4601 */
4602 else if (cap->nchar == '\'' || cap->nchar == '`')
4603 {
4604 pos = &curwin->w_cursor;
4605 for (n = cap->count1; n > 0; --n)
4606 {
4607 prev_pos = *pos;
4608 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4609 cap->nchar == '\'');
4610 if (pos == NULL)
4611 break;
4612 }
4613 if (pos == NULL)
4614 pos = &prev_pos;
4615 nv_cursormark(cap, cap->nchar == '\'', pos);
4616 }
4617
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 /*
4619 * [ or ] followed by a middle mouse click: put selected text with
4620 * indent adjustment. Any other button just does as usual.
4621 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004622 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
4624 (void)do_mouse(cap->oap, cap->nchar,
4625 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4626 cap->count1, PUT_FIXINDENT);
4627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
4629#ifdef FEAT_FOLDING
4630 /*
4631 * "[z" and "]z": move to start or end of open fold.
4632 */
4633 else if (cap->nchar == 'z')
4634 {
4635 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4636 cap->count1) == FAIL)
4637 clearopbeep(cap->oap);
4638 }
4639#endif
4640
4641#ifdef FEAT_DIFF
4642 /*
4643 * "[c" and "]c": move to next or previous diff-change.
4644 */
4645 else if (cap->nchar == 'c')
4646 {
4647 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4648 cap->count1) == FAIL)
4649 clearopbeep(cap->oap);
4650 }
4651#endif
4652
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004653#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004654 /*
4655 * "[s", "[S", "]s" and "]S": move to next spell error.
4656 */
4657 else if (cap->nchar == 's' || cap->nchar == 'S')
4658 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004659 setpcmark();
4660 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004661 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4662 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004663 {
4664 clearopbeep(cap->oap);
4665 break;
4666 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004667 else
4668 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004669# ifdef FEAT_FOLDING
4670 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4671 foldOpenCursor();
4672# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004673 }
4674#endif
4675
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004676 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 else
4678 clearopbeep(cap->oap);
4679}
4680
4681/*
4682 * Handle Normal mode "%" command.
4683 */
4684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004685nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004688#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 linenr_T lnum = curwin->w_cursor.lnum;
4690#endif
4691
4692 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004693 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 if (cap->count0 > 100)
4696 clearopbeep(cap->oap);
4697 else
4698 {
4699 cap->oap->motion_type = MLINE;
4700 setpcmark();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004701 // Round up, so CTRL-G will give same value. Watch out for a
4702 // large line count, the line number must not go negative!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (curbuf->b_ml.ml_line_count > 1000000)
4704 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4705 / 100L * cap->count0;
4706 else
4707 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4708 cap->count0 + 99L) / 100L;
4709 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4710 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4711 beginline(BL_SOL | BL_FIX);
4712 }
4713 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004714 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
4716 cap->oap->motion_type = MCHAR;
4717 cap->oap->use_reg_one = TRUE;
4718 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4719 clearopbeep(cap->oap);
4720 else
4721 {
4722 setpcmark();
4723 curwin->w_cursor = *pos;
4724 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
4728 }
4729#ifdef FEAT_FOLDING
4730 if (cap->oap->op_type == OP_NOP
4731 && lnum != curwin->w_cursor.lnum
4732 && (fdo_flags & FDO_PERCENT)
4733 && KeyTyped)
4734 foldOpenCursor();
4735#endif
4736}
4737
4738/*
4739 * Handle "(" and ")" commands.
4740 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4741 */
4742 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004743nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744{
4745 cap->oap->motion_type = MCHAR;
4746 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004747 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004748 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 curwin->w_set_curswant = TRUE;
4750
4751 if (findsent(cap->arg, cap->count1) == FAIL)
4752 clearopbeep(cap->oap);
4753 else
4754 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004755 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004756 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#ifdef FEAT_FOLDING
4759 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4760 foldOpenCursor();
4761#endif
4762 }
4763}
4764
4765/*
4766 * "m" command: Mark a position.
4767 */
4768 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004769nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 if (!checkclearop(cap->oap))
4772 {
4773 if (setmark(cap->nchar) == FAIL)
4774 clearopbeep(cap->oap);
4775 }
4776}
4777
4778/*
4779 * "{" and "}" commands.
4780 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4781 */
4782 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004783nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
4785 cap->oap->motion_type = MCHAR;
4786 cap->oap->inclusive = FALSE;
4787 cap->oap->use_reg_one = TRUE;
4788 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004789 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 clearopbeep(cap->oap);
4791 else
4792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794#ifdef FEAT_FOLDING
4795 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4796 foldOpenCursor();
4797#endif
4798 }
4799}
4800
4801/*
4802 * "u" command: Undo or make lower case.
4803 */
4804 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004805nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004807 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004809 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 cap->cmdchar = 'g';
4811 cap->nchar = 'u';
4812 nv_operator(cap);
4813 }
4814 else
4815 nv_kundo(cap);
4816}
4817
4818/*
4819 * <Undo> command.
4820 */
4821 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004822nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823{
4824 if (!checkclearopq(cap->oap))
4825 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004826#ifdef FEAT_JOB_CHANNEL
4827 if (bt_prompt(curbuf))
4828 {
4829 clearopbeep(cap->oap);
4830 return;
4831 }
4832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 u_undo((int)cap->count1);
4834 curwin->w_set_curswant = TRUE;
4835 }
4836}
4837
4838/*
4839 * Handle the "r" command.
4840 */
4841 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004842nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843{
4844 char_u *ptr;
4845 int had_ctrl_v;
4846 long n;
4847
4848 if (checkclearop(cap->oap))
4849 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004850#ifdef FEAT_JOB_CHANNEL
4851 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4852 {
4853 clearopbeep(cap->oap);
4854 return;
4855 }
4856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004858 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (cap->nchar == Ctrl_V)
4860 {
4861 had_ctrl_v = Ctrl_V;
4862 cap->nchar = get_literal();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004863 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 if (cap->nchar > DEL)
4865 had_ctrl_v = NUL;
4866 }
4867 else
4868 had_ctrl_v = NUL;
4869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004870 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004871 if (IS_SPECIAL(cap->nchar))
4872 {
4873 clearopbeep(cap->oap);
4874 return;
4875 }
4876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004877 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if (VIsual_active)
4879 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004880 if (got_int)
4881 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004882 if (had_ctrl_v)
4883 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004884 // Use a special (negative) number to make a difference between a
4885 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004886 if (cap->nchar == CAR)
4887 cap->nchar = REPLACE_CR_NCHAR;
4888 else if (cap->nchar == NL)
4889 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 nv_operator(cap);
4892 return;
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004895 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 if (virtual_active())
4897 {
4898 if (u_save_cursor() == FAIL)
4899 return;
4900 if (gchar_cursor() == NUL)
4901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004902 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 coladvance_force((colnr_T)(getviscol() + cap->count1));
4904 curwin->w_cursor.col -= cap->count1;
4905 }
4906 else if (gchar_cursor() == TAB)
4907 coladvance_force(getviscol());
4908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004910 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004912 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004913 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
4915 clearopbeep(cap->oap);
4916 return;
4917 }
4918
4919 /*
4920 * Replacing with a TAB is done by edit() when it is complicated because
4921 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4922 * Other characters are done below to avoid problems with things like
4923 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
4924 */
4925 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4926 {
4927 stuffnumReadbuff(cap->count1);
4928 stuffcharReadbuff('R');
4929 stuffcharReadbuff('\t');
4930 stuffcharReadbuff(ESC);
4931 return;
4932 }
4933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004934 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (u_save_cursor() == FAIL)
4936 return;
4937
4938 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4939 {
4940 /*
4941 * Replace character(s) by a single newline.
4942 * Strange vi behaviour: Only one newline is inserted.
4943 * Delete the characters here.
4944 * Insert the newline with an insert command, takes care of
4945 * autoindent. The insert command depends on being on the last
4946 * character of a line or not.
4947 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004948 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 stuffcharReadbuff('\r');
4950 stuffcharReadbuff(ESC);
4951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004952 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 invoke_edit(cap, TRUE, 'r', FALSE);
4954 }
4955 else
4956 {
4957 prep_redo(cap->oap->regname, cap->count1,
4958 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4959
4960 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (has_mbyte)
4962 {
4963 int old_State = State;
4964
4965 if (cap->ncharC1 != 0)
4966 AppendCharToRedobuff(cap->ncharC1);
4967 if (cap->ncharC2 != 0)
4968 AppendCharToRedobuff(cap->ncharC2);
4969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004970 // This is slow, but it handles replacing a single-byte with a
4971 // multi-byte and the other way around. Also handles adding
4972 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 for (n = cap->count1; n > 0; --n)
4974 {
4975 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004976 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4977 {
4978 int c = ins_copychar(curwin->w_cursor.lnum
4979 + (cap->nchar == Ctrl_Y ? -1 : 1));
4980 if (c != NUL)
4981 ins_char(c);
4982 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004983 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02004984 ++curwin->w_cursor.col;
4985 }
4986 else
4987 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 State = old_State;
4989 if (cap->ncharC1 != 0)
4990 ins_char(cap->ncharC1);
4991 if (cap->ncharC2 != 0)
4992 ins_char(cap->ncharC2);
4993 }
4994 }
4995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 /*
4998 * Replace the characters within one line.
4999 */
5000 for (n = cap->count1; n > 0; --n)
5001 {
5002 /*
5003 * Get ptr again, because u_save and/or showmatch() will have
5004 * released the line. At the same time we let know that the
5005 * line will be changed.
5006 */
5007 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005008 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5009 {
5010 int c = ins_copychar(curwin->w_cursor.lnum
5011 + (cap->nchar == Ctrl_Y ? -1 : 1));
5012 if (c != NUL)
5013 ptr[curwin->w_cursor.col] = c;
5014 }
5015 else
5016 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (p_sm && msg_silent == 0)
5018 showmatch(cap->nchar);
5019 ++curwin->w_cursor.col;
5020 }
5021#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005022 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005024 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
Bram Moolenaar009b2592004-10-24 19:18:58 +00005026 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005027 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005029 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031#endif
5032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005033 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 changed_bytes(curwin->w_cursor.lnum,
5035 (colnr_T)(curwin->w_cursor.col - cap->count1));
5036 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005037 --curwin->w_cursor.col; // cursor on the last replaced char
5038 // if the character on the left of the current cursor is a multi-byte
5039 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (has_mbyte)
5041 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 curbuf->b_op_end = curwin->w_cursor;
5043 curwin->w_set_curswant = TRUE;
5044 set_last_insert(cap->nchar);
5045 }
5046}
5047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048/*
5049 * 'o': Exchange start and end of Visual area.
5050 * 'O': same, but in block mode exchange left and right corners.
5051 */
5052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005053v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
5055 pos_T old_cursor;
5056 colnr_T left, right;
5057
5058 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5059 {
5060 old_cursor = curwin->w_cursor;
5061 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5062 curwin->w_cursor.lnum = VIsual.lnum;
5063 coladvance(left);
5064 VIsual = curwin->w_cursor;
5065
5066 curwin->w_cursor.lnum = old_cursor.lnum;
5067 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005068 // 'selection "exclusive" and cursor at right-bottom corner: move it
5069 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5071 ++curwin->w_curswant;
5072 coladvance(curwin->w_curswant);
5073 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005075 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
5077 curwin->w_cursor.lnum = VIsual.lnum;
5078 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5079 ++right;
5080 coladvance(right);
5081 VIsual = curwin->w_cursor;
5082
5083 curwin->w_cursor.lnum = old_cursor.lnum;
5084 coladvance(left);
5085 curwin->w_curswant = left;
5086 }
5087 }
5088 else
5089 {
5090 old_cursor = curwin->w_cursor;
5091 curwin->w_cursor = VIsual;
5092 VIsual = old_cursor;
5093 curwin->w_set_curswant = TRUE;
5094 }
5095}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096
5097/*
5098 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5099 */
5100 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005101nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005103 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 cap->cmdchar = 'c';
5106 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005107 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 VIsual_mode = 'V';
5109 nv_operator(cap);
5110 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005111 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005114 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 else
5116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (virtual_active())
5118 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5120 }
5121 }
5122}
5123
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124/*
5125 * "gr".
5126 */
5127 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005128nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (VIsual_active)
5131 {
5132 cap->cmdchar = 'r';
5133 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005134 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005136 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005139 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 else
5141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005142 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 cap->extra_char = get_literal();
5144 stuffcharReadbuff(cap->extra_char);
5145 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (virtual_active())
5147 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 invoke_edit(cap, TRUE, 'v', FALSE);
5149 }
5150 }
5151}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152
5153/*
5154 * Swap case for "~" command, when it does not work like an operator.
5155 */
5156 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005157n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158{
5159 long n;
5160 pos_T startpos;
5161 int did_change = 0;
5162#ifdef FEAT_NETBEANS_INTG
5163 pos_T pos;
5164 char_u *ptr;
5165 int count;
5166#endif
5167
5168 if (checkclearopq(cap->oap))
5169 return;
5170
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005171 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
5173 clearopbeep(cap->oap);
5174 return;
5175 }
5176
5177 prep_redo_cmd(cap);
5178
5179 if (u_save_cursor() == FAIL)
5180 return;
5181
5182 startpos = curwin->w_cursor;
5183#ifdef FEAT_NETBEANS_INTG
5184 pos = startpos;
5185#endif
5186 for (n = cap->count1; n > 0; --n)
5187 {
5188 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5189 inc_cursor();
5190 if (gchar_cursor() == NUL)
5191 {
5192 if (vim_strchr(p_ww, '~') != NULL
5193 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5194 {
5195#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005196 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
5198 if (did_change)
5199 {
5200 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005201 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005202 netbeans_removed(curbuf, pos.lnum, pos.col,
5203 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005205 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
5207 pos.col = 0;
5208 pos.lnum++;
5209 }
5210#endif
5211 ++curwin->w_cursor.lnum;
5212 curwin->w_cursor.col = 0;
5213 if (n > 1)
5214 {
5215 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5216 break;
5217 u_clearline();
5218 }
5219 }
5220 else
5221 break;
5222 }
5223 }
5224#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005225 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 ptr = ml_get(pos.lnum);
5228 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005229 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5230 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
5232#endif
5233
5234
5235 check_cursor();
5236 curwin->w_set_curswant = TRUE;
5237 if (did_change)
5238 {
5239 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5240 0L);
5241 curbuf->b_op_start = startpos;
5242 curbuf->b_op_end = curwin->w_cursor;
5243 if (curbuf->b_op_end.col > 0)
5244 --curbuf->b_op_end.col;
5245 }
5246}
5247
5248/*
5249 * Move cursor to mark.
5250 */
5251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005252nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 if (check_mark(pos) == FAIL)
5255 clearop(cap->oap);
5256 else
5257 {
5258 if (cap->cmdchar == '\''
5259 || cap->cmdchar == '`'
5260 || cap->cmdchar == '['
5261 || cap->cmdchar == ']')
5262 setpcmark();
5263 curwin->w_cursor = *pos;
5264 if (flag)
5265 beginline(BL_WHITE | BL_FIX);
5266 else
5267 check_cursor();
5268 }
5269 cap->oap->motion_type = flag ? MLINE : MCHAR;
5270 if (cap->cmdchar == '`')
5271 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005272 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 curwin->w_set_curswant = TRUE;
5274}
5275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276/*
5277 * Handle commands that are operators in Visual mode.
5278 */
5279 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005280v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
5282 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5283
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005284 // Uppercase means linewise, except in block mode, then "D" deletes till
5285 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (isupper(cap->cmdchar))
5287 {
5288 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005289 {
5290 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5294 curwin->w_curswant = MAXCOL;
5295 }
5296 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5297 nv_operator(cap);
5298}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
5300/*
5301 * "s" and "S" commands.
5302 */
5303 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005304nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005306#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005307 // When showing output of term_dumpdiff() swap the top and botom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005308 if (term_swap_diff() == OK)
5309 return;
5310#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005311#ifdef FEAT_JOB_CHANNEL
5312 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5313 {
5314 clearopbeep(cap->oap);
5315 return;
5316 }
5317#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005318 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
5320 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005321 {
5322 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 cap->cmdchar = 'c';
5326 nv_operator(cap);
5327 }
5328 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 nv_optrans(cap);
5330}
5331
5332/*
5333 * Abbreviated commands.
5334 */
5335 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005336nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
5338 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005339 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005341 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 if (VIsual_active)
5343 v_visop(cap);
5344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 nv_optrans(cap);
5346}
5347
5348/*
5349 * Translate a command into another command.
5350 */
5351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005352nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5355 (char_u *)"d$", (char_u *)"c$",
5356 (char_u *)"cl", (char_u *)"cc",
5357 (char_u *)"yy", (char_u *)":s\r"};
5358 static char_u *str = (char_u *)"xXDCsSY&";
5359
5360 if (!checkclearopq(cap->oap))
5361 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005362 // In Vi "2D" doesn't delete the next line. Can't translate it
5363 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005364 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5365 {
5366 cap->oap->start = curwin->w_cursor;
5367 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005368#ifdef FEAT_EVAL
5369 set_op_var(OP_DELETE);
5370#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005371 cap->count1 = 1;
5372 nv_dollar(cap);
5373 finish_op = TRUE;
5374 ResetRedobuff();
5375 AppendCharToRedobuff('D');
5376 }
5377 else
5378 {
5379 if (cap->count0)
5380 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005381 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384 cap->opcount = 0;
5385}
5386
5387/*
5388 * "'" and "`" commands. Also for "g'" and "g`".
5389 * cap->arg is TRUE for "'" and "g'".
5390 */
5391 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005392nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394 pos_T *pos;
5395 int c;
5396#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005397 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005398 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#endif
5400
5401 if (cap->cmdchar == 'g')
5402 c = cap->extra_char;
5403 else
5404 c = cap->nchar;
5405 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005406 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
5408 if (cap->arg)
5409 {
5410 check_cursor_lnum();
5411 beginline(BL_WHITE | BL_FIX);
5412 }
5413 else
5414 check_cursor();
5415 }
5416 else
5417 nv_cursormark(cap, cap->arg, pos);
5418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005419 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 if (!virtual_active())
5421 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005422 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423#ifdef FEAT_FOLDING
5424 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005425 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005426 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 && (fdo_flags & FDO_MARK)
5428 && old_KeyTyped)
5429 foldOpenCursor();
5430#endif
5431}
5432
5433/*
5434 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
5435 */
5436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005437nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
5439#ifdef FEAT_JUMPLIST
5440 pos_T *pos;
5441# ifdef FEAT_FOLDING
5442 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005443 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444# endif
5445
5446 if (!checkclearopq(cap->oap))
5447 {
5448 if (cap->cmdchar == 'g')
5449 pos = movechangelist((int)cap->count1);
5450 else
5451 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005452 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
5454 curwin->w_set_curswant = TRUE;
5455 check_cursor();
5456 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005457 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 nv_cursormark(cap, FALSE, pos);
5459 else if (cap->cmdchar == 'g')
5460 {
5461 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005462 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005464 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005466 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 else
5469 clearopbeep(cap->oap);
5470# ifdef FEAT_FOLDING
5471 if (cap->oap->op_type == OP_NOP
5472 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5473 && (fdo_flags & FDO_MARK)
5474 && old_KeyTyped)
5475 foldOpenCursor();
5476# endif
5477 }
5478#else
5479 clearopbeep(cap->oap);
5480#endif
5481}
5482
5483/*
5484 * Handle '"' command.
5485 */
5486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005487nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 if (checkclearop(cap->oap))
5490 return;
5491#ifdef FEAT_EVAL
5492 if (cap->nchar == '=')
5493 cap->nchar = get_expr_register();
5494#endif
5495 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5496 {
5497 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005498 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499#ifdef FEAT_EVAL
5500 set_reg_var(cap->oap->regname);
5501#endif
5502 }
5503 else
5504 clearopbeep(cap->oap);
5505}
5506
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507/*
5508 * Handle "v", "V" and "CTRL-V" commands.
5509 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5510 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005511 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 */
5513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005514nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005516 if (cap->cmdchar == Ctrl_Q)
5517 cap->cmdchar = Ctrl_V;
5518
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005519 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5520 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 if (cap->oap->op_type != OP_NOP)
5522 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005523 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005524 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return;
5526 }
5527
5528 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005529 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005531 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005533 else // toggle char/block mode
5534 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 VIsual_mode = cap->cmdchar;
5536 showmode();
5537 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005538 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005540 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005543 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005545 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 VIsual = curwin->w_cursor;
5547
5548 VIsual_active = TRUE;
5549 VIsual_reselect = TRUE;
5550 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005551 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005554 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005555 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 /*
5557 * For V and ^V, we multiply the number of lines even if there
5558 * was only one -- webb
5559 */
5560 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5561 {
5562 curwin->w_cursor.lnum +=
5563 resel_VIsual_line_count * cap->count0 - 1;
5564 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5565 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5566 }
5567 VIsual_mode = resel_VIsual_mode;
5568 if (VIsual_mode == 'v')
5569 {
5570 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005571 {
5572 validate_virtcol();
5573 curwin->w_curswant = curwin->w_virtcol
5574 + resel_VIsual_vcol * cap->count0 - 1;
5575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005577 curwin->w_curswant = resel_VIsual_vcol;
5578 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005580 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
5582 curwin->w_curswant = MAXCOL;
5583 coladvance((colnr_T)MAXCOL);
5584 }
5585 else if (VIsual_mode == Ctrl_V)
5586 {
5587 validate_virtcol();
5588 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005589 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 coladvance(curwin->w_curswant);
5591 }
5592 else
5593 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005594 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
5596 else
5597 {
5598 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005599 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 may_start_select('c');
5601 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005602 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005604 if (cap->count0 > 0 && --cap->count1 > 0)
5605 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005606 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005607 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5608 nv_right(cap);
5609 else if (VIsual_mode == 'V')
5610 nv_down(cap);
5611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613 }
5614}
5615
5616/*
5617 * Start selection for Shift-movement keys.
5618 */
5619 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005620start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005622 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 may_start_select('k');
5624 n_start_visual_mode('v');
5625}
5626
5627/*
5628 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5629 */
5630 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005631may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 VIsual_select = (stuff_empty() && typebuf_typed()
5634 && (vim_strchr(p_slm, c) != NULL));
5635}
5636
5637/*
5638 * Start Visual mode "c".
5639 * Should set VIsual_select before calling this.
5640 */
5641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005642n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005644#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005645 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005646 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005647#endif
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 VIsual_mode = c;
5650 VIsual_active = TRUE;
5651 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005652
5653 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005654 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005656 {
5657 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 VIsual = curwin->w_cursor;
5661
5662#ifdef FEAT_FOLDING
5663 foldAdjustVisual();
5664#endif
5665
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005667#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005668 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005669 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005670#endif
5671
Bram Moolenaar09df3122006-01-23 22:23:09 +00005672 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005673 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005675 // Make sure the clipboard gets updated. Needed because start and
5676 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 clip_star.vmode = NUL;
5678#endif
5679
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005680 // Only need to redraw this line, unless still need to redraw an old
5681 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 if (curwin->w_redr_type < INVERTED)
5683 {
5684 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5685 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5686 }
5687}
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690/*
5691 * CTRL-W: Window commands
5692 */
5693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005694nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005696 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005697 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005698 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005699 cap->cmdchar = ':';
5700 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005701 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005702 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005703 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005704 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705}
5706
5707/*
5708 * CTRL-Z: Suspend
5709 */
5710 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005711nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005715 end_visual_mode(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 do_cmdline_cmd((char_u *)"st");
5717}
5718
5719/*
5720 * Commands starting with "g".
5721 */
5722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005723nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 int i;
5728 int flag = FALSE;
5729
5730 switch (cap->nchar)
5731 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005732 case Ctrl_A:
5733 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734#ifdef MEM_PROFILE
5735 /*
5736 * "g^A": dump log of used memory.
5737 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005738 if (!VIsual_active && cap->nchar == Ctrl_A)
5739 vim_mem_profile_dump();
5740 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005742 /*
5743 * "g^A/g^X": sequentially increment visually selected region
5744 */
5745 if (VIsual_active)
5746 {
5747 cap->arg = TRUE;
5748 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005749 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005750 nv_addsub(cap);
5751 }
5752 else
5753 clearopbeep(oap);
5754 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 /*
5757 * "gR": Enter virtual replace mode.
5758 */
5759 case 'R':
5760 cap->arg = TRUE;
5761 nv_Replace(cap);
5762 break;
5763
5764 case 'r':
5765 nv_vreplace(cap);
5766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 case '&':
5769 do_cmdline_cmd((char_u *)"%s//~/&");
5770 break;
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 /*
5773 * "gv": Reselect the previous Visual area. If Visual already active,
5774 * exchange previous and current Visual area.
5775 */
5776 case 'v':
5777 if (checkclearop(oap))
5778 break;
5779
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005780 if ( curbuf->b_visual.vi_start.lnum == 0
5781 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5782 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 beep_flush();
5784 else
5785 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005786 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 if (VIsual_active)
5788 {
5789 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005790 VIsual_mode = curbuf->b_visual.vi_mode;
5791 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792# ifdef FEAT_EVAL
5793 curbuf->b_visual_mode_eval = i;
5794# endif
5795 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005796 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5797 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005799 tpos = curbuf->b_visual.vi_end;
5800 curbuf->b_visual.vi_end = curwin->w_cursor;
5801 curwin->w_cursor = curbuf->b_visual.vi_start;
5802 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 }
5804 else
5805 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005806 VIsual_mode = curbuf->b_visual.vi_mode;
5807 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5808 tpos = curbuf->b_visual.vi_end;
5809 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
5811
5812 VIsual_active = TRUE;
5813 VIsual_reselect = TRUE;
5814
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005815 // Set Visual to the start and w_cursor to the end of the Visual
5816 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 check_cursor();
5818 VIsual = curwin->w_cursor;
5819 curwin->w_cursor = tpos;
5820 check_cursor();
5821 update_topline();
5822 /*
5823 * When called from normal "g" command: start Select mode when
5824 * 'selectmode' contains "cmd". When called for K_SELECT, always
5825 * start Select mode.
5826 */
5827 if (cap->arg)
5828 VIsual_select = TRUE;
5829 else
5830 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005833 // Make sure the clipboard gets updated. Needed because start and
5834 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 clip_star.vmode = NUL;
5836#endif
5837 redraw_curbuf_later(INVERTED);
5838 showmode();
5839 }
5840 break;
5841 /*
5842 * "gV": Don't reselect the previous Visual area after a Select mode
5843 * mapping of menu.
5844 */
5845 case 'V':
5846 VIsual_reselect = FALSE;
5847 break;
5848
5849 /*
5850 * "gh": start Select mode.
5851 * "gH": start Select line mode.
5852 * "g^H": start Select block mode.
5853 */
5854 case K_BS:
5855 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005856 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 case 'h':
5858 case 'H':
5859 case Ctrl_H:
5860# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005861 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (cap->nchar == Ctrl_H)
5863 cap->cmdchar = Ctrl_V;
5864 else
5865# endif
5866 cap->cmdchar = cap->nchar + ('v' - 'h');
5867 cap->arg = TRUE;
5868 nv_visual(cap);
5869 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005870
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005871 // "gn", "gN" visually select next/previous search match
5872 // "gn" selects next match
5873 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005874 case 'N':
5875 case 'n':
5876 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005877 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879
5880 /*
5881 * "gj" and "gk" two new funny movement keys -- up and down
5882 * movement based on *screen* line rather than *file* line.
5883 */
5884 case 'j':
5885 case K_DOWN:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005886 // with 'nowrap' it works just like the normal "j" command; also when
5887 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 if (!curwin->w_p_wrap
5889#ifdef FEAT_FOLDING
5890 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5891#endif
5892 )
5893 {
5894 oap->motion_type = MLINE;
5895 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5896 }
5897 else
5898 i = nv_screengo(oap, FORWARD, cap->count1);
5899 if (i == FAIL)
5900 clearopbeep(oap);
5901 break;
5902
5903 case 'k':
5904 case K_UP:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005905 // with 'nowrap' it works just like the normal "k" command; also when
5906 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 if (!curwin->w_p_wrap
5908#ifdef FEAT_FOLDING
5909 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5910#endif
5911 )
5912 {
5913 oap->motion_type = MLINE;
5914 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5915 }
5916 else
5917 i = nv_screengo(oap, BACKWARD, cap->count1);
5918 if (i == FAIL)
5919 clearopbeep(oap);
5920 break;
5921
5922 /*
5923 * "gJ": join two lines without inserting a space.
5924 */
5925 case 'J':
5926 nv_join(cap);
5927 break;
5928
5929 /*
5930 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
5931 * "gm": middle of "g0" and "g$".
5932 */
5933 case '^':
5934 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005935 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936
5937 case '0':
5938 case 'm':
5939 case K_HOME:
5940 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 oap->motion_type = MCHAR;
5942 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005943 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005945 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 int width2 = width1 + curwin_col_off2();
5947
5948 validate_virtcol();
5949 i = 0;
5950 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5951 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5952 }
5953 else
5954 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005955 // Go to the middle of the screen line. When 'number' or
5956 // 'relativenumber' is on and lines are wrapping the middle can be more
5957 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02005959 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 + ((curwin->w_p_wrap && i > 0)
5961 ? curwin_col_off2() : 0)) / 2;
5962 coladvance((colnr_T)i);
5963 if (flag)
5964 {
5965 do
5966 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01005967 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01005968 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 }
5970 curwin->w_set_curswant = TRUE;
5971 break;
5972
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005973 case 'M':
5974 {
5975 char_u *ptr = ml_get_curline();
5976
5977 oap->motion_type = MCHAR;
5978 oap->inclusive = FALSE;
5979 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01005980 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01005981 else
5982 i = (int)STRLEN(ptr);
5983 if (cap->count0 > 0 && cap->count0 <= 100)
5984 coladvance((colnr_T)(i * cap->count0 / 100));
5985 else
5986 coladvance((colnr_T)(i / 2));
5987 curwin->w_set_curswant = TRUE;
5988 }
5989 break;
5990
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005992 // "g_": to the last non-blank character in the line or <count> lines
5993 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 cap->oap->motion_type = MCHAR;
5995 cap->oap->inclusive = TRUE;
5996 curwin->w_curswant = MAXCOL;
5997 if (cursor_down((long)(cap->count1 - 1),
5998 cap->oap->op_type == OP_NOP) == FAIL)
5999 clearopbeep(cap->oap);
6000 else
6001 {
6002 char_u *ptr = ml_get_curline();
6003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006004 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6006 --curwin->w_cursor.col;
6007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006008 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006010 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 --curwin->w_cursor.col;
6012 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006013 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 }
6015 break;
6016
6017 case '$':
6018 case K_END:
6019 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 {
6021 int col_off = curwin_col_off();
6022
6023 oap->motion_type = MCHAR;
6024 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006025 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006027 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 if (cap->count1 == 1)
6029 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006030 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 int width2 = width1 + curwin_col_off2();
6032
6033 validate_virtcol();
6034 i = width1 - 1;
6035 if (curwin->w_virtcol >= (colnr_T)width1)
6036 i += ((curwin->w_virtcol - width1) / width2 + 1)
6037 * width2;
6038 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006040 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006041 validate_virtcol();
6042 curwin->w_curswant = curwin->w_virtcol;
6043 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6045 {
6046 /*
6047 * Check for landing on a character that got split at
6048 * the end of the line. We do not want to advance to
6049 * the next screen line.
6050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (curwin->w_virtcol > (colnr_T)i)
6052 --curwin->w_cursor.col;
6053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 }
6055 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6056 clearopbeep(oap);
6057 }
6058 else
6059 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006060 if (cap->count1 > 1)
6061 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006062 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006063
Bram Moolenaar02631462017-09-22 15:20:32 +02006064 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006066
Bram Moolenaard5c82342019-07-27 18:44:57 +02006067 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006068 validate_virtcol();
6069 curwin->w_curswant = curwin->w_virtcol;
6070 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 }
6072 }
6073 break;
6074
6075 /*
6076 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6077 */
6078 case '*':
6079 case '#':
6080#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006081 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006083 case Ctrl_RSB: // :tag or :tselect for current identifier
6084 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 nv_ident(cap);
6086 break;
6087
6088 /*
6089 * ge and gE: go back to end of word
6090 */
6091 case 'e':
6092 case 'E':
6093 oap->motion_type = MCHAR;
6094 curwin->w_set_curswant = TRUE;
6095 oap->inclusive = TRUE;
6096 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6097 clearopbeep(oap);
6098 break;
6099
6100 /*
6101 * "g CTRL-G": display info about cursor position
6102 */
6103 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006104 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 break;
6106
6107 /*
6108 * "gi": start Insert at the last position.
6109 */
6110 case 'i':
6111 if (curbuf->b_last_insert.lnum != 0)
6112 {
6113 curwin->w_cursor = curbuf->b_last_insert;
6114 check_cursor_lnum();
6115 i = (int)STRLEN(ml_get_curline());
6116 if (curwin->w_cursor.col > (colnr_T)i)
6117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 if (virtual_active())
6119 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 curwin->w_cursor.col = i;
6121 }
6122 }
6123 cap->cmdchar = 'i';
6124 nv_edit(cap);
6125 break;
6126
6127 /*
6128 * "gI": Start insert in column 1.
6129 */
6130 case 'I':
6131 beginline(0);
6132 if (!checkclearopq(oap))
6133 invoke_edit(cap, FALSE, 'g', FALSE);
6134 break;
6135
6136#ifdef FEAT_SEARCHPATH
6137 /*
6138 * "gf": goto file, edit file under cursor
6139 * "]f" and "[f": can also be used.
6140 */
6141 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006142 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 nv_gotofile(cap);
6144 break;
6145#endif
6146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006147 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 case '\'':
6149 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006150 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 case '`':
6152 nv_gomark(cap);
6153 break;
6154
6155 /*
6156 * "gs": Goto sleep.
6157 */
6158 case 's':
6159 do_sleep(cap->count1 * 1000L);
6160 break;
6161
6162 /*
6163 * "ga": Display the ascii value of the character under the
6164 * cursor. It is displayed in decimal, hex, and octal. -- webb
6165 */
6166 case 'a':
6167 do_ascii(NULL);
6168 break;
6169
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 /*
6171 * "g8": Display the bytes used for the UTF-8 character under the
6172 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006173 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 */
6175 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006176 if (cap->count0 == 8)
6177 utf_find_illegal();
6178 else
6179 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006182 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006183 case '<':
6184 show_sb_text();
6185 break;
6186
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 /*
6188 * "gg": Goto the first line in file. With a count it goes to
6189 * that line number like for "G". -- webb
6190 */
6191 case 'g':
6192 cap->arg = FALSE;
6193 nv_goto(cap);
6194 break;
6195
6196 /*
6197 * Two-character operators:
6198 * "gq" Format text
6199 * "gw" Format text and keep cursor position
6200 * "g~" Toggle the case of the text.
6201 * "gu" Change text to lower case.
6202 * "gU" Change text to upper case.
6203 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006204 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 */
6206 case 'q':
6207 case 'w':
6208 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006209 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 case '~':
6211 case 'u':
6212 case 'U':
6213 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006214 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 nv_operator(cap);
6216 break;
6217
6218 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006219 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 * current function
6221 * "gD": idem, but in the current file.
6222 */
6223 case 'd':
6224 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006225 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 break;
6227
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 /*
6229 * g<*Mouse> : <C-*mouse>
6230 */
6231 case K_MIDDLEMOUSE:
6232 case K_MIDDLEDRAG:
6233 case K_MIDDLERELEASE:
6234 case K_LEFTMOUSE:
6235 case K_LEFTDRAG:
6236 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006237 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 case K_RIGHTMOUSE:
6239 case K_RIGHTDRAG:
6240 case K_RIGHTRELEASE:
6241 case K_X1MOUSE:
6242 case K_X1DRAG:
6243 case K_X1RELEASE:
6244 case K_X2MOUSE:
6245 case K_X2DRAG:
6246 case K_X2RELEASE:
6247 mod_mask = MOD_MASK_CTRL;
6248 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6249 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
6251 case K_IGNORE:
6252 break;
6253
6254 /*
6255 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6256 */
6257 case 'p':
6258 case 'P':
6259 nv_put(cap);
6260 break;
6261
6262#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006263 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 case 'o':
6265 goto_byte(cap->count0);
6266 break;
6267#endif
6268
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006269 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006271 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {
6273 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006274 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 break;
6276 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (!checkclearopq(oap))
6279 do_exmode(TRUE);
6280 break;
6281
6282#ifdef FEAT_JUMPLIST
6283 case ',':
6284 nv_pcmark(cap);
6285 break;
6286
6287 case ';':
6288 cap->count1 = -cap->count1;
6289 nv_pcmark(cap);
6290 break;
6291#endif
6292
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006293 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006294 if (!checkclearop(oap))
6295 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006296 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006297 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006298 if (!checkclearop(oap))
6299 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006300 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006301
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006302 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006303 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006304 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006305 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006306 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006307 break;
6308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 default:
6310 clearopbeep(oap);
6311 break;
6312 }
6313}
6314
6315/*
6316 * Handle "o" and "O" commands.
6317 */
6318 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006319n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006321#ifdef FEAT_CONCEAL
6322 linenr_T oldline = curwin->w_cursor.lnum;
6323#endif
6324
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 if (!checkclearopq(cap->oap))
6326 {
6327#ifdef FEAT_FOLDING
6328 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006329 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 (void)hasFolding(curwin->w_cursor.lnum,
6331 &curwin->w_cursor.lnum, NULL);
6332 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006333 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 (void)hasFolding(curwin->w_cursor.lnum,
6335 NULL, &curwin->w_cursor.lnum);
6336#endif
6337 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6338 (cap->cmdchar == 'O' ? 1 : 0)),
6339 (linenr_T)(curwin->w_cursor.lnum +
6340 (cap->cmdchar == 'o' ? 1 : 0))
6341 ) == OK
6342 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006343 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6344 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006346#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006347 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006348 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006349#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006350#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006351 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006352 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006353 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006354#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006355 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006356 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6357 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6359 }
6360 }
6361}
6362
6363/*
6364 * "." command: redo last change.
6365 */
6366 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006367nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
6369 if (!checkclearopq(cap->oap))
6370 {
6371 /*
6372 * If "restart_edit" is TRUE, the last but one command is repeated
6373 * instead of the last command (inserting text). This is used for
6374 * CTRL-O <.> in insert mode.
6375 */
6376 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6377 clearopbeep(cap->oap);
6378 }
6379}
6380
6381/*
6382 * CTRL-R: undo undo
6383 */
6384 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006385nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 if (!checkclearopq(cap->oap))
6388 {
6389 u_redo((int)cap->count1);
6390 curwin->w_set_curswant = TRUE;
6391 }
6392}
6393
6394/*
6395 * Handle "U" command.
6396 */
6397 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006398nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006400 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006401 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006403 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 cap->cmdchar = 'g';
6405 cap->nchar = 'U';
6406 nv_operator(cap);
6407 }
6408 else if (!checkclearopq(cap->oap))
6409 {
6410 u_undoline();
6411 curwin->w_set_curswant = TRUE;
6412 }
6413}
6414
6415/*
6416 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6417 * single character.
6418 */
6419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006420nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006422 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006423 {
6424#ifdef FEAT_JOB_CHANNEL
6425 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6426 {
6427 clearopbeep(cap->oap);
6428 return;
6429 }
6430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 else
6434 nv_operator(cap);
6435}
6436
6437/*
6438 * Handle an operator command.
6439 * The actual work is done by do_pending_operator().
6440 */
6441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006442nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 int op_type;
6445
6446 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006447#ifdef FEAT_JOB_CHANNEL
6448 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6449 {
6450 clearopbeep(cap->oap);
6451 return;
6452 }
6453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006455 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 nv_lineop(cap);
6457 else if (!checkclearop(cap->oap))
6458 {
6459 cap->oap->start = curwin->w_cursor;
6460 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006461#ifdef FEAT_EVAL
6462 set_op_var(op_type);
6463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 }
6465}
6466
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006467#ifdef FEAT_EVAL
6468/*
6469 * Set v:operator to the characters for "optype".
6470 */
6471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006473{
6474 char_u opchars[3];
6475
6476 if (optype == OP_NOP)
6477 set_vim_var_string(VV_OP, NULL, 0);
6478 else
6479 {
6480 opchars[0] = get_op_char(optype);
6481 opchars[1] = get_extra_op_char(optype);
6482 opchars[2] = NUL;
6483 set_vim_var_string(VV_OP, opchars, -1);
6484 }
6485}
6486#endif
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
6489 * Handle linewise operator "dd", "yy", etc.
6490 *
6491 * "_" is is a strange motion command that helps make operators more logical.
6492 * It is actually implemented, but not documented in the real Vi. This motion
6493 * command actually refers to "the current line". Commands like "dd" and "yy"
6494 * are really an alternate form of "d_" and "y_". It does accept a count, so
6495 * "d3_" works to delete 3 lines.
6496 */
6497 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006498nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
6500 cap->oap->motion_type = MLINE;
6501 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6502 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006503 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006504 && cap->oap->motion_force != 'v'
6505 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 || cap->oap->op_type == OP_LSHIFT
6507 || cap->oap->op_type == OP_RSHIFT)
6508 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006509 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 beginline(BL_WHITE | BL_FIX);
6511}
6512
6513/*
6514 * <Home> command.
6515 */
6516 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006517nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006519 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006520 if (mod_mask & MOD_MASK_CTRL)
6521 nv_goto(cap);
6522 else
6523 {
6524 cap->count0 = 1;
6525 nv_pipe(cap);
6526 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006527 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6528 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
6530
6531/*
6532 * "|" command.
6533 */
6534 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006535nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 cap->oap->motion_type = MCHAR;
6538 cap->oap->inclusive = FALSE;
6539 beginline(0);
6540 if (cap->count0 > 0)
6541 {
6542 coladvance((colnr_T)(cap->count0 - 1));
6543 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6544 }
6545 else
6546 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006547 // keep curswant at the column where we wanted to go, not where
6548 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 curwin->w_set_curswant = FALSE;
6550}
6551
6552/*
6553 * Handle back-word command "b" and "B".
6554 * cap->arg is 1 for "B"
6555 */
6556 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006557nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558{
6559 cap->oap->motion_type = MCHAR;
6560 cap->oap->inclusive = FALSE;
6561 curwin->w_set_curswant = TRUE;
6562 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6563 clearopbeep(cap->oap);
6564#ifdef FEAT_FOLDING
6565 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6566 foldOpenCursor();
6567#endif
6568}
6569
6570/*
6571 * Handle word motion commands "e", "E", "w" and "W".
6572 * cap->arg is TRUE for "E" and "W".
6573 */
6574 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006575nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 int n;
6578 int word_end;
6579 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006580 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581
6582 /*
6583 * Set inclusive for the "E" and "e" command.
6584 */
6585 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6586 word_end = TRUE;
6587 else
6588 word_end = FALSE;
6589 cap->oap->inclusive = word_end;
6590
6591 /*
6592 * "cw" and "cW" are a special case.
6593 */
6594 if (!word_end && cap->oap->op_type == OP_CHANGE)
6595 {
6596 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006597 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006599 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
6601 /*
6602 * Reproduce a funny Vi behaviour: "cw" on a blank only
6603 * changes one character, not all blanks until the start of
6604 * the next word. Only do this when the 'w' flag is included
6605 * in 'cpoptions'.
6606 */
6607 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6608 {
6609 cap->oap->inclusive = TRUE;
6610 cap->oap->motion_type = MCHAR;
6611 return;
6612 }
6613 }
6614 else
6615 {
6616 /*
6617 * This is a little strange. To match what the real Vi does,
6618 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6619 * that we are not on a space or a TAB. This seems impolite
6620 * at first, but it's really more what we mean when we say
6621 * 'cw'.
6622 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006623 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 * will change only one character! This is done by setting
6625 * flag.
6626 */
6627 cap->oap->inclusive = TRUE;
6628 word_end = TRUE;
6629 flag = TRUE;
6630 }
6631 }
6632 }
6633
6634 cap->oap->motion_type = MCHAR;
6635 curwin->w_set_curswant = TRUE;
6636 if (word_end)
6637 n = end_word(cap->count1, cap->arg, flag, FALSE);
6638 else
6639 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6640
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006641 // Don't leave the cursor on the NUL past the end of line. Unless we
6642 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006643 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006644 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645
6646 if (n == FAIL && cap->oap->op_type == OP_NOP)
6647 clearopbeep(cap->oap);
6648 else
6649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651#ifdef FEAT_FOLDING
6652 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6653 foldOpenCursor();
6654#endif
6655 }
6656}
6657
6658/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006659 * Used after a movement command: If the cursor ends up on the NUL after the
6660 * end of the line, may move it back to the last character and make the motion
6661 * inclusive.
6662 */
6663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006664adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006665{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006666 // The cursor cannot remain on the NUL when:
6667 // - the column is > 0
6668 // - not in Visual mode or 'selection' is "o"
6669 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006670 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006671 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006672 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006673 {
6674 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006675 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006676 if (has_mbyte)
6677 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006678 oap->inclusive = TRUE;
6679 }
6680}
6681
6682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 * "0" and "^" commands.
6684 * cap->arg is the argument for beginline().
6685 */
6686 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006687nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 cap->oap->motion_type = MCHAR;
6690 cap->oap->inclusive = FALSE;
6691 beginline(cap->arg);
6692#ifdef FEAT_FOLDING
6693 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6694 foldOpenCursor();
6695#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006696 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6697 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698}
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700/*
6701 * In exclusive Visual mode, may include the last character.
6702 */
6703 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006704adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006707 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 if (has_mbyte)
6710 inc_cursor();
6711 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 ++curwin->w_cursor.col;
6713 cap->oap->inclusive = FALSE;
6714 }
6715}
6716
6717/*
6718 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6719 * Should check VIsual_mode before calling this.
6720 * Returns TRUE when backed up to the previous line.
6721 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006722 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006723unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 pos_T *pp;
6726
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006727 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006729 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 pp = &curwin->w_cursor;
6731 else
6732 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (pp->coladd > 0)
6734 --pp->coladd;
6735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (pp->col > 0)
6737 {
6738 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006739 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
6741 else if (pp->lnum > 1)
6742 {
6743 --pp->lnum;
6744 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6745 return TRUE;
6746 }
6747 }
6748 return FALSE;
6749}
6750
6751/*
6752 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6753 */
6754 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006755nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
6757 if (VIsual_active)
6758 VIsual_select = TRUE;
6759 else if (VIsual_reselect)
6760 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006761 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 cap->arg = TRUE;
6763 nv_g_cmd(cap);
6764 }
6765}
6766
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
6768/*
6769 * "G", "gg", CTRL-END, CTRL-HOME.
6770 * cap->arg is TRUE for "G".
6771 */
6772 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006773nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 linenr_T lnum;
6776
6777 if (cap->arg)
6778 lnum = curbuf->b_ml.ml_line_count;
6779 else
6780 lnum = 1L;
6781 cap->oap->motion_type = MLINE;
6782 setpcmark();
6783
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006784 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 if (cap->count0 != 0)
6786 lnum = cap->count0;
6787 if (lnum < 1L)
6788 lnum = 1L;
6789 else if (lnum > curbuf->b_ml.ml_line_count)
6790 lnum = curbuf->b_ml.ml_line_count;
6791 curwin->w_cursor.lnum = lnum;
6792 beginline(BL_SOL | BL_FIX);
6793#ifdef FEAT_FOLDING
6794 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6795 foldOpenCursor();
6796#endif
6797}
6798
6799/*
6800 * CTRL-\ in Normal mode.
6801 */
6802 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006803nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6806 {
6807 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006808 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006809 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 restart_edit = 0;
6811#ifdef FEAT_CMDWIN
6812 if (cmdwin_type != 0)
6813 cmdwin_result = Ctrl_C;
6814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (VIsual_active)
6816 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006817 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 redraw_curbuf_later(INVERTED);
6819 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006820 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (cap->nchar == Ctrl_G && p_im)
6822 restart_edit = 'a';
6823 }
6824 else
6825 clearopbeep(cap->oap);
6826}
6827
6828/*
6829 * ESC in Normal mode: beep, but don't flush buffers.
6830 * Don't even beep if we are canceling a command.
6831 */
6832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006833nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
6835 int no_reason;
6836
6837 no_reason = (cap->oap->op_type == OP_NOP
6838 && cap->opcount == 0
6839 && cap->count0 == 0
6840 && cap->oap->regname == 0
6841 && !p_im);
6842
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006843 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {
6845 if (restart_edit == 0
6846#ifdef FEAT_CMDWIN
6847 && cmdwin_type == 0
6848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006851 {
6852 if (anyBufIsChanged())
6853 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6854 else
6855 msg(_("Type :qa and press <Enter> to exit Vim"));
6856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006858 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6859 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 if (!p_im)
6861 restart_edit = 0;
6862#ifdef FEAT_CMDWIN
6863 if (cmdwin_type != 0)
6864 {
6865 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006866 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 return;
6868 }
6869#endif
6870 }
6871
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (VIsual_active)
6873 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006874 end_visual_mode(); // stop Visual
6875 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 curwin->w_set_curswant = TRUE;
6877 redraw_curbuf_later(INVERTED);
6878 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006879 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006880 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 clearop(cap->oap);
6882
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006883 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6884 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006885 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 restart_edit = 'a';
6887}
6888
6889/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006890 * Move the cursor for the "A" command.
6891 */
6892 void
6893set_cursor_for_append_to_line(void)
6894{
6895 curwin->w_set_curswant = TRUE;
6896 if (ve_flags == VE_ALL)
6897 {
6898 int save_State = State;
6899
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006900 // Pretend Insert mode here to allow the cursor on the
6901 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006902 State = INSERT;
6903 coladvance((colnr_T)MAXCOL);
6904 State = save_State;
6905 }
6906 else
6907 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6908}
6909
6910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006912 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 */
6914 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006915nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006917 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6919 cap->cmdchar = 'i';
6920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006921 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006923 {
6924#ifdef FEAT_TERMINAL
6925 if (term_in_normal_mode())
6926 {
6927 end_visual_mode();
6928 clearop(cap->oap);
6929 term_enter_job_mode();
6930 return;
6931 }
6932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006937 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6938 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {
6940#ifdef FEAT_TEXTOBJ
6941 nv_object(cap);
6942#else
6943 clearopbeep(cap->oap);
6944#endif
6945 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006946#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006947 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006948 {
6949 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006950 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006951 return;
6952 }
6953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 else if (!curbuf->b_p_ma && !p_im)
6955 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006956 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006957 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006959 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006960 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006961 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006963 else if (cap->cmdchar == K_PS && VIsual_active)
6964 {
6965 pos_T old_pos = curwin->w_cursor;
6966 pos_T old_visual = VIsual;
6967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006968 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01006969 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6970 {
6971 shift_delete_registers();
6972 cap->oap->regname = '1';
6973 }
6974 else
6975 cap->oap->regname = '-';
6976 cap->cmdchar = 'd';
6977 cap->nchar = NUL;
6978 nv_operator(cap);
6979 do_pending_operator(cap, 0, FALSE);
6980 cap->cmdchar = K_PS;
6981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006982 // When the last char in the line was deleted then append. Detect this
6983 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006984 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
6985 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01006986 inc_cursor();
6987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006988 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01006989 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 else if (!checkclearopq(cap->oap))
6992 {
6993 switch (cap->cmdchar)
6994 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006995 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006996 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 break;
6998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006999 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007000 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7001 beginline(BL_WHITE);
7002 else
7003 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 break;
7005
Bram Moolenaara1891842017-02-04 21:34:31 +01007006 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007007 // Bracketed paste works like "a"ppend, unless the cursor is in
7008 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007009 if (curwin->w_cursor.col == 0)
7010 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007011 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007013 case 'a': // "a"ppend is like "i"nsert on the next character.
7014 // increment coladd when in virtual space, increment the
7015 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 if (virtual_active()
7017 && (curwin->w_cursor.coladd > 0
7018 || *ml_get_cursor() == NUL
7019 || *ml_get_cursor() == TAB))
7020 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007021 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 inc_cursor();
7023 break;
7024 }
7025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7027 {
7028 int save_State = State;
7029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007030 // Pretend Insert mode here to allow the cursor on the
7031 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 State = INSERT;
7033 coladvance(getviscol());
7034 State = save_State;
7035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036
7037 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7038 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007039 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007040 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007041 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042}
7043
7044/*
7045 * Invoke edit() and take care of "restart_edit" and the return value.
7046 */
7047 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007048invoke_edit(
7049 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007050 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007051 int cmd,
7052 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
7054 int restart_edit_save = 0;
7055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007056 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7057 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7058 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 if (repl || !stuff_empty())
7060 restart_edit_save = restart_edit;
7061 else
7062 restart_edit_save = 0;
7063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 restart_edit = 0;
7066
7067 if (edit(cmd, startln, cap->count1))
7068 cap->retval |= CA_COMMAND_BUSY;
7069
7070 if (restart_edit == 0)
7071 restart_edit = restart_edit_save;
7072}
7073
7074#ifdef FEAT_TEXTOBJ
7075/*
7076 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7077 */
7078 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007079nv_object(
7080 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
7082 int flag;
7083 int include;
7084 char_u *mps_save;
7085
7086 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007087 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007089 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007091 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 mps_save = curbuf->b_p_mps;
7093 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7094
7095 switch (cap->nchar)
7096 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007097 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 flag = current_word(cap->oap, cap->count1, include, FALSE);
7099 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 flag = current_word(cap->oap, cap->count1, include, TRUE);
7102 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007103 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 case '(':
7105 case ')':
7106 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7107 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 case '{':
7110 case '}':
7111 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7112 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007113 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 case ']':
7115 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7116 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007117 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 case '>':
7119 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7120 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007121 case 't': // "at" = a tag block (xml and html)
7122 // Do not adjust oap->end in do_pending_operator()
7123 // otherwise there are different results for 'dit'
7124 // (note leading whitespace in last line):
7125 // 1) <b> 2) <b>
7126 // foobar foobar
7127 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007128 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007129 flag = current_tagblock(cap->oap, cap->count1, include);
7130 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007131 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 flag = current_par(cap->oap, cap->count1, include, 'p');
7133 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007134 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 flag = current_sent(cap->oap, cap->count1, include);
7136 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007137 case '"': // "a"" = a double quoted string
7138 case '\'': // "a'" = a single quoted string
7139 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007140 flag = current_quote(cap->oap, cap->count1, include,
7141 cap->nchar);
7142 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007143#if 0 // TODO
7144 case 'S': // "aS" = a section
7145 case 'f': // "af" = a filename
7146 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147#endif
7148 default:
7149 flag = FAIL;
7150 break;
7151 }
7152
7153 curbuf->b_p_mps = mps_save;
7154 if (flag == FAIL)
7155 clearopbeep(cap->oap);
7156 adjust_cursor_col();
7157 curwin->w_set_curswant = TRUE;
7158}
7159#endif
7160
7161/*
7162 * "q" command: Start/stop recording.
7163 * "q:", "q/", "q?": edit command-line in command-line window.
7164 */
7165 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007166nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167{
7168 if (cap->oap->op_type == OP_FORMAT)
7169 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007170 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 cap->cmdchar = 'g';
7172 cap->nchar = 'q';
7173 nv_operator(cap);
7174 }
7175 else if (!checkclearop(cap->oap))
7176 {
7177#ifdef FEAT_CMDWIN
7178 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7179 {
7180 stuffcharReadbuff(cap->nchar);
7181 stuffcharReadbuff(K_CMDWIN);
7182 }
7183 else
7184#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007185 // (stop) recording into a named register, unless executing a
7186 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007187 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 clearopbeep(cap->oap);
7189 }
7190}
7191
7192/*
7193 * Handle the "@r" command.
7194 */
7195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007196nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 if (checkclearop(cap->oap))
7199 return;
7200#ifdef FEAT_EVAL
7201 if (cap->nchar == '=')
7202 {
7203 if (get_expr_register() == NUL)
7204 return;
7205 }
7206#endif
7207 while (cap->count1-- && !got_int)
7208 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007209 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {
7211 clearopbeep(cap->oap);
7212 break;
7213 }
7214 line_breakcheck();
7215 }
7216}
7217
7218/*
7219 * Handle the CTRL-U and CTRL-D commands.
7220 */
7221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007222nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7225 || (cap->cmdchar == Ctrl_D
7226 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7227 clearopbeep(cap->oap);
7228 else if (!checkclearop(cap->oap))
7229 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7230}
7231
7232/*
7233 * Handle "J" or "gJ" command.
7234 */
7235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007236nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007238 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007240 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {
7242 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007243 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7245 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007247 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007248 if (cap->count0 <= 2)
7249 {
7250 clearopbeep(cap->oap);
7251 return;
7252 }
7253 cap->count0 = curbuf->b_ml.ml_line_count
7254 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007256
7257 prep_redo(cap->oap->regname, cap->count0,
7258 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7259 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 }
7261}
7262
7263/*
7264 * "P", "gP", "p" and "gp" commands.
7265 */
7266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007267nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007269 nv_put_opt(cap, FALSE);
7270}
7271
7272/*
7273 * "P", "gP", "p" and "gp" commands.
7274 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7275 */
7276 static void
7277nv_put_opt(cmdarg_T *cap, int fix_indent)
7278{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 int regname = 0;
7280 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007281 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007282 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 int dir;
7284 int flags = 0;
7285
7286 if (cap->oap->op_type != OP_NOP)
7287 {
7288#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007289 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7291 {
7292 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007293 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 }
7295 else
7296#endif
7297 clearopbeep(cap->oap);
7298 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007299#ifdef FEAT_JOB_CHANNEL
7300 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7301 {
7302 clearopbeep(cap->oap);
7303 }
7304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 else
7306 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007307 if (fix_indent)
7308 {
7309 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7310 ? FORWARD : BACKWARD;
7311 flags |= PUT_FIXINDENT;
7312 }
7313 else
7314 dir = (cap->cmdchar == 'P'
7315 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 ? BACKWARD : FORWARD;
7317 prep_redo_cmd(cap);
7318 if (cap->cmdchar == 'g')
7319 flags |= PUT_CURSEND;
7320
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 if (VIsual_active)
7322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007323 // Putting in Visual mode: The put text replaces the selected
7324 // text. First delete the selected text, then put the new text.
7325 // Need to save and restore the registers that the delete
7326 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007327 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007329#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007331#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007332 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007333 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007334#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
7338 )
7339 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007340 // The delete is going to overwrite the register we want to
7341 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 reg1 = get_register(regname, TRUE);
7343 }
7344
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007345 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 cap->cmdchar = 'd';
7347 cap->nchar = NUL;
7348 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007349 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 nv_operator(cap);
7351 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007352 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007353 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007355 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 cap->oap->regname = regname;
7357
7358 if (reg1 != NULL)
7359 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007360 // Delete probably changed the register we want to put, save
7361 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 reg2 = get_register(regname, FALSE);
7363 put_register(regname, reg1);
7364 }
7365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366 // When deleted a linewise Visual area, put the register as
7367 // lines to avoid it joined with the next line. When deletion was
7368 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 if (VIsual_mode == 'V')
7370 flags |= PUT_LINE;
7371 else if (VIsual_mode == 'v')
7372 flags |= PUT_LINE_SPLIT;
7373 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7374 flags |= PUT_LINE_FORWARD;
7375 dir = BACKWARD;
7376 if ((VIsual_mode != 'V'
7377 && curwin->w_cursor.col < curbuf->b_op_start.col)
7378 || (VIsual_mode == 'V'
7379 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007380 // cursor is at the end of the line or end of file, put
7381 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007383 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007384 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 do_put(cap->oap->regname, dir, cap->count1, flags);
7387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007388 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 if (reg2 != NULL)
7390 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007391
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007392 // What to reselect with "gv"? Selecting the just put text seems to
7393 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007394 if (was_visual)
7395 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007396 curbuf->b_visual.vi_start = curbuf->b_op_start;
7397 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007398 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007399 if (*p_sel == 'e')
7400 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007401 }
7402
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007403 // When all lines were selected and deleted do_put() leaves an empty
7404 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007405 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007406 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007407 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007408 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007410 // If the cursor was in that line, move it to the end of the last
7411 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007412 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7413 {
7414 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7415 coladvance((colnr_T)MAXCOL);
7416 }
7417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 auto_format(FALSE, TRUE);
7419 }
7420}
7421
7422/*
7423 * "o" and "O" commands.
7424 */
7425 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007426nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007429 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7431 {
7432 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007433 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 }
7435 else
7436#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007437 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007439#ifdef FEAT_JOB_CHANNEL
7440 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007441 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 n_opencmd(cap);
7445}
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447#ifdef FEAT_NETBEANS_INTG
7448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007449nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450{
7451 netbeans_keycommand(cap->nchar);
7452}
7453#endif
7454
7455#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007457nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7460}
7461#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007462
Bram Moolenaar3918c952005-03-15 22:34:55 +00007463/*
7464 * Trigger CursorHold event.
7465 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7466 * input buffer. "did_cursorhold" is set to avoid retriggering.
7467 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007469nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007470{
7471 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7472 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007473 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007474}