blob: af4d3b2885459b40a0fa39f13403e44cd1604249 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010020#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010022#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020023static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
28
29/*
30 * nv_*(): functions called to handle Normal and Visual mode commands.
31 * n_*(): functions called to handle Normal mode commands.
32 * v_*(): functions called to handle Visual mode commands.
33 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void nv_ignore(cmdarg_T *cap);
35static void nv_nop(cmdarg_T *cap);
36static void nv_error(cmdarg_T *cap);
37static void nv_help(cmdarg_T *cap);
38static void nv_addsub(cmdarg_T *cap);
39static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void nv_ver_scrollbar(cmdarg_T *cap);
43static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000044#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000045#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void nv_tabline(cmdarg_T *cap);
47static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000048#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static void nv_exmode(cmdarg_T *cap);
50static void nv_colon(cmdarg_T *cap);
51static void nv_ctrlg(cmdarg_T *cap);
52static void nv_ctrlh(cmdarg_T *cap);
53static void nv_clear(cmdarg_T *cap);
54static void nv_ctrlo(cmdarg_T *cap);
55static void nv_hat(cmdarg_T *cap);
56static void nv_Zet(cmdarg_T *cap);
57static void nv_ident(cmdarg_T *cap);
58static void nv_tagpop(cmdarg_T *cap);
59static void nv_scroll(cmdarg_T *cap);
60static void nv_right(cmdarg_T *cap);
61static void nv_left(cmdarg_T *cap);
62static void nv_up(cmdarg_T *cap);
63static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void nv_end(cmdarg_T *cap);
65static void nv_dollar(cmdarg_T *cap);
66static void nv_search(cmdarg_T *cap);
67static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020068static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_csearch(cmdarg_T *cap);
70static void nv_brackets(cmdarg_T *cap);
71static void nv_percent(cmdarg_T *cap);
72static void nv_brace(cmdarg_T *cap);
73static void nv_mark(cmdarg_T *cap);
74static void nv_findpar(cmdarg_T *cap);
75static void nv_undo(cmdarg_T *cap);
76static void nv_kundo(cmdarg_T *cap);
77static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
80static void v_visop(cmdarg_T *cap);
81static void nv_subst(cmdarg_T *cap);
82static void nv_abbrev(cmdarg_T *cap);
83static void nv_optrans(cmdarg_T *cap);
84static void nv_gomark(cmdarg_T *cap);
85static void nv_pcmark(cmdarg_T *cap);
86static void nv_regname(cmdarg_T *cap);
87static void nv_visual(cmdarg_T *cap);
88static void n_start_visual_mode(int c);
89static void nv_window(cmdarg_T *cap);
90static void nv_suspend(cmdarg_T *cap);
91static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010092static void nv_dot(cmdarg_T *cap);
93static void nv_redo(cmdarg_T *cap);
94static void nv_Undo(cmdarg_T *cap);
95static void nv_tilde(cmdarg_T *cap);
96static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000097#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_lineop(cmdarg_T *cap);
101static void nv_home(cmdarg_T *cap);
102static void nv_pipe(cmdarg_T *cap);
103static void nv_bck_word(cmdarg_T *cap);
104static void nv_wordcmd(cmdarg_T *cap);
105static void nv_beginline(cmdarg_T *cap);
106static void adjust_cursor(oparg_T *oap);
107static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100108static void nv_select(cmdarg_T *cap);
109static void nv_goto(cmdarg_T *cap);
110static void nv_normal(cmdarg_T *cap);
111static void nv_esc(cmdarg_T *oap);
112static void nv_edit(cmdarg_T *cap);
113static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void nv_record(cmdarg_T *cap);
118static void nv_at(cmdarg_T *cap);
119static void nv_halfpage(cmdarg_T *cap);
120static void nv_join(cmdarg_T *cap);
121static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200122static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100130static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000132static char *e_noident = N_("E349: No identifier under cursor");
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
135 * Function to be called for a Normal or Visual mode command.
136 * The argument is a cmdarg_T.
137 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100138typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100140// Values for cmd_flags.
141#define NV_NCH 0x01 // may need to get a second char
142#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
143#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
144#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100146#define NV_SS 0x10 // may start selection
147#define NV_SSS 0x20 // may start selection with shift modifier
148#define NV_STS 0x40 // may stop selection without shift modif.
149#define NV_RL 0x80 // 'rightleft' modifies command
150#define NV_KEEPREG 0x100 // don't clear regname
151#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152
153/*
154 * Generally speaking, every Normal mode command should either clear any
155 * pending operator (with *clearop*()), or set the motion type variable
156 * oap->motion_type.
157 *
158 * When a cursor motion command is made, it is marked as being a character or
159 * line oriented motion. Then, if an operator is in effect, the operation
160 * becomes character or line oriented accordingly.
161 */
162
163/*
164 * This table contains one entry for every Normal or Visual mode command.
165 * The order doesn't matter, init_normal_cmds() will create a sorted index.
166 * It is faster when all keys from zero to '~' are present.
167 */
168static const struct nv_cmd
169{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100170 int cmd_char; // (first) command character
171 nv_func_T cmd_func; // function for this command
172 short_u cmd_flags; // NV_ flags
173 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174} nv_cmds[] =
175{
176 {NUL, nv_error, 0, 0},
177 {Ctrl_A, nv_addsub, 0, 0},
178 {Ctrl_B, nv_page, NV_STS, BACKWARD},
179 {Ctrl_C, nv_esc, 0, TRUE},
180 {Ctrl_D, nv_halfpage, 0, 0},
181 {Ctrl_E, nv_scroll_line, 0, TRUE},
182 {Ctrl_F, nv_page, NV_STS, FORWARD},
183 {Ctrl_G, nv_ctrlg, 0, 0},
184 {Ctrl_H, nv_ctrlh, 0, 0},
185 {Ctrl_I, nv_pcmark, 0, 0},
186 {NL, nv_down, 0, FALSE},
187 {Ctrl_K, nv_error, 0, 0},
188 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100189 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 {Ctrl_N, nv_down, NV_STS, FALSE},
191 {Ctrl_O, nv_ctrlo, 0, 0},
192 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000193 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {Ctrl_R, nv_redo, 0, 0},
195 {Ctrl_S, nv_ignore, 0, 0},
196 {Ctrl_T, nv_tagpop, NV_NCW, 0},
197 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_V, nv_visual, 0, FALSE},
199 {'V', nv_visual, 0, FALSE},
200 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 {Ctrl_W, nv_window, 0, 0},
202 {Ctrl_X, nv_addsub, 0, 0},
203 {Ctrl_Y, nv_scroll_line, 0, FALSE},
204 {Ctrl_Z, nv_suspend, 0, 0},
205 {ESC, nv_esc, 0, FALSE},
206 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
207 {Ctrl_RSB, nv_ident, NV_NCW, 0},
208 {Ctrl_HAT, nv_hat, NV_NCW, 0},
209 {Ctrl__, nv_error, 0, 0},
210 {' ', nv_right, 0, 0},
211 {'!', nv_operator, 0, 0},
212 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
213 {'#', nv_ident, 0, 0},
214 {'$', nv_dollar, 0, 0},
215 {'%', nv_percent, 0, 0},
216 {'&', nv_optrans, 0, 0},
217 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
218 {'(', nv_brace, 0, BACKWARD},
219 {')', nv_brace, 0, FORWARD},
220 {'*', nv_ident, 0, 0},
221 {'+', nv_down, 0, TRUE},
222 {',', nv_csearch, 0, TRUE},
223 {'-', nv_up, 0, TRUE},
224 {'.', nv_dot, NV_KEEPREG, 0},
225 {'/', nv_search, 0, FALSE},
226 {'0', nv_beginline, 0, 0},
227 {'1', nv_ignore, 0, 0},
228 {'2', nv_ignore, 0, 0},
229 {'3', nv_ignore, 0, 0},
230 {'4', nv_ignore, 0, 0},
231 {'5', nv_ignore, 0, 0},
232 {'6', nv_ignore, 0, 0},
233 {'7', nv_ignore, 0, 0},
234 {'8', nv_ignore, 0, 0},
235 {'9', nv_ignore, 0, 0},
236 {':', nv_colon, 0, 0},
237 {';', nv_csearch, 0, FALSE},
238 {'<', nv_operator, NV_RL, 0},
239 {'=', nv_operator, 0, 0},
240 {'>', nv_operator, NV_RL, 0},
241 {'?', nv_search, 0, FALSE},
242 {'@', nv_at, NV_NCH_NOP, FALSE},
243 {'A', nv_edit, 0, 0},
244 {'B', nv_bck_word, 0, 1},
245 {'C', nv_abbrev, NV_KEEPREG, 0},
246 {'D', nv_abbrev, NV_KEEPREG, 0},
247 {'E', nv_wordcmd, 0, TRUE},
248 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
249 {'G', nv_goto, 0, TRUE},
250 {'H', nv_scroll, 0, 0},
251 {'I', nv_edit, 0, 0},
252 {'J', nv_join, 0, 0},
253 {'K', nv_ident, 0, 0},
254 {'L', nv_scroll, 0, 0},
255 {'M', nv_scroll, 0, 0},
256 {'N', nv_next, 0, SEARCH_REV},
257 {'O', nv_open, 0, 0},
258 {'P', nv_put, 0, 0},
259 {'Q', nv_exmode, NV_NCW, 0},
260 {'R', nv_Replace, 0, FALSE},
261 {'S', nv_subst, NV_KEEPREG, 0},
262 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
263 {'U', nv_Undo, 0, 0},
264 {'W', nv_wordcmd, 0, TRUE},
265 {'X', nv_abbrev, NV_KEEPREG, 0},
266 {'Y', nv_abbrev, NV_KEEPREG, 0},
267 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
268 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
269 {'\\', nv_error, 0, 0},
270 {']', nv_brackets, NV_NCH_ALW, FORWARD},
271 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
272 {'_', nv_lineop, 0, 0},
273 {'`', nv_gomark, NV_NCH_ALW, FALSE},
274 {'a', nv_edit, NV_NCH, 0},
275 {'b', nv_bck_word, 0, 0},
276 {'c', nv_operator, 0, 0},
277 {'d', nv_operator, 0, 0},
278 {'e', nv_wordcmd, 0, FALSE},
279 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
280 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
281 {'h', nv_left, NV_RL, 0},
282 {'i', nv_edit, NV_NCH, 0},
283 {'j', nv_down, 0, FALSE},
284 {'k', nv_up, 0, FALSE},
285 {'l', nv_right, NV_RL, 0},
286 {'m', nv_mark, NV_NCH_NOP, 0},
287 {'n', nv_next, 0, 0},
288 {'o', nv_open, 0, 0},
289 {'p', nv_put, 0, 0},
290 {'q', nv_record, NV_NCH, 0},
291 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
292 {'s', nv_subst, NV_KEEPREG, 0},
293 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
294 {'u', nv_undo, 0, 0},
295 {'w', nv_wordcmd, 0, FALSE},
296 {'x', nv_abbrev, NV_KEEPREG, 0},
297 {'y', nv_operator, 0, 0},
298 {'z', nv_zet, NV_NCH_ALW, 0},
299 {'{', nv_findpar, 0, BACKWARD},
300 {'|', nv_pipe, 0, 0},
301 {'}', nv_findpar, 0, FORWARD},
302 {'~', nv_tilde, 0, 0},
303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100304 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200306 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
307 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
308 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
309 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 {K_LEFTMOUSE, nv_mouse, 0, 0},
311 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
312 {K_LEFTDRAG, nv_mouse, 0, 0},
313 {K_LEFTRELEASE, nv_mouse, 0, 0},
314 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100315 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
317 {K_MIDDLEDRAG, nv_mouse, 0, 0},
318 {K_MIDDLERELEASE, nv_mouse, 0, 0},
319 {K_RIGHTMOUSE, nv_mouse, 0, 0},
320 {K_RIGHTDRAG, nv_mouse, 0, 0},
321 {K_RIGHTRELEASE, nv_mouse, 0, 0},
322 {K_X1MOUSE, nv_mouse, 0, 0},
323 {K_X1DRAG, nv_mouse, 0, 0},
324 {K_X1RELEASE, nv_mouse, 0, 0},
325 {K_X2MOUSE, nv_mouse, 0, 0},
326 {K_X2DRAG, nv_mouse, 0, 0},
327 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000328 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000329 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {K_INS, nv_edit, 0, 0},
331 {K_KINS, nv_edit, 0, 0},
332 {K_BS, nv_ctrlh, 0, 0},
333 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
334 {K_S_UP, nv_page, NV_SS, BACKWARD},
335 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
336 {K_S_DOWN, nv_page, NV_SS, FORWARD},
337 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
339 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
340 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
341 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
342 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
343 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
344 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
345 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
346 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
347 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
348 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {K_S_END, nv_end, NV_SS, FALSE},
350 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
351 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
352 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {K_S_HOME, nv_home, NV_SS, 0},
354 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
355 {K_DEL, nv_abbrev, 0, 0},
356 {K_KDEL, nv_abbrev, 0, 0},
357 {K_UNDO, nv_kundo, 0, 0},
358 {K_HELP, nv_help, NV_NCW, 0},
359 {K_F1, nv_help, NV_NCW, 0},
360 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifdef FEAT_GUI
363 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
364 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
365#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#ifdef FEAT_GUI_TABLINE
367 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000368 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370#ifdef FEAT_NETBEANS_INTG
371 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
372#endif
373#ifdef FEAT_DND
374 {K_DROP, nv_drop, NV_STS, 0},
375#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000376 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100377 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378};
379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100380// Number of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static short nv_cmd_idx[NV_CMDS_SIZE];
385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386// The highest index for which
387// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static int nv_max_linear;
389
390/*
391 * Compare functions for qsort() below, that checks the command character
392 * through the index in nv_cmd_idx[].
393 */
394 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100395nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396{
397 int c1, c2;
398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100399 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 c1 = nv_cmds[*(const short *)s1].cmd_char;
401 c2 = nv_cmds[*(const short *)s2].cmd_char;
402 if (c1 < 0)
403 c1 = -c1;
404 if (c2 < 0)
405 c2 = -c2;
406 return c1 - c2;
407}
408
409/*
410 * Initialize the nv_cmd_idx[] table.
411 */
412 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100413init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
415 int i;
416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100417 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000418 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 nv_cmd_idx[i] = i;
420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100421 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100424 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000425 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
427 break;
428 nv_max_linear = i - 1;
429}
430
431/*
432 * Search for a command in the commands table.
433 * Returns -1 for invalid command.
434 */
435 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 int i;
439 int idx;
440 int top, bot;
441 int c;
442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100443 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (cmdchar >= 0x100)
445 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100447 // We use the absolute value of the character. Special keys have a
448 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (cmdchar < 0)
450 cmdchar = -cmdchar;
451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // If the character is in the first part: The character is the index into
453 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (cmdchar <= nv_max_linear)
455 return nv_cmd_idx[cmdchar];
456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100457 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 bot = nv_max_linear + 1;
459 top = NV_CMDS_SIZE - 1;
460 idx = -1;
461 while (bot <= top)
462 {
463 i = (top + bot) / 2;
464 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
465 if (c < 0)
466 c = -c;
467 if (cmdchar == c)
468 {
469 idx = nv_cmd_idx[i];
470 break;
471 }
472 if (cmdchar > c)
473 bot = i + 1;
474 else
475 top = i - 1;
476 }
477 return idx;
478}
479
480/*
481 * Execute a command in Normal mode.
482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100484normal_cmd(
485 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100486 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100490 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 int old_col = curwin->w_curswant;
492#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100493 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100495 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 static int old_mapped_len = 0;
498 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000499#ifdef FEAT_EVAL
500 int set_prevcount = FALSE;
501#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200502 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
Bram Moolenaara80faa82020-04-12 19:37:17 +0200504 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000506
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100507 // Use a count remembered from before entering an operator. After typing
508 // "3d" we return from normal_cmd() and come back here, the "3" is
509 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 ca.opcount = opcount;
511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 /*
513 * If there is an operator pending, then the command we take this time
514 * will terminate it. Finish_op tells us to finish the operation before
515 * returning this time (unless the operation was cancelled).
516 */
517#ifdef CURSOR_SHAPE
518 c = finish_op;
519#endif
520 finish_op = (oap->op_type != OP_NOP);
521#ifdef CURSOR_SHAPE
522 if (finish_op != c)
523 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100524 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525# ifdef FEAT_MOUSESHAPE
526 update_mouseshape(-1);
527# endif
528 }
529#endif
530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100531 // When not finishing an operator and no register name typed, reset the
532 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000536#ifdef FEAT_EVAL
537 set_prevcount = TRUE;
538#endif
539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100541 // Restore counts from before receiving K_CURSORHOLD. This means after
542 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
543 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000544 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
545 {
546 ca.opcount = oap->prev_opcount;
547 ca.count0 = oap->prev_count0;
548 oap->prev_opcount = 0;
549 oap->prev_count0 = 0;
550 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000551
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554 State = NORMAL_BUSY;
555#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100556 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100559#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100560 // Set v:count here, when called from main() and not a stuffed
561 // command, so that v:count can be used in an expression mapping
562 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100563 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100564 set_vcount_ca(&ca, &set_prevcount);
565#endif
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 /*
568 * Get the command character from the user.
569 */
570 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100571 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
573 /*
574 * If a mapping was started in Visual or Select mode, remember the length
575 * of the mapping. This is used below to not return to Insert mode for as
576 * long as the mapping is being executed.
577 */
578 if (restart_edit == 0)
579 old_mapped_len = 0;
580 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000581 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 old_mapped_len = typebuf_maplen();
583
584 if (c == NUL)
585 c = K_ZERO;
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 /*
588 * In Select mode, typed text replaces the selection.
589 */
590 if (VIsual_active
591 && VIsual_select
592 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
595 // 'insertmode' is set) fake a "d"elete command, Insert mode will
596 // restart automatically.
597 // Insert the typed character in the typeahead buffer, so that it can
598 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200599 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000600 if (restart_edit != 0)
601 c = 'd';
602 else
603 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 msg_nowait = TRUE; // don't delay going to insert mode
605 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608#ifdef FEAT_CMDL_INFO
609 need_flushbuf = add_to_showcmd(c);
610#endif
611
612getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
615 /*
616 * Handle a count before a command and compute ca.count0.
617 * Note that '0' is a command and not the start of a count, but it's
618 * part of a count after other digits.
619 */
620 while ( (c >= '1' && c <= '9')
621 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
622 {
623 if (c == K_DEL || c == K_KDEL)
624 {
625 ca.count0 /= 10;
626#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
629 }
630 else
631 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100632 if (ca.count0 < 0) // got too large!
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000634#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100635 // Set v:count here, when called from main() and not a stuffed
636 // command, so that v:count can be used in an expression mapping
637 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100638 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100639 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (ctrl_w)
642 {
643 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100644 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100646 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000647 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 --no_zero_mapping;
650 if (ctrl_w)
651 {
652 --no_mapping;
653 --allow_keys;
654 }
655#ifdef FEAT_CMDL_INFO
656 need_flushbuf |= add_to_showcmd(c);
657#endif
658 }
659
660 /*
661 * If we got CTRL-W there may be a/another count
662 */
663 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
664 {
665 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 ca.count0 = 0;
668 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 ++allow_keys; // no mapping for nchar, but keys
670 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 --no_mapping;
673 --allow_keys;
674#ifdef FEAT_CMDL_INFO
675 need_flushbuf |= add_to_showcmd(c);
676#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679 }
680
Bram Moolenaara983fe92008-07-31 20:04:27 +0000681 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 // Save the count values so that ca.opcount and ca.count0 are exactly
684 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000685 oap->prev_opcount = ca.opcount;
686 oap->prev_count0 = ca.count0;
687 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100688 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000689 {
690 /*
691 * If we're in the middle of an operator (including after entering a
692 * yank buffer with '"') AND we had a count before the operator, then
693 * that count overrides the current value of ca.count0.
694 * What this means effectively, is that commands like "3dw" get turned
695 * into "d3w" which makes things fall into place pretty neatly.
696 * If you give a count before AND after the operator, they are
697 * multiplied.
698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (ca.count0)
700 ca.count0 *= ca.opcount;
701 else
702 ca.count0 = ca.opcount;
703 }
704
705 /*
706 * Always remember the count. It will be set to zero (on the next call,
707 * above) when there is no pending operator.
708 * When called from main(), save the count for use by the "count" built-in
709 * variable.
710 */
711 ca.opcount = ca.count0;
712 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
713
714#ifdef FEAT_EVAL
715 /*
716 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100717 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100719 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000720 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#endif
722
723 /*
724 * Find the command character in the table of commands.
725 * For CTRL-W we already got nchar when looking for a count.
726 */
727 if (ctrl_w)
728 {
729 ca.nchar = c;
730 ca.cmdchar = Ctrl_W;
731 }
732 else
733 ca.cmdchar = c;
734 idx = find_command(ca.cmdchar);
735 if (idx < 0)
736 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100737 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 clearopbeep(oap);
739 goto normal_end;
740 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000741
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000742 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100744 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000746 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 goto normal_end;
748 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000749 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
750 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /*
753 * In Visual/Select mode, a few keys are handled in a special way.
754 */
755 if (VIsual_active)
756 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100757 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (km_stopsel
759 && (nv_cmds[idx].cmd_flags & NV_STS)
760 && !(mod_mask & MOD_MASK_SHIFT))
761 {
762 end_visual_mode();
763 redraw_curbuf_later(INVERTED);
764 }
765
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100766 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (km_startsel)
768 {
769 if (nv_cmds[idx].cmd_flags & NV_SS)
770 {
771 unshift_special(&ca);
772 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000773 if (idx < 0)
774 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100775 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000776 clearopbeep(oap);
777 goto normal_end;
778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
780 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
781 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786#ifdef FEAT_RIGHTLEFT
787 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
788 && (nv_cmds[idx].cmd_flags & NV_RL))
789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100790 // Invert horizontal movements and operations. Only when typed by the
791 // user directly, not when the result of a mapping or "x" translated
792 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 switch (ca.cmdchar)
794 {
795 case 'l': ca.cmdchar = 'h'; break;
796 case K_RIGHT: ca.cmdchar = K_LEFT; break;
797 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
798 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
799 case 'h': ca.cmdchar = 'l'; break;
800 case K_LEFT: ca.cmdchar = K_RIGHT; break;
801 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
802 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
803 case '>': ca.cmdchar = '<'; break;
804 case '<': ca.cmdchar = '>'; break;
805 }
806 idx = find_command(ca.cmdchar);
807 }
808#endif
809
810 /*
811 * Get an additional character if we need one.
812 */
813 if ((nv_cmds[idx].cmd_flags & NV_NCH)
814 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
815 && oap->op_type == OP_NOP)
816 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
817 || (ca.cmdchar == 'q'
818 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200819 && reg_recording == 0
820 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100822 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
824 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100825 int repl = FALSE; // get character for replace mode
826 int lit = FALSE; // get extra character literally
827 int langmap_active = FALSE; // using :lmap mappings
828 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100829#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100830 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#endif
832
833 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100834 ++allow_keys; // no mapping for nchar, but allow key codes
835 // Don't generate a CursorHold event here, most commands can't handle
836 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000837 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (ca.cmdchar == 'g')
839 {
840 /*
841 * For 'g' get the next character now, so that we can check for
842 * "gr", "g'" and "g`".
843 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000844 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#ifdef FEAT_CMDL_INFO
847 need_flushbuf |= add_to_showcmd(ca.nchar);
848#endif
849 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100850 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100852 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 else
862 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100863 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 repl = TRUE;
865 cp = &ca.nchar;
866 }
867 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
868
869 /*
870 * Get a second or third character.
871 */
872 if (cp != NULL)
873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (repl)
875 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100876 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100877#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
882 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100883 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 --no_mapping;
885 --allow_keys;
886 if (repl)
887 State = LREPLACE;
888 else
889 State = LANGMAP;
890 langmap_active = TRUE;
891 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100892#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100894 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
896 im_set_active(TRUE);
897#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200898 if ((State & INSERT) && !p_ek)
899 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200900#ifdef FEAT_JOB_CHANNEL
901 ch_log_output = TRUE;
902#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200903 // Disable bracketed paste and modifyOtherKeys here, we won't
904 // recognize the escape sequences with 'esckeys' off.
905 out_str(T_BD);
906 out_str(T_CTE);
907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000909 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
Bram Moolenaarca774f62020-08-30 20:46:38 +0200911 if ((State & INSERT) && !p_ek)
912 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200913#ifdef FEAT_JOB_CHANNEL
914 ch_log_output = TRUE;
915#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200916 // Re-enable bracketed paste mode and modifyOtherKeys
917 out_str(T_BE);
918 out_str(T_CTI);
919 }
920
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (langmap_active)
922 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100923 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 ++no_mapping;
925 ++allow_keys;
926 State = NORMAL_BUSY;
927 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100928#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (lang)
930 {
931 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
932 im_save_status(&curbuf->b_p_iminsert);
933 im_set_active(FALSE);
934 }
935 p_smd = save_smd;
936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#ifdef FEAT_CMDL_INFO
939 need_flushbuf |= add_to_showcmd(*cp);
940#endif
941
942 if (!lit)
943 {
944#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100945 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (*cp == Ctrl_K
947 && ((nv_cmds[idx].cmd_flags & NV_LANG)
948 || cp == &ca.extra_char)
949 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
950 {
951 c = get_digraph(FALSE);
952 if (c > 0)
953 {
954 *cp = c;
955# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100956 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 del_from_showcmd(3);
958 need_flushbuf |= add_to_showcmd(*cp);
959# endif
960 }
961 }
962#endif
963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100964 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100967 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 if (p_hkmap && lang && KeyTyped)
969 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970#endif
971 }
972
973 /*
974 * When the next character is CTRL-\ a following CTRL-N means the
975 * command is aborted and we go to Normal mode.
976 */
977 if (cp == &ca.extra_char
978 && ca.nchar == Ctrl_BSL
979 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
980 {
981 ca.cmdchar = Ctrl_BSL;
982 ca.nchar = ca.extra_char;
983 idx = find_command(ca.cmdchar);
984 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200985 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200986 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 else if (*cp == Ctrl_BSL)
988 {
989 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100991 // There is a busy wait here when typing "f<C-\>" and then
992 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 while ((c = vpeekc()) <= 0 && towait > 0L)
994 {
995 do_sleep(towait > 50L ? 50L : towait);
996 towait -= 50L;
997 }
998 if (c > 0)
999 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001000 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 if (c != Ctrl_N && c != Ctrl_G)
1002 vungetc(c);
1003 else
1004 {
1005 ca.cmdchar = Ctrl_BSL;
1006 ca.nchar = c;
1007 idx = find_command(ca.cmdchar);
1008 }
1009 }
1010 }
1011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001012 // When getting a text character and the next character is a
1013 // multi-byte character, it could be a composing character.
1014 // However, don't wait for it to arrive. Also, do enable mapping,
1015 // because if it's put back with vungetc() it's too late to apply
1016 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001017 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 while (enc_utf8 && lang && (c = vpeekc()) > 0
1019 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1020 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001021 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (!utf_iscomposing(c))
1023 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001024 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 break;
1026 }
1027 else if (ca.ncharC1 == 0)
1028 ca.ncharC1 = c;
1029 else
1030 ca.ncharC2 = c;
1031 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001032 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 }
1034 --no_mapping;
1035 --allow_keys;
1036 }
1037
1038#ifdef FEAT_CMDL_INFO
1039 /*
1040 * Flush the showcmd characters onto the screen so we can see them while
1041 * the command is being executed. Only do this when the shown command was
1042 * actually displayed, otherwise this will slow down a lot when executing
1043 * mappings.
1044 */
1045 if (need_flushbuf)
1046 out_flush();
1047#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001048 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001049 {
1050 if (ex_normal_busy)
1051 did_cursorhold = save_did_cursorhold;
1052 else
1053 did_cursorhold = FALSE;
1054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056 State = NORMAL;
1057
1058 if (ca.nchar == ESC)
1059 {
1060 clearop(oap);
1061 if (restart_edit == 0 && goto_im())
1062 restart_edit = 'a';
1063 goto normal_end;
1064 }
1065
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001066 if (ca.cmdchar != K_IGNORE)
1067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001068 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001069 msg_col = 0;
1070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001072 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001074 // When 'keymodel' contains "startsel" some keys start Select/Visual
1075 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 if (!VIsual_active && km_startsel)
1077 {
1078 if (nv_cmds[idx].cmd_flags & NV_SS)
1079 {
1080 start_selection();
1081 unshift_special(&ca);
1082 idx = find_command(ca.cmdchar);
1083 }
1084 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1085 && (mod_mask & MOD_MASK_SHIFT))
1086 {
1087 start_selection();
1088 mod_mask &= ~MOD_MASK_SHIFT;
1089 }
1090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
1092 /*
1093 * Execute the command!
1094 * Call the command function found in the commands table.
1095 */
1096 ca.arg = nv_cmds[idx].cmd_arg;
1097 (nv_cmds[idx].cmd_func)(&ca);
1098
1099 /*
1100 * If we didn't start or finish an operator, reset oap->regname, unless we
1101 * need it later.
1102 */
1103 if (!finish_op
1104 && !oap->op_type
1105 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1106 {
1107 clearop(oap);
1108#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001109 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110#endif
1111 }
1112
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001113 // Get the length of mapped chars again after typing a count, second
1114 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001115 if (old_mapped_len > 0)
1116 old_mapped_len = typebuf_maplen();
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 /*
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001119 * If an operation is pending, handle it. But not for K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 */
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001121 if (ca.cmdchar != K_IGNORE)
1122 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
1124 /*
1125 * Wait for a moment when a message is displayed that will be overwritten
1126 * by the mode message.
1127 * In Visual mode and with "^O" in Insert mode, a short message will be
1128 * overwritten by the mode message. Wait a bit, until a key is hit.
1129 * In Visual mode, it's more important to keep the Visual area updated
1130 * than keeping a message (e.g. from a /pat search).
1131 * Only do this if the command was typed, not from a mapping.
1132 * Don't wait when emsg_silent is non-zero.
1133 * Also wait a bit after an error message, e.g. for "^O:".
1134 * Don't redraw the screen, it would remove the message.
1135 */
1136 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001137 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 || (VIsual_active
1140 && old_pos.lnum == curwin->w_cursor.lnum
1141 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 )
1143 && (clear_cmdline
1144 || redraw_cmdline)
1145 && (msg_didout || (msg_didany && msg_scroll))
1146 && !msg_nowait
1147 && KeyTyped)
1148 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 && (msg_scroll
1151 || emsg_on_display)))
1152 && oap->regname == 0
1153 && !(ca.retval & CA_COMMAND_BUSY)
1154 && stuff_empty()
1155 && typebuf_typed()
1156 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001157 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 && !did_wait_return
1159 && oap->op_type == OP_NOP)
1160 {
1161 int save_State = State;
1162
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001163 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (restart_edit != 0)
1165 State = INSERT;
1166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001167 // If need to redraw, and there is a "keep_msg", redraw before the
1168 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1170 {
1171 char_u *kmsg;
1172
1173 kmsg = keep_msg;
1174 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001175 // Showmode() will clear keep_msg, but we want to use it anyway.
1176 // First update w_topline.
1177 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001179 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001181
1182 kmsg = vim_strsave(keep_msg);
1183 if (kmsg != NULL)
1184 {
1185 msg_attr((char *)kmsg, keep_msg_attr);
1186 vim_free(kmsg);
1187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001190#ifdef CURSOR_SHAPE
1191 ui_cursor_shape(); // may show different cursor shape
1192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 cursor_on();
1194 out_flush();
1195 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001196 ui_delay(1003L, TRUE); // wait at least one second
1197 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 State = save_State;
1199
1200 msg_scroll = FALSE;
1201 emsg_on_display = FALSE;
1202 }
1203
1204 /*
1205 * Finish up after executing a Normal mode command.
1206 */
1207normal_end:
1208
1209 msg_nowait = FALSE;
1210
Bram Moolenaarcc613032020-06-07 21:31:18 +02001211#ifdef FEAT_EVAL
1212 if (finish_op)
1213 reset_reg_var();
1214#endif
1215
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001216 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#ifdef CURSOR_SHAPE
1218 c = finish_op;
1219#endif
1220 finish_op = FALSE;
1221#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001222 // Redraw the cursor with another shape, if we were in Operator-pending
1223 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (c || ca.cmdchar == 'r')
1225 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001226 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227# ifdef FEAT_MOUSESHAPE
1228 update_mouseshape(-1);
1229# endif
1230 }
1231#endif
1232
1233#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001234 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001235 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 clear_showcmd();
1237#endif
1238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001239 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 vim_free(ca.searchbuf);
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (has_mbyte)
1243 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (curwin->w_p_scb && toplevel)
1246 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001247 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 do_check_scrollbind(TRUE);
1249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
Bram Moolenaar860cae12010-06-05 23:22:07 +02001251 if (curwin->w_p_crb && toplevel)
1252 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001253 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001254 do_check_cursorbind();
1255 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001256
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001257#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001258 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001259 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001260 restart_edit = 0;
1261#endif
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1265 * if still inside a mapping that started in Visual mode).
1266 * May switch from Visual to Select mode after CTRL-O command.
1267 */
1268 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1270 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 && !(ca.retval & CA_COMMAND_BUSY)
1272 && stuff_empty()
1273 && oap->regname == 0)
1274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (restart_VIsual_select == 1)
1276 {
1277 VIsual_select = TRUE;
1278 showmode();
1279 restart_VIsual_select = 0;
1280 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001281 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 (void)edit(restart_edit, FALSE, 1L);
1283 }
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if (restart_VIsual_select == 2)
1286 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001288 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 opcount = ca.opcount;
1290}
1291
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001292#ifdef FEAT_EVAL
1293/*
1294 * Set v:count and v:count1 according to "cap".
1295 * Set v:prevcount only when "set_prevcount" is TRUE.
1296 */
1297 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001298set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001299{
1300 long count = cap->count0;
1301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001302 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001303 if (cap->opcount != 0)
1304 count = cap->opcount * (count == 0 ? 1 : count);
1305 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001307}
1308#endif
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001311 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 * if not.
1313 */
1314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001315check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 static int did_check = FALSE;
1318
1319 if (full_screen)
1320 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001321 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001322 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 did_check = TRUE;
1324 }
1325}
1326
1327/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001328 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * This function should ALWAYS be called to end Visual mode, except from
1330 * do_pending_operator().
1331 */
1332 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001333end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335#ifdef FEAT_CLIPBOARD
1336 /*
1337 * If we are using the clipboard, then remember what was selected in case
1338 * we need to paste it somewhere while we still own the selection.
1339 * Only do this when the clipboard is already owned. Don't want to grab
1340 * the selection when hitting ESC.
1341 */
1342 if (clip_star.available && clip_star.owned)
1343 clip_auto_select();
1344#endif
1345
1346 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 setmouse();
1348 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001351 curbuf->b_visual.vi_mode = VIsual_mode;
1352 curbuf->b_visual.vi_start = VIsual;
1353 curbuf->b_visual.vi_end = curwin->w_cursor;
1354 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355#ifdef FEAT_EVAL
1356 curbuf->b_visual_mode_eval = VIsual_mode;
1357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (!virtual_active())
1359 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001360 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001362 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363}
1364
1365/*
1366 * Reset VIsual_active and VIsual_reselect.
1367 */
1368 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001369reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 if (VIsual_active)
1372 {
1373 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001374 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376 VIsual_reselect = FALSE;
1377}
1378
1379/*
1380 * Reset VIsual_active and VIsual_reselect if it's set.
1381 */
1382 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001383reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384{
1385 if (VIsual_active)
1386 {
1387 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001388 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 VIsual_reselect = FALSE;
1390 }
1391}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001393 void
1394restore_visual_mode(void)
1395{
1396 if (VIsual_mode_orig != NUL)
1397 {
1398 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1399 VIsual_mode_orig = NUL;
1400 }
1401}
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403/*
1404 * Check for a balloon-eval special item to include when searching for an
1405 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1406 * Returns TRUE if the character at "*ptr" should be included.
1407 * "dir" is FORWARD or BACKWARD, the direction of searching.
1408 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1409 * "bnp" points to a counter for square brackets.
1410 */
1411 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001412find_is_eval_item(
1413 char_u *ptr,
1414 int *colp,
1415 int *bnp,
1416 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001418 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1420 ++*bnp;
1421 if (*bnp > 0)
1422 {
1423 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1424 --*bnp;
1425 return TRUE;
1426 }
1427
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001428 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 if (*ptr == '.')
1430 return TRUE;
1431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001432 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1434 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1435 {
1436 *colp += dir;
1437 return TRUE;
1438 }
1439 return FALSE;
1440}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442/*
1443 * Find the identifier under or to the right of the cursor.
1444 * "find_type" can have one of three values:
1445 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001446 * FIND_STRING: find any non-white text
1447 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001448 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 *
1450 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001451 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001453 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 * This doesn't match the real Vi but I like it a little better and it
1455 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001456 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 * When FIND_IDENT isn't defined, we backup until a blank.
1458 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001459 * Returns the length of the text, or zero if no text is found.
1460 * If text is found, a pointer to the text is put in "*text". This
1461 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 */
1463 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001464find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001467 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468}
1469
1470/*
1471 * Like find_ident_under_cursor(), but for any window and any position.
1472 * However: Uses 'iskeyword' from the current window!.
1473 */
1474 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001475find_ident_at_pos(
1476 win_T *wp,
1477 linenr_T lnum,
1478 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001479 char_u **text,
1480 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001481 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
1483 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001484 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 int this_class = 0;
1487 int prev_class;
1488 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001489 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491 /*
1492 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001493 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 */
1495 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1496 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1497 {
1498 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001499 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 */
1501 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 if (has_mbyte)
1503 {
1504 while (ptr[col] != NUL)
1505 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001506 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 this_class = mb_get_class(ptr + col);
1510 if (this_class != 0 && (i == 1 || this_class != 1))
1511 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001512 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514 }
1515 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001517 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 )
1520 ++col;
1521
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001522 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
1525 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001526 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (has_mbyte)
1529 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001530 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1532 this_class = mb_get_class((char_u *)"a");
1533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001535 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1538 prev_class = mb_get_class(ptr + prevcol);
1539 if (this_class != prev_class
1540 && (i == 0
1541 || prev_class == 0
1542 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 && (!(find_type & FIND_EVAL)
1544 || prevcol == 0
1545 || !find_is_eval_item(ptr + prevcol, &prevcol,
1546 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 )
1548 break;
1549 col = prevcol;
1550 }
1551
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001552 // If we don't want just any old text, or we've found an
1553 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (this_class > 2)
1555 this_class = 2;
1556 if (!(find_type & FIND_STRING) || this_class == 2)
1557 break;
1558 }
1559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
1561 while (col > 0
1562 && ((i == 0
1563 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001564 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 && (!(find_type & FIND_IDENT)
1566 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 || ((find_type & FIND_EVAL)
1568 && col > 1
1569 && find_is_eval_item(ptr + col - 1, &col,
1570 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 ))
1572 --col;
1573
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001574 // If we don't want just any old text, or we've found an
1575 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1577 break;
1578 }
1579 }
1580
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001581 if (ptr[col] == NUL || (i == 0
1582 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001584 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001585 if ((find_type & FIND_NOERROR) == 0)
1586 {
1587 if (find_type & FIND_STRING)
1588 emsg(_("E348: No string under cursor"));
1589 else
1590 emsg(_(e_noident));
1591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 return 0;
1593 }
1594 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001595 *text = ptr;
1596 if (textcol != NULL)
1597 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001600 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 bn = 0;
1603 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (has_mbyte)
1606 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001607 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 this_class = mb_get_class(ptr);
1609 while (ptr[col] != NUL
1610 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1611 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 || ((find_type & FIND_EVAL)
1613 && col <= (int)startcol
1614 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001616 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 }
1618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001620 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 || ((find_type & FIND_EVAL)
1622 && col <= (int)startcol
1623 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627 return col;
1628}
1629
1630/*
1631 * Prepare for redo of a normal command.
1632 */
1633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001634prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635{
1636 prep_redo(cap->oap->regname, cap->count0,
1637 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1638}
1639
1640/*
1641 * Prepare for redo of any command.
1642 * Note that only the last argument can be a multi-byte char.
1643 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001644 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001645prep_redo(
1646 int regname,
1647 long num,
1648 int cmd1,
1649 int cmd2,
1650 int cmd3,
1651 int cmd4,
1652 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001655 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
1657 AppendCharToRedobuff('"');
1658 AppendCharToRedobuff(regname);
1659 }
1660 if (num)
1661 AppendNumberToRedobuff(num);
1662
1663 if (cmd1 != NUL)
1664 AppendCharToRedobuff(cmd1);
1665 if (cmd2 != NUL)
1666 AppendCharToRedobuff(cmd2);
1667 if (cmd3 != NUL)
1668 AppendCharToRedobuff(cmd3);
1669 if (cmd4 != NUL)
1670 AppendCharToRedobuff(cmd4);
1671 if (cmd5 != NUL)
1672 AppendCharToRedobuff(cmd5);
1673}
1674
1675/*
1676 * check for operator active and clear it
1677 *
1678 * return TRUE if operator was active
1679 */
1680 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001681checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 if (oap->op_type == OP_NOP)
1684 return FALSE;
1685 clearopbeep(oap);
1686 return TRUE;
1687}
1688
1689/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001690 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001692 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
1694 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001695checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001697 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 return FALSE;
1699 clearopbeep(oap);
1700 return TRUE;
1701}
1702
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001703 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001704clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 oap->op_type = OP_NOP;
1707 oap->regname = 0;
1708 oap->motion_force = NUL;
1709 oap->use_reg_one = FALSE;
1710}
1711
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001712 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001713clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 clearop(oap);
1716 beep_flush();
1717}
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719/*
1720 * Remove the shift modifier from a special key.
1721 */
1722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001723unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 switch (cap->cmdchar)
1726 {
1727 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1728 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1729 case K_S_UP: cap->cmdchar = K_UP; break;
1730 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1731 case K_S_HOME: cap->cmdchar = K_HOME; break;
1732 case K_S_END: cap->cmdchar = K_END; break;
1733 }
1734 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001737/*
1738 * If the mode is currently displayed clear the command line or update the
1739 * command displayed.
1740 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001742may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001743{
1744 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001745 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001746#ifdef FEAT_CMDL_INFO
1747 else
1748 clear_showcmd();
1749#endif
1750}
1751
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1753/*
1754 * Routines for displaying a partly typed command
1755 */
1756
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001757#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001759static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760static int showcmd_is_clear = TRUE;
1761static int showcmd_visual = FALSE;
1762
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001763static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
1765 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001766clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 if (!p_sc)
1769 return;
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (VIsual_active && !char_avail())
1772 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001773 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 long lines;
1775 colnr_T leftcol, rightcol;
1776 linenr_T top, bot;
1777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001778 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001779 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 {
1781 top = VIsual.lnum;
1782 bot = curwin->w_cursor.lnum;
1783 }
1784 else
1785 {
1786 top = curwin->w_cursor.lnum;
1787 bot = VIsual.lnum;
1788 }
1789# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001790 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001791 (void)hasFolding(top, &top, NULL);
1792 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793# endif
1794 lines = bot - top + 1;
1795
1796 if (VIsual_mode == Ctrl_V)
1797 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001798# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001799 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001800 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001801
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001802 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001803 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001804 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001807# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001808 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001809 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1812 (long)(rightcol - leftcol + 1));
1813 }
1814 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1815 sprintf((char *)showcmd_buf, "%ld", lines);
1816 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001817 {
1818 char_u *s, *e;
1819 int l;
1820 int bytes = 0;
1821 int chars = 0;
1822
1823 if (cursor_bot)
1824 {
1825 s = ml_get_pos(&VIsual);
1826 e = ml_get_cursor();
1827 }
1828 else
1829 {
1830 s = ml_get_cursor();
1831 e = ml_get_pos(&VIsual);
1832 }
1833 while ((*p_sel != 'e') ? s <= e : s < e)
1834 {
1835 l = (*mb_ptr2len)(s);
1836 if (l == 0)
1837 {
1838 ++bytes;
1839 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001840 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001841 }
1842 bytes += l;
1843 ++chars;
1844 s += l;
1845 }
1846 if (bytes == chars)
1847 sprintf((char *)showcmd_buf, "%d", chars);
1848 else
1849 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1850 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001851 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 showcmd_visual = TRUE;
1853 }
1854 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 {
1856 showcmd_buf[0] = NUL;
1857 showcmd_visual = FALSE;
1858
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001859 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (showcmd_is_clear)
1861 return;
1862 }
1863
1864 display_showcmd();
1865}
1866
1867/*
1868 * Add 'c' to string of shown command chars.
1869 * Return TRUE if output has been written (and setcursor() has been called).
1870 */
1871 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001872add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873{
1874 char_u *p;
1875 int old_len;
1876 int extra_len;
1877 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 int i;
1879 static int ignore[] =
1880 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001881#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1883 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001884#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001885 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001886 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1888 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001889 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001891 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 0
1893 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
Bram Moolenaar09df3122006-01-23 22:23:09 +00001895 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 return FALSE;
1897
1898 if (showcmd_visual)
1899 {
1900 showcmd_buf[0] = NUL;
1901 showcmd_visual = FALSE;
1902 }
1903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001904 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (IS_SPECIAL(c))
1906 for (i = 0; ignore[i] != 0; ++i)
1907 if (ignore[i] == c)
1908 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001911 if (*p == ' ')
1912 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 old_len = (int)STRLEN(showcmd_buf);
1914 extra_len = (int)STRLEN(p);
1915 overflow = old_len + extra_len - SHOWCMD_COLS;
1916 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001917 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1918 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 STRCAT(showcmd_buf, p);
1920
1921 if (char_avail())
1922 return FALSE;
1923
1924 display_showcmd();
1925
1926 return TRUE;
1927}
1928
1929 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001930add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
1932 if (!add_to_showcmd(c))
1933 setcursor();
1934}
1935
1936/*
1937 * Delete 'len' characters from the end of the shown command.
1938 */
1939 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001940del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
1942 int old_len;
1943
1944 if (!p_sc)
1945 return;
1946
1947 old_len = (int)STRLEN(showcmd_buf);
1948 if (len > old_len)
1949 len = old_len;
1950 showcmd_buf[old_len - len] = NUL;
1951
1952 if (!char_avail())
1953 display_showcmd();
1954}
1955
1956/*
1957 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
1958 * something and there is a partial mapping.
1959 */
1960 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001961push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962{
1963 if (p_sc)
1964 STRCPY(old_showcmd_buf, showcmd_buf);
1965}
1966
1967 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001968pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969{
1970 if (!p_sc)
1971 return;
1972
1973 STRCPY(showcmd_buf, old_showcmd_buf);
1974
1975 display_showcmd();
1976}
1977
1978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001979display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980{
1981 int len;
1982
1983 cursor_off();
1984
1985 len = (int)STRLEN(showcmd_buf);
1986 if (len == 0)
1987 showcmd_is_clear = TRUE;
1988 else
1989 {
1990 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
1991 showcmd_is_clear = FALSE;
1992 }
1993
1994 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00001995 * clear the rest of an old message by outputting up to SHOWCMD_COLS
1996 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 */
1998 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
1999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002000 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001}
2002#endif
2003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004/*
2005 * When "check" is FALSE, prepare for commands that scroll the window.
2006 * When "check" is TRUE, take care of scroll-binding after the window has
2007 * scrolled. Called from normal_cmd() and edit().
2008 */
2009 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002010do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011{
2012 static win_T *old_curwin = NULL;
2013 static linenr_T old_topline = 0;
2014#ifdef FEAT_DIFF
2015 static int old_topfill = 0;
2016#endif
2017 static buf_T *old_buf = NULL;
2018 static colnr_T old_leftcol = 0;
2019
2020 if (check && curwin->w_p_scb)
2021 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002022 // If a ":syncbind" command was just used, don't scroll, only reset
2023 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (did_syncbind)
2025 did_syncbind = FALSE;
2026 else if (curwin == old_curwin)
2027 {
2028 /*
2029 * Synchronize other windows, as necessary according to
2030 * 'scrollbind'. Don't do this after an ":edit" command, except
2031 * when 'diff' is set.
2032 */
2033 if ((curwin->w_buffer == old_buf
2034#ifdef FEAT_DIFF
2035 || curwin->w_p_diff
2036#endif
2037 )
2038 && (curwin->w_topline != old_topline
2039#ifdef FEAT_DIFF
2040 || curwin->w_topfill != old_topfill
2041#endif
2042 || curwin->w_leftcol != old_leftcol))
2043 {
2044 check_scrollbind(curwin->w_topline - old_topline,
2045 (long)(curwin->w_leftcol - old_leftcol));
2046 }
2047 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002048 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
2050 /*
2051 * When switching between windows, make sure that the relative
2052 * vertical offset is valid for the new window. The relative
2053 * offset is invalid whenever another 'scrollbind' window has
2054 * scrolled to a point that would force the current window to
2055 * scroll past the beginning or end of its buffer. When the
2056 * resync is performed, some of the other 'scrollbind' windows may
2057 * need to jump so that the current window's relative position is
2058 * visible on-screen.
2059 */
2060 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2061 }
2062 curwin->w_scbind_pos = curwin->w_topline;
2063 }
2064
2065 old_curwin = curwin;
2066 old_topline = curwin->w_topline;
2067#ifdef FEAT_DIFF
2068 old_topfill = curwin->w_topfill;
2069#endif
2070 old_buf = curwin->w_buffer;
2071 old_leftcol = curwin->w_leftcol;
2072}
2073
2074/*
2075 * Synchronize any windows that have "scrollbind" set, based on the
2076 * number of rows by which the current window has changed
2077 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2078 */
2079 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002080check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081{
2082 int want_ver;
2083 int want_hor;
2084 win_T *old_curwin = curwin;
2085 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 int old_VIsual_select = VIsual_select;
2087 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 colnr_T tgt_leftcol = curwin->w_leftcol;
2089 long topline;
2090 long y;
2091
2092 /*
2093 * check 'scrollopt' string for vertical and horizontal scroll options
2094 */
2095 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2096#ifdef FEAT_DIFF
2097 want_ver |= old_curwin->w_p_diff;
2098#endif
2099 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2100
2101 /*
2102 * loop through the scrollbound windows and scroll accordingly
2103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002105 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
2107 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002108 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (curwin != old_curwin && curwin->w_p_scb)
2110 {
2111 /*
2112 * do the vertical scroll
2113 */
2114 if (want_ver)
2115 {
2116#ifdef FEAT_DIFF
2117 if (old_curwin->w_p_diff && curwin->w_p_diff)
2118 {
2119 diff_set_topline(old_curwin, curwin);
2120 }
2121 else
2122#endif
2123 {
2124 curwin->w_scbind_pos += topline_diff;
2125 topline = curwin->w_scbind_pos;
2126 if (topline > curbuf->b_ml.ml_line_count)
2127 topline = curbuf->b_ml.ml_line_count;
2128 if (topline < 1)
2129 topline = 1;
2130
2131 y = topline - curwin->w_topline;
2132 if (y > 0)
2133 scrollup(y, FALSE);
2134 else
2135 scrolldown(-y, FALSE);
2136 }
2137
2138 redraw_later(VALID);
2139 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142
2143 /*
2144 * do the horizontal scroll
2145 */
2146 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2147 {
2148 curwin->w_leftcol = tgt_leftcol;
2149 leftcol_changed();
2150 }
2151 }
2152 }
2153
2154 /*
2155 * reset current-window
2156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 VIsual_select = old_VIsual_select;
2158 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 curwin = old_curwin;
2160 curbuf = old_curbuf;
2161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163/*
2164 * Command character that's ignored.
2165 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002166 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002169nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002171 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172}
2173
2174/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002175 * Command character that doesn't do anything, but unlike nv_ignore() does
2176 * start edit(). Used for "startinsert" executed while starting up.
2177 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002179nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002180{
2181}
2182
2183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 * Command character doesn't exist.
2185 */
2186 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002187nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188{
2189 clearopbeep(cap->oap);
2190}
2191
2192/*
2193 * <Help> and <F1> commands.
2194 */
2195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002196nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 if (!checkclearopq(cap->oap))
2199 ex_help(NULL);
2200}
2201
2202/*
2203 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2204 */
2205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002206nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002208#ifdef FEAT_JOB_CHANNEL
2209 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2210 clearopbeep(cap->oap);
2211 else
2212#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002213 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002214 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002215 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002216 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2217 op_addsub(cap->oap, cap->count1, cap->arg);
2218 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002219 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002220 else if (VIsual_active)
2221 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002222 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002223 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224}
2225
2226/*
2227 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2228 */
2229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002230nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002234 if (mod_mask & MOD_MASK_CTRL)
2235 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002236 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002237 if (cap->arg == BACKWARD)
2238 goto_tabpage(-(int)cap->count1);
2239 else
2240 goto_tabpage((int)cap->count0);
2241 }
2242 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002243 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245}
2246
2247/*
2248 * Implementation of "gd" and "gD" command.
2249 */
2250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002251nv_gd(
2252 oparg_T *oap,
2253 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002254 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 char_u *ptr;
2258
Bram Moolenaard9d30582005-05-18 22:10:28 +00002259 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002260 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2261 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002263#ifdef FEAT_FOLDING
2264 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2265 foldOpenCursor();
2266#endif
2267}
2268
2269/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002270 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2271 * otherwise.
2272 */
2273 static int
2274is_ident(char_u *line, int offset)
2275{
2276 int i;
2277 int incomment = FALSE;
2278 int instring = 0;
2279 int prev = 0;
2280
2281 for (i = 0; i < offset && line[i] != NUL; i++)
2282 {
2283 if (instring != 0)
2284 {
2285 if (prev != '\\' && line[i] == instring)
2286 instring = 0;
2287 }
2288 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2289 {
2290 instring = line[i];
2291 }
2292 else
2293 {
2294 if (incomment)
2295 {
2296 if (prev == '*' && line[i] == '/')
2297 incomment = FALSE;
2298 }
2299 else if (prev == '/' && line[i] == '*')
2300 {
2301 incomment = TRUE;
2302 }
2303 else if (prev == '/' && line[i] == '/')
2304 {
2305 return FALSE;
2306 }
2307 }
2308
2309 prev = line[i];
2310 }
2311
2312 return incomment == FALSE && instring == 0;
2313}
2314
2315/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002316 * Search for variable declaration of "ptr[len]".
2317 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2318 * current file ("gD").
2319 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002320 * Return FAIL when not found.
2321 */
2322 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002323find_decl(
2324 char_u *ptr,
2325 int len,
2326 int locally,
2327 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002328 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002329{
2330 char_u *pat;
2331 pos_T old_pos;
2332 pos_T par_pos;
2333 pos_T found_pos;
2334 int t;
2335 int save_p_ws;
2336 int save_p_scs;
2337 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002338 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002339 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002340 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002341
2342 if ((pat = alloc(len + 7)) == NULL)
2343 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002344
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002345 // Put "\V" before the pattern to avoid that the special meaning of "."
2346 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002347 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2348 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 old_pos = curwin->w_cursor;
2350 save_p_ws = p_ws;
2351 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002352 p_ws = FALSE; // don't wrap around end of file now
2353 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 /*
2356 * With "gD" go to line 1.
2357 * With "gd" Search back for the start of the current function, then go
2358 * back until a blank line. If this fails go to line 1.
2359 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002360 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002362 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002364 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366 else
2367 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002368 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2370 --curwin->w_cursor.lnum;
2371 }
2372 curwin->w_cursor.col = 0;
2373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002374 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002375 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002376 for (;;)
2377 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002378 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002379 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002380 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002381 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002382
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002383 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002384 {
2385 pos_T *pos;
2386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002387 // Check that the block the match is in doesn't end before the
2388 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002389 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2390 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2391 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002392 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002393 // There can't be a useful match before the end of this block.
2394 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002395 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002396 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002397 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002398 }
2399
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002400 if (t == FAIL)
2401 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002402 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002403 if (found_pos.lnum != 0)
2404 {
2405 curwin->w_cursor = found_pos;
2406 t = OK;
2407 }
2408 break;
2409 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002410 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002411 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002412 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002413 ++curwin->w_cursor.lnum;
2414 curwin->w_cursor.col = 0;
2415 continue;
2416 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002417 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002419 // If the current position is not a valid identifier and a previous
2420 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002421 if (!valid && found_pos.lnum != 0)
2422 {
2423 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002424 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002425 }
2426
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002427 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002428 if (valid && !locally)
2429 break;
2430 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002431 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002432 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002433 if (found_pos.lnum != 0)
2434 curwin->w_cursor = found_pos;
2435 break;
2436 }
2437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002438 // For finding a local variable and the match is before the "{" or
2439 // inside a comment, continue searching. For K&R style function
2440 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002441 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002442 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002443 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002444 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 // Remove SEARCH_START from flags to avoid getting stuck at one
2446 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002447 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002449
2450 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002452 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 curwin->w_cursor = old_pos;
2454 }
2455 else
2456 {
2457 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002458 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 reset_search_dir();
2460 }
2461
2462 vim_free(pat);
2463 p_ws = save_p_ws;
2464 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002465
2466 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467}
2468
2469/*
2470 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2471 * lines rather than lines in the file.
2472 * 'dist' must be positive.
2473 *
2474 * Return OK if able to move cursor, FAIL otherwise.
2475 */
2476 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002477nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
2479 int linelen = linetabsize(ml_get_curline());
2480 int retval = OK;
2481 int atend = FALSE;
2482 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002483 int col_off1; // margin offset for first screen line
2484 int col_off2; // margin offset for wrapped screen line
2485 int width1; // text width for first screen line
2486 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
2488 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002489 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
2491 col_off1 = curwin_col_off();
2492 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002493 width1 = curwin->w_width - col_off1;
2494 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002495 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002496 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 /*
2501 * Instead of sticking at the last character of the buffer line we
2502 * try to stick in the last column of the screen.
2503 */
2504 if (curwin->w_curswant == MAXCOL)
2505 {
2506 atend = TRUE;
2507 validate_virtcol();
2508 if (width1 <= 0)
2509 curwin->w_curswant = 0;
2510 else
2511 {
2512 curwin->w_curswant = width1 - 1;
2513 if (curwin->w_virtcol > curwin->w_curswant)
2514 curwin->w_curswant += ((curwin->w_virtcol
2515 - curwin->w_curswant - 1) / width2 + 1) * width2;
2516 }
2517 }
2518 else
2519 {
2520 if (linelen > width1)
2521 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2522 else
2523 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002524 if (curwin->w_curswant >= (colnr_T)n)
2525 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
2527
2528 while (dist--)
2529 {
2530 if (dir == BACKWARD)
2531 {
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002532 if ((long)curwin->w_curswant >= width1)
2533 // Move back within the line. This can give a negative value
2534 // for w_curswant if width1 < width2 (with cpoptions+=n),
2535 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 curwin->w_curswant -= width2;
2537 else
2538 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002539 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (curwin->w_cursor.lnum == 1)
2541 {
2542 retval = FAIL;
2543 break;
2544 }
2545 --curwin->w_cursor.lnum;
2546#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002547 // Move to the start of a closed fold. Don't do that when
2548 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (!(fdo_flags & FDO_ALL))
2550 (void)hasFolding(curwin->w_cursor.lnum,
2551 &curwin->w_cursor.lnum, NULL);
2552#endif
2553 linelen = linetabsize(ml_get_curline());
2554 if (linelen > width1)
2555 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2556 + 1) * width2;
2557 }
2558 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002559 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 if (linelen > width1)
2562 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2563 else
2564 n = width1;
2565 if (curwin->w_curswant + width2 < (colnr_T)n)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002566 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 curwin->w_curswant += width2;
2568 else
2569 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002570 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002572 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2574 &curwin->w_cursor.lnum);
2575#endif
2576 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2577 {
2578 retval = FAIL;
2579 break;
2580 }
2581 curwin->w_cursor.lnum++;
2582 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002583 // Check if the cursor has moved below the number display
2584 // when width1 < width2 (with cpoptions+=n). Subtract width2
2585 // to get a negative value for w_curswant, which will get
2586 // clipped to column 0.
2587 if (curwin->w_curswant >= width1)
2588 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002589 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591 }
2592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002595 if (virtual_active() && atend)
2596 coladvance(MAXCOL);
2597 else
2598 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2601 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002602 colnr_T virtcol;
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 /*
2605 * Check for landing on a character that got split at the end of the
2606 * last line. We want to advance a screenline, not end up in the same
2607 * screenline or move two screenlines.
2608 */
2609 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002610 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002611#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002612 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2613 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002614#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002615
2616 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 && (curwin->w_curswant < (colnr_T)width1
2618 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2619 : ((curwin->w_curswant - width1) % width2
2620 > (colnr_T)width2 / 2)))
2621 --curwin->w_cursor.col;
2622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
2624 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002625 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 return retval;
2628}
2629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630/*
2631 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2632 * cap->arg must be TRUE for CTRL-E.
2633 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002635nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 if (!checkclearop(cap->oap))
2638 scroll_redraw(cap->arg, cap->count1);
2639}
2640
2641/*
2642 * Scroll "count" lines up or down, and redraw.
2643 */
2644 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002645scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 linenr_T prev_topline = curwin->w_topline;
2648#ifdef FEAT_DIFF
2649 int prev_topfill = curwin->w_topfill;
2650#endif
2651 linenr_T prev_lnum = curwin->w_cursor.lnum;
2652
2653 if (up)
2654 scrollup(count, TRUE);
2655 else
2656 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002657 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002659 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2660 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 cursor_correct();
2662 check_cursor_moved(curwin);
2663 curwin->w_valid |= VALID_TOPLINE;
2664
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002665 // If moved back to where we were, at least move the cursor, otherwise
2666 // we get stuck at one position. Don't move the cursor up if the
2667 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 while (curwin->w_topline == prev_topline
2669#ifdef FEAT_DIFF
2670 && curwin->w_topfill == prev_topfill
2671#endif
2672 )
2673 {
2674 if (up)
2675 {
2676 if (curwin->w_cursor.lnum > prev_lnum
2677 || cursor_down(1L, FALSE) == FAIL)
2678 break;
2679 }
2680 else
2681 {
2682 if (curwin->w_cursor.lnum < prev_lnum
2683 || prev_topline == 1L
2684 || cursor_up(1L, FALSE) == FAIL)
2685 break;
2686 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002687 // Mark w_topline as valid, otherwise the screen jumps back at the
2688 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 check_cursor_moved(curwin);
2690 curwin->w_valid |= VALID_TOPLINE;
2691 }
2692 }
2693 if (curwin->w_cursor.lnum != prev_lnum)
2694 coladvance(curwin->w_curswant);
2695 redraw_later(VALID);
2696}
2697
2698/*
2699 * Commands that start with "z".
2700 */
2701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002702nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 long n;
2705 colnr_T col;
2706 int nchar = cap->nchar;
2707#ifdef FEAT_FOLDING
2708 long old_fdl = curwin->w_p_fdl;
2709 int old_fen = curwin->w_p_fen;
2710#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002711#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002712 int undo = FALSE;
2713#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002714 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 if (VIM_ISDIGIT(nchar))
2717 {
2718 /*
2719 * "z123{nchar}": edit the count before obtaining {nchar}
2720 */
2721 if (checkclearop(cap->oap))
2722 return;
2723 n = nchar - '0';
2724 for (;;)
2725 {
2726#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002727 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#endif
2729 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002731 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 --no_mapping;
2734 --allow_keys;
2735#ifdef FEAT_CMDL_INFO
2736 (void)add_to_showcmd(nchar);
2737#endif
2738 if (nchar == K_DEL || nchar == K_KDEL)
2739 n /= 10;
2740 else if (VIM_ISDIGIT(nchar))
2741 n = n * 10 + (nchar - '0');
2742 else if (nchar == CAR)
2743 {
2744#ifdef FEAT_GUI
2745 need_mouse_correct = TRUE;
2746#endif
2747 win_setheight((int)n);
2748 break;
2749 }
2750 else if (nchar == 'l'
2751 || nchar == 'h'
2752 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002753 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
2755 cap->count1 = n ? n * cap->count1 : cap->count1;
2756 goto dozet;
2757 }
2758 else
2759 {
2760 clearopbeep(cap->oap);
2761 break;
2762 }
2763 }
2764 cap->oap->op_type = OP_NOP;
2765 return;
2766 }
2767
2768dozet:
2769 if (
2770#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2772 // and "zC" only in Visual mode. "zj" and "zk" are motion
2773 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 cap->nchar != 'f' && cap->nchar != 'F'
2775 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2776 && cap->nchar != 'j' && cap->nchar != 'k'
2777 &&
2778#endif
2779 checkclearop(cap->oap))
2780 return;
2781
2782 /*
2783 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2784 * If line number given, set cursor.
2785 */
2786 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2787 && cap->count0
2788 && cap->count0 != curwin->w_cursor.lnum)
2789 {
2790 setpcmark();
2791 if (cap->count0 > curbuf->b_ml.ml_line_count)
2792 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2793 else
2794 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002795 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 }
2797
2798 switch (nchar)
2799 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002800 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 case '+':
2802 if (cap->count0 == 0)
2803 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002804 // No count given: put cursor at the line below screen
2805 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2807 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2808 else
2809 curwin->w_cursor.lnum = curwin->w_botline;
2810 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002811 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 case NL:
2813 case CAR:
2814 case K_KENTER:
2815 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002816 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
2818 case 't': scroll_cursor_top(0, TRUE);
2819 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002820 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 break;
2822
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002825 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826
2827 case 'z': scroll_cursor_halfway(TRUE);
2828 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002829 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 break;
2831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002832 // "z^", "z-" and "zb": put cursor at bottom of screen
2833 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2834 // when <count> is at bottom of window, and puts that one at
2835 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 if (cap->count0 != 0)
2837 {
2838 scroll_cursor_bot(0, TRUE);
2839 curwin->w_cursor.lnum = curwin->w_topline;
2840 }
2841 else if (curwin->w_topline == 1)
2842 curwin->w_cursor.lnum = 1;
2843 else
2844 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002845 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 case '-':
2847 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002848 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850 case 'b': scroll_cursor_bot(0, TRUE);
2851 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002852 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 break;
2854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002855 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002857 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002860 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 case 'h':
2862 case K_LEFT:
2863 if (!curwin->w_p_wrap)
2864 {
2865 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2866 curwin->w_leftcol = 0;
2867 else
2868 curwin->w_leftcol -= (colnr_T)cap->count1;
2869 leftcol_changed();
2870 }
2871 break;
2872
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002873 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002874 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002875 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 case 'l':
2879 case K_RIGHT:
2880 if (!curwin->w_p_wrap)
2881 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002882 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 curwin->w_leftcol += (colnr_T)cap->count1;
2884 leftcol_changed();
2885 }
2886 break;
2887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002888 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 case 's': if (!curwin->w_p_wrap)
2890 {
2891#ifdef FEAT_FOLDING
2892 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002893 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 else
2895#endif
2896 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002897 if ((long)col > siso)
2898 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 else
2900 col = 0;
2901 if (curwin->w_leftcol != col)
2902 {
2903 curwin->w_leftcol = col;
2904 redraw_later(NOT_VALID);
2905 }
2906 }
2907 break;
2908
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 case 'e': if (!curwin->w_p_wrap)
2911 {
2912#ifdef FEAT_FOLDING
2913 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 else
2916#endif
2917 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002918 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002919 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 col = 0;
2921 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002922 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 if (curwin->w_leftcol != col)
2924 {
2925 curwin->w_leftcol = col;
2926 redraw_later(NOT_VALID);
2927 }
2928 }
2929 break;
2930
2931#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // "zF": create fold command
2933 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 case 'F':
2935 case 'f': if (foldManualAllowed(TRUE))
2936 {
2937 cap->nchar = 'f';
2938 nv_operator(cap);
2939 curwin->w_p_fen = TRUE;
2940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
2943 {
2944 nv_operator(cap);
2945 finish_op = TRUE;
2946 }
2947 }
2948 else
2949 clearopbeep(cap->oap);
2950 break;
2951
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002952 // "zd": delete fold at cursor
2953 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 case 'd':
2955 case 'D': if (foldManualAllowed(FALSE))
2956 {
2957 if (VIsual_active)
2958 nv_operator(cap);
2959 else
2960 deleteFold(curwin->w_cursor.lnum,
2961 curwin->w_cursor.lnum, nchar == 'D', FALSE);
2962 }
2963 break;
2964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002965 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 case 'E': if (foldmethodIsManual(curwin))
2967 {
2968 clearFolding(curwin);
2969 changed_window_setting();
2970 }
2971 else if (foldmethodIsMarker(curwin))
2972 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
2973 TRUE, FALSE);
2974 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002975 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 break;
2977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 case 'n': curwin->w_p_fen = FALSE;
2980 break;
2981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 case 'N': curwin->w_p_fen = TRUE;
2984 break;
2985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
2988 break;
2989
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002990 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
2992 openFold(curwin->w_cursor.lnum, cap->count1);
2993 else
2994 {
2995 closeFold(curwin->w_cursor.lnum, cap->count1);
2996 curwin->w_p_fen = TRUE;
2997 }
2998 break;
2999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3002 openFoldRecurse(curwin->w_cursor.lnum);
3003 else
3004 {
3005 closeFoldRecurse(curwin->w_cursor.lnum);
3006 curwin->w_p_fen = TRUE;
3007 }
3008 break;
3009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 case 'o': if (VIsual_active)
3012 nv_operator(cap);
3013 else
3014 openFold(curwin->w_cursor.lnum, cap->count1);
3015 break;
3016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003017 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 case 'O': if (VIsual_active)
3019 nv_operator(cap);
3020 else
3021 openFoldRecurse(curwin->w_cursor.lnum);
3022 break;
3023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 case 'c': if (VIsual_active)
3026 nv_operator(cap);
3027 else
3028 closeFold(curwin->w_cursor.lnum, cap->count1);
3029 curwin->w_p_fen = TRUE;
3030 break;
3031
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003032 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 case 'C': if (VIsual_active)
3034 nv_operator(cap);
3035 else
3036 closeFoldRecurse(curwin->w_cursor.lnum);
3037 curwin->w_p_fen = TRUE;
3038 break;
3039
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003040 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 case 'v': foldOpenCursor();
3042 break;
3043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003046 curwin->w_foldinvalid = TRUE; // recompute folds
3047 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 foldOpenCursor();
3049 break;
3050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003051 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003053 curwin->w_foldinvalid = TRUE; // recompute folds
3054 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 break;
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003059 {
3060 curwin->w_p_fdl -= cap->count1;
3061 if (curwin->w_p_fdl < 0)
3062 curwin->w_p_fdl = 0;
3063 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003064 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 curwin->w_p_fen = TRUE;
3066 break;
3067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003070 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 curwin->w_p_fen = TRUE;
3072 break;
3073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003074 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003075 case 'r': curwin->w_p_fdl += cap->count1;
3076 {
3077 int d = getDeepestNesting();
3078
3079 if (curwin->w_p_fdl >= d)
3080 curwin->w_p_fdl = d;
3081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 break;
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003086 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 break;
3088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003089 case 'j': // "zj" move to next fold downwards
3090 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3092 cap->count1) == FAIL)
3093 clearopbeep(cap->oap);
3094 break;
3095
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003096#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003098#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003100 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003101 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003102 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003103 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003104 --no_mapping;
3105 --allow_keys;
3106#ifdef FEAT_CMDL_INFO
3107 (void)add_to_showcmd(nchar);
3108#endif
3109 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3110 {
3111 clearopbeep(cap->oap);
3112 break;
3113 }
3114 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003116
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003117 case 'g': // "zg": add good word to word list
3118 case 'w': // "zw": add wrong word to word list
3119 case 'G': // "zG": add good word to temp word list
3120 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003121 {
3122 char_u *ptr = NULL;
3123 int len;
3124
3125 if (checkclearop(cap->oap))
3126 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003127 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3128 == FAIL)
3129 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003130 if (ptr == NULL)
3131 {
3132 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // Find bad word under the cursor. When 'spell' is
3135 // off this fails and find_ident_under_cursor() is
3136 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003137 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003138 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003139 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003140 if (len != 0 && curwin->w_cursor.col <= pos.col)
3141 ptr = ml_get_pos(&curwin->w_cursor);
3142 curwin->w_cursor = pos;
3143 }
3144
Bram Moolenaarb765d632005-06-07 21:00:02 +00003145 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3146 FIND_IDENT)) == 0)
3147 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003148 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3149 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003150 (nchar == 'G' || nchar == 'W')
3151 ? 0 : (int)cap->count1,
3152 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003153 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003154 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003156 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003157 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003158 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003159 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003160#endif
3161
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 default: clearopbeep(cap->oap);
3163 }
3164
3165#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003166 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 if (old_fen != curwin->w_p_fen)
3168 {
3169# ifdef FEAT_DIFF
3170 win_T *wp;
3171
3172 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3173 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003174 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 FOR_ALL_WINDOWS(wp)
3176 {
3177 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3178 {
3179 wp->w_p_fen = curwin->w_p_fen;
3180 changed_window_setting_win(wp);
3181 }
3182 }
3183 }
3184# endif
3185 changed_window_setting();
3186 }
3187
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003188 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (old_fdl != curwin->w_p_fdl)
3190 newFoldLevel();
3191#endif
3192}
3193
3194#ifdef FEAT_GUI
3195/*
3196 * Vertical scrollbar movement.
3197 */
3198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003199nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 if (cap->oap->op_type != OP_NOP)
3202 clearopbeep(cap->oap);
3203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003204 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 gui_do_scroll();
3206}
3207
3208/*
3209 * Horizontal scrollbar movement.
3210 */
3211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003212nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
3214 if (cap->oap->op_type != OP_NOP)
3215 clearopbeep(cap->oap);
3216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003217 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003218 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219}
3220#endif
3221
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003222#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003223/*
3224 * Click in GUI tab.
3225 */
3226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003227nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003228{
3229 if (cap->oap->op_type != OP_NOP)
3230 clearopbeep(cap->oap);
3231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003232 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003233 goto_tabpage(current_tab);
3234}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003235
3236/*
3237 * Selected item in tab line menu.
3238 */
3239 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003240nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003241{
3242 if (cap->oap->op_type != OP_NOP)
3243 clearopbeep(cap->oap);
3244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003245 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003246 handle_tabmenu();
3247}
3248
3249/*
3250 * Handle selecting an item of the GUI tab line menu.
3251 * Used in Normal and Insert mode.
3252 */
3253 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003254handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003255{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003256 switch (current_tabmenu)
3257 {
3258 case TABLINE_MENU_CLOSE:
3259 if (current_tab == 0)
3260 do_cmdline_cmd((char_u *)"tabclose");
3261 else
3262 {
3263 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3264 current_tab);
3265 do_cmdline_cmd(IObuff);
3266 }
3267 break;
3268
3269 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003270 if (current_tab == 0)
3271 do_cmdline_cmd((char_u *)"$tabnew");
3272 else
3273 {
3274 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3275 current_tab - 1);
3276 do_cmdline_cmd(IObuff);
3277 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003278 break;
3279
3280 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003281 if (current_tab == 0)
3282 do_cmdline_cmd((char_u *)"browse $tabnew");
3283 else
3284 {
3285 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3286 current_tab - 1);
3287 do_cmdline_cmd(IObuff);
3288 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003289 break;
3290 }
3291}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003292#endif
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294/*
3295 * "Q" command.
3296 */
3297 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003298nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 /*
3301 * Ignore 'Q' in Visual mode, just give a beep.
3302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003304 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003305 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 do_exmode(FALSE);
3307}
3308
3309/*
3310 * Handle a ":" command.
3311 */
3312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003313nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003316 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (VIsual_active)
3319 nv_operator(cap);
3320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
3322 if (cap->oap->op_type != OP_NOP)
3323 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003324 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 cap->oap->motion_type = MCHAR;
3326 cap->oap->inclusive = FALSE;
3327 }
3328 else if (cap->count0)
3329 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003330 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 stuffcharReadbuff('.');
3332 if (cap->count0 > 1)
3333 {
3334 stuffReadbuff((char_u *)",.+");
3335 stuffnumReadbuff((long)cap->count0 - 1L);
3336 }
3337 }
3338
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003339 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (KeyTyped)
3341 compute_cmdrow();
3342
3343 old_p_im = p_im;
3344
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003345 // get a command line and execute it
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003346 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3348
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003349 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 if (p_im != old_p_im)
3351 {
3352 if (p_im)
3353 restart_edit = 'i';
3354 else
3355 restart_edit = 0;
3356 }
3357
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003358 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003359 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003360 clearop(cap->oap);
3361 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3363 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003364 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3365 || did_emsg
3366 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003367 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 clearopbeep(cap->oap);
3369 }
3370}
3371
3372/*
3373 * Handle CTRL-G command.
3374 */
3375 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003376nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003378 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 {
3380 VIsual_select = !VIsual_select;
3381 showmode();
3382 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003383 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003384 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 fileinfo((int)cap->count0, FALSE, TRUE);
3386}
3387
3388/*
3389 * Handle CTRL-H <Backspace> command.
3390 */
3391 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003392nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (VIsual_active && VIsual_select)
3395 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003396 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 v_visop(cap);
3398 }
3399 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 nv_left(cap);
3401}
3402
3403/*
3404 * CTRL-L: clear screen and redraw.
3405 */
3406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003407nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
3409 if (!checkclearop(cap->oap))
3410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003412 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003413 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003414# ifdef FEAT_RELTIME
3415 {
3416 win_T *wp;
3417
3418 FOR_ALL_WINDOWS(wp)
3419 wp->w_s->b_syn_slow = FALSE;
3420 }
3421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422#endif
3423 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003424#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3425# ifdef VIMDLL
3426 if (!gui.in_use)
3427# endif
3428 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
3431}
3432
3433/*
3434 * CTRL-O: In Select mode: switch to Visual mode for one command.
3435 * Otherwise: Go to older pcmark.
3436 */
3437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003438nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (VIsual_active && VIsual_select)
3441 {
3442 VIsual_select = FALSE;
3443 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 cap->count1 = -cap->count1;
3449 nv_pcmark(cap);
3450 }
3451}
3452
3453/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003454 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3455 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 */
3457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003458nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 if (!checkclearopq(cap->oap))
3461 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3462 GETF_SETMARK|GETF_ALT, FALSE);
3463}
3464
3465/*
3466 * "Z" commands.
3467 */
3468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003469nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
3471 if (!checkclearopq(cap->oap))
3472 {
3473 switch (cap->nchar)
3474 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003475 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 case 'Z': do_cmdline_cmd((char_u *)"x");
3477 break;
3478
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003479 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 case 'Q': do_cmdline_cmd((char_u *)"q!");
3481 break;
3482
3483 default: clearopbeep(cap->oap);
3484 }
3485 }
3486}
3487
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488/*
3489 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3490 */
3491 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003492do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 oparg_T oa;
3495 cmdarg_T ca;
3496
3497 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003498 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 ca.oap = &oa;
3500 ca.cmdchar = c1;
3501 ca.nchar = c2;
3502 nv_ident(&ca);
3503}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
3505/*
3506 * Handle the commands that use the word under the cursor.
3507 * [g] CTRL-] :ta to current identifier
3508 * [g] 'K' run program for current identifier
3509 * [g] '*' / to current identifier or string
3510 * [g] '#' ? to current identifier or string
3511 * g ']' :tselect for current identifier
3512 */
3513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003514nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 char_u *ptr = NULL;
3517 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003518 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003519 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003521 char_u *kp; // value of 'keywordprg'
3522 int kp_help; // 'keywordprg' is ":he"
3523 int kp_ex; // 'keywordprg' starts with ":"
3524 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003526 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003527 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 char_u *aux_ptr;
3529 int isman;
3530 int isman_s;
3531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003532 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
3534 cmdchar = cap->nchar;
3535 g_cmd = TRUE;
3536 }
3537 else
3538 {
3539 cmdchar = cap->cmdchar;
3540 g_cmd = FALSE;
3541 }
3542
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003543 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 cmdchar = '#';
3545
3546 /*
3547 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3548 */
3549 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3552 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (checkclearopq(cap->oap))
3554 return;
3555 }
3556
3557 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3558 (cmdchar == '*' || cmdchar == '#')
3559 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3560 {
3561 clearop(cap->oap);
3562 return;
3563 }
3564
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003565 // Allocate buffer to put the command in. Inserting backslashes can
3566 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3567 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3569 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3570 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003571 if (kp_help && *skipwhite(ptr) == NUL)
3572 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003573 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003574 return;
3575 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003576 kp_ex = (*kp == ':');
3577 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3578 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (buf == NULL)
3580 return;
3581 buf[0] = NUL;
3582
3583 switch (cmdchar)
3584 {
3585 case '*':
3586 case '#':
3587 /*
3588 * Put cursor at start of word, makes search skip the word
3589 * under the cursor.
3590 * Call setpcmark() first, so "*``" puts the cursor back where
3591 * it was.
3592 */
3593 setpcmark();
3594 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3595
3596 if (!g_cmd && vim_iswordp(ptr))
3597 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003598 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 break;
3600
3601 case 'K':
3602 if (kp_help)
3603 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003604 else if (kp_ex)
3605 {
3606 if (cap->count0 != 0)
3607 vim_snprintf((char *)buf, buflen, "%s %ld",
3608 kp, cap->count0);
3609 else
3610 STRCPY(buf, kp);
3611 STRCAT(buf, " ");
3612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 else
3614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003615 // An external command will probably use an argument starting
3616 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003617 while (*ptr == '-' && n > 0)
3618 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003619 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003620 --n;
3621 }
3622 if (n == 0)
3623 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003624 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003625 vim_free(buf);
3626 return;
3627 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003628
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003629 // When a count is given, turn it into a range. Is this
3630 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 isman = (STRCMP(kp, "man") == 0);
3632 isman_s = (STRCMP(kp, "man -s") == 0);
3633 if (cap->count0 != 0 && !(isman || isman_s))
3634 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3635
3636 STRCAT(buf, "! ");
3637 if (cap->count0 == 0 && isman_s)
3638 STRCAT(buf, "man");
3639 else
3640 STRCAT(buf, kp);
3641 STRCAT(buf, " ");
3642 if (cap->count0 != 0 && (isman || isman_s))
3643 {
3644 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3645 STRCAT(buf, " ");
3646 }
3647 }
3648 break;
3649
3650 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003651 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652#ifdef FEAT_CSCOPE
3653 if (p_cst)
3654 STRCPY(buf, "cstag ");
3655 else
3656#endif
3657 STRCPY(buf, "ts ");
3658 break;
3659
3660 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003661 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (curbuf->b_help)
3663 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003665 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003666 if (g_cmd)
3667 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003668 else if (cap->count0 == 0)
3669 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003670 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003671 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 }
3674
3675 /*
3676 * Now grab the chars in the identifier
3677 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003678 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003680 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003681 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003682 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003683 p = vim_strsave_fnameescape(ptr, FALSE);
3684 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003685 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003686 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003687 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003688 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003690 vim_free(buf);
3691 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003693 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003694 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003695 {
3696 vim_free(buf);
3697 vim_free(p);
3698 return;
3699 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003700 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003701 STRCAT(buf, p);
3702 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003704 else
3705 {
3706 if (cmdchar == '*')
3707 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
3708 else if (cmdchar == '#')
3709 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003710 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003711 {
3712 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003713 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003714 aux_ptr = (char_u *)"";
3715 else
3716 aux_ptr = (char_u *)"\\|\"\n[";
3717 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003718 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003719 aux_ptr = (char_u *)"\\|\"\n*?[";
3720
3721 p = buf + STRLEN(buf);
3722 while (n-- > 0)
3723 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003724 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003725 if (vim_strchr(aux_ptr, *ptr) != NULL)
3726 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003727 // When current byte is a part of multibyte character, copy all
3728 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003729 if (has_mbyte)
3730 {
3731 int i;
3732 int len = (*mb_ptr2len)(ptr) - 1;
3733
3734 for (i = 0; i < len && n >= 1; ++i, --n)
3735 *p++ = *ptr++;
3736 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003737 *p++ = *ptr++;
3738 }
3739 *p = NUL;
3740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
3742 /*
3743 * Execute the command.
3744 */
3745 if (cmdchar == '*' || cmdchar == '#')
3746 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003747 if (!g_cmd && (has_mbyte
3748 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3749 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003751
3752 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003753 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003755
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003756 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003759 {
3760 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003762 g_tag_at_cursor = FALSE;
3763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 vim_free(buf);
3766}
3767
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768/*
3769 * Get visually selected text, within one line only.
3770 * Returns FAIL if more than one line selected.
3771 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003772 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003773get_visual_text(
3774 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003775 char_u **pp, // return: start of selected text
3776 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777{
3778 if (VIsual_mode != 'V')
3779 unadjust_for_sel();
3780 if (VIsual.lnum != curwin->w_cursor.lnum)
3781 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003782 if (cap != NULL)
3783 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 return FAIL;
3785 }
3786 if (VIsual_mode == 'V')
3787 {
3788 *pp = ml_get_curline();
3789 *lenp = (int)STRLEN(*pp);
3790 }
3791 else
3792 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003793 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
3795 *pp = ml_get_pos(&curwin->w_cursor);
3796 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3797 }
3798 else
3799 {
3800 *pp = ml_get_pos(&VIsual);
3801 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003804 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003805 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807 reset_VIsual_and_resel();
3808 return OK;
3809}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811/*
3812 * CTRL-T: backwards in tag stack
3813 */
3814 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003815nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
3817 if (!checkclearopq(cap->oap))
3818 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3819}
3820
3821/*
3822 * Handle scrolling command 'H', 'L' and 'M'.
3823 */
3824 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003825nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826{
3827 int used = 0;
3828 long n;
3829#ifdef FEAT_FOLDING
3830 linenr_T lnum;
3831#endif
3832 int half;
3833
3834 cap->oap->motion_type = MLINE;
3835 setpcmark();
3836
3837 if (cap->cmdchar == 'L')
3838 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003839 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 curwin->w_cursor.lnum = curwin->w_botline - 1;
3841 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3842 curwin->w_cursor.lnum = 1;
3843 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003844 {
3845#ifdef FEAT_FOLDING
3846 if (hasAnyFolding(curwin))
3847 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003848 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003849 for (n = cap->count1 - 1; n > 0
3850 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3851 {
3852 (void)hasFolding(curwin->w_cursor.lnum,
3853 &curwin->w_cursor.lnum, NULL);
3854 --curwin->w_cursor.lnum;
3855 }
3856 }
3857 else
3858#endif
3859 curwin->w_cursor.lnum -= cap->count1 - 1;
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
3862 else
3863 {
3864 if (cap->cmdchar == 'M')
3865 {
3866#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003867 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 used -= diff_check_fill(curwin, curwin->w_topline)
3869 - curwin->w_topfill;
3870#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003871 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3873 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3874 {
3875#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003876 // Count half he number of filler lines to be "below this
3877 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3879 + n) / 2 >= half)
3880 {
3881 --n;
3882 break;
3883 }
3884#endif
3885 used += plines(curwin->w_topline + n);
3886 if (used >= half)
3887 break;
3888#ifdef FEAT_FOLDING
3889 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3890 n = lnum - curwin->w_topline;
3891#endif
3892 }
3893 if (n > 0 && used > curwin->w_height)
3894 --n;
3895 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003896 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003899#ifdef FEAT_FOLDING
3900 if (hasAnyFolding(curwin))
3901 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003902 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003903 lnum = curwin->w_topline;
3904 while (n-- > 0 && lnum < curwin->w_botline - 1)
3905 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003906 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003907 ++lnum;
3908 }
3909 n = lnum - curwin->w_topline;
3910 }
3911#endif
3912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 curwin->w_cursor.lnum = curwin->w_topline + n;
3914 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3915 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3916 }
3917
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003918 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003919 if (cap->oap->op_type == OP_NOP)
3920 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 beginline(BL_SOL | BL_FIX);
3922}
3923
3924/*
3925 * Cursor right commands.
3926 */
3927 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003928nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003931 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003933 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3934 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003935 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003936 if (mod_mask & MOD_MASK_CTRL)
3937 cap->arg = TRUE;
3938 nv_wordcmd(cap);
3939 return;
3940 }
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 cap->oap->motion_type = MCHAR;
3943 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003944 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003947 * In virtual edit mode, there's no such thing as "past_line", as lines
3948 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 */
3950 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003951 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
3953 for (n = cap->count1; n > 0; --n)
3954 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003955 if ((!past_line && oneright() == FAIL)
3956 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00003957 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
3959 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003960 * <Space> wraps to next line if 'whichwrap' has 's'.
3961 * 'l' wraps to next line if 'whichwrap' has 'l'.
3962 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 */
3964 if ( ((cap->cmdchar == ' '
3965 && vim_strchr(p_ww, 's') != NULL)
3966 || (cap->cmdchar == 'l'
3967 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00003968 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 && vim_strchr(p_ww, '>') != NULL))
3970 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
3971 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003972 // When deleting we also count the NL as a character.
3973 // Set cap->oap->inclusive when last char in the line is
3974 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003975 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003977 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 cap->oap->inclusive = TRUE;
3979 else
3980 {
3981 ++curwin->w_cursor.lnum;
3982 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 curwin->w_set_curswant = TRUE;
3985 cap->oap->inclusive = FALSE;
3986 }
3987 continue;
3988 }
3989 if (cap->oap->op_type == OP_NOP)
3990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003991 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 if (n == cap->count1)
3993 beep_flush();
3994 }
3995 else
3996 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003997 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 cap->oap->inclusive = TRUE;
3999 }
4000 break;
4001 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004002 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
4004 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if (virtual_active())
4006 oneright();
4007 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004010 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 ++curwin->w_cursor.col;
4013 }
4014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016#ifdef FEAT_FOLDING
4017 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4018 && cap->oap->op_type == OP_NOP)
4019 foldOpenCursor();
4020#endif
4021}
4022
4023/*
4024 * Cursor left commands.
4025 *
4026 * Returns TRUE when operator end should not be adjusted.
4027 */
4028 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004029nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
4031 long n;
4032
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004033 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4034 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004035 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004036 if (mod_mask & MOD_MASK_CTRL)
4037 cap->arg = 1;
4038 nv_bck_word(cap);
4039 return;
4040 }
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 cap->oap->motion_type = MCHAR;
4043 cap->oap->inclusive = FALSE;
4044 for (n = cap->count1; n > 0; --n)
4045 {
4046 if (oneleft() == FAIL)
4047 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004048 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4049 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4050 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 if ( (((cap->cmdchar == K_BS
4052 || cap->cmdchar == Ctrl_H)
4053 && vim_strchr(p_ww, 'b') != NULL)
4054 || (cap->cmdchar == 'h'
4055 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004056 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 && vim_strchr(p_ww, '<') != NULL))
4058 && curwin->w_cursor.lnum > 1)
4059 {
4060 --(curwin->w_cursor.lnum);
4061 coladvance((colnr_T)MAXCOL);
4062 curwin->w_set_curswant = TRUE;
4063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004064 // When the NL before the first char has to be deleted we
4065 // put the cursor on the NUL after the previous line.
4066 // This is a very special case, be careful!
4067 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if ( (cap->oap->op_type == OP_DELETE
4069 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004070 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004072 char_u *cp = ml_get_cursor();
4073
4074 if (*cp != NUL)
4075 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004076 if (has_mbyte)
4077 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4078 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004079 ++curwin->w_cursor.col;
4080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 cap->retval |= CA_NO_ADJ_OP_END;
4082 }
4083 continue;
4084 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004085 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4087 beep_flush();
4088 break;
4089 }
4090 }
4091#ifdef FEAT_FOLDING
4092 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4093 && cap->oap->op_type == OP_NOP)
4094 foldOpenCursor();
4095#endif
4096}
4097
4098/*
4099 * Cursor up commands.
4100 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4101 */
4102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004103nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004105 if (mod_mask & MOD_MASK_SHIFT)
4106 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004107 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004108 cap->arg = BACKWARD;
4109 nv_page(cap);
4110 }
4111 else
4112 {
4113 cap->oap->motion_type = MLINE;
4114 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4115 clearopbeep(cap->oap);
4116 else if (cap->arg)
4117 beginline(BL_WHITE | BL_FIX);
4118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119}
4120
4121/*
4122 * Cursor down commands.
4123 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4124 */
4125 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004126nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004128 if (mod_mask & MOD_MASK_SHIFT)
4129 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004130 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004131 cap->arg = FORWARD;
4132 nv_page(cap);
4133 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004134#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004135 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004136 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4137 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
4141#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004142 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004143 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 cmdwin_result = CAR;
4145 else
4146#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004147#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004148 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004149 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4150 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4151 {
4152 invoke_prompt_callback();
4153 if (restart_edit == 0)
4154 restart_edit = 'a';
4155 }
4156 else
4157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
4159 cap->oap->motion_type = MLINE;
4160 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4161 clearopbeep(cap->oap);
4162 else if (cap->arg)
4163 beginline(BL_WHITE | BL_FIX);
4164 }
4165 }
4166}
4167
4168#ifdef FEAT_SEARCHPATH
4169/*
4170 * Grab the file name under the cursor and edit it.
4171 */
4172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004173nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
4175 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004176 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004178 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004181 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return;
4183 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004184 if (curbuf_locked())
4185 {
4186 clearop(cap->oap);
4187 return;
4188 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004189#ifdef FEAT_PROP_POPUP
4190 if (ERROR_IF_TERM_POPUP_WINDOW)
4191 return;
4192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004194 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196 if (ptr != NULL)
4197 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004198 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004199 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004200 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004202 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004203 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004204 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004205 {
4206 curwin->w_cursor.lnum = lnum;
4207 check_cursor_lnum();
4208 beginline(BL_SOL | BL_FIX);
4209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 vim_free(ptr);
4211 }
4212 else
4213 clearop(cap->oap);
4214}
4215#endif
4216
4217/*
4218 * <End> command: to end of current line or last line.
4219 */
4220 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004221nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004223 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004225 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004227 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229 nv_dollar(cap);
4230}
4231
4232/*
4233 * Handle the "$" command.
4234 */
4235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004236nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238 cap->oap->motion_type = MCHAR;
4239 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004240 // In virtual mode when off the edge of a line and an operator
4241 // is pending (whew!) keep the cursor where it is.
4242 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 if (!virtual_active() || gchar_cursor() != NUL
4244 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004245 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 if (cursor_down((long)(cap->count1 - 1),
4247 cap->oap->op_type == OP_NOP) == FAIL)
4248 clearopbeep(cap->oap);
4249#ifdef FEAT_FOLDING
4250 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4251 foldOpenCursor();
4252#endif
4253}
4254
4255/*
4256 * Implementation of '?' and '/' commands.
4257 * If cap->arg is TRUE don't set PC mark.
4258 */
4259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004260nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
4262 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004263 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
4265 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4266 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004267 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 cap->cmdchar = 'g';
4269 cap->nchar = '?';
4270 nv_operator(cap);
4271 return;
4272 }
4273
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004274 // When using 'incsearch' the cursor may be moved to set a different search
4275 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004276 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278 if (cap->searchbuf == NULL)
4279 {
4280 clearop(oap);
4281 return;
4282 }
4283
Bram Moolenaar46539112015-02-17 15:43:57 +01004284 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004285 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004286 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287}
4288
4289/*
4290 * Handle "N" and "n" commands.
4291 * cap->arg is SEARCH_REV for "N", 0 for "n".
4292 */
4293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004294nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004296 pos_T old = curwin->w_cursor;
4297 int wrapped = FALSE;
4298 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004299
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004300 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004301 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004302 // Avoid getting stuck on the current cursor position, which can
4303 // happen when an offset is given and the cursor is on the last char
4304 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004305 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004306 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004307 cap->count1 -= 1;
4308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309}
4310
4311/*
4312 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4313 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004314 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004316 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004317normal_search(
4318 cmdarg_T *cap,
4319 int dir,
4320 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004321 int opt, // extra flags for do_search()
4322 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323{
4324 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004325 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326
4327 cap->oap->motion_type = MCHAR;
4328 cap->oap->inclusive = FALSE;
4329 cap->oap->use_reg_one = TRUE;
4330 curwin->w_set_curswant = TRUE;
4331
Bram Moolenaara80faa82020-04-12 19:37:17 +02004332 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004333 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004334 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4335 if (wrapped != NULL)
4336 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (i == 0)
4338 clearop(cap->oap);
4339 else
4340 {
4341 if (i == 2)
4342 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344#ifdef FEAT_FOLDING
4345 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4346 foldOpenCursor();
4347#endif
4348 }
4349
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004350 // "/$" will put the cursor after the end of the line, may need to
4351 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004353 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354}
4355
4356/*
4357 * Character search commands.
4358 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4359 * ',' and FALSE for ';'.
4360 * cap->nchar is NUL for ',' and ';' (repeat the search)
4361 */
4362 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004363nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
4365 int t_cmd;
4366
4367 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4368 t_cmd = TRUE;
4369 else
4370 t_cmd = FALSE;
4371
4372 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4374 clearopbeep(cap->oap);
4375 else
4376 {
4377 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004378 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4380 && (t_cmd || cap->oap->op_type != OP_NOP))
4381 {
4382 colnr_T scol, ecol;
4383
4384 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4385 curwin->w_cursor.coladd = ecol - scol;
4386 }
4387 else
4388 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#ifdef FEAT_FOLDING
4391 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4392 foldOpenCursor();
4393#endif
4394 }
4395}
4396
4397/*
4398 * "[" and "]" commands.
4399 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4400 */
4401 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004402nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004404 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004406 pos_T *pos = NULL; // init for GCC
4407 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 int flag;
4409 long n;
4410 int findc;
4411 int c;
4412
4413 cap->oap->motion_type = MCHAR;
4414 cap->oap->inclusive = FALSE;
4415 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004416 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418#ifdef FEAT_SEARCHPATH
4419 /*
4420 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4421 */
4422 if (cap->nchar == 'f')
4423 nv_gotofile(cap);
4424 else
4425#endif
4426
4427#ifdef FEAT_FIND_ID
4428 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004429 * Find the occurrence(s) of the identifier or define under cursor
4430 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 *
4432 * search list jump
4433 * fwd bwd fwd bwd fwd bwd
4434 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4435 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4436 */
4437 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004438# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004440# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 cap->nchar) != NULL)
4444 {
4445 char_u *ptr;
4446 int len;
4447
4448 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4449 clearop(cap->oap);
4450 else
4451 {
4452 find_pattern_in_path(ptr, 0, len, TRUE,
4453 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4454 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4455 cap->count1,
4456 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4457 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4458 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4459 (linenr_T)MAXLNUM);
4460 curwin->w_set_curswant = TRUE;
4461 }
4462 }
4463 else
4464#endif
4465
4466 /*
4467 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4468 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4469 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4470 * "[m" or "]m" search for prev/next start of (Java) method.
4471 * "[M" or "]M" search for prev/next end of (Java) method.
4472 */
4473 if ( (cap->cmdchar == '['
4474 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4475 || (cap->cmdchar == ']'
4476 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4477 {
4478 if (cap->nchar == '*')
4479 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 prev_pos.lnum = 0;
4481 if (cap->nchar == 'm' || cap->nchar == 'M')
4482 {
4483 if (cap->cmdchar == '[')
4484 findc = '{';
4485 else
4486 findc = '}';
4487 n = 9999;
4488 }
4489 else
4490 {
4491 findc = cap->nchar;
4492 n = cap->count1;
4493 }
4494 for ( ; n > 0; --n)
4495 {
4496 if ((pos = findmatchlimit(cap->oap, findc,
4497 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4498 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004499 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 {
4501 if (cap->nchar != 'm' && cap->nchar != 'M')
4502 clearopbeep(cap->oap);
4503 }
4504 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004505 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 break;
4507 }
4508 prev_pos = new_pos;
4509 curwin->w_cursor = *pos;
4510 new_pos = *pos;
4511 }
4512 curwin->w_cursor = old_pos;
4513
4514 /*
4515 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4516 * brought us to the match for "[m" and "]M" when inside a method.
4517 * Try finding the '{' or '}' we want to be at.
4518 * Also repeat for the given count.
4519 */
4520 if (cap->nchar == 'm' || cap->nchar == 'M')
4521 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004522 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 int norm = ((findc == '{') == (cap->nchar == 'm'));
4524
4525 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004526 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (prev_pos.lnum != 0)
4528 {
4529 pos = &prev_pos;
4530 curwin->w_cursor = prev_pos;
4531 if (norm)
4532 --n;
4533 }
4534 else
4535 pos = NULL;
4536 while (n > 0)
4537 {
4538 for (;;)
4539 {
4540 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4541 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004542 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (pos == NULL)
4544 clearopbeep(cap->oap);
4545 n = 0;
4546 break;
4547 }
4548 c = gchar_cursor();
4549 if (c == '{' || c == '}')
4550 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004551 // Must have found end/start of class: use it.
4552 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if ((c == findc && norm) || (n == 1 && !norm))
4554 {
4555 new_pos = curwin->w_cursor;
4556 pos = &new_pos;
4557 n = 0;
4558 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004559 // if no match found at all, we started outside of the
4560 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 else if (new_pos.lnum == 0)
4562 {
4563 new_pos = curwin->w_cursor;
4564 pos = &new_pos;
4565 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004566 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 else if ((pos = findmatchlimit(cap->oap, findc,
4568 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4569 0)) == NULL)
4570 n = 0;
4571 else
4572 curwin->w_cursor = *pos;
4573 break;
4574 }
4575 }
4576 --n;
4577 }
4578 curwin->w_cursor = old_pos;
4579 if (pos == NULL && new_pos.lnum != 0)
4580 clearopbeep(cap->oap);
4581 }
4582 if (pos != NULL)
4583 {
4584 setpcmark();
4585 curwin->w_cursor = *pos;
4586 curwin->w_set_curswant = TRUE;
4587#ifdef FEAT_FOLDING
4588 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4589 && cap->oap->op_type == OP_NOP)
4590 foldOpenCursor();
4591#endif
4592 }
4593 }
4594
4595 /*
4596 * "[[", "[]", "]]" and "][": move to start or end of function
4597 */
4598 else if (cap->nchar == '[' || cap->nchar == ']')
4599 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004600 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 flag = '{';
4602 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004603 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605 curwin->w_set_curswant = TRUE;
4606 /*
4607 * Imitate strange Vi behaviour: When using "]]" with an operator
4608 * we also stop at '}'.
4609 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004610 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 (cap->oap->op_type != OP_NOP
4612 && cap->arg == FORWARD && flag == '{')))
4613 clearopbeep(cap->oap);
4614 else
4615 {
4616 if (cap->oap->op_type == OP_NOP)
4617 beginline(BL_WHITE | BL_FIX);
4618#ifdef FEAT_FOLDING
4619 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4620 foldOpenCursor();
4621#endif
4622 }
4623 }
4624
4625 /*
4626 * "[p", "[P", "]P" and "]p": put with indent adjustment
4627 */
4628 else if (cap->nchar == 'p' || cap->nchar == 'P')
4629 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004630 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632
4633 /*
4634 * "['", "[`", "]'" and "]`": jump to next mark
4635 */
4636 else if (cap->nchar == '\'' || cap->nchar == '`')
4637 {
4638 pos = &curwin->w_cursor;
4639 for (n = cap->count1; n > 0; --n)
4640 {
4641 prev_pos = *pos;
4642 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4643 cap->nchar == '\'');
4644 if (pos == NULL)
4645 break;
4646 }
4647 if (pos == NULL)
4648 pos = &prev_pos;
4649 nv_cursormark(cap, cap->nchar == '\'', pos);
4650 }
4651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 /*
4653 * [ or ] followed by a middle mouse click: put selected text with
4654 * indent adjustment. Any other button just does as usual.
4655 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004656 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 {
4658 (void)do_mouse(cap->oap, cap->nchar,
4659 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4660 cap->count1, PUT_FIXINDENT);
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
4663#ifdef FEAT_FOLDING
4664 /*
4665 * "[z" and "]z": move to start or end of open fold.
4666 */
4667 else if (cap->nchar == 'z')
4668 {
4669 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4670 cap->count1) == FAIL)
4671 clearopbeep(cap->oap);
4672 }
4673#endif
4674
4675#ifdef FEAT_DIFF
4676 /*
4677 * "[c" and "]c": move to next or previous diff-change.
4678 */
4679 else if (cap->nchar == 'c')
4680 {
4681 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4682 cap->count1) == FAIL)
4683 clearopbeep(cap->oap);
4684 }
4685#endif
4686
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004687#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004688 /*
4689 * "[s", "[S", "]s" and "]S": move to next spell error.
4690 */
4691 else if (cap->nchar == 's' || cap->nchar == 'S')
4692 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004693 setpcmark();
4694 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004695 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4696 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004697 {
4698 clearopbeep(cap->oap);
4699 break;
4700 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004701 else
4702 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004703# ifdef FEAT_FOLDING
4704 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4705 foldOpenCursor();
4706# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004707 }
4708#endif
4709
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004710 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 else
4712 clearopbeep(cap->oap);
4713}
4714
4715/*
4716 * Handle Normal mode "%" command.
4717 */
4718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004719nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
4721 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004722#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 linenr_T lnum = curwin->w_cursor.lnum;
4724#endif
4725
4726 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004727 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
4729 if (cap->count0 > 100)
4730 clearopbeep(cap->oap);
4731 else
4732 {
4733 cap->oap->motion_type = MLINE;
4734 setpcmark();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004735 // Round up, so CTRL-G will give same value. Watch out for a
4736 // large line count, the line number must not go negative!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 if (curbuf->b_ml.ml_line_count > 1000000)
4738 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4739 / 100L * cap->count0;
4740 else
4741 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4742 cap->count0 + 99L) / 100L;
4743 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4744 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4745 beginline(BL_SOL | BL_FIX);
4746 }
4747 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004748 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
4750 cap->oap->motion_type = MCHAR;
4751 cap->oap->use_reg_one = TRUE;
4752 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4753 clearopbeep(cap->oap);
4754 else
4755 {
4756 setpcmark();
4757 curwin->w_cursor = *pos;
4758 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762 }
4763#ifdef FEAT_FOLDING
4764 if (cap->oap->op_type == OP_NOP
4765 && lnum != curwin->w_cursor.lnum
4766 && (fdo_flags & FDO_PERCENT)
4767 && KeyTyped)
4768 foldOpenCursor();
4769#endif
4770}
4771
4772/*
4773 * Handle "(" and ")" commands.
4774 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4775 */
4776 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004777nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
4779 cap->oap->motion_type = MCHAR;
4780 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004781 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004782 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 curwin->w_set_curswant = TRUE;
4784
4785 if (findsent(cap->arg, cap->count1) == FAIL)
4786 clearopbeep(cap->oap);
4787 else
4788 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004789 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004790 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792#ifdef FEAT_FOLDING
4793 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4794 foldOpenCursor();
4795#endif
4796 }
4797}
4798
4799/*
4800 * "m" command: Mark a position.
4801 */
4802 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004803nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
4805 if (!checkclearop(cap->oap))
4806 {
4807 if (setmark(cap->nchar) == FAIL)
4808 clearopbeep(cap->oap);
4809 }
4810}
4811
4812/*
4813 * "{" and "}" commands.
4814 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4815 */
4816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004817nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818{
4819 cap->oap->motion_type = MCHAR;
4820 cap->oap->inclusive = FALSE;
4821 cap->oap->use_reg_one = TRUE;
4822 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004823 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 clearopbeep(cap->oap);
4825 else
4826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#ifdef FEAT_FOLDING
4829 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4830 foldOpenCursor();
4831#endif
4832 }
4833}
4834
4835/*
4836 * "u" command: Undo or make lower case.
4837 */
4838 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004839nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004841 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004843 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 cap->cmdchar = 'g';
4845 cap->nchar = 'u';
4846 nv_operator(cap);
4847 }
4848 else
4849 nv_kundo(cap);
4850}
4851
4852/*
4853 * <Undo> command.
4854 */
4855 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004856nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
4858 if (!checkclearopq(cap->oap))
4859 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004860#ifdef FEAT_JOB_CHANNEL
4861 if (bt_prompt(curbuf))
4862 {
4863 clearopbeep(cap->oap);
4864 return;
4865 }
4866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 u_undo((int)cap->count1);
4868 curwin->w_set_curswant = TRUE;
4869 }
4870}
4871
4872/*
4873 * Handle the "r" command.
4874 */
4875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004876nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
4878 char_u *ptr;
4879 int had_ctrl_v;
4880 long n;
4881
4882 if (checkclearop(cap->oap))
4883 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004884#ifdef FEAT_JOB_CHANNEL
4885 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4886 {
4887 clearopbeep(cap->oap);
4888 return;
4889 }
4890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004892 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 if (cap->nchar == Ctrl_V)
4894 {
4895 had_ctrl_v = Ctrl_V;
4896 cap->nchar = get_literal();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004897 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (cap->nchar > DEL)
4899 had_ctrl_v = NUL;
4900 }
4901 else
4902 had_ctrl_v = NUL;
4903
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004904 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004905 if (IS_SPECIAL(cap->nchar))
4906 {
4907 clearopbeep(cap->oap);
4908 return;
4909 }
4910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004911 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 if (VIsual_active)
4913 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004914 if (got_int)
4915 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004916 if (had_ctrl_v)
4917 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004918 // Use a special (negative) number to make a difference between a
4919 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004920 if (cap->nchar == CAR)
4921 cap->nchar = REPLACE_CR_NCHAR;
4922 else if (cap->nchar == NL)
4923 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 nv_operator(cap);
4926 return;
4927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004929 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (virtual_active())
4931 {
4932 if (u_save_cursor() == FAIL)
4933 return;
4934 if (gchar_cursor() == NUL)
4935 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004936 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 coladvance_force((colnr_T)(getviscol() + cap->count1));
4938 curwin->w_cursor.col -= cap->count1;
4939 }
4940 else if (gchar_cursor() == TAB)
4941 coladvance_force(getviscol());
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004944 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004946 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004947 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 {
4949 clearopbeep(cap->oap);
4950 return;
4951 }
4952
4953 /*
4954 * Replacing with a TAB is done by edit() when it is complicated because
4955 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
4956 * Other characters are done below to avoid problems with things like
4957 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
4958 */
4959 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
4960 {
4961 stuffnumReadbuff(cap->count1);
4962 stuffcharReadbuff('R');
4963 stuffcharReadbuff('\t');
4964 stuffcharReadbuff(ESC);
4965 return;
4966 }
4967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004968 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (u_save_cursor() == FAIL)
4970 return;
4971
4972 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
4973 {
4974 /*
4975 * Replace character(s) by a single newline.
4976 * Strange vi behaviour: Only one newline is inserted.
4977 * Delete the characters here.
4978 * Insert the newline with an insert command, takes care of
4979 * autoindent. The insert command depends on being on the last
4980 * character of a line or not.
4981 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004982 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 stuffcharReadbuff('\r');
4984 stuffcharReadbuff(ESC);
4985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004986 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 invoke_edit(cap, TRUE, 'r', FALSE);
4988 }
4989 else
4990 {
4991 prep_redo(cap->oap->regname, cap->count1,
4992 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
4993
4994 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (has_mbyte)
4996 {
4997 int old_State = State;
4998
4999 if (cap->ncharC1 != 0)
5000 AppendCharToRedobuff(cap->ncharC1);
5001 if (cap->ncharC2 != 0)
5002 AppendCharToRedobuff(cap->ncharC2);
5003
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005004 // This is slow, but it handles replacing a single-byte with a
5005 // multi-byte and the other way around. Also handles adding
5006 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 for (n = cap->count1; n > 0; --n)
5008 {
5009 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005010 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5011 {
5012 int c = ins_copychar(curwin->w_cursor.lnum
5013 + (cap->nchar == Ctrl_Y ? -1 : 1));
5014 if (c != NUL)
5015 ins_char(c);
5016 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005017 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005018 ++curwin->w_cursor.col;
5019 }
5020 else
5021 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 State = old_State;
5023 if (cap->ncharC1 != 0)
5024 ins_char(cap->ncharC1);
5025 if (cap->ncharC2 != 0)
5026 ins_char(cap->ncharC2);
5027 }
5028 }
5029 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
5031 /*
5032 * Replace the characters within one line.
5033 */
5034 for (n = cap->count1; n > 0; --n)
5035 {
5036 /*
5037 * Get ptr again, because u_save and/or showmatch() will have
5038 * released the line. At the same time we let know that the
5039 * line will be changed.
5040 */
5041 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005042 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5043 {
5044 int c = ins_copychar(curwin->w_cursor.lnum
5045 + (cap->nchar == Ctrl_Y ? -1 : 1));
5046 if (c != NUL)
5047 ptr[curwin->w_cursor.col] = c;
5048 }
5049 else
5050 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (p_sm && msg_silent == 0)
5052 showmatch(cap->nchar);
5053 ++curwin->w_cursor.col;
5054 }
5055#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005056 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005058 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
Bram Moolenaar009b2592004-10-24 19:18:58 +00005060 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005061 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005063 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065#endif
5066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005067 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 changed_bytes(curwin->w_cursor.lnum,
5069 (colnr_T)(curwin->w_cursor.col - cap->count1));
5070 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005071 --curwin->w_cursor.col; // cursor on the last replaced char
5072 // if the character on the left of the current cursor is a multi-byte
5073 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 if (has_mbyte)
5075 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 curbuf->b_op_end = curwin->w_cursor;
5077 curwin->w_set_curswant = TRUE;
5078 set_last_insert(cap->nchar);
5079 }
5080}
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082/*
5083 * 'o': Exchange start and end of Visual area.
5084 * 'O': same, but in block mode exchange left and right corners.
5085 */
5086 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005087v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 pos_T old_cursor;
5090 colnr_T left, right;
5091
5092 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5093 {
5094 old_cursor = curwin->w_cursor;
5095 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5096 curwin->w_cursor.lnum = VIsual.lnum;
5097 coladvance(left);
5098 VIsual = curwin->w_cursor;
5099
5100 curwin->w_cursor.lnum = old_cursor.lnum;
5101 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005102 // 'selection "exclusive" and cursor at right-bottom corner: move it
5103 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5105 ++curwin->w_curswant;
5106 coladvance(curwin->w_curswant);
5107 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005109 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 curwin->w_cursor.lnum = VIsual.lnum;
5112 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5113 ++right;
5114 coladvance(right);
5115 VIsual = curwin->w_cursor;
5116
5117 curwin->w_cursor.lnum = old_cursor.lnum;
5118 coladvance(left);
5119 curwin->w_curswant = left;
5120 }
5121 }
5122 else
5123 {
5124 old_cursor = curwin->w_cursor;
5125 curwin->w_cursor = VIsual;
5126 VIsual = old_cursor;
5127 curwin->w_set_curswant = TRUE;
5128 }
5129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130
5131/*
5132 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5133 */
5134 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005135nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005137 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
5139 cap->cmdchar = 'c';
5140 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005141 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 VIsual_mode = 'V';
5143 nv_operator(cap);
5144 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005145 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005148 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 else
5150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (virtual_active())
5152 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5154 }
5155 }
5156}
5157
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158/*
5159 * "gr".
5160 */
5161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005162nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (VIsual_active)
5165 {
5166 cap->cmdchar = 'r';
5167 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005168 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005170 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
5172 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005173 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 else
5175 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005176 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 cap->extra_char = get_literal();
5178 stuffcharReadbuff(cap->extra_char);
5179 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if (virtual_active())
5181 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 invoke_edit(cap, TRUE, 'v', FALSE);
5183 }
5184 }
5185}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
5187/*
5188 * Swap case for "~" command, when it does not work like an operator.
5189 */
5190 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005191n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192{
5193 long n;
5194 pos_T startpos;
5195 int did_change = 0;
5196#ifdef FEAT_NETBEANS_INTG
5197 pos_T pos;
5198 char_u *ptr;
5199 int count;
5200#endif
5201
5202 if (checkclearopq(cap->oap))
5203 return;
5204
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005205 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
5207 clearopbeep(cap->oap);
5208 return;
5209 }
5210
5211 prep_redo_cmd(cap);
5212
5213 if (u_save_cursor() == FAIL)
5214 return;
5215
5216 startpos = curwin->w_cursor;
5217#ifdef FEAT_NETBEANS_INTG
5218 pos = startpos;
5219#endif
5220 for (n = cap->count1; n > 0; --n)
5221 {
5222 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5223 inc_cursor();
5224 if (gchar_cursor() == NUL)
5225 {
5226 if (vim_strchr(p_ww, '~') != NULL
5227 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5228 {
5229#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005230 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
5232 if (did_change)
5233 {
5234 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005235 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005236 netbeans_removed(curbuf, pos.lnum, pos.col,
5237 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005239 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241 pos.col = 0;
5242 pos.lnum++;
5243 }
5244#endif
5245 ++curwin->w_cursor.lnum;
5246 curwin->w_cursor.col = 0;
5247 if (n > 1)
5248 {
5249 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5250 break;
5251 u_clearline();
5252 }
5253 }
5254 else
5255 break;
5256 }
5257 }
5258#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005259 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
5261 ptr = ml_get(pos.lnum);
5262 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005263 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5264 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266#endif
5267
5268
5269 check_cursor();
5270 curwin->w_set_curswant = TRUE;
5271 if (did_change)
5272 {
5273 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5274 0L);
5275 curbuf->b_op_start = startpos;
5276 curbuf->b_op_end = curwin->w_cursor;
5277 if (curbuf->b_op_end.col > 0)
5278 --curbuf->b_op_end.col;
5279 }
5280}
5281
5282/*
5283 * Move cursor to mark.
5284 */
5285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 if (check_mark(pos) == FAIL)
5289 clearop(cap->oap);
5290 else
5291 {
5292 if (cap->cmdchar == '\''
5293 || cap->cmdchar == '`'
5294 || cap->cmdchar == '['
5295 || cap->cmdchar == ']')
5296 setpcmark();
5297 curwin->w_cursor = *pos;
5298 if (flag)
5299 beginline(BL_WHITE | BL_FIX);
5300 else
5301 check_cursor();
5302 }
5303 cap->oap->motion_type = flag ? MLINE : MCHAR;
5304 if (cap->cmdchar == '`')
5305 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005306 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 curwin->w_set_curswant = TRUE;
5308}
5309
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310/*
5311 * Handle commands that are operators in Visual mode.
5312 */
5313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005314v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
5316 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5317
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005318 // Uppercase means linewise, except in block mode, then "D" deletes till
5319 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 if (isupper(cap->cmdchar))
5321 {
5322 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005323 {
5324 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5328 curwin->w_curswant = MAXCOL;
5329 }
5330 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5331 nv_operator(cap);
5332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
5334/*
5335 * "s" and "S" commands.
5336 */
5337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005338nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005340#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005341 // When showing output of term_dumpdiff() swap the top and botom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005342 if (term_swap_diff() == OK)
5343 return;
5344#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005345#ifdef FEAT_JOB_CHANNEL
5346 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5347 {
5348 clearopbeep(cap->oap);
5349 return;
5350 }
5351#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005352 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
5354 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005355 {
5356 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 cap->cmdchar = 'c';
5360 nv_operator(cap);
5361 }
5362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 nv_optrans(cap);
5364}
5365
5366/*
5367 * Abbreviated commands.
5368 */
5369 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005370nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371{
5372 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005373 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005375 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (VIsual_active)
5377 v_visop(cap);
5378 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 nv_optrans(cap);
5380}
5381
5382/*
5383 * Translate a command into another command.
5384 */
5385 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005386nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
5388 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5389 (char_u *)"d$", (char_u *)"c$",
5390 (char_u *)"cl", (char_u *)"cc",
5391 (char_u *)"yy", (char_u *)":s\r"};
5392 static char_u *str = (char_u *)"xXDCsSY&";
5393
5394 if (!checkclearopq(cap->oap))
5395 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005396 // In Vi "2D" doesn't delete the next line. Can't translate it
5397 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005398 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5399 {
5400 cap->oap->start = curwin->w_cursor;
5401 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005402#ifdef FEAT_EVAL
5403 set_op_var(OP_DELETE);
5404#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005405 cap->count1 = 1;
5406 nv_dollar(cap);
5407 finish_op = TRUE;
5408 ResetRedobuff();
5409 AppendCharToRedobuff('D');
5410 }
5411 else
5412 {
5413 if (cap->count0)
5414 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005415 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 cap->opcount = 0;
5419}
5420
5421/*
5422 * "'" and "`" commands. Also for "g'" and "g`".
5423 * cap->arg is TRUE for "'" and "g'".
5424 */
5425 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005426nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427{
5428 pos_T *pos;
5429 int c;
5430#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005431 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005432 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433#endif
5434
5435 if (cap->cmdchar == 'g')
5436 c = cap->extra_char;
5437 else
5438 c = cap->nchar;
5439 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005440 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
5442 if (cap->arg)
5443 {
5444 check_cursor_lnum();
5445 beginline(BL_WHITE | BL_FIX);
5446 }
5447 else
5448 check_cursor();
5449 }
5450 else
5451 nv_cursormark(cap, cap->arg, pos);
5452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005453 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 if (!virtual_active())
5455 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005456 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457#ifdef FEAT_FOLDING
5458 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005459 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005460 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 && (fdo_flags & FDO_MARK)
5462 && old_KeyTyped)
5463 foldOpenCursor();
5464#endif
5465}
5466
5467/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005468 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 */
5470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005471nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
5473#ifdef FEAT_JUMPLIST
5474 pos_T *pos;
5475# ifdef FEAT_FOLDING
5476 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005477 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478# endif
5479
5480 if (!checkclearopq(cap->oap))
5481 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005482 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5483 {
5484 if (goto_tabpage_lastused() == FAIL)
5485 clearopbeep(cap->oap);
5486 return;
5487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 if (cap->cmdchar == 'g')
5489 pos = movechangelist((int)cap->count1);
5490 else
5491 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005492 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 {
5494 curwin->w_set_curswant = TRUE;
5495 check_cursor();
5496 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005497 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 nv_cursormark(cap, FALSE, pos);
5499 else if (cap->cmdchar == 'g')
5500 {
5501 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005502 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005504 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005506 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 }
5508 else
5509 clearopbeep(cap->oap);
5510# ifdef FEAT_FOLDING
5511 if (cap->oap->op_type == OP_NOP
5512 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5513 && (fdo_flags & FDO_MARK)
5514 && old_KeyTyped)
5515 foldOpenCursor();
5516# endif
5517 }
5518#else
5519 clearopbeep(cap->oap);
5520#endif
5521}
5522
5523/*
5524 * Handle '"' command.
5525 */
5526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005527nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529 if (checkclearop(cap->oap))
5530 return;
5531#ifdef FEAT_EVAL
5532 if (cap->nchar == '=')
5533 cap->nchar = get_expr_register();
5534#endif
5535 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5536 {
5537 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005538 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539#ifdef FEAT_EVAL
5540 set_reg_var(cap->oap->regname);
5541#endif
5542 }
5543 else
5544 clearopbeep(cap->oap);
5545}
5546
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547/*
5548 * Handle "v", "V" and "CTRL-V" commands.
5549 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5550 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005551 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 */
5553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005554nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005556 if (cap->cmdchar == Ctrl_Q)
5557 cap->cmdchar = Ctrl_V;
5558
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005559 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5560 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 if (cap->oap->op_type != OP_NOP)
5562 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005563 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005564 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 return;
5566 }
5567
5568 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005569 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005571 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005573 else // toggle char/block mode
5574 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 VIsual_mode = cap->cmdchar;
5576 showmode();
5577 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005578 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005580 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
5582 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005583 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005585 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 VIsual = curwin->w_cursor;
5587
5588 VIsual_active = TRUE;
5589 VIsual_reselect = TRUE;
5590 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005591 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005594 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005595 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 /*
5597 * For V and ^V, we multiply the number of lines even if there
5598 * was only one -- webb
5599 */
5600 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5601 {
5602 curwin->w_cursor.lnum +=
5603 resel_VIsual_line_count * cap->count0 - 1;
5604 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5605 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5606 }
5607 VIsual_mode = resel_VIsual_mode;
5608 if (VIsual_mode == 'v')
5609 {
5610 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005611 {
5612 validate_virtcol();
5613 curwin->w_curswant = curwin->w_virtcol
5614 + resel_VIsual_vcol * cap->count0 - 1;
5615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005617 curwin->w_curswant = resel_VIsual_vcol;
5618 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005620 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 curwin->w_curswant = MAXCOL;
5623 coladvance((colnr_T)MAXCOL);
5624 }
5625 else if (VIsual_mode == Ctrl_V)
5626 {
5627 validate_virtcol();
5628 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005629 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 coladvance(curwin->w_curswant);
5631 }
5632 else
5633 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005634 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 }
5636 else
5637 {
5638 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005639 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 may_start_select('c');
5641 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005642 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005643 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005644 if (cap->count0 > 0 && --cap->count1 > 0)
5645 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005646 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005647 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5648 nv_right(cap);
5649 else if (VIsual_mode == 'V')
5650 nv_down(cap);
5651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 }
5653 }
5654}
5655
5656/*
5657 * Start selection for Shift-movement keys.
5658 */
5659 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005660start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005662 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 may_start_select('k');
5664 n_start_visual_mode('v');
5665}
5666
5667/*
5668 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5669 */
5670 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005671may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 VIsual_select = (stuff_empty() && typebuf_typed()
5674 && (vim_strchr(p_slm, c) != NULL));
5675}
5676
5677/*
5678 * Start Visual mode "c".
5679 * Should set VIsual_select before calling this.
5680 */
5681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005682n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005684#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005685 // Check for redraw before changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005686 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005687#endif
5688
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 VIsual_mode = c;
5690 VIsual_active = TRUE;
5691 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005692
5693 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005694 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005696 {
5697 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 VIsual = curwin->w_cursor;
5701
5702#ifdef FEAT_FOLDING
5703 foldAdjustVisual();
5704#endif
5705
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005707#ifdef FEAT_CONCEAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005708 // Check for redraw after changing the state.
Bram Moolenaarb9464822018-05-10 15:09:49 +02005709 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005710#endif
5711
Bram Moolenaar09df3122006-01-23 22:23:09 +00005712 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005713 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005715 // Make sure the clipboard gets updated. Needed because start and
5716 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 clip_star.vmode = NUL;
5718#endif
5719
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005720 // Only need to redraw this line, unless still need to redraw an old
5721 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 if (curwin->w_redr_type < INVERTED)
5723 {
5724 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5725 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5726 }
5727}
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
5730/*
5731 * CTRL-W: Window commands
5732 */
5733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005734nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005736 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005737 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005738 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005739 cap->cmdchar = ':';
5740 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005741 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005742 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005743 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005744 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745}
5746
5747/*
5748 * CTRL-Z: Suspend
5749 */
5750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005751nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
5753 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005755 end_visual_mode(); // stop Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 do_cmdline_cmd((char_u *)"st");
5757}
5758
5759/*
5760 * Commands starting with "g".
5761 */
5762 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005763nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 int i;
5768 int flag = FALSE;
5769
5770 switch (cap->nchar)
5771 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005772 case Ctrl_A:
5773 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774#ifdef MEM_PROFILE
5775 /*
5776 * "g^A": dump log of used memory.
5777 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005778 if (!VIsual_active && cap->nchar == Ctrl_A)
5779 vim_mem_profile_dump();
5780 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005782 /*
5783 * "g^A/g^X": sequentially increment visually selected region
5784 */
5785 if (VIsual_active)
5786 {
5787 cap->arg = TRUE;
5788 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005789 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005790 nv_addsub(cap);
5791 }
5792 else
5793 clearopbeep(oap);
5794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 /*
5797 * "gR": Enter virtual replace mode.
5798 */
5799 case 'R':
5800 cap->arg = TRUE;
5801 nv_Replace(cap);
5802 break;
5803
5804 case 'r':
5805 nv_vreplace(cap);
5806 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807
5808 case '&':
5809 do_cmdline_cmd((char_u *)"%s//~/&");
5810 break;
5811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 /*
5813 * "gv": Reselect the previous Visual area. If Visual already active,
5814 * exchange previous and current Visual area.
5815 */
5816 case 'v':
5817 if (checkclearop(oap))
5818 break;
5819
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005820 if ( curbuf->b_visual.vi_start.lnum == 0
5821 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5822 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 beep_flush();
5824 else
5825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005826 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 if (VIsual_active)
5828 {
5829 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005830 VIsual_mode = curbuf->b_visual.vi_mode;
5831 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832# ifdef FEAT_EVAL
5833 curbuf->b_visual_mode_eval = i;
5834# endif
5835 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005836 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5837 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005839 tpos = curbuf->b_visual.vi_end;
5840 curbuf->b_visual.vi_end = curwin->w_cursor;
5841 curwin->w_cursor = curbuf->b_visual.vi_start;
5842 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
5844 else
5845 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005846 VIsual_mode = curbuf->b_visual.vi_mode;
5847 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5848 tpos = curbuf->b_visual.vi_end;
5849 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
5851
5852 VIsual_active = TRUE;
5853 VIsual_reselect = TRUE;
5854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005855 // Set Visual to the start and w_cursor to the end of the Visual
5856 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 check_cursor();
5858 VIsual = curwin->w_cursor;
5859 curwin->w_cursor = tpos;
5860 check_cursor();
5861 update_topline();
5862 /*
5863 * When called from normal "g" command: start Select mode when
5864 * 'selectmode' contains "cmd". When called for K_SELECT, always
5865 * start Select mode.
5866 */
5867 if (cap->arg)
5868 VIsual_select = TRUE;
5869 else
5870 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005873 // Make sure the clipboard gets updated. Needed because start and
5874 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 clip_star.vmode = NUL;
5876#endif
5877 redraw_curbuf_later(INVERTED);
5878 showmode();
5879 }
5880 break;
5881 /*
5882 * "gV": Don't reselect the previous Visual area after a Select mode
5883 * mapping of menu.
5884 */
5885 case 'V':
5886 VIsual_reselect = FALSE;
5887 break;
5888
5889 /*
5890 * "gh": start Select mode.
5891 * "gH": start Select line mode.
5892 * "g^H": start Select block mode.
5893 */
5894 case K_BS:
5895 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005896 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 case 'h':
5898 case 'H':
5899 case Ctrl_H:
5900# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005901 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (cap->nchar == Ctrl_H)
5903 cap->cmdchar = Ctrl_V;
5904 else
5905# endif
5906 cap->cmdchar = cap->nchar + ('v' - 'h');
5907 cap->arg = TRUE;
5908 nv_visual(cap);
5909 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005910
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005911 // "gn", "gN" visually select next/previous search match
5912 // "gn" selects next match
5913 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005914 case 'N':
5915 case 'n':
5916 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005917 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
5920 /*
5921 * "gj" and "gk" two new funny movement keys -- up and down
5922 * movement based on *screen* line rather than *file* line.
5923 */
5924 case 'j':
5925 case K_DOWN:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005926 // with 'nowrap' it works just like the normal "j" command; also when
5927 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 if (!curwin->w_p_wrap
5929#ifdef FEAT_FOLDING
5930 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5931#endif
5932 )
5933 {
5934 oap->motion_type = MLINE;
5935 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5936 }
5937 else
5938 i = nv_screengo(oap, FORWARD, cap->count1);
5939 if (i == FAIL)
5940 clearopbeep(oap);
5941 break;
5942
5943 case 'k':
5944 case K_UP:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005945 // with 'nowrap' it works just like the normal "k" command; also when
5946 // in a closed fold
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 if (!curwin->w_p_wrap
5948#ifdef FEAT_FOLDING
5949 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
5950#endif
5951 )
5952 {
5953 oap->motion_type = MLINE;
5954 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
5955 }
5956 else
5957 i = nv_screengo(oap, BACKWARD, cap->count1);
5958 if (i == FAIL)
5959 clearopbeep(oap);
5960 break;
5961
5962 /*
5963 * "gJ": join two lines without inserting a space.
5964 */
5965 case 'J':
5966 nv_join(cap);
5967 break;
5968
5969 /*
5970 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
5971 * "gm": middle of "g0" and "g$".
5972 */
5973 case '^':
5974 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005975 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
5977 case '0':
5978 case 'm':
5979 case K_HOME:
5980 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 oap->motion_type = MCHAR;
5982 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005983 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005985 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 int width2 = width1 + curwin_col_off2();
5987
5988 validate_virtcol();
5989 i = 0;
5990 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5991 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5992 }
5993 else
5994 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005995 // Go to the middle of the screen line. When 'number' or
5996 // 'relativenumber' is on and lines are wrapping the middle can be more
5997 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02005999 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 + ((curwin->w_p_wrap && i > 0)
6001 ? curwin_col_off2() : 0)) / 2;
6002 coladvance((colnr_T)i);
6003 if (flag)
6004 {
6005 do
6006 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006007 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006008 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 }
6010 curwin->w_set_curswant = TRUE;
6011 break;
6012
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006013 case 'M':
6014 {
6015 char_u *ptr = ml_get_curline();
6016
6017 oap->motion_type = MCHAR;
6018 oap->inclusive = FALSE;
6019 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006020 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006021 else
6022 i = (int)STRLEN(ptr);
6023 if (cap->count0 > 0 && cap->count0 <= 100)
6024 coladvance((colnr_T)(i * cap->count0 / 100));
6025 else
6026 coladvance((colnr_T)(i / 2));
6027 curwin->w_set_curswant = TRUE;
6028 }
6029 break;
6030
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006032 // "g_": to the last non-blank character in the line or <count> lines
6033 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 cap->oap->motion_type = MCHAR;
6035 cap->oap->inclusive = TRUE;
6036 curwin->w_curswant = MAXCOL;
6037 if (cursor_down((long)(cap->count1 - 1),
6038 cap->oap->op_type == OP_NOP) == FAIL)
6039 clearopbeep(cap->oap);
6040 else
6041 {
6042 char_u *ptr = ml_get_curline();
6043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006044 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6046 --curwin->w_cursor.col;
6047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006048 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006050 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 --curwin->w_cursor.col;
6052 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006053 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 }
6055 break;
6056
6057 case '$':
6058 case K_END:
6059 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 {
6061 int col_off = curwin_col_off();
6062
6063 oap->motion_type = MCHAR;
6064 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006065 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006067 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (cap->count1 == 1)
6069 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006070 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 int width2 = width1 + curwin_col_off2();
6072
6073 validate_virtcol();
6074 i = width1 - 1;
6075 if (curwin->w_virtcol >= (colnr_T)width1)
6076 i += ((curwin->w_virtcol - width1) / width2 + 1)
6077 * width2;
6078 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006080 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006081 validate_virtcol();
6082 curwin->w_curswant = curwin->w_virtcol;
6083 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6085 {
6086 /*
6087 * Check for landing on a character that got split at
6088 * the end of the line. We do not want to advance to
6089 * the next screen line.
6090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (curwin->w_virtcol > (colnr_T)i)
6092 --curwin->w_cursor.col;
6093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 }
6095 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6096 clearopbeep(oap);
6097 }
6098 else
6099 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006100 if (cap->count1 > 1)
6101 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006102 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006103
Bram Moolenaar02631462017-09-22 15:20:32 +02006104 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006106
Bram Moolenaard5c82342019-07-27 18:44:57 +02006107 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006108 validate_virtcol();
6109 curwin->w_curswant = curwin->w_virtcol;
6110 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 }
6112 }
6113 break;
6114
6115 /*
6116 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6117 */
6118 case '*':
6119 case '#':
6120#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006121 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006123 case Ctrl_RSB: // :tag or :tselect for current identifier
6124 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 nv_ident(cap);
6126 break;
6127
6128 /*
6129 * ge and gE: go back to end of word
6130 */
6131 case 'e':
6132 case 'E':
6133 oap->motion_type = MCHAR;
6134 curwin->w_set_curswant = TRUE;
6135 oap->inclusive = TRUE;
6136 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6137 clearopbeep(oap);
6138 break;
6139
6140 /*
6141 * "g CTRL-G": display info about cursor position
6142 */
6143 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006144 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 break;
6146
6147 /*
6148 * "gi": start Insert at the last position.
6149 */
6150 case 'i':
6151 if (curbuf->b_last_insert.lnum != 0)
6152 {
6153 curwin->w_cursor = curbuf->b_last_insert;
6154 check_cursor_lnum();
6155 i = (int)STRLEN(ml_get_curline());
6156 if (curwin->w_cursor.col > (colnr_T)i)
6157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (virtual_active())
6159 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 curwin->w_cursor.col = i;
6161 }
6162 }
6163 cap->cmdchar = 'i';
6164 nv_edit(cap);
6165 break;
6166
6167 /*
6168 * "gI": Start insert in column 1.
6169 */
6170 case 'I':
6171 beginline(0);
6172 if (!checkclearopq(oap))
6173 invoke_edit(cap, FALSE, 'g', FALSE);
6174 break;
6175
6176#ifdef FEAT_SEARCHPATH
6177 /*
6178 * "gf": goto file, edit file under cursor
6179 * "]f" and "[f": can also be used.
6180 */
6181 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006182 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 nv_gotofile(cap);
6184 break;
6185#endif
6186
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006187 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 case '\'':
6189 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006190 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 case '`':
6192 nv_gomark(cap);
6193 break;
6194
6195 /*
6196 * "gs": Goto sleep.
6197 */
6198 case 's':
6199 do_sleep(cap->count1 * 1000L);
6200 break;
6201
6202 /*
6203 * "ga": Display the ascii value of the character under the
6204 * cursor. It is displayed in decimal, hex, and octal. -- webb
6205 */
6206 case 'a':
6207 do_ascii(NULL);
6208 break;
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 /*
6211 * "g8": Display the bytes used for the UTF-8 character under the
6212 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006213 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 */
6215 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006216 if (cap->count0 == 8)
6217 utf_find_illegal();
6218 else
6219 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006222 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006223 case '<':
6224 show_sb_text();
6225 break;
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 /*
6228 * "gg": Goto the first line in file. With a count it goes to
6229 * that line number like for "G". -- webb
6230 */
6231 case 'g':
6232 cap->arg = FALSE;
6233 nv_goto(cap);
6234 break;
6235
6236 /*
6237 * Two-character operators:
6238 * "gq" Format text
6239 * "gw" Format text and keep cursor position
6240 * "g~" Toggle the case of the text.
6241 * "gu" Change text to lower case.
6242 * "gU" Change text to upper case.
6243 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006244 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 */
6246 case 'q':
6247 case 'w':
6248 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006249 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 case '~':
6251 case 'u':
6252 case 'U':
6253 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006254 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 nv_operator(cap);
6256 break;
6257
6258 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006259 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 * current function
6261 * "gD": idem, but in the current file.
6262 */
6263 case 'd':
6264 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006265 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 break;
6267
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 /*
6269 * g<*Mouse> : <C-*mouse>
6270 */
6271 case K_MIDDLEMOUSE:
6272 case K_MIDDLEDRAG:
6273 case K_MIDDLERELEASE:
6274 case K_LEFTMOUSE:
6275 case K_LEFTDRAG:
6276 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006277 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 case K_RIGHTMOUSE:
6279 case K_RIGHTDRAG:
6280 case K_RIGHTRELEASE:
6281 case K_X1MOUSE:
6282 case K_X1DRAG:
6283 case K_X1RELEASE:
6284 case K_X2MOUSE:
6285 case K_X2DRAG:
6286 case K_X2RELEASE:
6287 mod_mask = MOD_MASK_CTRL;
6288 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6289 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290
6291 case K_IGNORE:
6292 break;
6293
6294 /*
6295 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6296 */
6297 case 'p':
6298 case 'P':
6299 nv_put(cap);
6300 break;
6301
6302#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006303 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 case 'o':
6305 goto_byte(cap->count0);
6306 break;
6307#endif
6308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006309 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006311 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 {
6313 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006314 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 break;
6316 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006317
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 if (!checkclearopq(oap))
6319 do_exmode(TRUE);
6320 break;
6321
6322#ifdef FEAT_JUMPLIST
6323 case ',':
6324 nv_pcmark(cap);
6325 break;
6326
6327 case ';':
6328 cap->count1 = -cap->count1;
6329 nv_pcmark(cap);
6330 break;
6331#endif
6332
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006333 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006334 if (!checkclearop(oap))
6335 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006336 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006337 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006338 if (!checkclearop(oap))
6339 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006340 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006341
Bram Moolenaar62a23252020-08-09 14:04:42 +02006342 case TAB:
6343 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6344 clearopbeep(oap);
6345 break;
6346
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006347 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006348 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006349 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006350 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006351 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006352 break;
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 default:
6355 clearopbeep(oap);
6356 break;
6357 }
6358}
6359
6360/*
6361 * Handle "o" and "O" commands.
6362 */
6363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006364n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006366#ifdef FEAT_CONCEAL
6367 linenr_T oldline = curwin->w_cursor.lnum;
6368#endif
6369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (!checkclearopq(cap->oap))
6371 {
6372#ifdef FEAT_FOLDING
6373 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006374 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 (void)hasFolding(curwin->w_cursor.lnum,
6376 &curwin->w_cursor.lnum, NULL);
6377 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006378 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 (void)hasFolding(curwin->w_cursor.lnum,
6380 NULL, &curwin->w_cursor.lnum);
6381#endif
6382 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6383 (cap->cmdchar == 'O' ? 1 : 0)),
6384 (linenr_T)(curwin->w_cursor.lnum +
6385 (cap->cmdchar == 'o' ? 1 : 0))
6386 ) == OK
6387 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006388 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6389 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006391#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006392 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006393 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006394#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006395#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006396 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006397 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006398 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006399#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006400 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006401 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6402 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6404 }
6405 }
6406}
6407
6408/*
6409 * "." command: redo last change.
6410 */
6411 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006412nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 if (!checkclearopq(cap->oap))
6415 {
6416 /*
6417 * If "restart_edit" is TRUE, the last but one command is repeated
6418 * instead of the last command (inserting text). This is used for
6419 * CTRL-O <.> in insert mode.
6420 */
6421 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6422 clearopbeep(cap->oap);
6423 }
6424}
6425
6426/*
6427 * CTRL-R: undo undo
6428 */
6429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006430nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431{
6432 if (!checkclearopq(cap->oap))
6433 {
6434 u_redo((int)cap->count1);
6435 curwin->w_set_curswant = TRUE;
6436 }
6437}
6438
6439/*
6440 * Handle "U" command.
6441 */
6442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006443nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006445 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006446 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006448 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 cap->cmdchar = 'g';
6450 cap->nchar = 'U';
6451 nv_operator(cap);
6452 }
6453 else if (!checkclearopq(cap->oap))
6454 {
6455 u_undoline();
6456 curwin->w_set_curswant = TRUE;
6457 }
6458}
6459
6460/*
6461 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6462 * single character.
6463 */
6464 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006465nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006467 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006468 {
6469#ifdef FEAT_JOB_CHANNEL
6470 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6471 {
6472 clearopbeep(cap->oap);
6473 return;
6474 }
6475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 else
6479 nv_operator(cap);
6480}
6481
6482/*
6483 * Handle an operator command.
6484 * The actual work is done by do_pending_operator().
6485 */
6486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006487nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488{
6489 int op_type;
6490
6491 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006492#ifdef FEAT_JOB_CHANNEL
6493 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6494 {
6495 clearopbeep(cap->oap);
6496 return;
6497 }
6498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006500 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 nv_lineop(cap);
6502 else if (!checkclearop(cap->oap))
6503 {
6504 cap->oap->start = curwin->w_cursor;
6505 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006506#ifdef FEAT_EVAL
6507 set_op_var(op_type);
6508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 }
6510}
6511
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006512#ifdef FEAT_EVAL
6513/*
6514 * Set v:operator to the characters for "optype".
6515 */
6516 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006517set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006518{
6519 char_u opchars[3];
6520
6521 if (optype == OP_NOP)
6522 set_vim_var_string(VV_OP, NULL, 0);
6523 else
6524 {
6525 opchars[0] = get_op_char(optype);
6526 opchars[1] = get_extra_op_char(optype);
6527 opchars[2] = NUL;
6528 set_vim_var_string(VV_OP, opchars, -1);
6529 }
6530}
6531#endif
6532
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533/*
6534 * Handle linewise operator "dd", "yy", etc.
6535 *
6536 * "_" is is a strange motion command that helps make operators more logical.
6537 * It is actually implemented, but not documented in the real Vi. This motion
6538 * command actually refers to "the current line". Commands like "dd" and "yy"
6539 * are really an alternate form of "d_" and "y_". It does accept a count, so
6540 * "d3_" works to delete 3 lines.
6541 */
6542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006543nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
6545 cap->oap->motion_type = MLINE;
6546 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6547 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006548 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006549 && cap->oap->motion_force != 'v'
6550 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 || cap->oap->op_type == OP_LSHIFT
6552 || cap->oap->op_type == OP_RSHIFT)
6553 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006554 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 beginline(BL_WHITE | BL_FIX);
6556}
6557
6558/*
6559 * <Home> command.
6560 */
6561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006562nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006564 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006565 if (mod_mask & MOD_MASK_CTRL)
6566 nv_goto(cap);
6567 else
6568 {
6569 cap->count0 = 1;
6570 nv_pipe(cap);
6571 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006572 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6573 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574}
6575
6576/*
6577 * "|" command.
6578 */
6579 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006580nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
6582 cap->oap->motion_type = MCHAR;
6583 cap->oap->inclusive = FALSE;
6584 beginline(0);
6585 if (cap->count0 > 0)
6586 {
6587 coladvance((colnr_T)(cap->count0 - 1));
6588 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6589 }
6590 else
6591 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006592 // keep curswant at the column where we wanted to go, not where
6593 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 curwin->w_set_curswant = FALSE;
6595}
6596
6597/*
6598 * Handle back-word command "b" and "B".
6599 * cap->arg is 1 for "B"
6600 */
6601 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006602nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 cap->oap->motion_type = MCHAR;
6605 cap->oap->inclusive = FALSE;
6606 curwin->w_set_curswant = TRUE;
6607 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6608 clearopbeep(cap->oap);
6609#ifdef FEAT_FOLDING
6610 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6611 foldOpenCursor();
6612#endif
6613}
6614
6615/*
6616 * Handle word motion commands "e", "E", "w" and "W".
6617 * cap->arg is TRUE for "E" and "W".
6618 */
6619 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006620nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 int n;
6623 int word_end;
6624 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006625 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627 /*
6628 * Set inclusive for the "E" and "e" command.
6629 */
6630 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6631 word_end = TRUE;
6632 else
6633 word_end = FALSE;
6634 cap->oap->inclusive = word_end;
6635
6636 /*
6637 * "cw" and "cW" are a special case.
6638 */
6639 if (!word_end && cap->oap->op_type == OP_CHANGE)
6640 {
6641 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006642 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006644 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
6646 /*
6647 * Reproduce a funny Vi behaviour: "cw" on a blank only
6648 * changes one character, not all blanks until the start of
6649 * the next word. Only do this when the 'w' flag is included
6650 * in 'cpoptions'.
6651 */
6652 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6653 {
6654 cap->oap->inclusive = TRUE;
6655 cap->oap->motion_type = MCHAR;
6656 return;
6657 }
6658 }
6659 else
6660 {
6661 /*
6662 * This is a little strange. To match what the real Vi does,
6663 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6664 * that we are not on a space or a TAB. This seems impolite
6665 * at first, but it's really more what we mean when we say
6666 * 'cw'.
6667 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006668 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 * will change only one character! This is done by setting
6670 * flag.
6671 */
6672 cap->oap->inclusive = TRUE;
6673 word_end = TRUE;
6674 flag = TRUE;
6675 }
6676 }
6677 }
6678
6679 cap->oap->motion_type = MCHAR;
6680 curwin->w_set_curswant = TRUE;
6681 if (word_end)
6682 n = end_word(cap->count1, cap->arg, flag, FALSE);
6683 else
6684 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6685
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006686 // Don't leave the cursor on the NUL past the end of line. Unless we
6687 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006688 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006689 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691 if (n == FAIL && cap->oap->op_type == OP_NOP)
6692 clearopbeep(cap->oap);
6693 else
6694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696#ifdef FEAT_FOLDING
6697 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6698 foldOpenCursor();
6699#endif
6700 }
6701}
6702
6703/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006704 * Used after a movement command: If the cursor ends up on the NUL after the
6705 * end of the line, may move it back to the last character and make the motion
6706 * inclusive.
6707 */
6708 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006709adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006710{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006711 // The cursor cannot remain on the NUL when:
6712 // - the column is > 0
6713 // - not in Visual mode or 'selection' is "o"
6714 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006715 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006716 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006717 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006718 {
6719 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006720 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006721 if (has_mbyte)
6722 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006723 oap->inclusive = TRUE;
6724 }
6725}
6726
6727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 * "0" and "^" commands.
6729 * cap->arg is the argument for beginline().
6730 */
6731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006732nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 cap->oap->motion_type = MCHAR;
6735 cap->oap->inclusive = FALSE;
6736 beginline(cap->arg);
6737#ifdef FEAT_FOLDING
6738 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6739 foldOpenCursor();
6740#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006741 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6742 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743}
6744
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745/*
6746 * In exclusive Visual mode, may include the last character.
6747 */
6748 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006749adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006752 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 if (has_mbyte)
6755 inc_cursor();
6756 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 ++curwin->w_cursor.col;
6758 cap->oap->inclusive = FALSE;
6759 }
6760}
6761
6762/*
6763 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6764 * Should check VIsual_mode before calling this.
6765 * Returns TRUE when backed up to the previous line.
6766 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006767 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006768unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
6770 pos_T *pp;
6771
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006772 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006774 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 pp = &curwin->w_cursor;
6776 else
6777 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (pp->coladd > 0)
6779 --pp->coladd;
6780 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 if (pp->col > 0)
6782 {
6783 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006784 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 }
6786 else if (pp->lnum > 1)
6787 {
6788 --pp->lnum;
6789 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6790 return TRUE;
6791 }
6792 }
6793 return FALSE;
6794}
6795
6796/*
6797 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6798 */
6799 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006800nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
6802 if (VIsual_active)
6803 VIsual_select = TRUE;
6804 else if (VIsual_reselect)
6805 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006806 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 cap->arg = TRUE;
6808 nv_g_cmd(cap);
6809 }
6810}
6811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
6813/*
6814 * "G", "gg", CTRL-END, CTRL-HOME.
6815 * cap->arg is TRUE for "G".
6816 */
6817 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006818nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
6820 linenr_T lnum;
6821
6822 if (cap->arg)
6823 lnum = curbuf->b_ml.ml_line_count;
6824 else
6825 lnum = 1L;
6826 cap->oap->motion_type = MLINE;
6827 setpcmark();
6828
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006829 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 if (cap->count0 != 0)
6831 lnum = cap->count0;
6832 if (lnum < 1L)
6833 lnum = 1L;
6834 else if (lnum > curbuf->b_ml.ml_line_count)
6835 lnum = curbuf->b_ml.ml_line_count;
6836 curwin->w_cursor.lnum = lnum;
6837 beginline(BL_SOL | BL_FIX);
6838#ifdef FEAT_FOLDING
6839 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6840 foldOpenCursor();
6841#endif
6842}
6843
6844/*
6845 * CTRL-\ in Normal mode.
6846 */
6847 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006848nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6851 {
6852 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006853 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006854 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 restart_edit = 0;
6856#ifdef FEAT_CMDWIN
6857 if (cmdwin_type != 0)
6858 cmdwin_result = Ctrl_C;
6859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 if (VIsual_active)
6861 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006862 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 redraw_curbuf_later(INVERTED);
6864 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006865 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 if (cap->nchar == Ctrl_G && p_im)
6867 restart_edit = 'a';
6868 }
6869 else
6870 clearopbeep(cap->oap);
6871}
6872
6873/*
6874 * ESC in Normal mode: beep, but don't flush buffers.
6875 * Don't even beep if we are canceling a command.
6876 */
6877 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006878nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
6880 int no_reason;
6881
6882 no_reason = (cap->oap->op_type == OP_NOP
6883 && cap->opcount == 0
6884 && cap->count0 == 0
6885 && cap->oap->regname == 0
6886 && !p_im);
6887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006888 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 {
6890 if (restart_edit == 0
6891#ifdef FEAT_CMDWIN
6892 && cmdwin_type == 0
6893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006896 {
6897 if (anyBufIsChanged())
6898 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6899 else
6900 msg(_("Type :qa and press <Enter> to exit Vim"));
6901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006903 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6904 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 if (!p_im)
6906 restart_edit = 0;
6907#ifdef FEAT_CMDWIN
6908 if (cmdwin_type != 0)
6909 {
6910 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006911 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 return;
6913 }
6914#endif
6915 }
6916
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 if (VIsual_active)
6918 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006919 end_visual_mode(); // stop Visual
6920 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 curwin->w_set_curswant = TRUE;
6922 redraw_curbuf_later(INVERTED);
6923 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006924 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02006925 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 clearop(cap->oap);
6927
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006928 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
6929 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01006930 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 restart_edit = 'a';
6932}
6933
6934/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006935 * Move the cursor for the "A" command.
6936 */
6937 void
6938set_cursor_for_append_to_line(void)
6939{
6940 curwin->w_set_curswant = TRUE;
6941 if (ve_flags == VE_ALL)
6942 {
6943 int save_State = State;
6944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006945 // Pretend Insert mode here to allow the cursor on the
6946 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02006947 State = INSERT;
6948 coladvance((colnr_T)MAXCOL);
6949 State = save_State;
6950 }
6951 else
6952 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
6953}
6954
6955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01006957 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 */
6959 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006960nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006962 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
6964 cap->cmdchar = 'i';
6965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006966 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02006968 {
6969#ifdef FEAT_TERMINAL
6970 if (term_in_normal_mode())
6971 {
6972 end_visual_mode();
6973 clearop(cap->oap);
6974 term_enter_job_mode();
6975 return;
6976 }
6977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02006979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006981 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006982 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
6983 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {
6985#ifdef FEAT_TEXTOBJ
6986 nv_object(cap);
6987#else
6988 clearopbeep(cap->oap);
6989#endif
6990 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02006991#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02006992 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02006993 {
6994 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02006995 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02006996 return;
6997 }
6998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 else if (!curbuf->b_p_ma && !p_im)
7000 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 // Only give this error when 'insertmode' is off.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007002 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007004 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007005 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007006 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007008 else if (cap->cmdchar == K_PS && VIsual_active)
7009 {
7010 pos_T old_pos = curwin->w_cursor;
7011 pos_T old_visual = VIsual;
7012
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007013 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007014 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7015 {
7016 shift_delete_registers();
7017 cap->oap->regname = '1';
7018 }
7019 else
7020 cap->oap->regname = '-';
7021 cap->cmdchar = 'd';
7022 cap->nchar = NUL;
7023 nv_operator(cap);
7024 do_pending_operator(cap, 0, FALSE);
7025 cap->cmdchar = K_PS;
7026
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007027 // When the last char in the line was deleted then append. Detect this
7028 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007029 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7030 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007031 inc_cursor();
7032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007033 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007034 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 else if (!checkclearopq(cap->oap))
7037 {
7038 switch (cap->cmdchar)
7039 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007040 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007041 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 break;
7043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007044 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007045 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7046 beginline(BL_WHITE);
7047 else
7048 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 break;
7050
Bram Moolenaara1891842017-02-04 21:34:31 +01007051 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007052 // Bracketed paste works like "a"ppend, unless the cursor is in
7053 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007054 if (curwin->w_cursor.col == 0)
7055 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007056 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007058 case 'a': // "a"ppend is like "i"nsert on the next character.
7059 // increment coladd when in virtual space, increment the
7060 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 if (virtual_active()
7062 && (curwin->w_cursor.coladd > 0
7063 || *ml_get_cursor() == NUL
7064 || *ml_get_cursor() == TAB))
7065 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007066 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 inc_cursor();
7068 break;
7069 }
7070
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7072 {
7073 int save_State = State;
7074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007075 // Pretend Insert mode here to allow the cursor on the
7076 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 State = INSERT;
7078 coladvance(getviscol());
7079 State = save_State;
7080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
7082 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7083 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007084 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007085 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007086 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087}
7088
7089/*
7090 * Invoke edit() and take care of "restart_edit" and the return value.
7091 */
7092 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007093invoke_edit(
7094 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007095 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007096 int cmd,
7097 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098{
7099 int restart_edit_save = 0;
7100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007101 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7102 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7103 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 if (repl || !stuff_empty())
7105 restart_edit_save = restart_edit;
7106 else
7107 restart_edit_save = 0;
7108
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007109 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 restart_edit = 0;
7111
7112 if (edit(cmd, startln, cap->count1))
7113 cap->retval |= CA_COMMAND_BUSY;
7114
7115 if (restart_edit == 0)
7116 restart_edit = restart_edit_save;
7117}
7118
7119#ifdef FEAT_TEXTOBJ
7120/*
7121 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7122 */
7123 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007124nv_object(
7125 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
7127 int flag;
7128 int include;
7129 char_u *mps_save;
7130
7131 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007132 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007134 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007136 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 mps_save = curbuf->b_p_mps;
7138 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7139
7140 switch (cap->nchar)
7141 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007142 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 flag = current_word(cap->oap, cap->count1, include, FALSE);
7144 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007145 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 flag = current_word(cap->oap, cap->count1, include, TRUE);
7147 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007148 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 case '(':
7150 case ')':
7151 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7152 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007153 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 case '{':
7155 case '}':
7156 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7157 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007158 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 case ']':
7160 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7161 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007162 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 case '>':
7164 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7165 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007166 case 't': // "at" = a tag block (xml and html)
7167 // Do not adjust oap->end in do_pending_operator()
7168 // otherwise there are different results for 'dit'
7169 // (note leading whitespace in last line):
7170 // 1) <b> 2) <b>
7171 // foobar foobar
7172 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007173 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007174 flag = current_tagblock(cap->oap, cap->count1, include);
7175 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 flag = current_par(cap->oap, cap->count1, include, 'p');
7178 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007179 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 flag = current_sent(cap->oap, cap->count1, include);
7181 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007182 case '"': // "a"" = a double quoted string
7183 case '\'': // "a'" = a single quoted string
7184 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007185 flag = current_quote(cap->oap, cap->count1, include,
7186 cap->nchar);
7187 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007188#if 0 // TODO
7189 case 'S': // "aS" = a section
7190 case 'f': // "af" = a filename
7191 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192#endif
7193 default:
7194 flag = FAIL;
7195 break;
7196 }
7197
7198 curbuf->b_p_mps = mps_save;
7199 if (flag == FAIL)
7200 clearopbeep(cap->oap);
7201 adjust_cursor_col();
7202 curwin->w_set_curswant = TRUE;
7203}
7204#endif
7205
7206/*
7207 * "q" command: Start/stop recording.
7208 * "q:", "q/", "q?": edit command-line in command-line window.
7209 */
7210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007211nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 if (cap->oap->op_type == OP_FORMAT)
7214 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007215 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 cap->cmdchar = 'g';
7217 cap->nchar = 'q';
7218 nv_operator(cap);
7219 }
7220 else if (!checkclearop(cap->oap))
7221 {
7222#ifdef FEAT_CMDWIN
7223 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7224 {
7225 stuffcharReadbuff(cap->nchar);
7226 stuffcharReadbuff(K_CMDWIN);
7227 }
7228 else
7229#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007230 // (stop) recording into a named register, unless executing a
7231 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007232 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 clearopbeep(cap->oap);
7234 }
7235}
7236
7237/*
7238 * Handle the "@r" command.
7239 */
7240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007241nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
7243 if (checkclearop(cap->oap))
7244 return;
7245#ifdef FEAT_EVAL
7246 if (cap->nchar == '=')
7247 {
7248 if (get_expr_register() == NUL)
7249 return;
7250 }
7251#endif
7252 while (cap->count1-- && !got_int)
7253 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007254 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
7256 clearopbeep(cap->oap);
7257 break;
7258 }
7259 line_breakcheck();
7260 }
7261}
7262
7263/*
7264 * Handle the CTRL-U and CTRL-D commands.
7265 */
7266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007267nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7270 || (cap->cmdchar == Ctrl_D
7271 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7272 clearopbeep(cap->oap);
7273 else if (!checkclearop(cap->oap))
7274 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7275}
7276
7277/*
7278 * Handle "J" or "gJ" command.
7279 */
7280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007281nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007283 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007285 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 {
7287 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007288 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7290 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007292 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007293 if (cap->count0 <= 2)
7294 {
7295 clearopbeep(cap->oap);
7296 return;
7297 }
7298 cap->count0 = curbuf->b_ml.ml_line_count
7299 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007301
7302 prep_redo(cap->oap->regname, cap->count0,
7303 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7304 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 }
7306}
7307
7308/*
7309 * "P", "gP", "p" and "gp" commands.
7310 */
7311 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007312nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007314 nv_put_opt(cap, FALSE);
7315}
7316
7317/*
7318 * "P", "gP", "p" and "gp" commands.
7319 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7320 */
7321 static void
7322nv_put_opt(cmdarg_T *cap, int fix_indent)
7323{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 int regname = 0;
7325 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007326 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007327 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 int dir;
7329 int flags = 0;
7330
7331 if (cap->oap->op_type != OP_NOP)
7332 {
7333#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007334 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7336 {
7337 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007338 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 }
7340 else
7341#endif
7342 clearopbeep(cap->oap);
7343 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007344#ifdef FEAT_JOB_CHANNEL
7345 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7346 {
7347 clearopbeep(cap->oap);
7348 }
7349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 else
7351 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007352 if (fix_indent)
7353 {
7354 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7355 ? FORWARD : BACKWARD;
7356 flags |= PUT_FIXINDENT;
7357 }
7358 else
7359 dir = (cap->cmdchar == 'P'
7360 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 ? BACKWARD : FORWARD;
7362 prep_redo_cmd(cap);
7363 if (cap->cmdchar == 'g')
7364 flags |= PUT_CURSEND;
7365
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 if (VIsual_active)
7367 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007368 // Putting in Visual mode: The put text replaces the selected
7369 // text. First delete the selected text, then put the new text.
7370 // Need to save and restore the registers that the delete
7371 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007372 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007374#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007376#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007377 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007378 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007379#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
7383 )
7384 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007385 // The delete is going to overwrite the register we want to
7386 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 reg1 = get_register(regname, TRUE);
7388 }
7389
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007390 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 cap->cmdchar = 'd';
7392 cap->nchar = NUL;
7393 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007394 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 nv_operator(cap);
7396 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007397 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007398 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007400 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 cap->oap->regname = regname;
7402
7403 if (reg1 != NULL)
7404 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007405 // Delete probably changed the register we want to put, save
7406 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 reg2 = get_register(regname, FALSE);
7408 put_register(regname, reg1);
7409 }
7410
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007411 // When deleted a linewise Visual area, put the register as
7412 // lines to avoid it joined with the next line. When deletion was
7413 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 if (VIsual_mode == 'V')
7415 flags |= PUT_LINE;
7416 else if (VIsual_mode == 'v')
7417 flags |= PUT_LINE_SPLIT;
7418 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7419 flags |= PUT_LINE_FORWARD;
7420 dir = BACKWARD;
7421 if ((VIsual_mode != 'V'
7422 && curwin->w_cursor.col < curbuf->b_op_start.col)
7423 || (VIsual_mode == 'V'
7424 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007425 // cursor is at the end of the line or end of file, put
7426 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007428 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007429 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007431 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007433 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 if (reg2 != NULL)
7435 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007436
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007437 // What to reselect with "gv"? Selecting the just put text seems to
7438 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007439 if (was_visual)
7440 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007441 curbuf->b_visual.vi_start = curbuf->b_op_start;
7442 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007443 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007444 if (*p_sel == 'e')
7445 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007446 }
7447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007448 // When all lines were selected and deleted do_put() leaves an empty
7449 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007450 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007451 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007452 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007453 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007454
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007455 // If the cursor was in that line, move it to the end of the last
7456 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007457 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7458 {
7459 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7460 coladvance((colnr_T)MAXCOL);
7461 }
7462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 auto_format(FALSE, TRUE);
7464 }
7465}
7466
7467/*
7468 * "o" and "O" commands.
7469 */
7470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007471nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472{
7473#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007474 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7476 {
7477 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007478 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 }
7480 else
7481#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007482 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007484#ifdef FEAT_JOB_CHANNEL
7485 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007486 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 n_opencmd(cap);
7490}
7491
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492#ifdef FEAT_NETBEANS_INTG
7493 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007494nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
7496 netbeans_keycommand(cap->nchar);
7497}
7498#endif
7499
7500#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007502nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007504 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505}
7506#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007507
Bram Moolenaar3918c952005-03-15 22:34:55 +00007508/*
7509 * Trigger CursorHold event.
7510 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7511 * input buffer. "did_cursorhold" is set to avoid retriggering.
7512 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007514nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007515{
7516 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7517 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007518 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007519}