blob: 50f7b3185d4eacf597a07692c6b108aab1ba0077 [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);
68static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
69static 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
140/* 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 */
145
146#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 */
152
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{
170 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 */
174} 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
304 /* pound sign */
305 {POUND, nv_ident, 0, 0},
306#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200307 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
308 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
309 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
310 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 {K_LEFTMOUSE, nv_mouse, 0, 0},
312 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
313 {K_LEFTDRAG, nv_mouse, 0, 0},
314 {K_LEFTRELEASE, nv_mouse, 0, 0},
315 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100316 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
318 {K_MIDDLEDRAG, nv_mouse, 0, 0},
319 {K_MIDDLERELEASE, nv_mouse, 0, 0},
320 {K_RIGHTMOUSE, nv_mouse, 0, 0},
321 {K_RIGHTDRAG, nv_mouse, 0, 0},
322 {K_RIGHTRELEASE, nv_mouse, 0, 0},
323 {K_X1MOUSE, nv_mouse, 0, 0},
324 {K_X1DRAG, nv_mouse, 0, 0},
325 {K_X1RELEASE, nv_mouse, 0, 0},
326 {K_X2MOUSE, nv_mouse, 0, 0},
327 {K_X2DRAG, nv_mouse, 0, 0},
328 {K_X2RELEASE, nv_mouse, 0, 0},
329#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000330 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000331 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 {K_INS, nv_edit, 0, 0},
333 {K_KINS, nv_edit, 0, 0},
334 {K_BS, nv_ctrlh, 0, 0},
335 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
336 {K_S_UP, nv_page, NV_SS, BACKWARD},
337 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
338 {K_S_DOWN, nv_page, NV_SS, FORWARD},
339 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
340 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
341 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
342 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
343 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
344 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
345 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
346 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
347 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
348 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
349 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
350 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 {K_S_END, nv_end, NV_SS, FALSE},
352 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
353 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
354 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 {K_S_HOME, nv_home, NV_SS, 0},
356 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
357 {K_DEL, nv_abbrev, 0, 0},
358 {K_KDEL, nv_abbrev, 0, 0},
359 {K_UNDO, nv_kundo, 0, 0},
360 {K_HELP, nv_help, NV_NCW, 0},
361 {K_F1, nv_help, NV_NCW, 0},
362 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364#ifdef FEAT_GUI
365 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
366 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
367#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000368#ifdef FEAT_GUI_TABLINE
369 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000370 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372#ifdef FEAT_NETBEANS_INTG
373 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
374#endif
375#ifdef FEAT_DND
376 {K_DROP, nv_drop, NV_STS, 0},
377#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000378 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100379 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380};
381
382/* Number of commands in nv_cmds[]. */
383#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
384
385/* Sorted index of commands in nv_cmds[]. */
386static short nv_cmd_idx[NV_CMDS_SIZE];
387
388/* The highest index for which
389 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
390static int nv_max_linear;
391
392/*
393 * Compare functions for qsort() below, that checks the command character
394 * through the index in nv_cmd_idx[].
395 */
396 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100397nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
399 int c1, c2;
400
401 /* The commands are sorted on absolute value. */
402 c1 = nv_cmds[*(const short *)s1].cmd_char;
403 c2 = nv_cmds[*(const short *)s2].cmd_char;
404 if (c1 < 0)
405 c1 = -c1;
406 if (c2 < 0)
407 c2 = -c2;
408 return c1 - c2;
409}
410
411/*
412 * Initialize the nv_cmd_idx[] table.
413 */
414 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100415init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416{
417 int i;
418
419 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000420 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 nv_cmd_idx[i] = i;
422
423 /* Sort the commands by the command character. */
424 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
425
426 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000427 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
429 break;
430 nv_max_linear = i - 1;
431}
432
433/*
434 * Search for a command in the commands table.
435 * Returns -1 for invalid command.
436 */
437 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100438find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
440 int i;
441 int idx;
442 int top, bot;
443 int c;
444
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 /* A multi-byte character is never a command. */
446 if (cmdchar >= 0x100)
447 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449 /* We use the absolute value of the character. Special keys have a
450 * negative value, but are sorted on their absolute value. */
451 if (cmdchar < 0)
452 cmdchar = -cmdchar;
453
454 /* If the character is in the first part: The character is the index into
455 * nv_cmd_idx[]. */
456 if (cmdchar <= nv_max_linear)
457 return nv_cmd_idx[cmdchar];
458
459 /* Perform a binary search. */
460 bot = nv_max_linear + 1;
461 top = NV_CMDS_SIZE - 1;
462 idx = -1;
463 while (bot <= top)
464 {
465 i = (top + bot) / 2;
466 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
467 if (c < 0)
468 c = -c;
469 if (cmdchar == c)
470 {
471 idx = nv_cmd_idx[i];
472 break;
473 }
474 if (cmdchar > c)
475 bot = i + 1;
476 else
477 top = i - 1;
478 }
479 return idx;
480}
481
482/*
483 * Execute a command in Normal mode.
484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100486normal_cmd(
487 oparg_T *oap,
488 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 cmdarg_T ca; /* command arguments */
491 int c;
492 int ctrl_w = FALSE; /* got CTRL-W command */
493 int old_col = curwin->w_curswant;
494#ifdef FEAT_CMDL_INFO
495 int need_flushbuf; /* need to call out_flush() */
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 pos_T old_pos; /* cursor position before command */
498 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 static int old_mapped_len = 0;
500 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000501#ifdef FEAT_EVAL
502 int set_prevcount = FALSE;
503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
505 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
506 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000507
508 /* Use a count remembered from before entering an operator. After typing
509 * "3d" we return from normal_cmd() and come back here, the "3" is
510 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 ca.opcount = opcount;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 /*
514 * If there is an operator pending, then the command we take this time
515 * will terminate it. Finish_op tells us to finish the operation before
516 * returning this time (unless the operation was cancelled).
517 */
518#ifdef CURSOR_SHAPE
519 c = finish_op;
520#endif
521 finish_op = (oap->op_type != OP_NOP);
522#ifdef CURSOR_SHAPE
523 if (finish_op != c)
524 {
525 ui_cursor_shape(); /* may show different cursor shape */
526# ifdef FEAT_MOUSESHAPE
527 update_mouseshape(-1);
528# endif
529 }
530#endif
531
Bram Moolenaara983fe92008-07-31 20:04:27 +0000532 /* When not finishing an operator and no register name typed, reset the
533 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000537#ifdef FEAT_EVAL
538 set_prevcount = TRUE;
539#endif
540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaara983fe92008-07-31 20:04:27 +0000542 /* Restore counts from before receiving K_CURSORHOLD. This means after
543 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
544 * "3 * 2". */
545 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
546 {
547 ca.opcount = oap->prev_opcount;
548 ca.count0 = oap->prev_count0;
549 oap->prev_opcount = 0;
550 oap->prev_count0 = 0;
551 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 State = NORMAL_BUSY;
556#ifdef USE_ON_FLY_SCROLL
557 dont_scroll = FALSE; /* allow scrolling here */
558#endif
559
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100560#ifdef FEAT_EVAL
561 /* Set v:count here, when called from main() and not a stuffed
562 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100563 * when there is no count. Do set it for redo. */
564 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100565 set_vcount_ca(&ca, &set_prevcount);
566#endif
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /*
569 * Get the command character from the user.
570 */
571 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100572 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 /*
575 * If a mapping was started in Visual or Select mode, remember the length
576 * of the mapping. This is used below to not return to Insert mode for as
577 * long as the mapping is being executed.
578 */
579 if (restart_edit == 0)
580 old_mapped_len = 0;
581 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000582 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 old_mapped_len = typebuf_maplen();
584
585 if (c == NUL)
586 c = K_ZERO;
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 /*
589 * In Select mode, typed text replaces the selection.
590 */
591 if (VIsual_active
592 && VIsual_select
593 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
594 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000595 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
596 * 'insertmode' is set) fake a "d"elete command, Insert mode will
597 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000598 * Insert the typed character in the typeahead buffer, so that it can
599 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000600 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000601 if (restart_edit != 0)
602 c = 'd';
603 else
604 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000605 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200606 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609#ifdef FEAT_CMDL_INFO
610 need_flushbuf = add_to_showcmd(c);
611#endif
612
613getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 /*
617 * Handle a count before a command and compute ca.count0.
618 * Note that '0' is a command and not the start of a count, but it's
619 * part of a count after other digits.
620 */
621 while ( (c >= '1' && c <= '9')
622 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
623 {
624 if (c == K_DEL || c == K_KDEL)
625 {
626 ca.count0 /= 10;
627#ifdef FEAT_CMDL_INFO
628 del_from_showcmd(4); /* delete the digit and ~@% */
629#endif
630 }
631 else
632 ca.count0 = ca.count0 * 10 + (c - '0');
633 if (ca.count0 < 0) /* got too large! */
634 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000635#ifdef FEAT_EVAL
636 /* Set v:count here, when called from main() and not a stuffed
637 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100638 * right after the count. Do set it for redo. */
639 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100640 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (ctrl_w)
643 {
644 ++no_mapping;
645 ++allow_keys; /* no mapping for nchar, but keys */
646 }
647 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000648 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 --no_zero_mapping;
651 if (ctrl_w)
652 {
653 --no_mapping;
654 --allow_keys;
655 }
656#ifdef FEAT_CMDL_INFO
657 need_flushbuf |= add_to_showcmd(c);
658#endif
659 }
660
661 /*
662 * If we got CTRL-W there may be a/another count
663 */
664 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
665 {
666 ctrl_w = TRUE;
667 ca.opcount = ca.count0; /* remember first count */
668 ca.count0 = 0;
669 ++no_mapping;
670 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000671 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 --no_mapping;
674 --allow_keys;
675#ifdef FEAT_CMDL_INFO
676 need_flushbuf |= add_to_showcmd(c);
677#endif
678 goto getcount; /* jump back */
679 }
680 }
681
Bram Moolenaara983fe92008-07-31 20:04:27 +0000682 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000684 /* Save the count values so that ca.opcount and ca.count0 are exactly
685 * the same when coming back here after handling K_CURSORHOLD. */
686 oap->prev_opcount = ca.opcount;
687 oap->prev_count0 = ca.count0;
688 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100689 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000690 {
691 /*
692 * If we're in the middle of an operator (including after entering a
693 * yank buffer with '"') AND we had a count before the operator, then
694 * that count overrides the current value of ca.count0.
695 * What this means effectively, is that commands like "3dw" get turned
696 * into "d3w" which makes things fall into place pretty neatly.
697 * If you give a count before AND after the operator, they are
698 * multiplied.
699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (ca.count0)
701 ca.count0 *= ca.opcount;
702 else
703 ca.count0 = ca.opcount;
704 }
705
706 /*
707 * Always remember the count. It will be set to zero (on the next call,
708 * above) when there is no pending operator.
709 * When called from main(), save the count for use by the "count" built-in
710 * variable.
711 */
712 ca.opcount = ca.count0;
713 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
714
715#ifdef FEAT_EVAL
716 /*
717 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100718 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100720 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000721 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#endif
723
724 /*
725 * Find the command character in the table of commands.
726 * For CTRL-W we already got nchar when looking for a count.
727 */
728 if (ctrl_w)
729 {
730 ca.nchar = c;
731 ca.cmdchar = Ctrl_W;
732 }
733 else
734 ca.cmdchar = c;
735 idx = find_command(ca.cmdchar);
736 if (idx < 0)
737 {
738 /* Not a known command: beep. */
739 clearopbeep(oap);
740 goto normal_end;
741 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000742
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000743 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200745 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000747 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 goto normal_end;
749 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000750 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
751 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 /*
754 * In Visual/Select mode, a few keys are handled in a special way.
755 */
756 if (VIsual_active)
757 {
758 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
759 if (km_stopsel
760 && (nv_cmds[idx].cmd_flags & NV_STS)
761 && !(mod_mask & MOD_MASK_SHIFT))
762 {
763 end_visual_mode();
764 redraw_curbuf_later(INVERTED);
765 }
766
767 /* Keys that work different when 'keymodel' contains "startsel" */
768 if (km_startsel)
769 {
770 if (nv_cmds[idx].cmd_flags & NV_SS)
771 {
772 unshift_special(&ca);
773 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000774 if (idx < 0)
775 {
776 /* Just in case */
777 clearopbeep(oap);
778 goto normal_end;
779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
782 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787#ifdef FEAT_RIGHTLEFT
788 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
789 && (nv_cmds[idx].cmd_flags & NV_RL))
790 {
791 /* Invert horizontal movements and operations. Only when typed by the
792 * user directly, not when the result of a mapping or "x" translated
793 * to "dl". */
794 switch (ca.cmdchar)
795 {
796 case 'l': ca.cmdchar = 'h'; break;
797 case K_RIGHT: ca.cmdchar = K_LEFT; break;
798 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
799 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
800 case 'h': ca.cmdchar = 'l'; break;
801 case K_LEFT: ca.cmdchar = K_RIGHT; break;
802 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
803 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
804 case '>': ca.cmdchar = '<'; break;
805 case '<': ca.cmdchar = '>'; break;
806 }
807 idx = find_command(ca.cmdchar);
808 }
809#endif
810
811 /*
812 * Get an additional character if we need one.
813 */
814 if ((nv_cmds[idx].cmd_flags & NV_NCH)
815 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
816 && oap->op_type == OP_NOP)
817 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
818 || (ca.cmdchar == 'q'
819 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200820 && reg_recording == 0
821 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100823 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {
825 int *cp;
826 int repl = FALSE; /* get character for replace mode */
827 int lit = FALSE; /* get extra character literally */
828 int langmap_active = FALSE; /* using :lmap mappings */
829 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100830#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 int save_smd; /* saved value of p_smd */
832#endif
833
834 ++no_mapping;
835 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000836 /* Don't generate a CursorHold event here, most commands can't handle
837 * it, e.g., nv_replace(), nv_csearch(). */
838 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (ca.cmdchar == 'g')
840 {
841 /*
842 * For 'g' get the next character now, so that we can check for
843 * "gr", "g'" and "g`".
844 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000845 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifdef FEAT_CMDL_INFO
848 need_flushbuf |= add_to_showcmd(ca.nchar);
849#endif
850 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100851 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
853 cp = &ca.extra_char; /* need to get a third character */
854 if (ca.nchar != 'r')
855 lit = TRUE; /* get it literally */
856 else
857 repl = TRUE; /* get it in replace mode */
858 }
859 else
860 cp = NULL; /* no third character needed */
861 }
862 else
863 {
864 if (ca.cmdchar == 'r') /* get it in replace mode */
865 repl = TRUE;
866 cp = &ca.nchar;
867 }
868 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
869
870 /*
871 * Get a second or third character.
872 */
873 if (cp != NULL)
874 {
875#ifdef CURSOR_SHAPE
876 if (repl)
877 {
878 State = REPLACE; /* pretend Replace mode */
879 ui_cursor_shape(); /* show different cursor shape */
880 }
881#endif
882 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
883 {
884 /* Allow mappings defined with ":lmap". */
885 --no_mapping;
886 --allow_keys;
887 if (repl)
888 State = LREPLACE;
889 else
890 State = LANGMAP;
891 langmap_active = TRUE;
892 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100893#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 save_smd = p_smd;
895 p_smd = FALSE; /* Don't let the IM code show the mode here */
896 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
897 im_set_active(TRUE);
898#endif
899
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000900 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
902 if (langmap_active)
903 {
904 /* Undo the decrement done above */
905 ++no_mapping;
906 ++allow_keys;
907 State = NORMAL_BUSY;
908 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100909#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (lang)
911 {
912 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
913 im_save_status(&curbuf->b_p_iminsert);
914 im_set_active(FALSE);
915 }
916 p_smd = save_smd;
917#endif
918#ifdef CURSOR_SHAPE
919 State = NORMAL_BUSY;
920#endif
921#ifdef FEAT_CMDL_INFO
922 need_flushbuf |= add_to_showcmd(*cp);
923#endif
924
925 if (!lit)
926 {
927#ifdef FEAT_DIGRAPHS
928 /* Typing CTRL-K gets a digraph. */
929 if (*cp == Ctrl_K
930 && ((nv_cmds[idx].cmd_flags & NV_LANG)
931 || cp == &ca.extra_char)
932 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
933 {
934 c = get_digraph(FALSE);
935 if (c > 0)
936 {
937 *cp = c;
938# ifdef FEAT_CMDL_INFO
939 /* Guessing how to update showcmd here... */
940 del_from_showcmd(3);
941 need_flushbuf |= add_to_showcmd(*cp);
942# endif
943 }
944 }
945#endif
946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 /* adjust chars > 127, except after "tTfFr" commands */
948 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#ifdef FEAT_RIGHTLEFT
950 /* adjust Hebrew mapped char */
951 if (p_hkmap && lang && KeyTyped)
952 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953#endif
954 }
955
956 /*
957 * When the next character is CTRL-\ a following CTRL-N means the
958 * command is aborted and we go to Normal mode.
959 */
960 if (cp == &ca.extra_char
961 && ca.nchar == Ctrl_BSL
962 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
963 {
964 ca.cmdchar = Ctrl_BSL;
965 ca.nchar = ca.extra_char;
966 idx = find_command(ca.cmdchar);
967 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200968 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200969 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 else if (*cp == Ctrl_BSL)
971 {
972 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
973
974 /* There is a busy wait here when typing "f<C-\>" and then
975 * something different from CTRL-N. Can't be avoided. */
976 while ((c = vpeekc()) <= 0 && towait > 0L)
977 {
978 do_sleep(towait > 50L ? 50L : towait);
979 towait -= 50L;
980 }
981 if (c > 0)
982 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000983 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (c != Ctrl_N && c != Ctrl_G)
985 vungetc(c);
986 else
987 {
988 ca.cmdchar = Ctrl_BSL;
989 ca.nchar = c;
990 idx = find_command(ca.cmdchar);
991 }
992 }
993 }
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 /* When getting a text character and the next character is a
996 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +0200997 * However, don't wait for it to arrive. Also, do enable mapping,
998 * because if it's put back with vungetc() it's too late to apply
999 * mapping. */
1000 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 while (enc_utf8 && lang && (c = vpeekc()) > 0
1002 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1003 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001004 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 if (!utf_iscomposing(c))
1006 {
1007 vungetc(c); /* it wasn't, put it back */
1008 break;
1009 }
1010 else if (ca.ncharC1 == 0)
1011 ca.ncharC1 = c;
1012 else
1013 ca.ncharC2 = c;
1014 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001015 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 --no_mapping;
1018 --allow_keys;
1019 }
1020
1021#ifdef FEAT_CMDL_INFO
1022 /*
1023 * Flush the showcmd characters onto the screen so we can see them while
1024 * the command is being executed. Only do this when the shown command was
1025 * actually displayed, otherwise this will slow down a lot when executing
1026 * mappings.
1027 */
1028 if (need_flushbuf)
1029 out_flush();
1030#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001031 if (ca.cmdchar != K_IGNORE)
1032 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 State = NORMAL;
1035
1036 if (ca.nchar == ESC)
1037 {
1038 clearop(oap);
1039 if (restart_edit == 0 && goto_im())
1040 restart_edit = 'a';
1041 goto normal_end;
1042 }
1043
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001044 if (ca.cmdchar != K_IGNORE)
1045 {
1046 msg_didout = FALSE; /* don't scroll screen up for normal command */
1047 msg_col = 0;
1048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 old_pos = curwin->w_cursor; /* remember where cursor was */
1051
1052 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1053 * mode. */
1054 if (!VIsual_active && km_startsel)
1055 {
1056 if (nv_cmds[idx].cmd_flags & NV_SS)
1057 {
1058 start_selection();
1059 unshift_special(&ca);
1060 idx = find_command(ca.cmdchar);
1061 }
1062 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1063 && (mod_mask & MOD_MASK_SHIFT))
1064 {
1065 start_selection();
1066 mod_mask &= ~MOD_MASK_SHIFT;
1067 }
1068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
1070 /*
1071 * Execute the command!
1072 * Call the command function found in the commands table.
1073 */
1074 ca.arg = nv_cmds[idx].cmd_arg;
1075 (nv_cmds[idx].cmd_func)(&ca);
1076
1077 /*
1078 * If we didn't start or finish an operator, reset oap->regname, unless we
1079 * need it later.
1080 */
1081 if (!finish_op
1082 && !oap->op_type
1083 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1084 {
1085 clearop(oap);
1086#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001087 {
1088 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001089
Bram Moolenaar536681b2011-05-10 16:12:45 +02001090 /* Adjust the register according to 'clipboard', so that when
1091 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001092# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001093 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001094# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001095 set_reg_var(regname);
1096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097#endif
1098 }
1099
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001100 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001101 * character or "z333<cr>". */
1102 if (old_mapped_len > 0)
1103 old_mapped_len = typebuf_maplen();
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 /*
1106 * If an operation is pending, handle it...
1107 */
1108 do_pending_operator(&ca, old_col, FALSE);
1109
1110 /*
1111 * Wait for a moment when a message is displayed that will be overwritten
1112 * by the mode message.
1113 * In Visual mode and with "^O" in Insert mode, a short message will be
1114 * overwritten by the mode message. Wait a bit, until a key is hit.
1115 * In Visual mode, it's more important to keep the Visual area updated
1116 * than keeping a message (e.g. from a /pat search).
1117 * Only do this if the command was typed, not from a mapping.
1118 * Don't wait when emsg_silent is non-zero.
1119 * Also wait a bit after an error message, e.g. for "^O:".
1120 * Don't redraw the screen, it would remove the message.
1121 */
1122 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001123 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 || (VIsual_active
1126 && old_pos.lnum == curwin->w_cursor.lnum
1127 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 )
1129 && (clear_cmdline
1130 || redraw_cmdline)
1131 && (msg_didout || (msg_didany && msg_scroll))
1132 && !msg_nowait
1133 && KeyTyped)
1134 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 && (msg_scroll
1137 || emsg_on_display)))
1138 && oap->regname == 0
1139 && !(ca.retval & CA_COMMAND_BUSY)
1140 && stuff_empty()
1141 && typebuf_typed()
1142 && emsg_silent == 0
1143 && !did_wait_return
1144 && oap->op_type == OP_NOP)
1145 {
1146 int save_State = State;
1147
1148 /* Draw the cursor with the right shape here */
1149 if (restart_edit != 0)
1150 State = INSERT;
1151
1152 /* If need to redraw, and there is a "keep_msg", redraw before the
1153 * delay */
1154 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1155 {
1156 char_u *kmsg;
1157
1158 kmsg = keep_msg;
1159 keep_msg = NULL;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001160 // showmode() will clear keep_msg, but we want to use it anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001162 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001164
1165 kmsg = vim_strsave(keep_msg);
1166 if (kmsg != NULL)
1167 {
1168 msg_attr((char *)kmsg, keep_msg_attr);
1169 vim_free(kmsg);
1170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 }
1172 setcursor();
1173 cursor_on();
1174 out_flush();
1175 if (msg_scroll || emsg_on_display)
1176 ui_delay(1000L, TRUE); /* wait at least one second */
1177 ui_delay(3000L, FALSE); /* wait up to three seconds */
1178 State = save_State;
1179
1180 msg_scroll = FALSE;
1181 emsg_on_display = FALSE;
1182 }
1183
1184 /*
1185 * Finish up after executing a Normal mode command.
1186 */
1187normal_end:
1188
1189 msg_nowait = FALSE;
1190
1191 /* Reset finish_op, in case it was set */
1192#ifdef CURSOR_SHAPE
1193 c = finish_op;
1194#endif
1195 finish_op = FALSE;
1196#ifdef CURSOR_SHAPE
1197 /* Redraw the cursor with another shape, if we were in Operator-pending
1198 * mode or did a replace command. */
1199 if (c || ca.cmdchar == 'r')
1200 {
1201 ui_cursor_shape(); /* may show different cursor shape */
1202# ifdef FEAT_MOUSESHAPE
1203 update_mouseshape(-1);
1204# endif
1205 }
1206#endif
1207
1208#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001209 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001210 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 clear_showcmd();
1212#endif
1213
1214 checkpcmark(); /* check if we moved since setting pcmark */
1215 vim_free(ca.searchbuf);
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (has_mbyte)
1218 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if (curwin->w_p_scb && toplevel)
1221 {
1222 validate_cursor(); /* may need to update w_leftcol */
1223 do_check_scrollbind(TRUE);
1224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar860cae12010-06-05 23:22:07 +02001226 if (curwin->w_p_crb && toplevel)
1227 {
1228 validate_cursor(); /* may need to update w_leftcol */
1229 do_check_cursorbind();
1230 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001231
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001232#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001233 /* don't go to Insert mode if a terminal has a running job */
1234 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001235 restart_edit = 0;
1236#endif
1237
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 /*
1239 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1240 * if still inside a mapping that started in Visual mode).
1241 * May switch from Visual to Select mode after CTRL-O command.
1242 */
1243 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1245 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 && !(ca.retval & CA_COMMAND_BUSY)
1247 && stuff_empty()
1248 && oap->regname == 0)
1249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 if (restart_VIsual_select == 1)
1251 {
1252 VIsual_select = TRUE;
1253 showmode();
1254 restart_VIsual_select = 0;
1255 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001256 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 (void)edit(restart_edit, FALSE, 1L);
1258 }
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (restart_VIsual_select == 2)
1261 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
1263 /* Save count before an operator for next time. */
1264 opcount = ca.opcount;
1265}
1266
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001267#ifdef FEAT_EVAL
1268/*
1269 * Set v:count and v:count1 according to "cap".
1270 * Set v:prevcount only when "set_prevcount" is TRUE.
1271 */
1272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001273set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001274{
1275 long count = cap->count0;
1276
1277 /* multiply with cap->opcount the same way as above */
1278 if (cap->opcount != 0)
1279 count = cap->opcount * (count == 0 ? 1 : count);
1280 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1281 *set_prevcount = FALSE; /* only set v:prevcount once */
1282}
1283#endif
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 * Check if highlighting for visual mode is possible, give a warning message
1287 * if not.
1288 */
1289 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001290check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 static int did_check = FALSE;
1293
1294 if (full_screen)
1295 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001296 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001297 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 did_check = TRUE;
1299 }
1300}
1301
1302/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001303 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 * This function should ALWAYS be called to end Visual mode, except from
1305 * do_pending_operator().
1306 */
1307 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001308end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
1310#ifdef FEAT_CLIPBOARD
1311 /*
1312 * If we are using the clipboard, then remember what was selected in case
1313 * we need to paste it somewhere while we still own the selection.
1314 * Only do this when the clipboard is already owned. Don't want to grab
1315 * the selection when hitting ESC.
1316 */
1317 if (clip_star.available && clip_star.owned)
1318 clip_auto_select();
1319#endif
1320
1321 VIsual_active = FALSE;
1322#ifdef FEAT_MOUSE
1323 setmouse();
1324 mouse_dragging = 0;
1325#endif
1326
1327 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001328 curbuf->b_visual.vi_mode = VIsual_mode;
1329 curbuf->b_visual.vi_start = VIsual;
1330 curbuf->b_visual.vi_end = curwin->w_cursor;
1331 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332#ifdef FEAT_EVAL
1333 curbuf->b_visual_mode_eval = VIsual_mode;
1334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (!virtual_active())
1336 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001337 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001339 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340}
1341
1342/*
1343 * Reset VIsual_active and VIsual_reselect.
1344 */
1345 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001346reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
1348 if (VIsual_active)
1349 {
1350 end_visual_mode();
1351 redraw_curbuf_later(INVERTED); /* delete the inversion later */
1352 }
1353 VIsual_reselect = FALSE;
1354}
1355
1356/*
1357 * Reset VIsual_active and VIsual_reselect if it's set.
1358 */
1359 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001360reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 if (VIsual_active)
1363 {
1364 end_visual_mode();
1365 redraw_curbuf_later(INVERTED); /* delete the inversion later */
1366 VIsual_reselect = FALSE;
1367 }
1368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001370 void
1371restore_visual_mode(void)
1372{
1373 if (VIsual_mode_orig != NUL)
1374 {
1375 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1376 VIsual_mode_orig = NUL;
1377 }
1378}
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380/*
1381 * Check for a balloon-eval special item to include when searching for an
1382 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1383 * Returns TRUE if the character at "*ptr" should be included.
1384 * "dir" is FORWARD or BACKWARD, the direction of searching.
1385 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1386 * "bnp" points to a counter for square brackets.
1387 */
1388 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001389find_is_eval_item(
1390 char_u *ptr,
1391 int *colp,
1392 int *bnp,
1393 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395 /* Accept everything inside []. */
1396 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1397 ++*bnp;
1398 if (*bnp > 0)
1399 {
1400 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1401 --*bnp;
1402 return TRUE;
1403 }
1404
1405 /* skip over "s.var" */
1406 if (*ptr == '.')
1407 return TRUE;
1408
1409 /* two-character item: s->var */
1410 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1411 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1412 {
1413 *colp += dir;
1414 return TRUE;
1415 }
1416 return FALSE;
1417}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
1419/*
1420 * Find the identifier under or to the right of the cursor.
1421 * "find_type" can have one of three values:
1422 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001423 * FIND_STRING: find any non-white text
1424 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001425 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 *
1427 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001428 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001430 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 * This doesn't match the real Vi but I like it a little better and it
1432 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001433 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 * When FIND_IDENT isn't defined, we backup until a blank.
1435 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001436 * Returns the length of the text, or zero if no text is found.
1437 * If text is found, a pointer to the text is put in "*text". This
1438 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 */
1440 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001441find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
1443 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001444 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445}
1446
1447/*
1448 * Like find_ident_under_cursor(), but for any window and any position.
1449 * However: Uses 'iskeyword' from the current window!.
1450 */
1451 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001452find_ident_at_pos(
1453 win_T *wp,
1454 linenr_T lnum,
1455 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001456 char_u **text,
1457 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001458 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001461 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 int this_class = 0;
1464 int prev_class;
1465 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001466 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
1468 /*
1469 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001470 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 */
1472 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1473 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1474 {
1475 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001476 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 */
1478 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (has_mbyte)
1480 {
1481 while (ptr[col] != NUL)
1482 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001483 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1485 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 this_class = mb_get_class(ptr + col);
1487 if (this_class != 0 && (i == 1 || this_class != 1))
1488 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001489 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491 }
1492 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001494 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 )
1497 ++col;
1498
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001499 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
1502 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001503 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (has_mbyte)
1506 {
1507 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1509 this_class = mb_get_class((char_u *)"a");
1510 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001512 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1515 prev_class = mb_get_class(ptr + prevcol);
1516 if (this_class != prev_class
1517 && (i == 0
1518 || prev_class == 0
1519 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 && (!(find_type & FIND_EVAL)
1521 || prevcol == 0
1522 || !find_is_eval_item(ptr + prevcol, &prevcol,
1523 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 )
1525 break;
1526 col = prevcol;
1527 }
1528
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001529 // If we don't want just any old text, or we've found an
1530 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if (this_class > 2)
1532 this_class = 2;
1533 if (!(find_type & FIND_STRING) || this_class == 2)
1534 break;
1535 }
1536 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
1538 while (col > 0
1539 && ((i == 0
1540 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001541 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 && (!(find_type & FIND_IDENT)
1543 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 || ((find_type & FIND_EVAL)
1545 && col > 1
1546 && find_is_eval_item(ptr + col - 1, &col,
1547 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 ))
1549 --col;
1550
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001551 // If we don't want just any old text, or we've found an
1552 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1554 break;
1555 }
1556 }
1557
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001558 if (ptr[col] == NUL || (i == 0
1559 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001561 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001562 if ((find_type & FIND_NOERROR) == 0)
1563 {
1564 if (find_type & FIND_STRING)
1565 emsg(_("E348: No string under cursor"));
1566 else
1567 emsg(_(e_noident));
1568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return 0;
1570 }
1571 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001572 *text = ptr;
1573 if (textcol != NULL)
1574 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001577 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 bn = 0;
1580 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 if (has_mbyte)
1583 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001584 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 this_class = mb_get_class(ptr);
1586 while (ptr[col] != NUL
1587 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1588 : mb_get_class(ptr + col) != 0)
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 Moolenaar0fa313a2005-08-10 21:07:57 +00001593 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001597 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 || ((find_type & FIND_EVAL)
1599 && col <= (int)startcol
1600 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604 return col;
1605}
1606
1607/*
1608 * Prepare for redo of a normal command.
1609 */
1610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001611prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612{
1613 prep_redo(cap->oap->regname, cap->count0,
1614 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1615}
1616
1617/*
1618 * Prepare for redo of any command.
1619 * Note that only the last argument can be a multi-byte char.
1620 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001622prep_redo(
1623 int regname,
1624 long num,
1625 int cmd1,
1626 int cmd2,
1627 int cmd3,
1628 int cmd4,
1629 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
1631 ResetRedobuff();
1632 if (regname != 0) /* yank from specified buffer */
1633 {
1634 AppendCharToRedobuff('"');
1635 AppendCharToRedobuff(regname);
1636 }
1637 if (num)
1638 AppendNumberToRedobuff(num);
1639
1640 if (cmd1 != NUL)
1641 AppendCharToRedobuff(cmd1);
1642 if (cmd2 != NUL)
1643 AppendCharToRedobuff(cmd2);
1644 if (cmd3 != NUL)
1645 AppendCharToRedobuff(cmd3);
1646 if (cmd4 != NUL)
1647 AppendCharToRedobuff(cmd4);
1648 if (cmd5 != NUL)
1649 AppendCharToRedobuff(cmd5);
1650}
1651
1652/*
1653 * check for operator active and clear it
1654 *
1655 * return TRUE if operator was active
1656 */
1657 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001658checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 if (oap->op_type == OP_NOP)
1661 return FALSE;
1662 clearopbeep(oap);
1663 return TRUE;
1664}
1665
1666/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001667 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001669 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 */
1671 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001672checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001674 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 return FALSE;
1676 clearopbeep(oap);
1677 return TRUE;
1678}
1679
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001680 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001681clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 oap->op_type = OP_NOP;
1684 oap->regname = 0;
1685 oap->motion_force = NUL;
1686 oap->use_reg_one = FALSE;
1687}
1688
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001690clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 clearop(oap);
1693 beep_flush();
1694}
1695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * Remove the shift modifier from a special key.
1698 */
1699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001700unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 switch (cap->cmdchar)
1703 {
1704 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1705 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1706 case K_S_UP: cap->cmdchar = K_UP; break;
1707 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1708 case K_S_HOME: cap->cmdchar = K_HOME; break;
1709 case K_S_END: cap->cmdchar = K_END; break;
1710 }
1711 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001714/*
1715 * If the mode is currently displayed clear the command line or update the
1716 * command displayed.
1717 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001718 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001719may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001720{
1721 if (mode_displayed)
1722 clear_cmdline = TRUE; /* unshow visual mode later */
1723#ifdef FEAT_CMDL_INFO
1724 else
1725 clear_showcmd();
1726#endif
1727}
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1730/*
1731 * Routines for displaying a partly typed command
1732 */
1733
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001734#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735static char_u showcmd_buf[SHOWCMD_BUFLEN];
1736static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
1737static int showcmd_is_clear = TRUE;
1738static int showcmd_visual = FALSE;
1739
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001740static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001743clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744{
1745 if (!p_sc)
1746 return;
1747
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (VIsual_active && !char_avail())
1749 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001750 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 long lines;
1752 colnr_T leftcol, rightcol;
1753 linenr_T top, bot;
1754
1755 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00001756 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
1758 top = VIsual.lnum;
1759 bot = curwin->w_cursor.lnum;
1760 }
1761 else
1762 {
1763 top = curwin->w_cursor.lnum;
1764 bot = VIsual.lnum;
1765 }
1766# ifdef FEAT_FOLDING
1767 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02001768 (void)hasFolding(top, &top, NULL);
1769 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770# endif
1771 lines = bot - top + 1;
1772
1773 if (VIsual_mode == Ctrl_V)
1774 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001775# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001776 char_u *saved_sbr = p_sbr;
1777
1778 /* Make 'sbr' empty for a moment to get the correct size. */
1779 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001782# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001783 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1786 (long)(rightcol - leftcol + 1));
1787 }
1788 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1789 sprintf((char *)showcmd_buf, "%ld", lines);
1790 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001791 {
1792 char_u *s, *e;
1793 int l;
1794 int bytes = 0;
1795 int chars = 0;
1796
1797 if (cursor_bot)
1798 {
1799 s = ml_get_pos(&VIsual);
1800 e = ml_get_cursor();
1801 }
1802 else
1803 {
1804 s = ml_get_cursor();
1805 e = ml_get_pos(&VIsual);
1806 }
1807 while ((*p_sel != 'e') ? s <= e : s < e)
1808 {
1809 l = (*mb_ptr2len)(s);
1810 if (l == 0)
1811 {
1812 ++bytes;
1813 ++chars;
1814 break; /* end of line */
1815 }
1816 bytes += l;
1817 ++chars;
1818 s += l;
1819 }
1820 if (bytes == chars)
1821 sprintf((char *)showcmd_buf, "%d", chars);
1822 else
1823 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
1826 showcmd_visual = TRUE;
1827 }
1828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
1830 showcmd_buf[0] = NUL;
1831 showcmd_visual = FALSE;
1832
1833 /* Don't actually display something if there is nothing to clear. */
1834 if (showcmd_is_clear)
1835 return;
1836 }
1837
1838 display_showcmd();
1839}
1840
1841/*
1842 * Add 'c' to string of shown command chars.
1843 * Return TRUE if output has been written (and setcursor() has been called).
1844 */
1845 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001846add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
1848 char_u *p;
1849 int old_len;
1850 int extra_len;
1851 int overflow;
1852#if defined(FEAT_MOUSE)
1853 int i;
1854 static int ignore[] =
1855 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00001856# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1858 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00001859# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001860 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001861 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1863 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001864 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001866 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 0
1868 };
1869#endif
1870
Bram Moolenaar09df3122006-01-23 22:23:09 +00001871 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 return FALSE;
1873
1874 if (showcmd_visual)
1875 {
1876 showcmd_buf[0] = NUL;
1877 showcmd_visual = FALSE;
1878 }
1879
1880#if defined(FEAT_MOUSE)
1881 /* Ignore keys that are scrollbar updates and mouse clicks */
1882 if (IS_SPECIAL(c))
1883 for (i = 0; ignore[i] != 0; ++i)
1884 if (ignore[i] == c)
1885 return FALSE;
1886#endif
1887
1888 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001889 if (*p == ' ')
1890 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 old_len = (int)STRLEN(showcmd_buf);
1892 extra_len = (int)STRLEN(p);
1893 overflow = old_len + extra_len - SHOWCMD_COLS;
1894 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001895 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1896 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 STRCAT(showcmd_buf, p);
1898
1899 if (char_avail())
1900 return FALSE;
1901
1902 display_showcmd();
1903
1904 return TRUE;
1905}
1906
1907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001908add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909{
1910 if (!add_to_showcmd(c))
1911 setcursor();
1912}
1913
1914/*
1915 * Delete 'len' characters from the end of the shown command.
1916 */
1917 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001918del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 int old_len;
1921
1922 if (!p_sc)
1923 return;
1924
1925 old_len = (int)STRLEN(showcmd_buf);
1926 if (len > old_len)
1927 len = old_len;
1928 showcmd_buf[old_len - len] = NUL;
1929
1930 if (!char_avail())
1931 display_showcmd();
1932}
1933
1934/*
1935 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1936 * something and there is a partial mapping.
1937 */
1938 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001939push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940{
1941 if (p_sc)
1942 STRCPY(old_showcmd_buf, showcmd_buf);
1943}
1944
1945 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001946pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947{
1948 if (!p_sc)
1949 return;
1950
1951 STRCPY(showcmd_buf, old_showcmd_buf);
1952
1953 display_showcmd();
1954}
1955
1956 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001957display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 int len;
1960
1961 cursor_off();
1962
1963 len = (int)STRLEN(showcmd_buf);
1964 if (len == 0)
1965 showcmd_is_clear = TRUE;
1966 else
1967 {
1968 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1969 showcmd_is_clear = FALSE;
1970 }
1971
1972 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001973 * clear the rest of an old message by outputting up to SHOWCMD_COLS
1974 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 */
1976 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1977
1978 setcursor(); /* put cursor back where it belongs */
1979}
1980#endif
1981
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982/*
1983 * When "check" is FALSE, prepare for commands that scroll the window.
1984 * When "check" is TRUE, take care of scroll-binding after the window has
1985 * scrolled. Called from normal_cmd() and edit().
1986 */
1987 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001988do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 static win_T *old_curwin = NULL;
1991 static linenr_T old_topline = 0;
1992#ifdef FEAT_DIFF
1993 static int old_topfill = 0;
1994#endif
1995 static buf_T *old_buf = NULL;
1996 static colnr_T old_leftcol = 0;
1997
1998 if (check && curwin->w_p_scb)
1999 {
2000 /* If a ":syncbind" command was just used, don't scroll, only reset
2001 * the values. */
2002 if (did_syncbind)
2003 did_syncbind = FALSE;
2004 else if (curwin == old_curwin)
2005 {
2006 /*
2007 * Synchronize other windows, as necessary according to
2008 * 'scrollbind'. Don't do this after an ":edit" command, except
2009 * when 'diff' is set.
2010 */
2011 if ((curwin->w_buffer == old_buf
2012#ifdef FEAT_DIFF
2013 || curwin->w_p_diff
2014#endif
2015 )
2016 && (curwin->w_topline != old_topline
2017#ifdef FEAT_DIFF
2018 || curwin->w_topfill != old_topfill
2019#endif
2020 || curwin->w_leftcol != old_leftcol))
2021 {
2022 check_scrollbind(curwin->w_topline - old_topline,
2023 (long)(curwin->w_leftcol - old_leftcol));
2024 }
2025 }
2026 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
2027 {
2028 /*
2029 * When switching between windows, make sure that the relative
2030 * vertical offset is valid for the new window. The relative
2031 * offset is invalid whenever another 'scrollbind' window has
2032 * scrolled to a point that would force the current window to
2033 * scroll past the beginning or end of its buffer. When the
2034 * resync is performed, some of the other 'scrollbind' windows may
2035 * need to jump so that the current window's relative position is
2036 * visible on-screen.
2037 */
2038 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2039 }
2040 curwin->w_scbind_pos = curwin->w_topline;
2041 }
2042
2043 old_curwin = curwin;
2044 old_topline = curwin->w_topline;
2045#ifdef FEAT_DIFF
2046 old_topfill = curwin->w_topfill;
2047#endif
2048 old_buf = curwin->w_buffer;
2049 old_leftcol = curwin->w_leftcol;
2050}
2051
2052/*
2053 * Synchronize any windows that have "scrollbind" set, based on the
2054 * number of rows by which the current window has changed
2055 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2056 */
2057 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002058check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059{
2060 int want_ver;
2061 int want_hor;
2062 win_T *old_curwin = curwin;
2063 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 int old_VIsual_select = VIsual_select;
2065 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 colnr_T tgt_leftcol = curwin->w_leftcol;
2067 long topline;
2068 long y;
2069
2070 /*
2071 * check 'scrollopt' string for vertical and horizontal scroll options
2072 */
2073 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2074#ifdef FEAT_DIFF
2075 want_ver |= old_curwin->w_p_diff;
2076#endif
2077 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2078
2079 /*
2080 * loop through the scrollbound windows and scroll accordingly
2081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002083 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 curbuf = curwin->w_buffer;
2086 /* skip original window and windows with 'noscrollbind' */
2087 if (curwin != old_curwin && curwin->w_p_scb)
2088 {
2089 /*
2090 * do the vertical scroll
2091 */
2092 if (want_ver)
2093 {
2094#ifdef FEAT_DIFF
2095 if (old_curwin->w_p_diff && curwin->w_p_diff)
2096 {
2097 diff_set_topline(old_curwin, curwin);
2098 }
2099 else
2100#endif
2101 {
2102 curwin->w_scbind_pos += topline_diff;
2103 topline = curwin->w_scbind_pos;
2104 if (topline > curbuf->b_ml.ml_line_count)
2105 topline = curbuf->b_ml.ml_line_count;
2106 if (topline < 1)
2107 topline = 1;
2108
2109 y = topline - curwin->w_topline;
2110 if (y > 0)
2111 scrollup(y, FALSE);
2112 else
2113 scrolldown(-y, FALSE);
2114 }
2115
2116 redraw_later(VALID);
2117 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
2120
2121 /*
2122 * do the horizontal scroll
2123 */
2124 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2125 {
2126 curwin->w_leftcol = tgt_leftcol;
2127 leftcol_changed();
2128 }
2129 }
2130 }
2131
2132 /*
2133 * reset current-window
2134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 VIsual_select = old_VIsual_select;
2136 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 curwin = old_curwin;
2138 curbuf = old_curbuf;
2139}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
2141/*
2142 * Command character that's ignored.
2143 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002144 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
Bram Moolenaarfc735152005-03-22 22:54:12 +00002149 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150}
2151
2152/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002153 * Command character that doesn't do anything, but unlike nv_ignore() does
2154 * start edit(). Used for "startinsert" executed while starting up.
2155 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002156 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002157nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002158{
2159}
2160
2161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 * Command character doesn't exist.
2163 */
2164 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002165nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 clearopbeep(cap->oap);
2168}
2169
2170/*
2171 * <Help> and <F1> commands.
2172 */
2173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002174nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 if (!checkclearopq(cap->oap))
2177 ex_help(NULL);
2178}
2179
2180/*
2181 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2182 */
2183 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002184nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002186#ifdef FEAT_JOB_CHANNEL
2187 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2188 clearopbeep(cap->oap);
2189 else
2190#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002191 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002192 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002193 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002194 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2195 op_addsub(cap->oap, cap->count1, cap->arg);
2196 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002197 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002198 else if (VIsual_active)
2199 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002200 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002201 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202}
2203
2204/*
2205 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2206 */
2207 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002208nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209{
2210 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002211 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002212 if (mod_mask & MOD_MASK_CTRL)
2213 {
2214 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
2215 if (cap->arg == BACKWARD)
2216 goto_tabpage(-(int)cap->count1);
2217 else
2218 goto_tabpage((int)cap->count0);
2219 }
2220 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002221 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223}
2224
2225/*
2226 * Implementation of "gd" and "gD" command.
2227 */
2228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002229nv_gd(
2230 oparg_T *oap,
2231 int nchar,
2232 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 char_u *ptr;
2236
Bram Moolenaard9d30582005-05-18 22:10:28 +00002237 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002238 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2239 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002241#ifdef FEAT_FOLDING
2242 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2243 foldOpenCursor();
2244#endif
2245}
2246
2247/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002248 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2249 * otherwise.
2250 */
2251 static int
2252is_ident(char_u *line, int offset)
2253{
2254 int i;
2255 int incomment = FALSE;
2256 int instring = 0;
2257 int prev = 0;
2258
2259 for (i = 0; i < offset && line[i] != NUL; i++)
2260 {
2261 if (instring != 0)
2262 {
2263 if (prev != '\\' && line[i] == instring)
2264 instring = 0;
2265 }
2266 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2267 {
2268 instring = line[i];
2269 }
2270 else
2271 {
2272 if (incomment)
2273 {
2274 if (prev == '*' && line[i] == '/')
2275 incomment = FALSE;
2276 }
2277 else if (prev == '/' && line[i] == '*')
2278 {
2279 incomment = TRUE;
2280 }
2281 else if (prev == '/' && line[i] == '/')
2282 {
2283 return FALSE;
2284 }
2285 }
2286
2287 prev = line[i];
2288 }
2289
2290 return incomment == FALSE && instring == 0;
2291}
2292
2293/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002294 * Search for variable declaration of "ptr[len]".
2295 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2296 * current file ("gD").
2297 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002298 * Return FAIL when not found.
2299 */
2300 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002301find_decl(
2302 char_u *ptr,
2303 int len,
2304 int locally,
2305 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002306 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002307{
2308 char_u *pat;
2309 pos_T old_pos;
2310 pos_T par_pos;
2311 pos_T found_pos;
2312 int t;
2313 int save_p_ws;
2314 int save_p_scs;
2315 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002316 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002317 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002318 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002319
2320 if ((pat = alloc(len + 7)) == NULL)
2321 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002322
2323 /* Put "\V" before the pattern to avoid that the special meaning of "."
2324 * and "~" causes trouble. */
2325 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2326 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 old_pos = curwin->w_cursor;
2328 save_p_ws = p_ws;
2329 save_p_scs = p_scs;
2330 p_ws = FALSE; /* don't wrap around end of file now */
2331 p_scs = FALSE; /* don't switch ignorecase off now */
2332
2333 /*
2334 * With "gD" go to line 1.
2335 * With "gd" Search back for the start of the current function, then go
2336 * back until a blank line. If this fails go to line 1.
2337 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002338 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
2340 setpcmark(); /* Set in findpar() otherwise */
2341 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002342 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
2344 else
2345 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002346 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2348 --curwin->w_cursor.lnum;
2349 }
2350 curwin->w_cursor.col = 0;
2351
2352 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002353 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002354 for (;;)
2355 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002356 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002357 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002358 if (curwin->w_cursor.lnum >= old_pos.lnum)
2359 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002360
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002361 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002362 {
2363 pos_T *pos;
2364
2365 /* Check that the block the match is in doesn't end before the
2366 * position where we started the search from. */
2367 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2368 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2369 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002370 {
2371 /* There can't be a useful match before the end of this block.
2372 * Skip to the end. */
2373 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002374 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002375 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002376 }
2377
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002378 if (t == FAIL)
2379 {
2380 /* If we previously found a valid position, use it. */
2381 if (found_pos.lnum != 0)
2382 {
2383 curwin->w_cursor = found_pos;
2384 t = OK;
2385 }
2386 break;
2387 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002388 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002389 {
2390 /* Ignore this line, continue at start of next line. */
2391 ++curwin->w_cursor.lnum;
2392 curwin->w_cursor.col = 0;
2393 continue;
2394 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002395 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2396
2397 /* If the current position is not a valid identifier and a previous
2398 * match is present, favor that one instead. */
2399 if (!valid && found_pos.lnum != 0)
2400 {
2401 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002402 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002403 }
2404
2405 /* Global search: use first valid match found */
2406 if (valid && !locally)
2407 break;
2408 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002409 {
2410 /* If we previously found a valid position, use it. */
2411 if (found_pos.lnum != 0)
2412 curwin->w_cursor = found_pos;
2413 break;
2414 }
2415
Bram Moolenaar226630a2016-10-08 19:21:31 +02002416 /* For finding a local variable and the match is before the "{" or
2417 * inside a comment, continue searching. For K&R style function
2418 * declarations this skips the function header without types. */
2419 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002420 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002421 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002422 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002423 /* Remove SEARCH_START from flags to avoid getting stuck at one
2424 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002425 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002427
2428 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002430 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 curwin->w_cursor = old_pos;
2432 }
2433 else
2434 {
2435 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 /* "n" searches forward now */
2437 reset_search_dir();
2438 }
2439
2440 vim_free(pat);
2441 p_ws = save_p_ws;
2442 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002443
2444 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445}
2446
2447/*
2448 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2449 * lines rather than lines in the file.
2450 * 'dist' must be positive.
2451 *
2452 * Return OK if able to move cursor, FAIL otherwise.
2453 */
2454 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002455nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 int linelen = linetabsize(ml_get_curline());
2458 int retval = OK;
2459 int atend = FALSE;
2460 int n;
2461 int col_off1; /* margin offset for first screen line */
2462 int col_off2; /* margin offset for wrapped screen line */
2463 int width1; /* text width for first screen line */
2464 int width2; /* test width for wrapped screen line */
2465
2466 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002467 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
2469 col_off1 = curwin_col_off();
2470 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002471 width1 = curwin->w_width - col_off1;
2472 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002473 if (width2 == 0)
2474 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 /*
2479 * Instead of sticking at the last character of the buffer line we
2480 * try to stick in the last column of the screen.
2481 */
2482 if (curwin->w_curswant == MAXCOL)
2483 {
2484 atend = TRUE;
2485 validate_virtcol();
2486 if (width1 <= 0)
2487 curwin->w_curswant = 0;
2488 else
2489 {
2490 curwin->w_curswant = width1 - 1;
2491 if (curwin->w_virtcol > curwin->w_curswant)
2492 curwin->w_curswant += ((curwin->w_virtcol
2493 - curwin->w_curswant - 1) / width2 + 1) * width2;
2494 }
2495 }
2496 else
2497 {
2498 if (linelen > width1)
2499 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2500 else
2501 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002502 if (curwin->w_curswant >= (colnr_T)n)
2503 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 }
2505
2506 while (dist--)
2507 {
2508 if (dir == BACKWARD)
2509 {
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002510 if ((long)curwin->w_curswant >= width1)
2511 // Move back within the line. This can give a negative value
2512 // for w_curswant if width1 < width2 (with cpoptions+=n),
2513 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 curwin->w_curswant -= width2;
2515 else
2516 {
2517 /* to previous line */
2518 if (curwin->w_cursor.lnum == 1)
2519 {
2520 retval = FAIL;
2521 break;
2522 }
2523 --curwin->w_cursor.lnum;
2524#ifdef FEAT_FOLDING
2525 /* Move to the start of a closed fold. Don't do that when
2526 * 'foldopen' contains "all": it will open in a moment. */
2527 if (!(fdo_flags & FDO_ALL))
2528 (void)hasFolding(curwin->w_cursor.lnum,
2529 &curwin->w_cursor.lnum, NULL);
2530#endif
2531 linelen = linetabsize(ml_get_curline());
2532 if (linelen > width1)
2533 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2534 + 1) * width2;
2535 }
2536 }
2537 else /* dir == FORWARD */
2538 {
2539 if (linelen > width1)
2540 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2541 else
2542 n = width1;
2543 if (curwin->w_curswant + width2 < (colnr_T)n)
2544 /* move forward within line */
2545 curwin->w_curswant += width2;
2546 else
2547 {
2548 /* to next line */
2549#ifdef FEAT_FOLDING
2550 /* Move to the end of a closed fold. */
2551 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2552 &curwin->w_cursor.lnum);
2553#endif
2554 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2555 {
2556 retval = FAIL;
2557 break;
2558 }
2559 curwin->w_cursor.lnum++;
2560 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002561 // Check if the cursor has moved below the number display
2562 // when width1 < width2 (with cpoptions+=n). Subtract width2
2563 // to get a negative value for w_curswant, which will get
2564 // clipped to column 0.
2565 if (curwin->w_curswant >= width1)
2566 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002567 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 }
2570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002573 if (virtual_active() && atend)
2574 coladvance(MAXCOL);
2575 else
2576 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2579 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002580 colnr_T virtcol;
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 /*
2583 * Check for landing on a character that got split at the end of the
2584 * last line. We want to advance a screenline, not end up in the same
2585 * screenline or move two screenlines.
2586 */
2587 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002588 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002589#if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02002590 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
2591 virtcol -= vim_strsize(p_sbr);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002592#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002593
2594 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 && (curwin->w_curswant < (colnr_T)width1
2596 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2597 : ((curwin->w_curswant - width1) % width2
2598 > (colnr_T)width2 / 2)))
2599 --curwin->w_cursor.col;
2600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
2602 if (atend)
2603 curwin->w_curswant = MAXCOL; /* stick in the last column */
2604
2605 return retval;
2606}
2607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608/*
2609 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2610 * cap->arg must be TRUE for CTRL-E.
2611 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002613nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 if (!checkclearop(cap->oap))
2616 scroll_redraw(cap->arg, cap->count1);
2617}
2618
2619/*
2620 * Scroll "count" lines up or down, and redraw.
2621 */
2622 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002623scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 linenr_T prev_topline = curwin->w_topline;
2626#ifdef FEAT_DIFF
2627 int prev_topfill = curwin->w_topfill;
2628#endif
2629 linenr_T prev_lnum = curwin->w_cursor.lnum;
2630
2631 if (up)
2632 scrollup(count, TRUE);
2633 else
2634 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002635 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
2637 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
2638 * valid, otherwise the screen jumps back at the end of the file. */
2639 cursor_correct();
2640 check_cursor_moved(curwin);
2641 curwin->w_valid |= VALID_TOPLINE;
2642
2643 /* If moved back to where we were, at least move the cursor, otherwise
2644 * we get stuck at one position. Don't move the cursor up if the
2645 * first line of the buffer is already on the screen */
2646 while (curwin->w_topline == prev_topline
2647#ifdef FEAT_DIFF
2648 && curwin->w_topfill == prev_topfill
2649#endif
2650 )
2651 {
2652 if (up)
2653 {
2654 if (curwin->w_cursor.lnum > prev_lnum
2655 || cursor_down(1L, FALSE) == FAIL)
2656 break;
2657 }
2658 else
2659 {
2660 if (curwin->w_cursor.lnum < prev_lnum
2661 || prev_topline == 1L
2662 || cursor_up(1L, FALSE) == FAIL)
2663 break;
2664 }
2665 /* Mark w_topline as valid, otherwise the screen jumps back at the
2666 * end of the file. */
2667 check_cursor_moved(curwin);
2668 curwin->w_valid |= VALID_TOPLINE;
2669 }
2670 }
2671 if (curwin->w_cursor.lnum != prev_lnum)
2672 coladvance(curwin->w_curswant);
2673 redraw_later(VALID);
2674}
2675
2676/*
2677 * Commands that start with "z".
2678 */
2679 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002680nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 long n;
2683 colnr_T col;
2684 int nchar = cap->nchar;
2685#ifdef FEAT_FOLDING
2686 long old_fdl = curwin->w_p_fdl;
2687 int old_fen = curwin->w_p_fen;
2688#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002689#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002690 int undo = FALSE;
2691#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002692 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 if (VIM_ISDIGIT(nchar))
2695 {
2696 /*
2697 * "z123{nchar}": edit the count before obtaining {nchar}
2698 */
2699 if (checkclearop(cap->oap))
2700 return;
2701 n = nchar - '0';
2702 for (;;)
2703 {
2704#ifdef USE_ON_FLY_SCROLL
2705 dont_scroll = TRUE; /* disallow scrolling here */
2706#endif
2707 ++no_mapping;
2708 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002709 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 --no_mapping;
2712 --allow_keys;
2713#ifdef FEAT_CMDL_INFO
2714 (void)add_to_showcmd(nchar);
2715#endif
2716 if (nchar == K_DEL || nchar == K_KDEL)
2717 n /= 10;
2718 else if (VIM_ISDIGIT(nchar))
2719 n = n * 10 + (nchar - '0');
2720 else if (nchar == CAR)
2721 {
2722#ifdef FEAT_GUI
2723 need_mouse_correct = TRUE;
2724#endif
2725 win_setheight((int)n);
2726 break;
2727 }
2728 else if (nchar == 'l'
2729 || nchar == 'h'
2730 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002731 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
2733 cap->count1 = n ? n * cap->count1 : cap->count1;
2734 goto dozet;
2735 }
2736 else
2737 {
2738 clearopbeep(cap->oap);
2739 break;
2740 }
2741 }
2742 cap->oap->op_type = OP_NOP;
2743 return;
2744 }
2745
2746dozet:
2747 if (
2748#ifdef FEAT_FOLDING
2749 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2750 * and "zC" only in Visual mode. "zj" and "zk" are motion
2751 * commands. */
2752 cap->nchar != 'f' && cap->nchar != 'F'
2753 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2754 && cap->nchar != 'j' && cap->nchar != 'k'
2755 &&
2756#endif
2757 checkclearop(cap->oap))
2758 return;
2759
2760 /*
2761 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2762 * If line number given, set cursor.
2763 */
2764 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2765 && cap->count0
2766 && cap->count0 != curwin->w_cursor.lnum)
2767 {
2768 setpcmark();
2769 if (cap->count0 > curbuf->b_ml.ml_line_count)
2770 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2771 else
2772 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002773 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775
2776 switch (nchar)
2777 {
2778 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
2779 case '+':
2780 if (cap->count0 == 0)
2781 {
2782 /* No count given: put cursor at the line below screen */
2783 validate_botline(); /* make sure w_botline is valid */
2784 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2785 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2786 else
2787 curwin->w_cursor.lnum = curwin->w_botline;
2788 }
2789 /* FALLTHROUGH */
2790 case NL:
2791 case CAR:
2792 case K_KENTER:
2793 beginline(BL_WHITE | BL_FIX);
2794 /* FALLTHROUGH */
2795
2796 case 't': scroll_cursor_top(0, TRUE);
2797 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002798 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 break;
2800
2801 /* "z." and "zz": put cursor in middle of screen */
2802 case '.': beginline(BL_WHITE | BL_FIX);
2803 /* FALLTHROUGH */
2804
2805 case 'z': scroll_cursor_halfway(TRUE);
2806 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002807 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 break;
2809
2810 /* "z^", "z-" and "zb": put cursor at bottom of screen */
2811 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
2812 * when <count> is at bottom of window, and puts that one at
2813 * bottom of window. */
2814 if (cap->count0 != 0)
2815 {
2816 scroll_cursor_bot(0, TRUE);
2817 curwin->w_cursor.lnum = curwin->w_topline;
2818 }
2819 else if (curwin->w_topline == 1)
2820 curwin->w_cursor.lnum = 1;
2821 else
2822 curwin->w_cursor.lnum = curwin->w_topline - 1;
2823 /* FALLTHROUGH */
2824 case '-':
2825 beginline(BL_WHITE | BL_FIX);
2826 /* FALLTHROUGH */
2827
2828 case 'b': scroll_cursor_bot(0, TRUE);
2829 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002830 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 break;
2832
2833 /* "zH" - scroll screen right half-page */
2834 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002835 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 /* FALLTHROUGH */
2837
2838 /* "zh" - scroll screen to the right */
2839 case 'h':
2840 case K_LEFT:
2841 if (!curwin->w_p_wrap)
2842 {
2843 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2844 curwin->w_leftcol = 0;
2845 else
2846 curwin->w_leftcol -= (colnr_T)cap->count1;
2847 leftcol_changed();
2848 }
2849 break;
2850
2851 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02002852 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 /* FALLTHROUGH */
2854
2855 /* "zl" - scroll screen to the left */
2856 case 'l':
2857 case K_RIGHT:
2858 if (!curwin->w_p_wrap)
2859 {
2860 /* scroll the window left */
2861 curwin->w_leftcol += (colnr_T)cap->count1;
2862 leftcol_changed();
2863 }
2864 break;
2865
2866 /* "zs" - scroll screen, cursor at the start */
2867 case 's': if (!curwin->w_p_wrap)
2868 {
2869#ifdef FEAT_FOLDING
2870 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2871 col = 0; /* like the cursor is in col 0 */
2872 else
2873#endif
2874 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002875 if ((long)col > siso)
2876 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 else
2878 col = 0;
2879 if (curwin->w_leftcol != col)
2880 {
2881 curwin->w_leftcol = col;
2882 redraw_later(NOT_VALID);
2883 }
2884 }
2885 break;
2886
2887 /* "ze" - scroll screen, cursor at the end */
2888 case 'e': if (!curwin->w_p_wrap)
2889 {
2890#ifdef FEAT_FOLDING
2891 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2892 col = 0; /* like the cursor is in col 0 */
2893 else
2894#endif
2895 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002896 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002897 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 col = 0;
2899 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002900 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (curwin->w_leftcol != col)
2902 {
2903 curwin->w_leftcol = col;
2904 redraw_later(NOT_VALID);
2905 }
2906 }
2907 break;
2908
2909#ifdef FEAT_FOLDING
2910 /* "zF": create fold command */
2911 /* "zf": create fold operator */
2912 case 'F':
2913 case 'f': if (foldManualAllowed(TRUE))
2914 {
2915 cap->nchar = 'f';
2916 nv_operator(cap);
2917 curwin->w_p_fen = TRUE;
2918
2919 /* "zF" is like "zfzf" */
2920 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2921 {
2922 nv_operator(cap);
2923 finish_op = TRUE;
2924 }
2925 }
2926 else
2927 clearopbeep(cap->oap);
2928 break;
2929
2930 /* "zd": delete fold at cursor */
2931 /* "zD": delete fold at cursor recursively */
2932 case 'd':
2933 case 'D': if (foldManualAllowed(FALSE))
2934 {
2935 if (VIsual_active)
2936 nv_operator(cap);
2937 else
2938 deleteFold(curwin->w_cursor.lnum,
2939 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2940 }
2941 break;
2942
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002943 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 case 'E': if (foldmethodIsManual(curwin))
2945 {
2946 clearFolding(curwin);
2947 changed_window_setting();
2948 }
2949 else if (foldmethodIsMarker(curwin))
2950 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2951 TRUE, FALSE);
2952 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002953 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 break;
2955
2956 /* "zn": fold none: reset 'foldenable' */
2957 case 'n': curwin->w_p_fen = FALSE;
2958 break;
2959
2960 /* "zN": fold Normal: set 'foldenable' */
2961 case 'N': curwin->w_p_fen = TRUE;
2962 break;
2963
2964 /* "zi": invert folding: toggle 'foldenable' */
2965 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2966 break;
2967
2968 /* "za": open closed fold or close open fold at cursor */
2969 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2970 openFold(curwin->w_cursor.lnum, cap->count1);
2971 else
2972 {
2973 closeFold(curwin->w_cursor.lnum, cap->count1);
2974 curwin->w_p_fen = TRUE;
2975 }
2976 break;
2977
2978 /* "zA": open fold at cursor recursively */
2979 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2980 openFoldRecurse(curwin->w_cursor.lnum);
2981 else
2982 {
2983 closeFoldRecurse(curwin->w_cursor.lnum);
2984 curwin->w_p_fen = TRUE;
2985 }
2986 break;
2987
2988 /* "zo": open fold at cursor or Visual area */
2989 case 'o': if (VIsual_active)
2990 nv_operator(cap);
2991 else
2992 openFold(curwin->w_cursor.lnum, cap->count1);
2993 break;
2994
2995 /* "zO": open fold recursively */
2996 case 'O': if (VIsual_active)
2997 nv_operator(cap);
2998 else
2999 openFoldRecurse(curwin->w_cursor.lnum);
3000 break;
3001
3002 /* "zc": close fold at cursor or Visual area */
3003 case 'c': if (VIsual_active)
3004 nv_operator(cap);
3005 else
3006 closeFold(curwin->w_cursor.lnum, cap->count1);
3007 curwin->w_p_fen = TRUE;
3008 break;
3009
3010 /* "zC": close fold recursively */
3011 case 'C': if (VIsual_active)
3012 nv_operator(cap);
3013 else
3014 closeFoldRecurse(curwin->w_cursor.lnum);
3015 curwin->w_p_fen = TRUE;
3016 break;
3017
3018 /* "zv": open folds at the cursor */
3019 case 'v': foldOpenCursor();
3020 break;
3021
3022 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
3023 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02003024 curwin->w_foldinvalid = TRUE; /* recompute folds */
3025 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 foldOpenCursor();
3027 break;
3028
3029 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
3030 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02003031 curwin->w_foldinvalid = TRUE; /* recompute folds */
3032 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 break;
3034
3035 /* "zm": fold more */
3036 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003037 {
3038 curwin->w_p_fdl -= cap->count1;
3039 if (curwin->w_p_fdl < 0)
3040 curwin->w_p_fdl = 0;
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 old_fdl = -1; /* force an update */
3043 curwin->w_p_fen = TRUE;
3044 break;
3045
3046 /* "zM": close all folds */
3047 case 'M': curwin->w_p_fdl = 0;
3048 old_fdl = -1; /* force an update */
3049 curwin->w_p_fen = TRUE;
3050 break;
3051
3052 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003053 case 'r': curwin->w_p_fdl += cap->count1;
3054 {
3055 int d = getDeepestNesting();
3056
3057 if (curwin->w_p_fdl >= d)
3058 curwin->w_p_fdl = d;
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 break;
3061
3062 /* "zR": open all folds */
3063 case 'R': curwin->w_p_fdl = getDeepestNesting();
3064 old_fdl = -1; /* force an update */
3065 break;
3066
3067 case 'j': /* "zj" move to next fold downwards */
3068 case 'k': /* "zk" move to next fold upwards */
3069 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3070 cap->count1) == FAIL)
3071 clearopbeep(cap->oap);
3072 break;
3073
3074#endif /* FEAT_FOLDING */
3075
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003076#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00003077 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
3078 ++no_mapping;
3079 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003080 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003081 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003082 --no_mapping;
3083 --allow_keys;
3084#ifdef FEAT_CMDL_INFO
3085 (void)add_to_showcmd(nchar);
3086#endif
3087 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3088 {
3089 clearopbeep(cap->oap);
3090 break;
3091 }
3092 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003093 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00003094
Bram Moolenaarb765d632005-06-07 21:00:02 +00003095 case 'g': /* "zg": add good word to word list */
3096 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00003097 case 'G': /* "zG": add good word to temp word list */
3098 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003099 {
3100 char_u *ptr = NULL;
3101 int len;
3102
3103 if (checkclearop(cap->oap))
3104 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003105 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3106 == FAIL)
3107 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003108 if (ptr == NULL)
3109 {
3110 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003111
Bram Moolenaar134bf072013-09-25 18:54:24 +02003112 /* Find bad word under the cursor. When 'spell' is
3113 * off this fails and find_ident_under_cursor() is
3114 * used below. */
3115 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003116 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003117 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003118 if (len != 0 && curwin->w_cursor.col <= pos.col)
3119 ptr = ml_get_pos(&curwin->w_cursor);
3120 curwin->w_cursor = pos;
3121 }
3122
Bram Moolenaarb765d632005-06-07 21:00:02 +00003123 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3124 FIND_IDENT)) == 0)
3125 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003126 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3127 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003128 (nchar == 'G' || nchar == 'W')
3129 ? 0 : (int)cap->count1,
3130 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003131 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003132 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003133
Bram Moolenaar43abc522005-12-10 20:15:02 +00003134 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003135 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003136 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003137 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003138#endif
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 default: clearopbeep(cap->oap);
3141 }
3142
3143#ifdef FEAT_FOLDING
3144 /* Redraw when 'foldenable' changed */
3145 if (old_fen != curwin->w_p_fen)
3146 {
3147# ifdef FEAT_DIFF
3148 win_T *wp;
3149
3150 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3151 {
3152 /* Adjust 'foldenable' in diff-synced windows. */
3153 FOR_ALL_WINDOWS(wp)
3154 {
3155 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3156 {
3157 wp->w_p_fen = curwin->w_p_fen;
3158 changed_window_setting_win(wp);
3159 }
3160 }
3161 }
3162# endif
3163 changed_window_setting();
3164 }
3165
3166 /* Redraw when 'foldlevel' changed. */
3167 if (old_fdl != curwin->w_p_fdl)
3168 newFoldLevel();
3169#endif
3170}
3171
3172#ifdef FEAT_GUI
3173/*
3174 * Vertical scrollbar movement.
3175 */
3176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003177nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178{
3179 if (cap->oap->op_type != OP_NOP)
3180 clearopbeep(cap->oap);
3181
3182 /* Even if an operator was pending, we still want to scroll */
3183 gui_do_scroll();
3184}
3185
3186/*
3187 * Horizontal scrollbar movement.
3188 */
3189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003190nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 if (cap->oap->op_type != OP_NOP)
3193 clearopbeep(cap->oap);
3194
3195 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003196 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197}
3198#endif
3199
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003200#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003201/*
3202 * Click in GUI tab.
3203 */
3204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003205nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003206{
3207 if (cap->oap->op_type != OP_NOP)
3208 clearopbeep(cap->oap);
3209
3210 /* Even if an operator was pending, we still want to jump tabs. */
3211 goto_tabpage(current_tab);
3212}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003213
3214/*
3215 * Selected item in tab line menu.
3216 */
3217 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003218nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003219{
3220 if (cap->oap->op_type != OP_NOP)
3221 clearopbeep(cap->oap);
3222
3223 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003224 handle_tabmenu();
3225}
3226
3227/*
3228 * Handle selecting an item of the GUI tab line menu.
3229 * Used in Normal and Insert mode.
3230 */
3231 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003232handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003233{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003234 switch (current_tabmenu)
3235 {
3236 case TABLINE_MENU_CLOSE:
3237 if (current_tab == 0)
3238 do_cmdline_cmd((char_u *)"tabclose");
3239 else
3240 {
3241 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3242 current_tab);
3243 do_cmdline_cmd(IObuff);
3244 }
3245 break;
3246
3247 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003248 if (current_tab == 0)
3249 do_cmdline_cmd((char_u *)"$tabnew");
3250 else
3251 {
3252 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3253 current_tab - 1);
3254 do_cmdline_cmd(IObuff);
3255 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003256 break;
3257
3258 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003259 if (current_tab == 0)
3260 do_cmdline_cmd((char_u *)"browse $tabnew");
3261 else
3262 {
3263 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3264 current_tab - 1);
3265 do_cmdline_cmd(IObuff);
3266 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003267 break;
3268 }
3269}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003270#endif
3271
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272/*
3273 * "Q" command.
3274 */
3275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003276nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 /*
3279 * Ignore 'Q' in Visual mode, just give a beep.
3280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003282 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003283 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 do_exmode(FALSE);
3285}
3286
3287/*
3288 * Handle a ":" command.
3289 */
3290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003291nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003294 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (VIsual_active)
3297 nv_operator(cap);
3298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 if (cap->oap->op_type != OP_NOP)
3301 {
3302 /* Using ":" as a movement is characterwise exclusive. */
3303 cap->oap->motion_type = MCHAR;
3304 cap->oap->inclusive = FALSE;
3305 }
3306 else if (cap->count0)
3307 {
3308 /* translate "count:" into ":.,.+(count - 1)" */
3309 stuffcharReadbuff('.');
3310 if (cap->count0 > 1)
3311 {
3312 stuffReadbuff((char_u *)",.+");
3313 stuffnumReadbuff((long)cap->count0 - 1L);
3314 }
3315 }
3316
3317 /* When typing, don't type below an old message */
3318 if (KeyTyped)
3319 compute_cmdrow();
3320
3321 old_p_im = p_im;
3322
3323 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003324 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3326
3327 /* If 'insertmode' changed, enter or exit Insert mode */
3328 if (p_im != old_p_im)
3329 {
3330 if (p_im)
3331 restart_edit = 'i';
3332 else
3333 restart_edit = 0;
3334 }
3335
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003336 if (cmd_result == FAIL)
3337 /* The Ex command failed, do not execute the operator. */
3338 clearop(cap->oap);
3339 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3341 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003342 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3343 || did_emsg
3344 ))
3345 /* The start of the operator has become invalid by the Ex command.
3346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 clearopbeep(cap->oap);
3348 }
3349}
3350
3351/*
3352 * Handle CTRL-G command.
3353 */
3354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003355nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 if (VIsual_active) /* toggle Selection/Visual mode */
3358 {
3359 VIsual_select = !VIsual_select;
3360 showmode();
3361 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003362 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 /* print full name if count given or :cd used */
3364 fileinfo((int)cap->count0, FALSE, TRUE);
3365}
3366
3367/*
3368 * Handle CTRL-H <Backspace> command.
3369 */
3370 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003371nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (VIsual_active && VIsual_select)
3374 {
3375 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
3376 v_visop(cap);
3377 }
3378 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 nv_left(cap);
3380}
3381
3382/*
3383 * CTRL-L: clear screen and redraw.
3384 */
3385 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003386nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 if (!checkclearop(cap->oap))
3389 {
3390#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
3391 /*
3392 * Right now, the BeBox doesn't seem to have an easy way to detect
3393 * window resizing, so we cheat and make the user detect it
3394 * manually with CTRL-L instead
3395 */
3396 ui_get_shellsize();
3397#endif
3398#ifdef FEAT_SYN_HL
3399 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003400 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003401# ifdef FEAT_RELTIME
3402 {
3403 win_T *wp;
3404
3405 FOR_ALL_WINDOWS(wp)
3406 wp->w_s->b_syn_slow = FALSE;
3407 }
3408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409#endif
3410 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003411#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3412# ifdef VIMDLL
3413 if (!gui.in_use)
3414# endif
3415 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418}
3419
3420/*
3421 * CTRL-O: In Select mode: switch to Visual mode for one command.
3422 * Otherwise: Go to older pcmark.
3423 */
3424 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003425nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (VIsual_active && VIsual_select)
3428 {
3429 VIsual_select = FALSE;
3430 showmode();
3431 restart_VIsual_select = 2; /* restart Select mode later */
3432 }
3433 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
3435 cap->count1 = -cap->count1;
3436 nv_pcmark(cap);
3437 }
3438}
3439
3440/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003441 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3442 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 */
3444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003445nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 if (!checkclearopq(cap->oap))
3448 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3449 GETF_SETMARK|GETF_ALT, FALSE);
3450}
3451
3452/*
3453 * "Z" commands.
3454 */
3455 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003456nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
3458 if (!checkclearopq(cap->oap))
3459 {
3460 switch (cap->nchar)
3461 {
3462 /* "ZZ": equivalent to ":x". */
3463 case 'Z': do_cmdline_cmd((char_u *)"x");
3464 break;
3465
3466 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
3467 case 'Q': do_cmdline_cmd((char_u *)"q!");
3468 break;
3469
3470 default: clearopbeep(cap->oap);
3471 }
3472 }
3473}
3474
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475/*
3476 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3477 */
3478 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003479do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 oparg_T oa;
3482 cmdarg_T ca;
3483
3484 clear_oparg(&oa);
3485 vim_memset(&ca, 0, sizeof(ca));
3486 ca.oap = &oa;
3487 ca.cmdchar = c1;
3488 ca.nchar = c2;
3489 nv_ident(&ca);
3490}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492/*
3493 * Handle the commands that use the word under the cursor.
3494 * [g] CTRL-] :ta to current identifier
3495 * [g] 'K' run program for current identifier
3496 * [g] '*' / to current identifier or string
3497 * [g] '#' ? to current identifier or string
3498 * g ']' :tselect for current identifier
3499 */
3500 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003501nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
3503 char_u *ptr = NULL;
3504 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003505 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003506 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 char_u *p;
3508 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003509 int kp_help; /* 'keywordprg' is ":he" */
3510 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 int n = 0; /* init for GCC */
3512 int cmdchar;
3513 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003514 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 char_u *aux_ptr;
3516 int isman;
3517 int isman_s;
3518
3519 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
3520 {
3521 cmdchar = cap->nchar;
3522 g_cmd = TRUE;
3523 }
3524 else
3525 {
3526 cmdchar = cap->cmdchar;
3527 g_cmd = FALSE;
3528 }
3529
3530 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
3531 cmdchar = '#';
3532
3533 /*
3534 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3535 */
3536 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3539 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (checkclearopq(cap->oap))
3541 return;
3542 }
3543
3544 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3545 (cmdchar == '*' || cmdchar == '#')
3546 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3547 {
3548 clearop(cap->oap);
3549 return;
3550 }
3551
3552 /* Allocate buffer to put the command in. Inserting backslashes can
3553 * double the length of the word. p_kp / curbuf->b_p_kp could be added
3554 * and some numbers. */
3555 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3556 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3557 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003558 if (kp_help && *skipwhite(ptr) == NUL)
3559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003560 emsg(_(e_noident)); /* found white space only */
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003561 return;
3562 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003563 kp_ex = (*kp == ':');
3564 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3565 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (buf == NULL)
3567 return;
3568 buf[0] = NUL;
3569
3570 switch (cmdchar)
3571 {
3572 case '*':
3573 case '#':
3574 /*
3575 * Put cursor at start of word, makes search skip the word
3576 * under the cursor.
3577 * Call setpcmark() first, so "*``" puts the cursor back where
3578 * it was.
3579 */
3580 setpcmark();
3581 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3582
3583 if (!g_cmd && vim_iswordp(ptr))
3584 STRCPY(buf, "\\<");
3585 no_smartcase = TRUE; /* don't use 'smartcase' now */
3586 break;
3587
3588 case 'K':
3589 if (kp_help)
3590 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003591 else if (kp_ex)
3592 {
3593 if (cap->count0 != 0)
3594 vim_snprintf((char *)buf, buflen, "%s %ld",
3595 kp, cap->count0);
3596 else
3597 STRCPY(buf, kp);
3598 STRCAT(buf, " ");
3599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 else
3601 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003602 /* An external command will probably use an argument starting
3603 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003604 while (*ptr == '-' && n > 0)
3605 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003606 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003607 --n;
3608 }
3609 if (n == 0)
3610 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003611 emsg(_(e_noident)); /* found dashes only */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003612 vim_free(buf);
3613 return;
3614 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003615
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 /* When a count is given, turn it into a range. Is this
3617 * really what we want? */
3618 isman = (STRCMP(kp, "man") == 0);
3619 isman_s = (STRCMP(kp, "man -s") == 0);
3620 if (cap->count0 != 0 && !(isman || isman_s))
3621 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3622
3623 STRCAT(buf, "! ");
3624 if (cap->count0 == 0 && isman_s)
3625 STRCAT(buf, "man");
3626 else
3627 STRCAT(buf, kp);
3628 STRCAT(buf, " ");
3629 if (cap->count0 != 0 && (isman || isman_s))
3630 {
3631 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3632 STRCAT(buf, " ");
3633 }
3634 }
3635 break;
3636
3637 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003638 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#ifdef FEAT_CSCOPE
3640 if (p_cst)
3641 STRCPY(buf, "cstag ");
3642 else
3643#endif
3644 STRCPY(buf, "ts ");
3645 break;
3646
3647 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003648 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (curbuf->b_help)
3650 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003652 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003653 if (g_cmd)
3654 STRCPY(buf, "tj ");
3655 else
3656 sprintf((char *)buf, "%ldta ", cap->count0);
3657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659
3660 /*
3661 * Now grab the chars in the identifier
3662 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003663 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003665 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003666 if (kp_ex)
3667 /* Escape the argument properly for an Ex command */
3668 p = vim_strsave_fnameescape(ptr, FALSE);
3669 else
3670 /* Escape the argument properly for a shell command */
3671 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003672 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003673 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003675 vim_free(buf);
3676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003678 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003679 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003680 {
3681 vim_free(buf);
3682 vim_free(p);
3683 return;
3684 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003685 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003686 STRCAT(buf, p);
3687 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003689 else
3690 {
3691 if (cmdchar == '*')
3692 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
3693 else if (cmdchar == '#')
3694 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003695 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003696 {
3697 if (curbuf->b_help)
3698 /* ":help" handles unescaped argument */
3699 aux_ptr = (char_u *)"";
3700 else
3701 aux_ptr = (char_u *)"\\|\"\n[";
3702 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003703 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003704 aux_ptr = (char_u *)"\\|\"\n*?[";
3705
3706 p = buf + STRLEN(buf);
3707 while (n-- > 0)
3708 {
3709 /* put a backslash before \ and some others */
3710 if (vim_strchr(aux_ptr, *ptr) != NULL)
3711 *p++ = '\\';
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003712 /* When current byte is a part of multibyte character, copy all
3713 * bytes of that character. */
3714 if (has_mbyte)
3715 {
3716 int i;
3717 int len = (*mb_ptr2len)(ptr) - 1;
3718
3719 for (i = 0; i < len && n >= 1; ++i, --n)
3720 *p++ = *ptr++;
3721 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003722 *p++ = *ptr++;
3723 }
3724 *p = NUL;
3725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 /*
3728 * Execute the command.
3729 */
3730 if (cmdchar == '*' || cmdchar == '#')
3731 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003732 if (!g_cmd && (has_mbyte
3733 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3734 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003736
3737 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003738 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003740
Bram Moolenaar46539112015-02-17 15:43:57 +01003741 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003744 {
3745 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003747 g_tag_at_cursor = FALSE;
3748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 vim_free(buf);
3751}
3752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753/*
3754 * Get visually selected text, within one line only.
3755 * Returns FAIL if more than one line selected.
3756 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003757 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003758get_visual_text(
3759 cmdarg_T *cap,
3760 char_u **pp, /* return: start of selected text */
3761 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
3763 if (VIsual_mode != 'V')
3764 unadjust_for_sel();
3765 if (VIsual.lnum != curwin->w_cursor.lnum)
3766 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003767 if (cap != NULL)
3768 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return FAIL;
3770 }
3771 if (VIsual_mode == 'V')
3772 {
3773 *pp = ml_get_curline();
3774 *lenp = (int)STRLEN(*pp);
3775 }
3776 else
3777 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003778 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 {
3780 *pp = ml_get_pos(&curwin->w_cursor);
3781 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3782 }
3783 else
3784 {
3785 *pp = ml_get_pos(&VIsual);
3786 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (has_mbyte)
3789 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003790 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 reset_VIsual_and_resel();
3793 return OK;
3794}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
3796/*
3797 * CTRL-T: backwards in tag stack
3798 */
3799 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003800nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801{
3802 if (!checkclearopq(cap->oap))
3803 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3804}
3805
3806/*
3807 * Handle scrolling command 'H', 'L' and 'M'.
3808 */
3809 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003810nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
3812 int used = 0;
3813 long n;
3814#ifdef FEAT_FOLDING
3815 linenr_T lnum;
3816#endif
3817 int half;
3818
3819 cap->oap->motion_type = MLINE;
3820 setpcmark();
3821
3822 if (cap->cmdchar == 'L')
3823 {
3824 validate_botline(); /* make sure curwin->w_botline is valid */
3825 curwin->w_cursor.lnum = curwin->w_botline - 1;
3826 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3827 curwin->w_cursor.lnum = 1;
3828 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003829 {
3830#ifdef FEAT_FOLDING
3831 if (hasAnyFolding(curwin))
3832 {
3833 /* Count a fold for one screen line. */
3834 for (n = cap->count1 - 1; n > 0
3835 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3836 {
3837 (void)hasFolding(curwin->w_cursor.lnum,
3838 &curwin->w_cursor.lnum, NULL);
3839 --curwin->w_cursor.lnum;
3840 }
3841 }
3842 else
3843#endif
3844 curwin->w_cursor.lnum -= cap->count1 - 1;
3845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847 else
3848 {
3849 if (cap->cmdchar == 'M')
3850 {
3851#ifdef FEAT_DIFF
3852 /* Don't count filler lines above the window. */
3853 used -= diff_check_fill(curwin, curwin->w_topline)
3854 - curwin->w_topfill;
3855#endif
3856 validate_botline(); /* make sure w_empty_rows is valid */
3857 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3858 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3859 {
3860#ifdef FEAT_DIFF
3861 /* Count half he number of filler lines to be "below this
3862 * line" and half to be "above the next line". */
3863 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3864 + n) / 2 >= half)
3865 {
3866 --n;
3867 break;
3868 }
3869#endif
3870 used += plines(curwin->w_topline + n);
3871 if (used >= half)
3872 break;
3873#ifdef FEAT_FOLDING
3874 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3875 n = lnum - curwin->w_topline;
3876#endif
3877 }
3878 if (n > 0 && used > curwin->w_height)
3879 --n;
3880 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003881 else /* (cap->cmdchar == 'H') */
3882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003884#ifdef FEAT_FOLDING
3885 if (hasAnyFolding(curwin))
3886 {
3887 /* Count a fold for one screen line. */
3888 lnum = curwin->w_topline;
3889 while (n-- > 0 && lnum < curwin->w_botline - 1)
3890 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003891 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003892 ++lnum;
3893 }
3894 n = lnum - curwin->w_topline;
3895 }
3896#endif
3897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 curwin->w_cursor.lnum = curwin->w_topline + n;
3899 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3900 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3901 }
3902
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003903 /* Correct for 'so', except when an operator is pending. */
3904 if (cap->oap->op_type == OP_NOP)
3905 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 beginline(BL_SOL | BL_FIX);
3907}
3908
3909/*
3910 * Cursor right commands.
3911 */
3912 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003913nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
3915 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003916 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003918 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3919 {
3920 /* <C-Right> and <S-Right> move a word or WORD right */
3921 if (mod_mask & MOD_MASK_CTRL)
3922 cap->arg = TRUE;
3923 nv_wordcmd(cap);
3924 return;
3925 }
3926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 cap->oap->motion_type = MCHAR;
3928 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003929 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003932 * In virtual edit mode, there's no such thing as "past_line", as lines
3933 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 */
3935 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003936 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 for (n = cap->count1; n > 0; --n)
3939 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003940 if ((!past_line && oneright() == FAIL)
3941 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003942 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
3944 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003945 * <Space> wraps to next line if 'whichwrap' has 's'.
3946 * 'l' wraps to next line if 'whichwrap' has 'l'.
3947 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 */
3949 if ( ((cap->cmdchar == ' '
3950 && vim_strchr(p_ww, 's') != NULL)
3951 || (cap->cmdchar == 'l'
3952 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003953 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 && vim_strchr(p_ww, '>') != NULL))
3955 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3956 {
3957 /* When deleting we also count the NL as a character.
3958 * Set cap->oap->inclusive when last char in the line is
3959 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003960 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003962 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 cap->oap->inclusive = TRUE;
3964 else
3965 {
3966 ++curwin->w_cursor.lnum;
3967 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 curwin->w_set_curswant = TRUE;
3970 cap->oap->inclusive = FALSE;
3971 }
3972 continue;
3973 }
3974 if (cap->oap->op_type == OP_NOP)
3975 {
3976 /* Only beep and flush if not moved at all */
3977 if (n == cap->count1)
3978 beep_flush();
3979 }
3980 else
3981 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003982 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 cap->oap->inclusive = TRUE;
3984 }
3985 break;
3986 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003987 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
3989 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (virtual_active())
3991 oneright();
3992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003995 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 ++curwin->w_cursor.col;
3998 }
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001#ifdef FEAT_FOLDING
4002 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4003 && cap->oap->op_type == OP_NOP)
4004 foldOpenCursor();
4005#endif
4006}
4007
4008/*
4009 * Cursor left commands.
4010 *
4011 * Returns TRUE when operator end should not be adjusted.
4012 */
4013 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004014nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
4016 long n;
4017
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004018 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4019 {
4020 /* <C-Left> and <S-Left> move a word or WORD left */
4021 if (mod_mask & MOD_MASK_CTRL)
4022 cap->arg = 1;
4023 nv_bck_word(cap);
4024 return;
4025 }
4026
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 cap->oap->motion_type = MCHAR;
4028 cap->oap->inclusive = FALSE;
4029 for (n = cap->count1; n > 0; --n)
4030 {
4031 if (oneleft() == FAIL)
4032 {
4033 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4034 * 'h' wraps to previous line if 'whichwrap' has 'h'.
4035 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
4036 */
4037 if ( (((cap->cmdchar == K_BS
4038 || cap->cmdchar == Ctrl_H)
4039 && vim_strchr(p_ww, 'b') != NULL)
4040 || (cap->cmdchar == 'h'
4041 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004042 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 && vim_strchr(p_ww, '<') != NULL))
4044 && curwin->w_cursor.lnum > 1)
4045 {
4046 --(curwin->w_cursor.lnum);
4047 coladvance((colnr_T)MAXCOL);
4048 curwin->w_set_curswant = TRUE;
4049
4050 /* When the NL before the first char has to be deleted we
4051 * put the cursor on the NUL after the previous line.
4052 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00004053 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 if ( (cap->oap->op_type == OP_DELETE
4055 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004056 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004058 char_u *cp = ml_get_cursor();
4059
4060 if (*cp != NUL)
4061 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004062 if (has_mbyte)
4063 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4064 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004065 ++curwin->w_cursor.col;
4066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 cap->retval |= CA_NO_ADJ_OP_END;
4068 }
4069 continue;
4070 }
4071 /* Only beep and flush if not moved at all */
4072 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4073 beep_flush();
4074 break;
4075 }
4076 }
4077#ifdef FEAT_FOLDING
4078 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4079 && cap->oap->op_type == OP_NOP)
4080 foldOpenCursor();
4081#endif
4082}
4083
4084/*
4085 * Cursor up commands.
4086 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4087 */
4088 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004089nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004091 if (mod_mask & MOD_MASK_SHIFT)
4092 {
4093 /* <S-Up> is page up */
4094 cap->arg = BACKWARD;
4095 nv_page(cap);
4096 }
4097 else
4098 {
4099 cap->oap->motion_type = MLINE;
4100 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4101 clearopbeep(cap->oap);
4102 else if (cap->arg)
4103 beginline(BL_WHITE | BL_FIX);
4104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105}
4106
4107/*
4108 * Cursor down commands.
4109 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4110 */
4111 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004112nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004114 if (mod_mask & MOD_MASK_SHIFT)
4115 {
4116 /* <S-Down> is page down */
4117 cap->arg = FORWARD;
4118 nv_page(cap);
4119 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004120#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004121 /* Quickfix window only: view the result under the cursor. */
4122 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4123 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
4127#ifdef FEAT_CMDWIN
4128 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004129 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 cmdwin_result = CAR;
4131 else
4132#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004133#ifdef FEAT_JOB_CHANNEL
4134 /* In a prompt buffer a <CR> in the last line invokes the callback. */
4135 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4136 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4137 {
4138 invoke_prompt_callback();
4139 if (restart_edit == 0)
4140 restart_edit = 'a';
4141 }
4142 else
4143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 {
4145 cap->oap->motion_type = MLINE;
4146 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4147 clearopbeep(cap->oap);
4148 else if (cap->arg)
4149 beginline(BL_WHITE | BL_FIX);
4150 }
4151 }
4152}
4153
4154#ifdef FEAT_SEARCHPATH
4155/*
4156 * Grab the file name under the cursor and edit it.
4157 */
4158 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004159nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
4161 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004162 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004164 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004167 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return;
4169 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004170 if (curbuf_locked())
4171 {
4172 clearop(cap->oap);
4173 return;
4174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004176 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
4178 if (ptr != NULL)
4179 {
4180 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004181 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004182 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004184 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004185 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004186 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004187 {
4188 curwin->w_cursor.lnum = lnum;
4189 check_cursor_lnum();
4190 beginline(BL_SOL | BL_FIX);
4191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 vim_free(ptr);
4193 }
4194 else
4195 clearop(cap->oap);
4196}
4197#endif
4198
4199/*
4200 * <End> command: to end of current line or last line.
4201 */
4202 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004203nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004205 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004207 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 nv_goto(cap);
4209 cap->count1 = 1; /* to end of current line */
4210 }
4211 nv_dollar(cap);
4212}
4213
4214/*
4215 * Handle the "$" command.
4216 */
4217 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004218nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 cap->oap->motion_type = MCHAR;
4221 cap->oap->inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 /* In virtual mode when off the edge of a line and an operator
4223 * is pending (whew!) keep the cursor where it is.
4224 * Otherwise, send it to the end of the line. */
4225 if (!virtual_active() || gchar_cursor() != NUL
4226 || cap->oap->op_type == OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 curwin->w_curswant = MAXCOL; /* so we stay at the end */
4228 if (cursor_down((long)(cap->count1 - 1),
4229 cap->oap->op_type == OP_NOP) == FAIL)
4230 clearopbeep(cap->oap);
4231#ifdef FEAT_FOLDING
4232 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4233 foldOpenCursor();
4234#endif
4235}
4236
4237/*
4238 * Implementation of '?' and '/' commands.
4239 * If cap->arg is TRUE don't set PC mark.
4240 */
4241 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004242nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243{
4244 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004245 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
4247 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4248 {
4249 /* Translate "g??" to "g?g?" */
4250 cap->cmdchar = 'g';
4251 cap->nchar = '?';
4252 nv_operator(cap);
4253 return;
4254 }
4255
Bram Moolenaardda933d2016-09-03 21:04:58 +02004256 /* When using 'incsearch' the cursor may be moved to set a different search
4257 * start position. */
Bram Moolenaare96a2492019-06-25 04:12:16 +02004258 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260 if (cap->searchbuf == NULL)
4261 {
4262 clearop(oap);
4263 return;
4264 }
4265
Bram Moolenaar46539112015-02-17 15:43:57 +01004266 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004267 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02004268 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269}
4270
4271/*
4272 * Handle "N" and "n" commands.
4273 * cap->arg is SEARCH_REV for "N", 0 for "n".
4274 */
4275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004276nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
Bram Moolenaar46539112015-02-17 15:43:57 +01004278 pos_T old = curwin->w_cursor;
4279 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
4280
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004281 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004282 {
4283 /* Avoid getting stuck on the current cursor position, which can
4284 * happen when an offset is given and the cursor is on the last char
4285 * in the buffer: Repeat with count + 1. */
4286 cap->count1 += 1;
4287 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
4288 cap->count1 -= 1;
4289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290}
4291
4292/*
4293 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4294 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004295 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004297 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004298normal_search(
4299 cmdarg_T *cap,
4300 int dir,
4301 char_u *pat,
4302 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303{
4304 int i;
4305
4306 cap->oap->motion_type = MCHAR;
4307 cap->oap->inclusive = FALSE;
4308 cap->oap->use_reg_one = TRUE;
4309 curwin->w_set_curswant = TRUE;
4310
4311 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004312 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (i == 0)
4314 clearop(cap->oap);
4315 else
4316 {
4317 if (i == 2)
4318 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320#ifdef FEAT_FOLDING
4321 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4322 foldOpenCursor();
4323#endif
4324 }
4325
4326 /* "/$" will put the cursor after the end of the line, may need to
4327 * correct that here */
4328 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004329 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330}
4331
4332/*
4333 * Character search commands.
4334 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4335 * ',' and FALSE for ';'.
4336 * cap->nchar is NUL for ',' and ';' (repeat the search)
4337 */
4338 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004339nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 int t_cmd;
4342
4343 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4344 t_cmd = TRUE;
4345 else
4346 t_cmd = FALSE;
4347
4348 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4350 clearopbeep(cap->oap);
4351 else
4352 {
4353 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 /* Include a Tab for "tx" and for "dfx". */
4355 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4356 && (t_cmd || cap->oap->op_type != OP_NOP))
4357 {
4358 colnr_T scol, ecol;
4359
4360 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4361 curwin->w_cursor.coladd = ecol - scol;
4362 }
4363 else
4364 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366#ifdef FEAT_FOLDING
4367 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4368 foldOpenCursor();
4369#endif
4370 }
4371}
4372
4373/*
4374 * "[" and "]" commands.
4375 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4376 */
4377 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004378nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004380 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 pos_T prev_pos;
4382 pos_T *pos = NULL; /* init for GCC */
4383 pos_T old_pos; /* cursor position before command */
4384 int flag;
4385 long n;
4386 int findc;
4387 int c;
4388
4389 cap->oap->motion_type = MCHAR;
4390 cap->oap->inclusive = FALSE;
4391 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004392 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394#ifdef FEAT_SEARCHPATH
4395 /*
4396 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4397 */
4398 if (cap->nchar == 'f')
4399 nv_gotofile(cap);
4400 else
4401#endif
4402
4403#ifdef FEAT_FIND_ID
4404 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004405 * Find the occurrence(s) of the identifier or define under cursor
4406 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 *
4408 * search list jump
4409 * fwd bwd fwd bwd fwd bwd
4410 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4411 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4412 */
4413 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004414# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004416# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 cap->nchar) != NULL)
4420 {
4421 char_u *ptr;
4422 int len;
4423
4424 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4425 clearop(cap->oap);
4426 else
4427 {
4428 find_pattern_in_path(ptr, 0, len, TRUE,
4429 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4430 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4431 cap->count1,
4432 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4433 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4434 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4435 (linenr_T)MAXLNUM);
4436 curwin->w_set_curswant = TRUE;
4437 }
4438 }
4439 else
4440#endif
4441
4442 /*
4443 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4444 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4445 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4446 * "[m" or "]m" search for prev/next start of (Java) method.
4447 * "[M" or "]M" search for prev/next end of (Java) method.
4448 */
4449 if ( (cap->cmdchar == '['
4450 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4451 || (cap->cmdchar == ']'
4452 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4453 {
4454 if (cap->nchar == '*')
4455 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 prev_pos.lnum = 0;
4457 if (cap->nchar == 'm' || cap->nchar == 'M')
4458 {
4459 if (cap->cmdchar == '[')
4460 findc = '{';
4461 else
4462 findc = '}';
4463 n = 9999;
4464 }
4465 else
4466 {
4467 findc = cap->nchar;
4468 n = cap->count1;
4469 }
4470 for ( ; n > 0; --n)
4471 {
4472 if ((pos = findmatchlimit(cap->oap, findc,
4473 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4474 {
4475 if (new_pos.lnum == 0) /* nothing found */
4476 {
4477 if (cap->nchar != 'm' && cap->nchar != 'M')
4478 clearopbeep(cap->oap);
4479 }
4480 else
4481 pos = &new_pos; /* use last one found */
4482 break;
4483 }
4484 prev_pos = new_pos;
4485 curwin->w_cursor = *pos;
4486 new_pos = *pos;
4487 }
4488 curwin->w_cursor = old_pos;
4489
4490 /*
4491 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4492 * brought us to the match for "[m" and "]M" when inside a method.
4493 * Try finding the '{' or '}' we want to be at.
4494 * Also repeat for the given count.
4495 */
4496 if (cap->nchar == 'm' || cap->nchar == 'M')
4497 {
4498 /* norm is TRUE for "]M" and "[m" */
4499 int norm = ((findc == '{') == (cap->nchar == 'm'));
4500
4501 n = cap->count1;
4502 /* found a match: we were inside a method */
4503 if (prev_pos.lnum != 0)
4504 {
4505 pos = &prev_pos;
4506 curwin->w_cursor = prev_pos;
4507 if (norm)
4508 --n;
4509 }
4510 else
4511 pos = NULL;
4512 while (n > 0)
4513 {
4514 for (;;)
4515 {
4516 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4517 {
4518 /* if not found anything, that's an error */
4519 if (pos == NULL)
4520 clearopbeep(cap->oap);
4521 n = 0;
4522 break;
4523 }
4524 c = gchar_cursor();
4525 if (c == '{' || c == '}')
4526 {
4527 /* Must have found end/start of class: use it.
4528 * Or found the place to be at. */
4529 if ((c == findc && norm) || (n == 1 && !norm))
4530 {
4531 new_pos = curwin->w_cursor;
4532 pos = &new_pos;
4533 n = 0;
4534 }
4535 /* if no match found at all, we started outside of the
4536 * class and we're inside now. Just go on. */
4537 else if (new_pos.lnum == 0)
4538 {
4539 new_pos = curwin->w_cursor;
4540 pos = &new_pos;
4541 }
4542 /* found start/end of other method: go to match */
4543 else if ((pos = findmatchlimit(cap->oap, findc,
4544 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4545 0)) == NULL)
4546 n = 0;
4547 else
4548 curwin->w_cursor = *pos;
4549 break;
4550 }
4551 }
4552 --n;
4553 }
4554 curwin->w_cursor = old_pos;
4555 if (pos == NULL && new_pos.lnum != 0)
4556 clearopbeep(cap->oap);
4557 }
4558 if (pos != NULL)
4559 {
4560 setpcmark();
4561 curwin->w_cursor = *pos;
4562 curwin->w_set_curswant = TRUE;
4563#ifdef FEAT_FOLDING
4564 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4565 && cap->oap->op_type == OP_NOP)
4566 foldOpenCursor();
4567#endif
4568 }
4569 }
4570
4571 /*
4572 * "[[", "[]", "]]" and "][": move to start or end of function
4573 */
4574 else if (cap->nchar == '[' || cap->nchar == ']')
4575 {
4576 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
4577 flag = '{';
4578 else
4579 flag = '}'; /* "][" or "[]" */
4580
4581 curwin->w_set_curswant = TRUE;
4582 /*
4583 * Imitate strange Vi behaviour: When using "]]" with an operator
4584 * we also stop at '}'.
4585 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004586 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 (cap->oap->op_type != OP_NOP
4588 && cap->arg == FORWARD && flag == '{')))
4589 clearopbeep(cap->oap);
4590 else
4591 {
4592 if (cap->oap->op_type == OP_NOP)
4593 beginline(BL_WHITE | BL_FIX);
4594#ifdef FEAT_FOLDING
4595 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4596 foldOpenCursor();
4597#endif
4598 }
4599 }
4600
4601 /*
4602 * "[p", "[P", "]P" and "]p": put with indent adjustment
4603 */
4604 else if (cap->nchar == 'p' || cap->nchar == 'P')
4605 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004606 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608
4609 /*
4610 * "['", "[`", "]'" and "]`": jump to next mark
4611 */
4612 else if (cap->nchar == '\'' || cap->nchar == '`')
4613 {
4614 pos = &curwin->w_cursor;
4615 for (n = cap->count1; n > 0; --n)
4616 {
4617 prev_pos = *pos;
4618 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4619 cap->nchar == '\'');
4620 if (pos == NULL)
4621 break;
4622 }
4623 if (pos == NULL)
4624 pos = &prev_pos;
4625 nv_cursormark(cap, cap->nchar == '\'', pos);
4626 }
4627
4628#ifdef FEAT_MOUSE
4629 /*
4630 * [ or ] followed by a middle mouse click: put selected text with
4631 * indent adjustment. Any other button just does as usual.
4632 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004633 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635 (void)do_mouse(cap->oap, cap->nchar,
4636 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4637 cap->count1, PUT_FIXINDENT);
4638 }
4639#endif /* FEAT_MOUSE */
4640
4641#ifdef FEAT_FOLDING
4642 /*
4643 * "[z" and "]z": move to start or end of open fold.
4644 */
4645 else if (cap->nchar == 'z')
4646 {
4647 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4648 cap->count1) == FAIL)
4649 clearopbeep(cap->oap);
4650 }
4651#endif
4652
4653#ifdef FEAT_DIFF
4654 /*
4655 * "[c" and "]c": move to next or previous diff-change.
4656 */
4657 else if (cap->nchar == 'c')
4658 {
4659 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4660 cap->count1) == FAIL)
4661 clearopbeep(cap->oap);
4662 }
4663#endif
4664
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004665#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004666 /*
4667 * "[s", "[S", "]s" and "]S": move to next spell error.
4668 */
4669 else if (cap->nchar == 's' || cap->nchar == 'S')
4670 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004671 setpcmark();
4672 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004673 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4674 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004675 {
4676 clearopbeep(cap->oap);
4677 break;
4678 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004679 else
4680 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004681# ifdef FEAT_FOLDING
4682 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4683 foldOpenCursor();
4684# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004685 }
4686#endif
4687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 /* Not a valid cap->nchar. */
4689 else
4690 clearopbeep(cap->oap);
4691}
4692
4693/*
4694 * Handle Normal mode "%" command.
4695 */
4696 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004697nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
4699 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004700#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 linenr_T lnum = curwin->w_cursor.lnum;
4702#endif
4703
4704 cap->oap->inclusive = TRUE;
4705 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
4706 {
4707 if (cap->count0 > 100)
4708 clearopbeep(cap->oap);
4709 else
4710 {
4711 cap->oap->motion_type = MLINE;
4712 setpcmark();
4713 /* Round up, so CTRL-G will give same value. Watch out for a
4714 * large line count, the line number must not go negative! */
4715 if (curbuf->b_ml.ml_line_count > 1000000)
4716 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4717 / 100L * cap->count0;
4718 else
4719 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4720 cap->count0 + 99L) / 100L;
4721 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4722 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4723 beginline(BL_SOL | BL_FIX);
4724 }
4725 }
4726 else /* "%" : go to matching paren */
4727 {
4728 cap->oap->motion_type = MCHAR;
4729 cap->oap->use_reg_one = TRUE;
4730 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4731 clearopbeep(cap->oap);
4732 else
4733 {
4734 setpcmark();
4735 curwin->w_cursor = *pos;
4736 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
4740 }
4741#ifdef FEAT_FOLDING
4742 if (cap->oap->op_type == OP_NOP
4743 && lnum != curwin->w_cursor.lnum
4744 && (fdo_flags & FDO_PERCENT)
4745 && KeyTyped)
4746 foldOpenCursor();
4747#endif
4748}
4749
4750/*
4751 * Handle "(" and ")" commands.
4752 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4753 */
4754 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004755nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
4757 cap->oap->motion_type = MCHAR;
4758 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004759 /* The motion used to be inclusive for "(", but that is not what Vi does. */
4760 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 curwin->w_set_curswant = TRUE;
4762
4763 if (findsent(cap->arg, cap->count1) == FAIL)
4764 clearopbeep(cap->oap);
4765 else
4766 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004767 /* Don't leave the cursor on the NUL past end of line. */
4768 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770#ifdef FEAT_FOLDING
4771 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4772 foldOpenCursor();
4773#endif
4774 }
4775}
4776
4777/*
4778 * "m" command: Mark a position.
4779 */
4780 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004781nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 if (!checkclearop(cap->oap))
4784 {
4785 if (setmark(cap->nchar) == FAIL)
4786 clearopbeep(cap->oap);
4787 }
4788}
4789
4790/*
4791 * "{" and "}" commands.
4792 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4793 */
4794 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004795nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
4797 cap->oap->motion_type = MCHAR;
4798 cap->oap->inclusive = FALSE;
4799 cap->oap->use_reg_one = TRUE;
4800 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004801 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 clearopbeep(cap->oap);
4803 else
4804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806#ifdef FEAT_FOLDING
4807 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4808 foldOpenCursor();
4809#endif
4810 }
4811}
4812
4813/*
4814 * "u" command: Undo or make lower case.
4815 */
4816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004817nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004819 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
4822 cap->cmdchar = 'g';
4823 cap->nchar = 'u';
4824 nv_operator(cap);
4825 }
4826 else
4827 nv_kundo(cap);
4828}
4829
4830/*
4831 * <Undo> command.
4832 */
4833 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004834nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835{
4836 if (!checkclearopq(cap->oap))
4837 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004838#ifdef FEAT_JOB_CHANNEL
4839 if (bt_prompt(curbuf))
4840 {
4841 clearopbeep(cap->oap);
4842 return;
4843 }
4844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 u_undo((int)cap->count1);
4846 curwin->w_set_curswant = TRUE;
4847 }
4848}
4849
4850/*
4851 * Handle the "r" command.
4852 */
4853 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004854nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 char_u *ptr;
4857 int had_ctrl_v;
4858 long n;
4859
4860 if (checkclearop(cap->oap))
4861 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004862#ifdef FEAT_JOB_CHANNEL
4863 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4864 {
4865 clearopbeep(cap->oap);
4866 return;
4867 }
4868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 /* get another character */
4871 if (cap->nchar == Ctrl_V)
4872 {
4873 had_ctrl_v = Ctrl_V;
4874 cap->nchar = get_literal();
4875 /* Don't redo a multibyte character with CTRL-V. */
4876 if (cap->nchar > DEL)
4877 had_ctrl_v = NUL;
4878 }
4879 else
4880 had_ctrl_v = NUL;
4881
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004882 /* Abort if the character is a special key. */
4883 if (IS_SPECIAL(cap->nchar))
4884 {
4885 clearopbeep(cap->oap);
4886 return;
4887 }
4888
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 /* Visual mode "r" */
4890 if (VIsual_active)
4891 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004892 if (got_int)
4893 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004894 if (had_ctrl_v)
4895 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004896 /* Use a special (negative) number to make a difference between a
4897 * literal CR or NL and a line break. */
4898 if (cap->nchar == CAR)
4899 cap->nchar = REPLACE_CR_NCHAR;
4900 else if (cap->nchar == NL)
4901 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 nv_operator(cap);
4904 return;
4905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 /* Break tabs, etc. */
4908 if (virtual_active())
4909 {
4910 if (u_save_cursor() == FAIL)
4911 return;
4912 if (gchar_cursor() == NUL)
4913 {
4914 /* Add extra space and put the cursor on the first one. */
4915 coladvance_force((colnr_T)(getviscol() + cap->count1));
4916 curwin->w_cursor.col -= cap->count1;
4917 }
4918 else if (gchar_cursor() == TAB)
4919 coladvance_force(getviscol());
4920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004922 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004924 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004925 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
4927 clearopbeep(cap->oap);
4928 return;
4929 }
4930
4931 /*
4932 * Replacing with a TAB is done by edit() when it is complicated because
4933 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4934 * Other characters are done below to avoid problems with things like
4935 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
4936 */
4937 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4938 {
4939 stuffnumReadbuff(cap->count1);
4940 stuffcharReadbuff('R');
4941 stuffcharReadbuff('\t');
4942 stuffcharReadbuff(ESC);
4943 return;
4944 }
4945
4946 /* save line for undo */
4947 if (u_save_cursor() == FAIL)
4948 return;
4949
4950 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4951 {
4952 /*
4953 * Replace character(s) by a single newline.
4954 * Strange vi behaviour: Only one newline is inserted.
4955 * Delete the characters here.
4956 * Insert the newline with an insert command, takes care of
4957 * autoindent. The insert command depends on being on the last
4958 * character of a line or not.
4959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 (void)del_chars(cap->count1, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 stuffcharReadbuff('\r');
4962 stuffcharReadbuff(ESC);
4963
4964 /* Give 'r' to edit(), to get the redo command right. */
4965 invoke_edit(cap, TRUE, 'r', FALSE);
4966 }
4967 else
4968 {
4969 prep_redo(cap->oap->regname, cap->count1,
4970 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4971
4972 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (has_mbyte)
4974 {
4975 int old_State = State;
4976
4977 if (cap->ncharC1 != 0)
4978 AppendCharToRedobuff(cap->ncharC1);
4979 if (cap->ncharC2 != 0)
4980 AppendCharToRedobuff(cap->ncharC2);
4981
4982 /* This is slow, but it handles replacing a single-byte with a
4983 * multi-byte and the other way around. Also handles adding
4984 * composing characters for utf-8. */
4985 for (n = cap->count1; n > 0; --n)
4986 {
4987 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004988 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4989 {
4990 int c = ins_copychar(curwin->w_cursor.lnum
4991 + (cap->nchar == Ctrl_Y ? -1 : 1));
4992 if (c != NUL)
4993 ins_char(c);
4994 else
4995 /* will be decremented further down */
4996 ++curwin->w_cursor.col;
4997 }
4998 else
4999 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 State = old_State;
5001 if (cap->ncharC1 != 0)
5002 ins_char(cap->ncharC1);
5003 if (cap->ncharC2 != 0)
5004 ins_char(cap->ncharC2);
5005 }
5006 }
5007 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
5009 /*
5010 * Replace the characters within one line.
5011 */
5012 for (n = cap->count1; n > 0; --n)
5013 {
5014 /*
5015 * Get ptr again, because u_save and/or showmatch() will have
5016 * released the line. At the same time we let know that the
5017 * line will be changed.
5018 */
5019 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005020 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5021 {
5022 int c = ins_copychar(curwin->w_cursor.lnum
5023 + (cap->nchar == Ctrl_Y ? -1 : 1));
5024 if (c != NUL)
5025 ptr[curwin->w_cursor.col] = c;
5026 }
5027 else
5028 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (p_sm && msg_silent == 0)
5030 showmatch(cap->nchar);
5031 ++curwin->w_cursor.col;
5032 }
5033#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005034 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005036 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
Bram Moolenaar009b2592004-10-24 19:18:58 +00005038 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005039 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005041 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
5043#endif
5044
5045 /* mark the buffer as changed and prepare for displaying */
5046 changed_bytes(curwin->w_cursor.lnum,
5047 (colnr_T)(curwin->w_cursor.col - cap->count1));
5048 }
5049 --curwin->w_cursor.col; /* cursor on the last replaced char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 /* if the character on the left of the current cursor is a multi-byte
5051 * character, move two characters left */
5052 if (has_mbyte)
5053 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 curbuf->b_op_end = curwin->w_cursor;
5055 curwin->w_set_curswant = TRUE;
5056 set_last_insert(cap->nchar);
5057 }
5058}
5059
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060/*
5061 * 'o': Exchange start and end of Visual area.
5062 * 'O': same, but in block mode exchange left and right corners.
5063 */
5064 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005065v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066{
5067 pos_T old_cursor;
5068 colnr_T left, right;
5069
5070 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5071 {
5072 old_cursor = curwin->w_cursor;
5073 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5074 curwin->w_cursor.lnum = VIsual.lnum;
5075 coladvance(left);
5076 VIsual = curwin->w_cursor;
5077
5078 curwin->w_cursor.lnum = old_cursor.lnum;
5079 curwin->w_curswant = right;
5080 /* 'selection "exclusive" and cursor at right-bottom corner: move it
5081 * right one column */
5082 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5083 ++curwin->w_curswant;
5084 coladvance(curwin->w_curswant);
5085 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005087 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
5089 curwin->w_cursor.lnum = VIsual.lnum;
5090 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5091 ++right;
5092 coladvance(right);
5093 VIsual = curwin->w_cursor;
5094
5095 curwin->w_cursor.lnum = old_cursor.lnum;
5096 coladvance(left);
5097 curwin->w_curswant = left;
5098 }
5099 }
5100 else
5101 {
5102 old_cursor = curwin->w_cursor;
5103 curwin->w_cursor = VIsual;
5104 VIsual = old_cursor;
5105 curwin->w_set_curswant = TRUE;
5106 }
5107}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109/*
5110 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5111 */
5112 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005113nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (VIsual_active) /* "R" is replace lines */
5116 {
5117 cap->cmdchar = 'c';
5118 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01005119 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 VIsual_mode = 'V';
5121 nv_operator(cap);
5122 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005123 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
5125 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005126 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 else
5128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (virtual_active())
5130 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5132 }
5133 }
5134}
5135
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136/*
5137 * "gr".
5138 */
5139 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005140nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (VIsual_active)
5143 {
5144 cap->cmdchar = 'r';
5145 cap->nchar = cap->extra_char;
5146 nv_replace(cap); /* Do same as "r" in Visual mode for now */
5147 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005148 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
5150 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005151 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 else
5153 {
5154 if (cap->extra_char == Ctrl_V) /* get another character */
5155 cap->extra_char = get_literal();
5156 stuffcharReadbuff(cap->extra_char);
5157 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (virtual_active())
5159 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 invoke_edit(cap, TRUE, 'v', FALSE);
5161 }
5162 }
5163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
5165/*
5166 * Swap case for "~" command, when it does not work like an operator.
5167 */
5168 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005169n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170{
5171 long n;
5172 pos_T startpos;
5173 int did_change = 0;
5174#ifdef FEAT_NETBEANS_INTG
5175 pos_T pos;
5176 char_u *ptr;
5177 int count;
5178#endif
5179
5180 if (checkclearopq(cap->oap))
5181 return;
5182
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005183 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
5185 clearopbeep(cap->oap);
5186 return;
5187 }
5188
5189 prep_redo_cmd(cap);
5190
5191 if (u_save_cursor() == FAIL)
5192 return;
5193
5194 startpos = curwin->w_cursor;
5195#ifdef FEAT_NETBEANS_INTG
5196 pos = startpos;
5197#endif
5198 for (n = cap->count1; n > 0; --n)
5199 {
5200 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5201 inc_cursor();
5202 if (gchar_cursor() == NUL)
5203 {
5204 if (vim_strchr(p_ww, '~') != NULL
5205 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5206 {
5207#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005208 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 {
5210 if (did_change)
5211 {
5212 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005213 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005214 netbeans_removed(curbuf, pos.lnum, pos.col,
5215 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005217 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219 pos.col = 0;
5220 pos.lnum++;
5221 }
5222#endif
5223 ++curwin->w_cursor.lnum;
5224 curwin->w_cursor.col = 0;
5225 if (n > 1)
5226 {
5227 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5228 break;
5229 u_clearline();
5230 }
5231 }
5232 else
5233 break;
5234 }
5235 }
5236#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005237 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
5239 ptr = ml_get(pos.lnum);
5240 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005241 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5242 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244#endif
5245
5246
5247 check_cursor();
5248 curwin->w_set_curswant = TRUE;
5249 if (did_change)
5250 {
5251 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5252 0L);
5253 curbuf->b_op_start = startpos;
5254 curbuf->b_op_end = curwin->w_cursor;
5255 if (curbuf->b_op_end.col > 0)
5256 --curbuf->b_op_end.col;
5257 }
5258}
5259
5260/*
5261 * Move cursor to mark.
5262 */
5263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005264nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
5266 if (check_mark(pos) == FAIL)
5267 clearop(cap->oap);
5268 else
5269 {
5270 if (cap->cmdchar == '\''
5271 || cap->cmdchar == '`'
5272 || cap->cmdchar == '['
5273 || cap->cmdchar == ']')
5274 setpcmark();
5275 curwin->w_cursor = *pos;
5276 if (flag)
5277 beginline(BL_WHITE | BL_FIX);
5278 else
5279 check_cursor();
5280 }
5281 cap->oap->motion_type = flag ? MLINE : MCHAR;
5282 if (cap->cmdchar == '`')
5283 cap->oap->use_reg_one = TRUE;
5284 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
5285 curwin->w_set_curswant = TRUE;
5286}
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288/*
5289 * Handle commands that are operators in Visual mode.
5290 */
5291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293{
5294 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5295
5296 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005297 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 if (isupper(cap->cmdchar))
5299 {
5300 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005301 {
5302 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5306 curwin->w_curswant = MAXCOL;
5307 }
5308 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5309 nv_operator(cap);
5310}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
5312/*
5313 * "s" and "S" commands.
5314 */
5315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005316nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005318#ifdef FEAT_TERMINAL
5319 /* When showing output of term_dumpdiff() swap the top and botom. */
5320 if (term_swap_diff() == OK)
5321 return;
5322#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005323#ifdef FEAT_JOB_CHANNEL
5324 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5325 {
5326 clearopbeep(cap->oap);
5327 return;
5328 }
5329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
5331 {
5332 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005333 {
5334 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 cap->cmdchar = 'c';
5338 nv_operator(cap);
5339 }
5340 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 nv_optrans(cap);
5342}
5343
5344/*
5345 * Abbreviated commands.
5346 */
5347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005348nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
5351 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
5352
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 /* in Visual mode these commands are operators */
5354 if (VIsual_active)
5355 v_visop(cap);
5356 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 nv_optrans(cap);
5358}
5359
5360/*
5361 * Translate a command into another command.
5362 */
5363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005364nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5367 (char_u *)"d$", (char_u *)"c$",
5368 (char_u *)"cl", (char_u *)"cc",
5369 (char_u *)"yy", (char_u *)":s\r"};
5370 static char_u *str = (char_u *)"xXDCsSY&";
5371
5372 if (!checkclearopq(cap->oap))
5373 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005374 // In Vi "2D" doesn't delete the next line. Can't translate it
5375 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005376 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5377 {
5378 cap->oap->start = curwin->w_cursor;
5379 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005380#ifdef FEAT_EVAL
5381 set_op_var(OP_DELETE);
5382#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005383 cap->count1 = 1;
5384 nv_dollar(cap);
5385 finish_op = TRUE;
5386 ResetRedobuff();
5387 AppendCharToRedobuff('D');
5388 }
5389 else
5390 {
5391 if (cap->count0)
5392 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005393 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
5396 cap->opcount = 0;
5397}
5398
5399/*
5400 * "'" and "`" commands. Also for "g'" and "g`".
5401 * cap->arg is TRUE for "'" and "g'".
5402 */
5403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005404nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 pos_T *pos;
5407 int c;
5408#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005409 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 int old_KeyTyped = KeyTyped; /* getting file may reset it */
5411#endif
5412
5413 if (cap->cmdchar == 'g')
5414 c = cap->extra_char;
5415 else
5416 c = cap->nchar;
5417 pos = getmark(c, (cap->oap->op_type == OP_NOP));
5418 if (pos == (pos_T *)-1) /* jumped to other file */
5419 {
5420 if (cap->arg)
5421 {
5422 check_cursor_lnum();
5423 beginline(BL_WHITE | BL_FIX);
5424 }
5425 else
5426 check_cursor();
5427 }
5428 else
5429 nv_cursormark(cap, cap->arg, pos);
5430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 /* May need to clear the coladd that a mark includes. */
5432 if (!virtual_active())
5433 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005434 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435#ifdef FEAT_FOLDING
5436 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005437 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005438 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 && (fdo_flags & FDO_MARK)
5440 && old_KeyTyped)
5441 foldOpenCursor();
5442#endif
5443}
5444
5445/*
5446 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
5447 */
5448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005449nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
5451#ifdef FEAT_JUMPLIST
5452 pos_T *pos;
5453# ifdef FEAT_FOLDING
5454 linenr_T lnum = curwin->w_cursor.lnum;
5455 int old_KeyTyped = KeyTyped; /* getting file may reset it */
5456# endif
5457
5458 if (!checkclearopq(cap->oap))
5459 {
5460 if (cap->cmdchar == 'g')
5461 pos = movechangelist((int)cap->count1);
5462 else
5463 pos = movemark((int)cap->count1);
5464 if (pos == (pos_T *)-1) /* jump to other file */
5465 {
5466 curwin->w_set_curswant = TRUE;
5467 check_cursor();
5468 }
5469 else if (pos != NULL) /* can jump */
5470 nv_cursormark(cap, FALSE, pos);
5471 else if (cap->cmdchar == 'g')
5472 {
5473 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005474 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005476 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005478 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480 else
5481 clearopbeep(cap->oap);
5482# ifdef FEAT_FOLDING
5483 if (cap->oap->op_type == OP_NOP
5484 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5485 && (fdo_flags & FDO_MARK)
5486 && old_KeyTyped)
5487 foldOpenCursor();
5488# endif
5489 }
5490#else
5491 clearopbeep(cap->oap);
5492#endif
5493}
5494
5495/*
5496 * Handle '"' command.
5497 */
5498 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005499nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501 if (checkclearop(cap->oap))
5502 return;
5503#ifdef FEAT_EVAL
5504 if (cap->nchar == '=')
5505 cap->nchar = get_expr_register();
5506#endif
5507 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5508 {
5509 cap->oap->regname = cap->nchar;
5510 cap->opcount = cap->count0; /* remember count before '"' */
5511#ifdef FEAT_EVAL
5512 set_reg_var(cap->oap->regname);
5513#endif
5514 }
5515 else
5516 clearopbeep(cap->oap);
5517}
5518
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519/*
5520 * Handle "v", "V" and "CTRL-V" commands.
5521 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5522 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005523 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 */
5525 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005526nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005528 if (cap->cmdchar == Ctrl_Q)
5529 cap->cmdchar = Ctrl_V;
5530
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5532 * characterwise, linewise, or blockwise. */
5533 if (cap->oap->op_type != OP_NOP)
5534 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005535 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 finish_op = FALSE; /* operator doesn't finish now but later */
5537 return;
5538 }
5539
5540 VIsual_select = cap->arg;
5541 if (VIsual_active) /* change Visual mode */
5542 {
5543 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
5544 end_visual_mode();
5545 else /* toggle char/block mode */
5546 { /* or char/line mode */
5547 VIsual_mode = cap->cmdchar;
5548 showmode();
5549 }
5550 redraw_curbuf_later(INVERTED); /* update the inversion */
5551 }
5552 else /* start Visual mode */
5553 {
5554 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005555 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005557 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 VIsual = curwin->w_cursor;
5559
5560 VIsual_active = TRUE;
5561 VIsual_reselect = TRUE;
5562 if (!cap->arg)
5563 /* start Select mode when 'selectmode' contains "cmd" */
5564 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005566 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 redraw_cmdline = TRUE; /* show visual mode later */
5568 /*
5569 * For V and ^V, we multiply the number of lines even if there
5570 * was only one -- webb
5571 */
5572 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5573 {
5574 curwin->w_cursor.lnum +=
5575 resel_VIsual_line_count * cap->count0 - 1;
5576 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5577 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5578 }
5579 VIsual_mode = resel_VIsual_mode;
5580 if (VIsual_mode == 'v')
5581 {
5582 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005583 {
5584 validate_virtcol();
5585 curwin->w_curswant = curwin->w_virtcol
5586 + resel_VIsual_vcol * cap->count0 - 1;
5587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005589 curwin->w_curswant = resel_VIsual_vcol;
5590 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005592 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
5594 curwin->w_curswant = MAXCOL;
5595 coladvance((colnr_T)MAXCOL);
5596 }
5597 else if (VIsual_mode == Ctrl_V)
5598 {
5599 validate_virtcol();
5600 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005601 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 coladvance(curwin->w_curswant);
5603 }
5604 else
5605 curwin->w_set_curswant = TRUE;
5606 redraw_curbuf_later(INVERTED); /* show the inversion */
5607 }
5608 else
5609 {
5610 if (!cap->arg)
5611 /* start Select mode when 'selectmode' contains "cmd" */
5612 may_start_select('c');
5613 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005614 if (VIsual_mode != 'V' && *p_sel == 'e')
5615 ++cap->count1; /* include one more char */
5616 if (cap->count0 > 0 && --cap->count1 > 0)
5617 {
5618 /* With a count select that many characters or lines. */
5619 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5620 nv_right(cap);
5621 else if (VIsual_mode == 'V')
5622 nv_down(cap);
5623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
5625 }
5626}
5627
5628/*
5629 * Start selection for Shift-movement keys.
5630 */
5631 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005632start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 /* if 'selectmode' contains "key", start Select mode */
5635 may_start_select('k');
5636 n_start_visual_mode('v');
5637}
5638
5639/*
5640 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5641 */
5642 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005643may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
5645 VIsual_select = (stuff_empty() && typebuf_typed()
5646 && (vim_strchr(p_slm, c) != NULL));
5647}
5648
5649/*
5650 * Start Visual mode "c".
5651 * Should set VIsual_select before calling this.
5652 */
5653 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005654n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005656#ifdef FEAT_CONCEAL
5657 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02005658 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005659#endif
5660
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 VIsual_mode = c;
5662 VIsual_active = TRUE;
5663 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005664
5665 // Corner case: the 0 position in a tab may change when going into
5666 // virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005668 {
5669 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 VIsual = curwin->w_cursor;
5673
5674#ifdef FEAT_FOLDING
5675 foldAdjustVisual();
5676#endif
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005679#ifdef FEAT_CONCEAL
5680 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02005681 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005682#endif
5683
Bram Moolenaar09df3122006-01-23 22:23:09 +00005684 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 redraw_cmdline = TRUE; /* show visual mode later */
5686#ifdef FEAT_CLIPBOARD
5687 /* Make sure the clipboard gets updated. Needed because start and
5688 * end may still be the same, and the selection needs to be owned */
5689 clip_star.vmode = NUL;
5690#endif
5691
5692 /* Only need to redraw this line, unless still need to redraw an old
5693 * Visual area (when 'lazyredraw' is set). */
5694 if (curwin->w_redr_type < INVERTED)
5695 {
5696 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5697 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5698 }
5699}
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
5702/*
5703 * CTRL-W: Window commands
5704 */
5705 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005706nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005708 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005709 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02005710 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005711 cap->cmdchar = ':';
5712 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005713 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005714 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005715 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717}
5718
5719/*
5720 * CTRL-Z: Suspend
5721 */
5722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005723nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 if (VIsual_active)
5727 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 do_cmdline_cmd((char_u *)"st");
5729}
5730
5731/*
5732 * Commands starting with "g".
5733 */
5734 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005735nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 int i;
5740 int flag = FALSE;
5741
5742 switch (cap->nchar)
5743 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005744 case Ctrl_A:
5745 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746#ifdef MEM_PROFILE
5747 /*
5748 * "g^A": dump log of used memory.
5749 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005750 if (!VIsual_active && cap->nchar == Ctrl_A)
5751 vim_mem_profile_dump();
5752 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005754 /*
5755 * "g^A/g^X": sequentially increment visually selected region
5756 */
5757 if (VIsual_active)
5758 {
5759 cap->arg = TRUE;
5760 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005761 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005762 nv_addsub(cap);
5763 }
5764 else
5765 clearopbeep(oap);
5766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 /*
5769 * "gR": Enter virtual replace mode.
5770 */
5771 case 'R':
5772 cap->arg = TRUE;
5773 nv_Replace(cap);
5774 break;
5775
5776 case 'r':
5777 nv_vreplace(cap);
5778 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
5780 case '&':
5781 do_cmdline_cmd((char_u *)"%s//~/&");
5782 break;
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 /*
5785 * "gv": Reselect the previous Visual area. If Visual already active,
5786 * exchange previous and current Visual area.
5787 */
5788 case 'v':
5789 if (checkclearop(oap))
5790 break;
5791
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005792 if ( curbuf->b_visual.vi_start.lnum == 0
5793 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5794 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 beep_flush();
5796 else
5797 {
5798 /* set w_cursor to the start of the Visual area, tpos to the end */
5799 if (VIsual_active)
5800 {
5801 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005802 VIsual_mode = curbuf->b_visual.vi_mode;
5803 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804# ifdef FEAT_EVAL
5805 curbuf->b_visual_mode_eval = i;
5806# endif
5807 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005808 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5809 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005811 tpos = curbuf->b_visual.vi_end;
5812 curbuf->b_visual.vi_end = curwin->w_cursor;
5813 curwin->w_cursor = curbuf->b_visual.vi_start;
5814 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
5816 else
5817 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005818 VIsual_mode = curbuf->b_visual.vi_mode;
5819 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5820 tpos = curbuf->b_visual.vi_end;
5821 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 }
5823
5824 VIsual_active = TRUE;
5825 VIsual_reselect = TRUE;
5826
5827 /* Set Visual to the start and w_cursor to the end of the Visual
5828 * area. Make sure they are on an existing character. */
5829 check_cursor();
5830 VIsual = curwin->w_cursor;
5831 curwin->w_cursor = tpos;
5832 check_cursor();
5833 update_topline();
5834 /*
5835 * When called from normal "g" command: start Select mode when
5836 * 'selectmode' contains "cmd". When called for K_SELECT, always
5837 * start Select mode.
5838 */
5839 if (cap->arg)
5840 VIsual_select = TRUE;
5841 else
5842 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844#ifdef FEAT_CLIPBOARD
5845 /* Make sure the clipboard gets updated. Needed because start and
5846 * end are still the same, and the selection needs to be owned */
5847 clip_star.vmode = NUL;
5848#endif
5849 redraw_curbuf_later(INVERTED);
5850 showmode();
5851 }
5852 break;
5853 /*
5854 * "gV": Don't reselect the previous Visual area after a Select mode
5855 * mapping of menu.
5856 */
5857 case 'V':
5858 VIsual_reselect = FALSE;
5859 break;
5860
5861 /*
5862 * "gh": start Select mode.
5863 * "gH": start Select line mode.
5864 * "g^H": start Select block mode.
5865 */
5866 case K_BS:
5867 cap->nchar = Ctrl_H;
5868 /* FALLTHROUGH */
5869 case 'h':
5870 case 'H':
5871 case Ctrl_H:
5872# ifdef EBCDIC
5873 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
5874 if (cap->nchar == Ctrl_H)
5875 cap->cmdchar = Ctrl_V;
5876 else
5877# endif
5878 cap->cmdchar = cap->nchar + ('v' - 'h');
5879 cap->arg = TRUE;
5880 nv_visual(cap);
5881 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005882
5883 /* "gn", "gN" visually select next/previous search match
5884 * "gn" selects next match
5885 * "gN" selects previous match
5886 */
5887 case 'N':
5888 case 'n':
5889 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005890 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
5893 /*
5894 * "gj" and "gk" two new funny movement keys -- up and down
5895 * movement based on *screen* line rather than *file* line.
5896 */
5897 case 'j':
5898 case K_DOWN:
5899 /* with 'nowrap' it works just like the normal "j" command; also when
5900 * in a closed fold */
5901 if (!curwin->w_p_wrap
5902#ifdef FEAT_FOLDING
5903 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5904#endif
5905 )
5906 {
5907 oap->motion_type = MLINE;
5908 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5909 }
5910 else
5911 i = nv_screengo(oap, FORWARD, cap->count1);
5912 if (i == FAIL)
5913 clearopbeep(oap);
5914 break;
5915
5916 case 'k':
5917 case K_UP:
5918 /* with 'nowrap' it works just like the normal "k" command; also when
5919 * in a closed fold */
5920 if (!curwin->w_p_wrap
5921#ifdef FEAT_FOLDING
5922 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5923#endif
5924 )
5925 {
5926 oap->motion_type = MLINE;
5927 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5928 }
5929 else
5930 i = nv_screengo(oap, BACKWARD, cap->count1);
5931 if (i == FAIL)
5932 clearopbeep(oap);
5933 break;
5934
5935 /*
5936 * "gJ": join two lines without inserting a space.
5937 */
5938 case 'J':
5939 nv_join(cap);
5940 break;
5941
5942 /*
5943 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
5944 * "gm": middle of "g0" and "g$".
5945 */
5946 case '^':
5947 flag = TRUE;
5948 /* FALLTHROUGH */
5949
5950 case '0':
5951 case 'm':
5952 case K_HOME:
5953 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 oap->motion_type = MCHAR;
5955 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005956 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005958 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 int width2 = width1 + curwin_col_off2();
5960
5961 validate_virtcol();
5962 i = 0;
5963 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5964 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5965 }
5966 else
5967 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02005968 /* Go to the middle of the screen line. When 'number' or
5969 * 'relativenumber' is on and lines are wrapping the middle can be more
5970 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02005972 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 + ((curwin->w_p_wrap && i > 0)
5974 ? curwin_col_off2() : 0)) / 2;
5975 coladvance((colnr_T)i);
5976 if (flag)
5977 {
5978 do
5979 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01005980 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01005981 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
5983 curwin->w_set_curswant = TRUE;
5984 break;
5985
5986 case '_':
5987 /* "g_": to the last non-blank character in the line or <count> lines
5988 * downward. */
5989 cap->oap->motion_type = MCHAR;
5990 cap->oap->inclusive = TRUE;
5991 curwin->w_curswant = MAXCOL;
5992 if (cursor_down((long)(cap->count1 - 1),
5993 cap->oap->op_type == OP_NOP) == FAIL)
5994 clearopbeep(cap->oap);
5995 else
5996 {
5997 char_u *ptr = ml_get_curline();
5998
5999 /* In Visual mode we may end up after the line. */
6000 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6001 --curwin->w_cursor.col;
6002
6003 /* Decrease the cursor column until it's on a non-blank. */
6004 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006005 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 --curwin->w_cursor.col;
6007 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006008 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 }
6010 break;
6011
6012 case '$':
6013 case K_END:
6014 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 {
6016 int col_off = curwin_col_off();
6017
6018 oap->motion_type = MCHAR;
6019 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006020 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 {
6022 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6023 if (cap->count1 == 1)
6024 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006025 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 int width2 = width1 + curwin_col_off2();
6027
6028 validate_virtcol();
6029 i = width1 - 1;
6030 if (curwin->w_virtcol >= (colnr_T)width1)
6031 i += ((curwin->w_virtcol - width1) / width2 + 1)
6032 * width2;
6033 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006034
6035 /* Make sure we stick in this column. */
6036 validate_virtcol();
6037 curwin->w_curswant = curwin->w_virtcol;
6038 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6040 {
6041 /*
6042 * Check for landing on a character that got split at
6043 * the end of the line. We do not want to advance to
6044 * the next screen line.
6045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 if (curwin->w_virtcol > (colnr_T)i)
6047 --curwin->w_cursor.col;
6048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 }
6050 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6051 clearopbeep(oap);
6052 }
6053 else
6054 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006055 if (cap->count1 > 1)
6056 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006057 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006058
Bram Moolenaar02631462017-09-22 15:20:32 +02006059 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006061
Bram Moolenaard5c82342019-07-27 18:44:57 +02006062 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006063 validate_virtcol();
6064 curwin->w_curswant = curwin->w_virtcol;
6065 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 }
6067 }
6068 break;
6069
6070 /*
6071 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6072 */
6073 case '*':
6074 case '#':
6075#if POUND != '#'
6076 case POUND: /* pound sign (sometimes equal to '#') */
6077#endif
6078 case Ctrl_RSB: /* :tag or :tselect for current identifier */
6079 case ']': /* :tselect for current identifier */
6080 nv_ident(cap);
6081 break;
6082
6083 /*
6084 * ge and gE: go back to end of word
6085 */
6086 case 'e':
6087 case 'E':
6088 oap->motion_type = MCHAR;
6089 curwin->w_set_curswant = TRUE;
6090 oap->inclusive = TRUE;
6091 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6092 clearopbeep(oap);
6093 break;
6094
6095 /*
6096 * "g CTRL-G": display info about cursor position
6097 */
6098 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006099 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 break;
6101
6102 /*
6103 * "gi": start Insert at the last position.
6104 */
6105 case 'i':
6106 if (curbuf->b_last_insert.lnum != 0)
6107 {
6108 curwin->w_cursor = curbuf->b_last_insert;
6109 check_cursor_lnum();
6110 i = (int)STRLEN(ml_get_curline());
6111 if (curwin->w_cursor.col > (colnr_T)i)
6112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (virtual_active())
6114 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 curwin->w_cursor.col = i;
6116 }
6117 }
6118 cap->cmdchar = 'i';
6119 nv_edit(cap);
6120 break;
6121
6122 /*
6123 * "gI": Start insert in column 1.
6124 */
6125 case 'I':
6126 beginline(0);
6127 if (!checkclearopq(oap))
6128 invoke_edit(cap, FALSE, 'g', FALSE);
6129 break;
6130
6131#ifdef FEAT_SEARCHPATH
6132 /*
6133 * "gf": goto file, edit file under cursor
6134 * "]f" and "[f": can also be used.
6135 */
6136 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006137 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 nv_gotofile(cap);
6139 break;
6140#endif
6141
6142 /* "g'm" and "g`m": jump to mark without setting pcmark */
6143 case '\'':
6144 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006145 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 case '`':
6147 nv_gomark(cap);
6148 break;
6149
6150 /*
6151 * "gs": Goto sleep.
6152 */
6153 case 's':
6154 do_sleep(cap->count1 * 1000L);
6155 break;
6156
6157 /*
6158 * "ga": Display the ascii value of the character under the
6159 * cursor. It is displayed in decimal, hex, and octal. -- webb
6160 */
6161 case 'a':
6162 do_ascii(NULL);
6163 break;
6164
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 /*
6166 * "g8": Display the bytes used for the UTF-8 character under the
6167 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006168 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 */
6170 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006171 if (cap->count0 == 8)
6172 utf_find_illegal();
6173 else
6174 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
Bram Moolenaar60402d62017-04-20 18:54:50 +02006177 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006178 case '<':
6179 show_sb_text();
6180 break;
6181
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 /*
6183 * "gg": Goto the first line in file. With a count it goes to
6184 * that line number like for "G". -- webb
6185 */
6186 case 'g':
6187 cap->arg = FALSE;
6188 nv_goto(cap);
6189 break;
6190
6191 /*
6192 * Two-character operators:
6193 * "gq" Format text
6194 * "gw" Format text and keep cursor position
6195 * "g~" Toggle the case of the text.
6196 * "gu" Change text to lower case.
6197 * "gU" Change text to upper case.
6198 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006199 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 */
6201 case 'q':
6202 case 'w':
6203 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006204 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 case '~':
6206 case 'u':
6207 case 'U':
6208 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006209 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 nv_operator(cap);
6211 break;
6212
6213 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006214 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 * current function
6216 * "gD": idem, but in the current file.
6217 */
6218 case 'd':
6219 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006220 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 break;
6222
6223#ifdef FEAT_MOUSE
6224 /*
6225 * g<*Mouse> : <C-*mouse>
6226 */
6227 case K_MIDDLEMOUSE:
6228 case K_MIDDLEDRAG:
6229 case K_MIDDLERELEASE:
6230 case K_LEFTMOUSE:
6231 case K_LEFTDRAG:
6232 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006233 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 case K_RIGHTMOUSE:
6235 case K_RIGHTDRAG:
6236 case K_RIGHTRELEASE:
6237 case K_X1MOUSE:
6238 case K_X1DRAG:
6239 case K_X1RELEASE:
6240 case K_X2MOUSE:
6241 case K_X2DRAG:
6242 case K_X2RELEASE:
6243 mod_mask = MOD_MASK_CTRL;
6244 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6245 break;
6246#endif
6247
6248 case K_IGNORE:
6249 break;
6250
6251 /*
6252 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6253 */
6254 case 'p':
6255 case 'P':
6256 nv_put(cap);
6257 break;
6258
6259#ifdef FEAT_BYTEOFF
6260 /* "go": goto byte count from start of buffer */
6261 case 'o':
6262 goto_byte(cap->count0);
6263 break;
6264#endif
6265
6266 /* "gQ": improved Ex mode */
6267 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006268 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
6270 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006271 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 break;
6273 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006274
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 if (!checkclearopq(oap))
6276 do_exmode(TRUE);
6277 break;
6278
6279#ifdef FEAT_JUMPLIST
6280 case ',':
6281 nv_pcmark(cap);
6282 break;
6283
6284 case ';':
6285 cap->count1 = -cap->count1;
6286 nv_pcmark(cap);
6287 break;
6288#endif
6289
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006290 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006291 if (!checkclearop(oap))
6292 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006293 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006294 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006295 if (!checkclearop(oap))
6296 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006297 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006298
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006299 case '+':
6300 case '-': /* "g+" and "g-": undo or redo along the timeline */
6301 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006302 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006303 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006304 break;
6305
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 default:
6307 clearopbeep(oap);
6308 break;
6309 }
6310}
6311
6312/*
6313 * Handle "o" and "O" commands.
6314 */
6315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006316n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006318#ifdef FEAT_CONCEAL
6319 linenr_T oldline = curwin->w_cursor.lnum;
6320#endif
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 if (!checkclearopq(cap->oap))
6323 {
6324#ifdef FEAT_FOLDING
6325 if (cap->cmdchar == 'O')
6326 /* Open above the first line of a folded sequence of lines */
6327 (void)hasFolding(curwin->w_cursor.lnum,
6328 &curwin->w_cursor.lnum, NULL);
6329 else
6330 /* Open below the last line of a folded sequence of lines */
6331 (void)hasFolding(curwin->w_cursor.lnum,
6332 NULL, &curwin->w_cursor.lnum);
6333#endif
6334 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6335 (cap->cmdchar == 'O' ? 1 : 0)),
6336 (linenr_T)(curwin->w_cursor.lnum +
6337 (cap->cmdchar == 'o' ? 1 : 0))
6338 ) == OK
6339 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006340 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6341 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006343#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006344 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006345 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006346#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006347#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006348 if (curwin->w_p_cul)
6349 /* force redraw of cursorline */
6350 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006351#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006352 /* When '#' is in 'cpoptions' ignore the count. */
6353 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6354 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6356 }
6357 }
6358}
6359
6360/*
6361 * "." command: redo last change.
6362 */
6363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006364nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
6366 if (!checkclearopq(cap->oap))
6367 {
6368 /*
6369 * If "restart_edit" is TRUE, the last but one command is repeated
6370 * instead of the last command (inserting text). This is used for
6371 * CTRL-O <.> in insert mode.
6372 */
6373 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6374 clearopbeep(cap->oap);
6375 }
6376}
6377
6378/*
6379 * CTRL-R: undo undo
6380 */
6381 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006382nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 if (!checkclearopq(cap->oap))
6385 {
6386 u_redo((int)cap->count1);
6387 curwin->w_set_curswant = TRUE;
6388 }
6389}
6390
6391/*
6392 * Handle "U" command.
6393 */
6394 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006395nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006398 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 {
6400 /* translate "gUU" to "gUgU" */
6401 cap->cmdchar = 'g';
6402 cap->nchar = 'U';
6403 nv_operator(cap);
6404 }
6405 else if (!checkclearopq(cap->oap))
6406 {
6407 u_undoline();
6408 curwin->w_set_curswant = TRUE;
6409 }
6410}
6411
6412/*
6413 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6414 * single character.
6415 */
6416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006417nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006419 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006420 {
6421#ifdef FEAT_JOB_CHANNEL
6422 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6423 {
6424 clearopbeep(cap->oap);
6425 return;
6426 }
6427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 else
6431 nv_operator(cap);
6432}
6433
6434/*
6435 * Handle an operator command.
6436 * The actual work is done by do_pending_operator().
6437 */
6438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006439nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
6441 int op_type;
6442
6443 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006444#ifdef FEAT_JOB_CHANNEL
6445 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6446 {
6447 clearopbeep(cap->oap);
6448 return;
6449 }
6450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
6452 if (op_type == cap->oap->op_type) /* double operator works on lines */
6453 nv_lineop(cap);
6454 else if (!checkclearop(cap->oap))
6455 {
6456 cap->oap->start = curwin->w_cursor;
6457 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006458#ifdef FEAT_EVAL
6459 set_op_var(op_type);
6460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
6462}
6463
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006464#ifdef FEAT_EVAL
6465/*
6466 * Set v:operator to the characters for "optype".
6467 */
6468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006469set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006470{
6471 char_u opchars[3];
6472
6473 if (optype == OP_NOP)
6474 set_vim_var_string(VV_OP, NULL, 0);
6475 else
6476 {
6477 opchars[0] = get_op_char(optype);
6478 opchars[1] = get_extra_op_char(optype);
6479 opchars[2] = NUL;
6480 set_vim_var_string(VV_OP, opchars, -1);
6481 }
6482}
6483#endif
6484
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485/*
6486 * Handle linewise operator "dd", "yy", etc.
6487 *
6488 * "_" is is a strange motion command that helps make operators more logical.
6489 * It is actually implemented, but not documented in the real Vi. This motion
6490 * command actually refers to "the current line". Commands like "dd" and "yy"
6491 * are really an alternate form of "d_" and "y_". It does accept a count, so
6492 * "d3_" works to delete 3 lines.
6493 */
6494 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006495nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 cap->oap->motion_type = MLINE;
6498 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6499 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006500 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
6501 && cap->oap->motion_force != 'v'
6502 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 || cap->oap->op_type == OP_LSHIFT
6504 || cap->oap->op_type == OP_RSHIFT)
6505 beginline(BL_SOL | BL_FIX);
6506 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
6507 beginline(BL_WHITE | BL_FIX);
6508}
6509
6510/*
6511 * <Home> command.
6512 */
6513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006514nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006516 /* CTRL-HOME is like "gg" */
6517 if (mod_mask & MOD_MASK_CTRL)
6518 nv_goto(cap);
6519 else
6520 {
6521 cap->count0 = 1;
6522 nv_pipe(cap);
6523 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00006524 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
6525 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526}
6527
6528/*
6529 * "|" command.
6530 */
6531 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006532nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 cap->oap->motion_type = MCHAR;
6535 cap->oap->inclusive = FALSE;
6536 beginline(0);
6537 if (cap->count0 > 0)
6538 {
6539 coladvance((colnr_T)(cap->count0 - 1));
6540 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6541 }
6542 else
6543 curwin->w_curswant = 0;
6544 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01006545 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 curwin->w_set_curswant = FALSE;
6547}
6548
6549/*
6550 * Handle back-word command "b" and "B".
6551 * cap->arg is 1 for "B"
6552 */
6553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006554nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
6556 cap->oap->motion_type = MCHAR;
6557 cap->oap->inclusive = FALSE;
6558 curwin->w_set_curswant = TRUE;
6559 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6560 clearopbeep(cap->oap);
6561#ifdef FEAT_FOLDING
6562 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6563 foldOpenCursor();
6564#endif
6565}
6566
6567/*
6568 * Handle word motion commands "e", "E", "w" and "W".
6569 * cap->arg is TRUE for "E" and "W".
6570 */
6571 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006572nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 int n;
6575 int word_end;
6576 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006577 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
6579 /*
6580 * Set inclusive for the "E" and "e" command.
6581 */
6582 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6583 word_end = TRUE;
6584 else
6585 word_end = FALSE;
6586 cap->oap->inclusive = word_end;
6587
6588 /*
6589 * "cw" and "cW" are a special case.
6590 */
6591 if (!word_end && cap->oap->op_type == OP_CHANGE)
6592 {
6593 n = gchar_cursor();
6594 if (n != NUL) /* not an empty line */
6595 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006596 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 {
6598 /*
6599 * Reproduce a funny Vi behaviour: "cw" on a blank only
6600 * changes one character, not all blanks until the start of
6601 * the next word. Only do this when the 'w' flag is included
6602 * in 'cpoptions'.
6603 */
6604 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6605 {
6606 cap->oap->inclusive = TRUE;
6607 cap->oap->motion_type = MCHAR;
6608 return;
6609 }
6610 }
6611 else
6612 {
6613 /*
6614 * This is a little strange. To match what the real Vi does,
6615 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6616 * that we are not on a space or a TAB. This seems impolite
6617 * at first, but it's really more what we mean when we say
6618 * 'cw'.
6619 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006620 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 * will change only one character! This is done by setting
6622 * flag.
6623 */
6624 cap->oap->inclusive = TRUE;
6625 word_end = TRUE;
6626 flag = TRUE;
6627 }
6628 }
6629 }
6630
6631 cap->oap->motion_type = MCHAR;
6632 curwin->w_set_curswant = TRUE;
6633 if (word_end)
6634 n = end_word(cap->count1, cap->arg, flag, FALSE);
6635 else
6636 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6637
Bram Moolenaardfefb982008-04-01 10:06:39 +00006638 /* Don't leave the cursor on the NUL past the end of line. Unless we
6639 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006640 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006641 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
6643 if (n == FAIL && cap->oap->op_type == OP_NOP)
6644 clearopbeep(cap->oap);
6645 else
6646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648#ifdef FEAT_FOLDING
6649 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6650 foldOpenCursor();
6651#endif
6652 }
6653}
6654
6655/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006656 * Used after a movement command: If the cursor ends up on the NUL after the
6657 * end of the line, may move it back to the last character and make the motion
6658 * inclusive.
6659 */
6660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006661adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006662{
6663 /* The cursor cannot remain on the NUL when:
6664 * - the column is > 0
6665 * - not in Visual mode or 'selection' is "o"
6666 * - 'virtualedit' is not "all" and not "onemore".
6667 */
6668 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006669 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006670 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006671 {
6672 --curwin->w_cursor.col;
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006673 /* prevent cursor from moving on the trail byte */
6674 if (has_mbyte)
6675 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006676 oap->inclusive = TRUE;
6677 }
6678}
6679
6680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 * "0" and "^" commands.
6682 * cap->arg is the argument for beginline().
6683 */
6684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006685nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 cap->oap->motion_type = MCHAR;
6688 cap->oap->inclusive = FALSE;
6689 beginline(cap->arg);
6690#ifdef FEAT_FOLDING
6691 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6692 foldOpenCursor();
6693#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00006694 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
6695 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696}
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698/*
6699 * In exclusive Visual mode, may include the last character.
6700 */
6701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006702adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006705 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (has_mbyte)
6708 inc_cursor();
6709 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 ++curwin->w_cursor.col;
6711 cap->oap->inclusive = FALSE;
6712 }
6713}
6714
6715/*
6716 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6717 * Should check VIsual_mode before calling this.
6718 * Returns TRUE when backed up to the previous line.
6719 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006720 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006721unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 pos_T *pp;
6724
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006725 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006727 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 pp = &curwin->w_cursor;
6729 else
6730 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 if (pp->coladd > 0)
6732 --pp->coladd;
6733 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 if (pp->col > 0)
6735 {
6736 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006737 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 }
6739 else if (pp->lnum > 1)
6740 {
6741 --pp->lnum;
6742 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6743 return TRUE;
6744 }
6745 }
6746 return FALSE;
6747}
6748
6749/*
6750 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6751 */
6752 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006753nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 if (VIsual_active)
6756 VIsual_select = TRUE;
6757 else if (VIsual_reselect)
6758 {
6759 cap->nchar = 'v'; /* fake "gv" command */
6760 cap->arg = TRUE;
6761 nv_g_cmd(cap);
6762 }
6763}
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766/*
6767 * "G", "gg", CTRL-END, CTRL-HOME.
6768 * cap->arg is TRUE for "G".
6769 */
6770 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006771nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 linenr_T lnum;
6774
6775 if (cap->arg)
6776 lnum = curbuf->b_ml.ml_line_count;
6777 else
6778 lnum = 1L;
6779 cap->oap->motion_type = MLINE;
6780 setpcmark();
6781
6782 /* When a count is given, use it instead of the default lnum */
6783 if (cap->count0 != 0)
6784 lnum = cap->count0;
6785 if (lnum < 1L)
6786 lnum = 1L;
6787 else if (lnum > curbuf->b_ml.ml_line_count)
6788 lnum = curbuf->b_ml.ml_line_count;
6789 curwin->w_cursor.lnum = lnum;
6790 beginline(BL_SOL | BL_FIX);
6791#ifdef FEAT_FOLDING
6792 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6793 foldOpenCursor();
6794#endif
6795}
6796
6797/*
6798 * CTRL-\ in Normal mode.
6799 */
6800 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006801nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802{
6803 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6804 {
6805 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006806 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 clear_cmdline = TRUE; /* unshow mode later */
6808 restart_edit = 0;
6809#ifdef FEAT_CMDWIN
6810 if (cmdwin_type != 0)
6811 cmdwin_result = Ctrl_C;
6812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (VIsual_active)
6814 {
6815 end_visual_mode(); /* stop Visual */
6816 redraw_curbuf_later(INVERTED);
6817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
6819 if (cap->nchar == Ctrl_G && p_im)
6820 restart_edit = 'a';
6821 }
6822 else
6823 clearopbeep(cap->oap);
6824}
6825
6826/*
6827 * ESC in Normal mode: beep, but don't flush buffers.
6828 * Don't even beep if we are canceling a command.
6829 */
6830 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006831nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
6833 int no_reason;
6834
6835 no_reason = (cap->oap->op_type == OP_NOP
6836 && cap->opcount == 0
6837 && cap->count0 == 0
6838 && cap->oap->regname == 0
6839 && !p_im);
6840
6841 if (cap->arg) /* TRUE for CTRL-C */
6842 {
6843 if (restart_edit == 0
6844#ifdef FEAT_CMDWIN
6845 && cmdwin_type == 0
6846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006849 {
6850 if (anyBufIsChanged())
6851 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6852 else
6853 msg(_("Type :qa and press <Enter> to exit Vim"));
6854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
6857 * set again below when halfway a mapping. */
6858 if (!p_im)
6859 restart_edit = 0;
6860#ifdef FEAT_CMDWIN
6861 if (cmdwin_type != 0)
6862 {
6863 cmdwin_result = K_IGNORE;
6864 got_int = FALSE; /* don't stop executing autocommands et al. */
6865 return;
6866 }
6867#endif
6868 }
6869
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 if (VIsual_active)
6871 {
6872 end_visual_mode(); /* stop Visual */
6873 check_cursor_col(); /* make sure cursor is not beyond EOL */
6874 curwin->w_set_curswant = TRUE;
6875 redraw_curbuf_later(INVERTED);
6876 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006877 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006878 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 clearop(cap->oap);
6880
6881 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
6882 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01006883 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 restart_edit = 'a';
6885}
6886
6887/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006888 * Move the cursor for the "A" command.
6889 */
6890 void
6891set_cursor_for_append_to_line(void)
6892{
6893 curwin->w_set_curswant = TRUE;
6894 if (ve_flags == VE_ALL)
6895 {
6896 int save_State = State;
6897
6898 /* Pretend Insert mode here to allow the cursor on the
6899 * character past the end of the line */
6900 State = INSERT;
6901 coladvance((colnr_T)MAXCOL);
6902 State = save_State;
6903 }
6904 else
6905 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6906}
6907
6908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006910 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 */
6912 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006913nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 /* <Insert> is equal to "i" */
6916 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6917 cap->cmdchar = 'i';
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 /* in Visual mode "A" and "I" are an operator */
6920 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006921 {
6922#ifdef FEAT_TERMINAL
6923 if (term_in_normal_mode())
6924 {
6925 end_visual_mode();
6926 clearop(cap->oap);
6927 term_enter_job_mode();
6928 return;
6929 }
6930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
6934 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006935 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6936 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {
6938#ifdef FEAT_TEXTOBJ
6939 nv_object(cap);
6940#else
6941 clearopbeep(cap->oap);
6942#endif
6943 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006944#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006945 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006946 {
6947 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006948 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006949 return;
6950 }
6951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 else if (!curbuf->b_p_ma && !p_im)
6953 {
6954 /* Only give this error when 'insertmode' is off. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006955 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006957 if (cap->cmdchar == K_PS)
6958 /* drop the pasted text */
6959 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006961 else if (cap->cmdchar == K_PS && VIsual_active)
6962 {
6963 pos_T old_pos = curwin->w_cursor;
6964 pos_T old_visual = VIsual;
6965
6966 /* In Visual mode the selected text is deleted. */
6967 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6968 {
6969 shift_delete_registers();
6970 cap->oap->regname = '1';
6971 }
6972 else
6973 cap->oap->regname = '-';
6974 cap->cmdchar = 'd';
6975 cap->nchar = NUL;
6976 nv_operator(cap);
6977 do_pending_operator(cap, 0, FALSE);
6978 cap->cmdchar = K_PS;
6979
6980 /* When the last char in the line was deleted then append. Detect this
6981 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006982 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
6983 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01006984 inc_cursor();
6985
6986 /* Insert to replace the deleted text with the pasted text. */
6987 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 else if (!checkclearopq(cap->oap))
6990 {
6991 switch (cap->cmdchar)
6992 {
6993 case 'A': /* "A"ppend after the line */
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006994 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 break;
6996
6997 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006998 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6999 beginline(BL_WHITE);
7000 else
7001 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 break;
7003
Bram Moolenaara1891842017-02-04 21:34:31 +01007004 case K_PS:
7005 /* Bracketed paste works like "a"ppend, unless the cursor is in
7006 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007007 if (curwin->w_cursor.col == 0)
7008 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007009 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007010
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 case 'a': /* "a"ppend is like "i"nsert on the next character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 /* increment coladd when in virtual space, increment the
7013 * column otherwise, also to append after an unprintable char */
7014 if (virtual_active()
7015 && (curwin->w_cursor.coladd > 0
7016 || *ml_get_cursor() == NUL
7017 || *ml_get_cursor() == TAB))
7018 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007019 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 inc_cursor();
7021 break;
7022 }
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7025 {
7026 int save_State = State;
7027
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007028 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 * character past the end of the line */
7030 State = INSERT;
7031 coladvance(getviscol());
7032 State = save_State;
7033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7036 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007037 else if (cap->cmdchar == K_PS)
7038 /* drop the pasted text */
7039 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040}
7041
7042/*
7043 * Invoke edit() and take care of "restart_edit" and the return value.
7044 */
7045 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007046invoke_edit(
7047 cmdarg_T *cap,
7048 int repl, /* "r" or "gr" command */
7049 int cmd,
7050 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 int restart_edit_save = 0;
7053
7054 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
7055 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7056 * it. */
7057 if (repl || !stuff_empty())
7058 restart_edit_save = restart_edit;
7059 else
7060 restart_edit_save = 0;
7061
7062 /* Always reset "restart_edit", this is not a restarted edit. */
7063 restart_edit = 0;
7064
7065 if (edit(cmd, startln, cap->count1))
7066 cap->retval |= CA_COMMAND_BUSY;
7067
7068 if (restart_edit == 0)
7069 restart_edit = restart_edit_save;
7070}
7071
7072#ifdef FEAT_TEXTOBJ
7073/*
7074 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7075 */
7076 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007077nv_object(
7078 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079{
7080 int flag;
7081 int include;
7082 char_u *mps_save;
7083
7084 if (cap->cmdchar == 'i')
7085 include = FALSE; /* "ix" = inner object: exclude white space */
7086 else
7087 include = TRUE; /* "ax" = an object: include white space */
7088
7089 /* Make sure (), [], {} and <> are in 'matchpairs' */
7090 mps_save = curbuf->b_p_mps;
7091 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7092
7093 switch (cap->nchar)
7094 {
7095 case 'w': /* "aw" = a word */
7096 flag = current_word(cap->oap, cap->count1, include, FALSE);
7097 break;
7098 case 'W': /* "aW" = a WORD */
7099 flag = current_word(cap->oap, cap->count1, include, TRUE);
7100 break;
7101 case 'b': /* "ab" = a braces block */
7102 case '(':
7103 case ')':
7104 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7105 break;
7106 case 'B': /* "aB" = a Brackets block */
7107 case '{':
7108 case '}':
7109 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7110 break;
7111 case '[': /* "a[" = a [] block */
7112 case ']':
7113 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7114 break;
7115 case '<': /* "a<" = a <> block */
7116 case '>':
7117 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7118 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007119 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007120 /* Do not adjust oap->end in do_pending_operator()
7121 * otherwise there are different results for 'dit'
7122 * (note leading whitespace in last line):
7123 * 1) <b> 2) <b>
7124 * foobar foobar
7125 * </b> </b>
7126 */
7127 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007128 flag = current_tagblock(cap->oap, cap->count1, include);
7129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 case 'p': /* "ap" = a paragraph */
7131 flag = current_par(cap->oap, cap->count1, include, 'p');
7132 break;
7133 case 's': /* "as" = a sentence */
7134 flag = current_sent(cap->oap, cap->count1, include);
7135 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007136 case '"': /* "a"" = a double quoted string */
7137 case '\'': /* "a'" = a single quoted string */
7138 case '`': /* "a`" = a backtick quoted string */
7139 flag = current_quote(cap->oap, cap->count1, include,
7140 cap->nchar);
7141 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142#if 0 /* TODO */
7143 case 'S': /* "aS" = a section */
7144 case 'f': /* "af" = a filename */
7145 case 'u': /* "au" = a URL */
7146#endif
7147 default:
7148 flag = FAIL;
7149 break;
7150 }
7151
7152 curbuf->b_p_mps = mps_save;
7153 if (flag == FAIL)
7154 clearopbeep(cap->oap);
7155 adjust_cursor_col();
7156 curwin->w_set_curswant = TRUE;
7157}
7158#endif
7159
7160/*
7161 * "q" command: Start/stop recording.
7162 * "q:", "q/", "q?": edit command-line in command-line window.
7163 */
7164 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007165nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
7167 if (cap->oap->op_type == OP_FORMAT)
7168 {
7169 /* "gqq" is the same as "gqgq": format line */
7170 cap->cmdchar = 'g';
7171 cap->nchar = 'q';
7172 nv_operator(cap);
7173 }
7174 else if (!checkclearop(cap->oap))
7175 {
7176#ifdef FEAT_CMDWIN
7177 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7178 {
7179 stuffcharReadbuff(cap->nchar);
7180 stuffcharReadbuff(K_CMDWIN);
7181 }
7182 else
7183#endif
7184 /* (stop) recording into a named register, unless executing a
7185 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007186 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 clearopbeep(cap->oap);
7188 }
7189}
7190
7191/*
7192 * Handle the "@r" command.
7193 */
7194 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007195nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196{
7197 if (checkclearop(cap->oap))
7198 return;
7199#ifdef FEAT_EVAL
7200 if (cap->nchar == '=')
7201 {
7202 if (get_expr_register() == NUL)
7203 return;
7204 }
7205#endif
7206 while (cap->count1-- && !got_int)
7207 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007208 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {
7210 clearopbeep(cap->oap);
7211 break;
7212 }
7213 line_breakcheck();
7214 }
7215}
7216
7217/*
7218 * Handle the CTRL-U and CTRL-D commands.
7219 */
7220 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007221nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7224 || (cap->cmdchar == Ctrl_D
7225 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7226 clearopbeep(cap->oap);
7227 else if (!checkclearop(cap->oap))
7228 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7229}
7230
7231/*
7232 * Handle "J" or "gJ" command.
7233 */
7234 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007235nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (VIsual_active) /* join the visual lines */
7238 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007239 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {
7241 if (cap->count0 <= 1)
7242 cap->count0 = 2; /* default for join is two lines! */
7243 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7244 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007246 /* can't join when on the last line */
7247 if (cap->count0 <= 2)
7248 {
7249 clearopbeep(cap->oap);
7250 return;
7251 }
7252 cap->count0 = curbuf->b_ml.ml_line_count
7253 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007255
7256 prep_redo(cap->oap->regname, cap->count0,
7257 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7258 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260}
7261
7262/*
7263 * "P", "gP", "p" and "gp" commands.
7264 */
7265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007266nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007268 nv_put_opt(cap, FALSE);
7269}
7270
7271/*
7272 * "P", "gP", "p" and "gp" commands.
7273 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7274 */
7275 static void
7276nv_put_opt(cmdarg_T *cap, int fix_indent)
7277{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 int regname = 0;
7279 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007280 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007281 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 int dir;
7283 int flags = 0;
7284
7285 if (cap->oap->op_type != OP_NOP)
7286 {
7287#ifdef FEAT_DIFF
7288 /* "dp" is ":diffput" */
7289 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7290 {
7291 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007292 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 }
7294 else
7295#endif
7296 clearopbeep(cap->oap);
7297 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007298#ifdef FEAT_JOB_CHANNEL
7299 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7300 {
7301 clearopbeep(cap->oap);
7302 }
7303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 else
7305 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007306 if (fix_indent)
7307 {
7308 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7309 ? FORWARD : BACKWARD;
7310 flags |= PUT_FIXINDENT;
7311 }
7312 else
7313 dir = (cap->cmdchar == 'P'
7314 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 ? BACKWARD : FORWARD;
7316 prep_redo_cmd(cap);
7317 if (cap->cmdchar == 'g')
7318 flags |= PUT_CURSEND;
7319
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 if (VIsual_active)
7321 {
7322 /* Putting in Visual mode: The put text replaces the selected
7323 * text. First delete the selected text, then put the new text.
7324 * Need to save and restore the registers that the delete
7325 * overwrites if the old contents is being put.
7326 */
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 Moolenaarba6e8582012-12-12 18:20:32 +01007340 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 * put, save it first. */
7342 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
7355 /* delete PUT_LINE_BACKWARD; */
7356 cap->oap->regname = regname;
7357
7358 if (reg1 != NULL)
7359 {
7360 /* Delete probably changed the register we want to put, save
7361 * it first. Then put back what was there before the delete. */
7362 reg2 = get_register(regname, FALSE);
7363 put_register(regname, reg1);
7364 }
7365
7366 /* 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. */
7369 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))
7380 /* cursor is at the end of the line or end of file, put
7381 * forward. */
7382 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007383 /* May have been reset in do_put(). */
7384 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 Moolenaar071d4272004-06-13 20:20:40 +00007388 /* If a register was saved, put it back now. */
7389 if (reg2 != NULL)
7390 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007391
7392 /* What to reselect with "gv"? Selecting the just put text seems to
7393 * be the most useful, since the original text was removed. */
7394 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 Moolenaard29c6fe2015-11-19 20:11:54 +01007398 /* need to adjust cursor position */
7399 if (*p_sel == 'e')
7400 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007401 }
7402
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007403 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007404 * 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 Moolenaarcf3630f2005-01-08 16:04:29 +00007407 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
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
7410 /* If the cursor was in that line, move it to the end of the last
7411 * line. */
7412 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
7429 /* "do" is ":diffget" */
7430 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 Moolenaar071d4272004-06-13 20:20:40 +00007437 if (VIsual_active) /* switch start and end of visual */
7438 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 Moolenaarfc735152005-03-22 22:54:12 +00007473 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007474}