blob: 966c1a1faa94addcbcb9551b3d35b4527021992f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010020#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010022#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020023static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
29/*
30 * nv_*(): functions called to handle Normal and Visual mode commands.
31 * n_*(): functions called to handle Normal mode commands.
32 * v_*(): functions called to handle Visual mode commands.
33 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void nv_ignore(cmdarg_T *cap);
35static void nv_nop(cmdarg_T *cap);
36static void nv_error(cmdarg_T *cap);
37static void nv_help(cmdarg_T *cap);
38static void nv_addsub(cmdarg_T *cap);
39static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_ver_scrollbar(cmdarg_T *cap);
43static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000045#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void nv_tabline(cmdarg_T *cap);
47static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000048#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static void nv_exmode(cmdarg_T *cap);
50static void nv_colon(cmdarg_T *cap);
51static void nv_ctrlg(cmdarg_T *cap);
52static void nv_ctrlh(cmdarg_T *cap);
53static void nv_clear(cmdarg_T *cap);
54static void nv_ctrlo(cmdarg_T *cap);
55static void nv_hat(cmdarg_T *cap);
56static void nv_Zet(cmdarg_T *cap);
57static void nv_ident(cmdarg_T *cap);
58static void nv_tagpop(cmdarg_T *cap);
59static void nv_scroll(cmdarg_T *cap);
60static void nv_right(cmdarg_T *cap);
61static void nv_left(cmdarg_T *cap);
62static void nv_up(cmdarg_T *cap);
63static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void nv_end(cmdarg_T *cap);
65static void nv_dollar(cmdarg_T *cap);
66static void nv_search(cmdarg_T *cap);
67static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020068static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_csearch(cmdarg_T *cap);
70static void nv_brackets(cmdarg_T *cap);
71static void nv_percent(cmdarg_T *cap);
72static void nv_brace(cmdarg_T *cap);
73static void nv_mark(cmdarg_T *cap);
74static void nv_findpar(cmdarg_T *cap);
75static void nv_undo(cmdarg_T *cap);
76static void nv_kundo(cmdarg_T *cap);
77static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
80static void v_visop(cmdarg_T *cap);
81static void nv_subst(cmdarg_T *cap);
82static void nv_abbrev(cmdarg_T *cap);
83static void nv_optrans(cmdarg_T *cap);
84static void nv_gomark(cmdarg_T *cap);
85static void nv_pcmark(cmdarg_T *cap);
86static void nv_regname(cmdarg_T *cap);
87static void nv_visual(cmdarg_T *cap);
88static void n_start_visual_mode(int c);
89static void nv_window(cmdarg_T *cap);
90static void nv_suspend(cmdarg_T *cap);
91static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_dot(cmdarg_T *cap);
93static void nv_redo(cmdarg_T *cap);
94static void nv_Undo(cmdarg_T *cap);
95static void nv_tilde(cmdarg_T *cap);
96static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_lineop(cmdarg_T *cap);
101static void nv_home(cmdarg_T *cap);
102static void nv_pipe(cmdarg_T *cap);
103static void nv_bck_word(cmdarg_T *cap);
104static void nv_wordcmd(cmdarg_T *cap);
105static void nv_beginline(cmdarg_T *cap);
106static void adjust_cursor(oparg_T *oap);
107static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100108static void nv_select(cmdarg_T *cap);
109static void nv_goto(cmdarg_T *cap);
110static void nv_normal(cmdarg_T *cap);
111static void nv_esc(cmdarg_T *oap);
112static void nv_edit(cmdarg_T *cap);
113static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_record(cmdarg_T *cap);
118static void nv_at(cmdarg_T *cap);
119static void nv_halfpage(cmdarg_T *cap);
120static void nv_join(cmdarg_T *cap);
121static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200122static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100130static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000132static char *e_noident = N_("E349: No identifier under cursor");
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
135 * Function to be called for a Normal or Visual mode command.
136 * The argument is a cmdarg_T.
137 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100138typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100140// Values for cmd_flags.
141#define NV_NCH 0x01 // may need to get a second char
142#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
143#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
144#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100146#define NV_SS 0x10 // may start selection
147#define NV_SSS 0x20 // may start selection with shift modifier
148#define NV_STS 0x40 // may stop selection without shift modif.
149#define NV_RL 0x80 // 'rightleft' modifies command
150#define NV_KEEPREG 0x100 // don't clear regname
151#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153/*
154 * Generally speaking, every Normal mode command should either clear any
155 * pending operator (with *clearop*()), or set the motion type variable
156 * oap->motion_type.
157 *
158 * When a cursor motion command is made, it is marked as being a character or
159 * line oriented motion. Then, if an operator is in effect, the operation
160 * becomes character or line oriented accordingly.
161 */
162
163/*
164 * This table contains one entry for every Normal or Visual mode command.
165 * The order doesn't matter, init_normal_cmds() will create a sorted index.
166 * It is faster when all keys from zero to '~' are present.
167 */
168static const struct nv_cmd
169{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100170 int cmd_char; // (first) command character
171 nv_func_T cmd_func; // function for this command
172 short_u cmd_flags; // NV_ flags
173 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174} nv_cmds[] =
175{
176 {NUL, nv_error, 0, 0},
177 {Ctrl_A, nv_addsub, 0, 0},
178 {Ctrl_B, nv_page, NV_STS, BACKWARD},
179 {Ctrl_C, nv_esc, 0, TRUE},
180 {Ctrl_D, nv_halfpage, 0, 0},
181 {Ctrl_E, nv_scroll_line, 0, TRUE},
182 {Ctrl_F, nv_page, NV_STS, FORWARD},
183 {Ctrl_G, nv_ctrlg, 0, 0},
184 {Ctrl_H, nv_ctrlh, 0, 0},
185 {Ctrl_I, nv_pcmark, 0, 0},
186 {NL, nv_down, 0, FALSE},
187 {Ctrl_K, nv_error, 0, 0},
188 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100189 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {Ctrl_N, nv_down, NV_STS, FALSE},
191 {Ctrl_O, nv_ctrlo, 0, 0},
192 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000193 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {Ctrl_R, nv_redo, 0, 0},
195 {Ctrl_S, nv_ignore, 0, 0},
196 {Ctrl_T, nv_tagpop, NV_NCW, 0},
197 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_V, nv_visual, 0, FALSE},
199 {'V', nv_visual, 0, FALSE},
200 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {Ctrl_W, nv_window, 0, 0},
202 {Ctrl_X, nv_addsub, 0, 0},
203 {Ctrl_Y, nv_scroll_line, 0, FALSE},
204 {Ctrl_Z, nv_suspend, 0, 0},
205 {ESC, nv_esc, 0, FALSE},
206 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
207 {Ctrl_RSB, nv_ident, NV_NCW, 0},
208 {Ctrl_HAT, nv_hat, NV_NCW, 0},
209 {Ctrl__, nv_error, 0, 0},
210 {' ', nv_right, 0, 0},
211 {'!', nv_operator, 0, 0},
212 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
213 {'#', nv_ident, 0, 0},
214 {'$', nv_dollar, 0, 0},
215 {'%', nv_percent, 0, 0},
216 {'&', nv_optrans, 0, 0},
217 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
218 {'(', nv_brace, 0, BACKWARD},
219 {')', nv_brace, 0, FORWARD},
220 {'*', nv_ident, 0, 0},
221 {'+', nv_down, 0, TRUE},
222 {',', nv_csearch, 0, TRUE},
223 {'-', nv_up, 0, TRUE},
224 {'.', nv_dot, NV_KEEPREG, 0},
225 {'/', nv_search, 0, FALSE},
226 {'0', nv_beginline, 0, 0},
227 {'1', nv_ignore, 0, 0},
228 {'2', nv_ignore, 0, 0},
229 {'3', nv_ignore, 0, 0},
230 {'4', nv_ignore, 0, 0},
231 {'5', nv_ignore, 0, 0},
232 {'6', nv_ignore, 0, 0},
233 {'7', nv_ignore, 0, 0},
234 {'8', nv_ignore, 0, 0},
235 {'9', nv_ignore, 0, 0},
236 {':', nv_colon, 0, 0},
237 {';', nv_csearch, 0, FALSE},
238 {'<', nv_operator, NV_RL, 0},
239 {'=', nv_operator, 0, 0},
240 {'>', nv_operator, NV_RL, 0},
241 {'?', nv_search, 0, FALSE},
242 {'@', nv_at, NV_NCH_NOP, FALSE},
243 {'A', nv_edit, 0, 0},
244 {'B', nv_bck_word, 0, 1},
245 {'C', nv_abbrev, NV_KEEPREG, 0},
246 {'D', nv_abbrev, NV_KEEPREG, 0},
247 {'E', nv_wordcmd, 0, TRUE},
248 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
249 {'G', nv_goto, 0, TRUE},
250 {'H', nv_scroll, 0, 0},
251 {'I', nv_edit, 0, 0},
252 {'J', nv_join, 0, 0},
253 {'K', nv_ident, 0, 0},
254 {'L', nv_scroll, 0, 0},
255 {'M', nv_scroll, 0, 0},
256 {'N', nv_next, 0, SEARCH_REV},
257 {'O', nv_open, 0, 0},
258 {'P', nv_put, 0, 0},
259 {'Q', nv_exmode, NV_NCW, 0},
260 {'R', nv_Replace, 0, FALSE},
261 {'S', nv_subst, NV_KEEPREG, 0},
262 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
263 {'U', nv_Undo, 0, 0},
264 {'W', nv_wordcmd, 0, TRUE},
265 {'X', nv_abbrev, NV_KEEPREG, 0},
266 {'Y', nv_abbrev, NV_KEEPREG, 0},
267 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
268 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
269 {'\\', nv_error, 0, 0},
270 {']', nv_brackets, NV_NCH_ALW, FORWARD},
271 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
272 {'_', nv_lineop, 0, 0},
273 {'`', nv_gomark, NV_NCH_ALW, FALSE},
274 {'a', nv_edit, NV_NCH, 0},
275 {'b', nv_bck_word, 0, 0},
276 {'c', nv_operator, 0, 0},
277 {'d', nv_operator, 0, 0},
278 {'e', nv_wordcmd, 0, FALSE},
279 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
280 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
281 {'h', nv_left, NV_RL, 0},
282 {'i', nv_edit, NV_NCH, 0},
283 {'j', nv_down, 0, FALSE},
284 {'k', nv_up, 0, FALSE},
285 {'l', nv_right, NV_RL, 0},
286 {'m', nv_mark, NV_NCH_NOP, 0},
287 {'n', nv_next, 0, 0},
288 {'o', nv_open, 0, 0},
289 {'p', nv_put, 0, 0},
290 {'q', nv_record, NV_NCH, 0},
291 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
292 {'s', nv_subst, NV_KEEPREG, 0},
293 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
294 {'u', nv_undo, 0, 0},
295 {'w', nv_wordcmd, 0, FALSE},
296 {'x', nv_abbrev, NV_KEEPREG, 0},
297 {'y', nv_operator, 0, 0},
298 {'z', nv_zet, NV_NCH_ALW, 0},
299 {'{', nv_findpar, 0, BACKWARD},
300 {'|', nv_pipe, 0, 0},
301 {'}', nv_findpar, 0, FORWARD},
302 {'~', nv_tilde, 0, 0},
303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100304 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200306 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
307 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
308 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
309 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 {K_LEFTMOUSE, nv_mouse, 0, 0},
311 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
312 {K_LEFTDRAG, nv_mouse, 0, 0},
313 {K_LEFTRELEASE, nv_mouse, 0, 0},
314 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100315 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
317 {K_MIDDLEDRAG, nv_mouse, 0, 0},
318 {K_MIDDLERELEASE, nv_mouse, 0, 0},
319 {K_RIGHTMOUSE, nv_mouse, 0, 0},
320 {K_RIGHTDRAG, nv_mouse, 0, 0},
321 {K_RIGHTRELEASE, nv_mouse, 0, 0},
322 {K_X1MOUSE, nv_mouse, 0, 0},
323 {K_X1DRAG, nv_mouse, 0, 0},
324 {K_X1RELEASE, nv_mouse, 0, 0},
325 {K_X2MOUSE, nv_mouse, 0, 0},
326 {K_X2DRAG, nv_mouse, 0, 0},
327 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000328 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000329 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {K_INS, nv_edit, 0, 0},
331 {K_KINS, nv_edit, 0, 0},
332 {K_BS, nv_ctrlh, 0, 0},
333 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
334 {K_S_UP, nv_page, NV_SS, BACKWARD},
335 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
336 {K_S_DOWN, nv_page, NV_SS, FORWARD},
337 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
339 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
340 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
341 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
342 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
343 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
344 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
345 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
346 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
347 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
348 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_S_END, nv_end, NV_SS, FALSE},
350 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
351 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
352 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {K_S_HOME, nv_home, NV_SS, 0},
354 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
355 {K_DEL, nv_abbrev, 0, 0},
356 {K_KDEL, nv_abbrev, 0, 0},
357 {K_UNDO, nv_kundo, 0, 0},
358 {K_HELP, nv_help, NV_NCW, 0},
359 {K_F1, nv_help, NV_NCW, 0},
360 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifdef FEAT_GUI
363 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
364 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
365#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#ifdef FEAT_GUI_TABLINE
367 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000368 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#ifdef FEAT_NETBEANS_INTG
371 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
372#endif
373#ifdef FEAT_DND
374 {K_DROP, nv_drop, NV_STS, 0},
375#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100377 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378};
379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100380// Number of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static short nv_cmd_idx[NV_CMDS_SIZE];
385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386// The highest index for which
387// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static int nv_max_linear;
389
390/*
391 * Compare functions for qsort() below, that checks the command character
392 * through the index in nv_cmd_idx[].
393 */
394 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100395nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396{
397 int c1, c2;
398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100399 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 c1 = nv_cmds[*(const short *)s1].cmd_char;
401 c2 = nv_cmds[*(const short *)s2].cmd_char;
402 if (c1 < 0)
403 c1 = -c1;
404 if (c2 < 0)
405 c2 = -c2;
406 return c1 - c2;
407}
408
409/*
410 * Initialize the nv_cmd_idx[] table.
411 */
412 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100413init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
415 int i;
416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100417 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000418 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 nv_cmd_idx[i] = i;
420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100421 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100424 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000425 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
427 break;
428 nv_max_linear = i - 1;
429}
430
431/*
432 * Search for a command in the commands table.
433 * Returns -1 for invalid command.
434 */
435 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 int i;
439 int idx;
440 int top, bot;
441 int c;
442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100443 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (cmdchar >= 0x100)
445 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100447 // We use the absolute value of the character. Special keys have a
448 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (cmdchar < 0)
450 cmdchar = -cmdchar;
451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // If the character is in the first part: The character is the index into
453 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (cmdchar <= nv_max_linear)
455 return nv_cmd_idx[cmdchar];
456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100457 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 bot = nv_max_linear + 1;
459 top = NV_CMDS_SIZE - 1;
460 idx = -1;
461 while (bot <= top)
462 {
463 i = (top + bot) / 2;
464 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
465 if (c < 0)
466 c = -c;
467 if (cmdchar == c)
468 {
469 idx = nv_cmd_idx[i];
470 break;
471 }
472 if (cmdchar > c)
473 bot = i + 1;
474 else
475 top = i - 1;
476 }
477 return idx;
478}
479
480/*
481 * Execute a command in Normal mode.
482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100484normal_cmd(
485 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100486 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100490 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 int old_col = curwin->w_curswant;
492#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100493 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100495 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 static int old_mapped_len = 0;
498 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000499#ifdef FEAT_EVAL
500 int set_prevcount = FALSE;
501#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200502 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
Bram Moolenaara80faa82020-04-12 19:37:17 +0200504 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000506
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100507 // Use a count remembered from before entering an operator. After typing
508 // "3d" we return from normal_cmd() and come back here, the "3" is
509 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 ca.opcount = opcount;
511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 /*
513 * If there is an operator pending, then the command we take this time
514 * will terminate it. Finish_op tells us to finish the operation before
515 * returning this time (unless the operation was cancelled).
516 */
517#ifdef CURSOR_SHAPE
518 c = finish_op;
519#endif
520 finish_op = (oap->op_type != OP_NOP);
521#ifdef CURSOR_SHAPE
522 if (finish_op != c)
523 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100524 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525# ifdef FEAT_MOUSESHAPE
526 update_mouseshape(-1);
527# endif
528 }
529#endif
530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100531 // When not finishing an operator and no register name typed, reset the
532 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000536#ifdef FEAT_EVAL
537 set_prevcount = TRUE;
538#endif
539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100541 // Restore counts from before receiving K_CURSORHOLD. This means after
542 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
543 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000544 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
545 {
546 ca.opcount = oap->prev_opcount;
547 ca.count0 = oap->prev_count0;
548 oap->prev_opcount = 0;
549 oap->prev_count0 = 0;
550 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000551
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554 State = NORMAL_BUSY;
555#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100556 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100559#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100560 // Set v:count here, when called from main() and not a stuffed
561 // command, so that v:count can be used in an expression mapping
562 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100563 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100564 set_vcount_ca(&ca, &set_prevcount);
565#endif
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 /*
568 * Get the command character from the user.
569 */
570 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100571 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
573 /*
574 * If a mapping was started in Visual or Select mode, remember the length
575 * of the mapping. This is used below to not return to Insert mode for as
576 * long as the mapping is being executed.
577 */
578 if (restart_edit == 0)
579 old_mapped_len = 0;
580 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000581 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 old_mapped_len = typebuf_maplen();
583
584 if (c == NUL)
585 c = K_ZERO;
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 /*
588 * In Select mode, typed text replaces the selection.
589 */
590 if (VIsual_active
591 && VIsual_select
592 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
595 // 'insertmode' is set) fake a "d"elete command, Insert mode will
596 // restart automatically.
597 // Insert the typed character in the typeahead buffer, so that it can
598 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200599 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000600 if (restart_edit != 0)
601 c = 'd';
602 else
603 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 msg_nowait = TRUE; // don't delay going to insert mode
605 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608#ifdef FEAT_CMDL_INFO
609 need_flushbuf = add_to_showcmd(c);
610#endif
611
612getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
615 /*
616 * Handle a count before a command and compute ca.count0.
617 * Note that '0' is a command and not the start of a count, but it's
618 * part of a count after other digits.
619 */
620 while ( (c >= '1' && c <= '9')
621 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
622 {
623 if (c == K_DEL || c == K_KDEL)
624 {
625 ca.count0 /= 10;
626#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
629 }
630 else
631 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100632 if (ca.count0 < 0) // got too large!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000634#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100635 // Set v:count here, when called from main() and not a stuffed
636 // command, so that v:count can be used in an expression mapping
637 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100638 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100639 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (ctrl_w)
642 {
643 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100644 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100646 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000647 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 --no_zero_mapping;
650 if (ctrl_w)
651 {
652 --no_mapping;
653 --allow_keys;
654 }
655#ifdef FEAT_CMDL_INFO
656 need_flushbuf |= add_to_showcmd(c);
657#endif
658 }
659
660 /*
661 * If we got CTRL-W there may be a/another count
662 */
663 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
664 {
665 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 ca.count0 = 0;
668 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 ++allow_keys; // no mapping for nchar, but keys
670 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 --no_mapping;
673 --allow_keys;
674#ifdef FEAT_CMDL_INFO
675 need_flushbuf |= add_to_showcmd(c);
676#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679 }
680
Bram Moolenaara983fe92008-07-31 20:04:27 +0000681 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 // Save the count values so that ca.opcount and ca.count0 are exactly
684 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000685 oap->prev_opcount = ca.opcount;
686 oap->prev_count0 = ca.count0;
687 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100688 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000689 {
690 /*
691 * If we're in the middle of an operator (including after entering a
692 * yank buffer with '"') AND we had a count before the operator, then
693 * that count overrides the current value of ca.count0.
694 * What this means effectively, is that commands like "3dw" get turned
695 * into "d3w" which makes things fall into place pretty neatly.
696 * If you give a count before AND after the operator, they are
697 * multiplied.
698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (ca.count0)
700 ca.count0 *= ca.opcount;
701 else
702 ca.count0 = ca.opcount;
703 }
704
705 /*
706 * Always remember the count. It will be set to zero (on the next call,
707 * above) when there is no pending operator.
708 * When called from main(), save the count for use by the "count" built-in
709 * variable.
710 */
711 ca.opcount = ca.count0;
712 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
713
714#ifdef FEAT_EVAL
715 /*
716 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100717 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100719 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000720 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#endif
722
723 /*
724 * Find the command character in the table of commands.
725 * For CTRL-W we already got nchar when looking for a count.
726 */
727 if (ctrl_w)
728 {
729 ca.nchar = c;
730 ca.cmdchar = Ctrl_W;
731 }
732 else
733 ca.cmdchar = c;
734 idx = find_command(ca.cmdchar);
735 if (idx < 0)
736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 clearopbeep(oap);
739 goto normal_end;
740 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000741
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000742 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100744 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000746 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 goto normal_end;
748 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000749 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
750 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /*
753 * In Visual/Select mode, a few keys are handled in a special way.
754 */
755 if (VIsual_active)
756 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100757 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (km_stopsel
759 && (nv_cmds[idx].cmd_flags & NV_STS)
760 && !(mod_mask & MOD_MASK_SHIFT))
761 {
762 end_visual_mode();
763 redraw_curbuf_later(INVERTED);
764 }
765
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100766 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (km_startsel)
768 {
769 if (nv_cmds[idx].cmd_flags & NV_SS)
770 {
771 unshift_special(&ca);
772 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000773 if (idx < 0)
774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100775 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000776 clearopbeep(oap);
777 goto normal_end;
778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
781 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786#ifdef FEAT_RIGHTLEFT
787 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
788 && (nv_cmds[idx].cmd_flags & NV_RL))
789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100790 // Invert horizontal movements and operations. Only when typed by the
791 // user directly, not when the result of a mapping or "x" translated
792 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 switch (ca.cmdchar)
794 {
795 case 'l': ca.cmdchar = 'h'; break;
796 case K_RIGHT: ca.cmdchar = K_LEFT; break;
797 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
798 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
799 case 'h': ca.cmdchar = 'l'; break;
800 case K_LEFT: ca.cmdchar = K_RIGHT; break;
801 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
802 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
803 case '>': ca.cmdchar = '<'; break;
804 case '<': ca.cmdchar = '>'; break;
805 }
806 idx = find_command(ca.cmdchar);
807 }
808#endif
809
810 /*
811 * Get an additional character if we need one.
812 */
813 if ((nv_cmds[idx].cmd_flags & NV_NCH)
814 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
815 && oap->op_type == OP_NOP)
816 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
817 || (ca.cmdchar == 'q'
818 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200819 && reg_recording == 0
820 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100822 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
824 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100825 int repl = FALSE; // get character for replace mode
826 int lit = FALSE; // get extra character literally
827 int langmap_active = FALSE; // using :lmap mappings
828 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100829#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100830 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#endif
832
833 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100834 ++allow_keys; // no mapping for nchar, but allow key codes
835 // Don't generate a CursorHold event here, most commands can't handle
836 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000837 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (ca.cmdchar == 'g')
839 {
840 /*
841 * For 'g' get the next character now, so that we can check for
842 * "gr", "g'" and "g`".
843 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000844 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#ifdef FEAT_CMDL_INFO
847 need_flushbuf |= add_to_showcmd(ca.nchar);
848#endif
849 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100850 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100852 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 else
862 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100863 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 repl = TRUE;
865 cp = &ca.nchar;
866 }
867 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
868
869 /*
870 * Get a second or third character.
871 */
872 if (cp != NULL)
873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (repl)
875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100876 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100877#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
882 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100883 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 --no_mapping;
885 --allow_keys;
886 if (repl)
887 State = LREPLACE;
888 else
889 State = LANGMAP;
890 langmap_active = TRUE;
891 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100892#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100894 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
896 im_set_active(TRUE);
897#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200898 if ((State & INSERT) && !p_ek)
899 {
900 // Disable bracketed paste and modifyOtherKeys here, we won't
901 // recognize the escape sequences with 'esckeys' off.
902 out_str(T_BD);
903 out_str(T_CTE);
904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000906 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaarca774f62020-08-30 20:46:38 +0200908 if ((State & INSERT) && !p_ek)
909 {
910 // Re-enable bracketed paste mode and modifyOtherKeys
911 out_str(T_BE);
912 out_str(T_CTI);
913 }
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (langmap_active)
916 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 ++no_mapping;
919 ++allow_keys;
920 State = NORMAL_BUSY;
921 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100922#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (lang)
924 {
925 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
926 im_save_status(&curbuf->b_p_iminsert);
927 im_set_active(FALSE);
928 }
929 p_smd = save_smd;
930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932#ifdef FEAT_CMDL_INFO
933 need_flushbuf |= add_to_showcmd(*cp);
934#endif
935
936 if (!lit)
937 {
938#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100939 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if (*cp == Ctrl_K
941 && ((nv_cmds[idx].cmd_flags & NV_LANG)
942 || cp == &ca.extra_char)
943 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
944 {
945 c = get_digraph(FALSE);
946 if (c > 0)
947 {
948 *cp = c;
949# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100950 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 del_from_showcmd(3);
952 need_flushbuf |= add_to_showcmd(*cp);
953# endif
954 }
955 }
956#endif
957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100961 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (p_hkmap && lang && KeyTyped)
963 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#endif
965 }
966
967 /*
968 * When the next character is CTRL-\ a following CTRL-N means the
969 * command is aborted and we go to Normal mode.
970 */
971 if (cp == &ca.extra_char
972 && ca.nchar == Ctrl_BSL
973 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
974 {
975 ca.cmdchar = Ctrl_BSL;
976 ca.nchar = ca.extra_char;
977 idx = find_command(ca.cmdchar);
978 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200979 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200980 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 else if (*cp == Ctrl_BSL)
982 {
983 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
984
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100985 // There is a busy wait here when typing "f<C-\>" and then
986 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 while ((c = vpeekc()) <= 0 && towait > 0L)
988 {
989 do_sleep(towait > 50L ? 50L : towait);
990 towait -= 50L;
991 }
992 if (c > 0)
993 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000994 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if (c != Ctrl_N && c != Ctrl_G)
996 vungetc(c);
997 else
998 {
999 ca.cmdchar = Ctrl_BSL;
1000 ca.nchar = c;
1001 idx = find_command(ca.cmdchar);
1002 }
1003 }
1004 }
1005
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001006 // When getting a text character and the next character is a
1007 // multi-byte character, it could be a composing character.
1008 // However, don't wait for it to arrive. Also, do enable mapping,
1009 // because if it's put back with vungetc() it's too late to apply
1010 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001011 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 while (enc_utf8 && lang && (c = vpeekc()) > 0
1013 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1014 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001015 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (!utf_iscomposing(c))
1017 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001018 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 break;
1020 }
1021 else if (ca.ncharC1 == 0)
1022 ca.ncharC1 = c;
1023 else
1024 ca.ncharC2 = c;
1025 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001026 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 }
1028 --no_mapping;
1029 --allow_keys;
1030 }
1031
1032#ifdef FEAT_CMDL_INFO
1033 /*
1034 * Flush the showcmd characters onto the screen so we can see them while
1035 * the command is being executed. Only do this when the shown command was
1036 * actually displayed, otherwise this will slow down a lot when executing
1037 * mappings.
1038 */
1039 if (need_flushbuf)
1040 out_flush();
1041#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001042 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001043 {
1044 if (ex_normal_busy)
1045 did_cursorhold = save_did_cursorhold;
1046 else
1047 did_cursorhold = FALSE;
1048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050 State = NORMAL;
1051
1052 if (ca.nchar == ESC)
1053 {
1054 clearop(oap);
1055 if (restart_edit == 0 && goto_im())
1056 restart_edit = 'a';
1057 goto normal_end;
1058 }
1059
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001060 if (ca.cmdchar != K_IGNORE)
1061 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001062 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001063 msg_col = 0;
1064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001066 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001068 // When 'keymodel' contains "startsel" some keys start Select/Visual
1069 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (!VIsual_active && km_startsel)
1071 {
1072 if (nv_cmds[idx].cmd_flags & NV_SS)
1073 {
1074 start_selection();
1075 unshift_special(&ca);
1076 idx = find_command(ca.cmdchar);
1077 }
1078 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1079 && (mod_mask & MOD_MASK_SHIFT))
1080 {
1081 start_selection();
1082 mod_mask &= ~MOD_MASK_SHIFT;
1083 }
1084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 /*
1087 * Execute the command!
1088 * Call the command function found in the commands table.
1089 */
1090 ca.arg = nv_cmds[idx].cmd_arg;
1091 (nv_cmds[idx].cmd_func)(&ca);
1092
1093 /*
1094 * If we didn't start or finish an operator, reset oap->regname, unless we
1095 * need it later.
1096 */
1097 if (!finish_op
1098 && !oap->op_type
1099 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1100 {
1101 clearop(oap);
1102#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001103 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#endif
1105 }
1106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001107 // Get the length of mapped chars again after typing a count, second
1108 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001109 if (old_mapped_len > 0)
1110 old_mapped_len = typebuf_maplen();
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 /*
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001113 * If an operation is pending, handle it. But not for K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001115 if (ca.cmdchar != K_IGNORE)
1116 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 /*
1119 * Wait for a moment when a message is displayed that will be overwritten
1120 * by the mode message.
1121 * In Visual mode and with "^O" in Insert mode, a short message will be
1122 * overwritten by the mode message. Wait a bit, until a key is hit.
1123 * In Visual mode, it's more important to keep the Visual area updated
1124 * than keeping a message (e.g. from a /pat search).
1125 * Only do this if the command was typed, not from a mapping.
1126 * Don't wait when emsg_silent is non-zero.
1127 * Also wait a bit after an error message, e.g. for "^O:".
1128 * Don't redraw the screen, it would remove the message.
1129 */
1130 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001131 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 || (VIsual_active
1134 && old_pos.lnum == curwin->w_cursor.lnum
1135 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 )
1137 && (clear_cmdline
1138 || redraw_cmdline)
1139 && (msg_didout || (msg_didany && msg_scroll))
1140 && !msg_nowait
1141 && KeyTyped)
1142 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 && (msg_scroll
1145 || emsg_on_display)))
1146 && oap->regname == 0
1147 && !(ca.retval & CA_COMMAND_BUSY)
1148 && stuff_empty()
1149 && typebuf_typed()
1150 && emsg_silent == 0
1151 && !did_wait_return
1152 && oap->op_type == OP_NOP)
1153 {
1154 int save_State = State;
1155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001156 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (restart_edit != 0)
1158 State = INSERT;
1159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001160 // If need to redraw, and there is a "keep_msg", redraw before the
1161 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1163 {
1164 char_u *kmsg;
1165
1166 kmsg = keep_msg;
1167 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001168 // Showmode() will clear keep_msg, but we want to use it anyway.
1169 // First update w_topline.
1170 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001172 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001174
1175 kmsg = vim_strsave(keep_msg);
1176 if (kmsg != NULL)
1177 {
1178 msg_attr((char *)kmsg, keep_msg_attr);
1179 vim_free(kmsg);
1180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
1182 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001183#ifdef CURSOR_SHAPE
1184 ui_cursor_shape(); // may show different cursor shape
1185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 cursor_on();
1187 out_flush();
1188 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001189 ui_delay(1003L, TRUE); // wait at least one second
1190 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 State = save_State;
1192
1193 msg_scroll = FALSE;
1194 emsg_on_display = FALSE;
1195 }
1196
1197 /*
1198 * Finish up after executing a Normal mode command.
1199 */
1200normal_end:
1201
1202 msg_nowait = FALSE;
1203
Bram Moolenaarcc613032020-06-07 21:31:18 +02001204#ifdef FEAT_EVAL
1205 if (finish_op)
1206 reset_reg_var();
1207#endif
1208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001209 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210#ifdef CURSOR_SHAPE
1211 c = finish_op;
1212#endif
1213 finish_op = FALSE;
1214#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001215 // Redraw the cursor with another shape, if we were in Operator-pending
1216 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (c || ca.cmdchar == 'r')
1218 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001219 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220# ifdef FEAT_MOUSESHAPE
1221 update_mouseshape(-1);
1222# endif
1223 }
1224#endif
1225
1226#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001227 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001228 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 clear_showcmd();
1230#endif
1231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001232 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 vim_free(ca.searchbuf);
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (has_mbyte)
1236 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (curwin->w_p_scb && toplevel)
1239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 do_check_scrollbind(TRUE);
1242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
Bram Moolenaar860cae12010-06-05 23:22:07 +02001244 if (curwin->w_p_crb && toplevel)
1245 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001246 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001247 do_check_cursorbind();
1248 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001249
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001250#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001251 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001252 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001253 restart_edit = 0;
1254#endif
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 /*
1257 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1258 * if still inside a mapping that started in Visual mode).
1259 * May switch from Visual to Select mode after CTRL-O command.
1260 */
1261 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1263 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 && !(ca.retval & CA_COMMAND_BUSY)
1265 && stuff_empty()
1266 && oap->regname == 0)
1267 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 if (restart_VIsual_select == 1)
1269 {
1270 VIsual_select = TRUE;
1271 showmode();
1272 restart_VIsual_select = 0;
1273 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001274 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 (void)edit(restart_edit, FALSE, 1L);
1276 }
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (restart_VIsual_select == 2)
1279 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001281 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 opcount = ca.opcount;
1283}
1284
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001285#ifdef FEAT_EVAL
1286/*
1287 * Set v:count and v:count1 according to "cap".
1288 * Set v:prevcount only when "set_prevcount" is TRUE.
1289 */
1290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001291set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001292{
1293 long count = cap->count0;
1294
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001295 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001296 if (cap->opcount != 0)
1297 count = cap->opcount * (count == 0 ? 1 : count);
1298 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001299 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001300}
1301#endif
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001304 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * if not.
1306 */
1307 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001308check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
1310 static int did_check = FALSE;
1311
1312 if (full_screen)
1313 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001314 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001315 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 did_check = TRUE;
1317 }
1318}
1319
1320/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001321 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 * This function should ALWAYS be called to end Visual mode, except from
1323 * do_pending_operator().
1324 */
1325 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001326end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327{
1328#ifdef FEAT_CLIPBOARD
1329 /*
1330 * If we are using the clipboard, then remember what was selected in case
1331 * we need to paste it somewhere while we still own the selection.
1332 * Only do this when the clipboard is already owned. Don't want to grab
1333 * the selection when hitting ESC.
1334 */
1335 if (clip_star.available && clip_star.owned)
1336 clip_auto_select();
1337#endif
1338
1339 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 setmouse();
1341 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001343 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001344 curbuf->b_visual.vi_mode = VIsual_mode;
1345 curbuf->b_visual.vi_start = VIsual;
1346 curbuf->b_visual.vi_end = curwin->w_cursor;
1347 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348#ifdef FEAT_EVAL
1349 curbuf->b_visual_mode_eval = VIsual_mode;
1350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (!virtual_active())
1352 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001353 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001355 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356}
1357
1358/*
1359 * Reset VIsual_active and VIsual_reselect.
1360 */
1361 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001362reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
1364 if (VIsual_active)
1365 {
1366 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001367 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369 VIsual_reselect = FALSE;
1370}
1371
1372/*
1373 * Reset VIsual_active and VIsual_reselect if it's set.
1374 */
1375 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001376reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
1378 if (VIsual_active)
1379 {
1380 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001381 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 VIsual_reselect = FALSE;
1383 }
1384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001386 void
1387restore_visual_mode(void)
1388{
1389 if (VIsual_mode_orig != NUL)
1390 {
1391 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1392 VIsual_mode_orig = NUL;
1393 }
1394}
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396/*
1397 * Check for a balloon-eval special item to include when searching for an
1398 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1399 * Returns TRUE if the character at "*ptr" should be included.
1400 * "dir" is FORWARD or BACKWARD, the direction of searching.
1401 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1402 * "bnp" points to a counter for square brackets.
1403 */
1404 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001405find_is_eval_item(
1406 char_u *ptr,
1407 int *colp,
1408 int *bnp,
1409 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001411 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1413 ++*bnp;
1414 if (*bnp > 0)
1415 {
1416 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1417 --*bnp;
1418 return TRUE;
1419 }
1420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001421 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (*ptr == '.')
1423 return TRUE;
1424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001425 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1427 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1428 {
1429 *colp += dir;
1430 return TRUE;
1431 }
1432 return FALSE;
1433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
1435/*
1436 * Find the identifier under or to the right of the cursor.
1437 * "find_type" can have one of three values:
1438 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001439 * FIND_STRING: find any non-white text
1440 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001441 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 *
1443 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001444 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001446 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 * This doesn't match the real Vi but I like it a little better and it
1448 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001449 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 * When FIND_IDENT isn't defined, we backup until a blank.
1451 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001452 * Returns the length of the text, or zero if no text is found.
1453 * If text is found, a pointer to the text is put in "*text". This
1454 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001457find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001460 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461}
1462
1463/*
1464 * Like find_ident_under_cursor(), but for any window and any position.
1465 * However: Uses 'iskeyword' from the current window!.
1466 */
1467 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001468find_ident_at_pos(
1469 win_T *wp,
1470 linenr_T lnum,
1471 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001472 char_u **text,
1473 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001474 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001477 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 int this_class = 0;
1480 int prev_class;
1481 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001482 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 /*
1485 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001486 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 */
1488 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1489 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1490 {
1491 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001492 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 */
1494 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (has_mbyte)
1496 {
1497 while (ptr[col] != NUL)
1498 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001499 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 this_class = mb_get_class(ptr + col);
1503 if (this_class != 0 && (i == 1 || this_class != 1))
1504 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001505 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 }
1507 }
1508 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001510 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 )
1513 ++col;
1514
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001515 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
1518 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001519 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (has_mbyte)
1522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001523 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1525 this_class = mb_get_class((char_u *)"a");
1526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001528 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1531 prev_class = mb_get_class(ptr + prevcol);
1532 if (this_class != prev_class
1533 && (i == 0
1534 || prev_class == 0
1535 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 && (!(find_type & FIND_EVAL)
1537 || prevcol == 0
1538 || !find_is_eval_item(ptr + prevcol, &prevcol,
1539 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 )
1541 break;
1542 col = prevcol;
1543 }
1544
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001545 // If we don't want just any old text, or we've found an
1546 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (this_class > 2)
1548 this_class = 2;
1549 if (!(find_type & FIND_STRING) || this_class == 2)
1550 break;
1551 }
1552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
1554 while (col > 0
1555 && ((i == 0
1556 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001557 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 && (!(find_type & FIND_IDENT)
1559 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 || ((find_type & FIND_EVAL)
1561 && col > 1
1562 && find_is_eval_item(ptr + col - 1, &col,
1563 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 ))
1565 --col;
1566
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001567 // If we don't want just any old text, or we've found an
1568 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1570 break;
1571 }
1572 }
1573
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001574 if (ptr[col] == NUL || (i == 0
1575 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001577 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001578 if ((find_type & FIND_NOERROR) == 0)
1579 {
1580 if (find_type & FIND_STRING)
1581 emsg(_("E348: No string under cursor"));
1582 else
1583 emsg(_(e_noident));
1584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 return 0;
1586 }
1587 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001588 *text = ptr;
1589 if (textcol != NULL)
1590 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
1592 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001593 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 bn = 0;
1596 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (has_mbyte)
1599 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001600 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 this_class = mb_get_class(ptr);
1602 while (ptr[col] != NUL
1603 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1604 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 || ((find_type & FIND_EVAL)
1606 && col <= (int)startcol
1607 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001609 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001613 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 || ((find_type & FIND_EVAL)
1615 && col <= (int)startcol
1616 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
1620 return col;
1621}
1622
1623/*
1624 * Prepare for redo of a normal command.
1625 */
1626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001627prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 prep_redo(cap->oap->regname, cap->count0,
1630 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1631}
1632
1633/*
1634 * Prepare for redo of any command.
1635 * Note that only the last argument can be a multi-byte char.
1636 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001637 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001638prep_redo(
1639 int regname,
1640 long num,
1641 int cmd1,
1642 int cmd2,
1643 int cmd3,
1644 int cmd4,
1645 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
1647 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001648 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 AppendCharToRedobuff('"');
1651 AppendCharToRedobuff(regname);
1652 }
1653 if (num)
1654 AppendNumberToRedobuff(num);
1655
1656 if (cmd1 != NUL)
1657 AppendCharToRedobuff(cmd1);
1658 if (cmd2 != NUL)
1659 AppendCharToRedobuff(cmd2);
1660 if (cmd3 != NUL)
1661 AppendCharToRedobuff(cmd3);
1662 if (cmd4 != NUL)
1663 AppendCharToRedobuff(cmd4);
1664 if (cmd5 != NUL)
1665 AppendCharToRedobuff(cmd5);
1666}
1667
1668/*
1669 * check for operator active and clear it
1670 *
1671 * return TRUE if operator was active
1672 */
1673 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001674checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 if (oap->op_type == OP_NOP)
1677 return FALSE;
1678 clearopbeep(oap);
1679 return TRUE;
1680}
1681
1682/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001683 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001685 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 */
1687 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001688checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001690 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 return FALSE;
1692 clearopbeep(oap);
1693 return TRUE;
1694}
1695
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001696 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001697clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 oap->op_type = OP_NOP;
1700 oap->regname = 0;
1701 oap->motion_force = NUL;
1702 oap->use_reg_one = FALSE;
1703}
1704
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001705 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001706clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 clearop(oap);
1709 beep_flush();
1710}
1711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712/*
1713 * Remove the shift modifier from a special key.
1714 */
1715 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001716unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718 switch (cap->cmdchar)
1719 {
1720 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1721 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1722 case K_S_UP: cap->cmdchar = K_UP; break;
1723 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1724 case K_S_HOME: cap->cmdchar = K_HOME; break;
1725 case K_S_END: cap->cmdchar = K_END; break;
1726 }
1727 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1728}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001730/*
1731 * If the mode is currently displayed clear the command line or update the
1732 * command displayed.
1733 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001734 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001735may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001736{
1737 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001738 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001739#ifdef FEAT_CMDL_INFO
1740 else
1741 clear_showcmd();
1742#endif
1743}
1744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1746/*
1747 * Routines for displaying a partly typed command
1748 */
1749
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001750#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001752static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753static int showcmd_is_clear = TRUE;
1754static int showcmd_visual = FALSE;
1755
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001756static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
1758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001759clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 if (!p_sc)
1762 return;
1763
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 if (VIsual_active && !char_avail())
1765 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001766 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 long lines;
1768 colnr_T leftcol, rightcol;
1769 linenr_T top, bot;
1770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001771 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001772 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
1774 top = VIsual.lnum;
1775 bot = curwin->w_cursor.lnum;
1776 }
1777 else
1778 {
1779 top = curwin->w_cursor.lnum;
1780 bot = VIsual.lnum;
1781 }
1782# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001783 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001784 (void)hasFolding(top, &top, NULL);
1785 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786# endif
1787 lines = bot - top + 1;
1788
1789 if (VIsual_mode == Ctrl_V)
1790 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001791# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001792 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001793 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001794
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001795 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001796 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001797 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001800# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001801 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001802 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1805 (long)(rightcol - leftcol + 1));
1806 }
1807 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1808 sprintf((char *)showcmd_buf, "%ld", lines);
1809 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001810 {
1811 char_u *s, *e;
1812 int l;
1813 int bytes = 0;
1814 int chars = 0;
1815
1816 if (cursor_bot)
1817 {
1818 s = ml_get_pos(&VIsual);
1819 e = ml_get_cursor();
1820 }
1821 else
1822 {
1823 s = ml_get_cursor();
1824 e = ml_get_pos(&VIsual);
1825 }
1826 while ((*p_sel != 'e') ? s <= e : s < e)
1827 {
1828 l = (*mb_ptr2len)(s);
1829 if (l == 0)
1830 {
1831 ++bytes;
1832 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001833 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001834 }
1835 bytes += l;
1836 ++chars;
1837 s += l;
1838 }
1839 if (bytes == chars)
1840 sprintf((char *)showcmd_buf, "%d", chars);
1841 else
1842 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1843 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001844 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 showcmd_visual = TRUE;
1846 }
1847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
1849 showcmd_buf[0] = NUL;
1850 showcmd_visual = FALSE;
1851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001852 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (showcmd_is_clear)
1854 return;
1855 }
1856
1857 display_showcmd();
1858}
1859
1860/*
1861 * Add 'c' to string of shown command chars.
1862 * Return TRUE if output has been written (and setcursor() has been called).
1863 */
1864 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001865add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 char_u *p;
1868 int old_len;
1869 int extra_len;
1870 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 int i;
1872 static int ignore[] =
1873 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001874#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1876 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001877#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001878 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001879 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1881 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001882 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001884 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 0
1886 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
Bram Moolenaar09df3122006-01-23 22:23:09 +00001888 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 return FALSE;
1890
1891 if (showcmd_visual)
1892 {
1893 showcmd_buf[0] = NUL;
1894 showcmd_visual = FALSE;
1895 }
1896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001897 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (IS_SPECIAL(c))
1899 for (i = 0; ignore[i] != 0; ++i)
1900 if (ignore[i] == c)
1901 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
1903 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001904 if (*p == ' ')
1905 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 old_len = (int)STRLEN(showcmd_buf);
1907 extra_len = (int)STRLEN(p);
1908 overflow = old_len + extra_len - SHOWCMD_COLS;
1909 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001910 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1911 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 STRCAT(showcmd_buf, p);
1913
1914 if (char_avail())
1915 return FALSE;
1916
1917 display_showcmd();
1918
1919 return TRUE;
1920}
1921
1922 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001923add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924{
1925 if (!add_to_showcmd(c))
1926 setcursor();
1927}
1928
1929/*
1930 * Delete 'len' characters from the end of the shown command.
1931 */
1932 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001933del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
1935 int old_len;
1936
1937 if (!p_sc)
1938 return;
1939
1940 old_len = (int)STRLEN(showcmd_buf);
1941 if (len > old_len)
1942 len = old_len;
1943 showcmd_buf[old_len - len] = NUL;
1944
1945 if (!char_avail())
1946 display_showcmd();
1947}
1948
1949/*
1950 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1951 * something and there is a partial mapping.
1952 */
1953 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001954push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955{
1956 if (p_sc)
1957 STRCPY(old_showcmd_buf, showcmd_buf);
1958}
1959
1960 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001961pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963 if (!p_sc)
1964 return;
1965
1966 STRCPY(showcmd_buf, old_showcmd_buf);
1967
1968 display_showcmd();
1969}
1970
1971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001972display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973{
1974 int len;
1975
1976 cursor_off();
1977
1978 len = (int)STRLEN(showcmd_buf);
1979 if (len == 0)
1980 showcmd_is_clear = TRUE;
1981 else
1982 {
1983 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1984 showcmd_is_clear = FALSE;
1985 }
1986
1987 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001988 * clear the rest of an old message by outputting up to SHOWCMD_COLS
1989 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 */
1991 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001993 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994}
1995#endif
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997/*
1998 * When "check" is FALSE, prepare for commands that scroll the window.
1999 * When "check" is TRUE, take care of scroll-binding after the window has
2000 * scrolled. Called from normal_cmd() and edit().
2001 */
2002 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002003do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004{
2005 static win_T *old_curwin = NULL;
2006 static linenr_T old_topline = 0;
2007#ifdef FEAT_DIFF
2008 static int old_topfill = 0;
2009#endif
2010 static buf_T *old_buf = NULL;
2011 static colnr_T old_leftcol = 0;
2012
2013 if (check && curwin->w_p_scb)
2014 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002015 // If a ":syncbind" command was just used, don't scroll, only reset
2016 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (did_syncbind)
2018 did_syncbind = FALSE;
2019 else if (curwin == old_curwin)
2020 {
2021 /*
2022 * Synchronize other windows, as necessary according to
2023 * 'scrollbind'. Don't do this after an ":edit" command, except
2024 * when 'diff' is set.
2025 */
2026 if ((curwin->w_buffer == old_buf
2027#ifdef FEAT_DIFF
2028 || curwin->w_p_diff
2029#endif
2030 )
2031 && (curwin->w_topline != old_topline
2032#ifdef FEAT_DIFF
2033 || curwin->w_topfill != old_topfill
2034#endif
2035 || curwin->w_leftcol != old_leftcol))
2036 {
2037 check_scrollbind(curwin->w_topline - old_topline,
2038 (long)(curwin->w_leftcol - old_leftcol));
2039 }
2040 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002041 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
2043 /*
2044 * When switching between windows, make sure that the relative
2045 * vertical offset is valid for the new window. The relative
2046 * offset is invalid whenever another 'scrollbind' window has
2047 * scrolled to a point that would force the current window to
2048 * scroll past the beginning or end of its buffer. When the
2049 * resync is performed, some of the other 'scrollbind' windows may
2050 * need to jump so that the current window's relative position is
2051 * visible on-screen.
2052 */
2053 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2054 }
2055 curwin->w_scbind_pos = curwin->w_topline;
2056 }
2057
2058 old_curwin = curwin;
2059 old_topline = curwin->w_topline;
2060#ifdef FEAT_DIFF
2061 old_topfill = curwin->w_topfill;
2062#endif
2063 old_buf = curwin->w_buffer;
2064 old_leftcol = curwin->w_leftcol;
2065}
2066
2067/*
2068 * Synchronize any windows that have "scrollbind" set, based on the
2069 * number of rows by which the current window has changed
2070 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2071 */
2072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002073check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074{
2075 int want_ver;
2076 int want_hor;
2077 win_T *old_curwin = curwin;
2078 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 int old_VIsual_select = VIsual_select;
2080 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 colnr_T tgt_leftcol = curwin->w_leftcol;
2082 long topline;
2083 long y;
2084
2085 /*
2086 * check 'scrollopt' string for vertical and horizontal scroll options
2087 */
2088 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2089#ifdef FEAT_DIFF
2090 want_ver |= old_curwin->w_p_diff;
2091#endif
2092 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2093
2094 /*
2095 * loop through the scrollbound windows and scroll accordingly
2096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002098 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
2100 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002101 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 if (curwin != old_curwin && curwin->w_p_scb)
2103 {
2104 /*
2105 * do the vertical scroll
2106 */
2107 if (want_ver)
2108 {
2109#ifdef FEAT_DIFF
2110 if (old_curwin->w_p_diff && curwin->w_p_diff)
2111 {
2112 diff_set_topline(old_curwin, curwin);
2113 }
2114 else
2115#endif
2116 {
2117 curwin->w_scbind_pos += topline_diff;
2118 topline = curwin->w_scbind_pos;
2119 if (topline > curbuf->b_ml.ml_line_count)
2120 topline = curbuf->b_ml.ml_line_count;
2121 if (topline < 1)
2122 topline = 1;
2123
2124 y = topline - curwin->w_topline;
2125 if (y > 0)
2126 scrollup(y, FALSE);
2127 else
2128 scrolldown(-y, FALSE);
2129 }
2130
2131 redraw_later(VALID);
2132 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135
2136 /*
2137 * do the horizontal scroll
2138 */
2139 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2140 {
2141 curwin->w_leftcol = tgt_leftcol;
2142 leftcol_changed();
2143 }
2144 }
2145 }
2146
2147 /*
2148 * reset current-window
2149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 VIsual_select = old_VIsual_select;
2151 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 curwin = old_curwin;
2153 curbuf = old_curbuf;
2154}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156/*
2157 * Command character that's ignored.
2158 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002159 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002162nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002164 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165}
2166
2167/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002168 * Command character that doesn't do anything, but unlike nv_ignore() does
2169 * start edit(). Used for "startinsert" executed while starting up.
2170 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002171 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002172nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002173{
2174}
2175
2176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 * Command character doesn't exist.
2178 */
2179 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002180nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182 clearopbeep(cap->oap);
2183}
2184
2185/*
2186 * <Help> and <F1> commands.
2187 */
2188 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002189nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190{
2191 if (!checkclearopq(cap->oap))
2192 ex_help(NULL);
2193}
2194
2195/*
2196 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2197 */
2198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002199nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002201#ifdef FEAT_JOB_CHANNEL
2202 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2203 clearopbeep(cap->oap);
2204 else
2205#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002206 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002207 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002208 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002209 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2210 op_addsub(cap->oap, cap->count1, cap->arg);
2211 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002212 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002213 else if (VIsual_active)
2214 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002215 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002216 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217}
2218
2219/*
2220 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2221 */
2222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002223nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224{
2225 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002226 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002227 if (mod_mask & MOD_MASK_CTRL)
2228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002229 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002230 if (cap->arg == BACKWARD)
2231 goto_tabpage(-(int)cap->count1);
2232 else
2233 goto_tabpage((int)cap->count0);
2234 }
2235 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002236 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238}
2239
2240/*
2241 * Implementation of "gd" and "gD" command.
2242 */
2243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002244nv_gd(
2245 oparg_T *oap,
2246 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002247 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
2249 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 char_u *ptr;
2251
Bram Moolenaard9d30582005-05-18 22:10:28 +00002252 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002253 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2254 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002256#ifdef FEAT_FOLDING
2257 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2258 foldOpenCursor();
2259#endif
2260}
2261
2262/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002263 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2264 * otherwise.
2265 */
2266 static int
2267is_ident(char_u *line, int offset)
2268{
2269 int i;
2270 int incomment = FALSE;
2271 int instring = 0;
2272 int prev = 0;
2273
2274 for (i = 0; i < offset && line[i] != NUL; i++)
2275 {
2276 if (instring != 0)
2277 {
2278 if (prev != '\\' && line[i] == instring)
2279 instring = 0;
2280 }
2281 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2282 {
2283 instring = line[i];
2284 }
2285 else
2286 {
2287 if (incomment)
2288 {
2289 if (prev == '*' && line[i] == '/')
2290 incomment = FALSE;
2291 }
2292 else if (prev == '/' && line[i] == '*')
2293 {
2294 incomment = TRUE;
2295 }
2296 else if (prev == '/' && line[i] == '/')
2297 {
2298 return FALSE;
2299 }
2300 }
2301
2302 prev = line[i];
2303 }
2304
2305 return incomment == FALSE && instring == 0;
2306}
2307
2308/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002309 * Search for variable declaration of "ptr[len]".
2310 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2311 * current file ("gD").
2312 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002313 * Return FAIL when not found.
2314 */
2315 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002316find_decl(
2317 char_u *ptr,
2318 int len,
2319 int locally,
2320 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002321 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002322{
2323 char_u *pat;
2324 pos_T old_pos;
2325 pos_T par_pos;
2326 pos_T found_pos;
2327 int t;
2328 int save_p_ws;
2329 int save_p_scs;
2330 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002331 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002332 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002333 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002334
2335 if ((pat = alloc(len + 7)) == NULL)
2336 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002337
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002338 // Put "\V" before the pattern to avoid that the special meaning of "."
2339 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002340 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2341 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 old_pos = curwin->w_cursor;
2343 save_p_ws = p_ws;
2344 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002345 p_ws = FALSE; // don't wrap around end of file now
2346 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348 /*
2349 * With "gD" go to line 1.
2350 * With "gd" Search back for the start of the current function, then go
2351 * back until a blank line. If this fails go to line 1.
2352 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002353 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002355 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002357 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359 else
2360 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002361 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2363 --curwin->w_cursor.lnum;
2364 }
2365 curwin->w_cursor.col = 0;
2366
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002367 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002368 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002369 for (;;)
2370 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002371 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002372 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002373 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002374 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002375
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002376 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002377 {
2378 pos_T *pos;
2379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002380 // Check that the block the match is in doesn't end before the
2381 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002382 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2383 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2384 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002385 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002386 // There can't be a useful match before the end of this block.
2387 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002388 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002389 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002390 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002391 }
2392
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002393 if (t == FAIL)
2394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002396 if (found_pos.lnum != 0)
2397 {
2398 curwin->w_cursor = found_pos;
2399 t = OK;
2400 }
2401 break;
2402 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002403 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002404 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002405 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002406 ++curwin->w_cursor.lnum;
2407 curwin->w_cursor.col = 0;
2408 continue;
2409 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002410 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2411
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002412 // If the current position is not a valid identifier and a previous
2413 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002414 if (!valid && found_pos.lnum != 0)
2415 {
2416 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002417 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002418 }
2419
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002420 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002421 if (valid && !locally)
2422 break;
2423 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002424 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002425 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002426 if (found_pos.lnum != 0)
2427 curwin->w_cursor = found_pos;
2428 break;
2429 }
2430
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002431 // For finding a local variable and the match is before the "{" or
2432 // inside a comment, continue searching. For K&R style function
2433 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002434 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002435 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002436 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002437 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002438 // Remove SEARCH_START from flags to avoid getting stuck at one
2439 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002440 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002442
2443 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002445 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 curwin->w_cursor = old_pos;
2447 }
2448 else
2449 {
2450 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002451 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 reset_search_dir();
2453 }
2454
2455 vim_free(pat);
2456 p_ws = save_p_ws;
2457 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002458
2459 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460}
2461
2462/*
2463 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2464 * lines rather than lines in the file.
2465 * 'dist' must be positive.
2466 *
2467 * Return OK if able to move cursor, FAIL otherwise.
2468 */
2469 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002470nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 int linelen = linetabsize(ml_get_curline());
2473 int retval = OK;
2474 int atend = FALSE;
2475 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002476 int col_off1; // margin offset for first screen line
2477 int col_off2; // margin offset for wrapped screen line
2478 int width1; // text width for first screen line
2479 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
2481 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002482 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484 col_off1 = curwin_col_off();
2485 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002486 width1 = curwin->w_width - col_off1;
2487 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002488 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002489 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 /*
2494 * Instead of sticking at the last character of the buffer line we
2495 * try to stick in the last column of the screen.
2496 */
2497 if (curwin->w_curswant == MAXCOL)
2498 {
2499 atend = TRUE;
2500 validate_virtcol();
2501 if (width1 <= 0)
2502 curwin->w_curswant = 0;
2503 else
2504 {
2505 curwin->w_curswant = width1 - 1;
2506 if (curwin->w_virtcol > curwin->w_curswant)
2507 curwin->w_curswant += ((curwin->w_virtcol
2508 - curwin->w_curswant - 1) / width2 + 1) * width2;
2509 }
2510 }
2511 else
2512 {
2513 if (linelen > width1)
2514 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2515 else
2516 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002517 if (curwin->w_curswant >= (colnr_T)n)
2518 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520
2521 while (dist--)
2522 {
2523 if (dir == BACKWARD)
2524 {
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002525 if ((long)curwin->w_curswant >= width1)
2526 // Move back within the line. This can give a negative value
2527 // for w_curswant if width1 < width2 (with cpoptions+=n),
2528 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 curwin->w_curswant -= width2;
2530 else
2531 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002532 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (curwin->w_cursor.lnum == 1)
2534 {
2535 retval = FAIL;
2536 break;
2537 }
2538 --curwin->w_cursor.lnum;
2539#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002540 // Move to the start of a closed fold. Don't do that when
2541 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (!(fdo_flags & FDO_ALL))
2543 (void)hasFolding(curwin->w_cursor.lnum,
2544 &curwin->w_cursor.lnum, NULL);
2545#endif
2546 linelen = linetabsize(ml_get_curline());
2547 if (linelen > width1)
2548 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2549 + 1) * width2;
2550 }
2551 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002552 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
2554 if (linelen > width1)
2555 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2556 else
2557 n = width1;
2558 if (curwin->w_curswant + width2 < (colnr_T)n)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002559 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 curwin->w_curswant += width2;
2561 else
2562 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002563 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002565 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2567 &curwin->w_cursor.lnum);
2568#endif
2569 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2570 {
2571 retval = FAIL;
2572 break;
2573 }
2574 curwin->w_cursor.lnum++;
2575 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002576 // Check if the cursor has moved below the number display
2577 // when width1 < width2 (with cpoptions+=n). Subtract width2
2578 // to get a negative value for w_curswant, which will get
2579 // clipped to column 0.
2580 if (curwin->w_curswant >= width1)
2581 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002582 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584 }
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002588 if (virtual_active() && atend)
2589 coladvance(MAXCOL);
2590 else
2591 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2594 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002595 colnr_T virtcol;
2596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 /*
2598 * Check for landing on a character that got split at the end of the
2599 * last line. We want to advance a screenline, not end up in the same
2600 * screenline or move two screenlines.
2601 */
2602 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002603 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002604#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002605 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2606 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002607#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002608
2609 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 && (curwin->w_curswant < (colnr_T)width1
2611 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2612 : ((curwin->w_curswant - width1) % width2
2613 > (colnr_T)width2 / 2)))
2614 --curwin->w_cursor.col;
2615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616
2617 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002618 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620 return retval;
2621}
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623/*
2624 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2625 * cap->arg must be TRUE for CTRL-E.
2626 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002627 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002628nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 if (!checkclearop(cap->oap))
2631 scroll_redraw(cap->arg, cap->count1);
2632}
2633
2634/*
2635 * Scroll "count" lines up or down, and redraw.
2636 */
2637 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002638scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639{
2640 linenr_T prev_topline = curwin->w_topline;
2641#ifdef FEAT_DIFF
2642 int prev_topfill = curwin->w_topfill;
2643#endif
2644 linenr_T prev_lnum = curwin->w_cursor.lnum;
2645
2646 if (up)
2647 scrollup(count, TRUE);
2648 else
2649 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002650 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002652 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2653 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 cursor_correct();
2655 check_cursor_moved(curwin);
2656 curwin->w_valid |= VALID_TOPLINE;
2657
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002658 // If moved back to where we were, at least move the cursor, otherwise
2659 // we get stuck at one position. Don't move the cursor up if the
2660 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 while (curwin->w_topline == prev_topline
2662#ifdef FEAT_DIFF
2663 && curwin->w_topfill == prev_topfill
2664#endif
2665 )
2666 {
2667 if (up)
2668 {
2669 if (curwin->w_cursor.lnum > prev_lnum
2670 || cursor_down(1L, FALSE) == FAIL)
2671 break;
2672 }
2673 else
2674 {
2675 if (curwin->w_cursor.lnum < prev_lnum
2676 || prev_topline == 1L
2677 || cursor_up(1L, FALSE) == FAIL)
2678 break;
2679 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002680 // Mark w_topline as valid, otherwise the screen jumps back at the
2681 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 check_cursor_moved(curwin);
2683 curwin->w_valid |= VALID_TOPLINE;
2684 }
2685 }
2686 if (curwin->w_cursor.lnum != prev_lnum)
2687 coladvance(curwin->w_curswant);
2688 redraw_later(VALID);
2689}
2690
2691/*
2692 * Commands that start with "z".
2693 */
2694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002695nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
2697 long n;
2698 colnr_T col;
2699 int nchar = cap->nchar;
2700#ifdef FEAT_FOLDING
2701 long old_fdl = curwin->w_p_fdl;
2702 int old_fen = curwin->w_p_fen;
2703#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002704#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002705 int undo = FALSE;
2706#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002707 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
2709 if (VIM_ISDIGIT(nchar))
2710 {
2711 /*
2712 * "z123{nchar}": edit the count before obtaining {nchar}
2713 */
2714 if (checkclearop(cap->oap))
2715 return;
2716 n = nchar - '0';
2717 for (;;)
2718 {
2719#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002720 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721#endif
2722 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002723 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002724 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 --no_mapping;
2727 --allow_keys;
2728#ifdef FEAT_CMDL_INFO
2729 (void)add_to_showcmd(nchar);
2730#endif
2731 if (nchar == K_DEL || nchar == K_KDEL)
2732 n /= 10;
2733 else if (VIM_ISDIGIT(nchar))
2734 n = n * 10 + (nchar - '0');
2735 else if (nchar == CAR)
2736 {
2737#ifdef FEAT_GUI
2738 need_mouse_correct = TRUE;
2739#endif
2740 win_setheight((int)n);
2741 break;
2742 }
2743 else if (nchar == 'l'
2744 || nchar == 'h'
2745 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002746 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
2748 cap->count1 = n ? n * cap->count1 : cap->count1;
2749 goto dozet;
2750 }
2751 else
2752 {
2753 clearopbeep(cap->oap);
2754 break;
2755 }
2756 }
2757 cap->oap->op_type = OP_NOP;
2758 return;
2759 }
2760
2761dozet:
2762 if (
2763#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2765 // and "zC" only in Visual mode. "zj" and "zk" are motion
2766 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 cap->nchar != 'f' && cap->nchar != 'F'
2768 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2769 && cap->nchar != 'j' && cap->nchar != 'k'
2770 &&
2771#endif
2772 checkclearop(cap->oap))
2773 return;
2774
2775 /*
2776 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2777 * If line number given, set cursor.
2778 */
2779 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2780 && cap->count0
2781 && cap->count0 != curwin->w_cursor.lnum)
2782 {
2783 setpcmark();
2784 if (cap->count0 > curbuf->b_ml.ml_line_count)
2785 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2786 else
2787 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002788 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790
2791 switch (nchar)
2792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 case '+':
2795 if (cap->count0 == 0)
2796 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // No count given: put cursor at the line below screen
2798 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2800 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2801 else
2802 curwin->w_cursor.lnum = curwin->w_botline;
2803 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 case NL:
2806 case CAR:
2807 case K_KENTER:
2808 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002809 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810
2811 case 't': scroll_cursor_top(0, TRUE);
2812 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002813 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 break;
2815
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002816 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002818 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
2820 case 'z': scroll_cursor_halfway(TRUE);
2821 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002822 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 break;
2824
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002825 // "z^", "z-" and "zb": put cursor at bottom of screen
2826 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2827 // when <count> is at bottom of window, and puts that one at
2828 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 if (cap->count0 != 0)
2830 {
2831 scroll_cursor_bot(0, TRUE);
2832 curwin->w_cursor.lnum = curwin->w_topline;
2833 }
2834 else if (curwin->w_topline == 1)
2835 curwin->w_cursor.lnum = 1;
2836 else
2837 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002838 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 case '-':
2840 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002841 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842
2843 case 'b': scroll_cursor_bot(0, TRUE);
2844 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002845 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 break;
2847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002848 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002850 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002851 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 case 'h':
2855 case K_LEFT:
2856 if (!curwin->w_p_wrap)
2857 {
2858 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2859 curwin->w_leftcol = 0;
2860 else
2861 curwin->w_leftcol -= (colnr_T)cap->count1;
2862 leftcol_changed();
2863 }
2864 break;
2865
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002866 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002867 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002870 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 case 'l':
2872 case K_RIGHT:
2873 if (!curwin->w_p_wrap)
2874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002875 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 curwin->w_leftcol += (colnr_T)cap->count1;
2877 leftcol_changed();
2878 }
2879 break;
2880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 case 's': if (!curwin->w_p_wrap)
2883 {
2884#ifdef FEAT_FOLDING
2885 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002886 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 else
2888#endif
2889 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002890 if ((long)col > siso)
2891 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 else
2893 col = 0;
2894 if (curwin->w_leftcol != col)
2895 {
2896 curwin->w_leftcol = col;
2897 redraw_later(NOT_VALID);
2898 }
2899 }
2900 break;
2901
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 case 'e': if (!curwin->w_p_wrap)
2904 {
2905#ifdef FEAT_FOLDING
2906 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 else
2909#endif
2910 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002911 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002912 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 col = 0;
2914 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002915 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 if (curwin->w_leftcol != col)
2917 {
2918 curwin->w_leftcol = col;
2919 redraw_later(NOT_VALID);
2920 }
2921 }
2922 break;
2923
2924#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002925 // "zF": create fold command
2926 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 case 'F':
2928 case 'f': if (foldManualAllowed(TRUE))
2929 {
2930 cap->nchar = 'f';
2931 nv_operator(cap);
2932 curwin->w_p_fen = TRUE;
2933
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2936 {
2937 nv_operator(cap);
2938 finish_op = TRUE;
2939 }
2940 }
2941 else
2942 clearopbeep(cap->oap);
2943 break;
2944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 // "zd": delete fold at cursor
2946 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 case 'd':
2948 case 'D': if (foldManualAllowed(FALSE))
2949 {
2950 if (VIsual_active)
2951 nv_operator(cap);
2952 else
2953 deleteFold(curwin->w_cursor.lnum,
2954 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2955 }
2956 break;
2957
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 case 'E': if (foldmethodIsManual(curwin))
2960 {
2961 clearFolding(curwin);
2962 changed_window_setting();
2963 }
2964 else if (foldmethodIsMarker(curwin))
2965 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2966 TRUE, FALSE);
2967 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002968 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 break;
2970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002971 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 case 'n': curwin->w_p_fen = FALSE;
2973 break;
2974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002975 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 case 'N': curwin->w_p_fen = TRUE;
2977 break;
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2981 break;
2982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2985 openFold(curwin->w_cursor.lnum, cap->count1);
2986 else
2987 {
2988 closeFold(curwin->w_cursor.lnum, cap->count1);
2989 curwin->w_p_fen = TRUE;
2990 }
2991 break;
2992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002993 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2995 openFoldRecurse(curwin->w_cursor.lnum);
2996 else
2997 {
2998 closeFoldRecurse(curwin->w_cursor.lnum);
2999 curwin->w_p_fen = TRUE;
3000 }
3001 break;
3002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 case 'o': if (VIsual_active)
3005 nv_operator(cap);
3006 else
3007 openFold(curwin->w_cursor.lnum, cap->count1);
3008 break;
3009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 case 'O': if (VIsual_active)
3012 nv_operator(cap);
3013 else
3014 openFoldRecurse(curwin->w_cursor.lnum);
3015 break;
3016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 case 'c': if (VIsual_active)
3019 nv_operator(cap);
3020 else
3021 closeFold(curwin->w_cursor.lnum, cap->count1);
3022 curwin->w_p_fen = TRUE;
3023 break;
3024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 case 'C': if (VIsual_active)
3027 nv_operator(cap);
3028 else
3029 closeFoldRecurse(curwin->w_cursor.lnum);
3030 curwin->w_p_fen = TRUE;
3031 break;
3032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 case 'v': foldOpenCursor();
3035 break;
3036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003037 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003039 curwin->w_foldinvalid = TRUE; // recompute folds
3040 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 foldOpenCursor();
3042 break;
3043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003046 curwin->w_foldinvalid = TRUE; // recompute folds
3047 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 break;
3049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003050 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003052 {
3053 curwin->w_p_fdl -= cap->count1;
3054 if (curwin->w_p_fdl < 0)
3055 curwin->w_p_fdl = 0;
3056 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 curwin->w_p_fen = TRUE;
3059 break;
3060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003061 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003063 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 curwin->w_p_fen = TRUE;
3065 break;
3066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003067 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003068 case 'r': curwin->w_p_fdl += cap->count1;
3069 {
3070 int d = getDeepestNesting();
3071
3072 if (curwin->w_p_fdl >= d)
3073 curwin->w_p_fdl = d;
3074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 break;
3076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003077 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003079 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 break;
3081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 case 'j': // "zj" move to next fold downwards
3083 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3085 cap->count1) == FAIL)
3086 clearopbeep(cap->oap);
3087 break;
3088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003089#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003091#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003092 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003093 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003095 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003096 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003097 --no_mapping;
3098 --allow_keys;
3099#ifdef FEAT_CMDL_INFO
3100 (void)add_to_showcmd(nchar);
3101#endif
3102 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3103 {
3104 clearopbeep(cap->oap);
3105 break;
3106 }
3107 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003110 case 'g': // "zg": add good word to word list
3111 case 'w': // "zw": add wrong word to word list
3112 case 'G': // "zG": add good word to temp word list
3113 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003114 {
3115 char_u *ptr = NULL;
3116 int len;
3117
3118 if (checkclearop(cap->oap))
3119 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003120 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3121 == FAIL)
3122 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003123 if (ptr == NULL)
3124 {
3125 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003126
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // Find bad word under the cursor. When 'spell' is
3128 // off this fails and find_ident_under_cursor() is
3129 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003130 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003131 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003132 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003133 if (len != 0 && curwin->w_cursor.col <= pos.col)
3134 ptr = ml_get_pos(&curwin->w_cursor);
3135 curwin->w_cursor = pos;
3136 }
3137
Bram Moolenaarb765d632005-06-07 21:00:02 +00003138 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3139 FIND_IDENT)) == 0)
3140 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003141 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3142 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003143 (nchar == 'G' || nchar == 'W')
3144 ? 0 : (int)cap->count1,
3145 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003146 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003147 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003150 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003151 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003152 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003153#endif
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 default: clearopbeep(cap->oap);
3156 }
3157
3158#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003159 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (old_fen != curwin->w_p_fen)
3161 {
3162# ifdef FEAT_DIFF
3163 win_T *wp;
3164
3165 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3166 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 FOR_ALL_WINDOWS(wp)
3169 {
3170 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3171 {
3172 wp->w_p_fen = curwin->w_p_fen;
3173 changed_window_setting_win(wp);
3174 }
3175 }
3176 }
3177# endif
3178 changed_window_setting();
3179 }
3180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003181 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (old_fdl != curwin->w_p_fdl)
3183 newFoldLevel();
3184#endif
3185}
3186
3187#ifdef FEAT_GUI
3188/*
3189 * Vertical scrollbar movement.
3190 */
3191 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003192nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
3194 if (cap->oap->op_type != OP_NOP)
3195 clearopbeep(cap->oap);
3196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 gui_do_scroll();
3199}
3200
3201/*
3202 * Horizontal scrollbar movement.
3203 */
3204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003205nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
3207 if (cap->oap->op_type != OP_NOP)
3208 clearopbeep(cap->oap);
3209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003210 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003211 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212}
3213#endif
3214
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003215#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003216/*
3217 * Click in GUI tab.
3218 */
3219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003220nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003221{
3222 if (cap->oap->op_type != OP_NOP)
3223 clearopbeep(cap->oap);
3224
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003225 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003226 goto_tabpage(current_tab);
3227}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003228
3229/*
3230 * Selected item in tab line menu.
3231 */
3232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003233nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +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 jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003239 handle_tabmenu();
3240}
3241
3242/*
3243 * Handle selecting an item of the GUI tab line menu.
3244 * Used in Normal and Insert mode.
3245 */
3246 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003247handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003248{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003249 switch (current_tabmenu)
3250 {
3251 case TABLINE_MENU_CLOSE:
3252 if (current_tab == 0)
3253 do_cmdline_cmd((char_u *)"tabclose");
3254 else
3255 {
3256 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3257 current_tab);
3258 do_cmdline_cmd(IObuff);
3259 }
3260 break;
3261
3262 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003263 if (current_tab == 0)
3264 do_cmdline_cmd((char_u *)"$tabnew");
3265 else
3266 {
3267 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3268 current_tab - 1);
3269 do_cmdline_cmd(IObuff);
3270 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003271 break;
3272
3273 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003274 if (current_tab == 0)
3275 do_cmdline_cmd((char_u *)"browse $tabnew");
3276 else
3277 {
3278 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3279 current_tab - 1);
3280 do_cmdline_cmd(IObuff);
3281 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003282 break;
3283 }
3284}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003285#endif
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287/*
3288 * "Q" command.
3289 */
3290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003291nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 /*
3294 * Ignore 'Q' in Visual mode, just give a beep.
3295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003297 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003298 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 do_exmode(FALSE);
3300}
3301
3302/*
3303 * Handle a ":" command.
3304 */
3305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003306nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003309 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (VIsual_active)
3312 nv_operator(cap);
3313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
3315 if (cap->oap->op_type != OP_NOP)
3316 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003317 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 cap->oap->motion_type = MCHAR;
3319 cap->oap->inclusive = FALSE;
3320 }
3321 else if (cap->count0)
3322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003323 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 stuffcharReadbuff('.');
3325 if (cap->count0 > 1)
3326 {
3327 stuffReadbuff((char_u *)",.+");
3328 stuffnumReadbuff((long)cap->count0 - 1L);
3329 }
3330 }
3331
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003332 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (KeyTyped)
3334 compute_cmdrow();
3335
3336 old_p_im = p_im;
3337
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003338 // get a command line and execute it
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003339 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3341
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003342 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 if (p_im != old_p_im)
3344 {
3345 if (p_im)
3346 restart_edit = 'i';
3347 else
3348 restart_edit = 0;
3349 }
3350
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003351 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003352 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003353 clearop(cap->oap);
3354 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3356 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003357 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3358 || did_emsg
3359 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003360 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 clearopbeep(cap->oap);
3362 }
3363}
3364
3365/*
3366 * Handle CTRL-G command.
3367 */
3368 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003369nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003371 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
3373 VIsual_select = !VIsual_select;
3374 showmode();
3375 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003376 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003377 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 fileinfo((int)cap->count0, FALSE, TRUE);
3379}
3380
3381/*
3382 * Handle CTRL-H <Backspace> command.
3383 */
3384 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003385nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 if (VIsual_active && VIsual_select)
3388 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003389 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 v_visop(cap);
3391 }
3392 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 nv_left(cap);
3394}
3395
3396/*
3397 * CTRL-L: clear screen and redraw.
3398 */
3399 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003400nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 if (!checkclearop(cap->oap))
3403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003405 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003406 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003407# ifdef FEAT_RELTIME
3408 {
3409 win_T *wp;
3410
3411 FOR_ALL_WINDOWS(wp)
3412 wp->w_s->b_syn_slow = FALSE;
3413 }
3414# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415#endif
3416 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003417#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3418# ifdef VIMDLL
3419 if (!gui.in_use)
3420# endif
3421 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
3424}
3425
3426/*
3427 * CTRL-O: In Select mode: switch to Visual mode for one command.
3428 * Otherwise: Go to older pcmark.
3429 */
3430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003431nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (VIsual_active && VIsual_select)
3434 {
3435 VIsual_select = FALSE;
3436 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003437 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
3441 cap->count1 = -cap->count1;
3442 nv_pcmark(cap);
3443 }
3444}
3445
3446/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003447 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3448 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 */
3450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003451nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452{
3453 if (!checkclearopq(cap->oap))
3454 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3455 GETF_SETMARK|GETF_ALT, FALSE);
3456}
3457
3458/*
3459 * "Z" commands.
3460 */
3461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003462nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
3464 if (!checkclearopq(cap->oap))
3465 {
3466 switch (cap->nchar)
3467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 case 'Z': do_cmdline_cmd((char_u *)"x");
3470 break;
3471
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003472 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 case 'Q': do_cmdline_cmd((char_u *)"q!");
3474 break;
3475
3476 default: clearopbeep(cap->oap);
3477 }
3478 }
3479}
3480
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481/*
3482 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3483 */
3484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003485do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487 oparg_T oa;
3488 cmdarg_T ca;
3489
3490 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003491 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 ca.oap = &oa;
3493 ca.cmdchar = c1;
3494 ca.nchar = c2;
3495 nv_ident(&ca);
3496}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498/*
3499 * Handle the commands that use the word under the cursor.
3500 * [g] CTRL-] :ta to current identifier
3501 * [g] 'K' run program for current identifier
3502 * [g] '*' / to current identifier or string
3503 * [g] '#' ? to current identifier or string
3504 * g ']' :tselect for current identifier
3505 */
3506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003507nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
3509 char_u *ptr = NULL;
3510 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003511 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003512 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003514 char_u *kp; // value of 'keywordprg'
3515 int kp_help; // 'keywordprg' is ":he"
3516 int kp_ex; // 'keywordprg' starts with ":"
3517 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003519 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003520 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 char_u *aux_ptr;
3522 int isman;
3523 int isman_s;
3524
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003525 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 cmdchar = cap->nchar;
3528 g_cmd = TRUE;
3529 }
3530 else
3531 {
3532 cmdchar = cap->cmdchar;
3533 g_cmd = FALSE;
3534 }
3535
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003536 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 cmdchar = '#';
3538
3539 /*
3540 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3541 */
3542 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3545 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (checkclearopq(cap->oap))
3547 return;
3548 }
3549
3550 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3551 (cmdchar == '*' || cmdchar == '#')
3552 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3553 {
3554 clearop(cap->oap);
3555 return;
3556 }
3557
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003558 // Allocate buffer to put the command in. Inserting backslashes can
3559 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3560 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3562 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3563 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003564 if (kp_help && *skipwhite(ptr) == NUL)
3565 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003566 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003567 return;
3568 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003569 kp_ex = (*kp == ':');
3570 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3571 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (buf == NULL)
3573 return;
3574 buf[0] = NUL;
3575
3576 switch (cmdchar)
3577 {
3578 case '*':
3579 case '#':
3580 /*
3581 * Put cursor at start of word, makes search skip the word
3582 * under the cursor.
3583 * Call setpcmark() first, so "*``" puts the cursor back where
3584 * it was.
3585 */
3586 setpcmark();
3587 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3588
3589 if (!g_cmd && vim_iswordp(ptr))
3590 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003591 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 break;
3593
3594 case 'K':
3595 if (kp_help)
3596 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003597 else if (kp_ex)
3598 {
3599 if (cap->count0 != 0)
3600 vim_snprintf((char *)buf, buflen, "%s %ld",
3601 kp, cap->count0);
3602 else
3603 STRCPY(buf, kp);
3604 STRCAT(buf, " ");
3605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 else
3607 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003608 // An external command will probably use an argument starting
3609 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003610 while (*ptr == '-' && n > 0)
3611 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003612 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003613 --n;
3614 }
3615 if (n == 0)
3616 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003617 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003618 vim_free(buf);
3619 return;
3620 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003621
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003622 // When a count is given, turn it into a range. Is this
3623 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 isman = (STRCMP(kp, "man") == 0);
3625 isman_s = (STRCMP(kp, "man -s") == 0);
3626 if (cap->count0 != 0 && !(isman || isman_s))
3627 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3628
3629 STRCAT(buf, "! ");
3630 if (cap->count0 == 0 && isman_s)
3631 STRCAT(buf, "man");
3632 else
3633 STRCAT(buf, kp);
3634 STRCAT(buf, " ");
3635 if (cap->count0 != 0 && (isman || isman_s))
3636 {
3637 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3638 STRCAT(buf, " ");
3639 }
3640 }
3641 break;
3642
3643 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003644 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645#ifdef FEAT_CSCOPE
3646 if (p_cst)
3647 STRCPY(buf, "cstag ");
3648 else
3649#endif
3650 STRCPY(buf, "ts ");
3651 break;
3652
3653 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003654 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (curbuf->b_help)
3656 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003658 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003659 if (g_cmd)
3660 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003661 else if (cap->count0 == 0)
3662 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003663 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003664 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667
3668 /*
3669 * Now grab the chars in the identifier
3670 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003671 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003673 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003674 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003675 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003676 p = vim_strsave_fnameescape(ptr, FALSE);
3677 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003678 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003679 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003680 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003681 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003683 vim_free(buf);
3684 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003686 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003687 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003688 {
3689 vim_free(buf);
3690 vim_free(p);
3691 return;
3692 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003693 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003694 STRCAT(buf, p);
3695 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003697 else
3698 {
3699 if (cmdchar == '*')
3700 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
3701 else if (cmdchar == '#')
3702 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003703 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003704 {
3705 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003706 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003707 aux_ptr = (char_u *)"";
3708 else
3709 aux_ptr = (char_u *)"\\|\"\n[";
3710 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003711 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003712 aux_ptr = (char_u *)"\\|\"\n*?[";
3713
3714 p = buf + STRLEN(buf);
3715 while (n-- > 0)
3716 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003717 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003718 if (vim_strchr(aux_ptr, *ptr) != NULL)
3719 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003720 // When current byte is a part of multibyte character, copy all
3721 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003722 if (has_mbyte)
3723 {
3724 int i;
3725 int len = (*mb_ptr2len)(ptr) - 1;
3726
3727 for (i = 0; i < len && n >= 1; ++i, --n)
3728 *p++ = *ptr++;
3729 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003730 *p++ = *ptr++;
3731 }
3732 *p = NUL;
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735 /*
3736 * Execute the command.
3737 */
3738 if (cmdchar == '*' || cmdchar == '#')
3739 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003740 if (!g_cmd && (has_mbyte
3741 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3742 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003744
3745 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003746 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003748
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003749 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003752 {
3753 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003755 g_tag_at_cursor = FALSE;
3756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
3758 vim_free(buf);
3759}
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761/*
3762 * Get visually selected text, within one line only.
3763 * Returns FAIL if more than one line selected.
3764 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003765 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003766get_visual_text(
3767 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003768 char_u **pp, // return: start of selected text
3769 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 if (VIsual_mode != 'V')
3772 unadjust_for_sel();
3773 if (VIsual.lnum != curwin->w_cursor.lnum)
3774 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003775 if (cap != NULL)
3776 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 return FAIL;
3778 }
3779 if (VIsual_mode == 'V')
3780 {
3781 *pp = ml_get_curline();
3782 *lenp = (int)STRLEN(*pp);
3783 }
3784 else
3785 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003786 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
3788 *pp = ml_get_pos(&curwin->w_cursor);
3789 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3790 }
3791 else
3792 {
3793 *pp = ml_get_pos(&VIsual);
3794 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003797 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003798 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800 reset_VIsual_and_resel();
3801 return OK;
3802}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
3804/*
3805 * CTRL-T: backwards in tag stack
3806 */
3807 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003808nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809{
3810 if (!checkclearopq(cap->oap))
3811 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3812}
3813
3814/*
3815 * Handle scrolling command 'H', 'L' and 'M'.
3816 */
3817 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003818nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 int used = 0;
3821 long n;
3822#ifdef FEAT_FOLDING
3823 linenr_T lnum;
3824#endif
3825 int half;
3826
3827 cap->oap->motion_type = MLINE;
3828 setpcmark();
3829
3830 if (cap->cmdchar == 'L')
3831 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003832 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 curwin->w_cursor.lnum = curwin->w_botline - 1;
3834 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3835 curwin->w_cursor.lnum = 1;
3836 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003837 {
3838#ifdef FEAT_FOLDING
3839 if (hasAnyFolding(curwin))
3840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003841 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003842 for (n = cap->count1 - 1; n > 0
3843 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3844 {
3845 (void)hasFolding(curwin->w_cursor.lnum,
3846 &curwin->w_cursor.lnum, NULL);
3847 --curwin->w_cursor.lnum;
3848 }
3849 }
3850 else
3851#endif
3852 curwin->w_cursor.lnum -= cap->count1 - 1;
3853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 else
3856 {
3857 if (cap->cmdchar == 'M')
3858 {
3859#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003860 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 used -= diff_check_fill(curwin, curwin->w_topline)
3862 - curwin->w_topfill;
3863#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003864 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3866 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3867 {
3868#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003869 // Count half he number of filler lines to be "below this
3870 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3872 + n) / 2 >= half)
3873 {
3874 --n;
3875 break;
3876 }
3877#endif
3878 used += plines(curwin->w_topline + n);
3879 if (used >= half)
3880 break;
3881#ifdef FEAT_FOLDING
3882 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3883 n = lnum - curwin->w_topline;
3884#endif
3885 }
3886 if (n > 0 && used > curwin->w_height)
3887 --n;
3888 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003889 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003892#ifdef FEAT_FOLDING
3893 if (hasAnyFolding(curwin))
3894 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003895 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003896 lnum = curwin->w_topline;
3897 while (n-- > 0 && lnum < curwin->w_botline - 1)
3898 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003899 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003900 ++lnum;
3901 }
3902 n = lnum - curwin->w_topline;
3903 }
3904#endif
3905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 curwin->w_cursor.lnum = curwin->w_topline + n;
3907 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3908 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3909 }
3910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003911 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003912 if (cap->oap->op_type == OP_NOP)
3913 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 beginline(BL_SOL | BL_FIX);
3915}
3916
3917/*
3918 * Cursor right commands.
3919 */
3920 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003921nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
3923 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003924 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003926 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3927 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003928 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003929 if (mod_mask & MOD_MASK_CTRL)
3930 cap->arg = TRUE;
3931 nv_wordcmd(cap);
3932 return;
3933 }
3934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 cap->oap->motion_type = MCHAR;
3936 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003937 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003940 * In virtual edit mode, there's no such thing as "past_line", as lines
3941 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 */
3943 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003944 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
3946 for (n = cap->count1; n > 0; --n)
3947 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003948 if ((!past_line && oneright() == FAIL)
3949 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003950 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
3952 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003953 * <Space> wraps to next line if 'whichwrap' has 's'.
3954 * 'l' wraps to next line if 'whichwrap' has 'l'.
3955 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 */
3957 if ( ((cap->cmdchar == ' '
3958 && vim_strchr(p_ww, 's') != NULL)
3959 || (cap->cmdchar == 'l'
3960 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003961 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 && vim_strchr(p_ww, '>') != NULL))
3963 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3964 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003965 // When deleting we also count the NL as a character.
3966 // Set cap->oap->inclusive when last char in the line is
3967 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003968 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003970 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 cap->oap->inclusive = TRUE;
3972 else
3973 {
3974 ++curwin->w_cursor.lnum;
3975 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 curwin->w_set_curswant = TRUE;
3978 cap->oap->inclusive = FALSE;
3979 }
3980 continue;
3981 }
3982 if (cap->oap->op_type == OP_NOP)
3983 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003984 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (n == cap->count1)
3986 beep_flush();
3987 }
3988 else
3989 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003990 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 cap->oap->inclusive = TRUE;
3992 }
3993 break;
3994 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003995 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
3997 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (virtual_active())
3999 oneright();
4000 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004003 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 ++curwin->w_cursor.col;
4006 }
4007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009#ifdef FEAT_FOLDING
4010 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4011 && cap->oap->op_type == OP_NOP)
4012 foldOpenCursor();
4013#endif
4014}
4015
4016/*
4017 * Cursor left commands.
4018 *
4019 * Returns TRUE when operator end should not be adjusted.
4020 */
4021 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004022nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023{
4024 long n;
4025
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004026 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4027 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004028 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004029 if (mod_mask & MOD_MASK_CTRL)
4030 cap->arg = 1;
4031 nv_bck_word(cap);
4032 return;
4033 }
4034
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 cap->oap->motion_type = MCHAR;
4036 cap->oap->inclusive = FALSE;
4037 for (n = cap->count1; n > 0; --n)
4038 {
4039 if (oneleft() == FAIL)
4040 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004041 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4042 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4043 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if ( (((cap->cmdchar == K_BS
4045 || cap->cmdchar == Ctrl_H)
4046 && vim_strchr(p_ww, 'b') != NULL)
4047 || (cap->cmdchar == 'h'
4048 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004049 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 && vim_strchr(p_ww, '<') != NULL))
4051 && curwin->w_cursor.lnum > 1)
4052 {
4053 --(curwin->w_cursor.lnum);
4054 coladvance((colnr_T)MAXCOL);
4055 curwin->w_set_curswant = TRUE;
4056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004057 // When the NL before the first char has to be deleted we
4058 // put the cursor on the NUL after the previous line.
4059 // This is a very special case, be careful!
4060 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 if ( (cap->oap->op_type == OP_DELETE
4062 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004063 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004065 char_u *cp = ml_get_cursor();
4066
4067 if (*cp != NUL)
4068 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004069 if (has_mbyte)
4070 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4071 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004072 ++curwin->w_cursor.col;
4073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 cap->retval |= CA_NO_ADJ_OP_END;
4075 }
4076 continue;
4077 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004078 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4080 beep_flush();
4081 break;
4082 }
4083 }
4084#ifdef FEAT_FOLDING
4085 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4086 && cap->oap->op_type == OP_NOP)
4087 foldOpenCursor();
4088#endif
4089}
4090
4091/*
4092 * Cursor up commands.
4093 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4094 */
4095 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004096nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004098 if (mod_mask & MOD_MASK_SHIFT)
4099 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004100 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004101 cap->arg = BACKWARD;
4102 nv_page(cap);
4103 }
4104 else
4105 {
4106 cap->oap->motion_type = MLINE;
4107 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4108 clearopbeep(cap->oap);
4109 else if (cap->arg)
4110 beginline(BL_WHITE | BL_FIX);
4111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112}
4113
4114/*
4115 * Cursor down commands.
4116 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4117 */
4118 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004119nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004121 if (mod_mask & MOD_MASK_SHIFT)
4122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004123 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004124 cap->arg = FORWARD;
4125 nv_page(cap);
4126 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004127#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004128 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004129 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4130 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004132 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
4134#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004135 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004136 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 cmdwin_result = CAR;
4138 else
4139#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004141 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004142 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4143 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4144 {
4145 invoke_prompt_callback();
4146 if (restart_edit == 0)
4147 restart_edit = 'a';
4148 }
4149 else
4150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
4152 cap->oap->motion_type = MLINE;
4153 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4154 clearopbeep(cap->oap);
4155 else if (cap->arg)
4156 beginline(BL_WHITE | BL_FIX);
4157 }
4158 }
4159}
4160
4161#ifdef FEAT_SEARCHPATH
4162/*
4163 * Grab the file name under the cursor and edit it.
4164 */
4165 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004166nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
4168 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004169 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004171 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
4173 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004174 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return;
4176 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004177 if (curbuf_locked())
4178 {
4179 clearop(cap->oap);
4180 return;
4181 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004182#ifdef FEAT_PROP_POPUP
4183 if (ERROR_IF_TERM_POPUP_WINDOW)
4184 return;
4185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004187 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189 if (ptr != NULL)
4190 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004191 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004192 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004193 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004195 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004196 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004197 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004198 {
4199 curwin->w_cursor.lnum = lnum;
4200 check_cursor_lnum();
4201 beginline(BL_SOL | BL_FIX);
4202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 vim_free(ptr);
4204 }
4205 else
4206 clearop(cap->oap);
4207}
4208#endif
4209
4210/*
4211 * <End> command: to end of current line or last line.
4212 */
4213 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004214nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004216 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004218 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004220 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 nv_dollar(cap);
4223}
4224
4225/*
4226 * Handle the "$" command.
4227 */
4228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004229nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 cap->oap->motion_type = MCHAR;
4232 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004233 // In virtual mode when off the edge of a line and an operator
4234 // is pending (whew!) keep the cursor where it is.
4235 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 if (!virtual_active() || gchar_cursor() != NUL
4237 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004238 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 if (cursor_down((long)(cap->count1 - 1),
4240 cap->oap->op_type == OP_NOP) == FAIL)
4241 clearopbeep(cap->oap);
4242#ifdef FEAT_FOLDING
4243 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4244 foldOpenCursor();
4245#endif
4246}
4247
4248/*
4249 * Implementation of '?' and '/' commands.
4250 * If cap->arg is TRUE don't set PC mark.
4251 */
4252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004253nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
4255 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004256 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
4258 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4259 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004260 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 cap->cmdchar = 'g';
4262 cap->nchar = '?';
4263 nv_operator(cap);
4264 return;
4265 }
4266
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004267 // When using 'incsearch' the cursor may be moved to set a different search
4268 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004269 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
4271 if (cap->searchbuf == NULL)
4272 {
4273 clearop(oap);
4274 return;
4275 }
4276
Bram Moolenaar46539112015-02-17 15:43:57 +01004277 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004278 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004279 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280}
4281
4282/*
4283 * Handle "N" and "n" commands.
4284 * cap->arg is SEARCH_REV for "N", 0 for "n".
4285 */
4286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004287nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004289 pos_T old = curwin->w_cursor;
4290 int wrapped = FALSE;
4291 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004292
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004293 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004294 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004295 // Avoid getting stuck on the current cursor position, which can
4296 // happen when an offset is given and the cursor is on the last char
4297 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004298 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004299 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004300 cap->count1 -= 1;
4301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302}
4303
4304/*
4305 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4306 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004307 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004309 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004310normal_search(
4311 cmdarg_T *cap,
4312 int dir,
4313 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004314 int opt, // extra flags for do_search()
4315 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
4317 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004318 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 cap->oap->motion_type = MCHAR;
4321 cap->oap->inclusive = FALSE;
4322 cap->oap->use_reg_one = TRUE;
4323 curwin->w_set_curswant = TRUE;
4324
Bram Moolenaara80faa82020-04-12 19:37:17 +02004325 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004326 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004327 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4328 if (wrapped != NULL)
4329 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (i == 0)
4331 clearop(cap->oap);
4332 else
4333 {
4334 if (i == 2)
4335 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337#ifdef FEAT_FOLDING
4338 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4339 foldOpenCursor();
4340#endif
4341 }
4342
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004343 // "/$" will put the cursor after the end of the line, may need to
4344 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004346 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347}
4348
4349/*
4350 * Character search commands.
4351 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4352 * ',' and FALSE for ';'.
4353 * cap->nchar is NUL for ',' and ';' (repeat the search)
4354 */
4355 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004356nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
4358 int t_cmd;
4359
4360 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4361 t_cmd = TRUE;
4362 else
4363 t_cmd = FALSE;
4364
4365 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4367 clearopbeep(cap->oap);
4368 else
4369 {
4370 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004371 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4373 && (t_cmd || cap->oap->op_type != OP_NOP))
4374 {
4375 colnr_T scol, ecol;
4376
4377 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4378 curwin->w_cursor.coladd = ecol - scol;
4379 }
4380 else
4381 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#ifdef FEAT_FOLDING
4384 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4385 foldOpenCursor();
4386#endif
4387 }
4388}
4389
4390/*
4391 * "[" and "]" commands.
4392 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4393 */
4394 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004395nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004397 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004399 pos_T *pos = NULL; // init for GCC
4400 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 int flag;
4402 long n;
4403 int findc;
4404 int c;
4405
4406 cap->oap->motion_type = MCHAR;
4407 cap->oap->inclusive = FALSE;
4408 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004409 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
4411#ifdef FEAT_SEARCHPATH
4412 /*
4413 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4414 */
4415 if (cap->nchar == 'f')
4416 nv_gotofile(cap);
4417 else
4418#endif
4419
4420#ifdef FEAT_FIND_ID
4421 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004422 * Find the occurrence(s) of the identifier or define under cursor
4423 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 *
4425 * search list jump
4426 * fwd bwd fwd bwd fwd bwd
4427 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4428 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4429 */
4430 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004431# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004433# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004435# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 cap->nchar) != NULL)
4437 {
4438 char_u *ptr;
4439 int len;
4440
4441 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4442 clearop(cap->oap);
4443 else
4444 {
4445 find_pattern_in_path(ptr, 0, len, TRUE,
4446 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4447 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4448 cap->count1,
4449 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4450 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4451 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4452 (linenr_T)MAXLNUM);
4453 curwin->w_set_curswant = TRUE;
4454 }
4455 }
4456 else
4457#endif
4458
4459 /*
4460 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4461 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4462 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4463 * "[m" or "]m" search for prev/next start of (Java) method.
4464 * "[M" or "]M" search for prev/next end of (Java) method.
4465 */
4466 if ( (cap->cmdchar == '['
4467 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4468 || (cap->cmdchar == ']'
4469 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4470 {
4471 if (cap->nchar == '*')
4472 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 prev_pos.lnum = 0;
4474 if (cap->nchar == 'm' || cap->nchar == 'M')
4475 {
4476 if (cap->cmdchar == '[')
4477 findc = '{';
4478 else
4479 findc = '}';
4480 n = 9999;
4481 }
4482 else
4483 {
4484 findc = cap->nchar;
4485 n = cap->count1;
4486 }
4487 for ( ; n > 0; --n)
4488 {
4489 if ((pos = findmatchlimit(cap->oap, findc,
4490 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4491 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004492 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
4494 if (cap->nchar != 'm' && cap->nchar != 'M')
4495 clearopbeep(cap->oap);
4496 }
4497 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004498 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 break;
4500 }
4501 prev_pos = new_pos;
4502 curwin->w_cursor = *pos;
4503 new_pos = *pos;
4504 }
4505 curwin->w_cursor = old_pos;
4506
4507 /*
4508 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4509 * brought us to the match for "[m" and "]M" when inside a method.
4510 * Try finding the '{' or '}' we want to be at.
4511 * Also repeat for the given count.
4512 */
4513 if (cap->nchar == 'm' || cap->nchar == 'M')
4514 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004515 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 int norm = ((findc == '{') == (cap->nchar == 'm'));
4517
4518 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004519 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (prev_pos.lnum != 0)
4521 {
4522 pos = &prev_pos;
4523 curwin->w_cursor = prev_pos;
4524 if (norm)
4525 --n;
4526 }
4527 else
4528 pos = NULL;
4529 while (n > 0)
4530 {
4531 for (;;)
4532 {
4533 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4534 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004535 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 if (pos == NULL)
4537 clearopbeep(cap->oap);
4538 n = 0;
4539 break;
4540 }
4541 c = gchar_cursor();
4542 if (c == '{' || c == '}')
4543 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004544 // Must have found end/start of class: use it.
4545 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if ((c == findc && norm) || (n == 1 && !norm))
4547 {
4548 new_pos = curwin->w_cursor;
4549 pos = &new_pos;
4550 n = 0;
4551 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004552 // if no match found at all, we started outside of the
4553 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 else if (new_pos.lnum == 0)
4555 {
4556 new_pos = curwin->w_cursor;
4557 pos = &new_pos;
4558 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004559 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 else if ((pos = findmatchlimit(cap->oap, findc,
4561 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4562 0)) == NULL)
4563 n = 0;
4564 else
4565 curwin->w_cursor = *pos;
4566 break;
4567 }
4568 }
4569 --n;
4570 }
4571 curwin->w_cursor = old_pos;
4572 if (pos == NULL && new_pos.lnum != 0)
4573 clearopbeep(cap->oap);
4574 }
4575 if (pos != NULL)
4576 {
4577 setpcmark();
4578 curwin->w_cursor = *pos;
4579 curwin->w_set_curswant = TRUE;
4580#ifdef FEAT_FOLDING
4581 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4582 && cap->oap->op_type == OP_NOP)
4583 foldOpenCursor();
4584#endif
4585 }
4586 }
4587
4588 /*
4589 * "[[", "[]", "]]" and "][": move to start or end of function
4590 */
4591 else if (cap->nchar == '[' || cap->nchar == ']')
4592 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004593 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 flag = '{';
4595 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004596 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 curwin->w_set_curswant = TRUE;
4599 /*
4600 * Imitate strange Vi behaviour: When using "]]" with an operator
4601 * we also stop at '}'.
4602 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004603 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 (cap->oap->op_type != OP_NOP
4605 && cap->arg == FORWARD && flag == '{')))
4606 clearopbeep(cap->oap);
4607 else
4608 {
4609 if (cap->oap->op_type == OP_NOP)
4610 beginline(BL_WHITE | BL_FIX);
4611#ifdef FEAT_FOLDING
4612 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4613 foldOpenCursor();
4614#endif
4615 }
4616 }
4617
4618 /*
4619 * "[p", "[P", "]P" and "]p": put with indent adjustment
4620 */
4621 else if (cap->nchar == 'p' || cap->nchar == 'P')
4622 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004623 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625
4626 /*
4627 * "['", "[`", "]'" and "]`": jump to next mark
4628 */
4629 else if (cap->nchar == '\'' || cap->nchar == '`')
4630 {
4631 pos = &curwin->w_cursor;
4632 for (n = cap->count1; n > 0; --n)
4633 {
4634 prev_pos = *pos;
4635 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4636 cap->nchar == '\'');
4637 if (pos == NULL)
4638 break;
4639 }
4640 if (pos == NULL)
4641 pos = &prev_pos;
4642 nv_cursormark(cap, cap->nchar == '\'', pos);
4643 }
4644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 /*
4646 * [ or ] followed by a middle mouse click: put selected text with
4647 * indent adjustment. Any other button just does as usual.
4648 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004649 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 (void)do_mouse(cap->oap, cap->nchar,
4652 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4653 cap->count1, PUT_FIXINDENT);
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
4656#ifdef FEAT_FOLDING
4657 /*
4658 * "[z" and "]z": move to start or end of open fold.
4659 */
4660 else if (cap->nchar == 'z')
4661 {
4662 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4663 cap->count1) == FAIL)
4664 clearopbeep(cap->oap);
4665 }
4666#endif
4667
4668#ifdef FEAT_DIFF
4669 /*
4670 * "[c" and "]c": move to next or previous diff-change.
4671 */
4672 else if (cap->nchar == 'c')
4673 {
4674 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4675 cap->count1) == FAIL)
4676 clearopbeep(cap->oap);
4677 }
4678#endif
4679
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004680#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004681 /*
4682 * "[s", "[S", "]s" and "]S": move to next spell error.
4683 */
4684 else if (cap->nchar == 's' || cap->nchar == 'S')
4685 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004686 setpcmark();
4687 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004688 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4689 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004690 {
4691 clearopbeep(cap->oap);
4692 break;
4693 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004694 else
4695 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004696# ifdef FEAT_FOLDING
4697 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4698 foldOpenCursor();
4699# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004700 }
4701#endif
4702
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004703 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 else
4705 clearopbeep(cap->oap);
4706}
4707
4708/*
4709 * Handle Normal mode "%" command.
4710 */
4711 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004712nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
4714 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004715#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 linenr_T lnum = curwin->w_cursor.lnum;
4717#endif
4718
4719 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004720 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
4722 if (cap->count0 > 100)
4723 clearopbeep(cap->oap);
4724 else
4725 {
4726 cap->oap->motion_type = MLINE;
4727 setpcmark();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004728 // Round up, so CTRL-G will give same value. Watch out for a
4729 // large line count, the line number must not go negative!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (curbuf->b_ml.ml_line_count > 1000000)
4731 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4732 / 100L * cap->count0;
4733 else
4734 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4735 cap->count0 + 99L) / 100L;
4736 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4737 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4738 beginline(BL_SOL | BL_FIX);
4739 }
4740 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004741 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
4743 cap->oap->motion_type = MCHAR;
4744 cap->oap->use_reg_one = TRUE;
4745 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4746 clearopbeep(cap->oap);
4747 else
4748 {
4749 setpcmark();
4750 curwin->w_cursor = *pos;
4751 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
4755 }
4756#ifdef FEAT_FOLDING
4757 if (cap->oap->op_type == OP_NOP
4758 && lnum != curwin->w_cursor.lnum
4759 && (fdo_flags & FDO_PERCENT)
4760 && KeyTyped)
4761 foldOpenCursor();
4762#endif
4763}
4764
4765/*
4766 * Handle "(" and ")" commands.
4767 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4768 */
4769 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004770nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
4772 cap->oap->motion_type = MCHAR;
4773 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004774 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004775 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 curwin->w_set_curswant = TRUE;
4777
4778 if (findsent(cap->arg, cap->count1) == FAIL)
4779 clearopbeep(cap->oap);
4780 else
4781 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004782 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004783 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785#ifdef FEAT_FOLDING
4786 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4787 foldOpenCursor();
4788#endif
4789 }
4790}
4791
4792/*
4793 * "m" command: Mark a position.
4794 */
4795 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004796nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
4798 if (!checkclearop(cap->oap))
4799 {
4800 if (setmark(cap->nchar) == FAIL)
4801 clearopbeep(cap->oap);
4802 }
4803}
4804
4805/*
4806 * "{" and "}" commands.
4807 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4808 */
4809 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004810nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812 cap->oap->motion_type = MCHAR;
4813 cap->oap->inclusive = FALSE;
4814 cap->oap->use_reg_one = TRUE;
4815 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004816 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 clearopbeep(cap->oap);
4818 else
4819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821#ifdef FEAT_FOLDING
4822 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4823 foldOpenCursor();
4824#endif
4825 }
4826}
4827
4828/*
4829 * "u" command: Undo or make lower case.
4830 */
4831 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004832nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004834 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004836 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 cap->cmdchar = 'g';
4838 cap->nchar = 'u';
4839 nv_operator(cap);
4840 }
4841 else
4842 nv_kundo(cap);
4843}
4844
4845/*
4846 * <Undo> command.
4847 */
4848 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004849nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850{
4851 if (!checkclearopq(cap->oap))
4852 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004853#ifdef FEAT_JOB_CHANNEL
4854 if (bt_prompt(curbuf))
4855 {
4856 clearopbeep(cap->oap);
4857 return;
4858 }
4859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 u_undo((int)cap->count1);
4861 curwin->w_set_curswant = TRUE;
4862 }
4863}
4864
4865/*
4866 * Handle the "r" command.
4867 */
4868 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004869nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
4871 char_u *ptr;
4872 int had_ctrl_v;
4873 long n;
4874
4875 if (checkclearop(cap->oap))
4876 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004877#ifdef FEAT_JOB_CHANNEL
4878 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4879 {
4880 clearopbeep(cap->oap);
4881 return;
4882 }
4883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004885 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (cap->nchar == Ctrl_V)
4887 {
4888 had_ctrl_v = Ctrl_V;
4889 cap->nchar = get_literal();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004890 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (cap->nchar > DEL)
4892 had_ctrl_v = NUL;
4893 }
4894 else
4895 had_ctrl_v = NUL;
4896
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004897 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004898 if (IS_SPECIAL(cap->nchar))
4899 {
4900 clearopbeep(cap->oap);
4901 return;
4902 }
4903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004904 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (VIsual_active)
4906 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004907 if (got_int)
4908 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004909 if (had_ctrl_v)
4910 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004911 // Use a special (negative) number to make a difference between a
4912 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004913 if (cap->nchar == CAR)
4914 cap->nchar = REPLACE_CR_NCHAR;
4915 else if (cap->nchar == NL)
4916 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 nv_operator(cap);
4919 return;
4920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004922 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (virtual_active())
4924 {
4925 if (u_save_cursor() == FAIL)
4926 return;
4927 if (gchar_cursor() == NUL)
4928 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004929 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 coladvance_force((colnr_T)(getviscol() + cap->count1));
4931 curwin->w_cursor.col -= cap->count1;
4932 }
4933 else if (gchar_cursor() == TAB)
4934 coladvance_force(getviscol());
4935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004937 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004939 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004940 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
4942 clearopbeep(cap->oap);
4943 return;
4944 }
4945
4946 /*
4947 * Replacing with a TAB is done by edit() when it is complicated because
4948 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4949 * Other characters are done below to avoid problems with things like
4950 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
4951 */
4952 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4953 {
4954 stuffnumReadbuff(cap->count1);
4955 stuffcharReadbuff('R');
4956 stuffcharReadbuff('\t');
4957 stuffcharReadbuff(ESC);
4958 return;
4959 }
4960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (u_save_cursor() == FAIL)
4963 return;
4964
4965 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4966 {
4967 /*
4968 * Replace character(s) by a single newline.
4969 * Strange vi behaviour: Only one newline is inserted.
4970 * Delete the characters here.
4971 * Insert the newline with an insert command, takes care of
4972 * autoindent. The insert command depends on being on the last
4973 * character of a line or not.
4974 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004975 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 stuffcharReadbuff('\r');
4977 stuffcharReadbuff(ESC);
4978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004979 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 invoke_edit(cap, TRUE, 'r', FALSE);
4981 }
4982 else
4983 {
4984 prep_redo(cap->oap->regname, cap->count1,
4985 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4986
4987 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (has_mbyte)
4989 {
4990 int old_State = State;
4991
4992 if (cap->ncharC1 != 0)
4993 AppendCharToRedobuff(cap->ncharC1);
4994 if (cap->ncharC2 != 0)
4995 AppendCharToRedobuff(cap->ncharC2);
4996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004997 // This is slow, but it handles replacing a single-byte with a
4998 // multi-byte and the other way around. Also handles adding
4999 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 for (n = cap->count1; n > 0; --n)
5001 {
5002 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005003 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5004 {
5005 int c = ins_copychar(curwin->w_cursor.lnum
5006 + (cap->nchar == Ctrl_Y ? -1 : 1));
5007 if (c != NUL)
5008 ins_char(c);
5009 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005010 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005011 ++curwin->w_cursor.col;
5012 }
5013 else
5014 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 State = old_State;
5016 if (cap->ncharC1 != 0)
5017 ins_char(cap->ncharC1);
5018 if (cap->ncharC2 != 0)
5019 ins_char(cap->ncharC2);
5020 }
5021 }
5022 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
5024 /*
5025 * Replace the characters within one line.
5026 */
5027 for (n = cap->count1; n > 0; --n)
5028 {
5029 /*
5030 * Get ptr again, because u_save and/or showmatch() will have
5031 * released the line. At the same time we let know that the
5032 * line will be changed.
5033 */
5034 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005035 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5036 {
5037 int c = ins_copychar(curwin->w_cursor.lnum
5038 + (cap->nchar == Ctrl_Y ? -1 : 1));
5039 if (c != NUL)
5040 ptr[curwin->w_cursor.col] = c;
5041 }
5042 else
5043 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (p_sm && msg_silent == 0)
5045 showmatch(cap->nchar);
5046 ++curwin->w_cursor.col;
5047 }
5048#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005049 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005051 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052
Bram Moolenaar009b2592004-10-24 19:18:58 +00005053 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005054 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005056 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058#endif
5059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005060 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 changed_bytes(curwin->w_cursor.lnum,
5062 (colnr_T)(curwin->w_cursor.col - cap->count1));
5063 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005064 --curwin->w_cursor.col; // cursor on the last replaced char
5065 // if the character on the left of the current cursor is a multi-byte
5066 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (has_mbyte)
5068 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 curbuf->b_op_end = curwin->w_cursor;
5070 curwin->w_set_curswant = TRUE;
5071 set_last_insert(cap->nchar);
5072 }
5073}
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075/*
5076 * 'o': Exchange start and end of Visual area.
5077 * 'O': same, but in block mode exchange left and right corners.
5078 */
5079 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005080v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
5082 pos_T old_cursor;
5083 colnr_T left, right;
5084
5085 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5086 {
5087 old_cursor = curwin->w_cursor;
5088 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5089 curwin->w_cursor.lnum = VIsual.lnum;
5090 coladvance(left);
5091 VIsual = curwin->w_cursor;
5092
5093 curwin->w_cursor.lnum = old_cursor.lnum;
5094 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005095 // 'selection "exclusive" and cursor at right-bottom corner: move it
5096 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5098 ++curwin->w_curswant;
5099 coladvance(curwin->w_curswant);
5100 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005102 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 {
5104 curwin->w_cursor.lnum = VIsual.lnum;
5105 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5106 ++right;
5107 coladvance(right);
5108 VIsual = curwin->w_cursor;
5109
5110 curwin->w_cursor.lnum = old_cursor.lnum;
5111 coladvance(left);
5112 curwin->w_curswant = left;
5113 }
5114 }
5115 else
5116 {
5117 old_cursor = curwin->w_cursor;
5118 curwin->w_cursor = VIsual;
5119 VIsual = old_cursor;
5120 curwin->w_set_curswant = TRUE;
5121 }
5122}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124/*
5125 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5126 */
5127 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005128nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005130 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
5132 cap->cmdchar = 'c';
5133 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005134 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 VIsual_mode = 'V';
5136 nv_operator(cap);
5137 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005138 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {
5140 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005141 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 else
5143 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 if (virtual_active())
5145 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5147 }
5148 }
5149}
5150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151/*
5152 * "gr".
5153 */
5154 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005155nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (VIsual_active)
5158 {
5159 cap->cmdchar = 'r';
5160 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005161 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005163 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
5165 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005166 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 else
5168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005169 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 cap->extra_char = get_literal();
5171 stuffcharReadbuff(cap->extra_char);
5172 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (virtual_active())
5174 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 invoke_edit(cap, TRUE, 'v', FALSE);
5176 }
5177 }
5178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180/*
5181 * Swap case for "~" command, when it does not work like an operator.
5182 */
5183 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005184n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
5186 long n;
5187 pos_T startpos;
5188 int did_change = 0;
5189#ifdef FEAT_NETBEANS_INTG
5190 pos_T pos;
5191 char_u *ptr;
5192 int count;
5193#endif
5194
5195 if (checkclearopq(cap->oap))
5196 return;
5197
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005198 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 {
5200 clearopbeep(cap->oap);
5201 return;
5202 }
5203
5204 prep_redo_cmd(cap);
5205
5206 if (u_save_cursor() == FAIL)
5207 return;
5208
5209 startpos = curwin->w_cursor;
5210#ifdef FEAT_NETBEANS_INTG
5211 pos = startpos;
5212#endif
5213 for (n = cap->count1; n > 0; --n)
5214 {
5215 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5216 inc_cursor();
5217 if (gchar_cursor() == NUL)
5218 {
5219 if (vim_strchr(p_ww, '~') != NULL
5220 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5221 {
5222#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005223 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 {
5225 if (did_change)
5226 {
5227 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005228 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005229 netbeans_removed(curbuf, pos.lnum, pos.col,
5230 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005232 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
5234 pos.col = 0;
5235 pos.lnum++;
5236 }
5237#endif
5238 ++curwin->w_cursor.lnum;
5239 curwin->w_cursor.col = 0;
5240 if (n > 1)
5241 {
5242 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5243 break;
5244 u_clearline();
5245 }
5246 }
5247 else
5248 break;
5249 }
5250 }
5251#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005252 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
5254 ptr = ml_get(pos.lnum);
5255 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005256 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5257 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259#endif
5260
5261
5262 check_cursor();
5263 curwin->w_set_curswant = TRUE;
5264 if (did_change)
5265 {
5266 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5267 0L);
5268 curbuf->b_op_start = startpos;
5269 curbuf->b_op_end = curwin->w_cursor;
5270 if (curbuf->b_op_end.col > 0)
5271 --curbuf->b_op_end.col;
5272 }
5273}
5274
5275/*
5276 * Move cursor to mark.
5277 */
5278 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005279nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280{
5281 if (check_mark(pos) == FAIL)
5282 clearop(cap->oap);
5283 else
5284 {
5285 if (cap->cmdchar == '\''
5286 || cap->cmdchar == '`'
5287 || cap->cmdchar == '['
5288 || cap->cmdchar == ']')
5289 setpcmark();
5290 curwin->w_cursor = *pos;
5291 if (flag)
5292 beginline(BL_WHITE | BL_FIX);
5293 else
5294 check_cursor();
5295 }
5296 cap->oap->motion_type = flag ? MLINE : MCHAR;
5297 if (cap->cmdchar == '`')
5298 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005299 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 curwin->w_set_curswant = TRUE;
5301}
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303/*
5304 * Handle commands that are operators in Visual mode.
5305 */
5306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005307v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5310
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005311 // Uppercase means linewise, except in block mode, then "D" deletes till
5312 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (isupper(cap->cmdchar))
5314 {
5315 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005316 {
5317 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5321 curwin->w_curswant = MAXCOL;
5322 }
5323 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5324 nv_operator(cap);
5325}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327/*
5328 * "s" and "S" commands.
5329 */
5330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005331nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005333#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005334 // When showing output of term_dumpdiff() swap the top and botom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005335 if (term_swap_diff() == OK)
5336 return;
5337#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005338#ifdef FEAT_JOB_CHANNEL
5339 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5340 {
5341 clearopbeep(cap->oap);
5342 return;
5343 }
5344#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005345 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005348 {
5349 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 cap->cmdchar = 'c';
5353 nv_operator(cap);
5354 }
5355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 nv_optrans(cap);
5357}
5358
5359/*
5360 * Abbreviated commands.
5361 */
5362 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005363nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
5365 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005366 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005368 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 if (VIsual_active)
5370 v_visop(cap);
5371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 nv_optrans(cap);
5373}
5374
5375/*
5376 * Translate a command into another command.
5377 */
5378 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005379nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
5381 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5382 (char_u *)"d$", (char_u *)"c$",
5383 (char_u *)"cl", (char_u *)"cc",
5384 (char_u *)"yy", (char_u *)":s\r"};
5385 static char_u *str = (char_u *)"xXDCsSY&";
5386
5387 if (!checkclearopq(cap->oap))
5388 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005389 // In Vi "2D" doesn't delete the next line. Can't translate it
5390 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005391 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5392 {
5393 cap->oap->start = curwin->w_cursor;
5394 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005395#ifdef FEAT_EVAL
5396 set_op_var(OP_DELETE);
5397#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005398 cap->count1 = 1;
5399 nv_dollar(cap);
5400 finish_op = TRUE;
5401 ResetRedobuff();
5402 AppendCharToRedobuff('D');
5403 }
5404 else
5405 {
5406 if (cap->count0)
5407 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005408 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
5411 cap->opcount = 0;
5412}
5413
5414/*
5415 * "'" and "`" commands. Also for "g'" and "g`".
5416 * cap->arg is TRUE for "'" and "g'".
5417 */
5418 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005419nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 pos_T *pos;
5422 int c;
5423#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005424 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005425 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426#endif
5427
5428 if (cap->cmdchar == 'g')
5429 c = cap->extra_char;
5430 else
5431 c = cap->nchar;
5432 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005433 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
5435 if (cap->arg)
5436 {
5437 check_cursor_lnum();
5438 beginline(BL_WHITE | BL_FIX);
5439 }
5440 else
5441 check_cursor();
5442 }
5443 else
5444 nv_cursormark(cap, cap->arg, pos);
5445
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005446 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (!virtual_active())
5448 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005449 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450#ifdef FEAT_FOLDING
5451 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005452 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005453 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 && (fdo_flags & FDO_MARK)
5455 && old_KeyTyped)
5456 foldOpenCursor();
5457#endif
5458}
5459
5460/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005461 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 */
5463 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005464nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465{
5466#ifdef FEAT_JUMPLIST
5467 pos_T *pos;
5468# ifdef FEAT_FOLDING
5469 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005470 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471# endif
5472
5473 if (!checkclearopq(cap->oap))
5474 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005475 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5476 {
5477 if (goto_tabpage_lastused() == FAIL)
5478 clearopbeep(cap->oap);
5479 return;
5480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 if (cap->cmdchar == 'g')
5482 pos = movechangelist((int)cap->count1);
5483 else
5484 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005485 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 {
5487 curwin->w_set_curswant = TRUE;
5488 check_cursor();
5489 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005490 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 nv_cursormark(cap, FALSE, pos);
5492 else if (cap->cmdchar == 'g')
5493 {
5494 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005495 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005497 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005499 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501 else
5502 clearopbeep(cap->oap);
5503# ifdef FEAT_FOLDING
5504 if (cap->oap->op_type == OP_NOP
5505 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5506 && (fdo_flags & FDO_MARK)
5507 && old_KeyTyped)
5508 foldOpenCursor();
5509# endif
5510 }
5511#else
5512 clearopbeep(cap->oap);
5513#endif
5514}
5515
5516/*
5517 * Handle '"' command.
5518 */
5519 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005520nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 if (checkclearop(cap->oap))
5523 return;
5524#ifdef FEAT_EVAL
5525 if (cap->nchar == '=')
5526 cap->nchar = get_expr_register();
5527#endif
5528 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5529 {
5530 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005531 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532#ifdef FEAT_EVAL
5533 set_reg_var(cap->oap->regname);
5534#endif
5535 }
5536 else
5537 clearopbeep(cap->oap);
5538}
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540/*
5541 * Handle "v", "V" and "CTRL-V" commands.
5542 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5543 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005544 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 */
5546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005547nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005549 if (cap->cmdchar == Ctrl_Q)
5550 cap->cmdchar = Ctrl_V;
5551
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005552 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5553 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 if (cap->oap->op_type != OP_NOP)
5555 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005556 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005557 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 return;
5559 }
5560
5561 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005562 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005564 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005566 else // toggle char/block mode
5567 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 VIsual_mode = cap->cmdchar;
5569 showmode();
5570 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005571 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005573 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 {
5575 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005576 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005578 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 VIsual = curwin->w_cursor;
5580
5581 VIsual_active = TRUE;
5582 VIsual_reselect = TRUE;
5583 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005584 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005587 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005588 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 /*
5590 * For V and ^V, we multiply the number of lines even if there
5591 * was only one -- webb
5592 */
5593 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5594 {
5595 curwin->w_cursor.lnum +=
5596 resel_VIsual_line_count * cap->count0 - 1;
5597 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5598 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5599 }
5600 VIsual_mode = resel_VIsual_mode;
5601 if (VIsual_mode == 'v')
5602 {
5603 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005604 {
5605 validate_virtcol();
5606 curwin->w_curswant = curwin->w_virtcol
5607 + resel_VIsual_vcol * cap->count0 - 1;
5608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005610 curwin->w_curswant = resel_VIsual_vcol;
5611 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005613 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
5615 curwin->w_curswant = MAXCOL;
5616 coladvance((colnr_T)MAXCOL);
5617 }
5618 else if (VIsual_mode == Ctrl_V)
5619 {
5620 validate_virtcol();
5621 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005622 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 coladvance(curwin->w_curswant);
5624 }
5625 else
5626 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
5629 else
5630 {
5631 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005632 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 may_start_select('c');
5634 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005635 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005636 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005637 if (cap->count0 > 0 && --cap->count1 > 0)
5638 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005639 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005640 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5641 nv_right(cap);
5642 else if (VIsual_mode == 'V')
5643 nv_down(cap);
5644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
5646 }
5647}
5648
5649/*
5650 * Start selection for Shift-movement keys.
5651 */
5652 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005653start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005655 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 may_start_select('k');
5657 n_start_visual_mode('v');
5658}
5659
5660/*
5661 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5662 */
5663 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005664may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 VIsual_select = (stuff_empty() && typebuf_typed()
5667 && (vim_strchr(p_slm, c) != NULL));
5668}
5669
5670/*
5671 * Start Visual mode "c".
5672 * Should set VIsual_select before calling this.
5673 */
5674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005675n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005677#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005678 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005679 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005680#endif
5681
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 VIsual_mode = c;
5683 VIsual_active = TRUE;
5684 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005685
5686 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005687 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005689 {
5690 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 VIsual = curwin->w_cursor;
5694
5695#ifdef FEAT_FOLDING
5696 foldAdjustVisual();
5697#endif
5698
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005700#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005701 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005702 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005703#endif
5704
Bram Moolenaar09df3122006-01-23 22:23:09 +00005705 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005706 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005708 // Make sure the clipboard gets updated. Needed because start and
5709 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 clip_star.vmode = NUL;
5711#endif
5712
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005713 // Only need to redraw this line, unless still need to redraw an old
5714 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 if (curwin->w_redr_type < INVERTED)
5716 {
5717 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5718 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5719 }
5720}
5721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
5723/*
5724 * CTRL-W: Window commands
5725 */
5726 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005727nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005729 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005731 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005732 cap->cmdchar = ':';
5733 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005734 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005735 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005736 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005737 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738}
5739
5740/*
5741 * CTRL-Z: Suspend
5742 */
5743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005744nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
5746 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005748 end_visual_mode(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 do_cmdline_cmd((char_u *)"st");
5750}
5751
5752/*
5753 * Commands starting with "g".
5754 */
5755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005756nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
5758 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 int i;
5761 int flag = FALSE;
5762
5763 switch (cap->nchar)
5764 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005765 case Ctrl_A:
5766 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767#ifdef MEM_PROFILE
5768 /*
5769 * "g^A": dump log of used memory.
5770 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005771 if (!VIsual_active && cap->nchar == Ctrl_A)
5772 vim_mem_profile_dump();
5773 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005775 /*
5776 * "g^A/g^X": sequentially increment visually selected region
5777 */
5778 if (VIsual_active)
5779 {
5780 cap->arg = TRUE;
5781 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005782 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005783 nv_addsub(cap);
5784 }
5785 else
5786 clearopbeep(oap);
5787 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 /*
5790 * "gR": Enter virtual replace mode.
5791 */
5792 case 'R':
5793 cap->arg = TRUE;
5794 nv_Replace(cap);
5795 break;
5796
5797 case 'r':
5798 nv_vreplace(cap);
5799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
5801 case '&':
5802 do_cmdline_cmd((char_u *)"%s//~/&");
5803 break;
5804
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 /*
5806 * "gv": Reselect the previous Visual area. If Visual already active,
5807 * exchange previous and current Visual area.
5808 */
5809 case 'v':
5810 if (checkclearop(oap))
5811 break;
5812
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005813 if ( curbuf->b_visual.vi_start.lnum == 0
5814 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5815 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 beep_flush();
5817 else
5818 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005819 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 if (VIsual_active)
5821 {
5822 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005823 VIsual_mode = curbuf->b_visual.vi_mode;
5824 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825# ifdef FEAT_EVAL
5826 curbuf->b_visual_mode_eval = i;
5827# endif
5828 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005829 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5830 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005832 tpos = curbuf->b_visual.vi_end;
5833 curbuf->b_visual.vi_end = curwin->w_cursor;
5834 curwin->w_cursor = curbuf->b_visual.vi_start;
5835 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
5837 else
5838 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005839 VIsual_mode = curbuf->b_visual.vi_mode;
5840 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5841 tpos = curbuf->b_visual.vi_end;
5842 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
5844
5845 VIsual_active = TRUE;
5846 VIsual_reselect = TRUE;
5847
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005848 // Set Visual to the start and w_cursor to the end of the Visual
5849 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 check_cursor();
5851 VIsual = curwin->w_cursor;
5852 curwin->w_cursor = tpos;
5853 check_cursor();
5854 update_topline();
5855 /*
5856 * When called from normal "g" command: start Select mode when
5857 * 'selectmode' contains "cmd". When called for K_SELECT, always
5858 * start Select mode.
5859 */
5860 if (cap->arg)
5861 VIsual_select = TRUE;
5862 else
5863 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005866 // Make sure the clipboard gets updated. Needed because start and
5867 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 clip_star.vmode = NUL;
5869#endif
5870 redraw_curbuf_later(INVERTED);
5871 showmode();
5872 }
5873 break;
5874 /*
5875 * "gV": Don't reselect the previous Visual area after a Select mode
5876 * mapping of menu.
5877 */
5878 case 'V':
5879 VIsual_reselect = FALSE;
5880 break;
5881
5882 /*
5883 * "gh": start Select mode.
5884 * "gH": start Select line mode.
5885 * "g^H": start Select block mode.
5886 */
5887 case K_BS:
5888 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005889 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 case 'h':
5891 case 'H':
5892 case Ctrl_H:
5893# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005894 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (cap->nchar == Ctrl_H)
5896 cap->cmdchar = Ctrl_V;
5897 else
5898# endif
5899 cap->cmdchar = cap->nchar + ('v' - 'h');
5900 cap->arg = TRUE;
5901 nv_visual(cap);
5902 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005904 // "gn", "gN" visually select next/previous search match
5905 // "gn" selects next match
5906 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005907 case 'N':
5908 case 'n':
5909 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005910 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005911 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
5913 /*
5914 * "gj" and "gk" two new funny movement keys -- up and down
5915 * movement based on *screen* line rather than *file* line.
5916 */
5917 case 'j':
5918 case K_DOWN:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005919 // with 'nowrap' it works just like the normal "j" command; also when
5920 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 if (!curwin->w_p_wrap
5922#ifdef FEAT_FOLDING
5923 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5924#endif
5925 )
5926 {
5927 oap->motion_type = MLINE;
5928 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5929 }
5930 else
5931 i = nv_screengo(oap, FORWARD, cap->count1);
5932 if (i == FAIL)
5933 clearopbeep(oap);
5934 break;
5935
5936 case 'k':
5937 case K_UP:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005938 // with 'nowrap' it works just like the normal "k" command; also when
5939 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 if (!curwin->w_p_wrap
5941#ifdef FEAT_FOLDING
5942 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5943#endif
5944 )
5945 {
5946 oap->motion_type = MLINE;
5947 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5948 }
5949 else
5950 i = nv_screengo(oap, BACKWARD, cap->count1);
5951 if (i == FAIL)
5952 clearopbeep(oap);
5953 break;
5954
5955 /*
5956 * "gJ": join two lines without inserting a space.
5957 */
5958 case 'J':
5959 nv_join(cap);
5960 break;
5961
5962 /*
5963 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
5964 * "gm": middle of "g0" and "g$".
5965 */
5966 case '^':
5967 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005968 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969
5970 case '0':
5971 case 'm':
5972 case K_HOME:
5973 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 oap->motion_type = MCHAR;
5975 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005976 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005978 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 int width2 = width1 + curwin_col_off2();
5980
5981 validate_virtcol();
5982 i = 0;
5983 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5984 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5985 }
5986 else
5987 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005988 // Go to the middle of the screen line. When 'number' or
5989 // 'relativenumber' is on and lines are wrapping the middle can be more
5990 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02005992 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 + ((curwin->w_p_wrap && i > 0)
5994 ? curwin_col_off2() : 0)) / 2;
5995 coladvance((colnr_T)i);
5996 if (flag)
5997 {
5998 do
5999 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006000 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006001 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
6003 curwin->w_set_curswant = TRUE;
6004 break;
6005
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006006 case 'M':
6007 {
6008 char_u *ptr = ml_get_curline();
6009
6010 oap->motion_type = MCHAR;
6011 oap->inclusive = FALSE;
6012 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006013 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006014 else
6015 i = (int)STRLEN(ptr);
6016 if (cap->count0 > 0 && cap->count0 <= 100)
6017 coladvance((colnr_T)(i * cap->count0 / 100));
6018 else
6019 coladvance((colnr_T)(i / 2));
6020 curwin->w_set_curswant = TRUE;
6021 }
6022 break;
6023
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006025 // "g_": to the last non-blank character in the line or <count> lines
6026 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 cap->oap->motion_type = MCHAR;
6028 cap->oap->inclusive = TRUE;
6029 curwin->w_curswant = MAXCOL;
6030 if (cursor_down((long)(cap->count1 - 1),
6031 cap->oap->op_type == OP_NOP) == FAIL)
6032 clearopbeep(cap->oap);
6033 else
6034 {
6035 char_u *ptr = ml_get_curline();
6036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006037 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6039 --curwin->w_cursor.col;
6040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006041 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006043 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 --curwin->w_cursor.col;
6045 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006046 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 }
6048 break;
6049
6050 case '$':
6051 case K_END:
6052 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 {
6054 int col_off = curwin_col_off();
6055
6056 oap->motion_type = MCHAR;
6057 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006058 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006060 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 if (cap->count1 == 1)
6062 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006063 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 int width2 = width1 + curwin_col_off2();
6065
6066 validate_virtcol();
6067 i = width1 - 1;
6068 if (curwin->w_virtcol >= (colnr_T)width1)
6069 i += ((curwin->w_virtcol - width1) / width2 + 1)
6070 * width2;
6071 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006073 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006074 validate_virtcol();
6075 curwin->w_curswant = curwin->w_virtcol;
6076 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6078 {
6079 /*
6080 * Check for landing on a character that got split at
6081 * the end of the line. We do not want to advance to
6082 * the next screen line.
6083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (curwin->w_virtcol > (colnr_T)i)
6085 --curwin->w_cursor.col;
6086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 }
6088 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6089 clearopbeep(oap);
6090 }
6091 else
6092 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006093 if (cap->count1 > 1)
6094 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006095 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006096
Bram Moolenaar02631462017-09-22 15:20:32 +02006097 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006099
Bram Moolenaard5c82342019-07-27 18:44:57 +02006100 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006101 validate_virtcol();
6102 curwin->w_curswant = curwin->w_virtcol;
6103 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 }
6105 }
6106 break;
6107
6108 /*
6109 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6110 */
6111 case '*':
6112 case '#':
6113#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006114 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006116 case Ctrl_RSB: // :tag or :tselect for current identifier
6117 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 nv_ident(cap);
6119 break;
6120
6121 /*
6122 * ge and gE: go back to end of word
6123 */
6124 case 'e':
6125 case 'E':
6126 oap->motion_type = MCHAR;
6127 curwin->w_set_curswant = TRUE;
6128 oap->inclusive = TRUE;
6129 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6130 clearopbeep(oap);
6131 break;
6132
6133 /*
6134 * "g CTRL-G": display info about cursor position
6135 */
6136 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006137 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 break;
6139
6140 /*
6141 * "gi": start Insert at the last position.
6142 */
6143 case 'i':
6144 if (curbuf->b_last_insert.lnum != 0)
6145 {
6146 curwin->w_cursor = curbuf->b_last_insert;
6147 check_cursor_lnum();
6148 i = (int)STRLEN(ml_get_curline());
6149 if (curwin->w_cursor.col > (colnr_T)i)
6150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (virtual_active())
6152 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 curwin->w_cursor.col = i;
6154 }
6155 }
6156 cap->cmdchar = 'i';
6157 nv_edit(cap);
6158 break;
6159
6160 /*
6161 * "gI": Start insert in column 1.
6162 */
6163 case 'I':
6164 beginline(0);
6165 if (!checkclearopq(oap))
6166 invoke_edit(cap, FALSE, 'g', FALSE);
6167 break;
6168
6169#ifdef FEAT_SEARCHPATH
6170 /*
6171 * "gf": goto file, edit file under cursor
6172 * "]f" and "[f": can also be used.
6173 */
6174 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006175 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 nv_gotofile(cap);
6177 break;
6178#endif
6179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006180 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 case '\'':
6182 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006183 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 case '`':
6185 nv_gomark(cap);
6186 break;
6187
6188 /*
6189 * "gs": Goto sleep.
6190 */
6191 case 's':
6192 do_sleep(cap->count1 * 1000L);
6193 break;
6194
6195 /*
6196 * "ga": Display the ascii value of the character under the
6197 * cursor. It is displayed in decimal, hex, and octal. -- webb
6198 */
6199 case 'a':
6200 do_ascii(NULL);
6201 break;
6202
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 /*
6204 * "g8": Display the bytes used for the UTF-8 character under the
6205 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006206 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 */
6208 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006209 if (cap->count0 == 8)
6210 utf_find_illegal();
6211 else
6212 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006215 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006216 case '<':
6217 show_sb_text();
6218 break;
6219
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 /*
6221 * "gg": Goto the first line in file. With a count it goes to
6222 * that line number like for "G". -- webb
6223 */
6224 case 'g':
6225 cap->arg = FALSE;
6226 nv_goto(cap);
6227 break;
6228
6229 /*
6230 * Two-character operators:
6231 * "gq" Format text
6232 * "gw" Format text and keep cursor position
6233 * "g~" Toggle the case of the text.
6234 * "gu" Change text to lower case.
6235 * "gU" Change text to upper case.
6236 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006237 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 */
6239 case 'q':
6240 case 'w':
6241 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006242 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 case '~':
6244 case 'u':
6245 case 'U':
6246 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006247 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 nv_operator(cap);
6249 break;
6250
6251 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006252 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 * current function
6254 * "gD": idem, but in the current file.
6255 */
6256 case 'd':
6257 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006258 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 break;
6260
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 /*
6262 * g<*Mouse> : <C-*mouse>
6263 */
6264 case K_MIDDLEMOUSE:
6265 case K_MIDDLEDRAG:
6266 case K_MIDDLERELEASE:
6267 case K_LEFTMOUSE:
6268 case K_LEFTDRAG:
6269 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006270 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 case K_RIGHTMOUSE:
6272 case K_RIGHTDRAG:
6273 case K_RIGHTRELEASE:
6274 case K_X1MOUSE:
6275 case K_X1DRAG:
6276 case K_X1RELEASE:
6277 case K_X2MOUSE:
6278 case K_X2DRAG:
6279 case K_X2RELEASE:
6280 mod_mask = MOD_MASK_CTRL;
6281 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
6284 case K_IGNORE:
6285 break;
6286
6287 /*
6288 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6289 */
6290 case 'p':
6291 case 'P':
6292 nv_put(cap);
6293 break;
6294
6295#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006296 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 case 'o':
6298 goto_byte(cap->count0);
6299 break;
6300#endif
6301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006302 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006304 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
6306 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006307 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 break;
6309 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (!checkclearopq(oap))
6312 do_exmode(TRUE);
6313 break;
6314
6315#ifdef FEAT_JUMPLIST
6316 case ',':
6317 nv_pcmark(cap);
6318 break;
6319
6320 case ';':
6321 cap->count1 = -cap->count1;
6322 nv_pcmark(cap);
6323 break;
6324#endif
6325
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006326 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006327 if (!checkclearop(oap))
6328 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006329 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006330 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006331 if (!checkclearop(oap))
6332 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006333 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006334
Bram Moolenaar62a23252020-08-09 14:04:42 +02006335 case TAB:
6336 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6337 clearopbeep(oap);
6338 break;
6339
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006340 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006341 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006342 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006343 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006344 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006345 break;
6346
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 default:
6348 clearopbeep(oap);
6349 break;
6350 }
6351}
6352
6353/*
6354 * Handle "o" and "O" commands.
6355 */
6356 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006357n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006359#ifdef FEAT_CONCEAL
6360 linenr_T oldline = curwin->w_cursor.lnum;
6361#endif
6362
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 if (!checkclearopq(cap->oap))
6364 {
6365#ifdef FEAT_FOLDING
6366 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006367 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 (void)hasFolding(curwin->w_cursor.lnum,
6369 &curwin->w_cursor.lnum, NULL);
6370 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006371 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 (void)hasFolding(curwin->w_cursor.lnum,
6373 NULL, &curwin->w_cursor.lnum);
6374#endif
6375 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6376 (cap->cmdchar == 'O' ? 1 : 0)),
6377 (linenr_T)(curwin->w_cursor.lnum +
6378 (cap->cmdchar == 'o' ? 1 : 0))
6379 ) == OK
6380 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006381 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6382 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006384#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006385 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006386 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006387#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006388#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006389 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006390 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006391 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006392#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006393 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006394 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6395 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6397 }
6398 }
6399}
6400
6401/*
6402 * "." command: redo last change.
6403 */
6404 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006405nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 if (!checkclearopq(cap->oap))
6408 {
6409 /*
6410 * If "restart_edit" is TRUE, the last but one command is repeated
6411 * instead of the last command (inserting text). This is used for
6412 * CTRL-O <.> in insert mode.
6413 */
6414 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6415 clearopbeep(cap->oap);
6416 }
6417}
6418
6419/*
6420 * CTRL-R: undo undo
6421 */
6422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006423nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
6425 if (!checkclearopq(cap->oap))
6426 {
6427 u_redo((int)cap->count1);
6428 curwin->w_set_curswant = TRUE;
6429 }
6430}
6431
6432/*
6433 * Handle "U" command.
6434 */
6435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006436nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006438 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006439 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006441 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 cap->cmdchar = 'g';
6443 cap->nchar = 'U';
6444 nv_operator(cap);
6445 }
6446 else if (!checkclearopq(cap->oap))
6447 {
6448 u_undoline();
6449 curwin->w_set_curswant = TRUE;
6450 }
6451}
6452
6453/*
6454 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6455 * single character.
6456 */
6457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006458nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006460 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006461 {
6462#ifdef FEAT_JOB_CHANNEL
6463 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6464 {
6465 clearopbeep(cap->oap);
6466 return;
6467 }
6468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 else
6472 nv_operator(cap);
6473}
6474
6475/*
6476 * Handle an operator command.
6477 * The actual work is done by do_pending_operator().
6478 */
6479 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006480nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
6482 int op_type;
6483
6484 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006485#ifdef FEAT_JOB_CHANNEL
6486 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6487 {
6488 clearopbeep(cap->oap);
6489 return;
6490 }
6491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006493 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 nv_lineop(cap);
6495 else if (!checkclearop(cap->oap))
6496 {
6497 cap->oap->start = curwin->w_cursor;
6498 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006499#ifdef FEAT_EVAL
6500 set_op_var(op_type);
6501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 }
6503}
6504
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006505#ifdef FEAT_EVAL
6506/*
6507 * Set v:operator to the characters for "optype".
6508 */
6509 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006510set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006511{
6512 char_u opchars[3];
6513
6514 if (optype == OP_NOP)
6515 set_vim_var_string(VV_OP, NULL, 0);
6516 else
6517 {
6518 opchars[0] = get_op_char(optype);
6519 opchars[1] = get_extra_op_char(optype);
6520 opchars[2] = NUL;
6521 set_vim_var_string(VV_OP, opchars, -1);
6522 }
6523}
6524#endif
6525
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526/*
6527 * Handle linewise operator "dd", "yy", etc.
6528 *
6529 * "_" is is a strange motion command that helps make operators more logical.
6530 * It is actually implemented, but not documented in the real Vi. This motion
6531 * command actually refers to "the current line". Commands like "dd" and "yy"
6532 * are really an alternate form of "d_" and "y_". It does accept a count, so
6533 * "d3_" works to delete 3 lines.
6534 */
6535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006536nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 cap->oap->motion_type = MLINE;
6539 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6540 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006541 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006542 && cap->oap->motion_force != 'v'
6543 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 || cap->oap->op_type == OP_LSHIFT
6545 || cap->oap->op_type == OP_RSHIFT)
6546 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006547 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 beginline(BL_WHITE | BL_FIX);
6549}
6550
6551/*
6552 * <Home> command.
6553 */
6554 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006555nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006557 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006558 if (mod_mask & MOD_MASK_CTRL)
6559 nv_goto(cap);
6560 else
6561 {
6562 cap->count0 = 1;
6563 nv_pipe(cap);
6564 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006565 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6566 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567}
6568
6569/*
6570 * "|" command.
6571 */
6572 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006573nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
6575 cap->oap->motion_type = MCHAR;
6576 cap->oap->inclusive = FALSE;
6577 beginline(0);
6578 if (cap->count0 > 0)
6579 {
6580 coladvance((colnr_T)(cap->count0 - 1));
6581 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6582 }
6583 else
6584 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006585 // keep curswant at the column where we wanted to go, not where
6586 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 curwin->w_set_curswant = FALSE;
6588}
6589
6590/*
6591 * Handle back-word command "b" and "B".
6592 * cap->arg is 1 for "B"
6593 */
6594 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006595nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596{
6597 cap->oap->motion_type = MCHAR;
6598 cap->oap->inclusive = FALSE;
6599 curwin->w_set_curswant = TRUE;
6600 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6601 clearopbeep(cap->oap);
6602#ifdef FEAT_FOLDING
6603 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6604 foldOpenCursor();
6605#endif
6606}
6607
6608/*
6609 * Handle word motion commands "e", "E", "w" and "W".
6610 * cap->arg is TRUE for "E" and "W".
6611 */
6612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006613nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615 int n;
6616 int word_end;
6617 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006618 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619
6620 /*
6621 * Set inclusive for the "E" and "e" command.
6622 */
6623 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6624 word_end = TRUE;
6625 else
6626 word_end = FALSE;
6627 cap->oap->inclusive = word_end;
6628
6629 /*
6630 * "cw" and "cW" are a special case.
6631 */
6632 if (!word_end && cap->oap->op_type == OP_CHANGE)
6633 {
6634 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006635 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006637 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 {
6639 /*
6640 * Reproduce a funny Vi behaviour: "cw" on a blank only
6641 * changes one character, not all blanks until the start of
6642 * the next word. Only do this when the 'w' flag is included
6643 * in 'cpoptions'.
6644 */
6645 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6646 {
6647 cap->oap->inclusive = TRUE;
6648 cap->oap->motion_type = MCHAR;
6649 return;
6650 }
6651 }
6652 else
6653 {
6654 /*
6655 * This is a little strange. To match what the real Vi does,
6656 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6657 * that we are not on a space or a TAB. This seems impolite
6658 * at first, but it's really more what we mean when we say
6659 * 'cw'.
6660 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006661 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 * will change only one character! This is done by setting
6663 * flag.
6664 */
6665 cap->oap->inclusive = TRUE;
6666 word_end = TRUE;
6667 flag = TRUE;
6668 }
6669 }
6670 }
6671
6672 cap->oap->motion_type = MCHAR;
6673 curwin->w_set_curswant = TRUE;
6674 if (word_end)
6675 n = end_word(cap->count1, cap->arg, flag, FALSE);
6676 else
6677 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6678
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006679 // Don't leave the cursor on the NUL past the end of line. Unless we
6680 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006681 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006682 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
6684 if (n == FAIL && cap->oap->op_type == OP_NOP)
6685 clearopbeep(cap->oap);
6686 else
6687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689#ifdef FEAT_FOLDING
6690 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6691 foldOpenCursor();
6692#endif
6693 }
6694}
6695
6696/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006697 * Used after a movement command: If the cursor ends up on the NUL after the
6698 * end of the line, may move it back to the last character and make the motion
6699 * inclusive.
6700 */
6701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006702adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006703{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006704 // The cursor cannot remain on the NUL when:
6705 // - the column is > 0
6706 // - not in Visual mode or 'selection' is "o"
6707 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006708 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006709 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006710 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006711 {
6712 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006713 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006714 if (has_mbyte)
6715 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006716 oap->inclusive = TRUE;
6717 }
6718}
6719
6720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 * "0" and "^" commands.
6722 * cap->arg is the argument for beginline().
6723 */
6724 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006725nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 cap->oap->motion_type = MCHAR;
6728 cap->oap->inclusive = FALSE;
6729 beginline(cap->arg);
6730#ifdef FEAT_FOLDING
6731 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6732 foldOpenCursor();
6733#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006734 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6735 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736}
6737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738/*
6739 * In exclusive Visual mode, may include the last character.
6740 */
6741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006742adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743{
6744 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006745 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 if (has_mbyte)
6748 inc_cursor();
6749 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 ++curwin->w_cursor.col;
6751 cap->oap->inclusive = FALSE;
6752 }
6753}
6754
6755/*
6756 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6757 * Should check VIsual_mode before calling this.
6758 * Returns TRUE when backed up to the previous line.
6759 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006760 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006761unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 pos_T *pp;
6764
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006765 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006767 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 pp = &curwin->w_cursor;
6769 else
6770 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 if (pp->coladd > 0)
6772 --pp->coladd;
6773 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 if (pp->col > 0)
6775 {
6776 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006777 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 }
6779 else if (pp->lnum > 1)
6780 {
6781 --pp->lnum;
6782 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6783 return TRUE;
6784 }
6785 }
6786 return FALSE;
6787}
6788
6789/*
6790 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6791 */
6792 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006793nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 if (VIsual_active)
6796 VIsual_select = TRUE;
6797 else if (VIsual_reselect)
6798 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006799 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 cap->arg = TRUE;
6801 nv_g_cmd(cap);
6802 }
6803}
6804
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806/*
6807 * "G", "gg", CTRL-END, CTRL-HOME.
6808 * cap->arg is TRUE for "G".
6809 */
6810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006811nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
6813 linenr_T lnum;
6814
6815 if (cap->arg)
6816 lnum = curbuf->b_ml.ml_line_count;
6817 else
6818 lnum = 1L;
6819 cap->oap->motion_type = MLINE;
6820 setpcmark();
6821
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006822 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 if (cap->count0 != 0)
6824 lnum = cap->count0;
6825 if (lnum < 1L)
6826 lnum = 1L;
6827 else if (lnum > curbuf->b_ml.ml_line_count)
6828 lnum = curbuf->b_ml.ml_line_count;
6829 curwin->w_cursor.lnum = lnum;
6830 beginline(BL_SOL | BL_FIX);
6831#ifdef FEAT_FOLDING
6832 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6833 foldOpenCursor();
6834#endif
6835}
6836
6837/*
6838 * CTRL-\ in Normal mode.
6839 */
6840 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006841nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6844 {
6845 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006846 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006847 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 restart_edit = 0;
6849#ifdef FEAT_CMDWIN
6850 if (cmdwin_type != 0)
6851 cmdwin_result = Ctrl_C;
6852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 if (VIsual_active)
6854 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006855 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 redraw_curbuf_later(INVERTED);
6857 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006858 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 if (cap->nchar == Ctrl_G && p_im)
6860 restart_edit = 'a';
6861 }
6862 else
6863 clearopbeep(cap->oap);
6864}
6865
6866/*
6867 * ESC in Normal mode: beep, but don't flush buffers.
6868 * Don't even beep if we are canceling a command.
6869 */
6870 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006871nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
6873 int no_reason;
6874
6875 no_reason = (cap->oap->op_type == OP_NOP
6876 && cap->opcount == 0
6877 && cap->count0 == 0
6878 && cap->oap->regname == 0
6879 && !p_im);
6880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006881 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
6883 if (restart_edit == 0
6884#ifdef FEAT_CMDWIN
6885 && cmdwin_type == 0
6886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006889 {
6890 if (anyBufIsChanged())
6891 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6892 else
6893 msg(_("Type :qa and press <Enter> to exit Vim"));
6894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006896 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6897 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 if (!p_im)
6899 restart_edit = 0;
6900#ifdef FEAT_CMDWIN
6901 if (cmdwin_type != 0)
6902 {
6903 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006904 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 return;
6906 }
6907#endif
6908 }
6909
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 if (VIsual_active)
6911 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006912 end_visual_mode(); // stop Visual
6913 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 curwin->w_set_curswant = TRUE;
6915 redraw_curbuf_later(INVERTED);
6916 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006917 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006918 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 clearop(cap->oap);
6920
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006921 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6922 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006923 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 restart_edit = 'a';
6925}
6926
6927/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006928 * Move the cursor for the "A" command.
6929 */
6930 void
6931set_cursor_for_append_to_line(void)
6932{
6933 curwin->w_set_curswant = TRUE;
6934 if (ve_flags == VE_ALL)
6935 {
6936 int save_State = State;
6937
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006938 // Pretend Insert mode here to allow the cursor on the
6939 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006940 State = INSERT;
6941 coladvance((colnr_T)MAXCOL);
6942 State = save_State;
6943 }
6944 else
6945 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6946}
6947
6948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006950 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 */
6952 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006953nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006955 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6957 cap->cmdchar = 'i';
6958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006959 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006961 {
6962#ifdef FEAT_TERMINAL
6963 if (term_in_normal_mode())
6964 {
6965 end_visual_mode();
6966 clearop(cap->oap);
6967 term_enter_job_mode();
6968 return;
6969 }
6970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006974 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006975 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6976 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {
6978#ifdef FEAT_TEXTOBJ
6979 nv_object(cap);
6980#else
6981 clearopbeep(cap->oap);
6982#endif
6983 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006984#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006985 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006986 {
6987 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006988 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006989 return;
6990 }
6991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 else if (!curbuf->b_p_ma && !p_im)
6993 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006994 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006995 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01006997 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006998 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01006999 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007001 else if (cap->cmdchar == K_PS && VIsual_active)
7002 {
7003 pos_T old_pos = curwin->w_cursor;
7004 pos_T old_visual = VIsual;
7005
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007006 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007007 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7008 {
7009 shift_delete_registers();
7010 cap->oap->regname = '1';
7011 }
7012 else
7013 cap->oap->regname = '-';
7014 cap->cmdchar = 'd';
7015 cap->nchar = NUL;
7016 nv_operator(cap);
7017 do_pending_operator(cap, 0, FALSE);
7018 cap->cmdchar = K_PS;
7019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007020 // When the last char in the line was deleted then append. Detect this
7021 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007022 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7023 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007024 inc_cursor();
7025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007026 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007027 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 else if (!checkclearopq(cap->oap))
7030 {
7031 switch (cap->cmdchar)
7032 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007033 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007034 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 break;
7036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007037 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007038 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7039 beginline(BL_WHITE);
7040 else
7041 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 break;
7043
Bram Moolenaara1891842017-02-04 21:34:31 +01007044 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007045 // Bracketed paste works like "a"ppend, unless the cursor is in
7046 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007047 if (curwin->w_cursor.col == 0)
7048 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007049 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 case 'a': // "a"ppend is like "i"nsert on the next character.
7052 // increment coladd when in virtual space, increment the
7053 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 if (virtual_active()
7055 && (curwin->w_cursor.coladd > 0
7056 || *ml_get_cursor() == NUL
7057 || *ml_get_cursor() == TAB))
7058 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007059 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 inc_cursor();
7061 break;
7062 }
7063
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7065 {
7066 int save_State = State;
7067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 // Pretend Insert mode here to allow the cursor on the
7069 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 State = INSERT;
7071 coladvance(getviscol());
7072 State = save_State;
7073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
7075 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7076 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007077 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007078 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007079 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080}
7081
7082/*
7083 * Invoke edit() and take care of "restart_edit" and the return value.
7084 */
7085 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007086invoke_edit(
7087 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007089 int cmd,
7090 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
7092 int restart_edit_save = 0;
7093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7095 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7096 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 if (repl || !stuff_empty())
7098 restart_edit_save = restart_edit;
7099 else
7100 restart_edit_save = 0;
7101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007102 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 restart_edit = 0;
7104
7105 if (edit(cmd, startln, cap->count1))
7106 cap->retval |= CA_COMMAND_BUSY;
7107
7108 if (restart_edit == 0)
7109 restart_edit = restart_edit_save;
7110}
7111
7112#ifdef FEAT_TEXTOBJ
7113/*
7114 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7115 */
7116 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007117nv_object(
7118 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
7120 int flag;
7121 int include;
7122 char_u *mps_save;
7123
7124 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007125 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007127 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007129 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 mps_save = curbuf->b_p_mps;
7131 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7132
7133 switch (cap->nchar)
7134 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007135 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 flag = current_word(cap->oap, cap->count1, include, FALSE);
7137 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007138 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 flag = current_word(cap->oap, cap->count1, include, TRUE);
7140 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007141 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 case '(':
7143 case ')':
7144 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7145 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007146 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 case '{':
7148 case '}':
7149 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7150 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007151 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 case ']':
7153 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7154 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007155 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 case '>':
7157 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7158 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007159 case 't': // "at" = a tag block (xml and html)
7160 // Do not adjust oap->end in do_pending_operator()
7161 // otherwise there are different results for 'dit'
7162 // (note leading whitespace in last line):
7163 // 1) <b> 2) <b>
7164 // foobar foobar
7165 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007166 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007167 flag = current_tagblock(cap->oap, cap->count1, include);
7168 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007169 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 flag = current_par(cap->oap, cap->count1, include, 'p');
7171 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007172 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 flag = current_sent(cap->oap, cap->count1, include);
7174 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007175 case '"': // "a"" = a double quoted string
7176 case '\'': // "a'" = a single quoted string
7177 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007178 flag = current_quote(cap->oap, cap->count1, include,
7179 cap->nchar);
7180 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181#if 0 // TODO
7182 case 'S': // "aS" = a section
7183 case 'f': // "af" = a filename
7184 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185#endif
7186 default:
7187 flag = FAIL;
7188 break;
7189 }
7190
7191 curbuf->b_p_mps = mps_save;
7192 if (flag == FAIL)
7193 clearopbeep(cap->oap);
7194 adjust_cursor_col();
7195 curwin->w_set_curswant = TRUE;
7196}
7197#endif
7198
7199/*
7200 * "q" command: Start/stop recording.
7201 * "q:", "q/", "q?": edit command-line in command-line window.
7202 */
7203 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007204nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205{
7206 if (cap->oap->op_type == OP_FORMAT)
7207 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007208 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 cap->cmdchar = 'g';
7210 cap->nchar = 'q';
7211 nv_operator(cap);
7212 }
7213 else if (!checkclearop(cap->oap))
7214 {
7215#ifdef FEAT_CMDWIN
7216 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7217 {
7218 stuffcharReadbuff(cap->nchar);
7219 stuffcharReadbuff(K_CMDWIN);
7220 }
7221 else
7222#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007223 // (stop) recording into a named register, unless executing a
7224 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007225 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 clearopbeep(cap->oap);
7227 }
7228}
7229
7230/*
7231 * Handle the "@r" command.
7232 */
7233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007234nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
7236 if (checkclearop(cap->oap))
7237 return;
7238#ifdef FEAT_EVAL
7239 if (cap->nchar == '=')
7240 {
7241 if (get_expr_register() == NUL)
7242 return;
7243 }
7244#endif
7245 while (cap->count1-- && !got_int)
7246 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007247 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
7249 clearopbeep(cap->oap);
7250 break;
7251 }
7252 line_breakcheck();
7253 }
7254}
7255
7256/*
7257 * Handle the CTRL-U and CTRL-D commands.
7258 */
7259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007260nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
7262 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7263 || (cap->cmdchar == Ctrl_D
7264 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7265 clearopbeep(cap->oap);
7266 else if (!checkclearop(cap->oap))
7267 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7268}
7269
7270/*
7271 * Handle "J" or "gJ" command.
7272 */
7273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007274nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007276 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007278 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 {
7280 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007281 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7283 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007285 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007286 if (cap->count0 <= 2)
7287 {
7288 clearopbeep(cap->oap);
7289 return;
7290 }
7291 cap->count0 = curbuf->b_ml.ml_line_count
7292 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007294
7295 prep_redo(cap->oap->regname, cap->count0,
7296 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7297 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 }
7299}
7300
7301/*
7302 * "P", "gP", "p" and "gp" commands.
7303 */
7304 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007305nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007307 nv_put_opt(cap, FALSE);
7308}
7309
7310/*
7311 * "P", "gP", "p" and "gp" commands.
7312 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7313 */
7314 static void
7315nv_put_opt(cmdarg_T *cap, int fix_indent)
7316{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 int regname = 0;
7318 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007319 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007320 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 int dir;
7322 int flags = 0;
7323
7324 if (cap->oap->op_type != OP_NOP)
7325 {
7326#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007327 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7329 {
7330 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007331 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 }
7333 else
7334#endif
7335 clearopbeep(cap->oap);
7336 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007337#ifdef FEAT_JOB_CHANNEL
7338 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7339 {
7340 clearopbeep(cap->oap);
7341 }
7342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 else
7344 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007345 if (fix_indent)
7346 {
7347 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7348 ? FORWARD : BACKWARD;
7349 flags |= PUT_FIXINDENT;
7350 }
7351 else
7352 dir = (cap->cmdchar == 'P'
7353 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 ? BACKWARD : FORWARD;
7355 prep_redo_cmd(cap);
7356 if (cap->cmdchar == 'g')
7357 flags |= PUT_CURSEND;
7358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (VIsual_active)
7360 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007361 // Putting in Visual mode: The put text replaces the selected
7362 // text. First delete the selected text, then put the new text.
7363 // Need to save and restore the registers that the delete
7364 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007365 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007367#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007369#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007370 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007371 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007372#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376 )
7377 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007378 // The delete is going to overwrite the register we want to
7379 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 reg1 = get_register(regname, TRUE);
7381 }
7382
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007383 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 cap->cmdchar = 'd';
7385 cap->nchar = NUL;
7386 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007387 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 nv_operator(cap);
7389 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007390 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007391 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007393 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 cap->oap->regname = regname;
7395
7396 if (reg1 != NULL)
7397 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007398 // Delete probably changed the register we want to put, save
7399 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 reg2 = get_register(regname, FALSE);
7401 put_register(regname, reg1);
7402 }
7403
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007404 // When deleted a linewise Visual area, put the register as
7405 // lines to avoid it joined with the next line. When deletion was
7406 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 if (VIsual_mode == 'V')
7408 flags |= PUT_LINE;
7409 else if (VIsual_mode == 'v')
7410 flags |= PUT_LINE_SPLIT;
7411 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7412 flags |= PUT_LINE_FORWARD;
7413 dir = BACKWARD;
7414 if ((VIsual_mode != 'V'
7415 && curwin->w_cursor.col < curbuf->b_op_start.col)
7416 || (VIsual_mode == 'V'
7417 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007418 // cursor is at the end of the line or end of file, put
7419 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007421 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007422 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 do_put(cap->oap->regname, dir, cap->count1, flags);
7425
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007426 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 if (reg2 != NULL)
7428 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007430 // What to reselect with "gv"? Selecting the just put text seems to
7431 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007432 if (was_visual)
7433 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007434 curbuf->b_visual.vi_start = curbuf->b_op_start;
7435 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007436 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007437 if (*p_sel == 'e')
7438 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007439 }
7440
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007441 // When all lines were selected and deleted do_put() leaves an empty
7442 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007443 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007444 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007445 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007446 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007448 // If the cursor was in that line, move it to the end of the last
7449 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007450 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7451 {
7452 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7453 coladvance((colnr_T)MAXCOL);
7454 }
7455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 auto_format(FALSE, TRUE);
7457 }
7458}
7459
7460/*
7461 * "o" and "O" commands.
7462 */
7463 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007464nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465{
7466#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007467 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7469 {
7470 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007471 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 }
7473 else
7474#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007475 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007477#ifdef FEAT_JOB_CHANNEL
7478 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007479 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 n_opencmd(cap);
7483}
7484
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485#ifdef FEAT_NETBEANS_INTG
7486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007487nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488{
7489 netbeans_keycommand(cap->nchar);
7490}
7491#endif
7492
7493#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007495nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
7497 do_put('~', BACKWARD, 1L, PUT_CURSEND);
7498}
7499#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007500
Bram Moolenaar3918c952005-03-15 22:34:55 +00007501/*
7502 * Trigger CursorHold event.
7503 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7504 * input buffer. "did_cursorhold" is set to avoid retriggering.
7505 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007507nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007508{
7509 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7510 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007511 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007512}