blob: 87f1956fa9ed5e3e8612ae4339e3684ae99c5689 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010020#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010022#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020023static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
29/*
30 * nv_*(): functions called to handle Normal and Visual mode commands.
31 * n_*(): functions called to handle Normal mode commands.
32 * v_*(): functions called to handle Visual mode commands.
33 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void nv_ignore(cmdarg_T *cap);
35static void nv_nop(cmdarg_T *cap);
36static void nv_error(cmdarg_T *cap);
37static void nv_help(cmdarg_T *cap);
38static void nv_addsub(cmdarg_T *cap);
39static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_ver_scrollbar(cmdarg_T *cap);
43static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000045#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void nv_tabline(cmdarg_T *cap);
47static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000048#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static void nv_exmode(cmdarg_T *cap);
50static void nv_colon(cmdarg_T *cap);
51static void nv_ctrlg(cmdarg_T *cap);
52static void nv_ctrlh(cmdarg_T *cap);
53static void nv_clear(cmdarg_T *cap);
54static void nv_ctrlo(cmdarg_T *cap);
55static void nv_hat(cmdarg_T *cap);
56static void nv_Zet(cmdarg_T *cap);
57static void nv_ident(cmdarg_T *cap);
58static void nv_tagpop(cmdarg_T *cap);
59static void nv_scroll(cmdarg_T *cap);
60static void nv_right(cmdarg_T *cap);
61static void nv_left(cmdarg_T *cap);
62static void nv_up(cmdarg_T *cap);
63static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void nv_end(cmdarg_T *cap);
65static void nv_dollar(cmdarg_T *cap);
66static void nv_search(cmdarg_T *cap);
67static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020068static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_csearch(cmdarg_T *cap);
70static void nv_brackets(cmdarg_T *cap);
71static void nv_percent(cmdarg_T *cap);
72static void nv_brace(cmdarg_T *cap);
73static void nv_mark(cmdarg_T *cap);
74static void nv_findpar(cmdarg_T *cap);
75static void nv_undo(cmdarg_T *cap);
76static void nv_kundo(cmdarg_T *cap);
77static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
80static void v_visop(cmdarg_T *cap);
81static void nv_subst(cmdarg_T *cap);
82static void nv_abbrev(cmdarg_T *cap);
83static void nv_optrans(cmdarg_T *cap);
84static void nv_gomark(cmdarg_T *cap);
85static void nv_pcmark(cmdarg_T *cap);
86static void nv_regname(cmdarg_T *cap);
87static void nv_visual(cmdarg_T *cap);
88static void n_start_visual_mode(int c);
89static void nv_window(cmdarg_T *cap);
90static void nv_suspend(cmdarg_T *cap);
91static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_dot(cmdarg_T *cap);
93static void nv_redo(cmdarg_T *cap);
94static void nv_Undo(cmdarg_T *cap);
95static void nv_tilde(cmdarg_T *cap);
96static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_lineop(cmdarg_T *cap);
101static void nv_home(cmdarg_T *cap);
102static void nv_pipe(cmdarg_T *cap);
103static void nv_bck_word(cmdarg_T *cap);
104static void nv_wordcmd(cmdarg_T *cap);
105static void nv_beginline(cmdarg_T *cap);
106static void adjust_cursor(oparg_T *oap);
107static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100108static void nv_select(cmdarg_T *cap);
109static void nv_goto(cmdarg_T *cap);
110static void nv_normal(cmdarg_T *cap);
111static void nv_esc(cmdarg_T *oap);
112static void nv_edit(cmdarg_T *cap);
113static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_record(cmdarg_T *cap);
118static void nv_at(cmdarg_T *cap);
119static void nv_halfpage(cmdarg_T *cap);
120static void nv_join(cmdarg_T *cap);
121static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200122static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100130static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000132static char *e_noident = N_("E349: No identifier under cursor");
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
135 * Function to be called for a Normal or Visual mode command.
136 * The argument is a cmdarg_T.
137 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100138typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100140// Values for cmd_flags.
141#define NV_NCH 0x01 // may need to get a second char
142#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
143#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
144#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100146#define NV_SS 0x10 // may start selection
147#define NV_SSS 0x20 // may start selection with shift modifier
148#define NV_STS 0x40 // may stop selection without shift modif.
149#define NV_RL 0x80 // 'rightleft' modifies command
150#define NV_KEEPREG 0x100 // don't clear regname
151#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153/*
154 * Generally speaking, every Normal mode command should either clear any
155 * pending operator (with *clearop*()), or set the motion type variable
156 * oap->motion_type.
157 *
158 * When a cursor motion command is made, it is marked as being a character or
159 * line oriented motion. Then, if an operator is in effect, the operation
160 * becomes character or line oriented accordingly.
161 */
162
163/*
164 * This table contains one entry for every Normal or Visual mode command.
165 * The order doesn't matter, init_normal_cmds() will create a sorted index.
166 * It is faster when all keys from zero to '~' are present.
167 */
168static const struct nv_cmd
169{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100170 int cmd_char; // (first) command character
171 nv_func_T cmd_func; // function for this command
172 short_u cmd_flags; // NV_ flags
173 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174} nv_cmds[] =
175{
176 {NUL, nv_error, 0, 0},
177 {Ctrl_A, nv_addsub, 0, 0},
178 {Ctrl_B, nv_page, NV_STS, BACKWARD},
179 {Ctrl_C, nv_esc, 0, TRUE},
180 {Ctrl_D, nv_halfpage, 0, 0},
181 {Ctrl_E, nv_scroll_line, 0, TRUE},
182 {Ctrl_F, nv_page, NV_STS, FORWARD},
183 {Ctrl_G, nv_ctrlg, 0, 0},
184 {Ctrl_H, nv_ctrlh, 0, 0},
185 {Ctrl_I, nv_pcmark, 0, 0},
186 {NL, nv_down, 0, FALSE},
187 {Ctrl_K, nv_error, 0, 0},
188 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100189 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {Ctrl_N, nv_down, NV_STS, FALSE},
191 {Ctrl_O, nv_ctrlo, 0, 0},
192 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000193 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {Ctrl_R, nv_redo, 0, 0},
195 {Ctrl_S, nv_ignore, 0, 0},
196 {Ctrl_T, nv_tagpop, NV_NCW, 0},
197 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_V, nv_visual, 0, FALSE},
199 {'V', nv_visual, 0, FALSE},
200 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {Ctrl_W, nv_window, 0, 0},
202 {Ctrl_X, nv_addsub, 0, 0},
203 {Ctrl_Y, nv_scroll_line, 0, FALSE},
204 {Ctrl_Z, nv_suspend, 0, 0},
205 {ESC, nv_esc, 0, FALSE},
206 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
207 {Ctrl_RSB, nv_ident, NV_NCW, 0},
208 {Ctrl_HAT, nv_hat, NV_NCW, 0},
209 {Ctrl__, nv_error, 0, 0},
210 {' ', nv_right, 0, 0},
211 {'!', nv_operator, 0, 0},
212 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
213 {'#', nv_ident, 0, 0},
214 {'$', nv_dollar, 0, 0},
215 {'%', nv_percent, 0, 0},
216 {'&', nv_optrans, 0, 0},
217 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
218 {'(', nv_brace, 0, BACKWARD},
219 {')', nv_brace, 0, FORWARD},
220 {'*', nv_ident, 0, 0},
221 {'+', nv_down, 0, TRUE},
222 {',', nv_csearch, 0, TRUE},
223 {'-', nv_up, 0, TRUE},
224 {'.', nv_dot, NV_KEEPREG, 0},
225 {'/', nv_search, 0, FALSE},
226 {'0', nv_beginline, 0, 0},
227 {'1', nv_ignore, 0, 0},
228 {'2', nv_ignore, 0, 0},
229 {'3', nv_ignore, 0, 0},
230 {'4', nv_ignore, 0, 0},
231 {'5', nv_ignore, 0, 0},
232 {'6', nv_ignore, 0, 0},
233 {'7', nv_ignore, 0, 0},
234 {'8', nv_ignore, 0, 0},
235 {'9', nv_ignore, 0, 0},
236 {':', nv_colon, 0, 0},
237 {';', nv_csearch, 0, FALSE},
238 {'<', nv_operator, NV_RL, 0},
239 {'=', nv_operator, 0, 0},
240 {'>', nv_operator, NV_RL, 0},
241 {'?', nv_search, 0, FALSE},
242 {'@', nv_at, NV_NCH_NOP, FALSE},
243 {'A', nv_edit, 0, 0},
244 {'B', nv_bck_word, 0, 1},
245 {'C', nv_abbrev, NV_KEEPREG, 0},
246 {'D', nv_abbrev, NV_KEEPREG, 0},
247 {'E', nv_wordcmd, 0, TRUE},
248 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
249 {'G', nv_goto, 0, TRUE},
250 {'H', nv_scroll, 0, 0},
251 {'I', nv_edit, 0, 0},
252 {'J', nv_join, 0, 0},
253 {'K', nv_ident, 0, 0},
254 {'L', nv_scroll, 0, 0},
255 {'M', nv_scroll, 0, 0},
256 {'N', nv_next, 0, SEARCH_REV},
257 {'O', nv_open, 0, 0},
258 {'P', nv_put, 0, 0},
259 {'Q', nv_exmode, NV_NCW, 0},
260 {'R', nv_Replace, 0, FALSE},
261 {'S', nv_subst, NV_KEEPREG, 0},
262 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
263 {'U', nv_Undo, 0, 0},
264 {'W', nv_wordcmd, 0, TRUE},
265 {'X', nv_abbrev, NV_KEEPREG, 0},
266 {'Y', nv_abbrev, NV_KEEPREG, 0},
267 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
268 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
269 {'\\', nv_error, 0, 0},
270 {']', nv_brackets, NV_NCH_ALW, FORWARD},
271 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
272 {'_', nv_lineop, 0, 0},
273 {'`', nv_gomark, NV_NCH_ALW, FALSE},
274 {'a', nv_edit, NV_NCH, 0},
275 {'b', nv_bck_word, 0, 0},
276 {'c', nv_operator, 0, 0},
277 {'d', nv_operator, 0, 0},
278 {'e', nv_wordcmd, 0, FALSE},
279 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
280 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
281 {'h', nv_left, NV_RL, 0},
282 {'i', nv_edit, NV_NCH, 0},
283 {'j', nv_down, 0, FALSE},
284 {'k', nv_up, 0, FALSE},
285 {'l', nv_right, NV_RL, 0},
286 {'m', nv_mark, NV_NCH_NOP, 0},
287 {'n', nv_next, 0, 0},
288 {'o', nv_open, 0, 0},
289 {'p', nv_put, 0, 0},
290 {'q', nv_record, NV_NCH, 0},
291 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
292 {'s', nv_subst, NV_KEEPREG, 0},
293 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
294 {'u', nv_undo, 0, 0},
295 {'w', nv_wordcmd, 0, FALSE},
296 {'x', nv_abbrev, NV_KEEPREG, 0},
297 {'y', nv_operator, 0, 0},
298 {'z', nv_zet, NV_NCH_ALW, 0},
299 {'{', nv_findpar, 0, BACKWARD},
300 {'|', nv_pipe, 0, 0},
301 {'}', nv_findpar, 0, FORWARD},
302 {'~', nv_tilde, 0, 0},
303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100304 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200306 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
307 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
308 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
309 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 {K_LEFTMOUSE, nv_mouse, 0, 0},
311 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
312 {K_LEFTDRAG, nv_mouse, 0, 0},
313 {K_LEFTRELEASE, nv_mouse, 0, 0},
314 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100315 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
317 {K_MIDDLEDRAG, nv_mouse, 0, 0},
318 {K_MIDDLERELEASE, nv_mouse, 0, 0},
319 {K_RIGHTMOUSE, nv_mouse, 0, 0},
320 {K_RIGHTDRAG, nv_mouse, 0, 0},
321 {K_RIGHTRELEASE, nv_mouse, 0, 0},
322 {K_X1MOUSE, nv_mouse, 0, 0},
323 {K_X1DRAG, nv_mouse, 0, 0},
324 {K_X1RELEASE, nv_mouse, 0, 0},
325 {K_X2MOUSE, nv_mouse, 0, 0},
326 {K_X2DRAG, nv_mouse, 0, 0},
327 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000328 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000329 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {K_INS, nv_edit, 0, 0},
331 {K_KINS, nv_edit, 0, 0},
332 {K_BS, nv_ctrlh, 0, 0},
333 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
334 {K_S_UP, nv_page, NV_SS, BACKWARD},
335 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
336 {K_S_DOWN, nv_page, NV_SS, FORWARD},
337 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
339 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
340 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
341 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
342 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
343 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
344 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
345 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
346 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
347 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
348 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_S_END, nv_end, NV_SS, FALSE},
350 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
351 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
352 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {K_S_HOME, nv_home, NV_SS, 0},
354 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
355 {K_DEL, nv_abbrev, 0, 0},
356 {K_KDEL, nv_abbrev, 0, 0},
357 {K_UNDO, nv_kundo, 0, 0},
358 {K_HELP, nv_help, NV_NCW, 0},
359 {K_F1, nv_help, NV_NCW, 0},
360 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifdef FEAT_GUI
363 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
364 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
365#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#ifdef FEAT_GUI_TABLINE
367 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000368 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#ifdef FEAT_NETBEANS_INTG
371 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
372#endif
373#ifdef FEAT_DND
374 {K_DROP, nv_drop, NV_STS, 0},
375#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100377 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100378 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379};
380
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100381// Number of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
383
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100384// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static short nv_cmd_idx[NV_CMDS_SIZE];
386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387// The highest index for which
388// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static int nv_max_linear;
390
391/*
392 * Compare functions for qsort() below, that checks the command character
393 * through the index in nv_cmd_idx[].
394 */
395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100396nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
398 int c1, c2;
399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100400 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 c1 = nv_cmds[*(const short *)s1].cmd_char;
402 c2 = nv_cmds[*(const short *)s2].cmd_char;
403 if (c1 < 0)
404 c1 = -c1;
405 if (c2 < 0)
406 c2 = -c2;
407 return c1 - c2;
408}
409
410/*
411 * Initialize the nv_cmd_idx[] table.
412 */
413 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100414init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
416 int i;
417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000419 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 nv_cmd_idx[i] = i;
421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100425 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000426 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
428 break;
429 nv_max_linear = i - 1;
430}
431
432/*
433 * Search for a command in the commands table.
434 * Returns -1 for invalid command.
435 */
436 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100437find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 int i;
440 int idx;
441 int top, bot;
442 int c;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 if (cmdchar >= 0x100)
446 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100448 // We use the absolute value of the character. Special keys have a
449 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (cmdchar < 0)
451 cmdchar = -cmdchar;
452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100453 // If the character is in the first part: The character is the index into
454 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (cmdchar <= nv_max_linear)
456 return nv_cmd_idx[cmdchar];
457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100458 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 bot = nv_max_linear + 1;
460 top = NV_CMDS_SIZE - 1;
461 idx = -1;
462 while (bot <= top)
463 {
464 i = (top + bot) / 2;
465 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
466 if (c < 0)
467 c = -c;
468 if (cmdchar == c)
469 {
470 idx = nv_cmd_idx[i];
471 break;
472 }
473 if (cmdchar > c)
474 bot = i + 1;
475 else
476 top = i - 1;
477 }
478 return idx;
479}
480
481/*
482 * Execute a command in Normal mode.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100485normal_cmd(
486 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100489 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 int old_col = curwin->w_curswant;
493#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 static int old_mapped_len = 0;
499 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000500#ifdef FEAT_EVAL
501 int set_prevcount = FALSE;
502#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200503 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaara80faa82020-04-12 19:37:17 +0200505 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 // Use a count remembered from before entering an operator. After typing
509 // "3d" we return from normal_cmd() and come back here, the "3" is
510 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 ca.opcount = opcount;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 /*
514 * If there is an operator pending, then the command we take this time
515 * will terminate it. Finish_op tells us to finish the operation before
516 * returning this time (unless the operation was cancelled).
517 */
518#ifdef CURSOR_SHAPE
519 c = finish_op;
520#endif
521 finish_op = (oap->op_type != OP_NOP);
522#ifdef CURSOR_SHAPE
523 if (finish_op != c)
524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100525 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# ifdef FEAT_MOUSESHAPE
527 update_mouseshape(-1);
528# endif
529 }
530#endif
531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 // When not finishing an operator and no register name typed, reset the
533 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000537#ifdef FEAT_EVAL
538 set_prevcount = TRUE;
539#endif
540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 // Restore counts from before receiving K_CURSORHOLD. This means after
543 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
544 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000545 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
546 {
547 ca.opcount = oap->prev_opcount;
548 ca.count0 = oap->prev_count0;
549 oap->prev_opcount = 0;
550 oap->prev_count0 = 0;
551 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 State = NORMAL_BUSY;
556#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100560#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 // Set v:count here, when called from main() and not a stuffed
562 // command, so that v:count can be used in an expression mapping
563 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100564 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100565 set_vcount_ca(&ca, &set_prevcount);
566#endif
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /*
569 * Get the command character from the user.
570 */
571 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100572 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 /*
575 * If a mapping was started in Visual or Select mode, remember the length
576 * of the mapping. This is used below to not return to Insert mode for as
577 * long as the mapping is being executed.
578 */
579 if (restart_edit == 0)
580 old_mapped_len = 0;
581 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000582 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 old_mapped_len = typebuf_maplen();
584
585 if (c == NUL)
586 c = K_ZERO;
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 /*
589 * In Select mode, typed text replaces the selection.
590 */
591 if (VIsual_active
592 && VIsual_select
593 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100595 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
596 // 'insertmode' is set) fake a "d"elete command, Insert mode will
597 // restart automatically.
598 // Insert the typed character in the typeahead buffer, so that it can
599 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200600 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000601 if (restart_edit != 0)
602 c = 'd';
603 else
604 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 msg_nowait = TRUE; // don't delay going to insert mode
606 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609#ifdef FEAT_CMDL_INFO
610 need_flushbuf = add_to_showcmd(c);
611#endif
612
613getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 /*
617 * Handle a count before a command and compute ca.count0.
618 * Note that '0' is a command and not the start of a count, but it's
619 * part of a count after other digits.
620 */
621 while ( (c >= '1' && c <= '9')
622 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
623 {
624 if (c == K_DEL || c == K_KDEL)
625 {
626 ca.count0 /= 10;
627#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100628 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#endif
630 }
631 else
632 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100633 if (ca.count0 < 0) // got too large!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000635#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100636 // Set v:count here, when called from main() and not a stuffed
637 // command, so that v:count can be used in an expression mapping
638 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100639 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100640 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (ctrl_w)
643 {
644 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100645 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100647 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000648 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 --no_zero_mapping;
651 if (ctrl_w)
652 {
653 --no_mapping;
654 --allow_keys;
655 }
656#ifdef FEAT_CMDL_INFO
657 need_flushbuf |= add_to_showcmd(c);
658#endif
659 }
660
661 /*
662 * If we got CTRL-W there may be a/another count
663 */
664 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
665 {
666 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100667 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 ca.count0 = 0;
669 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100670 ++allow_keys; // no mapping for nchar, but keys
671 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 --no_mapping;
674 --allow_keys;
675#ifdef FEAT_CMDL_INFO
676 need_flushbuf |= add_to_showcmd(c);
677#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100678 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 }
680 }
681
Bram Moolenaara983fe92008-07-31 20:04:27 +0000682 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 // Save the count values so that ca.opcount and ca.count0 are exactly
685 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000686 oap->prev_opcount = ca.opcount;
687 oap->prev_count0 = ca.count0;
688 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100689 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000690 {
691 /*
692 * If we're in the middle of an operator (including after entering a
693 * yank buffer with '"') AND we had a count before the operator, then
694 * that count overrides the current value of ca.count0.
695 * What this means effectively, is that commands like "3dw" get turned
696 * into "d3w" which makes things fall into place pretty neatly.
697 * If you give a count before AND after the operator, they are
698 * multiplied.
699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (ca.count0)
701 ca.count0 *= ca.opcount;
702 else
703 ca.count0 = ca.opcount;
704 }
705
706 /*
707 * Always remember the count. It will be set to zero (on the next call,
708 * above) when there is no pending operator.
709 * When called from main(), save the count for use by the "count" built-in
710 * variable.
711 */
712 ca.opcount = ca.count0;
713 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
714
715#ifdef FEAT_EVAL
716 /*
717 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100718 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100720 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000721 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#endif
723
724 /*
725 * Find the command character in the table of commands.
726 * For CTRL-W we already got nchar when looking for a count.
727 */
728 if (ctrl_w)
729 {
730 ca.nchar = c;
731 ca.cmdchar = Ctrl_W;
732 }
733 else
734 ca.cmdchar = c;
735 idx = find_command(ca.cmdchar);
736 if (idx < 0)
737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100738 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 clearopbeep(oap);
740 goto normal_end;
741 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000742
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000743 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100745 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000747 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 goto normal_end;
749 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000750 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
751 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 /*
754 * In Visual/Select mode, a few keys are handled in a special way.
755 */
756 if (VIsual_active)
757 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100758 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 if (km_stopsel
760 && (nv_cmds[idx].cmd_flags & NV_STS)
761 && !(mod_mask & MOD_MASK_SHIFT))
762 {
763 end_visual_mode();
764 redraw_curbuf_later(INVERTED);
765 }
766
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100767 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (km_startsel)
769 {
770 if (nv_cmds[idx].cmd_flags & NV_SS)
771 {
772 unshift_special(&ca);
773 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000774 if (idx < 0)
775 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100776 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000777 clearopbeep(oap);
778 goto normal_end;
779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
782 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787#ifdef FEAT_RIGHTLEFT
788 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
789 && (nv_cmds[idx].cmd_flags & NV_RL))
790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100791 // Invert horizontal movements and operations. Only when typed by the
792 // user directly, not when the result of a mapping or "x" translated
793 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 switch (ca.cmdchar)
795 {
796 case 'l': ca.cmdchar = 'h'; break;
797 case K_RIGHT: ca.cmdchar = K_LEFT; break;
798 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
799 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
800 case 'h': ca.cmdchar = 'l'; break;
801 case K_LEFT: ca.cmdchar = K_RIGHT; break;
802 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
803 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
804 case '>': ca.cmdchar = '<'; break;
805 case '<': ca.cmdchar = '>'; break;
806 }
807 idx = find_command(ca.cmdchar);
808 }
809#endif
810
811 /*
812 * Get an additional character if we need one.
813 */
814 if ((nv_cmds[idx].cmd_flags & NV_NCH)
815 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
816 && oap->op_type == OP_NOP)
817 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
818 || (ca.cmdchar == 'q'
819 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200820 && reg_recording == 0
821 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100823 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 {
825 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100826 int repl = FALSE; // get character for replace mode
827 int lit = FALSE; // get extra character literally
828 int langmap_active = FALSE; // using :lmap mappings
829 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100830#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100831 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#endif
833
834 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100835 ++allow_keys; // no mapping for nchar, but allow key codes
836 // Don't generate a CursorHold event here, most commands can't handle
837 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000838 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (ca.cmdchar == 'g')
840 {
841 /*
842 * For 'g' get the next character now, so that we can check for
843 * "gr", "g'" and "g`".
844 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000845 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifdef FEAT_CMDL_INFO
848 need_flushbuf |= add_to_showcmd(ca.nchar);
849#endif
850 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100851 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100855 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100860 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 }
862 else
863 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 repl = TRUE;
866 cp = &ca.nchar;
867 }
868 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
869
870 /*
871 * Get a second or third character.
872 */
873 if (cp != NULL)
874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (repl)
876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100877 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100878#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100879 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
883 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100884 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 --no_mapping;
886 --allow_keys;
887 if (repl)
888 State = LREPLACE;
889 else
890 State = LANGMAP;
891 langmap_active = TRUE;
892 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100893#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100895 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
897 im_set_active(TRUE);
898#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200899 if ((State & INSERT) && !p_ek)
900 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200901#ifdef FEAT_JOB_CHANNEL
902 ch_log_output = TRUE;
903#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200904 // Disable bracketed paste and modifyOtherKeys here, we won't
905 // recognize the escape sequences with 'esckeys' off.
906 out_str(T_BD);
907 out_str(T_CTE);
908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000910 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaarca774f62020-08-30 20:46:38 +0200912 if ((State & INSERT) && !p_ek)
913 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200914#ifdef FEAT_JOB_CHANNEL
915 ch_log_output = TRUE;
916#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200917 // Re-enable bracketed paste mode and modifyOtherKeys
918 out_str(T_BE);
919 out_str(T_CTI);
920 }
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (langmap_active)
923 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100924 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 ++no_mapping;
926 ++allow_keys;
927 State = NORMAL_BUSY;
928 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100929#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (lang)
931 {
932 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
933 im_save_status(&curbuf->b_p_iminsert);
934 im_set_active(FALSE);
935 }
936 p_smd = save_smd;
937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939#ifdef FEAT_CMDL_INFO
940 need_flushbuf |= add_to_showcmd(*cp);
941#endif
942
943 if (!lit)
944 {
945#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100946 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (*cp == Ctrl_K
948 && ((nv_cmds[idx].cmd_flags & NV_LANG)
949 || cp == &ca.extra_char)
950 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
951 {
952 c = get_digraph(FALSE);
953 if (c > 0)
954 {
955 *cp = c;
956# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100957 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 del_from_showcmd(3);
959 need_flushbuf |= add_to_showcmd(*cp);
960# endif
961 }
962 }
963#endif
964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100965 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100968 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (p_hkmap && lang && KeyTyped)
970 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#endif
972 }
973
974 /*
975 * When the next character is CTRL-\ a following CTRL-N means the
976 * command is aborted and we go to Normal mode.
977 */
978 if (cp == &ca.extra_char
979 && ca.nchar == Ctrl_BSL
980 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
981 {
982 ca.cmdchar = Ctrl_BSL;
983 ca.nchar = ca.extra_char;
984 idx = find_command(ca.cmdchar);
985 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200986 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200987 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 else if (*cp == Ctrl_BSL)
989 {
990 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100992 // There is a busy wait here when typing "f<C-\>" and then
993 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 while ((c = vpeekc()) <= 0 && towait > 0L)
995 {
996 do_sleep(towait > 50L ? 50L : towait);
997 towait -= 50L;
998 }
999 if (c > 0)
1000 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001001 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (c != Ctrl_N && c != Ctrl_G)
1003 vungetc(c);
1004 else
1005 {
1006 ca.cmdchar = Ctrl_BSL;
1007 ca.nchar = c;
1008 idx = find_command(ca.cmdchar);
1009 }
1010 }
1011 }
1012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001013 // When getting a text character and the next character is a
1014 // multi-byte character, it could be a composing character.
1015 // However, don't wait for it to arrive. Also, do enable mapping,
1016 // because if it's put back with vungetc() it's too late to apply
1017 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001018 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 while (enc_utf8 && lang && (c = vpeekc()) > 0
1020 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1021 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001022 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (!utf_iscomposing(c))
1024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001025 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 break;
1027 }
1028 else if (ca.ncharC1 == 0)
1029 ca.ncharC1 = c;
1030 else
1031 ca.ncharC2 = c;
1032 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001033 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035 --no_mapping;
1036 --allow_keys;
1037 }
1038
1039#ifdef FEAT_CMDL_INFO
1040 /*
1041 * Flush the showcmd characters onto the screen so we can see them while
1042 * the command is being executed. Only do this when the shown command was
1043 * actually displayed, otherwise this will slow down a lot when executing
1044 * mappings.
1045 */
1046 if (need_flushbuf)
1047 out_flush();
1048#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001049 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001050 {
1051 if (ex_normal_busy)
1052 did_cursorhold = save_did_cursorhold;
1053 else
1054 did_cursorhold = FALSE;
1055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
1057 State = NORMAL;
1058
1059 if (ca.nchar == ESC)
1060 {
1061 clearop(oap);
1062 if (restart_edit == 0 && goto_im())
1063 restart_edit = 'a';
1064 goto normal_end;
1065 }
1066
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001067 if (ca.cmdchar != K_IGNORE)
1068 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001069 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001070 msg_col = 0;
1071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001073 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001075 // When 'keymodel' contains "startsel" some keys start Select/Visual
1076 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (!VIsual_active && km_startsel)
1078 {
1079 if (nv_cmds[idx].cmd_flags & NV_SS)
1080 {
1081 start_selection();
1082 unshift_special(&ca);
1083 idx = find_command(ca.cmdchar);
1084 }
1085 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1086 && (mod_mask & MOD_MASK_SHIFT))
1087 {
1088 start_selection();
1089 mod_mask &= ~MOD_MASK_SHIFT;
1090 }
1091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
1093 /*
1094 * Execute the command!
1095 * Call the command function found in the commands table.
1096 */
1097 ca.arg = nv_cmds[idx].cmd_arg;
1098 (nv_cmds[idx].cmd_func)(&ca);
1099
1100 /*
1101 * If we didn't start or finish an operator, reset oap->regname, unless we
1102 * need it later.
1103 */
1104 if (!finish_op
1105 && !oap->op_type
1106 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1107 {
1108 clearop(oap);
1109#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001110 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#endif
1112 }
1113
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001114 // Get the length of mapped chars again after typing a count, second
1115 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001116 if (old_mapped_len > 0)
1117 old_mapped_len = typebuf_maplen();
1118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 /*
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001120 * If an operation is pending, handle it. But not for K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001122 if (ca.cmdchar != K_IGNORE)
1123 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
1125 /*
1126 * Wait for a moment when a message is displayed that will be overwritten
1127 * by the mode message.
1128 * In Visual mode and with "^O" in Insert mode, a short message will be
1129 * overwritten by the mode message. Wait a bit, until a key is hit.
1130 * In Visual mode, it's more important to keep the Visual area updated
1131 * than keeping a message (e.g. from a /pat search).
1132 * Only do this if the command was typed, not from a mapping.
1133 * Don't wait when emsg_silent is non-zero.
1134 * Also wait a bit after an error message, e.g. for "^O:".
1135 * Don't redraw the screen, it would remove the message.
1136 */
1137 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001138 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 || (VIsual_active
1141 && old_pos.lnum == curwin->w_cursor.lnum
1142 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 )
1144 && (clear_cmdline
1145 || redraw_cmdline)
1146 && (msg_didout || (msg_didany && msg_scroll))
1147 && !msg_nowait
1148 && KeyTyped)
1149 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 && (msg_scroll
1152 || emsg_on_display)))
1153 && oap->regname == 0
1154 && !(ca.retval & CA_COMMAND_BUSY)
1155 && stuff_empty()
1156 && typebuf_typed()
1157 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001158 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 && !did_wait_return
1160 && oap->op_type == OP_NOP)
1161 {
1162 int save_State = State;
1163
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001164 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (restart_edit != 0)
1166 State = INSERT;
1167
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001168 // If need to redraw, and there is a "keep_msg", redraw before the
1169 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1171 {
1172 char_u *kmsg;
1173
1174 kmsg = keep_msg;
1175 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001176 // Showmode() will clear keep_msg, but we want to use it anyway.
1177 // First update w_topline.
1178 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001180 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001182
1183 kmsg = vim_strsave(keep_msg);
1184 if (kmsg != NULL)
1185 {
1186 msg_attr((char *)kmsg, keep_msg_attr);
1187 vim_free(kmsg);
1188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001191#ifdef CURSOR_SHAPE
1192 ui_cursor_shape(); // may show different cursor shape
1193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 cursor_on();
1195 out_flush();
1196 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001197 ui_delay(1003L, TRUE); // wait at least one second
1198 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 State = save_State;
1200
1201 msg_scroll = FALSE;
1202 emsg_on_display = FALSE;
1203 }
1204
1205 /*
1206 * Finish up after executing a Normal mode command.
1207 */
1208normal_end:
1209
1210 msg_nowait = FALSE;
1211
Bram Moolenaarcc613032020-06-07 21:31:18 +02001212#ifdef FEAT_EVAL
1213 if (finish_op)
1214 reset_reg_var();
1215#endif
1216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001217 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218#ifdef CURSOR_SHAPE
1219 c = finish_op;
1220#endif
1221 finish_op = FALSE;
1222#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001223 // Redraw the cursor with another shape, if we were in Operator-pending
1224 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 if (c || ca.cmdchar == 'r')
1226 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001227 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228# ifdef FEAT_MOUSESHAPE
1229 update_mouseshape(-1);
1230# endif
1231 }
1232#endif
1233
1234#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001235 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001236 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 clear_showcmd();
1238#endif
1239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 vim_free(ca.searchbuf);
1242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 if (has_mbyte)
1244 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (curwin->w_p_scb && toplevel)
1247 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001248 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 do_check_scrollbind(TRUE);
1250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaar860cae12010-06-05 23:22:07 +02001252 if (curwin->w_p_crb && toplevel)
1253 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001255 do_check_cursorbind();
1256 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001257
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001258#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001259 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001260 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001261 restart_edit = 0;
1262#endif
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 /*
1265 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1266 * if still inside a mapping that started in Visual mode).
1267 * May switch from Visual to Select mode after CTRL-O command.
1268 */
1269 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1271 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && !(ca.retval & CA_COMMAND_BUSY)
1273 && stuff_empty()
1274 && oap->regname == 0)
1275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 if (restart_VIsual_select == 1)
1277 {
1278 VIsual_select = TRUE;
1279 showmode();
1280 restart_VIsual_select = 0;
1281 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001282 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 (void)edit(restart_edit, FALSE, 1L);
1284 }
1285
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (restart_VIsual_select == 2)
1287 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001289 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 opcount = ca.opcount;
1291}
1292
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001293#ifdef FEAT_EVAL
1294/*
1295 * Set v:count and v:count1 according to "cap".
1296 * Set v:prevcount only when "set_prevcount" is TRUE.
1297 */
1298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001299set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001300{
1301 long count = cap->count0;
1302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001303 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001304 if (cap->opcount != 0)
1305 count = cap->opcount * (count == 0 ? 1 : count);
1306 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001307 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001308}
1309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001312 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * if not.
1314 */
1315 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001316check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
1318 static int did_check = FALSE;
1319
1320 if (full_screen)
1321 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001322 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001323 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 did_check = TRUE;
1325 }
1326}
1327
1328/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001329 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * This function should ALWAYS be called to end Visual mode, except from
1331 * do_pending_operator().
1332 */
1333 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001334end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336#ifdef FEAT_CLIPBOARD
1337 /*
1338 * If we are using the clipboard, then remember what was selected in case
1339 * we need to paste it somewhere while we still own the selection.
1340 * Only do this when the clipboard is already owned. Don't want to grab
1341 * the selection when hitting ESC.
1342 */
1343 if (clip_star.available && clip_star.owned)
1344 clip_auto_select();
1345#endif
1346
1347 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 setmouse();
1349 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001351 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001352 curbuf->b_visual.vi_mode = VIsual_mode;
1353 curbuf->b_visual.vi_start = VIsual;
1354 curbuf->b_visual.vi_end = curwin->w_cursor;
1355 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#ifdef FEAT_EVAL
1357 curbuf->b_visual_mode_eval = VIsual_mode;
1358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 if (!virtual_active())
1360 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001361 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001363 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364}
1365
1366/*
1367 * Reset VIsual_active and VIsual_reselect.
1368 */
1369 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001370reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
1372 if (VIsual_active)
1373 {
1374 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001375 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377 VIsual_reselect = FALSE;
1378}
1379
1380/*
1381 * Reset VIsual_active and VIsual_reselect if it's set.
1382 */
1383 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001384reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
1386 if (VIsual_active)
1387 {
1388 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001389 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 VIsual_reselect = FALSE;
1391 }
1392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001394 void
1395restore_visual_mode(void)
1396{
1397 if (VIsual_mode_orig != NUL)
1398 {
1399 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1400 VIsual_mode_orig = NUL;
1401 }
1402}
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404/*
1405 * Check for a balloon-eval special item to include when searching for an
1406 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1407 * Returns TRUE if the character at "*ptr" should be included.
1408 * "dir" is FORWARD or BACKWARD, the direction of searching.
1409 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1410 * "bnp" points to a counter for square brackets.
1411 */
1412 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001413find_is_eval_item(
1414 char_u *ptr,
1415 int *colp,
1416 int *bnp,
1417 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001419 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1421 ++*bnp;
1422 if (*bnp > 0)
1423 {
1424 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1425 --*bnp;
1426 return TRUE;
1427 }
1428
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001429 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 if (*ptr == '.')
1431 return TRUE;
1432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001433 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1435 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1436 {
1437 *colp += dir;
1438 return TRUE;
1439 }
1440 return FALSE;
1441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443/*
1444 * Find the identifier under or to the right of the cursor.
1445 * "find_type" can have one of three values:
1446 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001447 * FIND_STRING: find any non-white text
1448 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001449 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 *
1451 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001452 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001454 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 * This doesn't match the real Vi but I like it a little better and it
1456 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001457 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 * When FIND_IDENT isn't defined, we backup until a blank.
1459 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001460 * Returns the length of the text, or zero if no text is found.
1461 * If text is found, a pointer to the text is put in "*text". This
1462 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
1464 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001465find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001468 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469}
1470
1471/*
1472 * Like find_ident_under_cursor(), but for any window and any position.
1473 * However: Uses 'iskeyword' from the current window!.
1474 */
1475 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001476find_ident_at_pos(
1477 win_T *wp,
1478 linenr_T lnum,
1479 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001480 char_u **text,
1481 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001482 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001485 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 int this_class = 0;
1488 int prev_class;
1489 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001490 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 /*
1493 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001494 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1497 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1498 {
1499 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001500 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 if (has_mbyte)
1504 {
1505 while (ptr[col] != NUL)
1506 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001507 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1509 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 this_class = mb_get_class(ptr + col);
1511 if (this_class != 0 && (i == 1 || this_class != 1))
1512 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001513 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 }
1515 }
1516 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001518 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 )
1521 ++col;
1522
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001523 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
1526 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001527 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (has_mbyte)
1530 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001531 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1533 this_class = mb_get_class((char_u *)"a");
1534 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001536 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
1538 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1539 prev_class = mb_get_class(ptr + prevcol);
1540 if (this_class != prev_class
1541 && (i == 0
1542 || prev_class == 0
1543 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 && (!(find_type & FIND_EVAL)
1545 || prevcol == 0
1546 || !find_is_eval_item(ptr + prevcol, &prevcol,
1547 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 )
1549 break;
1550 col = prevcol;
1551 }
1552
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001553 // If we don't want just any old text, or we've found an
1554 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 if (this_class > 2)
1556 this_class = 2;
1557 if (!(find_type & FIND_STRING) || this_class == 2)
1558 break;
1559 }
1560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562 while (col > 0
1563 && ((i == 0
1564 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001565 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 && (!(find_type & FIND_IDENT)
1567 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 || ((find_type & FIND_EVAL)
1569 && col > 1
1570 && find_is_eval_item(ptr + col - 1, &col,
1571 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 ))
1573 --col;
1574
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001575 // If we don't want just any old text, or we've found an
1576 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1578 break;
1579 }
1580 }
1581
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001582 if (ptr[col] == NUL || (i == 0
1583 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001585 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001586 if ((find_type & FIND_NOERROR) == 0)
1587 {
1588 if (find_type & FIND_STRING)
1589 emsg(_("E348: No string under cursor"));
1590 else
1591 emsg(_(e_noident));
1592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 return 0;
1594 }
1595 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001596 *text = ptr;
1597 if (textcol != NULL)
1598 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
1600 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001601 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 bn = 0;
1604 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (has_mbyte)
1607 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001608 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 this_class = mb_get_class(ptr);
1610 while (ptr[col] != NUL
1611 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1612 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 || ((find_type & FIND_EVAL)
1614 && col <= (int)startcol
1615 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001617 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 }
1619 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001621 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 || ((find_type & FIND_EVAL)
1623 && col <= (int)startcol
1624 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628 return col;
1629}
1630
1631/*
1632 * Prepare for redo of a normal command.
1633 */
1634 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001635prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 prep_redo(cap->oap->regname, cap->count0,
1638 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1639}
1640
1641/*
1642 * Prepare for redo of any command.
1643 * Note that only the last argument can be a multi-byte char.
1644 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001645 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001646prep_redo(
1647 int regname,
1648 long num,
1649 int cmd1,
1650 int cmd2,
1651 int cmd3,
1652 int cmd4,
1653 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654{
1655 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001656 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 {
1658 AppendCharToRedobuff('"');
1659 AppendCharToRedobuff(regname);
1660 }
1661 if (num)
1662 AppendNumberToRedobuff(num);
1663
1664 if (cmd1 != NUL)
1665 AppendCharToRedobuff(cmd1);
1666 if (cmd2 != NUL)
1667 AppendCharToRedobuff(cmd2);
1668 if (cmd3 != NUL)
1669 AppendCharToRedobuff(cmd3);
1670 if (cmd4 != NUL)
1671 AppendCharToRedobuff(cmd4);
1672 if (cmd5 != NUL)
1673 AppendCharToRedobuff(cmd5);
1674}
1675
1676/*
1677 * check for operator active and clear it
1678 *
1679 * return TRUE if operator was active
1680 */
1681 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001682checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 if (oap->op_type == OP_NOP)
1685 return FALSE;
1686 clearopbeep(oap);
1687 return TRUE;
1688}
1689
1690/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001691 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001693 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 */
1695 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001696checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001698 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 return FALSE;
1700 clearopbeep(oap);
1701 return TRUE;
1702}
1703
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001704 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001705clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706{
1707 oap->op_type = OP_NOP;
1708 oap->regname = 0;
1709 oap->motion_force = NUL;
1710 oap->use_reg_one = FALSE;
1711}
1712
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001714clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 clearop(oap);
1717 beep_flush();
1718}
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720/*
1721 * Remove the shift modifier from a special key.
1722 */
1723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001724unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 switch (cap->cmdchar)
1727 {
1728 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1729 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1730 case K_S_UP: cap->cmdchar = K_UP; break;
1731 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1732 case K_S_HOME: cap->cmdchar = K_HOME; break;
1733 case K_S_END: cap->cmdchar = K_END; break;
1734 }
1735 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001738/*
1739 * If the mode is currently displayed clear the command line or update the
1740 * command displayed.
1741 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001742 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001743may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001744{
1745 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001746 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001747#ifdef FEAT_CMDL_INFO
1748 else
1749 clear_showcmd();
1750#endif
1751}
1752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1754/*
1755 * Routines for displaying a partly typed command
1756 */
1757
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001758#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001760static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761static int showcmd_is_clear = TRUE;
1762static int showcmd_visual = FALSE;
1763
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001764static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001767clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 if (!p_sc)
1770 return;
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 if (VIsual_active && !char_avail())
1773 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001774 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 long lines;
1776 colnr_T leftcol, rightcol;
1777 linenr_T top, bot;
1778
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001779 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001780 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 {
1782 top = VIsual.lnum;
1783 bot = curwin->w_cursor.lnum;
1784 }
1785 else
1786 {
1787 top = curwin->w_cursor.lnum;
1788 bot = VIsual.lnum;
1789 }
1790# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001791 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001792 (void)hasFolding(top, &top, NULL);
1793 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794# endif
1795 lines = bot - top + 1;
1796
1797 if (VIsual_mode == Ctrl_V)
1798 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001799# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001800 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001801 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001802
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001803 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001804 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001805 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001808# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001809 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001810 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1813 (long)(rightcol - leftcol + 1));
1814 }
1815 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1816 sprintf((char *)showcmd_buf, "%ld", lines);
1817 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001818 {
1819 char_u *s, *e;
1820 int l;
1821 int bytes = 0;
1822 int chars = 0;
1823
1824 if (cursor_bot)
1825 {
1826 s = ml_get_pos(&VIsual);
1827 e = ml_get_cursor();
1828 }
1829 else
1830 {
1831 s = ml_get_cursor();
1832 e = ml_get_pos(&VIsual);
1833 }
1834 while ((*p_sel != 'e') ? s <= e : s < e)
1835 {
1836 l = (*mb_ptr2len)(s);
1837 if (l == 0)
1838 {
1839 ++bytes;
1840 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001841 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001842 }
1843 bytes += l;
1844 ++chars;
1845 s += l;
1846 }
1847 if (bytes == chars)
1848 sprintf((char *)showcmd_buf, "%d", chars);
1849 else
1850 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1851 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001852 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 showcmd_visual = TRUE;
1854 }
1855 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
1857 showcmd_buf[0] = NUL;
1858 showcmd_visual = FALSE;
1859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001860 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (showcmd_is_clear)
1862 return;
1863 }
1864
1865 display_showcmd();
1866}
1867
1868/*
1869 * Add 'c' to string of shown command chars.
1870 * Return TRUE if output has been written (and setcursor() has been called).
1871 */
1872 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001873add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874{
1875 char_u *p;
1876 int old_len;
1877 int extra_len;
1878 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 int i;
1880 static int ignore[] =
1881 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001882#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1884 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001885#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001886 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001887 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1889 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001890 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001892 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 0
1894 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
Bram Moolenaar09df3122006-01-23 22:23:09 +00001896 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 return FALSE;
1898
1899 if (showcmd_visual)
1900 {
1901 showcmd_buf[0] = NUL;
1902 showcmd_visual = FALSE;
1903 }
1904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001905 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (IS_SPECIAL(c))
1907 for (i = 0; ignore[i] != 0; ++i)
1908 if (ignore[i] == c)
1909 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
1911 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001912 if (*p == ' ')
1913 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 old_len = (int)STRLEN(showcmd_buf);
1915 extra_len = (int)STRLEN(p);
1916 overflow = old_len + extra_len - SHOWCMD_COLS;
1917 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001918 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1919 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 STRCAT(showcmd_buf, p);
1921
1922 if (char_avail())
1923 return FALSE;
1924
1925 display_showcmd();
1926
1927 return TRUE;
1928}
1929
1930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001931add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
1933 if (!add_to_showcmd(c))
1934 setcursor();
1935}
1936
1937/*
1938 * Delete 'len' characters from the end of the shown command.
1939 */
1940 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001941del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942{
1943 int old_len;
1944
1945 if (!p_sc)
1946 return;
1947
1948 old_len = (int)STRLEN(showcmd_buf);
1949 if (len > old_len)
1950 len = old_len;
1951 showcmd_buf[old_len - len] = NUL;
1952
1953 if (!char_avail())
1954 display_showcmd();
1955}
1956
1957/*
1958 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1959 * something and there is a partial mapping.
1960 */
1961 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001962push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
1964 if (p_sc)
1965 STRCPY(old_showcmd_buf, showcmd_buf);
1966}
1967
1968 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001969pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
1971 if (!p_sc)
1972 return;
1973
1974 STRCPY(showcmd_buf, old_showcmd_buf);
1975
1976 display_showcmd();
1977}
1978
1979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001980display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982 int len;
1983
1984 cursor_off();
1985
1986 len = (int)STRLEN(showcmd_buf);
1987 if (len == 0)
1988 showcmd_is_clear = TRUE;
1989 else
1990 {
1991 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1992 showcmd_is_clear = FALSE;
1993 }
1994
1995 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001996 * clear the rest of an old message by outputting up to SHOWCMD_COLS
1997 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 */
1999 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002001 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002}
2003#endif
2004
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005/*
2006 * When "check" is FALSE, prepare for commands that scroll the window.
2007 * When "check" is TRUE, take care of scroll-binding after the window has
2008 * scrolled. Called from normal_cmd() and edit().
2009 */
2010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002011do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 static win_T *old_curwin = NULL;
2014 static linenr_T old_topline = 0;
2015#ifdef FEAT_DIFF
2016 static int old_topfill = 0;
2017#endif
2018 static buf_T *old_buf = NULL;
2019 static colnr_T old_leftcol = 0;
2020
2021 if (check && curwin->w_p_scb)
2022 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002023 // If a ":syncbind" command was just used, don't scroll, only reset
2024 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (did_syncbind)
2026 did_syncbind = FALSE;
2027 else if (curwin == old_curwin)
2028 {
2029 /*
2030 * Synchronize other windows, as necessary according to
2031 * 'scrollbind'. Don't do this after an ":edit" command, except
2032 * when 'diff' is set.
2033 */
2034 if ((curwin->w_buffer == old_buf
2035#ifdef FEAT_DIFF
2036 || curwin->w_p_diff
2037#endif
2038 )
2039 && (curwin->w_topline != old_topline
2040#ifdef FEAT_DIFF
2041 || curwin->w_topfill != old_topfill
2042#endif
2043 || curwin->w_leftcol != old_leftcol))
2044 {
2045 check_scrollbind(curwin->w_topline - old_topline,
2046 (long)(curwin->w_leftcol - old_leftcol));
2047 }
2048 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002049 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
2051 /*
2052 * When switching between windows, make sure that the relative
2053 * vertical offset is valid for the new window. The relative
2054 * offset is invalid whenever another 'scrollbind' window has
2055 * scrolled to a point that would force the current window to
2056 * scroll past the beginning or end of its buffer. When the
2057 * resync is performed, some of the other 'scrollbind' windows may
2058 * need to jump so that the current window's relative position is
2059 * visible on-screen.
2060 */
2061 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2062 }
2063 curwin->w_scbind_pos = curwin->w_topline;
2064 }
2065
2066 old_curwin = curwin;
2067 old_topline = curwin->w_topline;
2068#ifdef FEAT_DIFF
2069 old_topfill = curwin->w_topfill;
2070#endif
2071 old_buf = curwin->w_buffer;
2072 old_leftcol = curwin->w_leftcol;
2073}
2074
2075/*
2076 * Synchronize any windows that have "scrollbind" set, based on the
2077 * number of rows by which the current window has changed
2078 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2079 */
2080 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002081check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083 int want_ver;
2084 int want_hor;
2085 win_T *old_curwin = curwin;
2086 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 int old_VIsual_select = VIsual_select;
2088 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 colnr_T tgt_leftcol = curwin->w_leftcol;
2090 long topline;
2091 long y;
2092
2093 /*
2094 * check 'scrollopt' string for vertical and horizontal scroll options
2095 */
2096 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2097#ifdef FEAT_DIFF
2098 want_ver |= old_curwin->w_p_diff;
2099#endif
2100 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2101
2102 /*
2103 * loop through the scrollbound windows and scroll accordingly
2104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002106 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
2108 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002109 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 if (curwin != old_curwin && curwin->w_p_scb)
2111 {
2112 /*
2113 * do the vertical scroll
2114 */
2115 if (want_ver)
2116 {
2117#ifdef FEAT_DIFF
2118 if (old_curwin->w_p_diff && curwin->w_p_diff)
2119 {
2120 diff_set_topline(old_curwin, curwin);
2121 }
2122 else
2123#endif
2124 {
2125 curwin->w_scbind_pos += topline_diff;
2126 topline = curwin->w_scbind_pos;
2127 if (topline > curbuf->b_ml.ml_line_count)
2128 topline = curbuf->b_ml.ml_line_count;
2129 if (topline < 1)
2130 topline = 1;
2131
2132 y = topline - curwin->w_topline;
2133 if (y > 0)
2134 scrollup(y, FALSE);
2135 else
2136 scrolldown(-y, FALSE);
2137 }
2138
2139 redraw_later(VALID);
2140 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143
2144 /*
2145 * do the horizontal scroll
2146 */
2147 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2148 {
2149 curwin->w_leftcol = tgt_leftcol;
2150 leftcol_changed();
2151 }
2152 }
2153 }
2154
2155 /*
2156 * reset current-window
2157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 VIsual_select = old_VIsual_select;
2159 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 curwin = old_curwin;
2161 curbuf = old_curbuf;
2162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163
2164/*
2165 * Command character that's ignored.
2166 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002167 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002170nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002172 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173}
2174
2175/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002176 * Command character that doesn't do anything, but unlike nv_ignore() does
2177 * start edit(). Used for "startinsert" executed while starting up.
2178 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002179 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002180nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002181{
2182}
2183
2184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 * Command character doesn't exist.
2186 */
2187 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002188nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
2190 clearopbeep(cap->oap);
2191}
2192
2193/*
2194 * <Help> and <F1> commands.
2195 */
2196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002197nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198{
2199 if (!checkclearopq(cap->oap))
2200 ex_help(NULL);
2201}
2202
2203/*
2204 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2205 */
2206 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002207nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002209#ifdef FEAT_JOB_CHANNEL
2210 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2211 clearopbeep(cap->oap);
2212 else
2213#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002214 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002215 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002216 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002217 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2218 op_addsub(cap->oap, cap->count1, cap->arg);
2219 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002220 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002221 else if (VIsual_active)
2222 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002223 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002224 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225}
2226
2227/*
2228 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2229 */
2230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002231nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002234 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002235 if (mod_mask & MOD_MASK_CTRL)
2236 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002237 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238 if (cap->arg == BACKWARD)
2239 goto_tabpage(-(int)cap->count1);
2240 else
2241 goto_tabpage((int)cap->count0);
2242 }
2243 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002244 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246}
2247
2248/*
2249 * Implementation of "gd" and "gD" command.
2250 */
2251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002252nv_gd(
2253 oparg_T *oap,
2254 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002255 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 char_u *ptr;
2259
Bram Moolenaard9d30582005-05-18 22:10:28 +00002260 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002261 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2262 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002264#ifdef FEAT_FOLDING
2265 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2266 foldOpenCursor();
2267#endif
2268}
2269
2270/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002271 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2272 * otherwise.
2273 */
2274 static int
2275is_ident(char_u *line, int offset)
2276{
2277 int i;
2278 int incomment = FALSE;
2279 int instring = 0;
2280 int prev = 0;
2281
2282 for (i = 0; i < offset && line[i] != NUL; i++)
2283 {
2284 if (instring != 0)
2285 {
2286 if (prev != '\\' && line[i] == instring)
2287 instring = 0;
2288 }
2289 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2290 {
2291 instring = line[i];
2292 }
2293 else
2294 {
2295 if (incomment)
2296 {
2297 if (prev == '*' && line[i] == '/')
2298 incomment = FALSE;
2299 }
2300 else if (prev == '/' && line[i] == '*')
2301 {
2302 incomment = TRUE;
2303 }
2304 else if (prev == '/' && line[i] == '/')
2305 {
2306 return FALSE;
2307 }
2308 }
2309
2310 prev = line[i];
2311 }
2312
2313 return incomment == FALSE && instring == 0;
2314}
2315
2316/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002317 * Search for variable declaration of "ptr[len]".
2318 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2319 * current file ("gD").
2320 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002321 * Return FAIL when not found.
2322 */
2323 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002324find_decl(
2325 char_u *ptr,
2326 int len,
2327 int locally,
2328 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002329 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002330{
2331 char_u *pat;
2332 pos_T old_pos;
2333 pos_T par_pos;
2334 pos_T found_pos;
2335 int t;
2336 int save_p_ws;
2337 int save_p_scs;
2338 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002339 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002340 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002341 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002342
2343 if ((pat = alloc(len + 7)) == NULL)
2344 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002345
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002346 // Put "\V" before the pattern to avoid that the special meaning of "."
2347 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002348 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2349 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 old_pos = curwin->w_cursor;
2351 save_p_ws = p_ws;
2352 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002353 p_ws = FALSE; // don't wrap around end of file now
2354 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356 /*
2357 * With "gD" go to line 1.
2358 * With "gd" Search back for the start of the current function, then go
2359 * back until a blank line. If this fails go to line 1.
2360 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002361 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002363 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002365 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367 else
2368 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002369 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2371 --curwin->w_cursor.lnum;
2372 }
2373 curwin->w_cursor.col = 0;
2374
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002375 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002376 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002377 for (;;)
2378 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002379 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002380 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002381 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002382 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002383
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002384 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002385 {
2386 pos_T *pos;
2387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 // Check that the block the match is in doesn't end before the
2389 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002390 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2391 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2392 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002393 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002394 // There can't be a useful match before the end of this block.
2395 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002396 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002397 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002398 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002399 }
2400
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002401 if (t == FAIL)
2402 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002403 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002404 if (found_pos.lnum != 0)
2405 {
2406 curwin->w_cursor = found_pos;
2407 t = OK;
2408 }
2409 break;
2410 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002411 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002412 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002413 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002414 ++curwin->w_cursor.lnum;
2415 curwin->w_cursor.col = 0;
2416 continue;
2417 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002418 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2419
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002420 // If the current position is not a valid identifier and a previous
2421 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002422 if (!valid && found_pos.lnum != 0)
2423 {
2424 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002425 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002426 }
2427
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002428 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002429 if (valid && !locally)
2430 break;
2431 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002432 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002433 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002434 if (found_pos.lnum != 0)
2435 curwin->w_cursor = found_pos;
2436 break;
2437 }
2438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002439 // For finding a local variable and the match is before the "{" or
2440 // inside a comment, continue searching. For K&R style function
2441 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002442 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002443 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002444 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002445 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002446 // Remove SEARCH_START from flags to avoid getting stuck at one
2447 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002448 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002450
2451 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002453 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 curwin->w_cursor = old_pos;
2455 }
2456 else
2457 {
2458 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002459 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 reset_search_dir();
2461 }
2462
2463 vim_free(pat);
2464 p_ws = save_p_ws;
2465 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002466
2467 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468}
2469
2470/*
2471 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2472 * lines rather than lines in the file.
2473 * 'dist' must be positive.
2474 *
2475 * Return OK if able to move cursor, FAIL otherwise.
2476 */
2477 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002478nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
2480 int linelen = linetabsize(ml_get_curline());
2481 int retval = OK;
2482 int atend = FALSE;
2483 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002484 int col_off1; // margin offset for first screen line
2485 int col_off2; // margin offset for wrapped screen line
2486 int width1; // text width for first screen line
2487 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002490 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
2492 col_off1 = curwin_col_off();
2493 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002494 width1 = curwin->w_width - col_off1;
2495 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002496 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002497 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /*
2502 * Instead of sticking at the last character of the buffer line we
2503 * try to stick in the last column of the screen.
2504 */
2505 if (curwin->w_curswant == MAXCOL)
2506 {
2507 atend = TRUE;
2508 validate_virtcol();
2509 if (width1 <= 0)
2510 curwin->w_curswant = 0;
2511 else
2512 {
2513 curwin->w_curswant = width1 - 1;
2514 if (curwin->w_virtcol > curwin->w_curswant)
2515 curwin->w_curswant += ((curwin->w_virtcol
2516 - curwin->w_curswant - 1) / width2 + 1) * width2;
2517 }
2518 }
2519 else
2520 {
2521 if (linelen > width1)
2522 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2523 else
2524 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002525 if (curwin->w_curswant >= (colnr_T)n)
2526 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528
2529 while (dist--)
2530 {
2531 if (dir == BACKWARD)
2532 {
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002533 if ((long)curwin->w_curswant >= width1)
2534 // Move back within the line. This can give a negative value
2535 // for w_curswant if width1 < width2 (with cpoptions+=n),
2536 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 curwin->w_curswant -= width2;
2538 else
2539 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002540 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (curwin->w_cursor.lnum == 1)
2542 {
2543 retval = FAIL;
2544 break;
2545 }
2546 --curwin->w_cursor.lnum;
2547#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002548 // Move to the start of a closed fold. Don't do that when
2549 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 if (!(fdo_flags & FDO_ALL))
2551 (void)hasFolding(curwin->w_cursor.lnum,
2552 &curwin->w_cursor.lnum, NULL);
2553#endif
2554 linelen = linetabsize(ml_get_curline());
2555 if (linelen > width1)
2556 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2557 + 1) * width2;
2558 }
2559 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002560 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
2562 if (linelen > width1)
2563 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2564 else
2565 n = width1;
2566 if (curwin->w_curswant + width2 < (colnr_T)n)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002567 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 curwin->w_curswant += width2;
2569 else
2570 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002571 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002573 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2575 &curwin->w_cursor.lnum);
2576#endif
2577 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2578 {
2579 retval = FAIL;
2580 break;
2581 }
2582 curwin->w_cursor.lnum++;
2583 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002584 // Check if the cursor has moved below the number display
2585 // when width1 < width2 (with cpoptions+=n). Subtract width2
2586 // to get a negative value for w_curswant, which will get
2587 // clipped to column 0.
2588 if (curwin->w_curswant >= width1)
2589 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002590 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592 }
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002596 if (virtual_active() && atend)
2597 coladvance(MAXCOL);
2598 else
2599 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2602 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002603 colnr_T virtcol;
2604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 /*
2606 * Check for landing on a character that got split at the end of the
2607 * last line. We want to advance a screenline, not end up in the same
2608 * screenline or move two screenlines.
2609 */
2610 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002611 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002612#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002613 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2614 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002615#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002616
2617 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 && (curwin->w_curswant < (colnr_T)width1
2619 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2620 : ((curwin->w_curswant - width1) % width2
2621 > (colnr_T)width2 / 2)))
2622 --curwin->w_cursor.col;
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002626 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
2628 return retval;
2629}
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631/*
2632 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2633 * cap->arg must be TRUE for CTRL-E.
2634 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002635 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002636nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 if (!checkclearop(cap->oap))
2639 scroll_redraw(cap->arg, cap->count1);
2640}
2641
2642/*
2643 * Scroll "count" lines up or down, and redraw.
2644 */
2645 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002646scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
2648 linenr_T prev_topline = curwin->w_topline;
2649#ifdef FEAT_DIFF
2650 int prev_topfill = curwin->w_topfill;
2651#endif
2652 linenr_T prev_lnum = curwin->w_cursor.lnum;
2653
2654 if (up)
2655 scrollup(count, TRUE);
2656 else
2657 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002658 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002660 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2661 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 cursor_correct();
2663 check_cursor_moved(curwin);
2664 curwin->w_valid |= VALID_TOPLINE;
2665
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // If moved back to where we were, at least move the cursor, otherwise
2667 // we get stuck at one position. Don't move the cursor up if the
2668 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 while (curwin->w_topline == prev_topline
2670#ifdef FEAT_DIFF
2671 && curwin->w_topfill == prev_topfill
2672#endif
2673 )
2674 {
2675 if (up)
2676 {
2677 if (curwin->w_cursor.lnum > prev_lnum
2678 || cursor_down(1L, FALSE) == FAIL)
2679 break;
2680 }
2681 else
2682 {
2683 if (curwin->w_cursor.lnum < prev_lnum
2684 || prev_topline == 1L
2685 || cursor_up(1L, FALSE) == FAIL)
2686 break;
2687 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002688 // Mark w_topline as valid, otherwise the screen jumps back at the
2689 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 check_cursor_moved(curwin);
2691 curwin->w_valid |= VALID_TOPLINE;
2692 }
2693 }
2694 if (curwin->w_cursor.lnum != prev_lnum)
2695 coladvance(curwin->w_curswant);
2696 redraw_later(VALID);
2697}
2698
2699/*
2700 * Commands that start with "z".
2701 */
2702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002703nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 long n;
2706 colnr_T col;
2707 int nchar = cap->nchar;
2708#ifdef FEAT_FOLDING
2709 long old_fdl = curwin->w_p_fdl;
2710 int old_fen = curwin->w_p_fen;
2711#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002712#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002713 int undo = FALSE;
2714#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002715 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 if (VIM_ISDIGIT(nchar))
2718 {
2719 /*
2720 * "z123{nchar}": edit the count before obtaining {nchar}
2721 */
2722 if (checkclearop(cap->oap))
2723 return;
2724 n = nchar - '0';
2725 for (;;)
2726 {
2727#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002728 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729#endif
2730 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002732 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 --no_mapping;
2735 --allow_keys;
2736#ifdef FEAT_CMDL_INFO
2737 (void)add_to_showcmd(nchar);
2738#endif
2739 if (nchar == K_DEL || nchar == K_KDEL)
2740 n /= 10;
2741 else if (VIM_ISDIGIT(nchar))
2742 n = n * 10 + (nchar - '0');
2743 else if (nchar == CAR)
2744 {
2745#ifdef FEAT_GUI
2746 need_mouse_correct = TRUE;
2747#endif
2748 win_setheight((int)n);
2749 break;
2750 }
2751 else if (nchar == 'l'
2752 || nchar == 'h'
2753 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002754 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
2756 cap->count1 = n ? n * cap->count1 : cap->count1;
2757 goto dozet;
2758 }
2759 else
2760 {
2761 clearopbeep(cap->oap);
2762 break;
2763 }
2764 }
2765 cap->oap->op_type = OP_NOP;
2766 return;
2767 }
2768
2769dozet:
2770 if (
2771#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002772 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2773 // and "zC" only in Visual mode. "zj" and "zk" are motion
2774 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 cap->nchar != 'f' && cap->nchar != 'F'
2776 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2777 && cap->nchar != 'j' && cap->nchar != 'k'
2778 &&
2779#endif
2780 checkclearop(cap->oap))
2781 return;
2782
2783 /*
2784 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2785 * If line number given, set cursor.
2786 */
2787 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2788 && cap->count0
2789 && cap->count0 != curwin->w_cursor.lnum)
2790 {
2791 setpcmark();
2792 if (cap->count0 > curbuf->b_ml.ml_line_count)
2793 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2794 else
2795 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002796 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798
2799 switch (nchar)
2800 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002801 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 case '+':
2803 if (cap->count0 == 0)
2804 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002805 // No count given: put cursor at the line below screen
2806 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2808 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2809 else
2810 curwin->w_cursor.lnum = curwin->w_botline;
2811 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002812 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 case NL:
2814 case CAR:
2815 case K_KENTER:
2816 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002817 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818
2819 case 't': scroll_cursor_top(0, TRUE);
2820 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002821 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 break;
2823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002826 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
2828 case 'z': scroll_cursor_halfway(TRUE);
2829 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002830 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 break;
2832
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002833 // "z^", "z-" and "zb": put cursor at bottom of screen
2834 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2835 // when <count> is at bottom of window, and puts that one at
2836 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 if (cap->count0 != 0)
2838 {
2839 scroll_cursor_bot(0, TRUE);
2840 curwin->w_cursor.lnum = curwin->w_topline;
2841 }
2842 else if (curwin->w_topline == 1)
2843 curwin->w_cursor.lnum = 1;
2844 else
2845 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002846 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 case '-':
2848 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002849 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 case 'b': scroll_cursor_bot(0, TRUE);
2852 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002853 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 break;
2855
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002858 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002859 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002861 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 case 'h':
2863 case K_LEFT:
2864 if (!curwin->w_p_wrap)
2865 {
2866 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2867 curwin->w_leftcol = 0;
2868 else
2869 curwin->w_leftcol -= (colnr_T)cap->count1;
2870 leftcol_changed();
2871 }
2872 break;
2873
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002874 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002875 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 case 'l':
2880 case K_RIGHT:
2881 if (!curwin->w_p_wrap)
2882 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002883 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 curwin->w_leftcol += (colnr_T)cap->count1;
2885 leftcol_changed();
2886 }
2887 break;
2888
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002889 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 case 's': if (!curwin->w_p_wrap)
2891 {
2892#ifdef FEAT_FOLDING
2893 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002894 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 else
2896#endif
2897 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002898 if ((long)col > siso)
2899 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 else
2901 col = 0;
2902 if (curwin->w_leftcol != col)
2903 {
2904 curwin->w_leftcol = col;
2905 redraw_later(NOT_VALID);
2906 }
2907 }
2908 break;
2909
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 case 'e': if (!curwin->w_p_wrap)
2912 {
2913#ifdef FEAT_FOLDING
2914 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002915 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 else
2917#endif
2918 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002919 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002920 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 col = 0;
2922 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002923 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 if (curwin->w_leftcol != col)
2925 {
2926 curwin->w_leftcol = col;
2927 redraw_later(NOT_VALID);
2928 }
2929 }
2930 break;
2931
2932#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 // "zF": create fold command
2934 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 case 'F':
2936 case 'f': if (foldManualAllowed(TRUE))
2937 {
2938 cap->nchar = 'f';
2939 nv_operator(cap);
2940 curwin->w_p_fen = TRUE;
2941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2944 {
2945 nv_operator(cap);
2946 finish_op = TRUE;
2947 }
2948 }
2949 else
2950 clearopbeep(cap->oap);
2951 break;
2952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002953 // "zd": delete fold at cursor
2954 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 case 'd':
2956 case 'D': if (foldManualAllowed(FALSE))
2957 {
2958 if (VIsual_active)
2959 nv_operator(cap);
2960 else
2961 deleteFold(curwin->w_cursor.lnum,
2962 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2963 }
2964 break;
2965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 case 'E': if (foldmethodIsManual(curwin))
2968 {
2969 clearFolding(curwin);
2970 changed_window_setting();
2971 }
2972 else if (foldmethodIsMarker(curwin))
2973 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2974 TRUE, FALSE);
2975 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002976 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 break;
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 case 'n': curwin->w_p_fen = FALSE;
2981 break;
2982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 case 'N': curwin->w_p_fen = TRUE;
2985 break;
2986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2989 break;
2990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2993 openFold(curwin->w_cursor.lnum, cap->count1);
2994 else
2995 {
2996 closeFold(curwin->w_cursor.lnum, cap->count1);
2997 curwin->w_p_fen = TRUE;
2998 }
2999 break;
3000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003001 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3003 openFoldRecurse(curwin->w_cursor.lnum);
3004 else
3005 {
3006 closeFoldRecurse(curwin->w_cursor.lnum);
3007 curwin->w_p_fen = TRUE;
3008 }
3009 break;
3010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003011 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 case 'o': if (VIsual_active)
3013 nv_operator(cap);
3014 else
3015 openFold(curwin->w_cursor.lnum, cap->count1);
3016 break;
3017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003018 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 case 'O': if (VIsual_active)
3020 nv_operator(cap);
3021 else
3022 openFoldRecurse(curwin->w_cursor.lnum);
3023 break;
3024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 case 'c': if (VIsual_active)
3027 nv_operator(cap);
3028 else
3029 closeFold(curwin->w_cursor.lnum, cap->count1);
3030 curwin->w_p_fen = TRUE;
3031 break;
3032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 case 'C': if (VIsual_active)
3035 nv_operator(cap);
3036 else
3037 closeFoldRecurse(curwin->w_cursor.lnum);
3038 curwin->w_p_fen = TRUE;
3039 break;
3040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003041 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 case 'v': foldOpenCursor();
3043 break;
3044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003045 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003047 curwin->w_foldinvalid = TRUE; // recompute folds
3048 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 foldOpenCursor();
3050 break;
3051
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003052 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003054 curwin->w_foldinvalid = TRUE; // recompute folds
3055 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 break;
3057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003058 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003060 {
3061 curwin->w_p_fdl -= cap->count1;
3062 if (curwin->w_p_fdl < 0)
3063 curwin->w_p_fdl = 0;
3064 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003065 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 curwin->w_p_fen = TRUE;
3067 break;
3068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003071 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 curwin->w_p_fen = TRUE;
3073 break;
3074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003075 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003076 case 'r': curwin->w_p_fdl += cap->count1;
3077 {
3078 int d = getDeepestNesting();
3079
3080 if (curwin->w_p_fdl >= d)
3081 curwin->w_p_fdl = d;
3082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 break;
3084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003087 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 break;
3089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 case 'j': // "zj" move to next fold downwards
3091 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3093 cap->count1) == FAIL)
3094 clearopbeep(cap->oap);
3095 break;
3096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003097#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003099#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003100 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003101 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003103 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003104 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003105 --no_mapping;
3106 --allow_keys;
3107#ifdef FEAT_CMDL_INFO
3108 (void)add_to_showcmd(nchar);
3109#endif
3110 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3111 {
3112 clearopbeep(cap->oap);
3113 break;
3114 }
3115 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003116 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003118 case 'g': // "zg": add good word to word list
3119 case 'w': // "zw": add wrong word to word list
3120 case 'G': // "zG": add good word to temp word list
3121 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003122 {
3123 char_u *ptr = NULL;
3124 int len;
3125
3126 if (checkclearop(cap->oap))
3127 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003128 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3129 == FAIL)
3130 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003131 if (ptr == NULL)
3132 {
3133 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003135 // Find bad word under the cursor. When 'spell' is
3136 // off this fails and find_ident_under_cursor() is
3137 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003138 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003139 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003140 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003141 if (len != 0 && curwin->w_cursor.col <= pos.col)
3142 ptr = ml_get_pos(&curwin->w_cursor);
3143 curwin->w_cursor = pos;
3144 }
3145
Bram Moolenaarb765d632005-06-07 21:00:02 +00003146 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3147 FIND_IDENT)) == 0)
3148 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003149 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3150 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003151 (nchar == 'G' || nchar == 'W')
3152 ? 0 : (int)cap->count1,
3153 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003154 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003155 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003157 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003158 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003159 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003160 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003161#endif
3162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 default: clearopbeep(cap->oap);
3164 }
3165
3166#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 if (old_fen != curwin->w_p_fen)
3169 {
3170# ifdef FEAT_DIFF
3171 win_T *wp;
3172
3173 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3174 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003175 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 FOR_ALL_WINDOWS(wp)
3177 {
3178 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3179 {
3180 wp->w_p_fen = curwin->w_p_fen;
3181 changed_window_setting_win(wp);
3182 }
3183 }
3184 }
3185# endif
3186 changed_window_setting();
3187 }
3188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003189 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (old_fdl != curwin->w_p_fdl)
3191 newFoldLevel();
3192#endif
3193}
3194
3195#ifdef FEAT_GUI
3196/*
3197 * Vertical scrollbar movement.
3198 */
3199 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003200nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 if (cap->oap->op_type != OP_NOP)
3203 clearopbeep(cap->oap);
3204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 gui_do_scroll();
3207}
3208
3209/*
3210 * Horizontal scrollbar movement.
3211 */
3212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003213nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
3215 if (cap->oap->op_type != OP_NOP)
3216 clearopbeep(cap->oap);
3217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003218 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003219 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220}
3221#endif
3222
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003223#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003224/*
3225 * Click in GUI tab.
3226 */
3227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003228nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003229{
3230 if (cap->oap->op_type != OP_NOP)
3231 clearopbeep(cap->oap);
3232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003234 goto_tabpage(current_tab);
3235}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003236
3237/*
3238 * Selected item in tab line menu.
3239 */
3240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003241nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003242{
3243 if (cap->oap->op_type != OP_NOP)
3244 clearopbeep(cap->oap);
3245
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003246 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003247 handle_tabmenu();
3248}
3249
3250/*
3251 * Handle selecting an item of the GUI tab line menu.
3252 * Used in Normal and Insert mode.
3253 */
3254 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003255handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003256{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003257 switch (current_tabmenu)
3258 {
3259 case TABLINE_MENU_CLOSE:
3260 if (current_tab == 0)
3261 do_cmdline_cmd((char_u *)"tabclose");
3262 else
3263 {
3264 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3265 current_tab);
3266 do_cmdline_cmd(IObuff);
3267 }
3268 break;
3269
3270 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003271 if (current_tab == 0)
3272 do_cmdline_cmd((char_u *)"$tabnew");
3273 else
3274 {
3275 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3276 current_tab - 1);
3277 do_cmdline_cmd(IObuff);
3278 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003279 break;
3280
3281 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003282 if (current_tab == 0)
3283 do_cmdline_cmd((char_u *)"browse $tabnew");
3284 else
3285 {
3286 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3287 current_tab - 1);
3288 do_cmdline_cmd(IObuff);
3289 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003290 break;
3291 }
3292}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003293#endif
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295/*
3296 * "Q" command.
3297 */
3298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003299nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 /*
3302 * Ignore 'Q' in Visual mode, just give a beep.
3303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003305 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003306 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 do_exmode(FALSE);
3308}
3309
3310/*
3311 * Handle a ":" command.
3312 */
3313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003314nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003316 int old_p_im;
3317 int cmd_result;
3318 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319
Bram Moolenaar957cf672020-11-12 14:21:06 +01003320 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 nv_operator(cap);
3322 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
3324 if (cap->oap->op_type != OP_NOP)
3325 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003326 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 cap->oap->motion_type = MCHAR;
3328 cap->oap->inclusive = FALSE;
3329 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003330 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003332 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 stuffcharReadbuff('.');
3334 if (cap->count0 > 1)
3335 {
3336 stuffReadbuff((char_u *)",.+");
3337 stuffnumReadbuff((long)cap->count0 - 1L);
3338 }
3339 }
3340
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003341 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (KeyTyped)
3343 compute_cmdrow();
3344
3345 old_p_im = p_im;
3346
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003347 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003348 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003351 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (p_im != old_p_im)
3353 {
3354 if (p_im)
3355 restart_edit = 'i';
3356 else
3357 restart_edit = 0;
3358 }
3359
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003360 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003361 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003362 clearop(cap->oap);
3363 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3365 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003366 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3367 || did_emsg
3368 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003369 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 clearopbeep(cap->oap);
3371 }
3372}
3373
3374/*
3375 * Handle CTRL-G command.
3376 */
3377 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003378nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003380 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
3382 VIsual_select = !VIsual_select;
3383 showmode();
3384 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003385 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003386 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 fileinfo((int)cap->count0, FALSE, TRUE);
3388}
3389
3390/*
3391 * Handle CTRL-H <Backspace> command.
3392 */
3393 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003394nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (VIsual_active && VIsual_select)
3397 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003398 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 v_visop(cap);
3400 }
3401 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 nv_left(cap);
3403}
3404
3405/*
3406 * CTRL-L: clear screen and redraw.
3407 */
3408 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003409nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410{
3411 if (!checkclearop(cap->oap))
3412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003414 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003415 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003416# ifdef FEAT_RELTIME
3417 {
3418 win_T *wp;
3419
3420 FOR_ALL_WINDOWS(wp)
3421 wp->w_s->b_syn_slow = FALSE;
3422 }
3423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424#endif
3425 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003426#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3427# ifdef VIMDLL
3428 if (!gui.in_use)
3429# endif
3430 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
3433}
3434
3435/*
3436 * CTRL-O: In Select mode: switch to Visual mode for one command.
3437 * Otherwise: Go to older pcmark.
3438 */
3439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003440nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (VIsual_active && VIsual_select)
3443 {
3444 VIsual_select = FALSE;
3445 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003446 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
3450 cap->count1 = -cap->count1;
3451 nv_pcmark(cap);
3452 }
3453}
3454
3455/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003456 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3457 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 */
3459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003460nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461{
3462 if (!checkclearopq(cap->oap))
3463 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3464 GETF_SETMARK|GETF_ALT, FALSE);
3465}
3466
3467/*
3468 * "Z" commands.
3469 */
3470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003471nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 if (!checkclearopq(cap->oap))
3474 {
3475 switch (cap->nchar)
3476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003477 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 case 'Z': do_cmdline_cmd((char_u *)"x");
3479 break;
3480
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003481 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 case 'Q': do_cmdline_cmd((char_u *)"q!");
3483 break;
3484
3485 default: clearopbeep(cap->oap);
3486 }
3487 }
3488}
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490/*
3491 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3492 */
3493 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003494do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 oparg_T oa;
3497 cmdarg_T ca;
3498
3499 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003500 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 ca.oap = &oa;
3502 ca.cmdchar = c1;
3503 ca.nchar = c2;
3504 nv_ident(&ca);
3505}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507/*
3508 * Handle the commands that use the word under the cursor.
3509 * [g] CTRL-] :ta to current identifier
3510 * [g] 'K' run program for current identifier
3511 * [g] '*' / to current identifier or string
3512 * [g] '#' ? to current identifier or string
3513 * g ']' :tselect for current identifier
3514 */
3515 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003516nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
3518 char_u *ptr = NULL;
3519 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003520 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003521 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003523 char_u *kp; // value of 'keywordprg'
3524 int kp_help; // 'keywordprg' is ":he"
3525 int kp_ex; // 'keywordprg' starts with ":"
3526 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003528 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003529 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 char_u *aux_ptr;
3531 int isman;
3532 int isman_s;
3533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003534 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 {
3536 cmdchar = cap->nchar;
3537 g_cmd = TRUE;
3538 }
3539 else
3540 {
3541 cmdchar = cap->cmdchar;
3542 g_cmd = FALSE;
3543 }
3544
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003545 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 cmdchar = '#';
3547
3548 /*
3549 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3550 */
3551 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3554 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (checkclearopq(cap->oap))
3556 return;
3557 }
3558
3559 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3560 (cmdchar == '*' || cmdchar == '#')
3561 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3562 {
3563 clearop(cap->oap);
3564 return;
3565 }
3566
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003567 // Allocate buffer to put the command in. Inserting backslashes can
3568 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3569 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3571 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3572 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003573 if (kp_help && *skipwhite(ptr) == NUL)
3574 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003575 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003576 return;
3577 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003578 kp_ex = (*kp == ':');
3579 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3580 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (buf == NULL)
3582 return;
3583 buf[0] = NUL;
3584
3585 switch (cmdchar)
3586 {
3587 case '*':
3588 case '#':
3589 /*
3590 * Put cursor at start of word, makes search skip the word
3591 * under the cursor.
3592 * Call setpcmark() first, so "*``" puts the cursor back where
3593 * it was.
3594 */
3595 setpcmark();
3596 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3597
3598 if (!g_cmd && vim_iswordp(ptr))
3599 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003600 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 break;
3602
3603 case 'K':
3604 if (kp_help)
3605 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003606 else if (kp_ex)
3607 {
3608 if (cap->count0 != 0)
3609 vim_snprintf((char *)buf, buflen, "%s %ld",
3610 kp, cap->count0);
3611 else
3612 STRCPY(buf, kp);
3613 STRCAT(buf, " ");
3614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 else
3616 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003617 // An external command will probably use an argument starting
3618 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003619 while (*ptr == '-' && n > 0)
3620 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003621 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003622 --n;
3623 }
3624 if (n == 0)
3625 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003626 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003627 vim_free(buf);
3628 return;
3629 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003630
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003631 // When a count is given, turn it into a range. Is this
3632 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 isman = (STRCMP(kp, "man") == 0);
3634 isman_s = (STRCMP(kp, "man -s") == 0);
3635 if (cap->count0 != 0 && !(isman || isman_s))
3636 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3637
3638 STRCAT(buf, "! ");
3639 if (cap->count0 == 0 && isman_s)
3640 STRCAT(buf, "man");
3641 else
3642 STRCAT(buf, kp);
3643 STRCAT(buf, " ");
3644 if (cap->count0 != 0 && (isman || isman_s))
3645 {
3646 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3647 STRCAT(buf, " ");
3648 }
3649 }
3650 break;
3651
3652 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003653 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654#ifdef FEAT_CSCOPE
3655 if (p_cst)
3656 STRCPY(buf, "cstag ");
3657 else
3658#endif
3659 STRCPY(buf, "ts ");
3660 break;
3661
3662 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003663 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 if (curbuf->b_help)
3665 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003667 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003668 if (g_cmd)
3669 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003670 else if (cap->count0 == 0)
3671 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003672 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003673 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676
3677 /*
3678 * Now grab the chars in the identifier
3679 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003680 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003682 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003683 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003684 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003685 p = vim_strsave_fnameescape(ptr, FALSE);
3686 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003687 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003688 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003689 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003690 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003692 vim_free(buf);
3693 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003695 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003696 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003697 {
3698 vim_free(buf);
3699 vim_free(p);
3700 return;
3701 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003702 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003703 STRCAT(buf, p);
3704 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003706 else
3707 {
3708 if (cmdchar == '*')
3709 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
3710 else if (cmdchar == '#')
3711 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003712 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003713 {
3714 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003715 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003716 aux_ptr = (char_u *)"";
3717 else
3718 aux_ptr = (char_u *)"\\|\"\n[";
3719 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003720 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003721 aux_ptr = (char_u *)"\\|\"\n*?[";
3722
3723 p = buf + STRLEN(buf);
3724 while (n-- > 0)
3725 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003726 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003727 if (vim_strchr(aux_ptr, *ptr) != NULL)
3728 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003729 // When current byte is a part of multibyte character, copy all
3730 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003731 if (has_mbyte)
3732 {
3733 int i;
3734 int len = (*mb_ptr2len)(ptr) - 1;
3735
3736 for (i = 0; i < len && n >= 1; ++i, --n)
3737 *p++ = *ptr++;
3738 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003739 *p++ = *ptr++;
3740 }
3741 *p = NUL;
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744 /*
3745 * Execute the command.
3746 */
3747 if (cmdchar == '*' || cmdchar == '#')
3748 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003749 if (!g_cmd && (has_mbyte
3750 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3751 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003753
3754 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003755 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003757
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003758 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
3760 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003761 {
3762 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003764 g_tag_at_cursor = FALSE;
3765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767 vim_free(buf);
3768}
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770/*
3771 * Get visually selected text, within one line only.
3772 * Returns FAIL if more than one line selected.
3773 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003774 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003775get_visual_text(
3776 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003777 char_u **pp, // return: start of selected text
3778 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 if (VIsual_mode != 'V')
3781 unadjust_for_sel();
3782 if (VIsual.lnum != curwin->w_cursor.lnum)
3783 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003784 if (cap != NULL)
3785 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787 }
3788 if (VIsual_mode == 'V')
3789 {
3790 *pp = ml_get_curline();
3791 *lenp = (int)STRLEN(*pp);
3792 }
3793 else
3794 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003795 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
3797 *pp = ml_get_pos(&curwin->w_cursor);
3798 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3799 }
3800 else
3801 {
3802 *pp = ml_get_pos(&VIsual);
3803 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003806 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003807 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 reset_VIsual_and_resel();
3810 return OK;
3811}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813/*
3814 * CTRL-T: backwards in tag stack
3815 */
3816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003817nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 if (!checkclearopq(cap->oap))
3820 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3821}
3822
3823/*
3824 * Handle scrolling command 'H', 'L' and 'M'.
3825 */
3826 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003827nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 int used = 0;
3830 long n;
3831#ifdef FEAT_FOLDING
3832 linenr_T lnum;
3833#endif
3834 int half;
3835
3836 cap->oap->motion_type = MLINE;
3837 setpcmark();
3838
3839 if (cap->cmdchar == 'L')
3840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003841 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 curwin->w_cursor.lnum = curwin->w_botline - 1;
3843 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3844 curwin->w_cursor.lnum = 1;
3845 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003846 {
3847#ifdef FEAT_FOLDING
3848 if (hasAnyFolding(curwin))
3849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003850 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003851 for (n = cap->count1 - 1; n > 0
3852 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3853 {
3854 (void)hasFolding(curwin->w_cursor.lnum,
3855 &curwin->w_cursor.lnum, NULL);
3856 --curwin->w_cursor.lnum;
3857 }
3858 }
3859 else
3860#endif
3861 curwin->w_cursor.lnum -= cap->count1 - 1;
3862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864 else
3865 {
3866 if (cap->cmdchar == 'M')
3867 {
3868#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003869 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 used -= diff_check_fill(curwin, curwin->w_topline)
3871 - curwin->w_topfill;
3872#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003873 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3875 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3876 {
3877#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003878 // Count half he number of filler lines to be "below this
3879 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3881 + n) / 2 >= half)
3882 {
3883 --n;
3884 break;
3885 }
3886#endif
3887 used += plines(curwin->w_topline + n);
3888 if (used >= half)
3889 break;
3890#ifdef FEAT_FOLDING
3891 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3892 n = lnum - curwin->w_topline;
3893#endif
3894 }
3895 if (n > 0 && used > curwin->w_height)
3896 --n;
3897 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003898 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003901#ifdef FEAT_FOLDING
3902 if (hasAnyFolding(curwin))
3903 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003904 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003905 lnum = curwin->w_topline;
3906 while (n-- > 0 && lnum < curwin->w_botline - 1)
3907 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003908 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003909 ++lnum;
3910 }
3911 n = lnum - curwin->w_topline;
3912 }
3913#endif
3914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 curwin->w_cursor.lnum = curwin->w_topline + n;
3916 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3917 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3918 }
3919
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003920 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003921 if (cap->oap->op_type == OP_NOP)
3922 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 beginline(BL_SOL | BL_FIX);
3924}
3925
3926/*
3927 * Cursor right commands.
3928 */
3929 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003930nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
3932 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003933 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003935 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3936 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003937 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003938 if (mod_mask & MOD_MASK_CTRL)
3939 cap->arg = TRUE;
3940 nv_wordcmd(cap);
3941 return;
3942 }
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 cap->oap->motion_type = MCHAR;
3945 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003946 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003949 * In virtual edit mode, there's no such thing as "past_line", as lines
3950 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 */
3952 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003953 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
3955 for (n = cap->count1; n > 0; --n)
3956 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003957 if ((!past_line && oneright() == FAIL)
3958 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003959 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
3961 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003962 * <Space> wraps to next line if 'whichwrap' has 's'.
3963 * 'l' wraps to next line if 'whichwrap' has 'l'.
3964 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 */
3966 if ( ((cap->cmdchar == ' '
3967 && vim_strchr(p_ww, 's') != NULL)
3968 || (cap->cmdchar == 'l'
3969 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003970 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 && vim_strchr(p_ww, '>') != NULL))
3972 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3973 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003974 // When deleting we also count the NL as a character.
3975 // Set cap->oap->inclusive when last char in the line is
3976 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003977 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003979 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 cap->oap->inclusive = TRUE;
3981 else
3982 {
3983 ++curwin->w_cursor.lnum;
3984 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 curwin->w_set_curswant = TRUE;
3987 cap->oap->inclusive = FALSE;
3988 }
3989 continue;
3990 }
3991 if (cap->oap->op_type == OP_NOP)
3992 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003993 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (n == cap->count1)
3995 beep_flush();
3996 }
3997 else
3998 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003999 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 cap->oap->inclusive = TRUE;
4001 }
4002 break;
4003 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004004 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
4006 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (virtual_active())
4008 oneright();
4009 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004012 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 ++curwin->w_cursor.col;
4015 }
4016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 }
4018#ifdef FEAT_FOLDING
4019 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4020 && cap->oap->op_type == OP_NOP)
4021 foldOpenCursor();
4022#endif
4023}
4024
4025/*
4026 * Cursor left commands.
4027 *
4028 * Returns TRUE when operator end should not be adjusted.
4029 */
4030 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004031nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032{
4033 long n;
4034
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004035 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004037 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004038 if (mod_mask & MOD_MASK_CTRL)
4039 cap->arg = 1;
4040 nv_bck_word(cap);
4041 return;
4042 }
4043
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 cap->oap->motion_type = MCHAR;
4045 cap->oap->inclusive = FALSE;
4046 for (n = cap->count1; n > 0; --n)
4047 {
4048 if (oneleft() == FAIL)
4049 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004050 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4051 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4052 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if ( (((cap->cmdchar == K_BS
4054 || cap->cmdchar == Ctrl_H)
4055 && vim_strchr(p_ww, 'b') != NULL)
4056 || (cap->cmdchar == 'h'
4057 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004058 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 && vim_strchr(p_ww, '<') != NULL))
4060 && curwin->w_cursor.lnum > 1)
4061 {
4062 --(curwin->w_cursor.lnum);
4063 coladvance((colnr_T)MAXCOL);
4064 curwin->w_set_curswant = TRUE;
4065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004066 // When the NL before the first char has to be deleted we
4067 // put the cursor on the NUL after the previous line.
4068 // This is a very special case, be careful!
4069 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if ( (cap->oap->op_type == OP_DELETE
4071 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004072 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004074 char_u *cp = ml_get_cursor();
4075
4076 if (*cp != NUL)
4077 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004078 if (has_mbyte)
4079 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4080 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004081 ++curwin->w_cursor.col;
4082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 cap->retval |= CA_NO_ADJ_OP_END;
4084 }
4085 continue;
4086 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004087 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4089 beep_flush();
4090 break;
4091 }
4092 }
4093#ifdef FEAT_FOLDING
4094 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4095 && cap->oap->op_type == OP_NOP)
4096 foldOpenCursor();
4097#endif
4098}
4099
4100/*
4101 * Cursor up commands.
4102 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4103 */
4104 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004105nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004107 if (mod_mask & MOD_MASK_SHIFT)
4108 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004110 cap->arg = BACKWARD;
4111 nv_page(cap);
4112 }
4113 else
4114 {
4115 cap->oap->motion_type = MLINE;
4116 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4117 clearopbeep(cap->oap);
4118 else if (cap->arg)
4119 beginline(BL_WHITE | BL_FIX);
4120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121}
4122
4123/*
4124 * Cursor down commands.
4125 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4126 */
4127 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004128nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004130 if (mod_mask & MOD_MASK_SHIFT)
4131 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004132 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004133 cap->arg = FORWARD;
4134 nv_page(cap);
4135 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004136#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004137 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004138 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4139 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004141 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 {
4143#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004144 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004145 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 cmdwin_result = CAR;
4147 else
4148#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004149#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004150 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004151 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4152 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4153 {
4154 invoke_prompt_callback();
4155 if (restart_edit == 0)
4156 restart_edit = 'a';
4157 }
4158 else
4159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
4161 cap->oap->motion_type = MLINE;
4162 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4163 clearopbeep(cap->oap);
4164 else if (cap->arg)
4165 beginline(BL_WHITE | BL_FIX);
4166 }
4167 }
4168}
4169
4170#ifdef FEAT_SEARCHPATH
4171/*
4172 * Grab the file name under the cursor and edit it.
4173 */
4174 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004175nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
4177 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004178 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004180 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
4182 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004183 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return;
4185 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004186 if (curbuf_locked())
4187 {
4188 clearop(cap->oap);
4189 return;
4190 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004191#ifdef FEAT_PROP_POPUP
4192 if (ERROR_IF_TERM_POPUP_WINDOW)
4193 return;
4194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004196 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
4198 if (ptr != NULL)
4199 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004200 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004201 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004202 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004204 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004205 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004206 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004207 {
4208 curwin->w_cursor.lnum = lnum;
4209 check_cursor_lnum();
4210 beginline(BL_SOL | BL_FIX);
4211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 vim_free(ptr);
4213 }
4214 else
4215 clearop(cap->oap);
4216}
4217#endif
4218
4219/*
4220 * <End> command: to end of current line or last line.
4221 */
4222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004223nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004225 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004227 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004229 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231 nv_dollar(cap);
4232}
4233
4234/*
4235 * Handle the "$" command.
4236 */
4237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004238nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
4240 cap->oap->motion_type = MCHAR;
4241 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004242 // In virtual mode when off the edge of a line and an operator
4243 // is pending (whew!) keep the cursor where it is.
4244 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 if (!virtual_active() || gchar_cursor() != NUL
4246 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004247 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (cursor_down((long)(cap->count1 - 1),
4249 cap->oap->op_type == OP_NOP) == FAIL)
4250 clearopbeep(cap->oap);
4251#ifdef FEAT_FOLDING
4252 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4253 foldOpenCursor();
4254#endif
4255}
4256
4257/*
4258 * Implementation of '?' and '/' commands.
4259 * If cap->arg is TRUE don't set PC mark.
4260 */
4261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004262nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
4264 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004265 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4268 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004269 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 cap->cmdchar = 'g';
4271 cap->nchar = '?';
4272 nv_operator(cap);
4273 return;
4274 }
4275
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004276 // When using 'incsearch' the cursor may be moved to set a different search
4277 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004278 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 if (cap->searchbuf == NULL)
4281 {
4282 clearop(oap);
4283 return;
4284 }
4285
Bram Moolenaar46539112015-02-17 15:43:57 +01004286 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004287 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004288 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289}
4290
4291/*
4292 * Handle "N" and "n" commands.
4293 * cap->arg is SEARCH_REV for "N", 0 for "n".
4294 */
4295 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004296nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004298 pos_T old = curwin->w_cursor;
4299 int wrapped = FALSE;
4300 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004301
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004302 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004303 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004304 // Avoid getting stuck on the current cursor position, which can
4305 // happen when an offset is given and the cursor is on the last char
4306 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004307 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004308 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004309 cap->count1 -= 1;
4310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311}
4312
4313/*
4314 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4315 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004316 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004318 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004319normal_search(
4320 cmdarg_T *cap,
4321 int dir,
4322 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004323 int opt, // extra flags for do_search()
4324 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325{
4326 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004327 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 cap->oap->motion_type = MCHAR;
4330 cap->oap->inclusive = FALSE;
4331 cap->oap->use_reg_one = TRUE;
4332 curwin->w_set_curswant = TRUE;
4333
Bram Moolenaara80faa82020-04-12 19:37:17 +02004334 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004335 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004336 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4337 if (wrapped != NULL)
4338 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 if (i == 0)
4340 clearop(cap->oap);
4341 else
4342 {
4343 if (i == 2)
4344 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346#ifdef FEAT_FOLDING
4347 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4348 foldOpenCursor();
4349#endif
4350 }
4351
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004352 // "/$" will put the cursor after the end of the line, may need to
4353 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004355 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356}
4357
4358/*
4359 * Character search commands.
4360 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4361 * ',' and FALSE for ';'.
4362 * cap->nchar is NUL for ',' and ';' (repeat the search)
4363 */
4364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004365nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
4367 int t_cmd;
4368
4369 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4370 t_cmd = TRUE;
4371 else
4372 t_cmd = FALSE;
4373
4374 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4376 clearopbeep(cap->oap);
4377 else
4378 {
4379 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004380 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4382 && (t_cmd || cap->oap->op_type != OP_NOP))
4383 {
4384 colnr_T scol, ecol;
4385
4386 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4387 curwin->w_cursor.coladd = ecol - scol;
4388 }
4389 else
4390 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392#ifdef FEAT_FOLDING
4393 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4394 foldOpenCursor();
4395#endif
4396 }
4397}
4398
4399/*
4400 * "[" and "]" commands.
4401 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4402 */
4403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004404nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004406 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004408 pos_T *pos = NULL; // init for GCC
4409 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 int flag;
4411 long n;
4412 int findc;
4413 int c;
4414
4415 cap->oap->motion_type = MCHAR;
4416 cap->oap->inclusive = FALSE;
4417 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004418 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419
4420#ifdef FEAT_SEARCHPATH
4421 /*
4422 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4423 */
4424 if (cap->nchar == 'f')
4425 nv_gotofile(cap);
4426 else
4427#endif
4428
4429#ifdef FEAT_FIND_ID
4430 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004431 * Find the occurrence(s) of the identifier or define under cursor
4432 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 *
4434 * search list jump
4435 * fwd bwd fwd bwd fwd bwd
4436 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4437 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4438 */
4439 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004440# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004442# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004444# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 cap->nchar) != NULL)
4446 {
4447 char_u *ptr;
4448 int len;
4449
4450 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4451 clearop(cap->oap);
4452 else
4453 {
4454 find_pattern_in_path(ptr, 0, len, TRUE,
4455 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4456 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4457 cap->count1,
4458 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4459 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4460 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4461 (linenr_T)MAXLNUM);
4462 curwin->w_set_curswant = TRUE;
4463 }
4464 }
4465 else
4466#endif
4467
4468 /*
4469 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4470 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4471 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4472 * "[m" or "]m" search for prev/next start of (Java) method.
4473 * "[M" or "]M" search for prev/next end of (Java) method.
4474 */
4475 if ( (cap->cmdchar == '['
4476 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4477 || (cap->cmdchar == ']'
4478 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4479 {
4480 if (cap->nchar == '*')
4481 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 prev_pos.lnum = 0;
4483 if (cap->nchar == 'm' || cap->nchar == 'M')
4484 {
4485 if (cap->cmdchar == '[')
4486 findc = '{';
4487 else
4488 findc = '}';
4489 n = 9999;
4490 }
4491 else
4492 {
4493 findc = cap->nchar;
4494 n = cap->count1;
4495 }
4496 for ( ; n > 0; --n)
4497 {
4498 if ((pos = findmatchlimit(cap->oap, findc,
4499 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4500 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004501 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
4503 if (cap->nchar != 'm' && cap->nchar != 'M')
4504 clearopbeep(cap->oap);
4505 }
4506 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004507 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 break;
4509 }
4510 prev_pos = new_pos;
4511 curwin->w_cursor = *pos;
4512 new_pos = *pos;
4513 }
4514 curwin->w_cursor = old_pos;
4515
4516 /*
4517 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4518 * brought us to the match for "[m" and "]M" when inside a method.
4519 * Try finding the '{' or '}' we want to be at.
4520 * Also repeat for the given count.
4521 */
4522 if (cap->nchar == 'm' || cap->nchar == 'M')
4523 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004524 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 int norm = ((findc == '{') == (cap->nchar == 'm'));
4526
4527 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004528 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 if (prev_pos.lnum != 0)
4530 {
4531 pos = &prev_pos;
4532 curwin->w_cursor = prev_pos;
4533 if (norm)
4534 --n;
4535 }
4536 else
4537 pos = NULL;
4538 while (n > 0)
4539 {
4540 for (;;)
4541 {
4542 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4543 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004544 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (pos == NULL)
4546 clearopbeep(cap->oap);
4547 n = 0;
4548 break;
4549 }
4550 c = gchar_cursor();
4551 if (c == '{' || c == '}')
4552 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004553 // Must have found end/start of class: use it.
4554 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if ((c == findc && norm) || (n == 1 && !norm))
4556 {
4557 new_pos = curwin->w_cursor;
4558 pos = &new_pos;
4559 n = 0;
4560 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004561 // if no match found at all, we started outside of the
4562 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 else if (new_pos.lnum == 0)
4564 {
4565 new_pos = curwin->w_cursor;
4566 pos = &new_pos;
4567 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004568 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else if ((pos = findmatchlimit(cap->oap, findc,
4570 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4571 0)) == NULL)
4572 n = 0;
4573 else
4574 curwin->w_cursor = *pos;
4575 break;
4576 }
4577 }
4578 --n;
4579 }
4580 curwin->w_cursor = old_pos;
4581 if (pos == NULL && new_pos.lnum != 0)
4582 clearopbeep(cap->oap);
4583 }
4584 if (pos != NULL)
4585 {
4586 setpcmark();
4587 curwin->w_cursor = *pos;
4588 curwin->w_set_curswant = TRUE;
4589#ifdef FEAT_FOLDING
4590 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4591 && cap->oap->op_type == OP_NOP)
4592 foldOpenCursor();
4593#endif
4594 }
4595 }
4596
4597 /*
4598 * "[[", "[]", "]]" and "][": move to start or end of function
4599 */
4600 else if (cap->nchar == '[' || cap->nchar == ']')
4601 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004602 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 flag = '{';
4604 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004605 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 curwin->w_set_curswant = TRUE;
4608 /*
4609 * Imitate strange Vi behaviour: When using "]]" with an operator
4610 * we also stop at '}'.
4611 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004612 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 (cap->oap->op_type != OP_NOP
4614 && cap->arg == FORWARD && flag == '{')))
4615 clearopbeep(cap->oap);
4616 else
4617 {
4618 if (cap->oap->op_type == OP_NOP)
4619 beginline(BL_WHITE | BL_FIX);
4620#ifdef FEAT_FOLDING
4621 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4622 foldOpenCursor();
4623#endif
4624 }
4625 }
4626
4627 /*
4628 * "[p", "[P", "]P" and "]p": put with indent adjustment
4629 */
4630 else if (cap->nchar == 'p' || cap->nchar == 'P')
4631 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004632 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
4634
4635 /*
4636 * "['", "[`", "]'" and "]`": jump to next mark
4637 */
4638 else if (cap->nchar == '\'' || cap->nchar == '`')
4639 {
4640 pos = &curwin->w_cursor;
4641 for (n = cap->count1; n > 0; --n)
4642 {
4643 prev_pos = *pos;
4644 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4645 cap->nchar == '\'');
4646 if (pos == NULL)
4647 break;
4648 }
4649 if (pos == NULL)
4650 pos = &prev_pos;
4651 nv_cursormark(cap, cap->nchar == '\'', pos);
4652 }
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 /*
4655 * [ or ] followed by a middle mouse click: put selected text with
4656 * indent adjustment. Any other button just does as usual.
4657 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004658 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
4660 (void)do_mouse(cap->oap, cap->nchar,
4661 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4662 cap->count1, PUT_FIXINDENT);
4663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
4665#ifdef FEAT_FOLDING
4666 /*
4667 * "[z" and "]z": move to start or end of open fold.
4668 */
4669 else if (cap->nchar == 'z')
4670 {
4671 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4672 cap->count1) == FAIL)
4673 clearopbeep(cap->oap);
4674 }
4675#endif
4676
4677#ifdef FEAT_DIFF
4678 /*
4679 * "[c" and "]c": move to next or previous diff-change.
4680 */
4681 else if (cap->nchar == 'c')
4682 {
4683 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4684 cap->count1) == FAIL)
4685 clearopbeep(cap->oap);
4686 }
4687#endif
4688
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004689#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004690 /*
4691 * "[s", "[S", "]s" and "]S": move to next spell error.
4692 */
4693 else if (cap->nchar == 's' || cap->nchar == 'S')
4694 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004695 setpcmark();
4696 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004697 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4698 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004699 {
4700 clearopbeep(cap->oap);
4701 break;
4702 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004703 else
4704 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004705# ifdef FEAT_FOLDING
4706 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4707 foldOpenCursor();
4708# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004709 }
4710#endif
4711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004712 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 else
4714 clearopbeep(cap->oap);
4715}
4716
4717/*
4718 * Handle Normal mode "%" command.
4719 */
4720 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004721nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004724#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 linenr_T lnum = curwin->w_cursor.lnum;
4726#endif
4727
4728 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004729 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 if (cap->count0 > 100)
4732 clearopbeep(cap->oap);
4733 else
4734 {
4735 cap->oap->motion_type = MLINE;
4736 setpcmark();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004737 // Round up, so CTRL-G will give same value. Watch out for a
4738 // large line count, the line number must not go negative!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if (curbuf->b_ml.ml_line_count > 1000000)
4740 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4741 / 100L * cap->count0;
4742 else
4743 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4744 cap->count0 + 99L) / 100L;
4745 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4746 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4747 beginline(BL_SOL | BL_FIX);
4748 }
4749 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004750 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 cap->oap->motion_type = MCHAR;
4753 cap->oap->use_reg_one = TRUE;
4754 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4755 clearopbeep(cap->oap);
4756 else
4757 {
4758 setpcmark();
4759 curwin->w_cursor = *pos;
4760 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 }
4764 }
4765#ifdef FEAT_FOLDING
4766 if (cap->oap->op_type == OP_NOP
4767 && lnum != curwin->w_cursor.lnum
4768 && (fdo_flags & FDO_PERCENT)
4769 && KeyTyped)
4770 foldOpenCursor();
4771#endif
4772}
4773
4774/*
4775 * Handle "(" and ")" commands.
4776 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4777 */
4778 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004779nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781 cap->oap->motion_type = MCHAR;
4782 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004783 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004784 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 curwin->w_set_curswant = TRUE;
4786
4787 if (findsent(cap->arg, cap->count1) == FAIL)
4788 clearopbeep(cap->oap);
4789 else
4790 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004791 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004792 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794#ifdef FEAT_FOLDING
4795 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4796 foldOpenCursor();
4797#endif
4798 }
4799}
4800
4801/*
4802 * "m" command: Mark a position.
4803 */
4804 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004805nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 if (!checkclearop(cap->oap))
4808 {
4809 if (setmark(cap->nchar) == FAIL)
4810 clearopbeep(cap->oap);
4811 }
4812}
4813
4814/*
4815 * "{" and "}" commands.
4816 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4817 */
4818 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004819nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
4821 cap->oap->motion_type = MCHAR;
4822 cap->oap->inclusive = FALSE;
4823 cap->oap->use_reg_one = TRUE;
4824 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004825 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 clearopbeep(cap->oap);
4827 else
4828 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#ifdef FEAT_FOLDING
4831 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4832 foldOpenCursor();
4833#endif
4834 }
4835}
4836
4837/*
4838 * "u" command: Undo or make lower case.
4839 */
4840 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004841nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004843 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004845 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 cap->cmdchar = 'g';
4847 cap->nchar = 'u';
4848 nv_operator(cap);
4849 }
4850 else
4851 nv_kundo(cap);
4852}
4853
4854/*
4855 * <Undo> command.
4856 */
4857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004858nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
4860 if (!checkclearopq(cap->oap))
4861 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004862#ifdef FEAT_JOB_CHANNEL
4863 if (bt_prompt(curbuf))
4864 {
4865 clearopbeep(cap->oap);
4866 return;
4867 }
4868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 u_undo((int)cap->count1);
4870 curwin->w_set_curswant = TRUE;
4871 }
4872}
4873
4874/*
4875 * Handle the "r" command.
4876 */
4877 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004878nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
4880 char_u *ptr;
4881 int had_ctrl_v;
4882 long n;
4883
4884 if (checkclearop(cap->oap))
4885 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004886#ifdef FEAT_JOB_CHANNEL
4887 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4888 {
4889 clearopbeep(cap->oap);
4890 return;
4891 }
4892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004894 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (cap->nchar == Ctrl_V)
4896 {
4897 had_ctrl_v = Ctrl_V;
4898 cap->nchar = get_literal();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004899 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 if (cap->nchar > DEL)
4901 had_ctrl_v = NUL;
4902 }
4903 else
4904 had_ctrl_v = NUL;
4905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004906 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004907 if (IS_SPECIAL(cap->nchar))
4908 {
4909 clearopbeep(cap->oap);
4910 return;
4911 }
4912
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004913 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (VIsual_active)
4915 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004916 if (got_int)
4917 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004918 if (had_ctrl_v)
4919 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004920 // Use a special (negative) number to make a difference between a
4921 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004922 if (cap->nchar == CAR)
4923 cap->nchar = REPLACE_CR_NCHAR;
4924 else if (cap->nchar == NL)
4925 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 nv_operator(cap);
4928 return;
4929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004931 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (virtual_active())
4933 {
4934 if (u_save_cursor() == FAIL)
4935 return;
4936 if (gchar_cursor() == NUL)
4937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004938 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 coladvance_force((colnr_T)(getviscol() + cap->count1));
4940 curwin->w_cursor.col -= cap->count1;
4941 }
4942 else if (gchar_cursor() == TAB)
4943 coladvance_force(getviscol());
4944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004946 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004948 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004949 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 clearopbeep(cap->oap);
4952 return;
4953 }
4954
4955 /*
4956 * Replacing with a TAB is done by edit() when it is complicated because
4957 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4958 * Other characters are done below to avoid problems with things like
4959 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
4960 */
4961 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4962 {
4963 stuffnumReadbuff(cap->count1);
4964 stuffcharReadbuff('R');
4965 stuffcharReadbuff('\t');
4966 stuffcharReadbuff(ESC);
4967 return;
4968 }
4969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004970 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (u_save_cursor() == FAIL)
4972 return;
4973
4974 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4975 {
4976 /*
4977 * Replace character(s) by a single newline.
4978 * Strange vi behaviour: Only one newline is inserted.
4979 * Delete the characters here.
4980 * Insert the newline with an insert command, takes care of
4981 * autoindent. The insert command depends on being on the last
4982 * character of a line or not.
4983 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004984 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 stuffcharReadbuff('\r');
4986 stuffcharReadbuff(ESC);
4987
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004988 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 invoke_edit(cap, TRUE, 'r', FALSE);
4990 }
4991 else
4992 {
4993 prep_redo(cap->oap->regname, cap->count1,
4994 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4995
4996 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (has_mbyte)
4998 {
4999 int old_State = State;
5000
5001 if (cap->ncharC1 != 0)
5002 AppendCharToRedobuff(cap->ncharC1);
5003 if (cap->ncharC2 != 0)
5004 AppendCharToRedobuff(cap->ncharC2);
5005
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005006 // This is slow, but it handles replacing a single-byte with a
5007 // multi-byte and the other way around. Also handles adding
5008 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 for (n = cap->count1; n > 0; --n)
5010 {
5011 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005012 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5013 {
5014 int c = ins_copychar(curwin->w_cursor.lnum
5015 + (cap->nchar == Ctrl_Y ? -1 : 1));
5016 if (c != NUL)
5017 ins_char(c);
5018 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005019 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005020 ++curwin->w_cursor.col;
5021 }
5022 else
5023 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 State = old_State;
5025 if (cap->ncharC1 != 0)
5026 ins_char(cap->ncharC1);
5027 if (cap->ncharC2 != 0)
5028 ins_char(cap->ncharC2);
5029 }
5030 }
5031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
5033 /*
5034 * Replace the characters within one line.
5035 */
5036 for (n = cap->count1; n > 0; --n)
5037 {
5038 /*
5039 * Get ptr again, because u_save and/or showmatch() will have
5040 * released the line. At the same time we let know that the
5041 * line will be changed.
5042 */
5043 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005044 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5045 {
5046 int c = ins_copychar(curwin->w_cursor.lnum
5047 + (cap->nchar == Ctrl_Y ? -1 : 1));
5048 if (c != NUL)
5049 ptr[curwin->w_cursor.col] = c;
5050 }
5051 else
5052 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 if (p_sm && msg_silent == 0)
5054 showmatch(cap->nchar);
5055 ++curwin->w_cursor.col;
5056 }
5057#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005058 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005060 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
Bram Moolenaar009b2592004-10-24 19:18:58 +00005062 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005063 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005065 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067#endif
5068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005069 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 changed_bytes(curwin->w_cursor.lnum,
5071 (colnr_T)(curwin->w_cursor.col - cap->count1));
5072 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005073 --curwin->w_cursor.col; // cursor on the last replaced char
5074 // if the character on the left of the current cursor is a multi-byte
5075 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (has_mbyte)
5077 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 curbuf->b_op_end = curwin->w_cursor;
5079 curwin->w_set_curswant = TRUE;
5080 set_last_insert(cap->nchar);
5081 }
5082}
5083
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084/*
5085 * 'o': Exchange start and end of Visual area.
5086 * 'O': same, but in block mode exchange left and right corners.
5087 */
5088 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005089v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090{
5091 pos_T old_cursor;
5092 colnr_T left, right;
5093
5094 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5095 {
5096 old_cursor = curwin->w_cursor;
5097 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5098 curwin->w_cursor.lnum = VIsual.lnum;
5099 coladvance(left);
5100 VIsual = curwin->w_cursor;
5101
5102 curwin->w_cursor.lnum = old_cursor.lnum;
5103 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005104 // 'selection "exclusive" and cursor at right-bottom corner: move it
5105 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5107 ++curwin->w_curswant;
5108 coladvance(curwin->w_curswant);
5109 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005111 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 curwin->w_cursor.lnum = VIsual.lnum;
5114 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5115 ++right;
5116 coladvance(right);
5117 VIsual = curwin->w_cursor;
5118
5119 curwin->w_cursor.lnum = old_cursor.lnum;
5120 coladvance(left);
5121 curwin->w_curswant = left;
5122 }
5123 }
5124 else
5125 {
5126 old_cursor = curwin->w_cursor;
5127 curwin->w_cursor = VIsual;
5128 VIsual = old_cursor;
5129 curwin->w_set_curswant = TRUE;
5130 }
5131}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133/*
5134 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5135 */
5136 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005137nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005139 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 cap->cmdchar = 'c';
5142 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005143 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 VIsual_mode = 'V';
5145 nv_operator(cap);
5146 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005147 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005150 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 else
5152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (virtual_active())
5154 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5156 }
5157 }
5158}
5159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160/*
5161 * "gr".
5162 */
5163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005164nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 if (VIsual_active)
5167 {
5168 cap->cmdchar = 'r';
5169 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005170 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005172 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
5174 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005175 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 else
5177 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005178 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 cap->extra_char = get_literal();
5180 stuffcharReadbuff(cap->extra_char);
5181 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (virtual_active())
5183 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 invoke_edit(cap, TRUE, 'v', FALSE);
5185 }
5186 }
5187}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189/*
5190 * Swap case for "~" command, when it does not work like an operator.
5191 */
5192 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005193n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 long n;
5196 pos_T startpos;
5197 int did_change = 0;
5198#ifdef FEAT_NETBEANS_INTG
5199 pos_T pos;
5200 char_u *ptr;
5201 int count;
5202#endif
5203
5204 if (checkclearopq(cap->oap))
5205 return;
5206
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005207 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
5209 clearopbeep(cap->oap);
5210 return;
5211 }
5212
5213 prep_redo_cmd(cap);
5214
5215 if (u_save_cursor() == FAIL)
5216 return;
5217
5218 startpos = curwin->w_cursor;
5219#ifdef FEAT_NETBEANS_INTG
5220 pos = startpos;
5221#endif
5222 for (n = cap->count1; n > 0; --n)
5223 {
5224 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5225 inc_cursor();
5226 if (gchar_cursor() == NUL)
5227 {
5228 if (vim_strchr(p_ww, '~') != NULL
5229 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5230 {
5231#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005232 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
5234 if (did_change)
5235 {
5236 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005237 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005238 netbeans_removed(curbuf, pos.lnum, pos.col,
5239 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005241 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243 pos.col = 0;
5244 pos.lnum++;
5245 }
5246#endif
5247 ++curwin->w_cursor.lnum;
5248 curwin->w_cursor.col = 0;
5249 if (n > 1)
5250 {
5251 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5252 break;
5253 u_clearline();
5254 }
5255 }
5256 else
5257 break;
5258 }
5259 }
5260#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005261 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
5263 ptr = ml_get(pos.lnum);
5264 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005265 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5266 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
5268#endif
5269
5270
5271 check_cursor();
5272 curwin->w_set_curswant = TRUE;
5273 if (did_change)
5274 {
5275 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5276 0L);
5277 curbuf->b_op_start = startpos;
5278 curbuf->b_op_end = curwin->w_cursor;
5279 if (curbuf->b_op_end.col > 0)
5280 --curbuf->b_op_end.col;
5281 }
5282}
5283
5284/*
5285 * Move cursor to mark.
5286 */
5287 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005288nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
5290 if (check_mark(pos) == FAIL)
5291 clearop(cap->oap);
5292 else
5293 {
5294 if (cap->cmdchar == '\''
5295 || cap->cmdchar == '`'
5296 || cap->cmdchar == '['
5297 || cap->cmdchar == ']')
5298 setpcmark();
5299 curwin->w_cursor = *pos;
5300 if (flag)
5301 beginline(BL_WHITE | BL_FIX);
5302 else
5303 check_cursor();
5304 }
5305 cap->oap->motion_type = flag ? MLINE : MCHAR;
5306 if (cap->cmdchar == '`')
5307 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005308 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 curwin->w_set_curswant = TRUE;
5310}
5311
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312/*
5313 * Handle commands that are operators in Visual mode.
5314 */
5315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005316v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005320 // Uppercase means linewise, except in block mode, then "D" deletes till
5321 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 if (isupper(cap->cmdchar))
5323 {
5324 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005325 {
5326 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5330 curwin->w_curswant = MAXCOL;
5331 }
5332 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5333 nv_operator(cap);
5334}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
5336/*
5337 * "s" and "S" commands.
5338 */
5339 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005340nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005342#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005343 // When showing output of term_dumpdiff() swap the top and botom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005344 if (term_swap_diff() == OK)
5345 return;
5346#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005347#ifdef FEAT_JOB_CHANNEL
5348 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5349 {
5350 clearopbeep(cap->oap);
5351 return;
5352 }
5353#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005354 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
5356 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005357 {
5358 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 cap->cmdchar = 'c';
5362 nv_operator(cap);
5363 }
5364 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 nv_optrans(cap);
5366}
5367
5368/*
5369 * Abbreviated commands.
5370 */
5371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005372nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
5374 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005375 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005377 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 if (VIsual_active)
5379 v_visop(cap);
5380 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 nv_optrans(cap);
5382}
5383
5384/*
5385 * Translate a command into another command.
5386 */
5387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005388nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389{
5390 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5391 (char_u *)"d$", (char_u *)"c$",
5392 (char_u *)"cl", (char_u *)"cc",
5393 (char_u *)"yy", (char_u *)":s\r"};
5394 static char_u *str = (char_u *)"xXDCsSY&";
5395
5396 if (!checkclearopq(cap->oap))
5397 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005398 // In Vi "2D" doesn't delete the next line. Can't translate it
5399 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005400 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5401 {
5402 cap->oap->start = curwin->w_cursor;
5403 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005404#ifdef FEAT_EVAL
5405 set_op_var(OP_DELETE);
5406#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005407 cap->count1 = 1;
5408 nv_dollar(cap);
5409 finish_op = TRUE;
5410 ResetRedobuff();
5411 AppendCharToRedobuff('D');
5412 }
5413 else
5414 {
5415 if (cap->count0)
5416 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005417 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
5420 cap->opcount = 0;
5421}
5422
5423/*
5424 * "'" and "`" commands. Also for "g'" and "g`".
5425 * cap->arg is TRUE for "'" and "g'".
5426 */
5427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005428nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 pos_T *pos;
5431 int c;
5432#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005433 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005434 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435#endif
5436
5437 if (cap->cmdchar == 'g')
5438 c = cap->extra_char;
5439 else
5440 c = cap->nchar;
5441 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005442 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 {
5444 if (cap->arg)
5445 {
5446 check_cursor_lnum();
5447 beginline(BL_WHITE | BL_FIX);
5448 }
5449 else
5450 check_cursor();
5451 }
5452 else
5453 nv_cursormark(cap, cap->arg, pos);
5454
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005455 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (!virtual_active())
5457 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005458 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459#ifdef FEAT_FOLDING
5460 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005461 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005462 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 && (fdo_flags & FDO_MARK)
5464 && old_KeyTyped)
5465 foldOpenCursor();
5466#endif
5467}
5468
5469/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005470 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 */
5472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005473nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475#ifdef FEAT_JUMPLIST
5476 pos_T *pos;
5477# ifdef FEAT_FOLDING
5478 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005479 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480# endif
5481
5482 if (!checkclearopq(cap->oap))
5483 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005484 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5485 {
5486 if (goto_tabpage_lastused() == FAIL)
5487 clearopbeep(cap->oap);
5488 return;
5489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 if (cap->cmdchar == 'g')
5491 pos = movechangelist((int)cap->count1);
5492 else
5493 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005494 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 {
5496 curwin->w_set_curswant = TRUE;
5497 check_cursor();
5498 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005499 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 nv_cursormark(cap, FALSE, pos);
5501 else if (cap->cmdchar == 'g')
5502 {
5503 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005504 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005506 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005508 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 }
5510 else
5511 clearopbeep(cap->oap);
5512# ifdef FEAT_FOLDING
5513 if (cap->oap->op_type == OP_NOP
5514 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5515 && (fdo_flags & FDO_MARK)
5516 && old_KeyTyped)
5517 foldOpenCursor();
5518# endif
5519 }
5520#else
5521 clearopbeep(cap->oap);
5522#endif
5523}
5524
5525/*
5526 * Handle '"' command.
5527 */
5528 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005529nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 if (checkclearop(cap->oap))
5532 return;
5533#ifdef FEAT_EVAL
5534 if (cap->nchar == '=')
5535 cap->nchar = get_expr_register();
5536#endif
5537 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5538 {
5539 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005540 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#ifdef FEAT_EVAL
5542 set_reg_var(cap->oap->regname);
5543#endif
5544 }
5545 else
5546 clearopbeep(cap->oap);
5547}
5548
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549/*
5550 * Handle "v", "V" and "CTRL-V" commands.
5551 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5552 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005553 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 */
5555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005556nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005558 if (cap->cmdchar == Ctrl_Q)
5559 cap->cmdchar = Ctrl_V;
5560
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005561 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5562 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 if (cap->oap->op_type != OP_NOP)
5564 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005565 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005566 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 return;
5568 }
5569
5570 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005571 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005573 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005575 else // toggle char/block mode
5576 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 VIsual_mode = cap->cmdchar;
5578 showmode();
5579 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005580 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005582 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
5584 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005585 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005587 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 VIsual = curwin->w_cursor;
5589
5590 VIsual_active = TRUE;
5591 VIsual_reselect = TRUE;
5592 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005593 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005596 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005597 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 /*
5599 * For V and ^V, we multiply the number of lines even if there
5600 * was only one -- webb
5601 */
5602 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5603 {
5604 curwin->w_cursor.lnum +=
5605 resel_VIsual_line_count * cap->count0 - 1;
5606 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5607 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5608 }
5609 VIsual_mode = resel_VIsual_mode;
5610 if (VIsual_mode == 'v')
5611 {
5612 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005613 {
5614 validate_virtcol();
5615 curwin->w_curswant = curwin->w_virtcol
5616 + resel_VIsual_vcol * cap->count0 - 1;
5617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005619 curwin->w_curswant = resel_VIsual_vcol;
5620 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005622 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 {
5624 curwin->w_curswant = MAXCOL;
5625 coladvance((colnr_T)MAXCOL);
5626 }
5627 else if (VIsual_mode == Ctrl_V)
5628 {
5629 validate_virtcol();
5630 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005631 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 coladvance(curwin->w_curswant);
5633 }
5634 else
5635 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005636 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638 else
5639 {
5640 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005641 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 may_start_select('c');
5643 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005644 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005645 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005646 if (cap->count0 > 0 && --cap->count1 > 0)
5647 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005648 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005649 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5650 nv_right(cap);
5651 else if (VIsual_mode == 'V')
5652 nv_down(cap);
5653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655 }
5656}
5657
5658/*
5659 * Start selection for Shift-movement keys.
5660 */
5661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005662start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005664 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 may_start_select('k');
5666 n_start_visual_mode('v');
5667}
5668
5669/*
5670 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5671 */
5672 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005673may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
5675 VIsual_select = (stuff_empty() && typebuf_typed()
5676 && (vim_strchr(p_slm, c) != NULL));
5677}
5678
5679/*
5680 * Start Visual mode "c".
5681 * Should set VIsual_select before calling this.
5682 */
5683 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005684n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005686#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005687 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005688 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005689#endif
5690
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 VIsual_mode = c;
5692 VIsual_active = TRUE;
5693 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005694
5695 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005696 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005698 {
5699 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 VIsual = curwin->w_cursor;
5703
5704#ifdef FEAT_FOLDING
5705 foldAdjustVisual();
5706#endif
5707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005709#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005710 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005711 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005712#endif
5713
Bram Moolenaar09df3122006-01-23 22:23:09 +00005714 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005715 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005717 // Make sure the clipboard gets updated. Needed because start and
5718 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 clip_star.vmode = NUL;
5720#endif
5721
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005722 // Only need to redraw this line, unless still need to redraw an old
5723 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 if (curwin->w_redr_type < INVERTED)
5725 {
5726 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5727 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5728 }
5729}
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732/*
5733 * CTRL-W: Window commands
5734 */
5735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005736nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005738 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005739 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005740 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005741 cap->cmdchar = ':';
5742 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005743 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005744 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005745 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005746 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747}
5748
5749/*
5750 * CTRL-Z: Suspend
5751 */
5752 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005753nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
5755 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005757 end_visual_mode(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 do_cmdline_cmd((char_u *)"st");
5759}
5760
5761/*
5762 * Commands starting with "g".
5763 */
5764 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005765nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 int i;
5770 int flag = FALSE;
5771
5772 switch (cap->nchar)
5773 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005774 case Ctrl_A:
5775 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776#ifdef MEM_PROFILE
5777 /*
5778 * "g^A": dump log of used memory.
5779 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005780 if (!VIsual_active && cap->nchar == Ctrl_A)
5781 vim_mem_profile_dump();
5782 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005784 /*
5785 * "g^A/g^X": sequentially increment visually selected region
5786 */
5787 if (VIsual_active)
5788 {
5789 cap->arg = TRUE;
5790 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005791 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005792 nv_addsub(cap);
5793 }
5794 else
5795 clearopbeep(oap);
5796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 /*
5799 * "gR": Enter virtual replace mode.
5800 */
5801 case 'R':
5802 cap->arg = TRUE;
5803 nv_Replace(cap);
5804 break;
5805
5806 case 'r':
5807 nv_vreplace(cap);
5808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
5810 case '&':
5811 do_cmdline_cmd((char_u *)"%s//~/&");
5812 break;
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 /*
5815 * "gv": Reselect the previous Visual area. If Visual already active,
5816 * exchange previous and current Visual area.
5817 */
5818 case 'v':
5819 if (checkclearop(oap))
5820 break;
5821
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005822 if ( curbuf->b_visual.vi_start.lnum == 0
5823 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5824 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 beep_flush();
5826 else
5827 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005828 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 if (VIsual_active)
5830 {
5831 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005832 VIsual_mode = curbuf->b_visual.vi_mode;
5833 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834# ifdef FEAT_EVAL
5835 curbuf->b_visual_mode_eval = i;
5836# endif
5837 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005838 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5839 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005841 tpos = curbuf->b_visual.vi_end;
5842 curbuf->b_visual.vi_end = curwin->w_cursor;
5843 curwin->w_cursor = curbuf->b_visual.vi_start;
5844 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 }
5846 else
5847 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005848 VIsual_mode = curbuf->b_visual.vi_mode;
5849 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5850 tpos = curbuf->b_visual.vi_end;
5851 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 }
5853
5854 VIsual_active = TRUE;
5855 VIsual_reselect = TRUE;
5856
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005857 // Set Visual to the start and w_cursor to the end of the Visual
5858 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 check_cursor();
5860 VIsual = curwin->w_cursor;
5861 curwin->w_cursor = tpos;
5862 check_cursor();
5863 update_topline();
5864 /*
5865 * When called from normal "g" command: start Select mode when
5866 * 'selectmode' contains "cmd". When called for K_SELECT, always
5867 * start Select mode.
5868 */
5869 if (cap->arg)
5870 VIsual_select = TRUE;
5871 else
5872 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005875 // Make sure the clipboard gets updated. Needed because start and
5876 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 clip_star.vmode = NUL;
5878#endif
5879 redraw_curbuf_later(INVERTED);
5880 showmode();
5881 }
5882 break;
5883 /*
5884 * "gV": Don't reselect the previous Visual area after a Select mode
5885 * mapping of menu.
5886 */
5887 case 'V':
5888 VIsual_reselect = FALSE;
5889 break;
5890
5891 /*
5892 * "gh": start Select mode.
5893 * "gH": start Select line mode.
5894 * "g^H": start Select block mode.
5895 */
5896 case K_BS:
5897 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005898 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 case 'h':
5900 case 'H':
5901 case Ctrl_H:
5902# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005903 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 if (cap->nchar == Ctrl_H)
5905 cap->cmdchar = Ctrl_V;
5906 else
5907# endif
5908 cap->cmdchar = cap->nchar + ('v' - 'h');
5909 cap->arg = TRUE;
5910 nv_visual(cap);
5911 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005912
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005913 // "gn", "gN" visually select next/previous search match
5914 // "gn" selects next match
5915 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005916 case 'N':
5917 case 'n':
5918 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005919 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005920 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
5922 /*
5923 * "gj" and "gk" two new funny movement keys -- up and down
5924 * movement based on *screen* line rather than *file* line.
5925 */
5926 case 'j':
5927 case K_DOWN:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005928 // with 'nowrap' it works just like the normal "j" command; also when
5929 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 if (!curwin->w_p_wrap
5931#ifdef FEAT_FOLDING
5932 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5933#endif
5934 )
5935 {
5936 oap->motion_type = MLINE;
5937 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5938 }
5939 else
5940 i = nv_screengo(oap, FORWARD, cap->count1);
5941 if (i == FAIL)
5942 clearopbeep(oap);
5943 break;
5944
5945 case 'k':
5946 case K_UP:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005947 // with 'nowrap' it works just like the normal "k" command; also when
5948 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 if (!curwin->w_p_wrap
5950#ifdef FEAT_FOLDING
5951 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5952#endif
5953 )
5954 {
5955 oap->motion_type = MLINE;
5956 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5957 }
5958 else
5959 i = nv_screengo(oap, BACKWARD, cap->count1);
5960 if (i == FAIL)
5961 clearopbeep(oap);
5962 break;
5963
5964 /*
5965 * "gJ": join two lines without inserting a space.
5966 */
5967 case 'J':
5968 nv_join(cap);
5969 break;
5970
5971 /*
5972 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
5973 * "gm": middle of "g0" and "g$".
5974 */
5975 case '^':
5976 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005977 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978
5979 case '0':
5980 case 'm':
5981 case K_HOME:
5982 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 oap->motion_type = MCHAR;
5984 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005985 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005987 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 int width2 = width1 + curwin_col_off2();
5989
5990 validate_virtcol();
5991 i = 0;
5992 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5993 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5994 }
5995 else
5996 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005997 // Go to the middle of the screen line. When 'number' or
5998 // 'relativenumber' is on and lines are wrapping the middle can be more
5999 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006001 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 + ((curwin->w_p_wrap && i > 0)
6003 ? curwin_col_off2() : 0)) / 2;
6004 coladvance((colnr_T)i);
6005 if (flag)
6006 {
6007 do
6008 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006009 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006010 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 }
6012 curwin->w_set_curswant = TRUE;
6013 break;
6014
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006015 case 'M':
6016 {
6017 char_u *ptr = ml_get_curline();
6018
6019 oap->motion_type = MCHAR;
6020 oap->inclusive = FALSE;
6021 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006022 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006023 else
6024 i = (int)STRLEN(ptr);
6025 if (cap->count0 > 0 && cap->count0 <= 100)
6026 coladvance((colnr_T)(i * cap->count0 / 100));
6027 else
6028 coladvance((colnr_T)(i / 2));
6029 curwin->w_set_curswant = TRUE;
6030 }
6031 break;
6032
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006034 // "g_": to the last non-blank character in the line or <count> lines
6035 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 cap->oap->motion_type = MCHAR;
6037 cap->oap->inclusive = TRUE;
6038 curwin->w_curswant = MAXCOL;
6039 if (cursor_down((long)(cap->count1 - 1),
6040 cap->oap->op_type == OP_NOP) == FAIL)
6041 clearopbeep(cap->oap);
6042 else
6043 {
6044 char_u *ptr = ml_get_curline();
6045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006046 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6048 --curwin->w_cursor.col;
6049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006050 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006052 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 --curwin->w_cursor.col;
6054 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006055 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 }
6057 break;
6058
6059 case '$':
6060 case K_END:
6061 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 {
6063 int col_off = curwin_col_off();
6064
6065 oap->motion_type = MCHAR;
6066 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006067 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006069 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (cap->count1 == 1)
6071 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006072 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 int width2 = width1 + curwin_col_off2();
6074
6075 validate_virtcol();
6076 i = width1 - 1;
6077 if (curwin->w_virtcol >= (colnr_T)width1)
6078 i += ((curwin->w_virtcol - width1) / width2 + 1)
6079 * width2;
6080 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006082 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006083 validate_virtcol();
6084 curwin->w_curswant = curwin->w_virtcol;
6085 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6087 {
6088 /*
6089 * Check for landing on a character that got split at
6090 * the end of the line. We do not want to advance to
6091 * the next screen line.
6092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (curwin->w_virtcol > (colnr_T)i)
6094 --curwin->w_cursor.col;
6095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 }
6097 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6098 clearopbeep(oap);
6099 }
6100 else
6101 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006102 if (cap->count1 > 1)
6103 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006104 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006105
Bram Moolenaar02631462017-09-22 15:20:32 +02006106 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006108
Bram Moolenaard5c82342019-07-27 18:44:57 +02006109 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006110 validate_virtcol();
6111 curwin->w_curswant = curwin->w_virtcol;
6112 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 }
6114 }
6115 break;
6116
6117 /*
6118 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6119 */
6120 case '*':
6121 case '#':
6122#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006123 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006125 case Ctrl_RSB: // :tag or :tselect for current identifier
6126 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 nv_ident(cap);
6128 break;
6129
6130 /*
6131 * ge and gE: go back to end of word
6132 */
6133 case 'e':
6134 case 'E':
6135 oap->motion_type = MCHAR;
6136 curwin->w_set_curswant = TRUE;
6137 oap->inclusive = TRUE;
6138 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6139 clearopbeep(oap);
6140 break;
6141
6142 /*
6143 * "g CTRL-G": display info about cursor position
6144 */
6145 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006146 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 break;
6148
6149 /*
6150 * "gi": start Insert at the last position.
6151 */
6152 case 'i':
6153 if (curbuf->b_last_insert.lnum != 0)
6154 {
6155 curwin->w_cursor = curbuf->b_last_insert;
6156 check_cursor_lnum();
6157 i = (int)STRLEN(ml_get_curline());
6158 if (curwin->w_cursor.col > (colnr_T)i)
6159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (virtual_active())
6161 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 curwin->w_cursor.col = i;
6163 }
6164 }
6165 cap->cmdchar = 'i';
6166 nv_edit(cap);
6167 break;
6168
6169 /*
6170 * "gI": Start insert in column 1.
6171 */
6172 case 'I':
6173 beginline(0);
6174 if (!checkclearopq(oap))
6175 invoke_edit(cap, FALSE, 'g', FALSE);
6176 break;
6177
6178#ifdef FEAT_SEARCHPATH
6179 /*
6180 * "gf": goto file, edit file under cursor
6181 * "]f" and "[f": can also be used.
6182 */
6183 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006184 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 nv_gotofile(cap);
6186 break;
6187#endif
6188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006189 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 case '\'':
6191 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006192 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 case '`':
6194 nv_gomark(cap);
6195 break;
6196
6197 /*
6198 * "gs": Goto sleep.
6199 */
6200 case 's':
6201 do_sleep(cap->count1 * 1000L);
6202 break;
6203
6204 /*
6205 * "ga": Display the ascii value of the character under the
6206 * cursor. It is displayed in decimal, hex, and octal. -- webb
6207 */
6208 case 'a':
6209 do_ascii(NULL);
6210 break;
6211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 /*
6213 * "g8": Display the bytes used for the UTF-8 character under the
6214 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006215 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 */
6217 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006218 if (cap->count0 == 8)
6219 utf_find_illegal();
6220 else
6221 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006224 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006225 case '<':
6226 show_sb_text();
6227 break;
6228
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 /*
6230 * "gg": Goto the first line in file. With a count it goes to
6231 * that line number like for "G". -- webb
6232 */
6233 case 'g':
6234 cap->arg = FALSE;
6235 nv_goto(cap);
6236 break;
6237
6238 /*
6239 * Two-character operators:
6240 * "gq" Format text
6241 * "gw" Format text and keep cursor position
6242 * "g~" Toggle the case of the text.
6243 * "gu" Change text to lower case.
6244 * "gU" Change text to upper case.
6245 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006246 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 */
6248 case 'q':
6249 case 'w':
6250 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006251 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 case '~':
6253 case 'u':
6254 case 'U':
6255 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006256 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 nv_operator(cap);
6258 break;
6259
6260 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006261 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 * current function
6263 * "gD": idem, but in the current file.
6264 */
6265 case 'd':
6266 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006267 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 break;
6269
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 /*
6271 * g<*Mouse> : <C-*mouse>
6272 */
6273 case K_MIDDLEMOUSE:
6274 case K_MIDDLEDRAG:
6275 case K_MIDDLERELEASE:
6276 case K_LEFTMOUSE:
6277 case K_LEFTDRAG:
6278 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006279 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 case K_RIGHTMOUSE:
6281 case K_RIGHTDRAG:
6282 case K_RIGHTRELEASE:
6283 case K_X1MOUSE:
6284 case K_X1DRAG:
6285 case K_X1RELEASE:
6286 case K_X2MOUSE:
6287 case K_X2DRAG:
6288 case K_X2RELEASE:
6289 mod_mask = MOD_MASK_CTRL;
6290 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292
6293 case K_IGNORE:
6294 break;
6295
6296 /*
6297 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6298 */
6299 case 'p':
6300 case 'P':
6301 nv_put(cap);
6302 break;
6303
6304#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006305 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 case 'o':
6307 goto_byte(cap->count0);
6308 break;
6309#endif
6310
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006311 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006313 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 {
6315 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006316 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 break;
6318 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006319
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 if (!checkclearopq(oap))
6321 do_exmode(TRUE);
6322 break;
6323
6324#ifdef FEAT_JUMPLIST
6325 case ',':
6326 nv_pcmark(cap);
6327 break;
6328
6329 case ';':
6330 cap->count1 = -cap->count1;
6331 nv_pcmark(cap);
6332 break;
6333#endif
6334
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006335 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006336 if (!checkclearop(oap))
6337 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006338 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006339 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006340 if (!checkclearop(oap))
6341 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006342 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006343
Bram Moolenaar62a23252020-08-09 14:04:42 +02006344 case TAB:
6345 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6346 clearopbeep(oap);
6347 break;
6348
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006349 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006350 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006351 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006352 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006353 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006354 break;
6355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 default:
6357 clearopbeep(oap);
6358 break;
6359 }
6360}
6361
6362/*
6363 * Handle "o" and "O" commands.
6364 */
6365 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006366n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006368#ifdef FEAT_CONCEAL
6369 linenr_T oldline = curwin->w_cursor.lnum;
6370#endif
6371
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (!checkclearopq(cap->oap))
6373 {
6374#ifdef FEAT_FOLDING
6375 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006376 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 (void)hasFolding(curwin->w_cursor.lnum,
6378 &curwin->w_cursor.lnum, NULL);
6379 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006380 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 (void)hasFolding(curwin->w_cursor.lnum,
6382 NULL, &curwin->w_cursor.lnum);
6383#endif
6384 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6385 (cap->cmdchar == 'O' ? 1 : 0)),
6386 (linenr_T)(curwin->w_cursor.lnum +
6387 (cap->cmdchar == 'o' ? 1 : 0))
6388 ) == OK
6389 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006390 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6391 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006393#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006394 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006395 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006396#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006397#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006398 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006399 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006400 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006401#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006402 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006403 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6404 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6406 }
6407 }
6408}
6409
6410/*
6411 * "." command: redo last change.
6412 */
6413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006414nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 if (!checkclearopq(cap->oap))
6417 {
6418 /*
6419 * If "restart_edit" is TRUE, the last but one command is repeated
6420 * instead of the last command (inserting text). This is used for
6421 * CTRL-O <.> in insert mode.
6422 */
6423 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6424 clearopbeep(cap->oap);
6425 }
6426}
6427
6428/*
6429 * CTRL-R: undo undo
6430 */
6431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006432nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 if (!checkclearopq(cap->oap))
6435 {
6436 u_redo((int)cap->count1);
6437 curwin->w_set_curswant = TRUE;
6438 }
6439}
6440
6441/*
6442 * Handle "U" command.
6443 */
6444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006445nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006447 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006448 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006450 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 cap->cmdchar = 'g';
6452 cap->nchar = 'U';
6453 nv_operator(cap);
6454 }
6455 else if (!checkclearopq(cap->oap))
6456 {
6457 u_undoline();
6458 curwin->w_set_curswant = TRUE;
6459 }
6460}
6461
6462/*
6463 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6464 * single character.
6465 */
6466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006467nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006469 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006470 {
6471#ifdef FEAT_JOB_CHANNEL
6472 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6473 {
6474 clearopbeep(cap->oap);
6475 return;
6476 }
6477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 else
6481 nv_operator(cap);
6482}
6483
6484/*
6485 * Handle an operator command.
6486 * The actual work is done by do_pending_operator().
6487 */
6488 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006489nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491 int op_type;
6492
6493 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006494#ifdef FEAT_JOB_CHANNEL
6495 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6496 {
6497 clearopbeep(cap->oap);
6498 return;
6499 }
6500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006502 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 nv_lineop(cap);
6504 else if (!checkclearop(cap->oap))
6505 {
6506 cap->oap->start = curwin->w_cursor;
6507 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006508#ifdef FEAT_EVAL
6509 set_op_var(op_type);
6510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 }
6512}
6513
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006514#ifdef FEAT_EVAL
6515/*
6516 * Set v:operator to the characters for "optype".
6517 */
6518 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006519set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006520{
6521 char_u opchars[3];
6522
6523 if (optype == OP_NOP)
6524 set_vim_var_string(VV_OP, NULL, 0);
6525 else
6526 {
6527 opchars[0] = get_op_char(optype);
6528 opchars[1] = get_extra_op_char(optype);
6529 opchars[2] = NUL;
6530 set_vim_var_string(VV_OP, opchars, -1);
6531 }
6532}
6533#endif
6534
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535/*
6536 * Handle linewise operator "dd", "yy", etc.
6537 *
6538 * "_" is is a strange motion command that helps make operators more logical.
6539 * It is actually implemented, but not documented in the real Vi. This motion
6540 * command actually refers to "the current line". Commands like "dd" and "yy"
6541 * are really an alternate form of "d_" and "y_". It does accept a count, so
6542 * "d3_" works to delete 3 lines.
6543 */
6544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006545nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 cap->oap->motion_type = MLINE;
6548 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6549 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006550 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006551 && cap->oap->motion_force != 'v'
6552 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 || cap->oap->op_type == OP_LSHIFT
6554 || cap->oap->op_type == OP_RSHIFT)
6555 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006556 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 beginline(BL_WHITE | BL_FIX);
6558}
6559
6560/*
6561 * <Home> command.
6562 */
6563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006564nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006566 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006567 if (mod_mask & MOD_MASK_CTRL)
6568 nv_goto(cap);
6569 else
6570 {
6571 cap->count0 = 1;
6572 nv_pipe(cap);
6573 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006574 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6575 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576}
6577
6578/*
6579 * "|" command.
6580 */
6581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006582nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 cap->oap->motion_type = MCHAR;
6585 cap->oap->inclusive = FALSE;
6586 beginline(0);
6587 if (cap->count0 > 0)
6588 {
6589 coladvance((colnr_T)(cap->count0 - 1));
6590 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6591 }
6592 else
6593 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006594 // keep curswant at the column where we wanted to go, not where
6595 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 curwin->w_set_curswant = FALSE;
6597}
6598
6599/*
6600 * Handle back-word command "b" and "B".
6601 * cap->arg is 1 for "B"
6602 */
6603 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006604nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606 cap->oap->motion_type = MCHAR;
6607 cap->oap->inclusive = FALSE;
6608 curwin->w_set_curswant = TRUE;
6609 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6610 clearopbeep(cap->oap);
6611#ifdef FEAT_FOLDING
6612 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6613 foldOpenCursor();
6614#endif
6615}
6616
6617/*
6618 * Handle word motion commands "e", "E", "w" and "W".
6619 * cap->arg is TRUE for "E" and "W".
6620 */
6621 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006622nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 int n;
6625 int word_end;
6626 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006627 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 /*
6630 * Set inclusive for the "E" and "e" command.
6631 */
6632 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6633 word_end = TRUE;
6634 else
6635 word_end = FALSE;
6636 cap->oap->inclusive = word_end;
6637
6638 /*
6639 * "cw" and "cW" are a special case.
6640 */
6641 if (!word_end && cap->oap->op_type == OP_CHANGE)
6642 {
6643 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006644 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006646 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {
6648 /*
6649 * Reproduce a funny Vi behaviour: "cw" on a blank only
6650 * changes one character, not all blanks until the start of
6651 * the next word. Only do this when the 'w' flag is included
6652 * in 'cpoptions'.
6653 */
6654 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6655 {
6656 cap->oap->inclusive = TRUE;
6657 cap->oap->motion_type = MCHAR;
6658 return;
6659 }
6660 }
6661 else
6662 {
6663 /*
6664 * This is a little strange. To match what the real Vi does,
6665 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6666 * that we are not on a space or a TAB. This seems impolite
6667 * at first, but it's really more what we mean when we say
6668 * 'cw'.
6669 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006670 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 * will change only one character! This is done by setting
6672 * flag.
6673 */
6674 cap->oap->inclusive = TRUE;
6675 word_end = TRUE;
6676 flag = TRUE;
6677 }
6678 }
6679 }
6680
6681 cap->oap->motion_type = MCHAR;
6682 curwin->w_set_curswant = TRUE;
6683 if (word_end)
6684 n = end_word(cap->count1, cap->arg, flag, FALSE);
6685 else
6686 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6687
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006688 // Don't leave the cursor on the NUL past the end of line. Unless we
6689 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006690 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006691 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
6693 if (n == FAIL && cap->oap->op_type == OP_NOP)
6694 clearopbeep(cap->oap);
6695 else
6696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698#ifdef FEAT_FOLDING
6699 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6700 foldOpenCursor();
6701#endif
6702 }
6703}
6704
6705/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006706 * Used after a movement command: If the cursor ends up on the NUL after the
6707 * end of the line, may move it back to the last character and make the motion
6708 * inclusive.
6709 */
6710 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006711adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006712{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006713 // The cursor cannot remain on the NUL when:
6714 // - the column is > 0
6715 // - not in Visual mode or 'selection' is "o"
6716 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006717 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006718 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006719 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006720 {
6721 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006722 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006723 if (has_mbyte)
6724 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006725 oap->inclusive = TRUE;
6726 }
6727}
6728
6729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 * "0" and "^" commands.
6731 * cap->arg is the argument for beginline().
6732 */
6733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006734nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 cap->oap->motion_type = MCHAR;
6737 cap->oap->inclusive = FALSE;
6738 beginline(cap->arg);
6739#ifdef FEAT_FOLDING
6740 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6741 foldOpenCursor();
6742#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006743 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6744 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745}
6746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747/*
6748 * In exclusive Visual mode, may include the last character.
6749 */
6750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006751adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006754 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (has_mbyte)
6757 inc_cursor();
6758 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 ++curwin->w_cursor.col;
6760 cap->oap->inclusive = FALSE;
6761 }
6762}
6763
6764/*
6765 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6766 * Should check VIsual_mode before calling this.
6767 * Returns TRUE when backed up to the previous line.
6768 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006769 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006770unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771{
6772 pos_T *pp;
6773
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006774 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006776 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 pp = &curwin->w_cursor;
6778 else
6779 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 if (pp->coladd > 0)
6781 --pp->coladd;
6782 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 if (pp->col > 0)
6784 {
6785 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006786 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 }
6788 else if (pp->lnum > 1)
6789 {
6790 --pp->lnum;
6791 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6792 return TRUE;
6793 }
6794 }
6795 return FALSE;
6796}
6797
6798/*
6799 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6800 */
6801 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006802nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803{
6804 if (VIsual_active)
6805 VIsual_select = TRUE;
6806 else if (VIsual_reselect)
6807 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006808 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 cap->arg = TRUE;
6810 nv_g_cmd(cap);
6811 }
6812}
6813
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
6815/*
6816 * "G", "gg", CTRL-END, CTRL-HOME.
6817 * cap->arg is TRUE for "G".
6818 */
6819 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006820nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 linenr_T lnum;
6823
6824 if (cap->arg)
6825 lnum = curbuf->b_ml.ml_line_count;
6826 else
6827 lnum = 1L;
6828 cap->oap->motion_type = MLINE;
6829 setpcmark();
6830
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006831 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 if (cap->count0 != 0)
6833 lnum = cap->count0;
6834 if (lnum < 1L)
6835 lnum = 1L;
6836 else if (lnum > curbuf->b_ml.ml_line_count)
6837 lnum = curbuf->b_ml.ml_line_count;
6838 curwin->w_cursor.lnum = lnum;
6839 beginline(BL_SOL | BL_FIX);
6840#ifdef FEAT_FOLDING
6841 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6842 foldOpenCursor();
6843#endif
6844}
6845
6846/*
6847 * CTRL-\ in Normal mode.
6848 */
6849 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006850nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851{
6852 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6853 {
6854 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006855 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006856 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 restart_edit = 0;
6858#ifdef FEAT_CMDWIN
6859 if (cmdwin_type != 0)
6860 cmdwin_result = Ctrl_C;
6861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 if (VIsual_active)
6863 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006864 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 redraw_curbuf_later(INVERTED);
6866 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006867 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 if (cap->nchar == Ctrl_G && p_im)
6869 restart_edit = 'a';
6870 }
6871 else
6872 clearopbeep(cap->oap);
6873}
6874
6875/*
6876 * ESC in Normal mode: beep, but don't flush buffers.
6877 * Don't even beep if we are canceling a command.
6878 */
6879 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006880nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881{
6882 int no_reason;
6883
6884 no_reason = (cap->oap->op_type == OP_NOP
6885 && cap->opcount == 0
6886 && cap->count0 == 0
6887 && cap->oap->regname == 0
6888 && !p_im);
6889
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006890 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
6892 if (restart_edit == 0
6893#ifdef FEAT_CMDWIN
6894 && cmdwin_type == 0
6895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006898 {
6899 if (anyBufIsChanged())
6900 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6901 else
6902 msg(_("Type :qa and press <Enter> to exit Vim"));
6903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6906 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (!p_im)
6908 restart_edit = 0;
6909#ifdef FEAT_CMDWIN
6910 if (cmdwin_type != 0)
6911 {
6912 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006913 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 return;
6915 }
6916#endif
6917 }
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 if (VIsual_active)
6920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006921 end_visual_mode(); // stop Visual
6922 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 curwin->w_set_curswant = TRUE;
6924 redraw_curbuf_later(INVERTED);
6925 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006926 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006927 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 clearop(cap->oap);
6929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006930 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6931 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006932 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 restart_edit = 'a';
6934}
6935
6936/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006937 * Move the cursor for the "A" command.
6938 */
6939 void
6940set_cursor_for_append_to_line(void)
6941{
6942 curwin->w_set_curswant = TRUE;
6943 if (ve_flags == VE_ALL)
6944 {
6945 int save_State = State;
6946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006947 // Pretend Insert mode here to allow the cursor on the
6948 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006949 State = INSERT;
6950 coladvance((colnr_T)MAXCOL);
6951 State = save_State;
6952 }
6953 else
6954 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6955}
6956
6957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006959 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 */
6961 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006962nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006964 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6966 cap->cmdchar = 'i';
6967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006968 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006970 {
6971#ifdef FEAT_TERMINAL
6972 if (term_in_normal_mode())
6973 {
6974 end_visual_mode();
6975 clearop(cap->oap);
6976 term_enter_job_mode();
6977 return;
6978 }
6979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006983 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006984 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6985 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 {
6987#ifdef FEAT_TEXTOBJ
6988 nv_object(cap);
6989#else
6990 clearopbeep(cap->oap);
6991#endif
6992 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006993#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006994 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006995 {
6996 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006997 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006998 return;
6999 }
7000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 else if (!curbuf->b_p_ma && !p_im)
7002 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007003 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007004 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007006 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007007 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007008 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007010 else if (cap->cmdchar == K_PS && VIsual_active)
7011 {
7012 pos_T old_pos = curwin->w_cursor;
7013 pos_T old_visual = VIsual;
7014
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007015 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007016 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7017 {
7018 shift_delete_registers();
7019 cap->oap->regname = '1';
7020 }
7021 else
7022 cap->oap->regname = '-';
7023 cap->cmdchar = 'd';
7024 cap->nchar = NUL;
7025 nv_operator(cap);
7026 do_pending_operator(cap, 0, FALSE);
7027 cap->cmdchar = K_PS;
7028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007029 // When the last char in the line was deleted then append. Detect this
7030 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007031 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7032 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007033 inc_cursor();
7034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007035 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007036 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 else if (!checkclearopq(cap->oap))
7039 {
7040 switch (cap->cmdchar)
7041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007042 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007043 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 break;
7045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007046 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007047 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7048 beginline(BL_WHITE);
7049 else
7050 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 break;
7052
Bram Moolenaara1891842017-02-04 21:34:31 +01007053 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007054 // Bracketed paste works like "a"ppend, unless the cursor is in
7055 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007056 if (curwin->w_cursor.col == 0)
7057 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007060 case 'a': // "a"ppend is like "i"nsert on the next character.
7061 // increment coladd when in virtual space, increment the
7062 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 if (virtual_active()
7064 && (curwin->w_cursor.coladd > 0
7065 || *ml_get_cursor() == NUL
7066 || *ml_get_cursor() == TAB))
7067 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007068 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 inc_cursor();
7070 break;
7071 }
7072
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7074 {
7075 int save_State = State;
7076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007077 // Pretend Insert mode here to allow the cursor on the
7078 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 State = INSERT;
7080 coladvance(getviscol());
7081 State = save_State;
7082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
7084 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7085 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007086 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007087 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007088 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089}
7090
7091/*
7092 * Invoke edit() and take care of "restart_edit" and the return value.
7093 */
7094 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007095invoke_edit(
7096 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007097 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007098 int cmd,
7099 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 int restart_edit_save = 0;
7102
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007103 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7104 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7105 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (repl || !stuff_empty())
7107 restart_edit_save = restart_edit;
7108 else
7109 restart_edit_save = 0;
7110
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007111 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 restart_edit = 0;
7113
7114 if (edit(cmd, startln, cap->count1))
7115 cap->retval |= CA_COMMAND_BUSY;
7116
7117 if (restart_edit == 0)
7118 restart_edit = restart_edit_save;
7119}
7120
7121#ifdef FEAT_TEXTOBJ
7122/*
7123 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7124 */
7125 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007126nv_object(
7127 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 int flag;
7130 int include;
7131 char_u *mps_save;
7132
7133 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007134 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007136 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007138 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 mps_save = curbuf->b_p_mps;
7140 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7141
7142 switch (cap->nchar)
7143 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007144 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 flag = current_word(cap->oap, cap->count1, include, FALSE);
7146 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007147 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 flag = current_word(cap->oap, cap->count1, include, TRUE);
7149 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007150 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 case '(':
7152 case ')':
7153 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7154 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007155 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 case '{':
7157 case '}':
7158 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7159 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007160 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 case ']':
7162 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7163 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007164 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 case '>':
7166 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7167 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007168 case 't': // "at" = a tag block (xml and html)
7169 // Do not adjust oap->end in do_pending_operator()
7170 // otherwise there are different results for 'dit'
7171 // (note leading whitespace in last line):
7172 // 1) <b> 2) <b>
7173 // foobar foobar
7174 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007175 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007176 flag = current_tagblock(cap->oap, cap->count1, include);
7177 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007178 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 flag = current_par(cap->oap, cap->count1, include, 'p');
7180 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 flag = current_sent(cap->oap, cap->count1, include);
7183 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007184 case '"': // "a"" = a double quoted string
7185 case '\'': // "a'" = a single quoted string
7186 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007187 flag = current_quote(cap->oap, cap->count1, include,
7188 cap->nchar);
7189 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007190#if 0 // TODO
7191 case 'S': // "aS" = a section
7192 case 'f': // "af" = a filename
7193 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194#endif
7195 default:
7196 flag = FAIL;
7197 break;
7198 }
7199
7200 curbuf->b_p_mps = mps_save;
7201 if (flag == FAIL)
7202 clearopbeep(cap->oap);
7203 adjust_cursor_col();
7204 curwin->w_set_curswant = TRUE;
7205}
7206#endif
7207
7208/*
7209 * "q" command: Start/stop recording.
7210 * "q:", "q/", "q?": edit command-line in command-line window.
7211 */
7212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007213nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 if (cap->oap->op_type == OP_FORMAT)
7216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007217 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 cap->cmdchar = 'g';
7219 cap->nchar = 'q';
7220 nv_operator(cap);
7221 }
7222 else if (!checkclearop(cap->oap))
7223 {
7224#ifdef FEAT_CMDWIN
7225 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7226 {
7227 stuffcharReadbuff(cap->nchar);
7228 stuffcharReadbuff(K_CMDWIN);
7229 }
7230 else
7231#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007232 // (stop) recording into a named register, unless executing a
7233 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007234 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 clearopbeep(cap->oap);
7236 }
7237}
7238
7239/*
7240 * Handle the "@r" command.
7241 */
7242 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007243nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
7245 if (checkclearop(cap->oap))
7246 return;
7247#ifdef FEAT_EVAL
7248 if (cap->nchar == '=')
7249 {
7250 if (get_expr_register() == NUL)
7251 return;
7252 }
7253#endif
7254 while (cap->count1-- && !got_int)
7255 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007256 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
7258 clearopbeep(cap->oap);
7259 break;
7260 }
7261 line_breakcheck();
7262 }
7263}
7264
7265/*
7266 * Handle the CTRL-U and CTRL-D commands.
7267 */
7268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007269nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
7271 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7272 || (cap->cmdchar == Ctrl_D
7273 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7274 clearopbeep(cap->oap);
7275 else if (!checkclearop(cap->oap))
7276 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7277}
7278
7279/*
7280 * Handle "J" or "gJ" command.
7281 */
7282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007283nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007285 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007287 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
7289 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007290 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7292 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007294 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007295 if (cap->count0 <= 2)
7296 {
7297 clearopbeep(cap->oap);
7298 return;
7299 }
7300 cap->count0 = curbuf->b_ml.ml_line_count
7301 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007303
7304 prep_redo(cap->oap->regname, cap->count0,
7305 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7306 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 }
7308}
7309
7310/*
7311 * "P", "gP", "p" and "gp" commands.
7312 */
7313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007314nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007316 nv_put_opt(cap, FALSE);
7317}
7318
7319/*
7320 * "P", "gP", "p" and "gp" commands.
7321 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7322 */
7323 static void
7324nv_put_opt(cmdarg_T *cap, int fix_indent)
7325{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 int regname = 0;
7327 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007328 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007329 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 int dir;
7331 int flags = 0;
7332
7333 if (cap->oap->op_type != OP_NOP)
7334 {
7335#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007336 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7338 {
7339 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007340 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 }
7342 else
7343#endif
7344 clearopbeep(cap->oap);
7345 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007346#ifdef FEAT_JOB_CHANNEL
7347 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7348 {
7349 clearopbeep(cap->oap);
7350 }
7351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 else
7353 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007354 if (fix_indent)
7355 {
7356 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7357 ? FORWARD : BACKWARD;
7358 flags |= PUT_FIXINDENT;
7359 }
7360 else
7361 dir = (cap->cmdchar == 'P'
7362 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 ? BACKWARD : FORWARD;
7364 prep_redo_cmd(cap);
7365 if (cap->cmdchar == 'g')
7366 flags |= PUT_CURSEND;
7367
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 if (VIsual_active)
7369 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007370 // Putting in Visual mode: The put text replaces the selected
7371 // text. First delete the selected text, then put the new text.
7372 // Need to save and restore the registers that the delete
7373 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007374 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007376#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007378#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007379 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007380 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007381#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
7385 )
7386 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007387 // The delete is going to overwrite the register we want to
7388 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 reg1 = get_register(regname, TRUE);
7390 }
7391
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007392 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 cap->cmdchar = 'd';
7394 cap->nchar = NUL;
7395 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007396 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 nv_operator(cap);
7398 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007399 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007400 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007402 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 cap->oap->regname = regname;
7404
7405 if (reg1 != NULL)
7406 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007407 // Delete probably changed the register we want to put, save
7408 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 reg2 = get_register(regname, FALSE);
7410 put_register(regname, reg1);
7411 }
7412
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007413 // When deleted a linewise Visual area, put the register as
7414 // lines to avoid it joined with the next line. When deletion was
7415 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 if (VIsual_mode == 'V')
7417 flags |= PUT_LINE;
7418 else if (VIsual_mode == 'v')
7419 flags |= PUT_LINE_SPLIT;
7420 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7421 flags |= PUT_LINE_FORWARD;
7422 dir = BACKWARD;
7423 if ((VIsual_mode != 'V'
7424 && curwin->w_cursor.col < curbuf->b_op_start.col)
7425 || (VIsual_mode == 'V'
7426 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007427 // cursor is at the end of the line or end of file, put
7428 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007430 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007431 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007433 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007435 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 if (reg2 != NULL)
7437 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007439 // What to reselect with "gv"? Selecting the just put text seems to
7440 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007441 if (was_visual)
7442 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007443 curbuf->b_visual.vi_start = curbuf->b_op_start;
7444 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007445 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007446 if (*p_sel == 'e')
7447 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007448 }
7449
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007450 // When all lines were selected and deleted do_put() leaves an empty
7451 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007452 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007453 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007454 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007455 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007457 // If the cursor was in that line, move it to the end of the last
7458 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007459 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7460 {
7461 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7462 coladvance((colnr_T)MAXCOL);
7463 }
7464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 auto_format(FALSE, TRUE);
7466 }
7467}
7468
7469/*
7470 * "o" and "O" commands.
7471 */
7472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007473nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007476 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7478 {
7479 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007480 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482 else
7483#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007484 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007486#ifdef FEAT_JOB_CHANNEL
7487 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007488 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 n_opencmd(cap);
7492}
7493
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494#ifdef FEAT_NETBEANS_INTG
7495 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007496nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 netbeans_keycommand(cap->nchar);
7499}
7500#endif
7501
7502#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007504nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007506 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507}
7508#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007509
Bram Moolenaar3918c952005-03-15 22:34:55 +00007510/*
7511 * Trigger CursorHold event.
7512 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7513 * input buffer. "did_cursorhold" is set to avoid retriggering.
7514 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007515 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007516nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007517{
7518 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7519 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007520 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007521}