blob: c70971f8e98fee6cac79b7f1b8c8d777c23d1476 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010020#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010022#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020023static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
29/*
30 * nv_*(): functions called to handle Normal and Visual mode commands.
31 * n_*(): functions called to handle Normal mode commands.
32 * v_*(): functions called to handle Visual mode commands.
33 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void nv_ignore(cmdarg_T *cap);
35static void nv_nop(cmdarg_T *cap);
36static void nv_error(cmdarg_T *cap);
37static void nv_help(cmdarg_T *cap);
38static void nv_addsub(cmdarg_T *cap);
39static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_ver_scrollbar(cmdarg_T *cap);
43static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000045#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void nv_tabline(cmdarg_T *cap);
47static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000048#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static void nv_exmode(cmdarg_T *cap);
50static void nv_colon(cmdarg_T *cap);
51static void nv_ctrlg(cmdarg_T *cap);
52static void nv_ctrlh(cmdarg_T *cap);
53static void nv_clear(cmdarg_T *cap);
54static void nv_ctrlo(cmdarg_T *cap);
55static void nv_hat(cmdarg_T *cap);
56static void nv_Zet(cmdarg_T *cap);
57static void nv_ident(cmdarg_T *cap);
58static void nv_tagpop(cmdarg_T *cap);
59static void nv_scroll(cmdarg_T *cap);
60static void nv_right(cmdarg_T *cap);
61static void nv_left(cmdarg_T *cap);
62static void nv_up(cmdarg_T *cap);
63static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void nv_end(cmdarg_T *cap);
65static void nv_dollar(cmdarg_T *cap);
66static void nv_search(cmdarg_T *cap);
67static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020068static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_csearch(cmdarg_T *cap);
70static void nv_brackets(cmdarg_T *cap);
71static void nv_percent(cmdarg_T *cap);
72static void nv_brace(cmdarg_T *cap);
73static void nv_mark(cmdarg_T *cap);
74static void nv_findpar(cmdarg_T *cap);
75static void nv_undo(cmdarg_T *cap);
76static void nv_kundo(cmdarg_T *cap);
77static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
80static void v_visop(cmdarg_T *cap);
81static void nv_subst(cmdarg_T *cap);
82static void nv_abbrev(cmdarg_T *cap);
83static void nv_optrans(cmdarg_T *cap);
84static void nv_gomark(cmdarg_T *cap);
85static void nv_pcmark(cmdarg_T *cap);
86static void nv_regname(cmdarg_T *cap);
87static void nv_visual(cmdarg_T *cap);
88static void n_start_visual_mode(int c);
89static void nv_window(cmdarg_T *cap);
90static void nv_suspend(cmdarg_T *cap);
91static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_dot(cmdarg_T *cap);
93static void nv_redo(cmdarg_T *cap);
94static void nv_Undo(cmdarg_T *cap);
95static void nv_tilde(cmdarg_T *cap);
96static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_lineop(cmdarg_T *cap);
101static void nv_home(cmdarg_T *cap);
102static void nv_pipe(cmdarg_T *cap);
103static void nv_bck_word(cmdarg_T *cap);
104static void nv_wordcmd(cmdarg_T *cap);
105static void nv_beginline(cmdarg_T *cap);
106static void adjust_cursor(oparg_T *oap);
107static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100108static void nv_select(cmdarg_T *cap);
109static void nv_goto(cmdarg_T *cap);
110static void nv_normal(cmdarg_T *cap);
111static void nv_esc(cmdarg_T *oap);
112static void nv_edit(cmdarg_T *cap);
113static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_record(cmdarg_T *cap);
118static void nv_at(cmdarg_T *cap);
119static void nv_halfpage(cmdarg_T *cap);
120static void nv_join(cmdarg_T *cap);
121static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200122static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100130static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000132static char *e_noident = N_("E349: No identifier under cursor");
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
135 * Function to be called for a Normal or Visual mode command.
136 * The argument is a cmdarg_T.
137 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100138typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100140// Values for cmd_flags.
141#define NV_NCH 0x01 // may need to get a second char
142#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
143#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
144#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100146#define NV_SS 0x10 // may start selection
147#define NV_SSS 0x20 // may start selection with shift modifier
148#define NV_STS 0x40 // may stop selection without shift modif.
149#define NV_RL 0x80 // 'rightleft' modifies command
150#define NV_KEEPREG 0x100 // don't clear regname
151#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153/*
154 * Generally speaking, every Normal mode command should either clear any
155 * pending operator (with *clearop*()), or set the motion type variable
156 * oap->motion_type.
157 *
158 * When a cursor motion command is made, it is marked as being a character or
159 * line oriented motion. Then, if an operator is in effect, the operation
160 * becomes character or line oriented accordingly.
161 */
162
163/*
164 * This table contains one entry for every Normal or Visual mode command.
165 * The order doesn't matter, init_normal_cmds() will create a sorted index.
166 * It is faster when all keys from zero to '~' are present.
167 */
168static const struct nv_cmd
169{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100170 int cmd_char; // (first) command character
171 nv_func_T cmd_func; // function for this command
172 short_u cmd_flags; // NV_ flags
173 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174} nv_cmds[] =
175{
176 {NUL, nv_error, 0, 0},
177 {Ctrl_A, nv_addsub, 0, 0},
178 {Ctrl_B, nv_page, NV_STS, BACKWARD},
179 {Ctrl_C, nv_esc, 0, TRUE},
180 {Ctrl_D, nv_halfpage, 0, 0},
181 {Ctrl_E, nv_scroll_line, 0, TRUE},
182 {Ctrl_F, nv_page, NV_STS, FORWARD},
183 {Ctrl_G, nv_ctrlg, 0, 0},
184 {Ctrl_H, nv_ctrlh, 0, 0},
185 {Ctrl_I, nv_pcmark, 0, 0},
186 {NL, nv_down, 0, FALSE},
187 {Ctrl_K, nv_error, 0, 0},
188 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100189 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {Ctrl_N, nv_down, NV_STS, FALSE},
191 {Ctrl_O, nv_ctrlo, 0, 0},
192 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000193 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {Ctrl_R, nv_redo, 0, 0},
195 {Ctrl_S, nv_ignore, 0, 0},
196 {Ctrl_T, nv_tagpop, NV_NCW, 0},
197 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_V, nv_visual, 0, FALSE},
199 {'V', nv_visual, 0, FALSE},
200 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {Ctrl_W, nv_window, 0, 0},
202 {Ctrl_X, nv_addsub, 0, 0},
203 {Ctrl_Y, nv_scroll_line, 0, FALSE},
204 {Ctrl_Z, nv_suspend, 0, 0},
205 {ESC, nv_esc, 0, FALSE},
206 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
207 {Ctrl_RSB, nv_ident, NV_NCW, 0},
208 {Ctrl_HAT, nv_hat, NV_NCW, 0},
209 {Ctrl__, nv_error, 0, 0},
210 {' ', nv_right, 0, 0},
211 {'!', nv_operator, 0, 0},
212 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
213 {'#', nv_ident, 0, 0},
214 {'$', nv_dollar, 0, 0},
215 {'%', nv_percent, 0, 0},
216 {'&', nv_optrans, 0, 0},
217 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
218 {'(', nv_brace, 0, BACKWARD},
219 {')', nv_brace, 0, FORWARD},
220 {'*', nv_ident, 0, 0},
221 {'+', nv_down, 0, TRUE},
222 {',', nv_csearch, 0, TRUE},
223 {'-', nv_up, 0, TRUE},
224 {'.', nv_dot, NV_KEEPREG, 0},
225 {'/', nv_search, 0, FALSE},
226 {'0', nv_beginline, 0, 0},
227 {'1', nv_ignore, 0, 0},
228 {'2', nv_ignore, 0, 0},
229 {'3', nv_ignore, 0, 0},
230 {'4', nv_ignore, 0, 0},
231 {'5', nv_ignore, 0, 0},
232 {'6', nv_ignore, 0, 0},
233 {'7', nv_ignore, 0, 0},
234 {'8', nv_ignore, 0, 0},
235 {'9', nv_ignore, 0, 0},
236 {':', nv_colon, 0, 0},
237 {';', nv_csearch, 0, FALSE},
238 {'<', nv_operator, NV_RL, 0},
239 {'=', nv_operator, 0, 0},
240 {'>', nv_operator, NV_RL, 0},
241 {'?', nv_search, 0, FALSE},
242 {'@', nv_at, NV_NCH_NOP, FALSE},
243 {'A', nv_edit, 0, 0},
244 {'B', nv_bck_word, 0, 1},
245 {'C', nv_abbrev, NV_KEEPREG, 0},
246 {'D', nv_abbrev, NV_KEEPREG, 0},
247 {'E', nv_wordcmd, 0, TRUE},
248 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
249 {'G', nv_goto, 0, TRUE},
250 {'H', nv_scroll, 0, 0},
251 {'I', nv_edit, 0, 0},
252 {'J', nv_join, 0, 0},
253 {'K', nv_ident, 0, 0},
254 {'L', nv_scroll, 0, 0},
255 {'M', nv_scroll, 0, 0},
256 {'N', nv_next, 0, SEARCH_REV},
257 {'O', nv_open, 0, 0},
258 {'P', nv_put, 0, 0},
259 {'Q', nv_exmode, NV_NCW, 0},
260 {'R', nv_Replace, 0, FALSE},
261 {'S', nv_subst, NV_KEEPREG, 0},
262 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
263 {'U', nv_Undo, 0, 0},
264 {'W', nv_wordcmd, 0, TRUE},
265 {'X', nv_abbrev, NV_KEEPREG, 0},
266 {'Y', nv_abbrev, NV_KEEPREG, 0},
267 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
268 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
269 {'\\', nv_error, 0, 0},
270 {']', nv_brackets, NV_NCH_ALW, FORWARD},
271 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
272 {'_', nv_lineop, 0, 0},
273 {'`', nv_gomark, NV_NCH_ALW, FALSE},
274 {'a', nv_edit, NV_NCH, 0},
275 {'b', nv_bck_word, 0, 0},
276 {'c', nv_operator, 0, 0},
277 {'d', nv_operator, 0, 0},
278 {'e', nv_wordcmd, 0, FALSE},
279 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
280 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
281 {'h', nv_left, NV_RL, 0},
282 {'i', nv_edit, NV_NCH, 0},
283 {'j', nv_down, 0, FALSE},
284 {'k', nv_up, 0, FALSE},
285 {'l', nv_right, NV_RL, 0},
286 {'m', nv_mark, NV_NCH_NOP, 0},
287 {'n', nv_next, 0, 0},
288 {'o', nv_open, 0, 0},
289 {'p', nv_put, 0, 0},
290 {'q', nv_record, NV_NCH, 0},
291 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
292 {'s', nv_subst, NV_KEEPREG, 0},
293 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
294 {'u', nv_undo, 0, 0},
295 {'w', nv_wordcmd, 0, FALSE},
296 {'x', nv_abbrev, NV_KEEPREG, 0},
297 {'y', nv_operator, 0, 0},
298 {'z', nv_zet, NV_NCH_ALW, 0},
299 {'{', nv_findpar, 0, BACKWARD},
300 {'|', nv_pipe, 0, 0},
301 {'}', nv_findpar, 0, FORWARD},
302 {'~', nv_tilde, 0, 0},
303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100304 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200306 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
307 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
308 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
309 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 {K_LEFTMOUSE, nv_mouse, 0, 0},
311 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
312 {K_LEFTDRAG, nv_mouse, 0, 0},
313 {K_LEFTRELEASE, nv_mouse, 0, 0},
314 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100315 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
317 {K_MIDDLEDRAG, nv_mouse, 0, 0},
318 {K_MIDDLERELEASE, nv_mouse, 0, 0},
319 {K_RIGHTMOUSE, nv_mouse, 0, 0},
320 {K_RIGHTDRAG, nv_mouse, 0, 0},
321 {K_RIGHTRELEASE, nv_mouse, 0, 0},
322 {K_X1MOUSE, nv_mouse, 0, 0},
323 {K_X1DRAG, nv_mouse, 0, 0},
324 {K_X1RELEASE, nv_mouse, 0, 0},
325 {K_X2MOUSE, nv_mouse, 0, 0},
326 {K_X2DRAG, nv_mouse, 0, 0},
327 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000328 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000329 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {K_INS, nv_edit, 0, 0},
331 {K_KINS, nv_edit, 0, 0},
332 {K_BS, nv_ctrlh, 0, 0},
333 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
334 {K_S_UP, nv_page, NV_SS, BACKWARD},
335 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
336 {K_S_DOWN, nv_page, NV_SS, FORWARD},
337 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
339 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
340 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
341 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
342 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
343 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
344 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
345 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
346 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
347 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
348 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_S_END, nv_end, NV_SS, FALSE},
350 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
351 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
352 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {K_S_HOME, nv_home, NV_SS, 0},
354 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
355 {K_DEL, nv_abbrev, 0, 0},
356 {K_KDEL, nv_abbrev, 0, 0},
357 {K_UNDO, nv_kundo, 0, 0},
358 {K_HELP, nv_help, NV_NCW, 0},
359 {K_F1, nv_help, NV_NCW, 0},
360 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifdef FEAT_GUI
363 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
364 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
365#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#ifdef FEAT_GUI_TABLINE
367 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000368 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#ifdef FEAT_NETBEANS_INTG
371 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
372#endif
373#ifdef FEAT_DND
374 {K_DROP, nv_drop, NV_STS, 0},
375#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100377 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100378 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379};
380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100381// Number of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100384// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static short nv_cmd_idx[NV_CMDS_SIZE];
386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387// The highest index for which
388// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static int nv_max_linear;
390
391/*
392 * Compare functions for qsort() below, that checks the command character
393 * through the index in nv_cmd_idx[].
394 */
395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100396nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
398 int c1, c2;
399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100400 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 c1 = nv_cmds[*(const short *)s1].cmd_char;
402 c2 = nv_cmds[*(const short *)s2].cmd_char;
403 if (c1 < 0)
404 c1 = -c1;
405 if (c2 < 0)
406 c2 = -c2;
407 return c1 - c2;
408}
409
410/*
411 * Initialize the nv_cmd_idx[] table.
412 */
413 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100414init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
416 int i;
417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000419 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 nv_cmd_idx[i] = i;
421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100425 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000426 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
428 break;
429 nv_max_linear = i - 1;
430}
431
432/*
433 * Search for a command in the commands table.
434 * Returns -1 for invalid command.
435 */
436 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100437find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 int i;
440 int idx;
441 int top, bot;
442 int c;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 if (cmdchar >= 0x100)
446 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100448 // We use the absolute value of the character. Special keys have a
449 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (cmdchar < 0)
451 cmdchar = -cmdchar;
452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100453 // If the character is in the first part: The character is the index into
454 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (cmdchar <= nv_max_linear)
456 return nv_cmd_idx[cmdchar];
457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100458 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 bot = nv_max_linear + 1;
460 top = NV_CMDS_SIZE - 1;
461 idx = -1;
462 while (bot <= top)
463 {
464 i = (top + bot) / 2;
465 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
466 if (c < 0)
467 c = -c;
468 if (cmdchar == c)
469 {
470 idx = nv_cmd_idx[i];
471 break;
472 }
473 if (cmdchar > c)
474 bot = i + 1;
475 else
476 top = i - 1;
477 }
478 return idx;
479}
480
481/*
482 * Execute a command in Normal mode.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100485normal_cmd(
486 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100489 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 int old_col = curwin->w_curswant;
493#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 static int old_mapped_len = 0;
499 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000500#ifdef FEAT_EVAL
501 int set_prevcount = FALSE;
502#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200503 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaara80faa82020-04-12 19:37:17 +0200505 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 // Use a count remembered from before entering an operator. After typing
509 // "3d" we return from normal_cmd() and come back here, the "3" is
510 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 ca.opcount = opcount;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 /*
514 * If there is an operator pending, then the command we take this time
515 * will terminate it. Finish_op tells us to finish the operation before
516 * returning this time (unless the operation was cancelled).
517 */
518#ifdef CURSOR_SHAPE
519 c = finish_op;
520#endif
521 finish_op = (oap->op_type != OP_NOP);
522#ifdef CURSOR_SHAPE
523 if (finish_op != c)
524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100525 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# ifdef FEAT_MOUSESHAPE
527 update_mouseshape(-1);
528# endif
529 }
530#endif
531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 // When not finishing an operator and no register name typed, reset the
533 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000537#ifdef FEAT_EVAL
538 set_prevcount = TRUE;
539#endif
540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 // Restore counts from before receiving K_CURSORHOLD. This means after
543 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
544 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000545 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
546 {
547 ca.opcount = oap->prev_opcount;
548 ca.count0 = oap->prev_count0;
549 oap->prev_opcount = 0;
550 oap->prev_count0 = 0;
551 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 State = NORMAL_BUSY;
556#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100560#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 // Set v:count here, when called from main() and not a stuffed
562 // command, so that v:count can be used in an expression mapping
563 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100564 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100565 set_vcount_ca(&ca, &set_prevcount);
566#endif
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /*
569 * Get the command character from the user.
570 */
571 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100572 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 /*
575 * If a mapping was started in Visual or Select mode, remember the length
576 * of the mapping. This is used below to not return to Insert mode for as
577 * long as the mapping is being executed.
578 */
579 if (restart_edit == 0)
580 old_mapped_len = 0;
581 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000582 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 old_mapped_len = typebuf_maplen();
584
585 if (c == NUL)
586 c = K_ZERO;
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 /*
589 * In Select mode, typed text replaces the selection.
590 */
591 if (VIsual_active
592 && VIsual_select
593 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100595 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
596 // 'insertmode' is set) fake a "d"elete command, Insert mode will
597 // restart automatically.
598 // Insert the typed character in the typeahead buffer, so that it can
599 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200600 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000601 if (restart_edit != 0)
602 c = 'd';
603 else
604 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 msg_nowait = TRUE; // don't delay going to insert mode
606 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609#ifdef FEAT_CMDL_INFO
610 need_flushbuf = add_to_showcmd(c);
611#endif
612
613getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 /*
617 * Handle a count before a command and compute ca.count0.
618 * Note that '0' is a command and not the start of a count, but it's
619 * part of a count after other digits.
620 */
621 while ( (c >= '1' && c <= '9')
622 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
623 {
624 if (c == K_DEL || c == K_KDEL)
625 {
626 ca.count0 /= 10;
627#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#endif
630 }
631 else
632 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100633 if (ca.count0 < 0) // got too large!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000635#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100636 // Set v:count here, when called from main() and not a stuffed
637 // command, so that v:count can be used in an expression mapping
638 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100639 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100640 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (ctrl_w)
643 {
644 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100645 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100647 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000648 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 --no_zero_mapping;
651 if (ctrl_w)
652 {
653 --no_mapping;
654 --allow_keys;
655 }
656#ifdef FEAT_CMDL_INFO
657 need_flushbuf |= add_to_showcmd(c);
658#endif
659 }
660
661 /*
662 * If we got CTRL-W there may be a/another count
663 */
664 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
665 {
666 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 ca.count0 = 0;
669 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 ++allow_keys; // no mapping for nchar, but keys
671 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 --no_mapping;
674 --allow_keys;
675#ifdef FEAT_CMDL_INFO
676 need_flushbuf |= add_to_showcmd(c);
677#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 }
681
Bram Moolenaara983fe92008-07-31 20:04:27 +0000682 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 // Save the count values so that ca.opcount and ca.count0 are exactly
685 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000686 oap->prev_opcount = ca.opcount;
687 oap->prev_count0 = ca.count0;
688 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100689 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000690 {
691 /*
692 * If we're in the middle of an operator (including after entering a
693 * yank buffer with '"') AND we had a count before the operator, then
694 * that count overrides the current value of ca.count0.
695 * What this means effectively, is that commands like "3dw" get turned
696 * into "d3w" which makes things fall into place pretty neatly.
697 * If you give a count before AND after the operator, they are
698 * multiplied.
699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (ca.count0)
701 ca.count0 *= ca.opcount;
702 else
703 ca.count0 = ca.opcount;
704 }
705
706 /*
707 * Always remember the count. It will be set to zero (on the next call,
708 * above) when there is no pending operator.
709 * When called from main(), save the count for use by the "count" built-in
710 * variable.
711 */
712 ca.opcount = ca.count0;
713 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
714
715#ifdef FEAT_EVAL
716 /*
717 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100718 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100720 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000721 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#endif
723
724 /*
725 * Find the command character in the table of commands.
726 * For CTRL-W we already got nchar when looking for a count.
727 */
728 if (ctrl_w)
729 {
730 ca.nchar = c;
731 ca.cmdchar = Ctrl_W;
732 }
733 else
734 ca.cmdchar = c;
735 idx = find_command(ca.cmdchar);
736 if (idx < 0)
737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100738 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 clearopbeep(oap);
740 goto normal_end;
741 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000742
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000743 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100745 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000747 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 goto normal_end;
749 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000750 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
751 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 /*
754 * In Visual/Select mode, a few keys are handled in a special way.
755 */
756 if (VIsual_active)
757 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100758 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 if (km_stopsel
760 && (nv_cmds[idx].cmd_flags & NV_STS)
761 && !(mod_mask & MOD_MASK_SHIFT))
762 {
763 end_visual_mode();
764 redraw_curbuf_later(INVERTED);
765 }
766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100767 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (km_startsel)
769 {
770 if (nv_cmds[idx].cmd_flags & NV_SS)
771 {
772 unshift_special(&ca);
773 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000774 if (idx < 0)
775 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100776 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000777 clearopbeep(oap);
778 goto normal_end;
779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
782 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787#ifdef FEAT_RIGHTLEFT
788 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
789 && (nv_cmds[idx].cmd_flags & NV_RL))
790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // Invert horizontal movements and operations. Only when typed by the
792 // user directly, not when the result of a mapping or "x" translated
793 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 switch (ca.cmdchar)
795 {
796 case 'l': ca.cmdchar = 'h'; break;
797 case K_RIGHT: ca.cmdchar = K_LEFT; break;
798 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
799 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
800 case 'h': ca.cmdchar = 'l'; break;
801 case K_LEFT: ca.cmdchar = K_RIGHT; break;
802 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
803 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
804 case '>': ca.cmdchar = '<'; break;
805 case '<': ca.cmdchar = '>'; break;
806 }
807 idx = find_command(ca.cmdchar);
808 }
809#endif
810
811 /*
812 * Get an additional character if we need one.
813 */
814 if ((nv_cmds[idx].cmd_flags & NV_NCH)
815 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
816 && oap->op_type == OP_NOP)
817 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
818 || (ca.cmdchar == 'q'
819 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200820 && reg_recording == 0
821 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100823 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {
825 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100826 int repl = FALSE; // get character for replace mode
827 int lit = FALSE; // get extra character literally
828 int langmap_active = FALSE; // using :lmap mappings
829 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100830#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100831 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#endif
833
834 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100835 ++allow_keys; // no mapping for nchar, but allow key codes
836 // Don't generate a CursorHold event here, most commands can't handle
837 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000838 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (ca.cmdchar == 'g')
840 {
841 /*
842 * For 'g' get the next character now, so that we can check for
843 * "gr", "g'" and "g`".
844 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000845 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifdef FEAT_CMDL_INFO
848 need_flushbuf |= add_to_showcmd(ca.nchar);
849#endif
850 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100851 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100855 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100860 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
862 else
863 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 repl = TRUE;
866 cp = &ca.nchar;
867 }
868 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
869
870 /*
871 * Get a second or third character.
872 */
873 if (cp != NULL)
874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (repl)
876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100877 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100878#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100879 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
883 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100884 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 --no_mapping;
886 --allow_keys;
887 if (repl)
888 State = LREPLACE;
889 else
890 State = LANGMAP;
891 langmap_active = TRUE;
892 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100893#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100895 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
897 im_set_active(TRUE);
898#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200899 if ((State & INSERT) && !p_ek)
900 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200901#ifdef FEAT_JOB_CHANNEL
902 ch_log_output = TRUE;
903#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200904 // Disable bracketed paste and modifyOtherKeys here, we won't
905 // recognize the escape sequences with 'esckeys' off.
906 out_str(T_BD);
907 out_str(T_CTE);
908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000910 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaarca774f62020-08-30 20:46:38 +0200912 if ((State & INSERT) && !p_ek)
913 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200914#ifdef FEAT_JOB_CHANNEL
915 ch_log_output = TRUE;
916#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200917 // Re-enable bracketed paste mode and modifyOtherKeys
918 out_str(T_BE);
919 out_str(T_CTI);
920 }
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (langmap_active)
923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100924 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 ++no_mapping;
926 ++allow_keys;
927 State = NORMAL_BUSY;
928 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100929#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (lang)
931 {
932 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
933 im_save_status(&curbuf->b_p_iminsert);
934 im_set_active(FALSE);
935 }
936 p_smd = save_smd;
937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#ifdef FEAT_CMDL_INFO
940 need_flushbuf |= add_to_showcmd(*cp);
941#endif
942
943 if (!lit)
944 {
945#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100946 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (*cp == Ctrl_K
948 && ((nv_cmds[idx].cmd_flags & NV_LANG)
949 || cp == &ca.extra_char)
950 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
951 {
952 c = get_digraph(FALSE);
953 if (c > 0)
954 {
955 *cp = c;
956# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100957 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 del_from_showcmd(3);
959 need_flushbuf |= add_to_showcmd(*cp);
960# endif
961 }
962 }
963#endif
964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100965 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100968 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (p_hkmap && lang && KeyTyped)
970 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#endif
972 }
973
974 /*
975 * When the next character is CTRL-\ a following CTRL-N means the
976 * command is aborted and we go to Normal mode.
977 */
978 if (cp == &ca.extra_char
979 && ca.nchar == Ctrl_BSL
980 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
981 {
982 ca.cmdchar = Ctrl_BSL;
983 ca.nchar = ca.extra_char;
984 idx = find_command(ca.cmdchar);
985 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200986 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200987 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 else if (*cp == Ctrl_BSL)
989 {
990 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100992 // There is a busy wait here when typing "f<C-\>" and then
993 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 while ((c = vpeekc()) <= 0 && towait > 0L)
995 {
Bram Moolenaare2edc2e2021-01-16 20:21:23 +0100996 do_sleep(towait > 50L ? 50L : towait, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 towait -= 50L;
998 }
999 if (c > 0)
1000 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001001 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (c != Ctrl_N && c != Ctrl_G)
1003 vungetc(c);
1004 else
1005 {
1006 ca.cmdchar = Ctrl_BSL;
1007 ca.nchar = c;
1008 idx = find_command(ca.cmdchar);
1009 }
1010 }
1011 }
1012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001013 // When getting a text character and the next character is a
1014 // multi-byte character, it could be a composing character.
1015 // However, don't wait for it to arrive. Also, do enable mapping,
1016 // because if it's put back with vungetc() it's too late to apply
1017 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001018 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 while (enc_utf8 && lang && (c = vpeekc()) > 0
1020 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1021 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001022 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (!utf_iscomposing(c))
1024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001025 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 break;
1027 }
1028 else if (ca.ncharC1 == 0)
1029 ca.ncharC1 = c;
1030 else
1031 ca.ncharC2 = c;
1032 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001033 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035 --no_mapping;
1036 --allow_keys;
1037 }
1038
1039#ifdef FEAT_CMDL_INFO
1040 /*
1041 * Flush the showcmd characters onto the screen so we can see them while
1042 * the command is being executed. Only do this when the shown command was
1043 * actually displayed, otherwise this will slow down a lot when executing
1044 * mappings.
1045 */
1046 if (need_flushbuf)
1047 out_flush();
1048#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001049 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001050 {
1051 if (ex_normal_busy)
1052 did_cursorhold = save_did_cursorhold;
1053 else
1054 did_cursorhold = FALSE;
1055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
1057 State = NORMAL;
1058
1059 if (ca.nchar == ESC)
1060 {
1061 clearop(oap);
1062 if (restart_edit == 0 && goto_im())
1063 restart_edit = 'a';
1064 goto normal_end;
1065 }
1066
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001067 if (ca.cmdchar != K_IGNORE)
1068 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001069 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001070 msg_col = 0;
1071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001073 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001075 // When 'keymodel' contains "startsel" some keys start Select/Visual
1076 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (!VIsual_active && km_startsel)
1078 {
1079 if (nv_cmds[idx].cmd_flags & NV_SS)
1080 {
1081 start_selection();
1082 unshift_special(&ca);
1083 idx = find_command(ca.cmdchar);
1084 }
1085 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1086 && (mod_mask & MOD_MASK_SHIFT))
1087 {
1088 start_selection();
1089 mod_mask &= ~MOD_MASK_SHIFT;
1090 }
1091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
1093 /*
1094 * Execute the command!
1095 * Call the command function found in the commands table.
1096 */
1097 ca.arg = nv_cmds[idx].cmd_arg;
1098 (nv_cmds[idx].cmd_func)(&ca);
1099
1100 /*
1101 * If we didn't start or finish an operator, reset oap->regname, unless we
1102 * need it later.
1103 */
1104 if (!finish_op
1105 && !oap->op_type
1106 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1107 {
1108 clearop(oap);
1109#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001110 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#endif
1112 }
1113
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001114 // Get the length of mapped chars again after typing a count, second
1115 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001116 if (old_mapped_len > 0)
1117 old_mapped_len = typebuf_maplen();
1118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 /*
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001120 * If an operation is pending, handle it. But not for K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001122 if (ca.cmdchar != K_IGNORE)
1123 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125 /*
1126 * Wait for a moment when a message is displayed that will be overwritten
1127 * by the mode message.
1128 * In Visual mode and with "^O" in Insert mode, a short message will be
1129 * overwritten by the mode message. Wait a bit, until a key is hit.
1130 * In Visual mode, it's more important to keep the Visual area updated
1131 * than keeping a message (e.g. from a /pat search).
1132 * Only do this if the command was typed, not from a mapping.
1133 * Don't wait when emsg_silent is non-zero.
1134 * Also wait a bit after an error message, e.g. for "^O:".
1135 * Don't redraw the screen, it would remove the message.
1136 */
1137 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001138 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 || (VIsual_active
1141 && old_pos.lnum == curwin->w_cursor.lnum
1142 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 )
1144 && (clear_cmdline
1145 || redraw_cmdline)
1146 && (msg_didout || (msg_didany && msg_scroll))
1147 && !msg_nowait
1148 && KeyTyped)
1149 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 && (msg_scroll
1152 || emsg_on_display)))
1153 && oap->regname == 0
1154 && !(ca.retval & CA_COMMAND_BUSY)
1155 && stuff_empty()
1156 && typebuf_typed()
1157 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001158 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 && !did_wait_return
1160 && oap->op_type == OP_NOP)
1161 {
1162 int save_State = State;
1163
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001164 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (restart_edit != 0)
1166 State = INSERT;
1167
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001168 // If need to redraw, and there is a "keep_msg", redraw before the
1169 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1171 {
1172 char_u *kmsg;
1173
1174 kmsg = keep_msg;
1175 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001176 // Showmode() will clear keep_msg, but we want to use it anyway.
1177 // First update w_topline.
1178 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001180 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001182
1183 kmsg = vim_strsave(keep_msg);
1184 if (kmsg != NULL)
1185 {
1186 msg_attr((char *)kmsg, keep_msg_attr);
1187 vim_free(kmsg);
1188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001191#ifdef CURSOR_SHAPE
1192 ui_cursor_shape(); // may show different cursor shape
1193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 cursor_on();
1195 out_flush();
1196 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001197 ui_delay(1003L, TRUE); // wait at least one second
1198 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 State = save_State;
1200
1201 msg_scroll = FALSE;
1202 emsg_on_display = FALSE;
1203 }
1204
1205 /*
1206 * Finish up after executing a Normal mode command.
1207 */
1208normal_end:
1209
1210 msg_nowait = FALSE;
1211
Bram Moolenaarcc613032020-06-07 21:31:18 +02001212#ifdef FEAT_EVAL
1213 if (finish_op)
1214 reset_reg_var();
1215#endif
1216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#ifdef CURSOR_SHAPE
1219 c = finish_op;
1220#endif
1221 finish_op = FALSE;
1222#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001223 // Redraw the cursor with another shape, if we were in Operator-pending
1224 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (c || ca.cmdchar == 'r')
1226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228# ifdef FEAT_MOUSESHAPE
1229 update_mouseshape(-1);
1230# endif
1231 }
1232#endif
1233
1234#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001235 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001236 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 clear_showcmd();
1238#endif
1239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 vim_free(ca.searchbuf);
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (has_mbyte)
1244 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (curwin->w_p_scb && toplevel)
1247 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001248 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 do_check_scrollbind(TRUE);
1250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaar860cae12010-06-05 23:22:07 +02001252 if (curwin->w_p_crb && toplevel)
1253 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 do_check_cursorbind();
1256 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001257
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001258#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001259 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001260 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001261 restart_edit = 0;
1262#endif
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /*
1265 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1266 * if still inside a mapping that started in Visual mode).
1267 * May switch from Visual to Select mode after CTRL-O command.
1268 */
1269 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1271 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && !(ca.retval & CA_COMMAND_BUSY)
1273 && stuff_empty()
1274 && oap->regname == 0)
1275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (restart_VIsual_select == 1)
1277 {
1278 VIsual_select = TRUE;
1279 showmode();
1280 restart_VIsual_select = 0;
1281 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001282 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 (void)edit(restart_edit, FALSE, 1L);
1284 }
1285
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (restart_VIsual_select == 2)
1287 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001289 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 opcount = ca.opcount;
1291}
1292
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001293#ifdef FEAT_EVAL
1294/*
1295 * Set v:count and v:count1 according to "cap".
1296 * Set v:prevcount only when "set_prevcount" is TRUE.
1297 */
1298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001299set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001300{
1301 long count = cap->count0;
1302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001303 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001304 if (cap->opcount != 0)
1305 count = cap->opcount * (count == 0 ? 1 : count);
1306 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001307 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001308}
1309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001312 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * if not.
1314 */
1315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001316check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
1318 static int did_check = FALSE;
1319
1320 if (full_screen)
1321 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001322 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001323 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 did_check = TRUE;
1325 }
1326}
1327
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001328#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1329/*
1330 * Call yank_do_autocmd() for "regname".
1331 */
1332 static void
1333call_yank_do_autocmd(int regname)
1334{
1335 oparg_T oa;
1336 yankreg_T *reg;
1337
1338 clear_oparg(&oa);
1339 oa.regname = regname;
1340 oa.op_type = OP_YANK;
1341 oa.is_VIsual = TRUE;
1342 reg = get_register(regname, TRUE);
1343 yank_do_autocmd(&oa, reg);
1344 free_register(reg);
1345}
1346#endif
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001349 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * This function should ALWAYS be called to end Visual mode, except from
1351 * do_pending_operator().
1352 */
1353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001354end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
1356#ifdef FEAT_CLIPBOARD
1357 /*
1358 * If we are using the clipboard, then remember what was selected in case
1359 * we need to paste it somewhere while we still own the selection.
1360 * Only do this when the clipboard is already owned. Don't want to grab
1361 * the selection when hitting ESC.
1362 */
1363 if (clip_star.available && clip_star.owned)
1364 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001365
1366# if defined(FEAT_EVAL)
1367 // Emit a TextYankPost for the automatic copy of the selection into the
1368 // star and/or plus register.
1369 if (has_textyankpost())
1370 {
1371 if (clip_isautosel_star())
1372 call_yank_do_autocmd('*');
1373 if (clip_isautosel_plus())
1374 call_yank_do_autocmd('+');
1375 }
1376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377#endif
1378
1379 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 setmouse();
1381 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001383 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001384 curbuf->b_visual.vi_mode = VIsual_mode;
1385 curbuf->b_visual.vi_start = VIsual;
1386 curbuf->b_visual.vi_end = curwin->w_cursor;
1387 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388#ifdef FEAT_EVAL
1389 curbuf->b_visual_mode_eval = VIsual_mode;
1390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (!virtual_active())
1392 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001393 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001395 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396}
1397
1398/*
1399 * Reset VIsual_active and VIsual_reselect.
1400 */
1401 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001402reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
1404 if (VIsual_active)
1405 {
1406 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001407 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 VIsual_reselect = FALSE;
1410}
1411
1412/*
1413 * Reset VIsual_active and VIsual_reselect if it's set.
1414 */
1415 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001416reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
1418 if (VIsual_active)
1419 {
1420 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001421 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 VIsual_reselect = FALSE;
1423 }
1424}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001426 void
1427restore_visual_mode(void)
1428{
1429 if (VIsual_mode_orig != NUL)
1430 {
1431 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1432 VIsual_mode_orig = NUL;
1433 }
1434}
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436/*
1437 * Check for a balloon-eval special item to include when searching for an
1438 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1439 * Returns TRUE if the character at "*ptr" should be included.
1440 * "dir" is FORWARD or BACKWARD, the direction of searching.
1441 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1442 * "bnp" points to a counter for square brackets.
1443 */
1444 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001445find_is_eval_item(
1446 char_u *ptr,
1447 int *colp,
1448 int *bnp,
1449 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001451 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1453 ++*bnp;
1454 if (*bnp > 0)
1455 {
1456 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1457 --*bnp;
1458 return TRUE;
1459 }
1460
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001461 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (*ptr == '.')
1463 return TRUE;
1464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001465 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1467 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1468 {
1469 *colp += dir;
1470 return TRUE;
1471 }
1472 return FALSE;
1473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
1475/*
1476 * Find the identifier under or to the right of the cursor.
1477 * "find_type" can have one of three values:
1478 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001479 * FIND_STRING: find any non-white text
1480 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001481 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 *
1483 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001484 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001486 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 * This doesn't match the real Vi but I like it a little better and it
1488 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001489 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 * When FIND_IDENT isn't defined, we backup until a blank.
1491 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001492 * Returns the length of the text, or zero if no text is found.
1493 * If text is found, a pointer to the text is put in "*text". This
1494 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001497find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001500 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501}
1502
1503/*
1504 * Like find_ident_under_cursor(), but for any window and any position.
1505 * However: Uses 'iskeyword' from the current window!.
1506 */
1507 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001508find_ident_at_pos(
1509 win_T *wp,
1510 linenr_T lnum,
1511 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001512 char_u **text,
1513 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001514 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
1516 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001517 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 int this_class = 0;
1520 int prev_class;
1521 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001522 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 /*
1525 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001526 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 */
1528 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1529 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1530 {
1531 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001532 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 */
1534 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 if (has_mbyte)
1536 {
1537 while (ptr[col] != NUL)
1538 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001539 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1541 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 this_class = mb_get_class(ptr + col);
1543 if (this_class != 0 && (i == 1 || this_class != 1))
1544 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001545 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547 }
1548 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001550 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 )
1553 ++col;
1554
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001555 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
1558 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001559 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (has_mbyte)
1562 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001563 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1565 this_class = mb_get_class((char_u *)"a");
1566 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001568 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1571 prev_class = mb_get_class(ptr + prevcol);
1572 if (this_class != prev_class
1573 && (i == 0
1574 || prev_class == 0
1575 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 && (!(find_type & FIND_EVAL)
1577 || prevcol == 0
1578 || !find_is_eval_item(ptr + prevcol, &prevcol,
1579 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 )
1581 break;
1582 col = prevcol;
1583 }
1584
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001585 // If we don't want just any old text, or we've found an
1586 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (this_class > 2)
1588 this_class = 2;
1589 if (!(find_type & FIND_STRING) || this_class == 2)
1590 break;
1591 }
1592 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594 while (col > 0
1595 && ((i == 0
1596 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001597 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 && (!(find_type & FIND_IDENT)
1599 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 || ((find_type & FIND_EVAL)
1601 && col > 1
1602 && find_is_eval_item(ptr + col - 1, &col,
1603 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 ))
1605 --col;
1606
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001607 // If we don't want just any old text, or we've found an
1608 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1610 break;
1611 }
1612 }
1613
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001614 if (ptr[col] == NUL || (i == 0
1615 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001617 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001618 if ((find_type & FIND_NOERROR) == 0)
1619 {
1620 if (find_type & FIND_STRING)
1621 emsg(_("E348: No string under cursor"));
1622 else
1623 emsg(_(e_noident));
1624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 return 0;
1626 }
1627 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001628 *text = ptr;
1629 if (textcol != NULL)
1630 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001633 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 bn = 0;
1636 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (has_mbyte)
1639 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001640 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 this_class = mb_get_class(ptr);
1642 while (ptr[col] != NUL
1643 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1644 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 || ((find_type & FIND_EVAL)
1646 && col <= (int)startcol
1647 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001649 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
1651 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001653 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 || ((find_type & FIND_EVAL)
1655 && col <= (int)startcol
1656 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 return col;
1661}
1662
1663/*
1664 * Prepare for redo of a normal command.
1665 */
1666 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001667prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
1669 prep_redo(cap->oap->regname, cap->count0,
1670 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1671}
1672
1673/*
1674 * Prepare for redo of any command.
1675 * Note that only the last argument can be a multi-byte char.
1676 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001677 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001678prep_redo(
1679 int regname,
1680 long num,
1681 int cmd1,
1682 int cmd2,
1683 int cmd3,
1684 int cmd4,
1685 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001688 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 {
1690 AppendCharToRedobuff('"');
1691 AppendCharToRedobuff(regname);
1692 }
1693 if (num)
1694 AppendNumberToRedobuff(num);
1695
1696 if (cmd1 != NUL)
1697 AppendCharToRedobuff(cmd1);
1698 if (cmd2 != NUL)
1699 AppendCharToRedobuff(cmd2);
1700 if (cmd3 != NUL)
1701 AppendCharToRedobuff(cmd3);
1702 if (cmd4 != NUL)
1703 AppendCharToRedobuff(cmd4);
1704 if (cmd5 != NUL)
1705 AppendCharToRedobuff(cmd5);
1706}
1707
1708/*
1709 * check for operator active and clear it
1710 *
1711 * return TRUE if operator was active
1712 */
1713 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001714checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 if (oap->op_type == OP_NOP)
1717 return FALSE;
1718 clearopbeep(oap);
1719 return TRUE;
1720}
1721
1722/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001723 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001725 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 */
1727 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001728checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001730 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 return FALSE;
1732 clearopbeep(oap);
1733 return TRUE;
1734}
1735
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001736 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001737clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
1739 oap->op_type = OP_NOP;
1740 oap->regname = 0;
1741 oap->motion_force = NUL;
1742 oap->use_reg_one = FALSE;
1743}
1744
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001745 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001746clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 clearop(oap);
1749 beep_flush();
1750}
1751
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752/*
1753 * Remove the shift modifier from a special key.
1754 */
1755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001756unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 switch (cap->cmdchar)
1759 {
1760 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1761 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1762 case K_S_UP: cap->cmdchar = K_UP; break;
1763 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1764 case K_S_HOME: cap->cmdchar = K_HOME; break;
1765 case K_S_END: cap->cmdchar = K_END; break;
1766 }
1767 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001770/*
1771 * If the mode is currently displayed clear the command line or update the
1772 * command displayed.
1773 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001774 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001775may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001776{
1777 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001778 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001779#ifdef FEAT_CMDL_INFO
1780 else
1781 clear_showcmd();
1782#endif
1783}
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1786/*
1787 * Routines for displaying a partly typed command
1788 */
1789
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001790#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001792static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793static int showcmd_is_clear = TRUE;
1794static int showcmd_visual = FALSE;
1795
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001796static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001799clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 if (!p_sc)
1802 return;
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (VIsual_active && !char_avail())
1805 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001806 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 long lines;
1808 colnr_T leftcol, rightcol;
1809 linenr_T top, bot;
1810
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001811 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001812 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 {
1814 top = VIsual.lnum;
1815 bot = curwin->w_cursor.lnum;
1816 }
1817 else
1818 {
1819 top = curwin->w_cursor.lnum;
1820 bot = VIsual.lnum;
1821 }
1822# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001823 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001824 (void)hasFolding(top, &top, NULL);
1825 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826# endif
1827 lines = bot - top + 1;
1828
1829 if (VIsual_mode == Ctrl_V)
1830 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001831# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001832 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001833 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001834
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001835 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001836 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001837 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001840# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001841 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001842 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1845 (long)(rightcol - leftcol + 1));
1846 }
1847 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1848 sprintf((char *)showcmd_buf, "%ld", lines);
1849 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001850 {
1851 char_u *s, *e;
1852 int l;
1853 int bytes = 0;
1854 int chars = 0;
1855
1856 if (cursor_bot)
1857 {
1858 s = ml_get_pos(&VIsual);
1859 e = ml_get_cursor();
1860 }
1861 else
1862 {
1863 s = ml_get_cursor();
1864 e = ml_get_pos(&VIsual);
1865 }
1866 while ((*p_sel != 'e') ? s <= e : s < e)
1867 {
1868 l = (*mb_ptr2len)(s);
1869 if (l == 0)
1870 {
1871 ++bytes;
1872 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001873 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001874 }
1875 bytes += l;
1876 ++chars;
1877 s += l;
1878 }
1879 if (bytes == chars)
1880 sprintf((char *)showcmd_buf, "%d", chars);
1881 else
1882 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1883 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001884 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 showcmd_visual = TRUE;
1886 }
1887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
1889 showcmd_buf[0] = NUL;
1890 showcmd_visual = FALSE;
1891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001892 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (showcmd_is_clear)
1894 return;
1895 }
1896
1897 display_showcmd();
1898}
1899
1900/*
1901 * Add 'c' to string of shown command chars.
1902 * Return TRUE if output has been written (and setcursor() has been called).
1903 */
1904 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001905add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 char_u *p;
1908 int old_len;
1909 int extra_len;
1910 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 int i;
1912 static int ignore[] =
1913 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001914#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1916 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001917#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001918 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001919 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1921 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001922 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001924 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 0
1926 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaar09df3122006-01-23 22:23:09 +00001928 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 return FALSE;
1930
1931 if (showcmd_visual)
1932 {
1933 showcmd_buf[0] = NUL;
1934 showcmd_visual = FALSE;
1935 }
1936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001937 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 if (IS_SPECIAL(c))
1939 for (i = 0; ignore[i] != 0; ++i)
1940 if (ignore[i] == c)
1941 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
1943 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001944 if (*p == ' ')
1945 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 old_len = (int)STRLEN(showcmd_buf);
1947 extra_len = (int)STRLEN(p);
1948 overflow = old_len + extra_len - SHOWCMD_COLS;
1949 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001950 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1951 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 STRCAT(showcmd_buf, p);
1953
1954 if (char_avail())
1955 return FALSE;
1956
1957 display_showcmd();
1958
1959 return TRUE;
1960}
1961
1962 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001963add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964{
1965 if (!add_to_showcmd(c))
1966 setcursor();
1967}
1968
1969/*
1970 * Delete 'len' characters from the end of the shown command.
1971 */
1972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001973del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 int old_len;
1976
1977 if (!p_sc)
1978 return;
1979
1980 old_len = (int)STRLEN(showcmd_buf);
1981 if (len > old_len)
1982 len = old_len;
1983 showcmd_buf[old_len - len] = NUL;
1984
1985 if (!char_avail())
1986 display_showcmd();
1987}
1988
1989/*
1990 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1991 * something and there is a partial mapping.
1992 */
1993 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001994push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995{
1996 if (p_sc)
1997 STRCPY(old_showcmd_buf, showcmd_buf);
1998}
1999
2000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002001pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002{
2003 if (!p_sc)
2004 return;
2005
2006 STRCPY(showcmd_buf, old_showcmd_buf);
2007
2008 display_showcmd();
2009}
2010
2011 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002012display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013{
2014 int len;
2015
2016 cursor_off();
2017
2018 len = (int)STRLEN(showcmd_buf);
2019 if (len == 0)
2020 showcmd_is_clear = TRUE;
2021 else
2022 {
2023 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2024 showcmd_is_clear = FALSE;
2025 }
2026
2027 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002028 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2029 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 */
2031 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002033 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034}
2035#endif
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037/*
2038 * When "check" is FALSE, prepare for commands that scroll the window.
2039 * When "check" is TRUE, take care of scroll-binding after the window has
2040 * scrolled. Called from normal_cmd() and edit().
2041 */
2042 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002043do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
2045 static win_T *old_curwin = NULL;
2046 static linenr_T old_topline = 0;
2047#ifdef FEAT_DIFF
2048 static int old_topfill = 0;
2049#endif
2050 static buf_T *old_buf = NULL;
2051 static colnr_T old_leftcol = 0;
2052
2053 if (check && curwin->w_p_scb)
2054 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002055 // If a ":syncbind" command was just used, don't scroll, only reset
2056 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (did_syncbind)
2058 did_syncbind = FALSE;
2059 else if (curwin == old_curwin)
2060 {
2061 /*
2062 * Synchronize other windows, as necessary according to
2063 * 'scrollbind'. Don't do this after an ":edit" command, except
2064 * when 'diff' is set.
2065 */
2066 if ((curwin->w_buffer == old_buf
2067#ifdef FEAT_DIFF
2068 || curwin->w_p_diff
2069#endif
2070 )
2071 && (curwin->w_topline != old_topline
2072#ifdef FEAT_DIFF
2073 || curwin->w_topfill != old_topfill
2074#endif
2075 || curwin->w_leftcol != old_leftcol))
2076 {
2077 check_scrollbind(curwin->w_topline - old_topline,
2078 (long)(curwin->w_leftcol - old_leftcol));
2079 }
2080 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002081 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
2083 /*
2084 * When switching between windows, make sure that the relative
2085 * vertical offset is valid for the new window. The relative
2086 * offset is invalid whenever another 'scrollbind' window has
2087 * scrolled to a point that would force the current window to
2088 * scroll past the beginning or end of its buffer. When the
2089 * resync is performed, some of the other 'scrollbind' windows may
2090 * need to jump so that the current window's relative position is
2091 * visible on-screen.
2092 */
2093 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2094 }
2095 curwin->w_scbind_pos = curwin->w_topline;
2096 }
2097
2098 old_curwin = curwin;
2099 old_topline = curwin->w_topline;
2100#ifdef FEAT_DIFF
2101 old_topfill = curwin->w_topfill;
2102#endif
2103 old_buf = curwin->w_buffer;
2104 old_leftcol = curwin->w_leftcol;
2105}
2106
2107/*
2108 * Synchronize any windows that have "scrollbind" set, based on the
2109 * number of rows by which the current window has changed
2110 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2111 */
2112 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002113check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114{
2115 int want_ver;
2116 int want_hor;
2117 win_T *old_curwin = curwin;
2118 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 int old_VIsual_select = VIsual_select;
2120 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 colnr_T tgt_leftcol = curwin->w_leftcol;
2122 long topline;
2123 long y;
2124
2125 /*
2126 * check 'scrollopt' string for vertical and horizontal scroll options
2127 */
2128 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2129#ifdef FEAT_DIFF
2130 want_ver |= old_curwin->w_p_diff;
2131#endif
2132 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2133
2134 /*
2135 * loop through the scrollbound windows and scroll accordingly
2136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002138 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002141 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (curwin != old_curwin && curwin->w_p_scb)
2143 {
2144 /*
2145 * do the vertical scroll
2146 */
2147 if (want_ver)
2148 {
2149#ifdef FEAT_DIFF
2150 if (old_curwin->w_p_diff && curwin->w_p_diff)
2151 {
2152 diff_set_topline(old_curwin, curwin);
2153 }
2154 else
2155#endif
2156 {
2157 curwin->w_scbind_pos += topline_diff;
2158 topline = curwin->w_scbind_pos;
2159 if (topline > curbuf->b_ml.ml_line_count)
2160 topline = curbuf->b_ml.ml_line_count;
2161 if (topline < 1)
2162 topline = 1;
2163
2164 y = topline - curwin->w_topline;
2165 if (y > 0)
2166 scrollup(y, FALSE);
2167 else
2168 scrolldown(-y, FALSE);
2169 }
2170
2171 redraw_later(VALID);
2172 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175
2176 /*
2177 * do the horizontal scroll
2178 */
2179 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2180 {
2181 curwin->w_leftcol = tgt_leftcol;
2182 leftcol_changed();
2183 }
2184 }
2185 }
2186
2187 /*
2188 * reset current-window
2189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 VIsual_select = old_VIsual_select;
2191 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 curwin = old_curwin;
2193 curbuf = old_curbuf;
2194}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
2196/*
2197 * Command character that's ignored.
2198 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002199 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002202nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002204 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205}
2206
2207/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002208 * Command character that doesn't do anything, but unlike nv_ignore() does
2209 * start edit(). Used for "startinsert" executed while starting up.
2210 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002212nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002213{
2214}
2215
2216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 * Command character doesn't exist.
2218 */
2219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002220nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222 clearopbeep(cap->oap);
2223}
2224
2225/*
2226 * <Help> and <F1> commands.
2227 */
2228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002229nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230{
2231 if (!checkclearopq(cap->oap))
2232 ex_help(NULL);
2233}
2234
2235/*
2236 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2237 */
2238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002239nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002241#ifdef FEAT_JOB_CHANNEL
2242 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2243 clearopbeep(cap->oap);
2244 else
2245#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002246 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002247 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002248 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002249 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2250 op_addsub(cap->oap, cap->count1, cap->arg);
2251 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002252 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002253 else if (VIsual_active)
2254 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002255 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002256 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257}
2258
2259/*
2260 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2261 */
2262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002263nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002266 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267 if (mod_mask & MOD_MASK_CTRL)
2268 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002269 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270 if (cap->arg == BACKWARD)
2271 goto_tabpage(-(int)cap->count1);
2272 else
2273 goto_tabpage((int)cap->count0);
2274 }
2275 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002276 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278}
2279
2280/*
2281 * Implementation of "gd" and "gD" command.
2282 */
2283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002284nv_gd(
2285 oparg_T *oap,
2286 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002287 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288{
2289 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 char_u *ptr;
2291
Bram Moolenaard9d30582005-05-18 22:10:28 +00002292 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002293 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2294 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002296#ifdef FEAT_FOLDING
2297 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2298 foldOpenCursor();
2299#endif
2300}
2301
2302/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002303 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2304 * otherwise.
2305 */
2306 static int
2307is_ident(char_u *line, int offset)
2308{
2309 int i;
2310 int incomment = FALSE;
2311 int instring = 0;
2312 int prev = 0;
2313
2314 for (i = 0; i < offset && line[i] != NUL; i++)
2315 {
2316 if (instring != 0)
2317 {
2318 if (prev != '\\' && line[i] == instring)
2319 instring = 0;
2320 }
2321 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2322 {
2323 instring = line[i];
2324 }
2325 else
2326 {
2327 if (incomment)
2328 {
2329 if (prev == '*' && line[i] == '/')
2330 incomment = FALSE;
2331 }
2332 else if (prev == '/' && line[i] == '*')
2333 {
2334 incomment = TRUE;
2335 }
2336 else if (prev == '/' && line[i] == '/')
2337 {
2338 return FALSE;
2339 }
2340 }
2341
2342 prev = line[i];
2343 }
2344
2345 return incomment == FALSE && instring == 0;
2346}
2347
2348/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002349 * Search for variable declaration of "ptr[len]".
2350 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2351 * current file ("gD").
2352 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002353 * Return FAIL when not found.
2354 */
2355 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002356find_decl(
2357 char_u *ptr,
2358 int len,
2359 int locally,
2360 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002361 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002362{
2363 char_u *pat;
2364 pos_T old_pos;
2365 pos_T par_pos;
2366 pos_T found_pos;
2367 int t;
2368 int save_p_ws;
2369 int save_p_scs;
2370 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002371 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002372 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002373 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002374
2375 if ((pat = alloc(len + 7)) == NULL)
2376 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002377
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002378 // Put "\V" before the pattern to avoid that the special meaning of "."
2379 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002380 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2381 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 old_pos = curwin->w_cursor;
2383 save_p_ws = p_ws;
2384 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002385 p_ws = FALSE; // don't wrap around end of file now
2386 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 /*
2389 * With "gD" go to line 1.
2390 * With "gd" Search back for the start of the current function, then go
2391 * back until a blank line. If this fails go to line 1.
2392 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002393 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002397 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
2399 else
2400 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002401 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2403 --curwin->w_cursor.lnum;
2404 }
2405 curwin->w_cursor.col = 0;
2406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002407 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002408 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002409 for (;;)
2410 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002411 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002412 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002413 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002414 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002415
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002416 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002417 {
2418 pos_T *pos;
2419
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002420 // Check that the block the match is in doesn't end before the
2421 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002422 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2423 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2424 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002425 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002426 // There can't be a useful match before the end of this block.
2427 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002428 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002429 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002430 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002431 }
2432
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002433 if (t == FAIL)
2434 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002435 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002436 if (found_pos.lnum != 0)
2437 {
2438 curwin->w_cursor = found_pos;
2439 t = OK;
2440 }
2441 break;
2442 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002443 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002446 ++curwin->w_cursor.lnum;
2447 curwin->w_cursor.col = 0;
2448 continue;
2449 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002450 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002452 // If the current position is not a valid identifier and a previous
2453 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002454 if (!valid && found_pos.lnum != 0)
2455 {
2456 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002457 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002458 }
2459
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002460 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002461 if (valid && !locally)
2462 break;
2463 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002464 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002465 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002466 if (found_pos.lnum != 0)
2467 curwin->w_cursor = found_pos;
2468 break;
2469 }
2470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002471 // For finding a local variable and the match is before the "{" or
2472 // inside a comment, continue searching. For K&R style function
2473 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002474 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002475 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002476 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002477 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002478 // Remove SEARCH_START from flags to avoid getting stuck at one
2479 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002480 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002482
2483 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002485 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 curwin->w_cursor = old_pos;
2487 }
2488 else
2489 {
2490 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002491 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 reset_search_dir();
2493 }
2494
2495 vim_free(pat);
2496 p_ws = save_p_ws;
2497 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002498
2499 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500}
2501
2502/*
2503 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2504 * lines rather than lines in the file.
2505 * 'dist' must be positive.
2506 *
2507 * Return OK if able to move cursor, FAIL otherwise.
2508 */
2509 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002510nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 int linelen = linetabsize(ml_get_curline());
2513 int retval = OK;
2514 int atend = FALSE;
2515 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002516 int col_off1; // margin offset for first screen line
2517 int col_off2; // margin offset for wrapped screen line
2518 int width1; // text width for first screen line
2519 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
2521 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002522 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
2524 col_off1 = curwin_col_off();
2525 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002526 width1 = curwin->w_width - col_off1;
2527 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002528 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002529 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 /*
2534 * Instead of sticking at the last character of the buffer line we
2535 * try to stick in the last column of the screen.
2536 */
2537 if (curwin->w_curswant == MAXCOL)
2538 {
2539 atend = TRUE;
2540 validate_virtcol();
2541 if (width1 <= 0)
2542 curwin->w_curswant = 0;
2543 else
2544 {
2545 curwin->w_curswant = width1 - 1;
2546 if (curwin->w_virtcol > curwin->w_curswant)
2547 curwin->w_curswant += ((curwin->w_virtcol
2548 - curwin->w_curswant - 1) / width2 + 1) * width2;
2549 }
2550 }
2551 else
2552 {
2553 if (linelen > width1)
2554 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2555 else
2556 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002557 if (curwin->w_curswant >= (colnr_T)n)
2558 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560
2561 while (dist--)
2562 {
2563 if (dir == BACKWARD)
2564 {
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002565 if ((long)curwin->w_curswant >= width1)
2566 // Move back within the line. This can give a negative value
2567 // for w_curswant if width1 < width2 (with cpoptions+=n),
2568 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 curwin->w_curswant -= width2;
2570 else
2571 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002572 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002574 // Move to the start of a closed fold. Don't do that when
2575 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (!(fdo_flags & FDO_ALL))
2577 (void)hasFolding(curwin->w_cursor.lnum,
2578 &curwin->w_cursor.lnum, NULL);
2579#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002580 if (curwin->w_cursor.lnum == 1)
2581 {
2582 retval = FAIL;
2583 break;
2584 }
2585 --curwin->w_cursor.lnum;
2586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 linelen = linetabsize(ml_get_curline());
2588 if (linelen > width1)
2589 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2590 + 1) * width2;
2591 }
2592 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002593 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
2595 if (linelen > width1)
2596 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2597 else
2598 n = width1;
2599 if (curwin->w_curswant + width2 < (colnr_T)n)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002600 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 curwin->w_curswant += width2;
2602 else
2603 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002604 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002606 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2608 &curwin->w_cursor.lnum);
2609#endif
2610 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2611 {
2612 retval = FAIL;
2613 break;
2614 }
2615 curwin->w_cursor.lnum++;
2616 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002617 // Check if the cursor has moved below the number display
2618 // when width1 < width2 (with cpoptions+=n). Subtract width2
2619 // to get a negative value for w_curswant, which will get
2620 // clipped to column 0.
2621 if (curwin->w_curswant >= width1)
2622 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002623 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625 }
2626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002629 if (virtual_active() && atend)
2630 coladvance(MAXCOL);
2631 else
2632 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2635 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002636 colnr_T virtcol;
2637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 /*
2639 * Check for landing on a character that got split at the end of the
2640 * last line. We want to advance a screenline, not end up in the same
2641 * screenline or move two screenlines.
2642 */
2643 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002644 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002645#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002646 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2647 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002648#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002649
2650 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 && (curwin->w_curswant < (colnr_T)width1
2652 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2653 : ((curwin->w_curswant - width1) % width2
2654 > (colnr_T)width2 / 2)))
2655 --curwin->w_cursor.col;
2656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002659 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 return retval;
2662}
2663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664/*
2665 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2666 * cap->arg must be TRUE for CTRL-E.
2667 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002669nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
2671 if (!checkclearop(cap->oap))
2672 scroll_redraw(cap->arg, cap->count1);
2673}
2674
2675/*
2676 * Scroll "count" lines up or down, and redraw.
2677 */
2678 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002679scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 linenr_T prev_topline = curwin->w_topline;
2682#ifdef FEAT_DIFF
2683 int prev_topfill = curwin->w_topfill;
2684#endif
2685 linenr_T prev_lnum = curwin->w_cursor.lnum;
2686
2687 if (up)
2688 scrollup(count, TRUE);
2689 else
2690 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002691 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002693 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2694 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 cursor_correct();
2696 check_cursor_moved(curwin);
2697 curwin->w_valid |= VALID_TOPLINE;
2698
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002699 // If moved back to where we were, at least move the cursor, otherwise
2700 // we get stuck at one position. Don't move the cursor up if the
2701 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 while (curwin->w_topline == prev_topline
2703#ifdef FEAT_DIFF
2704 && curwin->w_topfill == prev_topfill
2705#endif
2706 )
2707 {
2708 if (up)
2709 {
2710 if (curwin->w_cursor.lnum > prev_lnum
2711 || cursor_down(1L, FALSE) == FAIL)
2712 break;
2713 }
2714 else
2715 {
2716 if (curwin->w_cursor.lnum < prev_lnum
2717 || prev_topline == 1L
2718 || cursor_up(1L, FALSE) == FAIL)
2719 break;
2720 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002721 // Mark w_topline as valid, otherwise the screen jumps back at the
2722 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 check_cursor_moved(curwin);
2724 curwin->w_valid |= VALID_TOPLINE;
2725 }
2726 }
2727 if (curwin->w_cursor.lnum != prev_lnum)
2728 coladvance(curwin->w_curswant);
2729 redraw_later(VALID);
2730}
2731
2732/*
2733 * Commands that start with "z".
2734 */
2735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002736nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737{
2738 long n;
2739 colnr_T col;
2740 int nchar = cap->nchar;
2741#ifdef FEAT_FOLDING
2742 long old_fdl = curwin->w_p_fdl;
2743 int old_fen = curwin->w_p_fen;
2744#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002745#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002746 int undo = FALSE;
2747#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002748 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 if (VIM_ISDIGIT(nchar))
2751 {
2752 /*
2753 * "z123{nchar}": edit the count before obtaining {nchar}
2754 */
2755 if (checkclearop(cap->oap))
2756 return;
2757 n = nchar - '0';
2758 for (;;)
2759 {
2760#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002761 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762#endif
2763 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002765 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 --no_mapping;
2768 --allow_keys;
2769#ifdef FEAT_CMDL_INFO
2770 (void)add_to_showcmd(nchar);
2771#endif
2772 if (nchar == K_DEL || nchar == K_KDEL)
2773 n /= 10;
2774 else if (VIM_ISDIGIT(nchar))
2775 n = n * 10 + (nchar - '0');
2776 else if (nchar == CAR)
2777 {
2778#ifdef FEAT_GUI
2779 need_mouse_correct = TRUE;
2780#endif
2781 win_setheight((int)n);
2782 break;
2783 }
2784 else if (nchar == 'l'
2785 || nchar == 'h'
2786 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002787 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
2789 cap->count1 = n ? n * cap->count1 : cap->count1;
2790 goto dozet;
2791 }
2792 else
2793 {
2794 clearopbeep(cap->oap);
2795 break;
2796 }
2797 }
2798 cap->oap->op_type = OP_NOP;
2799 return;
2800 }
2801
2802dozet:
2803 if (
2804#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002805 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2806 // and "zC" only in Visual mode. "zj" and "zk" are motion
2807 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 cap->nchar != 'f' && cap->nchar != 'F'
2809 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2810 && cap->nchar != 'j' && cap->nchar != 'k'
2811 &&
2812#endif
2813 checkclearop(cap->oap))
2814 return;
2815
2816 /*
2817 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2818 * If line number given, set cursor.
2819 */
2820 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2821 && cap->count0
2822 && cap->count0 != curwin->w_cursor.lnum)
2823 {
2824 setpcmark();
2825 if (cap->count0 > curbuf->b_ml.ml_line_count)
2826 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2827 else
2828 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002829 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 }
2831
2832 switch (nchar)
2833 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002834 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 case '+':
2836 if (cap->count0 == 0)
2837 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // No count given: put cursor at the line below screen
2839 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2841 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2842 else
2843 curwin->w_cursor.lnum = curwin->w_botline;
2844 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 case NL:
2847 case CAR:
2848 case K_KENTER:
2849 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002850 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
2852 case 't': scroll_cursor_top(0, TRUE);
2853 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002854 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 break;
2856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
2861 case 'z': scroll_cursor_halfway(TRUE);
2862 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002863 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 break;
2865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // "z^", "z-" and "zb": put cursor at bottom of screen
2867 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2868 // when <count> is at bottom of window, and puts that one at
2869 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 if (cap->count0 != 0)
2871 {
2872 scroll_cursor_bot(0, TRUE);
2873 curwin->w_cursor.lnum = curwin->w_topline;
2874 }
2875 else if (curwin->w_topline == 1)
2876 curwin->w_cursor.lnum = 1;
2877 else
2878 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002879 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 case '-':
2881 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883
2884 case 'b': scroll_cursor_bot(0, TRUE);
2885 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002886 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 break;
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002891 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002894 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 case 'h':
2896 case K_LEFT:
2897 if (!curwin->w_p_wrap)
2898 {
2899 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2900 curwin->w_leftcol = 0;
2901 else
2902 curwin->w_leftcol -= (colnr_T)cap->count1;
2903 leftcol_changed();
2904 }
2905 break;
2906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002908 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002911 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 case 'l':
2913 case K_RIGHT:
2914 if (!curwin->w_p_wrap)
2915 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 curwin->w_leftcol += (colnr_T)cap->count1;
2918 leftcol_changed();
2919 }
2920 break;
2921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002922 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 case 's': if (!curwin->w_p_wrap)
2924 {
2925#ifdef FEAT_FOLDING
2926 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 else
2929#endif
2930 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002931 if ((long)col > siso)
2932 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 else
2934 col = 0;
2935 if (curwin->w_leftcol != col)
2936 {
2937 curwin->w_leftcol = col;
2938 redraw_later(NOT_VALID);
2939 }
2940 }
2941 break;
2942
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002943 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 case 'e': if (!curwin->w_p_wrap)
2945 {
2946#ifdef FEAT_FOLDING
2947 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 else
2950#endif
2951 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002952 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002953 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 col = 0;
2955 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002956 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (curwin->w_leftcol != col)
2958 {
2959 curwin->w_leftcol = col;
2960 redraw_later(NOT_VALID);
2961 }
2962 }
2963 break;
2964
2965#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 // "zF": create fold command
2967 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 case 'F':
2969 case 'f': if (foldManualAllowed(TRUE))
2970 {
2971 cap->nchar = 'f';
2972 nv_operator(cap);
2973 curwin->w_p_fen = TRUE;
2974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002975 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2977 {
2978 nv_operator(cap);
2979 finish_op = TRUE;
2980 }
2981 }
2982 else
2983 clearopbeep(cap->oap);
2984 break;
2985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 // "zd": delete fold at cursor
2987 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 case 'd':
2989 case 'D': if (foldManualAllowed(FALSE))
2990 {
2991 if (VIsual_active)
2992 nv_operator(cap);
2993 else
2994 deleteFold(curwin->w_cursor.lnum,
2995 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2996 }
2997 break;
2998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 case 'E': if (foldmethodIsManual(curwin))
3001 {
3002 clearFolding(curwin);
3003 changed_window_setting();
3004 }
3005 else if (foldmethodIsMarker(curwin))
3006 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3007 TRUE, FALSE);
3008 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003009 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 break;
3011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 case 'n': curwin->w_p_fen = FALSE;
3014 break;
3015
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003016 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 case 'N': curwin->w_p_fen = TRUE;
3018 break;
3019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3022 break;
3023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3026 openFold(curwin->w_cursor.lnum, cap->count1);
3027 else
3028 {
3029 closeFold(curwin->w_cursor.lnum, cap->count1);
3030 curwin->w_p_fen = TRUE;
3031 }
3032 break;
3033
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3036 openFoldRecurse(curwin->w_cursor.lnum);
3037 else
3038 {
3039 closeFoldRecurse(curwin->w_cursor.lnum);
3040 curwin->w_p_fen = TRUE;
3041 }
3042 break;
3043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 case 'o': if (VIsual_active)
3046 nv_operator(cap);
3047 else
3048 openFold(curwin->w_cursor.lnum, cap->count1);
3049 break;
3050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003051 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 case 'O': if (VIsual_active)
3053 nv_operator(cap);
3054 else
3055 openFoldRecurse(curwin->w_cursor.lnum);
3056 break;
3057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003058 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 case 'c': if (VIsual_active)
3060 nv_operator(cap);
3061 else
3062 closeFold(curwin->w_cursor.lnum, cap->count1);
3063 curwin->w_p_fen = TRUE;
3064 break;
3065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003066 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 case 'C': if (VIsual_active)
3068 nv_operator(cap);
3069 else
3070 closeFoldRecurse(curwin->w_cursor.lnum);
3071 curwin->w_p_fen = TRUE;
3072 break;
3073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003074 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 case 'v': foldOpenCursor();
3076 break;
3077
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003078 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003080 curwin->w_foldinvalid = TRUE; // recompute folds
3081 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 foldOpenCursor();
3083 break;
3084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003087 curwin->w_foldinvalid = TRUE; // recompute folds
3088 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 break;
3090
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003093 {
3094 curwin->w_p_fdl -= cap->count1;
3095 if (curwin->w_p_fdl < 0)
3096 curwin->w_p_fdl = 0;
3097 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 curwin->w_p_fen = TRUE;
3100 break;
3101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003104 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 curwin->w_p_fen = TRUE;
3106 break;
3107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003109 case 'r': curwin->w_p_fdl += cap->count1;
3110 {
3111 int d = getDeepestNesting();
3112
3113 if (curwin->w_p_fdl >= d)
3114 curwin->w_p_fdl = d;
3115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 break;
3117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003118 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003120 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 break;
3122
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 case 'j': // "zj" move to next fold downwards
3124 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3126 cap->count1) == FAIL)
3127 clearopbeep(cap->oap);
3128 break;
3129
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003130#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003132#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003134 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003135 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003136 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003137 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003138 --no_mapping;
3139 --allow_keys;
3140#ifdef FEAT_CMDL_INFO
3141 (void)add_to_showcmd(nchar);
3142#endif
3143 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3144 {
3145 clearopbeep(cap->oap);
3146 break;
3147 }
3148 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003150
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003151 case 'g': // "zg": add good word to word list
3152 case 'w': // "zw": add wrong word to word list
3153 case 'G': // "zG": add good word to temp word list
3154 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003155 {
3156 char_u *ptr = NULL;
3157 int len;
3158
3159 if (checkclearop(cap->oap))
3160 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003161 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3162 == FAIL)
3163 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003164 if (ptr == NULL)
3165 {
3166 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003167
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003168 // Find bad word under the cursor. When 'spell' is
3169 // off this fails and find_ident_under_cursor() is
3170 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003171 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003172 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003173 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003174 if (len != 0 && curwin->w_cursor.col <= pos.col)
3175 ptr = ml_get_pos(&curwin->w_cursor);
3176 curwin->w_cursor = pos;
3177 }
3178
Bram Moolenaarb765d632005-06-07 21:00:02 +00003179 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3180 FIND_IDENT)) == 0)
3181 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003182 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3183 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003184 (nchar == 'G' || nchar == 'W')
3185 ? 0 : (int)cap->count1,
3186 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003187 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003188 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003190 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003191 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003192 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003193 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003194#endif
3195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 default: clearopbeep(cap->oap);
3197 }
3198
3199#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003200 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (old_fen != curwin->w_p_fen)
3202 {
3203# ifdef FEAT_DIFF
3204 win_T *wp;
3205
3206 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3207 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003208 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 FOR_ALL_WINDOWS(wp)
3210 {
3211 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3212 {
3213 wp->w_p_fen = curwin->w_p_fen;
3214 changed_window_setting_win(wp);
3215 }
3216 }
3217 }
3218# endif
3219 changed_window_setting();
3220 }
3221
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (old_fdl != curwin->w_p_fdl)
3224 newFoldLevel();
3225#endif
3226}
3227
3228#ifdef FEAT_GUI
3229/*
3230 * Vertical scrollbar movement.
3231 */
3232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003233nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 if (cap->oap->op_type != OP_NOP)
3236 clearopbeep(cap->oap);
3237
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003238 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 gui_do_scroll();
3240}
3241
3242/*
3243 * Horizontal scrollbar movement.
3244 */
3245 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003246nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 if (cap->oap->op_type != OP_NOP)
3249 clearopbeep(cap->oap);
3250
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003251 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003252 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253}
3254#endif
3255
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003256#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003257/*
3258 * Click in GUI tab.
3259 */
3260 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003261nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003262{
3263 if (cap->oap->op_type != OP_NOP)
3264 clearopbeep(cap->oap);
3265
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003266 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003267 goto_tabpage(current_tab);
3268}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003269
3270/*
3271 * Selected item in tab line menu.
3272 */
3273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003274nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003275{
3276 if (cap->oap->op_type != OP_NOP)
3277 clearopbeep(cap->oap);
3278
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003279 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003280 handle_tabmenu();
3281}
3282
3283/*
3284 * Handle selecting an item of the GUI tab line menu.
3285 * Used in Normal and Insert mode.
3286 */
3287 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003288handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003289{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003290 switch (current_tabmenu)
3291 {
3292 case TABLINE_MENU_CLOSE:
3293 if (current_tab == 0)
3294 do_cmdline_cmd((char_u *)"tabclose");
3295 else
3296 {
3297 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3298 current_tab);
3299 do_cmdline_cmd(IObuff);
3300 }
3301 break;
3302
3303 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003304 if (current_tab == 0)
3305 do_cmdline_cmd((char_u *)"$tabnew");
3306 else
3307 {
3308 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3309 current_tab - 1);
3310 do_cmdline_cmd(IObuff);
3311 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003312 break;
3313
3314 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003315 if (current_tab == 0)
3316 do_cmdline_cmd((char_u *)"browse $tabnew");
3317 else
3318 {
3319 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3320 current_tab - 1);
3321 do_cmdline_cmd(IObuff);
3322 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003323 break;
3324 }
3325}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003326#endif
3327
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328/*
3329 * "Q" command.
3330 */
3331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003332nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 /*
3335 * Ignore 'Q' in Visual mode, just give a beep.
3336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003338 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003339 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 do_exmode(FALSE);
3341}
3342
3343/*
3344 * Handle a ":" command.
3345 */
3346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003347nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003349 int old_p_im;
3350 int cmd_result;
3351 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
Bram Moolenaar957cf672020-11-12 14:21:06 +01003353 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 nv_operator(cap);
3355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
3357 if (cap->oap->op_type != OP_NOP)
3358 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003359 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 cap->oap->motion_type = MCHAR;
3361 cap->oap->inclusive = FALSE;
3362 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003363 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003365 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 stuffcharReadbuff('.');
3367 if (cap->count0 > 1)
3368 {
3369 stuffReadbuff((char_u *)",.+");
3370 stuffnumReadbuff((long)cap->count0 - 1L);
3371 }
3372 }
3373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003374 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (KeyTyped)
3376 compute_cmdrow();
3377
3378 old_p_im = p_im;
3379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003380 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003381 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003384 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (p_im != old_p_im)
3386 {
3387 if (p_im)
3388 restart_edit = 'i';
3389 else
3390 restart_edit = 0;
3391 }
3392
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003393 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003394 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003395 clearop(cap->oap);
3396 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3398 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003399 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3400 || did_emsg
3401 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003402 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 clearopbeep(cap->oap);
3404 }
3405}
3406
3407/*
3408 * Handle CTRL-G command.
3409 */
3410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003411nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003413 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
3415 VIsual_select = !VIsual_select;
3416 showmode();
3417 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003418 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 fileinfo((int)cap->count0, FALSE, TRUE);
3421}
3422
3423/*
3424 * Handle CTRL-H <Backspace> command.
3425 */
3426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003427nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (VIsual_active && VIsual_select)
3430 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003431 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 v_visop(cap);
3433 }
3434 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 nv_left(cap);
3436}
3437
3438/*
3439 * CTRL-L: clear screen and redraw.
3440 */
3441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003442nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 if (!checkclearop(cap->oap))
3445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003447 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003448 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003449# ifdef FEAT_RELTIME
3450 {
3451 win_T *wp;
3452
3453 FOR_ALL_WINDOWS(wp)
3454 wp->w_s->b_syn_slow = FALSE;
3455 }
3456# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#endif
3458 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003459#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3460# ifdef VIMDLL
3461 if (!gui.in_use)
3462# endif
3463 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466}
3467
3468/*
3469 * CTRL-O: In Select mode: switch to Visual mode for one command.
3470 * Otherwise: Go to older pcmark.
3471 */
3472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003473nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (VIsual_active && VIsual_select)
3476 {
3477 VIsual_select = FALSE;
3478 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003479 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
3483 cap->count1 = -cap->count1;
3484 nv_pcmark(cap);
3485 }
3486}
3487
3488/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003489 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3490 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
3492 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003493nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 if (!checkclearopq(cap->oap))
3496 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3497 GETF_SETMARK|GETF_ALT, FALSE);
3498}
3499
3500/*
3501 * "Z" commands.
3502 */
3503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003504nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
3506 if (!checkclearopq(cap->oap))
3507 {
3508 switch (cap->nchar)
3509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003510 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 case 'Z': do_cmdline_cmd((char_u *)"x");
3512 break;
3513
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003514 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 case 'Q': do_cmdline_cmd((char_u *)"q!");
3516 break;
3517
3518 default: clearopbeep(cap->oap);
3519 }
3520 }
3521}
3522
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523/*
3524 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3525 */
3526 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003527do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
3529 oparg_T oa;
3530 cmdarg_T ca;
3531
3532 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003533 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 ca.oap = &oa;
3535 ca.cmdchar = c1;
3536 ca.nchar = c2;
3537 nv_ident(&ca);
3538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
3540/*
3541 * Handle the commands that use the word under the cursor.
3542 * [g] CTRL-] :ta to current identifier
3543 * [g] 'K' run program for current identifier
3544 * [g] '*' / to current identifier or string
3545 * [g] '#' ? to current identifier or string
3546 * g ']' :tselect for current identifier
3547 */
3548 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003549nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
3551 char_u *ptr = NULL;
3552 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003553 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003554 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003556 char_u *kp; // value of 'keywordprg'
3557 int kp_help; // 'keywordprg' is ":he"
3558 int kp_ex; // 'keywordprg' starts with ":"
3559 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003561 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003562 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 char_u *aux_ptr;
3564 int isman;
3565 int isman_s;
3566
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003567 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 cmdchar = cap->nchar;
3570 g_cmd = TRUE;
3571 }
3572 else
3573 {
3574 cmdchar = cap->cmdchar;
3575 g_cmd = FALSE;
3576 }
3577
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003578 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 cmdchar = '#';
3580
3581 /*
3582 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3583 */
3584 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3587 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (checkclearopq(cap->oap))
3589 return;
3590 }
3591
3592 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3593 (cmdchar == '*' || cmdchar == '#')
3594 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3595 {
3596 clearop(cap->oap);
3597 return;
3598 }
3599
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003600 // Allocate buffer to put the command in. Inserting backslashes can
3601 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3602 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3604 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3605 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003606 if (kp_help && *skipwhite(ptr) == NUL)
3607 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003608 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003609 return;
3610 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003611 kp_ex = (*kp == ':');
3612 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3613 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (buf == NULL)
3615 return;
3616 buf[0] = NUL;
3617
3618 switch (cmdchar)
3619 {
3620 case '*':
3621 case '#':
3622 /*
3623 * Put cursor at start of word, makes search skip the word
3624 * under the cursor.
3625 * Call setpcmark() first, so "*``" puts the cursor back where
3626 * it was.
3627 */
3628 setpcmark();
3629 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3630
3631 if (!g_cmd && vim_iswordp(ptr))
3632 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003633 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 break;
3635
3636 case 'K':
3637 if (kp_help)
3638 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003639 else if (kp_ex)
3640 {
3641 if (cap->count0 != 0)
3642 vim_snprintf((char *)buf, buflen, "%s %ld",
3643 kp, cap->count0);
3644 else
3645 STRCPY(buf, kp);
3646 STRCAT(buf, " ");
3647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 else
3649 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003650 // An external command will probably use an argument starting
3651 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003652 while (*ptr == '-' && n > 0)
3653 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003654 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003655 --n;
3656 }
3657 if (n == 0)
3658 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003659 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003660 vim_free(buf);
3661 return;
3662 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003663
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003664 // When a count is given, turn it into a range. Is this
3665 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 isman = (STRCMP(kp, "man") == 0);
3667 isman_s = (STRCMP(kp, "man -s") == 0);
3668 if (cap->count0 != 0 && !(isman || isman_s))
3669 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3670
3671 STRCAT(buf, "! ");
3672 if (cap->count0 == 0 && isman_s)
3673 STRCAT(buf, "man");
3674 else
3675 STRCAT(buf, kp);
3676 STRCAT(buf, " ");
3677 if (cap->count0 != 0 && (isman || isman_s))
3678 {
3679 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3680 STRCAT(buf, " ");
3681 }
3682 }
3683 break;
3684
3685 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003686 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687#ifdef FEAT_CSCOPE
3688 if (p_cst)
3689 STRCPY(buf, "cstag ");
3690 else
3691#endif
3692 STRCPY(buf, "ts ");
3693 break;
3694
3695 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003696 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (curbuf->b_help)
3698 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003700 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003701 if (g_cmd)
3702 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003703 else if (cap->count0 == 0)
3704 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003705 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003706 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709
3710 /*
3711 * Now grab the chars in the identifier
3712 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003713 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003715 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003716 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003717 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003718 p = vim_strsave_fnameescape(ptr, FALSE);
3719 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003720 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003721 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003722 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003723 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003725 vim_free(buf);
3726 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003728 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003729 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003730 {
3731 vim_free(buf);
3732 vim_free(p);
3733 return;
3734 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003735 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003736 STRCAT(buf, p);
3737 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003739 else
3740 {
3741 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003742 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003743 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003744 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003745 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003746 {
3747 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003748 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003749 aux_ptr = (char_u *)"";
3750 else
3751 aux_ptr = (char_u *)"\\|\"\n[";
3752 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003753 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003754 aux_ptr = (char_u *)"\\|\"\n*?[";
3755
3756 p = buf + STRLEN(buf);
3757 while (n-- > 0)
3758 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003759 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003760 if (vim_strchr(aux_ptr, *ptr) != NULL)
3761 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003762 // When current byte is a part of multibyte character, copy all
3763 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003764 if (has_mbyte)
3765 {
3766 int i;
3767 int len = (*mb_ptr2len)(ptr) - 1;
3768
3769 for (i = 0; i < len && n >= 1; ++i, --n)
3770 *p++ = *ptr++;
3771 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003772 *p++ = *ptr++;
3773 }
3774 *p = NUL;
3775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 /*
3778 * Execute the command.
3779 */
3780 if (cmdchar == '*' || cmdchar == '#')
3781 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003782 if (!g_cmd && (has_mbyte
3783 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3784 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003786
3787 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003788 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003790
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003791 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003794 {
3795 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003797 g_tag_at_cursor = FALSE;
3798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
3800 vim_free(buf);
3801}
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803/*
3804 * Get visually selected text, within one line only.
3805 * Returns FAIL if more than one line selected.
3806 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003807 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003808get_visual_text(
3809 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003810 char_u **pp, // return: start of selected text
3811 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812{
3813 if (VIsual_mode != 'V')
3814 unadjust_for_sel();
3815 if (VIsual.lnum != curwin->w_cursor.lnum)
3816 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003817 if (cap != NULL)
3818 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 return FAIL;
3820 }
3821 if (VIsual_mode == 'V')
3822 {
3823 *pp = ml_get_curline();
3824 *lenp = (int)STRLEN(*pp);
3825 }
3826 else
3827 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003828 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
3830 *pp = ml_get_pos(&curwin->w_cursor);
3831 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3832 }
3833 else
3834 {
3835 *pp = ml_get_pos(&VIsual);
3836 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003839 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003840 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 reset_VIsual_and_resel();
3843 return OK;
3844}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845
3846/*
3847 * CTRL-T: backwards in tag stack
3848 */
3849 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003850nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
3852 if (!checkclearopq(cap->oap))
3853 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3854}
3855
3856/*
3857 * Handle scrolling command 'H', 'L' and 'M'.
3858 */
3859 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003860nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 int used = 0;
3863 long n;
3864#ifdef FEAT_FOLDING
3865 linenr_T lnum;
3866#endif
3867 int half;
3868
3869 cap->oap->motion_type = MLINE;
3870 setpcmark();
3871
3872 if (cap->cmdchar == 'L')
3873 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003874 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 curwin->w_cursor.lnum = curwin->w_botline - 1;
3876 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3877 curwin->w_cursor.lnum = 1;
3878 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003879 {
3880#ifdef FEAT_FOLDING
3881 if (hasAnyFolding(curwin))
3882 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003883 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003884 for (n = cap->count1 - 1; n > 0
3885 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3886 {
3887 (void)hasFolding(curwin->w_cursor.lnum,
3888 &curwin->w_cursor.lnum, NULL);
3889 --curwin->w_cursor.lnum;
3890 }
3891 }
3892 else
3893#endif
3894 curwin->w_cursor.lnum -= cap->count1 - 1;
3895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897 else
3898 {
3899 if (cap->cmdchar == 'M')
3900 {
3901#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003902 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 used -= diff_check_fill(curwin, curwin->w_topline)
3904 - curwin->w_topfill;
3905#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003906 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3908 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3909 {
3910#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003911 // Count half he number of filler lines to be "below this
3912 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3914 + n) / 2 >= half)
3915 {
3916 --n;
3917 break;
3918 }
3919#endif
3920 used += plines(curwin->w_topline + n);
3921 if (used >= half)
3922 break;
3923#ifdef FEAT_FOLDING
3924 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3925 n = lnum - curwin->w_topline;
3926#endif
3927 }
3928 if (n > 0 && used > curwin->w_height)
3929 --n;
3930 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003931 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003934#ifdef FEAT_FOLDING
3935 if (hasAnyFolding(curwin))
3936 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003937 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003938 lnum = curwin->w_topline;
3939 while (n-- > 0 && lnum < curwin->w_botline - 1)
3940 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003941 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003942 ++lnum;
3943 }
3944 n = lnum - curwin->w_topline;
3945 }
3946#endif
3947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 curwin->w_cursor.lnum = curwin->w_topline + n;
3949 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3950 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3951 }
3952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003953 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003954 if (cap->oap->op_type == OP_NOP)
3955 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 beginline(BL_SOL | BL_FIX);
3957}
3958
3959/*
3960 * Cursor right commands.
3961 */
3962 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003963nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964{
3965 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003966 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003968 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3969 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003970 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003971 if (mod_mask & MOD_MASK_CTRL)
3972 cap->arg = TRUE;
3973 nv_wordcmd(cap);
3974 return;
3975 }
3976
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 cap->oap->motion_type = MCHAR;
3978 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003979 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003982 * In virtual edit mode, there's no such thing as "past_line", as lines
3983 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 */
3985 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003986 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
3988 for (n = cap->count1; n > 0; --n)
3989 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003990 if ((!past_line && oneright() == FAIL)
3991 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003992 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 {
3994 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003995 * <Space> wraps to next line if 'whichwrap' has 's'.
3996 * 'l' wraps to next line if 'whichwrap' has 'l'.
3997 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 */
3999 if ( ((cap->cmdchar == ' '
4000 && vim_strchr(p_ww, 's') != NULL)
4001 || (cap->cmdchar == 'l'
4002 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004003 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 && vim_strchr(p_ww, '>') != NULL))
4005 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4006 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004007 // When deleting we also count the NL as a character.
4008 // Set cap->oap->inclusive when last char in the line is
4009 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004010 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004012 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 cap->oap->inclusive = TRUE;
4014 else
4015 {
4016 ++curwin->w_cursor.lnum;
4017 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 curwin->w_set_curswant = TRUE;
4020 cap->oap->inclusive = FALSE;
4021 }
4022 continue;
4023 }
4024 if (cap->oap->op_type == OP_NOP)
4025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004026 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 if (n == cap->count1)
4028 beep_flush();
4029 }
4030 else
4031 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004032 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 cap->oap->inclusive = TRUE;
4034 }
4035 break;
4036 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004037 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
4039 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 if (virtual_active())
4041 oneright();
4042 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004045 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 ++curwin->w_cursor.col;
4048 }
4049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
4051#ifdef FEAT_FOLDING
4052 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4053 && cap->oap->op_type == OP_NOP)
4054 foldOpenCursor();
4055#endif
4056}
4057
4058/*
4059 * Cursor left commands.
4060 *
4061 * Returns TRUE when operator end should not be adjusted.
4062 */
4063 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004064nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
4066 long n;
4067
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004068 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4069 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004070 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004071 if (mod_mask & MOD_MASK_CTRL)
4072 cap->arg = 1;
4073 nv_bck_word(cap);
4074 return;
4075 }
4076
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 cap->oap->motion_type = MCHAR;
4078 cap->oap->inclusive = FALSE;
4079 for (n = cap->count1; n > 0; --n)
4080 {
4081 if (oneleft() == FAIL)
4082 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004083 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4084 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4085 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 if ( (((cap->cmdchar == K_BS
4087 || cap->cmdchar == Ctrl_H)
4088 && vim_strchr(p_ww, 'b') != NULL)
4089 || (cap->cmdchar == 'h'
4090 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004091 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 && vim_strchr(p_ww, '<') != NULL))
4093 && curwin->w_cursor.lnum > 1)
4094 {
4095 --(curwin->w_cursor.lnum);
4096 coladvance((colnr_T)MAXCOL);
4097 curwin->w_set_curswant = TRUE;
4098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004099 // When the NL before the first char has to be deleted we
4100 // put the cursor on the NUL after the previous line.
4101 // This is a very special case, be careful!
4102 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if ( (cap->oap->op_type == OP_DELETE
4104 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004105 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004107 char_u *cp = ml_get_cursor();
4108
4109 if (*cp != NUL)
4110 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004111 if (has_mbyte)
4112 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4113 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004114 ++curwin->w_cursor.col;
4115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 cap->retval |= CA_NO_ADJ_OP_END;
4117 }
4118 continue;
4119 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004120 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4122 beep_flush();
4123 break;
4124 }
4125 }
4126#ifdef FEAT_FOLDING
4127 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4128 && cap->oap->op_type == OP_NOP)
4129 foldOpenCursor();
4130#endif
4131}
4132
4133/*
4134 * Cursor up commands.
4135 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4136 */
4137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004138nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004140 if (mod_mask & MOD_MASK_SHIFT)
4141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004142 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004143 cap->arg = BACKWARD;
4144 nv_page(cap);
4145 }
4146 else
4147 {
4148 cap->oap->motion_type = MLINE;
4149 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4150 clearopbeep(cap->oap);
4151 else if (cap->arg)
4152 beginline(BL_WHITE | BL_FIX);
4153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154}
4155
4156/*
4157 * Cursor down commands.
4158 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4159 */
4160 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004161nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004163 if (mod_mask & MOD_MASK_SHIFT)
4164 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004165 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004166 cap->arg = FORWARD;
4167 nv_page(cap);
4168 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004169#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004170 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004171 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4172 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004174 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
4176#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004177 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004178 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 cmdwin_result = CAR;
4180 else
4181#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004182#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004183 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004184 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4185 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4186 {
4187 invoke_prompt_callback();
4188 if (restart_edit == 0)
4189 restart_edit = 'a';
4190 }
4191 else
4192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
4194 cap->oap->motion_type = MLINE;
4195 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4196 clearopbeep(cap->oap);
4197 else if (cap->arg)
4198 beginline(BL_WHITE | BL_FIX);
4199 }
4200 }
4201}
4202
4203#ifdef FEAT_SEARCHPATH
4204/*
4205 * Grab the file name under the cursor and edit it.
4206 */
4207 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004208nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
4210 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004211 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004213 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 {
4215 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004216 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return;
4218 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004219 if (curbuf_locked())
4220 {
4221 clearop(cap->oap);
4222 return;
4223 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004224#ifdef FEAT_PROP_POPUP
4225 if (ERROR_IF_TERM_POPUP_WINDOW)
4226 return;
4227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004229 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 if (ptr != NULL)
4232 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004233 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004234 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004235 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004237 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004238 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004239 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004240 {
4241 curwin->w_cursor.lnum = lnum;
4242 check_cursor_lnum();
4243 beginline(BL_SOL | BL_FIX);
4244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 vim_free(ptr);
4246 }
4247 else
4248 clearop(cap->oap);
4249}
4250#endif
4251
4252/*
4253 * <End> command: to end of current line or last line.
4254 */
4255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004256nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004258 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004260 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004262 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 nv_dollar(cap);
4265}
4266
4267/*
4268 * Handle the "$" command.
4269 */
4270 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004271nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
4273 cap->oap->motion_type = MCHAR;
4274 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004275 // In virtual mode when off the edge of a line and an operator
4276 // is pending (whew!) keep the cursor where it is.
4277 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 if (!virtual_active() || gchar_cursor() != NUL
4279 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004280 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 if (cursor_down((long)(cap->count1 - 1),
4282 cap->oap->op_type == OP_NOP) == FAIL)
4283 clearopbeep(cap->oap);
4284#ifdef FEAT_FOLDING
4285 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4286 foldOpenCursor();
4287#endif
4288}
4289
4290/*
4291 * Implementation of '?' and '/' commands.
4292 * If cap->arg is TRUE don't set PC mark.
4293 */
4294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004295nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296{
4297 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004298 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
4300 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4301 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004302 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 cap->cmdchar = 'g';
4304 cap->nchar = '?';
4305 nv_operator(cap);
4306 return;
4307 }
4308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004309 // When using 'incsearch' the cursor may be moved to set a different search
4310 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004311 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 if (cap->searchbuf == NULL)
4314 {
4315 clearop(oap);
4316 return;
4317 }
4318
Bram Moolenaar46539112015-02-17 15:43:57 +01004319 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004320 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004321 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322}
4323
4324/*
4325 * Handle "N" and "n" commands.
4326 * cap->arg is SEARCH_REV for "N", 0 for "n".
4327 */
4328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004329nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004331 pos_T old = curwin->w_cursor;
4332 int wrapped = FALSE;
4333 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004334
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004335 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004336 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004337 // Avoid getting stuck on the current cursor position, which can
4338 // happen when an offset is given and the cursor is on the last char
4339 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004340 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004341 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004342 cap->count1 -= 1;
4343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344}
4345
4346/*
4347 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4348 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004349 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004351 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004352normal_search(
4353 cmdarg_T *cap,
4354 int dir,
4355 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004356 int opt, // extra flags for do_search()
4357 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358{
4359 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004360 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362 cap->oap->motion_type = MCHAR;
4363 cap->oap->inclusive = FALSE;
4364 cap->oap->use_reg_one = TRUE;
4365 curwin->w_set_curswant = TRUE;
4366
Bram Moolenaara80faa82020-04-12 19:37:17 +02004367 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004368 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004369 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4370 if (wrapped != NULL)
4371 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 if (i == 0)
4373 clearop(cap->oap);
4374 else
4375 {
4376 if (i == 2)
4377 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#ifdef FEAT_FOLDING
4380 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4381 foldOpenCursor();
4382#endif
4383 }
4384
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004385 // "/$" will put the cursor after the end of the line, may need to
4386 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004388 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389}
4390
4391/*
4392 * Character search commands.
4393 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4394 * ',' and FALSE for ';'.
4395 * cap->nchar is NUL for ',' and ';' (repeat the search)
4396 */
4397 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004398nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
4400 int t_cmd;
4401
4402 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4403 t_cmd = TRUE;
4404 else
4405 t_cmd = FALSE;
4406
4407 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4409 clearopbeep(cap->oap);
4410 else
4411 {
4412 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004413 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4415 && (t_cmd || cap->oap->op_type != OP_NOP))
4416 {
4417 colnr_T scol, ecol;
4418
4419 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4420 curwin->w_cursor.coladd = ecol - scol;
4421 }
4422 else
4423 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425#ifdef FEAT_FOLDING
4426 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4427 foldOpenCursor();
4428#endif
4429 }
4430}
4431
4432/*
4433 * "[" and "]" commands.
4434 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4435 */
4436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004437nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004439 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004441 pos_T *pos = NULL; // init for GCC
4442 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 int flag;
4444 long n;
4445 int findc;
4446 int c;
4447
4448 cap->oap->motion_type = MCHAR;
4449 cap->oap->inclusive = FALSE;
4450 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004451 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
4453#ifdef FEAT_SEARCHPATH
4454 /*
4455 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4456 */
4457 if (cap->nchar == 'f')
4458 nv_gotofile(cap);
4459 else
4460#endif
4461
4462#ifdef FEAT_FIND_ID
4463 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004464 * Find the occurrence(s) of the identifier or define under cursor
4465 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 *
4467 * search list jump
4468 * fwd bwd fwd bwd fwd bwd
4469 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4470 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4471 */
4472 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004473# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004475# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004477# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 cap->nchar) != NULL)
4479 {
4480 char_u *ptr;
4481 int len;
4482
4483 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4484 clearop(cap->oap);
4485 else
4486 {
4487 find_pattern_in_path(ptr, 0, len, TRUE,
4488 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4489 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4490 cap->count1,
4491 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4492 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4493 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4494 (linenr_T)MAXLNUM);
4495 curwin->w_set_curswant = TRUE;
4496 }
4497 }
4498 else
4499#endif
4500
4501 /*
4502 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4503 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4504 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4505 * "[m" or "]m" search for prev/next start of (Java) method.
4506 * "[M" or "]M" search for prev/next end of (Java) method.
4507 */
4508 if ( (cap->cmdchar == '['
4509 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4510 || (cap->cmdchar == ']'
4511 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4512 {
4513 if (cap->nchar == '*')
4514 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 prev_pos.lnum = 0;
4516 if (cap->nchar == 'm' || cap->nchar == 'M')
4517 {
4518 if (cap->cmdchar == '[')
4519 findc = '{';
4520 else
4521 findc = '}';
4522 n = 9999;
4523 }
4524 else
4525 {
4526 findc = cap->nchar;
4527 n = cap->count1;
4528 }
4529 for ( ; n > 0; --n)
4530 {
4531 if ((pos = findmatchlimit(cap->oap, findc,
4532 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4533 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004534 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 {
4536 if (cap->nchar != 'm' && cap->nchar != 'M')
4537 clearopbeep(cap->oap);
4538 }
4539 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004540 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 break;
4542 }
4543 prev_pos = new_pos;
4544 curwin->w_cursor = *pos;
4545 new_pos = *pos;
4546 }
4547 curwin->w_cursor = old_pos;
4548
4549 /*
4550 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4551 * brought us to the match for "[m" and "]M" when inside a method.
4552 * Try finding the '{' or '}' we want to be at.
4553 * Also repeat for the given count.
4554 */
4555 if (cap->nchar == 'm' || cap->nchar == 'M')
4556 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004557 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 int norm = ((findc == '{') == (cap->nchar == 'm'));
4559
4560 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004561 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 if (prev_pos.lnum != 0)
4563 {
4564 pos = &prev_pos;
4565 curwin->w_cursor = prev_pos;
4566 if (norm)
4567 --n;
4568 }
4569 else
4570 pos = NULL;
4571 while (n > 0)
4572 {
4573 for (;;)
4574 {
4575 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4576 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004577 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (pos == NULL)
4579 clearopbeep(cap->oap);
4580 n = 0;
4581 break;
4582 }
4583 c = gchar_cursor();
4584 if (c == '{' || c == '}')
4585 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004586 // Must have found end/start of class: use it.
4587 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if ((c == findc && norm) || (n == 1 && !norm))
4589 {
4590 new_pos = curwin->w_cursor;
4591 pos = &new_pos;
4592 n = 0;
4593 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004594 // if no match found at all, we started outside of the
4595 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 else if (new_pos.lnum == 0)
4597 {
4598 new_pos = curwin->w_cursor;
4599 pos = &new_pos;
4600 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004601 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 else if ((pos = findmatchlimit(cap->oap, findc,
4603 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4604 0)) == NULL)
4605 n = 0;
4606 else
4607 curwin->w_cursor = *pos;
4608 break;
4609 }
4610 }
4611 --n;
4612 }
4613 curwin->w_cursor = old_pos;
4614 if (pos == NULL && new_pos.lnum != 0)
4615 clearopbeep(cap->oap);
4616 }
4617 if (pos != NULL)
4618 {
4619 setpcmark();
4620 curwin->w_cursor = *pos;
4621 curwin->w_set_curswant = TRUE;
4622#ifdef FEAT_FOLDING
4623 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4624 && cap->oap->op_type == OP_NOP)
4625 foldOpenCursor();
4626#endif
4627 }
4628 }
4629
4630 /*
4631 * "[[", "[]", "]]" and "][": move to start or end of function
4632 */
4633 else if (cap->nchar == '[' || cap->nchar == ']')
4634 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004635 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 flag = '{';
4637 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004638 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639
4640 curwin->w_set_curswant = TRUE;
4641 /*
4642 * Imitate strange Vi behaviour: When using "]]" with an operator
4643 * we also stop at '}'.
4644 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004645 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 (cap->oap->op_type != OP_NOP
4647 && cap->arg == FORWARD && flag == '{')))
4648 clearopbeep(cap->oap);
4649 else
4650 {
4651 if (cap->oap->op_type == OP_NOP)
4652 beginline(BL_WHITE | BL_FIX);
4653#ifdef FEAT_FOLDING
4654 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4655 foldOpenCursor();
4656#endif
4657 }
4658 }
4659
4660 /*
4661 * "[p", "[P", "]P" and "]p": put with indent adjustment
4662 */
4663 else if (cap->nchar == 'p' || cap->nchar == 'P')
4664 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004665 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667
4668 /*
4669 * "['", "[`", "]'" and "]`": jump to next mark
4670 */
4671 else if (cap->nchar == '\'' || cap->nchar == '`')
4672 {
4673 pos = &curwin->w_cursor;
4674 for (n = cap->count1; n > 0; --n)
4675 {
4676 prev_pos = *pos;
4677 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4678 cap->nchar == '\'');
4679 if (pos == NULL)
4680 break;
4681 }
4682 if (pos == NULL)
4683 pos = &prev_pos;
4684 nv_cursormark(cap, cap->nchar == '\'', pos);
4685 }
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 /*
4688 * [ or ] followed by a middle mouse click: put selected text with
4689 * indent adjustment. Any other button just does as usual.
4690 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004691 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693 (void)do_mouse(cap->oap, cap->nchar,
4694 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4695 cap->count1, PUT_FIXINDENT);
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
4698#ifdef FEAT_FOLDING
4699 /*
4700 * "[z" and "]z": move to start or end of open fold.
4701 */
4702 else if (cap->nchar == 'z')
4703 {
4704 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4705 cap->count1) == FAIL)
4706 clearopbeep(cap->oap);
4707 }
4708#endif
4709
4710#ifdef FEAT_DIFF
4711 /*
4712 * "[c" and "]c": move to next or previous diff-change.
4713 */
4714 else if (cap->nchar == 'c')
4715 {
4716 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4717 cap->count1) == FAIL)
4718 clearopbeep(cap->oap);
4719 }
4720#endif
4721
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004722#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004723 /*
4724 * "[s", "[S", "]s" and "]S": move to next spell error.
4725 */
4726 else if (cap->nchar == 's' || cap->nchar == 'S')
4727 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004728 setpcmark();
4729 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004730 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4731 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004732 {
4733 clearopbeep(cap->oap);
4734 break;
4735 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004736 else
4737 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004738# ifdef FEAT_FOLDING
4739 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4740 foldOpenCursor();
4741# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004742 }
4743#endif
4744
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004745 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 else
4747 clearopbeep(cap->oap);
4748}
4749
4750/*
4751 * Handle Normal mode "%" command.
4752 */
4753 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004754nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
4756 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004757#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 linenr_T lnum = curwin->w_cursor.lnum;
4759#endif
4760
4761 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004762 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
4764 if (cap->count0 > 100)
4765 clearopbeep(cap->oap);
4766 else
4767 {
4768 cap->oap->motion_type = MLINE;
4769 setpcmark();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004770 // Round up, so CTRL-G will give same value. Watch out for a
4771 // large line count, the line number must not go negative!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 if (curbuf->b_ml.ml_line_count > 1000000)
4773 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4774 / 100L * cap->count0;
4775 else
4776 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4777 cap->count0 + 99L) / 100L;
4778 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4779 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4780 beginline(BL_SOL | BL_FIX);
4781 }
4782 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
4785 cap->oap->motion_type = MCHAR;
4786 cap->oap->use_reg_one = TRUE;
4787 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4788 clearopbeep(cap->oap);
4789 else
4790 {
4791 setpcmark();
4792 curwin->w_cursor = *pos;
4793 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
4797 }
4798#ifdef FEAT_FOLDING
4799 if (cap->oap->op_type == OP_NOP
4800 && lnum != curwin->w_cursor.lnum
4801 && (fdo_flags & FDO_PERCENT)
4802 && KeyTyped)
4803 foldOpenCursor();
4804#endif
4805}
4806
4807/*
4808 * Handle "(" and ")" commands.
4809 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4810 */
4811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004812nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 cap->oap->motion_type = MCHAR;
4815 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004816 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004817 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 curwin->w_set_curswant = TRUE;
4819
4820 if (findsent(cap->arg, cap->count1) == FAIL)
4821 clearopbeep(cap->oap);
4822 else
4823 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004824 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004825 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#ifdef FEAT_FOLDING
4828 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4829 foldOpenCursor();
4830#endif
4831 }
4832}
4833
4834/*
4835 * "m" command: Mark a position.
4836 */
4837 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004838nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
4840 if (!checkclearop(cap->oap))
4841 {
4842 if (setmark(cap->nchar) == FAIL)
4843 clearopbeep(cap->oap);
4844 }
4845}
4846
4847/*
4848 * "{" and "}" commands.
4849 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4850 */
4851 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004852nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 cap->oap->motion_type = MCHAR;
4855 cap->oap->inclusive = FALSE;
4856 cap->oap->use_reg_one = TRUE;
4857 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004858 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 clearopbeep(cap->oap);
4860 else
4861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#ifdef FEAT_FOLDING
4864 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4865 foldOpenCursor();
4866#endif
4867 }
4868}
4869
4870/*
4871 * "u" command: Undo or make lower case.
4872 */
4873 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004874nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004876 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004878 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 cap->cmdchar = 'g';
4880 cap->nchar = 'u';
4881 nv_operator(cap);
4882 }
4883 else
4884 nv_kundo(cap);
4885}
4886
4887/*
4888 * <Undo> command.
4889 */
4890 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004891nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892{
4893 if (!checkclearopq(cap->oap))
4894 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004895#ifdef FEAT_JOB_CHANNEL
4896 if (bt_prompt(curbuf))
4897 {
4898 clearopbeep(cap->oap);
4899 return;
4900 }
4901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 u_undo((int)cap->count1);
4903 curwin->w_set_curswant = TRUE;
4904 }
4905}
4906
4907/*
4908 * Handle the "r" command.
4909 */
4910 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004911nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
4913 char_u *ptr;
4914 int had_ctrl_v;
4915 long n;
4916
4917 if (checkclearop(cap->oap))
4918 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004919#ifdef FEAT_JOB_CHANNEL
4920 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4921 {
4922 clearopbeep(cap->oap);
4923 return;
4924 }
4925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004927 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (cap->nchar == Ctrl_V)
4929 {
4930 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004931 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004932 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (cap->nchar > DEL)
4934 had_ctrl_v = NUL;
4935 }
4936 else
4937 had_ctrl_v = NUL;
4938
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004939 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004940 if (IS_SPECIAL(cap->nchar))
4941 {
4942 clearopbeep(cap->oap);
4943 return;
4944 }
4945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004946 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 if (VIsual_active)
4948 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004949 if (got_int)
4950 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004951 if (had_ctrl_v)
4952 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004953 // Use a special (negative) number to make a difference between a
4954 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004955 if (cap->nchar == CAR)
4956 cap->nchar = REPLACE_CR_NCHAR;
4957 else if (cap->nchar == NL)
4958 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 nv_operator(cap);
4961 return;
4962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004964 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (virtual_active())
4966 {
4967 if (u_save_cursor() == FAIL)
4968 return;
4969 if (gchar_cursor() == NUL)
4970 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004971 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 coladvance_force((colnr_T)(getviscol() + cap->count1));
4973 curwin->w_cursor.col -= cap->count1;
4974 }
4975 else if (gchar_cursor() == TAB)
4976 coladvance_force(getviscol());
4977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004979 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004981 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004982 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 clearopbeep(cap->oap);
4985 return;
4986 }
4987
4988 /*
4989 * Replacing with a TAB is done by edit() when it is complicated because
4990 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4991 * Other characters are done below to avoid problems with things like
4992 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
4993 */
4994 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4995 {
4996 stuffnumReadbuff(cap->count1);
4997 stuffcharReadbuff('R');
4998 stuffcharReadbuff('\t');
4999 stuffcharReadbuff(ESC);
5000 return;
5001 }
5002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005003 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 if (u_save_cursor() == FAIL)
5005 return;
5006
5007 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5008 {
5009 /*
5010 * Replace character(s) by a single newline.
5011 * Strange vi behaviour: Only one newline is inserted.
5012 * Delete the characters here.
5013 * Insert the newline with an insert command, takes care of
5014 * autoindent. The insert command depends on being on the last
5015 * character of a line or not.
5016 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005017 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 stuffcharReadbuff('\r');
5019 stuffcharReadbuff(ESC);
5020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005021 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 invoke_edit(cap, TRUE, 'r', FALSE);
5023 }
5024 else
5025 {
5026 prep_redo(cap->oap->regname, cap->count1,
5027 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5028
5029 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (has_mbyte)
5031 {
5032 int old_State = State;
5033
5034 if (cap->ncharC1 != 0)
5035 AppendCharToRedobuff(cap->ncharC1);
5036 if (cap->ncharC2 != 0)
5037 AppendCharToRedobuff(cap->ncharC2);
5038
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005039 // This is slow, but it handles replacing a single-byte with a
5040 // multi-byte and the other way around. Also handles adding
5041 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 for (n = cap->count1; n > 0; --n)
5043 {
5044 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005045 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5046 {
5047 int c = ins_copychar(curwin->w_cursor.lnum
5048 + (cap->nchar == Ctrl_Y ? -1 : 1));
5049 if (c != NUL)
5050 ins_char(c);
5051 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005052 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005053 ++curwin->w_cursor.col;
5054 }
5055 else
5056 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 State = old_State;
5058 if (cap->ncharC1 != 0)
5059 ins_char(cap->ncharC1);
5060 if (cap->ncharC2 != 0)
5061 ins_char(cap->ncharC2);
5062 }
5063 }
5064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 /*
5067 * Replace the characters within one line.
5068 */
5069 for (n = cap->count1; n > 0; --n)
5070 {
5071 /*
5072 * Get ptr again, because u_save and/or showmatch() will have
5073 * released the line. At the same time we let know that the
5074 * line will be changed.
5075 */
5076 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005077 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5078 {
5079 int c = ins_copychar(curwin->w_cursor.lnum
5080 + (cap->nchar == Ctrl_Y ? -1 : 1));
5081 if (c != NUL)
5082 ptr[curwin->w_cursor.col] = c;
5083 }
5084 else
5085 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (p_sm && msg_silent == 0)
5087 showmatch(cap->nchar);
5088 ++curwin->w_cursor.col;
5089 }
5090#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005091 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005093 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
Bram Moolenaar009b2592004-10-24 19:18:58 +00005095 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005096 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005098 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100#endif
5101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005102 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 changed_bytes(curwin->w_cursor.lnum,
5104 (colnr_T)(curwin->w_cursor.col - cap->count1));
5105 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005106 --curwin->w_cursor.col; // cursor on the last replaced char
5107 // if the character on the left of the current cursor is a multi-byte
5108 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (has_mbyte)
5110 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 curbuf->b_op_end = curwin->w_cursor;
5112 curwin->w_set_curswant = TRUE;
5113 set_last_insert(cap->nchar);
5114 }
5115}
5116
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117/*
5118 * 'o': Exchange start and end of Visual area.
5119 * 'O': same, but in block mode exchange left and right corners.
5120 */
5121 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005122v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123{
5124 pos_T old_cursor;
5125 colnr_T left, right;
5126
5127 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5128 {
5129 old_cursor = curwin->w_cursor;
5130 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5131 curwin->w_cursor.lnum = VIsual.lnum;
5132 coladvance(left);
5133 VIsual = curwin->w_cursor;
5134
5135 curwin->w_cursor.lnum = old_cursor.lnum;
5136 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005137 // 'selection "exclusive" and cursor at right-bottom corner: move it
5138 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5140 ++curwin->w_curswant;
5141 coladvance(curwin->w_curswant);
5142 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005144 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 curwin->w_cursor.lnum = VIsual.lnum;
5147 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5148 ++right;
5149 coladvance(right);
5150 VIsual = curwin->w_cursor;
5151
5152 curwin->w_cursor.lnum = old_cursor.lnum;
5153 coladvance(left);
5154 curwin->w_curswant = left;
5155 }
5156 }
5157 else
5158 {
5159 old_cursor = curwin->w_cursor;
5160 curwin->w_cursor = VIsual;
5161 VIsual = old_cursor;
5162 curwin->w_set_curswant = TRUE;
5163 }
5164}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166/*
5167 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5168 */
5169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005172 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
5174 cap->cmdchar = 'c';
5175 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005176 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 VIsual_mode = 'V';
5178 nv_operator(cap);
5179 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005180 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
5182 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005183 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 else
5185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (virtual_active())
5187 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5189 }
5190 }
5191}
5192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193/*
5194 * "gr".
5195 */
5196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005197nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (VIsual_active)
5200 {
5201 cap->cmdchar = 'r';
5202 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005203 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005205 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
5207 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005208 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 else
5210 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005211 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005212 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 stuffcharReadbuff(cap->extra_char);
5214 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (virtual_active())
5216 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 invoke_edit(cap, TRUE, 'v', FALSE);
5218 }
5219 }
5220}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222/*
5223 * Swap case for "~" command, when it does not work like an operator.
5224 */
5225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005226n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
5228 long n;
5229 pos_T startpos;
5230 int did_change = 0;
5231#ifdef FEAT_NETBEANS_INTG
5232 pos_T pos;
5233 char_u *ptr;
5234 int count;
5235#endif
5236
5237 if (checkclearopq(cap->oap))
5238 return;
5239
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005240 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 {
5242 clearopbeep(cap->oap);
5243 return;
5244 }
5245
5246 prep_redo_cmd(cap);
5247
5248 if (u_save_cursor() == FAIL)
5249 return;
5250
5251 startpos = curwin->w_cursor;
5252#ifdef FEAT_NETBEANS_INTG
5253 pos = startpos;
5254#endif
5255 for (n = cap->count1; n > 0; --n)
5256 {
5257 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5258 inc_cursor();
5259 if (gchar_cursor() == NUL)
5260 {
5261 if (vim_strchr(p_ww, '~') != NULL
5262 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5263 {
5264#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005265 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
5267 if (did_change)
5268 {
5269 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005270 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005271 netbeans_removed(curbuf, pos.lnum, pos.col,
5272 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005274 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 }
5276 pos.col = 0;
5277 pos.lnum++;
5278 }
5279#endif
5280 ++curwin->w_cursor.lnum;
5281 curwin->w_cursor.col = 0;
5282 if (n > 1)
5283 {
5284 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5285 break;
5286 u_clearline();
5287 }
5288 }
5289 else
5290 break;
5291 }
5292 }
5293#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005294 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
5296 ptr = ml_get(pos.lnum);
5297 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005298 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5299 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
5301#endif
5302
5303
5304 check_cursor();
5305 curwin->w_set_curswant = TRUE;
5306 if (did_change)
5307 {
5308 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5309 0L);
5310 curbuf->b_op_start = startpos;
5311 curbuf->b_op_end = curwin->w_cursor;
5312 if (curbuf->b_op_end.col > 0)
5313 --curbuf->b_op_end.col;
5314 }
5315}
5316
5317/*
5318 * Move cursor to mark.
5319 */
5320 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005321nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322{
5323 if (check_mark(pos) == FAIL)
5324 clearop(cap->oap);
5325 else
5326 {
5327 if (cap->cmdchar == '\''
5328 || cap->cmdchar == '`'
5329 || cap->cmdchar == '['
5330 || cap->cmdchar == ']')
5331 setpcmark();
5332 curwin->w_cursor = *pos;
5333 if (flag)
5334 beginline(BL_WHITE | BL_FIX);
5335 else
5336 check_cursor();
5337 }
5338 cap->oap->motion_type = flag ? MLINE : MCHAR;
5339 if (cap->cmdchar == '`')
5340 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005341 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 curwin->w_set_curswant = TRUE;
5343}
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345/*
5346 * Handle commands that are operators in Visual mode.
5347 */
5348 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005349v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005353 // Uppercase means linewise, except in block mode, then "D" deletes till
5354 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (isupper(cap->cmdchar))
5356 {
5357 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005358 {
5359 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5363 curwin->w_curswant = MAXCOL;
5364 }
5365 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5366 nv_operator(cap);
5367}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369/*
5370 * "s" and "S" commands.
5371 */
5372 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005373nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005375#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005376 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005377 if (term_swap_diff() == OK)
5378 return;
5379#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005380#ifdef FEAT_JOB_CHANNEL
5381 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5382 {
5383 clearopbeep(cap->oap);
5384 return;
5385 }
5386#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005387 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
5389 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005390 {
5391 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 cap->cmdchar = 'c';
5395 nv_operator(cap);
5396 }
5397 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 nv_optrans(cap);
5399}
5400
5401/*
5402 * Abbreviated commands.
5403 */
5404 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005405nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005408 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005410 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 if (VIsual_active)
5412 v_visop(cap);
5413 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 nv_optrans(cap);
5415}
5416
5417/*
5418 * Translate a command into another command.
5419 */
5420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005421nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5424 (char_u *)"d$", (char_u *)"c$",
5425 (char_u *)"cl", (char_u *)"cc",
5426 (char_u *)"yy", (char_u *)":s\r"};
5427 static char_u *str = (char_u *)"xXDCsSY&";
5428
5429 if (!checkclearopq(cap->oap))
5430 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005431 // In Vi "2D" doesn't delete the next line. Can't translate it
5432 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005433 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5434 {
5435 cap->oap->start = curwin->w_cursor;
5436 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005437#ifdef FEAT_EVAL
5438 set_op_var(OP_DELETE);
5439#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005440 cap->count1 = 1;
5441 nv_dollar(cap);
5442 finish_op = TRUE;
5443 ResetRedobuff();
5444 AppendCharToRedobuff('D');
5445 }
5446 else
5447 {
5448 if (cap->count0)
5449 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005450 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453 cap->opcount = 0;
5454}
5455
5456/*
5457 * "'" and "`" commands. Also for "g'" and "g`".
5458 * cap->arg is TRUE for "'" and "g'".
5459 */
5460 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005461nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
5463 pos_T *pos;
5464 int c;
5465#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005466 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005467 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468#endif
5469
5470 if (cap->cmdchar == 'g')
5471 c = cap->extra_char;
5472 else
5473 c = cap->nchar;
5474 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005475 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
5477 if (cap->arg)
5478 {
5479 check_cursor_lnum();
5480 beginline(BL_WHITE | BL_FIX);
5481 }
5482 else
5483 check_cursor();
5484 }
5485 else
5486 nv_cursormark(cap, cap->arg, pos);
5487
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005488 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 if (!virtual_active())
5490 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005491 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492#ifdef FEAT_FOLDING
5493 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005494 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005495 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 && (fdo_flags & FDO_MARK)
5497 && old_KeyTyped)
5498 foldOpenCursor();
5499#endif
5500}
5501
5502/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005503 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 */
5505 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005506nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508#ifdef FEAT_JUMPLIST
5509 pos_T *pos;
5510# ifdef FEAT_FOLDING
5511 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005512 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513# endif
5514
5515 if (!checkclearopq(cap->oap))
5516 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005517 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5518 {
5519 if (goto_tabpage_lastused() == FAIL)
5520 clearopbeep(cap->oap);
5521 return;
5522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 if (cap->cmdchar == 'g')
5524 pos = movechangelist((int)cap->count1);
5525 else
5526 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005527 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
5529 curwin->w_set_curswant = TRUE;
5530 check_cursor();
5531 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005532 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 nv_cursormark(cap, FALSE, pos);
5534 else if (cap->cmdchar == 'g')
5535 {
5536 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005537 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005539 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005541 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543 else
5544 clearopbeep(cap->oap);
5545# ifdef FEAT_FOLDING
5546 if (cap->oap->op_type == OP_NOP
5547 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5548 && (fdo_flags & FDO_MARK)
5549 && old_KeyTyped)
5550 foldOpenCursor();
5551# endif
5552 }
5553#else
5554 clearopbeep(cap->oap);
5555#endif
5556}
5557
5558/*
5559 * Handle '"' command.
5560 */
5561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005562nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 if (checkclearop(cap->oap))
5565 return;
5566#ifdef FEAT_EVAL
5567 if (cap->nchar == '=')
5568 cap->nchar = get_expr_register();
5569#endif
5570 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5571 {
5572 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005573 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574#ifdef FEAT_EVAL
5575 set_reg_var(cap->oap->regname);
5576#endif
5577 }
5578 else
5579 clearopbeep(cap->oap);
5580}
5581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582/*
5583 * Handle "v", "V" and "CTRL-V" commands.
5584 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5585 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005586 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 */
5588 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005589nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005591 if (cap->cmdchar == Ctrl_Q)
5592 cap->cmdchar = Ctrl_V;
5593
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005594 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5595 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 if (cap->oap->op_type != OP_NOP)
5597 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005598 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005599 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 return;
5601 }
5602
5603 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005604 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005606 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005608 else // toggle char/block mode
5609 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 VIsual_mode = cap->cmdchar;
5611 showmode();
5612 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005613 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005615 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
5617 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005618 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005620 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 VIsual = curwin->w_cursor;
5622
5623 VIsual_active = TRUE;
5624 VIsual_reselect = TRUE;
5625 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005626 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005629 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005630 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 /*
5632 * For V and ^V, we multiply the number of lines even if there
5633 * was only one -- webb
5634 */
5635 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5636 {
5637 curwin->w_cursor.lnum +=
5638 resel_VIsual_line_count * cap->count0 - 1;
5639 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5640 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5641 }
5642 VIsual_mode = resel_VIsual_mode;
5643 if (VIsual_mode == 'v')
5644 {
5645 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005646 {
5647 validate_virtcol();
5648 curwin->w_curswant = curwin->w_virtcol
5649 + resel_VIsual_vcol * cap->count0 - 1;
5650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005652 curwin->w_curswant = resel_VIsual_vcol;
5653 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005655 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
5657 curwin->w_curswant = MAXCOL;
5658 coladvance((colnr_T)MAXCOL);
5659 }
5660 else if (VIsual_mode == Ctrl_V)
5661 {
5662 validate_virtcol();
5663 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005664 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 coladvance(curwin->w_curswant);
5666 }
5667 else
5668 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005669 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 }
5671 else
5672 {
5673 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005674 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 may_start_select('c');
5676 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005677 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005678 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005679 if (cap->count0 > 0 && --cap->count1 > 0)
5680 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005681 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005682 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5683 nv_right(cap);
5684 else if (VIsual_mode == 'V')
5685 nv_down(cap);
5686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 }
5689}
5690
5691/*
5692 * Start selection for Shift-movement keys.
5693 */
5694 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005695start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005697 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 may_start_select('k');
5699 n_start_visual_mode('v');
5700}
5701
5702/*
5703 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5704 */
5705 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005706may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 VIsual_select = (stuff_empty() && typebuf_typed()
5709 && (vim_strchr(p_slm, c) != NULL));
5710}
5711
5712/*
5713 * Start Visual mode "c".
5714 * Should set VIsual_select before calling this.
5715 */
5716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005717n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005719#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005720 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005721 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005722#endif
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 VIsual_mode = c;
5725 VIsual_active = TRUE;
5726 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005727
5728 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005729 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005731 {
5732 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 VIsual = curwin->w_cursor;
5736
5737#ifdef FEAT_FOLDING
5738 foldAdjustVisual();
5739#endif
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005742#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005743 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005744 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005745#endif
5746
Bram Moolenaar09df3122006-01-23 22:23:09 +00005747 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005748 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005750 // Make sure the clipboard gets updated. Needed because start and
5751 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 clip_star.vmode = NUL;
5753#endif
5754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005755 // Only need to redraw this line, unless still need to redraw an old
5756 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 if (curwin->w_redr_type < INVERTED)
5758 {
5759 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5760 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5761 }
5762}
5763
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765/*
5766 * CTRL-W: Window commands
5767 */
5768 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005769nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005771 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005773 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005774 cap->cmdchar = ':';
5775 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005776 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005777 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005778 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005779 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780}
5781
5782/*
5783 * CTRL-Z: Suspend
5784 */
5785 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005786nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005790 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005791 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792}
5793
5794/*
5795 * Commands starting with "g".
5796 */
5797 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005798nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 int i;
5803 int flag = FALSE;
5804
5805 switch (cap->nchar)
5806 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005807 case Ctrl_A:
5808 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809#ifdef MEM_PROFILE
5810 /*
5811 * "g^A": dump log of used memory.
5812 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005813 if (!VIsual_active && cap->nchar == Ctrl_A)
5814 vim_mem_profile_dump();
5815 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005817 /*
5818 * "g^A/g^X": sequentially increment visually selected region
5819 */
5820 if (VIsual_active)
5821 {
5822 cap->arg = TRUE;
5823 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005824 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005825 nv_addsub(cap);
5826 }
5827 else
5828 clearopbeep(oap);
5829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 /*
5832 * "gR": Enter virtual replace mode.
5833 */
5834 case 'R':
5835 cap->arg = TRUE;
5836 nv_Replace(cap);
5837 break;
5838
5839 case 'r':
5840 nv_vreplace(cap);
5841 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
5843 case '&':
5844 do_cmdline_cmd((char_u *)"%s//~/&");
5845 break;
5846
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 /*
5848 * "gv": Reselect the previous Visual area. If Visual already active,
5849 * exchange previous and current Visual area.
5850 */
5851 case 'v':
5852 if (checkclearop(oap))
5853 break;
5854
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005855 if ( curbuf->b_visual.vi_start.lnum == 0
5856 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5857 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 beep_flush();
5859 else
5860 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005861 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (VIsual_active)
5863 {
5864 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005865 VIsual_mode = curbuf->b_visual.vi_mode;
5866 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867# ifdef FEAT_EVAL
5868 curbuf->b_visual_mode_eval = i;
5869# endif
5870 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005871 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5872 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005874 tpos = curbuf->b_visual.vi_end;
5875 curbuf->b_visual.vi_end = curwin->w_cursor;
5876 curwin->w_cursor = curbuf->b_visual.vi_start;
5877 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 }
5879 else
5880 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005881 VIsual_mode = curbuf->b_visual.vi_mode;
5882 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5883 tpos = curbuf->b_visual.vi_end;
5884 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
5886
5887 VIsual_active = TRUE;
5888 VIsual_reselect = TRUE;
5889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005890 // Set Visual to the start and w_cursor to the end of the Visual
5891 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 check_cursor();
5893 VIsual = curwin->w_cursor;
5894 curwin->w_cursor = tpos;
5895 check_cursor();
5896 update_topline();
5897 /*
5898 * When called from normal "g" command: start Select mode when
5899 * 'selectmode' contains "cmd". When called for K_SELECT, always
5900 * start Select mode.
5901 */
5902 if (cap->arg)
5903 VIsual_select = TRUE;
5904 else
5905 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005908 // Make sure the clipboard gets updated. Needed because start and
5909 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 clip_star.vmode = NUL;
5911#endif
5912 redraw_curbuf_later(INVERTED);
5913 showmode();
5914 }
5915 break;
5916 /*
5917 * "gV": Don't reselect the previous Visual area after a Select mode
5918 * mapping of menu.
5919 */
5920 case 'V':
5921 VIsual_reselect = FALSE;
5922 break;
5923
5924 /*
5925 * "gh": start Select mode.
5926 * "gH": start Select line mode.
5927 * "g^H": start Select block mode.
5928 */
5929 case K_BS:
5930 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005931 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 case 'h':
5933 case 'H':
5934 case Ctrl_H:
5935# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005936 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 if (cap->nchar == Ctrl_H)
5938 cap->cmdchar = Ctrl_V;
5939 else
5940# endif
5941 cap->cmdchar = cap->nchar + ('v' - 'h');
5942 cap->arg = TRUE;
5943 nv_visual(cap);
5944 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005946 // "gn", "gN" visually select next/previous search match
5947 // "gn" selects next match
5948 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005949 case 'N':
5950 case 'n':
5951 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005952 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
5955 /*
5956 * "gj" and "gk" two new funny movement keys -- up and down
5957 * movement based on *screen* line rather than *file* line.
5958 */
5959 case 'j':
5960 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005961 // with 'nowrap' it works just like the normal "j" command.
5962 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 {
5964 oap->motion_type = MLINE;
5965 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5966 }
5967 else
5968 i = nv_screengo(oap, FORWARD, cap->count1);
5969 if (i == FAIL)
5970 clearopbeep(oap);
5971 break;
5972
5973 case 'k':
5974 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005975 // with 'nowrap' it works just like the normal "k" command.
5976 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {
5978 oap->motion_type = MLINE;
5979 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5980 }
5981 else
5982 i = nv_screengo(oap, BACKWARD, cap->count1);
5983 if (i == FAIL)
5984 clearopbeep(oap);
5985 break;
5986
5987 /*
5988 * "gJ": join two lines without inserting a space.
5989 */
5990 case 'J':
5991 nv_join(cap);
5992 break;
5993
5994 /*
5995 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
5996 * "gm": middle of "g0" and "g$".
5997 */
5998 case '^':
5999 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006000 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001
6002 case '0':
6003 case 'm':
6004 case K_HOME:
6005 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 oap->motion_type = MCHAR;
6007 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006008 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006010 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 int width2 = width1 + curwin_col_off2();
6012
6013 validate_virtcol();
6014 i = 0;
6015 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6016 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6017 }
6018 else
6019 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006020 // Go to the middle of the screen line. When 'number' or
6021 // 'relativenumber' is on and lines are wrapping the middle can be more
6022 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006024 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 + ((curwin->w_p_wrap && i > 0)
6026 ? curwin_col_off2() : 0)) / 2;
6027 coladvance((colnr_T)i);
6028 if (flag)
6029 {
6030 do
6031 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006032 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006033 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 }
6035 curwin->w_set_curswant = TRUE;
6036 break;
6037
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006038 case 'M':
6039 {
6040 char_u *ptr = ml_get_curline();
6041
6042 oap->motion_type = MCHAR;
6043 oap->inclusive = FALSE;
6044 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006045 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006046 else
6047 i = (int)STRLEN(ptr);
6048 if (cap->count0 > 0 && cap->count0 <= 100)
6049 coladvance((colnr_T)(i * cap->count0 / 100));
6050 else
6051 coladvance((colnr_T)(i / 2));
6052 curwin->w_set_curswant = TRUE;
6053 }
6054 break;
6055
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006057 // "g_": to the last non-blank character in the line or <count> lines
6058 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 cap->oap->motion_type = MCHAR;
6060 cap->oap->inclusive = TRUE;
6061 curwin->w_curswant = MAXCOL;
6062 if (cursor_down((long)(cap->count1 - 1),
6063 cap->oap->op_type == OP_NOP) == FAIL)
6064 clearopbeep(cap->oap);
6065 else
6066 {
6067 char_u *ptr = ml_get_curline();
6068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006069 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6071 --curwin->w_cursor.col;
6072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006073 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006075 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 --curwin->w_cursor.col;
6077 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006078 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 }
6080 break;
6081
6082 case '$':
6083 case K_END:
6084 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 {
6086 int col_off = curwin_col_off();
6087
6088 oap->motion_type = MCHAR;
6089 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006090 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006092 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (cap->count1 == 1)
6094 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006095 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 int width2 = width1 + curwin_col_off2();
6097
6098 validate_virtcol();
6099 i = width1 - 1;
6100 if (curwin->w_virtcol >= (colnr_T)width1)
6101 i += ((curwin->w_virtcol - width1) / width2 + 1)
6102 * width2;
6103 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006104
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006105 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006106 validate_virtcol();
6107 curwin->w_curswant = curwin->w_virtcol;
6108 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6110 {
6111 /*
6112 * Check for landing on a character that got split at
6113 * the end of the line. We do not want to advance to
6114 * the next screen line.
6115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 if (curwin->w_virtcol > (colnr_T)i)
6117 --curwin->w_cursor.col;
6118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 }
6120 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6121 clearopbeep(oap);
6122 }
6123 else
6124 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006125 if (cap->count1 > 1)
6126 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006127 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006128
Bram Moolenaar02631462017-09-22 15:20:32 +02006129 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006131
Bram Moolenaard5c82342019-07-27 18:44:57 +02006132 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006133 validate_virtcol();
6134 curwin->w_curswant = curwin->w_virtcol;
6135 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 }
6137 }
6138 break;
6139
6140 /*
6141 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6142 */
6143 case '*':
6144 case '#':
6145#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006146 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006148 case Ctrl_RSB: // :tag or :tselect for current identifier
6149 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 nv_ident(cap);
6151 break;
6152
6153 /*
6154 * ge and gE: go back to end of word
6155 */
6156 case 'e':
6157 case 'E':
6158 oap->motion_type = MCHAR;
6159 curwin->w_set_curswant = TRUE;
6160 oap->inclusive = TRUE;
6161 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6162 clearopbeep(oap);
6163 break;
6164
6165 /*
6166 * "g CTRL-G": display info about cursor position
6167 */
6168 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006169 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 break;
6171
6172 /*
6173 * "gi": start Insert at the last position.
6174 */
6175 case 'i':
6176 if (curbuf->b_last_insert.lnum != 0)
6177 {
6178 curwin->w_cursor = curbuf->b_last_insert;
6179 check_cursor_lnum();
6180 i = (int)STRLEN(ml_get_curline());
6181 if (curwin->w_cursor.col > (colnr_T)i)
6182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 if (virtual_active())
6184 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 curwin->w_cursor.col = i;
6186 }
6187 }
6188 cap->cmdchar = 'i';
6189 nv_edit(cap);
6190 break;
6191
6192 /*
6193 * "gI": Start insert in column 1.
6194 */
6195 case 'I':
6196 beginline(0);
6197 if (!checkclearopq(oap))
6198 invoke_edit(cap, FALSE, 'g', FALSE);
6199 break;
6200
6201#ifdef FEAT_SEARCHPATH
6202 /*
6203 * "gf": goto file, edit file under cursor
6204 * "]f" and "[f": can also be used.
6205 */
6206 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006207 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 nv_gotofile(cap);
6209 break;
6210#endif
6211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006212 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 case '\'':
6214 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006215 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 case '`':
6217 nv_gomark(cap);
6218 break;
6219
6220 /*
6221 * "gs": Goto sleep.
6222 */
6223 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006224 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 break;
6226
6227 /*
6228 * "ga": Display the ascii value of the character under the
6229 * cursor. It is displayed in decimal, hex, and octal. -- webb
6230 */
6231 case 'a':
6232 do_ascii(NULL);
6233 break;
6234
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 /*
6236 * "g8": Display the bytes used for the UTF-8 character under the
6237 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006238 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 */
6240 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006241 if (cap->count0 == 8)
6242 utf_find_illegal();
6243 else
6244 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006247 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006248 case '<':
6249 show_sb_text();
6250 break;
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 /*
6253 * "gg": Goto the first line in file. With a count it goes to
6254 * that line number like for "G". -- webb
6255 */
6256 case 'g':
6257 cap->arg = FALSE;
6258 nv_goto(cap);
6259 break;
6260
6261 /*
6262 * Two-character operators:
6263 * "gq" Format text
6264 * "gw" Format text and keep cursor position
6265 * "g~" Toggle the case of the text.
6266 * "gu" Change text to lower case.
6267 * "gU" Change text to upper case.
6268 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006269 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 */
6271 case 'q':
6272 case 'w':
6273 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006274 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 case '~':
6276 case 'u':
6277 case 'U':
6278 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006279 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 nv_operator(cap);
6281 break;
6282
6283 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006284 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 * current function
6286 * "gD": idem, but in the current file.
6287 */
6288 case 'd':
6289 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006290 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 break;
6292
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 /*
6294 * g<*Mouse> : <C-*mouse>
6295 */
6296 case K_MIDDLEMOUSE:
6297 case K_MIDDLEDRAG:
6298 case K_MIDDLERELEASE:
6299 case K_LEFTMOUSE:
6300 case K_LEFTDRAG:
6301 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006302 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 case K_RIGHTMOUSE:
6304 case K_RIGHTDRAG:
6305 case K_RIGHTRELEASE:
6306 case K_X1MOUSE:
6307 case K_X1DRAG:
6308 case K_X1RELEASE:
6309 case K_X2MOUSE:
6310 case K_X2DRAG:
6311 case K_X2RELEASE:
6312 mod_mask = MOD_MASK_CTRL;
6313 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6314 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 case K_IGNORE:
6317 break;
6318
6319 /*
6320 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6321 */
6322 case 'p':
6323 case 'P':
6324 nv_put(cap);
6325 break;
6326
6327#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006328 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 case 'o':
6330 goto_byte(cap->count0);
6331 break;
6332#endif
6333
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006334 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006336 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
6338 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006339 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 break;
6341 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006342
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (!checkclearopq(oap))
6344 do_exmode(TRUE);
6345 break;
6346
6347#ifdef FEAT_JUMPLIST
6348 case ',':
6349 nv_pcmark(cap);
6350 break;
6351
6352 case ';':
6353 cap->count1 = -cap->count1;
6354 nv_pcmark(cap);
6355 break;
6356#endif
6357
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006359 if (!checkclearop(oap))
6360 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006361 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006362 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006363 if (!checkclearop(oap))
6364 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006365 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006366
Bram Moolenaar62a23252020-08-09 14:04:42 +02006367 case TAB:
6368 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6369 clearopbeep(oap);
6370 break;
6371
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006372 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006373 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006374 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006375 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006376 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006377 break;
6378
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 default:
6380 clearopbeep(oap);
6381 break;
6382 }
6383}
6384
6385/*
6386 * Handle "o" and "O" commands.
6387 */
6388 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006389n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006391#ifdef FEAT_CONCEAL
6392 linenr_T oldline = curwin->w_cursor.lnum;
6393#endif
6394
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 if (!checkclearopq(cap->oap))
6396 {
6397#ifdef FEAT_FOLDING
6398 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006399 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 (void)hasFolding(curwin->w_cursor.lnum,
6401 &curwin->w_cursor.lnum, NULL);
6402 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006403 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 (void)hasFolding(curwin->w_cursor.lnum,
6405 NULL, &curwin->w_cursor.lnum);
6406#endif
6407 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6408 (cap->cmdchar == 'O' ? 1 : 0)),
6409 (linenr_T)(curwin->w_cursor.lnum +
6410 (cap->cmdchar == 'o' ? 1 : 0))
6411 ) == OK
6412 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006413 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6414 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006416#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006417 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006418 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006419#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006420#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006421 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006422 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006423 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006424#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006425 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006426 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6427 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6429 }
6430 }
6431}
6432
6433/*
6434 * "." command: redo last change.
6435 */
6436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006437nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438{
6439 if (!checkclearopq(cap->oap))
6440 {
6441 /*
6442 * If "restart_edit" is TRUE, the last but one command is repeated
6443 * instead of the last command (inserting text). This is used for
6444 * CTRL-O <.> in insert mode.
6445 */
6446 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6447 clearopbeep(cap->oap);
6448 }
6449}
6450
6451/*
6452 * CTRL-R: undo undo
6453 */
6454 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006455nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
6457 if (!checkclearopq(cap->oap))
6458 {
6459 u_redo((int)cap->count1);
6460 curwin->w_set_curswant = TRUE;
6461 }
6462}
6463
6464/*
6465 * Handle "U" command.
6466 */
6467 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006468nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006470 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006471 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006473 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 cap->cmdchar = 'g';
6475 cap->nchar = 'U';
6476 nv_operator(cap);
6477 }
6478 else if (!checkclearopq(cap->oap))
6479 {
6480 u_undoline();
6481 curwin->w_set_curswant = TRUE;
6482 }
6483}
6484
6485/*
6486 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6487 * single character.
6488 */
6489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006490nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006492 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006493 {
6494#ifdef FEAT_JOB_CHANNEL
6495 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6496 {
6497 clearopbeep(cap->oap);
6498 return;
6499 }
6500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 else
6504 nv_operator(cap);
6505}
6506
6507/*
6508 * Handle an operator command.
6509 * The actual work is done by do_pending_operator().
6510 */
6511 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006512nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
6514 int op_type;
6515
6516 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006517#ifdef FEAT_JOB_CHANNEL
6518 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6519 {
6520 clearopbeep(cap->oap);
6521 return;
6522 }
6523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006525 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 nv_lineop(cap);
6527 else if (!checkclearop(cap->oap))
6528 {
6529 cap->oap->start = curwin->w_cursor;
6530 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006531#ifdef FEAT_EVAL
6532 set_op_var(op_type);
6533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
6535}
6536
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006537#ifdef FEAT_EVAL
6538/*
6539 * Set v:operator to the characters for "optype".
6540 */
6541 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006542set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006543{
6544 char_u opchars[3];
6545
6546 if (optype == OP_NOP)
6547 set_vim_var_string(VV_OP, NULL, 0);
6548 else
6549 {
6550 opchars[0] = get_op_char(optype);
6551 opchars[1] = get_extra_op_char(optype);
6552 opchars[2] = NUL;
6553 set_vim_var_string(VV_OP, opchars, -1);
6554 }
6555}
6556#endif
6557
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558/*
6559 * Handle linewise operator "dd", "yy", etc.
6560 *
6561 * "_" is is a strange motion command that helps make operators more logical.
6562 * It is actually implemented, but not documented in the real Vi. This motion
6563 * command actually refers to "the current line". Commands like "dd" and "yy"
6564 * are really an alternate form of "d_" and "y_". It does accept a count, so
6565 * "d3_" works to delete 3 lines.
6566 */
6567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006568nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 cap->oap->motion_type = MLINE;
6571 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6572 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006573 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006574 && cap->oap->motion_force != 'v'
6575 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 || cap->oap->op_type == OP_LSHIFT
6577 || cap->oap->op_type == OP_RSHIFT)
6578 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006579 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 beginline(BL_WHITE | BL_FIX);
6581}
6582
6583/*
6584 * <Home> command.
6585 */
6586 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006587nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006589 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006590 if (mod_mask & MOD_MASK_CTRL)
6591 nv_goto(cap);
6592 else
6593 {
6594 cap->count0 = 1;
6595 nv_pipe(cap);
6596 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006597 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6598 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599}
6600
6601/*
6602 * "|" command.
6603 */
6604 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006605nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 cap->oap->motion_type = MCHAR;
6608 cap->oap->inclusive = FALSE;
6609 beginline(0);
6610 if (cap->count0 > 0)
6611 {
6612 coladvance((colnr_T)(cap->count0 - 1));
6613 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6614 }
6615 else
6616 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006617 // keep curswant at the column where we wanted to go, not where
6618 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 curwin->w_set_curswant = FALSE;
6620}
6621
6622/*
6623 * Handle back-word command "b" and "B".
6624 * cap->arg is 1 for "B"
6625 */
6626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006627nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 cap->oap->motion_type = MCHAR;
6630 cap->oap->inclusive = FALSE;
6631 curwin->w_set_curswant = TRUE;
6632 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6633 clearopbeep(cap->oap);
6634#ifdef FEAT_FOLDING
6635 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6636 foldOpenCursor();
6637#endif
6638}
6639
6640/*
6641 * Handle word motion commands "e", "E", "w" and "W".
6642 * cap->arg is TRUE for "E" and "W".
6643 */
6644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006645nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 int n;
6648 int word_end;
6649 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006650 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652 /*
6653 * Set inclusive for the "E" and "e" command.
6654 */
6655 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6656 word_end = TRUE;
6657 else
6658 word_end = FALSE;
6659 cap->oap->inclusive = word_end;
6660
6661 /*
6662 * "cw" and "cW" are a special case.
6663 */
6664 if (!word_end && cap->oap->op_type == OP_CHANGE)
6665 {
6666 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006667 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006669 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 {
6671 /*
6672 * Reproduce a funny Vi behaviour: "cw" on a blank only
6673 * changes one character, not all blanks until the start of
6674 * the next word. Only do this when the 'w' flag is included
6675 * in 'cpoptions'.
6676 */
6677 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6678 {
6679 cap->oap->inclusive = TRUE;
6680 cap->oap->motion_type = MCHAR;
6681 return;
6682 }
6683 }
6684 else
6685 {
6686 /*
6687 * This is a little strange. To match what the real Vi does,
6688 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6689 * that we are not on a space or a TAB. This seems impolite
6690 * at first, but it's really more what we mean when we say
6691 * 'cw'.
6692 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006693 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 * will change only one character! This is done by setting
6695 * flag.
6696 */
6697 cap->oap->inclusive = TRUE;
6698 word_end = TRUE;
6699 flag = TRUE;
6700 }
6701 }
6702 }
6703
6704 cap->oap->motion_type = MCHAR;
6705 curwin->w_set_curswant = TRUE;
6706 if (word_end)
6707 n = end_word(cap->count1, cap->arg, flag, FALSE);
6708 else
6709 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6710
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006711 // Don't leave the cursor on the NUL past the end of line. Unless we
6712 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006713 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006714 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716 if (n == FAIL && cap->oap->op_type == OP_NOP)
6717 clearopbeep(cap->oap);
6718 else
6719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721#ifdef FEAT_FOLDING
6722 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6723 foldOpenCursor();
6724#endif
6725 }
6726}
6727
6728/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006729 * Used after a movement command: If the cursor ends up on the NUL after the
6730 * end of the line, may move it back to the last character and make the motion
6731 * inclusive.
6732 */
6733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006734adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006735{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006736 // The cursor cannot remain on the NUL when:
6737 // - the column is > 0
6738 // - not in Visual mode or 'selection' is "o"
6739 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006740 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006741 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006742 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006743 {
6744 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006745 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006746 if (has_mbyte)
6747 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006748 oap->inclusive = TRUE;
6749 }
6750}
6751
6752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 * "0" and "^" commands.
6754 * cap->arg is the argument for beginline().
6755 */
6756 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006757nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758{
6759 cap->oap->motion_type = MCHAR;
6760 cap->oap->inclusive = FALSE;
6761 beginline(cap->arg);
6762#ifdef FEAT_FOLDING
6763 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6764 foldOpenCursor();
6765#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006766 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6767 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768}
6769
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770/*
6771 * In exclusive Visual mode, may include the last character.
6772 */
6773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006774adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775{
6776 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006777 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (has_mbyte)
6780 inc_cursor();
6781 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 ++curwin->w_cursor.col;
6783 cap->oap->inclusive = FALSE;
6784 }
6785}
6786
6787/*
6788 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6789 * Should check VIsual_mode before calling this.
6790 * Returns TRUE when backed up to the previous line.
6791 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006792 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006793unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 pos_T *pp;
6796
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006797 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006799 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 pp = &curwin->w_cursor;
6801 else
6802 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 if (pp->coladd > 0)
6804 --pp->coladd;
6805 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (pp->col > 0)
6807 {
6808 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006809 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 }
6811 else if (pp->lnum > 1)
6812 {
6813 --pp->lnum;
6814 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6815 return TRUE;
6816 }
6817 }
6818 return FALSE;
6819}
6820
6821/*
6822 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6823 */
6824 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006825nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826{
6827 if (VIsual_active)
6828 VIsual_select = TRUE;
6829 else if (VIsual_reselect)
6830 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006831 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 cap->arg = TRUE;
6833 nv_g_cmd(cap);
6834 }
6835}
6836
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837
6838/*
6839 * "G", "gg", CTRL-END, CTRL-HOME.
6840 * cap->arg is TRUE for "G".
6841 */
6842 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006843nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 linenr_T lnum;
6846
6847 if (cap->arg)
6848 lnum = curbuf->b_ml.ml_line_count;
6849 else
6850 lnum = 1L;
6851 cap->oap->motion_type = MLINE;
6852 setpcmark();
6853
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006854 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 if (cap->count0 != 0)
6856 lnum = cap->count0;
6857 if (lnum < 1L)
6858 lnum = 1L;
6859 else if (lnum > curbuf->b_ml.ml_line_count)
6860 lnum = curbuf->b_ml.ml_line_count;
6861 curwin->w_cursor.lnum = lnum;
6862 beginline(BL_SOL | BL_FIX);
6863#ifdef FEAT_FOLDING
6864 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6865 foldOpenCursor();
6866#endif
6867}
6868
6869/*
6870 * CTRL-\ in Normal mode.
6871 */
6872 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006873nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874{
6875 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6876 {
6877 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006878 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006879 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 restart_edit = 0;
6881#ifdef FEAT_CMDWIN
6882 if (cmdwin_type != 0)
6883 cmdwin_result = Ctrl_C;
6884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (VIsual_active)
6886 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006887 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 redraw_curbuf_later(INVERTED);
6889 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006890 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 if (cap->nchar == Ctrl_G && p_im)
6892 restart_edit = 'a';
6893 }
6894 else
6895 clearopbeep(cap->oap);
6896}
6897
6898/*
6899 * ESC in Normal mode: beep, but don't flush buffers.
6900 * Don't even beep if we are canceling a command.
6901 */
6902 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006903nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
6905 int no_reason;
6906
6907 no_reason = (cap->oap->op_type == OP_NOP
6908 && cap->opcount == 0
6909 && cap->count0 == 0
6910 && cap->oap->regname == 0
6911 && !p_im);
6912
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006913 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {
6915 if (restart_edit == 0
6916#ifdef FEAT_CMDWIN
6917 && cmdwin_type == 0
6918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006921 {
6922 if (anyBufIsChanged())
6923 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6924 else
6925 msg(_("Type :qa and press <Enter> to exit Vim"));
6926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006928 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6929 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 if (!p_im)
6931 restart_edit = 0;
6932#ifdef FEAT_CMDWIN
6933 if (cmdwin_type != 0)
6934 {
6935 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 return;
6938 }
6939#endif
6940 }
6941
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 if (VIsual_active)
6943 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006944 end_visual_mode(); // stop Visual
6945 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 curwin->w_set_curswant = TRUE;
6947 redraw_curbuf_later(INVERTED);
6948 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006949 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006950 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 clearop(cap->oap);
6952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006953 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6954 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006955 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 restart_edit = 'a';
6957}
6958
6959/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006960 * Move the cursor for the "A" command.
6961 */
6962 void
6963set_cursor_for_append_to_line(void)
6964{
6965 curwin->w_set_curswant = TRUE;
6966 if (ve_flags == VE_ALL)
6967 {
6968 int save_State = State;
6969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006970 // Pretend Insert mode here to allow the cursor on the
6971 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006972 State = INSERT;
6973 coladvance((colnr_T)MAXCOL);
6974 State = save_State;
6975 }
6976 else
6977 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6978}
6979
6980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006982 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 */
6984 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006985nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006987 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6989 cap->cmdchar = 'i';
6990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006993 {
6994#ifdef FEAT_TERMINAL
6995 if (term_in_normal_mode())
6996 {
6997 end_visual_mode();
6998 clearop(cap->oap);
6999 term_enter_job_mode();
7000 return;
7001 }
7002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007006 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007007 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7008 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {
7010#ifdef FEAT_TEXTOBJ
7011 nv_object(cap);
7012#else
7013 clearopbeep(cap->oap);
7014#endif
7015 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007016#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007017 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007018 {
7019 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007020 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007021 return;
7022 }
7023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 else if (!curbuf->b_p_ma && !p_im)
7025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007026 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007027 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007029 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007030 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007031 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007033 else if (cap->cmdchar == K_PS && VIsual_active)
7034 {
7035 pos_T old_pos = curwin->w_cursor;
7036 pos_T old_visual = VIsual;
7037
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007038 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007039 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7040 {
7041 shift_delete_registers();
7042 cap->oap->regname = '1';
7043 }
7044 else
7045 cap->oap->regname = '-';
7046 cap->cmdchar = 'd';
7047 cap->nchar = NUL;
7048 nv_operator(cap);
7049 do_pending_operator(cap, 0, FALSE);
7050 cap->cmdchar = K_PS;
7051
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007052 // When the last char in the line was deleted then append. Detect this
7053 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007054 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7055 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007056 inc_cursor();
7057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007059 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 else if (!checkclearopq(cap->oap))
7062 {
7063 switch (cap->cmdchar)
7064 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007065 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007066 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 break;
7068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007069 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007070 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7071 beginline(BL_WHITE);
7072 else
7073 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 break;
7075
Bram Moolenaara1891842017-02-04 21:34:31 +01007076 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 // Bracketed paste works like "a"ppend, unless the cursor is in
7078 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007079 if (curwin->w_cursor.col == 0)
7080 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007083 case 'a': // "a"ppend is like "i"nsert on the next character.
7084 // increment coladd when in virtual space, increment the
7085 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 if (virtual_active()
7087 && (curwin->w_cursor.coladd > 0
7088 || *ml_get_cursor() == NUL
7089 || *ml_get_cursor() == TAB))
7090 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007091 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 inc_cursor();
7093 break;
7094 }
7095
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7097 {
7098 int save_State = State;
7099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007100 // Pretend Insert mode here to allow the cursor on the
7101 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 State = INSERT;
7103 coladvance(getviscol());
7104 State = save_State;
7105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7108 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007109 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007110 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007111 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112}
7113
7114/*
7115 * Invoke edit() and take care of "restart_edit" and the return value.
7116 */
7117 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007118invoke_edit(
7119 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007120 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007121 int cmd,
7122 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123{
7124 int restart_edit_save = 0;
7125
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007126 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7127 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7128 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (repl || !stuff_empty())
7130 restart_edit_save = restart_edit;
7131 else
7132 restart_edit_save = 0;
7133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007134 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 restart_edit = 0;
7136
7137 if (edit(cmd, startln, cap->count1))
7138 cap->retval |= CA_COMMAND_BUSY;
7139
7140 if (restart_edit == 0)
7141 restart_edit = restart_edit_save;
7142}
7143
7144#ifdef FEAT_TEXTOBJ
7145/*
7146 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7147 */
7148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007149nv_object(
7150 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 int flag;
7153 int include;
7154 char_u *mps_save;
7155
7156 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007157 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007159 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007161 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 mps_save = curbuf->b_p_mps;
7163 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7164
7165 switch (cap->nchar)
7166 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007167 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 flag = current_word(cap->oap, cap->count1, include, FALSE);
7169 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007170 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 flag = current_word(cap->oap, cap->count1, include, TRUE);
7172 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007173 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 case '(':
7175 case ')':
7176 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7177 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007178 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 case '{':
7180 case '}':
7181 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7182 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007183 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 case ']':
7185 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7186 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007187 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 case '>':
7189 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7190 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007191 case 't': // "at" = a tag block (xml and html)
7192 // Do not adjust oap->end in do_pending_operator()
7193 // otherwise there are different results for 'dit'
7194 // (note leading whitespace in last line):
7195 // 1) <b> 2) <b>
7196 // foobar foobar
7197 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007198 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007199 flag = current_tagblock(cap->oap, cap->count1, include);
7200 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007201 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 flag = current_par(cap->oap, cap->count1, include, 'p');
7203 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007204 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 flag = current_sent(cap->oap, cap->count1, include);
7206 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007207 case '"': // "a"" = a double quoted string
7208 case '\'': // "a'" = a single quoted string
7209 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007210 flag = current_quote(cap->oap, cap->count1, include,
7211 cap->nchar);
7212 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007213#if 0 // TODO
7214 case 'S': // "aS" = a section
7215 case 'f': // "af" = a filename
7216 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217#endif
7218 default:
7219 flag = FAIL;
7220 break;
7221 }
7222
7223 curbuf->b_p_mps = mps_save;
7224 if (flag == FAIL)
7225 clearopbeep(cap->oap);
7226 adjust_cursor_col();
7227 curwin->w_set_curswant = TRUE;
7228}
7229#endif
7230
7231/*
7232 * "q" command: Start/stop recording.
7233 * "q:", "q/", "q?": edit command-line in command-line window.
7234 */
7235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007236nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 if (cap->oap->op_type == OP_FORMAT)
7239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007240 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 cap->cmdchar = 'g';
7242 cap->nchar = 'q';
7243 nv_operator(cap);
7244 }
7245 else if (!checkclearop(cap->oap))
7246 {
7247#ifdef FEAT_CMDWIN
7248 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7249 {
7250 stuffcharReadbuff(cap->nchar);
7251 stuffcharReadbuff(K_CMDWIN);
7252 }
7253 else
7254#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007255 // (stop) recording into a named register, unless executing a
7256 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007257 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 clearopbeep(cap->oap);
7259 }
7260}
7261
7262/*
7263 * Handle the "@r" command.
7264 */
7265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007266nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267{
7268 if (checkclearop(cap->oap))
7269 return;
7270#ifdef FEAT_EVAL
7271 if (cap->nchar == '=')
7272 {
7273 if (get_expr_register() == NUL)
7274 return;
7275 }
7276#endif
7277 while (cap->count1-- && !got_int)
7278 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007279 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {
7281 clearopbeep(cap->oap);
7282 break;
7283 }
7284 line_breakcheck();
7285 }
7286}
7287
7288/*
7289 * Handle the CTRL-U and CTRL-D commands.
7290 */
7291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007292nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7295 || (cap->cmdchar == Ctrl_D
7296 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7297 clearopbeep(cap->oap);
7298 else if (!checkclearop(cap->oap))
7299 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7300}
7301
7302/*
7303 * Handle "J" or "gJ" command.
7304 */
7305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007306nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007308 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007310 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 {
7312 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007313 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7315 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007317 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007318 if (cap->count0 <= 2)
7319 {
7320 clearopbeep(cap->oap);
7321 return;
7322 }
7323 cap->count0 = curbuf->b_ml.ml_line_count
7324 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007326
7327 prep_redo(cap->oap->regname, cap->count0,
7328 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7329 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 }
7331}
7332
7333/*
7334 * "P", "gP", "p" and "gp" commands.
7335 */
7336 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007337nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007339 nv_put_opt(cap, FALSE);
7340}
7341
7342/*
7343 * "P", "gP", "p" and "gp" commands.
7344 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7345 */
7346 static void
7347nv_put_opt(cmdarg_T *cap, int fix_indent)
7348{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 int regname = 0;
7350 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007351 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007352 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 int dir;
7354 int flags = 0;
7355
7356 if (cap->oap->op_type != OP_NOP)
7357 {
7358#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007359 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7361 {
7362 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007363 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
7365 else
7366#endif
7367 clearopbeep(cap->oap);
7368 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007369#ifdef FEAT_JOB_CHANNEL
7370 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7371 {
7372 clearopbeep(cap->oap);
7373 }
7374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 else
7376 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007377 if (fix_indent)
7378 {
7379 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7380 ? FORWARD : BACKWARD;
7381 flags |= PUT_FIXINDENT;
7382 }
7383 else
7384 dir = (cap->cmdchar == 'P'
7385 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 ? BACKWARD : FORWARD;
7387 prep_redo_cmd(cap);
7388 if (cap->cmdchar == 'g')
7389 flags |= PUT_CURSEND;
7390
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if (VIsual_active)
7392 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007393 // Putting in Visual mode: The put text replaces the selected
7394 // text. First delete the selected text, then put the new text.
7395 // Need to save and restore the registers that the delete
7396 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007397 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007399#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007401#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007402 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007403 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007404#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
7408 )
7409 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007410 // The delete is going to overwrite the register we want to
7411 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 reg1 = get_register(regname, TRUE);
7413 }
7414
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007415 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 cap->cmdchar = 'd';
7417 cap->nchar = NUL;
7418 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007419 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 nv_operator(cap);
7421 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007422 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007423 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007425 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 cap->oap->regname = regname;
7427
7428 if (reg1 != NULL)
7429 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007430 // Delete probably changed the register we want to put, save
7431 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 reg2 = get_register(regname, FALSE);
7433 put_register(regname, reg1);
7434 }
7435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007436 // When deleted a linewise Visual area, put the register as
7437 // lines to avoid it joined with the next line. When deletion was
7438 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 if (VIsual_mode == 'V')
7440 flags |= PUT_LINE;
7441 else if (VIsual_mode == 'v')
7442 flags |= PUT_LINE_SPLIT;
7443 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7444 flags |= PUT_LINE_FORWARD;
7445 dir = BACKWARD;
7446 if ((VIsual_mode != 'V'
7447 && curwin->w_cursor.col < curbuf->b_op_start.col)
7448 || (VIsual_mode == 'V'
7449 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007450 // cursor is at the end of the line or end of file, put
7451 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007453 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007454 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007456 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007458 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (reg2 != NULL)
7460 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007462 // What to reselect with "gv"? Selecting the just put text seems to
7463 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007464 if (was_visual)
7465 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007466 curbuf->b_visual.vi_start = curbuf->b_op_start;
7467 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007468 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007469 if (*p_sel == 'e')
7470 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007471 }
7472
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007473 // When all lines were selected and deleted do_put() leaves an empty
7474 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007475 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007476 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007477 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007478 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007480 // If the cursor was in that line, move it to the end of the last
7481 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007482 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7483 {
7484 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7485 coladvance((colnr_T)MAXCOL);
7486 }
7487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 auto_format(FALSE, TRUE);
7489 }
7490}
7491
7492/*
7493 * "o" and "O" commands.
7494 */
7495 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007496nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007499 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7501 {
7502 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007503 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 }
7505 else
7506#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007507 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007509#ifdef FEAT_JOB_CHANNEL
7510 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007511 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 n_opencmd(cap);
7515}
7516
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517#ifdef FEAT_NETBEANS_INTG
7518 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007519nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 netbeans_keycommand(cap->nchar);
7522}
7523#endif
7524
7525#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007527nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007529 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530}
7531#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007532
Bram Moolenaar3918c952005-03-15 22:34:55 +00007533/*
7534 * Trigger CursorHold event.
7535 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7536 * input buffer. "did_cursorhold" is set to avoid retriggering.
7537 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007539nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007540{
7541 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7542 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007543 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007544}