blob: 21840369f78d3fc0d82ab3872394605f5eb1b85d [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
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000630normal_cmd_get_more_chars(
631 int idx_arg,
632 cmdarg_T *cap,
633 int *need_flushbuf UNUSED)
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000634{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000635 int idx = idx_arg;
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000636 int c;
637 int *cp;
638 int repl = FALSE; // get character for replace mode
639 int lit = FALSE; // get extra character literally
640 int langmap_active = FALSE; // using :lmap mappings
641 int lang; // getting a text character
642#ifdef HAVE_INPUT_METHOD
643 int save_smd; // saved value of p_smd
644#endif
645
646 ++no_mapping;
647 ++allow_keys; // no mapping for nchar, but allow key codes
648 // Don't generate a CursorHold event here, most commands can't handle
649 // it, e.g., nv_replace(), nv_csearch().
650 did_cursorhold = TRUE;
651 if (cap->cmdchar == 'g')
652 {
653 /*
654 * For 'g' get the next character now, so that we can check for
655 * "gr", "g'" and "g`".
656 */
657 cap->nchar = plain_vgetc();
658 LANGMAP_ADJUST(cap->nchar, TRUE);
659#ifdef FEAT_CMDL_INFO
660 *need_flushbuf |= add_to_showcmd(cap->nchar);
661#endif
662 if (cap->nchar == 'r' || cap->nchar == '\'' || cap->nchar == '`'
663 || cap->nchar == Ctrl_BSL)
664 {
665 cp = &cap->extra_char; // need to get a third character
666 if (cap->nchar != 'r')
667 lit = TRUE; // get it literally
668 else
669 repl = TRUE; // get it in replace mode
670 }
671 else
672 cp = NULL; // no third character needed
673 }
674 else
675 {
676 if (cap->cmdchar == 'r') // get it in replace mode
677 repl = TRUE;
678 cp = &cap->nchar;
679 }
680 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
681
682 /*
683 * Get a second or third character.
684 */
685 if (cp != NULL)
686 {
687 if (repl)
688 {
689 State = REPLACE; // pretend Replace mode
690#ifdef CURSOR_SHAPE
691 ui_cursor_shape(); // show different cursor shape
692#endif
693 }
694 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
695 {
696 // Allow mappings defined with ":lmap".
697 --no_mapping;
698 --allow_keys;
699 if (repl)
700 State = LREPLACE;
701 else
702 State = LANGMAP;
703 langmap_active = TRUE;
704 }
705#ifdef HAVE_INPUT_METHOD
706 save_smd = p_smd;
707 p_smd = FALSE; // Don't let the IM code show the mode here
708 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
709 im_set_active(TRUE);
710#endif
711 if ((State & INSERT) && !p_ek)
712 {
713#ifdef FEAT_JOB_CHANNEL
714 ch_log_output = TRUE;
715#endif
716 // Disable bracketed paste and modifyOtherKeys here, we won't
717 // recognize the escape sequences with 'esckeys' off.
718 out_str(T_BD);
719 out_str(T_CTE);
720 }
721
722 *cp = plain_vgetc();
723
724 if ((State & INSERT) && !p_ek)
725 {
726#ifdef FEAT_JOB_CHANNEL
727 ch_log_output = TRUE;
728#endif
729 // Re-enable bracketed paste mode and modifyOtherKeys
730 out_str(T_BE);
731 out_str(T_CTI);
732 }
733
734 if (langmap_active)
735 {
736 // Undo the decrement done above
737 ++no_mapping;
738 ++allow_keys;
739 State = NORMAL_BUSY;
740 }
741#ifdef HAVE_INPUT_METHOD
742 if (lang)
743 {
744 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
745 im_save_status(&curbuf->b_p_iminsert);
746 im_set_active(FALSE);
747 }
748 p_smd = save_smd;
749#endif
750 State = NORMAL_BUSY;
751#ifdef FEAT_CMDL_INFO
752 *need_flushbuf |= add_to_showcmd(*cp);
753#endif
754
755 if (!lit)
756 {
757#ifdef FEAT_DIGRAPHS
758 // Typing CTRL-K gets a digraph.
759 if (*cp == Ctrl_K
760 && ((nv_cmds[idx].cmd_flags & NV_LANG)
761 || cp == &cap->extra_char)
762 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
763 {
764 c = get_digraph(FALSE);
765 if (c > 0)
766 {
767 *cp = c;
768# ifdef FEAT_CMDL_INFO
769 // Guessing how to update showcmd here...
770 del_from_showcmd(3);
771 *need_flushbuf |= add_to_showcmd(*cp);
772# endif
773 }
774 }
775#endif
776
777 // adjust chars > 127, except after "tTfFr" commands
778 LANGMAP_ADJUST(*cp, !lang);
779#ifdef FEAT_RIGHTLEFT
780 // adjust Hebrew mapped char
781 if (p_hkmap && lang && KeyTyped)
782 *cp = hkmap(*cp);
783#endif
784 }
785
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000786 // When the next character is CTRL-\ a following CTRL-N means the
787 // command is aborted and we go to Normal mode.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000788 if (cp == &cap->extra_char
789 && cap->nchar == Ctrl_BSL
790 && (cap->extra_char == Ctrl_N || cap->extra_char == Ctrl_G))
791 {
792 cap->cmdchar = Ctrl_BSL;
793 cap->nchar = cap->extra_char;
794 idx = find_command(cap->cmdchar);
795 }
796 else if ((cap->nchar == 'n' || cap->nchar == 'N') && cap->cmdchar == 'g')
797 cap->oap->op_type = get_op_type(*cp, NUL);
798 else if (*cp == Ctrl_BSL)
799 {
800 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
801
802 // There is a busy wait here when typing "f<C-\>" and then
803 // something different from CTRL-N. Can't be avoided.
804 while ((c = vpeekc()) <= 0 && towait > 0L)
805 {
806 do_sleep(towait > 50L ? 50L : towait, FALSE);
807 towait -= 50L;
808 }
809 if (c > 0)
810 {
811 c = plain_vgetc();
812 if (c != Ctrl_N && c != Ctrl_G)
813 vungetc(c);
814 else
815 {
816 cap->cmdchar = Ctrl_BSL;
817 cap->nchar = c;
818 idx = find_command(cap->cmdchar);
819 }
820 }
821 }
822
823 // When getting a text character and the next character is a
824 // multi-byte character, it could be a composing character.
825 // However, don't wait for it to arrive. Also, do enable mapping,
826 // because if it's put back with vungetc() it's too late to apply
827 // mapping.
828 --no_mapping;
829 while (enc_utf8 && lang && (c = vpeekc()) > 0
830 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
831 {
832 c = plain_vgetc();
833 if (!utf_iscomposing(c))
834 {
835 vungetc(c); // it wasn't, put it back
836 break;
837 }
838 else if (cap->ncharC1 == 0)
839 cap->ncharC1 = c;
840 else
841 cap->ncharC2 = c;
842 }
843 ++no_mapping;
844 }
845 --no_mapping;
846 --allow_keys;
847
848 return idx;
849}
850
851/*
852 * Returns TRUE if after processing a normal mode command, need to wait for a
853 * moment when a message is displayed that will be overwritten by the mode
854 * message.
855 */
856 static int
857normal_cmd_need_to_wait_for_msg(cmdarg_T *cap, pos_T *old_pos)
858{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000859 // In Visual mode and with "^O" in Insert mode, a short message will be
860 // overwritten by the mode message. Wait a bit, until a key is hit.
861 // In Visual mode, it's more important to keep the Visual area updated
862 // than keeping a message (e.g. from a /pat search).
863 // Only do this if the command was typed, not from a mapping.
864 // Don't wait when emsg_silent is non-zero.
865 // Also wait a bit after an error message, e.g. for "^O:".
866 // Don't redraw the screen, it would remove the message.
Yegappan Lakshmanan46634352022-01-24 16:30:30 +0000867 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
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +0000971 // If there is an operator pending, then the command we take this time
972 // will terminate it. Finish_op tells us to finish the operation before
973 // returning this time (unless the operation was cancelled).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#ifdef CURSOR_SHAPE
975 c = finish_op;
976#endif
977 finish_op = (oap->op_type != OP_NOP);
978#ifdef CURSOR_SHAPE
979 if (finish_op != c)
980 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100981 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982# ifdef FEAT_MOUSESHAPE
983 update_mouseshape(-1);
984# endif
985 }
986#endif
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100987 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100989 // When not finishing an operator and no register name typed, reset the
990 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000994#ifdef FEAT_EVAL
995 set_prevcount = TRUE;
996#endif
997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100999 // Restore counts from before receiving K_CURSORHOLD. This means after
1000 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
1001 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +00001002 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
1003 {
1004 ca.opcount = oap->prev_opcount;
1005 ca.count0 = oap->prev_count0;
1006 oap->prev_opcount = 0;
1007 oap->prev_count0 = 0;
1008 }
Bram Moolenaara983fe92008-07-31 20:04:27 +00001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012 State = NORMAL_BUSY;
1013#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
1016
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001017#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001018 // Set v:count here, when called from main() and not a stuffed
1019 // command, so that v:count can be used in an expression mapping
1020 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +01001021 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001022 set_vcount_ca(&ca, &set_prevcount);
1023#endif
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 /*
1026 * Get the command character from the user.
1027 */
1028 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +01001029 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001031 // If a mapping was started in Visual or Select mode, remember the length
1032 // of the mapping. This is used below to not return to Insert mode for as
1033 // long as the mapping is being executed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (restart_edit == 0)
1035 old_mapped_len = 0;
1036 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001037 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 old_mapped_len = typebuf_maplen();
1039
1040 if (c == NUL)
1041 c = K_ZERO;
1042
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001043 // In Select mode, typed text replaces the selection.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (VIsual_active
1045 && VIsual_select
1046 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
1047 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001048 int len;
1049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001050 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
1051 // 'insertmode' is set) fake a "d"elete command, Insert mode will
1052 // restart automatically.
1053 // Insert the typed character in the typeahead buffer, so that it can
1054 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001055 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
1056
zeertzjqfbf4f1c2022-01-28 12:50:43 +00001057 // When recording and gotchars() was called the character will be
1058 // recorded again, remove the previous recording.
1059 if (KeyTyped)
1060 ungetchars(len);
Bram Moolenaarc88e9772022-01-03 13:47:50 +00001061
Bram Moolenaar686f51e2005-05-20 21:19:57 +00001062 if (restart_edit != 0)
1063 c = 'd';
1064 else
1065 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001066 msg_nowait = TRUE; // don't delay going to insert mode
1067 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001070 // If the window was made so small that nothing shows, make it at least one
1071 // line and one column when typing a command.
1072 if (KeyTyped && !KeyStuffed)
1073 win_ensure_size();
1074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#ifdef FEAT_CMDL_INFO
1076 need_flushbuf = add_to_showcmd(c);
1077#endif
1078
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001079 // Get the command count
1080 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
1081 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001083 // Find the command character in the table of commands.
1084 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (ctrl_w)
1086 {
1087 ca.nchar = c;
1088 ca.cmdchar = Ctrl_W;
1089 }
1090 else
1091 ca.cmdchar = c;
1092 idx = find_command(ca.cmdchar);
1093 if (idx < 0)
1094 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001095 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 clearopbeep(oap);
1097 goto normal_end;
1098 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001099
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001100 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001102 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001104 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 goto normal_end;
1106 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001107 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
1108 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001110 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if (VIsual_active)
1112 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001113 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (km_stopsel
1115 && (nv_cmds[idx].cmd_flags & NV_STS)
1116 && !(mod_mask & MOD_MASK_SHIFT))
1117 {
1118 end_visual_mode();
1119 redraw_curbuf_later(INVERTED);
1120 }
1121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001122 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 if (km_startsel)
1124 {
1125 if (nv_cmds[idx].cmd_flags & NV_SS)
1126 {
1127 unshift_special(&ca);
1128 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001129 if (idx < 0)
1130 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001131 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001132 clearopbeep(oap);
1133 goto normal_end;
1134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 }
1136 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1137 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
1142#ifdef FEAT_RIGHTLEFT
1143 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
1144 && (nv_cmds[idx].cmd_flags & NV_RL))
1145 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001146 // Invert horizontal movements and operations. Only when typed by the
1147 // user directly, not when the result of a mapping or "x" translated
1148 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 switch (ca.cmdchar)
1150 {
1151 case 'l': ca.cmdchar = 'h'; break;
1152 case K_RIGHT: ca.cmdchar = K_LEFT; break;
1153 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
1154 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
1155 case 'h': ca.cmdchar = 'l'; break;
1156 case K_LEFT: ca.cmdchar = K_RIGHT; break;
1157 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
1158 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
1159 case '>': ca.cmdchar = '<'; break;
1160 case '<': ca.cmdchar = '>'; break;
1161 }
1162 idx = find_command(ca.cmdchar);
1163 }
1164#endif
1165
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001166 // Get additional characters if we need them.
1167 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
1168 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
1170#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001171 // Flush the showcmd characters onto the screen so we can see them while
1172 // the command is being executed. Only do this when the shown command was
1173 // actually displayed, otherwise this will slow down a lot when executing
1174 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (need_flushbuf)
1176 out_flush();
1177#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001178 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001179 {
1180 if (ex_normal_busy)
1181 did_cursorhold = save_did_cursorhold;
1182 else
1183 did_cursorhold = FALSE;
1184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
1186 State = NORMAL;
1187
1188 if (ca.nchar == ESC)
1189 {
1190 clearop(oap);
1191 if (restart_edit == 0 && goto_im())
1192 restart_edit = 'a';
1193 goto normal_end;
1194 }
1195
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001196 if (ca.cmdchar != K_IGNORE)
1197 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001198 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001199 msg_col = 0;
1200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001202 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001204 // When 'keymodel' contains "startsel" some keys start Select/Visual
1205 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (!VIsual_active && km_startsel)
1207 {
1208 if (nv_cmds[idx].cmd_flags & NV_SS)
1209 {
1210 start_selection();
1211 unshift_special(&ca);
1212 idx = find_command(ca.cmdchar);
1213 }
1214 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1215 && (mod_mask & MOD_MASK_SHIFT))
1216 {
1217 start_selection();
1218 mod_mask &= ~MOD_MASK_SHIFT;
1219 }
1220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001222 // Execute the command!
1223 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 ca.arg = nv_cmds[idx].cmd_arg;
1225 (nv_cmds[idx].cmd_func)(&ca);
1226
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001227 // If we didn't start or finish an operator, reset oap->regname, unless we
1228 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (!finish_op
1230 && !oap->op_type
1231 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1232 {
1233 clearop(oap);
1234#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001235 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236#endif
1237 }
1238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001239 // Get the length of mapped chars again after typing a count, second
1240 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001241 if (old_mapped_len > 0)
1242 old_mapped_len = typebuf_maplen();
1243
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001244 // If an operation is pending, handle it. But not for K_IGNORE or
1245 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001246 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001247 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001249 // Wait for a moment when a message is displayed that will be overwritten
1250 // by the mode message.
1251 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
1252 normal_cmd_wait_for_msg();
1253
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001254 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255normal_end:
1256
1257 msg_nowait = FALSE;
1258
Bram Moolenaarcc613032020-06-07 21:31:18 +02001259#ifdef FEAT_EVAL
1260 if (finish_op)
1261 reset_reg_var();
1262#endif
1263
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001264 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265#ifdef CURSOR_SHAPE
1266 c = finish_op;
1267#endif
1268 finish_op = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001269 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001271 // Redraw the cursor with another shape, if we were in Operator-pending
1272 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (c || ca.cmdchar == 'r')
1274 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276# ifdef FEAT_MOUSESHAPE
1277 update_mouseshape(-1);
1278# endif
1279 }
1280#endif
1281
1282#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001283 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001284 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 clear_showcmd();
1286#endif
1287
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001288 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 vim_free(ca.searchbuf);
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (has_mbyte)
1292 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 if (curwin->w_p_scb && toplevel)
1295 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001296 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 do_check_scrollbind(TRUE);
1298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
Bram Moolenaar860cae12010-06-05 23:22:07 +02001300 if (curwin->w_p_crb && toplevel)
1301 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001302 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001303 do_check_cursorbind();
1304 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001305
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001306#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001307 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001308 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001309 restart_edit = 0;
1310#endif
1311
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001312 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1313 // if still inside a mapping that started in Visual mode).
1314 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1317 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 && !(ca.retval & CA_COMMAND_BUSY)
1319 && stuff_empty()
1320 && oap->regname == 0)
1321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (restart_VIsual_select == 1)
1323 {
1324 VIsual_select = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001325 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 showmode();
1327 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001328 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001330 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 (void)edit(restart_edit, FALSE, 1L);
1332 }
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 if (restart_VIsual_select == 2)
1335 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001337 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 opcount = ca.opcount;
1339}
1340
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001341#ifdef FEAT_EVAL
1342/*
1343 * Set v:count and v:count1 according to "cap".
1344 * Set v:prevcount only when "set_prevcount" is TRUE.
1345 */
1346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001347set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001348{
1349 long count = cap->count0;
1350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001351 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001352 if (cap->opcount != 0)
1353 count = cap->opcount * (count == 0 ? 1 : count);
1354 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001355 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001356}
1357#endif
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001360 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 * if not.
1362 */
1363 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001364check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 static int did_check = FALSE;
1367
1368 if (full_screen)
1369 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001370 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001371 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 did_check = TRUE;
1373 }
1374}
1375
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001376#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1377/*
1378 * Call yank_do_autocmd() for "regname".
1379 */
1380 static void
1381call_yank_do_autocmd(int regname)
1382{
1383 oparg_T oa;
1384 yankreg_T *reg;
1385
1386 clear_oparg(&oa);
1387 oa.regname = regname;
1388 oa.op_type = OP_YANK;
1389 oa.is_VIsual = TRUE;
1390 reg = get_register(regname, TRUE);
1391 yank_do_autocmd(&oa, reg);
1392 free_register(reg);
1393}
1394#endif
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001397 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001398 * This function or the next should ALWAYS be called to end Visual mode, except
1399 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 */
1401 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001402end_visual_mode()
1403{
1404 end_visual_mode_keep_button();
1405 reset_held_button();
1406}
1407
1408 void
1409end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
1411#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001412 // If we are using the clipboard, then remember what was selected in case
1413 // we need to paste it somewhere while we still own the selection.
1414 // Only do this when the clipboard is already owned. Don't want to grab
1415 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (clip_star.available && clip_star.owned)
1417 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001418
1419# if defined(FEAT_EVAL)
1420 // Emit a TextYankPost for the automatic copy of the selection into the
1421 // star and/or plus register.
1422 if (has_textyankpost())
1423 {
1424 if (clip_isautosel_star())
1425 call_yank_do_autocmd('*');
1426 if (clip_isautosel_plus())
1427 call_yank_do_autocmd('+');
1428 }
1429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430#endif
1431
1432 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 setmouse();
1434 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001437 curbuf->b_visual.vi_mode = VIsual_mode;
1438 curbuf->b_visual.vi_start = VIsual;
1439 curbuf->b_visual.vi_end = curwin->w_cursor;
1440 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#ifdef FEAT_EVAL
1442 curbuf->b_visual_mode_eval = VIsual_mode;
1443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (!virtual_active())
1445 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001446 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001448 adjust_cursor_eol();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001449 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450}
1451
1452/*
1453 * Reset VIsual_active and VIsual_reselect.
1454 */
1455 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001456reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 if (VIsual_active)
1459 {
1460 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001461 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 VIsual_reselect = FALSE;
1464}
1465
1466/*
1467 * Reset VIsual_active and VIsual_reselect if it's set.
1468 */
1469 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001470reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 if (VIsual_active)
1473 {
1474 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001475 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 VIsual_reselect = FALSE;
1477 }
1478}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001480 void
1481restore_visual_mode(void)
1482{
1483 if (VIsual_mode_orig != NUL)
1484 {
1485 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1486 VIsual_mode_orig = NUL;
1487 }
1488}
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490/*
1491 * Check for a balloon-eval special item to include when searching for an
1492 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1493 * Returns TRUE if the character at "*ptr" should be included.
1494 * "dir" is FORWARD or BACKWARD, the direction of searching.
1495 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1496 * "bnp" points to a counter for square brackets.
1497 */
1498 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001499find_is_eval_item(
1500 char_u *ptr,
1501 int *colp,
1502 int *bnp,
1503 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001505 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1507 ++*bnp;
1508 if (*bnp > 0)
1509 {
1510 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1511 --*bnp;
1512 return TRUE;
1513 }
1514
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001515 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 if (*ptr == '.')
1517 return TRUE;
1518
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001519 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1521 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1522 {
1523 *colp += dir;
1524 return TRUE;
1525 }
1526 return FALSE;
1527}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
1529/*
1530 * Find the identifier under or to the right of the cursor.
1531 * "find_type" can have one of three values:
1532 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001533 * FIND_STRING: find any non-white text
1534 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001535 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 *
1537 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001538 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001540 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 * This doesn't match the real Vi but I like it a little better and it
1542 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001543 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 * When FIND_IDENT isn't defined, we backup until a blank.
1545 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001546 * Returns the length of the text, or zero if no text is found.
1547 * If text is found, a pointer to the text is put in "*text". This
1548 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 */
1550 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001551find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001554 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555}
1556
1557/*
1558 * Like find_ident_under_cursor(), but for any window and any position.
1559 * However: Uses 'iskeyword' from the current window!.
1560 */
1561 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001562find_ident_at_pos(
1563 win_T *wp,
1564 linenr_T lnum,
1565 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001566 char_u **text,
1567 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001568 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
1570 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001571 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 int this_class = 0;
1574 int prev_class;
1575 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001576 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001578 // if i == 0: try to find an identifier
1579 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1581 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1582 {
1583 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001584 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 */
1586 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (has_mbyte)
1588 {
1589 while (ptr[col] != NUL)
1590 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001591 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1593 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 this_class = mb_get_class(ptr + col);
1595 if (this_class != 0 && (i == 1 || this_class != 1))
1596 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001597 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 }
1600 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001602 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 )
1605 ++col;
1606
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001607 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
1610 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001611 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (has_mbyte)
1614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001615 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1617 this_class = mb_get_class((char_u *)"a");
1618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001620 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
1622 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1623 prev_class = mb_get_class(ptr + prevcol);
1624 if (this_class != prev_class
1625 && (i == 0
1626 || prev_class == 0
1627 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 && (!(find_type & FIND_EVAL)
1629 || prevcol == 0
1630 || !find_is_eval_item(ptr + prevcol, &prevcol,
1631 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 )
1633 break;
1634 col = prevcol;
1635 }
1636
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001637 // If we don't want just any old text, or we've found an
1638 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (this_class > 2)
1640 this_class = 2;
1641 if (!(find_type & FIND_STRING) || this_class == 2)
1642 break;
1643 }
1644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 while (col > 0
1647 && ((i == 0
1648 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001649 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 && (!(find_type & FIND_IDENT)
1651 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 || ((find_type & FIND_EVAL)
1653 && col > 1
1654 && find_is_eval_item(ptr + col - 1, &col,
1655 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 ))
1657 --col;
1658
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001659 // If we don't want just any old text, or we've found an
1660 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1662 break;
1663 }
1664 }
1665
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001666 if (ptr[col] == NUL || (i == 0
1667 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001669 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001670 if ((find_type & FIND_NOERROR) == 0)
1671 {
1672 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001673 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001674 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001675 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 return 0;
1678 }
1679 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001680 *text = ptr;
1681 if (textcol != NULL)
1682 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001685 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 bn = 0;
1688 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if (has_mbyte)
1691 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001692 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 this_class = mb_get_class(ptr);
1694 while (ptr[col] != NUL
1695 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1696 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 || ((find_type & FIND_EVAL)
1698 && col <= (int)startcol
1699 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001701 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001705 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 || ((find_type & FIND_EVAL)
1707 && col <= (int)startcol
1708 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712 return col;
1713}
1714
1715/*
1716 * Prepare for redo of a normal command.
1717 */
1718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001719prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 prep_redo(cap->oap->regname, cap->count0,
1722 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1723}
1724
1725/*
1726 * Prepare for redo of any command.
1727 * Note that only the last argument can be a multi-byte char.
1728 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001729 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001730prep_redo(
1731 int regname,
1732 long num,
1733 int cmd1,
1734 int cmd2,
1735 int cmd3,
1736 int cmd4,
1737 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001739 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1740}
1741
1742/*
1743 * Prepare for redo of any command with extra count after "cmd2".
1744 */
1745 void
1746prep_redo_num2(
1747 int regname,
1748 long num1,
1749 int cmd1,
1750 int cmd2,
1751 long num2,
1752 int cmd3,
1753 int cmd4,
1754 int cmd5)
1755{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001757 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
1759 AppendCharToRedobuff('"');
1760 AppendCharToRedobuff(regname);
1761 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001762 if (num1 != 0)
1763 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 if (cmd1 != NUL)
1765 AppendCharToRedobuff(cmd1);
1766 if (cmd2 != NUL)
1767 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001768 if (num2 != 0)
1769 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (cmd3 != NUL)
1771 AppendCharToRedobuff(cmd3);
1772 if (cmd4 != NUL)
1773 AppendCharToRedobuff(cmd4);
1774 if (cmd5 != NUL)
1775 AppendCharToRedobuff(cmd5);
1776}
1777
1778/*
1779 * check for operator active and clear it
1780 *
1781 * return TRUE if operator was active
1782 */
1783 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001784checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785{
1786 if (oap->op_type == OP_NOP)
1787 return FALSE;
1788 clearopbeep(oap);
1789 return TRUE;
1790}
1791
1792/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001793 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001795 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
1797 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001798checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001800 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 return FALSE;
1802 clearopbeep(oap);
1803 return TRUE;
1804}
1805
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001807clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808{
1809 oap->op_type = OP_NOP;
1810 oap->regname = 0;
1811 oap->motion_force = NUL;
1812 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001813 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814}
1815
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001817clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
1819 clearop(oap);
1820 beep_flush();
1821}
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
1824 * Remove the shift modifier from a special key.
1825 */
1826 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001827unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
1829 switch (cap->cmdchar)
1830 {
1831 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1832 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1833 case K_S_UP: cap->cmdchar = K_UP; break;
1834 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1835 case K_S_HOME: cap->cmdchar = K_HOME; break;
1836 case K_S_END: cap->cmdchar = K_END; break;
1837 }
1838 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1839}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001841/*
1842 * If the mode is currently displayed clear the command line or update the
1843 * command displayed.
1844 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001845 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001846may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001847{
1848 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001849 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001850#ifdef FEAT_CMDL_INFO
1851 else
1852 clear_showcmd();
1853#endif
1854}
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1857/*
1858 * Routines for displaying a partly typed command
1859 */
1860
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001861#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001863static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864static int showcmd_is_clear = TRUE;
1865static int showcmd_visual = FALSE;
1866
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001867static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001870clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
1872 if (!p_sc)
1873 return;
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (VIsual_active && !char_avail())
1876 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001877 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 long lines;
1879 colnr_T leftcol, rightcol;
1880 linenr_T top, bot;
1881
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001882 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001883 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 {
1885 top = VIsual.lnum;
1886 bot = curwin->w_cursor.lnum;
1887 }
1888 else
1889 {
1890 top = curwin->w_cursor.lnum;
1891 bot = VIsual.lnum;
1892 }
1893# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001894 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001895 (void)hasFolding(top, &top, NULL);
1896 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897# endif
1898 lines = bot - top + 1;
1899
1900 if (VIsual_mode == Ctrl_V)
1901 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001902# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001903 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001904 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001905
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001906 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001907 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001908 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001911# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001912 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001913 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1916 (long)(rightcol - leftcol + 1));
1917 }
1918 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1919 sprintf((char *)showcmd_buf, "%ld", lines);
1920 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001921 {
1922 char_u *s, *e;
1923 int l;
1924 int bytes = 0;
1925 int chars = 0;
1926
1927 if (cursor_bot)
1928 {
1929 s = ml_get_pos(&VIsual);
1930 e = ml_get_cursor();
1931 }
1932 else
1933 {
1934 s = ml_get_cursor();
1935 e = ml_get_pos(&VIsual);
1936 }
1937 while ((*p_sel != 'e') ? s <= e : s < e)
1938 {
1939 l = (*mb_ptr2len)(s);
1940 if (l == 0)
1941 {
1942 ++bytes;
1943 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001944 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001945 }
1946 bytes += l;
1947 ++chars;
1948 s += l;
1949 }
1950 if (bytes == chars)
1951 sprintf((char *)showcmd_buf, "%d", chars);
1952 else
1953 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1954 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001955 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 showcmd_visual = TRUE;
1957 }
1958 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
1960 showcmd_buf[0] = NUL;
1961 showcmd_visual = FALSE;
1962
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001963 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (showcmd_is_clear)
1965 return;
1966 }
1967
1968 display_showcmd();
1969}
1970
1971/*
1972 * Add 'c' to string of shown command chars.
1973 * Return TRUE if output has been written (and setcursor() has been called).
1974 */
1975 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001976add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
1978 char_u *p;
1979 int old_len;
1980 int extra_len;
1981 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 int i;
1983 static int ignore[] =
1984 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001985#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1987 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001988#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001989 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001990 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1992 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001993 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001995 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 0
1997 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998
Bram Moolenaar09df3122006-01-23 22:23:09 +00001999 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 return FALSE;
2001
2002 if (showcmd_visual)
2003 {
2004 showcmd_buf[0] = NUL;
2005 showcmd_visual = FALSE;
2006 }
2007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002008 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (IS_SPECIAL(c))
2010 for (i = 0; ignore[i] != 0; ++i)
2011 if (ignore[i] == c)
2012 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01002015 if (*p == ' ')
2016 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 old_len = (int)STRLEN(showcmd_buf);
2018 extra_len = (int)STRLEN(p);
2019 overflow = old_len + extra_len - SHOWCMD_COLS;
2020 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00002021 mch_memmove(showcmd_buf, showcmd_buf + overflow,
2022 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 STRCAT(showcmd_buf, p);
2024
2025 if (char_avail())
2026 return FALSE;
2027
2028 display_showcmd();
2029
2030 return TRUE;
2031}
2032
2033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002034add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035{
2036 if (!add_to_showcmd(c))
2037 setcursor();
2038}
2039
2040/*
2041 * Delete 'len' characters from the end of the shown command.
2042 */
2043 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002044del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045{
2046 int old_len;
2047
2048 if (!p_sc)
2049 return;
2050
2051 old_len = (int)STRLEN(showcmd_buf);
2052 if (len > old_len)
2053 len = old_len;
2054 showcmd_buf[old_len - len] = NUL;
2055
2056 if (!char_avail())
2057 display_showcmd();
2058}
2059
2060/*
2061 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2062 * something and there is a partial mapping.
2063 */
2064 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002065push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
2067 if (p_sc)
2068 STRCPY(old_showcmd_buf, showcmd_buf);
2069}
2070
2071 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002072pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073{
2074 if (!p_sc)
2075 return;
2076
2077 STRCPY(showcmd_buf, old_showcmd_buf);
2078
2079 display_showcmd();
2080}
2081
2082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002083display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085 int len;
2086
2087 cursor_off();
2088
2089 len = (int)STRLEN(showcmd_buf);
2090 if (len == 0)
2091 showcmd_is_clear = TRUE;
2092 else
2093 {
2094 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2095 showcmd_is_clear = FALSE;
2096 }
2097
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002098 // clear the rest of an old message by outputting up to SHOWCMD_COLS
2099 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002102 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103}
2104#endif
2105
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106/*
2107 * When "check" is FALSE, prepare for commands that scroll the window.
2108 * When "check" is TRUE, take care of scroll-binding after the window has
2109 * scrolled. Called from normal_cmd() and edit().
2110 */
2111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002112do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 static win_T *old_curwin = NULL;
2115 static linenr_T old_topline = 0;
2116#ifdef FEAT_DIFF
2117 static int old_topfill = 0;
2118#endif
2119 static buf_T *old_buf = NULL;
2120 static colnr_T old_leftcol = 0;
2121
2122 if (check && curwin->w_p_scb)
2123 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002124 // If a ":syncbind" command was just used, don't scroll, only reset
2125 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (did_syncbind)
2127 did_syncbind = FALSE;
2128 else if (curwin == old_curwin)
2129 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002130 // Synchronize other windows, as necessary according to
2131 // 'scrollbind'. Don't do this after an ":edit" command, except
2132 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if ((curwin->w_buffer == old_buf
2134#ifdef FEAT_DIFF
2135 || curwin->w_p_diff
2136#endif
2137 )
2138 && (curwin->w_topline != old_topline
2139#ifdef FEAT_DIFF
2140 || curwin->w_topfill != old_topfill
2141#endif
2142 || curwin->w_leftcol != old_leftcol))
2143 {
2144 check_scrollbind(curwin->w_topline - old_topline,
2145 (long)(curwin->w_leftcol - old_leftcol));
2146 }
2147 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002148 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002150 // When switching between windows, make sure that the relative
2151 // vertical offset is valid for the new window. The relative
2152 // offset is invalid whenever another 'scrollbind' window has
2153 // scrolled to a point that would force the current window to
2154 // scroll past the beginning or end of its buffer. When the
2155 // resync is performed, some of the other 'scrollbind' windows may
2156 // need to jump so that the current window's relative position is
2157 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2159 }
2160 curwin->w_scbind_pos = curwin->w_topline;
2161 }
2162
2163 old_curwin = curwin;
2164 old_topline = curwin->w_topline;
2165#ifdef FEAT_DIFF
2166 old_topfill = curwin->w_topfill;
2167#endif
2168 old_buf = curwin->w_buffer;
2169 old_leftcol = curwin->w_leftcol;
2170}
2171
2172/*
2173 * Synchronize any windows that have "scrollbind" set, based on the
2174 * number of rows by which the current window has changed
2175 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2176 */
2177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002178check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
2180 int want_ver;
2181 int want_hor;
2182 win_T *old_curwin = curwin;
2183 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 int old_VIsual_select = VIsual_select;
2185 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 colnr_T tgt_leftcol = curwin->w_leftcol;
2187 long topline;
2188 long y;
2189
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002190 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2192#ifdef FEAT_DIFF
2193 want_ver |= old_curwin->w_p_diff;
2194#endif
2195 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2196
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002197 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002199 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
2201 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002202 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (curwin != old_curwin && curwin->w_p_scb)
2204 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002205 // do the vertical scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (want_ver)
2207 {
2208#ifdef FEAT_DIFF
2209 if (old_curwin->w_p_diff && curwin->w_p_diff)
2210 {
2211 diff_set_topline(old_curwin, curwin);
2212 }
2213 else
2214#endif
2215 {
2216 curwin->w_scbind_pos += topline_diff;
2217 topline = curwin->w_scbind_pos;
2218 if (topline > curbuf->b_ml.ml_line_count)
2219 topline = curbuf->b_ml.ml_line_count;
2220 if (topline < 1)
2221 topline = 1;
2222
2223 y = topline - curwin->w_topline;
2224 if (y > 0)
2225 scrollup(y, FALSE);
2226 else
2227 scrolldown(-y, FALSE);
2228 }
2229
2230 redraw_later(VALID);
2231 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002235 // do the horizontal scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2237 {
2238 curwin->w_leftcol = tgt_leftcol;
2239 leftcol_changed();
2240 }
2241 }
2242 }
2243
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002244 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 VIsual_select = old_VIsual_select;
2246 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 curwin = old_curwin;
2248 curbuf = old_curbuf;
2249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251/*
2252 * Command character that's ignored.
2253 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002254 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002257nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002259 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260}
2261
2262/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002263 * Command character that doesn't do anything, but unlike nv_ignore() does
2264 * start edit(). Used for "startinsert" executed while starting up.
2265 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002267nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002268{
2269}
2270
2271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 * Command character doesn't exist.
2273 */
2274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002275nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 clearopbeep(cap->oap);
2278}
2279
2280/*
2281 * <Help> and <F1> commands.
2282 */
2283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002284nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
2286 if (!checkclearopq(cap->oap))
2287 ex_help(NULL);
2288}
2289
2290/*
2291 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2292 */
2293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002294nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002296#ifdef FEAT_JOB_CHANNEL
2297 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2298 clearopbeep(cap->oap);
2299 else
2300#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002301 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002302 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002303 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002304 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2305 op_addsub(cap->oap, cap->count1, cap->arg);
2306 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002307 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002308 else if (VIsual_active)
2309 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002310 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002311 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312}
2313
2314/*
2315 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2316 */
2317 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002318nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002321 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002322 if (mod_mask & MOD_MASK_CTRL)
2323 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002324 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002325 if (cap->arg == BACKWARD)
2326 goto_tabpage(-(int)cap->count1);
2327 else
2328 goto_tabpage((int)cap->count0);
2329 }
2330 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002331 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333}
2334
2335/*
2336 * Implementation of "gd" and "gD" command.
2337 */
2338 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002339nv_gd(
2340 oparg_T *oap,
2341 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002342 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 char_u *ptr;
2346
Bram Moolenaard9d30582005-05-18 22:10:28 +00002347 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002348 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002349 == FAIL)
2350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002352 }
2353 else
2354 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002355#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002356 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2357 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002358#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002359 // clear any search statistics
2360 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2361 clear_cmdline = TRUE;
2362 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002363}
2364
2365/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002366 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2367 * otherwise.
2368 */
2369 static int
2370is_ident(char_u *line, int offset)
2371{
2372 int i;
2373 int incomment = FALSE;
2374 int instring = 0;
2375 int prev = 0;
2376
2377 for (i = 0; i < offset && line[i] != NUL; i++)
2378 {
2379 if (instring != 0)
2380 {
2381 if (prev != '\\' && line[i] == instring)
2382 instring = 0;
2383 }
2384 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2385 {
2386 instring = line[i];
2387 }
2388 else
2389 {
2390 if (incomment)
2391 {
2392 if (prev == '*' && line[i] == '/')
2393 incomment = FALSE;
2394 }
2395 else if (prev == '/' && line[i] == '*')
2396 {
2397 incomment = TRUE;
2398 }
2399 else if (prev == '/' && line[i] == '/')
2400 {
2401 return FALSE;
2402 }
2403 }
2404
2405 prev = line[i];
2406 }
2407
2408 return incomment == FALSE && instring == 0;
2409}
2410
2411/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002412 * Search for variable declaration of "ptr[len]".
2413 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2414 * current file ("gD").
2415 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002416 * Return FAIL when not found.
2417 */
2418 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002419find_decl(
2420 char_u *ptr,
2421 int len,
2422 int locally,
2423 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002424 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002425{
2426 char_u *pat;
2427 pos_T old_pos;
2428 pos_T par_pos;
2429 pos_T found_pos;
2430 int t;
2431 int save_p_ws;
2432 int save_p_scs;
2433 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002434 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002435 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002436 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002437
2438 if ((pat = alloc(len + 7)) == NULL)
2439 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002440
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002441 // Put "\V" before the pattern to avoid that the special meaning of "."
2442 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002443 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2444 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 old_pos = curwin->w_cursor;
2446 save_p_ws = p_ws;
2447 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002448 p_ws = FALSE; // don't wrap around end of file now
2449 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002451 // With "gD" go to line 1.
2452 // With "gd" Search back for the start of the current function, then go
2453 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002456 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002458 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460 else
2461 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002462 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2464 --curwin->w_cursor.lnum;
2465 }
2466 curwin->w_cursor.col = 0;
2467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002468 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002469 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002470 for (;;)
2471 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002472 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002473 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002474 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002475 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002476
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002477 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002478 {
2479 pos_T *pos;
2480
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002481 // Check that the block the match is in doesn't end before the
2482 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002483 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2484 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2485 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002486 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002487 // There can't be a useful match before the end of this block.
2488 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002489 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002490 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002491 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002492 }
2493
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002494 if (t == FAIL)
2495 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002496 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002497 if (found_pos.lnum != 0)
2498 {
2499 curwin->w_cursor = found_pos;
2500 t = OK;
2501 }
2502 break;
2503 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002504 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002505 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002506 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002507 ++curwin->w_cursor.lnum;
2508 curwin->w_cursor.col = 0;
2509 continue;
2510 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002511 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2512
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002513 // If the current position is not a valid identifier and a previous
2514 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002515 if (!valid && found_pos.lnum != 0)
2516 {
2517 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002518 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002519 }
2520
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002521 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002522 if (valid && !locally)
2523 break;
2524 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002525 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002526 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002527 if (found_pos.lnum != 0)
2528 curwin->w_cursor = found_pos;
2529 break;
2530 }
2531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002532 // For finding a local variable and the match is before the "{" or
2533 // inside a comment, continue searching. For K&R style function
2534 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002535 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002536 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002537 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002538 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002539 // Remove SEARCH_START from flags to avoid getting stuck at one
2540 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002541 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002543
2544 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002546 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 curwin->w_cursor = old_pos;
2548 }
2549 else
2550 {
2551 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002552 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 reset_search_dir();
2554 }
2555
2556 vim_free(pat);
2557 p_ws = save_p_ws;
2558 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002559
2560 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561}
2562
2563/*
2564 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2565 * lines rather than lines in the file.
2566 * 'dist' must be positive.
2567 *
2568 * Return OK if able to move cursor, FAIL otherwise.
2569 */
2570 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002571nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573 int linelen = linetabsize(ml_get_curline());
2574 int retval = OK;
2575 int atend = FALSE;
2576 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002577 int col_off1; // margin offset for first screen line
2578 int col_off2; // margin offset for wrapped screen line
2579 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002580 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002583 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 col_off1 = curwin_col_off();
2586 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002587 width1 = curwin->w_width - col_off1;
2588 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002589 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002590 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002593 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002594 // Instead of sticking at the last character of the buffer line we
2595 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 if (curwin->w_curswant == MAXCOL)
2597 {
2598 atend = TRUE;
2599 validate_virtcol();
2600 if (width1 <= 0)
2601 curwin->w_curswant = 0;
2602 else
2603 {
2604 curwin->w_curswant = width1 - 1;
2605 if (curwin->w_virtcol > curwin->w_curswant)
2606 curwin->w_curswant += ((curwin->w_virtcol
2607 - curwin->w_curswant - 1) / width2 + 1) * width2;
2608 }
2609 }
2610 else
2611 {
2612 if (linelen > width1)
2613 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2614 else
2615 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002616 if (curwin->w_curswant >= (colnr_T)n)
2617 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
2619
2620 while (dist--)
2621 {
2622 if (dir == BACKWARD)
2623 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002624 if ((long)curwin->w_curswant >= width1
2625#ifdef FEAT_FOLDING
2626 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2627#endif
2628 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002629 // Move back within the line. This can give a negative value
2630 // for w_curswant if width1 < width2 (with cpoptions+=n),
2631 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 curwin->w_curswant -= width2;
2633 else
2634 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002635 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002637 // Move to the start of a closed fold. Don't do that when
2638 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (!(fdo_flags & FDO_ALL))
2640 (void)hasFolding(curwin->w_cursor.lnum,
2641 &curwin->w_cursor.lnum, NULL);
2642#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002643 if (curwin->w_cursor.lnum == 1)
2644 {
2645 retval = FAIL;
2646 break;
2647 }
2648 --curwin->w_cursor.lnum;
2649
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 linelen = linetabsize(ml_get_curline());
2651 if (linelen > width1)
2652 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2653 + 1) * width2;
2654 }
2655 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002656 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
2658 if (linelen > width1)
2659 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2660 else
2661 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002662 if (curwin->w_curswant + width2 < (colnr_T)n
2663#ifdef FEAT_FOLDING
2664 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2665#endif
2666 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002667 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 curwin->w_curswant += width2;
2669 else
2670 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002671 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002673 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2675 &curwin->w_cursor.lnum);
2676#endif
2677 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2678 {
2679 retval = FAIL;
2680 break;
2681 }
2682 curwin->w_cursor.lnum++;
2683 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002684 // Check if the cursor has moved below the number display
2685 // when width1 < width2 (with cpoptions+=n). Subtract width2
2686 // to get a negative value for w_curswant, which will get
2687 // clipped to column 0.
2688 if (curwin->w_curswant >= width1)
2689 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002690 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692 }
2693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002696 if (virtual_active() && atend)
2697 coladvance(MAXCOL);
2698 else
2699 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2702 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002703 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002704 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002705
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002706 // Check for landing on a character that got split at the end of the
2707 // last line. We want to advance a screenline, not end up in the same
2708 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002710 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002711#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002712 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2713 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002714#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002715
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002716 c = (*mb_ptr2char)(ml_get_cursor());
2717 if (dir == FORWARD && virtcol < curwin->w_curswant
2718 && (curwin->w_curswant <= (colnr_T)width1)
2719 && !vim_isprintc(c) && c > 255)
2720 oneright();
2721
Bram Moolenaar773b1582014-08-29 14:20:51 +02002722 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 && (curwin->w_curswant < (colnr_T)width1
2724 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2725 : ((curwin->w_curswant - width1) % width2
2726 > (colnr_T)width2 / 2)))
2727 --curwin->w_cursor.col;
2728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002731 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 return retval;
2734}
2735
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736/*
2737 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2738 * cap->arg must be TRUE for CTRL-E.
2739 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002740 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002741nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
2743 if (!checkclearop(cap->oap))
2744 scroll_redraw(cap->arg, cap->count1);
2745}
2746
2747/*
2748 * Scroll "count" lines up or down, and redraw.
2749 */
2750 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002751scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 linenr_T prev_topline = curwin->w_topline;
2754#ifdef FEAT_DIFF
2755 int prev_topfill = curwin->w_topfill;
2756#endif
2757 linenr_T prev_lnum = curwin->w_cursor.lnum;
2758
2759 if (up)
2760 scrollup(count, TRUE);
2761 else
2762 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002763 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002765 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2766 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 cursor_correct();
2768 check_cursor_moved(curwin);
2769 curwin->w_valid |= VALID_TOPLINE;
2770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002771 // If moved back to where we were, at least move the cursor, otherwise
2772 // we get stuck at one position. Don't move the cursor up if the
2773 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 while (curwin->w_topline == prev_topline
2775#ifdef FEAT_DIFF
2776 && curwin->w_topfill == prev_topfill
2777#endif
2778 )
2779 {
2780 if (up)
2781 {
2782 if (curwin->w_cursor.lnum > prev_lnum
2783 || cursor_down(1L, FALSE) == FAIL)
2784 break;
2785 }
2786 else
2787 {
2788 if (curwin->w_cursor.lnum < prev_lnum
2789 || prev_topline == 1L
2790 || cursor_up(1L, FALSE) == FAIL)
2791 break;
2792 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002793 // Mark w_topline as valid, otherwise the screen jumps back at the
2794 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 check_cursor_moved(curwin);
2796 curwin->w_valid |= VALID_TOPLINE;
2797 }
2798 }
2799 if (curwin->w_cursor.lnum != prev_lnum)
2800 coladvance(curwin->w_curswant);
2801 redraw_later(VALID);
2802}
2803
2804/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002805 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2806 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2807 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002808 */
2809 static int
2810nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2811{
2812 int nchar = *nchar_arg;
2813 long n;
2814
2815 // "z123{nchar}": edit the count before obtaining {nchar}
2816 if (checkclearop(cap->oap))
2817 return FALSE;
2818 n = nchar - '0';
2819
2820 for (;;)
2821 {
2822#ifdef USE_ON_FLY_SCROLL
2823 dont_scroll = TRUE; // disallow scrolling here
2824#endif
2825 ++no_mapping;
2826 ++allow_keys; // no mapping for nchar, but allow key codes
2827 nchar = plain_vgetc();
2828 LANGMAP_ADJUST(nchar, TRUE);
2829 --no_mapping;
2830 --allow_keys;
2831#ifdef FEAT_CMDL_INFO
2832 (void)add_to_showcmd(nchar);
2833#endif
2834 if (nchar == K_DEL || nchar == K_KDEL)
2835 n /= 10;
2836 else if (VIM_ISDIGIT(nchar))
2837 n = n * 10 + (nchar - '0');
2838 else if (nchar == CAR)
2839 {
2840#ifdef FEAT_GUI
2841 need_mouse_correct = TRUE;
2842#endif
2843 win_setheight((int)n);
2844 break;
2845 }
2846 else if (nchar == 'l'
2847 || nchar == 'h'
2848 || nchar == K_LEFT
2849 || nchar == K_RIGHT)
2850 {
2851 cap->count1 = n ? n * cap->count1 : cap->count1;
2852 *nchar_arg = nchar;
2853 return TRUE;
2854 }
2855 else
2856 {
2857 clearopbeep(cap->oap);
2858 break;
2859 }
2860 }
2861 cap->oap->op_type = OP_NOP;
2862 return FALSE;
2863}
2864
2865#ifdef FEAT_SPELL
2866/*
2867 * "zug" and "zuw": undo "zg" and "zw"
2868 * "zg": add good word to word list
2869 * "zw": add wrong word to word list
2870 * "zG": add good word to temp word list
2871 * "zW": add wrong word to temp word list
2872 */
2873 static int
2874nv_zg_zw(cmdarg_T *cap, int nchar)
2875{
2876 char_u *ptr = NULL;
2877 int len;
2878 int undo = FALSE;
2879
2880 if (nchar == 'u')
2881 {
2882 ++no_mapping;
2883 ++allow_keys; // no mapping for nchar, but allow key codes
2884 nchar = plain_vgetc();
2885 LANGMAP_ADJUST(nchar, TRUE);
2886 --no_mapping;
2887 --allow_keys;
2888#ifdef FEAT_CMDL_INFO
2889 (void)add_to_showcmd(nchar);
2890#endif
2891 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2892 {
2893 clearopbeep(cap->oap);
2894 return OK;
2895 }
2896 undo = TRUE;
2897 }
2898
2899 if (checkclearop(cap->oap))
2900 return OK;
2901 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2902 return FAIL;
2903 if (ptr == NULL)
2904 {
2905 pos_T pos = curwin->w_cursor;
2906
2907 // Find bad word under the cursor. When 'spell' is
2908 // off this fails and find_ident_under_cursor() is
2909 // used below.
2910 emsg_off++;
2911 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2912 emsg_off--;
2913 if (len != 0 && curwin->w_cursor.col <= pos.col)
2914 ptr = ml_get_pos(&curwin->w_cursor);
2915 curwin->w_cursor = pos;
2916 }
2917
2918 if (ptr == NULL
2919 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2920 return FAIL;
2921 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2922 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2923 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2924
2925 return OK;
2926}
2927#endif
2928
2929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 * Commands that start with "z".
2931 */
2932 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002933nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934{
2935 long n;
2936 colnr_T col;
2937 int nchar = cap->nchar;
2938#ifdef FEAT_FOLDING
2939 long old_fdl = curwin->w_p_fdl;
2940 int old_fen = curwin->w_p_fen;
2941#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002942 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002944 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if (
2948#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002949 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2950 // and "zC" only in Visual mode. "zj" and "zk" are motion
2951 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 cap->nchar != 'f' && cap->nchar != 'F'
2953 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2954 && cap->nchar != 'j' && cap->nchar != 'k'
2955 &&
2956#endif
2957 checkclearop(cap->oap))
2958 return;
2959
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002960 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2961 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2963 && cap->count0
2964 && cap->count0 != curwin->w_cursor.lnum)
2965 {
2966 setpcmark();
2967 if (cap->count0 > curbuf->b_ml.ml_line_count)
2968 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2969 else
2970 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002971 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
2973
2974 switch (nchar)
2975 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 case '+':
2978 if (cap->count0 == 0)
2979 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002980 // No count given: put cursor at the line below screen
2981 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2983 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2984 else
2985 curwin->w_cursor.lnum = curwin->w_botline;
2986 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 case NL:
2989 case CAR:
2990 case K_KENTER:
2991 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002992 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993
2994 case 't': scroll_cursor_top(0, TRUE);
2995 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002996 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 break;
2998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002999 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003001 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 case 'z': scroll_cursor_halfway(TRUE);
3004 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003005 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 break;
3007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003008 // "z^", "z-" and "zb": put cursor at bottom of screen
3009 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
3010 // when <count> is at bottom of window, and puts that one at
3011 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (cap->count0 != 0)
3013 {
3014 scroll_cursor_bot(0, TRUE);
3015 curwin->w_cursor.lnum = curwin->w_topline;
3016 }
3017 else if (curwin->w_topline == 1)
3018 curwin->w_cursor.lnum = 1;
3019 else
3020 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003021 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 case '-':
3023 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026 case 'b': scroll_cursor_bot(0, TRUE);
3027 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003028 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 break;
3030
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003031 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02003033 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003034 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003036 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 case 'h':
3038 case K_LEFT:
3039 if (!curwin->w_p_wrap)
3040 {
3041 if ((colnr_T)cap->count1 > curwin->w_leftcol)
3042 curwin->w_leftcol = 0;
3043 else
3044 curwin->w_leftcol -= (colnr_T)cap->count1;
3045 leftcol_changed();
3046 }
3047 break;
3048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003049 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02003050 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003051 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003053 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 case 'l':
3055 case K_RIGHT:
3056 if (!curwin->w_p_wrap)
3057 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003058 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 curwin->w_leftcol += (colnr_T)cap->count1;
3060 leftcol_changed();
3061 }
3062 break;
3063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003064 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 case 's': if (!curwin->w_p_wrap)
3066 {
3067#ifdef FEAT_FOLDING
3068 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 else
3071#endif
3072 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01003073 if ((long)col > siso)
3074 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 else
3076 col = 0;
3077 if (curwin->w_leftcol != col)
3078 {
3079 curwin->w_leftcol = col;
3080 redraw_later(NOT_VALID);
3081 }
3082 }
3083 break;
3084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003085 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 case 'e': if (!curwin->w_p_wrap)
3087 {
3088#ifdef FEAT_FOLDING
3089 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 else
3092#endif
3093 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02003094 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01003095 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 col = 0;
3097 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01003098 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 if (curwin->w_leftcol != col)
3100 {
3101 curwin->w_leftcol = col;
3102 redraw_later(NOT_VALID);
3103 }
3104 }
3105 break;
3106
Christian Brabandt2fa93842021-05-30 22:17:25 +02003107 // "zp", "zP" in block mode put without addind trailing spaces
3108 case 'P':
3109 case 'p': nv_put(cap);
3110 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02003111 // "zy" Yank without trailing spaces
3112 case 'y': nv_operator(cap);
3113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 // "zF": create fold command
3116 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 case 'F':
3118 case 'f': if (foldManualAllowed(TRUE))
3119 {
3120 cap->nchar = 'f';
3121 nv_operator(cap);
3122 curwin->w_p_fen = TRUE;
3123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003124 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3126 {
3127 nv_operator(cap);
3128 finish_op = TRUE;
3129 }
3130 }
3131 else
3132 clearopbeep(cap->oap);
3133 break;
3134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003135 // "zd": delete fold at cursor
3136 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 case 'd':
3138 case 'D': if (foldManualAllowed(FALSE))
3139 {
3140 if (VIsual_active)
3141 nv_operator(cap);
3142 else
3143 deleteFold(curwin->w_cursor.lnum,
3144 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3145 }
3146 break;
3147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003148 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 case 'E': if (foldmethodIsManual(curwin))
3150 {
3151 clearFolding(curwin);
3152 changed_window_setting();
3153 }
3154 else if (foldmethodIsMarker(curwin))
3155 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3156 TRUE, FALSE);
3157 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003158 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 break;
3160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003161 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 case 'n': curwin->w_p_fen = FALSE;
3163 break;
3164
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 case 'N': curwin->w_p_fen = TRUE;
3167 break;
3168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003169 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3171 break;
3172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3175 openFold(curwin->w_cursor.lnum, cap->count1);
3176 else
3177 {
3178 closeFold(curwin->w_cursor.lnum, cap->count1);
3179 curwin->w_p_fen = TRUE;
3180 }
3181 break;
3182
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003183 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3185 openFoldRecurse(curwin->w_cursor.lnum);
3186 else
3187 {
3188 closeFoldRecurse(curwin->w_cursor.lnum);
3189 curwin->w_p_fen = TRUE;
3190 }
3191 break;
3192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003193 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 case 'o': if (VIsual_active)
3195 nv_operator(cap);
3196 else
3197 openFold(curwin->w_cursor.lnum, cap->count1);
3198 break;
3199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003200 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 case 'O': if (VIsual_active)
3202 nv_operator(cap);
3203 else
3204 openFoldRecurse(curwin->w_cursor.lnum);
3205 break;
3206
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003207 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 case 'c': if (VIsual_active)
3209 nv_operator(cap);
3210 else
3211 closeFold(curwin->w_cursor.lnum, cap->count1);
3212 curwin->w_p_fen = TRUE;
3213 break;
3214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003215 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 case 'C': if (VIsual_active)
3217 nv_operator(cap);
3218 else
3219 closeFoldRecurse(curwin->w_cursor.lnum);
3220 curwin->w_p_fen = TRUE;
3221 break;
3222
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003223 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 case 'v': foldOpenCursor();
3225 break;
3226
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003227 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003229 curwin->w_foldinvalid = TRUE; // recompute folds
3230 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 foldOpenCursor();
3232 break;
3233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003234 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003236 curwin->w_foldinvalid = TRUE; // recompute folds
3237 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 break;
3239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003240 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003242 {
3243 curwin->w_p_fdl -= cap->count1;
3244 if (curwin->w_p_fdl < 0)
3245 curwin->w_p_fdl = 0;
3246 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003247 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 curwin->w_p_fen = TRUE;
3249 break;
3250
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003251 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003253 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 curwin->w_p_fen = TRUE;
3255 break;
3256
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003257 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003258 case 'r': curwin->w_p_fdl += cap->count1;
3259 {
3260 int d = getDeepestNesting();
3261
3262 if (curwin->w_p_fdl >= d)
3263 curwin->w_p_fdl = d;
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 break;
3266
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003267 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003269 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 break;
3271
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003272 case 'j': // "zj" move to next fold downwards
3273 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3275 cap->count1) == FAIL)
3276 clearopbeep(cap->oap);
3277 break;
3278
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003279#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003281#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003283 case 'g': // "zg": add good word to word list
3284 case 'w': // "zw": add wrong word to word list
3285 case 'G': // "zG": add good word to temp word list
3286 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003287 if (nv_zg_zw(cap, nchar) == FAIL)
3288 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003289 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003290
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003291 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003292 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003293 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003294 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003295#endif
3296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 default: clearopbeep(cap->oap);
3298 }
3299
3300#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003301 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 if (old_fen != curwin->w_p_fen)
3303 {
3304# ifdef FEAT_DIFF
3305 win_T *wp;
3306
3307 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3308 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003309 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 FOR_ALL_WINDOWS(wp)
3311 {
3312 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3313 {
3314 wp->w_p_fen = curwin->w_p_fen;
3315 changed_window_setting_win(wp);
3316 }
3317 }
3318 }
3319# endif
3320 changed_window_setting();
3321 }
3322
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003323 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (old_fdl != curwin->w_p_fdl)
3325 newFoldLevel();
3326#endif
3327}
3328
3329#ifdef FEAT_GUI
3330/*
3331 * Vertical scrollbar movement.
3332 */
3333 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003334nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 if (cap->oap->op_type != OP_NOP)
3337 clearopbeep(cap->oap);
3338
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003339 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 gui_do_scroll();
3341}
3342
3343/*
3344 * Horizontal scrollbar movement.
3345 */
3346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003347nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 if (cap->oap->op_type != OP_NOP)
3350 clearopbeep(cap->oap);
3351
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003352 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003353 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354}
3355#endif
3356
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003357#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003358/*
3359 * Click in GUI tab.
3360 */
3361 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003362nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003363{
3364 if (cap->oap->op_type != OP_NOP)
3365 clearopbeep(cap->oap);
3366
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003367 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003368 goto_tabpage(current_tab);
3369}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003370
3371/*
3372 * Selected item in tab line menu.
3373 */
3374 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003375nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003376{
3377 if (cap->oap->op_type != OP_NOP)
3378 clearopbeep(cap->oap);
3379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003380 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003381 handle_tabmenu();
3382}
3383
3384/*
3385 * Handle selecting an item of the GUI tab line menu.
3386 * Used in Normal and Insert mode.
3387 */
3388 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003389handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003390{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003391 switch (current_tabmenu)
3392 {
3393 case TABLINE_MENU_CLOSE:
3394 if (current_tab == 0)
3395 do_cmdline_cmd((char_u *)"tabclose");
3396 else
3397 {
3398 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3399 current_tab);
3400 do_cmdline_cmd(IObuff);
3401 }
3402 break;
3403
3404 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003405 if (current_tab == 0)
3406 do_cmdline_cmd((char_u *)"$tabnew");
3407 else
3408 {
3409 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3410 current_tab - 1);
3411 do_cmdline_cmd(IObuff);
3412 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003413 break;
3414
3415 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003416 if (current_tab == 0)
3417 do_cmdline_cmd((char_u *)"browse $tabnew");
3418 else
3419 {
3420 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3421 current_tab - 1);
3422 do_cmdline_cmd(IObuff);
3423 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003424 break;
3425 }
3426}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003427#endif
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429/*
3430 * "Q" command.
3431 */
3432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003433nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003435 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003437 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003438 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 do_exmode(FALSE);
3440}
3441
3442/*
3443 * Handle a ":" command.
3444 */
3445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003446nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003448 int old_p_im;
3449 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003450 int is_cmdkey = cap->cmdchar == K_COMMAND
3451 || cap->cmdchar == K_SCRIPT_COMMAND;
3452 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
Bram Moolenaar957cf672020-11-12 14:21:06 +01003454 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 nv_operator(cap);
3456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
3458 if (cap->oap->op_type != OP_NOP)
3459 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003460 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 cap->oap->motion_type = MCHAR;
3462 cap->oap->inclusive = FALSE;
3463 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003464 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003466 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 stuffcharReadbuff('.');
3468 if (cap->count0 > 1)
3469 {
3470 stuffReadbuff((char_u *)",.+");
3471 stuffnumReadbuff((long)cap->count0 - 1L);
3472 }
3473 }
3474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003475 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (KeyTyped)
3477 compute_cmdrow();
3478
3479 old_p_im = p_im;
3480
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003481 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003482 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3483 if (is_cmdkey)
3484 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3485 else
3486 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003488 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (p_im != old_p_im)
3490 {
3491 if (p_im)
3492 restart_edit = 'i';
3493 else
3494 restart_edit = 0;
3495 }
3496
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003497 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003498 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003499 clearop(cap->oap);
3500 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3502 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003503 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3504 || did_emsg
3505 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003506 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 clearopbeep(cap->oap);
3508 }
3509}
3510
3511/*
3512 * Handle CTRL-G command.
3513 */
3514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003515nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003517 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
3519 VIsual_select = !VIsual_select;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003520 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 showmode();
3522 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003523 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003524 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 fileinfo((int)cap->count0, FALSE, TRUE);
3526}
3527
3528/*
3529 * Handle CTRL-H <Backspace> command.
3530 */
3531 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003532nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (VIsual_active && VIsual_select)
3535 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003536 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 v_visop(cap);
3538 }
3539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 nv_left(cap);
3541}
3542
3543/*
3544 * CTRL-L: clear screen and redraw.
3545 */
3546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003547nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
3549 if (!checkclearop(cap->oap))
3550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003552 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003553 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003554# ifdef FEAT_RELTIME
3555 {
3556 win_T *wp;
3557
3558 FOR_ALL_WINDOWS(wp)
3559 wp->w_s->b_syn_slow = FALSE;
3560 }
3561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#endif
3563 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003564#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3565# ifdef VIMDLL
3566 if (!gui.in_use)
3567# endif
3568 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571}
3572
3573/*
3574 * CTRL-O: In Select mode: switch to Visual mode for one command.
3575 * Otherwise: Go to older pcmark.
3576 */
3577 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003578nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 if (VIsual_active && VIsual_select)
3581 {
3582 VIsual_select = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003583 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003585 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 cap->count1 = -cap->count1;
3590 nv_pcmark(cap);
3591 }
3592}
3593
3594/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003595 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3596 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 */
3598 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003599nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600{
3601 if (!checkclearopq(cap->oap))
3602 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3603 GETF_SETMARK|GETF_ALT, FALSE);
3604}
3605
3606/*
3607 * "Z" commands.
3608 */
3609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003610nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
3612 if (!checkclearopq(cap->oap))
3613 {
3614 switch (cap->nchar)
3615 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003616 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 case 'Z': do_cmdline_cmd((char_u *)"x");
3618 break;
3619
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003620 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 case 'Q': do_cmdline_cmd((char_u *)"q!");
3622 break;
3623
3624 default: clearopbeep(cap->oap);
3625 }
3626 }
3627}
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629/*
3630 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3631 */
3632 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003633do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
3635 oparg_T oa;
3636 cmdarg_T ca;
3637
3638 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003639 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 ca.oap = &oa;
3641 ca.cmdchar = c1;
3642 ca.nchar = c2;
3643 nv_ident(&ca);
3644}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
3646/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003647 * 'K' normal-mode command. Get the command to lookup the keyword under the
3648 * cursor.
3649 */
3650 static int
3651nv_K_getcmd(
3652 cmdarg_T *cap,
3653 char_u *kp,
3654 int kp_help,
3655 int kp_ex,
3656 char_u **ptr_arg,
3657 int n,
3658 char_u *buf,
3659 unsigned buflen)
3660{
3661 char_u *ptr = *ptr_arg;
3662 int isman;
3663 int isman_s;
3664
3665 if (kp_help)
3666 {
3667 // in the help buffer
3668 STRCPY(buf, "he! ");
3669 return n;
3670 }
3671
3672 if (kp_ex)
3673 {
3674 // 'keywordprog' is an ex command
3675 if (cap->count0 != 0)
3676 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3677 else
3678 STRCPY(buf, kp);
3679 STRCAT(buf, " ");
3680 return n;
3681 }
3682
3683 // An external command will probably use an argument starting
3684 // with "-" as an option. To avoid trouble we skip the "-".
3685 while (*ptr == '-' && n > 0)
3686 {
3687 ++ptr;
3688 --n;
3689 }
3690 if (n == 0)
3691 {
3692 // found dashes only
3693 emsg(_(e_no_identifier_under_cursor));
3694 vim_free(buf);
3695 *ptr_arg = ptr;
3696 return 0;
3697 }
3698
3699 // When a count is given, turn it into a range. Is this
3700 // really what we want?
3701 isman = (STRCMP(kp, "man") == 0);
3702 isman_s = (STRCMP(kp, "man -s") == 0);
3703 if (cap->count0 != 0 && !(isman || isman_s))
3704 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3705
3706 STRCAT(buf, "! ");
3707 if (cap->count0 == 0 && isman_s)
3708 STRCAT(buf, "man");
3709 else
3710 STRCAT(buf, kp);
3711 STRCAT(buf, " ");
3712 if (cap->count0 != 0 && (isman || isman_s))
3713 {
3714 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3715 STRCAT(buf, " ");
3716 }
3717
3718 *ptr_arg = ptr;
3719 return n;
3720}
3721
3722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 * Handle the commands that use the word under the cursor.
3724 * [g] CTRL-] :ta to current identifier
3725 * [g] 'K' run program for current identifier
3726 * [g] '*' / to current identifier or string
3727 * [g] '#' ? to current identifier or string
3728 * g ']' :tselect for current identifier
3729 */
3730 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003731nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
3733 char_u *ptr = NULL;
3734 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003735 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003736 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003738 char_u *kp; // value of 'keywordprg'
3739 int kp_help; // 'keywordprg' is ":he"
3740 int kp_ex; // 'keywordprg' starts with ":"
3741 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003743 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003744 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003747 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
3749 cmdchar = cap->nchar;
3750 g_cmd = TRUE;
3751 }
3752 else
3753 {
3754 cmdchar = cap->cmdchar;
3755 g_cmd = FALSE;
3756 }
3757
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003758 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 cmdchar = '#';
3760
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003761 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3765 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (checkclearopq(cap->oap))
3767 return;
3768 }
3769
3770 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3771 (cmdchar == '*' || cmdchar == '#')
3772 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3773 {
3774 clearop(cap->oap);
3775 return;
3776 }
3777
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003778 // Allocate buffer to put the command in. Inserting backslashes can
3779 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3780 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3782 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3783 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003784 if (kp_help && *skipwhite(ptr) == NUL)
3785 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003786 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003787 return;
3788 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003789 kp_ex = (*kp == ':');
3790 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3791 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (buf == NULL)
3793 return;
3794 buf[0] = NUL;
3795
3796 switch (cmdchar)
3797 {
3798 case '*':
3799 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003800 // Put cursor at start of word, makes search skip the word
3801 // under the cursor.
3802 // Call setpcmark() first, so "*``" puts the cursor back where
3803 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 setpcmark();
3805 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3806
3807 if (!g_cmd && vim_iswordp(ptr))
3808 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003809 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 break;
3811
3812 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003813 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3814 if (n == 0)
3815 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 break;
3817
3818 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003819 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820#ifdef FEAT_CSCOPE
3821 if (p_cst)
3822 STRCPY(buf, "cstag ");
3823 else
3824#endif
3825 STRCPY(buf, "ts ");
3826 break;
3827
3828 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003829 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (curbuf->b_help)
3831 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003833 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003834 if (g_cmd)
3835 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003836 else if (cap->count0 == 0)
3837 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003838 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003839 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003843 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003844 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003846 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003847 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003848 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003849 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003850 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003851 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003852 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003853 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003854 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003856 vim_free(buf);
3857 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003859 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003860 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003861 {
3862 vim_free(buf);
3863 vim_free(p);
3864 return;
3865 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003866 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003867 STRCAT(buf, p);
3868 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003870 else
3871 {
3872 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003873 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003874 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003875 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003876 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003877 {
3878 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003879 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003880 aux_ptr = (char_u *)"";
3881 else
3882 aux_ptr = (char_u *)"\\|\"\n[";
3883 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003884 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003885 aux_ptr = (char_u *)"\\|\"\n*?[";
3886
3887 p = buf + STRLEN(buf);
3888 while (n-- > 0)
3889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003890 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003891 if (vim_strchr(aux_ptr, *ptr) != NULL)
3892 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003893 // When current byte is a part of multibyte character, copy all
3894 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003895 if (has_mbyte)
3896 {
3897 int i;
3898 int len = (*mb_ptr2len)(ptr) - 1;
3899
3900 for (i = 0; i < len && n >= 1; ++i, --n)
3901 *p++ = *ptr++;
3902 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003903 *p++ = *ptr++;
3904 }
3905 *p = NUL;
3906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003908 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 if (cmdchar == '*' || cmdchar == '#')
3910 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003911 if (!g_cmd && (has_mbyte
3912 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3913 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003915
3916 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003917 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003919
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003920 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003923 {
3924 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003926 g_tag_at_cursor = FALSE;
3927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 vim_free(buf);
3930}
3931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932/*
3933 * Get visually selected text, within one line only.
3934 * Returns FAIL if more than one line selected.
3935 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003936 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003937get_visual_text(
3938 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003939 char_u **pp, // return: start of selected text
3940 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
3942 if (VIsual_mode != 'V')
3943 unadjust_for_sel();
3944 if (VIsual.lnum != curwin->w_cursor.lnum)
3945 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003946 if (cap != NULL)
3947 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 return FAIL;
3949 }
3950 if (VIsual_mode == 'V')
3951 {
3952 *pp = ml_get_curline();
3953 *lenp = (int)STRLEN(*pp);
3954 }
3955 else
3956 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003957 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
3959 *pp = ml_get_pos(&curwin->w_cursor);
3960 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3961 }
3962 else
3963 {
3964 *pp = ml_get_pos(&VIsual);
3965 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3966 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003967 if (**pp == NUL)
3968 *lenp = 0;
3969 if (has_mbyte && *lenp > 0)
3970 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003971 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 reset_VIsual_and_resel();
3974 return OK;
3975}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977/*
3978 * CTRL-T: backwards in tag stack
3979 */
3980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003981nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
3983 if (!checkclearopq(cap->oap))
3984 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3985}
3986
3987/*
3988 * Handle scrolling command 'H', 'L' and 'M'.
3989 */
3990 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003991nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
3993 int used = 0;
3994 long n;
3995#ifdef FEAT_FOLDING
3996 linenr_T lnum;
3997#endif
3998 int half;
3999
4000 cap->oap->motion_type = MLINE;
4001 setpcmark();
4002
4003 if (cap->cmdchar == 'L')
4004 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004005 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 curwin->w_cursor.lnum = curwin->w_botline - 1;
4007 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
4008 curwin->w_cursor.lnum = 1;
4009 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004010 {
4011#ifdef FEAT_FOLDING
4012 if (hasAnyFolding(curwin))
4013 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004014 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004015 for (n = cap->count1 - 1; n > 0
4016 && curwin->w_cursor.lnum > curwin->w_topline; --n)
4017 {
4018 (void)hasFolding(curwin->w_cursor.lnum,
4019 &curwin->w_cursor.lnum, NULL);
4020 --curwin->w_cursor.lnum;
4021 }
4022 }
4023 else
4024#endif
4025 curwin->w_cursor.lnum -= cap->count1 - 1;
4026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028 else
4029 {
4030 if (cap->cmdchar == 'M')
4031 {
4032#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004033 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 used -= diff_check_fill(curwin, curwin->w_topline)
4035 - curwin->w_topfill;
4036#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004037 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
4039 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
4040 {
4041#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004042 // Count half he number of filler lines to be "below this
4043 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
4045 + n) / 2 >= half)
4046 {
4047 --n;
4048 break;
4049 }
4050#endif
4051 used += plines(curwin->w_topline + n);
4052 if (used >= half)
4053 break;
4054#ifdef FEAT_FOLDING
4055 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
4056 n = lnum - curwin->w_topline;
4057#endif
4058 }
4059 if (n > 0 && used > curwin->w_height)
4060 --n;
4061 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004062 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004065#ifdef FEAT_FOLDING
4066 if (hasAnyFolding(curwin))
4067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004068 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004069 lnum = curwin->w_topline;
4070 while (n-- > 0 && lnum < curwin->w_botline - 1)
4071 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02004072 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004073 ++lnum;
4074 }
4075 n = lnum - curwin->w_topline;
4076 }
4077#endif
4078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 curwin->w_cursor.lnum = curwin->w_topline + n;
4080 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4081 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4082 }
4083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004084 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02004085 if (cap->oap->op_type == OP_NOP)
4086 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 beginline(BL_SOL | BL_FIX);
4088}
4089
4090/*
4091 * Cursor right commands.
4092 */
4093 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004094nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095{
4096 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004097 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004099 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4100 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004101 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004102 if (mod_mask & MOD_MASK_CTRL)
4103 cap->arg = TRUE;
4104 nv_wordcmd(cap);
4105 return;
4106 }
4107
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 cap->oap->motion_type = MCHAR;
4109 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004110 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004112 // In virtual edit mode, there's no such thing as "past_line", as lines
4113 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004115 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
4117 for (n = cap->count1; n > 0; --n)
4118 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004119 if ((!past_line && oneright() == FAIL)
4120 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004121 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004123 // <Space> wraps to next line if 'whichwrap' has 's'.
4124 // 'l' wraps to next line if 'whichwrap' has 'l'.
4125 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 if ( ((cap->cmdchar == ' '
4127 && vim_strchr(p_ww, 's') != NULL)
4128 || (cap->cmdchar == 'l'
4129 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004130 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 && vim_strchr(p_ww, '>') != NULL))
4132 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4133 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004134 // When deleting we also count the NL as a character.
4135 // Set cap->oap->inclusive when last char in the line is
4136 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004137 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004139 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 cap->oap->inclusive = TRUE;
4141 else
4142 {
4143 ++curwin->w_cursor.lnum;
4144 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 curwin->w_set_curswant = TRUE;
4147 cap->oap->inclusive = FALSE;
4148 }
4149 continue;
4150 }
4151 if (cap->oap->op_type == OP_NOP)
4152 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004153 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 if (n == cap->count1)
4155 beep_flush();
4156 }
4157 else
4158 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004159 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 cap->oap->inclusive = TRUE;
4161 }
4162 break;
4163 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004164 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 if (virtual_active())
4168 oneright();
4169 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004172 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 ++curwin->w_cursor.col;
4175 }
4176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 }
4178#ifdef FEAT_FOLDING
4179 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4180 && cap->oap->op_type == OP_NOP)
4181 foldOpenCursor();
4182#endif
4183}
4184
4185/*
4186 * Cursor left commands.
4187 *
4188 * Returns TRUE when operator end should not be adjusted.
4189 */
4190 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004191nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 long n;
4194
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004195 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4196 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004197 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004198 if (mod_mask & MOD_MASK_CTRL)
4199 cap->arg = 1;
4200 nv_bck_word(cap);
4201 return;
4202 }
4203
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 cap->oap->motion_type = MCHAR;
4205 cap->oap->inclusive = FALSE;
4206 for (n = cap->count1; n > 0; --n)
4207 {
4208 if (oneleft() == FAIL)
4209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004210 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4211 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4212 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if ( (((cap->cmdchar == K_BS
4214 || cap->cmdchar == Ctrl_H)
4215 && vim_strchr(p_ww, 'b') != NULL)
4216 || (cap->cmdchar == 'h'
4217 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004218 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 && vim_strchr(p_ww, '<') != NULL))
4220 && curwin->w_cursor.lnum > 1)
4221 {
4222 --(curwin->w_cursor.lnum);
4223 coladvance((colnr_T)MAXCOL);
4224 curwin->w_set_curswant = TRUE;
4225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004226 // When the NL before the first char has to be deleted we
4227 // put the cursor on the NUL after the previous line.
4228 // This is a very special case, be careful!
4229 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if ( (cap->oap->op_type == OP_DELETE
4231 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004232 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004234 char_u *cp = ml_get_cursor();
4235
4236 if (*cp != NUL)
4237 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004238 if (has_mbyte)
4239 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4240 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004241 ++curwin->w_cursor.col;
4242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 cap->retval |= CA_NO_ADJ_OP_END;
4244 }
4245 continue;
4246 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004247 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4249 beep_flush();
4250 break;
4251 }
4252 }
4253#ifdef FEAT_FOLDING
4254 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4255 && cap->oap->op_type == OP_NOP)
4256 foldOpenCursor();
4257#endif
4258}
4259
4260/*
4261 * Cursor up commands.
4262 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4263 */
4264 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004265nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004267 if (mod_mask & MOD_MASK_SHIFT)
4268 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004269 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004270 cap->arg = BACKWARD;
4271 nv_page(cap);
4272 }
4273 else
4274 {
4275 cap->oap->motion_type = MLINE;
4276 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4277 clearopbeep(cap->oap);
4278 else if (cap->arg)
4279 beginline(BL_WHITE | BL_FIX);
4280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281}
4282
4283/*
4284 * Cursor down commands.
4285 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4286 */
4287 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004288nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004290 if (mod_mask & MOD_MASK_SHIFT)
4291 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004292 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004293 cap->arg = FORWARD;
4294 nv_page(cap);
4295 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004296#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004297 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004298 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4299 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 {
4303#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004304 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004305 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 cmdwin_result = CAR;
4307 else
4308#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004309#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004310 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004311 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4312 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4313 {
4314 invoke_prompt_callback();
4315 if (restart_edit == 0)
4316 restart_edit = 'a';
4317 }
4318 else
4319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
4321 cap->oap->motion_type = MLINE;
4322 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4323 clearopbeep(cap->oap);
4324 else if (cap->arg)
4325 beginline(BL_WHITE | BL_FIX);
4326 }
4327 }
4328}
4329
4330#ifdef FEAT_SEARCHPATH
4331/*
4332 * Grab the file name under the cursor and edit it.
4333 */
4334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004335nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
4337 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004338 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004340 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 {
4342 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004343 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 return;
4345 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004346 if (curbuf_locked())
4347 {
4348 clearop(cap->oap);
4349 return;
4350 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004351#ifdef FEAT_PROP_POPUP
4352 if (ERROR_IF_TERM_POPUP_WINDOW)
4353 return;
4354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004356 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
4358 if (ptr != NULL)
4359 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004360 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004361 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004362 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004364 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004365 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004366 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004367 {
4368 curwin->w_cursor.lnum = lnum;
4369 check_cursor_lnum();
4370 beginline(BL_SOL | BL_FIX);
4371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 vim_free(ptr);
4373 }
4374 else
4375 clearop(cap->oap);
4376}
4377#endif
4378
4379/*
4380 * <End> command: to end of current line or last line.
4381 */
4382 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004383nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004385 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004387 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004389 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
4391 nv_dollar(cap);
4392}
4393
4394/*
4395 * Handle the "$" command.
4396 */
4397 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004398nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
4400 cap->oap->motion_type = MCHAR;
4401 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004402 // In virtual mode when off the edge of a line and an operator
4403 // is pending (whew!) keep the cursor where it is.
4404 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (!virtual_active() || gchar_cursor() != NUL
4406 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004407 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 if (cursor_down((long)(cap->count1 - 1),
4409 cap->oap->op_type == OP_NOP) == FAIL)
4410 clearopbeep(cap->oap);
4411#ifdef FEAT_FOLDING
4412 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4413 foldOpenCursor();
4414#endif
4415}
4416
4417/*
4418 * Implementation of '?' and '/' commands.
4419 * If cap->arg is TRUE don't set PC mark.
4420 */
4421 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004422nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004425 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4428 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004429 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 cap->cmdchar = 'g';
4431 cap->nchar = '?';
4432 nv_operator(cap);
4433 return;
4434 }
4435
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004436 // When using 'incsearch' the cursor may be moved to set a different search
4437 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004438 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 if (cap->searchbuf == NULL)
4441 {
4442 clearop(oap);
4443 return;
4444 }
4445
Bram Moolenaar46539112015-02-17 15:43:57 +01004446 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004447 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004448 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449}
4450
4451/*
4452 * Handle "N" and "n" commands.
4453 * cap->arg is SEARCH_REV for "N", 0 for "n".
4454 */
4455 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004456nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004458 pos_T old = curwin->w_cursor;
4459 int wrapped = FALSE;
4460 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004461
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004462 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004463 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004464 // Avoid getting stuck on the current cursor position, which can
4465 // happen when an offset is given and the cursor is on the last char
4466 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004467 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004468 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004469 cap->count1 -= 1;
4470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471}
4472
4473/*
4474 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4475 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004476 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004478 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004479normal_search(
4480 cmdarg_T *cap,
4481 int dir,
4482 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004483 int opt, // extra flags for do_search()
4484 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485{
4486 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004487 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 cap->oap->motion_type = MCHAR;
4490 cap->oap->inclusive = FALSE;
4491 cap->oap->use_reg_one = TRUE;
4492 curwin->w_set_curswant = TRUE;
4493
Bram Moolenaara80faa82020-04-12 19:37:17 +02004494 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004495 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004496 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4497 if (wrapped != NULL)
4498 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 if (i == 0)
4500 clearop(cap->oap);
4501 else
4502 {
4503 if (i == 2)
4504 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#ifdef FEAT_FOLDING
4507 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4508 foldOpenCursor();
4509#endif
4510 }
4511
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004512 // "/$" will put the cursor after the end of the line, may need to
4513 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004515 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516}
4517
4518/*
4519 * Character search commands.
4520 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4521 * ',' and FALSE for ';'.
4522 * cap->nchar is NUL for ',' and ';' (repeat the search)
4523 */
4524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004525nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526{
4527 int t_cmd;
4528
4529 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4530 t_cmd = TRUE;
4531 else
4532 t_cmd = FALSE;
4533
4534 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4536 clearopbeep(cap->oap);
4537 else
4538 {
4539 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004540 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4542 && (t_cmd || cap->oap->op_type != OP_NOP))
4543 {
4544 colnr_T scol, ecol;
4545
4546 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4547 curwin->w_cursor.coladd = ecol - scol;
4548 }
4549 else
4550 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552#ifdef FEAT_FOLDING
4553 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4554 foldOpenCursor();
4555#endif
4556 }
4557}
4558
4559/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004560 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4561 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4562 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4563 * "[m" or "]m" search for prev/next start of (Java) method.
4564 * "[M" or "]M" search for prev/next end of (Java) method.
4565 */
4566 static void
4567nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4568{
4569 pos_T new_pos = {0, 0, 0};
4570 pos_T *pos = NULL; // init for GCC
4571 pos_T prev_pos;
4572 long n;
4573 int findc;
4574 int c;
4575
4576 if (cap->nchar == '*')
4577 cap->nchar = '/';
4578 prev_pos.lnum = 0;
4579 if (cap->nchar == 'm' || cap->nchar == 'M')
4580 {
4581 if (cap->cmdchar == '[')
4582 findc = '{';
4583 else
4584 findc = '}';
4585 n = 9999;
4586 }
4587 else
4588 {
4589 findc = cap->nchar;
4590 n = cap->count1;
4591 }
4592 for ( ; n > 0; --n)
4593 {
4594 if ((pos = findmatchlimit(cap->oap, findc,
4595 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4596 {
4597 if (new_pos.lnum == 0) // nothing found
4598 {
4599 if (cap->nchar != 'm' && cap->nchar != 'M')
4600 clearopbeep(cap->oap);
4601 }
4602 else
4603 pos = &new_pos; // use last one found
4604 break;
4605 }
4606 prev_pos = new_pos;
4607 curwin->w_cursor = *pos;
4608 new_pos = *pos;
4609 }
4610 curwin->w_cursor = *old_pos;
4611
4612 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4613 // brought us to the match for "[m" and "]M" when inside a method.
4614 // Try finding the '{' or '}' we want to be at.
4615 // Also repeat for the given count.
4616 if (cap->nchar == 'm' || cap->nchar == 'M')
4617 {
4618 // norm is TRUE for "]M" and "[m"
4619 int norm = ((findc == '{') == (cap->nchar == 'm'));
4620
4621 n = cap->count1;
4622 // found a match: we were inside a method
4623 if (prev_pos.lnum != 0)
4624 {
4625 pos = &prev_pos;
4626 curwin->w_cursor = prev_pos;
4627 if (norm)
4628 --n;
4629 }
4630 else
4631 pos = NULL;
4632 while (n > 0)
4633 {
4634 for (;;)
4635 {
4636 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4637 {
4638 // if not found anything, that's an error
4639 if (pos == NULL)
4640 clearopbeep(cap->oap);
4641 n = 0;
4642 break;
4643 }
4644 c = gchar_cursor();
4645 if (c == '{' || c == '}')
4646 {
4647 // Must have found end/start of class: use it.
4648 // Or found the place to be at.
4649 if ((c == findc && norm) || (n == 1 && !norm))
4650 {
4651 new_pos = curwin->w_cursor;
4652 pos = &new_pos;
4653 n = 0;
4654 }
4655 // if no match found at all, we started outside of the
4656 // class and we're inside now. Just go on.
4657 else if (new_pos.lnum == 0)
4658 {
4659 new_pos = curwin->w_cursor;
4660 pos = &new_pos;
4661 }
4662 // found start/end of other method: go to match
4663 else if ((pos = findmatchlimit(cap->oap, findc,
4664 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4665 0)) == NULL)
4666 n = 0;
4667 else
4668 curwin->w_cursor = *pos;
4669 break;
4670 }
4671 }
4672 --n;
4673 }
4674 curwin->w_cursor = *old_pos;
4675 if (pos == NULL && new_pos.lnum != 0)
4676 clearopbeep(cap->oap);
4677 }
4678 if (pos != NULL)
4679 {
4680 setpcmark();
4681 curwin->w_cursor = *pos;
4682 curwin->w_set_curswant = TRUE;
4683#ifdef FEAT_FOLDING
4684 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4685 && cap->oap->op_type == OP_NOP)
4686 foldOpenCursor();
4687#endif
4688 }
4689}
4690
4691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 * "[" and "]" commands.
4693 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4694 */
4695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004696nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004699 pos_T *pos = NULL; // init for GCC
4700 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 int flag;
4702 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703
4704 cap->oap->motion_type = MCHAR;
4705 cap->oap->inclusive = FALSE;
4706 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004707 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004710 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (cap->nchar == 'f')
4712 nv_gotofile(cap);
4713 else
4714#endif
4715
4716#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004717 // Find the occurrence(s) of the identifier or define under cursor
4718 // in current and included files or jump to the first occurrence.
4719 //
4720 // search list jump
4721 // fwd bwd fwd bwd fwd bwd
4722 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4723 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004725# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004727# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 cap->nchar) != NULL)
4731 {
4732 char_u *ptr;
4733 int len;
4734
4735 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4736 clearop(cap->oap);
4737 else
4738 {
4739 find_pattern_in_path(ptr, 0, len, TRUE,
4740 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4741 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4742 cap->count1,
4743 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4744 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4745 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4746 (linenr_T)MAXLNUM);
4747 curwin->w_set_curswant = TRUE;
4748 }
4749 }
4750 else
4751#endif
4752
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004753 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4754 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4755 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4756 // "[m" or "]m" search for prev/next start of (Java) method.
4757 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 if ( (cap->cmdchar == '['
4759 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4760 || (cap->cmdchar == ']'
4761 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004762 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004764 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 else if (cap->nchar == '[' || cap->nchar == ']')
4766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004767 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 flag = '{';
4769 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004770 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004773 // Imitate strange Vi behaviour: When using "]]" with an operator
4774 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004775 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 (cap->oap->op_type != OP_NOP
4777 && cap->arg == FORWARD && flag == '{')))
4778 clearopbeep(cap->oap);
4779 else
4780 {
4781 if (cap->oap->op_type == OP_NOP)
4782 beginline(BL_WHITE | BL_FIX);
4783#ifdef FEAT_FOLDING
4784 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4785 foldOpenCursor();
4786#endif
4787 }
4788 }
4789
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004790 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 else if (cap->nchar == 'p' || cap->nchar == 'P')
4792 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004793 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004796 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 else if (cap->nchar == '\'' || cap->nchar == '`')
4798 {
4799 pos = &curwin->w_cursor;
4800 for (n = cap->count1; n > 0; --n)
4801 {
4802 prev_pos = *pos;
4803 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4804 cap->nchar == '\'');
4805 if (pos == NULL)
4806 break;
4807 }
4808 if (pos == NULL)
4809 pos = &prev_pos;
4810 nv_cursormark(cap, cap->nchar == '\'', pos);
4811 }
4812
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004813 // [ or ] followed by a middle mouse click: put selected text with
4814 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004815 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 (void)do_mouse(cap->oap, cap->nchar,
4818 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4819 cap->count1, PUT_FIXINDENT);
4820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
4822#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004823 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 else if (cap->nchar == 'z')
4825 {
4826 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4827 cap->count1) == FAIL)
4828 clearopbeep(cap->oap);
4829 }
4830#endif
4831
4832#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004833 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 else if (cap->nchar == 'c')
4835 {
4836 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4837 cap->count1) == FAIL)
4838 clearopbeep(cap->oap);
4839 }
4840#endif
4841
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004842#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004843 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004844 else if (cap->nchar == 's' || cap->nchar == 'S')
4845 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004846 setpcmark();
4847 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004848 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4849 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004850 {
4851 clearopbeep(cap->oap);
4852 break;
4853 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004854 else
4855 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004856# ifdef FEAT_FOLDING
4857 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4858 foldOpenCursor();
4859# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004860 }
4861#endif
4862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004863 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 else
4865 clearopbeep(cap->oap);
4866}
4867
4868/*
4869 * Handle Normal mode "%" command.
4870 */
4871 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004872nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873{
4874 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004875#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 linenr_T lnum = curwin->w_cursor.lnum;
4877#endif
4878
4879 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004880 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
4882 if (cap->count0 > 100)
4883 clearopbeep(cap->oap);
4884 else
4885 {
4886 cap->oap->motion_type = MLINE;
4887 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004888 // Round up, so 'normal 100%' always jumps at the line line.
4889 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4890 // overflow on 32-bits, so use a formula with less accuracy
4891 // to avoid overflows.
4892 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4894 / 100L * cap->count0;
4895 else
4896 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4897 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004898 if (curwin->w_cursor.lnum < 1)
4899 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4901 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4902 beginline(BL_SOL | BL_FIX);
4903 }
4904 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004905 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
4907 cap->oap->motion_type = MCHAR;
4908 cap->oap->use_reg_one = TRUE;
4909 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4910 clearopbeep(cap->oap);
4911 else
4912 {
4913 setpcmark();
4914 curwin->w_cursor = *pos;
4915 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 }
4920#ifdef FEAT_FOLDING
4921 if (cap->oap->op_type == OP_NOP
4922 && lnum != curwin->w_cursor.lnum
4923 && (fdo_flags & FDO_PERCENT)
4924 && KeyTyped)
4925 foldOpenCursor();
4926#endif
4927}
4928
4929/*
4930 * Handle "(" and ")" commands.
4931 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4932 */
4933 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004934nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936 cap->oap->motion_type = MCHAR;
4937 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004938 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004939 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 curwin->w_set_curswant = TRUE;
4941
4942 if (findsent(cap->arg, cap->count1) == FAIL)
4943 clearopbeep(cap->oap);
4944 else
4945 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004946 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004947 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#ifdef FEAT_FOLDING
4950 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4951 foldOpenCursor();
4952#endif
4953 }
4954}
4955
4956/*
4957 * "m" command: Mark a position.
4958 */
4959 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004960nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 if (!checkclearop(cap->oap))
4963 {
4964 if (setmark(cap->nchar) == FAIL)
4965 clearopbeep(cap->oap);
4966 }
4967}
4968
4969/*
4970 * "{" and "}" commands.
4971 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4972 */
4973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004974nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
4976 cap->oap->motion_type = MCHAR;
4977 cap->oap->inclusive = FALSE;
4978 cap->oap->use_reg_one = TRUE;
4979 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004980 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 clearopbeep(cap->oap);
4982 else
4983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985#ifdef FEAT_FOLDING
4986 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4987 foldOpenCursor();
4988#endif
4989 }
4990}
4991
4992/*
4993 * "u" command: Undo or make lower case.
4994 */
4995 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004996nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004998 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005000 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 cap->cmdchar = 'g';
5002 cap->nchar = 'u';
5003 nv_operator(cap);
5004 }
5005 else
5006 nv_kundo(cap);
5007}
5008
5009/*
5010 * <Undo> command.
5011 */
5012 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005013nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
5015 if (!checkclearopq(cap->oap))
5016 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02005017#ifdef FEAT_JOB_CHANNEL
5018 if (bt_prompt(curbuf))
5019 {
5020 clearopbeep(cap->oap);
5021 return;
5022 }
5023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 u_undo((int)cap->count1);
5025 curwin->w_set_curswant = TRUE;
5026 }
5027}
5028
5029/*
5030 * Handle the "r" command.
5031 */
5032 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005033nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
5035 char_u *ptr;
5036 int had_ctrl_v;
5037 long n;
5038
5039 if (checkclearop(cap->oap))
5040 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02005041#ifdef FEAT_JOB_CHANNEL
5042 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5043 {
5044 clearopbeep(cap->oap);
5045 return;
5046 }
5047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005049 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (cap->nchar == Ctrl_V)
5051 {
5052 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01005053 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005054 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (cap->nchar > DEL)
5056 had_ctrl_v = NUL;
5057 }
5058 else
5059 had_ctrl_v = NUL;
5060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005061 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005062 if (IS_SPECIAL(cap->nchar))
5063 {
5064 clearopbeep(cap->oap);
5065 return;
5066 }
5067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005068 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (VIsual_active)
5070 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00005071 if (got_int)
5072 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01005073 if (had_ctrl_v)
5074 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005075 // Use a special (negative) number to make a difference between a
5076 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01005077 if (cap->nchar == CAR)
5078 cap->nchar = REPLACE_CR_NCHAR;
5079 else if (cap->nchar == NL)
5080 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01005081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 nv_operator(cap);
5083 return;
5084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005086 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (virtual_active())
5088 {
5089 if (u_save_cursor() == FAIL)
5090 return;
5091 if (gchar_cursor() == NUL)
5092 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005093 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 coladvance_force((colnr_T)(getviscol() + cap->count1));
5095 curwin->w_cursor.col -= cap->count1;
5096 }
5097 else if (gchar_cursor() == TAB)
5098 coladvance_force(getviscol());
5099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005101 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005103 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005104 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
5106 clearopbeep(cap->oap);
5107 return;
5108 }
5109
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005110 // Replacing with a TAB is done by edit() when it is complicated because
5111 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5112 // Other characters are done below to avoid problems with things like
5113 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5115 {
5116 stuffnumReadbuff(cap->count1);
5117 stuffcharReadbuff('R');
5118 stuffcharReadbuff('\t');
5119 stuffcharReadbuff(ESC);
5120 return;
5121 }
5122
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005123 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (u_save_cursor() == FAIL)
5125 return;
5126
5127 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5128 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005129 // Replace character(s) by a single newline.
5130 // Strange vi behaviour: Only one newline is inserted.
5131 // Delete the characters here.
5132 // Insert the newline with an insert command, takes care of
5133 // autoindent. The insert command depends on being on the last
5134 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005135 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 stuffcharReadbuff('\r');
5137 stuffcharReadbuff(ESC);
5138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005139 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 invoke_edit(cap, TRUE, 'r', FALSE);
5141 }
5142 else
5143 {
5144 prep_redo(cap->oap->regname, cap->count1,
5145 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5146
5147 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (has_mbyte)
5149 {
5150 int old_State = State;
5151
5152 if (cap->ncharC1 != 0)
5153 AppendCharToRedobuff(cap->ncharC1);
5154 if (cap->ncharC2 != 0)
5155 AppendCharToRedobuff(cap->ncharC2);
5156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005157 // This is slow, but it handles replacing a single-byte with a
5158 // multi-byte and the other way around. Also handles adding
5159 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 for (n = cap->count1; n > 0; --n)
5161 {
5162 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005163 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5164 {
5165 int c = ins_copychar(curwin->w_cursor.lnum
5166 + (cap->nchar == Ctrl_Y ? -1 : 1));
5167 if (c != NUL)
5168 ins_char(c);
5169 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005170 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005171 ++curwin->w_cursor.col;
5172 }
5173 else
5174 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 State = old_State;
5176 if (cap->ncharC1 != 0)
5177 ins_char(cap->ncharC1);
5178 if (cap->ncharC2 != 0)
5179 ins_char(cap->ncharC2);
5180 }
5181 }
5182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005184 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 for (n = cap->count1; n > 0; --n)
5186 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005187 // Get ptr again, because u_save and/or showmatch() will have
5188 // released the line. This may also happen in ins_copychar().
5189 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02005190 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5191 {
5192 int c = ins_copychar(curwin->w_cursor.lnum
5193 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005194
5195 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005196 if (c != NUL)
5197 ptr[curwin->w_cursor.col] = c;
5198 }
5199 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005200 {
5201 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005202 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (p_sm && msg_silent == 0)
5205 showmatch(cap->nchar);
5206 ++curwin->w_cursor.col;
5207 }
5208#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005209 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005211 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
Bram Moolenaar009b2592004-10-24 19:18:58 +00005213 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005214 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005216 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218#endif
5219
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005220 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 changed_bytes(curwin->w_cursor.lnum,
5222 (colnr_T)(curwin->w_cursor.col - cap->count1));
5223 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005224 --curwin->w_cursor.col; // cursor on the last replaced char
5225 // if the character on the left of the current cursor is a multi-byte
5226 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 if (has_mbyte)
5228 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 curbuf->b_op_end = curwin->w_cursor;
5230 curwin->w_set_curswant = TRUE;
5231 set_last_insert(cap->nchar);
5232 }
5233}
5234
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235/*
5236 * 'o': Exchange start and end of Visual area.
5237 * 'O': same, but in block mode exchange left and right corners.
5238 */
5239 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005240v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241{
5242 pos_T old_cursor;
5243 colnr_T left, right;
5244
5245 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5246 {
5247 old_cursor = curwin->w_cursor;
5248 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5249 curwin->w_cursor.lnum = VIsual.lnum;
5250 coladvance(left);
5251 VIsual = curwin->w_cursor;
5252
5253 curwin->w_cursor.lnum = old_cursor.lnum;
5254 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005255 // 'selection "exclusive" and cursor at right-bottom corner: move it
5256 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5258 ++curwin->w_curswant;
5259 coladvance(curwin->w_curswant);
5260 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005262 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 {
5264 curwin->w_cursor.lnum = VIsual.lnum;
5265 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5266 ++right;
5267 coladvance(right);
5268 VIsual = curwin->w_cursor;
5269
5270 curwin->w_cursor.lnum = old_cursor.lnum;
5271 coladvance(left);
5272 curwin->w_curswant = left;
5273 }
5274 }
5275 else
5276 {
5277 old_cursor = curwin->w_cursor;
5278 curwin->w_cursor = VIsual;
5279 VIsual = old_cursor;
5280 curwin->w_set_curswant = TRUE;
5281 }
5282}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
5284/*
5285 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5286 */
5287 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005288nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005290 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 cap->cmdchar = 'c';
5293 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005294 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 VIsual_mode = 'V';
5296 nv_operator(cap);
5297 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005298 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
5300 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005301 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 else
5303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (virtual_active())
5305 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5307 }
5308 }
5309}
5310
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311/*
5312 * "gr".
5313 */
5314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005315nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (VIsual_active)
5318 {
5319 cap->cmdchar = 'r';
5320 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005321 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005323 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
5325 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005326 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 else
5328 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005329 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005330 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 stuffcharReadbuff(cap->extra_char);
5332 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 if (virtual_active())
5334 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 invoke_edit(cap, TRUE, 'v', FALSE);
5336 }
5337 }
5338}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
5340/*
5341 * Swap case for "~" command, when it does not work like an operator.
5342 */
5343 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005344n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345{
5346 long n;
5347 pos_T startpos;
5348 int did_change = 0;
5349#ifdef FEAT_NETBEANS_INTG
5350 pos_T pos;
5351 char_u *ptr;
5352 int count;
5353#endif
5354
5355 if (checkclearopq(cap->oap))
5356 return;
5357
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005358 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
5360 clearopbeep(cap->oap);
5361 return;
5362 }
5363
5364 prep_redo_cmd(cap);
5365
5366 if (u_save_cursor() == FAIL)
5367 return;
5368
5369 startpos = curwin->w_cursor;
5370#ifdef FEAT_NETBEANS_INTG
5371 pos = startpos;
5372#endif
5373 for (n = cap->count1; n > 0; --n)
5374 {
5375 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5376 inc_cursor();
5377 if (gchar_cursor() == NUL)
5378 {
5379 if (vim_strchr(p_ww, '~') != NULL
5380 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5381 {
5382#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005383 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
5385 if (did_change)
5386 {
5387 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005388 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005389 netbeans_removed(curbuf, pos.lnum, pos.col,
5390 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005392 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394 pos.col = 0;
5395 pos.lnum++;
5396 }
5397#endif
5398 ++curwin->w_cursor.lnum;
5399 curwin->w_cursor.col = 0;
5400 if (n > 1)
5401 {
5402 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5403 break;
5404 u_clearline();
5405 }
5406 }
5407 else
5408 break;
5409 }
5410 }
5411#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005412 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 ptr = ml_get(pos.lnum);
5415 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005416 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5417 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
5419#endif
5420
5421
5422 check_cursor();
5423 curwin->w_set_curswant = TRUE;
5424 if (did_change)
5425 {
5426 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5427 0L);
5428 curbuf->b_op_start = startpos;
5429 curbuf->b_op_end = curwin->w_cursor;
5430 if (curbuf->b_op_end.col > 0)
5431 --curbuf->b_op_end.col;
5432 }
5433}
5434
5435/*
5436 * Move cursor to mark.
5437 */
5438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005439nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 if (check_mark(pos) == FAIL)
5442 clearop(cap->oap);
5443 else
5444 {
5445 if (cap->cmdchar == '\''
5446 || cap->cmdchar == '`'
5447 || cap->cmdchar == '['
5448 || cap->cmdchar == ']')
5449 setpcmark();
5450 curwin->w_cursor = *pos;
5451 if (flag)
5452 beginline(BL_WHITE | BL_FIX);
5453 else
5454 check_cursor();
5455 }
5456 cap->oap->motion_type = flag ? MLINE : MCHAR;
5457 if (cap->cmdchar == '`')
5458 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005459 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 curwin->w_set_curswant = TRUE;
5461}
5462
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463/*
5464 * Handle commands that are operators in Visual mode.
5465 */
5466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005467v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
5469 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005471 // Uppercase means linewise, except in block mode, then "D" deletes till
5472 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 if (isupper(cap->cmdchar))
5474 {
5475 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005476 {
5477 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5481 curwin->w_curswant = MAXCOL;
5482 }
5483 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5484 nv_operator(cap);
5485}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
5487/*
5488 * "s" and "S" commands.
5489 */
5490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005491nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005493#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005494 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005495 if (term_swap_diff() == OK)
5496 return;
5497#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005498#ifdef FEAT_JOB_CHANNEL
5499 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5500 {
5501 clearopbeep(cap->oap);
5502 return;
5503 }
5504#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005505 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005508 {
5509 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 cap->cmdchar = 'c';
5513 nv_operator(cap);
5514 }
5515 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 nv_optrans(cap);
5517}
5518
5519/*
5520 * Abbreviated commands.
5521 */
5522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005523nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005526 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005528 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 if (VIsual_active)
5530 v_visop(cap);
5531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 nv_optrans(cap);
5533}
5534
5535/*
5536 * Translate a command into another command.
5537 */
5538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005539nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5542 (char_u *)"d$", (char_u *)"c$",
5543 (char_u *)"cl", (char_u *)"cc",
5544 (char_u *)"yy", (char_u *)":s\r"};
5545 static char_u *str = (char_u *)"xXDCsSY&";
5546
5547 if (!checkclearopq(cap->oap))
5548 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005549 // In Vi "2D" doesn't delete the next line. Can't translate it
5550 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005551 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5552 {
5553 cap->oap->start = curwin->w_cursor;
5554 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005555#ifdef FEAT_EVAL
5556 set_op_var(OP_DELETE);
5557#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005558 cap->count1 = 1;
5559 nv_dollar(cap);
5560 finish_op = TRUE;
5561 ResetRedobuff();
5562 AppendCharToRedobuff('D');
5563 }
5564 else
5565 {
5566 if (cap->count0)
5567 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005568 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571 cap->opcount = 0;
5572}
5573
5574/*
5575 * "'" and "`" commands. Also for "g'" and "g`".
5576 * cap->arg is TRUE for "'" and "g'".
5577 */
5578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005579nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 pos_T *pos;
5582 int c;
5583#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005584 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005585 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586#endif
5587
5588 if (cap->cmdchar == 'g')
5589 c = cap->extra_char;
5590 else
5591 c = cap->nchar;
5592 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005593 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
5595 if (cap->arg)
5596 {
5597 check_cursor_lnum();
5598 beginline(BL_WHITE | BL_FIX);
5599 }
5600 else
5601 check_cursor();
5602 }
5603 else
5604 nv_cursormark(cap, cap->arg, pos);
5605
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005606 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 if (!virtual_active())
5608 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005609 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610#ifdef FEAT_FOLDING
5611 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005612 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005613 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 && (fdo_flags & FDO_MARK)
5615 && old_KeyTyped)
5616 foldOpenCursor();
5617#endif
5618}
5619
5620/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005621 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 */
5623 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005624nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005627#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005629 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632 if (!checkclearopq(cap->oap))
5633 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005634 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5635 {
5636 if (goto_tabpage_lastused() == FAIL)
5637 clearopbeep(cap->oap);
5638 return;
5639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 if (cap->cmdchar == 'g')
5641 pos = movechangelist((int)cap->count1);
5642 else
5643 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005644 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
5646 curwin->w_set_curswant = TRUE;
5647 check_cursor();
5648 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005649 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 nv_cursormark(cap, FALSE, pos);
5651 else if (cap->cmdchar == 'g')
5652 {
5653 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005654 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005656 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005658 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660 else
5661 clearopbeep(cap->oap);
5662# ifdef FEAT_FOLDING
5663 if (cap->oap->op_type == OP_NOP
5664 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5665 && (fdo_flags & FDO_MARK)
5666 && old_KeyTyped)
5667 foldOpenCursor();
5668# endif
5669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670}
5671
5672/*
5673 * Handle '"' command.
5674 */
5675 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005676nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 if (checkclearop(cap->oap))
5679 return;
5680#ifdef FEAT_EVAL
5681 if (cap->nchar == '=')
5682 cap->nchar = get_expr_register();
5683#endif
5684 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5685 {
5686 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005687 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688#ifdef FEAT_EVAL
5689 set_reg_var(cap->oap->regname);
5690#endif
5691 }
5692 else
5693 clearopbeep(cap->oap);
5694}
5695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696/*
5697 * Handle "v", "V" and "CTRL-V" commands.
5698 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5699 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005700 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 */
5702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005703nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005705 if (cap->cmdchar == Ctrl_Q)
5706 cap->cmdchar = Ctrl_V;
5707
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005708 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5709 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 if (cap->oap->op_type != OP_NOP)
5711 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005712 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005713 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 return;
5715 }
5716
5717 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005718 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005720 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005722 else // toggle char/block mode
5723 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 VIsual_mode = cap->cmdchar;
5725 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005726 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005728 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005730 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
5732 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005733 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005735 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 VIsual = curwin->w_cursor;
5737
5738 VIsual_active = TRUE;
5739 VIsual_reselect = TRUE;
5740 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005741 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005744 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005745 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005746 // For V and ^V, we multiply the number of lines even if there
5747 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5749 {
5750 curwin->w_cursor.lnum +=
5751 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005752 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754 VIsual_mode = resel_VIsual_mode;
5755 if (VIsual_mode == 'v')
5756 {
5757 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005758 {
5759 validate_virtcol();
5760 curwin->w_curswant = curwin->w_virtcol
5761 + resel_VIsual_vcol * cap->count0 - 1;
5762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005764 curwin->w_curswant = resel_VIsual_vcol;
5765 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005767 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 {
5769 curwin->w_curswant = MAXCOL;
5770 coladvance((colnr_T)MAXCOL);
5771 }
5772 else if (VIsual_mode == Ctrl_V)
5773 {
5774 validate_virtcol();
5775 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005776 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 coladvance(curwin->w_curswant);
5778 }
5779 else
5780 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005781 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783 else
5784 {
5785 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005786 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 may_start_select('c');
5788 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005789 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005790 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005791 if (cap->count0 > 0 && --cap->count1 > 0)
5792 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005793 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005794 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5795 nv_right(cap);
5796 else if (VIsual_mode == 'V')
5797 nv_down(cap);
5798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
5800 }
5801}
5802
5803/*
5804 * Start selection for Shift-movement keys.
5805 */
5806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005807start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005809 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 may_start_select('k');
5811 n_start_visual_mode('v');
5812}
5813
5814/*
5815 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5816 */
5817 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005818may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
5820 VIsual_select = (stuff_empty() && typebuf_typed()
5821 && (vim_strchr(p_slm, c) != NULL));
5822}
5823
5824/*
5825 * Start Visual mode "c".
5826 * Should set VIsual_select before calling this.
5827 */
5828 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005829n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005831#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005832 int cursor_line_was_concealed = curwin->w_p_cole > 0
5833 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005834#endif
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 VIsual_mode = c;
5837 VIsual_active = TRUE;
5838 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005839
5840 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005841 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005842 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005843 {
5844 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 VIsual = curwin->w_cursor;
5848
5849#ifdef FEAT_FOLDING
5850 foldAdjustVisual();
5851#endif
5852
Bram Moolenaara0620062021-11-17 16:52:40 +00005853 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005855#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005856 // Check if redraw is needed after changing the state.
5857 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005858#endif
5859
Bram Moolenaar09df3122006-01-23 22:23:09 +00005860 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005861 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005863 // Make sure the clipboard gets updated. Needed because start and
5864 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 clip_star.vmode = NUL;
5866#endif
5867
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005868 // Only need to redraw this line, unless still need to redraw an old
5869 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 if (curwin->w_redr_type < INVERTED)
5871 {
5872 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5873 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5874 }
5875}
5876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
5878/*
5879 * CTRL-W: Window commands
5880 */
5881 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005882nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005884 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005885 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005886 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005887 cap->cmdchar = ':';
5888 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005889 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005890 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005891 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005892 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893}
5894
5895/*
5896 * CTRL-Z: Suspend
5897 */
5898 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005899nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005903 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005904 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905}
5906
5907/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005908 * "gv": Reselect the previous Visual area. If Visual already active,
5909 * exchange previous and current Visual area.
5910 */
5911 static void
5912nv_gv_cmd(cmdarg_T *cap)
5913{
5914 pos_T tpos;
5915 int i;
5916
5917 if (checkclearop(cap->oap))
5918 return;
5919
5920 if (curbuf->b_visual.vi_start.lnum == 0
5921 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5922 || curbuf->b_visual.vi_end.lnum == 0)
5923 {
5924 beep_flush();
5925 return;
5926 }
5927
5928 // set w_cursor to the start of the Visual area, tpos to the end
5929 if (VIsual_active)
5930 {
5931 i = VIsual_mode;
5932 VIsual_mode = curbuf->b_visual.vi_mode;
5933 curbuf->b_visual.vi_mode = i;
5934# ifdef FEAT_EVAL
5935 curbuf->b_visual_mode_eval = i;
5936# endif
5937 i = curwin->w_curswant;
5938 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5939 curbuf->b_visual.vi_curswant = i;
5940
5941 tpos = curbuf->b_visual.vi_end;
5942 curbuf->b_visual.vi_end = curwin->w_cursor;
5943 curwin->w_cursor = curbuf->b_visual.vi_start;
5944 curbuf->b_visual.vi_start = VIsual;
5945 }
5946 else
5947 {
5948 VIsual_mode = curbuf->b_visual.vi_mode;
5949 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5950 tpos = curbuf->b_visual.vi_end;
5951 curwin->w_cursor = curbuf->b_visual.vi_start;
5952 }
5953
5954 VIsual_active = TRUE;
5955 VIsual_reselect = TRUE;
5956
5957 // Set Visual to the start and w_cursor to the end of the Visual
5958 // area. Make sure they are on an existing character.
5959 check_cursor();
5960 VIsual = curwin->w_cursor;
5961 curwin->w_cursor = tpos;
5962 check_cursor();
5963 update_topline();
5964
5965 // When called from normal "g" command: start Select mode when
5966 // 'selectmode' contains "cmd". When called for K_SELECT, always
5967 // start Select mode.
5968 if (cap->arg)
5969 {
5970 VIsual_select = TRUE;
5971 VIsual_select_reg = 0;
5972 }
5973 else
5974 may_start_select('c');
5975 setmouse();
5976#ifdef FEAT_CLIPBOARD
5977 // Make sure the clipboard gets updated. Needed because start and
5978 // end are still the same, and the selection needs to be owned
5979 clip_star.vmode = NUL;
5980#endif
5981 redraw_curbuf_later(INVERTED);
5982 showmode();
5983}
5984
5985/*
5986 * "g0", "g^" : Like "0" and "^" but for screen lines.
5987 * "gm": middle of "g0" and "g$".
5988 */
5989 static void
5990nv_g_home_m_cmd(cmdarg_T *cap)
5991{
5992 int i;
5993 int flag = FALSE;
5994
5995 if (cap->nchar == '^')
5996 flag = TRUE;
5997
5998 cap->oap->motion_type = MCHAR;
5999 cap->oap->inclusive = FALSE;
6000 if (curwin->w_p_wrap && curwin->w_width != 0)
6001 {
6002 int width1 = curwin->w_width - curwin_col_off();
6003 int width2 = width1 + curwin_col_off2();
6004
6005 validate_virtcol();
6006 i = 0;
6007 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6008 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6009 }
6010 else
6011 i = curwin->w_leftcol;
6012 // Go to the middle of the screen line. When 'number' or
6013 // 'relativenumber' is on and lines are wrapping the middle can be more
6014 // to the left.
6015 if (cap->nchar == 'm')
6016 i += (curwin->w_width - curwin_col_off()
6017 + ((curwin->w_p_wrap && i > 0)
6018 ? curwin_col_off2() : 0)) / 2;
6019 coladvance((colnr_T)i);
6020 if (flag)
6021 {
6022 do
6023 i = gchar_cursor();
6024 while (VIM_ISWHITE(i) && oneright() == OK);
6025 curwin->w_valid &= ~VALID_WCOL;
6026 }
6027 curwin->w_set_curswant = TRUE;
6028}
6029
6030/*
6031 * "g_": to the last non-blank character in the line or <count> lines
6032 * downward.
6033 */
6034 static void
6035nv_g_underscore_cmd(cmdarg_T *cap)
6036{
6037 char_u *ptr;
6038
6039 cap->oap->motion_type = MCHAR;
6040 cap->oap->inclusive = TRUE;
6041 curwin->w_curswant = MAXCOL;
6042 if (cursor_down((long)(cap->count1 - 1),
6043 cap->oap->op_type == OP_NOP) == FAIL)
6044 {
6045 clearopbeep(cap->oap);
6046 return;
6047 }
6048
6049 ptr = ml_get_curline();
6050
6051 // In Visual mode we may end up after the line.
6052 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6053 --curwin->w_cursor.col;
6054
6055 // Decrease the cursor column until it's on a non-blank.
6056 while (curwin->w_cursor.col > 0
6057 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
6058 --curwin->w_cursor.col;
6059 curwin->w_set_curswant = TRUE;
6060 adjust_for_sel(cap);
6061}
6062
6063/*
6064 * "g$" : Like "$" but for screen lines.
6065 */
6066 static void
6067nv_g_dollar_cmd(cmdarg_T *cap)
6068{
6069 oparg_T *oap = cap->oap;
6070 int i;
6071 int col_off = curwin_col_off();
6072
6073 oap->motion_type = MCHAR;
6074 oap->inclusive = TRUE;
6075 if (curwin->w_p_wrap && curwin->w_width != 0)
6076 {
6077 curwin->w_curswant = MAXCOL; // so we stay at the end
6078 if (cap->count1 == 1)
6079 {
6080 int width1 = curwin->w_width - col_off;
6081 int width2 = width1 + curwin_col_off2();
6082
6083 validate_virtcol();
6084 i = width1 - 1;
6085 if (curwin->w_virtcol >= (colnr_T)width1)
6086 i += ((curwin->w_virtcol - width1) / width2 + 1)
6087 * width2;
6088 coladvance((colnr_T)i);
6089
6090 // Make sure we stick in this column.
6091 validate_virtcol();
6092 curwin->w_curswant = curwin->w_virtcol;
6093 curwin->w_set_curswant = FALSE;
6094 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6095 {
6096 // Check for landing on a character that got split at
6097 // the end of the line. We do not want to advance to
6098 // the next screen line.
6099 if (curwin->w_virtcol > (colnr_T)i)
6100 --curwin->w_cursor.col;
6101 }
6102 }
6103 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6104 clearopbeep(oap);
6105 }
6106 else
6107 {
6108 if (cap->count1 > 1)
6109 // if it fails, let the cursor still move to the last char
6110 (void)cursor_down(cap->count1 - 1, FALSE);
6111
6112 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
6113 coladvance((colnr_T)i);
6114
6115 // if the character doesn't fit move one back
6116 if (curwin->w_cursor.col > 0
6117 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6118 {
6119 colnr_T vcol;
6120
6121 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6122 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6123 --curwin->w_cursor.col;
6124 }
6125
6126 // Make sure we stick in this column.
6127 validate_virtcol();
6128 curwin->w_curswant = curwin->w_virtcol;
6129 curwin->w_set_curswant = FALSE;
6130 }
6131}
6132
6133/*
6134 * "gi": start Insert at the last position.
6135 */
6136 static void
6137nv_gi_cmd(cmdarg_T *cap)
6138{
6139 int i;
6140
6141 if (curbuf->b_last_insert.lnum != 0)
6142 {
6143 curwin->w_cursor = curbuf->b_last_insert;
6144 check_cursor_lnum();
6145 i = (int)STRLEN(ml_get_curline());
6146 if (curwin->w_cursor.col > (colnr_T)i)
6147 {
6148 if (virtual_active())
6149 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
6150 curwin->w_cursor.col = i;
6151 }
6152 }
6153 cap->cmdchar = 'i';
6154 nv_edit(cap);
6155}
6156
6157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 * Commands starting with "g".
6159 */
6160 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006161nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
6163 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166 switch (cap->nchar)
6167 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006168 case Ctrl_A:
6169 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006171 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006172 if (!VIsual_active && cap->nchar == Ctrl_A)
6173 vim_mem_profile_dump();
6174 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006176 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006177 if (VIsual_active)
6178 {
6179 cap->arg = TRUE;
6180 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006181 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006182 nv_addsub(cap);
6183 }
6184 else
6185 clearopbeep(oap);
6186 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006188 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 case 'R':
6190 cap->arg = TRUE;
6191 nv_Replace(cap);
6192 break;
6193
6194 case 'r':
6195 nv_vreplace(cap);
6196 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197
6198 case '&':
6199 do_cmdline_cmd((char_u *)"%s//~/&");
6200 break;
6201
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006202 // "gv": Reselect the previous Visual area. If Visual already active,
6203 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006205 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006207
6208 // "gV": Don't reselect the previous Visual area after a Select mode
6209 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 case 'V':
6211 VIsual_reselect = FALSE;
6212 break;
6213
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006214 // "gh": start Select mode.
6215 // "gH": start Select line mode.
6216 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 case K_BS:
6218 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006219 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 case 'h':
6221 case 'H':
6222 case Ctrl_H:
6223# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006224 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 if (cap->nchar == Ctrl_H)
6226 cap->cmdchar = Ctrl_V;
6227 else
6228# endif
6229 cap->cmdchar = cap->nchar + ('v' - 'h');
6230 cap->arg = TRUE;
6231 nv_visual(cap);
6232 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006234 // "gn", "gN" visually select next/previous search match
6235 // "gn" selects next match
6236 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006237 case 'N':
6238 case 'n':
6239 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006240 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006243 // "gj" and "gk" two new funny movement keys -- up and down
6244 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 case 'j':
6246 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006247 // with 'nowrap' it works just like the normal "j" command.
6248 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
6250 oap->motion_type = MLINE;
6251 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6252 }
6253 else
6254 i = nv_screengo(oap, FORWARD, cap->count1);
6255 if (i == FAIL)
6256 clearopbeep(oap);
6257 break;
6258
6259 case 'k':
6260 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006261 // with 'nowrap' it works just like the normal "k" command.
6262 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 {
6264 oap->motion_type = MLINE;
6265 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6266 }
6267 else
6268 i = nv_screengo(oap, BACKWARD, cap->count1);
6269 if (i == FAIL)
6270 clearopbeep(oap);
6271 break;
6272
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006273 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 case 'J':
6275 nv_join(cap);
6276 break;
6277
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006278 // "g0", "g^" : Like "0" and "^" but for screen lines.
6279 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 case '0':
6282 case 'm':
6283 case K_HOME:
6284 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006285 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 break;
6287
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006288 case 'M':
6289 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006290 oap->motion_type = MCHAR;
6291 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006292 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006293 if (cap->count0 > 0 && cap->count0 <= 100)
6294 coladvance((colnr_T)(i * cap->count0 / 100));
6295 else
6296 coladvance((colnr_T)(i / 2));
6297 curwin->w_set_curswant = TRUE;
6298 }
6299 break;
6300
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006301 // "g_": to the last non-blank character in the line or <count> lines
6302 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006304 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 break;
6306
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006307 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 case '$':
6309 case K_END:
6310 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006311 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 break;
6313
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006314 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 case '*':
6316 case '#':
6317#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006318 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006320 case Ctrl_RSB: // :tag or :tselect for current identifier
6321 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 nv_ident(cap);
6323 break;
6324
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006325 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 case 'e':
6327 case 'E':
6328 oap->motion_type = MCHAR;
6329 curwin->w_set_curswant = TRUE;
6330 oap->inclusive = TRUE;
6331 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6332 clearopbeep(oap);
6333 break;
6334
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006335 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006337 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 break;
6339
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006340 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006342 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 break;
6344
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006345 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 case 'I':
6347 beginline(0);
6348 if (!checkclearopq(oap))
6349 invoke_edit(cap, FALSE, 'g', FALSE);
6350 break;
6351
6352#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006353 // "gf": goto file, edit file under cursor
6354 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006356 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 nv_gotofile(cap);
6358 break;
6359#endif
6360
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006361 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 case '\'':
6363 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006364 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 case '`':
6366 nv_gomark(cap);
6367 break;
6368
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006369 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006371 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 break;
6373
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006374 // "ga": Display the ascii value of the character under the
6375 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 case 'a':
6377 do_ascii(NULL);
6378 break;
6379
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006380 // "g8": Display the bytes used for the UTF-8 character under the
6381 // cursor. It is displayed in hex.
6382 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006384 if (cap->count0 == 8)
6385 utf_find_illegal();
6386 else
6387 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006390 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006391 case '<':
6392 show_sb_text();
6393 break;
6394
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006395 // "gg": Goto the first line in file. With a count it goes to
6396 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 case 'g':
6398 cap->arg = FALSE;
6399 nv_goto(cap);
6400 break;
6401
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006402 // Two-character operators:
6403 // "gq" Format text
6404 // "gw" Format text and keep cursor position
6405 // "g~" Toggle the case of the text.
6406 // "gu" Change text to lower case.
6407 // "gU" Change text to upper case.
6408 // "g?" rot13 encoding
6409 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 case 'q':
6411 case 'w':
6412 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006413 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 case '~':
6415 case 'u':
6416 case 'U':
6417 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006418 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 nv_operator(cap);
6420 break;
6421
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006422 // "gd": Find first occurrence of pattern under the cursor in the
6423 // current function
6424 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 case 'd':
6426 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006427 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 break;
6429
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006430 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 case K_MIDDLEMOUSE:
6432 case K_MIDDLEDRAG:
6433 case K_MIDDLERELEASE:
6434 case K_LEFTMOUSE:
6435 case K_LEFTDRAG:
6436 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006437 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 case K_RIGHTMOUSE:
6439 case K_RIGHTDRAG:
6440 case K_RIGHTRELEASE:
6441 case K_X1MOUSE:
6442 case K_X1DRAG:
6443 case K_X1RELEASE:
6444 case K_X2MOUSE:
6445 case K_X2DRAG:
6446 case K_X2RELEASE:
6447 mod_mask = MOD_MASK_CTRL;
6448 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6449 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
6451 case K_IGNORE:
6452 break;
6453
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006454 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 case 'p':
6456 case 'P':
6457 nv_put(cap);
6458 break;
6459
6460#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006461 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 case 'o':
6463 goto_byte(cap->count0);
6464 break;
6465#endif
6466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006467 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006469 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
6471 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006472 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 break;
6474 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006475
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 if (!checkclearopq(oap))
6477 do_exmode(TRUE);
6478 break;
6479
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 case ',':
6481 nv_pcmark(cap);
6482 break;
6483
6484 case ';':
6485 cap->count1 = -cap->count1;
6486 nv_pcmark(cap);
6487 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006489 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006490 if (!checkclearop(oap))
6491 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006492 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006493 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006494 if (!checkclearop(oap))
6495 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006496 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006497
Bram Moolenaar62a23252020-08-09 14:04:42 +02006498 case TAB:
6499 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6500 clearopbeep(oap);
6501 break;
6502
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006503 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006504 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006505 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006506 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006507 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006508 break;
6509
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 default:
6511 clearopbeep(oap);
6512 break;
6513 }
6514}
6515
6516/*
6517 * Handle "o" and "O" commands.
6518 */
6519 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006520n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006522#ifdef FEAT_CONCEAL
6523 linenr_T oldline = curwin->w_cursor.lnum;
6524#endif
6525
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 if (!checkclearopq(cap->oap))
6527 {
6528#ifdef FEAT_FOLDING
6529 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006530 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 (void)hasFolding(curwin->w_cursor.lnum,
6532 &curwin->w_cursor.lnum, NULL);
6533 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006534 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 (void)hasFolding(curwin->w_cursor.lnum,
6536 NULL, &curwin->w_cursor.lnum);
6537#endif
6538 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6539 (cap->cmdchar == 'O' ? 1 : 0)),
6540 (linenr_T)(curwin->w_cursor.lnum +
6541 (cap->cmdchar == 'o' ? 1 : 0))
6542 ) == OK
6543 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006544 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006545 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006547#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006548 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006549 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006550#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006551#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006552 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006553 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006554 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006555#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006556 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006557 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6558 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6560 }
6561 }
6562}
6563
6564/*
6565 * "." command: redo last change.
6566 */
6567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006568nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 if (!checkclearopq(cap->oap))
6571 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006572 // If "restart_edit" is TRUE, the last but one command is repeated
6573 // instead of the last command (inserting text). This is used for
6574 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6576 clearopbeep(cap->oap);
6577 }
6578}
6579
6580/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006581 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 */
6583 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006584nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006586 if (VIsual_select && VIsual_active)
6587 {
6588 int reg;
6589 // Get register name
6590 ++no_mapping;
6591 ++allow_keys;
6592 reg = plain_vgetc();
6593 LANGMAP_ADJUST(reg, TRUE);
6594 --no_mapping;
6595 --allow_keys;
6596
6597 if (reg == '"')
6598 // the unnamed register is 0
6599 reg = 0;
6600
6601 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
6602 return;
6603 }
6604
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 if (!checkclearopq(cap->oap))
6606 {
6607 u_redo((int)cap->count1);
6608 curwin->w_set_curswant = TRUE;
6609 }
6610}
6611
6612/*
6613 * Handle "U" command.
6614 */
6615 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006616nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006618 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006619 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006621 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 cap->cmdchar = 'g';
6623 cap->nchar = 'U';
6624 nv_operator(cap);
6625 }
6626 else if (!checkclearopq(cap->oap))
6627 {
6628 u_undoline();
6629 curwin->w_set_curswant = TRUE;
6630 }
6631}
6632
6633/*
6634 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6635 * single character.
6636 */
6637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006638nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006640 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006641 {
6642#ifdef FEAT_JOB_CHANNEL
6643 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6644 {
6645 clearopbeep(cap->oap);
6646 return;
6647 }
6648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 else
6652 nv_operator(cap);
6653}
6654
6655/*
6656 * Handle an operator command.
6657 * The actual work is done by do_pending_operator().
6658 */
6659 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006660nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 int op_type;
6663
6664 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006665#ifdef FEAT_JOB_CHANNEL
6666 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6667 {
6668 clearopbeep(cap->oap);
6669 return;
6670 }
6671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006673 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 nv_lineop(cap);
6675 else if (!checkclearop(cap->oap))
6676 {
6677 cap->oap->start = curwin->w_cursor;
6678 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006679#ifdef FEAT_EVAL
6680 set_op_var(op_type);
6681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 }
6683}
6684
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006685#ifdef FEAT_EVAL
6686/*
6687 * Set v:operator to the characters for "optype".
6688 */
6689 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006690set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006691{
6692 char_u opchars[3];
6693
6694 if (optype == OP_NOP)
6695 set_vim_var_string(VV_OP, NULL, 0);
6696 else
6697 {
6698 opchars[0] = get_op_char(optype);
6699 opchars[1] = get_extra_op_char(optype);
6700 opchars[2] = NUL;
6701 set_vim_var_string(VV_OP, opchars, -1);
6702 }
6703}
6704#endif
6705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706/*
6707 * Handle linewise operator "dd", "yy", etc.
6708 *
6709 * "_" is is a strange motion command that helps make operators more logical.
6710 * It is actually implemented, but not documented in the real Vi. This motion
6711 * command actually refers to "the current line". Commands like "dd" and "yy"
6712 * are really an alternate form of "d_" and "y_". It does accept a count, so
6713 * "d3_" works to delete 3 lines.
6714 */
6715 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006716nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
6718 cap->oap->motion_type = MLINE;
6719 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6720 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006721 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006722 && cap->oap->motion_force != 'v'
6723 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 || cap->oap->op_type == OP_LSHIFT
6725 || cap->oap->op_type == OP_RSHIFT)
6726 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006727 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 beginline(BL_WHITE | BL_FIX);
6729}
6730
6731/*
6732 * <Home> command.
6733 */
6734 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006735nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006737 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006738 if (mod_mask & MOD_MASK_CTRL)
6739 nv_goto(cap);
6740 else
6741 {
6742 cap->count0 = 1;
6743 nv_pipe(cap);
6744 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006745 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6746 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747}
6748
6749/*
6750 * "|" command.
6751 */
6752 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006753nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754{
6755 cap->oap->motion_type = MCHAR;
6756 cap->oap->inclusive = FALSE;
6757 beginline(0);
6758 if (cap->count0 > 0)
6759 {
6760 coladvance((colnr_T)(cap->count0 - 1));
6761 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6762 }
6763 else
6764 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006765 // keep curswant at the column where we wanted to go, not where
6766 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 curwin->w_set_curswant = FALSE;
6768}
6769
6770/*
6771 * Handle back-word command "b" and "B".
6772 * cap->arg is 1 for "B"
6773 */
6774 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006775nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 cap->oap->motion_type = MCHAR;
6778 cap->oap->inclusive = FALSE;
6779 curwin->w_set_curswant = TRUE;
6780 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6781 clearopbeep(cap->oap);
6782#ifdef FEAT_FOLDING
6783 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6784 foldOpenCursor();
6785#endif
6786}
6787
6788/*
6789 * Handle word motion commands "e", "E", "w" and "W".
6790 * cap->arg is TRUE for "E" and "W".
6791 */
6792 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006793nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 int n;
6796 int word_end;
6797 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006798 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006800 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6802 word_end = TRUE;
6803 else
6804 word_end = FALSE;
6805 cap->oap->inclusive = word_end;
6806
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006807 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 if (!word_end && cap->oap->op_type == OP_CHANGE)
6809 {
6810 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006811 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006813 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006815 // Reproduce a funny Vi behaviour: "cw" on a blank only
6816 // changes one character, not all blanks until the start of
6817 // the next word. Only do this when the 'w' flag is included
6818 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6820 {
6821 cap->oap->inclusive = TRUE;
6822 cap->oap->motion_type = MCHAR;
6823 return;
6824 }
6825 }
6826 else
6827 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006828 // This is a little strange. To match what the real Vi does,
6829 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6830 // that we are not on a space or a TAB. This seems impolite
6831 // at first, but it's really more what we mean when we say
6832 // 'cw'.
6833 // Another strangeness: When standing on the end of a word
6834 // "ce" will change until the end of the next word, but "cw"
6835 // will change only one character! This is done by setting
6836 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 cap->oap->inclusive = TRUE;
6838 word_end = TRUE;
6839 flag = TRUE;
6840 }
6841 }
6842 }
6843
6844 cap->oap->motion_type = MCHAR;
6845 curwin->w_set_curswant = TRUE;
6846 if (word_end)
6847 n = end_word(cap->count1, cap->arg, flag, FALSE);
6848 else
6849 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6850
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006851 // Don't leave the cursor on the NUL past the end of line. Unless we
6852 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006853 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006854 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 if (n == FAIL && cap->oap->op_type == OP_NOP)
6857 clearopbeep(cap->oap);
6858 else
6859 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861#ifdef FEAT_FOLDING
6862 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6863 foldOpenCursor();
6864#endif
6865 }
6866}
6867
6868/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006869 * Used after a movement command: If the cursor ends up on the NUL after the
6870 * end of the line, may move it back to the last character and make the motion
6871 * inclusive.
6872 */
6873 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006874adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006875{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006876 // The cursor cannot remain on the NUL when:
6877 // - the column is > 0
6878 // - not in Visual mode or 'selection' is "o"
6879 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006880 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006881 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006882 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006883 {
6884 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006885 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006886 if (has_mbyte)
6887 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006888 oap->inclusive = TRUE;
6889 }
6890}
6891
6892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 * "0" and "^" commands.
6894 * cap->arg is the argument for beginline().
6895 */
6896 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006897nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898{
6899 cap->oap->motion_type = MCHAR;
6900 cap->oap->inclusive = FALSE;
6901 beginline(cap->arg);
6902#ifdef FEAT_FOLDING
6903 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6904 foldOpenCursor();
6905#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006906 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6907 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908}
6909
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910/*
6911 * In exclusive Visual mode, may include the last character.
6912 */
6913 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006914adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
6916 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006917 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 if (has_mbyte)
6920 inc_cursor();
6921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 ++curwin->w_cursor.col;
6923 cap->oap->inclusive = FALSE;
6924 }
6925}
6926
6927/*
6928 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6929 * Should check VIsual_mode before calling this.
6930 * Returns TRUE when backed up to the previous line.
6931 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006932 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006933unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
6935 pos_T *pp;
6936
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006937 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006939 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 pp = &curwin->w_cursor;
6941 else
6942 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 if (pp->coladd > 0)
6944 --pp->coladd;
6945 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 if (pp->col > 0)
6947 {
6948 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006949 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 }
6951 else if (pp->lnum > 1)
6952 {
6953 --pp->lnum;
6954 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6955 return TRUE;
6956 }
6957 }
6958 return FALSE;
6959}
6960
6961/*
6962 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6963 */
6964 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006965nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006970 VIsual_select_reg = 0;
6971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 else if (VIsual_reselect)
6973 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006974 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 cap->arg = TRUE;
6976 nv_g_cmd(cap);
6977 }
6978}
6979
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
6981/*
6982 * "G", "gg", CTRL-END, CTRL-HOME.
6983 * cap->arg is TRUE for "G".
6984 */
6985 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006986nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 linenr_T lnum;
6989
6990 if (cap->arg)
6991 lnum = curbuf->b_ml.ml_line_count;
6992 else
6993 lnum = 1L;
6994 cap->oap->motion_type = MLINE;
6995 setpcmark();
6996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006997 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (cap->count0 != 0)
6999 lnum = cap->count0;
7000 if (lnum < 1L)
7001 lnum = 1L;
7002 else if (lnum > curbuf->b_ml.ml_line_count)
7003 lnum = curbuf->b_ml.ml_line_count;
7004 curwin->w_cursor.lnum = lnum;
7005 beginline(BL_SOL | BL_FIX);
7006#ifdef FEAT_FOLDING
7007 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
7008 foldOpenCursor();
7009#endif
7010}
7011
7012/*
7013 * CTRL-\ in Normal mode.
7014 */
7015 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007016nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017{
7018 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
7019 {
7020 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00007021 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007022 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 restart_edit = 0;
7024#ifdef FEAT_CMDWIN
7025 if (cmdwin_type != 0)
7026 cmdwin_result = Ctrl_C;
7027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 if (VIsual_active)
7029 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007030 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 redraw_curbuf_later(INVERTED);
7032 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007033 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (cap->nchar == Ctrl_G && p_im)
7035 restart_edit = 'a';
7036 }
7037 else
7038 clearopbeep(cap->oap);
7039}
7040
7041/*
7042 * ESC in Normal mode: beep, but don't flush buffers.
7043 * Don't even beep if we are canceling a command.
7044 */
7045 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007046nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 int no_reason;
7049
7050 no_reason = (cap->oap->op_type == OP_NOP
7051 && cap->opcount == 0
7052 && cap->count0 == 0
7053 && cap->oap->regname == 0
7054 && !p_im);
7055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007056 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
7058 if (restart_edit == 0
7059#ifdef FEAT_CMDWIN
7060 && cmdwin_type == 0
7061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007064 {
7065 if (anyBufIsChanged())
7066 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
7067 else
7068 msg(_("Type :qa and press <Enter> to exit Vim"));
7069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007071 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
7072 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 if (!p_im)
7074 restart_edit = 0;
7075#ifdef FEAT_CMDWIN
7076 if (cmdwin_type != 0)
7077 {
7078 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007079 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 return;
7081 }
7082#endif
7083 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01007084#ifdef FEAT_CMDWIN
7085 else if (cmdwin_type != 0 && ex_normal_busy)
7086 {
7087 // When :normal runs out of characters while in the command line window
7088 // vgetorpeek() will return ESC. Exit the cmdline window to break the
7089 // loop.
7090 cmdwin_result = K_IGNORE;
7091 return;
7092 }
7093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 if (VIsual_active)
7096 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007097 end_visual_mode(); // stop Visual
7098 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 curwin->w_set_curswant = TRUE;
7100 redraw_curbuf_later(INVERTED);
7101 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007102 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007103 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 clearop(cap->oap);
7105
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007106 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7107 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007108 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 restart_edit = 'a';
7110}
7111
7112/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007113 * Move the cursor for the "A" command.
7114 */
7115 void
7116set_cursor_for_append_to_line(void)
7117{
7118 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007119 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007120 {
7121 int save_State = State;
7122
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007123 // Pretend Insert mode here to allow the cursor on the
7124 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007125 State = INSERT;
7126 coladvance((colnr_T)MAXCOL);
7127 State = save_State;
7128 }
7129 else
7130 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7131}
7132
7133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007135 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 */
7137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007138nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007140 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7142 cap->cmdchar = 'i';
7143
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007144 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007146 {
7147#ifdef FEAT_TERMINAL
7148 if (term_in_normal_mode())
7149 {
7150 end_visual_mode();
7151 clearop(cap->oap);
7152 term_enter_job_mode();
7153 return;
7154 }
7155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007159 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007160 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7161 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {
7163#ifdef FEAT_TEXTOBJ
7164 nv_object(cap);
7165#else
7166 clearopbeep(cap->oap);
7167#endif
7168 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007169#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007170 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007171 {
7172 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007173 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007174 return;
7175 }
7176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 else if (!curbuf->b_p_ma && !p_im)
7178 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007179 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007180 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007182 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007183 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007184 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007186 else if (cap->cmdchar == K_PS && VIsual_active)
7187 {
7188 pos_T old_pos = curwin->w_cursor;
7189 pos_T old_visual = VIsual;
7190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007191 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007192 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7193 {
7194 shift_delete_registers();
7195 cap->oap->regname = '1';
7196 }
7197 else
7198 cap->oap->regname = '-';
7199 cap->cmdchar = 'd';
7200 cap->nchar = NUL;
7201 nv_operator(cap);
7202 do_pending_operator(cap, 0, FALSE);
7203 cap->cmdchar = K_PS;
7204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007205 // When the last char in the line was deleted then append. Detect this
7206 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007207 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7208 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007209 inc_cursor();
7210
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007211 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007212 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 else if (!checkclearopq(cap->oap))
7215 {
7216 switch (cap->cmdchar)
7217 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007218 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007219 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 break;
7221
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007222 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007223 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7224 beginline(BL_WHITE);
7225 else
7226 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 break;
7228
Bram Moolenaara1891842017-02-04 21:34:31 +01007229 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007230 // Bracketed paste works like "a"ppend, unless the cursor is in
7231 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007232 if (curwin->w_cursor.col == 0)
7233 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007234 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007235
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007236 case 'a': // "a"ppend is like "i"nsert on the next character.
7237 // increment coladd when in virtual space, increment the
7238 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 if (virtual_active()
7240 && (curwin->w_cursor.coladd > 0
7241 || *ml_get_cursor() == NUL
7242 || *ml_get_cursor() == TAB))
7243 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007244 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 inc_cursor();
7246 break;
7247 }
7248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7250 {
7251 int save_State = State;
7252
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007253 // Pretend Insert mode here to allow the cursor on the
7254 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 State = INSERT;
7256 coladvance(getviscol());
7257 State = save_State;
7258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
7260 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7261 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007262 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007263 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007264 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265}
7266
7267/*
7268 * Invoke edit() and take care of "restart_edit" and the return value.
7269 */
7270 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007271invoke_edit(
7272 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007273 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007274 int cmd,
7275 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
7277 int restart_edit_save = 0;
7278
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007279 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7280 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7281 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 if (repl || !stuff_empty())
7283 restart_edit_save = restart_edit;
7284 else
7285 restart_edit_save = 0;
7286
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007287 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 restart_edit = 0;
7289
7290 if (edit(cmd, startln, cap->count1))
7291 cap->retval |= CA_COMMAND_BUSY;
7292
7293 if (restart_edit == 0)
7294 restart_edit = restart_edit_save;
7295}
7296
7297#ifdef FEAT_TEXTOBJ
7298/*
7299 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7300 */
7301 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007302nv_object(
7303 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304{
7305 int flag;
7306 int include;
7307 char_u *mps_save;
7308
7309 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007310 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007312 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007314 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 mps_save = curbuf->b_p_mps;
7316 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7317
7318 switch (cap->nchar)
7319 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007320 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 flag = current_word(cap->oap, cap->count1, include, FALSE);
7322 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007323 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 flag = current_word(cap->oap, cap->count1, include, TRUE);
7325 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007326 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 case '(':
7328 case ')':
7329 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7330 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007331 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 case '{':
7333 case '}':
7334 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7335 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007336 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 case ']':
7338 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7339 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007340 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 case '>':
7342 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7343 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007344 case 't': // "at" = a tag block (xml and html)
7345 // Do not adjust oap->end in do_pending_operator()
7346 // otherwise there are different results for 'dit'
7347 // (note leading whitespace in last line):
7348 // 1) <b> 2) <b>
7349 // foobar foobar
7350 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007351 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007352 flag = current_tagblock(cap->oap, cap->count1, include);
7353 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007354 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 flag = current_par(cap->oap, cap->count1, include, 'p');
7356 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007357 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 flag = current_sent(cap->oap, cap->count1, include);
7359 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007360 case '"': // "a"" = a double quoted string
7361 case '\'': // "a'" = a single quoted string
7362 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007363 flag = current_quote(cap->oap, cap->count1, include,
7364 cap->nchar);
7365 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366#if 0 // TODO
7367 case 'S': // "aS" = a section
7368 case 'f': // "af" = a filename
7369 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370#endif
7371 default:
7372 flag = FAIL;
7373 break;
7374 }
7375
7376 curbuf->b_p_mps = mps_save;
7377 if (flag == FAIL)
7378 clearopbeep(cap->oap);
7379 adjust_cursor_col();
7380 curwin->w_set_curswant = TRUE;
7381}
7382#endif
7383
7384/*
7385 * "q" command: Start/stop recording.
7386 * "q:", "q/", "q?": edit command-line in command-line window.
7387 */
7388 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007389nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
7391 if (cap->oap->op_type == OP_FORMAT)
7392 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007393 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 cap->cmdchar = 'g';
7395 cap->nchar = 'q';
7396 nv_operator(cap);
7397 }
7398 else if (!checkclearop(cap->oap))
7399 {
7400#ifdef FEAT_CMDWIN
7401 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7402 {
7403 stuffcharReadbuff(cap->nchar);
7404 stuffcharReadbuff(K_CMDWIN);
7405 }
7406 else
7407#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007408 // (stop) recording into a named register, unless executing a
7409 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007410 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 clearopbeep(cap->oap);
7412 }
7413}
7414
7415/*
7416 * Handle the "@r" command.
7417 */
7418 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007419nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 if (checkclearop(cap->oap))
7422 return;
7423#ifdef FEAT_EVAL
7424 if (cap->nchar == '=')
7425 {
7426 if (get_expr_register() == NUL)
7427 return;
7428 }
7429#endif
7430 while (cap->count1-- && !got_int)
7431 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007432 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 {
7434 clearopbeep(cap->oap);
7435 break;
7436 }
7437 line_breakcheck();
7438 }
7439}
7440
7441/*
7442 * Handle the CTRL-U and CTRL-D commands.
7443 */
7444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007445nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446{
7447 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7448 || (cap->cmdchar == Ctrl_D
7449 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7450 clearopbeep(cap->oap);
7451 else if (!checkclearop(cap->oap))
7452 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7453}
7454
7455/*
7456 * Handle "J" or "gJ" command.
7457 */
7458 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007459nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007461 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007463 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {
7465 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007466 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7468 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007470 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007471 if (cap->count0 <= 2)
7472 {
7473 clearopbeep(cap->oap);
7474 return;
7475 }
7476 cap->count0 = curbuf->b_ml.ml_line_count
7477 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007479
7480 prep_redo(cap->oap->regname, cap->count0,
7481 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7482 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484}
7485
7486/*
7487 * "P", "gP", "p" and "gp" commands.
7488 */
7489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007490nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007492 nv_put_opt(cap, FALSE);
7493}
7494
7495/*
7496 * "P", "gP", "p" and "gp" commands.
7497 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7498 */
7499 static void
7500nv_put_opt(cmdarg_T *cap, int fix_indent)
7501{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 int regname = 0;
7503 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007504 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007505 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 int dir;
7507 int flags = 0;
7508
7509 if (cap->oap->op_type != OP_NOP)
7510 {
7511#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007512 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7514 {
7515 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007516 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 }
7518 else
7519#endif
7520 clearopbeep(cap->oap);
7521 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007522#ifdef FEAT_JOB_CHANNEL
7523 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7524 {
7525 clearopbeep(cap->oap);
7526 }
7527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 else
7529 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007530 if (fix_indent)
7531 {
7532 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7533 ? FORWARD : BACKWARD;
7534 flags |= PUT_FIXINDENT;
7535 }
7536 else
7537 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007538 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7539 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 prep_redo_cmd(cap);
7541 if (cap->cmdchar == 'g')
7542 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007543 else if (cap->cmdchar == 'z')
7544 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 if (VIsual_active)
7547 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007548 // Putting in Visual mode: The put text replaces the selected
7549 // text. First delete the selected text, then put the new text.
7550 // Need to save and restore the registers that the delete
7551 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007552 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007554#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007556#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007557 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007558 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007559#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
7563 )
7564 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007565 // The delete is going to overwrite the register we want to
7566 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 reg1 = get_register(regname, TRUE);
7568 }
7569
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007570 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 cap->cmdchar = 'd';
7572 cap->nchar = NUL;
7573 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007574 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 nv_operator(cap);
7576 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007577 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007578 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007580 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 cap->oap->regname = regname;
7582
7583 if (reg1 != NULL)
7584 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007585 // Delete probably changed the register we want to put, save
7586 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 reg2 = get_register(regname, FALSE);
7588 put_register(regname, reg1);
7589 }
7590
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007591 // When deleted a linewise Visual area, put the register as
7592 // lines to avoid it joined with the next line. When deletion was
7593 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 if (VIsual_mode == 'V')
7595 flags |= PUT_LINE;
7596 else if (VIsual_mode == 'v')
7597 flags |= PUT_LINE_SPLIT;
7598 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7599 flags |= PUT_LINE_FORWARD;
7600 dir = BACKWARD;
7601 if ((VIsual_mode != 'V'
7602 && curwin->w_cursor.col < curbuf->b_op_start.col)
7603 || (VIsual_mode == 'V'
7604 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007605 // cursor is at the end of the line or end of file, put
7606 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007608 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007609 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007611 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007613 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 if (reg2 != NULL)
7615 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007616
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007617 // What to reselect with "gv"? Selecting the just put text seems to
7618 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007619 if (was_visual)
7620 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007621 curbuf->b_visual.vi_start = curbuf->b_op_start;
7622 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007623 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007624 if (*p_sel == 'e')
7625 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007626 }
7627
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007628 // When all lines were selected and deleted do_put() leaves an empty
7629 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007630 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007631 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007632 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007633 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007634
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007635 // If the cursor was in that line, move it to the end of the last
7636 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007637 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7638 {
7639 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7640 coladvance((colnr_T)MAXCOL);
7641 }
7642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 auto_format(FALSE, TRUE);
7644 }
7645}
7646
7647/*
7648 * "o" and "O" commands.
7649 */
7650 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007651nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007654 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7656 {
7657 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007658 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 }
7660 else
7661#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007662 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007664#ifdef FEAT_JOB_CHANNEL
7665 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007666 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 n_opencmd(cap);
7670}
7671
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672#ifdef FEAT_NETBEANS_INTG
7673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007674nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
7676 netbeans_keycommand(cap->nchar);
7677}
7678#endif
7679
7680#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007682nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007684 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685}
7686#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007687
Bram Moolenaar3918c952005-03-15 22:34:55 +00007688/*
7689 * Trigger CursorHold event.
7690 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7691 * input buffer. "did_cursorhold" is set to avoid retriggering.
7692 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007694nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007695{
7696 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7697 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007698 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007699}