blob: e69c14196824b67ee68c6222671c182beb049489 [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
1057 // When recording the character will be recorded again, remove the
1058 // previously recording.
1059 ungetchars(len);
1060
Bram Moolenaar686f51e2005-05-20 21:19:57 +00001061 if (restart_edit != 0)
1062 c = 'd';
1063 else
1064 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001065 msg_nowait = TRUE; // don't delay going to insert mode
1066 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
Bram Moolenaard0fb9072021-12-09 11:57:22 +00001069 // If the window was made so small that nothing shows, make it at least one
1070 // line and one column when typing a command.
1071 if (KeyTyped && !KeyStuffed)
1072 win_ensure_size();
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#ifdef FEAT_CMDL_INFO
1075 need_flushbuf = add_to_showcmd(c);
1076#endif
1077
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001078 // Get the command count
1079 c = normal_cmd_get_count(&ca, c, toplevel, set_prevcount, &ctrl_w,
1080 &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001082 // Find the command character in the table of commands.
1083 // For CTRL-W we already got nchar when looking for a count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (ctrl_w)
1085 {
1086 ca.nchar = c;
1087 ca.cmdchar = Ctrl_W;
1088 }
1089 else
1090 ca.cmdchar = c;
1091 idx = find_command(ca.cmdchar);
1092 if (idx < 0)
1093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001094 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 clearopbeep(oap);
1096 goto normal_end;
1097 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001098
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001099 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001101 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001103 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 goto normal_end;
1105 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001106 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
1107 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001109 // In Visual/Select mode, a few keys are handled in a special way.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (VIsual_active)
1111 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001112 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 if (km_stopsel
1114 && (nv_cmds[idx].cmd_flags & NV_STS)
1115 && !(mod_mask & MOD_MASK_SHIFT))
1116 {
1117 end_visual_mode();
1118 redraw_curbuf_later(INVERTED);
1119 }
1120
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001121 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 if (km_startsel)
1123 {
1124 if (nv_cmds[idx].cmd_flags & NV_SS)
1125 {
1126 unshift_special(&ca);
1127 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001128 if (idx < 0)
1129 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001130 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001131 clearopbeep(oap);
1132 goto normal_end;
1133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1136 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
1141#ifdef FEAT_RIGHTLEFT
1142 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
1143 && (nv_cmds[idx].cmd_flags & NV_RL))
1144 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001145 // Invert horizontal movements and operations. Only when typed by the
1146 // user directly, not when the result of a mapping or "x" translated
1147 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 switch (ca.cmdchar)
1149 {
1150 case 'l': ca.cmdchar = 'h'; break;
1151 case K_RIGHT: ca.cmdchar = K_LEFT; break;
1152 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
1153 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
1154 case 'h': ca.cmdchar = 'l'; break;
1155 case K_LEFT: ca.cmdchar = K_RIGHT; break;
1156 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
1157 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
1158 case '>': ca.cmdchar = '<'; break;
1159 case '<': ca.cmdchar = '>'; break;
1160 }
1161 idx = find_command(ca.cmdchar);
1162 }
1163#endif
1164
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001165 // Get additional characters if we need them.
1166 if (normal_cmd_needs_more_chars(&ca, nv_cmds[idx].cmd_flags))
1167 idx = normal_cmd_get_more_chars(idx, &ca, &need_flushbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168
1169#ifdef FEAT_CMDL_INFO
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001170 // Flush the showcmd characters onto the screen so we can see them while
1171 // the command is being executed. Only do this when the shown command was
1172 // actually displayed, otherwise this will slow down a lot when executing
1173 // mappings.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (need_flushbuf)
1175 out_flush();
1176#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001177 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001178 {
1179 if (ex_normal_busy)
1180 did_cursorhold = save_did_cursorhold;
1181 else
1182 did_cursorhold = FALSE;
1183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
1185 State = NORMAL;
1186
1187 if (ca.nchar == ESC)
1188 {
1189 clearop(oap);
1190 if (restart_edit == 0 && goto_im())
1191 restart_edit = 'a';
1192 goto normal_end;
1193 }
1194
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001195 if (ca.cmdchar != K_IGNORE)
1196 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001197 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001198 msg_col = 0;
1199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001201 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001203 // When 'keymodel' contains "startsel" some keys start Select/Visual
1204 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 if (!VIsual_active && km_startsel)
1206 {
1207 if (nv_cmds[idx].cmd_flags & NV_SS)
1208 {
1209 start_selection();
1210 unshift_special(&ca);
1211 idx = find_command(ca.cmdchar);
1212 }
1213 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1214 && (mod_mask & MOD_MASK_SHIFT))
1215 {
1216 start_selection();
1217 mod_mask &= ~MOD_MASK_SHIFT;
1218 }
1219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001221 // Execute the command!
1222 // Call the command function found in the commands table.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 ca.arg = nv_cmds[idx].cmd_arg;
1224 (nv_cmds[idx].cmd_func)(&ca);
1225
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001226 // If we didn't start or finish an operator, reset oap->regname, unless we
1227 // need it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (!finish_op
1229 && !oap->op_type
1230 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1231 {
1232 clearop(oap);
1233#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001234 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#endif
1236 }
1237
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001238 // Get the length of mapped chars again after typing a count, second
1239 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001240 if (old_mapped_len > 0)
1241 old_mapped_len = typebuf_maplen();
1242
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001243 // If an operation is pending, handle it. But not for K_IGNORE or
1244 // K_MOUSEMOVE.
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001245 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001246 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Yegappan Lakshmanan46634352022-01-24 16:30:30 +00001248 // Wait for a moment when a message is displayed that will be overwritten
1249 // by the mode message.
1250 if (normal_cmd_need_to_wait_for_msg(&ca, &old_pos))
1251 normal_cmd_wait_for_msg();
1252
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001253 // Finish up after executing a Normal mode command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254normal_end:
1255
1256 msg_nowait = FALSE;
1257
Bram Moolenaarcc613032020-06-07 21:31:18 +02001258#ifdef FEAT_EVAL
1259 if (finish_op)
1260 reset_reg_var();
1261#endif
1262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001263 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264#ifdef CURSOR_SHAPE
1265 c = finish_op;
1266#endif
1267 finish_op = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001268 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001270 // Redraw the cursor with another shape, if we were in Operator-pending
1271 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (c || ca.cmdchar == 'r')
1273 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001274 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275# ifdef FEAT_MOUSESHAPE
1276 update_mouseshape(-1);
1277# endif
1278 }
1279#endif
1280
1281#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001282 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001283 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 clear_showcmd();
1285#endif
1286
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001287 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 vim_free(ca.searchbuf);
1289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (has_mbyte)
1291 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (curwin->w_p_scb && toplevel)
1294 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001295 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 do_check_scrollbind(TRUE);
1297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
Bram Moolenaar860cae12010-06-05 23:22:07 +02001299 if (curwin->w_p_crb && toplevel)
1300 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001301 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001302 do_check_cursorbind();
1303 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001304
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001305#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001307 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001308 restart_edit = 0;
1309#endif
1310
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001311 // May restart edit(), if we got here with CTRL-O in Insert mode (but not
1312 // if still inside a mapping that started in Visual mode).
1313 // May switch from Visual to Select mode after CTRL-O command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1316 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && !(ca.retval & CA_COMMAND_BUSY)
1318 && stuff_empty()
1319 && oap->regname == 0)
1320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if (restart_VIsual_select == 1)
1322 {
1323 VIsual_select = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001324 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 showmode();
1326 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001327 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001329 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 (void)edit(restart_edit, FALSE, 1L);
1331 }
1332
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (restart_VIsual_select == 2)
1334 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001336 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 opcount = ca.opcount;
1338}
1339
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001340#ifdef FEAT_EVAL
1341/*
1342 * Set v:count and v:count1 according to "cap".
1343 * Set v:prevcount only when "set_prevcount" is TRUE.
1344 */
1345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001346set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001347{
1348 long count = cap->count0;
1349
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001350 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001351 if (cap->opcount != 0)
1352 count = cap->opcount * (count == 0 ? 1 : count);
1353 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001354 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001355}
1356#endif
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001359 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 * if not.
1361 */
1362 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001363check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 static int did_check = FALSE;
1366
1367 if (full_screen)
1368 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001369 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001370 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 did_check = TRUE;
1372 }
1373}
1374
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001375#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1376/*
1377 * Call yank_do_autocmd() for "regname".
1378 */
1379 static void
1380call_yank_do_autocmd(int regname)
1381{
1382 oparg_T oa;
1383 yankreg_T *reg;
1384
1385 clear_oparg(&oa);
1386 oa.regname = regname;
1387 oa.op_type = OP_YANK;
1388 oa.is_VIsual = TRUE;
1389 reg = get_register(regname, TRUE);
1390 yank_do_autocmd(&oa, reg);
1391 free_register(reg);
1392}
1393#endif
1394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001396 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001397 * This function or the next should ALWAYS be called to end Visual mode, except
1398 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 */
1400 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001401end_visual_mode()
1402{
1403 end_visual_mode_keep_button();
1404 reset_held_button();
1405}
1406
1407 void
1408end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410#ifdef FEAT_CLIPBOARD
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001411 // If we are using the clipboard, then remember what was selected in case
1412 // we need to paste it somewhere while we still own the selection.
1413 // Only do this when the clipboard is already owned. Don't want to grab
1414 // the selection when hitting ESC.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (clip_star.available && clip_star.owned)
1416 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001417
1418# if defined(FEAT_EVAL)
1419 // Emit a TextYankPost for the automatic copy of the selection into the
1420 // star and/or plus register.
1421 if (has_textyankpost())
1422 {
1423 if (clip_isautosel_star())
1424 call_yank_do_autocmd('*');
1425 if (clip_isautosel_plus())
1426 call_yank_do_autocmd('+');
1427 }
1428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429#endif
1430
1431 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 setmouse();
1433 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001435 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001436 curbuf->b_visual.vi_mode = VIsual_mode;
1437 curbuf->b_visual.vi_start = VIsual;
1438 curbuf->b_visual.vi_end = curwin->w_cursor;
1439 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440#ifdef FEAT_EVAL
1441 curbuf->b_visual_mode_eval = VIsual_mode;
1442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (!virtual_active())
1444 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001445 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001447 adjust_cursor_eol();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001448 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449}
1450
1451/*
1452 * Reset VIsual_active and VIsual_reselect.
1453 */
1454 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001455reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 if (VIsual_active)
1458 {
1459 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001460 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
1462 VIsual_reselect = FALSE;
1463}
1464
1465/*
1466 * Reset VIsual_active and VIsual_reselect if it's set.
1467 */
1468 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001469reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471 if (VIsual_active)
1472 {
1473 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001474 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 VIsual_reselect = FALSE;
1476 }
1477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001479 void
1480restore_visual_mode(void)
1481{
1482 if (VIsual_mode_orig != NUL)
1483 {
1484 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1485 VIsual_mode_orig = NUL;
1486 }
1487}
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489/*
1490 * Check for a balloon-eval special item to include when searching for an
1491 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1492 * Returns TRUE if the character at "*ptr" should be included.
1493 * "dir" is FORWARD or BACKWARD, the direction of searching.
1494 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1495 * "bnp" points to a counter for square brackets.
1496 */
1497 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001498find_is_eval_item(
1499 char_u *ptr,
1500 int *colp,
1501 int *bnp,
1502 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001504 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1506 ++*bnp;
1507 if (*bnp > 0)
1508 {
1509 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1510 --*bnp;
1511 return TRUE;
1512 }
1513
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001514 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 if (*ptr == '.')
1516 return TRUE;
1517
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001518 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1520 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1521 {
1522 *colp += dir;
1523 return TRUE;
1524 }
1525 return FALSE;
1526}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
1528/*
1529 * Find the identifier under or to the right of the cursor.
1530 * "find_type" can have one of three values:
1531 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001532 * FIND_STRING: find any non-white text
1533 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001534 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 *
1536 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001537 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001539 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 * This doesn't match the real Vi but I like it a little better and it
1541 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001542 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 * When FIND_IDENT isn't defined, we backup until a blank.
1544 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001545 * Returns the length of the text, or zero if no text is found.
1546 * If text is found, a pointer to the text is put in "*text". This
1547 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
1549 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001550find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551{
1552 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001553 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554}
1555
1556/*
1557 * Like find_ident_under_cursor(), but for any window and any position.
1558 * However: Uses 'iskeyword' from the current window!.
1559 */
1560 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001561find_ident_at_pos(
1562 win_T *wp,
1563 linenr_T lnum,
1564 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001565 char_u **text,
1566 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001567 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001570 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 int this_class = 0;
1573 int prev_class;
1574 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001575 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00001577 // if i == 0: try to find an identifier
1578 // if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1580 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1581 {
1582 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001583 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 */
1585 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (has_mbyte)
1587 {
1588 while (ptr[col] != NUL)
1589 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001590 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1592 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 this_class = mb_get_class(ptr + col);
1594 if (this_class != 0 && (i == 1 || this_class != 1))
1595 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001596 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 }
1599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001601 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 )
1604 ++col;
1605
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001606 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001610 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (has_mbyte)
1613 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001614 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1616 this_class = mb_get_class((char_u *)"a");
1617 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001619 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1622 prev_class = mb_get_class(ptr + prevcol);
1623 if (this_class != prev_class
1624 && (i == 0
1625 || prev_class == 0
1626 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 && (!(find_type & FIND_EVAL)
1628 || prevcol == 0
1629 || !find_is_eval_item(ptr + prevcol, &prevcol,
1630 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 )
1632 break;
1633 col = prevcol;
1634 }
1635
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001636 // If we don't want just any old text, or we've found an
1637 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (this_class > 2)
1639 this_class = 2;
1640 if (!(find_type & FIND_STRING) || this_class == 2)
1641 break;
1642 }
1643 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 while (col > 0
1646 && ((i == 0
1647 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001648 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 && (!(find_type & FIND_IDENT)
1650 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 || ((find_type & FIND_EVAL)
1652 && col > 1
1653 && find_is_eval_item(ptr + col - 1, &col,
1654 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 ))
1656 --col;
1657
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001658 // If we don't want just any old text, or we've found an
1659 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1661 break;
1662 }
1663 }
1664
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001665 if (ptr[col] == NUL || (i == 0
1666 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001668 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001669 if ((find_type & FIND_NOERROR) == 0)
1670 {
1671 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001672 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001673 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001674 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 return 0;
1677 }
1678 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001679 *text = ptr;
1680 if (textcol != NULL)
1681 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001684 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 bn = 0;
1687 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 if (has_mbyte)
1690 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001691 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 this_class = mb_get_class(ptr);
1693 while (ptr[col] != NUL
1694 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1695 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 || ((find_type & FIND_EVAL)
1697 && col <= (int)startcol
1698 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001700 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001704 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 || ((find_type & FIND_EVAL)
1706 && col <= (int)startcol
1707 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 return col;
1712}
1713
1714/*
1715 * Prepare for redo of a normal command.
1716 */
1717 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001718prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 prep_redo(cap->oap->regname, cap->count0,
1721 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1722}
1723
1724/*
1725 * Prepare for redo of any command.
1726 * Note that only the last argument can be a multi-byte char.
1727 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001728 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001729prep_redo(
1730 int regname,
1731 long num,
1732 int cmd1,
1733 int cmd2,
1734 int cmd3,
1735 int cmd4,
1736 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001738 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1739}
1740
1741/*
1742 * Prepare for redo of any command with extra count after "cmd2".
1743 */
1744 void
1745prep_redo_num2(
1746 int regname,
1747 long num1,
1748 int cmd1,
1749 int cmd2,
1750 long num2,
1751 int cmd3,
1752 int cmd4,
1753 int cmd5)
1754{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001756 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 {
1758 AppendCharToRedobuff('"');
1759 AppendCharToRedobuff(regname);
1760 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001761 if (num1 != 0)
1762 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (cmd1 != NUL)
1764 AppendCharToRedobuff(cmd1);
1765 if (cmd2 != NUL)
1766 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001767 if (num2 != 0)
1768 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (cmd3 != NUL)
1770 AppendCharToRedobuff(cmd3);
1771 if (cmd4 != NUL)
1772 AppendCharToRedobuff(cmd4);
1773 if (cmd5 != NUL)
1774 AppendCharToRedobuff(cmd5);
1775}
1776
1777/*
1778 * check for operator active and clear it
1779 *
1780 * return TRUE if operator was active
1781 */
1782 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001783checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 if (oap->op_type == OP_NOP)
1786 return FALSE;
1787 clearopbeep(oap);
1788 return TRUE;
1789}
1790
1791/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001792 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001794 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 */
1796 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001797checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001799 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 return FALSE;
1801 clearopbeep(oap);
1802 return TRUE;
1803}
1804
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001806clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807{
1808 oap->op_type = OP_NOP;
1809 oap->regname = 0;
1810 oap->motion_force = NUL;
1811 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001812 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813}
1814
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001815 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001816clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 clearop(oap);
1819 beep_flush();
1820}
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822/*
1823 * Remove the shift modifier from a special key.
1824 */
1825 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001826unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
1828 switch (cap->cmdchar)
1829 {
1830 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1831 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1832 case K_S_UP: cap->cmdchar = K_UP; break;
1833 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1834 case K_S_HOME: cap->cmdchar = K_HOME; break;
1835 case K_S_END: cap->cmdchar = K_END; break;
1836 }
1837 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1838}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001840/*
1841 * If the mode is currently displayed clear the command line or update the
1842 * command displayed.
1843 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001844 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001845may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001846{
1847 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001848 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001849#ifdef FEAT_CMDL_INFO
1850 else
1851 clear_showcmd();
1852#endif
1853}
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1856/*
1857 * Routines for displaying a partly typed command
1858 */
1859
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001860#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001862static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863static int showcmd_is_clear = TRUE;
1864static int showcmd_visual = FALSE;
1865
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001866static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001869clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870{
1871 if (!p_sc)
1872 return;
1873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (VIsual_active && !char_avail())
1875 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001876 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 long lines;
1878 colnr_T leftcol, rightcol;
1879 linenr_T top, bot;
1880
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001881 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001882 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 top = VIsual.lnum;
1885 bot = curwin->w_cursor.lnum;
1886 }
1887 else
1888 {
1889 top = curwin->w_cursor.lnum;
1890 bot = VIsual.lnum;
1891 }
1892# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001893 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001894 (void)hasFolding(top, &top, NULL);
1895 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896# endif
1897 lines = bot - top + 1;
1898
1899 if (VIsual_mode == Ctrl_V)
1900 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001901# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001902 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001903 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001904
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001905 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001906 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001907 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001910# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001911 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001912 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1915 (long)(rightcol - leftcol + 1));
1916 }
1917 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1918 sprintf((char *)showcmd_buf, "%ld", lines);
1919 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001920 {
1921 char_u *s, *e;
1922 int l;
1923 int bytes = 0;
1924 int chars = 0;
1925
1926 if (cursor_bot)
1927 {
1928 s = ml_get_pos(&VIsual);
1929 e = ml_get_cursor();
1930 }
1931 else
1932 {
1933 s = ml_get_cursor();
1934 e = ml_get_pos(&VIsual);
1935 }
1936 while ((*p_sel != 'e') ? s <= e : s < e)
1937 {
1938 l = (*mb_ptr2len)(s);
1939 if (l == 0)
1940 {
1941 ++bytes;
1942 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001943 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001944 }
1945 bytes += l;
1946 ++chars;
1947 s += l;
1948 }
1949 if (bytes == chars)
1950 sprintf((char *)showcmd_buf, "%d", chars);
1951 else
1952 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1953 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001954 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 showcmd_visual = TRUE;
1956 }
1957 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 showcmd_buf[0] = NUL;
1960 showcmd_visual = FALSE;
1961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001962 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (showcmd_is_clear)
1964 return;
1965 }
1966
1967 display_showcmd();
1968}
1969
1970/*
1971 * Add 'c' to string of shown command chars.
1972 * Return TRUE if output has been written (and setcursor() has been called).
1973 */
1974 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001975add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977 char_u *p;
1978 int old_len;
1979 int extra_len;
1980 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 int i;
1982 static int ignore[] =
1983 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001984#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1986 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001987#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001988 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001989 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1991 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001992 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001994 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 0
1996 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
Bram Moolenaar09df3122006-01-23 22:23:09 +00001998 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 return FALSE;
2000
2001 if (showcmd_visual)
2002 {
2003 showcmd_buf[0] = NUL;
2004 showcmd_visual = FALSE;
2005 }
2006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002007 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 if (IS_SPECIAL(c))
2009 for (i = 0; ignore[i] != 0; ++i)
2010 if (ignore[i] == c)
2011 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01002014 if (*p == ' ')
2015 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 old_len = (int)STRLEN(showcmd_buf);
2017 extra_len = (int)STRLEN(p);
2018 overflow = old_len + extra_len - SHOWCMD_COLS;
2019 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00002020 mch_memmove(showcmd_buf, showcmd_buf + overflow,
2021 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 STRCAT(showcmd_buf, p);
2023
2024 if (char_avail())
2025 return FALSE;
2026
2027 display_showcmd();
2028
2029 return TRUE;
2030}
2031
2032 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002033add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034{
2035 if (!add_to_showcmd(c))
2036 setcursor();
2037}
2038
2039/*
2040 * Delete 'len' characters from the end of the shown command.
2041 */
2042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002043del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
2045 int old_len;
2046
2047 if (!p_sc)
2048 return;
2049
2050 old_len = (int)STRLEN(showcmd_buf);
2051 if (len > old_len)
2052 len = old_len;
2053 showcmd_buf[old_len - len] = NUL;
2054
2055 if (!char_avail())
2056 display_showcmd();
2057}
2058
2059/*
2060 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2061 * something and there is a partial mapping.
2062 */
2063 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002064push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
2066 if (p_sc)
2067 STRCPY(old_showcmd_buf, showcmd_buf);
2068}
2069
2070 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002071pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072{
2073 if (!p_sc)
2074 return;
2075
2076 STRCPY(showcmd_buf, old_showcmd_buf);
2077
2078 display_showcmd();
2079}
2080
2081 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002082display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083{
2084 int len;
2085
2086 cursor_off();
2087
2088 len = (int)STRLEN(showcmd_buf);
2089 if (len == 0)
2090 showcmd_is_clear = TRUE;
2091 else
2092 {
2093 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2094 showcmd_is_clear = FALSE;
2095 }
2096
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002097 // clear the rest of an old message by outputting up to SHOWCMD_COLS
2098 // spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002101 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102}
2103#endif
2104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105/*
2106 * When "check" is FALSE, prepare for commands that scroll the window.
2107 * When "check" is TRUE, take care of scroll-binding after the window has
2108 * scrolled. Called from normal_cmd() and edit().
2109 */
2110 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002111do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112{
2113 static win_T *old_curwin = NULL;
2114 static linenr_T old_topline = 0;
2115#ifdef FEAT_DIFF
2116 static int old_topfill = 0;
2117#endif
2118 static buf_T *old_buf = NULL;
2119 static colnr_T old_leftcol = 0;
2120
2121 if (check && curwin->w_p_scb)
2122 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002123 // If a ":syncbind" command was just used, don't scroll, only reset
2124 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (did_syncbind)
2126 did_syncbind = FALSE;
2127 else if (curwin == old_curwin)
2128 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002129 // Synchronize other windows, as necessary according to
2130 // 'scrollbind'. Don't do this after an ":edit" command, except
2131 // when 'diff' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if ((curwin->w_buffer == old_buf
2133#ifdef FEAT_DIFF
2134 || curwin->w_p_diff
2135#endif
2136 )
2137 && (curwin->w_topline != old_topline
2138#ifdef FEAT_DIFF
2139 || curwin->w_topfill != old_topfill
2140#endif
2141 || curwin->w_leftcol != old_leftcol))
2142 {
2143 check_scrollbind(curwin->w_topline - old_topline,
2144 (long)(curwin->w_leftcol - old_leftcol));
2145 }
2146 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002147 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002149 // When switching between windows, make sure that the relative
2150 // vertical offset is valid for the new window. The relative
2151 // offset is invalid whenever another 'scrollbind' window has
2152 // scrolled to a point that would force the current window to
2153 // scroll past the beginning or end of its buffer. When the
2154 // resync is performed, some of the other 'scrollbind' windows may
2155 // need to jump so that the current window's relative position is
2156 // visible on-screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2158 }
2159 curwin->w_scbind_pos = curwin->w_topline;
2160 }
2161
2162 old_curwin = curwin;
2163 old_topline = curwin->w_topline;
2164#ifdef FEAT_DIFF
2165 old_topfill = curwin->w_topfill;
2166#endif
2167 old_buf = curwin->w_buffer;
2168 old_leftcol = curwin->w_leftcol;
2169}
2170
2171/*
2172 * Synchronize any windows that have "scrollbind" set, based on the
2173 * number of rows by which the current window has changed
2174 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2175 */
2176 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002177check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178{
2179 int want_ver;
2180 int want_hor;
2181 win_T *old_curwin = curwin;
2182 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 int old_VIsual_select = VIsual_select;
2184 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 colnr_T tgt_leftcol = curwin->w_leftcol;
2186 long topline;
2187 long y;
2188
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002189 // check 'scrollopt' string for vertical and horizontal scroll options
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2191#ifdef FEAT_DIFF
2192 want_ver |= old_curwin->w_p_diff;
2193#endif
2194 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2195
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002196 // loop through the scrollbound windows and scroll accordingly
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002198 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
2200 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002201 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (curwin != old_curwin && curwin->w_p_scb)
2203 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002204 // do the vertical scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (want_ver)
2206 {
2207#ifdef FEAT_DIFF
2208 if (old_curwin->w_p_diff && curwin->w_p_diff)
2209 {
2210 diff_set_topline(old_curwin, curwin);
2211 }
2212 else
2213#endif
2214 {
2215 curwin->w_scbind_pos += topline_diff;
2216 topline = curwin->w_scbind_pos;
2217 if (topline > curbuf->b_ml.ml_line_count)
2218 topline = curbuf->b_ml.ml_line_count;
2219 if (topline < 1)
2220 topline = 1;
2221
2222 y = topline - curwin->w_topline;
2223 if (y > 0)
2224 scrollup(y, FALSE);
2225 else
2226 scrolldown(-y, FALSE);
2227 }
2228
2229 redraw_later(VALID);
2230 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002234 // do the horizontal scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2236 {
2237 curwin->w_leftcol = tgt_leftcol;
2238 leftcol_changed();
2239 }
2240 }
2241 }
2242
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002243 // reset current-window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 VIsual_select = old_VIsual_select;
2245 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 curwin = old_curwin;
2247 curbuf = old_curbuf;
2248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250/*
2251 * Command character that's ignored.
2252 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002253 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002256nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002258 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259}
2260
2261/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002262 * Command character that doesn't do anything, but unlike nv_ignore() does
2263 * start edit(). Used for "startinsert" executed while starting up.
2264 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002266nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002267{
2268}
2269
2270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * Command character doesn't exist.
2272 */
2273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002274nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
2276 clearopbeep(cap->oap);
2277}
2278
2279/*
2280 * <Help> and <F1> commands.
2281 */
2282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002283nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 if (!checkclearopq(cap->oap))
2286 ex_help(NULL);
2287}
2288
2289/*
2290 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2291 */
2292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002293nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002295#ifdef FEAT_JOB_CHANNEL
2296 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2297 clearopbeep(cap->oap);
2298 else
2299#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002300 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002301 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002302 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002303 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2304 op_addsub(cap->oap, cap->count1, cap->arg);
2305 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002306 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002307 else if (VIsual_active)
2308 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002309 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002310 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311}
2312
2313/*
2314 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2315 */
2316 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002317nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002320 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002321 if (mod_mask & MOD_MASK_CTRL)
2322 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002323 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002324 if (cap->arg == BACKWARD)
2325 goto_tabpage(-(int)cap->count1);
2326 else
2327 goto_tabpage((int)cap->count0);
2328 }
2329 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002330 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332}
2333
2334/*
2335 * Implementation of "gd" and "gD" command.
2336 */
2337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002338nv_gd(
2339 oparg_T *oap,
2340 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002341 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342{
2343 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 char_u *ptr;
2345
Bram Moolenaard9d30582005-05-18 22:10:28 +00002346 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002347 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002348 == FAIL)
2349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002351 }
2352 else
2353 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002354#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002355 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2356 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002357#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002358 // clear any search statistics
2359 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2360 clear_cmdline = TRUE;
2361 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002362}
2363
2364/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002365 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2366 * otherwise.
2367 */
2368 static int
2369is_ident(char_u *line, int offset)
2370{
2371 int i;
2372 int incomment = FALSE;
2373 int instring = 0;
2374 int prev = 0;
2375
2376 for (i = 0; i < offset && line[i] != NUL; i++)
2377 {
2378 if (instring != 0)
2379 {
2380 if (prev != '\\' && line[i] == instring)
2381 instring = 0;
2382 }
2383 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2384 {
2385 instring = line[i];
2386 }
2387 else
2388 {
2389 if (incomment)
2390 {
2391 if (prev == '*' && line[i] == '/')
2392 incomment = FALSE;
2393 }
2394 else if (prev == '/' && line[i] == '*')
2395 {
2396 incomment = TRUE;
2397 }
2398 else if (prev == '/' && line[i] == '/')
2399 {
2400 return FALSE;
2401 }
2402 }
2403
2404 prev = line[i];
2405 }
2406
2407 return incomment == FALSE && instring == 0;
2408}
2409
2410/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002411 * Search for variable declaration of "ptr[len]".
2412 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2413 * current file ("gD").
2414 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002415 * Return FAIL when not found.
2416 */
2417 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002418find_decl(
2419 char_u *ptr,
2420 int len,
2421 int locally,
2422 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002423 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002424{
2425 char_u *pat;
2426 pos_T old_pos;
2427 pos_T par_pos;
2428 pos_T found_pos;
2429 int t;
2430 int save_p_ws;
2431 int save_p_scs;
2432 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002433 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002434 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002435 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002436
2437 if ((pat = alloc(len + 7)) == NULL)
2438 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002439
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002440 // Put "\V" before the pattern to avoid that the special meaning of "."
2441 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002442 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2443 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 old_pos = curwin->w_cursor;
2445 save_p_ws = p_ws;
2446 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002447 p_ws = FALSE; // don't wrap around end of file now
2448 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002450 // With "gD" go to line 1.
2451 // With "gd" Search back for the start of the current function, then go
2452 // back until a blank line. If this fails go to line 1.
Bram Moolenaar89d40322006-08-29 15:30:07 +00002453 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002455 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002457 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459 else
2460 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002461 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2463 --curwin->w_cursor.lnum;
2464 }
2465 curwin->w_cursor.col = 0;
2466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002467 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002468 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002469 for (;;)
2470 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002471 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002472 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002473 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002474 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002475
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002476 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002477 {
2478 pos_T *pos;
2479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002480 // Check that the block the match is in doesn't end before the
2481 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002482 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2483 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2484 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002485 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002486 // There can't be a useful match before the end of this block.
2487 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002488 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002489 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002490 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002491 }
2492
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002493 if (t == FAIL)
2494 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002495 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002496 if (found_pos.lnum != 0)
2497 {
2498 curwin->w_cursor = found_pos;
2499 t = OK;
2500 }
2501 break;
2502 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002503 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002504 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002505 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002506 ++curwin->w_cursor.lnum;
2507 curwin->w_cursor.col = 0;
2508 continue;
2509 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002510 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2511
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002512 // If the current position is not a valid identifier and a previous
2513 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002514 if (!valid && found_pos.lnum != 0)
2515 {
2516 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002517 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002518 }
2519
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002520 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002521 if (valid && !locally)
2522 break;
2523 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002525 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002526 if (found_pos.lnum != 0)
2527 curwin->w_cursor = found_pos;
2528 break;
2529 }
2530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002531 // For finding a local variable and the match is before the "{" or
2532 // inside a comment, continue searching. For K&R style function
2533 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002534 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002535 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002536 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002537 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002538 // Remove SEARCH_START from flags to avoid getting stuck at one
2539 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002540 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002542
2543 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002545 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 curwin->w_cursor = old_pos;
2547 }
2548 else
2549 {
2550 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002551 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 reset_search_dir();
2553 }
2554
2555 vim_free(pat);
2556 p_ws = save_p_ws;
2557 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002558
2559 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560}
2561
2562/*
2563 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2564 * lines rather than lines in the file.
2565 * 'dist' must be positive.
2566 *
2567 * Return OK if able to move cursor, FAIL otherwise.
2568 */
2569 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002570nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 int linelen = linetabsize(ml_get_curline());
2573 int retval = OK;
2574 int atend = FALSE;
2575 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002576 int col_off1; // margin offset for first screen line
2577 int col_off2; // margin offset for wrapped screen line
2578 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002579 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002582 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584 col_off1 = curwin_col_off();
2585 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002586 width1 = curwin->w_width - col_off1;
2587 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002588 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002589 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002592 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002593 // Instead of sticking at the last character of the buffer line we
2594 // try to stick in the last column of the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (curwin->w_curswant == MAXCOL)
2596 {
2597 atend = TRUE;
2598 validate_virtcol();
2599 if (width1 <= 0)
2600 curwin->w_curswant = 0;
2601 else
2602 {
2603 curwin->w_curswant = width1 - 1;
2604 if (curwin->w_virtcol > curwin->w_curswant)
2605 curwin->w_curswant += ((curwin->w_virtcol
2606 - curwin->w_curswant - 1) / width2 + 1) * width2;
2607 }
2608 }
2609 else
2610 {
2611 if (linelen > width1)
2612 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2613 else
2614 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002615 if (curwin->w_curswant >= (colnr_T)n)
2616 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
2618
2619 while (dist--)
2620 {
2621 if (dir == BACKWARD)
2622 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002623 if ((long)curwin->w_curswant >= width1
2624#ifdef FEAT_FOLDING
2625 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2626#endif
2627 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002628 // Move back within the line. This can give a negative value
2629 // for w_curswant if width1 < width2 (with cpoptions+=n),
2630 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 curwin->w_curswant -= width2;
2632 else
2633 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002634 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002636 // Move to the start of a closed fold. Don't do that when
2637 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (!(fdo_flags & FDO_ALL))
2639 (void)hasFolding(curwin->w_cursor.lnum,
2640 &curwin->w_cursor.lnum, NULL);
2641#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002642 if (curwin->w_cursor.lnum == 1)
2643 {
2644 retval = FAIL;
2645 break;
2646 }
2647 --curwin->w_cursor.lnum;
2648
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 linelen = linetabsize(ml_get_curline());
2650 if (linelen > width1)
2651 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2652 + 1) * width2;
2653 }
2654 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002655 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 {
2657 if (linelen > width1)
2658 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2659 else
2660 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002661 if (curwin->w_curswant + width2 < (colnr_T)n
2662#ifdef FEAT_FOLDING
2663 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2664#endif
2665 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 curwin->w_curswant += width2;
2668 else
2669 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002670 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002672 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2674 &curwin->w_cursor.lnum);
2675#endif
2676 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2677 {
2678 retval = FAIL;
2679 break;
2680 }
2681 curwin->w_cursor.lnum++;
2682 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002683 // Check if the cursor has moved below the number display
2684 // when width1 < width2 (with cpoptions+=n). Subtract width2
2685 // to get a negative value for w_curswant, which will get
2686 // clipped to column 0.
2687 if (curwin->w_curswant >= width1)
2688 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002689 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
2691 }
2692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002695 if (virtual_active() && atend)
2696 coladvance(MAXCOL);
2697 else
2698 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2701 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002702 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002703 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002704
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002705 // Check for landing on a character that got split at the end of the
2706 // last line. We want to advance a screenline, not end up in the same
2707 // screenline or move two screenlines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002709 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002710#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002711 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2712 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002713#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002714
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002715 c = (*mb_ptr2char)(ml_get_cursor());
2716 if (dir == FORWARD && virtcol < curwin->w_curswant
2717 && (curwin->w_curswant <= (colnr_T)width1)
2718 && !vim_isprintc(c) && c > 255)
2719 oneright();
2720
Bram Moolenaar773b1582014-08-29 14:20:51 +02002721 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 && (curwin->w_curswant < (colnr_T)width1
2723 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2724 : ((curwin->w_curswant - width1) % width2
2725 > (colnr_T)width2 / 2)))
2726 --curwin->w_cursor.col;
2727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002730 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 return retval;
2733}
2734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735/*
2736 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2737 * cap->arg must be TRUE for CTRL-E.
2738 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002740nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 if (!checkclearop(cap->oap))
2743 scroll_redraw(cap->arg, cap->count1);
2744}
2745
2746/*
2747 * Scroll "count" lines up or down, and redraw.
2748 */
2749 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002750scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 linenr_T prev_topline = curwin->w_topline;
2753#ifdef FEAT_DIFF
2754 int prev_topfill = curwin->w_topfill;
2755#endif
2756 linenr_T prev_lnum = curwin->w_cursor.lnum;
2757
2758 if (up)
2759 scrollup(count, TRUE);
2760 else
2761 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002762 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002764 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2765 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 cursor_correct();
2767 check_cursor_moved(curwin);
2768 curwin->w_valid |= VALID_TOPLINE;
2769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002770 // If moved back to where we were, at least move the cursor, otherwise
2771 // we get stuck at one position. Don't move the cursor up if the
2772 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 while (curwin->w_topline == prev_topline
2774#ifdef FEAT_DIFF
2775 && curwin->w_topfill == prev_topfill
2776#endif
2777 )
2778 {
2779 if (up)
2780 {
2781 if (curwin->w_cursor.lnum > prev_lnum
2782 || cursor_down(1L, FALSE) == FAIL)
2783 break;
2784 }
2785 else
2786 {
2787 if (curwin->w_cursor.lnum < prev_lnum
2788 || prev_topline == 1L
2789 || cursor_up(1L, FALSE) == FAIL)
2790 break;
2791 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002792 // Mark w_topline as valid, otherwise the screen jumps back at the
2793 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 check_cursor_moved(curwin);
2795 curwin->w_valid |= VALID_TOPLINE;
2796 }
2797 }
2798 if (curwin->w_cursor.lnum != prev_lnum)
2799 coladvance(curwin->w_curswant);
2800 redraw_later(VALID);
2801}
2802
2803/*
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00002804 * Get the count specified after a 'z' command. Only the 'z<CR>', 'zl', 'zh',
2805 * 'z<Left>', and 'z<Right>' commands accept a count after 'z'.
2806 * Returns TRUE to process the 'z' command and FALSE to skip it.
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002807 */
2808 static int
2809nv_z_get_count(cmdarg_T *cap, int *nchar_arg)
2810{
2811 int nchar = *nchar_arg;
2812 long n;
2813
2814 // "z123{nchar}": edit the count before obtaining {nchar}
2815 if (checkclearop(cap->oap))
2816 return FALSE;
2817 n = nchar - '0';
2818
2819 for (;;)
2820 {
2821#ifdef USE_ON_FLY_SCROLL
2822 dont_scroll = TRUE; // disallow scrolling here
2823#endif
2824 ++no_mapping;
2825 ++allow_keys; // no mapping for nchar, but allow key codes
2826 nchar = plain_vgetc();
2827 LANGMAP_ADJUST(nchar, TRUE);
2828 --no_mapping;
2829 --allow_keys;
2830#ifdef FEAT_CMDL_INFO
2831 (void)add_to_showcmd(nchar);
2832#endif
2833 if (nchar == K_DEL || nchar == K_KDEL)
2834 n /= 10;
2835 else if (VIM_ISDIGIT(nchar))
2836 n = n * 10 + (nchar - '0');
2837 else if (nchar == CAR)
2838 {
2839#ifdef FEAT_GUI
2840 need_mouse_correct = TRUE;
2841#endif
2842 win_setheight((int)n);
2843 break;
2844 }
2845 else if (nchar == 'l'
2846 || nchar == 'h'
2847 || nchar == K_LEFT
2848 || nchar == K_RIGHT)
2849 {
2850 cap->count1 = n ? n * cap->count1 : cap->count1;
2851 *nchar_arg = nchar;
2852 return TRUE;
2853 }
2854 else
2855 {
2856 clearopbeep(cap->oap);
2857 break;
2858 }
2859 }
2860 cap->oap->op_type = OP_NOP;
2861 return FALSE;
2862}
2863
2864#ifdef FEAT_SPELL
2865/*
2866 * "zug" and "zuw": undo "zg" and "zw"
2867 * "zg": add good word to word list
2868 * "zw": add wrong word to word list
2869 * "zG": add good word to temp word list
2870 * "zW": add wrong word to temp word list
2871 */
2872 static int
2873nv_zg_zw(cmdarg_T *cap, int nchar)
2874{
2875 char_u *ptr = NULL;
2876 int len;
2877 int undo = FALSE;
2878
2879 if (nchar == 'u')
2880 {
2881 ++no_mapping;
2882 ++allow_keys; // no mapping for nchar, but allow key codes
2883 nchar = plain_vgetc();
2884 LANGMAP_ADJUST(nchar, TRUE);
2885 --no_mapping;
2886 --allow_keys;
2887#ifdef FEAT_CMDL_INFO
2888 (void)add_to_showcmd(nchar);
2889#endif
2890 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
2891 {
2892 clearopbeep(cap->oap);
2893 return OK;
2894 }
2895 undo = TRUE;
2896 }
2897
2898 if (checkclearop(cap->oap))
2899 return OK;
2900 if (VIsual_active && get_visual_text(cap, &ptr, &len) == FAIL)
2901 return FAIL;
2902 if (ptr == NULL)
2903 {
2904 pos_T pos = curwin->w_cursor;
2905
2906 // Find bad word under the cursor. When 'spell' is
2907 // off this fails and find_ident_under_cursor() is
2908 // used below.
2909 emsg_off++;
2910 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
2911 emsg_off--;
2912 if (len != 0 && curwin->w_cursor.col <= pos.col)
2913 ptr = ml_get_pos(&curwin->w_cursor);
2914 curwin->w_cursor = pos;
2915 }
2916
2917 if (ptr == NULL
2918 && (len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
2919 return FAIL;
2920 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
2921 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
2922 (nchar == 'G' || nchar == 'W') ? 0 : (int)cap->count1, undo);
2923
2924 return OK;
2925}
2926#endif
2927
2928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 * Commands that start with "z".
2930 */
2931 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002932nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
2934 long n;
2935 colnr_T col;
2936 int nchar = cap->nchar;
2937#ifdef FEAT_FOLDING
2938 long old_fdl = curwin->w_p_fdl;
2939 int old_fen = curwin->w_p_fen;
2940#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002941 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002943 if (VIM_ISDIGIT(nchar) && !nv_z_get_count(cap, &nchar))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (
2947#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002948 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2949 // and "zC" only in Visual mode. "zj" and "zk" are motion
2950 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 cap->nchar != 'f' && cap->nchar != 'F'
2952 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2953 && cap->nchar != 'j' && cap->nchar != 'k'
2954 &&
2955#endif
2956 checkclearop(cap->oap))
2957 return;
2958
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00002959 // For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2960 // If line number given, set cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2962 && cap->count0
2963 && cap->count0 != curwin->w_cursor.lnum)
2964 {
2965 setpcmark();
2966 if (cap->count0 > curbuf->b_ml.ml_line_count)
2967 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2968 else
2969 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002970 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972
2973 switch (nchar)
2974 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002975 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 case '+':
2977 if (cap->count0 == 0)
2978 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // No count given: put cursor at the line below screen
2980 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2982 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2983 else
2984 curwin->w_cursor.lnum = curwin->w_botline;
2985 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 case NL:
2988 case CAR:
2989 case K_KENTER:
2990 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
2993 case 't': scroll_cursor_top(0, TRUE);
2994 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002995 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 break;
2997
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 case 'z': scroll_cursor_halfway(TRUE);
3003 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003004 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 break;
3006
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003007 // "z^", "z-" and "zb": put cursor at bottom of screen
3008 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
3009 // when <count> is at bottom of window, and puts that one at
3010 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (cap->count0 != 0)
3012 {
3013 scroll_cursor_bot(0, TRUE);
3014 curwin->w_cursor.lnum = curwin->w_topline;
3015 }
3016 else if (curwin->w_topline == 1)
3017 curwin->w_cursor.lnum = 1;
3018 else
3019 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003020 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 case '-':
3022 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
3025 case 'b': scroll_cursor_bot(0, TRUE);
3026 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003027 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 break;
3029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003030 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02003032 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003033 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003035 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 case 'h':
3037 case K_LEFT:
3038 if (!curwin->w_p_wrap)
3039 {
3040 if ((colnr_T)cap->count1 > curwin->w_leftcol)
3041 curwin->w_leftcol = 0;
3042 else
3043 curwin->w_leftcol -= (colnr_T)cap->count1;
3044 leftcol_changed();
3045 }
3046 break;
3047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003048 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02003049 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003050 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003052 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 case 'l':
3054 case K_RIGHT:
3055 if (!curwin->w_p_wrap)
3056 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 curwin->w_leftcol += (colnr_T)cap->count1;
3059 leftcol_changed();
3060 }
3061 break;
3062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003063 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 case 's': if (!curwin->w_p_wrap)
3065 {
3066#ifdef FEAT_FOLDING
3067 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 else
3070#endif
3071 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01003072 if ((long)col > siso)
3073 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 else
3075 col = 0;
3076 if (curwin->w_leftcol != col)
3077 {
3078 curwin->w_leftcol = col;
3079 redraw_later(NOT_VALID);
3080 }
3081 }
3082 break;
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 case 'e': if (!curwin->w_p_wrap)
3086 {
3087#ifdef FEAT_FOLDING
3088 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003089 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 else
3091#endif
3092 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02003093 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01003094 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 col = 0;
3096 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01003097 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (curwin->w_leftcol != col)
3099 {
3100 curwin->w_leftcol = col;
3101 redraw_later(NOT_VALID);
3102 }
3103 }
3104 break;
3105
Christian Brabandt2fa93842021-05-30 22:17:25 +02003106 // "zp", "zP" in block mode put without addind trailing spaces
3107 case 'P':
3108 case 'p': nv_put(cap);
3109 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02003110 // "zy" Yank without trailing spaces
3111 case 'y': nv_operator(cap);
3112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003114 // "zF": create fold command
3115 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 case 'F':
3117 case 'f': if (foldManualAllowed(TRUE))
3118 {
3119 cap->nchar = 'f';
3120 nv_operator(cap);
3121 curwin->w_p_fen = TRUE;
3122
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3125 {
3126 nv_operator(cap);
3127 finish_op = TRUE;
3128 }
3129 }
3130 else
3131 clearopbeep(cap->oap);
3132 break;
3133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // "zd": delete fold at cursor
3135 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 case 'd':
3137 case 'D': if (foldManualAllowed(FALSE))
3138 {
3139 if (VIsual_active)
3140 nv_operator(cap);
3141 else
3142 deleteFold(curwin->w_cursor.lnum,
3143 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3144 }
3145 break;
3146
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003147 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 case 'E': if (foldmethodIsManual(curwin))
3149 {
3150 clearFolding(curwin);
3151 changed_window_setting();
3152 }
3153 else if (foldmethodIsMarker(curwin))
3154 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3155 TRUE, FALSE);
3156 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003157 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 break;
3159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003160 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 case 'n': curwin->w_p_fen = FALSE;
3162 break;
3163
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003164 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 case 'N': curwin->w_p_fen = TRUE;
3166 break;
3167
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003168 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3170 break;
3171
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003172 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3174 openFold(curwin->w_cursor.lnum, cap->count1);
3175 else
3176 {
3177 closeFold(curwin->w_cursor.lnum, cap->count1);
3178 curwin->w_p_fen = TRUE;
3179 }
3180 break;
3181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3184 openFoldRecurse(curwin->w_cursor.lnum);
3185 else
3186 {
3187 closeFoldRecurse(curwin->w_cursor.lnum);
3188 curwin->w_p_fen = TRUE;
3189 }
3190 break;
3191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 case 'o': if (VIsual_active)
3194 nv_operator(cap);
3195 else
3196 openFold(curwin->w_cursor.lnum, cap->count1);
3197 break;
3198
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003199 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 case 'O': if (VIsual_active)
3201 nv_operator(cap);
3202 else
3203 openFoldRecurse(curwin->w_cursor.lnum);
3204 break;
3205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003206 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 case 'c': if (VIsual_active)
3208 nv_operator(cap);
3209 else
3210 closeFold(curwin->w_cursor.lnum, cap->count1);
3211 curwin->w_p_fen = TRUE;
3212 break;
3213
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003214 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 case 'C': if (VIsual_active)
3216 nv_operator(cap);
3217 else
3218 closeFoldRecurse(curwin->w_cursor.lnum);
3219 curwin->w_p_fen = TRUE;
3220 break;
3221
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003222 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 case 'v': foldOpenCursor();
3224 break;
3225
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003226 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003228 curwin->w_foldinvalid = TRUE; // recompute folds
3229 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 foldOpenCursor();
3231 break;
3232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003235 curwin->w_foldinvalid = TRUE; // recompute folds
3236 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 break;
3238
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003239 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003241 {
3242 curwin->w_p_fdl -= cap->count1;
3243 if (curwin->w_p_fdl < 0)
3244 curwin->w_p_fdl = 0;
3245 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003246 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 curwin->w_p_fen = TRUE;
3248 break;
3249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003250 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003252 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 curwin->w_p_fen = TRUE;
3254 break;
3255
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003256 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003257 case 'r': curwin->w_p_fdl += cap->count1;
3258 {
3259 int d = getDeepestNesting();
3260
3261 if (curwin->w_p_fdl >= d)
3262 curwin->w_p_fdl = d;
3263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 break;
3265
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003266 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003268 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 break;
3270
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003271 case 'j': // "zj" move to next fold downwards
3272 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3274 cap->count1) == FAIL)
3275 clearopbeep(cap->oap);
3276 break;
3277
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003278#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003280#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003281 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 case 'g': // "zg": add good word to word list
3283 case 'w': // "zw": add wrong word to word list
3284 case 'G': // "zG": add good word to temp word list
3285 case 'W': // "zW": add wrong word to temp word list
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003286 if (nv_zg_zw(cap, nchar) == FAIL)
3287 return;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003288 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003291 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003292 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003293 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003294#endif
3295
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 default: clearopbeep(cap->oap);
3297 }
3298
3299#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003300 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 if (old_fen != curwin->w_p_fen)
3302 {
3303# ifdef FEAT_DIFF
3304 win_T *wp;
3305
3306 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3307 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003308 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 FOR_ALL_WINDOWS(wp)
3310 {
3311 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3312 {
3313 wp->w_p_fen = curwin->w_p_fen;
3314 changed_window_setting_win(wp);
3315 }
3316 }
3317 }
3318# endif
3319 changed_window_setting();
3320 }
3321
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003322 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (old_fdl != curwin->w_p_fdl)
3324 newFoldLevel();
3325#endif
3326}
3327
3328#ifdef FEAT_GUI
3329/*
3330 * Vertical scrollbar movement.
3331 */
3332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003333nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 if (cap->oap->op_type != OP_NOP)
3336 clearopbeep(cap->oap);
3337
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003338 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 gui_do_scroll();
3340}
3341
3342/*
3343 * Horizontal scrollbar movement.
3344 */
3345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003346nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 if (cap->oap->op_type != OP_NOP)
3349 clearopbeep(cap->oap);
3350
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003351 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003352 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353}
3354#endif
3355
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003356#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003357/*
3358 * Click in GUI tab.
3359 */
3360 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003361nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003362{
3363 if (cap->oap->op_type != OP_NOP)
3364 clearopbeep(cap->oap);
3365
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003366 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003367 goto_tabpage(current_tab);
3368}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003369
3370/*
3371 * Selected item in tab line menu.
3372 */
3373 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003374nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003375{
3376 if (cap->oap->op_type != OP_NOP)
3377 clearopbeep(cap->oap);
3378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003379 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003380 handle_tabmenu();
3381}
3382
3383/*
3384 * Handle selecting an item of the GUI tab line menu.
3385 * Used in Normal and Insert mode.
3386 */
3387 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003388handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003389{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003390 switch (current_tabmenu)
3391 {
3392 case TABLINE_MENU_CLOSE:
3393 if (current_tab == 0)
3394 do_cmdline_cmd((char_u *)"tabclose");
3395 else
3396 {
3397 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3398 current_tab);
3399 do_cmdline_cmd(IObuff);
3400 }
3401 break;
3402
3403 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003404 if (current_tab == 0)
3405 do_cmdline_cmd((char_u *)"$tabnew");
3406 else
3407 {
3408 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3409 current_tab - 1);
3410 do_cmdline_cmd(IObuff);
3411 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003412 break;
3413
3414 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003415 if (current_tab == 0)
3416 do_cmdline_cmd((char_u *)"browse $tabnew");
3417 else
3418 {
3419 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3420 current_tab - 1);
3421 do_cmdline_cmd(IObuff);
3422 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003423 break;
3424 }
3425}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003426#endif
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428/*
3429 * "Q" command.
3430 */
3431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003432nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003434 // Ignore 'Q' in Visual mode, just give a beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003436 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003437 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 do_exmode(FALSE);
3439}
3440
3441/*
3442 * Handle a ":" command.
3443 */
3444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003445nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003447 int old_p_im;
3448 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003449 int is_cmdkey = cap->cmdchar == K_COMMAND
3450 || cap->cmdchar == K_SCRIPT_COMMAND;
3451 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
Bram Moolenaar957cf672020-11-12 14:21:06 +01003453 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 nv_operator(cap);
3455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
3457 if (cap->oap->op_type != OP_NOP)
3458 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 cap->oap->motion_type = MCHAR;
3461 cap->oap->inclusive = FALSE;
3462 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003463 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003465 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 stuffcharReadbuff('.');
3467 if (cap->count0 > 1)
3468 {
3469 stuffReadbuff((char_u *)",.+");
3470 stuffnumReadbuff((long)cap->count0 - 1L);
3471 }
3472 }
3473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003474 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (KeyTyped)
3476 compute_cmdrow();
3477
3478 old_p_im = p_im;
3479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003480 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003481 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3482 if (is_cmdkey)
3483 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3484 else
3485 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003487 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (p_im != old_p_im)
3489 {
3490 if (p_im)
3491 restart_edit = 'i';
3492 else
3493 restart_edit = 0;
3494 }
3495
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003496 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003497 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003498 clearop(cap->oap);
3499 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3501 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003502 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3503 || did_emsg
3504 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003505 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 clearopbeep(cap->oap);
3507 }
3508}
3509
3510/*
3511 * Handle CTRL-G command.
3512 */
3513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003514nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003516 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
3518 VIsual_select = !VIsual_select;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003519 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 showmode();
3521 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003522 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003523 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 fileinfo((int)cap->count0, FALSE, TRUE);
3525}
3526
3527/*
3528 * Handle CTRL-H <Backspace> command.
3529 */
3530 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003531nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (VIsual_active && VIsual_select)
3534 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003535 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 v_visop(cap);
3537 }
3538 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 nv_left(cap);
3540}
3541
3542/*
3543 * CTRL-L: clear screen and redraw.
3544 */
3545 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003546nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547{
3548 if (!checkclearop(cap->oap))
3549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003551 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003552 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003553# ifdef FEAT_RELTIME
3554 {
3555 win_T *wp;
3556
3557 FOR_ALL_WINDOWS(wp)
3558 wp->w_s->b_syn_slow = FALSE;
3559 }
3560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561#endif
3562 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003563#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3564# ifdef VIMDLL
3565 if (!gui.in_use)
3566# endif
3567 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
3570}
3571
3572/*
3573 * CTRL-O: In Select mode: switch to Visual mode for one command.
3574 * Otherwise: Go to older pcmark.
3575 */
3576 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003577nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (VIsual_active && VIsual_select)
3580 {
3581 VIsual_select = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003582 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003584 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 cap->count1 = -cap->count1;
3589 nv_pcmark(cap);
3590 }
3591}
3592
3593/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003594 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3595 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 */
3597 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003598nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
3600 if (!checkclearopq(cap->oap))
3601 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3602 GETF_SETMARK|GETF_ALT, FALSE);
3603}
3604
3605/*
3606 * "Z" commands.
3607 */
3608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003609nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 if (!checkclearopq(cap->oap))
3612 {
3613 switch (cap->nchar)
3614 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003615 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 case 'Z': do_cmdline_cmd((char_u *)"x");
3617 break;
3618
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003619 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 case 'Q': do_cmdline_cmd((char_u *)"q!");
3621 break;
3622
3623 default: clearopbeep(cap->oap);
3624 }
3625 }
3626}
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628/*
3629 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3630 */
3631 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003632do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
3634 oparg_T oa;
3635 cmdarg_T ca;
3636
3637 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003638 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 ca.oap = &oa;
3640 ca.cmdchar = c1;
3641 ca.nchar = c2;
3642 nv_ident(&ca);
3643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003646 * 'K' normal-mode command. Get the command to lookup the keyword under the
3647 * cursor.
3648 */
3649 static int
3650nv_K_getcmd(
3651 cmdarg_T *cap,
3652 char_u *kp,
3653 int kp_help,
3654 int kp_ex,
3655 char_u **ptr_arg,
3656 int n,
3657 char_u *buf,
3658 unsigned buflen)
3659{
3660 char_u *ptr = *ptr_arg;
3661 int isman;
3662 int isman_s;
3663
3664 if (kp_help)
3665 {
3666 // in the help buffer
3667 STRCPY(buf, "he! ");
3668 return n;
3669 }
3670
3671 if (kp_ex)
3672 {
3673 // 'keywordprog' is an ex command
3674 if (cap->count0 != 0)
3675 vim_snprintf((char *)buf, buflen, "%s %ld", kp, cap->count0);
3676 else
3677 STRCPY(buf, kp);
3678 STRCAT(buf, " ");
3679 return n;
3680 }
3681
3682 // An external command will probably use an argument starting
3683 // with "-" as an option. To avoid trouble we skip the "-".
3684 while (*ptr == '-' && n > 0)
3685 {
3686 ++ptr;
3687 --n;
3688 }
3689 if (n == 0)
3690 {
3691 // found dashes only
3692 emsg(_(e_no_identifier_under_cursor));
3693 vim_free(buf);
3694 *ptr_arg = ptr;
3695 return 0;
3696 }
3697
3698 // When a count is given, turn it into a range. Is this
3699 // really what we want?
3700 isman = (STRCMP(kp, "man") == 0);
3701 isman_s = (STRCMP(kp, "man -s") == 0);
3702 if (cap->count0 != 0 && !(isman || isman_s))
3703 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3704
3705 STRCAT(buf, "! ");
3706 if (cap->count0 == 0 && isman_s)
3707 STRCAT(buf, "man");
3708 else
3709 STRCAT(buf, kp);
3710 STRCAT(buf, " ");
3711 if (cap->count0 != 0 && (isman || isman_s))
3712 {
3713 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3714 STRCAT(buf, " ");
3715 }
3716
3717 *ptr_arg = ptr;
3718 return n;
3719}
3720
3721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 * Handle the commands that use the word under the cursor.
3723 * [g] CTRL-] :ta to current identifier
3724 * [g] 'K' run program for current identifier
3725 * [g] '*' / to current identifier or string
3726 * [g] '#' ? to current identifier or string
3727 * g ']' :tselect for current identifier
3728 */
3729 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003730nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
3732 char_u *ptr = NULL;
3733 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003734 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003735 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003737 char_u *kp; // value of 'keywordprg'
3738 int kp_help; // 'keywordprg' is ":he"
3739 int kp_ex; // 'keywordprg' starts with ":"
3740 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003742 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003743 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 char_u *aux_ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003746 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
3748 cmdchar = cap->nchar;
3749 g_cmd = TRUE;
3750 }
3751 else
3752 {
3753 cmdchar = cap->cmdchar;
3754 g_cmd = FALSE;
3755 }
3756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003757 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 cmdchar = '#';
3759
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003760 // The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3764 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (checkclearopq(cap->oap))
3766 return;
3767 }
3768
3769 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3770 (cmdchar == '*' || cmdchar == '#')
3771 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3772 {
3773 clearop(cap->oap);
3774 return;
3775 }
3776
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003777 // Allocate buffer to put the command in. Inserting backslashes can
3778 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3779 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3781 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3782 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003783 if (kp_help && *skipwhite(ptr) == NUL)
3784 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003785 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003786 return;
3787 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003788 kp_ex = (*kp == ':');
3789 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3790 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (buf == NULL)
3792 return;
3793 buf[0] = NUL;
3794
3795 switch (cmdchar)
3796 {
3797 case '*':
3798 case '#':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003799 // Put cursor at start of word, makes search skip the word
3800 // under the cursor.
3801 // Call setpcmark() first, so "*``" puts the cursor back where
3802 // it was.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 setpcmark();
3804 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3805
3806 if (!g_cmd && vim_iswordp(ptr))
3807 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003808 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 break;
3810
3811 case 'K':
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003812 n = nv_K_getcmd(cap, kp, kp_help, kp_ex, &ptr, n, buf, buflen);
3813 if (n == 0)
3814 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 break;
3816
3817 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003818 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819#ifdef FEAT_CSCOPE
3820 if (p_cst)
3821 STRCPY(buf, "cstag ");
3822 else
3823#endif
3824 STRCPY(buf, "ts ");
3825 break;
3826
3827 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003828 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (curbuf->b_help)
3830 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003832 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003833 if (g_cmd)
3834 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003835 else if (cap->count0 == 0)
3836 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003837 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003838 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003842 // Now grab the chars in the identifier
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003843 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003845 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003846 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003847 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003848 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003849 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003850 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003851 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003852 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003853 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003855 vim_free(buf);
3856 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003858 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003859 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003860 {
3861 vim_free(buf);
3862 vim_free(p);
3863 return;
3864 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003865 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003866 STRCAT(buf, p);
3867 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003869 else
3870 {
3871 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003872 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003873 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003874 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003875 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003876 {
3877 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003878 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003879 aux_ptr = (char_u *)"";
3880 else
3881 aux_ptr = (char_u *)"\\|\"\n[";
3882 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003883 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003884 aux_ptr = (char_u *)"\\|\"\n*?[";
3885
3886 p = buf + STRLEN(buf);
3887 while (n-- > 0)
3888 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003889 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003890 if (vim_strchr(aux_ptr, *ptr) != NULL)
3891 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003892 // When current byte is a part of multibyte character, copy all
3893 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003894 if (has_mbyte)
3895 {
3896 int i;
3897 int len = (*mb_ptr2len)(ptr) - 1;
3898
3899 for (i = 0; i < len && n >= 1; ++i, --n)
3900 *p++ = *ptr++;
3901 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003902 *p++ = *ptr++;
3903 }
3904 *p = NUL;
3905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00003907 // Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 if (cmdchar == '*' || cmdchar == '#')
3909 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003910 if (!g_cmd && (has_mbyte
3911 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3912 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003914
3915 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003916 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003918
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003919 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003922 {
3923 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003925 g_tag_at_cursor = FALSE;
3926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 vim_free(buf);
3929}
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931/*
3932 * Get visually selected text, within one line only.
3933 * Returns FAIL if more than one line selected.
3934 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003935 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003936get_visual_text(
3937 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003938 char_u **pp, // return: start of selected text
3939 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941 if (VIsual_mode != 'V')
3942 unadjust_for_sel();
3943 if (VIsual.lnum != curwin->w_cursor.lnum)
3944 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003945 if (cap != NULL)
3946 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 return FAIL;
3948 }
3949 if (VIsual_mode == 'V')
3950 {
3951 *pp = ml_get_curline();
3952 *lenp = (int)STRLEN(*pp);
3953 }
3954 else
3955 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003956 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 {
3958 *pp = ml_get_pos(&curwin->w_cursor);
3959 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3960 }
3961 else
3962 {
3963 *pp = ml_get_pos(&VIsual);
3964 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3965 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003966 if (**pp == NUL)
3967 *lenp = 0;
3968 if (has_mbyte && *lenp > 0)
3969 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003970 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972 reset_VIsual_and_resel();
3973 return OK;
3974}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976/*
3977 * CTRL-T: backwards in tag stack
3978 */
3979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003980nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
3982 if (!checkclearopq(cap->oap))
3983 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3984}
3985
3986/*
3987 * Handle scrolling command 'H', 'L' and 'M'.
3988 */
3989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003990nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
3992 int used = 0;
3993 long n;
3994#ifdef FEAT_FOLDING
3995 linenr_T lnum;
3996#endif
3997 int half;
3998
3999 cap->oap->motion_type = MLINE;
4000 setpcmark();
4001
4002 if (cap->cmdchar == 'L')
4003 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004004 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 curwin->w_cursor.lnum = curwin->w_botline - 1;
4006 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
4007 curwin->w_cursor.lnum = 1;
4008 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004009 {
4010#ifdef FEAT_FOLDING
4011 if (hasAnyFolding(curwin))
4012 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004013 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004014 for (n = cap->count1 - 1; n > 0
4015 && curwin->w_cursor.lnum > curwin->w_topline; --n)
4016 {
4017 (void)hasFolding(curwin->w_cursor.lnum,
4018 &curwin->w_cursor.lnum, NULL);
4019 --curwin->w_cursor.lnum;
4020 }
4021 }
4022 else
4023#endif
4024 curwin->w_cursor.lnum -= cap->count1 - 1;
4025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027 else
4028 {
4029 if (cap->cmdchar == 'M')
4030 {
4031#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004032 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 used -= diff_check_fill(curwin, curwin->w_topline)
4034 - curwin->w_topfill;
4035#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004036 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
4038 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
4039 {
4040#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004041 // Count half he number of filler lines to be "below this
4042 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
4044 + n) / 2 >= half)
4045 {
4046 --n;
4047 break;
4048 }
4049#endif
4050 used += plines(curwin->w_topline + n);
4051 if (used >= half)
4052 break;
4053#ifdef FEAT_FOLDING
4054 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
4055 n = lnum - curwin->w_topline;
4056#endif
4057 }
4058 if (n > 0 && used > curwin->w_height)
4059 --n;
4060 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004061 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004062 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004064#ifdef FEAT_FOLDING
4065 if (hasAnyFolding(curwin))
4066 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004067 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004068 lnum = curwin->w_topline;
4069 while (n-- > 0 && lnum < curwin->w_botline - 1)
4070 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02004071 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004072 ++lnum;
4073 }
4074 n = lnum - curwin->w_topline;
4075 }
4076#endif
4077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 curwin->w_cursor.lnum = curwin->w_topline + n;
4079 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4080 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4081 }
4082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004083 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02004084 if (cap->oap->op_type == OP_NOP)
4085 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 beginline(BL_SOL | BL_FIX);
4087}
4088
4089/*
4090 * Cursor right commands.
4091 */
4092 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004093nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004096 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004098 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4099 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004100 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004101 if (mod_mask & MOD_MASK_CTRL)
4102 cap->arg = TRUE;
4103 nv_wordcmd(cap);
4104 return;
4105 }
4106
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 cap->oap->motion_type = MCHAR;
4108 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004109 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004111 // In virtual edit mode, there's no such thing as "past_line", as lines
4112 // are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004114 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
4116 for (n = cap->count1; n > 0; --n)
4117 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004118 if ((!past_line && oneright() == FAIL)
4119 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004120 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004122 // <Space> wraps to next line if 'whichwrap' has 's'.
4123 // 'l' wraps to next line if 'whichwrap' has 'l'.
4124 // CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 if ( ((cap->cmdchar == ' '
4126 && vim_strchr(p_ww, 's') != NULL)
4127 || (cap->cmdchar == 'l'
4128 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004129 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 && vim_strchr(p_ww, '>') != NULL))
4131 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4132 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004133 // When deleting we also count the NL as a character.
4134 // Set cap->oap->inclusive when last char in the line is
4135 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004136 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004138 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 cap->oap->inclusive = TRUE;
4140 else
4141 {
4142 ++curwin->w_cursor.lnum;
4143 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 curwin->w_set_curswant = TRUE;
4146 cap->oap->inclusive = FALSE;
4147 }
4148 continue;
4149 }
4150 if (cap->oap->op_type == OP_NOP)
4151 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004152 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 if (n == cap->count1)
4154 beep_flush();
4155 }
4156 else
4157 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004158 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 cap->oap->inclusive = TRUE;
4160 }
4161 break;
4162 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004163 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
4165 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 if (virtual_active())
4167 oneright();
4168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004171 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 ++curwin->w_cursor.col;
4174 }
4175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177#ifdef FEAT_FOLDING
4178 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4179 && cap->oap->op_type == OP_NOP)
4180 foldOpenCursor();
4181#endif
4182}
4183
4184/*
4185 * Cursor left commands.
4186 *
4187 * Returns TRUE when operator end should not be adjusted.
4188 */
4189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004190nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
4192 long n;
4193
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004194 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4195 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004196 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004197 if (mod_mask & MOD_MASK_CTRL)
4198 cap->arg = 1;
4199 nv_bck_word(cap);
4200 return;
4201 }
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 cap->oap->motion_type = MCHAR;
4204 cap->oap->inclusive = FALSE;
4205 for (n = cap->count1; n > 0; --n)
4206 {
4207 if (oneleft() == FAIL)
4208 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004209 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4210 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4211 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 if ( (((cap->cmdchar == K_BS
4213 || cap->cmdchar == Ctrl_H)
4214 && vim_strchr(p_ww, 'b') != NULL)
4215 || (cap->cmdchar == 'h'
4216 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004217 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 && vim_strchr(p_ww, '<') != NULL))
4219 && curwin->w_cursor.lnum > 1)
4220 {
4221 --(curwin->w_cursor.lnum);
4222 coladvance((colnr_T)MAXCOL);
4223 curwin->w_set_curswant = TRUE;
4224
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004225 // When the NL before the first char has to be deleted we
4226 // put the cursor on the NUL after the previous line.
4227 // This is a very special case, be careful!
4228 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if ( (cap->oap->op_type == OP_DELETE
4230 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004231 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004233 char_u *cp = ml_get_cursor();
4234
4235 if (*cp != NUL)
4236 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004237 if (has_mbyte)
4238 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4239 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004240 ++curwin->w_cursor.col;
4241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 cap->retval |= CA_NO_ADJ_OP_END;
4243 }
4244 continue;
4245 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004246 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4248 beep_flush();
4249 break;
4250 }
4251 }
4252#ifdef FEAT_FOLDING
4253 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4254 && cap->oap->op_type == OP_NOP)
4255 foldOpenCursor();
4256#endif
4257}
4258
4259/*
4260 * Cursor up commands.
4261 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4262 */
4263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004264nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004266 if (mod_mask & MOD_MASK_SHIFT)
4267 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004268 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004269 cap->arg = BACKWARD;
4270 nv_page(cap);
4271 }
4272 else
4273 {
4274 cap->oap->motion_type = MLINE;
4275 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4276 clearopbeep(cap->oap);
4277 else if (cap->arg)
4278 beginline(BL_WHITE | BL_FIX);
4279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280}
4281
4282/*
4283 * Cursor down commands.
4284 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4285 */
4286 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004287nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004289 if (mod_mask & MOD_MASK_SHIFT)
4290 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004291 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004292 cap->arg = FORWARD;
4293 nv_page(cap);
4294 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004295#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004296 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004297 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4298 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 {
4302#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004303 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004304 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 cmdwin_result = CAR;
4306 else
4307#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004308#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004309 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004310 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4311 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4312 {
4313 invoke_prompt_callback();
4314 if (restart_edit == 0)
4315 restart_edit = 'a';
4316 }
4317 else
4318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 cap->oap->motion_type = MLINE;
4321 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4322 clearopbeep(cap->oap);
4323 else if (cap->arg)
4324 beginline(BL_WHITE | BL_FIX);
4325 }
4326 }
4327}
4328
4329#ifdef FEAT_SEARCHPATH
4330/*
4331 * Grab the file name under the cursor and edit it.
4332 */
4333 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004334nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
4336 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004337 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004339 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 {
4341 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004342 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 return;
4344 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004345 if (curbuf_locked())
4346 {
4347 clearop(cap->oap);
4348 return;
4349 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004350#ifdef FEAT_PROP_POPUP
4351 if (ERROR_IF_TERM_POPUP_WINDOW)
4352 return;
4353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004355 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 if (ptr != NULL)
4358 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004359 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004360 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004361 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004363 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004364 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004365 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004366 {
4367 curwin->w_cursor.lnum = lnum;
4368 check_cursor_lnum();
4369 beginline(BL_SOL | BL_FIX);
4370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 vim_free(ptr);
4372 }
4373 else
4374 clearop(cap->oap);
4375}
4376#endif
4377
4378/*
4379 * <End> command: to end of current line or last line.
4380 */
4381 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004382nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004384 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004386 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004388 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 nv_dollar(cap);
4391}
4392
4393/*
4394 * Handle the "$" command.
4395 */
4396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004397nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
4399 cap->oap->motion_type = MCHAR;
4400 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004401 // In virtual mode when off the edge of a line and an operator
4402 // is pending (whew!) keep the cursor where it is.
4403 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (!virtual_active() || gchar_cursor() != NUL
4405 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004406 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if (cursor_down((long)(cap->count1 - 1),
4408 cap->oap->op_type == OP_NOP) == FAIL)
4409 clearopbeep(cap->oap);
4410#ifdef FEAT_FOLDING
4411 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4412 foldOpenCursor();
4413#endif
4414}
4415
4416/*
4417 * Implementation of '?' and '/' commands.
4418 * If cap->arg is TRUE don't set PC mark.
4419 */
4420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004421nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004424 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4427 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004428 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 cap->cmdchar = 'g';
4430 cap->nchar = '?';
4431 nv_operator(cap);
4432 return;
4433 }
4434
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004435 // When using 'incsearch' the cursor may be moved to set a different search
4436 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004437 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439 if (cap->searchbuf == NULL)
4440 {
4441 clearop(oap);
4442 return;
4443 }
4444
Bram Moolenaar46539112015-02-17 15:43:57 +01004445 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004446 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004447 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448}
4449
4450/*
4451 * Handle "N" and "n" commands.
4452 * cap->arg is SEARCH_REV for "N", 0 for "n".
4453 */
4454 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004455nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004457 pos_T old = curwin->w_cursor;
4458 int wrapped = FALSE;
4459 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004460
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004461 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004462 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004463 // Avoid getting stuck on the current cursor position, which can
4464 // happen when an offset is given and the cursor is on the last char
4465 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004466 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004467 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004468 cap->count1 -= 1;
4469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470}
4471
4472/*
4473 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4474 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004475 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004477 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004478normal_search(
4479 cmdarg_T *cap,
4480 int dir,
4481 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004482 int opt, // extra flags for do_search()
4483 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
4485 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004486 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
4488 cap->oap->motion_type = MCHAR;
4489 cap->oap->inclusive = FALSE;
4490 cap->oap->use_reg_one = TRUE;
4491 curwin->w_set_curswant = TRUE;
4492
Bram Moolenaara80faa82020-04-12 19:37:17 +02004493 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004494 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004495 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4496 if (wrapped != NULL)
4497 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 if (i == 0)
4499 clearop(cap->oap);
4500 else
4501 {
4502 if (i == 2)
4503 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#ifdef FEAT_FOLDING
4506 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4507 foldOpenCursor();
4508#endif
4509 }
4510
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004511 // "/$" will put the cursor after the end of the line, may need to
4512 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004514 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515}
4516
4517/*
4518 * Character search commands.
4519 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4520 * ',' and FALSE for ';'.
4521 * cap->nchar is NUL for ',' and ';' (repeat the search)
4522 */
4523 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004524nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 int t_cmd;
4527
4528 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4529 t_cmd = TRUE;
4530 else
4531 t_cmd = FALSE;
4532
4533 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4535 clearopbeep(cap->oap);
4536 else
4537 {
4538 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004539 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4541 && (t_cmd || cap->oap->op_type != OP_NOP))
4542 {
4543 colnr_T scol, ecol;
4544
4545 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4546 curwin->w_cursor.coladd = ecol - scol;
4547 }
4548 else
4549 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#ifdef FEAT_FOLDING
4552 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4553 foldOpenCursor();
4554#endif
4555 }
4556}
4557
4558/*
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004559 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4560 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4561 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4562 * "[m" or "]m" search for prev/next start of (Java) method.
4563 * "[M" or "]M" search for prev/next end of (Java) method.
4564 */
4565 static void
4566nv_bracket_block(cmdarg_T *cap, pos_T *old_pos)
4567{
4568 pos_T new_pos = {0, 0, 0};
4569 pos_T *pos = NULL; // init for GCC
4570 pos_T prev_pos;
4571 long n;
4572 int findc;
4573 int c;
4574
4575 if (cap->nchar == '*')
4576 cap->nchar = '/';
4577 prev_pos.lnum = 0;
4578 if (cap->nchar == 'm' || cap->nchar == 'M')
4579 {
4580 if (cap->cmdchar == '[')
4581 findc = '{';
4582 else
4583 findc = '}';
4584 n = 9999;
4585 }
4586 else
4587 {
4588 findc = cap->nchar;
4589 n = cap->count1;
4590 }
4591 for ( ; n > 0; --n)
4592 {
4593 if ((pos = findmatchlimit(cap->oap, findc,
4594 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4595 {
4596 if (new_pos.lnum == 0) // nothing found
4597 {
4598 if (cap->nchar != 'm' && cap->nchar != 'M')
4599 clearopbeep(cap->oap);
4600 }
4601 else
4602 pos = &new_pos; // use last one found
4603 break;
4604 }
4605 prev_pos = new_pos;
4606 curwin->w_cursor = *pos;
4607 new_pos = *pos;
4608 }
4609 curwin->w_cursor = *old_pos;
4610
4611 // Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4612 // brought us to the match for "[m" and "]M" when inside a method.
4613 // Try finding the '{' or '}' we want to be at.
4614 // Also repeat for the given count.
4615 if (cap->nchar == 'm' || cap->nchar == 'M')
4616 {
4617 // norm is TRUE for "]M" and "[m"
4618 int norm = ((findc == '{') == (cap->nchar == 'm'));
4619
4620 n = cap->count1;
4621 // found a match: we were inside a method
4622 if (prev_pos.lnum != 0)
4623 {
4624 pos = &prev_pos;
4625 curwin->w_cursor = prev_pos;
4626 if (norm)
4627 --n;
4628 }
4629 else
4630 pos = NULL;
4631 while (n > 0)
4632 {
4633 for (;;)
4634 {
4635 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4636 {
4637 // if not found anything, that's an error
4638 if (pos == NULL)
4639 clearopbeep(cap->oap);
4640 n = 0;
4641 break;
4642 }
4643 c = gchar_cursor();
4644 if (c == '{' || c == '}')
4645 {
4646 // Must have found end/start of class: use it.
4647 // Or found the place to be at.
4648 if ((c == findc && norm) || (n == 1 && !norm))
4649 {
4650 new_pos = curwin->w_cursor;
4651 pos = &new_pos;
4652 n = 0;
4653 }
4654 // if no match found at all, we started outside of the
4655 // class and we're inside now. Just go on.
4656 else if (new_pos.lnum == 0)
4657 {
4658 new_pos = curwin->w_cursor;
4659 pos = &new_pos;
4660 }
4661 // found start/end of other method: go to match
4662 else if ((pos = findmatchlimit(cap->oap, findc,
4663 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4664 0)) == NULL)
4665 n = 0;
4666 else
4667 curwin->w_cursor = *pos;
4668 break;
4669 }
4670 }
4671 --n;
4672 }
4673 curwin->w_cursor = *old_pos;
4674 if (pos == NULL && new_pos.lnum != 0)
4675 clearopbeep(cap->oap);
4676 }
4677 if (pos != NULL)
4678 {
4679 setpcmark();
4680 curwin->w_cursor = *pos;
4681 curwin->w_set_curswant = TRUE;
4682#ifdef FEAT_FOLDING
4683 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4684 && cap->oap->op_type == OP_NOP)
4685 foldOpenCursor();
4686#endif
4687 }
4688}
4689
4690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 * "[" and "]" commands.
4692 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4693 */
4694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004695nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004698 pos_T *pos = NULL; // init for GCC
4699 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 int flag;
4701 long n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 cap->oap->motion_type = MCHAR;
4704 cap->oap->inclusive = FALSE;
4705 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004706 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004709 // "[f" or "]f" : Edit file under the cursor (same as "gf")
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (cap->nchar == 'f')
4711 nv_gotofile(cap);
4712 else
4713#endif
4714
4715#ifdef FEAT_FIND_ID
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004716 // Find the occurrence(s) of the identifier or define under cursor
4717 // in current and included files or jump to the first occurrence.
4718 //
4719 // search list jump
4720 // fwd bwd fwd bwd fwd bwd
4721 // identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4722 // define "]d" "[d" "]D" "[D" "]^D" "[^D"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004724# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004726# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 cap->nchar) != NULL)
4730 {
4731 char_u *ptr;
4732 int len;
4733
4734 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4735 clearop(cap->oap);
4736 else
4737 {
4738 find_pattern_in_path(ptr, 0, len, TRUE,
4739 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4740 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4741 cap->count1,
4742 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4743 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4744 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4745 (linenr_T)MAXLNUM);
4746 curwin->w_set_curswant = TRUE;
4747 }
4748 }
4749 else
4750#endif
4751
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004752 // "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4753 // "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4754 // "[/", "[*", "]/", "]*": go to Nth comment start/end.
4755 // "[m" or "]m" search for prev/next start of (Java) method.
4756 // "[M" or "]M" search for prev/next end of (Java) method.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if ( (cap->cmdchar == '['
4758 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4759 || (cap->cmdchar == ']'
4760 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004761 nv_bracket_block(cap, &old_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004763 // "[[", "[]", "]]" and "][": move to start or end of function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 else if (cap->nchar == '[' || cap->nchar == ']')
4765 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004766 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 flag = '{';
4768 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004769 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
4771 curwin->w_set_curswant = TRUE;
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004772 // Imitate strange Vi behaviour: When using "]]" with an operator
4773 // we also stop at '}'.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004774 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 (cap->oap->op_type != OP_NOP
4776 && cap->arg == FORWARD && flag == '{')))
4777 clearopbeep(cap->oap);
4778 else
4779 {
4780 if (cap->oap->op_type == OP_NOP)
4781 beginline(BL_WHITE | BL_FIX);
4782#ifdef FEAT_FOLDING
4783 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4784 foldOpenCursor();
4785#endif
4786 }
4787 }
4788
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004789 // "[p", "[P", "]P" and "]p": put with indent adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 else if (cap->nchar == 'p' || cap->nchar == 'P')
4791 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004792 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
4794
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004795 // "['", "[`", "]'" and "]`": jump to next mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 else if (cap->nchar == '\'' || cap->nchar == '`')
4797 {
4798 pos = &curwin->w_cursor;
4799 for (n = cap->count1; n > 0; --n)
4800 {
4801 prev_pos = *pos;
4802 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4803 cap->nchar == '\'');
4804 if (pos == NULL)
4805 break;
4806 }
4807 if (pos == NULL)
4808 pos = &prev_pos;
4809 nv_cursormark(cap, cap->nchar == '\'', pos);
4810 }
4811
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004812 // [ or ] followed by a middle mouse click: put selected text with
4813 // indent adjustment. Any other button just does as usual.
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004814 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816 (void)do_mouse(cap->oap, cap->nchar,
4817 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4818 cap->count1, PUT_FIXINDENT);
4819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821#ifdef FEAT_FOLDING
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004822 // "[z" and "]z": move to start or end of open fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 else if (cap->nchar == 'z')
4824 {
4825 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4826 cap->count1) == FAIL)
4827 clearopbeep(cap->oap);
4828 }
4829#endif
4830
4831#ifdef FEAT_DIFF
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004832 // "[c" and "]c": move to next or previous diff-change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 else if (cap->nchar == 'c')
4834 {
4835 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4836 cap->count1) == FAIL)
4837 clearopbeep(cap->oap);
4838 }
4839#endif
4840
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004841#ifdef FEAT_SPELL
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00004842 // "[s", "[S", "]s" and "]S": move to next spell error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004843 else if (cap->nchar == 's' || cap->nchar == 'S')
4844 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004845 setpcmark();
4846 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004847 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4848 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004849 {
4850 clearopbeep(cap->oap);
4851 break;
4852 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004853 else
4854 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004855# ifdef FEAT_FOLDING
4856 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4857 foldOpenCursor();
4858# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004859 }
4860#endif
4861
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004862 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 else
4864 clearopbeep(cap->oap);
4865}
4866
4867/*
4868 * Handle Normal mode "%" command.
4869 */
4870 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004871nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872{
4873 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004874#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 linenr_T lnum = curwin->w_cursor.lnum;
4876#endif
4877
4878 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004879 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 if (cap->count0 > 100)
4882 clearopbeep(cap->oap);
4883 else
4884 {
4885 cap->oap->motion_type = MLINE;
4886 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004887 // Round up, so 'normal 100%' always jumps at the line line.
4888 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4889 // overflow on 32-bits, so use a formula with less accuracy
4890 // to avoid overflows.
4891 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4893 / 100L * cap->count0;
4894 else
4895 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4896 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004897 if (curwin->w_cursor.lnum < 1)
4898 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4900 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4901 beginline(BL_SOL | BL_FIX);
4902 }
4903 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004904 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
4906 cap->oap->motion_type = MCHAR;
4907 cap->oap->use_reg_one = TRUE;
4908 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4909 clearopbeep(cap->oap);
4910 else
4911 {
4912 setpcmark();
4913 curwin->w_cursor = *pos;
4914 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 }
4919#ifdef FEAT_FOLDING
4920 if (cap->oap->op_type == OP_NOP
4921 && lnum != curwin->w_cursor.lnum
4922 && (fdo_flags & FDO_PERCENT)
4923 && KeyTyped)
4924 foldOpenCursor();
4925#endif
4926}
4927
4928/*
4929 * Handle "(" and ")" commands.
4930 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4931 */
4932 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004933nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
4935 cap->oap->motion_type = MCHAR;
4936 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004937 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004938 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 curwin->w_set_curswant = TRUE;
4940
4941 if (findsent(cap->arg, cap->count1) == FAIL)
4942 clearopbeep(cap->oap);
4943 else
4944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004945 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004946 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948#ifdef FEAT_FOLDING
4949 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4950 foldOpenCursor();
4951#endif
4952 }
4953}
4954
4955/*
4956 * "m" command: Mark a position.
4957 */
4958 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004959nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
4961 if (!checkclearop(cap->oap))
4962 {
4963 if (setmark(cap->nchar) == FAIL)
4964 clearopbeep(cap->oap);
4965 }
4966}
4967
4968/*
4969 * "{" and "}" commands.
4970 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4971 */
4972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004973nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
4975 cap->oap->motion_type = MCHAR;
4976 cap->oap->inclusive = FALSE;
4977 cap->oap->use_reg_one = TRUE;
4978 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004979 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 clearopbeep(cap->oap);
4981 else
4982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984#ifdef FEAT_FOLDING
4985 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4986 foldOpenCursor();
4987#endif
4988 }
4989}
4990
4991/*
4992 * "u" command: Undo or make lower case.
4993 */
4994 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004995nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004997 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004999 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 cap->cmdchar = 'g';
5001 cap->nchar = 'u';
5002 nv_operator(cap);
5003 }
5004 else
5005 nv_kundo(cap);
5006}
5007
5008/*
5009 * <Undo> command.
5010 */
5011 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005012nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013{
5014 if (!checkclearopq(cap->oap))
5015 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02005016#ifdef FEAT_JOB_CHANNEL
5017 if (bt_prompt(curbuf))
5018 {
5019 clearopbeep(cap->oap);
5020 return;
5021 }
5022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 u_undo((int)cap->count1);
5024 curwin->w_set_curswant = TRUE;
5025 }
5026}
5027
5028/*
5029 * Handle the "r" command.
5030 */
5031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005032nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
5034 char_u *ptr;
5035 int had_ctrl_v;
5036 long n;
5037
5038 if (checkclearop(cap->oap))
5039 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02005040#ifdef FEAT_JOB_CHANNEL
5041 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5042 {
5043 clearopbeep(cap->oap);
5044 return;
5045 }
5046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005048 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 if (cap->nchar == Ctrl_V)
5050 {
5051 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01005052 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005053 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (cap->nchar > DEL)
5055 had_ctrl_v = NUL;
5056 }
5057 else
5058 had_ctrl_v = NUL;
5059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005060 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005061 if (IS_SPECIAL(cap->nchar))
5062 {
5063 clearopbeep(cap->oap);
5064 return;
5065 }
5066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005067 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (VIsual_active)
5069 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00005070 if (got_int)
5071 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01005072 if (had_ctrl_v)
5073 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005074 // Use a special (negative) number to make a difference between a
5075 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01005076 if (cap->nchar == CAR)
5077 cap->nchar = REPLACE_CR_NCHAR;
5078 else if (cap->nchar == NL)
5079 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01005080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 nv_operator(cap);
5082 return;
5083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005085 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (virtual_active())
5087 {
5088 if (u_save_cursor() == FAIL)
5089 return;
5090 if (gchar_cursor() == NUL)
5091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005092 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 coladvance_force((colnr_T)(getviscol() + cap->count1));
5094 curwin->w_cursor.col -= cap->count1;
5095 }
5096 else if (gchar_cursor() == TAB)
5097 coladvance_force(getviscol());
5098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005100 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005102 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005103 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 clearopbeep(cap->oap);
5106 return;
5107 }
5108
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005109 // Replacing with a TAB is done by edit() when it is complicated because
5110 // 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5111 // Other characters are done below to avoid problems with things like
5112 // CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5114 {
5115 stuffnumReadbuff(cap->count1);
5116 stuffcharReadbuff('R');
5117 stuffcharReadbuff('\t');
5118 stuffcharReadbuff(ESC);
5119 return;
5120 }
5121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005122 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (u_save_cursor() == FAIL)
5124 return;
5125
5126 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5127 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005128 // Replace character(s) by a single newline.
5129 // Strange vi behaviour: Only one newline is inserted.
5130 // Delete the characters here.
5131 // Insert the newline with an insert command, takes care of
5132 // autoindent. The insert command depends on being on the last
5133 // character of a line or not.
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005134 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 stuffcharReadbuff('\r');
5136 stuffcharReadbuff(ESC);
5137
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005138 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 invoke_edit(cap, TRUE, 'r', FALSE);
5140 }
5141 else
5142 {
5143 prep_redo(cap->oap->regname, cap->count1,
5144 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5145
5146 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if (has_mbyte)
5148 {
5149 int old_State = State;
5150
5151 if (cap->ncharC1 != 0)
5152 AppendCharToRedobuff(cap->ncharC1);
5153 if (cap->ncharC2 != 0)
5154 AppendCharToRedobuff(cap->ncharC2);
5155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005156 // This is slow, but it handles replacing a single-byte with a
5157 // multi-byte and the other way around. Also handles adding
5158 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 for (n = cap->count1; n > 0; --n)
5160 {
5161 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005162 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5163 {
5164 int c = ins_copychar(curwin->w_cursor.lnum
5165 + (cap->nchar == Ctrl_Y ? -1 : 1));
5166 if (c != NUL)
5167 ins_char(c);
5168 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005169 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005170 ++curwin->w_cursor.col;
5171 }
5172 else
5173 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 State = old_State;
5175 if (cap->ncharC1 != 0)
5176 ins_char(cap->ncharC1);
5177 if (cap->ncharC2 != 0)
5178 ins_char(cap->ncharC2);
5179 }
5180 }
5181 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005183 // Replace the characters within one line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 for (n = cap->count1; n > 0; --n)
5185 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005186 // Get ptr again, because u_save and/or showmatch() will have
5187 // released the line. This may also happen in ins_copychar().
5188 // At the same time we let know that the line will be changed.
Bram Moolenaar8320da42012-04-30 18:18:47 +02005189 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5190 {
5191 int c = ins_copychar(curwin->w_cursor.lnum
5192 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005193
5194 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005195 if (c != NUL)
5196 ptr[curwin->w_cursor.col] = c;
5197 }
5198 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005199 {
5200 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005201 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (p_sm && msg_silent == 0)
5204 showmatch(cap->nchar);
5205 ++curwin->w_cursor.col;
5206 }
5207#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005208 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005210 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211
Bram Moolenaar009b2592004-10-24 19:18:58 +00005212 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005213 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005215 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217#endif
5218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005219 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 changed_bytes(curwin->w_cursor.lnum,
5221 (colnr_T)(curwin->w_cursor.col - cap->count1));
5222 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005223 --curwin->w_cursor.col; // cursor on the last replaced char
5224 // if the character on the left of the current cursor is a multi-byte
5225 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (has_mbyte)
5227 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 curbuf->b_op_end = curwin->w_cursor;
5229 curwin->w_set_curswant = TRUE;
5230 set_last_insert(cap->nchar);
5231 }
5232}
5233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234/*
5235 * 'o': Exchange start and end of Visual area.
5236 * 'O': same, but in block mode exchange left and right corners.
5237 */
5238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005239v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
5241 pos_T old_cursor;
5242 colnr_T left, right;
5243
5244 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5245 {
5246 old_cursor = curwin->w_cursor;
5247 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5248 curwin->w_cursor.lnum = VIsual.lnum;
5249 coladvance(left);
5250 VIsual = curwin->w_cursor;
5251
5252 curwin->w_cursor.lnum = old_cursor.lnum;
5253 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005254 // 'selection "exclusive" and cursor at right-bottom corner: move it
5255 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5257 ++curwin->w_curswant;
5258 coladvance(curwin->w_curswant);
5259 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005261 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
5263 curwin->w_cursor.lnum = VIsual.lnum;
5264 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5265 ++right;
5266 coladvance(right);
5267 VIsual = curwin->w_cursor;
5268
5269 curwin->w_cursor.lnum = old_cursor.lnum;
5270 coladvance(left);
5271 curwin->w_curswant = left;
5272 }
5273 }
5274 else
5275 {
5276 old_cursor = curwin->w_cursor;
5277 curwin->w_cursor = VIsual;
5278 VIsual = old_cursor;
5279 curwin->w_set_curswant = TRUE;
5280 }
5281}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283/*
5284 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5285 */
5286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005287nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005289 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
5291 cap->cmdchar = 'c';
5292 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005293 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 VIsual_mode = 'V';
5295 nv_operator(cap);
5296 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005297 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 {
5299 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005300 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 else
5302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if (virtual_active())
5304 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5306 }
5307 }
5308}
5309
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310/*
5311 * "gr".
5312 */
5313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005314nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 if (VIsual_active)
5317 {
5318 cap->cmdchar = 'r';
5319 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005320 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005322 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
5324 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005325 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 else
5327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005328 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005329 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 stuffcharReadbuff(cap->extra_char);
5331 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (virtual_active())
5333 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 invoke_edit(cap, TRUE, 'v', FALSE);
5335 }
5336 }
5337}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
5339/*
5340 * Swap case for "~" command, when it does not work like an operator.
5341 */
5342 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005343n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
5345 long n;
5346 pos_T startpos;
5347 int did_change = 0;
5348#ifdef FEAT_NETBEANS_INTG
5349 pos_T pos;
5350 char_u *ptr;
5351 int count;
5352#endif
5353
5354 if (checkclearopq(cap->oap))
5355 return;
5356
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005357 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
5359 clearopbeep(cap->oap);
5360 return;
5361 }
5362
5363 prep_redo_cmd(cap);
5364
5365 if (u_save_cursor() == FAIL)
5366 return;
5367
5368 startpos = curwin->w_cursor;
5369#ifdef FEAT_NETBEANS_INTG
5370 pos = startpos;
5371#endif
5372 for (n = cap->count1; n > 0; --n)
5373 {
5374 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5375 inc_cursor();
5376 if (gchar_cursor() == NUL)
5377 {
5378 if (vim_strchr(p_ww, '~') != NULL
5379 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5380 {
5381#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005382 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
5384 if (did_change)
5385 {
5386 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005387 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005388 netbeans_removed(curbuf, pos.lnum, pos.col,
5389 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005391 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 }
5393 pos.col = 0;
5394 pos.lnum++;
5395 }
5396#endif
5397 ++curwin->w_cursor.lnum;
5398 curwin->w_cursor.col = 0;
5399 if (n > 1)
5400 {
5401 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5402 break;
5403 u_clearline();
5404 }
5405 }
5406 else
5407 break;
5408 }
5409 }
5410#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005411 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
5413 ptr = ml_get(pos.lnum);
5414 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005415 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5416 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418#endif
5419
5420
5421 check_cursor();
5422 curwin->w_set_curswant = TRUE;
5423 if (did_change)
5424 {
5425 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5426 0L);
5427 curbuf->b_op_start = startpos;
5428 curbuf->b_op_end = curwin->w_cursor;
5429 if (curbuf->b_op_end.col > 0)
5430 --curbuf->b_op_end.col;
5431 }
5432}
5433
5434/*
5435 * Move cursor to mark.
5436 */
5437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005438nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439{
5440 if (check_mark(pos) == FAIL)
5441 clearop(cap->oap);
5442 else
5443 {
5444 if (cap->cmdchar == '\''
5445 || cap->cmdchar == '`'
5446 || cap->cmdchar == '['
5447 || cap->cmdchar == ']')
5448 setpcmark();
5449 curwin->w_cursor = *pos;
5450 if (flag)
5451 beginline(BL_WHITE | BL_FIX);
5452 else
5453 check_cursor();
5454 }
5455 cap->oap->motion_type = flag ? MLINE : MCHAR;
5456 if (cap->cmdchar == '`')
5457 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005458 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 curwin->w_set_curswant = TRUE;
5460}
5461
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462/*
5463 * Handle commands that are operators in Visual mode.
5464 */
5465 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005466v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005470 // Uppercase means linewise, except in block mode, then "D" deletes till
5471 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 if (isupper(cap->cmdchar))
5473 {
5474 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005475 {
5476 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5480 curwin->w_curswant = MAXCOL;
5481 }
5482 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5483 nv_operator(cap);
5484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486/*
5487 * "s" and "S" commands.
5488 */
5489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005490nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005492#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005493 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005494 if (term_swap_diff() == OK)
5495 return;
5496#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005497#ifdef FEAT_JOB_CHANNEL
5498 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5499 {
5500 clearopbeep(cap->oap);
5501 return;
5502 }
5503#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005504 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005507 {
5508 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 cap->cmdchar = 'c';
5512 nv_operator(cap);
5513 }
5514 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 nv_optrans(cap);
5516}
5517
5518/*
5519 * Abbreviated commands.
5520 */
5521 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005522nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
5524 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005525 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005527 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (VIsual_active)
5529 v_visop(cap);
5530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 nv_optrans(cap);
5532}
5533
5534/*
5535 * Translate a command into another command.
5536 */
5537 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005538nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5541 (char_u *)"d$", (char_u *)"c$",
5542 (char_u *)"cl", (char_u *)"cc",
5543 (char_u *)"yy", (char_u *)":s\r"};
5544 static char_u *str = (char_u *)"xXDCsSY&";
5545
5546 if (!checkclearopq(cap->oap))
5547 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005548 // In Vi "2D" doesn't delete the next line. Can't translate it
5549 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005550 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5551 {
5552 cap->oap->start = curwin->w_cursor;
5553 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005554#ifdef FEAT_EVAL
5555 set_op_var(OP_DELETE);
5556#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005557 cap->count1 = 1;
5558 nv_dollar(cap);
5559 finish_op = TRUE;
5560 ResetRedobuff();
5561 AppendCharToRedobuff('D');
5562 }
5563 else
5564 {
5565 if (cap->count0)
5566 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005567 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570 cap->opcount = 0;
5571}
5572
5573/*
5574 * "'" and "`" commands. Also for "g'" and "g`".
5575 * cap->arg is TRUE for "'" and "g'".
5576 */
5577 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005578nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 pos_T *pos;
5581 int c;
5582#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005583 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005584 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585#endif
5586
5587 if (cap->cmdchar == 'g')
5588 c = cap->extra_char;
5589 else
5590 c = cap->nchar;
5591 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005592 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
5594 if (cap->arg)
5595 {
5596 check_cursor_lnum();
5597 beginline(BL_WHITE | BL_FIX);
5598 }
5599 else
5600 check_cursor();
5601 }
5602 else
5603 nv_cursormark(cap, cap->arg, pos);
5604
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005605 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if (!virtual_active())
5607 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005608 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609#ifdef FEAT_FOLDING
5610 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005611 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005612 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 && (fdo_flags & FDO_MARK)
5614 && old_KeyTyped)
5615 foldOpenCursor();
5616#endif
5617}
5618
5619/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005620 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 */
5622 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005623nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005626#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005628 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
5631 if (!checkclearopq(cap->oap))
5632 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005633 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5634 {
5635 if (goto_tabpage_lastused() == FAIL)
5636 clearopbeep(cap->oap);
5637 return;
5638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 if (cap->cmdchar == 'g')
5640 pos = movechangelist((int)cap->count1);
5641 else
5642 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005643 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 {
5645 curwin->w_set_curswant = TRUE;
5646 check_cursor();
5647 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005648 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 nv_cursormark(cap, FALSE, pos);
5650 else if (cap->cmdchar == 'g')
5651 {
5652 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005653 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005655 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005657 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 }
5659 else
5660 clearopbeep(cap->oap);
5661# ifdef FEAT_FOLDING
5662 if (cap->oap->op_type == OP_NOP
5663 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5664 && (fdo_flags & FDO_MARK)
5665 && old_KeyTyped)
5666 foldOpenCursor();
5667# endif
5668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669}
5670
5671/*
5672 * Handle '"' command.
5673 */
5674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005675nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 if (checkclearop(cap->oap))
5678 return;
5679#ifdef FEAT_EVAL
5680 if (cap->nchar == '=')
5681 cap->nchar = get_expr_register();
5682#endif
5683 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5684 {
5685 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005686 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687#ifdef FEAT_EVAL
5688 set_reg_var(cap->oap->regname);
5689#endif
5690 }
5691 else
5692 clearopbeep(cap->oap);
5693}
5694
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695/*
5696 * Handle "v", "V" and "CTRL-V" commands.
5697 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5698 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005699 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 */
5701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005702nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005704 if (cap->cmdchar == Ctrl_Q)
5705 cap->cmdchar = Ctrl_V;
5706
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005707 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5708 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (cap->oap->op_type != OP_NOP)
5710 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005711 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005712 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 return;
5714 }
5715
5716 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005717 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005719 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005721 else // toggle char/block mode
5722 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 VIsual_mode = cap->cmdchar;
5724 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005725 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005727 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005729 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 {
5731 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005732 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005734 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 VIsual = curwin->w_cursor;
5736
5737 VIsual_active = TRUE;
5738 VIsual_reselect = TRUE;
5739 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005740 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005743 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005744 redraw_cmdline = TRUE; // show visual mode later
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00005745 // For V and ^V, we multiply the number of lines even if there
5746 // was only one -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5748 {
5749 curwin->w_cursor.lnum +=
5750 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005751 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 }
5753 VIsual_mode = resel_VIsual_mode;
5754 if (VIsual_mode == 'v')
5755 {
5756 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005757 {
5758 validate_virtcol();
5759 curwin->w_curswant = curwin->w_virtcol
5760 + resel_VIsual_vcol * cap->count0 - 1;
5761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005763 curwin->w_curswant = resel_VIsual_vcol;
5764 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005766 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 {
5768 curwin->w_curswant = MAXCOL;
5769 coladvance((colnr_T)MAXCOL);
5770 }
5771 else if (VIsual_mode == Ctrl_V)
5772 {
5773 validate_virtcol();
5774 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005775 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 coladvance(curwin->w_curswant);
5777 }
5778 else
5779 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005780 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
5782 else
5783 {
5784 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005785 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 may_start_select('c');
5787 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005788 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005789 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005790 if (cap->count0 > 0 && --cap->count1 > 0)
5791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005792 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005793 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5794 nv_right(cap);
5795 else if (VIsual_mode == 'V')
5796 nv_down(cap);
5797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
5799 }
5800}
5801
5802/*
5803 * Start selection for Shift-movement keys.
5804 */
5805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005806start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005808 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 may_start_select('k');
5810 n_start_visual_mode('v');
5811}
5812
5813/*
5814 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5815 */
5816 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005817may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
5819 VIsual_select = (stuff_empty() && typebuf_typed()
5820 && (vim_strchr(p_slm, c) != NULL));
5821}
5822
5823/*
5824 * Start Visual mode "c".
5825 * Should set VIsual_select before calling this.
5826 */
5827 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005828n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005830#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005831 int cursor_line_was_concealed = curwin->w_p_cole > 0
5832 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005833#endif
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 VIsual_mode = c;
5836 VIsual_active = TRUE;
5837 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005838
5839 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005840 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005841 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005842 {
5843 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 VIsual = curwin->w_cursor;
5847
5848#ifdef FEAT_FOLDING
5849 foldAdjustVisual();
5850#endif
5851
Bram Moolenaara0620062021-11-17 16:52:40 +00005852 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005854#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005855 // Check if redraw is needed after changing the state.
5856 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005857#endif
5858
Bram Moolenaar09df3122006-01-23 22:23:09 +00005859 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005860 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005862 // Make sure the clipboard gets updated. Needed because start and
5863 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 clip_star.vmode = NUL;
5865#endif
5866
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005867 // Only need to redraw this line, unless still need to redraw an old
5868 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 if (curwin->w_redr_type < INVERTED)
5870 {
5871 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5872 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5873 }
5874}
5875
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
5877/*
5878 * CTRL-W: Window commands
5879 */
5880 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005881nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005883 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005884 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005885 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005886 cap->cmdchar = ':';
5887 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005888 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005889 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005890 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005891 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892}
5893
5894/*
5895 * CTRL-Z: Suspend
5896 */
5897 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005898nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899{
5900 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005902 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005903 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904}
5905
5906/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005907 * "gv": Reselect the previous Visual area. If Visual already active,
5908 * exchange previous and current Visual area.
5909 */
5910 static void
5911nv_gv_cmd(cmdarg_T *cap)
5912{
5913 pos_T tpos;
5914 int i;
5915
5916 if (checkclearop(cap->oap))
5917 return;
5918
5919 if (curbuf->b_visual.vi_start.lnum == 0
5920 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5921 || curbuf->b_visual.vi_end.lnum == 0)
5922 {
5923 beep_flush();
5924 return;
5925 }
5926
5927 // set w_cursor to the start of the Visual area, tpos to the end
5928 if (VIsual_active)
5929 {
5930 i = VIsual_mode;
5931 VIsual_mode = curbuf->b_visual.vi_mode;
5932 curbuf->b_visual.vi_mode = i;
5933# ifdef FEAT_EVAL
5934 curbuf->b_visual_mode_eval = i;
5935# endif
5936 i = curwin->w_curswant;
5937 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5938 curbuf->b_visual.vi_curswant = i;
5939
5940 tpos = curbuf->b_visual.vi_end;
5941 curbuf->b_visual.vi_end = curwin->w_cursor;
5942 curwin->w_cursor = curbuf->b_visual.vi_start;
5943 curbuf->b_visual.vi_start = VIsual;
5944 }
5945 else
5946 {
5947 VIsual_mode = curbuf->b_visual.vi_mode;
5948 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5949 tpos = curbuf->b_visual.vi_end;
5950 curwin->w_cursor = curbuf->b_visual.vi_start;
5951 }
5952
5953 VIsual_active = TRUE;
5954 VIsual_reselect = TRUE;
5955
5956 // Set Visual to the start and w_cursor to the end of the Visual
5957 // area. Make sure they are on an existing character.
5958 check_cursor();
5959 VIsual = curwin->w_cursor;
5960 curwin->w_cursor = tpos;
5961 check_cursor();
5962 update_topline();
5963
5964 // When called from normal "g" command: start Select mode when
5965 // 'selectmode' contains "cmd". When called for K_SELECT, always
5966 // start Select mode.
5967 if (cap->arg)
5968 {
5969 VIsual_select = TRUE;
5970 VIsual_select_reg = 0;
5971 }
5972 else
5973 may_start_select('c');
5974 setmouse();
5975#ifdef FEAT_CLIPBOARD
5976 // Make sure the clipboard gets updated. Needed because start and
5977 // end are still the same, and the selection needs to be owned
5978 clip_star.vmode = NUL;
5979#endif
5980 redraw_curbuf_later(INVERTED);
5981 showmode();
5982}
5983
5984/*
5985 * "g0", "g^" : Like "0" and "^" but for screen lines.
5986 * "gm": middle of "g0" and "g$".
5987 */
5988 static void
5989nv_g_home_m_cmd(cmdarg_T *cap)
5990{
5991 int i;
5992 int flag = FALSE;
5993
5994 if (cap->nchar == '^')
5995 flag = TRUE;
5996
5997 cap->oap->motion_type = MCHAR;
5998 cap->oap->inclusive = FALSE;
5999 if (curwin->w_p_wrap && curwin->w_width != 0)
6000 {
6001 int width1 = curwin->w_width - curwin_col_off();
6002 int width2 = width1 + curwin_col_off2();
6003
6004 validate_virtcol();
6005 i = 0;
6006 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6007 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6008 }
6009 else
6010 i = curwin->w_leftcol;
6011 // Go to the middle of the screen line. When 'number' or
6012 // 'relativenumber' is on and lines are wrapping the middle can be more
6013 // to the left.
6014 if (cap->nchar == 'm')
6015 i += (curwin->w_width - curwin_col_off()
6016 + ((curwin->w_p_wrap && i > 0)
6017 ? curwin_col_off2() : 0)) / 2;
6018 coladvance((colnr_T)i);
6019 if (flag)
6020 {
6021 do
6022 i = gchar_cursor();
6023 while (VIM_ISWHITE(i) && oneright() == OK);
6024 curwin->w_valid &= ~VALID_WCOL;
6025 }
6026 curwin->w_set_curswant = TRUE;
6027}
6028
6029/*
6030 * "g_": to the last non-blank character in the line or <count> lines
6031 * downward.
6032 */
6033 static void
6034nv_g_underscore_cmd(cmdarg_T *cap)
6035{
6036 char_u *ptr;
6037
6038 cap->oap->motion_type = MCHAR;
6039 cap->oap->inclusive = TRUE;
6040 curwin->w_curswant = MAXCOL;
6041 if (cursor_down((long)(cap->count1 - 1),
6042 cap->oap->op_type == OP_NOP) == FAIL)
6043 {
6044 clearopbeep(cap->oap);
6045 return;
6046 }
6047
6048 ptr = ml_get_curline();
6049
6050 // In Visual mode we may end up after the line.
6051 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6052 --curwin->w_cursor.col;
6053
6054 // Decrease the cursor column until it's on a non-blank.
6055 while (curwin->w_cursor.col > 0
6056 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
6057 --curwin->w_cursor.col;
6058 curwin->w_set_curswant = TRUE;
6059 adjust_for_sel(cap);
6060}
6061
6062/*
6063 * "g$" : Like "$" but for screen lines.
6064 */
6065 static void
6066nv_g_dollar_cmd(cmdarg_T *cap)
6067{
6068 oparg_T *oap = cap->oap;
6069 int i;
6070 int col_off = curwin_col_off();
6071
6072 oap->motion_type = MCHAR;
6073 oap->inclusive = TRUE;
6074 if (curwin->w_p_wrap && curwin->w_width != 0)
6075 {
6076 curwin->w_curswant = MAXCOL; // so we stay at the end
6077 if (cap->count1 == 1)
6078 {
6079 int width1 = curwin->w_width - col_off;
6080 int width2 = width1 + curwin_col_off2();
6081
6082 validate_virtcol();
6083 i = width1 - 1;
6084 if (curwin->w_virtcol >= (colnr_T)width1)
6085 i += ((curwin->w_virtcol - width1) / width2 + 1)
6086 * width2;
6087 coladvance((colnr_T)i);
6088
6089 // Make sure we stick in this column.
6090 validate_virtcol();
6091 curwin->w_curswant = curwin->w_virtcol;
6092 curwin->w_set_curswant = FALSE;
6093 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6094 {
6095 // Check for landing on a character that got split at
6096 // the end of the line. We do not want to advance to
6097 // the next screen line.
6098 if (curwin->w_virtcol > (colnr_T)i)
6099 --curwin->w_cursor.col;
6100 }
6101 }
6102 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6103 clearopbeep(oap);
6104 }
6105 else
6106 {
6107 if (cap->count1 > 1)
6108 // if it fails, let the cursor still move to the last char
6109 (void)cursor_down(cap->count1 - 1, FALSE);
6110
6111 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
6112 coladvance((colnr_T)i);
6113
6114 // if the character doesn't fit move one back
6115 if (curwin->w_cursor.col > 0
6116 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6117 {
6118 colnr_T vcol;
6119
6120 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6121 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6122 --curwin->w_cursor.col;
6123 }
6124
6125 // Make sure we stick in this column.
6126 validate_virtcol();
6127 curwin->w_curswant = curwin->w_virtcol;
6128 curwin->w_set_curswant = FALSE;
6129 }
6130}
6131
6132/*
6133 * "gi": start Insert at the last position.
6134 */
6135 static void
6136nv_gi_cmd(cmdarg_T *cap)
6137{
6138 int i;
6139
6140 if (curbuf->b_last_insert.lnum != 0)
6141 {
6142 curwin->w_cursor = curbuf->b_last_insert;
6143 check_cursor_lnum();
6144 i = (int)STRLEN(ml_get_curline());
6145 if (curwin->w_cursor.col > (colnr_T)i)
6146 {
6147 if (virtual_active())
6148 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
6149 curwin->w_cursor.col = i;
6150 }
6151 }
6152 cap->cmdchar = 'i';
6153 nv_edit(cap);
6154}
6155
6156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 * Commands starting with "g".
6158 */
6159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006160nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
6162 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
6165 switch (cap->nchar)
6166 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006167 case Ctrl_A:
6168 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006170 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006171 if (!VIsual_active && cap->nchar == Ctrl_A)
6172 vim_mem_profile_dump();
6173 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006175 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006176 if (VIsual_active)
6177 {
6178 cap->arg = TRUE;
6179 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006180 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006181 nv_addsub(cap);
6182 }
6183 else
6184 clearopbeep(oap);
6185 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006187 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 case 'R':
6189 cap->arg = TRUE;
6190 nv_Replace(cap);
6191 break;
6192
6193 case 'r':
6194 nv_vreplace(cap);
6195 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
6197 case '&':
6198 do_cmdline_cmd((char_u *)"%s//~/&");
6199 break;
6200
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006201 // "gv": Reselect the previous Visual area. If Visual already active,
6202 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006204 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006206
6207 // "gV": Don't reselect the previous Visual area after a Select mode
6208 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 case 'V':
6210 VIsual_reselect = FALSE;
6211 break;
6212
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006213 // "gh": start Select mode.
6214 // "gH": start Select line mode.
6215 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 case K_BS:
6217 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006218 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 case 'h':
6220 case 'H':
6221 case Ctrl_H:
6222# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006223 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (cap->nchar == Ctrl_H)
6225 cap->cmdchar = Ctrl_V;
6226 else
6227# endif
6228 cap->cmdchar = cap->nchar + ('v' - 'h');
6229 cap->arg = TRUE;
6230 nv_visual(cap);
6231 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006233 // "gn", "gN" visually select next/previous search match
6234 // "gn" selects next match
6235 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006236 case 'N':
6237 case 'n':
6238 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006239 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006240 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006242 // "gj" and "gk" two new funny movement keys -- up and down
6243 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 case 'j':
6245 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006246 // with 'nowrap' it works just like the normal "j" command.
6247 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 {
6249 oap->motion_type = MLINE;
6250 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6251 }
6252 else
6253 i = nv_screengo(oap, FORWARD, cap->count1);
6254 if (i == FAIL)
6255 clearopbeep(oap);
6256 break;
6257
6258 case 'k':
6259 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006260 // with 'nowrap' it works just like the normal "k" command.
6261 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263 oap->motion_type = MLINE;
6264 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6265 }
6266 else
6267 i = nv_screengo(oap, BACKWARD, cap->count1);
6268 if (i == FAIL)
6269 clearopbeep(oap);
6270 break;
6271
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006272 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 case 'J':
6274 nv_join(cap);
6275 break;
6276
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006277 // "g0", "g^" : Like "0" and "^" but for screen lines.
6278 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 case '0':
6281 case 'm':
6282 case K_HOME:
6283 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006284 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 break;
6286
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006287 case 'M':
6288 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006289 oap->motion_type = MCHAR;
6290 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006291 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006292 if (cap->count0 > 0 && cap->count0 <= 100)
6293 coladvance((colnr_T)(i * cap->count0 / 100));
6294 else
6295 coladvance((colnr_T)(i / 2));
6296 curwin->w_set_curswant = TRUE;
6297 }
6298 break;
6299
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006300 // "g_": to the last non-blank character in the line or <count> lines
6301 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006303 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 break;
6305
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006306 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 case '$':
6308 case K_END:
6309 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006310 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 break;
6312
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006313 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 case '*':
6315 case '#':
6316#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006317 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006319 case Ctrl_RSB: // :tag or :tselect for current identifier
6320 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 nv_ident(cap);
6322 break;
6323
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006324 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 case 'e':
6326 case 'E':
6327 oap->motion_type = MCHAR;
6328 curwin->w_set_curswant = TRUE;
6329 oap->inclusive = TRUE;
6330 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6331 clearopbeep(oap);
6332 break;
6333
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006334 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006336 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 break;
6338
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006339 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006341 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 break;
6343
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006344 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 case 'I':
6346 beginline(0);
6347 if (!checkclearopq(oap))
6348 invoke_edit(cap, FALSE, 'g', FALSE);
6349 break;
6350
6351#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006352 // "gf": goto file, edit file under cursor
6353 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006355 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 nv_gotofile(cap);
6357 break;
6358#endif
6359
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006360 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 case '\'':
6362 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006363 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 case '`':
6365 nv_gomark(cap);
6366 break;
6367
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006368 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006370 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 break;
6372
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006373 // "ga": Display the ascii value of the character under the
6374 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 case 'a':
6376 do_ascii(NULL);
6377 break;
6378
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006379 // "g8": Display the bytes used for the UTF-8 character under the
6380 // cursor. It is displayed in hex.
6381 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006383 if (cap->count0 == 8)
6384 utf_find_illegal();
6385 else
6386 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006389 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006390 case '<':
6391 show_sb_text();
6392 break;
6393
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006394 // "gg": Goto the first line in file. With a count it goes to
6395 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 case 'g':
6397 cap->arg = FALSE;
6398 nv_goto(cap);
6399 break;
6400
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006401 // Two-character operators:
6402 // "gq" Format text
6403 // "gw" Format text and keep cursor position
6404 // "g~" Toggle the case of the text.
6405 // "gu" Change text to lower case.
6406 // "gU" Change text to upper case.
6407 // "g?" rot13 encoding
6408 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 case 'q':
6410 case 'w':
6411 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006412 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 case '~':
6414 case 'u':
6415 case 'U':
6416 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006417 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 nv_operator(cap);
6419 break;
6420
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006421 // "gd": Find first occurrence of pattern under the cursor in the
6422 // current function
6423 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 case 'd':
6425 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006426 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 break;
6428
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006429 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 case K_MIDDLEMOUSE:
6431 case K_MIDDLEDRAG:
6432 case K_MIDDLERELEASE:
6433 case K_LEFTMOUSE:
6434 case K_LEFTDRAG:
6435 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006436 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 case K_RIGHTMOUSE:
6438 case K_RIGHTDRAG:
6439 case K_RIGHTRELEASE:
6440 case K_X1MOUSE:
6441 case K_X1DRAG:
6442 case K_X1RELEASE:
6443 case K_X2MOUSE:
6444 case K_X2DRAG:
6445 case K_X2RELEASE:
6446 mod_mask = MOD_MASK_CTRL;
6447 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6448 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449
6450 case K_IGNORE:
6451 break;
6452
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006453 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 case 'p':
6455 case 'P':
6456 nv_put(cap);
6457 break;
6458
6459#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006460 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 case 'o':
6462 goto_byte(cap->count0);
6463 break;
6464#endif
6465
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006466 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006468 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 {
6470 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006471 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 break;
6473 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006474
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if (!checkclearopq(oap))
6476 do_exmode(TRUE);
6477 break;
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 case ',':
6480 nv_pcmark(cap);
6481 break;
6482
6483 case ';':
6484 cap->count1 = -cap->count1;
6485 nv_pcmark(cap);
6486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006488 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006489 if (!checkclearop(oap))
6490 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006492 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006493 if (!checkclearop(oap))
6494 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006495 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006496
Bram Moolenaar62a23252020-08-09 14:04:42 +02006497 case TAB:
6498 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6499 clearopbeep(oap);
6500 break;
6501
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006502 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006503 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006504 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006505 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006506 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006507 break;
6508
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 default:
6510 clearopbeep(oap);
6511 break;
6512 }
6513}
6514
6515/*
6516 * Handle "o" and "O" commands.
6517 */
6518 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006519n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006521#ifdef FEAT_CONCEAL
6522 linenr_T oldline = curwin->w_cursor.lnum;
6523#endif
6524
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (!checkclearopq(cap->oap))
6526 {
6527#ifdef FEAT_FOLDING
6528 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006529 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 (void)hasFolding(curwin->w_cursor.lnum,
6531 &curwin->w_cursor.lnum, NULL);
6532 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006533 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 (void)hasFolding(curwin->w_cursor.lnum,
6535 NULL, &curwin->w_cursor.lnum);
6536#endif
6537 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6538 (cap->cmdchar == 'O' ? 1 : 0)),
6539 (linenr_T)(curwin->w_cursor.lnum +
6540 (cap->cmdchar == 'o' ? 1 : 0))
6541 ) == OK
6542 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006543 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006544 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006546#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006547 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006548 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006549#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006550#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006551 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006552 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006553 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006554#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006555 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006556 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6557 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6559 }
6560 }
6561}
6562
6563/*
6564 * "." command: redo last change.
6565 */
6566 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006567nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568{
6569 if (!checkclearopq(cap->oap))
6570 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006571 // If "restart_edit" is TRUE, the last but one command is repeated
6572 // instead of the last command (inserting text). This is used for
6573 // CTRL-O <.> in insert mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6575 clearopbeep(cap->oap);
6576 }
6577}
6578
6579/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006580 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 */
6582 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006583nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006585 if (VIsual_select && VIsual_active)
6586 {
6587 int reg;
6588 // Get register name
6589 ++no_mapping;
6590 ++allow_keys;
6591 reg = plain_vgetc();
6592 LANGMAP_ADJUST(reg, TRUE);
6593 --no_mapping;
6594 --allow_keys;
6595
6596 if (reg == '"')
6597 // the unnamed register is 0
6598 reg = 0;
6599
6600 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
6601 return;
6602 }
6603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 if (!checkclearopq(cap->oap))
6605 {
6606 u_redo((int)cap->count1);
6607 curwin->w_set_curswant = TRUE;
6608 }
6609}
6610
6611/*
6612 * Handle "U" command.
6613 */
6614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006615nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006617 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006618 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006620 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 cap->cmdchar = 'g';
6622 cap->nchar = 'U';
6623 nv_operator(cap);
6624 }
6625 else if (!checkclearopq(cap->oap))
6626 {
6627 u_undoline();
6628 curwin->w_set_curswant = TRUE;
6629 }
6630}
6631
6632/*
6633 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6634 * single character.
6635 */
6636 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006637nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006639 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006640 {
6641#ifdef FEAT_JOB_CHANNEL
6642 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6643 {
6644 clearopbeep(cap->oap);
6645 return;
6646 }
6647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 else
6651 nv_operator(cap);
6652}
6653
6654/*
6655 * Handle an operator command.
6656 * The actual work is done by do_pending_operator().
6657 */
6658 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006659nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
6661 int op_type;
6662
6663 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006664#ifdef FEAT_JOB_CHANNEL
6665 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6666 {
6667 clearopbeep(cap->oap);
6668 return;
6669 }
6670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006672 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 nv_lineop(cap);
6674 else if (!checkclearop(cap->oap))
6675 {
6676 cap->oap->start = curwin->w_cursor;
6677 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006678#ifdef FEAT_EVAL
6679 set_op_var(op_type);
6680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 }
6682}
6683
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006684#ifdef FEAT_EVAL
6685/*
6686 * Set v:operator to the characters for "optype".
6687 */
6688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006689set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006690{
6691 char_u opchars[3];
6692
6693 if (optype == OP_NOP)
6694 set_vim_var_string(VV_OP, NULL, 0);
6695 else
6696 {
6697 opchars[0] = get_op_char(optype);
6698 opchars[1] = get_extra_op_char(optype);
6699 opchars[2] = NUL;
6700 set_vim_var_string(VV_OP, opchars, -1);
6701 }
6702}
6703#endif
6704
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705/*
6706 * Handle linewise operator "dd", "yy", etc.
6707 *
6708 * "_" is is a strange motion command that helps make operators more logical.
6709 * It is actually implemented, but not documented in the real Vi. This motion
6710 * command actually refers to "the current line". Commands like "dd" and "yy"
6711 * are really an alternate form of "d_" and "y_". It does accept a count, so
6712 * "d3_" works to delete 3 lines.
6713 */
6714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006715nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717 cap->oap->motion_type = MLINE;
6718 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6719 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006720 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006721 && cap->oap->motion_force != 'v'
6722 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 || cap->oap->op_type == OP_LSHIFT
6724 || cap->oap->op_type == OP_RSHIFT)
6725 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006726 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 beginline(BL_WHITE | BL_FIX);
6728}
6729
6730/*
6731 * <Home> command.
6732 */
6733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006734nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006736 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006737 if (mod_mask & MOD_MASK_CTRL)
6738 nv_goto(cap);
6739 else
6740 {
6741 cap->count0 = 1;
6742 nv_pipe(cap);
6743 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006744 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6745 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746}
6747
6748/*
6749 * "|" command.
6750 */
6751 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006752nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
6754 cap->oap->motion_type = MCHAR;
6755 cap->oap->inclusive = FALSE;
6756 beginline(0);
6757 if (cap->count0 > 0)
6758 {
6759 coladvance((colnr_T)(cap->count0 - 1));
6760 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6761 }
6762 else
6763 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006764 // keep curswant at the column where we wanted to go, not where
6765 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 curwin->w_set_curswant = FALSE;
6767}
6768
6769/*
6770 * Handle back-word command "b" and "B".
6771 * cap->arg is 1 for "B"
6772 */
6773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006774nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775{
6776 cap->oap->motion_type = MCHAR;
6777 cap->oap->inclusive = FALSE;
6778 curwin->w_set_curswant = TRUE;
6779 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6780 clearopbeep(cap->oap);
6781#ifdef FEAT_FOLDING
6782 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6783 foldOpenCursor();
6784#endif
6785}
6786
6787/*
6788 * Handle word motion commands "e", "E", "w" and "W".
6789 * cap->arg is TRUE for "E" and "W".
6790 */
6791 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006792nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 int n;
6795 int word_end;
6796 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006797 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006799 // Set inclusive for the "E" and "e" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6801 word_end = TRUE;
6802 else
6803 word_end = FALSE;
6804 cap->oap->inclusive = word_end;
6805
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006806 // "cw" and "cW" are a special case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 if (!word_end && cap->oap->op_type == OP_CHANGE)
6808 {
6809 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006810 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006812 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006814 // Reproduce a funny Vi behaviour: "cw" on a blank only
6815 // changes one character, not all blanks until the start of
6816 // the next word. Only do this when the 'w' flag is included
6817 // in 'cpoptions'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6819 {
6820 cap->oap->inclusive = TRUE;
6821 cap->oap->motion_type = MCHAR;
6822 return;
6823 }
6824 }
6825 else
6826 {
Yegappan Lakshmanana827bf32022-01-26 12:14:15 +00006827 // This is a little strange. To match what the real Vi does,
6828 // we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6829 // that we are not on a space or a TAB. This seems impolite
6830 // at first, but it's really more what we mean when we say
6831 // 'cw'.
6832 // Another strangeness: When standing on the end of a word
6833 // "ce" will change until the end of the next word, but "cw"
6834 // will change only one character! This is done by setting
6835 // flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 cap->oap->inclusive = TRUE;
6837 word_end = TRUE;
6838 flag = TRUE;
6839 }
6840 }
6841 }
6842
6843 cap->oap->motion_type = MCHAR;
6844 curwin->w_set_curswant = TRUE;
6845 if (word_end)
6846 n = end_word(cap->count1, cap->arg, flag, FALSE);
6847 else
6848 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6849
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006850 // Don't leave the cursor on the NUL past the end of line. Unless we
6851 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006852 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006853 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
6855 if (n == FAIL && cap->oap->op_type == OP_NOP)
6856 clearopbeep(cap->oap);
6857 else
6858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860#ifdef FEAT_FOLDING
6861 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6862 foldOpenCursor();
6863#endif
6864 }
6865}
6866
6867/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006868 * Used after a movement command: If the cursor ends up on the NUL after the
6869 * end of the line, may move it back to the last character and make the motion
6870 * inclusive.
6871 */
6872 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006873adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006874{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006875 // The cursor cannot remain on the NUL when:
6876 // - the column is > 0
6877 // - not in Visual mode or 'selection' is "o"
6878 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006879 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006880 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006881 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006882 {
6883 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006884 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006885 if (has_mbyte)
6886 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006887 oap->inclusive = TRUE;
6888 }
6889}
6890
6891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 * "0" and "^" commands.
6893 * cap->arg is the argument for beginline().
6894 */
6895 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006896nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 cap->oap->motion_type = MCHAR;
6899 cap->oap->inclusive = FALSE;
6900 beginline(cap->arg);
6901#ifdef FEAT_FOLDING
6902 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6903 foldOpenCursor();
6904#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006905 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6906 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907}
6908
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909/*
6910 * In exclusive Visual mode, may include the last character.
6911 */
6912 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006913adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006916 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (has_mbyte)
6919 inc_cursor();
6920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 ++curwin->w_cursor.col;
6922 cap->oap->inclusive = FALSE;
6923 }
6924}
6925
6926/*
6927 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6928 * Should check VIsual_mode before calling this.
6929 * Returns TRUE when backed up to the previous line.
6930 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006931 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006932unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
6934 pos_T *pp;
6935
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006936 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006938 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 pp = &curwin->w_cursor;
6940 else
6941 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 if (pp->coladd > 0)
6943 --pp->coladd;
6944 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 if (pp->col > 0)
6946 {
6947 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006948 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 }
6950 else if (pp->lnum > 1)
6951 {
6952 --pp->lnum;
6953 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6954 return TRUE;
6955 }
6956 }
6957 return FALSE;
6958}
6959
6960/*
6961 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6962 */
6963 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006964nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965{
6966 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006969 VIsual_select_reg = 0;
6970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 else if (VIsual_reselect)
6972 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006973 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 cap->arg = TRUE;
6975 nv_g_cmd(cap);
6976 }
6977}
6978
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979
6980/*
6981 * "G", "gg", CTRL-END, CTRL-HOME.
6982 * cap->arg is TRUE for "G".
6983 */
6984 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006985nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 linenr_T lnum;
6988
6989 if (cap->arg)
6990 lnum = curbuf->b_ml.ml_line_count;
6991 else
6992 lnum = 1L;
6993 cap->oap->motion_type = MLINE;
6994 setpcmark();
6995
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006996 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 if (cap->count0 != 0)
6998 lnum = cap->count0;
6999 if (lnum < 1L)
7000 lnum = 1L;
7001 else if (lnum > curbuf->b_ml.ml_line_count)
7002 lnum = curbuf->b_ml.ml_line_count;
7003 curwin->w_cursor.lnum = lnum;
7004 beginline(BL_SOL | BL_FIX);
7005#ifdef FEAT_FOLDING
7006 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
7007 foldOpenCursor();
7008#endif
7009}
7010
7011/*
7012 * CTRL-\ in Normal mode.
7013 */
7014 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007015nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
7018 {
7019 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00007020 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007021 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 restart_edit = 0;
7023#ifdef FEAT_CMDWIN
7024 if (cmdwin_type != 0)
7025 cmdwin_result = Ctrl_C;
7026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 if (VIsual_active)
7028 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007029 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 redraw_curbuf_later(INVERTED);
7031 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007032 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 if (cap->nchar == Ctrl_G && p_im)
7034 restart_edit = 'a';
7035 }
7036 else
7037 clearopbeep(cap->oap);
7038}
7039
7040/*
7041 * ESC in Normal mode: beep, but don't flush buffers.
7042 * Don't even beep if we are canceling a command.
7043 */
7044 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007045nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
7047 int no_reason;
7048
7049 no_reason = (cap->oap->op_type == OP_NOP
7050 && cap->opcount == 0
7051 && cap->count0 == 0
7052 && cap->oap->regname == 0
7053 && !p_im);
7054
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007055 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
7057 if (restart_edit == 0
7058#ifdef FEAT_CMDWIN
7059 && cmdwin_type == 0
7060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007063 {
7064 if (anyBufIsChanged())
7065 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
7066 else
7067 msg(_("Type :qa and press <Enter> to exit Vim"));
7068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007070 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
7071 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 if (!p_im)
7073 restart_edit = 0;
7074#ifdef FEAT_CMDWIN
7075 if (cmdwin_type != 0)
7076 {
7077 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007078 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 return;
7080 }
7081#endif
7082 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01007083#ifdef FEAT_CMDWIN
7084 else if (cmdwin_type != 0 && ex_normal_busy)
7085 {
7086 // When :normal runs out of characters while in the command line window
7087 // vgetorpeek() will return ESC. Exit the cmdline window to break the
7088 // loop.
7089 cmdwin_result = K_IGNORE;
7090 return;
7091 }
7092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 if (VIsual_active)
7095 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007096 end_visual_mode(); // stop Visual
7097 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 curwin->w_set_curswant = TRUE;
7099 redraw_curbuf_later(INVERTED);
7100 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007101 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007102 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 clearop(cap->oap);
7104
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007105 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7106 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007107 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 restart_edit = 'a';
7109}
7110
7111/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007112 * Move the cursor for the "A" command.
7113 */
7114 void
7115set_cursor_for_append_to_line(void)
7116{
7117 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007118 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007119 {
7120 int save_State = State;
7121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007122 // Pretend Insert mode here to allow the cursor on the
7123 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007124 State = INSERT;
7125 coladvance((colnr_T)MAXCOL);
7126 State = save_State;
7127 }
7128 else
7129 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7130}
7131
7132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007134 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 */
7136 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007137nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007139 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7141 cap->cmdchar = 'i';
7142
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007143 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007145 {
7146#ifdef FEAT_TERMINAL
7147 if (term_in_normal_mode())
7148 {
7149 end_visual_mode();
7150 clearop(cap->oap);
7151 term_enter_job_mode();
7152 return;
7153 }
7154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007158 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007159 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7160 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {
7162#ifdef FEAT_TEXTOBJ
7163 nv_object(cap);
7164#else
7165 clearopbeep(cap->oap);
7166#endif
7167 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007168#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007169 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007170 {
7171 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007172 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007173 return;
7174 }
7175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 else if (!curbuf->b_p_ma && !p_im)
7177 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007178 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007179 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007181 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007182 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007183 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007185 else if (cap->cmdchar == K_PS && VIsual_active)
7186 {
7187 pos_T old_pos = curwin->w_cursor;
7188 pos_T old_visual = VIsual;
7189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007190 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007191 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7192 {
7193 shift_delete_registers();
7194 cap->oap->regname = '1';
7195 }
7196 else
7197 cap->oap->regname = '-';
7198 cap->cmdchar = 'd';
7199 cap->nchar = NUL;
7200 nv_operator(cap);
7201 do_pending_operator(cap, 0, FALSE);
7202 cap->cmdchar = K_PS;
7203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007204 // When the last char in the line was deleted then append. Detect this
7205 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007206 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7207 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007208 inc_cursor();
7209
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007210 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007211 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 else if (!checkclearopq(cap->oap))
7214 {
7215 switch (cap->cmdchar)
7216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007217 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007218 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 break;
7220
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007221 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007222 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7223 beginline(BL_WHITE);
7224 else
7225 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 break;
7227
Bram Moolenaara1891842017-02-04 21:34:31 +01007228 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007229 // Bracketed paste works like "a"ppend, unless the cursor is in
7230 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007231 if (curwin->w_cursor.col == 0)
7232 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007233 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007234
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007235 case 'a': // "a"ppend is like "i"nsert on the next character.
7236 // increment coladd when in virtual space, increment the
7237 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 if (virtual_active()
7239 && (curwin->w_cursor.coladd > 0
7240 || *ml_get_cursor() == NUL
7241 || *ml_get_cursor() == TAB))
7242 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007243 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 inc_cursor();
7245 break;
7246 }
7247
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7249 {
7250 int save_State = State;
7251
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007252 // Pretend Insert mode here to allow the cursor on the
7253 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 State = INSERT;
7255 coladvance(getviscol());
7256 State = save_State;
7257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7260 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007261 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007262 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007263 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264}
7265
7266/*
7267 * Invoke edit() and take care of "restart_edit" and the return value.
7268 */
7269 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007270invoke_edit(
7271 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007272 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007273 int cmd,
7274 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276 int restart_edit_save = 0;
7277
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007278 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7279 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7280 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 if (repl || !stuff_empty())
7282 restart_edit_save = restart_edit;
7283 else
7284 restart_edit_save = 0;
7285
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007286 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 restart_edit = 0;
7288
7289 if (edit(cmd, startln, cap->count1))
7290 cap->retval |= CA_COMMAND_BUSY;
7291
7292 if (restart_edit == 0)
7293 restart_edit = restart_edit_save;
7294}
7295
7296#ifdef FEAT_TEXTOBJ
7297/*
7298 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7299 */
7300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007301nv_object(
7302 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 int flag;
7305 int include;
7306 char_u *mps_save;
7307
7308 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007309 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007311 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007313 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 mps_save = curbuf->b_p_mps;
7315 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7316
7317 switch (cap->nchar)
7318 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007319 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 flag = current_word(cap->oap, cap->count1, include, FALSE);
7321 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007322 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 flag = current_word(cap->oap, cap->count1, include, TRUE);
7324 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007325 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 case '(':
7327 case ')':
7328 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7329 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007330 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 case '{':
7332 case '}':
7333 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7334 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007335 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 case ']':
7337 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7338 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007339 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 case '>':
7341 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7342 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007343 case 't': // "at" = a tag block (xml and html)
7344 // Do not adjust oap->end in do_pending_operator()
7345 // otherwise there are different results for 'dit'
7346 // (note leading whitespace in last line):
7347 // 1) <b> 2) <b>
7348 // foobar foobar
7349 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007350 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007351 flag = current_tagblock(cap->oap, cap->count1, include);
7352 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007353 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 flag = current_par(cap->oap, cap->count1, include, 'p');
7355 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007356 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 flag = current_sent(cap->oap, cap->count1, include);
7358 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007359 case '"': // "a"" = a double quoted string
7360 case '\'': // "a'" = a single quoted string
7361 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007362 flag = current_quote(cap->oap, cap->count1, include,
7363 cap->nchar);
7364 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007365#if 0 // TODO
7366 case 'S': // "aS" = a section
7367 case 'f': // "af" = a filename
7368 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#endif
7370 default:
7371 flag = FAIL;
7372 break;
7373 }
7374
7375 curbuf->b_p_mps = mps_save;
7376 if (flag == FAIL)
7377 clearopbeep(cap->oap);
7378 adjust_cursor_col();
7379 curwin->w_set_curswant = TRUE;
7380}
7381#endif
7382
7383/*
7384 * "q" command: Start/stop recording.
7385 * "q:", "q/", "q?": edit command-line in command-line window.
7386 */
7387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007388nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389{
7390 if (cap->oap->op_type == OP_FORMAT)
7391 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007392 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 cap->cmdchar = 'g';
7394 cap->nchar = 'q';
7395 nv_operator(cap);
7396 }
7397 else if (!checkclearop(cap->oap))
7398 {
7399#ifdef FEAT_CMDWIN
7400 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7401 {
7402 stuffcharReadbuff(cap->nchar);
7403 stuffcharReadbuff(K_CMDWIN);
7404 }
7405 else
7406#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007407 // (stop) recording into a named register, unless executing a
7408 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007409 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 clearopbeep(cap->oap);
7411 }
7412}
7413
7414/*
7415 * Handle the "@r" command.
7416 */
7417 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007418nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419{
7420 if (checkclearop(cap->oap))
7421 return;
7422#ifdef FEAT_EVAL
7423 if (cap->nchar == '=')
7424 {
7425 if (get_expr_register() == NUL)
7426 return;
7427 }
7428#endif
7429 while (cap->count1-- && !got_int)
7430 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007431 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
7433 clearopbeep(cap->oap);
7434 break;
7435 }
7436 line_breakcheck();
7437 }
7438}
7439
7440/*
7441 * Handle the CTRL-U and CTRL-D commands.
7442 */
7443 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007444nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445{
7446 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7447 || (cap->cmdchar == Ctrl_D
7448 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7449 clearopbeep(cap->oap);
7450 else if (!checkclearop(cap->oap))
7451 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7452}
7453
7454/*
7455 * Handle "J" or "gJ" command.
7456 */
7457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007458nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007460 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007462 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 {
7464 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007465 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7467 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007469 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007470 if (cap->count0 <= 2)
7471 {
7472 clearopbeep(cap->oap);
7473 return;
7474 }
7475 cap->count0 = curbuf->b_ml.ml_line_count
7476 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007478
7479 prep_redo(cap->oap->regname, cap->count0,
7480 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7481 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 }
7483}
7484
7485/*
7486 * "P", "gP", "p" and "gp" commands.
7487 */
7488 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007489nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007491 nv_put_opt(cap, FALSE);
7492}
7493
7494/*
7495 * "P", "gP", "p" and "gp" commands.
7496 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7497 */
7498 static void
7499nv_put_opt(cmdarg_T *cap, int fix_indent)
7500{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 int regname = 0;
7502 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007503 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007504 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 int dir;
7506 int flags = 0;
7507
7508 if (cap->oap->op_type != OP_NOP)
7509 {
7510#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007511 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7513 {
7514 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007515 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 else
7518#endif
7519 clearopbeep(cap->oap);
7520 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007521#ifdef FEAT_JOB_CHANNEL
7522 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7523 {
7524 clearopbeep(cap->oap);
7525 }
7526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 else
7528 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007529 if (fix_indent)
7530 {
7531 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7532 ? FORWARD : BACKWARD;
7533 flags |= PUT_FIXINDENT;
7534 }
7535 else
7536 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007537 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7538 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 prep_redo_cmd(cap);
7540 if (cap->cmdchar == 'g')
7541 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007542 else if (cap->cmdchar == 'z')
7543 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 if (VIsual_active)
7546 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007547 // Putting in Visual mode: The put text replaces the selected
7548 // text. First delete the selected text, then put the new text.
7549 // Need to save and restore the registers that the delete
7550 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007551 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007553#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007555#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007556 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007557 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007558#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561
7562 )
7563 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007564 // The delete is going to overwrite the register we want to
7565 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 reg1 = get_register(regname, TRUE);
7567 }
7568
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007569 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 cap->cmdchar = 'd';
7571 cap->nchar = NUL;
7572 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007573 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 nv_operator(cap);
7575 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007576 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007577 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007579 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 cap->oap->regname = regname;
7581
7582 if (reg1 != NULL)
7583 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007584 // Delete probably changed the register we want to put, save
7585 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 reg2 = get_register(regname, FALSE);
7587 put_register(regname, reg1);
7588 }
7589
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007590 // When deleted a linewise Visual area, put the register as
7591 // lines to avoid it joined with the next line. When deletion was
7592 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 if (VIsual_mode == 'V')
7594 flags |= PUT_LINE;
7595 else if (VIsual_mode == 'v')
7596 flags |= PUT_LINE_SPLIT;
7597 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7598 flags |= PUT_LINE_FORWARD;
7599 dir = BACKWARD;
7600 if ((VIsual_mode != 'V'
7601 && curwin->w_cursor.col < curbuf->b_op_start.col)
7602 || (VIsual_mode == 'V'
7603 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007604 // cursor is at the end of the line or end of file, put
7605 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007607 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007608 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007610 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007612 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 if (reg2 != NULL)
7614 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007615
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007616 // What to reselect with "gv"? Selecting the just put text seems to
7617 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007618 if (was_visual)
7619 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007620 curbuf->b_visual.vi_start = curbuf->b_op_start;
7621 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007622 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007623 if (*p_sel == 'e')
7624 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007625 }
7626
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007627 // When all lines were selected and deleted do_put() leaves an empty
7628 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007629 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007630 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007631 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007632 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007633
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007634 // If the cursor was in that line, move it to the end of the last
7635 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007636 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7637 {
7638 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7639 coladvance((colnr_T)MAXCOL);
7640 }
7641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 auto_format(FALSE, TRUE);
7643 }
7644}
7645
7646/*
7647 * "o" and "O" commands.
7648 */
7649 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007650nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651{
7652#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007653 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7655 {
7656 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007657 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659 else
7660#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007661 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007663#ifdef FEAT_JOB_CHANNEL
7664 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007665 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 n_opencmd(cap);
7669}
7670
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671#ifdef FEAT_NETBEANS_INTG
7672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007673nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675 netbeans_keycommand(cap->nchar);
7676}
7677#endif
7678
7679#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007681nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007683 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684}
7685#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007686
Bram Moolenaar3918c952005-03-15 22:34:55 +00007687/*
7688 * Trigger CursorHold event.
7689 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7690 * input buffer. "did_cursorhold" is set to avoid retriggering.
7691 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007693nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007694{
7695 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7696 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007697 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007698}