blob: 3a5c8fe60ab46fc16fff55d523947e57e5ddc4a8 [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 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020022static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010025static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#endif
27
28/*
29 * nv_*(): functions called to handle Normal and Visual mode commands.
30 * n_*(): functions called to handle Normal mode commands.
31 * v_*(): functions called to handle Visual mode commands.
32 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010033static void nv_ignore(cmdarg_T *cap);
34static void nv_nop(cmdarg_T *cap);
35static void nv_error(cmdarg_T *cap);
36static void nv_help(cmdarg_T *cap);
37static void nv_addsub(cmdarg_T *cap);
38static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010041static void nv_ver_scrollbar(cmdarg_T *cap);
42static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_tabline(cmdarg_T *cap);
46static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000047#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void nv_exmode(cmdarg_T *cap);
49static void nv_colon(cmdarg_T *cap);
50static void nv_ctrlg(cmdarg_T *cap);
51static void nv_ctrlh(cmdarg_T *cap);
52static void nv_clear(cmdarg_T *cap);
53static void nv_ctrlo(cmdarg_T *cap);
54static void nv_hat(cmdarg_T *cap);
55static void nv_Zet(cmdarg_T *cap);
56static void nv_ident(cmdarg_T *cap);
57static void nv_tagpop(cmdarg_T *cap);
58static void nv_scroll(cmdarg_T *cap);
59static void nv_right(cmdarg_T *cap);
60static void nv_left(cmdarg_T *cap);
61static void nv_up(cmdarg_T *cap);
62static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010063static void nv_end(cmdarg_T *cap);
64static void nv_dollar(cmdarg_T *cap);
65static void nv_search(cmdarg_T *cap);
66static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020067static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010068static void nv_csearch(cmdarg_T *cap);
69static void nv_brackets(cmdarg_T *cap);
70static void nv_percent(cmdarg_T *cap);
71static void nv_brace(cmdarg_T *cap);
72static void nv_mark(cmdarg_T *cap);
73static void nv_findpar(cmdarg_T *cap);
74static void nv_undo(cmdarg_T *cap);
75static void nv_kundo(cmdarg_T *cap);
76static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010077static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
79static void v_visop(cmdarg_T *cap);
80static void nv_subst(cmdarg_T *cap);
81static void nv_abbrev(cmdarg_T *cap);
82static void nv_optrans(cmdarg_T *cap);
83static void nv_gomark(cmdarg_T *cap);
84static void nv_pcmark(cmdarg_T *cap);
85static void nv_regname(cmdarg_T *cap);
86static void nv_visual(cmdarg_T *cap);
87static void n_start_visual_mode(int c);
88static void nv_window(cmdarg_T *cap);
89static void nv_suspend(cmdarg_T *cap);
90static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010091static void nv_dot(cmdarg_T *cap);
Shougo Matsushita4ede01f2022-01-20 15:26:03 +000092static void nv_redo_or_register(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010093static void nv_Undo(cmdarg_T *cap);
94static void nv_tilde(cmdarg_T *cap);
95static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000096#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010097static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000098#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010099static void nv_lineop(cmdarg_T *cap);
100static void nv_home(cmdarg_T *cap);
101static void nv_pipe(cmdarg_T *cap);
102static void nv_bck_word(cmdarg_T *cap);
103static void nv_wordcmd(cmdarg_T *cap);
104static void nv_beginline(cmdarg_T *cap);
105static void adjust_cursor(oparg_T *oap);
106static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100107static void nv_select(cmdarg_T *cap);
108static void nv_goto(cmdarg_T *cap);
109static void nv_normal(cmdarg_T *cap);
110static void nv_esc(cmdarg_T *oap);
111static void nv_edit(cmdarg_T *cap);
112static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100114static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100116static void nv_record(cmdarg_T *cap);
117static void nv_at(cmdarg_T *cap);
118static void nv_halfpage(cmdarg_T *cap);
119static void nv_join(cmdarg_T *cap);
120static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200121static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100127static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100129static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131/*
132 * Function to be called for a Normal or Visual mode command.
133 * The argument is a cmdarg_T.
134 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100135typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100137// Values for cmd_flags.
138#define NV_NCH 0x01 // may need to get a second char
139#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
140#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
141#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100143#define NV_SS 0x10 // may start selection
144#define NV_SSS 0x20 // may start selection with shift modifier
145#define NV_STS 0x40 // may stop selection without shift modif.
146#define NV_RL 0x80 // 'rightleft' modifies command
147#define NV_KEEPREG 0x100 // don't clear regname
148#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150/*
151 * Generally speaking, every Normal mode command should either clear any
152 * pending operator (with *clearop*()), or set the motion type variable
153 * oap->motion_type.
154 *
155 * When a cursor motion command is made, it is marked as being a character or
156 * line oriented motion. Then, if an operator is in effect, the operation
157 * becomes character or line oriented accordingly.
158 */
159
160/*
161 * This table contains one entry for every Normal or Visual mode command.
162 * The order doesn't matter, init_normal_cmds() will create a sorted index.
163 * It is faster when all keys from zero to '~' are present.
164 */
165static const struct nv_cmd
166{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100167 int cmd_char; // (first) command character
168 nv_func_T cmd_func; // function for this command
169 short_u cmd_flags; // NV_ flags
170 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171} nv_cmds[] =
172{
173 {NUL, nv_error, 0, 0},
174 {Ctrl_A, nv_addsub, 0, 0},
175 {Ctrl_B, nv_page, NV_STS, BACKWARD},
176 {Ctrl_C, nv_esc, 0, TRUE},
177 {Ctrl_D, nv_halfpage, 0, 0},
178 {Ctrl_E, nv_scroll_line, 0, TRUE},
179 {Ctrl_F, nv_page, NV_STS, FORWARD},
180 {Ctrl_G, nv_ctrlg, 0, 0},
181 {Ctrl_H, nv_ctrlh, 0, 0},
182 {Ctrl_I, nv_pcmark, 0, 0},
183 {NL, nv_down, 0, FALSE},
184 {Ctrl_K, nv_error, 0, 0},
185 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100186 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {Ctrl_N, nv_down, NV_STS, FALSE},
188 {Ctrl_O, nv_ctrlo, 0, 0},
189 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000190 {Ctrl_Q, nv_visual, 0, FALSE},
Shougo Matsushita4ede01f2022-01-20 15:26:03 +0000191 {Ctrl_R, nv_redo_or_register, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {Ctrl_S, nv_ignore, 0, 0},
193 {Ctrl_T, nv_tagpop, NV_NCW, 0},
194 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {Ctrl_V, nv_visual, 0, FALSE},
196 {'V', nv_visual, 0, FALSE},
197 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_W, nv_window, 0, 0},
199 {Ctrl_X, nv_addsub, 0, 0},
200 {Ctrl_Y, nv_scroll_line, 0, FALSE},
201 {Ctrl_Z, nv_suspend, 0, 0},
202 {ESC, nv_esc, 0, FALSE},
203 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
204 {Ctrl_RSB, nv_ident, NV_NCW, 0},
205 {Ctrl_HAT, nv_hat, NV_NCW, 0},
206 {Ctrl__, nv_error, 0, 0},
207 {' ', nv_right, 0, 0},
208 {'!', nv_operator, 0, 0},
209 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
210 {'#', nv_ident, 0, 0},
211 {'$', nv_dollar, 0, 0},
212 {'%', nv_percent, 0, 0},
213 {'&', nv_optrans, 0, 0},
214 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
215 {'(', nv_brace, 0, BACKWARD},
216 {')', nv_brace, 0, FORWARD},
217 {'*', nv_ident, 0, 0},
218 {'+', nv_down, 0, TRUE},
219 {',', nv_csearch, 0, TRUE},
220 {'-', nv_up, 0, TRUE},
221 {'.', nv_dot, NV_KEEPREG, 0},
222 {'/', nv_search, 0, FALSE},
223 {'0', nv_beginline, 0, 0},
224 {'1', nv_ignore, 0, 0},
225 {'2', nv_ignore, 0, 0},
226 {'3', nv_ignore, 0, 0},
227 {'4', nv_ignore, 0, 0},
228 {'5', nv_ignore, 0, 0},
229 {'6', nv_ignore, 0, 0},
230 {'7', nv_ignore, 0, 0},
231 {'8', nv_ignore, 0, 0},
232 {'9', nv_ignore, 0, 0},
233 {':', nv_colon, 0, 0},
234 {';', nv_csearch, 0, FALSE},
235 {'<', nv_operator, NV_RL, 0},
236 {'=', nv_operator, 0, 0},
237 {'>', nv_operator, NV_RL, 0},
238 {'?', nv_search, 0, FALSE},
239 {'@', nv_at, NV_NCH_NOP, FALSE},
240 {'A', nv_edit, 0, 0},
241 {'B', nv_bck_word, 0, 1},
242 {'C', nv_abbrev, NV_KEEPREG, 0},
243 {'D', nv_abbrev, NV_KEEPREG, 0},
244 {'E', nv_wordcmd, 0, TRUE},
245 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
246 {'G', nv_goto, 0, TRUE},
247 {'H', nv_scroll, 0, 0},
248 {'I', nv_edit, 0, 0},
249 {'J', nv_join, 0, 0},
250 {'K', nv_ident, 0, 0},
251 {'L', nv_scroll, 0, 0},
252 {'M', nv_scroll, 0, 0},
253 {'N', nv_next, 0, SEARCH_REV},
254 {'O', nv_open, 0, 0},
255 {'P', nv_put, 0, 0},
256 {'Q', nv_exmode, NV_NCW, 0},
257 {'R', nv_Replace, 0, FALSE},
258 {'S', nv_subst, NV_KEEPREG, 0},
259 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
260 {'U', nv_Undo, 0, 0},
261 {'W', nv_wordcmd, 0, TRUE},
262 {'X', nv_abbrev, NV_KEEPREG, 0},
263 {'Y', nv_abbrev, NV_KEEPREG, 0},
264 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
265 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
266 {'\\', nv_error, 0, 0},
267 {']', nv_brackets, NV_NCH_ALW, FORWARD},
268 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
269 {'_', nv_lineop, 0, 0},
270 {'`', nv_gomark, NV_NCH_ALW, FALSE},
271 {'a', nv_edit, NV_NCH, 0},
272 {'b', nv_bck_word, 0, 0},
273 {'c', nv_operator, 0, 0},
274 {'d', nv_operator, 0, 0},
275 {'e', nv_wordcmd, 0, FALSE},
276 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
277 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
278 {'h', nv_left, NV_RL, 0},
279 {'i', nv_edit, NV_NCH, 0},
280 {'j', nv_down, 0, FALSE},
281 {'k', nv_up, 0, FALSE},
282 {'l', nv_right, NV_RL, 0},
283 {'m', nv_mark, NV_NCH_NOP, 0},
284 {'n', nv_next, 0, 0},
285 {'o', nv_open, 0, 0},
286 {'p', nv_put, 0, 0},
287 {'q', nv_record, NV_NCH, 0},
288 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
289 {'s', nv_subst, NV_KEEPREG, 0},
290 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
291 {'u', nv_undo, 0, 0},
292 {'w', nv_wordcmd, 0, FALSE},
293 {'x', nv_abbrev, NV_KEEPREG, 0},
294 {'y', nv_operator, 0, 0},
295 {'z', nv_zet, NV_NCH_ALW, 0},
296 {'{', nv_findpar, 0, BACKWARD},
297 {'|', nv_pipe, 0, 0},
298 {'}', nv_findpar, 0, FORWARD},
299 {'~', nv_tilde, 0, 0},
300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100301 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200303 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
304 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
305 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
306 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {K_LEFTMOUSE, nv_mouse, 0, 0},
308 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
309 {K_LEFTDRAG, nv_mouse, 0, 0},
310 {K_LEFTRELEASE, nv_mouse, 0, 0},
311 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100312 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
314 {K_MIDDLEDRAG, nv_mouse, 0, 0},
315 {K_MIDDLERELEASE, nv_mouse, 0, 0},
316 {K_RIGHTMOUSE, nv_mouse, 0, 0},
317 {K_RIGHTDRAG, nv_mouse, 0, 0},
318 {K_RIGHTRELEASE, nv_mouse, 0, 0},
319 {K_X1MOUSE, nv_mouse, 0, 0},
320 {K_X1DRAG, nv_mouse, 0, 0},
321 {K_X1RELEASE, nv_mouse, 0, 0},
322 {K_X2MOUSE, nv_mouse, 0, 0},
323 {K_X2DRAG, nv_mouse, 0, 0},
324 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000325 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000326 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {K_INS, nv_edit, 0, 0},
328 {K_KINS, nv_edit, 0, 0},
329 {K_BS, nv_ctrlh, 0, 0},
330 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
331 {K_S_UP, nv_page, NV_SS, BACKWARD},
332 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
333 {K_S_DOWN, nv_page, NV_SS, FORWARD},
334 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
335 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
336 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
337 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
339 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
340 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
341 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
342 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
343 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
344 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
345 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {K_S_END, nv_end, NV_SS, FALSE},
347 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
348 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
349 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 {K_S_HOME, nv_home, NV_SS, 0},
351 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
352 {K_DEL, nv_abbrev, 0, 0},
353 {K_KDEL, nv_abbrev, 0, 0},
354 {K_UNDO, nv_kundo, 0, 0},
355 {K_HELP, nv_help, NV_NCW, 0},
356 {K_F1, nv_help, NV_NCW, 0},
357 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef FEAT_GUI
360 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
361 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
362#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000363#ifdef FEAT_GUI_TABLINE
364 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000365 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#ifdef FEAT_NETBEANS_INTG
368 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
369#endif
370#ifdef FEAT_DND
371 {K_DROP, nv_drop, NV_STS, 0},
372#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000373 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100374 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100375 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaare32c3c42022-01-15 18:26:04 +0000376 {K_SCRIPT_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377};
378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100379// Number of commands in nv_cmds[].
K.Takataeeec2542021-06-02 13:28:16 +0200380#define NV_CMDS_SIZE ARRAY_LENGTH(nv_cmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000382#ifndef PROTO // cproto doesn't like this
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];
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387// The highest index for which
388// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static int nv_max_linear;
390
391/*
392 * Compare functions for qsort() below, that checks the command character
393 * through the index in nv_cmd_idx[].
394 */
395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100396nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
398 int c1, c2;
399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100400 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 c1 = nv_cmds[*(const short *)s1].cmd_char;
402 c2 = nv_cmds[*(const short *)s2].cmd_char;
403 if (c1 < 0)
404 c1 = -c1;
405 if (c2 < 0)
406 c2 = -c2;
407 return c1 - c2;
408}
409
410/*
411 * Initialize the nv_cmd_idx[] table.
412 */
413 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100414init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
416 int i;
417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000419 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 nv_cmd_idx[i] = i;
421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100425 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000426 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
428 break;
429 nv_max_linear = i - 1;
430}
431
432/*
433 * Search for a command in the commands table.
434 * Returns -1 for invalid command.
435 */
436 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100437find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 int i;
440 int idx;
441 int top, bot;
442 int c;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 if (cmdchar >= 0x100)
446 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100448 // We use the absolute value of the character. Special keys have a
449 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (cmdchar < 0)
451 cmdchar = -cmdchar;
452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100453 // If the character is in the first part: The character is the index into
454 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (cmdchar <= nv_max_linear)
456 return nv_cmd_idx[cmdchar];
457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100458 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 bot = nv_max_linear + 1;
460 top = NV_CMDS_SIZE - 1;
461 idx = -1;
462 while (bot <= top)
463 {
464 i = (top + bot) / 2;
465 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
466 if (c < 0)
467 c = -c;
468 if (cmdchar == c)
469 {
470 idx = nv_cmd_idx[i];
471 break;
472 }
473 if (cmdchar > c)
474 bot = i + 1;
475 else
476 top = i - 1;
477 }
478 return idx;
479}
480
481/*
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000482 * Handle the count before a normal command and set cap->count0.
483 */
484 static int
485normal_cmd_get_count(
486 cmdarg_T *cap,
487 int c,
488 int toplevel UNUSED,
489 int set_prevcount UNUSED,
490 int *ctrl_w,
491 int *need_flushbuf UNUSED)
492{
493getcount:
494 if (!(VIsual_active && VIsual_select))
495 {
496 // Handle a count before a command and compute ca.count0.
497 // Note that '0' is a command and not the start of a count, but it's
498 // part of a count after other digits.
499 while ((c >= '1' && c <= '9')
500 || (cap->count0 != 0 && (c == K_DEL || c == K_KDEL
501 || c == '0')))
502 {
503 if (c == K_DEL || c == K_KDEL)
504 {
505 cap->count0 /= 10;
506#ifdef FEAT_CMDL_INFO
507 del_from_showcmd(4); // delete the digit and ~@%
508#endif
509 }
510 else if (cap->count0 > 99999999L)
511 {
512 cap->count0 = 999999999L;
513 }
514 else
515 {
516 cap->count0 = cap->count0 * 10 + (c - '0');
517 }
518#ifdef FEAT_EVAL
519 // Set v:count here, when called from main() and not a stuffed
520 // command, so that v:count can be used in an expression mapping
521 // right after the count. Do set it for redo.
522 if (toplevel && readbuf1_empty())
523 set_vcount_ca(cap, &set_prevcount);
524#endif
525 if (*ctrl_w)
526 {
527 ++no_mapping;
528 ++allow_keys; // no mapping for nchar, but keys
529 }
530 ++no_zero_mapping; // don't map zero here
531 c = plain_vgetc();
532 LANGMAP_ADJUST(c, TRUE);
533 --no_zero_mapping;
534 if (*ctrl_w)
535 {
536 --no_mapping;
537 --allow_keys;
538 }
539#ifdef FEAT_CMDL_INFO
540 *need_flushbuf |= add_to_showcmd(c);
541#endif
542 }
543
544 // If we got CTRL-W there may be a/another count
545 if (c == Ctrl_W && !*ctrl_w && cap->oap->op_type == OP_NOP)
546 {
547 *ctrl_w = TRUE;
548 cap->opcount = cap->count0; // remember first count
549 cap->count0 = 0;
550 ++no_mapping;
551 ++allow_keys; // no mapping for nchar, but keys
552 c = plain_vgetc(); // get next character
553 LANGMAP_ADJUST(c, TRUE);
554 --no_mapping;
555 --allow_keys;
556#ifdef FEAT_CMDL_INFO
557 *need_flushbuf |= add_to_showcmd(c);
558#endif
559 goto getcount; // jump back
560 }
561 }
562
563 if (c == K_CURSORHOLD)
564 {
565 // Save the count values so that ca.opcount and ca.count0 are exactly
566 // the same when coming back here after handling K_CURSORHOLD.
567 cap->oap->prev_opcount = cap->opcount;
568 cap->oap->prev_count0 = cap->count0;
569 }
570 else if (cap->opcount != 0)
571 {
572 // If we're in the middle of an operator (including after entering a
573 // yank buffer with '"') AND we had a count before the operator, then
574 // that count overrides the current value of ca.count0.
575 // What this means effectively, is that commands like "3dw" get turned
576 // into "d3w" which makes things fall into place pretty neatly.
577 // If you give a count before AND after the operator, they are
578 // multiplied.
579 if (cap->count0)
580 {
581 if (cap->opcount >= 999999999L / cap->count0)
582 cap->count0 = 999999999L;
583 else
584 cap->count0 *= cap->opcount;
585 }
586 else
587 cap->count0 = cap->opcount;
588 }
589
590 // Always remember the count. It will be set to zero (on the next call,
591 // above) when there is no pending operator.
592 // When called from main(), save the count for use by the "count" built-in
593 // variable.
594 cap->opcount = cap->count0;
595 cap->count1 = (cap->count0 == 0 ? 1 : cap->count0);
596
597#ifdef FEAT_EVAL
598 // Only set v:count when called from main() and not a stuffed command.
599 // Do set it for redo.
600 if (toplevel && readbuf1_empty())
601 set_vcount(cap->count0, cap->count1, set_prevcount);
602#endif
603
604 return c;
605}
606
607/*
608 * Returns TRUE if the normal command (cap) needs a second character.
609 */
610 static int
611normal_cmd_needs_more_chars(cmdarg_T *cap, short_u cmd_flags)
612{
613 return ((cmd_flags & NV_NCH)
614 && (((cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
615 && cap->oap->op_type == OP_NOP)
616 || (cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
617 || (cap->cmdchar == 'q'
618 && cap->oap->op_type == OP_NOP
619 && reg_recording == 0
620 && reg_executing == 0)
621 || ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
622 && (cap->oap->op_type != OP_NOP || VIsual_active))));
623}
624
625/*
626 * Get one or more additional characters for a normal command.
627 * Return the updated command index (if changed).
628 */
629 static int
630normal_cmd_get_more_chars(int idx, cmdarg_T *cap, int *need_flushbuf UNUSED)
631{
632 int c;
633 int *cp;
634 int repl = FALSE; // get character for replace mode
635 int lit = FALSE; // get extra character literally
636 int langmap_active = FALSE; // using :lmap mappings
637 int lang; // getting a text character
638#ifdef HAVE_INPUT_METHOD
639 int save_smd; // saved value of p_smd
640#endif
641
642 ++no_mapping;
643 ++allow_keys; // no mapping for nchar, but allow key codes
644 // Don't generate a CursorHold event here, most commands can't handle
645 // it, e.g., nv_replace(), nv_csearch().
646 did_cursorhold = TRUE;
647 if (cap->cmdchar == 'g')
648 {
649 /*
650 * For 'g' get the next character now, so that we can check for
651 * "gr", "g'" and "g`".
652 */
653 cap->nchar = plain_vgetc();
654 LANGMAP_ADJUST(cap->nchar, TRUE);
655#ifdef FEAT_CMDL_INFO
656 *need_flushbuf |= add_to_showcmd(cap->nchar);
657#endif
658 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
659 || cap->nchar == Ctrl_BSL)
660 {
661 cp = &cap->extra_char; // need to get a third character
662 if (cap->nchar != 'r')
663 lit = TRUE; // get it literally
664 else
665 repl = TRUE; // get it in replace mode
666 }
667 else
668 cp = NULL; // no third character needed
669 }
670 else
671 {
672 if (cap->cmdchar == 'r') // get it in replace mode
673 repl = TRUE;
674 cp = &cap->nchar;
675 }
676 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
677
678 /*
679 * Get a second or third character.
680 */
681 if (cp != NULL)
682 {
683 if (repl)
684 {
685 State = REPLACE; // pretend Replace mode
686#ifdef CURSOR_SHAPE
687 ui_cursor_shape(); // show different cursor shape
688#endif
689 }
690 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
691 {
692 // Allow mappings defined with ":lmap".
693 --no_mapping;
694 --allow_keys;
695 if (repl)
696 State = LREPLACE;
697 else
698 State = LANGMAP;
699 langmap_active = TRUE;
700 }
701#ifdef HAVE_INPUT_METHOD
702 save_smd = p_smd;
703 p_smd = FALSE; // Don't let the IM code show the mode here
704 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
705 im_set_active(TRUE);
706#endif
707 if ((State & INSERT) && !p_ek)
708 {
709#ifdef FEAT_JOB_CHANNEL
710 ch_log_output = TRUE;
711#endif
712 // Disable bracketed paste and modifyOtherKeys here, we won't
713 // recognize the escape sequences with 'esckeys' off.
714 out_str(T_BD);
715 out_str(T_CTE);
716 }
717
718 *cp = plain_vgetc();
719
720 if ((State & INSERT) && !p_ek)
721 {
722#ifdef FEAT_JOB_CHANNEL
723 ch_log_output = TRUE;
724#endif
725 // Re-enable bracketed paste mode and modifyOtherKeys
726 out_str(T_BE);
727 out_str(T_CTI);
728 }
729
730 if (langmap_active)
731 {
732 // Undo the decrement done above
733 ++no_mapping;
734 ++allow_keys;
735 State = NORMAL_BUSY;
736 }
737#ifdef HAVE_INPUT_METHOD
738 if (lang)
739 {
740 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
741 im_save_status(&curbuf->b_p_iminsert);
742 im_set_active(FALSE);
743 }
744 p_smd = save_smd;
745#endif
746 State = NORMAL_BUSY;
747#ifdef FEAT_CMDL_INFO
748 *need_flushbuf |= add_to_showcmd(*cp);
749#endif
750
751 if (!lit)
752 {
753#ifdef FEAT_DIGRAPHS
754 // Typing CTRL-K gets a digraph.
755 if (*cp == Ctrl_K
756 && ((nv_cmds[idx].cmd_flags & NV_LANG)
757 || cp == &cap->extra_char)
758 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
759 {
760 c = get_digraph(FALSE);
761 if (c > 0)
762 {
763 *cp = c;
764# ifdef FEAT_CMDL_INFO
765 // Guessing how to update showcmd here...
766 del_from_showcmd(3);
767 *need_flushbuf |= add_to_showcmd(*cp);
768# endif
769 }
770 }
771#endif
772
773 // adjust chars > 127, except after "tTfFr" commands
774 LANGMAP_ADJUST(*cp, !lang);
775#ifdef FEAT_RIGHTLEFT
776 // adjust Hebrew mapped char
777 if (p_hkmap && lang && KeyTyped)
778 *cp = hkmap(*cp);
779#endif
780 }
781
782 /*
783 * When the next character is CTRL-\ a following CTRL-N means the
784 * command is aborted and we go to Normal mode.
785 */
786 if (cp == &cap->extra_char
787 && cap->nchar == Ctrl_BSL
788 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
789 {
790 cap->cmdchar = Ctrl_BSL;
791 cap->nchar = cap->extra_char;
792 idx = find_command(cap->cmdchar);
793 }
794 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
795 cap->oap->op_type = get_op_type(*cp, NUL);
796 else if (*cp == Ctrl_BSL)
797 {
798 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
799
800 // There is a busy wait here when typing "f<C-\>" and then
801 // something different from CTRL-N. Can't be avoided.
802 while ((c = vpeekc()) <= 0 && towait > 0L)
803 {
804 do_sleep(towait > 50L ? 50L : towait, FALSE);
805 towait -= 50L;
806 }
807 if (c > 0)
808 {
809 c = plain_vgetc();
810 if (c != Ctrl_N && c != Ctrl_G)
811 vungetc(c);
812 else
813 {
814 cap->cmdchar = Ctrl_BSL;
815 cap->nchar = c;
816 idx = find_command(cap->cmdchar);
817 }
818 }
819 }
820
821 // When getting a text character and the next character is a
822 // multi-byte character, it could be a composing character.
823 // However, don't wait for it to arrive. Also, do enable mapping,
824 // because if it's put back with vungetc() it's too late to apply
825 // mapping.
826 --no_mapping;
827 while (enc_utf8 && lang && (c = vpeekc()) > 0
828 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
829 {
830 c = plain_vgetc();
831 if (!utf_iscomposing(c))
832 {
833 vungetc(c); // it wasn't, put it back
834 break;
835 }
836 else if (cap->ncharC1 == 0)
837 cap->ncharC1 = c;
838 else
839 cap->ncharC2 = c;
840 }
841 ++no_mapping;
842 }
843 --no_mapping;
844 --allow_keys;
845
846 return idx;
847}
848
849/*
850 * Returns TRUE if after processing a normal mode command, need to wait for a
851 * moment when a message is displayed that will be overwritten by the mode
852 * message.
853 */
854 static int
855normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
856{
857 /*
858 * In Visual mode and with "^O" in Insert mode, a short message will be
859 * overwritten by the mode message. Wait a bit, until a key is hit.
860 * In Visual mode, it's more important to keep the Visual area updated
861 * than keeping a message (e.g. from a /pat search).
862 * Only do this if the command was typed, not from a mapping.
863 * Don't wait when emsg_silent is non-zero.
864 * Also wait a bit after an error message, e.g. for "^O:".
865 * Don't redraw the screen, it would remove the message.
866 */
867 return ( ((p_smd
868 && msg_silent == 0
869 && (restart_edit != 0
870 || (VIsual_active
871 && old_pos->lnum == curwin->w_cursor.lnum
872 && old_pos->col == curwin->w_cursor.col)
873 )
874 && (clear_cmdline
875 || redraw_cmdline)
876 && (msg_didout || (msg_didany && msg_scroll))
877 && !msg_nowait
878 && KeyTyped)
879 || (restart_edit != 0
880 && !VIsual_active
881 && (msg_scroll
882 || emsg_on_display)))
883 && cap->oap->regname == 0
884 && !(cap->retval & CA_COMMAND_BUSY)
885 && stuff_empty()
886 && typebuf_typed()
887 && emsg_silent == 0
888 && !in_assert_fails
889 && !did_wait_return
890 && cap->oap->op_type == OP_NOP);
891}
892
893/*
894 * After processing a normal mode command, wait for a moment when a message is
895 * displayed that will be overwritten by the mode message.
896 */
897 static void
898normal_cmd_wait_for_msg(void)
899{
900 int save_State = State;
901
902 // Draw the cursor with the right shape here
903 if (restart_edit != 0)
904 State = INSERT;
905
906 // If need to redraw, and there is a "keep_msg", redraw before the
907 // delay
908 if (must_redraw && keep_msg != NULL && !emsg_on_display)
909 {
910 char_u *kmsg;
911
912 kmsg = keep_msg;
913 keep_msg = NULL;
914 // Showmode() will clear keep_msg, but we want to use it anyway.
915 // First update w_topline.
916 setcursor();
917 update_screen(0);
918 // now reset it, otherwise it's put in the history again
919 keep_msg = kmsg;
920
921 kmsg = vim_strsave(keep_msg);
922 if (kmsg != NULL)
923 {
924 msg_attr((char *)kmsg, keep_msg_attr);
925 vim_free(kmsg);
926 }
927 }
928 setcursor();
929#ifdef CURSOR_SHAPE
930 ui_cursor_shape(); // may show different cursor shape
931#endif
932 cursor_on();
933 out_flush();
934 if (msg_scroll || emsg_on_display)
935 ui_delay(1003L, TRUE); // wait at least one second
936 ui_delay(3003L, FALSE); // wait up to three seconds
937 State = save_State;
938
939 msg_scroll = FALSE;
940 emsg_on_display = FALSE;
941}
942
943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 * Execute a command in Normal mode.
945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100947normal_cmd(
948 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100949 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100951 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100953 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 int old_col = curwin->w_curswant;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000955 int need_flushbuf = FALSE; // need to call out_flush()
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100956 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 static int old_mapped_len = 0;
959 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000960 int set_prevcount = FALSE;
Bram Moolenaarb146e012020-07-19 23:06:05 +0200961 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962
Bram Moolenaara80faa82020-04-12 19:37:17 +0200963 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100966 // Use a count remembered from before entering an operator. After typing
967 // "3d" we return from normal_cmd() and come back here, the "3" is
968 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 ca.opcount = opcount;
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 /*
972 * If there is an operator pending, then the command we take this time
973 * will terminate it. Finish_op tells us to finish the operation before
974 * returning this time (unless the operation was cancelled).
975 */
976#ifdef CURSOR_SHAPE
977 c = finish_op;
978#endif
979 finish_op = (oap->op_type != OP_NOP);
980#ifdef CURSOR_SHAPE
981 if (finish_op != c)
982 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100983 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984# ifdef FEAT_MOUSESHAPE
985 update_mouseshape(-1);
986# endif
987 }
988#endif
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100989 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100991 // When not finishing an operator and no register name typed, reset the
992 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000996#ifdef FEAT_EVAL
997 set_prevcount = TRUE;
998#endif
999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001001 // Restore counts from before receiving K_CURSORHOLD. This means after
1002 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
1003 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +00001004 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
1005 {
1006 ca.opcount = oap->prev_opcount;
1007 ca.count0 = oap->prev_count0;
1008 oap->prev_opcount = 0;
1009 oap->prev_count0 = 0;
1010 }
Bram Moolenaara983fe92008-07-31 20:04:27 +00001011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 State = NORMAL_BUSY;
1015#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001016 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017#endif
1018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001019#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001020 // Set v:count here, when called from main() and not a stuffed
1021 // command, so that v:count can be used in an expression mapping
1022 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001023 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001024 set_vcount_ca(&ca, &set_prevcount);
1025#endif
1026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 /*
1028 * Get the command character from the user.
1029 */
1030 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +01001031 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 /*
1034 * If a mapping was started in Visual or Select mode, remember the length
1035 * of the mapping. This is used below to not return to Insert mode for as
1036 * long as the mapping is being executed.
1037 */
1038 if (restart_edit == 0)
1039 old_mapped_len = 0;
1040 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001041 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 old_mapped_len = typebuf_maplen();
1043
1044 if (c == NUL)
1045 c = K_ZERO;
1046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 /*
1048 * In Select mode, typed text replaces the selection.
1049 */
1050 if (VIsual_active
1051 && VIsual_select
1052 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
1053 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001054 int len;
1055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001056 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
1057 // 'insertmode' is set) fake a "d"elete command, Insert mode will
1058 // restart automatically.
1059 // Insert the typed character in the typeahead buffer, so that it can
1060 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001061 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
1062
1063 // When recording the character will be recorded again, remove the
1064 // previously recording.
1065 ungetchars(len);
1066
Bram Moolenaar686f51e2005-05-20 21:19:57 +00001067 if (restart_edit != 0)
1068 c = 'd';
1069 else
1070 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001071 msg_nowait = TRUE; // don't delay going to insert mode
1072 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001075 // If the window was made so small that nothing shows, make it at least one
1076 // line and one column when typing a command.
1077 if (KeyTyped && !KeyStuffed)
1078 win_ensure_size();
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080#ifdef FEAT_CMDL_INFO
1081 need_flushbuf = add_to_showcmd(c);
1082#endif
1083
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001084 // Get the command count
1085 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
1086 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088 /*
1089 * Find the command character in the table of commands.
1090 * For CTRL-W we already got nchar when looking for a count.
1091 */
1092 if (ctrl_w)
1093 {
1094 ca.nchar = c;
1095 ca.cmdchar = Ctrl_W;
1096 }
1097 else
1098 ca.cmdchar = c;
1099 idx = find_command(ca.cmdchar);
1100 if (idx < 0)
1101 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001102 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 clearopbeep(oap);
1104 goto normal_end;
1105 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001106
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001107 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001109 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001111 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 goto normal_end;
1113 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001114 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
1115 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 /*
1118 * In Visual/Select mode, a few keys are handled in a special way.
1119 */
1120 if (VIsual_active)
1121 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001122 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 if (km_stopsel
1124 && (nv_cmds[idx].cmd_flags & NV_STS)
1125 && !(mod_mask & MOD_MASK_SHIFT))
1126 {
1127 end_visual_mode();
1128 redraw_curbuf_later(INVERTED);
1129 }
1130
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001131 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 if (km_startsel)
1133 {
1134 if (nv_cmds[idx].cmd_flags & NV_SS)
1135 {
1136 unshift_special(&ca);
1137 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001138 if (idx < 0)
1139 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001140 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001141 clearopbeep(oap);
1142 goto normal_end;
1143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1146 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 }
1149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
1151#ifdef FEAT_RIGHTLEFT
1152 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
1153 && (nv_cmds[idx].cmd_flags & NV_RL))
1154 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001155 // Invert horizontal movements and operations. Only when typed by the
1156 // user directly, not when the result of a mapping or "x" translated
1157 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 switch (ca.cmdchar)
1159 {
1160 case 'l': ca.cmdchar = 'h'; break;
1161 case K_RIGHT: ca.cmdchar = K_LEFT; break;
1162 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
1163 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
1164 case 'h': ca.cmdchar = 'l'; break;
1165 case K_LEFT: ca.cmdchar = K_RIGHT; break;
1166 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
1167 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
1168 case '>': ca.cmdchar = '<'; break;
1169 case '<': ca.cmdchar = '>'; break;
1170 }
1171 idx = find_command(ca.cmdchar);
1172 }
1173#endif
1174
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001175 // Get additional characters if we need them.
1176 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
1177 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
1179#ifdef FEAT_CMDL_INFO
1180 /*
1181 * Flush the showcmd characters onto the screen so we can see them while
1182 * the command is being executed. Only do this when the shown command was
1183 * actually displayed, otherwise this will slow down a lot when executing
1184 * mappings.
1185 */
1186 if (need_flushbuf)
1187 out_flush();
1188#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001189 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001190 {
1191 if (ex_normal_busy)
1192 did_cursorhold = save_did_cursorhold;
1193 else
1194 did_cursorhold = FALSE;
1195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
1197 State = NORMAL;
1198
1199 if (ca.nchar == ESC)
1200 {
1201 clearop(oap);
1202 if (restart_edit == 0 && goto_im())
1203 restart_edit = 'a';
1204 goto normal_end;
1205 }
1206
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001207 if (ca.cmdchar != K_IGNORE)
1208 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001209 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001210 msg_col = 0;
1211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001213 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001215 // When 'keymodel' contains "startsel" some keys start Select/Visual
1216 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 if (!VIsual_active && km_startsel)
1218 {
1219 if (nv_cmds[idx].cmd_flags & NV_SS)
1220 {
1221 start_selection();
1222 unshift_special(&ca);
1223 idx = find_command(ca.cmdchar);
1224 }
1225 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1226 && (mod_mask & MOD_MASK_SHIFT))
1227 {
1228 start_selection();
1229 mod_mask &= ~MOD_MASK_SHIFT;
1230 }
1231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
1233 /*
1234 * Execute the command!
1235 * Call the command function found in the commands table.
1236 */
1237 ca.arg = nv_cmds[idx].cmd_arg;
1238 (nv_cmds[idx].cmd_func)(&ca);
1239
1240 /*
1241 * If we didn't start or finish an operator, reset oap->regname, unless we
1242 * need it later.
1243 */
1244 if (!finish_op
1245 && !oap->op_type
1246 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1247 {
1248 clearop(oap);
1249#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001250 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251#endif
1252 }
1253
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 // Get the length of mapped chars again after typing a count, second
1255 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001256 if (old_mapped_len > 0)
1257 old_mapped_len = typebuf_maplen();
1258
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 /*
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001260 * If an operation is pending, handle it. But not for K_IGNORE or
1261 * K_MOUSEMOVE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 */
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001263 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001264 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001266 // Wait for a moment when a message is displayed that will be overwritten
1267 // by the mode message.
1268 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
1269 normal_cmd_wait_for_msg();
1270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 /*
1272 * Wait for a moment when a message is displayed that will be overwritten
1273 * by the mode message.
1274 * In Visual mode and with "^O" in Insert mode, a short message will be
1275 * overwritten by the mode message. Wait a bit, until a key is hit.
1276 * In Visual mode, it's more important to keep the Visual area updated
1277 * than keeping a message (e.g. from a /pat search).
1278 * Only do this if the command was typed, not from a mapping.
1279 * Don't wait when emsg_silent is non-zero.
1280 * Also wait a bit after an error message, e.g. for "^O:".
1281 * Don't redraw the screen, it would remove the message.
1282 */
1283 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001284 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 || (VIsual_active
1287 && old_pos.lnum == curwin->w_cursor.lnum
1288 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 )
1290 && (clear_cmdline
1291 || redraw_cmdline)
1292 && (msg_didout || (msg_didany && msg_scroll))
1293 && !msg_nowait
1294 && KeyTyped)
1295 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 && (msg_scroll
1298 || emsg_on_display)))
1299 && oap->regname == 0
1300 && !(ca.retval & CA_COMMAND_BUSY)
1301 && stuff_empty()
1302 && typebuf_typed()
1303 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001304 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 && !did_wait_return
1306 && oap->op_type == OP_NOP)
1307 {
1308 int save_State = State;
1309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001310 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (restart_edit != 0)
1312 State = INSERT;
1313
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001314 // If need to redraw, and there is a "keep_msg", redraw before the
1315 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1317 {
1318 char_u *kmsg;
1319
1320 kmsg = keep_msg;
1321 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001322 // Showmode() will clear keep_msg, but we want to use it anyway.
1323 // First update w_topline.
1324 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001326 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001328
1329 kmsg = vim_strsave(keep_msg);
1330 if (kmsg != NULL)
1331 {
1332 msg_attr((char *)kmsg, keep_msg_attr);
1333 vim_free(kmsg);
1334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001337#ifdef CURSOR_SHAPE
1338 ui_cursor_shape(); // may show different cursor shape
1339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 cursor_on();
1341 out_flush();
1342 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001343 ui_delay(1003L, TRUE); // wait at least one second
1344 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 State = save_State;
1346
1347 msg_scroll = FALSE;
1348 emsg_on_display = FALSE;
1349 }
1350
1351 /*
1352 * Finish up after executing a Normal mode command.
1353 */
1354normal_end:
1355
1356 msg_nowait = FALSE;
1357
Bram Moolenaarcc613032020-06-07 21:31:18 +02001358#ifdef FEAT_EVAL
1359 if (finish_op)
1360 reset_reg_var();
1361#endif
1362
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001363 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364#ifdef CURSOR_SHAPE
1365 c = finish_op;
1366#endif
1367 finish_op = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001368 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001370 // Redraw the cursor with another shape, if we were in Operator-pending
1371 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 if (c || ca.cmdchar == 'r')
1373 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001374 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375# ifdef FEAT_MOUSESHAPE
1376 update_mouseshape(-1);
1377# endif
1378 }
1379#endif
1380
1381#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001382 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001383 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 clear_showcmd();
1385#endif
1386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001387 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 vim_free(ca.searchbuf);
1389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (has_mbyte)
1391 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (curwin->w_p_scb && toplevel)
1394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001395 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 do_check_scrollbind(TRUE);
1397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaar860cae12010-06-05 23:22:07 +02001399 if (curwin->w_p_crb && toplevel)
1400 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001401 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001402 do_check_cursorbind();
1403 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001404
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001405#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001406 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001407 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001408 restart_edit = 0;
1409#endif
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 /*
1412 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1413 * if still inside a mapping that started in Visual mode).
1414 * May switch from Visual to Select mode after CTRL-O command.
1415 */
1416 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1418 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 && !(ca.retval & CA_COMMAND_BUSY)
1420 && stuff_empty()
1421 && oap->regname == 0)
1422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (restart_VIsual_select == 1)
1424 {
1425 VIsual_select = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001426 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 showmode();
1428 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001429 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001431 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 (void)edit(restart_edit, FALSE, 1L);
1433 }
1434
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (restart_VIsual_select == 2)
1436 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001438 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 opcount = ca.opcount;
1440}
1441
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001442#ifdef FEAT_EVAL
1443/*
1444 * Set v:count and v:count1 according to "cap".
1445 * Set v:prevcount only when "set_prevcount" is TRUE.
1446 */
1447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001448set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001449{
1450 long count = cap->count0;
1451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001452 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001453 if (cap->opcount != 0)
1454 count = cap->opcount * (count == 0 ? 1 : count);
1455 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001456 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001457}
1458#endif
1459
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001461 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 * if not.
1463 */
1464 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001465check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 static int did_check = FALSE;
1468
1469 if (full_screen)
1470 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001471 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001472 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 did_check = TRUE;
1474 }
1475}
1476
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001477#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1478/*
1479 * Call yank_do_autocmd() for "regname".
1480 */
1481 static void
1482call_yank_do_autocmd(int regname)
1483{
1484 oparg_T oa;
1485 yankreg_T *reg;
1486
1487 clear_oparg(&oa);
1488 oa.regname = regname;
1489 oa.op_type = OP_YANK;
1490 oa.is_VIsual = TRUE;
1491 reg = get_register(regname, TRUE);
1492 yank_do_autocmd(&oa, reg);
1493 free_register(reg);
1494}
1495#endif
1496
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001498 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001499 * This function or the next should ALWAYS be called to end Visual mode, except
1500 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 */
1502 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001503end_visual_mode()
1504{
1505 end_visual_mode_keep_button();
1506 reset_held_button();
1507}
1508
1509 void
1510end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512#ifdef FEAT_CLIPBOARD
1513 /*
1514 * If we are using the clipboard, then remember what was selected in case
1515 * we need to paste it somewhere while we still own the selection.
1516 * Only do this when the clipboard is already owned. Don't want to grab
1517 * the selection when hitting ESC.
1518 */
1519 if (clip_star.available && clip_star.owned)
1520 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001521
1522# if defined(FEAT_EVAL)
1523 // Emit a TextYankPost for the automatic copy of the selection into the
1524 // star and/or plus register.
1525 if (has_textyankpost())
1526 {
1527 if (clip_isautosel_star())
1528 call_yank_do_autocmd('*');
1529 if (clip_isautosel_plus())
1530 call_yank_do_autocmd('+');
1531 }
1532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#endif
1534
1535 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 setmouse();
1537 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001539 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001540 curbuf->b_visual.vi_mode = VIsual_mode;
1541 curbuf->b_visual.vi_start = VIsual;
1542 curbuf->b_visual.vi_end = curwin->w_cursor;
1543 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544#ifdef FEAT_EVAL
1545 curbuf->b_visual_mode_eval = VIsual_mode;
1546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 if (!virtual_active())
1548 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001549 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001551 adjust_cursor_eol();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001552 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553}
1554
1555/*
1556 * Reset VIsual_active and VIsual_reselect.
1557 */
1558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001559reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 if (VIsual_active)
1562 {
1563 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001564 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
1566 VIsual_reselect = FALSE;
1567}
1568
1569/*
1570 * Reset VIsual_active and VIsual_reselect if it's set.
1571 */
1572 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001573reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
1575 if (VIsual_active)
1576 {
1577 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001578 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 VIsual_reselect = FALSE;
1580 }
1581}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001583 void
1584restore_visual_mode(void)
1585{
1586 if (VIsual_mode_orig != NUL)
1587 {
1588 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1589 VIsual_mode_orig = NUL;
1590 }
1591}
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593/*
1594 * Check for a balloon-eval special item to include when searching for an
1595 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1596 * Returns TRUE if the character at "*ptr" should be included.
1597 * "dir" is FORWARD or BACKWARD, the direction of searching.
1598 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1599 * "bnp" points to a counter for square brackets.
1600 */
1601 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001602find_is_eval_item(
1603 char_u *ptr,
1604 int *colp,
1605 int *bnp,
1606 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001608 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1610 ++*bnp;
1611 if (*bnp > 0)
1612 {
1613 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1614 --*bnp;
1615 return TRUE;
1616 }
1617
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001618 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (*ptr == '.')
1620 return TRUE;
1621
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001622 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1624 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1625 {
1626 *colp += dir;
1627 return TRUE;
1628 }
1629 return FALSE;
1630}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632/*
1633 * Find the identifier under or to the right of the cursor.
1634 * "find_type" can have one of three values:
1635 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001636 * FIND_STRING: find any non-white text
1637 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001638 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 *
1640 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001641 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001643 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 * This doesn't match the real Vi but I like it a little better and it
1645 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001646 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 * When FIND_IDENT isn't defined, we backup until a blank.
1648 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001649 * Returns the length of the text, or zero if no text is found.
1650 * If text is found, a pointer to the text is put in "*text". This
1651 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 */
1653 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001654find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
1656 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001657 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658}
1659
1660/*
1661 * Like find_ident_under_cursor(), but for any window and any position.
1662 * However: Uses 'iskeyword' from the current window!.
1663 */
1664 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001665find_ident_at_pos(
1666 win_T *wp,
1667 linenr_T lnum,
1668 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001669 char_u **text,
1670 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001671 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001674 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 int this_class = 0;
1677 int prev_class;
1678 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001679 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 /*
1682 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001683 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
1685 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1686 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1687 {
1688 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001689 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 */
1691 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (has_mbyte)
1693 {
1694 while (ptr[col] != NUL)
1695 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001696 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1698 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 this_class = mb_get_class(ptr + col);
1700 if (this_class != 0 && (i == 1 || this_class != 1))
1701 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001702 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
1704 }
1705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001707 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 )
1710 ++col;
1711
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001712 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001716 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (has_mbyte)
1719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001720 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1722 this_class = mb_get_class((char_u *)"a");
1723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001725 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
1727 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1728 prev_class = mb_get_class(ptr + prevcol);
1729 if (this_class != prev_class
1730 && (i == 0
1731 || prev_class == 0
1732 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 && (!(find_type & FIND_EVAL)
1734 || prevcol == 0
1735 || !find_is_eval_item(ptr + prevcol, &prevcol,
1736 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 )
1738 break;
1739 col = prevcol;
1740 }
1741
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001742 // If we don't want just any old text, or we've found an
1743 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (this_class > 2)
1745 this_class = 2;
1746 if (!(find_type & FIND_STRING) || this_class == 2)
1747 break;
1748 }
1749 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 {
1751 while (col > 0
1752 && ((i == 0
1753 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001754 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 && (!(find_type & FIND_IDENT)
1756 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 || ((find_type & FIND_EVAL)
1758 && col > 1
1759 && find_is_eval_item(ptr + col - 1, &col,
1760 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 ))
1762 --col;
1763
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001764 // If we don't want just any old text, or we've found an
1765 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1767 break;
1768 }
1769 }
1770
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001771 if (ptr[col] == NUL || (i == 0
1772 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001774 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001775 if ((find_type & FIND_NOERROR) == 0)
1776 {
1777 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001778 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001779 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001780 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 return 0;
1783 }
1784 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001785 *text = ptr;
1786 if (textcol != NULL)
1787 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001790 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 bn = 0;
1793 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (has_mbyte)
1796 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001797 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 this_class = mb_get_class(ptr);
1799 while (ptr[col] != NUL
1800 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1801 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 || ((find_type & FIND_EVAL)
1803 && col <= (int)startcol
1804 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001806 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001810 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 || ((find_type & FIND_EVAL)
1812 && col <= (int)startcol
1813 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 return col;
1818}
1819
1820/*
1821 * Prepare for redo of a normal command.
1822 */
1823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001824prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
1826 prep_redo(cap->oap->regname, cap->count0,
1827 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1828}
1829
1830/*
1831 * Prepare for redo of any command.
1832 * Note that only the last argument can be a multi-byte char.
1833 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001834 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001835prep_redo(
1836 int regname,
1837 long num,
1838 int cmd1,
1839 int cmd2,
1840 int cmd3,
1841 int cmd4,
1842 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001844 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1845}
1846
1847/*
1848 * Prepare for redo of any command with extra count after "cmd2".
1849 */
1850 void
1851prep_redo_num2(
1852 int regname,
1853 long num1,
1854 int cmd1,
1855 int cmd2,
1856 long num2,
1857 int cmd3,
1858 int cmd4,
1859 int cmd5)
1860{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001862 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
1864 AppendCharToRedobuff('"');
1865 AppendCharToRedobuff(regname);
1866 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001867 if (num1 != 0)
1868 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (cmd1 != NUL)
1870 AppendCharToRedobuff(cmd1);
1871 if (cmd2 != NUL)
1872 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001873 if (num2 != 0)
1874 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (cmd3 != NUL)
1876 AppendCharToRedobuff(cmd3);
1877 if (cmd4 != NUL)
1878 AppendCharToRedobuff(cmd4);
1879 if (cmd5 != NUL)
1880 AppendCharToRedobuff(cmd5);
1881}
1882
1883/*
1884 * check for operator active and clear it
1885 *
1886 * return TRUE if operator was active
1887 */
1888 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001889checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 if (oap->op_type == OP_NOP)
1892 return FALSE;
1893 clearopbeep(oap);
1894 return TRUE;
1895}
1896
1897/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001898 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001900 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 */
1902 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001903checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001905 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 return FALSE;
1907 clearopbeep(oap);
1908 return TRUE;
1909}
1910
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001911 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001912clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913{
1914 oap->op_type = OP_NOP;
1915 oap->regname = 0;
1916 oap->motion_force = NUL;
1917 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001918 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919}
1920
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001922clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923{
1924 clearop(oap);
1925 beep_flush();
1926}
1927
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928/*
1929 * Remove the shift modifier from a special key.
1930 */
1931 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001932unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933{
1934 switch (cap->cmdchar)
1935 {
1936 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1937 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1938 case K_S_UP: cap->cmdchar = K_UP; break;
1939 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1940 case K_S_HOME: cap->cmdchar = K_HOME; break;
1941 case K_S_END: cap->cmdchar = K_END; break;
1942 }
1943 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1944}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001946/*
1947 * If the mode is currently displayed clear the command line or update the
1948 * command displayed.
1949 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001950 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001951may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001952{
1953 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001954 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001955#ifdef FEAT_CMDL_INFO
1956 else
1957 clear_showcmd();
1958#endif
1959}
1960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1962/*
1963 * Routines for displaying a partly typed command
1964 */
1965
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001966#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001968static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969static int showcmd_is_clear = TRUE;
1970static int showcmd_visual = FALSE;
1971
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001972static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
1974 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001975clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977 if (!p_sc)
1978 return;
1979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (VIsual_active && !char_avail())
1981 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001982 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 long lines;
1984 colnr_T leftcol, rightcol;
1985 linenr_T top, bot;
1986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001987 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001988 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
1990 top = VIsual.lnum;
1991 bot = curwin->w_cursor.lnum;
1992 }
1993 else
1994 {
1995 top = curwin->w_cursor.lnum;
1996 bot = VIsual.lnum;
1997 }
1998# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001999 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02002000 (void)hasFolding(top, &top, NULL);
2001 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002# endif
2003 lines = bot - top + 1;
2004
2005 if (VIsual_mode == Ctrl_V)
2006 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02002007# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00002008 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01002009 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00002010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002011 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00002012 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01002013 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02002014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02002016# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00002017 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01002018 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02002019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
2021 (long)(rightcol - leftcol + 1));
2022 }
2023 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
2024 sprintf((char *)showcmd_buf, "%ld", lines);
2025 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02002026 {
2027 char_u *s, *e;
2028 int l;
2029 int bytes = 0;
2030 int chars = 0;
2031
2032 if (cursor_bot)
2033 {
2034 s = ml_get_pos(&VIsual);
2035 e = ml_get_cursor();
2036 }
2037 else
2038 {
2039 s = ml_get_cursor();
2040 e = ml_get_pos(&VIsual);
2041 }
2042 while ((*p_sel != 'e') ? s <= e : s < e)
2043 {
2044 l = (*mb_ptr2len)(s);
2045 if (l == 0)
2046 {
2047 ++bytes;
2048 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002049 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02002050 }
2051 bytes += l;
2052 ++chars;
2053 s += l;
2054 }
2055 if (bytes == chars)
2056 sprintf((char *)showcmd_buf, "%d", chars);
2057 else
2058 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
2059 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002060 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 showcmd_visual = TRUE;
2062 }
2063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 showcmd_buf[0] = NUL;
2066 showcmd_visual = FALSE;
2067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002068 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 if (showcmd_is_clear)
2070 return;
2071 }
2072
2073 display_showcmd();
2074}
2075
2076/*
2077 * Add 'c' to string of shown command chars.
2078 * Return TRUE if output has been written (and setcursor() has been called).
2079 */
2080 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002081add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083 char_u *p;
2084 int old_len;
2085 int extra_len;
2086 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 int i;
2088 static int ignore[] =
2089 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002090#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
2092 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02002093#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01002094 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002095 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
2097 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002098 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002100 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 0
2102 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
Bram Moolenaar09df3122006-01-23 22:23:09 +00002104 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 return FALSE;
2106
2107 if (showcmd_visual)
2108 {
2109 showcmd_buf[0] = NUL;
2110 showcmd_visual = FALSE;
2111 }
2112
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002113 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (IS_SPECIAL(c))
2115 for (i = 0; ignore[i] != 0; ++i)
2116 if (ignore[i] == c)
2117 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
2119 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01002120 if (*p == ' ')
2121 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 old_len = (int)STRLEN(showcmd_buf);
2123 extra_len = (int)STRLEN(p);
2124 overflow = old_len + extra_len - SHOWCMD_COLS;
2125 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00002126 mch_memmove(showcmd_buf, showcmd_buf + overflow,
2127 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 STRCAT(showcmd_buf, p);
2129
2130 if (char_avail())
2131 return FALSE;
2132
2133 display_showcmd();
2134
2135 return TRUE;
2136}
2137
2138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002139add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 if (!add_to_showcmd(c))
2142 setcursor();
2143}
2144
2145/*
2146 * Delete 'len' characters from the end of the shown command.
2147 */
2148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002149del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150{
2151 int old_len;
2152
2153 if (!p_sc)
2154 return;
2155
2156 old_len = (int)STRLEN(showcmd_buf);
2157 if (len > old_len)
2158 len = old_len;
2159 showcmd_buf[old_len - len] = NUL;
2160
2161 if (!char_avail())
2162 display_showcmd();
2163}
2164
2165/*
2166 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2167 * something and there is a partial mapping.
2168 */
2169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002170push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 if (p_sc)
2173 STRCPY(old_showcmd_buf, showcmd_buf);
2174}
2175
2176 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002177pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 if (!p_sc)
2180 return;
2181
2182 STRCPY(showcmd_buf, old_showcmd_buf);
2183
2184 display_showcmd();
2185}
2186
2187 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002188display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
2190 int len;
2191
2192 cursor_off();
2193
2194 len = (int)STRLEN(showcmd_buf);
2195 if (len == 0)
2196 showcmd_is_clear = TRUE;
2197 else
2198 {
2199 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2200 showcmd_is_clear = FALSE;
2201 }
2202
2203 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002204 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2205 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
2207 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2208
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002209 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210}
2211#endif
2212
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213/*
2214 * When "check" is FALSE, prepare for commands that scroll the window.
2215 * When "check" is TRUE, take care of scroll-binding after the window has
2216 * scrolled. Called from normal_cmd() and edit().
2217 */
2218 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002219do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
2221 static win_T *old_curwin = NULL;
2222 static linenr_T old_topline = 0;
2223#ifdef FEAT_DIFF
2224 static int old_topfill = 0;
2225#endif
2226 static buf_T *old_buf = NULL;
2227 static colnr_T old_leftcol = 0;
2228
2229 if (check && curwin->w_p_scb)
2230 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002231 // If a ":syncbind" command was just used, don't scroll, only reset
2232 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (did_syncbind)
2234 did_syncbind = FALSE;
2235 else if (curwin == old_curwin)
2236 {
2237 /*
2238 * Synchronize other windows, as necessary according to
2239 * 'scrollbind'. Don't do this after an ":edit" command, except
2240 * when 'diff' is set.
2241 */
2242 if ((curwin->w_buffer == old_buf
2243#ifdef FEAT_DIFF
2244 || curwin->w_p_diff
2245#endif
2246 )
2247 && (curwin->w_topline != old_topline
2248#ifdef FEAT_DIFF
2249 || curwin->w_topfill != old_topfill
2250#endif
2251 || curwin->w_leftcol != old_leftcol))
2252 {
2253 check_scrollbind(curwin->w_topline - old_topline,
2254 (long)(curwin->w_leftcol - old_leftcol));
2255 }
2256 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002257 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 {
2259 /*
2260 * When switching between windows, make sure that the relative
2261 * vertical offset is valid for the new window. The relative
2262 * offset is invalid whenever another 'scrollbind' window has
2263 * scrolled to a point that would force the current window to
2264 * scroll past the beginning or end of its buffer. When the
2265 * resync is performed, some of the other 'scrollbind' windows may
2266 * need to jump so that the current window's relative position is
2267 * visible on-screen.
2268 */
2269 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2270 }
2271 curwin->w_scbind_pos = curwin->w_topline;
2272 }
2273
2274 old_curwin = curwin;
2275 old_topline = curwin->w_topline;
2276#ifdef FEAT_DIFF
2277 old_topfill = curwin->w_topfill;
2278#endif
2279 old_buf = curwin->w_buffer;
2280 old_leftcol = curwin->w_leftcol;
2281}
2282
2283/*
2284 * Synchronize any windows that have "scrollbind" set, based on the
2285 * number of rows by which the current window has changed
2286 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2287 */
2288 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002289check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 int want_ver;
2292 int want_hor;
2293 win_T *old_curwin = curwin;
2294 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 int old_VIsual_select = VIsual_select;
2296 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 colnr_T tgt_leftcol = curwin->w_leftcol;
2298 long topline;
2299 long y;
2300
2301 /*
2302 * check 'scrollopt' string for vertical and horizontal scroll options
2303 */
2304 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2305#ifdef FEAT_DIFF
2306 want_ver |= old_curwin->w_p_diff;
2307#endif
2308 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2309
2310 /*
2311 * loop through the scrollbound windows and scroll accordingly
2312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002314 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002317 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 if (curwin != old_curwin && curwin->w_p_scb)
2319 {
2320 /*
2321 * do the vertical scroll
2322 */
2323 if (want_ver)
2324 {
2325#ifdef FEAT_DIFF
2326 if (old_curwin->w_p_diff && curwin->w_p_diff)
2327 {
2328 diff_set_topline(old_curwin, curwin);
2329 }
2330 else
2331#endif
2332 {
2333 curwin->w_scbind_pos += topline_diff;
2334 topline = curwin->w_scbind_pos;
2335 if (topline > curbuf->b_ml.ml_line_count)
2336 topline = curbuf->b_ml.ml_line_count;
2337 if (topline < 1)
2338 topline = 1;
2339
2340 y = topline - curwin->w_topline;
2341 if (y > 0)
2342 scrollup(y, FALSE);
2343 else
2344 scrolldown(-y, FALSE);
2345 }
2346
2347 redraw_later(VALID);
2348 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351
2352 /*
2353 * do the horizontal scroll
2354 */
2355 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2356 {
2357 curwin->w_leftcol = tgt_leftcol;
2358 leftcol_changed();
2359 }
2360 }
2361 }
2362
2363 /*
2364 * reset current-window
2365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 VIsual_select = old_VIsual_select;
2367 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 curwin = old_curwin;
2369 curbuf = old_curbuf;
2370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372/*
2373 * Command character that's ignored.
2374 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002375 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002378nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002380 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381}
2382
2383/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002384 * Command character that doesn't do anything, but unlike nv_ignore() does
2385 * start edit(). Used for "startinsert" executed while starting up.
2386 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002388nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002389{
2390}
2391
2392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 * Command character doesn't exist.
2394 */
2395 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002396nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398 clearopbeep(cap->oap);
2399}
2400
2401/*
2402 * <Help> and <F1> commands.
2403 */
2404 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002405nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407 if (!checkclearopq(cap->oap))
2408 ex_help(NULL);
2409}
2410
2411/*
2412 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2413 */
2414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002415nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002417#ifdef FEAT_JOB_CHANNEL
2418 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2419 clearopbeep(cap->oap);
2420 else
2421#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002422 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002423 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002424 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002425 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2426 op_addsub(cap->oap, cap->count1, cap->arg);
2427 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002428 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002429 else if (VIsual_active)
2430 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002431 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002432 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433}
2434
2435/*
2436 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2437 */
2438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002439nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002442 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002443 if (mod_mask & MOD_MASK_CTRL)
2444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002446 if (cap->arg == BACKWARD)
2447 goto_tabpage(-(int)cap->count1);
2448 else
2449 goto_tabpage((int)cap->count0);
2450 }
2451 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002452 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454}
2455
2456/*
2457 * Implementation of "gd" and "gD" command.
2458 */
2459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002460nv_gd(
2461 oparg_T *oap,
2462 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002463 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 char_u *ptr;
2467
Bram Moolenaard9d30582005-05-18 22:10:28 +00002468 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002469 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002470 == FAIL)
2471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002473 }
2474 else
2475 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002476#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002477 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2478 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002479#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002480 // clear any search statistics
2481 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2482 clear_cmdline = TRUE;
2483 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002484}
2485
2486/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002487 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2488 * otherwise.
2489 */
2490 static int
2491is_ident(char_u *line, int offset)
2492{
2493 int i;
2494 int incomment = FALSE;
2495 int instring = 0;
2496 int prev = 0;
2497
2498 for (i = 0; i < offset && line[i] != NUL; i++)
2499 {
2500 if (instring != 0)
2501 {
2502 if (prev != '\\' && line[i] == instring)
2503 instring = 0;
2504 }
2505 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2506 {
2507 instring = line[i];
2508 }
2509 else
2510 {
2511 if (incomment)
2512 {
2513 if (prev == '*' && line[i] == '/')
2514 incomment = FALSE;
2515 }
2516 else if (prev == '/' && line[i] == '*')
2517 {
2518 incomment = TRUE;
2519 }
2520 else if (prev == '/' && line[i] == '/')
2521 {
2522 return FALSE;
2523 }
2524 }
2525
2526 prev = line[i];
2527 }
2528
2529 return incomment == FALSE && instring == 0;
2530}
2531
2532/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002533 * Search for variable declaration of "ptr[len]".
2534 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2535 * current file ("gD").
2536 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002537 * Return FAIL when not found.
2538 */
2539 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002540find_decl(
2541 char_u *ptr,
2542 int len,
2543 int locally,
2544 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002545 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002546{
2547 char_u *pat;
2548 pos_T old_pos;
2549 pos_T par_pos;
2550 pos_T found_pos;
2551 int t;
2552 int save_p_ws;
2553 int save_p_scs;
2554 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002555 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002556 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002557 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002558
2559 if ((pat = alloc(len + 7)) == NULL)
2560 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002562 // Put "\V" before the pattern to avoid that the special meaning of "."
2563 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002564 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2565 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 old_pos = curwin->w_cursor;
2567 save_p_ws = p_ws;
2568 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002569 p_ws = FALSE; // don't wrap around end of file now
2570 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571
2572 /*
2573 * With "gD" go to line 1.
2574 * With "gd" Search back for the start of the current function, then go
2575 * back until a blank line. If this fails go to line 1.
2576 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002577 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002579 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002581 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583 else
2584 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002585 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2587 --curwin->w_cursor.lnum;
2588 }
2589 curwin->w_cursor.col = 0;
2590
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002591 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002592 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002593 for (;;)
2594 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002595 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002596 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002597 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002598 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002599
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002600 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002601 {
2602 pos_T *pos;
2603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002604 // Check that the block the match is in doesn't end before the
2605 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002606 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2607 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2608 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002609 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002610 // There can't be a useful match before the end of this block.
2611 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002612 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002613 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002614 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002615 }
2616
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002617 if (t == FAIL)
2618 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002619 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002620 if (found_pos.lnum != 0)
2621 {
2622 curwin->w_cursor = found_pos;
2623 t = OK;
2624 }
2625 break;
2626 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002627 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002628 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002629 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002630 ++curwin->w_cursor.lnum;
2631 curwin->w_cursor.col = 0;
2632 continue;
2633 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002634 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2635
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002636 // If the current position is not a valid identifier and a previous
2637 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002638 if (!valid && found_pos.lnum != 0)
2639 {
2640 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002641 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002642 }
2643
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002644 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002645 if (valid && !locally)
2646 break;
2647 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002648 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002649 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002650 if (found_pos.lnum != 0)
2651 curwin->w_cursor = found_pos;
2652 break;
2653 }
2654
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002655 // For finding a local variable and the match is before the "{" or
2656 // inside a comment, continue searching. For K&R style function
2657 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002658 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002659 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002660 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002661 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002662 // Remove SEARCH_START from flags to avoid getting stuck at one
2663 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002664 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002666
2667 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002669 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 curwin->w_cursor = old_pos;
2671 }
2672 else
2673 {
2674 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002675 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 reset_search_dir();
2677 }
2678
2679 vim_free(pat);
2680 p_ws = save_p_ws;
2681 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002682
2683 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684}
2685
2686/*
2687 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2688 * lines rather than lines in the file.
2689 * 'dist' must be positive.
2690 *
2691 * Return OK if able to move cursor, FAIL otherwise.
2692 */
2693 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002694nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 int linelen = linetabsize(ml_get_curline());
2697 int retval = OK;
2698 int atend = FALSE;
2699 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002700 int col_off1; // margin offset for first screen line
2701 int col_off2; // margin offset for wrapped screen line
2702 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002703 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
2705 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002706 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707
2708 col_off1 = curwin_col_off();
2709 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002710 width1 = curwin->w_width - col_off1;
2711 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002712 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002713 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 /*
2718 * Instead of sticking at the last character of the buffer line we
2719 * try to stick in the last column of the screen.
2720 */
2721 if (curwin->w_curswant == MAXCOL)
2722 {
2723 atend = TRUE;
2724 validate_virtcol();
2725 if (width1 <= 0)
2726 curwin->w_curswant = 0;
2727 else
2728 {
2729 curwin->w_curswant = width1 - 1;
2730 if (curwin->w_virtcol > curwin->w_curswant)
2731 curwin->w_curswant += ((curwin->w_virtcol
2732 - curwin->w_curswant - 1) / width2 + 1) * width2;
2733 }
2734 }
2735 else
2736 {
2737 if (linelen > width1)
2738 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2739 else
2740 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002741 if (curwin->w_curswant >= (colnr_T)n)
2742 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744
2745 while (dist--)
2746 {
2747 if (dir == BACKWARD)
2748 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002749 if ((long)curwin->w_curswant >= width1
2750#ifdef FEAT_FOLDING
2751 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2752#endif
2753 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002754 // Move back within the line. This can give a negative value
2755 // for w_curswant if width1 < width2 (with cpoptions+=n),
2756 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 curwin->w_curswant -= width2;
2758 else
2759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002760 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002762 // Move to the start of a closed fold. Don't do that when
2763 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (!(fdo_flags & FDO_ALL))
2765 (void)hasFolding(curwin->w_cursor.lnum,
2766 &curwin->w_cursor.lnum, NULL);
2767#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002768 if (curwin->w_cursor.lnum == 1)
2769 {
2770 retval = FAIL;
2771 break;
2772 }
2773 --curwin->w_cursor.lnum;
2774
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 linelen = linetabsize(ml_get_curline());
2776 if (linelen > width1)
2777 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2778 + 1) * width2;
2779 }
2780 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002781 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
2783 if (linelen > width1)
2784 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2785 else
2786 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002787 if (curwin->w_curswant + width2 < (colnr_T)n
2788#ifdef FEAT_FOLDING
2789 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2790#endif
2791 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002792 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 curwin->w_curswant += width2;
2794 else
2795 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002796 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002798 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2800 &curwin->w_cursor.lnum);
2801#endif
2802 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2803 {
2804 retval = FAIL;
2805 break;
2806 }
2807 curwin->w_cursor.lnum++;
2808 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002809 // Check if the cursor has moved below the number display
2810 // when width1 < width2 (with cpoptions+=n). Subtract width2
2811 // to get a negative value for w_curswant, which will get
2812 // clipped to column 0.
2813 if (curwin->w_curswant >= width1)
2814 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002815 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
2817 }
2818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002821 if (virtual_active() && atend)
2822 coladvance(MAXCOL);
2823 else
2824 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2827 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002828 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002829 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002830
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 /*
2832 * Check for landing on a character that got split at the end of the
2833 * last line. We want to advance a screenline, not end up in the same
2834 * screenline or move two screenlines.
2835 */
2836 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002837 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002838#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002839 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2840 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002841#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002842
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002843 c = (*mb_ptr2char)(ml_get_cursor());
2844 if (dir == FORWARD && virtcol < curwin->w_curswant
2845 && (curwin->w_curswant <= (colnr_T)width1)
2846 && !vim_isprintc(c) && c > 255)
2847 oneright();
2848
Bram Moolenaar773b1582014-08-29 14:20:51 +02002849 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 && (curwin->w_curswant < (colnr_T)width1
2851 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2852 : ((curwin->w_curswant - width1) % width2
2853 > (colnr_T)width2 / 2)))
2854 --curwin->w_cursor.col;
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002858 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859
2860 return retval;
2861}
2862
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863/*
2864 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2865 * cap->arg must be TRUE for CTRL-E.
2866 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002868nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 if (!checkclearop(cap->oap))
2871 scroll_redraw(cap->arg, cap->count1);
2872}
2873
2874/*
2875 * Scroll "count" lines up or down, and redraw.
2876 */
2877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002878scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 linenr_T prev_topline = curwin->w_topline;
2881#ifdef FEAT_DIFF
2882 int prev_topfill = curwin->w_topfill;
2883#endif
2884 linenr_T prev_lnum = curwin->w_cursor.lnum;
2885
2886 if (up)
2887 scrollup(count, TRUE);
2888 else
2889 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002890 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002892 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2893 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 cursor_correct();
2895 check_cursor_moved(curwin);
2896 curwin->w_valid |= VALID_TOPLINE;
2897
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // If moved back to where we were, at least move the cursor, otherwise
2899 // we get stuck at one position. Don't move the cursor up if the
2900 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 while (curwin->w_topline == prev_topline
2902#ifdef FEAT_DIFF
2903 && curwin->w_topfill == prev_topfill
2904#endif
2905 )
2906 {
2907 if (up)
2908 {
2909 if (curwin->w_cursor.lnum > prev_lnum
2910 || cursor_down(1L, FALSE) == FAIL)
2911 break;
2912 }
2913 else
2914 {
2915 if (curwin->w_cursor.lnum < prev_lnum
2916 || prev_topline == 1L
2917 || cursor_up(1L, FALSE) == FAIL)
2918 break;
2919 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // Mark w_topline as valid, otherwise the screen jumps back at the
2921 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 check_cursor_moved(curwin);
2923 curwin->w_valid |= VALID_TOPLINE;
2924 }
2925 }
2926 if (curwin->w_cursor.lnum != prev_lnum)
2927 coladvance(curwin->w_curswant);
2928 redraw_later(VALID);
2929}
2930
2931/*
2932 * Commands that start with "z".
2933 */
2934 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002935nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936{
2937 long n;
2938 colnr_T col;
2939 int nchar = cap->nchar;
2940#ifdef FEAT_FOLDING
2941 long old_fdl = curwin->w_p_fdl;
2942 int old_fen = curwin->w_p_fen;
2943#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002944#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002945 int undo = FALSE;
2946#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002947 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 if (VIM_ISDIGIT(nchar))
2950 {
2951 /*
2952 * "z123{nchar}": edit the count before obtaining {nchar}
2953 */
2954 if (checkclearop(cap->oap))
2955 return;
2956 n = nchar - '0';
2957 for (;;)
2958 {
2959#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002960 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961#endif
2962 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002963 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002964 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 --no_mapping;
2967 --allow_keys;
2968#ifdef FEAT_CMDL_INFO
2969 (void)add_to_showcmd(nchar);
2970#endif
2971 if (nchar == K_DEL || nchar == K_KDEL)
2972 n /= 10;
2973 else if (VIM_ISDIGIT(nchar))
2974 n = n * 10 + (nchar - '0');
2975 else if (nchar == CAR)
2976 {
2977#ifdef FEAT_GUI
2978 need_mouse_correct = TRUE;
2979#endif
2980 win_setheight((int)n);
2981 break;
2982 }
2983 else if (nchar == 'l'
2984 || nchar == 'h'
2985 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002986 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
2988 cap->count1 = n ? n * cap->count1 : cap->count1;
2989 goto dozet;
2990 }
2991 else
2992 {
2993 clearopbeep(cap->oap);
2994 break;
2995 }
2996 }
2997 cap->oap->op_type = OP_NOP;
2998 return;
2999 }
3000
3001dozet:
3002 if (
3003#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003004 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
3005 // and "zC" only in Visual mode. "zj" and "zk" are motion
3006 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 cap->nchar != 'f' && cap->nchar != 'F'
3008 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
3009 && cap->nchar != 'j' && cap->nchar != 'k'
3010 &&
3011#endif
3012 checkclearop(cap->oap))
3013 return;
3014
3015 /*
3016 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
3017 * If line number given, set cursor.
3018 */
3019 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
3020 && cap->count0
3021 && cap->count0 != curwin->w_cursor.lnum)
3022 {
3023 setpcmark();
3024 if (cap->count0 > curbuf->b_ml.ml_line_count)
3025 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3026 else
3027 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003028 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 }
3030
3031 switch (nchar)
3032 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 case '+':
3035 if (cap->count0 == 0)
3036 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003037 // No count given: put cursor at the line below screen
3038 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3040 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3041 else
3042 curwin->w_cursor.lnum = curwin->w_botline;
3043 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003044 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 case NL:
3046 case CAR:
3047 case K_KENTER:
3048 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003049 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
3051 case 't': scroll_cursor_top(0, TRUE);
3052 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003053 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 break;
3055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003056 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003058 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
3060 case 'z': scroll_cursor_halfway(TRUE);
3061 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003062 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 break;
3064
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003065 // "z^", "z-" and "zb": put cursor at bottom of screen
3066 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
3067 // when <count> is at bottom of window, and puts that one at
3068 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 if (cap->count0 != 0)
3070 {
3071 scroll_cursor_bot(0, TRUE);
3072 curwin->w_cursor.lnum = curwin->w_topline;
3073 }
3074 else if (curwin->w_topline == 1)
3075 curwin->w_cursor.lnum = 1;
3076 else
3077 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003078 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 case '-':
3080 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003081 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
3083 case 'b': scroll_cursor_bot(0, TRUE);
3084 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003085 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 break;
3087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02003090 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003093 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 case 'h':
3095 case K_LEFT:
3096 if (!curwin->w_p_wrap)
3097 {
3098 if ((colnr_T)cap->count1 > curwin->w_leftcol)
3099 curwin->w_leftcol = 0;
3100 else
3101 curwin->w_leftcol -= (colnr_T)cap->count1;
3102 leftcol_changed();
3103 }
3104 break;
3105
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003106 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02003107 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003110 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 case 'l':
3112 case K_RIGHT:
3113 if (!curwin->w_p_wrap)
3114 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 curwin->w_leftcol += (colnr_T)cap->count1;
3117 leftcol_changed();
3118 }
3119 break;
3120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003121 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 case 's': if (!curwin->w_p_wrap)
3123 {
3124#ifdef FEAT_FOLDING
3125 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003126 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 else
3128#endif
3129 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01003130 if ((long)col > siso)
3131 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 else
3133 col = 0;
3134 if (curwin->w_leftcol != col)
3135 {
3136 curwin->w_leftcol = col;
3137 redraw_later(NOT_VALID);
3138 }
3139 }
3140 break;
3141
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003142 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 case 'e': if (!curwin->w_p_wrap)
3144 {
3145#ifdef FEAT_FOLDING
3146 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003147 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 else
3149#endif
3150 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02003151 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01003152 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 col = 0;
3154 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01003155 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (curwin->w_leftcol != col)
3157 {
3158 curwin->w_leftcol = col;
3159 redraw_later(NOT_VALID);
3160 }
3161 }
3162 break;
3163
Christian Brabandt2fa93842021-05-30 22:17:25 +02003164 // "zp", "zP" in block mode put without addind trailing spaces
3165 case 'P':
3166 case 'p': nv_put(cap);
3167 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02003168 // "zy" Yank without trailing spaces
3169 case 'y': nv_operator(cap);
3170 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003172 // "zF": create fold command
3173 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 case 'F':
3175 case 'f': if (foldManualAllowed(TRUE))
3176 {
3177 cap->nchar = 'f';
3178 nv_operator(cap);
3179 curwin->w_p_fen = TRUE;
3180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003181 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3183 {
3184 nv_operator(cap);
3185 finish_op = TRUE;
3186 }
3187 }
3188 else
3189 clearopbeep(cap->oap);
3190 break;
3191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 // "zd": delete fold at cursor
3193 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 case 'd':
3195 case 'D': if (foldManualAllowed(FALSE))
3196 {
3197 if (VIsual_active)
3198 nv_operator(cap);
3199 else
3200 deleteFold(curwin->w_cursor.lnum,
3201 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3202 }
3203 break;
3204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 case 'E': if (foldmethodIsManual(curwin))
3207 {
3208 clearFolding(curwin);
3209 changed_window_setting();
3210 }
3211 else if (foldmethodIsMarker(curwin))
3212 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3213 TRUE, FALSE);
3214 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003215 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 break;
3217
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003218 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 case 'n': curwin->w_p_fen = FALSE;
3220 break;
3221
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 case 'N': curwin->w_p_fen = TRUE;
3224 break;
3225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003226 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3228 break;
3229
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003230 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3232 openFold(curwin->w_cursor.lnum, cap->count1);
3233 else
3234 {
3235 closeFold(curwin->w_cursor.lnum, cap->count1);
3236 curwin->w_p_fen = TRUE;
3237 }
3238 break;
3239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003240 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3242 openFoldRecurse(curwin->w_cursor.lnum);
3243 else
3244 {
3245 closeFoldRecurse(curwin->w_cursor.lnum);
3246 curwin->w_p_fen = TRUE;
3247 }
3248 break;
3249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003250 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 case 'o': if (VIsual_active)
3252 nv_operator(cap);
3253 else
3254 openFold(curwin->w_cursor.lnum, cap->count1);
3255 break;
3256
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003257 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 case 'O': if (VIsual_active)
3259 nv_operator(cap);
3260 else
3261 openFoldRecurse(curwin->w_cursor.lnum);
3262 break;
3263
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003264 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 case 'c': if (VIsual_active)
3266 nv_operator(cap);
3267 else
3268 closeFold(curwin->w_cursor.lnum, cap->count1);
3269 curwin->w_p_fen = TRUE;
3270 break;
3271
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003272 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 case 'C': if (VIsual_active)
3274 nv_operator(cap);
3275 else
3276 closeFoldRecurse(curwin->w_cursor.lnum);
3277 curwin->w_p_fen = TRUE;
3278 break;
3279
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003280 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 case 'v': foldOpenCursor();
3282 break;
3283
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003284 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003286 curwin->w_foldinvalid = TRUE; // recompute folds
3287 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 foldOpenCursor();
3289 break;
3290
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003291 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003293 curwin->w_foldinvalid = TRUE; // recompute folds
3294 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 break;
3296
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003297 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003299 {
3300 curwin->w_p_fdl -= cap->count1;
3301 if (curwin->w_p_fdl < 0)
3302 curwin->w_p_fdl = 0;
3303 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003304 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 curwin->w_p_fen = TRUE;
3306 break;
3307
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003308 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003310 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 curwin->w_p_fen = TRUE;
3312 break;
3313
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003314 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003315 case 'r': curwin->w_p_fdl += cap->count1;
3316 {
3317 int d = getDeepestNesting();
3318
3319 if (curwin->w_p_fdl >= d)
3320 curwin->w_p_fdl = d;
3321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 break;
3323
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003324 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003326 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 break;
3328
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003329 case 'j': // "zj" move to next fold downwards
3330 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3332 cap->count1) == FAIL)
3333 clearopbeep(cap->oap);
3334 break;
3335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003336#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003338#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003339 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003340 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003341 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003342 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003343 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003344 --no_mapping;
3345 --allow_keys;
3346#ifdef FEAT_CMDL_INFO
3347 (void)add_to_showcmd(nchar);
3348#endif
3349 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3350 {
3351 clearopbeep(cap->oap);
3352 break;
3353 }
3354 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003355 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003356
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003357 case 'g': // "zg": add good word to word list
3358 case 'w': // "zw": add wrong word to word list
3359 case 'G': // "zG": add good word to temp word list
3360 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003361 {
3362 char_u *ptr = NULL;
3363 int len;
3364
3365 if (checkclearop(cap->oap))
3366 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003367 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3368 == FAIL)
3369 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003370 if (ptr == NULL)
3371 {
3372 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003374 // Find bad word under the cursor. When 'spell' is
3375 // off this fails and find_ident_under_cursor() is
3376 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003377 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003378 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003379 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003380 if (len != 0 && curwin->w_cursor.col <= pos.col)
3381 ptr = ml_get_pos(&curwin->w_cursor);
3382 curwin->w_cursor = pos;
3383 }
3384
Bram Moolenaarb765d632005-06-07 21:00:02 +00003385 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3386 FIND_IDENT)) == 0)
3387 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003388 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3389 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003390 (nchar == 'G' || nchar == 'W')
3391 ? 0 : (int)cap->count1,
3392 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003393 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003394 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003395
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003396 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003397 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003398 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003399 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003400#endif
3401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 default: clearopbeep(cap->oap);
3403 }
3404
3405#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003406 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 if (old_fen != curwin->w_p_fen)
3408 {
3409# ifdef FEAT_DIFF
3410 win_T *wp;
3411
3412 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3413 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003414 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 FOR_ALL_WINDOWS(wp)
3416 {
3417 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3418 {
3419 wp->w_p_fen = curwin->w_p_fen;
3420 changed_window_setting_win(wp);
3421 }
3422 }
3423 }
3424# endif
3425 changed_window_setting();
3426 }
3427
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003428 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (old_fdl != curwin->w_p_fdl)
3430 newFoldLevel();
3431#endif
3432}
3433
3434#ifdef FEAT_GUI
3435/*
3436 * Vertical scrollbar movement.
3437 */
3438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003439nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 if (cap->oap->op_type != OP_NOP)
3442 clearopbeep(cap->oap);
3443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 gui_do_scroll();
3446}
3447
3448/*
3449 * Horizontal scrollbar movement.
3450 */
3451 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003452nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 if (cap->oap->op_type != OP_NOP)
3455 clearopbeep(cap->oap);
3456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003457 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003458 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459}
3460#endif
3461
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003462#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003463/*
3464 * Click in GUI tab.
3465 */
3466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003467nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003468{
3469 if (cap->oap->op_type != OP_NOP)
3470 clearopbeep(cap->oap);
3471
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003472 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003473 goto_tabpage(current_tab);
3474}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003475
3476/*
3477 * Selected item in tab line menu.
3478 */
3479 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003480nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003481{
3482 if (cap->oap->op_type != OP_NOP)
3483 clearopbeep(cap->oap);
3484
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003485 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003486 handle_tabmenu();
3487}
3488
3489/*
3490 * Handle selecting an item of the GUI tab line menu.
3491 * Used in Normal and Insert mode.
3492 */
3493 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003494handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003495{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003496 switch (current_tabmenu)
3497 {
3498 case TABLINE_MENU_CLOSE:
3499 if (current_tab == 0)
3500 do_cmdline_cmd((char_u *)"tabclose");
3501 else
3502 {
3503 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3504 current_tab);
3505 do_cmdline_cmd(IObuff);
3506 }
3507 break;
3508
3509 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003510 if (current_tab == 0)
3511 do_cmdline_cmd((char_u *)"$tabnew");
3512 else
3513 {
3514 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3515 current_tab - 1);
3516 do_cmdline_cmd(IObuff);
3517 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003518 break;
3519
3520 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003521 if (current_tab == 0)
3522 do_cmdline_cmd((char_u *)"browse $tabnew");
3523 else
3524 {
3525 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3526 current_tab - 1);
3527 do_cmdline_cmd(IObuff);
3528 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003529 break;
3530 }
3531}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003532#endif
3533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534/*
3535 * "Q" command.
3536 */
3537 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003538nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539{
3540 /*
3541 * Ignore 'Q' in Visual mode, just give a beep.
3542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003544 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003545 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 do_exmode(FALSE);
3547}
3548
3549/*
3550 * Handle a ":" command.
3551 */
3552 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003553nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003555 int old_p_im;
3556 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003557 int is_cmdkey = cap->cmdchar == K_COMMAND
3558 || cap->cmdchar == K_SCRIPT_COMMAND;
3559 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
Bram Moolenaar957cf672020-11-12 14:21:06 +01003561 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 nv_operator(cap);
3563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 if (cap->oap->op_type != OP_NOP)
3566 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003567 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 cap->oap->motion_type = MCHAR;
3569 cap->oap->inclusive = FALSE;
3570 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003571 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003573 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 stuffcharReadbuff('.');
3575 if (cap->count0 > 1)
3576 {
3577 stuffReadbuff((char_u *)",.+");
3578 stuffnumReadbuff((long)cap->count0 - 1L);
3579 }
3580 }
3581
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003582 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (KeyTyped)
3584 compute_cmdrow();
3585
3586 old_p_im = p_im;
3587
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003588 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003589 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3590 if (is_cmdkey)
3591 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3592 else
3593 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003595 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (p_im != old_p_im)
3597 {
3598 if (p_im)
3599 restart_edit = 'i';
3600 else
3601 restart_edit = 0;
3602 }
3603
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003604 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003605 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003606 clearop(cap->oap);
3607 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3609 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003610 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3611 || did_emsg
3612 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003613 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 clearopbeep(cap->oap);
3615 }
3616}
3617
3618/*
3619 * Handle CTRL-G command.
3620 */
3621 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003622nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003624 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
3626 VIsual_select = !VIsual_select;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003627 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 showmode();
3629 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003630 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003631 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 fileinfo((int)cap->count0, FALSE, TRUE);
3633}
3634
3635/*
3636 * Handle CTRL-H <Backspace> command.
3637 */
3638 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003639nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if (VIsual_active && VIsual_select)
3642 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003643 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 v_visop(cap);
3645 }
3646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 nv_left(cap);
3648}
3649
3650/*
3651 * CTRL-L: clear screen and redraw.
3652 */
3653 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003654nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 if (!checkclearop(cap->oap))
3657 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003659 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003660 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003661# ifdef FEAT_RELTIME
3662 {
3663 win_T *wp;
3664
3665 FOR_ALL_WINDOWS(wp)
3666 wp->w_s->b_syn_slow = FALSE;
3667 }
3668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669#endif
3670 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003671#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3672# ifdef VIMDLL
3673 if (!gui.in_use)
3674# endif
3675 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678}
3679
3680/*
3681 * CTRL-O: In Select mode: switch to Visual mode for one command.
3682 * Otherwise: Go to older pcmark.
3683 */
3684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003685nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (VIsual_active && VIsual_select)
3688 {
3689 VIsual_select = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003690 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003692 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
3694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
3696 cap->count1 = -cap->count1;
3697 nv_pcmark(cap);
3698 }
3699}
3700
3701/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003702 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3703 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 */
3705 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003706nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 if (!checkclearopq(cap->oap))
3709 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3710 GETF_SETMARK|GETF_ALT, FALSE);
3711}
3712
3713/*
3714 * "Z" commands.
3715 */
3716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003717nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
3719 if (!checkclearopq(cap->oap))
3720 {
3721 switch (cap->nchar)
3722 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003723 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 case 'Z': do_cmdline_cmd((char_u *)"x");
3725 break;
3726
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003727 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 case 'Q': do_cmdline_cmd((char_u *)"q!");
3729 break;
3730
3731 default: clearopbeep(cap->oap);
3732 }
3733 }
3734}
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736/*
3737 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3738 */
3739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003740do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 oparg_T oa;
3743 cmdarg_T ca;
3744
3745 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003746 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 ca.oap = &oa;
3748 ca.cmdchar = c1;
3749 ca.nchar = c2;
3750 nv_ident(&ca);
3751}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752
3753/*
3754 * Handle the commands that use the word under the cursor.
3755 * [g] CTRL-] :ta to current identifier
3756 * [g] 'K' run program for current identifier
3757 * [g] '*' / to current identifier or string
3758 * [g] '#' ? to current identifier or string
3759 * g ']' :tselect for current identifier
3760 */
3761 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003762nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
3764 char_u *ptr = NULL;
3765 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003766 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003767 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003769 char_u *kp; // value of 'keywordprg'
3770 int kp_help; // 'keywordprg' is ":he"
3771 int kp_ex; // 'keywordprg' starts with ":"
3772 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003774 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003775 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 char_u *aux_ptr;
3777 int isman;
3778 int isman_s;
3779
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003780 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 {
3782 cmdchar = cap->nchar;
3783 g_cmd = TRUE;
3784 }
3785 else
3786 {
3787 cmdchar = cap->cmdchar;
3788 g_cmd = FALSE;
3789 }
3790
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003791 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 cmdchar = '#';
3793
3794 /*
3795 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3796 */
3797 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3800 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (checkclearopq(cap->oap))
3802 return;
3803 }
3804
3805 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3806 (cmdchar == '*' || cmdchar == '#')
3807 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3808 {
3809 clearop(cap->oap);
3810 return;
3811 }
3812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003813 // Allocate buffer to put the command in. Inserting backslashes can
3814 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3815 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3817 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3818 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003819 if (kp_help && *skipwhite(ptr) == NUL)
3820 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003821 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003822 return;
3823 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003824 kp_ex = (*kp == ':');
3825 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3826 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (buf == NULL)
3828 return;
3829 buf[0] = NUL;
3830
3831 switch (cmdchar)
3832 {
3833 case '*':
3834 case '#':
3835 /*
3836 * Put cursor at start of word, makes search skip the word
3837 * under the cursor.
3838 * Call setpcmark() first, so "*``" puts the cursor back where
3839 * it was.
3840 */
3841 setpcmark();
3842 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3843
3844 if (!g_cmd && vim_iswordp(ptr))
3845 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003846 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 break;
3848
3849 case 'K':
3850 if (kp_help)
3851 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003852 else if (kp_ex)
3853 {
3854 if (cap->count0 != 0)
3855 vim_snprintf((char *)buf, buflen, "%s %ld",
3856 kp, cap->count0);
3857 else
3858 STRCPY(buf, kp);
3859 STRCAT(buf, " ");
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 else
3862 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003863 // An external command will probably use an argument starting
3864 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003865 while (*ptr == '-' && n > 0)
3866 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003867 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003868 --n;
3869 }
3870 if (n == 0)
3871 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003872 // found dashes only
3873 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003874 vim_free(buf);
3875 return;
3876 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003877
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003878 // When a count is given, turn it into a range. Is this
3879 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 isman = (STRCMP(kp, "man") == 0);
3881 isman_s = (STRCMP(kp, "man -s") == 0);
3882 if (cap->count0 != 0 && !(isman || isman_s))
3883 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3884
3885 STRCAT(buf, "! ");
3886 if (cap->count0 == 0 && isman_s)
3887 STRCAT(buf, "man");
3888 else
3889 STRCAT(buf, kp);
3890 STRCAT(buf, " ");
3891 if (cap->count0 != 0 && (isman || isman_s))
3892 {
3893 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3894 STRCAT(buf, " ");
3895 }
3896 }
3897 break;
3898
3899 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003900 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901#ifdef FEAT_CSCOPE
3902 if (p_cst)
3903 STRCPY(buf, "cstag ");
3904 else
3905#endif
3906 STRCPY(buf, "ts ");
3907 break;
3908
3909 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003910 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (curbuf->b_help)
3912 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003914 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003915 if (g_cmd)
3916 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003917 else if (cap->count0 == 0)
3918 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003919 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003920 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923
3924 /*
3925 * Now grab the chars in the identifier
3926 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003927 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003929 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003930 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003931 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003932 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003933 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003934 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003935 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003936 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003937 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003939 vim_free(buf);
3940 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003942 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003943 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003944 {
3945 vim_free(buf);
3946 vim_free(p);
3947 return;
3948 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003949 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003950 STRCAT(buf, p);
3951 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003953 else
3954 {
3955 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003956 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003957 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003958 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003959 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003960 {
3961 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003962 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003963 aux_ptr = (char_u *)"";
3964 else
3965 aux_ptr = (char_u *)"\\|\"\n[";
3966 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003967 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003968 aux_ptr = (char_u *)"\\|\"\n*?[";
3969
3970 p = buf + STRLEN(buf);
3971 while (n-- > 0)
3972 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003973 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003974 if (vim_strchr(aux_ptr, *ptr) != NULL)
3975 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003976 // When current byte is a part of multibyte character, copy all
3977 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003978 if (has_mbyte)
3979 {
3980 int i;
3981 int len = (*mb_ptr2len)(ptr) - 1;
3982
3983 for (i = 0; i < len && n >= 1; ++i, --n)
3984 *p++ = *ptr++;
3985 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003986 *p++ = *ptr++;
3987 }
3988 *p = NUL;
3989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
3991 /*
3992 * Execute the command.
3993 */
3994 if (cmdchar == '*' || cmdchar == '#')
3995 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003996 if (!g_cmd && (has_mbyte
3997 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3998 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02004000
4001 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00004002 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02004004
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004005 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02004008 {
4009 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02004011 g_tag_at_cursor = FALSE;
4012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
4014 vim_free(buf);
4015}
4016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017/*
4018 * Get visually selected text, within one line only.
4019 * Returns FAIL if more than one line selected.
4020 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004021 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004022get_visual_text(
4023 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004024 char_u **pp, // return: start of selected text
4025 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026{
4027 if (VIsual_mode != 'V')
4028 unadjust_for_sel();
4029 if (VIsual.lnum != curwin->w_cursor.lnum)
4030 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004031 if (cap != NULL)
4032 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 return FAIL;
4034 }
4035 if (VIsual_mode == 'V')
4036 {
4037 *pp = ml_get_curline();
4038 *lenp = (int)STRLEN(*pp);
4039 }
4040 else
4041 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004042 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 {
4044 *pp = ml_get_pos(&curwin->w_cursor);
4045 *lenp = VIsual.col - curwin->w_cursor.col + 1;
4046 }
4047 else
4048 {
4049 *pp = ml_get_pos(&VIsual);
4050 *lenp = curwin->w_cursor.col - VIsual.col + 1;
4051 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00004052 if (**pp == NUL)
4053 *lenp = 0;
4054 if (has_mbyte && *lenp > 0)
4055 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004056 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058 reset_VIsual_and_resel();
4059 return OK;
4060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062/*
4063 * CTRL-T: backwards in tag stack
4064 */
4065 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004066nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067{
4068 if (!checkclearopq(cap->oap))
4069 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
4070}
4071
4072/*
4073 * Handle scrolling command 'H', 'L' and 'M'.
4074 */
4075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004076nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077{
4078 int used = 0;
4079 long n;
4080#ifdef FEAT_FOLDING
4081 linenr_T lnum;
4082#endif
4083 int half;
4084
4085 cap->oap->motion_type = MLINE;
4086 setpcmark();
4087
4088 if (cap->cmdchar == 'L')
4089 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004090 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 curwin->w_cursor.lnum = curwin->w_botline - 1;
4092 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
4093 curwin->w_cursor.lnum = 1;
4094 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004095 {
4096#ifdef FEAT_FOLDING
4097 if (hasAnyFolding(curwin))
4098 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004099 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004100 for (n = cap->count1 - 1; n > 0
4101 && curwin->w_cursor.lnum > curwin->w_topline; --n)
4102 {
4103 (void)hasFolding(curwin->w_cursor.lnum,
4104 &curwin->w_cursor.lnum, NULL);
4105 --curwin->w_cursor.lnum;
4106 }
4107 }
4108 else
4109#endif
4110 curwin->w_cursor.lnum -= cap->count1 - 1;
4111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113 else
4114 {
4115 if (cap->cmdchar == 'M')
4116 {
4117#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004118 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 used -= diff_check_fill(curwin, curwin->w_topline)
4120 - curwin->w_topfill;
4121#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004122 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
4124 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
4125 {
4126#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004127 // Count half he number of filler lines to be "below this
4128 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
4130 + n) / 2 >= half)
4131 {
4132 --n;
4133 break;
4134 }
4135#endif
4136 used += plines(curwin->w_topline + n);
4137 if (used >= half)
4138 break;
4139#ifdef FEAT_FOLDING
4140 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
4141 n = lnum - curwin->w_topline;
4142#endif
4143 }
4144 if (n > 0 && used > curwin->w_height)
4145 --n;
4146 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004147 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004150#ifdef FEAT_FOLDING
4151 if (hasAnyFolding(curwin))
4152 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004153 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004154 lnum = curwin->w_topline;
4155 while (n-- > 0 && lnum < curwin->w_botline - 1)
4156 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02004157 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004158 ++lnum;
4159 }
4160 n = lnum - curwin->w_topline;
4161 }
4162#endif
4163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 curwin->w_cursor.lnum = curwin->w_topline + n;
4165 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4166 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4167 }
4168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004169 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02004170 if (cap->oap->op_type == OP_NOP)
4171 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 beginline(BL_SOL | BL_FIX);
4173}
4174
4175/*
4176 * Cursor right commands.
4177 */
4178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004179nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180{
4181 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004182 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004184 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4185 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004186 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004187 if (mod_mask & MOD_MASK_CTRL)
4188 cap->arg = TRUE;
4189 nv_wordcmd(cap);
4190 return;
4191 }
4192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 cap->oap->motion_type = MCHAR;
4194 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004195 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004198 * In virtual edit mode, there's no such thing as "past_line", as lines
4199 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 */
4201 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004202 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204 for (n = cap->count1; n > 0; --n)
4205 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004206 if ((!past_line && oneright() == FAIL)
4207 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004208 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004211 * <Space> wraps to next line if 'whichwrap' has 's'.
4212 * 'l' wraps to next line if 'whichwrap' has 'l'.
4213 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 */
4215 if ( ((cap->cmdchar == ' '
4216 && vim_strchr(p_ww, 's') != NULL)
4217 || (cap->cmdchar == 'l'
4218 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004219 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 && vim_strchr(p_ww, '>') != NULL))
4221 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4222 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004223 // When deleting we also count the NL as a character.
4224 // Set cap->oap->inclusive when last char in the line is
4225 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004226 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004228 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 cap->oap->inclusive = TRUE;
4230 else
4231 {
4232 ++curwin->w_cursor.lnum;
4233 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 curwin->w_set_curswant = TRUE;
4236 cap->oap->inclusive = FALSE;
4237 }
4238 continue;
4239 }
4240 if (cap->oap->op_type == OP_NOP)
4241 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004242 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 if (n == cap->count1)
4244 beep_flush();
4245 }
4246 else
4247 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004248 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 cap->oap->inclusive = TRUE;
4250 }
4251 break;
4252 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004253 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 if (virtual_active())
4257 oneright();
4258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004261 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 ++curwin->w_cursor.col;
4264 }
4265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267#ifdef FEAT_FOLDING
4268 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4269 && cap->oap->op_type == OP_NOP)
4270 foldOpenCursor();
4271#endif
4272}
4273
4274/*
4275 * Cursor left commands.
4276 *
4277 * Returns TRUE when operator end should not be adjusted.
4278 */
4279 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004280nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281{
4282 long n;
4283
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004284 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4285 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004286 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004287 if (mod_mask & MOD_MASK_CTRL)
4288 cap->arg = 1;
4289 nv_bck_word(cap);
4290 return;
4291 }
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 cap->oap->motion_type = MCHAR;
4294 cap->oap->inclusive = FALSE;
4295 for (n = cap->count1; n > 0; --n)
4296 {
4297 if (oneleft() == FAIL)
4298 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004299 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4300 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4301 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 if ( (((cap->cmdchar == K_BS
4303 || cap->cmdchar == Ctrl_H)
4304 && vim_strchr(p_ww, 'b') != NULL)
4305 || (cap->cmdchar == 'h'
4306 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004307 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 && vim_strchr(p_ww, '<') != NULL))
4309 && curwin->w_cursor.lnum > 1)
4310 {
4311 --(curwin->w_cursor.lnum);
4312 coladvance((colnr_T)MAXCOL);
4313 curwin->w_set_curswant = TRUE;
4314
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004315 // When the NL before the first char has to be deleted we
4316 // put the cursor on the NUL after the previous line.
4317 // This is a very special case, be careful!
4318 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if ( (cap->oap->op_type == OP_DELETE
4320 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004321 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004323 char_u *cp = ml_get_cursor();
4324
4325 if (*cp != NUL)
4326 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004327 if (has_mbyte)
4328 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4329 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004330 ++curwin->w_cursor.col;
4331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 cap->retval |= CA_NO_ADJ_OP_END;
4333 }
4334 continue;
4335 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004336 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4338 beep_flush();
4339 break;
4340 }
4341 }
4342#ifdef FEAT_FOLDING
4343 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4344 && cap->oap->op_type == OP_NOP)
4345 foldOpenCursor();
4346#endif
4347}
4348
4349/*
4350 * Cursor up commands.
4351 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4352 */
4353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004354nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004356 if (mod_mask & MOD_MASK_SHIFT)
4357 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004358 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004359 cap->arg = BACKWARD;
4360 nv_page(cap);
4361 }
4362 else
4363 {
4364 cap->oap->motion_type = MLINE;
4365 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4366 clearopbeep(cap->oap);
4367 else if (cap->arg)
4368 beginline(BL_WHITE | BL_FIX);
4369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370}
4371
4372/*
4373 * Cursor down commands.
4374 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4375 */
4376 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004377nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004379 if (mod_mask & MOD_MASK_SHIFT)
4380 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004381 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004382 cap->arg = FORWARD;
4383 nv_page(cap);
4384 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004385#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004386 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004387 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4388 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004390 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 {
4392#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004393 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004394 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 cmdwin_result = CAR;
4396 else
4397#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004398#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004399 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004400 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4401 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4402 {
4403 invoke_prompt_callback();
4404 if (restart_edit == 0)
4405 restart_edit = 'a';
4406 }
4407 else
4408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 {
4410 cap->oap->motion_type = MLINE;
4411 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4412 clearopbeep(cap->oap);
4413 else if (cap->arg)
4414 beginline(BL_WHITE | BL_FIX);
4415 }
4416 }
4417}
4418
4419#ifdef FEAT_SEARCHPATH
4420/*
4421 * Grab the file name under the cursor and edit it.
4422 */
4423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004424nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
4426 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004427 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004429 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004432 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 return;
4434 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004435 if (curbuf_locked())
4436 {
4437 clearop(cap->oap);
4438 return;
4439 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004440#ifdef FEAT_PROP_POPUP
4441 if (ERROR_IF_TERM_POPUP_WINDOW)
4442 return;
4443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004445 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 if (ptr != NULL)
4448 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004449 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004450 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004451 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004453 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004454 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004455 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004456 {
4457 curwin->w_cursor.lnum = lnum;
4458 check_cursor_lnum();
4459 beginline(BL_SOL | BL_FIX);
4460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 vim_free(ptr);
4462 }
4463 else
4464 clearop(cap->oap);
4465}
4466#endif
4467
4468/*
4469 * <End> command: to end of current line or last line.
4470 */
4471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004472nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004474 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004476 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004478 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480 nv_dollar(cap);
4481}
4482
4483/*
4484 * Handle the "$" command.
4485 */
4486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004487nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489 cap->oap->motion_type = MCHAR;
4490 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004491 // In virtual mode when off the edge of a line and an operator
4492 // is pending (whew!) keep the cursor where it is.
4493 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 if (!virtual_active() || gchar_cursor() != NUL
4495 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004496 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (cursor_down((long)(cap->count1 - 1),
4498 cap->oap->op_type == OP_NOP) == FAIL)
4499 clearopbeep(cap->oap);
4500#ifdef FEAT_FOLDING
4501 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4502 foldOpenCursor();
4503#endif
4504}
4505
4506/*
4507 * Implementation of '?' and '/' commands.
4508 * If cap->arg is TRUE don't set PC mark.
4509 */
4510 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004511nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512{
4513 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004514 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
4516 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4517 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004518 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 cap->cmdchar = 'g';
4520 cap->nchar = '?';
4521 nv_operator(cap);
4522 return;
4523 }
4524
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004525 // When using 'incsearch' the cursor may be moved to set a different search
4526 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004527 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529 if (cap->searchbuf == NULL)
4530 {
4531 clearop(oap);
4532 return;
4533 }
4534
Bram Moolenaar46539112015-02-17 15:43:57 +01004535 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004536 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004537 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538}
4539
4540/*
4541 * Handle "N" and "n" commands.
4542 * cap->arg is SEARCH_REV for "N", 0 for "n".
4543 */
4544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004545nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004547 pos_T old = curwin->w_cursor;
4548 int wrapped = FALSE;
4549 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004550
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004551 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004552 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004553 // Avoid getting stuck on the current cursor position, which can
4554 // happen when an offset is given and the cursor is on the last char
4555 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004556 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004557 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004558 cap->count1 -= 1;
4559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560}
4561
4562/*
4563 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4564 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004565 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004567 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004568normal_search(
4569 cmdarg_T *cap,
4570 int dir,
4571 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004572 int opt, // extra flags for do_search()
4573 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574{
4575 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004576 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
4578 cap->oap->motion_type = MCHAR;
4579 cap->oap->inclusive = FALSE;
4580 cap->oap->use_reg_one = TRUE;
4581 curwin->w_set_curswant = TRUE;
4582
Bram Moolenaara80faa82020-04-12 19:37:17 +02004583 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004584 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004585 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4586 if (wrapped != NULL)
4587 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (i == 0)
4589 clearop(cap->oap);
4590 else
4591 {
4592 if (i == 2)
4593 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#ifdef FEAT_FOLDING
4596 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4597 foldOpenCursor();
4598#endif
4599 }
4600
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004601 // "/$" will put the cursor after the end of the line, may need to
4602 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004604 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605}
4606
4607/*
4608 * Character search commands.
4609 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4610 * ',' and FALSE for ';'.
4611 * cap->nchar is NUL for ',' and ';' (repeat the search)
4612 */
4613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004614nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615{
4616 int t_cmd;
4617
4618 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4619 t_cmd = TRUE;
4620 else
4621 t_cmd = FALSE;
4622
4623 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4625 clearopbeep(cap->oap);
4626 else
4627 {
4628 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004629 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4631 && (t_cmd || cap->oap->op_type != OP_NOP))
4632 {
4633 colnr_T scol, ecol;
4634
4635 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4636 curwin->w_cursor.coladd = ecol - scol;
4637 }
4638 else
4639 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641#ifdef FEAT_FOLDING
4642 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4643 foldOpenCursor();
4644#endif
4645 }
4646}
4647
4648/*
4649 * "[" and "]" commands.
4650 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4651 */
4652 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004653nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004655 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004657 pos_T *pos = NULL; // init for GCC
4658 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 int flag;
4660 long n;
4661 int findc;
4662 int c;
4663
4664 cap->oap->motion_type = MCHAR;
4665 cap->oap->inclusive = FALSE;
4666 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004667 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669#ifdef FEAT_SEARCHPATH
4670 /*
4671 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4672 */
4673 if (cap->nchar == 'f')
4674 nv_gotofile(cap);
4675 else
4676#endif
4677
4678#ifdef FEAT_FIND_ID
4679 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004680 * Find the occurrence(s) of the identifier or define under cursor
4681 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 *
4683 * search list jump
4684 * fwd bwd fwd bwd fwd bwd
4685 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4686 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4687 */
4688 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004689# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004691# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 cap->nchar) != NULL)
4695 {
4696 char_u *ptr;
4697 int len;
4698
4699 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4700 clearop(cap->oap);
4701 else
4702 {
4703 find_pattern_in_path(ptr, 0, len, TRUE,
4704 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4705 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4706 cap->count1,
4707 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4708 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4709 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4710 (linenr_T)MAXLNUM);
4711 curwin->w_set_curswant = TRUE;
4712 }
4713 }
4714 else
4715#endif
4716
4717 /*
4718 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4719 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4720 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4721 * "[m" or "]m" search for prev/next start of (Java) method.
4722 * "[M" or "]M" search for prev/next end of (Java) method.
4723 */
4724 if ( (cap->cmdchar == '['
4725 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4726 || (cap->cmdchar == ']'
4727 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4728 {
4729 if (cap->nchar == '*')
4730 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 prev_pos.lnum = 0;
4732 if (cap->nchar == 'm' || cap->nchar == 'M')
4733 {
4734 if (cap->cmdchar == '[')
4735 findc = '{';
4736 else
4737 findc = '}';
4738 n = 9999;
4739 }
4740 else
4741 {
4742 findc = cap->nchar;
4743 n = cap->count1;
4744 }
4745 for ( ; n > 0; --n)
4746 {
4747 if ((pos = findmatchlimit(cap->oap, findc,
4748 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4749 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004750 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 if (cap->nchar != 'm' && cap->nchar != 'M')
4753 clearopbeep(cap->oap);
4754 }
4755 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004756 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 break;
4758 }
4759 prev_pos = new_pos;
4760 curwin->w_cursor = *pos;
4761 new_pos = *pos;
4762 }
4763 curwin->w_cursor = old_pos;
4764
4765 /*
4766 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4767 * brought us to the match for "[m" and "]M" when inside a method.
4768 * Try finding the '{' or '}' we want to be at.
4769 * Also repeat for the given count.
4770 */
4771 if (cap->nchar == 'm' || cap->nchar == 'M')
4772 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004773 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 int norm = ((findc == '{') == (cap->nchar == 'm'));
4775
4776 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004777 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 if (prev_pos.lnum != 0)
4779 {
4780 pos = &prev_pos;
4781 curwin->w_cursor = prev_pos;
4782 if (norm)
4783 --n;
4784 }
4785 else
4786 pos = NULL;
4787 while (n > 0)
4788 {
4789 for (;;)
4790 {
4791 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004793 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 if (pos == NULL)
4795 clearopbeep(cap->oap);
4796 n = 0;
4797 break;
4798 }
4799 c = gchar_cursor();
4800 if (c == '{' || c == '}')
4801 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004802 // Must have found end/start of class: use it.
4803 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 if ((c == findc && norm) || (n == 1 && !norm))
4805 {
4806 new_pos = curwin->w_cursor;
4807 pos = &new_pos;
4808 n = 0;
4809 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004810 // if no match found at all, we started outside of the
4811 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 else if (new_pos.lnum == 0)
4813 {
4814 new_pos = curwin->w_cursor;
4815 pos = &new_pos;
4816 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004817 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 else if ((pos = findmatchlimit(cap->oap, findc,
4819 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4820 0)) == NULL)
4821 n = 0;
4822 else
4823 curwin->w_cursor = *pos;
4824 break;
4825 }
4826 }
4827 --n;
4828 }
4829 curwin->w_cursor = old_pos;
4830 if (pos == NULL && new_pos.lnum != 0)
4831 clearopbeep(cap->oap);
4832 }
4833 if (pos != NULL)
4834 {
4835 setpcmark();
4836 curwin->w_cursor = *pos;
4837 curwin->w_set_curswant = TRUE;
4838#ifdef FEAT_FOLDING
4839 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4840 && cap->oap->op_type == OP_NOP)
4841 foldOpenCursor();
4842#endif
4843 }
4844 }
4845
4846 /*
4847 * "[[", "[]", "]]" and "][": move to start or end of function
4848 */
4849 else if (cap->nchar == '[' || cap->nchar == ']')
4850 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004851 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 flag = '{';
4853 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004854 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856 curwin->w_set_curswant = TRUE;
4857 /*
4858 * Imitate strange Vi behaviour: When using "]]" with an operator
4859 * we also stop at '}'.
4860 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004861 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 (cap->oap->op_type != OP_NOP
4863 && cap->arg == FORWARD && flag == '{')))
4864 clearopbeep(cap->oap);
4865 else
4866 {
4867 if (cap->oap->op_type == OP_NOP)
4868 beginline(BL_WHITE | BL_FIX);
4869#ifdef FEAT_FOLDING
4870 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4871 foldOpenCursor();
4872#endif
4873 }
4874 }
4875
4876 /*
4877 * "[p", "[P", "]P" and "]p": put with indent adjustment
4878 */
4879 else if (cap->nchar == 'p' || cap->nchar == 'P')
4880 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004881 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883
4884 /*
4885 * "['", "[`", "]'" and "]`": jump to next mark
4886 */
4887 else if (cap->nchar == '\'' || cap->nchar == '`')
4888 {
4889 pos = &curwin->w_cursor;
4890 for (n = cap->count1; n > 0; --n)
4891 {
4892 prev_pos = *pos;
4893 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4894 cap->nchar == '\'');
4895 if (pos == NULL)
4896 break;
4897 }
4898 if (pos == NULL)
4899 pos = &prev_pos;
4900 nv_cursormark(cap, cap->nchar == '\'', pos);
4901 }
4902
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 /*
4904 * [ or ] followed by a middle mouse click: put selected text with
4905 * indent adjustment. Any other button just does as usual.
4906 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004907 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
4909 (void)do_mouse(cap->oap, cap->nchar,
4910 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4911 cap->count1, PUT_FIXINDENT);
4912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914#ifdef FEAT_FOLDING
4915 /*
4916 * "[z" and "]z": move to start or end of open fold.
4917 */
4918 else if (cap->nchar == 'z')
4919 {
4920 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4921 cap->count1) == FAIL)
4922 clearopbeep(cap->oap);
4923 }
4924#endif
4925
4926#ifdef FEAT_DIFF
4927 /*
4928 * "[c" and "]c": move to next or previous diff-change.
4929 */
4930 else if (cap->nchar == 'c')
4931 {
4932 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4933 cap->count1) == FAIL)
4934 clearopbeep(cap->oap);
4935 }
4936#endif
4937
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004938#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004939 /*
4940 * "[s", "[S", "]s" and "]S": move to next spell error.
4941 */
4942 else if (cap->nchar == 's' || cap->nchar == 'S')
4943 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004944 setpcmark();
4945 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004946 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4947 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004948 {
4949 clearopbeep(cap->oap);
4950 break;
4951 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004952 else
4953 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004954# ifdef FEAT_FOLDING
4955 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4956 foldOpenCursor();
4957# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004958 }
4959#endif
4960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 else
4963 clearopbeep(cap->oap);
4964}
4965
4966/*
4967 * Handle Normal mode "%" command.
4968 */
4969 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004970nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
4972 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004973#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 linenr_T lnum = curwin->w_cursor.lnum;
4975#endif
4976
4977 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004978 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 if (cap->count0 > 100)
4981 clearopbeep(cap->oap);
4982 else
4983 {
4984 cap->oap->motion_type = MLINE;
4985 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004986 // Round up, so 'normal 100%' always jumps at the line line.
4987 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4988 // overflow on 32-bits, so use a formula with less accuracy
4989 // to avoid overflows.
4990 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4992 / 100L * cap->count0;
4993 else
4994 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4995 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004996 if (curwin->w_cursor.lnum < 1)
4997 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4999 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5000 beginline(BL_SOL | BL_FIX);
5001 }
5002 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005003 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 cap->oap->motion_type = MCHAR;
5006 cap->oap->use_reg_one = TRUE;
5007 if ((pos = findmatch(cap->oap, NUL)) == NULL)
5008 clearopbeep(cap->oap);
5009 else
5010 {
5011 setpcmark();
5012 curwin->w_cursor = *pos;
5013 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 }
5018#ifdef FEAT_FOLDING
5019 if (cap->oap->op_type == OP_NOP
5020 && lnum != curwin->w_cursor.lnum
5021 && (fdo_flags & FDO_PERCENT)
5022 && KeyTyped)
5023 foldOpenCursor();
5024#endif
5025}
5026
5027/*
5028 * Handle "(" and ")" commands.
5029 * cap->arg is BACKWARD for "(" and FORWARD for ")".
5030 */
5031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005032nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
5034 cap->oap->motion_type = MCHAR;
5035 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005036 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00005037 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 curwin->w_set_curswant = TRUE;
5039
5040 if (findsent(cap->arg, cap->count1) == FAIL)
5041 clearopbeep(cap->oap);
5042 else
5043 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005044 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00005045 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047#ifdef FEAT_FOLDING
5048 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
5049 foldOpenCursor();
5050#endif
5051 }
5052}
5053
5054/*
5055 * "m" command: Mark a position.
5056 */
5057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005058nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
5060 if (!checkclearop(cap->oap))
5061 {
5062 if (setmark(cap->nchar) == FAIL)
5063 clearopbeep(cap->oap);
5064 }
5065}
5066
5067/*
5068 * "{" and "}" commands.
5069 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
5070 */
5071 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005072nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
5074 cap->oap->motion_type = MCHAR;
5075 cap->oap->inclusive = FALSE;
5076 cap->oap->use_reg_one = TRUE;
5077 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00005078 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 clearopbeep(cap->oap);
5080 else
5081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083#ifdef FEAT_FOLDING
5084 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
5085 foldOpenCursor();
5086#endif
5087 }
5088}
5089
5090/*
5091 * "u" command: Undo or make lower case.
5092 */
5093 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005094nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005096 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005098 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 cap->cmdchar = 'g';
5100 cap->nchar = 'u';
5101 nv_operator(cap);
5102 }
5103 else
5104 nv_kundo(cap);
5105}
5106
5107/*
5108 * <Undo> command.
5109 */
5110 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005111nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112{
5113 if (!checkclearopq(cap->oap))
5114 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02005115#ifdef FEAT_JOB_CHANNEL
5116 if (bt_prompt(curbuf))
5117 {
5118 clearopbeep(cap->oap);
5119 return;
5120 }
5121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 u_undo((int)cap->count1);
5123 curwin->w_set_curswant = TRUE;
5124 }
5125}
5126
5127/*
5128 * Handle the "r" command.
5129 */
5130 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005131nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 char_u *ptr;
5134 int had_ctrl_v;
5135 long n;
5136
5137 if (checkclearop(cap->oap))
5138 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02005139#ifdef FEAT_JOB_CHANNEL
5140 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5141 {
5142 clearopbeep(cap->oap);
5143 return;
5144 }
5145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005147 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (cap->nchar == Ctrl_V)
5149 {
5150 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01005151 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005152 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (cap->nchar > DEL)
5154 had_ctrl_v = NUL;
5155 }
5156 else
5157 had_ctrl_v = NUL;
5158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005159 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005160 if (IS_SPECIAL(cap->nchar))
5161 {
5162 clearopbeep(cap->oap);
5163 return;
5164 }
5165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005166 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 if (VIsual_active)
5168 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00005169 if (got_int)
5170 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01005171 if (had_ctrl_v)
5172 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005173 // Use a special (negative) number to make a difference between a
5174 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01005175 if (cap->nchar == CAR)
5176 cap->nchar = REPLACE_CR_NCHAR;
5177 else if (cap->nchar == NL)
5178 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01005179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 nv_operator(cap);
5181 return;
5182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005184 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (virtual_active())
5186 {
5187 if (u_save_cursor() == FAIL)
5188 return;
5189 if (gchar_cursor() == NUL)
5190 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005191 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 coladvance_force((colnr_T)(getviscol() + cap->count1));
5193 curwin->w_cursor.col -= cap->count1;
5194 }
5195 else if (gchar_cursor() == TAB)
5196 coladvance_force(getviscol());
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005199 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005201 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005202 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
5204 clearopbeep(cap->oap);
5205 return;
5206 }
5207
5208 /*
5209 * Replacing with a TAB is done by edit() when it is complicated because
5210 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5211 * Other characters are done below to avoid problems with things like
5212 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5213 */
5214 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5215 {
5216 stuffnumReadbuff(cap->count1);
5217 stuffcharReadbuff('R');
5218 stuffcharReadbuff('\t');
5219 stuffcharReadbuff(ESC);
5220 return;
5221 }
5222
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005223 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (u_save_cursor() == FAIL)
5225 return;
5226
5227 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5228 {
5229 /*
5230 * Replace character(s) by a single newline.
5231 * Strange vi behaviour: Only one newline is inserted.
5232 * Delete the characters here.
5233 * Insert the newline with an insert command, takes care of
5234 * autoindent. The insert command depends on being on the last
5235 * character of a line or not.
5236 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005237 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 stuffcharReadbuff('\r');
5239 stuffcharReadbuff(ESC);
5240
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005241 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 invoke_edit(cap, TRUE, 'r', FALSE);
5243 }
5244 else
5245 {
5246 prep_redo(cap->oap->regname, cap->count1,
5247 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5248
5249 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (has_mbyte)
5251 {
5252 int old_State = State;
5253
5254 if (cap->ncharC1 != 0)
5255 AppendCharToRedobuff(cap->ncharC1);
5256 if (cap->ncharC2 != 0)
5257 AppendCharToRedobuff(cap->ncharC2);
5258
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005259 // This is slow, but it handles replacing a single-byte with a
5260 // multi-byte and the other way around. Also handles adding
5261 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 for (n = cap->count1; n > 0; --n)
5263 {
5264 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005265 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5266 {
5267 int c = ins_copychar(curwin->w_cursor.lnum
5268 + (cap->nchar == Ctrl_Y ? -1 : 1));
5269 if (c != NUL)
5270 ins_char(c);
5271 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005272 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005273 ++curwin->w_cursor.col;
5274 }
5275 else
5276 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 State = old_State;
5278 if (cap->ncharC1 != 0)
5279 ins_char(cap->ncharC1);
5280 if (cap->ncharC2 != 0)
5281 ins_char(cap->ncharC2);
5282 }
5283 }
5284 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 {
5286 /*
5287 * Replace the characters within one line.
5288 */
5289 for (n = cap->count1; n > 0; --n)
5290 {
5291 /*
5292 * Get ptr again, because u_save and/or showmatch() will have
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005293 * released the line. This may also happen in ins_copychar().
5294 * At the same time we let know that the line will be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005296 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5297 {
5298 int c = ins_copychar(curwin->w_cursor.lnum
5299 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005300
5301 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005302 if (c != NUL)
5303 ptr[curwin->w_cursor.col] = c;
5304 }
5305 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005306 {
5307 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005308 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 if (p_sm && msg_silent == 0)
5311 showmatch(cap->nchar);
5312 ++curwin->w_cursor.col;
5313 }
5314#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005315 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005317 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
Bram Moolenaar009b2592004-10-24 19:18:58 +00005319 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005320 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005322 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324#endif
5325
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005326 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 changed_bytes(curwin->w_cursor.lnum,
5328 (colnr_T)(curwin->w_cursor.col - cap->count1));
5329 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005330 --curwin->w_cursor.col; // cursor on the last replaced char
5331 // if the character on the left of the current cursor is a multi-byte
5332 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (has_mbyte)
5334 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 curbuf->b_op_end = curwin->w_cursor;
5336 curwin->w_set_curswant = TRUE;
5337 set_last_insert(cap->nchar);
5338 }
5339}
5340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341/*
5342 * 'o': Exchange start and end of Visual area.
5343 * 'O': same, but in block mode exchange left and right corners.
5344 */
5345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005346v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
5348 pos_T old_cursor;
5349 colnr_T left, right;
5350
5351 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5352 {
5353 old_cursor = curwin->w_cursor;
5354 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5355 curwin->w_cursor.lnum = VIsual.lnum;
5356 coladvance(left);
5357 VIsual = curwin->w_cursor;
5358
5359 curwin->w_cursor.lnum = old_cursor.lnum;
5360 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005361 // 'selection "exclusive" and cursor at right-bottom corner: move it
5362 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5364 ++curwin->w_curswant;
5365 coladvance(curwin->w_curswant);
5366 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005368 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 {
5370 curwin->w_cursor.lnum = VIsual.lnum;
5371 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5372 ++right;
5373 coladvance(right);
5374 VIsual = curwin->w_cursor;
5375
5376 curwin->w_cursor.lnum = old_cursor.lnum;
5377 coladvance(left);
5378 curwin->w_curswant = left;
5379 }
5380 }
5381 else
5382 {
5383 old_cursor = curwin->w_cursor;
5384 curwin->w_cursor = VIsual;
5385 VIsual = old_cursor;
5386 curwin->w_set_curswant = TRUE;
5387 }
5388}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
5390/*
5391 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5392 */
5393 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005394nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005396 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
5398 cap->cmdchar = 'c';
5399 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005400 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 VIsual_mode = 'V';
5402 nv_operator(cap);
5403 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005404 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 {
5406 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005407 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else
5409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 if (virtual_active())
5411 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5413 }
5414 }
5415}
5416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417/*
5418 * "gr".
5419 */
5420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005421nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 if (VIsual_active)
5424 {
5425 cap->cmdchar = 'r';
5426 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005427 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005429 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
5431 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005432 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 else
5434 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005435 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005436 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 stuffcharReadbuff(cap->extra_char);
5438 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 if (virtual_active())
5440 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 invoke_edit(cap, TRUE, 'v', FALSE);
5442 }
5443 }
5444}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
5446/*
5447 * Swap case for "~" command, when it does not work like an operator.
5448 */
5449 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005450n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 long n;
5453 pos_T startpos;
5454 int did_change = 0;
5455#ifdef FEAT_NETBEANS_INTG
5456 pos_T pos;
5457 char_u *ptr;
5458 int count;
5459#endif
5460
5461 if (checkclearopq(cap->oap))
5462 return;
5463
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005464 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 {
5466 clearopbeep(cap->oap);
5467 return;
5468 }
5469
5470 prep_redo_cmd(cap);
5471
5472 if (u_save_cursor() == FAIL)
5473 return;
5474
5475 startpos = curwin->w_cursor;
5476#ifdef FEAT_NETBEANS_INTG
5477 pos = startpos;
5478#endif
5479 for (n = cap->count1; n > 0; --n)
5480 {
5481 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5482 inc_cursor();
5483 if (gchar_cursor() == NUL)
5484 {
5485 if (vim_strchr(p_ww, '~') != NULL
5486 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5487 {
5488#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005489 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
5491 if (did_change)
5492 {
5493 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005494 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005495 netbeans_removed(curbuf, pos.lnum, pos.col,
5496 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005498 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
5500 pos.col = 0;
5501 pos.lnum++;
5502 }
5503#endif
5504 ++curwin->w_cursor.lnum;
5505 curwin->w_cursor.col = 0;
5506 if (n > 1)
5507 {
5508 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5509 break;
5510 u_clearline();
5511 }
5512 }
5513 else
5514 break;
5515 }
5516 }
5517#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005518 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 {
5520 ptr = ml_get(pos.lnum);
5521 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005522 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5523 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 }
5525#endif
5526
5527
5528 check_cursor();
5529 curwin->w_set_curswant = TRUE;
5530 if (did_change)
5531 {
5532 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5533 0L);
5534 curbuf->b_op_start = startpos;
5535 curbuf->b_op_end = curwin->w_cursor;
5536 if (curbuf->b_op_end.col > 0)
5537 --curbuf->b_op_end.col;
5538 }
5539}
5540
5541/*
5542 * Move cursor to mark.
5543 */
5544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005545nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 if (check_mark(pos) == FAIL)
5548 clearop(cap->oap);
5549 else
5550 {
5551 if (cap->cmdchar == '\''
5552 || cap->cmdchar == '`'
5553 || cap->cmdchar == '['
5554 || cap->cmdchar == ']')
5555 setpcmark();
5556 curwin->w_cursor = *pos;
5557 if (flag)
5558 beginline(BL_WHITE | BL_FIX);
5559 else
5560 check_cursor();
5561 }
5562 cap->oap->motion_type = flag ? MLINE : MCHAR;
5563 if (cap->cmdchar == '`')
5564 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005565 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 curwin->w_set_curswant = TRUE;
5567}
5568
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569/*
5570 * Handle commands that are operators in Visual mode.
5571 */
5572 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005573v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
5575 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5576
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005577 // Uppercase means linewise, except in block mode, then "D" deletes till
5578 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 if (isupper(cap->cmdchar))
5580 {
5581 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005582 {
5583 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5587 curwin->w_curswant = MAXCOL;
5588 }
5589 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5590 nv_operator(cap);
5591}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
5593/*
5594 * "s" and "S" commands.
5595 */
5596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005597nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005599#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005600 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005601 if (term_swap_diff() == OK)
5602 return;
5603#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005604#ifdef FEAT_JOB_CHANNEL
5605 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5606 {
5607 clearopbeep(cap->oap);
5608 return;
5609 }
5610#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005611 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 {
5613 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005614 {
5615 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 cap->cmdchar = 'c';
5619 nv_operator(cap);
5620 }
5621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 nv_optrans(cap);
5623}
5624
5625/*
5626 * Abbreviated commands.
5627 */
5628 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005629nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005632 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005634 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 if (VIsual_active)
5636 v_visop(cap);
5637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 nv_optrans(cap);
5639}
5640
5641/*
5642 * Translate a command into another command.
5643 */
5644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005645nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5648 (char_u *)"d$", (char_u *)"c$",
5649 (char_u *)"cl", (char_u *)"cc",
5650 (char_u *)"yy", (char_u *)":s\r"};
5651 static char_u *str = (char_u *)"xXDCsSY&";
5652
5653 if (!checkclearopq(cap->oap))
5654 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005655 // In Vi "2D" doesn't delete the next line. Can't translate it
5656 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005657 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5658 {
5659 cap->oap->start = curwin->w_cursor;
5660 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005661#ifdef FEAT_EVAL
5662 set_op_var(OP_DELETE);
5663#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005664 cap->count1 = 1;
5665 nv_dollar(cap);
5666 finish_op = TRUE;
5667 ResetRedobuff();
5668 AppendCharToRedobuff('D');
5669 }
5670 else
5671 {
5672 if (cap->count0)
5673 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005674 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 cap->opcount = 0;
5678}
5679
5680/*
5681 * "'" and "`" commands. Also for "g'" and "g`".
5682 * cap->arg is TRUE for "'" and "g'".
5683 */
5684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005685nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 pos_T *pos;
5688 int c;
5689#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005690 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005691 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#endif
5693
5694 if (cap->cmdchar == 'g')
5695 c = cap->extra_char;
5696 else
5697 c = cap->nchar;
5698 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005699 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
5701 if (cap->arg)
5702 {
5703 check_cursor_lnum();
5704 beginline(BL_WHITE | BL_FIX);
5705 }
5706 else
5707 check_cursor();
5708 }
5709 else
5710 nv_cursormark(cap, cap->arg, pos);
5711
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005712 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 if (!virtual_active())
5714 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005715 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716#ifdef FEAT_FOLDING
5717 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005718 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005719 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 && (fdo_flags & FDO_MARK)
5721 && old_KeyTyped)
5722 foldOpenCursor();
5723#endif
5724}
5725
5726/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005727 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 */
5729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005730nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005733#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005735 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
5738 if (!checkclearopq(cap->oap))
5739 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005740 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5741 {
5742 if (goto_tabpage_lastused() == FAIL)
5743 clearopbeep(cap->oap);
5744 return;
5745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 if (cap->cmdchar == 'g')
5747 pos = movechangelist((int)cap->count1);
5748 else
5749 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005750 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 curwin->w_set_curswant = TRUE;
5753 check_cursor();
5754 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005755 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 nv_cursormark(cap, FALSE, pos);
5757 else if (cap->cmdchar == 'g')
5758 {
5759 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005760 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005762 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005764 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766 else
5767 clearopbeep(cap->oap);
5768# ifdef FEAT_FOLDING
5769 if (cap->oap->op_type == OP_NOP
5770 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5771 && (fdo_flags & FDO_MARK)
5772 && old_KeyTyped)
5773 foldOpenCursor();
5774# endif
5775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776}
5777
5778/*
5779 * Handle '"' command.
5780 */
5781 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005782nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
5784 if (checkclearop(cap->oap))
5785 return;
5786#ifdef FEAT_EVAL
5787 if (cap->nchar == '=')
5788 cap->nchar = get_expr_register();
5789#endif
5790 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5791 {
5792 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005793 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794#ifdef FEAT_EVAL
5795 set_reg_var(cap->oap->regname);
5796#endif
5797 }
5798 else
5799 clearopbeep(cap->oap);
5800}
5801
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802/*
5803 * Handle "v", "V" and "CTRL-V" commands.
5804 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5805 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005806 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 */
5808 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005809nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005811 if (cap->cmdchar == Ctrl_Q)
5812 cap->cmdchar = Ctrl_V;
5813
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005814 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5815 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (cap->oap->op_type != OP_NOP)
5817 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005818 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005819 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 return;
5821 }
5822
5823 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005824 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005826 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005828 else // toggle char/block mode
5829 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 VIsual_mode = cap->cmdchar;
5831 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005832 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005834 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005836 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
5838 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005839 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005841 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 VIsual = curwin->w_cursor;
5843
5844 VIsual_active = TRUE;
5845 VIsual_reselect = TRUE;
5846 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005847 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005850 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005851 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 /*
5853 * For V and ^V, we multiply the number of lines even if there
5854 * was only one -- webb
5855 */
5856 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5857 {
5858 curwin->w_cursor.lnum +=
5859 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005860 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
5862 VIsual_mode = resel_VIsual_mode;
5863 if (VIsual_mode == 'v')
5864 {
5865 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005866 {
5867 validate_virtcol();
5868 curwin->w_curswant = curwin->w_virtcol
5869 + resel_VIsual_vcol * cap->count0 - 1;
5870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005872 curwin->w_curswant = resel_VIsual_vcol;
5873 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005875 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 {
5877 curwin->w_curswant = MAXCOL;
5878 coladvance((colnr_T)MAXCOL);
5879 }
5880 else if (VIsual_mode == Ctrl_V)
5881 {
5882 validate_virtcol();
5883 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005884 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 coladvance(curwin->w_curswant);
5886 }
5887 else
5888 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005889 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
5891 else
5892 {
5893 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005894 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 may_start_select('c');
5896 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005897 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005898 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005899 if (cap->count0 > 0 && --cap->count1 > 0)
5900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005901 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005902 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5903 nv_right(cap);
5904 else if (VIsual_mode == 'V')
5905 nv_down(cap);
5906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908 }
5909}
5910
5911/*
5912 * Start selection for Shift-movement keys.
5913 */
5914 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005915start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005917 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 may_start_select('k');
5919 n_start_visual_mode('v');
5920}
5921
5922/*
5923 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5924 */
5925 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005926may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927{
5928 VIsual_select = (stuff_empty() && typebuf_typed()
5929 && (vim_strchr(p_slm, c) != NULL));
5930}
5931
5932/*
5933 * Start Visual mode "c".
5934 * Should set VIsual_select before calling this.
5935 */
5936 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005937n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005939#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005940 int cursor_line_was_concealed = curwin->w_p_cole > 0
5941 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005942#endif
5943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 VIsual_mode = c;
5945 VIsual_active = TRUE;
5946 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005947
5948 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005949 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005950 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005951 {
5952 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 VIsual = curwin->w_cursor;
5956
5957#ifdef FEAT_FOLDING
5958 foldAdjustVisual();
5959#endif
5960
Bram Moolenaara0620062021-11-17 16:52:40 +00005961 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005963#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005964 // Check if redraw is needed after changing the state.
5965 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005966#endif
5967
Bram Moolenaar09df3122006-01-23 22:23:09 +00005968 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005969 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005971 // Make sure the clipboard gets updated. Needed because start and
5972 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 clip_star.vmode = NUL;
5974#endif
5975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005976 // Only need to redraw this line, unless still need to redraw an old
5977 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 if (curwin->w_redr_type < INVERTED)
5979 {
5980 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5981 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5982 }
5983}
5984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
5986/*
5987 * CTRL-W: Window commands
5988 */
5989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005990nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005992 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005993 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005994 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005995 cap->cmdchar = ':';
5996 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005997 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005998 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005999 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006000 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001}
6002
6003/*
6004 * CTRL-Z: Suspend
6005 */
6006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006007nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006011 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01006012 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013}
6014
6015/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006016 * "gv": Reselect the previous Visual area. If Visual already active,
6017 * exchange previous and current Visual area.
6018 */
6019 static void
6020nv_gv_cmd(cmdarg_T *cap)
6021{
6022 pos_T tpos;
6023 int i;
6024
6025 if (checkclearop(cap->oap))
6026 return;
6027
6028 if (curbuf->b_visual.vi_start.lnum == 0
6029 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
6030 || curbuf->b_visual.vi_end.lnum == 0)
6031 {
6032 beep_flush();
6033 return;
6034 }
6035
6036 // set w_cursor to the start of the Visual area, tpos to the end
6037 if (VIsual_active)
6038 {
6039 i = VIsual_mode;
6040 VIsual_mode = curbuf->b_visual.vi_mode;
6041 curbuf->b_visual.vi_mode = i;
6042# ifdef FEAT_EVAL
6043 curbuf->b_visual_mode_eval = i;
6044# endif
6045 i = curwin->w_curswant;
6046 curwin->w_curswant = curbuf->b_visual.vi_curswant;
6047 curbuf->b_visual.vi_curswant = i;
6048
6049 tpos = curbuf->b_visual.vi_end;
6050 curbuf->b_visual.vi_end = curwin->w_cursor;
6051 curwin->w_cursor = curbuf->b_visual.vi_start;
6052 curbuf->b_visual.vi_start = VIsual;
6053 }
6054 else
6055 {
6056 VIsual_mode = curbuf->b_visual.vi_mode;
6057 curwin->w_curswant = curbuf->b_visual.vi_curswant;
6058 tpos = curbuf->b_visual.vi_end;
6059 curwin->w_cursor = curbuf->b_visual.vi_start;
6060 }
6061
6062 VIsual_active = TRUE;
6063 VIsual_reselect = TRUE;
6064
6065 // Set Visual to the start and w_cursor to the end of the Visual
6066 // area. Make sure they are on an existing character.
6067 check_cursor();
6068 VIsual = curwin->w_cursor;
6069 curwin->w_cursor = tpos;
6070 check_cursor();
6071 update_topline();
6072
6073 // When called from normal "g" command: start Select mode when
6074 // 'selectmode' contains "cmd". When called for K_SELECT, always
6075 // start Select mode.
6076 if (cap->arg)
6077 {
6078 VIsual_select = TRUE;
6079 VIsual_select_reg = 0;
6080 }
6081 else
6082 may_start_select('c');
6083 setmouse();
6084#ifdef FEAT_CLIPBOARD
6085 // Make sure the clipboard gets updated. Needed because start and
6086 // end are still the same, and the selection needs to be owned
6087 clip_star.vmode = NUL;
6088#endif
6089 redraw_curbuf_later(INVERTED);
6090 showmode();
6091}
6092
6093/*
6094 * "g0", "g^" : Like "0" and "^" but for screen lines.
6095 * "gm": middle of "g0" and "g$".
6096 */
6097 static void
6098nv_g_home_m_cmd(cmdarg_T *cap)
6099{
6100 int i;
6101 int flag = FALSE;
6102
6103 if (cap->nchar == '^')
6104 flag = TRUE;
6105
6106 cap->oap->motion_type = MCHAR;
6107 cap->oap->inclusive = FALSE;
6108 if (curwin->w_p_wrap && curwin->w_width != 0)
6109 {
6110 int width1 = curwin->w_width - curwin_col_off();
6111 int width2 = width1 + curwin_col_off2();
6112
6113 validate_virtcol();
6114 i = 0;
6115 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6116 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6117 }
6118 else
6119 i = curwin->w_leftcol;
6120 // Go to the middle of the screen line. When 'number' or
6121 // 'relativenumber' is on and lines are wrapping the middle can be more
6122 // to the left.
6123 if (cap->nchar == 'm')
6124 i += (curwin->w_width - curwin_col_off()
6125 + ((curwin->w_p_wrap && i > 0)
6126 ? curwin_col_off2() : 0)) / 2;
6127 coladvance((colnr_T)i);
6128 if (flag)
6129 {
6130 do
6131 i = gchar_cursor();
6132 while (VIM_ISWHITE(i) && oneright() == OK);
6133 curwin->w_valid &= ~VALID_WCOL;
6134 }
6135 curwin->w_set_curswant = TRUE;
6136}
6137
6138/*
6139 * "g_": to the last non-blank character in the line or <count> lines
6140 * downward.
6141 */
6142 static void
6143nv_g_underscore_cmd(cmdarg_T *cap)
6144{
6145 char_u *ptr;
6146
6147 cap->oap->motion_type = MCHAR;
6148 cap->oap->inclusive = TRUE;
6149 curwin->w_curswant = MAXCOL;
6150 if (cursor_down((long)(cap->count1 - 1),
6151 cap->oap->op_type == OP_NOP) == FAIL)
6152 {
6153 clearopbeep(cap->oap);
6154 return;
6155 }
6156
6157 ptr = ml_get_curline();
6158
6159 // In Visual mode we may end up after the line.
6160 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6161 --curwin->w_cursor.col;
6162
6163 // Decrease the cursor column until it's on a non-blank.
6164 while (curwin->w_cursor.col > 0
6165 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
6166 --curwin->w_cursor.col;
6167 curwin->w_set_curswant = TRUE;
6168 adjust_for_sel(cap);
6169}
6170
6171/*
6172 * "g$" : Like "$" but for screen lines.
6173 */
6174 static void
6175nv_g_dollar_cmd(cmdarg_T *cap)
6176{
6177 oparg_T *oap = cap->oap;
6178 int i;
6179 int col_off = curwin_col_off();
6180
6181 oap->motion_type = MCHAR;
6182 oap->inclusive = TRUE;
6183 if (curwin->w_p_wrap && curwin->w_width != 0)
6184 {
6185 curwin->w_curswant = MAXCOL; // so we stay at the end
6186 if (cap->count1 == 1)
6187 {
6188 int width1 = curwin->w_width - col_off;
6189 int width2 = width1 + curwin_col_off2();
6190
6191 validate_virtcol();
6192 i = width1 - 1;
6193 if (curwin->w_virtcol >= (colnr_T)width1)
6194 i += ((curwin->w_virtcol - width1) / width2 + 1)
6195 * width2;
6196 coladvance((colnr_T)i);
6197
6198 // Make sure we stick in this column.
6199 validate_virtcol();
6200 curwin->w_curswant = curwin->w_virtcol;
6201 curwin->w_set_curswant = FALSE;
6202 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6203 {
6204 // Check for landing on a character that got split at
6205 // the end of the line. We do not want to advance to
6206 // the next screen line.
6207 if (curwin->w_virtcol > (colnr_T)i)
6208 --curwin->w_cursor.col;
6209 }
6210 }
6211 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6212 clearopbeep(oap);
6213 }
6214 else
6215 {
6216 if (cap->count1 > 1)
6217 // if it fails, let the cursor still move to the last char
6218 (void)cursor_down(cap->count1 - 1, FALSE);
6219
6220 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
6221 coladvance((colnr_T)i);
6222
6223 // if the character doesn't fit move one back
6224 if (curwin->w_cursor.col > 0
6225 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6226 {
6227 colnr_T vcol;
6228
6229 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6230 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6231 --curwin->w_cursor.col;
6232 }
6233
6234 // Make sure we stick in this column.
6235 validate_virtcol();
6236 curwin->w_curswant = curwin->w_virtcol;
6237 curwin->w_set_curswant = FALSE;
6238 }
6239}
6240
6241/*
6242 * "gi": start Insert at the last position.
6243 */
6244 static void
6245nv_gi_cmd(cmdarg_T *cap)
6246{
6247 int i;
6248
6249 if (curbuf->b_last_insert.lnum != 0)
6250 {
6251 curwin->w_cursor = curbuf->b_last_insert;
6252 check_cursor_lnum();
6253 i = (int)STRLEN(ml_get_curline());
6254 if (curwin->w_cursor.col > (colnr_T)i)
6255 {
6256 if (virtual_active())
6257 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
6258 curwin->w_cursor.col = i;
6259 }
6260 }
6261 cap->cmdchar = 'i';
6262 nv_edit(cap);
6263}
6264
6265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 * Commands starting with "g".
6267 */
6268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006269nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
6271 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274 switch (cap->nchar)
6275 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006276 case Ctrl_A:
6277 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006279 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006280 if (!VIsual_active && cap->nchar == Ctrl_A)
6281 vim_mem_profile_dump();
6282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006284 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006285 if (VIsual_active)
6286 {
6287 cap->arg = TRUE;
6288 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006289 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006290 nv_addsub(cap);
6291 }
6292 else
6293 clearopbeep(oap);
6294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006296 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 case 'R':
6298 cap->arg = TRUE;
6299 nv_Replace(cap);
6300 break;
6301
6302 case 'r':
6303 nv_vreplace(cap);
6304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
6306 case '&':
6307 do_cmdline_cmd((char_u *)"%s//~/&");
6308 break;
6309
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006310 // "gv": Reselect the previous Visual area. If Visual already active,
6311 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006313 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006315
6316 // "gV": Don't reselect the previous Visual area after a Select mode
6317 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 case 'V':
6319 VIsual_reselect = FALSE;
6320 break;
6321
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006322 // "gh": start Select mode.
6323 // "gH": start Select line mode.
6324 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 case K_BS:
6326 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006327 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 case 'h':
6329 case 'H':
6330 case Ctrl_H:
6331# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006332 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 if (cap->nchar == Ctrl_H)
6334 cap->cmdchar = Ctrl_V;
6335 else
6336# endif
6337 cap->cmdchar = cap->nchar + ('v' - 'h');
6338 cap->arg = TRUE;
6339 nv_visual(cap);
6340 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006341
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006342 // "gn", "gN" visually select next/previous search match
6343 // "gn" selects next match
6344 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006345 case 'N':
6346 case 'n':
6347 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006348 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006351 // "gj" and "gk" two new funny movement keys -- up and down
6352 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 case 'j':
6354 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006355 // with 'nowrap' it works just like the normal "j" command.
6356 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 {
6358 oap->motion_type = MLINE;
6359 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6360 }
6361 else
6362 i = nv_screengo(oap, FORWARD, cap->count1);
6363 if (i == FAIL)
6364 clearopbeep(oap);
6365 break;
6366
6367 case 'k':
6368 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006369 // with 'nowrap' it works just like the normal "k" command.
6370 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 {
6372 oap->motion_type = MLINE;
6373 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6374 }
6375 else
6376 i = nv_screengo(oap, BACKWARD, cap->count1);
6377 if (i == FAIL)
6378 clearopbeep(oap);
6379 break;
6380
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006381 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 case 'J':
6383 nv_join(cap);
6384 break;
6385
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006386 // "g0", "g^" : Like "0" and "^" but for screen lines.
6387 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 case '0':
6390 case 'm':
6391 case K_HOME:
6392 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006393 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 break;
6395
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006396 case 'M':
6397 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006398 oap->motion_type = MCHAR;
6399 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006400 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006401 if (cap->count0 > 0 && cap->count0 <= 100)
6402 coladvance((colnr_T)(i * cap->count0 / 100));
6403 else
6404 coladvance((colnr_T)(i / 2));
6405 curwin->w_set_curswant = TRUE;
6406 }
6407 break;
6408
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006409 // "g_": to the last non-blank character in the line or <count> lines
6410 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006412 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 break;
6414
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006415 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 case '$':
6417 case K_END:
6418 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006419 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 break;
6421
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006422 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 case '*':
6424 case '#':
6425#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006426 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006428 case Ctrl_RSB: // :tag or :tselect for current identifier
6429 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 nv_ident(cap);
6431 break;
6432
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006433 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 case 'e':
6435 case 'E':
6436 oap->motion_type = MCHAR;
6437 curwin->w_set_curswant = TRUE;
6438 oap->inclusive = TRUE;
6439 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6440 clearopbeep(oap);
6441 break;
6442
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006443 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006445 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 break;
6447
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006448 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006450 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 break;
6452
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006453 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 case 'I':
6455 beginline(0);
6456 if (!checkclearopq(oap))
6457 invoke_edit(cap, FALSE, 'g', FALSE);
6458 break;
6459
6460#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006461 // "gf": goto file, edit file under cursor
6462 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006464 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 nv_gotofile(cap);
6466 break;
6467#endif
6468
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006469 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 case '\'':
6471 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006472 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 case '`':
6474 nv_gomark(cap);
6475 break;
6476
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006477 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006479 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 break;
6481
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006482 // "ga": Display the ascii value of the character under the
6483 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 case 'a':
6485 do_ascii(NULL);
6486 break;
6487
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006488 // "g8": Display the bytes used for the UTF-8 character under the
6489 // cursor. It is displayed in hex.
6490 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006492 if (cap->count0 == 8)
6493 utf_find_illegal();
6494 else
6495 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006498 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006499 case '<':
6500 show_sb_text();
6501 break;
6502
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006503 // "gg": Goto the first line in file. With a count it goes to
6504 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 case 'g':
6506 cap->arg = FALSE;
6507 nv_goto(cap);
6508 break;
6509
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006510 // Two-character operators:
6511 // "gq" Format text
6512 // "gw" Format text and keep cursor position
6513 // "g~" Toggle the case of the text.
6514 // "gu" Change text to lower case.
6515 // "gU" Change text to upper case.
6516 // "g?" rot13 encoding
6517 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 case 'q':
6519 case 'w':
6520 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006521 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 case '~':
6523 case 'u':
6524 case 'U':
6525 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006526 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 nv_operator(cap);
6528 break;
6529
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006530 // "gd": Find first occurrence of pattern under the cursor in the
6531 // current function
6532 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 case 'd':
6534 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006535 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 break;
6537
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006538 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 case K_MIDDLEMOUSE:
6540 case K_MIDDLEDRAG:
6541 case K_MIDDLERELEASE:
6542 case K_LEFTMOUSE:
6543 case K_LEFTDRAG:
6544 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006545 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 case K_RIGHTMOUSE:
6547 case K_RIGHTDRAG:
6548 case K_RIGHTRELEASE:
6549 case K_X1MOUSE:
6550 case K_X1DRAG:
6551 case K_X1RELEASE:
6552 case K_X2MOUSE:
6553 case K_X2DRAG:
6554 case K_X2RELEASE:
6555 mod_mask = MOD_MASK_CTRL;
6556 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6557 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558
6559 case K_IGNORE:
6560 break;
6561
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006562 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 case 'p':
6564 case 'P':
6565 nv_put(cap);
6566 break;
6567
6568#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006569 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 case 'o':
6571 goto_byte(cap->count0);
6572 break;
6573#endif
6574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006575 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006577 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 {
6579 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006580 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 break;
6582 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006583
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 if (!checkclearopq(oap))
6585 do_exmode(TRUE);
6586 break;
6587
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 case ',':
6589 nv_pcmark(cap);
6590 break;
6591
6592 case ';':
6593 cap->count1 = -cap->count1;
6594 nv_pcmark(cap);
6595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006597 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006598 if (!checkclearop(oap))
6599 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006600 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006601 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006602 if (!checkclearop(oap))
6603 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006604 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006605
Bram Moolenaar62a23252020-08-09 14:04:42 +02006606 case TAB:
6607 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6608 clearopbeep(oap);
6609 break;
6610
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006611 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006612 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006613 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006614 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006615 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006616 break;
6617
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 default:
6619 clearopbeep(oap);
6620 break;
6621 }
6622}
6623
6624/*
6625 * Handle "o" and "O" commands.
6626 */
6627 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006628n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006630#ifdef FEAT_CONCEAL
6631 linenr_T oldline = curwin->w_cursor.lnum;
6632#endif
6633
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 if (!checkclearopq(cap->oap))
6635 {
6636#ifdef FEAT_FOLDING
6637 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006638 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 (void)hasFolding(curwin->w_cursor.lnum,
6640 &curwin->w_cursor.lnum, NULL);
6641 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006642 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 (void)hasFolding(curwin->w_cursor.lnum,
6644 NULL, &curwin->w_cursor.lnum);
6645#endif
6646 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6647 (cap->cmdchar == 'O' ? 1 : 0)),
6648 (linenr_T)(curwin->w_cursor.lnum +
6649 (cap->cmdchar == 'o' ? 1 : 0))
6650 ) == OK
6651 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006652 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006653 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006655#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006656 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006657 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006658#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006659#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006660 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006661 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006662 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006663#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006664 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006665 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6666 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6668 }
6669 }
6670}
6671
6672/*
6673 * "." command: redo last change.
6674 */
6675 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006676nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 if (!checkclearopq(cap->oap))
6679 {
6680 /*
6681 * If "restart_edit" is TRUE, the last but one command is repeated
6682 * instead of the last command (inserting text). This is used for
6683 * CTRL-O <.> in insert mode.
6684 */
6685 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6686 clearopbeep(cap->oap);
6687 }
6688}
6689
6690/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006691 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 */
6693 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006694nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006696 if (VIsual_select && VIsual_active)
6697 {
6698 int reg;
6699 // Get register name
6700 ++no_mapping;
6701 ++allow_keys;
6702 reg = plain_vgetc();
6703 LANGMAP_ADJUST(reg, TRUE);
6704 --no_mapping;
6705 --allow_keys;
6706
6707 if (reg == '"')
6708 // the unnamed register is 0
6709 reg = 0;
6710
6711 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
6712 return;
6713 }
6714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (!checkclearopq(cap->oap))
6716 {
6717 u_redo((int)cap->count1);
6718 curwin->w_set_curswant = TRUE;
6719 }
6720}
6721
6722/*
6723 * Handle "U" command.
6724 */
6725 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006726nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006728 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006729 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006731 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 cap->cmdchar = 'g';
6733 cap->nchar = 'U';
6734 nv_operator(cap);
6735 }
6736 else if (!checkclearopq(cap->oap))
6737 {
6738 u_undoline();
6739 curwin->w_set_curswant = TRUE;
6740 }
6741}
6742
6743/*
6744 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6745 * single character.
6746 */
6747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006748nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006750 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006751 {
6752#ifdef FEAT_JOB_CHANNEL
6753 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6754 {
6755 clearopbeep(cap->oap);
6756 return;
6757 }
6758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 else
6762 nv_operator(cap);
6763}
6764
6765/*
6766 * Handle an operator command.
6767 * The actual work is done by do_pending_operator().
6768 */
6769 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006770nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771{
6772 int op_type;
6773
6774 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006775#ifdef FEAT_JOB_CHANNEL
6776 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6777 {
6778 clearopbeep(cap->oap);
6779 return;
6780 }
6781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006783 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 nv_lineop(cap);
6785 else if (!checkclearop(cap->oap))
6786 {
6787 cap->oap->start = curwin->w_cursor;
6788 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006789#ifdef FEAT_EVAL
6790 set_op_var(op_type);
6791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 }
6793}
6794
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006795#ifdef FEAT_EVAL
6796/*
6797 * Set v:operator to the characters for "optype".
6798 */
6799 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006800set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006801{
6802 char_u opchars[3];
6803
6804 if (optype == OP_NOP)
6805 set_vim_var_string(VV_OP, NULL, 0);
6806 else
6807 {
6808 opchars[0] = get_op_char(optype);
6809 opchars[1] = get_extra_op_char(optype);
6810 opchars[2] = NUL;
6811 set_vim_var_string(VV_OP, opchars, -1);
6812 }
6813}
6814#endif
6815
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816/*
6817 * Handle linewise operator "dd", "yy", etc.
6818 *
6819 * "_" is is a strange motion command that helps make operators more logical.
6820 * It is actually implemented, but not documented in the real Vi. This motion
6821 * command actually refers to "the current line". Commands like "dd" and "yy"
6822 * are really an alternate form of "d_" and "y_". It does accept a count, so
6823 * "d3_" works to delete 3 lines.
6824 */
6825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006826nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
6828 cap->oap->motion_type = MLINE;
6829 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6830 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006831 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006832 && cap->oap->motion_force != 'v'
6833 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 || cap->oap->op_type == OP_LSHIFT
6835 || cap->oap->op_type == OP_RSHIFT)
6836 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006837 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 beginline(BL_WHITE | BL_FIX);
6839}
6840
6841/*
6842 * <Home> command.
6843 */
6844 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006845nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006847 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006848 if (mod_mask & MOD_MASK_CTRL)
6849 nv_goto(cap);
6850 else
6851 {
6852 cap->count0 = 1;
6853 nv_pipe(cap);
6854 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006855 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6856 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857}
6858
6859/*
6860 * "|" command.
6861 */
6862 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006863nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864{
6865 cap->oap->motion_type = MCHAR;
6866 cap->oap->inclusive = FALSE;
6867 beginline(0);
6868 if (cap->count0 > 0)
6869 {
6870 coladvance((colnr_T)(cap->count0 - 1));
6871 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6872 }
6873 else
6874 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006875 // keep curswant at the column where we wanted to go, not where
6876 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 curwin->w_set_curswant = FALSE;
6878}
6879
6880/*
6881 * Handle back-word command "b" and "B".
6882 * cap->arg is 1 for "B"
6883 */
6884 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006885nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886{
6887 cap->oap->motion_type = MCHAR;
6888 cap->oap->inclusive = FALSE;
6889 curwin->w_set_curswant = TRUE;
6890 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6891 clearopbeep(cap->oap);
6892#ifdef FEAT_FOLDING
6893 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6894 foldOpenCursor();
6895#endif
6896}
6897
6898/*
6899 * Handle word motion commands "e", "E", "w" and "W".
6900 * cap->arg is TRUE for "E" and "W".
6901 */
6902 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006903nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
6905 int n;
6906 int word_end;
6907 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006908 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909
6910 /*
6911 * Set inclusive for the "E" and "e" command.
6912 */
6913 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6914 word_end = TRUE;
6915 else
6916 word_end = FALSE;
6917 cap->oap->inclusive = word_end;
6918
6919 /*
6920 * "cw" and "cW" are a special case.
6921 */
6922 if (!word_end && cap->oap->op_type == OP_CHANGE)
6923 {
6924 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006925 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006927 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
6929 /*
6930 * Reproduce a funny Vi behaviour: "cw" on a blank only
6931 * changes one character, not all blanks until the start of
6932 * the next word. Only do this when the 'w' flag is included
6933 * in 'cpoptions'.
6934 */
6935 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6936 {
6937 cap->oap->inclusive = TRUE;
6938 cap->oap->motion_type = MCHAR;
6939 return;
6940 }
6941 }
6942 else
6943 {
6944 /*
6945 * This is a little strange. To match what the real Vi does,
6946 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6947 * that we are not on a space or a TAB. This seems impolite
6948 * at first, but it's really more what we mean when we say
6949 * 'cw'.
6950 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006951 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 * will change only one character! This is done by setting
6953 * flag.
6954 */
6955 cap->oap->inclusive = TRUE;
6956 word_end = TRUE;
6957 flag = TRUE;
6958 }
6959 }
6960 }
6961
6962 cap->oap->motion_type = MCHAR;
6963 curwin->w_set_curswant = TRUE;
6964 if (word_end)
6965 n = end_word(cap->count1, cap->arg, flag, FALSE);
6966 else
6967 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006969 // Don't leave the cursor on the NUL past the end of line. Unless we
6970 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006971 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006972 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
6974 if (n == FAIL && cap->oap->op_type == OP_NOP)
6975 clearopbeep(cap->oap);
6976 else
6977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979#ifdef FEAT_FOLDING
6980 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6981 foldOpenCursor();
6982#endif
6983 }
6984}
6985
6986/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006987 * Used after a movement command: If the cursor ends up on the NUL after the
6988 * end of the line, may move it back to the last character and make the motion
6989 * inclusive.
6990 */
6991 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006992adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006993{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006994 // The cursor cannot remain on the NUL when:
6995 // - the column is > 0
6996 // - not in Visual mode or 'selection' is "o"
6997 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006998 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006999 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02007000 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007001 {
7002 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007003 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007004 if (has_mbyte)
7005 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007006 oap->inclusive = TRUE;
7007 }
7008}
7009
7010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 * "0" and "^" commands.
7012 * cap->arg is the argument for beginline().
7013 */
7014 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007015nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 cap->oap->motion_type = MCHAR;
7018 cap->oap->inclusive = FALSE;
7019 beginline(cap->arg);
7020#ifdef FEAT_FOLDING
7021 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7022 foldOpenCursor();
7023#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007024 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
7025 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026}
7027
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028/*
7029 * In exclusive Visual mode, may include the last character.
7030 */
7031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007032adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007035 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 if (has_mbyte)
7038 inc_cursor();
7039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 ++curwin->w_cursor.col;
7041 cap->oap->inclusive = FALSE;
7042 }
7043}
7044
7045/*
7046 * Exclude last character at end of Visual area for 'selection' == "exclusive".
7047 * Should check VIsual_mode before calling this.
7048 * Returns TRUE when backed up to the previous line.
7049 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02007050 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007051unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 pos_T *pp;
7054
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007055 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007057 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 pp = &curwin->w_cursor;
7059 else
7060 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 if (pp->coladd > 0)
7062 --pp->coladd;
7063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 if (pp->col > 0)
7065 {
7066 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02007067 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 }
7069 else if (pp->lnum > 1)
7070 {
7071 --pp->lnum;
7072 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
7073 return TRUE;
7074 }
7075 }
7076 return FALSE;
7077}
7078
7079/*
7080 * SELECT key in Normal or Visual mode: end of Select mode mapping.
7081 */
7082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007083nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
7085 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00007086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00007088 VIsual_select_reg = 0;
7089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 else if (VIsual_reselect)
7091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007092 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 cap->arg = TRUE;
7094 nv_g_cmd(cap);
7095 }
7096}
7097
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
7099/*
7100 * "G", "gg", CTRL-END, CTRL-HOME.
7101 * cap->arg is TRUE for "G".
7102 */
7103 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007104nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 linenr_T lnum;
7107
7108 if (cap->arg)
7109 lnum = curbuf->b_ml.ml_line_count;
7110 else
7111 lnum = 1L;
7112 cap->oap->motion_type = MLINE;
7113 setpcmark();
7114
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007115 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 if (cap->count0 != 0)
7117 lnum = cap->count0;
7118 if (lnum < 1L)
7119 lnum = 1L;
7120 else if (lnum > curbuf->b_ml.ml_line_count)
7121 lnum = curbuf->b_ml.ml_line_count;
7122 curwin->w_cursor.lnum = lnum;
7123 beginline(BL_SOL | BL_FIX);
7124#ifdef FEAT_FOLDING
7125 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
7126 foldOpenCursor();
7127#endif
7128}
7129
7130/*
7131 * CTRL-\ in Normal mode.
7132 */
7133 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007134nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
7136 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
7137 {
7138 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00007139 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007140 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 restart_edit = 0;
7142#ifdef FEAT_CMDWIN
7143 if (cmdwin_type != 0)
7144 cmdwin_result = Ctrl_C;
7145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (VIsual_active)
7147 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007148 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 redraw_curbuf_later(INVERTED);
7150 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007151 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 if (cap->nchar == Ctrl_G && p_im)
7153 restart_edit = 'a';
7154 }
7155 else
7156 clearopbeep(cap->oap);
7157}
7158
7159/*
7160 * ESC in Normal mode: beep, but don't flush buffers.
7161 * Don't even beep if we are canceling a command.
7162 */
7163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007164nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 int no_reason;
7167
7168 no_reason = (cap->oap->op_type == OP_NOP
7169 && cap->opcount == 0
7170 && cap->count0 == 0
7171 && cap->oap->regname == 0
7172 && !p_im);
7173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007174 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
7176 if (restart_edit == 0
7177#ifdef FEAT_CMDWIN
7178 && cmdwin_type == 0
7179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007182 {
7183 if (anyBufIsChanged())
7184 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
7185 else
7186 msg(_("Type :qa and press <Enter> to exit Vim"));
7187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007189 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
7190 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 if (!p_im)
7192 restart_edit = 0;
7193#ifdef FEAT_CMDWIN
7194 if (cmdwin_type != 0)
7195 {
7196 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007197 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 return;
7199 }
7200#endif
7201 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01007202#ifdef FEAT_CMDWIN
7203 else if (cmdwin_type != 0 && ex_normal_busy)
7204 {
7205 // When :normal runs out of characters while in the command line window
7206 // vgetorpeek() will return ESC. Exit the cmdline window to break the
7207 // loop.
7208 cmdwin_result = K_IGNORE;
7209 return;
7210 }
7211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 if (VIsual_active)
7214 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007215 end_visual_mode(); // stop Visual
7216 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 curwin->w_set_curswant = TRUE;
7218 redraw_curbuf_later(INVERTED);
7219 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007220 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007221 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 clearop(cap->oap);
7223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007224 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7225 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007226 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 restart_edit = 'a';
7228}
7229
7230/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007231 * Move the cursor for the "A" command.
7232 */
7233 void
7234set_cursor_for_append_to_line(void)
7235{
7236 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007237 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007238 {
7239 int save_State = State;
7240
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007241 // Pretend Insert mode here to allow the cursor on the
7242 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007243 State = INSERT;
7244 coladvance((colnr_T)MAXCOL);
7245 State = save_State;
7246 }
7247 else
7248 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7249}
7250
7251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007253 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 */
7255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007256nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007258 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7260 cap->cmdchar = 'i';
7261
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007262 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007264 {
7265#ifdef FEAT_TERMINAL
7266 if (term_in_normal_mode())
7267 {
7268 end_visual_mode();
7269 clearop(cap->oap);
7270 term_enter_job_mode();
7271 return;
7272 }
7273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007277 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007278 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7279 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 {
7281#ifdef FEAT_TEXTOBJ
7282 nv_object(cap);
7283#else
7284 clearopbeep(cap->oap);
7285#endif
7286 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007287#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007288 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007289 {
7290 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007291 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007292 return;
7293 }
7294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 else if (!curbuf->b_p_ma && !p_im)
7296 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007297 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007298 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007300 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007301 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007302 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007304 else if (cap->cmdchar == K_PS && VIsual_active)
7305 {
7306 pos_T old_pos = curwin->w_cursor;
7307 pos_T old_visual = VIsual;
7308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007309 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007310 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7311 {
7312 shift_delete_registers();
7313 cap->oap->regname = '1';
7314 }
7315 else
7316 cap->oap->regname = '-';
7317 cap->cmdchar = 'd';
7318 cap->nchar = NUL;
7319 nv_operator(cap);
7320 do_pending_operator(cap, 0, FALSE);
7321 cap->cmdchar = K_PS;
7322
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007323 // When the last char in the line was deleted then append. Detect this
7324 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007325 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7326 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007327 inc_cursor();
7328
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007329 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007330 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 else if (!checkclearopq(cap->oap))
7333 {
7334 switch (cap->cmdchar)
7335 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007336 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007337 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 break;
7339
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007340 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007341 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7342 beginline(BL_WHITE);
7343 else
7344 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 break;
7346
Bram Moolenaara1891842017-02-04 21:34:31 +01007347 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007348 // Bracketed paste works like "a"ppend, unless the cursor is in
7349 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007350 if (curwin->w_cursor.col == 0)
7351 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007352 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007353
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007354 case 'a': // "a"ppend is like "i"nsert on the next character.
7355 // increment coladd when in virtual space, increment the
7356 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 if (virtual_active()
7358 && (curwin->w_cursor.coladd > 0
7359 || *ml_get_cursor() == NUL
7360 || *ml_get_cursor() == TAB))
7361 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007362 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 inc_cursor();
7364 break;
7365 }
7366
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7368 {
7369 int save_State = State;
7370
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007371 // Pretend Insert mode here to allow the cursor on the
7372 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 State = INSERT;
7374 coladvance(getviscol());
7375 State = save_State;
7376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
7378 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7379 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007380 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007381 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007382 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383}
7384
7385/*
7386 * Invoke edit() and take care of "restart_edit" and the return value.
7387 */
7388 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007389invoke_edit(
7390 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007391 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007392 int cmd,
7393 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 int restart_edit_save = 0;
7396
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007397 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7398 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7399 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 if (repl || !stuff_empty())
7401 restart_edit_save = restart_edit;
7402 else
7403 restart_edit_save = 0;
7404
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007405 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 restart_edit = 0;
7407
7408 if (edit(cmd, startln, cap->count1))
7409 cap->retval |= CA_COMMAND_BUSY;
7410
7411 if (restart_edit == 0)
7412 restart_edit = restart_edit_save;
7413}
7414
7415#ifdef FEAT_TEXTOBJ
7416/*
7417 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7418 */
7419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007420nv_object(
7421 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
7423 int flag;
7424 int include;
7425 char_u *mps_save;
7426
7427 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007428 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007430 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007432 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 mps_save = curbuf->b_p_mps;
7434 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7435
7436 switch (cap->nchar)
7437 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007438 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 flag = current_word(cap->oap, cap->count1, include, FALSE);
7440 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007441 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 flag = current_word(cap->oap, cap->count1, include, TRUE);
7443 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007444 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 case '(':
7446 case ')':
7447 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7448 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007449 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 case '{':
7451 case '}':
7452 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7453 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007454 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 case ']':
7456 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7457 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007458 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 case '>':
7460 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7461 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007462 case 't': // "at" = a tag block (xml and html)
7463 // Do not adjust oap->end in do_pending_operator()
7464 // otherwise there are different results for 'dit'
7465 // (note leading whitespace in last line):
7466 // 1) <b> 2) <b>
7467 // foobar foobar
7468 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007469 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007470 flag = current_tagblock(cap->oap, cap->count1, include);
7471 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007472 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 flag = current_par(cap->oap, cap->count1, include, 'p');
7474 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007475 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 flag = current_sent(cap->oap, cap->count1, include);
7477 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007478 case '"': // "a"" = a double quoted string
7479 case '\'': // "a'" = a single quoted string
7480 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007481 flag = current_quote(cap->oap, cap->count1, include,
7482 cap->nchar);
7483 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007484#if 0 // TODO
7485 case 'S': // "aS" = a section
7486 case 'f': // "af" = a filename
7487 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488#endif
7489 default:
7490 flag = FAIL;
7491 break;
7492 }
7493
7494 curbuf->b_p_mps = mps_save;
7495 if (flag == FAIL)
7496 clearopbeep(cap->oap);
7497 adjust_cursor_col();
7498 curwin->w_set_curswant = TRUE;
7499}
7500#endif
7501
7502/*
7503 * "q" command: Start/stop recording.
7504 * "q:", "q/", "q?": edit command-line in command-line window.
7505 */
7506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007507nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508{
7509 if (cap->oap->op_type == OP_FORMAT)
7510 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007511 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 cap->cmdchar = 'g';
7513 cap->nchar = 'q';
7514 nv_operator(cap);
7515 }
7516 else if (!checkclearop(cap->oap))
7517 {
7518#ifdef FEAT_CMDWIN
7519 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7520 {
7521 stuffcharReadbuff(cap->nchar);
7522 stuffcharReadbuff(K_CMDWIN);
7523 }
7524 else
7525#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007526 // (stop) recording into a named register, unless executing a
7527 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007528 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 clearopbeep(cap->oap);
7530 }
7531}
7532
7533/*
7534 * Handle the "@r" command.
7535 */
7536 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007537nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
7539 if (checkclearop(cap->oap))
7540 return;
7541#ifdef FEAT_EVAL
7542 if (cap->nchar == '=')
7543 {
7544 if (get_expr_register() == NUL)
7545 return;
7546 }
7547#endif
7548 while (cap->count1-- && !got_int)
7549 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007550 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 {
7552 clearopbeep(cap->oap);
7553 break;
7554 }
7555 line_breakcheck();
7556 }
7557}
7558
7559/*
7560 * Handle the CTRL-U and CTRL-D commands.
7561 */
7562 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007563nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564{
7565 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7566 || (cap->cmdchar == Ctrl_D
7567 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7568 clearopbeep(cap->oap);
7569 else if (!checkclearop(cap->oap))
7570 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7571}
7572
7573/*
7574 * Handle "J" or "gJ" command.
7575 */
7576 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007577nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007579 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007581 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 {
7583 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007584 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7586 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007588 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007589 if (cap->count0 <= 2)
7590 {
7591 clearopbeep(cap->oap);
7592 return;
7593 }
7594 cap->count0 = curbuf->b_ml.ml_line_count
7595 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007597
7598 prep_redo(cap->oap->regname, cap->count0,
7599 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7600 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 }
7602}
7603
7604/*
7605 * "P", "gP", "p" and "gp" commands.
7606 */
7607 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007608nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007610 nv_put_opt(cap, FALSE);
7611}
7612
7613/*
7614 * "P", "gP", "p" and "gp" commands.
7615 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7616 */
7617 static void
7618nv_put_opt(cmdarg_T *cap, int fix_indent)
7619{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 int regname = 0;
7621 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007622 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007623 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 int dir;
7625 int flags = 0;
7626
7627 if (cap->oap->op_type != OP_NOP)
7628 {
7629#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007630 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7632 {
7633 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007634 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
7636 else
7637#endif
7638 clearopbeep(cap->oap);
7639 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007640#ifdef FEAT_JOB_CHANNEL
7641 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7642 {
7643 clearopbeep(cap->oap);
7644 }
7645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 else
7647 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007648 if (fix_indent)
7649 {
7650 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7651 ? FORWARD : BACKWARD;
7652 flags |= PUT_FIXINDENT;
7653 }
7654 else
7655 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007656 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7657 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 prep_redo_cmd(cap);
7659 if (cap->cmdchar == 'g')
7660 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007661 else if (cap->cmdchar == 'z')
7662 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 if (VIsual_active)
7665 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007666 // Putting in Visual mode: The put text replaces the selected
7667 // text. First delete the selected text, then put the new text.
7668 // Need to save and restore the registers that the delete
7669 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007670 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007672#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007674#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007675 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007676 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007677#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
7681 )
7682 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007683 // The delete is going to overwrite the register we want to
7684 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 reg1 = get_register(regname, TRUE);
7686 }
7687
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007688 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 cap->cmdchar = 'd';
7690 cap->nchar = NUL;
7691 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007692 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 nv_operator(cap);
7694 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007695 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007696 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007698 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 cap->oap->regname = regname;
7700
7701 if (reg1 != NULL)
7702 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007703 // Delete probably changed the register we want to put, save
7704 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 reg2 = get_register(regname, FALSE);
7706 put_register(regname, reg1);
7707 }
7708
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007709 // When deleted a linewise Visual area, put the register as
7710 // lines to avoid it joined with the next line. When deletion was
7711 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 if (VIsual_mode == 'V')
7713 flags |= PUT_LINE;
7714 else if (VIsual_mode == 'v')
7715 flags |= PUT_LINE_SPLIT;
7716 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7717 flags |= PUT_LINE_FORWARD;
7718 dir = BACKWARD;
7719 if ((VIsual_mode != 'V'
7720 && curwin->w_cursor.col < curbuf->b_op_start.col)
7721 || (VIsual_mode == 'V'
7722 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007723 // cursor is at the end of the line or end of file, put
7724 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007726 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007727 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007729 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007731 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 if (reg2 != NULL)
7733 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007734
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007735 // What to reselect with "gv"? Selecting the just put text seems to
7736 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007737 if (was_visual)
7738 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007739 curbuf->b_visual.vi_start = curbuf->b_op_start;
7740 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007741 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007742 if (*p_sel == 'e')
7743 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007744 }
7745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007746 // When all lines were selected and deleted do_put() leaves an empty
7747 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007748 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007749 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007750 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007751 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007752
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007753 // If the cursor was in that line, move it to the end of the last
7754 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007755 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7756 {
7757 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7758 coladvance((colnr_T)MAXCOL);
7759 }
7760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 auto_format(FALSE, TRUE);
7762 }
7763}
7764
7765/*
7766 * "o" and "O" commands.
7767 */
7768 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007769nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007772 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7774 {
7775 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007776 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 }
7778 else
7779#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007780 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007782#ifdef FEAT_JOB_CHANNEL
7783 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007784 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 n_opencmd(cap);
7788}
7789
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790#ifdef FEAT_NETBEANS_INTG
7791 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007792nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 netbeans_keycommand(cap->nchar);
7795}
7796#endif
7797
7798#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007800nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007802 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803}
7804#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007805
Bram Moolenaar3918c952005-03-15 22:34:55 +00007806/*
7807 * Trigger CursorHold event.
7808 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7809 * input buffer. "did_cursorhold" is set to avoid retriggering.
7810 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007812nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007813{
7814 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7815 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007816 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007817}