blob: 1fddeae07a1c8028eb74ca91b65ce9359f251bd1 [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;
2502 if (curwin->w_curswant > (colnr_T)n + 1)
2503 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
2504 * width2;
2505 }
2506
2507 while (dist--)
2508 {
2509 if (dir == BACKWARD)
2510 {
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02002511 if ((long)curwin->w_curswant > width2)
2512 // move back within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 curwin->w_curswant -= width2;
2514 else
2515 {
2516 /* to previous line */
2517 if (curwin->w_cursor.lnum == 1)
2518 {
2519 retval = FAIL;
2520 break;
2521 }
2522 --curwin->w_cursor.lnum;
2523#ifdef FEAT_FOLDING
2524 /* Move to the start of a closed fold. Don't do that when
2525 * 'foldopen' contains "all": it will open in a moment. */
2526 if (!(fdo_flags & FDO_ALL))
2527 (void)hasFolding(curwin->w_cursor.lnum,
2528 &curwin->w_cursor.lnum, NULL);
2529#endif
2530 linelen = linetabsize(ml_get_curline());
2531 if (linelen > width1)
2532 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2533 + 1) * width2;
2534 }
2535 }
2536 else /* dir == FORWARD */
2537 {
2538 if (linelen > width1)
2539 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2540 else
2541 n = width1;
2542 if (curwin->w_curswant + width2 < (colnr_T)n)
2543 /* move forward within line */
2544 curwin->w_curswant += width2;
2545 else
2546 {
2547 /* to next line */
2548#ifdef FEAT_FOLDING
2549 /* Move to the end of a closed fold. */
2550 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2551 &curwin->w_cursor.lnum);
2552#endif
2553 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2554 {
2555 retval = FAIL;
2556 break;
2557 }
2558 curwin->w_cursor.lnum++;
2559 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002560 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562 }
2563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002566 if (virtual_active() && atend)
2567 coladvance(MAXCOL);
2568 else
2569 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2572 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002573 colnr_T virtcol;
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 /*
2576 * Check for landing on a character that got split at the end of the
2577 * last line. We want to advance a screenline, not end up in the same
2578 * screenline or move two screenlines.
2579 */
2580 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002581 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002582#if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02002583 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
2584 virtcol -= vim_strsize(p_sbr);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002585#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002586
2587 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 && (curwin->w_curswant < (colnr_T)width1
2589 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2590 : ((curwin->w_curswant - width1) % width2
2591 > (colnr_T)width2 / 2)))
2592 --curwin->w_cursor.col;
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
2595 if (atend)
2596 curwin->w_curswant = MAXCOL; /* stick in the last column */
2597
2598 return retval;
2599}
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601/*
2602 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2603 * cap->arg must be TRUE for CTRL-E.
2604 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002605 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002606nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 if (!checkclearop(cap->oap))
2609 scroll_redraw(cap->arg, cap->count1);
2610}
2611
2612/*
2613 * Scroll "count" lines up or down, and redraw.
2614 */
2615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002616scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 linenr_T prev_topline = curwin->w_topline;
2619#ifdef FEAT_DIFF
2620 int prev_topfill = curwin->w_topfill;
2621#endif
2622 linenr_T prev_lnum = curwin->w_cursor.lnum;
2623
2624 if (up)
2625 scrollup(count, TRUE);
2626 else
2627 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002628 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
2630 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
2631 * valid, otherwise the screen jumps back at the end of the file. */
2632 cursor_correct();
2633 check_cursor_moved(curwin);
2634 curwin->w_valid |= VALID_TOPLINE;
2635
2636 /* If moved back to where we were, at least move the cursor, otherwise
2637 * we get stuck at one position. Don't move the cursor up if the
2638 * first line of the buffer is already on the screen */
2639 while (curwin->w_topline == prev_topline
2640#ifdef FEAT_DIFF
2641 && curwin->w_topfill == prev_topfill
2642#endif
2643 )
2644 {
2645 if (up)
2646 {
2647 if (curwin->w_cursor.lnum > prev_lnum
2648 || cursor_down(1L, FALSE) == FAIL)
2649 break;
2650 }
2651 else
2652 {
2653 if (curwin->w_cursor.lnum < prev_lnum
2654 || prev_topline == 1L
2655 || cursor_up(1L, FALSE) == FAIL)
2656 break;
2657 }
2658 /* Mark w_topline as valid, otherwise the screen jumps back at the
2659 * end of the file. */
2660 check_cursor_moved(curwin);
2661 curwin->w_valid |= VALID_TOPLINE;
2662 }
2663 }
2664 if (curwin->w_cursor.lnum != prev_lnum)
2665 coladvance(curwin->w_curswant);
2666 redraw_later(VALID);
2667}
2668
2669/*
2670 * Commands that start with "z".
2671 */
2672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002673nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 long n;
2676 colnr_T col;
2677 int nchar = cap->nchar;
2678#ifdef FEAT_FOLDING
2679 long old_fdl = curwin->w_p_fdl;
2680 int old_fen = curwin->w_p_fen;
2681#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002682#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002683 int undo = FALSE;
2684#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002685 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 if (VIM_ISDIGIT(nchar))
2688 {
2689 /*
2690 * "z123{nchar}": edit the count before obtaining {nchar}
2691 */
2692 if (checkclearop(cap->oap))
2693 return;
2694 n = nchar - '0';
2695 for (;;)
2696 {
2697#ifdef USE_ON_FLY_SCROLL
2698 dont_scroll = TRUE; /* disallow scrolling here */
2699#endif
2700 ++no_mapping;
2701 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002702 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 --no_mapping;
2705 --allow_keys;
2706#ifdef FEAT_CMDL_INFO
2707 (void)add_to_showcmd(nchar);
2708#endif
2709 if (nchar == K_DEL || nchar == K_KDEL)
2710 n /= 10;
2711 else if (VIM_ISDIGIT(nchar))
2712 n = n * 10 + (nchar - '0');
2713 else if (nchar == CAR)
2714 {
2715#ifdef FEAT_GUI
2716 need_mouse_correct = TRUE;
2717#endif
2718 win_setheight((int)n);
2719 break;
2720 }
2721 else if (nchar == 'l'
2722 || nchar == 'h'
2723 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002724 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
2726 cap->count1 = n ? n * cap->count1 : cap->count1;
2727 goto dozet;
2728 }
2729 else
2730 {
2731 clearopbeep(cap->oap);
2732 break;
2733 }
2734 }
2735 cap->oap->op_type = OP_NOP;
2736 return;
2737 }
2738
2739dozet:
2740 if (
2741#ifdef FEAT_FOLDING
2742 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2743 * and "zC" only in Visual mode. "zj" and "zk" are motion
2744 * commands. */
2745 cap->nchar != 'f' && cap->nchar != 'F'
2746 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2747 && cap->nchar != 'j' && cap->nchar != 'k'
2748 &&
2749#endif
2750 checkclearop(cap->oap))
2751 return;
2752
2753 /*
2754 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2755 * If line number given, set cursor.
2756 */
2757 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2758 && cap->count0
2759 && cap->count0 != curwin->w_cursor.lnum)
2760 {
2761 setpcmark();
2762 if (cap->count0 > curbuf->b_ml.ml_line_count)
2763 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2764 else
2765 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002766 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
2768
2769 switch (nchar)
2770 {
2771 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
2772 case '+':
2773 if (cap->count0 == 0)
2774 {
2775 /* No count given: put cursor at the line below screen */
2776 validate_botline(); /* make sure w_botline is valid */
2777 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2778 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2779 else
2780 curwin->w_cursor.lnum = curwin->w_botline;
2781 }
2782 /* FALLTHROUGH */
2783 case NL:
2784 case CAR:
2785 case K_KENTER:
2786 beginline(BL_WHITE | BL_FIX);
2787 /* FALLTHROUGH */
2788
2789 case 't': scroll_cursor_top(0, TRUE);
2790 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002791 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 break;
2793
2794 /* "z." and "zz": put cursor in middle of screen */
2795 case '.': beginline(BL_WHITE | BL_FIX);
2796 /* FALLTHROUGH */
2797
2798 case 'z': scroll_cursor_halfway(TRUE);
2799 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002800 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 break;
2802
2803 /* "z^", "z-" and "zb": put cursor at bottom of screen */
2804 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
2805 * when <count> is at bottom of window, and puts that one at
2806 * bottom of window. */
2807 if (cap->count0 != 0)
2808 {
2809 scroll_cursor_bot(0, TRUE);
2810 curwin->w_cursor.lnum = curwin->w_topline;
2811 }
2812 else if (curwin->w_topline == 1)
2813 curwin->w_cursor.lnum = 1;
2814 else
2815 curwin->w_cursor.lnum = curwin->w_topline - 1;
2816 /* FALLTHROUGH */
2817 case '-':
2818 beginline(BL_WHITE | BL_FIX);
2819 /* FALLTHROUGH */
2820
2821 case 'b': scroll_cursor_bot(0, TRUE);
2822 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002823 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 break;
2825
2826 /* "zH" - scroll screen right half-page */
2827 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002828 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 /* FALLTHROUGH */
2830
2831 /* "zh" - scroll screen to the right */
2832 case 'h':
2833 case K_LEFT:
2834 if (!curwin->w_p_wrap)
2835 {
2836 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2837 curwin->w_leftcol = 0;
2838 else
2839 curwin->w_leftcol -= (colnr_T)cap->count1;
2840 leftcol_changed();
2841 }
2842 break;
2843
2844 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02002845 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 /* FALLTHROUGH */
2847
2848 /* "zl" - scroll screen to the left */
2849 case 'l':
2850 case K_RIGHT:
2851 if (!curwin->w_p_wrap)
2852 {
2853 /* scroll the window left */
2854 curwin->w_leftcol += (colnr_T)cap->count1;
2855 leftcol_changed();
2856 }
2857 break;
2858
2859 /* "zs" - scroll screen, cursor at the start */
2860 case 's': if (!curwin->w_p_wrap)
2861 {
2862#ifdef FEAT_FOLDING
2863 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2864 col = 0; /* like the cursor is in col 0 */
2865 else
2866#endif
2867 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002868 if ((long)col > siso)
2869 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 else
2871 col = 0;
2872 if (curwin->w_leftcol != col)
2873 {
2874 curwin->w_leftcol = col;
2875 redraw_later(NOT_VALID);
2876 }
2877 }
2878 break;
2879
2880 /* "ze" - scroll screen, cursor at the end */
2881 case 'e': if (!curwin->w_p_wrap)
2882 {
2883#ifdef FEAT_FOLDING
2884 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2885 col = 0; /* like the cursor is in col 0 */
2886 else
2887#endif
2888 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002889 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002890 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 col = 0;
2892 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002893 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 if (curwin->w_leftcol != col)
2895 {
2896 curwin->w_leftcol = col;
2897 redraw_later(NOT_VALID);
2898 }
2899 }
2900 break;
2901
2902#ifdef FEAT_FOLDING
2903 /* "zF": create fold command */
2904 /* "zf": create fold operator */
2905 case 'F':
2906 case 'f': if (foldManualAllowed(TRUE))
2907 {
2908 cap->nchar = 'f';
2909 nv_operator(cap);
2910 curwin->w_p_fen = TRUE;
2911
2912 /* "zF" is like "zfzf" */
2913 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2914 {
2915 nv_operator(cap);
2916 finish_op = TRUE;
2917 }
2918 }
2919 else
2920 clearopbeep(cap->oap);
2921 break;
2922
2923 /* "zd": delete fold at cursor */
2924 /* "zD": delete fold at cursor recursively */
2925 case 'd':
2926 case 'D': if (foldManualAllowed(FALSE))
2927 {
2928 if (VIsual_active)
2929 nv_operator(cap);
2930 else
2931 deleteFold(curwin->w_cursor.lnum,
2932 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2933 }
2934 break;
2935
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002936 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 case 'E': if (foldmethodIsManual(curwin))
2938 {
2939 clearFolding(curwin);
2940 changed_window_setting();
2941 }
2942 else if (foldmethodIsMarker(curwin))
2943 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2944 TRUE, FALSE);
2945 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002946 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 break;
2948
2949 /* "zn": fold none: reset 'foldenable' */
2950 case 'n': curwin->w_p_fen = FALSE;
2951 break;
2952
2953 /* "zN": fold Normal: set 'foldenable' */
2954 case 'N': curwin->w_p_fen = TRUE;
2955 break;
2956
2957 /* "zi": invert folding: toggle 'foldenable' */
2958 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2959 break;
2960
2961 /* "za": open closed fold or close open fold at cursor */
2962 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2963 openFold(curwin->w_cursor.lnum, cap->count1);
2964 else
2965 {
2966 closeFold(curwin->w_cursor.lnum, cap->count1);
2967 curwin->w_p_fen = TRUE;
2968 }
2969 break;
2970
2971 /* "zA": open fold at cursor recursively */
2972 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2973 openFoldRecurse(curwin->w_cursor.lnum);
2974 else
2975 {
2976 closeFoldRecurse(curwin->w_cursor.lnum);
2977 curwin->w_p_fen = TRUE;
2978 }
2979 break;
2980
2981 /* "zo": open fold at cursor or Visual area */
2982 case 'o': if (VIsual_active)
2983 nv_operator(cap);
2984 else
2985 openFold(curwin->w_cursor.lnum, cap->count1);
2986 break;
2987
2988 /* "zO": open fold recursively */
2989 case 'O': if (VIsual_active)
2990 nv_operator(cap);
2991 else
2992 openFoldRecurse(curwin->w_cursor.lnum);
2993 break;
2994
2995 /* "zc": close fold at cursor or Visual area */
2996 case 'c': if (VIsual_active)
2997 nv_operator(cap);
2998 else
2999 closeFold(curwin->w_cursor.lnum, cap->count1);
3000 curwin->w_p_fen = TRUE;
3001 break;
3002
3003 /* "zC": close fold recursively */
3004 case 'C': if (VIsual_active)
3005 nv_operator(cap);
3006 else
3007 closeFoldRecurse(curwin->w_cursor.lnum);
3008 curwin->w_p_fen = TRUE;
3009 break;
3010
3011 /* "zv": open folds at the cursor */
3012 case 'v': foldOpenCursor();
3013 break;
3014
3015 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
3016 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02003017 curwin->w_foldinvalid = TRUE; /* recompute folds */
3018 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 foldOpenCursor();
3020 break;
3021
3022 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
3023 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02003024 curwin->w_foldinvalid = TRUE; /* recompute folds */
3025 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 break;
3027
3028 /* "zm": fold more */
3029 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003030 {
3031 curwin->w_p_fdl -= cap->count1;
3032 if (curwin->w_p_fdl < 0)
3033 curwin->w_p_fdl = 0;
3034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 old_fdl = -1; /* force an update */
3036 curwin->w_p_fen = TRUE;
3037 break;
3038
3039 /* "zM": close all folds */
3040 case 'M': curwin->w_p_fdl = 0;
3041 old_fdl = -1; /* force an update */
3042 curwin->w_p_fen = TRUE;
3043 break;
3044
3045 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003046 case 'r': curwin->w_p_fdl += cap->count1;
3047 {
3048 int d = getDeepestNesting();
3049
3050 if (curwin->w_p_fdl >= d)
3051 curwin->w_p_fdl = d;
3052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 break;
3054
3055 /* "zR": open all folds */
3056 case 'R': curwin->w_p_fdl = getDeepestNesting();
3057 old_fdl = -1; /* force an update */
3058 break;
3059
3060 case 'j': /* "zj" move to next fold downwards */
3061 case 'k': /* "zk" move to next fold upwards */
3062 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3063 cap->count1) == FAIL)
3064 clearopbeep(cap->oap);
3065 break;
3066
3067#endif /* FEAT_FOLDING */
3068
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003069#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00003070 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
3071 ++no_mapping;
3072 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003073 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003074 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003075 --no_mapping;
3076 --allow_keys;
3077#ifdef FEAT_CMDL_INFO
3078 (void)add_to_showcmd(nchar);
3079#endif
3080 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3081 {
3082 clearopbeep(cap->oap);
3083 break;
3084 }
3085 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003086 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00003087
Bram Moolenaarb765d632005-06-07 21:00:02 +00003088 case 'g': /* "zg": add good word to word list */
3089 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00003090 case 'G': /* "zG": add good word to temp word list */
3091 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003092 {
3093 char_u *ptr = NULL;
3094 int len;
3095
3096 if (checkclearop(cap->oap))
3097 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003098 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3099 == FAIL)
3100 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003101 if (ptr == NULL)
3102 {
3103 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003104
Bram Moolenaar134bf072013-09-25 18:54:24 +02003105 /* Find bad word under the cursor. When 'spell' is
3106 * off this fails and find_ident_under_cursor() is
3107 * used below. */
3108 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003109 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003110 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003111 if (len != 0 && curwin->w_cursor.col <= pos.col)
3112 ptr = ml_get_pos(&curwin->w_cursor);
3113 curwin->w_cursor = pos;
3114 }
3115
Bram Moolenaarb765d632005-06-07 21:00:02 +00003116 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3117 FIND_IDENT)) == 0)
3118 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003119 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3120 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003121 (nchar == 'G' || nchar == 'W')
3122 ? 0 : (int)cap->count1,
3123 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003124 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003125 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003126
Bram Moolenaar43abc522005-12-10 20:15:02 +00003127 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003128 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003129 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003130 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003131#endif
3132
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 default: clearopbeep(cap->oap);
3134 }
3135
3136#ifdef FEAT_FOLDING
3137 /* Redraw when 'foldenable' changed */
3138 if (old_fen != curwin->w_p_fen)
3139 {
3140# ifdef FEAT_DIFF
3141 win_T *wp;
3142
3143 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3144 {
3145 /* Adjust 'foldenable' in diff-synced windows. */
3146 FOR_ALL_WINDOWS(wp)
3147 {
3148 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3149 {
3150 wp->w_p_fen = curwin->w_p_fen;
3151 changed_window_setting_win(wp);
3152 }
3153 }
3154 }
3155# endif
3156 changed_window_setting();
3157 }
3158
3159 /* Redraw when 'foldlevel' changed. */
3160 if (old_fdl != curwin->w_p_fdl)
3161 newFoldLevel();
3162#endif
3163}
3164
3165#ifdef FEAT_GUI
3166/*
3167 * Vertical scrollbar movement.
3168 */
3169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003170nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 if (cap->oap->op_type != OP_NOP)
3173 clearopbeep(cap->oap);
3174
3175 /* Even if an operator was pending, we still want to scroll */
3176 gui_do_scroll();
3177}
3178
3179/*
3180 * Horizontal scrollbar movement.
3181 */
3182 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003183nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 if (cap->oap->op_type != OP_NOP)
3186 clearopbeep(cap->oap);
3187
3188 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003189 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190}
3191#endif
3192
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003193#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003194/*
3195 * Click in GUI tab.
3196 */
3197 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003198nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003199{
3200 if (cap->oap->op_type != OP_NOP)
3201 clearopbeep(cap->oap);
3202
3203 /* Even if an operator was pending, we still want to jump tabs. */
3204 goto_tabpage(current_tab);
3205}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003206
3207/*
3208 * Selected item in tab line menu.
3209 */
3210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003211nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003212{
3213 if (cap->oap->op_type != OP_NOP)
3214 clearopbeep(cap->oap);
3215
3216 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003217 handle_tabmenu();
3218}
3219
3220/*
3221 * Handle selecting an item of the GUI tab line menu.
3222 * Used in Normal and Insert mode.
3223 */
3224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003225handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003226{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003227 switch (current_tabmenu)
3228 {
3229 case TABLINE_MENU_CLOSE:
3230 if (current_tab == 0)
3231 do_cmdline_cmd((char_u *)"tabclose");
3232 else
3233 {
3234 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3235 current_tab);
3236 do_cmdline_cmd(IObuff);
3237 }
3238 break;
3239
3240 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003241 if (current_tab == 0)
3242 do_cmdline_cmd((char_u *)"$tabnew");
3243 else
3244 {
3245 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3246 current_tab - 1);
3247 do_cmdline_cmd(IObuff);
3248 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003249 break;
3250
3251 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003252 if (current_tab == 0)
3253 do_cmdline_cmd((char_u *)"browse $tabnew");
3254 else
3255 {
3256 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3257 current_tab - 1);
3258 do_cmdline_cmd(IObuff);
3259 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003260 break;
3261 }
3262}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003263#endif
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265/*
3266 * "Q" command.
3267 */
3268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003269nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 /*
3272 * Ignore 'Q' in Visual mode, just give a beep.
3273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003275 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003276 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 do_exmode(FALSE);
3278}
3279
3280/*
3281 * Handle a ":" command.
3282 */
3283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003284nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
3286 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003287 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (VIsual_active)
3290 nv_operator(cap);
3291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 if (cap->oap->op_type != OP_NOP)
3294 {
3295 /* Using ":" as a movement is characterwise exclusive. */
3296 cap->oap->motion_type = MCHAR;
3297 cap->oap->inclusive = FALSE;
3298 }
3299 else if (cap->count0)
3300 {
3301 /* translate "count:" into ":.,.+(count - 1)" */
3302 stuffcharReadbuff('.');
3303 if (cap->count0 > 1)
3304 {
3305 stuffReadbuff((char_u *)",.+");
3306 stuffnumReadbuff((long)cap->count0 - 1L);
3307 }
3308 }
3309
3310 /* When typing, don't type below an old message */
3311 if (KeyTyped)
3312 compute_cmdrow();
3313
3314 old_p_im = p_im;
3315
3316 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003317 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3319
3320 /* If 'insertmode' changed, enter or exit Insert mode */
3321 if (p_im != old_p_im)
3322 {
3323 if (p_im)
3324 restart_edit = 'i';
3325 else
3326 restart_edit = 0;
3327 }
3328
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003329 if (cmd_result == FAIL)
3330 /* The Ex command failed, do not execute the operator. */
3331 clearop(cap->oap);
3332 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3334 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003335 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3336 || did_emsg
3337 ))
3338 /* The start of the operator has become invalid by the Ex command.
3339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 clearopbeep(cap->oap);
3341 }
3342}
3343
3344/*
3345 * Handle CTRL-G command.
3346 */
3347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003348nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 if (VIsual_active) /* toggle Selection/Visual mode */
3351 {
3352 VIsual_select = !VIsual_select;
3353 showmode();
3354 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003355 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 /* print full name if count given or :cd used */
3357 fileinfo((int)cap->count0, FALSE, TRUE);
3358}
3359
3360/*
3361 * Handle CTRL-H <Backspace> command.
3362 */
3363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003364nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if (VIsual_active && VIsual_select)
3367 {
3368 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
3369 v_visop(cap);
3370 }
3371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 nv_left(cap);
3373}
3374
3375/*
3376 * CTRL-L: clear screen and redraw.
3377 */
3378 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003379nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
3381 if (!checkclearop(cap->oap))
3382 {
3383#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
3384 /*
3385 * Right now, the BeBox doesn't seem to have an easy way to detect
3386 * window resizing, so we cheat and make the user detect it
3387 * manually with CTRL-L instead
3388 */
3389 ui_get_shellsize();
3390#endif
3391#ifdef FEAT_SYN_HL
3392 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02003393 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003394# ifdef FEAT_RELTIME
3395 {
3396 win_T *wp;
3397
3398 FOR_ALL_WINDOWS(wp)
3399 wp->w_s->b_syn_slow = FALSE;
3400 }
3401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402#endif
3403 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003404#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3405# ifdef VIMDLL
3406 if (!gui.in_use)
3407# endif
3408 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411}
3412
3413/*
3414 * CTRL-O: In Select mode: switch to Visual mode for one command.
3415 * Otherwise: Go to older pcmark.
3416 */
3417 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003418nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (VIsual_active && VIsual_select)
3421 {
3422 VIsual_select = FALSE;
3423 showmode();
3424 restart_VIsual_select = 2; /* restart Select mode later */
3425 }
3426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 {
3428 cap->count1 = -cap->count1;
3429 nv_pcmark(cap);
3430 }
3431}
3432
3433/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003434 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3435 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 */
3437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003438nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 if (!checkclearopq(cap->oap))
3441 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3442 GETF_SETMARK|GETF_ALT, FALSE);
3443}
3444
3445/*
3446 * "Z" commands.
3447 */
3448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003449nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 if (!checkclearopq(cap->oap))
3452 {
3453 switch (cap->nchar)
3454 {
3455 /* "ZZ": equivalent to ":x". */
3456 case 'Z': do_cmdline_cmd((char_u *)"x");
3457 break;
3458
3459 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
3460 case 'Q': do_cmdline_cmd((char_u *)"q!");
3461 break;
3462
3463 default: clearopbeep(cap->oap);
3464 }
3465 }
3466}
3467
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468/*
3469 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3470 */
3471 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003472do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 oparg_T oa;
3475 cmdarg_T ca;
3476
3477 clear_oparg(&oa);
3478 vim_memset(&ca, 0, sizeof(ca));
3479 ca.oap = &oa;
3480 ca.cmdchar = c1;
3481 ca.nchar = c2;
3482 nv_ident(&ca);
3483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
3485/*
3486 * Handle the commands that use the word under the cursor.
3487 * [g] CTRL-] :ta to current identifier
3488 * [g] 'K' run program for current identifier
3489 * [g] '*' / to current identifier or string
3490 * [g] '#' ? to current identifier or string
3491 * g ']' :tselect for current identifier
3492 */
3493 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003494nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 char_u *ptr = NULL;
3497 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003498 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003499 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 char_u *p;
3501 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003502 int kp_help; /* 'keywordprg' is ":he" */
3503 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 int n = 0; /* init for GCC */
3505 int cmdchar;
3506 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003507 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 char_u *aux_ptr;
3509 int isman;
3510 int isman_s;
3511
3512 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
3513 {
3514 cmdchar = cap->nchar;
3515 g_cmd = TRUE;
3516 }
3517 else
3518 {
3519 cmdchar = cap->cmdchar;
3520 g_cmd = FALSE;
3521 }
3522
3523 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
3524 cmdchar = '#';
3525
3526 /*
3527 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3528 */
3529 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3530 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3532 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (checkclearopq(cap->oap))
3534 return;
3535 }
3536
3537 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3538 (cmdchar == '*' || cmdchar == '#')
3539 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3540 {
3541 clearop(cap->oap);
3542 return;
3543 }
3544
3545 /* Allocate buffer to put the command in. Inserting backslashes can
3546 * double the length of the word. p_kp / curbuf->b_p_kp could be added
3547 * and some numbers. */
3548 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3549 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3550 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003551 if (kp_help && *skipwhite(ptr) == NUL)
3552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003553 emsg(_(e_noident)); /* found white space only */
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003554 return;
3555 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003556 kp_ex = (*kp == ':');
3557 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3558 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 if (buf == NULL)
3560 return;
3561 buf[0] = NUL;
3562
3563 switch (cmdchar)
3564 {
3565 case '*':
3566 case '#':
3567 /*
3568 * Put cursor at start of word, makes search skip the word
3569 * under the cursor.
3570 * Call setpcmark() first, so "*``" puts the cursor back where
3571 * it was.
3572 */
3573 setpcmark();
3574 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3575
3576 if (!g_cmd && vim_iswordp(ptr))
3577 STRCPY(buf, "\\<");
3578 no_smartcase = TRUE; /* don't use 'smartcase' now */
3579 break;
3580
3581 case 'K':
3582 if (kp_help)
3583 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003584 else if (kp_ex)
3585 {
3586 if (cap->count0 != 0)
3587 vim_snprintf((char *)buf, buflen, "%s %ld",
3588 kp, cap->count0);
3589 else
3590 STRCPY(buf, kp);
3591 STRCAT(buf, " ");
3592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 else
3594 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003595 /* An external command will probably use an argument starting
3596 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003597 while (*ptr == '-' && n > 0)
3598 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003599 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003600 --n;
3601 }
3602 if (n == 0)
3603 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003604 emsg(_(e_noident)); /* found dashes only */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003605 vim_free(buf);
3606 return;
3607 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003608
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 /* When a count is given, turn it into a range. Is this
3610 * really what we want? */
3611 isman = (STRCMP(kp, "man") == 0);
3612 isman_s = (STRCMP(kp, "man -s") == 0);
3613 if (cap->count0 != 0 && !(isman || isman_s))
3614 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3615
3616 STRCAT(buf, "! ");
3617 if (cap->count0 == 0 && isman_s)
3618 STRCAT(buf, "man");
3619 else
3620 STRCAT(buf, kp);
3621 STRCAT(buf, " ");
3622 if (cap->count0 != 0 && (isman || isman_s))
3623 {
3624 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3625 STRCAT(buf, " ");
3626 }
3627 }
3628 break;
3629
3630 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003631 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632#ifdef FEAT_CSCOPE
3633 if (p_cst)
3634 STRCPY(buf, "cstag ");
3635 else
3636#endif
3637 STRCPY(buf, "ts ");
3638 break;
3639
3640 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003641 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if (curbuf->b_help)
3643 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003645 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003646 if (g_cmd)
3647 STRCPY(buf, "tj ");
3648 else
3649 sprintf((char *)buf, "%ldta ", cap->count0);
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652
3653 /*
3654 * Now grab the chars in the identifier
3655 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003656 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003658 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003659 if (kp_ex)
3660 /* Escape the argument properly for an Ex command */
3661 p = vim_strsave_fnameescape(ptr, FALSE);
3662 else
3663 /* Escape the argument properly for a shell command */
3664 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003665 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003666 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003668 vim_free(buf);
3669 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003671 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003672 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003673 {
3674 vim_free(buf);
3675 vim_free(p);
3676 return;
3677 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003678 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003679 STRCAT(buf, p);
3680 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003682 else
3683 {
3684 if (cmdchar == '*')
3685 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
3686 else if (cmdchar == '#')
3687 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003688 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003689 {
3690 if (curbuf->b_help)
3691 /* ":help" handles unescaped argument */
3692 aux_ptr = (char_u *)"";
3693 else
3694 aux_ptr = (char_u *)"\\|\"\n[";
3695 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003696 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003697 aux_ptr = (char_u *)"\\|\"\n*?[";
3698
3699 p = buf + STRLEN(buf);
3700 while (n-- > 0)
3701 {
3702 /* put a backslash before \ and some others */
3703 if (vim_strchr(aux_ptr, *ptr) != NULL)
3704 *p++ = '\\';
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003705 /* When current byte is a part of multibyte character, copy all
3706 * bytes of that character. */
3707 if (has_mbyte)
3708 {
3709 int i;
3710 int len = (*mb_ptr2len)(ptr) - 1;
3711
3712 for (i = 0; i < len && n >= 1; ++i, --n)
3713 *p++ = *ptr++;
3714 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003715 *p++ = *ptr++;
3716 }
3717 *p = NUL;
3718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
3720 /*
3721 * Execute the command.
3722 */
3723 if (cmdchar == '*' || cmdchar == '#')
3724 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003725 if (!g_cmd && (has_mbyte
3726 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3727 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003729
3730 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003731 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003733
Bram Moolenaar46539112015-02-17 15:43:57 +01003734 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003737 {
3738 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003740 g_tag_at_cursor = FALSE;
3741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
3743 vim_free(buf);
3744}
3745
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746/*
3747 * Get visually selected text, within one line only.
3748 * Returns FAIL if more than one line selected.
3749 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003750 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003751get_visual_text(
3752 cmdarg_T *cap,
3753 char_u **pp, /* return: start of selected text */
3754 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755{
3756 if (VIsual_mode != 'V')
3757 unadjust_for_sel();
3758 if (VIsual.lnum != curwin->w_cursor.lnum)
3759 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003760 if (cap != NULL)
3761 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 return FAIL;
3763 }
3764 if (VIsual_mode == 'V')
3765 {
3766 *pp = ml_get_curline();
3767 *lenp = (int)STRLEN(*pp);
3768 }
3769 else
3770 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003771 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
3773 *pp = ml_get_pos(&curwin->w_cursor);
3774 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3775 }
3776 else
3777 {
3778 *pp = ml_get_pos(&VIsual);
3779 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (has_mbyte)
3782 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003783 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
3785 reset_VIsual_and_resel();
3786 return OK;
3787}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
3789/*
3790 * CTRL-T: backwards in tag stack
3791 */
3792 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003793nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794{
3795 if (!checkclearopq(cap->oap))
3796 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3797}
3798
3799/*
3800 * Handle scrolling command 'H', 'L' and 'M'.
3801 */
3802 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003803nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
3805 int used = 0;
3806 long n;
3807#ifdef FEAT_FOLDING
3808 linenr_T lnum;
3809#endif
3810 int half;
3811
3812 cap->oap->motion_type = MLINE;
3813 setpcmark();
3814
3815 if (cap->cmdchar == 'L')
3816 {
3817 validate_botline(); /* make sure curwin->w_botline is valid */
3818 curwin->w_cursor.lnum = curwin->w_botline - 1;
3819 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3820 curwin->w_cursor.lnum = 1;
3821 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003822 {
3823#ifdef FEAT_FOLDING
3824 if (hasAnyFolding(curwin))
3825 {
3826 /* Count a fold for one screen line. */
3827 for (n = cap->count1 - 1; n > 0
3828 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3829 {
3830 (void)hasFolding(curwin->w_cursor.lnum,
3831 &curwin->w_cursor.lnum, NULL);
3832 --curwin->w_cursor.lnum;
3833 }
3834 }
3835 else
3836#endif
3837 curwin->w_cursor.lnum -= cap->count1 - 1;
3838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 else
3841 {
3842 if (cap->cmdchar == 'M')
3843 {
3844#ifdef FEAT_DIFF
3845 /* Don't count filler lines above the window. */
3846 used -= diff_check_fill(curwin, curwin->w_topline)
3847 - curwin->w_topfill;
3848#endif
3849 validate_botline(); /* make sure w_empty_rows is valid */
3850 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3851 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3852 {
3853#ifdef FEAT_DIFF
3854 /* Count half he number of filler lines to be "below this
3855 * line" and half to be "above the next line". */
3856 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3857 + n) / 2 >= half)
3858 {
3859 --n;
3860 break;
3861 }
3862#endif
3863 used += plines(curwin->w_topline + n);
3864 if (used >= half)
3865 break;
3866#ifdef FEAT_FOLDING
3867 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3868 n = lnum - curwin->w_topline;
3869#endif
3870 }
3871 if (n > 0 && used > curwin->w_height)
3872 --n;
3873 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003874 else /* (cap->cmdchar == 'H') */
3875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003877#ifdef FEAT_FOLDING
3878 if (hasAnyFolding(curwin))
3879 {
3880 /* Count a fold for one screen line. */
3881 lnum = curwin->w_topline;
3882 while (n-- > 0 && lnum < curwin->w_botline - 1)
3883 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003884 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003885 ++lnum;
3886 }
3887 n = lnum - curwin->w_topline;
3888 }
3889#endif
3890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 curwin->w_cursor.lnum = curwin->w_topline + n;
3892 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3893 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3894 }
3895
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003896 /* Correct for 'so', except when an operator is pending. */
3897 if (cap->oap->op_type == OP_NOP)
3898 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 beginline(BL_SOL | BL_FIX);
3900}
3901
3902/*
3903 * Cursor right commands.
3904 */
3905 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003906nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
3908 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003909 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003911 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3912 {
3913 /* <C-Right> and <S-Right> move a word or WORD right */
3914 if (mod_mask & MOD_MASK_CTRL)
3915 cap->arg = TRUE;
3916 nv_wordcmd(cap);
3917 return;
3918 }
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 cap->oap->motion_type = MCHAR;
3921 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003922 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003925 * In virtual edit mode, there's no such thing as "past_line", as lines
3926 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 */
3928 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003929 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931 for (n = cap->count1; n > 0; --n)
3932 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003933 if ((!past_line && oneright() == FAIL)
3934 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003935 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 {
3937 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003938 * <Space> wraps to next line if 'whichwrap' has 's'.
3939 * 'l' wraps to next line if 'whichwrap' has 'l'.
3940 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 */
3942 if ( ((cap->cmdchar == ' '
3943 && vim_strchr(p_ww, 's') != NULL)
3944 || (cap->cmdchar == 'l'
3945 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003946 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 && vim_strchr(p_ww, '>') != NULL))
3948 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3949 {
3950 /* When deleting we also count the NL as a character.
3951 * Set cap->oap->inclusive when last char in the line is
3952 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003953 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003955 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 cap->oap->inclusive = TRUE;
3957 else
3958 {
3959 ++curwin->w_cursor.lnum;
3960 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 curwin->w_set_curswant = TRUE;
3963 cap->oap->inclusive = FALSE;
3964 }
3965 continue;
3966 }
3967 if (cap->oap->op_type == OP_NOP)
3968 {
3969 /* Only beep and flush if not moved at all */
3970 if (n == cap->count1)
3971 beep_flush();
3972 }
3973 else
3974 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003975 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 cap->oap->inclusive = TRUE;
3977 }
3978 break;
3979 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003980 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
3982 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 if (virtual_active())
3984 oneright();
3985 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02003988 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 ++curwin->w_cursor.col;
3991 }
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994#ifdef FEAT_FOLDING
3995 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
3996 && cap->oap->op_type == OP_NOP)
3997 foldOpenCursor();
3998#endif
3999}
4000
4001/*
4002 * Cursor left commands.
4003 *
4004 * Returns TRUE when operator end should not be adjusted.
4005 */
4006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004007nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
4009 long n;
4010
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004011 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4012 {
4013 /* <C-Left> and <S-Left> move a word or WORD left */
4014 if (mod_mask & MOD_MASK_CTRL)
4015 cap->arg = 1;
4016 nv_bck_word(cap);
4017 return;
4018 }
4019
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 cap->oap->motion_type = MCHAR;
4021 cap->oap->inclusive = FALSE;
4022 for (n = cap->count1; n > 0; --n)
4023 {
4024 if (oneleft() == FAIL)
4025 {
4026 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4027 * 'h' wraps to previous line if 'whichwrap' has 'h'.
4028 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
4029 */
4030 if ( (((cap->cmdchar == K_BS
4031 || cap->cmdchar == Ctrl_H)
4032 && vim_strchr(p_ww, 'b') != NULL)
4033 || (cap->cmdchar == 'h'
4034 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004035 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 && vim_strchr(p_ww, '<') != NULL))
4037 && curwin->w_cursor.lnum > 1)
4038 {
4039 --(curwin->w_cursor.lnum);
4040 coladvance((colnr_T)MAXCOL);
4041 curwin->w_set_curswant = TRUE;
4042
4043 /* When the NL before the first char has to be deleted we
4044 * put the cursor on the NUL after the previous line.
4045 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00004046 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if ( (cap->oap->op_type == OP_DELETE
4048 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004049 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004051 char_u *cp = ml_get_cursor();
4052
4053 if (*cp != NUL)
4054 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004055 if (has_mbyte)
4056 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4057 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004058 ++curwin->w_cursor.col;
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 cap->retval |= CA_NO_ADJ_OP_END;
4061 }
4062 continue;
4063 }
4064 /* Only beep and flush if not moved at all */
4065 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4066 beep_flush();
4067 break;
4068 }
4069 }
4070#ifdef FEAT_FOLDING
4071 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4072 && cap->oap->op_type == OP_NOP)
4073 foldOpenCursor();
4074#endif
4075}
4076
4077/*
4078 * Cursor up commands.
4079 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4080 */
4081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004082nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004084 if (mod_mask & MOD_MASK_SHIFT)
4085 {
4086 /* <S-Up> is page up */
4087 cap->arg = BACKWARD;
4088 nv_page(cap);
4089 }
4090 else
4091 {
4092 cap->oap->motion_type = MLINE;
4093 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4094 clearopbeep(cap->oap);
4095 else if (cap->arg)
4096 beginline(BL_WHITE | BL_FIX);
4097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098}
4099
4100/*
4101 * Cursor down commands.
4102 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4103 */
4104 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004105nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004107 if (mod_mask & MOD_MASK_SHIFT)
4108 {
4109 /* <S-Down> is page down */
4110 cap->arg = FORWARD;
4111 nv_page(cap);
4112 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004113#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004114 /* Quickfix window only: view the result under the cursor. */
4115 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4116 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 {
4120#ifdef FEAT_CMDWIN
4121 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004122 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 cmdwin_result = CAR;
4124 else
4125#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004126#ifdef FEAT_JOB_CHANNEL
4127 /* In a prompt buffer a <CR> in the last line invokes the callback. */
4128 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4129 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4130 {
4131 invoke_prompt_callback();
4132 if (restart_edit == 0)
4133 restart_edit = 'a';
4134 }
4135 else
4136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 {
4138 cap->oap->motion_type = MLINE;
4139 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4140 clearopbeep(cap->oap);
4141 else if (cap->arg)
4142 beginline(BL_WHITE | BL_FIX);
4143 }
4144 }
4145}
4146
4147#ifdef FEAT_SEARCHPATH
4148/*
4149 * Grab the file name under the cursor and edit it.
4150 */
4151 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004152nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
4154 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004155 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004157 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
4159 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004160 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 return;
4162 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004163 if (curbuf_locked())
4164 {
4165 clearop(cap->oap);
4166 return;
4167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004169 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
4171 if (ptr != NULL)
4172 {
4173 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004174 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004175 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004177 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004178 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004179 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004180 {
4181 curwin->w_cursor.lnum = lnum;
4182 check_cursor_lnum();
4183 beginline(BL_SOL | BL_FIX);
4184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 vim_free(ptr);
4186 }
4187 else
4188 clearop(cap->oap);
4189}
4190#endif
4191
4192/*
4193 * <End> command: to end of current line or last line.
4194 */
4195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004196nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004198 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004200 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 nv_goto(cap);
4202 cap->count1 = 1; /* to end of current line */
4203 }
4204 nv_dollar(cap);
4205}
4206
4207/*
4208 * Handle the "$" command.
4209 */
4210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004211nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
4213 cap->oap->motion_type = MCHAR;
4214 cap->oap->inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 /* In virtual mode when off the edge of a line and an operator
4216 * is pending (whew!) keep the cursor where it is.
4217 * Otherwise, send it to the end of the line. */
4218 if (!virtual_active() || gchar_cursor() != NUL
4219 || cap->oap->op_type == OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 curwin->w_curswant = MAXCOL; /* so we stay at the end */
4221 if (cursor_down((long)(cap->count1 - 1),
4222 cap->oap->op_type == OP_NOP) == FAIL)
4223 clearopbeep(cap->oap);
4224#ifdef FEAT_FOLDING
4225 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4226 foldOpenCursor();
4227#endif
4228}
4229
4230/*
4231 * Implementation of '?' and '/' commands.
4232 * If cap->arg is TRUE don't set PC mark.
4233 */
4234 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004235nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004238 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
4240 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4241 {
4242 /* Translate "g??" to "g?g?" */
4243 cap->cmdchar = 'g';
4244 cap->nchar = '?';
4245 nv_operator(cap);
4246 return;
4247 }
4248
Bram Moolenaardda933d2016-09-03 21:04:58 +02004249 /* When using 'incsearch' the cursor may be moved to set a different search
4250 * start position. */
Bram Moolenaare96a2492019-06-25 04:12:16 +02004251 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 if (cap->searchbuf == NULL)
4254 {
4255 clearop(oap);
4256 return;
4257 }
4258
Bram Moolenaar46539112015-02-17 15:43:57 +01004259 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004260 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02004261 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262}
4263
4264/*
4265 * Handle "N" and "n" commands.
4266 * cap->arg is SEARCH_REV for "N", 0 for "n".
4267 */
4268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004269nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
Bram Moolenaar46539112015-02-17 15:43:57 +01004271 pos_T old = curwin->w_cursor;
4272 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
4273
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004274 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004275 {
4276 /* Avoid getting stuck on the current cursor position, which can
4277 * happen when an offset is given and the cursor is on the last char
4278 * in the buffer: Repeat with count + 1. */
4279 cap->count1 += 1;
4280 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
4281 cap->count1 -= 1;
4282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283}
4284
4285/*
4286 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4287 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004288 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004290 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004291normal_search(
4292 cmdarg_T *cap,
4293 int dir,
4294 char_u *pat,
4295 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296{
4297 int i;
4298
4299 cap->oap->motion_type = MCHAR;
4300 cap->oap->inclusive = FALSE;
4301 cap->oap->use_reg_one = TRUE;
4302 curwin->w_set_curswant = TRUE;
4303
4304 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004305 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (i == 0)
4307 clearop(cap->oap);
4308 else
4309 {
4310 if (i == 2)
4311 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313#ifdef FEAT_FOLDING
4314 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4315 foldOpenCursor();
4316#endif
4317 }
4318
4319 /* "/$" will put the cursor after the end of the line, may need to
4320 * correct that here */
4321 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004322 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323}
4324
4325/*
4326 * Character search commands.
4327 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4328 * ',' and FALSE for ';'.
4329 * cap->nchar is NUL for ',' and ';' (repeat the search)
4330 */
4331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004332nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
4334 int t_cmd;
4335
4336 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4337 t_cmd = TRUE;
4338 else
4339 t_cmd = FALSE;
4340
4341 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4343 clearopbeep(cap->oap);
4344 else
4345 {
4346 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 /* Include a Tab for "tx" and for "dfx". */
4348 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4349 && (t_cmd || cap->oap->op_type != OP_NOP))
4350 {
4351 colnr_T scol, ecol;
4352
4353 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4354 curwin->w_cursor.coladd = ecol - scol;
4355 }
4356 else
4357 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359#ifdef FEAT_FOLDING
4360 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4361 foldOpenCursor();
4362#endif
4363 }
4364}
4365
4366/*
4367 * "[" and "]" commands.
4368 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4369 */
4370 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004371nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004373 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 pos_T prev_pos;
4375 pos_T *pos = NULL; /* init for GCC */
4376 pos_T old_pos; /* cursor position before command */
4377 int flag;
4378 long n;
4379 int findc;
4380 int c;
4381
4382 cap->oap->motion_type = MCHAR;
4383 cap->oap->inclusive = FALSE;
4384 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004385 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387#ifdef FEAT_SEARCHPATH
4388 /*
4389 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4390 */
4391 if (cap->nchar == 'f')
4392 nv_gotofile(cap);
4393 else
4394#endif
4395
4396#ifdef FEAT_FIND_ID
4397 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004398 * Find the occurrence(s) of the identifier or define under cursor
4399 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 *
4401 * search list jump
4402 * fwd bwd fwd bwd fwd bwd
4403 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4404 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4405 */
4406 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004407# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004409# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004411# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 cap->nchar) != NULL)
4413 {
4414 char_u *ptr;
4415 int len;
4416
4417 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4418 clearop(cap->oap);
4419 else
4420 {
4421 find_pattern_in_path(ptr, 0, len, TRUE,
4422 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4423 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4424 cap->count1,
4425 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4426 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4427 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4428 (linenr_T)MAXLNUM);
4429 curwin->w_set_curswant = TRUE;
4430 }
4431 }
4432 else
4433#endif
4434
4435 /*
4436 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4437 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4438 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4439 * "[m" or "]m" search for prev/next start of (Java) method.
4440 * "[M" or "]M" search for prev/next end of (Java) method.
4441 */
4442 if ( (cap->cmdchar == '['
4443 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4444 || (cap->cmdchar == ']'
4445 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4446 {
4447 if (cap->nchar == '*')
4448 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 prev_pos.lnum = 0;
4450 if (cap->nchar == 'm' || cap->nchar == 'M')
4451 {
4452 if (cap->cmdchar == '[')
4453 findc = '{';
4454 else
4455 findc = '}';
4456 n = 9999;
4457 }
4458 else
4459 {
4460 findc = cap->nchar;
4461 n = cap->count1;
4462 }
4463 for ( ; n > 0; --n)
4464 {
4465 if ((pos = findmatchlimit(cap->oap, findc,
4466 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4467 {
4468 if (new_pos.lnum == 0) /* nothing found */
4469 {
4470 if (cap->nchar != 'm' && cap->nchar != 'M')
4471 clearopbeep(cap->oap);
4472 }
4473 else
4474 pos = &new_pos; /* use last one found */
4475 break;
4476 }
4477 prev_pos = new_pos;
4478 curwin->w_cursor = *pos;
4479 new_pos = *pos;
4480 }
4481 curwin->w_cursor = old_pos;
4482
4483 /*
4484 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4485 * brought us to the match for "[m" and "]M" when inside a method.
4486 * Try finding the '{' or '}' we want to be at.
4487 * Also repeat for the given count.
4488 */
4489 if (cap->nchar == 'm' || cap->nchar == 'M')
4490 {
4491 /* norm is TRUE for "]M" and "[m" */
4492 int norm = ((findc == '{') == (cap->nchar == 'm'));
4493
4494 n = cap->count1;
4495 /* found a match: we were inside a method */
4496 if (prev_pos.lnum != 0)
4497 {
4498 pos = &prev_pos;
4499 curwin->w_cursor = prev_pos;
4500 if (norm)
4501 --n;
4502 }
4503 else
4504 pos = NULL;
4505 while (n > 0)
4506 {
4507 for (;;)
4508 {
4509 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4510 {
4511 /* if not found anything, that's an error */
4512 if (pos == NULL)
4513 clearopbeep(cap->oap);
4514 n = 0;
4515 break;
4516 }
4517 c = gchar_cursor();
4518 if (c == '{' || c == '}')
4519 {
4520 /* Must have found end/start of class: use it.
4521 * Or found the place to be at. */
4522 if ((c == findc && norm) || (n == 1 && !norm))
4523 {
4524 new_pos = curwin->w_cursor;
4525 pos = &new_pos;
4526 n = 0;
4527 }
4528 /* if no match found at all, we started outside of the
4529 * class and we're inside now. Just go on. */
4530 else if (new_pos.lnum == 0)
4531 {
4532 new_pos = curwin->w_cursor;
4533 pos = &new_pos;
4534 }
4535 /* found start/end of other method: go to match */
4536 else if ((pos = findmatchlimit(cap->oap, findc,
4537 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4538 0)) == NULL)
4539 n = 0;
4540 else
4541 curwin->w_cursor = *pos;
4542 break;
4543 }
4544 }
4545 --n;
4546 }
4547 curwin->w_cursor = old_pos;
4548 if (pos == NULL && new_pos.lnum != 0)
4549 clearopbeep(cap->oap);
4550 }
4551 if (pos != NULL)
4552 {
4553 setpcmark();
4554 curwin->w_cursor = *pos;
4555 curwin->w_set_curswant = TRUE;
4556#ifdef FEAT_FOLDING
4557 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4558 && cap->oap->op_type == OP_NOP)
4559 foldOpenCursor();
4560#endif
4561 }
4562 }
4563
4564 /*
4565 * "[[", "[]", "]]" and "][": move to start or end of function
4566 */
4567 else if (cap->nchar == '[' || cap->nchar == ']')
4568 {
4569 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
4570 flag = '{';
4571 else
4572 flag = '}'; /* "][" or "[]" */
4573
4574 curwin->w_set_curswant = TRUE;
4575 /*
4576 * Imitate strange Vi behaviour: When using "]]" with an operator
4577 * we also stop at '}'.
4578 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004579 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 (cap->oap->op_type != OP_NOP
4581 && cap->arg == FORWARD && flag == '{')))
4582 clearopbeep(cap->oap);
4583 else
4584 {
4585 if (cap->oap->op_type == OP_NOP)
4586 beginline(BL_WHITE | BL_FIX);
4587#ifdef FEAT_FOLDING
4588 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4589 foldOpenCursor();
4590#endif
4591 }
4592 }
4593
4594 /*
4595 * "[p", "[P", "]P" and "]p": put with indent adjustment
4596 */
4597 else if (cap->nchar == 'p' || cap->nchar == 'P')
4598 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004599 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601
4602 /*
4603 * "['", "[`", "]'" and "]`": jump to next mark
4604 */
4605 else if (cap->nchar == '\'' || cap->nchar == '`')
4606 {
4607 pos = &curwin->w_cursor;
4608 for (n = cap->count1; n > 0; --n)
4609 {
4610 prev_pos = *pos;
4611 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4612 cap->nchar == '\'');
4613 if (pos == NULL)
4614 break;
4615 }
4616 if (pos == NULL)
4617 pos = &prev_pos;
4618 nv_cursormark(cap, cap->nchar == '\'', pos);
4619 }
4620
4621#ifdef FEAT_MOUSE
4622 /*
4623 * [ or ] followed by a middle mouse click: put selected text with
4624 * indent adjustment. Any other button just does as usual.
4625 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004626 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
4628 (void)do_mouse(cap->oap, cap->nchar,
4629 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4630 cap->count1, PUT_FIXINDENT);
4631 }
4632#endif /* FEAT_MOUSE */
4633
4634#ifdef FEAT_FOLDING
4635 /*
4636 * "[z" and "]z": move to start or end of open fold.
4637 */
4638 else if (cap->nchar == 'z')
4639 {
4640 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4641 cap->count1) == FAIL)
4642 clearopbeep(cap->oap);
4643 }
4644#endif
4645
4646#ifdef FEAT_DIFF
4647 /*
4648 * "[c" and "]c": move to next or previous diff-change.
4649 */
4650 else if (cap->nchar == 'c')
4651 {
4652 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4653 cap->count1) == FAIL)
4654 clearopbeep(cap->oap);
4655 }
4656#endif
4657
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004658#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004659 /*
4660 * "[s", "[S", "]s" and "]S": move to next spell error.
4661 */
4662 else if (cap->nchar == 's' || cap->nchar == 'S')
4663 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004664 setpcmark();
4665 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004666 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4667 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004668 {
4669 clearopbeep(cap->oap);
4670 break;
4671 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004672 else
4673 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004674# ifdef FEAT_FOLDING
4675 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4676 foldOpenCursor();
4677# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004678 }
4679#endif
4680
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 /* Not a valid cap->nchar. */
4682 else
4683 clearopbeep(cap->oap);
4684}
4685
4686/*
4687 * Handle Normal mode "%" command.
4688 */
4689 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004690nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004693#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 linenr_T lnum = curwin->w_cursor.lnum;
4695#endif
4696
4697 cap->oap->inclusive = TRUE;
4698 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
4699 {
4700 if (cap->count0 > 100)
4701 clearopbeep(cap->oap);
4702 else
4703 {
4704 cap->oap->motion_type = MLINE;
4705 setpcmark();
4706 /* Round up, so CTRL-G will give same value. Watch out for a
4707 * large line count, the line number must not go negative! */
4708 if (curbuf->b_ml.ml_line_count > 1000000)
4709 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4710 / 100L * cap->count0;
4711 else
4712 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4713 cap->count0 + 99L) / 100L;
4714 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4715 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4716 beginline(BL_SOL | BL_FIX);
4717 }
4718 }
4719 else /* "%" : go to matching paren */
4720 {
4721 cap->oap->motion_type = MCHAR;
4722 cap->oap->use_reg_one = TRUE;
4723 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4724 clearopbeep(cap->oap);
4725 else
4726 {
4727 setpcmark();
4728 curwin->w_cursor = *pos;
4729 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 }
4733 }
4734#ifdef FEAT_FOLDING
4735 if (cap->oap->op_type == OP_NOP
4736 && lnum != curwin->w_cursor.lnum
4737 && (fdo_flags & FDO_PERCENT)
4738 && KeyTyped)
4739 foldOpenCursor();
4740#endif
4741}
4742
4743/*
4744 * Handle "(" and ")" commands.
4745 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4746 */
4747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004748nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 cap->oap->motion_type = MCHAR;
4751 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00004752 /* The motion used to be inclusive for "(", but that is not what Vi does. */
4753 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 curwin->w_set_curswant = TRUE;
4755
4756 if (findsent(cap->arg, cap->count1) == FAIL)
4757 clearopbeep(cap->oap);
4758 else
4759 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004760 /* Don't leave the cursor on the NUL past end of line. */
4761 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763#ifdef FEAT_FOLDING
4764 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4765 foldOpenCursor();
4766#endif
4767 }
4768}
4769
4770/*
4771 * "m" command: Mark a position.
4772 */
4773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004774nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 if (!checkclearop(cap->oap))
4777 {
4778 if (setmark(cap->nchar) == FAIL)
4779 clearopbeep(cap->oap);
4780 }
4781}
4782
4783/*
4784 * "{" and "}" commands.
4785 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4786 */
4787 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004788nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789{
4790 cap->oap->motion_type = MCHAR;
4791 cap->oap->inclusive = FALSE;
4792 cap->oap->use_reg_one = TRUE;
4793 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004794 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 clearopbeep(cap->oap);
4796 else
4797 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#ifdef FEAT_FOLDING
4800 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4801 foldOpenCursor();
4802#endif
4803 }
4804}
4805
4806/*
4807 * "u" command: Undo or make lower case.
4808 */
4809 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004810nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004812 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
4815 cap->cmdchar = 'g';
4816 cap->nchar = 'u';
4817 nv_operator(cap);
4818 }
4819 else
4820 nv_kundo(cap);
4821}
4822
4823/*
4824 * <Undo> command.
4825 */
4826 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004827nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
4829 if (!checkclearopq(cap->oap))
4830 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004831#ifdef FEAT_JOB_CHANNEL
4832 if (bt_prompt(curbuf))
4833 {
4834 clearopbeep(cap->oap);
4835 return;
4836 }
4837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 u_undo((int)cap->count1);
4839 curwin->w_set_curswant = TRUE;
4840 }
4841}
4842
4843/*
4844 * Handle the "r" command.
4845 */
4846 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004847nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
4849 char_u *ptr;
4850 int had_ctrl_v;
4851 long n;
4852
4853 if (checkclearop(cap->oap))
4854 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004855#ifdef FEAT_JOB_CHANNEL
4856 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4857 {
4858 clearopbeep(cap->oap);
4859 return;
4860 }
4861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
4863 /* get another character */
4864 if (cap->nchar == Ctrl_V)
4865 {
4866 had_ctrl_v = Ctrl_V;
4867 cap->nchar = get_literal();
4868 /* Don't redo a multibyte character with CTRL-V. */
4869 if (cap->nchar > DEL)
4870 had_ctrl_v = NUL;
4871 }
4872 else
4873 had_ctrl_v = NUL;
4874
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004875 /* Abort if the character is a special key. */
4876 if (IS_SPECIAL(cap->nchar))
4877 {
4878 clearopbeep(cap->oap);
4879 return;
4880 }
4881
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 /* Visual mode "r" */
4883 if (VIsual_active)
4884 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004885 if (got_int)
4886 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004887 if (had_ctrl_v)
4888 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004889 /* Use a special (negative) number to make a difference between a
4890 * literal CR or NL and a line break. */
4891 if (cap->nchar == CAR)
4892 cap->nchar = REPLACE_CR_NCHAR;
4893 else if (cap->nchar == NL)
4894 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 nv_operator(cap);
4897 return;
4898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 /* Break tabs, etc. */
4901 if (virtual_active())
4902 {
4903 if (u_save_cursor() == FAIL)
4904 return;
4905 if (gchar_cursor() == NUL)
4906 {
4907 /* Add extra space and put the cursor on the first one. */
4908 coladvance_force((colnr_T)(getviscol() + cap->count1));
4909 curwin->w_cursor.col -= cap->count1;
4910 }
4911 else if (gchar_cursor() == TAB)
4912 coladvance_force(getviscol());
4913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004915 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004917 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004918 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
4920 clearopbeep(cap->oap);
4921 return;
4922 }
4923
4924 /*
4925 * Replacing with a TAB is done by edit() when it is complicated because
4926 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4927 * Other characters are done below to avoid problems with things like
4928 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
4929 */
4930 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4931 {
4932 stuffnumReadbuff(cap->count1);
4933 stuffcharReadbuff('R');
4934 stuffcharReadbuff('\t');
4935 stuffcharReadbuff(ESC);
4936 return;
4937 }
4938
4939 /* save line for undo */
4940 if (u_save_cursor() == FAIL)
4941 return;
4942
4943 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4944 {
4945 /*
4946 * Replace character(s) by a single newline.
4947 * Strange vi behaviour: Only one newline is inserted.
4948 * Delete the characters here.
4949 * Insert the newline with an insert command, takes care of
4950 * autoindent. The insert command depends on being on the last
4951 * character of a line or not.
4952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 (void)del_chars(cap->count1, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 stuffcharReadbuff('\r');
4955 stuffcharReadbuff(ESC);
4956
4957 /* Give 'r' to edit(), to get the redo command right. */
4958 invoke_edit(cap, TRUE, 'r', FALSE);
4959 }
4960 else
4961 {
4962 prep_redo(cap->oap->regname, cap->count1,
4963 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4964
4965 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (has_mbyte)
4967 {
4968 int old_State = State;
4969
4970 if (cap->ncharC1 != 0)
4971 AppendCharToRedobuff(cap->ncharC1);
4972 if (cap->ncharC2 != 0)
4973 AppendCharToRedobuff(cap->ncharC2);
4974
4975 /* This is slow, but it handles replacing a single-byte with a
4976 * multi-byte and the other way around. Also handles adding
4977 * composing characters for utf-8. */
4978 for (n = cap->count1; n > 0; --n)
4979 {
4980 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02004981 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
4982 {
4983 int c = ins_copychar(curwin->w_cursor.lnum
4984 + (cap->nchar == Ctrl_Y ? -1 : 1));
4985 if (c != NUL)
4986 ins_char(c);
4987 else
4988 /* will be decremented further down */
4989 ++curwin->w_cursor.col;
4990 }
4991 else
4992 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 State = old_State;
4994 if (cap->ncharC1 != 0)
4995 ins_char(cap->ncharC1);
4996 if (cap->ncharC2 != 0)
4997 ins_char(cap->ncharC2);
4998 }
4999 }
5000 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
5002 /*
5003 * Replace the characters within one line.
5004 */
5005 for (n = cap->count1; n > 0; --n)
5006 {
5007 /*
5008 * Get ptr again, because u_save and/or showmatch() will have
5009 * released the line. At the same time we let know that the
5010 * line will be changed.
5011 */
5012 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005013 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5014 {
5015 int c = ins_copychar(curwin->w_cursor.lnum
5016 + (cap->nchar == Ctrl_Y ? -1 : 1));
5017 if (c != NUL)
5018 ptr[curwin->w_cursor.col] = c;
5019 }
5020 else
5021 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 if (p_sm && msg_silent == 0)
5023 showmatch(cap->nchar);
5024 ++curwin->w_cursor.col;
5025 }
5026#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005027 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005029 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
Bram Moolenaar009b2592004-10-24 19:18:58 +00005031 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005032 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005034 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
5036#endif
5037
5038 /* mark the buffer as changed and prepare for displaying */
5039 changed_bytes(curwin->w_cursor.lnum,
5040 (colnr_T)(curwin->w_cursor.col - cap->count1));
5041 }
5042 --curwin->w_cursor.col; /* cursor on the last replaced char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 /* if the character on the left of the current cursor is a multi-byte
5044 * character, move two characters left */
5045 if (has_mbyte)
5046 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 curbuf->b_op_end = curwin->w_cursor;
5048 curwin->w_set_curswant = TRUE;
5049 set_last_insert(cap->nchar);
5050 }
5051}
5052
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053/*
5054 * 'o': Exchange start and end of Visual area.
5055 * 'O': same, but in block mode exchange left and right corners.
5056 */
5057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005058v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
5060 pos_T old_cursor;
5061 colnr_T left, right;
5062
5063 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5064 {
5065 old_cursor = curwin->w_cursor;
5066 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5067 curwin->w_cursor.lnum = VIsual.lnum;
5068 coladvance(left);
5069 VIsual = curwin->w_cursor;
5070
5071 curwin->w_cursor.lnum = old_cursor.lnum;
5072 curwin->w_curswant = right;
5073 /* 'selection "exclusive" and cursor at right-bottom corner: move it
5074 * right one column */
5075 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5076 ++curwin->w_curswant;
5077 coladvance(curwin->w_curswant);
5078 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005080 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 curwin->w_cursor.lnum = VIsual.lnum;
5083 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5084 ++right;
5085 coladvance(right);
5086 VIsual = curwin->w_cursor;
5087
5088 curwin->w_cursor.lnum = old_cursor.lnum;
5089 coladvance(left);
5090 curwin->w_curswant = left;
5091 }
5092 }
5093 else
5094 {
5095 old_cursor = curwin->w_cursor;
5096 curwin->w_cursor = VIsual;
5097 VIsual = old_cursor;
5098 curwin->w_set_curswant = TRUE;
5099 }
5100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
5102/*
5103 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5104 */
5105 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005106nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (VIsual_active) /* "R" is replace lines */
5109 {
5110 cap->cmdchar = 'c';
5111 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01005112 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 VIsual_mode = 'V';
5114 nv_operator(cap);
5115 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005116 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005119 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 else
5121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (virtual_active())
5123 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5125 }
5126 }
5127}
5128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129/*
5130 * "gr".
5131 */
5132 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005133nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 if (VIsual_active)
5136 {
5137 cap->cmdchar = 'r';
5138 cap->nchar = cap->extra_char;
5139 nv_replace(cap); /* Do same as "r" in Visual mode for now */
5140 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005141 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
5143 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005144 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 else
5146 {
5147 if (cap->extra_char == Ctrl_V) /* get another character */
5148 cap->extra_char = get_literal();
5149 stuffcharReadbuff(cap->extra_char);
5150 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (virtual_active())
5152 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 invoke_edit(cap, TRUE, 'v', FALSE);
5154 }
5155 }
5156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158/*
5159 * Swap case for "~" command, when it does not work like an operator.
5160 */
5161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005162n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
5164 long n;
5165 pos_T startpos;
5166 int did_change = 0;
5167#ifdef FEAT_NETBEANS_INTG
5168 pos_T pos;
5169 char_u *ptr;
5170 int count;
5171#endif
5172
5173 if (checkclearopq(cap->oap))
5174 return;
5175
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005176 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
5178 clearopbeep(cap->oap);
5179 return;
5180 }
5181
5182 prep_redo_cmd(cap);
5183
5184 if (u_save_cursor() == FAIL)
5185 return;
5186
5187 startpos = curwin->w_cursor;
5188#ifdef FEAT_NETBEANS_INTG
5189 pos = startpos;
5190#endif
5191 for (n = cap->count1; n > 0; --n)
5192 {
5193 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5194 inc_cursor();
5195 if (gchar_cursor() == NUL)
5196 {
5197 if (vim_strchr(p_ww, '~') != NULL
5198 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5199 {
5200#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005201 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
5203 if (did_change)
5204 {
5205 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005206 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005207 netbeans_removed(curbuf, pos.lnum, pos.col,
5208 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005210 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
5212 pos.col = 0;
5213 pos.lnum++;
5214 }
5215#endif
5216 ++curwin->w_cursor.lnum;
5217 curwin->w_cursor.col = 0;
5218 if (n > 1)
5219 {
5220 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5221 break;
5222 u_clearline();
5223 }
5224 }
5225 else
5226 break;
5227 }
5228 }
5229#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005230 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
5232 ptr = ml_get(pos.lnum);
5233 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005234 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5235 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
5237#endif
5238
5239
5240 check_cursor();
5241 curwin->w_set_curswant = TRUE;
5242 if (did_change)
5243 {
5244 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5245 0L);
5246 curbuf->b_op_start = startpos;
5247 curbuf->b_op_end = curwin->w_cursor;
5248 if (curbuf->b_op_end.col > 0)
5249 --curbuf->b_op_end.col;
5250 }
5251}
5252
5253/*
5254 * Move cursor to mark.
5255 */
5256 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005257nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
5259 if (check_mark(pos) == FAIL)
5260 clearop(cap->oap);
5261 else
5262 {
5263 if (cap->cmdchar == '\''
5264 || cap->cmdchar == '`'
5265 || cap->cmdchar == '['
5266 || cap->cmdchar == ']')
5267 setpcmark();
5268 curwin->w_cursor = *pos;
5269 if (flag)
5270 beginline(BL_WHITE | BL_FIX);
5271 else
5272 check_cursor();
5273 }
5274 cap->oap->motion_type = flag ? MLINE : MCHAR;
5275 if (cap->cmdchar == '`')
5276 cap->oap->use_reg_one = TRUE;
5277 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
5278 curwin->w_set_curswant = TRUE;
5279}
5280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281/*
5282 * Handle commands that are operators in Visual mode.
5283 */
5284 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005285v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286{
5287 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5288
5289 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005290 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 if (isupper(cap->cmdchar))
5292 {
5293 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005294 {
5295 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5299 curwin->w_curswant = MAXCOL;
5300 }
5301 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5302 nv_operator(cap);
5303}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
5305/*
5306 * "s" and "S" commands.
5307 */
5308 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005309nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005311#ifdef FEAT_TERMINAL
5312 /* When showing output of term_dumpdiff() swap the top and botom. */
5313 if (term_swap_diff() == OK)
5314 return;
5315#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005316#ifdef FEAT_JOB_CHANNEL
5317 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5318 {
5319 clearopbeep(cap->oap);
5320 return;
5321 }
5322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
5324 {
5325 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005326 {
5327 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 cap->cmdchar = 'c';
5331 nv_operator(cap);
5332 }
5333 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 nv_optrans(cap);
5335}
5336
5337/*
5338 * Abbreviated commands.
5339 */
5340 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005341nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
5343 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
5344 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 /* in Visual mode these commands are operators */
5347 if (VIsual_active)
5348 v_visop(cap);
5349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 nv_optrans(cap);
5351}
5352
5353/*
5354 * Translate a command into another command.
5355 */
5356 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005357nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
5359 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5360 (char_u *)"d$", (char_u *)"c$",
5361 (char_u *)"cl", (char_u *)"cc",
5362 (char_u *)"yy", (char_u *)":s\r"};
5363 static char_u *str = (char_u *)"xXDCsSY&";
5364
5365 if (!checkclearopq(cap->oap))
5366 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005367 // In Vi "2D" doesn't delete the next line. Can't translate it
5368 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005369 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5370 {
5371 cap->oap->start = curwin->w_cursor;
5372 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005373#ifdef FEAT_EVAL
5374 set_op_var(OP_DELETE);
5375#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005376 cap->count1 = 1;
5377 nv_dollar(cap);
5378 finish_op = TRUE;
5379 ResetRedobuff();
5380 AppendCharToRedobuff('D');
5381 }
5382 else
5383 {
5384 if (cap->count0)
5385 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005386 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 cap->opcount = 0;
5390}
5391
5392/*
5393 * "'" and "`" commands. Also for "g'" and "g`".
5394 * cap->arg is TRUE for "'" and "g'".
5395 */
5396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005397nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 pos_T *pos;
5400 int c;
5401#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005402 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 int old_KeyTyped = KeyTyped; /* getting file may reset it */
5404#endif
5405
5406 if (cap->cmdchar == 'g')
5407 c = cap->extra_char;
5408 else
5409 c = cap->nchar;
5410 pos = getmark(c, (cap->oap->op_type == OP_NOP));
5411 if (pos == (pos_T *)-1) /* jumped to other file */
5412 {
5413 if (cap->arg)
5414 {
5415 check_cursor_lnum();
5416 beginline(BL_WHITE | BL_FIX);
5417 }
5418 else
5419 check_cursor();
5420 }
5421 else
5422 nv_cursormark(cap, cap->arg, pos);
5423
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 /* May need to clear the coladd that a mark includes. */
5425 if (!virtual_active())
5426 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005427 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428#ifdef FEAT_FOLDING
5429 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005430 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005431 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 && (fdo_flags & FDO_MARK)
5433 && old_KeyTyped)
5434 foldOpenCursor();
5435#endif
5436}
5437
5438/*
5439 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
5440 */
5441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005442nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444#ifdef FEAT_JUMPLIST
5445 pos_T *pos;
5446# ifdef FEAT_FOLDING
5447 linenr_T lnum = curwin->w_cursor.lnum;
5448 int old_KeyTyped = KeyTyped; /* getting file may reset it */
5449# endif
5450
5451 if (!checkclearopq(cap->oap))
5452 {
5453 if (cap->cmdchar == 'g')
5454 pos = movechangelist((int)cap->count1);
5455 else
5456 pos = movemark((int)cap->count1);
5457 if (pos == (pos_T *)-1) /* jump to other file */
5458 {
5459 curwin->w_set_curswant = TRUE;
5460 check_cursor();
5461 }
5462 else if (pos != NULL) /* can jump */
5463 nv_cursormark(cap, FALSE, pos);
5464 else if (cap->cmdchar == 'g')
5465 {
5466 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005467 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005469 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005471 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473 else
5474 clearopbeep(cap->oap);
5475# ifdef FEAT_FOLDING
5476 if (cap->oap->op_type == OP_NOP
5477 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5478 && (fdo_flags & FDO_MARK)
5479 && old_KeyTyped)
5480 foldOpenCursor();
5481# endif
5482 }
5483#else
5484 clearopbeep(cap->oap);
5485#endif
5486}
5487
5488/*
5489 * Handle '"' command.
5490 */
5491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005492nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 if (checkclearop(cap->oap))
5495 return;
5496#ifdef FEAT_EVAL
5497 if (cap->nchar == '=')
5498 cap->nchar = get_expr_register();
5499#endif
5500 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5501 {
5502 cap->oap->regname = cap->nchar;
5503 cap->opcount = cap->count0; /* remember count before '"' */
5504#ifdef FEAT_EVAL
5505 set_reg_var(cap->oap->regname);
5506#endif
5507 }
5508 else
5509 clearopbeep(cap->oap);
5510}
5511
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512/*
5513 * Handle "v", "V" and "CTRL-V" commands.
5514 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5515 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005516 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 */
5518 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005519nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005521 if (cap->cmdchar == Ctrl_Q)
5522 cap->cmdchar = Ctrl_V;
5523
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5525 * characterwise, linewise, or blockwise. */
5526 if (cap->oap->op_type != OP_NOP)
5527 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005528 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 finish_op = FALSE; /* operator doesn't finish now but later */
5530 return;
5531 }
5532
5533 VIsual_select = cap->arg;
5534 if (VIsual_active) /* change Visual mode */
5535 {
5536 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
5537 end_visual_mode();
5538 else /* toggle char/block mode */
5539 { /* or char/line mode */
5540 VIsual_mode = cap->cmdchar;
5541 showmode();
5542 }
5543 redraw_curbuf_later(INVERTED); /* update the inversion */
5544 }
5545 else /* start Visual mode */
5546 {
5547 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005548 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005550 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 VIsual = curwin->w_cursor;
5552
5553 VIsual_active = TRUE;
5554 VIsual_reselect = TRUE;
5555 if (!cap->arg)
5556 /* start Select mode when 'selectmode' contains "cmd" */
5557 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005559 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 redraw_cmdline = TRUE; /* show visual mode later */
5561 /*
5562 * For V and ^V, we multiply the number of lines even if there
5563 * was only one -- webb
5564 */
5565 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5566 {
5567 curwin->w_cursor.lnum +=
5568 resel_VIsual_line_count * cap->count0 - 1;
5569 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5570 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5571 }
5572 VIsual_mode = resel_VIsual_mode;
5573 if (VIsual_mode == 'v')
5574 {
5575 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005576 {
5577 validate_virtcol();
5578 curwin->w_curswant = curwin->w_virtcol
5579 + resel_VIsual_vcol * cap->count0 - 1;
5580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005582 curwin->w_curswant = resel_VIsual_vcol;
5583 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005585 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
5587 curwin->w_curswant = MAXCOL;
5588 coladvance((colnr_T)MAXCOL);
5589 }
5590 else if (VIsual_mode == Ctrl_V)
5591 {
5592 validate_virtcol();
5593 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005594 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 coladvance(curwin->w_curswant);
5596 }
5597 else
5598 curwin->w_set_curswant = TRUE;
5599 redraw_curbuf_later(INVERTED); /* show the inversion */
5600 }
5601 else
5602 {
5603 if (!cap->arg)
5604 /* start Select mode when 'selectmode' contains "cmd" */
5605 may_start_select('c');
5606 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005607 if (VIsual_mode != 'V' && *p_sel == 'e')
5608 ++cap->count1; /* include one more char */
5609 if (cap->count0 > 0 && --cap->count1 > 0)
5610 {
5611 /* With a count select that many characters or lines. */
5612 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5613 nv_right(cap);
5614 else if (VIsual_mode == 'V')
5615 nv_down(cap);
5616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 }
5618 }
5619}
5620
5621/*
5622 * Start selection for Shift-movement keys.
5623 */
5624 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005625start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 /* if 'selectmode' contains "key", start Select mode */
5628 may_start_select('k');
5629 n_start_visual_mode('v');
5630}
5631
5632/*
5633 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5634 */
5635 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005636may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 VIsual_select = (stuff_empty() && typebuf_typed()
5639 && (vim_strchr(p_slm, c) != NULL));
5640}
5641
5642/*
5643 * Start Visual mode "c".
5644 * Should set VIsual_select before calling this.
5645 */
5646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005647n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005649#ifdef FEAT_CONCEAL
5650 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02005651 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005652#endif
5653
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 VIsual_mode = c;
5655 VIsual_active = TRUE;
5656 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005657
5658 // Corner case: the 0 position in a tab may change when going into
5659 // virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005661 {
5662 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 VIsual = curwin->w_cursor;
5666
5667#ifdef FEAT_FOLDING
5668 foldAdjustVisual();
5669#endif
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005672#ifdef FEAT_CONCEAL
5673 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02005674 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005675#endif
5676
Bram Moolenaar09df3122006-01-23 22:23:09 +00005677 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 redraw_cmdline = TRUE; /* show visual mode later */
5679#ifdef FEAT_CLIPBOARD
5680 /* Make sure the clipboard gets updated. Needed because start and
5681 * end may still be the same, and the selection needs to be owned */
5682 clip_star.vmode = NUL;
5683#endif
5684
5685 /* Only need to redraw this line, unless still need to redraw an old
5686 * Visual area (when 'lazyredraw' is set). */
5687 if (curwin->w_redr_type < INVERTED)
5688 {
5689 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5690 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5691 }
5692}
5693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
5695/*
5696 * CTRL-W: Window commands
5697 */
5698 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005699nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005701 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005702 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02005703 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005704 cap->cmdchar = ':';
5705 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005706 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005707 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005708 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710}
5711
5712/*
5713 * CTRL-Z: Suspend
5714 */
5715 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005716nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (VIsual_active)
5720 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 do_cmdline_cmd((char_u *)"st");
5722}
5723
5724/*
5725 * Commands starting with "g".
5726 */
5727 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005728nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 int i;
5733 int flag = FALSE;
5734
5735 switch (cap->nchar)
5736 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005737 case Ctrl_A:
5738 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739#ifdef MEM_PROFILE
5740 /*
5741 * "g^A": dump log of used memory.
5742 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005743 if (!VIsual_active && cap->nchar == Ctrl_A)
5744 vim_mem_profile_dump();
5745 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005747 /*
5748 * "g^A/g^X": sequentially increment visually selected region
5749 */
5750 if (VIsual_active)
5751 {
5752 cap->arg = TRUE;
5753 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005754 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005755 nv_addsub(cap);
5756 }
5757 else
5758 clearopbeep(oap);
5759 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 /*
5762 * "gR": Enter virtual replace mode.
5763 */
5764 case 'R':
5765 cap->arg = TRUE;
5766 nv_Replace(cap);
5767 break;
5768
5769 case 'r':
5770 nv_vreplace(cap);
5771 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
5773 case '&':
5774 do_cmdline_cmd((char_u *)"%s//~/&");
5775 break;
5776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 /*
5778 * "gv": Reselect the previous Visual area. If Visual already active,
5779 * exchange previous and current Visual area.
5780 */
5781 case 'v':
5782 if (checkclearop(oap))
5783 break;
5784
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005785 if ( curbuf->b_visual.vi_start.lnum == 0
5786 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5787 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 beep_flush();
5789 else
5790 {
5791 /* set w_cursor to the start of the Visual area, tpos to the end */
5792 if (VIsual_active)
5793 {
5794 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005795 VIsual_mode = curbuf->b_visual.vi_mode;
5796 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797# ifdef FEAT_EVAL
5798 curbuf->b_visual_mode_eval = i;
5799# endif
5800 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005801 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5802 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005804 tpos = curbuf->b_visual.vi_end;
5805 curbuf->b_visual.vi_end = curwin->w_cursor;
5806 curwin->w_cursor = curbuf->b_visual.vi_start;
5807 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
5809 else
5810 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005811 VIsual_mode = curbuf->b_visual.vi_mode;
5812 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5813 tpos = curbuf->b_visual.vi_end;
5814 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
5816
5817 VIsual_active = TRUE;
5818 VIsual_reselect = TRUE;
5819
5820 /* Set Visual to the start and w_cursor to the end of the Visual
5821 * area. Make sure they are on an existing character. */
5822 check_cursor();
5823 VIsual = curwin->w_cursor;
5824 curwin->w_cursor = tpos;
5825 check_cursor();
5826 update_topline();
5827 /*
5828 * When called from normal "g" command: start Select mode when
5829 * 'selectmode' contains "cmd". When called for K_SELECT, always
5830 * start Select mode.
5831 */
5832 if (cap->arg)
5833 VIsual_select = TRUE;
5834 else
5835 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837#ifdef FEAT_CLIPBOARD
5838 /* Make sure the clipboard gets updated. Needed because start and
5839 * end are still the same, and the selection needs to be owned */
5840 clip_star.vmode = NUL;
5841#endif
5842 redraw_curbuf_later(INVERTED);
5843 showmode();
5844 }
5845 break;
5846 /*
5847 * "gV": Don't reselect the previous Visual area after a Select mode
5848 * mapping of menu.
5849 */
5850 case 'V':
5851 VIsual_reselect = FALSE;
5852 break;
5853
5854 /*
5855 * "gh": start Select mode.
5856 * "gH": start Select line mode.
5857 * "g^H": start Select block mode.
5858 */
5859 case K_BS:
5860 cap->nchar = Ctrl_H;
5861 /* FALLTHROUGH */
5862 case 'h':
5863 case 'H':
5864 case Ctrl_H:
5865# ifdef EBCDIC
5866 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
5867 if (cap->nchar == Ctrl_H)
5868 cap->cmdchar = Ctrl_V;
5869 else
5870# endif
5871 cap->cmdchar = cap->nchar + ('v' - 'h');
5872 cap->arg = TRUE;
5873 nv_visual(cap);
5874 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005875
5876 /* "gn", "gN" visually select next/previous search match
5877 * "gn" selects next match
5878 * "gN" selects previous match
5879 */
5880 case 'N':
5881 case 'n':
5882 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005883 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005884 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
5886 /*
5887 * "gj" and "gk" two new funny movement keys -- up and down
5888 * movement based on *screen* line rather than *file* line.
5889 */
5890 case 'j':
5891 case K_DOWN:
5892 /* with 'nowrap' it works just like the normal "j" command; also when
5893 * in a closed fold */
5894 if (!curwin->w_p_wrap
5895#ifdef FEAT_FOLDING
5896 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5897#endif
5898 )
5899 {
5900 oap->motion_type = MLINE;
5901 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5902 }
5903 else
5904 i = nv_screengo(oap, FORWARD, cap->count1);
5905 if (i == FAIL)
5906 clearopbeep(oap);
5907 break;
5908
5909 case 'k':
5910 case K_UP:
5911 /* with 'nowrap' it works just like the normal "k" command; also when
5912 * in a closed fold */
5913 if (!curwin->w_p_wrap
5914#ifdef FEAT_FOLDING
5915 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5916#endif
5917 )
5918 {
5919 oap->motion_type = MLINE;
5920 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5921 }
5922 else
5923 i = nv_screengo(oap, BACKWARD, cap->count1);
5924 if (i == FAIL)
5925 clearopbeep(oap);
5926 break;
5927
5928 /*
5929 * "gJ": join two lines without inserting a space.
5930 */
5931 case 'J':
5932 nv_join(cap);
5933 break;
5934
5935 /*
5936 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
5937 * "gm": middle of "g0" and "g$".
5938 */
5939 case '^':
5940 flag = TRUE;
5941 /* FALLTHROUGH */
5942
5943 case '0':
5944 case 'm':
5945 case K_HOME:
5946 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 oap->motion_type = MCHAR;
5948 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005949 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005951 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 int width2 = width1 + curwin_col_off2();
5953
5954 validate_virtcol();
5955 i = 0;
5956 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5957 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5958 }
5959 else
5960 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02005961 /* Go to the middle of the screen line. When 'number' or
5962 * 'relativenumber' is on and lines are wrapping the middle can be more
5963 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02005965 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 + ((curwin->w_p_wrap && i > 0)
5967 ? curwin_col_off2() : 0)) / 2;
5968 coladvance((colnr_T)i);
5969 if (flag)
5970 {
5971 do
5972 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01005973 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01005974 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 }
5976 curwin->w_set_curswant = TRUE;
5977 break;
5978
5979 case '_':
5980 /* "g_": to the last non-blank character in the line or <count> lines
5981 * downward. */
5982 cap->oap->motion_type = MCHAR;
5983 cap->oap->inclusive = TRUE;
5984 curwin->w_curswant = MAXCOL;
5985 if (cursor_down((long)(cap->count1 - 1),
5986 cap->oap->op_type == OP_NOP) == FAIL)
5987 clearopbeep(cap->oap);
5988 else
5989 {
5990 char_u *ptr = ml_get_curline();
5991
5992 /* In Visual mode we may end up after the line. */
5993 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
5994 --curwin->w_cursor.col;
5995
5996 /* Decrease the cursor column until it's on a non-blank. */
5997 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01005998 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 --curwin->w_cursor.col;
6000 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006001 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
6003 break;
6004
6005 case '$':
6006 case K_END:
6007 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {
6009 int col_off = curwin_col_off();
6010
6011 oap->motion_type = MCHAR;
6012 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006013 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 {
6015 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6016 if (cap->count1 == 1)
6017 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006018 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 int width2 = width1 + curwin_col_off2();
6020
6021 validate_virtcol();
6022 i = width1 - 1;
6023 if (curwin->w_virtcol >= (colnr_T)width1)
6024 i += ((curwin->w_virtcol - width1) / width2 + 1)
6025 * width2;
6026 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006027
6028 /* Make sure we stick in this column. */
6029 validate_virtcol();
6030 curwin->w_curswant = curwin->w_virtcol;
6031 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6033 {
6034 /*
6035 * Check for landing on a character that got split at
6036 * the end of the line. We do not want to advance to
6037 * the next screen line.
6038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 if (curwin->w_virtcol > (colnr_T)i)
6040 --curwin->w_cursor.col;
6041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 }
6043 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6044 clearopbeep(oap);
6045 }
6046 else
6047 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006048 if (cap->count1 > 1)
6049 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006050 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006051
Bram Moolenaar02631462017-09-22 15:20:32 +02006052 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006054
Bram Moolenaard5c82342019-07-27 18:44:57 +02006055 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006056 validate_virtcol();
6057 curwin->w_curswant = curwin->w_virtcol;
6058 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 }
6060 }
6061 break;
6062
6063 /*
6064 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6065 */
6066 case '*':
6067 case '#':
6068#if POUND != '#'
6069 case POUND: /* pound sign (sometimes equal to '#') */
6070#endif
6071 case Ctrl_RSB: /* :tag or :tselect for current identifier */
6072 case ']': /* :tselect for current identifier */
6073 nv_ident(cap);
6074 break;
6075
6076 /*
6077 * ge and gE: go back to end of word
6078 */
6079 case 'e':
6080 case 'E':
6081 oap->motion_type = MCHAR;
6082 curwin->w_set_curswant = TRUE;
6083 oap->inclusive = TRUE;
6084 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6085 clearopbeep(oap);
6086 break;
6087
6088 /*
6089 * "g CTRL-G": display info about cursor position
6090 */
6091 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006092 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 break;
6094
6095 /*
6096 * "gi": start Insert at the last position.
6097 */
6098 case 'i':
6099 if (curbuf->b_last_insert.lnum != 0)
6100 {
6101 curwin->w_cursor = curbuf->b_last_insert;
6102 check_cursor_lnum();
6103 i = (int)STRLEN(ml_get_curline());
6104 if (curwin->w_cursor.col > (colnr_T)i)
6105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 if (virtual_active())
6107 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 curwin->w_cursor.col = i;
6109 }
6110 }
6111 cap->cmdchar = 'i';
6112 nv_edit(cap);
6113 break;
6114
6115 /*
6116 * "gI": Start insert in column 1.
6117 */
6118 case 'I':
6119 beginline(0);
6120 if (!checkclearopq(oap))
6121 invoke_edit(cap, FALSE, 'g', FALSE);
6122 break;
6123
6124#ifdef FEAT_SEARCHPATH
6125 /*
6126 * "gf": goto file, edit file under cursor
6127 * "]f" and "[f": can also be used.
6128 */
6129 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006130 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 nv_gotofile(cap);
6132 break;
6133#endif
6134
6135 /* "g'm" and "g`m": jump to mark without setting pcmark */
6136 case '\'':
6137 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006138 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 case '`':
6140 nv_gomark(cap);
6141 break;
6142
6143 /*
6144 * "gs": Goto sleep.
6145 */
6146 case 's':
6147 do_sleep(cap->count1 * 1000L);
6148 break;
6149
6150 /*
6151 * "ga": Display the ascii value of the character under the
6152 * cursor. It is displayed in decimal, hex, and octal. -- webb
6153 */
6154 case 'a':
6155 do_ascii(NULL);
6156 break;
6157
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 /*
6159 * "g8": Display the bytes used for the UTF-8 character under the
6160 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006161 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 */
6163 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006164 if (cap->count0 == 8)
6165 utf_find_illegal();
6166 else
6167 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
Bram Moolenaar60402d62017-04-20 18:54:50 +02006170 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006171 case '<':
6172 show_sb_text();
6173 break;
6174
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 /*
6176 * "gg": Goto the first line in file. With a count it goes to
6177 * that line number like for "G". -- webb
6178 */
6179 case 'g':
6180 cap->arg = FALSE;
6181 nv_goto(cap);
6182 break;
6183
6184 /*
6185 * Two-character operators:
6186 * "gq" Format text
6187 * "gw" Format text and keep cursor position
6188 * "g~" Toggle the case of the text.
6189 * "gu" Change text to lower case.
6190 * "gU" Change text to upper case.
6191 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006192 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 */
6194 case 'q':
6195 case 'w':
6196 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02006197 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 case '~':
6199 case 'u':
6200 case 'U':
6201 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006202 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 nv_operator(cap);
6204 break;
6205
6206 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006207 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 * current function
6209 * "gD": idem, but in the current file.
6210 */
6211 case 'd':
6212 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006213 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 break;
6215
6216#ifdef FEAT_MOUSE
6217 /*
6218 * g<*Mouse> : <C-*mouse>
6219 */
6220 case K_MIDDLEMOUSE:
6221 case K_MIDDLEDRAG:
6222 case K_MIDDLERELEASE:
6223 case K_LEFTMOUSE:
6224 case K_LEFTDRAG:
6225 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006226 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 case K_RIGHTMOUSE:
6228 case K_RIGHTDRAG:
6229 case K_RIGHTRELEASE:
6230 case K_X1MOUSE:
6231 case K_X1DRAG:
6232 case K_X1RELEASE:
6233 case K_X2MOUSE:
6234 case K_X2DRAG:
6235 case K_X2RELEASE:
6236 mod_mask = MOD_MASK_CTRL;
6237 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6238 break;
6239#endif
6240
6241 case K_IGNORE:
6242 break;
6243
6244 /*
6245 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6246 */
6247 case 'p':
6248 case 'P':
6249 nv_put(cap);
6250 break;
6251
6252#ifdef FEAT_BYTEOFF
6253 /* "go": goto byte count from start of buffer */
6254 case 'o':
6255 goto_byte(cap->count0);
6256 break;
6257#endif
6258
6259 /* "gQ": improved Ex mode */
6260 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006261 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006264 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 break;
6266 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006267
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (!checkclearopq(oap))
6269 do_exmode(TRUE);
6270 break;
6271
6272#ifdef FEAT_JUMPLIST
6273 case ',':
6274 nv_pcmark(cap);
6275 break;
6276
6277 case ';':
6278 cap->count1 = -cap->count1;
6279 nv_pcmark(cap);
6280 break;
6281#endif
6282
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006283 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006284 if (!checkclearop(oap))
6285 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006286 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006287 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006288 if (!checkclearop(oap))
6289 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006290 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006291
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006292 case '+':
6293 case '-': /* "g+" and "g-": undo or redo along the timeline */
6294 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006295 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006296 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006297 break;
6298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 default:
6300 clearopbeep(oap);
6301 break;
6302 }
6303}
6304
6305/*
6306 * Handle "o" and "O" commands.
6307 */
6308 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006309n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006311#ifdef FEAT_CONCEAL
6312 linenr_T oldline = curwin->w_cursor.lnum;
6313#endif
6314
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (!checkclearopq(cap->oap))
6316 {
6317#ifdef FEAT_FOLDING
6318 if (cap->cmdchar == 'O')
6319 /* Open above the first line of a folded sequence of lines */
6320 (void)hasFolding(curwin->w_cursor.lnum,
6321 &curwin->w_cursor.lnum, NULL);
6322 else
6323 /* Open below the last line of a folded sequence of lines */
6324 (void)hasFolding(curwin->w_cursor.lnum,
6325 NULL, &curwin->w_cursor.lnum);
6326#endif
6327 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6328 (cap->cmdchar == 'O' ? 1 : 0)),
6329 (linenr_T)(curwin->w_cursor.lnum +
6330 (cap->cmdchar == 'o' ? 1 : 0))
6331 ) == OK
6332 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006333 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6334 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006336#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006337 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006338 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006339#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006340#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006341 if (curwin->w_p_cul)
6342 /* force redraw of cursorline */
6343 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006344#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006345 /* When '#' is in 'cpoptions' ignore the count. */
6346 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6347 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6349 }
6350 }
6351}
6352
6353/*
6354 * "." command: redo last change.
6355 */
6356 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006357nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 if (!checkclearopq(cap->oap))
6360 {
6361 /*
6362 * If "restart_edit" is TRUE, the last but one command is repeated
6363 * instead of the last command (inserting text). This is used for
6364 * CTRL-O <.> in insert mode.
6365 */
6366 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6367 clearopbeep(cap->oap);
6368 }
6369}
6370
6371/*
6372 * CTRL-R: undo undo
6373 */
6374 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006375nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 if (!checkclearopq(cap->oap))
6378 {
6379 u_redo((int)cap->count1);
6380 curwin->w_set_curswant = TRUE;
6381 }
6382}
6383
6384/*
6385 * Handle "U" command.
6386 */
6387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006388nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
6390 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006391 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
6393 /* translate "gUU" to "gUgU" */
6394 cap->cmdchar = 'g';
6395 cap->nchar = 'U';
6396 nv_operator(cap);
6397 }
6398 else if (!checkclearopq(cap->oap))
6399 {
6400 u_undoline();
6401 curwin->w_set_curswant = TRUE;
6402 }
6403}
6404
6405/*
6406 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6407 * single character.
6408 */
6409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006410nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006412 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006413 {
6414#ifdef FEAT_JOB_CHANNEL
6415 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6416 {
6417 clearopbeep(cap->oap);
6418 return;
6419 }
6420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 else
6424 nv_operator(cap);
6425}
6426
6427/*
6428 * Handle an operator command.
6429 * The actual work is done by do_pending_operator().
6430 */
6431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006432nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 int op_type;
6435
6436 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006437#ifdef FEAT_JOB_CHANNEL
6438 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6439 {
6440 clearopbeep(cap->oap);
6441 return;
6442 }
6443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
6445 if (op_type == cap->oap->op_type) /* double operator works on lines */
6446 nv_lineop(cap);
6447 else if (!checkclearop(cap->oap))
6448 {
6449 cap->oap->start = curwin->w_cursor;
6450 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006451#ifdef FEAT_EVAL
6452 set_op_var(op_type);
6453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
6455}
6456
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006457#ifdef FEAT_EVAL
6458/*
6459 * Set v:operator to the characters for "optype".
6460 */
6461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006462set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006463{
6464 char_u opchars[3];
6465
6466 if (optype == OP_NOP)
6467 set_vim_var_string(VV_OP, NULL, 0);
6468 else
6469 {
6470 opchars[0] = get_op_char(optype);
6471 opchars[1] = get_extra_op_char(optype);
6472 opchars[2] = NUL;
6473 set_vim_var_string(VV_OP, opchars, -1);
6474 }
6475}
6476#endif
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478/*
6479 * Handle linewise operator "dd", "yy", etc.
6480 *
6481 * "_" is is a strange motion command that helps make operators more logical.
6482 * It is actually implemented, but not documented in the real Vi. This motion
6483 * command actually refers to "the current line". Commands like "dd" and "yy"
6484 * are really an alternate form of "d_" and "y_". It does accept a count, so
6485 * "d3_" works to delete 3 lines.
6486 */
6487 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006488nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 cap->oap->motion_type = MLINE;
6491 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6492 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006493 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
6494 && cap->oap->motion_force != 'v'
6495 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 || cap->oap->op_type == OP_LSHIFT
6497 || cap->oap->op_type == OP_RSHIFT)
6498 beginline(BL_SOL | BL_FIX);
6499 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
6500 beginline(BL_WHITE | BL_FIX);
6501}
6502
6503/*
6504 * <Home> command.
6505 */
6506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006507nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006509 /* CTRL-HOME is like "gg" */
6510 if (mod_mask & MOD_MASK_CTRL)
6511 nv_goto(cap);
6512 else
6513 {
6514 cap->count0 = 1;
6515 nv_pipe(cap);
6516 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00006517 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
6518 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519}
6520
6521/*
6522 * "|" command.
6523 */
6524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006525nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 cap->oap->motion_type = MCHAR;
6528 cap->oap->inclusive = FALSE;
6529 beginline(0);
6530 if (cap->count0 > 0)
6531 {
6532 coladvance((colnr_T)(cap->count0 - 1));
6533 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6534 }
6535 else
6536 curwin->w_curswant = 0;
6537 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01006538 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 curwin->w_set_curswant = FALSE;
6540}
6541
6542/*
6543 * Handle back-word command "b" and "B".
6544 * cap->arg is 1 for "B"
6545 */
6546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006547nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 cap->oap->motion_type = MCHAR;
6550 cap->oap->inclusive = FALSE;
6551 curwin->w_set_curswant = TRUE;
6552 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6553 clearopbeep(cap->oap);
6554#ifdef FEAT_FOLDING
6555 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6556 foldOpenCursor();
6557#endif
6558}
6559
6560/*
6561 * Handle word motion commands "e", "E", "w" and "W".
6562 * cap->arg is TRUE for "E" and "W".
6563 */
6564 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006565nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
6567 int n;
6568 int word_end;
6569 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006570 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 /*
6573 * Set inclusive for the "E" and "e" command.
6574 */
6575 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6576 word_end = TRUE;
6577 else
6578 word_end = FALSE;
6579 cap->oap->inclusive = word_end;
6580
6581 /*
6582 * "cw" and "cW" are a special case.
6583 */
6584 if (!word_end && cap->oap->op_type == OP_CHANGE)
6585 {
6586 n = gchar_cursor();
6587 if (n != NUL) /* not an empty line */
6588 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006589 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 {
6591 /*
6592 * Reproduce a funny Vi behaviour: "cw" on a blank only
6593 * changes one character, not all blanks until the start of
6594 * the next word. Only do this when the 'w' flag is included
6595 * in 'cpoptions'.
6596 */
6597 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6598 {
6599 cap->oap->inclusive = TRUE;
6600 cap->oap->motion_type = MCHAR;
6601 return;
6602 }
6603 }
6604 else
6605 {
6606 /*
6607 * This is a little strange. To match what the real Vi does,
6608 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6609 * that we are not on a space or a TAB. This seems impolite
6610 * at first, but it's really more what we mean when we say
6611 * 'cw'.
6612 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006613 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 * will change only one character! This is done by setting
6615 * flag.
6616 */
6617 cap->oap->inclusive = TRUE;
6618 word_end = TRUE;
6619 flag = TRUE;
6620 }
6621 }
6622 }
6623
6624 cap->oap->motion_type = MCHAR;
6625 curwin->w_set_curswant = TRUE;
6626 if (word_end)
6627 n = end_word(cap->count1, cap->arg, flag, FALSE);
6628 else
6629 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6630
Bram Moolenaardfefb982008-04-01 10:06:39 +00006631 /* Don't leave the cursor on the NUL past the end of line. Unless we
6632 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006633 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006634 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636 if (n == FAIL && cap->oap->op_type == OP_NOP)
6637 clearopbeep(cap->oap);
6638 else
6639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641#ifdef FEAT_FOLDING
6642 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6643 foldOpenCursor();
6644#endif
6645 }
6646}
6647
6648/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006649 * Used after a movement command: If the cursor ends up on the NUL after the
6650 * end of the line, may move it back to the last character and make the motion
6651 * inclusive.
6652 */
6653 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006654adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006655{
6656 /* The cursor cannot remain on the NUL when:
6657 * - the column is > 0
6658 * - not in Visual mode or 'selection' is "o"
6659 * - 'virtualedit' is not "all" and not "onemore".
6660 */
6661 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006662 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006663 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006664 {
6665 --curwin->w_cursor.col;
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006666 /* prevent cursor from moving on the trail byte */
6667 if (has_mbyte)
6668 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006669 oap->inclusive = TRUE;
6670 }
6671}
6672
6673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 * "0" and "^" commands.
6675 * cap->arg is the argument for beginline().
6676 */
6677 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006678nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 cap->oap->motion_type = MCHAR;
6681 cap->oap->inclusive = FALSE;
6682 beginline(cap->arg);
6683#ifdef FEAT_FOLDING
6684 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6685 foldOpenCursor();
6686#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00006687 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
6688 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689}
6690
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691/*
6692 * In exclusive Visual mode, may include the last character.
6693 */
6694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006695adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006698 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (has_mbyte)
6701 inc_cursor();
6702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 ++curwin->w_cursor.col;
6704 cap->oap->inclusive = FALSE;
6705 }
6706}
6707
6708/*
6709 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6710 * Should check VIsual_mode before calling this.
6711 * Returns TRUE when backed up to the previous line.
6712 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006713 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006714unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
6716 pos_T *pp;
6717
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006718 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006720 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 pp = &curwin->w_cursor;
6722 else
6723 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if (pp->coladd > 0)
6725 --pp->coladd;
6726 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 if (pp->col > 0)
6728 {
6729 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006730 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
6732 else if (pp->lnum > 1)
6733 {
6734 --pp->lnum;
6735 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6736 return TRUE;
6737 }
6738 }
6739 return FALSE;
6740}
6741
6742/*
6743 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6744 */
6745 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006746nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
6748 if (VIsual_active)
6749 VIsual_select = TRUE;
6750 else if (VIsual_reselect)
6751 {
6752 cap->nchar = 'v'; /* fake "gv" command */
6753 cap->arg = TRUE;
6754 nv_g_cmd(cap);
6755 }
6756}
6757
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759/*
6760 * "G", "gg", CTRL-END, CTRL-HOME.
6761 * cap->arg is TRUE for "G".
6762 */
6763 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006764nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766 linenr_T lnum;
6767
6768 if (cap->arg)
6769 lnum = curbuf->b_ml.ml_line_count;
6770 else
6771 lnum = 1L;
6772 cap->oap->motion_type = MLINE;
6773 setpcmark();
6774
6775 /* When a count is given, use it instead of the default lnum */
6776 if (cap->count0 != 0)
6777 lnum = cap->count0;
6778 if (lnum < 1L)
6779 lnum = 1L;
6780 else if (lnum > curbuf->b_ml.ml_line_count)
6781 lnum = curbuf->b_ml.ml_line_count;
6782 curwin->w_cursor.lnum = lnum;
6783 beginline(BL_SOL | BL_FIX);
6784#ifdef FEAT_FOLDING
6785 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6786 foldOpenCursor();
6787#endif
6788}
6789
6790/*
6791 * CTRL-\ in Normal mode.
6792 */
6793 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006794nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6797 {
6798 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006799 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 clear_cmdline = TRUE; /* unshow mode later */
6801 restart_edit = 0;
6802#ifdef FEAT_CMDWIN
6803 if (cmdwin_type != 0)
6804 cmdwin_result = Ctrl_C;
6805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (VIsual_active)
6807 {
6808 end_visual_mode(); /* stop Visual */
6809 redraw_curbuf_later(INVERTED);
6810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
6812 if (cap->nchar == Ctrl_G && p_im)
6813 restart_edit = 'a';
6814 }
6815 else
6816 clearopbeep(cap->oap);
6817}
6818
6819/*
6820 * ESC in Normal mode: beep, but don't flush buffers.
6821 * Don't even beep if we are canceling a command.
6822 */
6823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006824nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
6826 int no_reason;
6827
6828 no_reason = (cap->oap->op_type == OP_NOP
6829 && cap->opcount == 0
6830 && cap->count0 == 0
6831 && cap->oap->regname == 0
6832 && !p_im);
6833
6834 if (cap->arg) /* TRUE for CTRL-C */
6835 {
6836 if (restart_edit == 0
6837#ifdef FEAT_CMDWIN
6838 && cmdwin_type == 0
6839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006842 {
6843 if (anyBufIsChanged())
6844 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6845 else
6846 msg(_("Type :qa and press <Enter> to exit Vim"));
6847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848
6849 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
6850 * set again below when halfway a mapping. */
6851 if (!p_im)
6852 restart_edit = 0;
6853#ifdef FEAT_CMDWIN
6854 if (cmdwin_type != 0)
6855 {
6856 cmdwin_result = K_IGNORE;
6857 got_int = FALSE; /* don't stop executing autocommands et al. */
6858 return;
6859 }
6860#endif
6861 }
6862
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 if (VIsual_active)
6864 {
6865 end_visual_mode(); /* stop Visual */
6866 check_cursor_col(); /* make sure cursor is not beyond EOL */
6867 curwin->w_set_curswant = TRUE;
6868 redraw_curbuf_later(INVERTED);
6869 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006870 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006871 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 clearop(cap->oap);
6873
6874 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
6875 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01006876 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 restart_edit = 'a';
6878}
6879
6880/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006881 * Move the cursor for the "A" command.
6882 */
6883 void
6884set_cursor_for_append_to_line(void)
6885{
6886 curwin->w_set_curswant = TRUE;
6887 if (ve_flags == VE_ALL)
6888 {
6889 int save_State = State;
6890
6891 /* Pretend Insert mode here to allow the cursor on the
6892 * character past the end of the line */
6893 State = INSERT;
6894 coladvance((colnr_T)MAXCOL);
6895 State = save_State;
6896 }
6897 else
6898 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6899}
6900
6901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006903 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 */
6905 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006906nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907{
6908 /* <Insert> is equal to "i" */
6909 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6910 cap->cmdchar = 'i';
6911
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 /* in Visual mode "A" and "I" are an operator */
6913 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006914 {
6915#ifdef FEAT_TERMINAL
6916 if (term_in_normal_mode())
6917 {
6918 end_visual_mode();
6919 clearop(cap->oap);
6920 term_enter_job_mode();
6921 return;
6922 }
6923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006928 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6929 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 {
6931#ifdef FEAT_TEXTOBJ
6932 nv_object(cap);
6933#else
6934 clearopbeep(cap->oap);
6935#endif
6936 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006937#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006938 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006939 {
6940 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006941 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006942 return;
6943 }
6944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 else if (!curbuf->b_p_ma && !p_im)
6946 {
6947 /* Only give this error when 'insertmode' is off. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006948 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006950 if (cap->cmdchar == K_PS)
6951 /* drop the pasted text */
6952 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 }
Bram Moolenaara1891842017-02-04 21:34:31 +01006954 else if (cap->cmdchar == K_PS && VIsual_active)
6955 {
6956 pos_T old_pos = curwin->w_cursor;
6957 pos_T old_visual = VIsual;
6958
6959 /* In Visual mode the selected text is deleted. */
6960 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
6961 {
6962 shift_delete_registers();
6963 cap->oap->regname = '1';
6964 }
6965 else
6966 cap->oap->regname = '-';
6967 cap->cmdchar = 'd';
6968 cap->nchar = NUL;
6969 nv_operator(cap);
6970 do_pending_operator(cap, 0, FALSE);
6971 cap->cmdchar = K_PS;
6972
6973 /* When the last char in the line was deleted then append. Detect this
6974 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006975 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
6976 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01006977 inc_cursor();
6978
6979 /* Insert to replace the deleted text with the pasted text. */
6980 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
6981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 else if (!checkclearopq(cap->oap))
6983 {
6984 switch (cap->cmdchar)
6985 {
6986 case 'A': /* "A"ppend after the line */
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006987 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 break;
6989
6990 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006991 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
6992 beginline(BL_WHITE);
6993 else
6994 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 break;
6996
Bram Moolenaara1891842017-02-04 21:34:31 +01006997 case K_PS:
6998 /* Bracketed paste works like "a"ppend, unless the cursor is in
6999 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007000 if (curwin->w_cursor.col == 0)
7001 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007002 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007003
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 case 'a': /* "a"ppend is like "i"nsert on the next character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 /* increment coladd when in virtual space, increment the
7006 * column otherwise, also to append after an unprintable char */
7007 if (virtual_active()
7008 && (curwin->w_cursor.coladd > 0
7009 || *ml_get_cursor() == NUL
7010 || *ml_get_cursor() == TAB))
7011 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007012 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 inc_cursor();
7014 break;
7015 }
7016
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7018 {
7019 int save_State = State;
7020
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007021 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 * character past the end of the line */
7023 State = INSERT;
7024 coladvance(getviscol());
7025 State = save_State;
7026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
7028 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7029 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007030 else if (cap->cmdchar == K_PS)
7031 /* drop the pasted text */
7032 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033}
7034
7035/*
7036 * Invoke edit() and take care of "restart_edit" and the return value.
7037 */
7038 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007039invoke_edit(
7040 cmdarg_T *cap,
7041 int repl, /* "r" or "gr" command */
7042 int cmd,
7043 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 int restart_edit_save = 0;
7046
7047 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
7048 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7049 * it. */
7050 if (repl || !stuff_empty())
7051 restart_edit_save = restart_edit;
7052 else
7053 restart_edit_save = 0;
7054
7055 /* Always reset "restart_edit", this is not a restarted edit. */
7056 restart_edit = 0;
7057
7058 if (edit(cmd, startln, cap->count1))
7059 cap->retval |= CA_COMMAND_BUSY;
7060
7061 if (restart_edit == 0)
7062 restart_edit = restart_edit_save;
7063}
7064
7065#ifdef FEAT_TEXTOBJ
7066/*
7067 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7068 */
7069 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007070nv_object(
7071 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
7073 int flag;
7074 int include;
7075 char_u *mps_save;
7076
7077 if (cap->cmdchar == 'i')
7078 include = FALSE; /* "ix" = inner object: exclude white space */
7079 else
7080 include = TRUE; /* "ax" = an object: include white space */
7081
7082 /* Make sure (), [], {} and <> are in 'matchpairs' */
7083 mps_save = curbuf->b_p_mps;
7084 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7085
7086 switch (cap->nchar)
7087 {
7088 case 'w': /* "aw" = a word */
7089 flag = current_word(cap->oap, cap->count1, include, FALSE);
7090 break;
7091 case 'W': /* "aW" = a WORD */
7092 flag = current_word(cap->oap, cap->count1, include, TRUE);
7093 break;
7094 case 'b': /* "ab" = a braces block */
7095 case '(':
7096 case ')':
7097 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7098 break;
7099 case 'B': /* "aB" = a Brackets block */
7100 case '{':
7101 case '}':
7102 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7103 break;
7104 case '[': /* "a[" = a [] block */
7105 case ']':
7106 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7107 break;
7108 case '<': /* "a<" = a <> block */
7109 case '>':
7110 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7111 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007112 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007113 /* Do not adjust oap->end in do_pending_operator()
7114 * otherwise there are different results for 'dit'
7115 * (note leading whitespace in last line):
7116 * 1) <b> 2) <b>
7117 * foobar foobar
7118 * </b> </b>
7119 */
7120 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007121 flag = current_tagblock(cap->oap, cap->count1, include);
7122 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 case 'p': /* "ap" = a paragraph */
7124 flag = current_par(cap->oap, cap->count1, include, 'p');
7125 break;
7126 case 's': /* "as" = a sentence */
7127 flag = current_sent(cap->oap, cap->count1, include);
7128 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007129 case '"': /* "a"" = a double quoted string */
7130 case '\'': /* "a'" = a single quoted string */
7131 case '`': /* "a`" = a backtick quoted string */
7132 flag = current_quote(cap->oap, cap->count1, include,
7133 cap->nchar);
7134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135#if 0 /* TODO */
7136 case 'S': /* "aS" = a section */
7137 case 'f': /* "af" = a filename */
7138 case 'u': /* "au" = a URL */
7139#endif
7140 default:
7141 flag = FAIL;
7142 break;
7143 }
7144
7145 curbuf->b_p_mps = mps_save;
7146 if (flag == FAIL)
7147 clearopbeep(cap->oap);
7148 adjust_cursor_col();
7149 curwin->w_set_curswant = TRUE;
7150}
7151#endif
7152
7153/*
7154 * "q" command: Start/stop recording.
7155 * "q:", "q/", "q?": edit command-line in command-line window.
7156 */
7157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007158nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159{
7160 if (cap->oap->op_type == OP_FORMAT)
7161 {
7162 /* "gqq" is the same as "gqgq": format line */
7163 cap->cmdchar = 'g';
7164 cap->nchar = 'q';
7165 nv_operator(cap);
7166 }
7167 else if (!checkclearop(cap->oap))
7168 {
7169#ifdef FEAT_CMDWIN
7170 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7171 {
7172 stuffcharReadbuff(cap->nchar);
7173 stuffcharReadbuff(K_CMDWIN);
7174 }
7175 else
7176#endif
7177 /* (stop) recording into a named register, unless executing a
7178 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007179 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 clearopbeep(cap->oap);
7181 }
7182}
7183
7184/*
7185 * Handle the "@r" command.
7186 */
7187 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007188nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 if (checkclearop(cap->oap))
7191 return;
7192#ifdef FEAT_EVAL
7193 if (cap->nchar == '=')
7194 {
7195 if (get_expr_register() == NUL)
7196 return;
7197 }
7198#endif
7199 while (cap->count1-- && !got_int)
7200 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007201 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
7203 clearopbeep(cap->oap);
7204 break;
7205 }
7206 line_breakcheck();
7207 }
7208}
7209
7210/*
7211 * Handle the CTRL-U and CTRL-D commands.
7212 */
7213 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007214nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7217 || (cap->cmdchar == Ctrl_D
7218 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7219 clearopbeep(cap->oap);
7220 else if (!checkclearop(cap->oap))
7221 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7222}
7223
7224/*
7225 * Handle "J" or "gJ" command.
7226 */
7227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007228nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 if (VIsual_active) /* join the visual lines */
7231 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007232 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 {
7234 if (cap->count0 <= 1)
7235 cap->count0 = 2; /* default for join is two lines! */
7236 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7237 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007239 /* can't join when on the last line */
7240 if (cap->count0 <= 2)
7241 {
7242 clearopbeep(cap->oap);
7243 return;
7244 }
7245 cap->count0 = curbuf->b_ml.ml_line_count
7246 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007248
7249 prep_redo(cap->oap->regname, cap->count0,
7250 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7251 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 }
7253}
7254
7255/*
7256 * "P", "gP", "p" and "gp" commands.
7257 */
7258 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007259nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007261 nv_put_opt(cap, FALSE);
7262}
7263
7264/*
7265 * "P", "gP", "p" and "gp" commands.
7266 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7267 */
7268 static void
7269nv_put_opt(cmdarg_T *cap, int fix_indent)
7270{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 int regname = 0;
7272 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007273 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007274 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 int dir;
7276 int flags = 0;
7277
7278 if (cap->oap->op_type != OP_NOP)
7279 {
7280#ifdef FEAT_DIFF
7281 /* "dp" is ":diffput" */
7282 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7283 {
7284 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007285 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 }
7287 else
7288#endif
7289 clearopbeep(cap->oap);
7290 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007291#ifdef FEAT_JOB_CHANNEL
7292 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7293 {
7294 clearopbeep(cap->oap);
7295 }
7296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 else
7298 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007299 if (fix_indent)
7300 {
7301 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7302 ? FORWARD : BACKWARD;
7303 flags |= PUT_FIXINDENT;
7304 }
7305 else
7306 dir = (cap->cmdchar == 'P'
7307 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 ? BACKWARD : FORWARD;
7309 prep_redo_cmd(cap);
7310 if (cap->cmdchar == 'g')
7311 flags |= PUT_CURSEND;
7312
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 if (VIsual_active)
7314 {
7315 /* Putting in Visual mode: The put text replaces the selected
7316 * text. First delete the selected text, then put the new text.
7317 * Need to save and restore the registers that the delete
7318 * overwrites if the old contents is being put.
7319 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007320 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007322#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007324#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007325 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007326 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007327#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330
7331 )
7332 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007333 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 * put, save it first. */
7335 reg1 = get_register(regname, TRUE);
7336 }
7337
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007338 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 cap->cmdchar = 'd';
7340 cap->nchar = NUL;
7341 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007342 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 nv_operator(cap);
7344 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007345 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007346 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 /* delete PUT_LINE_BACKWARD; */
7349 cap->oap->regname = regname;
7350
7351 if (reg1 != NULL)
7352 {
7353 /* Delete probably changed the register we want to put, save
7354 * it first. Then put back what was there before the delete. */
7355 reg2 = get_register(regname, FALSE);
7356 put_register(regname, reg1);
7357 }
7358
7359 /* When deleted a linewise Visual area, put the register as
7360 * lines to avoid it joined with the next line. When deletion was
7361 * characterwise, split a line when putting lines. */
7362 if (VIsual_mode == 'V')
7363 flags |= PUT_LINE;
7364 else if (VIsual_mode == 'v')
7365 flags |= PUT_LINE_SPLIT;
7366 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7367 flags |= PUT_LINE_FORWARD;
7368 dir = BACKWARD;
7369 if ((VIsual_mode != 'V'
7370 && curwin->w_cursor.col < curbuf->b_op_start.col)
7371 || (VIsual_mode == 'V'
7372 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
7373 /* cursor is at the end of the line or end of file, put
7374 * forward. */
7375 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007376 /* May have been reset in do_put(). */
7377 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 do_put(cap->oap->regname, dir, cap->count1, flags);
7380
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 /* If a register was saved, put it back now. */
7382 if (reg2 != NULL)
7383 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007384
7385 /* What to reselect with "gv"? Selecting the just put text seems to
7386 * be the most useful, since the original text was removed. */
7387 if (was_visual)
7388 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007389 curbuf->b_visual.vi_start = curbuf->b_op_start;
7390 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007391 /* need to adjust cursor position */
7392 if (*p_sel == 'e')
7393 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007394 }
7395
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007396 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007397 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007398 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007399 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007400 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007401 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007402
7403 /* If the cursor was in that line, move it to the end of the last
7404 * line. */
7405 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7406 {
7407 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7408 coladvance((colnr_T)MAXCOL);
7409 }
7410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 auto_format(FALSE, TRUE);
7412 }
7413}
7414
7415/*
7416 * "o" and "O" commands.
7417 */
7418 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007419nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421#ifdef FEAT_DIFF
7422 /* "do" is ":diffget" */
7423 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7424 {
7425 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007426 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 else
7429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (VIsual_active) /* switch start and end of visual */
7431 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007432#ifdef FEAT_JOB_CHANNEL
7433 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007434 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 n_opencmd(cap);
7438}
7439
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440#ifdef FEAT_NETBEANS_INTG
7441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007442nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
7444 netbeans_keycommand(cap->nchar);
7445}
7446#endif
7447
7448#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007450nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451{
7452 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7453}
7454#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007455
Bram Moolenaar3918c952005-03-15 22:34:55 +00007456/*
7457 * Trigger CursorHold event.
7458 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7459 * input buffer. "did_cursorhold" is set to avoid retriggering.
7460 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007462nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007463{
7464 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7465 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00007466 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007467}