blob: 8dc036c47db38599da06dc8137c5e4af3914a878 [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/*
482 * Execute a command in Normal mode.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100485normal_cmd(
486 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100489 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 int old_col = curwin->w_curswant;
493#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 static int old_mapped_len = 0;
499 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000500#ifdef FEAT_EVAL
501 int set_prevcount = FALSE;
502#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200503 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaara80faa82020-04-12 19:37:17 +0200505 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 // Use a count remembered from before entering an operator. After typing
509 // "3d" we return from normal_cmd() and come back here, the "3" is
510 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 ca.opcount = opcount;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 /*
514 * If there is an operator pending, then the command we take this time
515 * will terminate it. Finish_op tells us to finish the operation before
516 * returning this time (unless the operation was cancelled).
517 */
518#ifdef CURSOR_SHAPE
519 c = finish_op;
520#endif
521 finish_op = (oap->op_type != OP_NOP);
522#ifdef CURSOR_SHAPE
523 if (finish_op != c)
524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100525 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# ifdef FEAT_MOUSESHAPE
527 update_mouseshape(-1);
528# endif
529 }
530#endif
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100531 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100533 // When not finishing an operator and no register name typed, reset the
534 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000538#ifdef FEAT_EVAL
539 set_prevcount = TRUE;
540#endif
541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100543 // Restore counts from before receiving K_CURSORHOLD. This means after
544 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
545 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000546 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
547 {
548 ca.opcount = oap->prev_opcount;
549 ca.count0 = oap->prev_count0;
550 oap->prev_opcount = 0;
551 oap->prev_count0 = 0;
552 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 State = NORMAL_BUSY;
557#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100558 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#endif
560
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100561#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100562 // Set v:count here, when called from main() and not a stuffed
563 // command, so that v:count can be used in an expression mapping
564 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100565 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100566 set_vcount_ca(&ca, &set_prevcount);
567#endif
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 /*
570 * Get the command character from the user.
571 */
572 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100573 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
575 /*
576 * If a mapping was started in Visual or Select mode, remember the length
577 * of the mapping. This is used below to not return to Insert mode for as
578 * long as the mapping is being executed.
579 */
580 if (restart_edit == 0)
581 old_mapped_len = 0;
582 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000583 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 old_mapped_len = typebuf_maplen();
585
586 if (c == NUL)
587 c = K_ZERO;
588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 /*
590 * In Select mode, typed text replaces the selection.
591 */
592 if (VIsual_active
593 && VIsual_select
594 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
595 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000596 int len;
597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100598 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
599 // 'insertmode' is set) fake a "d"elete command, Insert mode will
600 // restart automatically.
601 // Insert the typed character in the typeahead buffer, so that it can
602 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000603 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
604
605 // When recording the character will be recorded again, remove the
606 // previously recording.
607 ungetchars(len);
608
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000609 if (restart_edit != 0)
610 c = 'd';
611 else
612 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100613 msg_nowait = TRUE; // don't delay going to insert mode
614 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000617 // If the window was made so small that nothing shows, make it at least one
618 // line and one column when typing a command.
619 if (KeyTyped && !KeyStuffed)
620 win_ensure_size();
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#ifdef FEAT_CMDL_INFO
623 need_flushbuf = add_to_showcmd(c);
624#endif
625
626getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
629 /*
630 * Handle a count before a command and compute ca.count0.
631 * Note that '0' is a command and not the start of a count, but it's
632 * part of a count after other digits.
633 */
634 while ( (c >= '1' && c <= '9')
635 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
636 {
637 if (c == K_DEL || c == K_KDEL)
638 {
639 ca.count0 /= 10;
640#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100641 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#endif
643 }
Bram Moolenaar9b0e82f2021-11-24 13:40:29 +0000644 else if (ca.count0 > 99999999L)
Bram Moolenaar03725c52021-11-24 12:17:53 +0000645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 ca.count0 = 999999999L;
Bram Moolenaar03725c52021-11-24 12:17:53 +0000647 }
648 else
649 {
650 ca.count0 = ca.count0 * 10 + (c - '0');
651 }
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000652#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100653 // Set v:count here, when called from main() and not a stuffed
654 // command, so that v:count can be used in an expression mapping
655 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100656 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100657 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (ctrl_w)
660 {
661 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100662 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100664 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000665 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 --no_zero_mapping;
668 if (ctrl_w)
669 {
670 --no_mapping;
671 --allow_keys;
672 }
673#ifdef FEAT_CMDL_INFO
674 need_flushbuf |= add_to_showcmd(c);
675#endif
676 }
677
678 /*
679 * If we got CTRL-W there may be a/another count
680 */
681 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
682 {
683 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 ca.count0 = 0;
686 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100687 ++allow_keys; // no mapping for nchar, but keys
688 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 --no_mapping;
691 --allow_keys;
692#ifdef FEAT_CMDL_INFO
693 need_flushbuf |= add_to_showcmd(c);
694#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100695 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697 }
698
Bram Moolenaara983fe92008-07-31 20:04:27 +0000699 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100701 // Save the count values so that ca.opcount and ca.count0 are exactly
702 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000703 oap->prev_opcount = ca.opcount;
704 oap->prev_count0 = ca.count0;
705 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100706 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000707 {
708 /*
709 * If we're in the middle of an operator (including after entering a
710 * yank buffer with '"') AND we had a count before the operator, then
711 * that count overrides the current value of ca.count0.
712 * What this means effectively, is that commands like "3dw" get turned
713 * into "d3w" which makes things fall into place pretty neatly.
714 * If you give a count before AND after the operator, they are
715 * multiplied.
716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (ca.count0)
Bram Moolenaar03725c52021-11-24 12:17:53 +0000718 {
719 if (ca.opcount >= 999999999L / ca.count0)
720 ca.count0 = 999999999L;
721 else
722 ca.count0 *= ca.opcount;
723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 else
725 ca.count0 = ca.opcount;
726 }
727
728 /*
729 * Always remember the count. It will be set to zero (on the next call,
730 * above) when there is no pending operator.
731 * When called from main(), save the count for use by the "count" built-in
732 * variable.
733 */
734 ca.opcount = ca.count0;
735 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
736
737#ifdef FEAT_EVAL
738 /*
739 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100740 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100742 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000743 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#endif
745
746 /*
747 * Find the command character in the table of commands.
748 * For CTRL-W we already got nchar when looking for a count.
749 */
750 if (ctrl_w)
751 {
752 ca.nchar = c;
753 ca.cmdchar = Ctrl_W;
754 }
755 else
756 ca.cmdchar = c;
757 idx = find_command(ca.cmdchar);
758 if (idx < 0)
759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100760 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 clearopbeep(oap);
762 goto normal_end;
763 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000764
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000765 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100767 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000769 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 goto normal_end;
771 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000772 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
773 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 /*
776 * In Visual/Select mode, a few keys are handled in a special way.
777 */
778 if (VIsual_active)
779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100780 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (km_stopsel
782 && (nv_cmds[idx].cmd_flags & NV_STS)
783 && !(mod_mask & MOD_MASK_SHIFT))
784 {
785 end_visual_mode();
786 redraw_curbuf_later(INVERTED);
787 }
788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 if (km_startsel)
791 {
792 if (nv_cmds[idx].cmd_flags & NV_SS)
793 {
794 unshift_special(&ca);
795 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000796 if (idx < 0)
797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100798 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000799 clearopbeep(oap);
800 goto normal_end;
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
803 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
804 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809#ifdef FEAT_RIGHTLEFT
810 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
811 && (nv_cmds[idx].cmd_flags & NV_RL))
812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100813 // Invert horizontal movements and operations. Only when typed by the
814 // user directly, not when the result of a mapping or "x" translated
815 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 switch (ca.cmdchar)
817 {
818 case 'l': ca.cmdchar = 'h'; break;
819 case K_RIGHT: ca.cmdchar = K_LEFT; break;
820 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
821 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
822 case 'h': ca.cmdchar = 'l'; break;
823 case K_LEFT: ca.cmdchar = K_RIGHT; break;
824 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
825 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
826 case '>': ca.cmdchar = '<'; break;
827 case '<': ca.cmdchar = '>'; break;
828 }
829 idx = find_command(ca.cmdchar);
830 }
831#endif
832
833 /*
834 * Get an additional character if we need one.
835 */
836 if ((nv_cmds[idx].cmd_flags & NV_NCH)
837 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
838 && oap->op_type == OP_NOP)
839 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
840 || (ca.cmdchar == 'q'
841 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200842 && reg_recording == 0
843 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100845 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
847 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100848 int repl = FALSE; // get character for replace mode
849 int lit = FALSE; // get extra character literally
850 int langmap_active = FALSE; // using :lmap mappings
851 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100852#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
856 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 ++allow_keys; // no mapping for nchar, but allow key codes
858 // Don't generate a CursorHold event here, most commands can't handle
859 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000860 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (ca.cmdchar == 'g')
862 {
863 /*
864 * For 'g' get the next character now, so that we can check for
865 * "gr", "g'" and "g`".
866 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000867 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#ifdef FEAT_CMDL_INFO
870 need_flushbuf |= add_to_showcmd(ca.nchar);
871#endif
872 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100873 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100875 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100877 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100879 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100882 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884 else
885 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100886 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 repl = TRUE;
888 cp = &ca.nchar;
889 }
890 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
891
892 /*
893 * Get a second or third character.
894 */
895 if (cp != NULL)
896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (repl)
898 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100899 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100900#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100901 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
905 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100906 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 --no_mapping;
908 --allow_keys;
909 if (repl)
910 State = LREPLACE;
911 else
912 State = LANGMAP;
913 langmap_active = TRUE;
914 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100915#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
919 im_set_active(TRUE);
920#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200921 if ((State & INSERT) && !p_ek)
922 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200923#ifdef FEAT_JOB_CHANNEL
924 ch_log_output = TRUE;
925#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200926 // Disable bracketed paste and modifyOtherKeys here, we won't
927 // recognize the escape sequences with 'esckeys' off.
928 out_str(T_BD);
929 out_str(T_CTE);
930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000932 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
Bram Moolenaarca774f62020-08-30 20:46:38 +0200934 if ((State & INSERT) && !p_ek)
935 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200936#ifdef FEAT_JOB_CHANNEL
937 ch_log_output = TRUE;
938#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200939 // Re-enable bracketed paste mode and modifyOtherKeys
940 out_str(T_BE);
941 out_str(T_CTI);
942 }
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (langmap_active)
945 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100946 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 ++no_mapping;
948 ++allow_keys;
949 State = NORMAL_BUSY;
950 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100951#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 if (lang)
953 {
954 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
955 im_save_status(&curbuf->b_p_iminsert);
956 im_set_active(FALSE);
957 }
958 p_smd = save_smd;
959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#ifdef FEAT_CMDL_INFO
962 need_flushbuf |= add_to_showcmd(*cp);
963#endif
964
965 if (!lit)
966 {
967#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100968 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (*cp == Ctrl_K
970 && ((nv_cmds[idx].cmd_flags & NV_LANG)
971 || cp == &ca.extra_char)
972 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
973 {
974 c = get_digraph(FALSE);
975 if (c > 0)
976 {
977 *cp = c;
978# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100979 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 del_from_showcmd(3);
981 need_flushbuf |= add_to_showcmd(*cp);
982# endif
983 }
984 }
985#endif
986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100987 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100990 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (p_hkmap && lang && KeyTyped)
992 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994 }
995
996 /*
997 * When the next character is CTRL-\ a following CTRL-N means the
998 * command is aborted and we go to Normal mode.
999 */
1000 if (cp == &ca.extra_char
1001 && ca.nchar == Ctrl_BSL
1002 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1003 {
1004 ca.cmdchar = Ctrl_BSL;
1005 ca.nchar = ca.extra_char;
1006 idx = find_command(ca.cmdchar);
1007 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001008 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001009 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 else if (*cp == Ctrl_BSL)
1011 {
1012 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 // There is a busy wait here when typing "f<C-\>" and then
1015 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 while ((c = vpeekc()) <= 0 && towait > 0L)
1017 {
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01001018 do_sleep(towait > 50L ? 50L : towait, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 towait -= 50L;
1020 }
1021 if (c > 0)
1022 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001023 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (c != Ctrl_N && c != Ctrl_G)
1025 vungetc(c);
1026 else
1027 {
1028 ca.cmdchar = Ctrl_BSL;
1029 ca.nchar = c;
1030 idx = find_command(ca.cmdchar);
1031 }
1032 }
1033 }
1034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001035 // When getting a text character and the next character is a
1036 // multi-byte character, it could be a composing character.
1037 // However, don't wait for it to arrive. Also, do enable mapping,
1038 // because if it's put back with vungetc() it's too late to apply
1039 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001040 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 while (enc_utf8 && lang && (c = vpeekc()) > 0
1042 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1043 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001044 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (!utf_iscomposing(c))
1046 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001047 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 break;
1049 }
1050 else if (ca.ncharC1 == 0)
1051 ca.ncharC1 = c;
1052 else
1053 ca.ncharC2 = c;
1054 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001055 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057 --no_mapping;
1058 --allow_keys;
1059 }
1060
1061#ifdef FEAT_CMDL_INFO
1062 /*
1063 * Flush the showcmd characters onto the screen so we can see them while
1064 * the command is being executed. Only do this when the shown command was
1065 * actually displayed, otherwise this will slow down a lot when executing
1066 * mappings.
1067 */
1068 if (need_flushbuf)
1069 out_flush();
1070#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001071 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001072 {
1073 if (ex_normal_busy)
1074 did_cursorhold = save_did_cursorhold;
1075 else
1076 did_cursorhold = FALSE;
1077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
1079 State = NORMAL;
1080
1081 if (ca.nchar == ESC)
1082 {
1083 clearop(oap);
1084 if (restart_edit == 0 && goto_im())
1085 restart_edit = 'a';
1086 goto normal_end;
1087 }
1088
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001089 if (ca.cmdchar != K_IGNORE)
1090 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001091 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001092 msg_col = 0;
1093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001095 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001097 // When 'keymodel' contains "startsel" some keys start Select/Visual
1098 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (!VIsual_active && km_startsel)
1100 {
1101 if (nv_cmds[idx].cmd_flags & NV_SS)
1102 {
1103 start_selection();
1104 unshift_special(&ca);
1105 idx = find_command(ca.cmdchar);
1106 }
1107 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1108 && (mod_mask & MOD_MASK_SHIFT))
1109 {
1110 start_selection();
1111 mod_mask &= ~MOD_MASK_SHIFT;
1112 }
1113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
1115 /*
1116 * Execute the command!
1117 * Call the command function found in the commands table.
1118 */
1119 ca.arg = nv_cmds[idx].cmd_arg;
1120 (nv_cmds[idx].cmd_func)(&ca);
1121
1122 /*
1123 * If we didn't start or finish an operator, reset oap->regname, unless we
1124 * need it later.
1125 */
1126 if (!finish_op
1127 && !oap->op_type
1128 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1129 {
1130 clearop(oap);
1131#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001132 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133#endif
1134 }
1135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001136 // Get the length of mapped chars again after typing a count, second
1137 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001138 if (old_mapped_len > 0)
1139 old_mapped_len = typebuf_maplen();
1140
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 /*
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001142 * If an operation is pending, handle it. But not for K_IGNORE or
1143 * K_MOUSEMOVE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 */
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001145 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001146 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
1148 /*
1149 * Wait for a moment when a message is displayed that will be overwritten
1150 * by the mode message.
1151 * In Visual mode and with "^O" in Insert mode, a short message will be
1152 * overwritten by the mode message. Wait a bit, until a key is hit.
1153 * In Visual mode, it's more important to keep the Visual area updated
1154 * than keeping a message (e.g. from a /pat search).
1155 * Only do this if the command was typed, not from a mapping.
1156 * Don't wait when emsg_silent is non-zero.
1157 * Also wait a bit after an error message, e.g. for "^O:".
1158 * Don't redraw the screen, it would remove the message.
1159 */
1160 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001161 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 || (VIsual_active
1164 && old_pos.lnum == curwin->w_cursor.lnum
1165 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 )
1167 && (clear_cmdline
1168 || redraw_cmdline)
1169 && (msg_didout || (msg_didany && msg_scroll))
1170 && !msg_nowait
1171 && KeyTyped)
1172 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && (msg_scroll
1175 || emsg_on_display)))
1176 && oap->regname == 0
1177 && !(ca.retval & CA_COMMAND_BUSY)
1178 && stuff_empty()
1179 && typebuf_typed()
1180 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001181 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 && !did_wait_return
1183 && oap->op_type == OP_NOP)
1184 {
1185 int save_State = State;
1186
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001187 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (restart_edit != 0)
1189 State = INSERT;
1190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001191 // If need to redraw, and there is a "keep_msg", redraw before the
1192 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1194 {
1195 char_u *kmsg;
1196
1197 kmsg = keep_msg;
1198 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001199 // Showmode() will clear keep_msg, but we want to use it anyway.
1200 // First update w_topline.
1201 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001203 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001205
1206 kmsg = vim_strsave(keep_msg);
1207 if (kmsg != NULL)
1208 {
1209 msg_attr((char *)kmsg, keep_msg_attr);
1210 vim_free(kmsg);
1211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001214#ifdef CURSOR_SHAPE
1215 ui_cursor_shape(); // may show different cursor shape
1216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 cursor_on();
1218 out_flush();
1219 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001220 ui_delay(1003L, TRUE); // wait at least one second
1221 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 State = save_State;
1223
1224 msg_scroll = FALSE;
1225 emsg_on_display = FALSE;
1226 }
1227
1228 /*
1229 * Finish up after executing a Normal mode command.
1230 */
1231normal_end:
1232
1233 msg_nowait = FALSE;
1234
Bram Moolenaarcc613032020-06-07 21:31:18 +02001235#ifdef FEAT_EVAL
1236 if (finish_op)
1237 reset_reg_var();
1238#endif
1239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241#ifdef CURSOR_SHAPE
1242 c = finish_op;
1243#endif
1244 finish_op = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001245 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001247 // Redraw the cursor with another shape, if we were in Operator-pending
1248 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (c || ca.cmdchar == 'r')
1250 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001251 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252# ifdef FEAT_MOUSESHAPE
1253 update_mouseshape(-1);
1254# endif
1255 }
1256#endif
1257
1258#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001259 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001260 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 clear_showcmd();
1262#endif
1263
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001264 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 vim_free(ca.searchbuf);
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (has_mbyte)
1268 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (curwin->w_p_scb && toplevel)
1271 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001272 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 do_check_scrollbind(TRUE);
1274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaar860cae12010-06-05 23:22:07 +02001276 if (curwin->w_p_crb && toplevel)
1277 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001278 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001279 do_check_cursorbind();
1280 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001281
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001282#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001284 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001285 restart_edit = 0;
1286#endif
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 /*
1289 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1290 * if still inside a mapping that started in Visual mode).
1291 * May switch from Visual to Select mode after CTRL-O command.
1292 */
1293 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1295 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && !(ca.retval & CA_COMMAND_BUSY)
1297 && stuff_empty()
1298 && oap->regname == 0)
1299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (restart_VIsual_select == 1)
1301 {
1302 VIsual_select = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001303 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 showmode();
1305 restart_VIsual_select = 0;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00001306 VIsual_select_reg = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001308 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 (void)edit(restart_edit, FALSE, 1L);
1310 }
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 if (restart_VIsual_select == 2)
1313 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001315 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 opcount = ca.opcount;
1317}
1318
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001319#ifdef FEAT_EVAL
1320/*
1321 * Set v:count and v:count1 according to "cap".
1322 * Set v:prevcount only when "set_prevcount" is TRUE.
1323 */
1324 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001325set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001326{
1327 long count = cap->count0;
1328
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001329 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001330 if (cap->opcount != 0)
1331 count = cap->opcount * (count == 0 ? 1 : count);
1332 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001333 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001334}
1335#endif
1336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001338 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 * if not.
1340 */
1341 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001342check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
1344 static int did_check = FALSE;
1345
1346 if (full_screen)
1347 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001348 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001349 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 did_check = TRUE;
1351 }
1352}
1353
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001354#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1355/*
1356 * Call yank_do_autocmd() for "regname".
1357 */
1358 static void
1359call_yank_do_autocmd(int regname)
1360{
1361 oparg_T oa;
1362 yankreg_T *reg;
1363
1364 clear_oparg(&oa);
1365 oa.regname = regname;
1366 oa.op_type = OP_YANK;
1367 oa.is_VIsual = TRUE;
1368 reg = get_register(regname, TRUE);
1369 yank_do_autocmd(&oa, reg);
1370 free_register(reg);
1371}
1372#endif
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001375 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001376 * This function or the next should ALWAYS be called to end Visual mode, except
1377 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 */
1379 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001380end_visual_mode()
1381{
1382 end_visual_mode_keep_button();
1383 reset_held_button();
1384}
1385
1386 void
1387end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389#ifdef FEAT_CLIPBOARD
1390 /*
1391 * If we are using the clipboard, then remember what was selected in case
1392 * we need to paste it somewhere while we still own the selection.
1393 * Only do this when the clipboard is already owned. Don't want to grab
1394 * the selection when hitting ESC.
1395 */
1396 if (clip_star.available && clip_star.owned)
1397 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001398
1399# if defined(FEAT_EVAL)
1400 // Emit a TextYankPost for the automatic copy of the selection into the
1401 // star and/or plus register.
1402 if (has_textyankpost())
1403 {
1404 if (clip_isautosel_star())
1405 call_yank_do_autocmd('*');
1406 if (clip_isautosel_plus())
1407 call_yank_do_autocmd('+');
1408 }
1409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#endif
1411
1412 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 setmouse();
1414 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001416 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001417 curbuf->b_visual.vi_mode = VIsual_mode;
1418 curbuf->b_visual.vi_start = VIsual;
1419 curbuf->b_visual.vi_end = curwin->w_cursor;
1420 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421#ifdef FEAT_EVAL
1422 curbuf->b_visual_mode_eval = VIsual_mode;
1423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (!virtual_active())
1425 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001426 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001428 adjust_cursor_eol();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001429 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430}
1431
1432/*
1433 * Reset VIsual_active and VIsual_reselect.
1434 */
1435 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001436reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438 if (VIsual_active)
1439 {
1440 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001441 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443 VIsual_reselect = FALSE;
1444}
1445
1446/*
1447 * Reset VIsual_active and VIsual_reselect if it's set.
1448 */
1449 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001450reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 if (VIsual_active)
1453 {
1454 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001455 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 VIsual_reselect = FALSE;
1457 }
1458}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001460 void
1461restore_visual_mode(void)
1462{
1463 if (VIsual_mode_orig != NUL)
1464 {
1465 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1466 VIsual_mode_orig = NUL;
1467 }
1468}
1469
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470/*
1471 * Check for a balloon-eval special item to include when searching for an
1472 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1473 * Returns TRUE if the character at "*ptr" should be included.
1474 * "dir" is FORWARD or BACKWARD, the direction of searching.
1475 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1476 * "bnp" points to a counter for square brackets.
1477 */
1478 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001479find_is_eval_item(
1480 char_u *ptr,
1481 int *colp,
1482 int *bnp,
1483 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001485 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1487 ++*bnp;
1488 if (*bnp > 0)
1489 {
1490 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1491 --*bnp;
1492 return TRUE;
1493 }
1494
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001495 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (*ptr == '.')
1497 return TRUE;
1498
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001499 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1501 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1502 {
1503 *colp += dir;
1504 return TRUE;
1505 }
1506 return FALSE;
1507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
1509/*
1510 * Find the identifier under or to the right of the cursor.
1511 * "find_type" can have one of three values:
1512 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001513 * FIND_STRING: find any non-white text
1514 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001515 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 *
1517 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001518 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001520 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 * This doesn't match the real Vi but I like it a little better and it
1522 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001523 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 * When FIND_IDENT isn't defined, we backup until a blank.
1525 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001526 * Returns the length of the text, or zero if no text is found.
1527 * If text is found, a pointer to the text is put in "*text". This
1528 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 */
1530 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001531find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
1533 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001534 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535}
1536
1537/*
1538 * Like find_ident_under_cursor(), but for any window and any position.
1539 * However: Uses 'iskeyword' from the current window!.
1540 */
1541 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001542find_ident_at_pos(
1543 win_T *wp,
1544 linenr_T lnum,
1545 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001546 char_u **text,
1547 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001548 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001551 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 int this_class = 0;
1554 int prev_class;
1555 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001556 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
1558 /*
1559 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001560 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
1562 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1563 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1564 {
1565 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001566 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 */
1568 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (has_mbyte)
1570 {
1571 while (ptr[col] != NUL)
1572 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001573 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1575 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 this_class = mb_get_class(ptr + col);
1577 if (this_class != 0 && (i == 1 || this_class != 1))
1578 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001579 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
1581 }
1582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001584 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 )
1587 ++col;
1588
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001589 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
1592 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001593 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (has_mbyte)
1596 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001597 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1599 this_class = mb_get_class((char_u *)"a");
1600 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001602 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1605 prev_class = mb_get_class(ptr + prevcol);
1606 if (this_class != prev_class
1607 && (i == 0
1608 || prev_class == 0
1609 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 && (!(find_type & FIND_EVAL)
1611 || prevcol == 0
1612 || !find_is_eval_item(ptr + prevcol, &prevcol,
1613 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 )
1615 break;
1616 col = prevcol;
1617 }
1618
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001619 // If we don't want just any old text, or we've found an
1620 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (this_class > 2)
1622 this_class = 2;
1623 if (!(find_type & FIND_STRING) || this_class == 2)
1624 break;
1625 }
1626 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628 while (col > 0
1629 && ((i == 0
1630 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001631 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 && (!(find_type & FIND_IDENT)
1633 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 || ((find_type & FIND_EVAL)
1635 && col > 1
1636 && find_is_eval_item(ptr + col - 1, &col,
1637 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 ))
1639 --col;
1640
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001641 // If we don't want just any old text, or we've found an
1642 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1644 break;
1645 }
1646 }
1647
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001648 if (ptr[col] == NUL || (i == 0
1649 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001651 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001652 if ((find_type & FIND_NOERROR) == 0)
1653 {
1654 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001655 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001656 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001657 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 return 0;
1660 }
1661 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001662 *text = ptr;
1663 if (textcol != NULL)
1664 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001667 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 bn = 0;
1670 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (has_mbyte)
1673 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001674 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 this_class = mb_get_class(ptr);
1676 while (ptr[col] != NUL
1677 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1678 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 || ((find_type & FIND_EVAL)
1680 && col <= (int)startcol
1681 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001683 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 }
1685 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001687 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 || ((find_type & FIND_EVAL)
1689 && col <= (int)startcol
1690 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
1694 return col;
1695}
1696
1697/*
1698 * Prepare for redo of a normal command.
1699 */
1700 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001701prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
1703 prep_redo(cap->oap->regname, cap->count0,
1704 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1705}
1706
1707/*
1708 * Prepare for redo of any command.
1709 * Note that only the last argument can be a multi-byte char.
1710 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001711 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001712prep_redo(
1713 int regname,
1714 long num,
1715 int cmd1,
1716 int cmd2,
1717 int cmd3,
1718 int cmd4,
1719 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001721 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1722}
1723
1724/*
1725 * Prepare for redo of any command with extra count after "cmd2".
1726 */
1727 void
1728prep_redo_num2(
1729 int regname,
1730 long num1,
1731 int cmd1,
1732 int cmd2,
1733 long num2,
1734 int cmd3,
1735 int cmd4,
1736 int cmd5)
1737{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001739 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 {
1741 AppendCharToRedobuff('"');
1742 AppendCharToRedobuff(regname);
1743 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001744 if (num1 != 0)
1745 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 if (cmd1 != NUL)
1747 AppendCharToRedobuff(cmd1);
1748 if (cmd2 != NUL)
1749 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001750 if (num2 != 0)
1751 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (cmd3 != NUL)
1753 AppendCharToRedobuff(cmd3);
1754 if (cmd4 != NUL)
1755 AppendCharToRedobuff(cmd4);
1756 if (cmd5 != NUL)
1757 AppendCharToRedobuff(cmd5);
1758}
1759
1760/*
1761 * check for operator active and clear it
1762 *
1763 * return TRUE if operator was active
1764 */
1765 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001766checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 if (oap->op_type == OP_NOP)
1769 return FALSE;
1770 clearopbeep(oap);
1771 return TRUE;
1772}
1773
1774/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001775 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001777 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 */
1779 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001780checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001782 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 return FALSE;
1784 clearopbeep(oap);
1785 return TRUE;
1786}
1787
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001788 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001789clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790{
1791 oap->op_type = OP_NOP;
1792 oap->regname = 0;
1793 oap->motion_force = NUL;
1794 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001795 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796}
1797
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001798 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001799clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 clearop(oap);
1802 beep_flush();
1803}
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805/*
1806 * Remove the shift modifier from a special key.
1807 */
1808 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001809unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 switch (cap->cmdchar)
1812 {
1813 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1814 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1815 case K_S_UP: cap->cmdchar = K_UP; break;
1816 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1817 case K_S_HOME: cap->cmdchar = K_HOME; break;
1818 case K_S_END: cap->cmdchar = K_END; break;
1819 }
1820 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1821}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001823/*
1824 * If the mode is currently displayed clear the command line or update the
1825 * command displayed.
1826 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001827 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001828may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001829{
1830 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001831 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001832#ifdef FEAT_CMDL_INFO
1833 else
1834 clear_showcmd();
1835#endif
1836}
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1839/*
1840 * Routines for displaying a partly typed command
1841 */
1842
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001843#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001845static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846static int showcmd_is_clear = TRUE;
1847static int showcmd_visual = FALSE;
1848
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001849static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001852clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853{
1854 if (!p_sc)
1855 return;
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if (VIsual_active && !char_avail())
1858 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001859 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 long lines;
1861 colnr_T leftcol, rightcol;
1862 linenr_T top, bot;
1863
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001864 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001865 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 {
1867 top = VIsual.lnum;
1868 bot = curwin->w_cursor.lnum;
1869 }
1870 else
1871 {
1872 top = curwin->w_cursor.lnum;
1873 bot = VIsual.lnum;
1874 }
1875# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001876 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001877 (void)hasFolding(top, &top, NULL);
1878 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879# endif
1880 lines = bot - top + 1;
1881
1882 if (VIsual_mode == Ctrl_V)
1883 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001884# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001885 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001886 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001887
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001888 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001889 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001890 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001893# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001894 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001895 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001896# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1898 (long)(rightcol - leftcol + 1));
1899 }
1900 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1901 sprintf((char *)showcmd_buf, "%ld", lines);
1902 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001903 {
1904 char_u *s, *e;
1905 int l;
1906 int bytes = 0;
1907 int chars = 0;
1908
1909 if (cursor_bot)
1910 {
1911 s = ml_get_pos(&VIsual);
1912 e = ml_get_cursor();
1913 }
1914 else
1915 {
1916 s = ml_get_cursor();
1917 e = ml_get_pos(&VIsual);
1918 }
1919 while ((*p_sel != 'e') ? s <= e : s < e)
1920 {
1921 l = (*mb_ptr2len)(s);
1922 if (l == 0)
1923 {
1924 ++bytes;
1925 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001926 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001927 }
1928 bytes += l;
1929 ++chars;
1930 s += l;
1931 }
1932 if (bytes == chars)
1933 sprintf((char *)showcmd_buf, "%d", chars);
1934 else
1935 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1936 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001937 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 showcmd_visual = TRUE;
1939 }
1940 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
1942 showcmd_buf[0] = NUL;
1943 showcmd_visual = FALSE;
1944
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001945 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (showcmd_is_clear)
1947 return;
1948 }
1949
1950 display_showcmd();
1951}
1952
1953/*
1954 * Add 'c' to string of shown command chars.
1955 * Return TRUE if output has been written (and setcursor() has been called).
1956 */
1957 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001958add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959{
1960 char_u *p;
1961 int old_len;
1962 int extra_len;
1963 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 int i;
1965 static int ignore[] =
1966 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001967#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1969 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001970#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001971 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001972 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1974 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001975 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001977 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 0
1979 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
Bram Moolenaar09df3122006-01-23 22:23:09 +00001981 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 return FALSE;
1983
1984 if (showcmd_visual)
1985 {
1986 showcmd_buf[0] = NUL;
1987 showcmd_visual = FALSE;
1988 }
1989
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001990 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (IS_SPECIAL(c))
1992 for (i = 0; ignore[i] != 0; ++i)
1993 if (ignore[i] == c)
1994 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
1996 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001997 if (*p == ' ')
1998 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 old_len = (int)STRLEN(showcmd_buf);
2000 extra_len = (int)STRLEN(p);
2001 overflow = old_len + extra_len - SHOWCMD_COLS;
2002 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00002003 mch_memmove(showcmd_buf, showcmd_buf + overflow,
2004 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 STRCAT(showcmd_buf, p);
2006
2007 if (char_avail())
2008 return FALSE;
2009
2010 display_showcmd();
2011
2012 return TRUE;
2013}
2014
2015 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002016add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017{
2018 if (!add_to_showcmd(c))
2019 setcursor();
2020}
2021
2022/*
2023 * Delete 'len' characters from the end of the shown command.
2024 */
2025 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002026del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027{
2028 int old_len;
2029
2030 if (!p_sc)
2031 return;
2032
2033 old_len = (int)STRLEN(showcmd_buf);
2034 if (len > old_len)
2035 len = old_len;
2036 showcmd_buf[old_len - len] = NUL;
2037
2038 if (!char_avail())
2039 display_showcmd();
2040}
2041
2042/*
2043 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2044 * something and there is a partial mapping.
2045 */
2046 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002047push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048{
2049 if (p_sc)
2050 STRCPY(old_showcmd_buf, showcmd_buf);
2051}
2052
2053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002054pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
2056 if (!p_sc)
2057 return;
2058
2059 STRCPY(showcmd_buf, old_showcmd_buf);
2060
2061 display_showcmd();
2062}
2063
2064 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002065display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066{
2067 int len;
2068
2069 cursor_off();
2070
2071 len = (int)STRLEN(showcmd_buf);
2072 if (len == 0)
2073 showcmd_is_clear = TRUE;
2074 else
2075 {
2076 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2077 showcmd_is_clear = FALSE;
2078 }
2079
2080 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002081 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2082 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 */
2084 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2085
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002086 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087}
2088#endif
2089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090/*
2091 * When "check" is FALSE, prepare for commands that scroll the window.
2092 * When "check" is TRUE, take care of scroll-binding after the window has
2093 * scrolled. Called from normal_cmd() and edit().
2094 */
2095 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002096do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 static win_T *old_curwin = NULL;
2099 static linenr_T old_topline = 0;
2100#ifdef FEAT_DIFF
2101 static int old_topfill = 0;
2102#endif
2103 static buf_T *old_buf = NULL;
2104 static colnr_T old_leftcol = 0;
2105
2106 if (check && curwin->w_p_scb)
2107 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002108 // If a ":syncbind" command was just used, don't scroll, only reset
2109 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 if (did_syncbind)
2111 did_syncbind = FALSE;
2112 else if (curwin == old_curwin)
2113 {
2114 /*
2115 * Synchronize other windows, as necessary according to
2116 * 'scrollbind'. Don't do this after an ":edit" command, except
2117 * when 'diff' is set.
2118 */
2119 if ((curwin->w_buffer == old_buf
2120#ifdef FEAT_DIFF
2121 || curwin->w_p_diff
2122#endif
2123 )
2124 && (curwin->w_topline != old_topline
2125#ifdef FEAT_DIFF
2126 || curwin->w_topfill != old_topfill
2127#endif
2128 || curwin->w_leftcol != old_leftcol))
2129 {
2130 check_scrollbind(curwin->w_topline - old_topline,
2131 (long)(curwin->w_leftcol - old_leftcol));
2132 }
2133 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002134 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 /*
2137 * When switching between windows, make sure that the relative
2138 * vertical offset is valid for the new window. The relative
2139 * offset is invalid whenever another 'scrollbind' window has
2140 * scrolled to a point that would force the current window to
2141 * scroll past the beginning or end of its buffer. When the
2142 * resync is performed, some of the other 'scrollbind' windows may
2143 * need to jump so that the current window's relative position is
2144 * visible on-screen.
2145 */
2146 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2147 }
2148 curwin->w_scbind_pos = curwin->w_topline;
2149 }
2150
2151 old_curwin = curwin;
2152 old_topline = curwin->w_topline;
2153#ifdef FEAT_DIFF
2154 old_topfill = curwin->w_topfill;
2155#endif
2156 old_buf = curwin->w_buffer;
2157 old_leftcol = curwin->w_leftcol;
2158}
2159
2160/*
2161 * Synchronize any windows that have "scrollbind" set, based on the
2162 * number of rows by which the current window has changed
2163 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2164 */
2165 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002166check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
2168 int want_ver;
2169 int want_hor;
2170 win_T *old_curwin = curwin;
2171 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 int old_VIsual_select = VIsual_select;
2173 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 colnr_T tgt_leftcol = curwin->w_leftcol;
2175 long topline;
2176 long y;
2177
2178 /*
2179 * check 'scrollopt' string for vertical and horizontal scroll options
2180 */
2181 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2182#ifdef FEAT_DIFF
2183 want_ver |= old_curwin->w_p_diff;
2184#endif
2185 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2186
2187 /*
2188 * loop through the scrollbound windows and scroll accordingly
2189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002191 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002194 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (curwin != old_curwin && curwin->w_p_scb)
2196 {
2197 /*
2198 * do the vertical scroll
2199 */
2200 if (want_ver)
2201 {
2202#ifdef FEAT_DIFF
2203 if (old_curwin->w_p_diff && curwin->w_p_diff)
2204 {
2205 diff_set_topline(old_curwin, curwin);
2206 }
2207 else
2208#endif
2209 {
2210 curwin->w_scbind_pos += topline_diff;
2211 topline = curwin->w_scbind_pos;
2212 if (topline > curbuf->b_ml.ml_line_count)
2213 topline = curbuf->b_ml.ml_line_count;
2214 if (topline < 1)
2215 topline = 1;
2216
2217 y = topline - curwin->w_topline;
2218 if (y > 0)
2219 scrollup(y, FALSE);
2220 else
2221 scrolldown(-y, FALSE);
2222 }
2223
2224 redraw_later(VALID);
2225 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
2228
2229 /*
2230 * do the horizontal scroll
2231 */
2232 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2233 {
2234 curwin->w_leftcol = tgt_leftcol;
2235 leftcol_changed();
2236 }
2237 }
2238 }
2239
2240 /*
2241 * reset current-window
2242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 VIsual_select = old_VIsual_select;
2244 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 curwin = old_curwin;
2246 curbuf = old_curbuf;
2247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248
2249/*
2250 * Command character that's ignored.
2251 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002252 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002255nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002257 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258}
2259
2260/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002261 * Command character that doesn't do anything, but unlike nv_ignore() does
2262 * start edit(). Used for "startinsert" executed while starting up.
2263 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002264 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002265nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002266{
2267}
2268
2269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 * Command character doesn't exist.
2271 */
2272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002273nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 clearopbeep(cap->oap);
2276}
2277
2278/*
2279 * <Help> and <F1> commands.
2280 */
2281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002282nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283{
2284 if (!checkclearopq(cap->oap))
2285 ex_help(NULL);
2286}
2287
2288/*
2289 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2290 */
2291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002292nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002294#ifdef FEAT_JOB_CHANNEL
2295 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2296 clearopbeep(cap->oap);
2297 else
2298#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002299 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002300 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002301 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002302 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2303 op_addsub(cap->oap, cap->count1, cap->arg);
2304 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002305 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002306 else if (VIsual_active)
2307 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002308 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002309 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310}
2311
2312/*
2313 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2314 */
2315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002316nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
2318 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002319 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002320 if (mod_mask & MOD_MASK_CTRL)
2321 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002322 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002323 if (cap->arg == BACKWARD)
2324 goto_tabpage(-(int)cap->count1);
2325 else
2326 goto_tabpage((int)cap->count0);
2327 }
2328 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002329 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331}
2332
2333/*
2334 * Implementation of "gd" and "gD" command.
2335 */
2336 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002337nv_gd(
2338 oparg_T *oap,
2339 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002340 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341{
2342 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 char_u *ptr;
2344
Bram Moolenaard9d30582005-05-18 22:10:28 +00002345 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002346 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002347 == FAIL)
2348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002350 }
2351 else
2352 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002353#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002354 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2355 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002356#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002357 // clear any search statistics
2358 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2359 clear_cmdline = TRUE;
2360 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002361}
2362
2363/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002364 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2365 * otherwise.
2366 */
2367 static int
2368is_ident(char_u *line, int offset)
2369{
2370 int i;
2371 int incomment = FALSE;
2372 int instring = 0;
2373 int prev = 0;
2374
2375 for (i = 0; i < offset && line[i] != NUL; i++)
2376 {
2377 if (instring != 0)
2378 {
2379 if (prev != '\\' && line[i] == instring)
2380 instring = 0;
2381 }
2382 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2383 {
2384 instring = line[i];
2385 }
2386 else
2387 {
2388 if (incomment)
2389 {
2390 if (prev == '*' && line[i] == '/')
2391 incomment = FALSE;
2392 }
2393 else if (prev == '/' && line[i] == '*')
2394 {
2395 incomment = TRUE;
2396 }
2397 else if (prev == '/' && line[i] == '/')
2398 {
2399 return FALSE;
2400 }
2401 }
2402
2403 prev = line[i];
2404 }
2405
2406 return incomment == FALSE && instring == 0;
2407}
2408
2409/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002410 * Search for variable declaration of "ptr[len]".
2411 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2412 * current file ("gD").
2413 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002414 * Return FAIL when not found.
2415 */
2416 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417find_decl(
2418 char_u *ptr,
2419 int len,
2420 int locally,
2421 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002422 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002423{
2424 char_u *pat;
2425 pos_T old_pos;
2426 pos_T par_pos;
2427 pos_T found_pos;
2428 int t;
2429 int save_p_ws;
2430 int save_p_scs;
2431 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002432 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002433 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002434 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002435
2436 if ((pat = alloc(len + 7)) == NULL)
2437 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002439 // Put "\V" before the pattern to avoid that the special meaning of "."
2440 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002441 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2442 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 old_pos = curwin->w_cursor;
2444 save_p_ws = p_ws;
2445 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002446 p_ws = FALSE; // don't wrap around end of file now
2447 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 /*
2450 * 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.
2453 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002456 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002458 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460 else
2461 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002462 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2464 --curwin->w_cursor.lnum;
2465 }
2466 curwin->w_cursor.col = 0;
2467
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002468 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002469 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002470 for (;;)
2471 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002472 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002473 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002474 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002475 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002476
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002477 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002478 {
2479 pos_T *pos;
2480
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002481 // Check that the block the match is in doesn't end before the
2482 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002483 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2484 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2485 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002486 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002487 // There can't be a useful match before the end of this block.
2488 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002489 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002490 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002491 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002492 }
2493
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002494 if (t == FAIL)
2495 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002496 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002497 if (found_pos.lnum != 0)
2498 {
2499 curwin->w_cursor = found_pos;
2500 t = OK;
2501 }
2502 break;
2503 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002504 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002505 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002506 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002507 ++curwin->w_cursor.lnum;
2508 curwin->w_cursor.col = 0;
2509 continue;
2510 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002511 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2512
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002513 // If the current position is not a valid identifier and a previous
2514 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002515 if (!valid && found_pos.lnum != 0)
2516 {
2517 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002518 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002519 }
2520
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002521 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002522 if (valid && !locally)
2523 break;
2524 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002525 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002526 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002527 if (found_pos.lnum != 0)
2528 curwin->w_cursor = found_pos;
2529 break;
2530 }
2531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002532 // For finding a local variable and the match is before the "{" or
2533 // inside a comment, continue searching. For K&R style function
2534 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002535 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002536 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002537 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002538 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002539 // Remove SEARCH_START from flags to avoid getting stuck at one
2540 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002541 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002543
2544 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002546 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 curwin->w_cursor = old_pos;
2548 }
2549 else
2550 {
2551 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002552 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 reset_search_dir();
2554 }
2555
2556 vim_free(pat);
2557 p_ws = save_p_ws;
2558 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002559
2560 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561}
2562
2563/*
2564 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2565 * lines rather than lines in the file.
2566 * 'dist' must be positive.
2567 *
2568 * Return OK if able to move cursor, FAIL otherwise.
2569 */
2570 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002571nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573 int linelen = linetabsize(ml_get_curline());
2574 int retval = OK;
2575 int atend = FALSE;
2576 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002577 int col_off1; // margin offset for first screen line
2578 int col_off2; // margin offset for wrapped screen line
2579 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002580 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002583 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 col_off1 = curwin_col_off();
2586 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002587 width1 = curwin->w_width - col_off1;
2588 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002589 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002590 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 /*
2595 * Instead of sticking at the last character of the buffer line we
2596 * try to stick in the last column of the screen.
2597 */
2598 if (curwin->w_curswant == MAXCOL)
2599 {
2600 atend = TRUE;
2601 validate_virtcol();
2602 if (width1 <= 0)
2603 curwin->w_curswant = 0;
2604 else
2605 {
2606 curwin->w_curswant = width1 - 1;
2607 if (curwin->w_virtcol > curwin->w_curswant)
2608 curwin->w_curswant += ((curwin->w_virtcol
2609 - curwin->w_curswant - 1) / width2 + 1) * width2;
2610 }
2611 }
2612 else
2613 {
2614 if (linelen > width1)
2615 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2616 else
2617 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002618 if (curwin->w_curswant >= (colnr_T)n)
2619 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 }
2621
2622 while (dist--)
2623 {
2624 if (dir == BACKWARD)
2625 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002626 if ((long)curwin->w_curswant >= width1
2627#ifdef FEAT_FOLDING
2628 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2629#endif
2630 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002631 // Move back within the line. This can give a negative value
2632 // for w_curswant if width1 < width2 (with cpoptions+=n),
2633 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 curwin->w_curswant -= width2;
2635 else
2636 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002637 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002639 // Move to the start of a closed fold. Don't do that when
2640 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (!(fdo_flags & FDO_ALL))
2642 (void)hasFolding(curwin->w_cursor.lnum,
2643 &curwin->w_cursor.lnum, NULL);
2644#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002645 if (curwin->w_cursor.lnum == 1)
2646 {
2647 retval = FAIL;
2648 break;
2649 }
2650 --curwin->w_cursor.lnum;
2651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 linelen = linetabsize(ml_get_curline());
2653 if (linelen > width1)
2654 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2655 + 1) * width2;
2656 }
2657 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002658 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
2660 if (linelen > width1)
2661 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2662 else
2663 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002664 if (curwin->w_curswant + width2 < (colnr_T)n
2665#ifdef FEAT_FOLDING
2666 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2667#endif
2668 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002669 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 curwin->w_curswant += width2;
2671 else
2672 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002673 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002675 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2677 &curwin->w_cursor.lnum);
2678#endif
2679 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2680 {
2681 retval = FAIL;
2682 break;
2683 }
2684 curwin->w_cursor.lnum++;
2685 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002686 // Check if the cursor has moved below the number display
2687 // when width1 < width2 (with cpoptions+=n). Subtract width2
2688 // to get a negative value for w_curswant, which will get
2689 // clipped to column 0.
2690 if (curwin->w_curswant >= width1)
2691 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002692 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 }
2694 }
2695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002698 if (virtual_active() && atend)
2699 coladvance(MAXCOL);
2700 else
2701 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2704 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002705 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002706 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 /*
2709 * Check for landing on a character that got split at the end of the
2710 * last line. We want to advance a screenline, not end up in the same
2711 * screenline or move two screenlines.
2712 */
2713 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002714 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002715#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002716 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2717 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002718#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002719
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002720 c = (*mb_ptr2char)(ml_get_cursor());
2721 if (dir == FORWARD && virtcol < curwin->w_curswant
2722 && (curwin->w_curswant <= (colnr_T)width1)
2723 && !vim_isprintc(c) && c > 255)
2724 oneright();
2725
Bram Moolenaar773b1582014-08-29 14:20:51 +02002726 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 && (curwin->w_curswant < (colnr_T)width1
2728 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2729 : ((curwin->w_curswant - width1) % width2
2730 > (colnr_T)width2 / 2)))
2731 --curwin->w_cursor.col;
2732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
2734 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002735 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 return retval;
2738}
2739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740/*
2741 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2742 * cap->arg must be TRUE for CTRL-E.
2743 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002744 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002745nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 if (!checkclearop(cap->oap))
2748 scroll_redraw(cap->arg, cap->count1);
2749}
2750
2751/*
2752 * Scroll "count" lines up or down, and redraw.
2753 */
2754 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002755scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 linenr_T prev_topline = curwin->w_topline;
2758#ifdef FEAT_DIFF
2759 int prev_topfill = curwin->w_topfill;
2760#endif
2761 linenr_T prev_lnum = curwin->w_cursor.lnum;
2762
2763 if (up)
2764 scrollup(count, TRUE);
2765 else
2766 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002767 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002769 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2770 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 cursor_correct();
2772 check_cursor_moved(curwin);
2773 curwin->w_valid |= VALID_TOPLINE;
2774
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002775 // If moved back to where we were, at least move the cursor, otherwise
2776 // we get stuck at one position. Don't move the cursor up if the
2777 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 while (curwin->w_topline == prev_topline
2779#ifdef FEAT_DIFF
2780 && curwin->w_topfill == prev_topfill
2781#endif
2782 )
2783 {
2784 if (up)
2785 {
2786 if (curwin->w_cursor.lnum > prev_lnum
2787 || cursor_down(1L, FALSE) == FAIL)
2788 break;
2789 }
2790 else
2791 {
2792 if (curwin->w_cursor.lnum < prev_lnum
2793 || prev_topline == 1L
2794 || cursor_up(1L, FALSE) == FAIL)
2795 break;
2796 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002797 // Mark w_topline as valid, otherwise the screen jumps back at the
2798 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 check_cursor_moved(curwin);
2800 curwin->w_valid |= VALID_TOPLINE;
2801 }
2802 }
2803 if (curwin->w_cursor.lnum != prev_lnum)
2804 coladvance(curwin->w_curswant);
2805 redraw_later(VALID);
2806}
2807
2808/*
2809 * Commands that start with "z".
2810 */
2811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002812nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813{
2814 long n;
2815 colnr_T col;
2816 int nchar = cap->nchar;
2817#ifdef FEAT_FOLDING
2818 long old_fdl = curwin->w_p_fdl;
2819 int old_fen = curwin->w_p_fen;
2820#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002821#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002822 int undo = FALSE;
2823#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002824 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
2826 if (VIM_ISDIGIT(nchar))
2827 {
2828 /*
2829 * "z123{nchar}": edit the count before obtaining {nchar}
2830 */
2831 if (checkclearop(cap->oap))
2832 return;
2833 n = nchar - '0';
2834 for (;;)
2835 {
2836#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002837 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838#endif
2839 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002840 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002841 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 --no_mapping;
2844 --allow_keys;
2845#ifdef FEAT_CMDL_INFO
2846 (void)add_to_showcmd(nchar);
2847#endif
2848 if (nchar == K_DEL || nchar == K_KDEL)
2849 n /= 10;
2850 else if (VIM_ISDIGIT(nchar))
2851 n = n * 10 + (nchar - '0');
2852 else if (nchar == CAR)
2853 {
2854#ifdef FEAT_GUI
2855 need_mouse_correct = TRUE;
2856#endif
2857 win_setheight((int)n);
2858 break;
2859 }
2860 else if (nchar == 'l'
2861 || nchar == 'h'
2862 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002863 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
2865 cap->count1 = n ? n * cap->count1 : cap->count1;
2866 goto dozet;
2867 }
2868 else
2869 {
2870 clearopbeep(cap->oap);
2871 break;
2872 }
2873 }
2874 cap->oap->op_type = OP_NOP;
2875 return;
2876 }
2877
2878dozet:
2879 if (
2880#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002881 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2882 // and "zC" only in Visual mode. "zj" and "zk" are motion
2883 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 cap->nchar != 'f' && cap->nchar != 'F'
2885 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2886 && cap->nchar != 'j' && cap->nchar != 'k'
2887 &&
2888#endif
2889 checkclearop(cap->oap))
2890 return;
2891
2892 /*
2893 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2894 * If line number given, set cursor.
2895 */
2896 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2897 && cap->count0
2898 && cap->count0 != curwin->w_cursor.lnum)
2899 {
2900 setpcmark();
2901 if (cap->count0 > curbuf->b_ml.ml_line_count)
2902 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2903 else
2904 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002905 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
2907
2908 switch (nchar)
2909 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 case '+':
2912 if (cap->count0 == 0)
2913 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 // No count given: put cursor at the line below screen
2915 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2917 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2918 else
2919 curwin->w_cursor.lnum = curwin->w_botline;
2920 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002921 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 case NL:
2923 case CAR:
2924 case K_KENTER:
2925 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
2928 case 't': scroll_cursor_top(0, TRUE);
2929 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002930 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 break;
2932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
2937 case 'z': scroll_cursor_halfway(TRUE);
2938 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002939 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 break;
2941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002942 // "z^", "z-" and "zb": put cursor at bottom of screen
2943 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2944 // when <count> is at bottom of window, and puts that one at
2945 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (cap->count0 != 0)
2947 {
2948 scroll_cursor_bot(0, TRUE);
2949 curwin->w_cursor.lnum = curwin->w_topline;
2950 }
2951 else if (curwin->w_topline == 1)
2952 curwin->w_cursor.lnum = 1;
2953 else
2954 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002955 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 case '-':
2957 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002958 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
2960 case 'b': scroll_cursor_bot(0, TRUE);
2961 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002962 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 break;
2964
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002965 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002967 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002970 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 case 'h':
2972 case K_LEFT:
2973 if (!curwin->w_p_wrap)
2974 {
2975 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2976 curwin->w_leftcol = 0;
2977 else
2978 curwin->w_leftcol -= (colnr_T)cap->count1;
2979 leftcol_changed();
2980 }
2981 break;
2982
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002984 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002985 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002987 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 case 'l':
2989 case K_RIGHT:
2990 if (!curwin->w_p_wrap)
2991 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002992 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 curwin->w_leftcol += (colnr_T)cap->count1;
2994 leftcol_changed();
2995 }
2996 break;
2997
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002998 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 case 's': if (!curwin->w_p_wrap)
3000 {
3001#ifdef FEAT_FOLDING
3002 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003003 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 else
3005#endif
3006 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01003007 if ((long)col > siso)
3008 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 else
3010 col = 0;
3011 if (curwin->w_leftcol != col)
3012 {
3013 curwin->w_leftcol = col;
3014 redraw_later(NOT_VALID);
3015 }
3016 }
3017 break;
3018
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003019 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 case 'e': if (!curwin->w_p_wrap)
3021 {
3022#ifdef FEAT_FOLDING
3023 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 else
3026#endif
3027 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02003028 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01003029 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 col = 0;
3031 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01003032 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (curwin->w_leftcol != col)
3034 {
3035 curwin->w_leftcol = col;
3036 redraw_later(NOT_VALID);
3037 }
3038 }
3039 break;
3040
Christian Brabandt2fa93842021-05-30 22:17:25 +02003041 // "zp", "zP" in block mode put without addind trailing spaces
3042 case 'P':
3043 case 'p': nv_put(cap);
3044 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02003045 // "zy" Yank without trailing spaces
3046 case 'y': nv_operator(cap);
3047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003049 // "zF": create fold command
3050 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 case 'F':
3052 case 'f': if (foldManualAllowed(TRUE))
3053 {
3054 cap->nchar = 'f';
3055 nv_operator(cap);
3056 curwin->w_p_fen = TRUE;
3057
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003058 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3060 {
3061 nv_operator(cap);
3062 finish_op = TRUE;
3063 }
3064 }
3065 else
3066 clearopbeep(cap->oap);
3067 break;
3068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 // "zd": delete fold at cursor
3070 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 case 'd':
3072 case 'D': if (foldManualAllowed(FALSE))
3073 {
3074 if (VIsual_active)
3075 nv_operator(cap);
3076 else
3077 deleteFold(curwin->w_cursor.lnum,
3078 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3079 }
3080 break;
3081
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003082 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 case 'E': if (foldmethodIsManual(curwin))
3084 {
3085 clearFolding(curwin);
3086 changed_window_setting();
3087 }
3088 else if (foldmethodIsMarker(curwin))
3089 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3090 TRUE, FALSE);
3091 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003092 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 break;
3094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003095 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 case 'n': curwin->w_p_fen = FALSE;
3097 break;
3098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 case 'N': curwin->w_p_fen = TRUE;
3101 break;
3102
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003103 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3105 break;
3106
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003107 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3109 openFold(curwin->w_cursor.lnum, cap->count1);
3110 else
3111 {
3112 closeFold(curwin->w_cursor.lnum, cap->count1);
3113 curwin->w_p_fen = TRUE;
3114 }
3115 break;
3116
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003117 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3119 openFoldRecurse(curwin->w_cursor.lnum);
3120 else
3121 {
3122 closeFoldRecurse(curwin->w_cursor.lnum);
3123 curwin->w_p_fen = TRUE;
3124 }
3125 break;
3126
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 case 'o': if (VIsual_active)
3129 nv_operator(cap);
3130 else
3131 openFold(curwin->w_cursor.lnum, cap->count1);
3132 break;
3133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 case 'O': if (VIsual_active)
3136 nv_operator(cap);
3137 else
3138 openFoldRecurse(curwin->w_cursor.lnum);
3139 break;
3140
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003141 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 case 'c': if (VIsual_active)
3143 nv_operator(cap);
3144 else
3145 closeFold(curwin->w_cursor.lnum, cap->count1);
3146 curwin->w_p_fen = TRUE;
3147 break;
3148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 case 'C': if (VIsual_active)
3151 nv_operator(cap);
3152 else
3153 closeFoldRecurse(curwin->w_cursor.lnum);
3154 curwin->w_p_fen = TRUE;
3155 break;
3156
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003157 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 case 'v': foldOpenCursor();
3159 break;
3160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003161 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003163 curwin->w_foldinvalid = TRUE; // recompute folds
3164 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 foldOpenCursor();
3166 break;
3167
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003168 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003170 curwin->w_foldinvalid = TRUE; // recompute folds
3171 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 break;
3173
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003174 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003176 {
3177 curwin->w_p_fdl -= cap->count1;
3178 if (curwin->w_p_fdl < 0)
3179 curwin->w_p_fdl = 0;
3180 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003181 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 curwin->w_p_fen = TRUE;
3183 break;
3184
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003185 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003187 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 curwin->w_p_fen = TRUE;
3189 break;
3190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003191 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003192 case 'r': curwin->w_p_fdl += cap->count1;
3193 {
3194 int d = getDeepestNesting();
3195
3196 if (curwin->w_p_fdl >= d)
3197 curwin->w_p_fdl = d;
3198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 break;
3200
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003201 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003203 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 break;
3205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003206 case 'j': // "zj" move to next fold downwards
3207 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3209 cap->count1) == FAIL)
3210 clearopbeep(cap->oap);
3211 break;
3212
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003213#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003215#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003216 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003217 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003218 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003219 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003220 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003221 --no_mapping;
3222 --allow_keys;
3223#ifdef FEAT_CMDL_INFO
3224 (void)add_to_showcmd(nchar);
3225#endif
3226 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3227 {
3228 clearopbeep(cap->oap);
3229 break;
3230 }
3231 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003232 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003233
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003234 case 'g': // "zg": add good word to word list
3235 case 'w': // "zw": add wrong word to word list
3236 case 'G': // "zG": add good word to temp word list
3237 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003238 {
3239 char_u *ptr = NULL;
3240 int len;
3241
3242 if (checkclearop(cap->oap))
3243 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003244 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3245 == FAIL)
3246 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003247 if (ptr == NULL)
3248 {
3249 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003250
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003251 // Find bad word under the cursor. When 'spell' is
3252 // off this fails and find_ident_under_cursor() is
3253 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003254 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003255 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003256 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003257 if (len != 0 && curwin->w_cursor.col <= pos.col)
3258 ptr = ml_get_pos(&curwin->w_cursor);
3259 curwin->w_cursor = pos;
3260 }
3261
Bram Moolenaarb765d632005-06-07 21:00:02 +00003262 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3263 FIND_IDENT)) == 0)
3264 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003265 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3266 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003267 (nchar == 'G' || nchar == 'W')
3268 ? 0 : (int)cap->count1,
3269 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003270 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003271 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003272
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003273 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003274 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003275 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003276 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003277#endif
3278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 default: clearopbeep(cap->oap);
3280 }
3281
3282#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003283 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (old_fen != curwin->w_p_fen)
3285 {
3286# ifdef FEAT_DIFF
3287 win_T *wp;
3288
3289 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3290 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003291 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 FOR_ALL_WINDOWS(wp)
3293 {
3294 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3295 {
3296 wp->w_p_fen = curwin->w_p_fen;
3297 changed_window_setting_win(wp);
3298 }
3299 }
3300 }
3301# endif
3302 changed_window_setting();
3303 }
3304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003305 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (old_fdl != curwin->w_p_fdl)
3307 newFoldLevel();
3308#endif
3309}
3310
3311#ifdef FEAT_GUI
3312/*
3313 * Vertical scrollbar movement.
3314 */
3315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003316nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 if (cap->oap->op_type != OP_NOP)
3319 clearopbeep(cap->oap);
3320
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003321 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 gui_do_scroll();
3323}
3324
3325/*
3326 * Horizontal scrollbar movement.
3327 */
3328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003329nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330{
3331 if (cap->oap->op_type != OP_NOP)
3332 clearopbeep(cap->oap);
3333
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003334 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003335 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
3337#endif
3338
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003339#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003340/*
3341 * Click in GUI tab.
3342 */
3343 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003344nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003345{
3346 if (cap->oap->op_type != OP_NOP)
3347 clearopbeep(cap->oap);
3348
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003349 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003350 goto_tabpage(current_tab);
3351}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003352
3353/*
3354 * Selected item in tab line menu.
3355 */
3356 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003357nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003358{
3359 if (cap->oap->op_type != OP_NOP)
3360 clearopbeep(cap->oap);
3361
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003362 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003363 handle_tabmenu();
3364}
3365
3366/*
3367 * Handle selecting an item of the GUI tab line menu.
3368 * Used in Normal and Insert mode.
3369 */
3370 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003371handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003372{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003373 switch (current_tabmenu)
3374 {
3375 case TABLINE_MENU_CLOSE:
3376 if (current_tab == 0)
3377 do_cmdline_cmd((char_u *)"tabclose");
3378 else
3379 {
3380 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3381 current_tab);
3382 do_cmdline_cmd(IObuff);
3383 }
3384 break;
3385
3386 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003387 if (current_tab == 0)
3388 do_cmdline_cmd((char_u *)"$tabnew");
3389 else
3390 {
3391 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3392 current_tab - 1);
3393 do_cmdline_cmd(IObuff);
3394 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003395 break;
3396
3397 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003398 if (current_tab == 0)
3399 do_cmdline_cmd((char_u *)"browse $tabnew");
3400 else
3401 {
3402 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3403 current_tab - 1);
3404 do_cmdline_cmd(IObuff);
3405 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003406 break;
3407 }
3408}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003409#endif
3410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411/*
3412 * "Q" command.
3413 */
3414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003415nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416{
3417 /*
3418 * Ignore 'Q' in Visual mode, just give a beep.
3419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003421 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003422 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 do_exmode(FALSE);
3424}
3425
3426/*
3427 * Handle a ":" command.
3428 */
3429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003430nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003432 int old_p_im;
3433 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003434 int is_cmdkey = cap->cmdchar == K_COMMAND
3435 || cap->cmdchar == K_SCRIPT_COMMAND;
3436 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
Bram Moolenaar957cf672020-11-12 14:21:06 +01003438 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 nv_operator(cap);
3440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 {
3442 if (cap->oap->op_type != OP_NOP)
3443 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 cap->oap->motion_type = MCHAR;
3446 cap->oap->inclusive = FALSE;
3447 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003448 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003450 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 stuffcharReadbuff('.');
3452 if (cap->count0 > 1)
3453 {
3454 stuffReadbuff((char_u *)",.+");
3455 stuffnumReadbuff((long)cap->count0 - 1L);
3456 }
3457 }
3458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003459 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 if (KeyTyped)
3461 compute_cmdrow();
3462
3463 old_p_im = p_im;
3464
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003465 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003466 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3467 if (is_cmdkey)
3468 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3469 else
3470 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003472 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 if (p_im != old_p_im)
3474 {
3475 if (p_im)
3476 restart_edit = 'i';
3477 else
3478 restart_edit = 0;
3479 }
3480
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003481 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003482 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003483 clearop(cap->oap);
3484 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3486 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003487 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3488 || did_emsg
3489 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003490 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 clearopbeep(cap->oap);
3492 }
3493}
3494
3495/*
3496 * Handle CTRL-G command.
3497 */
3498 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003499nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003501 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
3503 VIsual_select = !VIsual_select;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003504 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 showmode();
3506 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003507 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003508 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 fileinfo((int)cap->count0, FALSE, TRUE);
3510}
3511
3512/*
3513 * Handle CTRL-H <Backspace> command.
3514 */
3515 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003516nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (VIsual_active && VIsual_select)
3519 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003520 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 v_visop(cap);
3522 }
3523 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 nv_left(cap);
3525}
3526
3527/*
3528 * CTRL-L: clear screen and redraw.
3529 */
3530 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003531nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532{
3533 if (!checkclearop(cap->oap))
3534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003536 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003537 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003538# ifdef FEAT_RELTIME
3539 {
3540 win_T *wp;
3541
3542 FOR_ALL_WINDOWS(wp)
3543 wp->w_s->b_syn_slow = FALSE;
3544 }
3545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546#endif
3547 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003548#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3549# ifdef VIMDLL
3550 if (!gui.in_use)
3551# endif
3552 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555}
3556
3557/*
3558 * CTRL-O: In Select mode: switch to Visual mode for one command.
3559 * Otherwise: Go to older pcmark.
3560 */
3561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003562nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (VIsual_active && VIsual_select)
3565 {
3566 VIsual_select = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003567 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003569 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
3573 cap->count1 = -cap->count1;
3574 nv_pcmark(cap);
3575 }
3576}
3577
3578/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003579 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3580 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 */
3582 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003583nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
3585 if (!checkclearopq(cap->oap))
3586 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3587 GETF_SETMARK|GETF_ALT, FALSE);
3588}
3589
3590/*
3591 * "Z" commands.
3592 */
3593 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003594nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
3596 if (!checkclearopq(cap->oap))
3597 {
3598 switch (cap->nchar)
3599 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003600 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 case 'Z': do_cmdline_cmd((char_u *)"x");
3602 break;
3603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003604 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 case 'Q': do_cmdline_cmd((char_u *)"q!");
3606 break;
3607
3608 default: clearopbeep(cap->oap);
3609 }
3610 }
3611}
3612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613/*
3614 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3615 */
3616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003617do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
3619 oparg_T oa;
3620 cmdarg_T ca;
3621
3622 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003623 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 ca.oap = &oa;
3625 ca.cmdchar = c1;
3626 ca.nchar = c2;
3627 nv_ident(&ca);
3628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630/*
3631 * Handle the commands that use the word under the cursor.
3632 * [g] CTRL-] :ta to current identifier
3633 * [g] 'K' run program for current identifier
3634 * [g] '*' / to current identifier or string
3635 * [g] '#' ? to current identifier or string
3636 * g ']' :tselect for current identifier
3637 */
3638 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003639nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640{
3641 char_u *ptr = NULL;
3642 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003643 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003644 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003646 char_u *kp; // value of 'keywordprg'
3647 int kp_help; // 'keywordprg' is ":he"
3648 int kp_ex; // 'keywordprg' starts with ":"
3649 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003651 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003652 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 char_u *aux_ptr;
3654 int isman;
3655 int isman_s;
3656
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003657 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 cmdchar = cap->nchar;
3660 g_cmd = TRUE;
3661 }
3662 else
3663 {
3664 cmdchar = cap->cmdchar;
3665 g_cmd = FALSE;
3666 }
3667
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003668 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 cmdchar = '#';
3670
3671 /*
3672 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3673 */
3674 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3677 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 if (checkclearopq(cap->oap))
3679 return;
3680 }
3681
3682 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3683 (cmdchar == '*' || cmdchar == '#')
3684 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3685 {
3686 clearop(cap->oap);
3687 return;
3688 }
3689
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003690 // Allocate buffer to put the command in. Inserting backslashes can
3691 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3692 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3694 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3695 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003696 if (kp_help && *skipwhite(ptr) == NUL)
3697 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003698 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003699 return;
3700 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003701 kp_ex = (*kp == ':');
3702 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3703 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (buf == NULL)
3705 return;
3706 buf[0] = NUL;
3707
3708 switch (cmdchar)
3709 {
3710 case '*':
3711 case '#':
3712 /*
3713 * Put cursor at start of word, makes search skip the word
3714 * under the cursor.
3715 * Call setpcmark() first, so "*``" puts the cursor back where
3716 * it was.
3717 */
3718 setpcmark();
3719 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3720
3721 if (!g_cmd && vim_iswordp(ptr))
3722 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003723 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 break;
3725
3726 case 'K':
3727 if (kp_help)
3728 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003729 else if (kp_ex)
3730 {
3731 if (cap->count0 != 0)
3732 vim_snprintf((char *)buf, buflen, "%s %ld",
3733 kp, cap->count0);
3734 else
3735 STRCPY(buf, kp);
3736 STRCAT(buf, " ");
3737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 else
3739 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003740 // An external command will probably use an argument starting
3741 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003742 while (*ptr == '-' && n > 0)
3743 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003744 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003745 --n;
3746 }
3747 if (n == 0)
3748 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003749 // found dashes only
3750 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003751 vim_free(buf);
3752 return;
3753 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003754
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003755 // When a count is given, turn it into a range. Is this
3756 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 isman = (STRCMP(kp, "man") == 0);
3758 isman_s = (STRCMP(kp, "man -s") == 0);
3759 if (cap->count0 != 0 && !(isman || isman_s))
3760 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3761
3762 STRCAT(buf, "! ");
3763 if (cap->count0 == 0 && isman_s)
3764 STRCAT(buf, "man");
3765 else
3766 STRCAT(buf, kp);
3767 STRCAT(buf, " ");
3768 if (cap->count0 != 0 && (isman || isman_s))
3769 {
3770 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3771 STRCAT(buf, " ");
3772 }
3773 }
3774 break;
3775
3776 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003777 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778#ifdef FEAT_CSCOPE
3779 if (p_cst)
3780 STRCPY(buf, "cstag ");
3781 else
3782#endif
3783 STRCPY(buf, "ts ");
3784 break;
3785
3786 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003787 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (curbuf->b_help)
3789 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003791 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003792 if (g_cmd)
3793 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003794 else if (cap->count0 == 0)
3795 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003796 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003797 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800
3801 /*
3802 * Now grab the chars in the identifier
3803 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003804 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003806 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003807 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003808 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003809 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003810 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003811 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003812 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003813 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003814 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003816 vim_free(buf);
3817 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003819 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003820 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003821 {
3822 vim_free(buf);
3823 vim_free(p);
3824 return;
3825 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003826 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003827 STRCAT(buf, p);
3828 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003830 else
3831 {
3832 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003833 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003834 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003835 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003836 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003837 {
3838 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003839 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003840 aux_ptr = (char_u *)"";
3841 else
3842 aux_ptr = (char_u *)"\\|\"\n[";
3843 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003844 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003845 aux_ptr = (char_u *)"\\|\"\n*?[";
3846
3847 p = buf + STRLEN(buf);
3848 while (n-- > 0)
3849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003850 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003851 if (vim_strchr(aux_ptr, *ptr) != NULL)
3852 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003853 // When current byte is a part of multibyte character, copy all
3854 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003855 if (has_mbyte)
3856 {
3857 int i;
3858 int len = (*mb_ptr2len)(ptr) - 1;
3859
3860 for (i = 0; i < len && n >= 1; ++i, --n)
3861 *p++ = *ptr++;
3862 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003863 *p++ = *ptr++;
3864 }
3865 *p = NUL;
3866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867
3868 /*
3869 * Execute the command.
3870 */
3871 if (cmdchar == '*' || cmdchar == '#')
3872 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003873 if (!g_cmd && (has_mbyte
3874 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3875 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003877
3878 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003879 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003881
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003882 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 }
3884 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003885 {
3886 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003888 g_tag_at_cursor = FALSE;
3889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
3891 vim_free(buf);
3892}
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894/*
3895 * Get visually selected text, within one line only.
3896 * Returns FAIL if more than one line selected.
3897 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003898 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003899get_visual_text(
3900 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003901 char_u **pp, // return: start of selected text
3902 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
3904 if (VIsual_mode != 'V')
3905 unadjust_for_sel();
3906 if (VIsual.lnum != curwin->w_cursor.lnum)
3907 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003908 if (cap != NULL)
3909 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 return FAIL;
3911 }
3912 if (VIsual_mode == 'V')
3913 {
3914 *pp = ml_get_curline();
3915 *lenp = (int)STRLEN(*pp);
3916 }
3917 else
3918 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003919 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
3921 *pp = ml_get_pos(&curwin->w_cursor);
3922 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3923 }
3924 else
3925 {
3926 *pp = ml_get_pos(&VIsual);
3927 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3928 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003929 if (**pp == NUL)
3930 *lenp = 0;
3931 if (has_mbyte && *lenp > 0)
3932 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003933 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935 reset_VIsual_and_resel();
3936 return OK;
3937}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939/*
3940 * CTRL-T: backwards in tag stack
3941 */
3942 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003943nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944{
3945 if (!checkclearopq(cap->oap))
3946 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3947}
3948
3949/*
3950 * Handle scrolling command 'H', 'L' and 'M'.
3951 */
3952 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003953nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 int used = 0;
3956 long n;
3957#ifdef FEAT_FOLDING
3958 linenr_T lnum;
3959#endif
3960 int half;
3961
3962 cap->oap->motion_type = MLINE;
3963 setpcmark();
3964
3965 if (cap->cmdchar == 'L')
3966 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003967 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 curwin->w_cursor.lnum = curwin->w_botline - 1;
3969 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3970 curwin->w_cursor.lnum = 1;
3971 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003972 {
3973#ifdef FEAT_FOLDING
3974 if (hasAnyFolding(curwin))
3975 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003976 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003977 for (n = cap->count1 - 1; n > 0
3978 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3979 {
3980 (void)hasFolding(curwin->w_cursor.lnum,
3981 &curwin->w_cursor.lnum, NULL);
3982 --curwin->w_cursor.lnum;
3983 }
3984 }
3985 else
3986#endif
3987 curwin->w_cursor.lnum -= cap->count1 - 1;
3988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990 else
3991 {
3992 if (cap->cmdchar == 'M')
3993 {
3994#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003995 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 used -= diff_check_fill(curwin, curwin->w_topline)
3997 - curwin->w_topfill;
3998#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003999 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
4001 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
4002 {
4003#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004004 // Count half he number of filler lines to be "below this
4005 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
4007 + n) / 2 >= half)
4008 {
4009 --n;
4010 break;
4011 }
4012#endif
4013 used += plines(curwin->w_topline + n);
4014 if (used >= half)
4015 break;
4016#ifdef FEAT_FOLDING
4017 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
4018 n = lnum - curwin->w_topline;
4019#endif
4020 }
4021 if (n > 0 && used > curwin->w_height)
4022 --n;
4023 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004024 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004025 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004027#ifdef FEAT_FOLDING
4028 if (hasAnyFolding(curwin))
4029 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004030 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004031 lnum = curwin->w_topline;
4032 while (n-- > 0 && lnum < curwin->w_botline - 1)
4033 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02004034 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004035 ++lnum;
4036 }
4037 n = lnum - curwin->w_topline;
4038 }
4039#endif
4040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 curwin->w_cursor.lnum = curwin->w_topline + n;
4042 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4043 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4044 }
4045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004046 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02004047 if (cap->oap->op_type == OP_NOP)
4048 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 beginline(BL_SOL | BL_FIX);
4050}
4051
4052/*
4053 * Cursor right commands.
4054 */
4055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004056nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
4058 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004059 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004061 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4062 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004063 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004064 if (mod_mask & MOD_MASK_CTRL)
4065 cap->arg = TRUE;
4066 nv_wordcmd(cap);
4067 return;
4068 }
4069
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 cap->oap->motion_type = MCHAR;
4071 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004072 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004075 * In virtual edit mode, there's no such thing as "past_line", as lines
4076 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 */
4078 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004079 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 for (n = cap->count1; n > 0; --n)
4082 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004083 if ((!past_line && oneright() == FAIL)
4084 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004085 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
4087 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004088 * <Space> wraps to next line if 'whichwrap' has 's'.
4089 * 'l' wraps to next line if 'whichwrap' has 'l'.
4090 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 */
4092 if ( ((cap->cmdchar == ' '
4093 && vim_strchr(p_ww, 's') != NULL)
4094 || (cap->cmdchar == 'l'
4095 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004096 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 && vim_strchr(p_ww, '>') != NULL))
4098 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4099 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004100 // When deleting we also count the NL as a character.
4101 // Set cap->oap->inclusive when last char in the line is
4102 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004103 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004105 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 cap->oap->inclusive = TRUE;
4107 else
4108 {
4109 ++curwin->w_cursor.lnum;
4110 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 curwin->w_set_curswant = TRUE;
4113 cap->oap->inclusive = FALSE;
4114 }
4115 continue;
4116 }
4117 if (cap->oap->op_type == OP_NOP)
4118 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004119 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (n == cap->count1)
4121 beep_flush();
4122 }
4123 else
4124 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004125 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 cap->oap->inclusive = TRUE;
4127 }
4128 break;
4129 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004130 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
4132 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (virtual_active())
4134 oneright();
4135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004138 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 ++curwin->w_cursor.col;
4141 }
4142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144#ifdef FEAT_FOLDING
4145 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4146 && cap->oap->op_type == OP_NOP)
4147 foldOpenCursor();
4148#endif
4149}
4150
4151/*
4152 * Cursor left commands.
4153 *
4154 * Returns TRUE when operator end should not be adjusted.
4155 */
4156 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004157nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
4159 long n;
4160
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004161 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004163 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004164 if (mod_mask & MOD_MASK_CTRL)
4165 cap->arg = 1;
4166 nv_bck_word(cap);
4167 return;
4168 }
4169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 cap->oap->motion_type = MCHAR;
4171 cap->oap->inclusive = FALSE;
4172 for (n = cap->count1; n > 0; --n)
4173 {
4174 if (oneleft() == FAIL)
4175 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004176 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4177 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4178 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 if ( (((cap->cmdchar == K_BS
4180 || cap->cmdchar == Ctrl_H)
4181 && vim_strchr(p_ww, 'b') != NULL)
4182 || (cap->cmdchar == 'h'
4183 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004184 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 && vim_strchr(p_ww, '<') != NULL))
4186 && curwin->w_cursor.lnum > 1)
4187 {
4188 --(curwin->w_cursor.lnum);
4189 coladvance((colnr_T)MAXCOL);
4190 curwin->w_set_curswant = TRUE;
4191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004192 // When the NL before the first char has to be deleted we
4193 // put the cursor on the NUL after the previous line.
4194 // This is a very special case, be careful!
4195 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 if ( (cap->oap->op_type == OP_DELETE
4197 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004198 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004200 char_u *cp = ml_get_cursor();
4201
4202 if (*cp != NUL)
4203 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004204 if (has_mbyte)
4205 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4206 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004207 ++curwin->w_cursor.col;
4208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 cap->retval |= CA_NO_ADJ_OP_END;
4210 }
4211 continue;
4212 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004213 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4215 beep_flush();
4216 break;
4217 }
4218 }
4219#ifdef FEAT_FOLDING
4220 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4221 && cap->oap->op_type == OP_NOP)
4222 foldOpenCursor();
4223#endif
4224}
4225
4226/*
4227 * Cursor up commands.
4228 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4229 */
4230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004231nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004233 if (mod_mask & MOD_MASK_SHIFT)
4234 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004235 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004236 cap->arg = BACKWARD;
4237 nv_page(cap);
4238 }
4239 else
4240 {
4241 cap->oap->motion_type = MLINE;
4242 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4243 clearopbeep(cap->oap);
4244 else if (cap->arg)
4245 beginline(BL_WHITE | BL_FIX);
4246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247}
4248
4249/*
4250 * Cursor down commands.
4251 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4252 */
4253 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004254nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004256 if (mod_mask & MOD_MASK_SHIFT)
4257 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004258 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004259 cap->arg = FORWARD;
4260 nv_page(cap);
4261 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004262#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004263 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004264 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4265 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
4269#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004270 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004271 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 cmdwin_result = CAR;
4273 else
4274#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004275#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004276 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004277 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4278 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4279 {
4280 invoke_prompt_callback();
4281 if (restart_edit == 0)
4282 restart_edit = 'a';
4283 }
4284 else
4285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 {
4287 cap->oap->motion_type = MLINE;
4288 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4289 clearopbeep(cap->oap);
4290 else if (cap->arg)
4291 beginline(BL_WHITE | BL_FIX);
4292 }
4293 }
4294}
4295
4296#ifdef FEAT_SEARCHPATH
4297/*
4298 * Grab the file name under the cursor and edit it.
4299 */
4300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004301nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302{
4303 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004304 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004306 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
4308 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004309 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return;
4311 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004312 if (curbuf_locked())
4313 {
4314 clearop(cap->oap);
4315 return;
4316 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004317#ifdef FEAT_PROP_POPUP
4318 if (ERROR_IF_TERM_POPUP_WINDOW)
4319 return;
4320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004322 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 if (ptr != NULL)
4325 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004326 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004327 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004328 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004330 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004331 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004332 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004333 {
4334 curwin->w_cursor.lnum = lnum;
4335 check_cursor_lnum();
4336 beginline(BL_SOL | BL_FIX);
4337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 vim_free(ptr);
4339 }
4340 else
4341 clearop(cap->oap);
4342}
4343#endif
4344
4345/*
4346 * <End> command: to end of current line or last line.
4347 */
4348 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004349nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004351 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004353 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004355 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 nv_dollar(cap);
4358}
4359
4360/*
4361 * Handle the "$" command.
4362 */
4363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004364nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
4366 cap->oap->motion_type = MCHAR;
4367 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004368 // In virtual mode when off the edge of a line and an operator
4369 // is pending (whew!) keep the cursor where it is.
4370 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 if (!virtual_active() || gchar_cursor() != NUL
4372 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004373 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 if (cursor_down((long)(cap->count1 - 1),
4375 cap->oap->op_type == OP_NOP) == FAIL)
4376 clearopbeep(cap->oap);
4377#ifdef FEAT_FOLDING
4378 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4379 foldOpenCursor();
4380#endif
4381}
4382
4383/*
4384 * Implementation of '?' and '/' commands.
4385 * If cap->arg is TRUE don't set PC mark.
4386 */
4387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004388nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389{
4390 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004391 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4394 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004395 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 cap->cmdchar = 'g';
4397 cap->nchar = '?';
4398 nv_operator(cap);
4399 return;
4400 }
4401
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004402 // When using 'incsearch' the cursor may be moved to set a different search
4403 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004404 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 if (cap->searchbuf == NULL)
4407 {
4408 clearop(oap);
4409 return;
4410 }
4411
Bram Moolenaar46539112015-02-17 15:43:57 +01004412 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004413 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004414 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415}
4416
4417/*
4418 * Handle "N" and "n" commands.
4419 * cap->arg is SEARCH_REV for "N", 0 for "n".
4420 */
4421 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004422nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004424 pos_T old = curwin->w_cursor;
4425 int wrapped = FALSE;
4426 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004427
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004428 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004429 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004430 // Avoid getting stuck on the current cursor position, which can
4431 // happen when an offset is given and the cursor is on the last char
4432 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004433 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004434 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004435 cap->count1 -= 1;
4436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437}
4438
4439/*
4440 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4441 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004442 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004444 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004445normal_search(
4446 cmdarg_T *cap,
4447 int dir,
4448 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004449 int opt, // extra flags for do_search()
4450 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004453 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
4455 cap->oap->motion_type = MCHAR;
4456 cap->oap->inclusive = FALSE;
4457 cap->oap->use_reg_one = TRUE;
4458 curwin->w_set_curswant = TRUE;
4459
Bram Moolenaara80faa82020-04-12 19:37:17 +02004460 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004461 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004462 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4463 if (wrapped != NULL)
4464 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (i == 0)
4466 clearop(cap->oap);
4467 else
4468 {
4469 if (i == 2)
4470 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472#ifdef FEAT_FOLDING
4473 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4474 foldOpenCursor();
4475#endif
4476 }
4477
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004478 // "/$" will put the cursor after the end of the line, may need to
4479 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004481 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482}
4483
4484/*
4485 * Character search commands.
4486 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4487 * ',' and FALSE for ';'.
4488 * cap->nchar is NUL for ',' and ';' (repeat the search)
4489 */
4490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004491nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492{
4493 int t_cmd;
4494
4495 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4496 t_cmd = TRUE;
4497 else
4498 t_cmd = FALSE;
4499
4500 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4502 clearopbeep(cap->oap);
4503 else
4504 {
4505 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004506 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4508 && (t_cmd || cap->oap->op_type != OP_NOP))
4509 {
4510 colnr_T scol, ecol;
4511
4512 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4513 curwin->w_cursor.coladd = ecol - scol;
4514 }
4515 else
4516 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#ifdef FEAT_FOLDING
4519 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4520 foldOpenCursor();
4521#endif
4522 }
4523}
4524
4525/*
4526 * "[" and "]" commands.
4527 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4528 */
4529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004530nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004532 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004534 pos_T *pos = NULL; // init for GCC
4535 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 int flag;
4537 long n;
4538 int findc;
4539 int c;
4540
4541 cap->oap->motion_type = MCHAR;
4542 cap->oap->inclusive = FALSE;
4543 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004544 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545
4546#ifdef FEAT_SEARCHPATH
4547 /*
4548 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4549 */
4550 if (cap->nchar == 'f')
4551 nv_gotofile(cap);
4552 else
4553#endif
4554
4555#ifdef FEAT_FIND_ID
4556 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004557 * Find the occurrence(s) of the identifier or define under cursor
4558 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 *
4560 * search list jump
4561 * fwd bwd fwd bwd fwd bwd
4562 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4563 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4564 */
4565 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004566# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004568# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 cap->nchar) != NULL)
4572 {
4573 char_u *ptr;
4574 int len;
4575
4576 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4577 clearop(cap->oap);
4578 else
4579 {
4580 find_pattern_in_path(ptr, 0, len, TRUE,
4581 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4582 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4583 cap->count1,
4584 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4585 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4586 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4587 (linenr_T)MAXLNUM);
4588 curwin->w_set_curswant = TRUE;
4589 }
4590 }
4591 else
4592#endif
4593
4594 /*
4595 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4596 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4597 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4598 * "[m" or "]m" search for prev/next start of (Java) method.
4599 * "[M" or "]M" search for prev/next end of (Java) method.
4600 */
4601 if ( (cap->cmdchar == '['
4602 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4603 || (cap->cmdchar == ']'
4604 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4605 {
4606 if (cap->nchar == '*')
4607 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 prev_pos.lnum = 0;
4609 if (cap->nchar == 'm' || cap->nchar == 'M')
4610 {
4611 if (cap->cmdchar == '[')
4612 findc = '{';
4613 else
4614 findc = '}';
4615 n = 9999;
4616 }
4617 else
4618 {
4619 findc = cap->nchar;
4620 n = cap->count1;
4621 }
4622 for ( ; n > 0; --n)
4623 {
4624 if ((pos = findmatchlimit(cap->oap, findc,
4625 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4626 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004627 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
4629 if (cap->nchar != 'm' && cap->nchar != 'M')
4630 clearopbeep(cap->oap);
4631 }
4632 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004633 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 break;
4635 }
4636 prev_pos = new_pos;
4637 curwin->w_cursor = *pos;
4638 new_pos = *pos;
4639 }
4640 curwin->w_cursor = old_pos;
4641
4642 /*
4643 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4644 * brought us to the match for "[m" and "]M" when inside a method.
4645 * Try finding the '{' or '}' we want to be at.
4646 * Also repeat for the given count.
4647 */
4648 if (cap->nchar == 'm' || cap->nchar == 'M')
4649 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004650 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 int norm = ((findc == '{') == (cap->nchar == 'm'));
4652
4653 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004654 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (prev_pos.lnum != 0)
4656 {
4657 pos = &prev_pos;
4658 curwin->w_cursor = prev_pos;
4659 if (norm)
4660 --n;
4661 }
4662 else
4663 pos = NULL;
4664 while (n > 0)
4665 {
4666 for (;;)
4667 {
4668 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4669 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004670 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if (pos == NULL)
4672 clearopbeep(cap->oap);
4673 n = 0;
4674 break;
4675 }
4676 c = gchar_cursor();
4677 if (c == '{' || c == '}')
4678 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004679 // Must have found end/start of class: use it.
4680 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if ((c == findc && norm) || (n == 1 && !norm))
4682 {
4683 new_pos = curwin->w_cursor;
4684 pos = &new_pos;
4685 n = 0;
4686 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004687 // if no match found at all, we started outside of the
4688 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 else if (new_pos.lnum == 0)
4690 {
4691 new_pos = curwin->w_cursor;
4692 pos = &new_pos;
4693 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004694 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 else if ((pos = findmatchlimit(cap->oap, findc,
4696 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4697 0)) == NULL)
4698 n = 0;
4699 else
4700 curwin->w_cursor = *pos;
4701 break;
4702 }
4703 }
4704 --n;
4705 }
4706 curwin->w_cursor = old_pos;
4707 if (pos == NULL && new_pos.lnum != 0)
4708 clearopbeep(cap->oap);
4709 }
4710 if (pos != NULL)
4711 {
4712 setpcmark();
4713 curwin->w_cursor = *pos;
4714 curwin->w_set_curswant = TRUE;
4715#ifdef FEAT_FOLDING
4716 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4717 && cap->oap->op_type == OP_NOP)
4718 foldOpenCursor();
4719#endif
4720 }
4721 }
4722
4723 /*
4724 * "[[", "[]", "]]" and "][": move to start or end of function
4725 */
4726 else if (cap->nchar == '[' || cap->nchar == ']')
4727 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004728 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 flag = '{';
4730 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004731 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732
4733 curwin->w_set_curswant = TRUE;
4734 /*
4735 * Imitate strange Vi behaviour: When using "]]" with an operator
4736 * we also stop at '}'.
4737 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004738 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 (cap->oap->op_type != OP_NOP
4740 && cap->arg == FORWARD && flag == '{')))
4741 clearopbeep(cap->oap);
4742 else
4743 {
4744 if (cap->oap->op_type == OP_NOP)
4745 beginline(BL_WHITE | BL_FIX);
4746#ifdef FEAT_FOLDING
4747 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4748 foldOpenCursor();
4749#endif
4750 }
4751 }
4752
4753 /*
4754 * "[p", "[P", "]P" and "]p": put with indent adjustment
4755 */
4756 else if (cap->nchar == 'p' || cap->nchar == 'P')
4757 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004758 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760
4761 /*
4762 * "['", "[`", "]'" and "]`": jump to next mark
4763 */
4764 else if (cap->nchar == '\'' || cap->nchar == '`')
4765 {
4766 pos = &curwin->w_cursor;
4767 for (n = cap->count1; n > 0; --n)
4768 {
4769 prev_pos = *pos;
4770 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4771 cap->nchar == '\'');
4772 if (pos == NULL)
4773 break;
4774 }
4775 if (pos == NULL)
4776 pos = &prev_pos;
4777 nv_cursormark(cap, cap->nchar == '\'', pos);
4778 }
4779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 /*
4781 * [ or ] followed by a middle mouse click: put selected text with
4782 * indent adjustment. Any other button just does as usual.
4783 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004784 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 (void)do_mouse(cap->oap, cap->nchar,
4787 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4788 cap->count1, PUT_FIXINDENT);
4789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
4791#ifdef FEAT_FOLDING
4792 /*
4793 * "[z" and "]z": move to start or end of open fold.
4794 */
4795 else if (cap->nchar == 'z')
4796 {
4797 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4798 cap->count1) == FAIL)
4799 clearopbeep(cap->oap);
4800 }
4801#endif
4802
4803#ifdef FEAT_DIFF
4804 /*
4805 * "[c" and "]c": move to next or previous diff-change.
4806 */
4807 else if (cap->nchar == 'c')
4808 {
4809 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4810 cap->count1) == FAIL)
4811 clearopbeep(cap->oap);
4812 }
4813#endif
4814
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004815#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004816 /*
4817 * "[s", "[S", "]s" and "]S": move to next spell error.
4818 */
4819 else if (cap->nchar == 's' || cap->nchar == 'S')
4820 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004821 setpcmark();
4822 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004823 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4824 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004825 {
4826 clearopbeep(cap->oap);
4827 break;
4828 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004829 else
4830 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004831# ifdef FEAT_FOLDING
4832 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4833 foldOpenCursor();
4834# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004835 }
4836#endif
4837
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004838 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 else
4840 clearopbeep(cap->oap);
4841}
4842
4843/*
4844 * Handle Normal mode "%" command.
4845 */
4846 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004847nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
4849 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004850#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 linenr_T lnum = curwin->w_cursor.lnum;
4852#endif
4853
4854 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004855 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
4857 if (cap->count0 > 100)
4858 clearopbeep(cap->oap);
4859 else
4860 {
4861 cap->oap->motion_type = MLINE;
4862 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004863 // Round up, so 'normal 100%' always jumps at the line line.
4864 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4865 // overflow on 32-bits, so use a formula with less accuracy
4866 // to avoid overflows.
4867 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4869 / 100L * cap->count0;
4870 else
4871 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4872 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004873 if (curwin->w_cursor.lnum < 1)
4874 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4876 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4877 beginline(BL_SOL | BL_FIX);
4878 }
4879 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004880 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
4882 cap->oap->motion_type = MCHAR;
4883 cap->oap->use_reg_one = TRUE;
4884 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4885 clearopbeep(cap->oap);
4886 else
4887 {
4888 setpcmark();
4889 curwin->w_cursor = *pos;
4890 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 }
4895#ifdef FEAT_FOLDING
4896 if (cap->oap->op_type == OP_NOP
4897 && lnum != curwin->w_cursor.lnum
4898 && (fdo_flags & FDO_PERCENT)
4899 && KeyTyped)
4900 foldOpenCursor();
4901#endif
4902}
4903
4904/*
4905 * Handle "(" and ")" commands.
4906 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4907 */
4908 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004909nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
4911 cap->oap->motion_type = MCHAR;
4912 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004913 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004914 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 curwin->w_set_curswant = TRUE;
4916
4917 if (findsent(cap->arg, cap->count1) == FAIL)
4918 clearopbeep(cap->oap);
4919 else
4920 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004921 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004922 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924#ifdef FEAT_FOLDING
4925 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4926 foldOpenCursor();
4927#endif
4928 }
4929}
4930
4931/*
4932 * "m" command: Mark a position.
4933 */
4934 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004935nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 if (!checkclearop(cap->oap))
4938 {
4939 if (setmark(cap->nchar) == FAIL)
4940 clearopbeep(cap->oap);
4941 }
4942}
4943
4944/*
4945 * "{" and "}" commands.
4946 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4947 */
4948 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004949nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 cap->oap->motion_type = MCHAR;
4952 cap->oap->inclusive = FALSE;
4953 cap->oap->use_reg_one = TRUE;
4954 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004955 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 clearopbeep(cap->oap);
4957 else
4958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#ifdef FEAT_FOLDING
4961 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4962 foldOpenCursor();
4963#endif
4964 }
4965}
4966
4967/*
4968 * "u" command: Undo or make lower case.
4969 */
4970 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004971nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004973 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004975 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 cap->cmdchar = 'g';
4977 cap->nchar = 'u';
4978 nv_operator(cap);
4979 }
4980 else
4981 nv_kundo(cap);
4982}
4983
4984/*
4985 * <Undo> command.
4986 */
4987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004988nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990 if (!checkclearopq(cap->oap))
4991 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004992#ifdef FEAT_JOB_CHANNEL
4993 if (bt_prompt(curbuf))
4994 {
4995 clearopbeep(cap->oap);
4996 return;
4997 }
4998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 u_undo((int)cap->count1);
5000 curwin->w_set_curswant = TRUE;
5001 }
5002}
5003
5004/*
5005 * Handle the "r" command.
5006 */
5007 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005008nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009{
5010 char_u *ptr;
5011 int had_ctrl_v;
5012 long n;
5013
5014 if (checkclearop(cap->oap))
5015 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02005016#ifdef FEAT_JOB_CHANNEL
5017 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5018 {
5019 clearopbeep(cap->oap);
5020 return;
5021 }
5022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005024 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (cap->nchar == Ctrl_V)
5026 {
5027 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01005028 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005029 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (cap->nchar > DEL)
5031 had_ctrl_v = NUL;
5032 }
5033 else
5034 had_ctrl_v = NUL;
5035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005036 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005037 if (IS_SPECIAL(cap->nchar))
5038 {
5039 clearopbeep(cap->oap);
5040 return;
5041 }
5042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005043 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (VIsual_active)
5045 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00005046 if (got_int)
5047 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01005048 if (had_ctrl_v)
5049 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005050 // Use a special (negative) number to make a difference between a
5051 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01005052 if (cap->nchar == CAR)
5053 cap->nchar = REPLACE_CR_NCHAR;
5054 else if (cap->nchar == NL)
5055 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01005056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 nv_operator(cap);
5058 return;
5059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005061 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (virtual_active())
5063 {
5064 if (u_save_cursor() == FAIL)
5065 return;
5066 if (gchar_cursor() == NUL)
5067 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005068 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 coladvance_force((colnr_T)(getviscol() + cap->count1));
5070 curwin->w_cursor.col -= cap->count1;
5071 }
5072 else if (gchar_cursor() == TAB)
5073 coladvance_force(getviscol());
5074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005076 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005078 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005079 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 clearopbeep(cap->oap);
5082 return;
5083 }
5084
5085 /*
5086 * Replacing with a TAB is done by edit() when it is complicated because
5087 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5088 * Other characters are done below to avoid problems with things like
5089 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5090 */
5091 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5092 {
5093 stuffnumReadbuff(cap->count1);
5094 stuffcharReadbuff('R');
5095 stuffcharReadbuff('\t');
5096 stuffcharReadbuff(ESC);
5097 return;
5098 }
5099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005100 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (u_save_cursor() == FAIL)
5102 return;
5103
5104 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5105 {
5106 /*
5107 * Replace character(s) by a single newline.
5108 * Strange vi behaviour: Only one newline is inserted.
5109 * Delete the characters here.
5110 * Insert the newline with an insert command, takes care of
5111 * autoindent. The insert command depends on being on the last
5112 * character of a line or not.
5113 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005114 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 stuffcharReadbuff('\r');
5116 stuffcharReadbuff(ESC);
5117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005118 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 invoke_edit(cap, TRUE, 'r', FALSE);
5120 }
5121 else
5122 {
5123 prep_redo(cap->oap->regname, cap->count1,
5124 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5125
5126 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (has_mbyte)
5128 {
5129 int old_State = State;
5130
5131 if (cap->ncharC1 != 0)
5132 AppendCharToRedobuff(cap->ncharC1);
5133 if (cap->ncharC2 != 0)
5134 AppendCharToRedobuff(cap->ncharC2);
5135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005136 // This is slow, but it handles replacing a single-byte with a
5137 // multi-byte and the other way around. Also handles adding
5138 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 for (n = cap->count1; n > 0; --n)
5140 {
5141 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005142 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5143 {
5144 int c = ins_copychar(curwin->w_cursor.lnum
5145 + (cap->nchar == Ctrl_Y ? -1 : 1));
5146 if (c != NUL)
5147 ins_char(c);
5148 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005149 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005150 ++curwin->w_cursor.col;
5151 }
5152 else
5153 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 State = old_State;
5155 if (cap->ncharC1 != 0)
5156 ins_char(cap->ncharC1);
5157 if (cap->ncharC2 != 0)
5158 ins_char(cap->ncharC2);
5159 }
5160 }
5161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
5163 /*
5164 * Replace the characters within one line.
5165 */
5166 for (n = cap->count1; n > 0; --n)
5167 {
5168 /*
5169 * Get ptr again, because u_save and/or showmatch() will have
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005170 * released the line. This may also happen in ins_copychar().
5171 * At the same time we let know that the line will be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005173 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5174 {
5175 int c = ins_copychar(curwin->w_cursor.lnum
5176 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005177
5178 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005179 if (c != NUL)
5180 ptr[curwin->w_cursor.col] = c;
5181 }
5182 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005183 {
5184 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005185 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (p_sm && msg_silent == 0)
5188 showmatch(cap->nchar);
5189 ++curwin->w_cursor.col;
5190 }
5191#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005192 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005194 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
Bram Moolenaar009b2592004-10-24 19:18:58 +00005196 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005197 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005199 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201#endif
5202
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005203 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 changed_bytes(curwin->w_cursor.lnum,
5205 (colnr_T)(curwin->w_cursor.col - cap->count1));
5206 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005207 --curwin->w_cursor.col; // cursor on the last replaced char
5208 // if the character on the left of the current cursor is a multi-byte
5209 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (has_mbyte)
5211 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 curbuf->b_op_end = curwin->w_cursor;
5213 curwin->w_set_curswant = TRUE;
5214 set_last_insert(cap->nchar);
5215 }
5216}
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218/*
5219 * 'o': Exchange start and end of Visual area.
5220 * 'O': same, but in block mode exchange left and right corners.
5221 */
5222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005223v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 pos_T old_cursor;
5226 colnr_T left, right;
5227
5228 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5229 {
5230 old_cursor = curwin->w_cursor;
5231 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5232 curwin->w_cursor.lnum = VIsual.lnum;
5233 coladvance(left);
5234 VIsual = curwin->w_cursor;
5235
5236 curwin->w_cursor.lnum = old_cursor.lnum;
5237 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005238 // 'selection "exclusive" and cursor at right-bottom corner: move it
5239 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5241 ++curwin->w_curswant;
5242 coladvance(curwin->w_curswant);
5243 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005245 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
5247 curwin->w_cursor.lnum = VIsual.lnum;
5248 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5249 ++right;
5250 coladvance(right);
5251 VIsual = curwin->w_cursor;
5252
5253 curwin->w_cursor.lnum = old_cursor.lnum;
5254 coladvance(left);
5255 curwin->w_curswant = left;
5256 }
5257 }
5258 else
5259 {
5260 old_cursor = curwin->w_cursor;
5261 curwin->w_cursor = VIsual;
5262 VIsual = old_cursor;
5263 curwin->w_set_curswant = TRUE;
5264 }
5265}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
5267/*
5268 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5269 */
5270 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005271nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005273 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
5275 cap->cmdchar = 'c';
5276 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005277 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 VIsual_mode = 'V';
5279 nv_operator(cap);
5280 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005281 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
5283 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005284 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 else
5286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 if (virtual_active())
5288 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5290 }
5291 }
5292}
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294/*
5295 * "gr".
5296 */
5297 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005298nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (VIsual_active)
5301 {
5302 cap->cmdchar = 'r';
5303 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005304 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005306 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
5308 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005309 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 else
5311 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005312 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005313 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 stuffcharReadbuff(cap->extra_char);
5315 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 if (virtual_active())
5317 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 invoke_edit(cap, TRUE, 'v', FALSE);
5319 }
5320 }
5321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323/*
5324 * Swap case for "~" command, when it does not work like an operator.
5325 */
5326 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005327n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
5329 long n;
5330 pos_T startpos;
5331 int did_change = 0;
5332#ifdef FEAT_NETBEANS_INTG
5333 pos_T pos;
5334 char_u *ptr;
5335 int count;
5336#endif
5337
5338 if (checkclearopq(cap->oap))
5339 return;
5340
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005341 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 clearopbeep(cap->oap);
5344 return;
5345 }
5346
5347 prep_redo_cmd(cap);
5348
5349 if (u_save_cursor() == FAIL)
5350 return;
5351
5352 startpos = curwin->w_cursor;
5353#ifdef FEAT_NETBEANS_INTG
5354 pos = startpos;
5355#endif
5356 for (n = cap->count1; n > 0; --n)
5357 {
5358 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5359 inc_cursor();
5360 if (gchar_cursor() == NUL)
5361 {
5362 if (vim_strchr(p_ww, '~') != NULL
5363 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5364 {
5365#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005366 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
5368 if (did_change)
5369 {
5370 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005371 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005372 netbeans_removed(curbuf, pos.lnum, pos.col,
5373 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005375 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377 pos.col = 0;
5378 pos.lnum++;
5379 }
5380#endif
5381 ++curwin->w_cursor.lnum;
5382 curwin->w_cursor.col = 0;
5383 if (n > 1)
5384 {
5385 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5386 break;
5387 u_clearline();
5388 }
5389 }
5390 else
5391 break;
5392 }
5393 }
5394#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005395 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
5397 ptr = ml_get(pos.lnum);
5398 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005399 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5400 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402#endif
5403
5404
5405 check_cursor();
5406 curwin->w_set_curswant = TRUE;
5407 if (did_change)
5408 {
5409 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5410 0L);
5411 curbuf->b_op_start = startpos;
5412 curbuf->b_op_end = curwin->w_cursor;
5413 if (curbuf->b_op_end.col > 0)
5414 --curbuf->b_op_end.col;
5415 }
5416}
5417
5418/*
5419 * Move cursor to mark.
5420 */
5421 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005422nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 if (check_mark(pos) == FAIL)
5425 clearop(cap->oap);
5426 else
5427 {
5428 if (cap->cmdchar == '\''
5429 || cap->cmdchar == '`'
5430 || cap->cmdchar == '['
5431 || cap->cmdchar == ']')
5432 setpcmark();
5433 curwin->w_cursor = *pos;
5434 if (flag)
5435 beginline(BL_WHITE | BL_FIX);
5436 else
5437 check_cursor();
5438 }
5439 cap->oap->motion_type = flag ? MLINE : MCHAR;
5440 if (cap->cmdchar == '`')
5441 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005442 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 curwin->w_set_curswant = TRUE;
5444}
5445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446/*
5447 * Handle commands that are operators in Visual mode.
5448 */
5449 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005450v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005454 // Uppercase means linewise, except in block mode, then "D" deletes till
5455 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (isupper(cap->cmdchar))
5457 {
5458 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005459 {
5460 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5464 curwin->w_curswant = MAXCOL;
5465 }
5466 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5467 nv_operator(cap);
5468}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
5470/*
5471 * "s" and "S" commands.
5472 */
5473 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005474nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005476#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005477 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005478 if (term_swap_diff() == OK)
5479 return;
5480#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005481#ifdef FEAT_JOB_CHANNEL
5482 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5483 {
5484 clearopbeep(cap->oap);
5485 return;
5486 }
5487#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005488 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 {
5490 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005491 {
5492 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 cap->cmdchar = 'c';
5496 nv_operator(cap);
5497 }
5498 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 nv_optrans(cap);
5500}
5501
5502/*
5503 * Abbreviated commands.
5504 */
5505 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005506nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005509 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005511 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 if (VIsual_active)
5513 v_visop(cap);
5514 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 nv_optrans(cap);
5516}
5517
5518/*
5519 * Translate a command into another command.
5520 */
5521 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005522nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
5524 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5525 (char_u *)"d$", (char_u *)"c$",
5526 (char_u *)"cl", (char_u *)"cc",
5527 (char_u *)"yy", (char_u *)":s\r"};
5528 static char_u *str = (char_u *)"xXDCsSY&";
5529
5530 if (!checkclearopq(cap->oap))
5531 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005532 // In Vi "2D" doesn't delete the next line. Can't translate it
5533 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005534 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5535 {
5536 cap->oap->start = curwin->w_cursor;
5537 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005538#ifdef FEAT_EVAL
5539 set_op_var(OP_DELETE);
5540#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005541 cap->count1 = 1;
5542 nv_dollar(cap);
5543 finish_op = TRUE;
5544 ResetRedobuff();
5545 AppendCharToRedobuff('D');
5546 }
5547 else
5548 {
5549 if (cap->count0)
5550 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005551 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554 cap->opcount = 0;
5555}
5556
5557/*
5558 * "'" and "`" commands. Also for "g'" and "g`".
5559 * cap->arg is TRUE for "'" and "g'".
5560 */
5561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005562nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 pos_T *pos;
5565 int c;
5566#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005567 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005568 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569#endif
5570
5571 if (cap->cmdchar == 'g')
5572 c = cap->extra_char;
5573 else
5574 c = cap->nchar;
5575 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005576 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
5578 if (cap->arg)
5579 {
5580 check_cursor_lnum();
5581 beginline(BL_WHITE | BL_FIX);
5582 }
5583 else
5584 check_cursor();
5585 }
5586 else
5587 nv_cursormark(cap, cap->arg, pos);
5588
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005589 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 if (!virtual_active())
5591 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005592 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#ifdef FEAT_FOLDING
5594 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005595 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005596 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 && (fdo_flags & FDO_MARK)
5598 && old_KeyTyped)
5599 foldOpenCursor();
5600#endif
5601}
5602
5603/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005604 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 */
5606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005607nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005610#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005612 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
5615 if (!checkclearopq(cap->oap))
5616 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005617 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5618 {
5619 if (goto_tabpage_lastused() == FAIL)
5620 clearopbeep(cap->oap);
5621 return;
5622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 if (cap->cmdchar == 'g')
5624 pos = movechangelist((int)cap->count1);
5625 else
5626 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005627 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 {
5629 curwin->w_set_curswant = TRUE;
5630 check_cursor();
5631 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005632 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 nv_cursormark(cap, FALSE, pos);
5634 else if (cap->cmdchar == 'g')
5635 {
5636 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005637 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005639 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005641 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 }
5643 else
5644 clearopbeep(cap->oap);
5645# ifdef FEAT_FOLDING
5646 if (cap->oap->op_type == OP_NOP
5647 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5648 && (fdo_flags & FDO_MARK)
5649 && old_KeyTyped)
5650 foldOpenCursor();
5651# endif
5652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653}
5654
5655/*
5656 * Handle '"' command.
5657 */
5658 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005659nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661 if (checkclearop(cap->oap))
5662 return;
5663#ifdef FEAT_EVAL
5664 if (cap->nchar == '=')
5665 cap->nchar = get_expr_register();
5666#endif
5667 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5668 {
5669 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005670 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_EVAL
5672 set_reg_var(cap->oap->regname);
5673#endif
5674 }
5675 else
5676 clearopbeep(cap->oap);
5677}
5678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679/*
5680 * Handle "v", "V" and "CTRL-V" commands.
5681 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5682 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005683 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 */
5685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005686nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005688 if (cap->cmdchar == Ctrl_Q)
5689 cap->cmdchar = Ctrl_V;
5690
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005691 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5692 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 if (cap->oap->op_type != OP_NOP)
5694 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005695 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005696 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 return;
5698 }
5699
5700 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005701 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005703 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005705 else // toggle char/block mode
5706 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 VIsual_mode = cap->cmdchar;
5708 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005709 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005711 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005713 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
5715 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005716 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005718 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 VIsual = curwin->w_cursor;
5720
5721 VIsual_active = TRUE;
5722 VIsual_reselect = TRUE;
5723 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005724 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005727 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005728 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 /*
5730 * For V and ^V, we multiply the number of lines even if there
5731 * was only one -- webb
5732 */
5733 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5734 {
5735 curwin->w_cursor.lnum +=
5736 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005737 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739 VIsual_mode = resel_VIsual_mode;
5740 if (VIsual_mode == 'v')
5741 {
5742 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005743 {
5744 validate_virtcol();
5745 curwin->w_curswant = curwin->w_virtcol
5746 + resel_VIsual_vcol * cap->count0 - 1;
5747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005749 curwin->w_curswant = resel_VIsual_vcol;
5750 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005752 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 curwin->w_curswant = MAXCOL;
5755 coladvance((colnr_T)MAXCOL);
5756 }
5757 else if (VIsual_mode == Ctrl_V)
5758 {
5759 validate_virtcol();
5760 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005761 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 coladvance(curwin->w_curswant);
5763 }
5764 else
5765 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005766 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
5768 else
5769 {
5770 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005771 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 may_start_select('c');
5773 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005774 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005775 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005776 if (cap->count0 > 0 && --cap->count1 > 0)
5777 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005778 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005779 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5780 nv_right(cap);
5781 else if (VIsual_mode == 'V')
5782 nv_down(cap);
5783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
5785 }
5786}
5787
5788/*
5789 * Start selection for Shift-movement keys.
5790 */
5791 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005792start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005794 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 may_start_select('k');
5796 n_start_visual_mode('v');
5797}
5798
5799/*
5800 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5801 */
5802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005803may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 VIsual_select = (stuff_empty() && typebuf_typed()
5806 && (vim_strchr(p_slm, c) != NULL));
5807}
5808
5809/*
5810 * Start Visual mode "c".
5811 * Should set VIsual_select before calling this.
5812 */
5813 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005814n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005816#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005817 int cursor_line_was_concealed = curwin->w_p_cole > 0
5818 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005819#endif
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 VIsual_mode = c;
5822 VIsual_active = TRUE;
5823 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005824
5825 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005826 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005827 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005828 {
5829 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 VIsual = curwin->w_cursor;
5833
5834#ifdef FEAT_FOLDING
5835 foldAdjustVisual();
5836#endif
5837
Bram Moolenaara0620062021-11-17 16:52:40 +00005838 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005840#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005841 // Check if redraw is needed after changing the state.
5842 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005843#endif
5844
Bram Moolenaar09df3122006-01-23 22:23:09 +00005845 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005846 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005848 // Make sure the clipboard gets updated. Needed because start and
5849 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 clip_star.vmode = NUL;
5851#endif
5852
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005853 // Only need to redraw this line, unless still need to redraw an old
5854 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 if (curwin->w_redr_type < INVERTED)
5856 {
5857 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5858 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5859 }
5860}
5861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863/*
5864 * CTRL-W: Window commands
5865 */
5866 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005867nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005869 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005870 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005871 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005872 cap->cmdchar = ':';
5873 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005874 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005875 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005876 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005877 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878}
5879
5880/*
5881 * CTRL-Z: Suspend
5882 */
5883 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005884nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005888 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005889 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890}
5891
5892/*
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00005893 * "gv": Reselect the previous Visual area. If Visual already active,
5894 * exchange previous and current Visual area.
5895 */
5896 static void
5897nv_gv_cmd(cmdarg_T *cap)
5898{
5899 pos_T tpos;
5900 int i;
5901
5902 if (checkclearop(cap->oap))
5903 return;
5904
5905 if (curbuf->b_visual.vi_start.lnum == 0
5906 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5907 || curbuf->b_visual.vi_end.lnum == 0)
5908 {
5909 beep_flush();
5910 return;
5911 }
5912
5913 // set w_cursor to the start of the Visual area, tpos to the end
5914 if (VIsual_active)
5915 {
5916 i = VIsual_mode;
5917 VIsual_mode = curbuf->b_visual.vi_mode;
5918 curbuf->b_visual.vi_mode = i;
5919# ifdef FEAT_EVAL
5920 curbuf->b_visual_mode_eval = i;
5921# endif
5922 i = curwin->w_curswant;
5923 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5924 curbuf->b_visual.vi_curswant = i;
5925
5926 tpos = curbuf->b_visual.vi_end;
5927 curbuf->b_visual.vi_end = curwin->w_cursor;
5928 curwin->w_cursor = curbuf->b_visual.vi_start;
5929 curbuf->b_visual.vi_start = VIsual;
5930 }
5931 else
5932 {
5933 VIsual_mode = curbuf->b_visual.vi_mode;
5934 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5935 tpos = curbuf->b_visual.vi_end;
5936 curwin->w_cursor = curbuf->b_visual.vi_start;
5937 }
5938
5939 VIsual_active = TRUE;
5940 VIsual_reselect = TRUE;
5941
5942 // Set Visual to the start and w_cursor to the end of the Visual
5943 // area. Make sure they are on an existing character.
5944 check_cursor();
5945 VIsual = curwin->w_cursor;
5946 curwin->w_cursor = tpos;
5947 check_cursor();
5948 update_topline();
5949
5950 // When called from normal "g" command: start Select mode when
5951 // 'selectmode' contains "cmd". When called for K_SELECT, always
5952 // start Select mode.
5953 if (cap->arg)
5954 {
5955 VIsual_select = TRUE;
5956 VIsual_select_reg = 0;
5957 }
5958 else
5959 may_start_select('c');
5960 setmouse();
5961#ifdef FEAT_CLIPBOARD
5962 // Make sure the clipboard gets updated. Needed because start and
5963 // end are still the same, and the selection needs to be owned
5964 clip_star.vmode = NUL;
5965#endif
5966 redraw_curbuf_later(INVERTED);
5967 showmode();
5968}
5969
5970/*
5971 * "g0", "g^" : Like "0" and "^" but for screen lines.
5972 * "gm": middle of "g0" and "g$".
5973 */
5974 static void
5975nv_g_home_m_cmd(cmdarg_T *cap)
5976{
5977 int i;
5978 int flag = FALSE;
5979
5980 if (cap->nchar == '^')
5981 flag = TRUE;
5982
5983 cap->oap->motion_type = MCHAR;
5984 cap->oap->inclusive = FALSE;
5985 if (curwin->w_p_wrap && curwin->w_width != 0)
5986 {
5987 int width1 = curwin->w_width - curwin_col_off();
5988 int width2 = width1 + curwin_col_off2();
5989
5990 validate_virtcol();
5991 i = 0;
5992 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
5993 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
5994 }
5995 else
5996 i = curwin->w_leftcol;
5997 // Go to the middle of the screen line. When 'number' or
5998 // 'relativenumber' is on and lines are wrapping the middle can be more
5999 // to the left.
6000 if (cap->nchar == 'm')
6001 i += (curwin->w_width - curwin_col_off()
6002 + ((curwin->w_p_wrap && i > 0)
6003 ? curwin_col_off2() : 0)) / 2;
6004 coladvance((colnr_T)i);
6005 if (flag)
6006 {
6007 do
6008 i = gchar_cursor();
6009 while (VIM_ISWHITE(i) && oneright() == OK);
6010 curwin->w_valid &= ~VALID_WCOL;
6011 }
6012 curwin->w_set_curswant = TRUE;
6013}
6014
6015/*
6016 * "g_": to the last non-blank character in the line or <count> lines
6017 * downward.
6018 */
6019 static void
6020nv_g_underscore_cmd(cmdarg_T *cap)
6021{
6022 char_u *ptr;
6023
6024 cap->oap->motion_type = MCHAR;
6025 cap->oap->inclusive = TRUE;
6026 curwin->w_curswant = MAXCOL;
6027 if (cursor_down((long)(cap->count1 - 1),
6028 cap->oap->op_type == OP_NOP) == FAIL)
6029 {
6030 clearopbeep(cap->oap);
6031 return;
6032 }
6033
6034 ptr = ml_get_curline();
6035
6036 // In Visual mode we may end up after the line.
6037 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6038 --curwin->w_cursor.col;
6039
6040 // Decrease the cursor column until it's on a non-blank.
6041 while (curwin->w_cursor.col > 0
6042 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
6043 --curwin->w_cursor.col;
6044 curwin->w_set_curswant = TRUE;
6045 adjust_for_sel(cap);
6046}
6047
6048/*
6049 * "g$" : Like "$" but for screen lines.
6050 */
6051 static void
6052nv_g_dollar_cmd(cmdarg_T *cap)
6053{
6054 oparg_T *oap = cap->oap;
6055 int i;
6056 int col_off = curwin_col_off();
6057
6058 oap->motion_type = MCHAR;
6059 oap->inclusive = TRUE;
6060 if (curwin->w_p_wrap && curwin->w_width != 0)
6061 {
6062 curwin->w_curswant = MAXCOL; // so we stay at the end
6063 if (cap->count1 == 1)
6064 {
6065 int width1 = curwin->w_width - col_off;
6066 int width2 = width1 + curwin_col_off2();
6067
6068 validate_virtcol();
6069 i = width1 - 1;
6070 if (curwin->w_virtcol >= (colnr_T)width1)
6071 i += ((curwin->w_virtcol - width1) / width2 + 1)
6072 * width2;
6073 coladvance((colnr_T)i);
6074
6075 // Make sure we stick in this column.
6076 validate_virtcol();
6077 curwin->w_curswant = curwin->w_virtcol;
6078 curwin->w_set_curswant = FALSE;
6079 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6080 {
6081 // Check for landing on a character that got split at
6082 // the end of the line. We do not want to advance to
6083 // the next screen line.
6084 if (curwin->w_virtcol > (colnr_T)i)
6085 --curwin->w_cursor.col;
6086 }
6087 }
6088 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6089 clearopbeep(oap);
6090 }
6091 else
6092 {
6093 if (cap->count1 > 1)
6094 // if it fails, let the cursor still move to the last char
6095 (void)cursor_down(cap->count1 - 1, FALSE);
6096
6097 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
6098 coladvance((colnr_T)i);
6099
6100 // if the character doesn't fit move one back
6101 if (curwin->w_cursor.col > 0
6102 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6103 {
6104 colnr_T vcol;
6105
6106 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6107 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6108 --curwin->w_cursor.col;
6109 }
6110
6111 // Make sure we stick in this column.
6112 validate_virtcol();
6113 curwin->w_curswant = curwin->w_virtcol;
6114 curwin->w_set_curswant = FALSE;
6115 }
6116}
6117
6118/*
6119 * "gi": start Insert at the last position.
6120 */
6121 static void
6122nv_gi_cmd(cmdarg_T *cap)
6123{
6124 int i;
6125
6126 if (curbuf->b_last_insert.lnum != 0)
6127 {
6128 curwin->w_cursor = curbuf->b_last_insert;
6129 check_cursor_lnum();
6130 i = (int)STRLEN(ml_get_curline());
6131 if (curwin->w_cursor.col > (colnr_T)i)
6132 {
6133 if (virtual_active())
6134 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
6135 curwin->w_cursor.col = i;
6136 }
6137 }
6138 cap->cmdchar = 'i';
6139 nv_edit(cap);
6140}
6141
6142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 * Commands starting with "g".
6144 */
6145 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006146nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
6151 switch (cap->nchar)
6152 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006153 case Ctrl_A:
6154 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155#ifdef MEM_PROFILE
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006156 // "g^A": dump log of used memory.
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006157 if (!VIsual_active && cap->nchar == Ctrl_A)
6158 vim_mem_profile_dump();
6159 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160#endif
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006161 // "g^A/g^X": sequentially increment visually selected region
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006162 if (VIsual_active)
6163 {
6164 cap->arg = TRUE;
6165 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006166 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006167 nv_addsub(cap);
6168 }
6169 else
6170 clearopbeep(oap);
6171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006173 // "gR": Enter virtual replace mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 case 'R':
6175 cap->arg = TRUE;
6176 nv_Replace(cap);
6177 break;
6178
6179 case 'r':
6180 nv_vreplace(cap);
6181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182
6183 case '&':
6184 do_cmdline_cmd((char_u *)"%s//~/&");
6185 break;
6186
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006187 // "gv": Reselect the previous Visual area. If Visual already active,
6188 // exchange previous and current Visual area.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 case 'v':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006190 nv_gv_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 break;
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006192
6193 // "gV": Don't reselect the previous Visual area after a Select mode
6194 // mapping of menu.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 case 'V':
6196 VIsual_reselect = FALSE;
6197 break;
6198
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006199 // "gh": start Select mode.
6200 // "gH": start Select line mode.
6201 // "g^H": start Select block mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 case K_BS:
6203 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006204 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 case 'h':
6206 case 'H':
6207 case Ctrl_H:
6208# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006209 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 if (cap->nchar == Ctrl_H)
6211 cap->cmdchar = Ctrl_V;
6212 else
6213# endif
6214 cap->cmdchar = cap->nchar + ('v' - 'h');
6215 cap->arg = TRUE;
6216 nv_visual(cap);
6217 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006219 // "gn", "gN" visually select next/previous search match
6220 // "gn" selects next match
6221 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006222 case 'N':
6223 case 'n':
6224 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006225 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006226 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006228 // "gj" and "gk" two new funny movement keys -- up and down
6229 // movement based on *screen* line rather than *file* line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 case 'j':
6231 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006232 // with 'nowrap' it works just like the normal "j" command.
6233 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
6235 oap->motion_type = MLINE;
6236 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6237 }
6238 else
6239 i = nv_screengo(oap, FORWARD, cap->count1);
6240 if (i == FAIL)
6241 clearopbeep(oap);
6242 break;
6243
6244 case 'k':
6245 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006246 // with 'nowrap' it works just like the normal "k" command.
6247 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 {
6249 oap->motion_type = MLINE;
6250 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6251 }
6252 else
6253 i = nv_screengo(oap, BACKWARD, cap->count1);
6254 if (i == FAIL)
6255 clearopbeep(oap);
6256 break;
6257
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006258 // "gJ": join two lines without inserting a space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 case 'J':
6260 nv_join(cap);
6261 break;
6262
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006263 // "g0", "g^" : Like "0" and "^" but for screen lines.
6264 // "gm": middle of "g0" and "g$".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 case '^':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 case '0':
6267 case 'm':
6268 case K_HOME:
6269 case K_KHOME:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006270 nv_g_home_m_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 break;
6272
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006273 case 'M':
6274 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006275 oap->motion_type = MCHAR;
6276 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006277 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006278 if (cap->count0 > 0 && cap->count0 <= 100)
6279 coladvance((colnr_T)(i * cap->count0 / 100));
6280 else
6281 coladvance((colnr_T)(i / 2));
6282 curwin->w_set_curswant = TRUE;
6283 }
6284 break;
6285
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006286 // "g_": to the last non-blank character in the line or <count> lines
6287 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 case '_':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006289 nv_g_underscore_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 break;
6291
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006292 // "g$" : Like "$" but for screen lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 case '$':
6294 case K_END:
6295 case K_KEND:
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006296 nv_g_dollar_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 break;
6298
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006299 // "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 case '*':
6301 case '#':
6302#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006303 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006305 case Ctrl_RSB: // :tag or :tselect for current identifier
6306 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 nv_ident(cap);
6308 break;
6309
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006310 // ge and gE: go back to end of word
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 case 'e':
6312 case 'E':
6313 oap->motion_type = MCHAR;
6314 curwin->w_set_curswant = TRUE;
6315 oap->inclusive = TRUE;
6316 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6317 clearopbeep(oap);
6318 break;
6319
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006320 // "g CTRL-G": display info about cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006322 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 break;
6324
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006325 // "gi": start Insert at the last position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 case 'i':
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006327 nv_gi_cmd(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 break;
6329
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006330 // "gI": Start insert in column 1.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 case 'I':
6332 beginline(0);
6333 if (!checkclearopq(oap))
6334 invoke_edit(cap, FALSE, 'g', FALSE);
6335 break;
6336
6337#ifdef FEAT_SEARCHPATH
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006338 // "gf": goto file, edit file under cursor
6339 // "]f" and "[f": can also be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006341 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 nv_gotofile(cap);
6343 break;
6344#endif
6345
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006346 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 case '\'':
6348 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006349 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 case '`':
6351 nv_gomark(cap);
6352 break;
6353
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006354 // "gs": Goto sleep.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006356 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 break;
6358
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006359 // "ga": Display the ascii value of the character under the
6360 // cursor. It is displayed in decimal, hex, and octal. -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 case 'a':
6362 do_ascii(NULL);
6363 break;
6364
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006365 // "g8": Display the bytes used for the UTF-8 character under the
6366 // cursor. It is displayed in hex.
6367 // "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006369 if (cap->count0 == 8)
6370 utf_find_illegal();
6371 else
6372 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006375 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006376 case '<':
6377 show_sb_text();
6378 break;
6379
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006380 // "gg": Goto the first line in file. With a count it goes to
6381 // that line number like for "G". -- webb
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 case 'g':
6383 cap->arg = FALSE;
6384 nv_goto(cap);
6385 break;
6386
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006387 // Two-character operators:
6388 // "gq" Format text
6389 // "gw" Format text and keep cursor position
6390 // "g~" Toggle the case of the text.
6391 // "gu" Change text to lower case.
6392 // "gU" Change text to upper case.
6393 // "g?" rot13 encoding
6394 // "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 case 'q':
6396 case 'w':
6397 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006398 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 case '~':
6400 case 'u':
6401 case 'U':
6402 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006403 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 nv_operator(cap);
6405 break;
6406
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006407 // "gd": Find first occurrence of pattern under the cursor in the
6408 // current function
6409 // "gD": idem, but in the current file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 case 'd':
6411 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006412 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 break;
6414
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006415 // g<*Mouse> : <C-*mouse>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 case K_MIDDLEMOUSE:
6417 case K_MIDDLEDRAG:
6418 case K_MIDDLERELEASE:
6419 case K_LEFTMOUSE:
6420 case K_LEFTDRAG:
6421 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006422 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 case K_RIGHTMOUSE:
6424 case K_RIGHTDRAG:
6425 case K_RIGHTRELEASE:
6426 case K_X1MOUSE:
6427 case K_X1DRAG:
6428 case K_X1RELEASE:
6429 case K_X2MOUSE:
6430 case K_X2DRAG:
6431 case K_X2RELEASE:
6432 mod_mask = MOD_MASK_CTRL;
6433 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435
6436 case K_IGNORE:
6437 break;
6438
Yegappan Lakshmanan05386ca2022-01-20 20:18:27 +00006439 // "gP" and "gp": same as "P" and "p" but leave cursor just after new text
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 case 'p':
6441 case 'P':
6442 nv_put(cap);
6443 break;
6444
6445#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006446 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 case 'o':
6448 goto_byte(cap->count0);
6449 break;
6450#endif
6451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006452 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006454 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 {
6456 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006457 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 break;
6459 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006460
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (!checkclearopq(oap))
6462 do_exmode(TRUE);
6463 break;
6464
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 case ',':
6466 nv_pcmark(cap);
6467 break;
6468
6469 case ';':
6470 cap->count1 = -cap->count1;
6471 nv_pcmark(cap);
6472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006474 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006475 if (!checkclearop(oap))
6476 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006478 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006479 if (!checkclearop(oap))
6480 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006481 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006482
Bram Moolenaar62a23252020-08-09 14:04:42 +02006483 case TAB:
6484 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6485 clearopbeep(oap);
6486 break;
6487
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006488 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006489 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006490 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006491 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006492 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006493 break;
6494
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 default:
6496 clearopbeep(oap);
6497 break;
6498 }
6499}
6500
6501/*
6502 * Handle "o" and "O" commands.
6503 */
6504 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006505n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006507#ifdef FEAT_CONCEAL
6508 linenr_T oldline = curwin->w_cursor.lnum;
6509#endif
6510
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 if (!checkclearopq(cap->oap))
6512 {
6513#ifdef FEAT_FOLDING
6514 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006515 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 (void)hasFolding(curwin->w_cursor.lnum,
6517 &curwin->w_cursor.lnum, NULL);
6518 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006519 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 (void)hasFolding(curwin->w_cursor.lnum,
6521 NULL, &curwin->w_cursor.lnum);
6522#endif
6523 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6524 (cap->cmdchar == 'O' ? 1 : 0)),
6525 (linenr_T)(curwin->w_cursor.lnum +
6526 (cap->cmdchar == 'o' ? 1 : 0))
6527 ) == OK
6528 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006529 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006530 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006532#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006533 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006534 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006535#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006536#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006537 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006538 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006539 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006540#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006541 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006542 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6543 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6545 }
6546 }
6547}
6548
6549/*
6550 * "." command: redo last change.
6551 */
6552 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006553nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
6555 if (!checkclearopq(cap->oap))
6556 {
6557 /*
6558 * If "restart_edit" is TRUE, the last but one command is repeated
6559 * instead of the last command (inserting text). This is used for
6560 * CTRL-O <.> in insert mode.
6561 */
6562 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6563 clearopbeep(cap->oap);
6564 }
6565}
6566
6567/*
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006568 * CTRL-R: undo undo or specify register in select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 */
6570 static void
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006571nv_redo_or_register(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006573 if (VIsual_select && VIsual_active)
6574 {
6575 int reg;
6576 // Get register name
6577 ++no_mapping;
6578 ++allow_keys;
6579 reg = plain_vgetc();
6580 LANGMAP_ADJUST(reg, TRUE);
6581 --no_mapping;
6582 --allow_keys;
6583
6584 if (reg == '"')
6585 // the unnamed register is 0
6586 reg = 0;
6587
6588 VIsual_select_reg = valid_yank_reg(reg, TRUE) ? reg : 0;
6589 return;
6590 }
6591
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 if (!checkclearopq(cap->oap))
6593 {
6594 u_redo((int)cap->count1);
6595 curwin->w_set_curswant = TRUE;
6596 }
6597}
6598
6599/*
6600 * Handle "U" command.
6601 */
6602 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006603nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006605 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006606 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006608 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 cap->cmdchar = 'g';
6610 cap->nchar = 'U';
6611 nv_operator(cap);
6612 }
6613 else if (!checkclearopq(cap->oap))
6614 {
6615 u_undoline();
6616 curwin->w_set_curswant = TRUE;
6617 }
6618}
6619
6620/*
6621 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6622 * single character.
6623 */
6624 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006625nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006627 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006628 {
6629#ifdef FEAT_JOB_CHANNEL
6630 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6631 {
6632 clearopbeep(cap->oap);
6633 return;
6634 }
6635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 else
6639 nv_operator(cap);
6640}
6641
6642/*
6643 * Handle an operator command.
6644 * The actual work is done by do_pending_operator().
6645 */
6646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006647nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 int op_type;
6650
6651 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006652#ifdef FEAT_JOB_CHANNEL
6653 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6654 {
6655 clearopbeep(cap->oap);
6656 return;
6657 }
6658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006660 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 nv_lineop(cap);
6662 else if (!checkclearop(cap->oap))
6663 {
6664 cap->oap->start = curwin->w_cursor;
6665 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006666#ifdef FEAT_EVAL
6667 set_op_var(op_type);
6668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 }
6670}
6671
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006672#ifdef FEAT_EVAL
6673/*
6674 * Set v:operator to the characters for "optype".
6675 */
6676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006677set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006678{
6679 char_u opchars[3];
6680
6681 if (optype == OP_NOP)
6682 set_vim_var_string(VV_OP, NULL, 0);
6683 else
6684 {
6685 opchars[0] = get_op_char(optype);
6686 opchars[1] = get_extra_op_char(optype);
6687 opchars[2] = NUL;
6688 set_vim_var_string(VV_OP, opchars, -1);
6689 }
6690}
6691#endif
6692
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693/*
6694 * Handle linewise operator "dd", "yy", etc.
6695 *
6696 * "_" is is a strange motion command that helps make operators more logical.
6697 * It is actually implemented, but not documented in the real Vi. This motion
6698 * command actually refers to "the current line". Commands like "dd" and "yy"
6699 * are really an alternate form of "d_" and "y_". It does accept a count, so
6700 * "d3_" works to delete 3 lines.
6701 */
6702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006703nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
6705 cap->oap->motion_type = MLINE;
6706 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6707 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006708 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006709 && cap->oap->motion_force != 'v'
6710 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 || cap->oap->op_type == OP_LSHIFT
6712 || cap->oap->op_type == OP_RSHIFT)
6713 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006714 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 beginline(BL_WHITE | BL_FIX);
6716}
6717
6718/*
6719 * <Home> command.
6720 */
6721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006722nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006724 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006725 if (mod_mask & MOD_MASK_CTRL)
6726 nv_goto(cap);
6727 else
6728 {
6729 cap->count0 = 1;
6730 nv_pipe(cap);
6731 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006732 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6733 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734}
6735
6736/*
6737 * "|" command.
6738 */
6739 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006740nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742 cap->oap->motion_type = MCHAR;
6743 cap->oap->inclusive = FALSE;
6744 beginline(0);
6745 if (cap->count0 > 0)
6746 {
6747 coladvance((colnr_T)(cap->count0 - 1));
6748 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6749 }
6750 else
6751 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006752 // keep curswant at the column where we wanted to go, not where
6753 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 curwin->w_set_curswant = FALSE;
6755}
6756
6757/*
6758 * Handle back-word command "b" and "B".
6759 * cap->arg is 1 for "B"
6760 */
6761 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006762nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
6764 cap->oap->motion_type = MCHAR;
6765 cap->oap->inclusive = FALSE;
6766 curwin->w_set_curswant = TRUE;
6767 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6768 clearopbeep(cap->oap);
6769#ifdef FEAT_FOLDING
6770 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6771 foldOpenCursor();
6772#endif
6773}
6774
6775/*
6776 * Handle word motion commands "e", "E", "w" and "W".
6777 * cap->arg is TRUE for "E" and "W".
6778 */
6779 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006780nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781{
6782 int n;
6783 int word_end;
6784 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006785 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787 /*
6788 * Set inclusive for the "E" and "e" command.
6789 */
6790 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6791 word_end = TRUE;
6792 else
6793 word_end = FALSE;
6794 cap->oap->inclusive = word_end;
6795
6796 /*
6797 * "cw" and "cW" are a special case.
6798 */
6799 if (!word_end && cap->oap->op_type == OP_CHANGE)
6800 {
6801 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006802 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006804 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {
6806 /*
6807 * Reproduce a funny Vi behaviour: "cw" on a blank only
6808 * changes one character, not all blanks until the start of
6809 * the next word. Only do this when the 'w' flag is included
6810 * in 'cpoptions'.
6811 */
6812 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6813 {
6814 cap->oap->inclusive = TRUE;
6815 cap->oap->motion_type = MCHAR;
6816 return;
6817 }
6818 }
6819 else
6820 {
6821 /*
6822 * This is a little strange. To match what the real Vi does,
6823 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6824 * that we are not on a space or a TAB. This seems impolite
6825 * at first, but it's really more what we mean when we say
6826 * 'cw'.
6827 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006828 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 * will change only one character! This is done by setting
6830 * flag.
6831 */
6832 cap->oap->inclusive = TRUE;
6833 word_end = TRUE;
6834 flag = TRUE;
6835 }
6836 }
6837 }
6838
6839 cap->oap->motion_type = MCHAR;
6840 curwin->w_set_curswant = TRUE;
6841 if (word_end)
6842 n = end_word(cap->count1, cap->arg, flag, FALSE);
6843 else
6844 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006846 // Don't leave the cursor on the NUL past the end of line. Unless we
6847 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006848 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006849 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850
6851 if (n == FAIL && cap->oap->op_type == OP_NOP)
6852 clearopbeep(cap->oap);
6853 else
6854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856#ifdef FEAT_FOLDING
6857 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6858 foldOpenCursor();
6859#endif
6860 }
6861}
6862
6863/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006864 * Used after a movement command: If the cursor ends up on the NUL after the
6865 * end of the line, may move it back to the last character and make the motion
6866 * inclusive.
6867 */
6868 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006869adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006870{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006871 // The cursor cannot remain on the NUL when:
6872 // - the column is > 0
6873 // - not in Visual mode or 'selection' is "o"
6874 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006875 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006876 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006877 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006878 {
6879 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006880 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006881 if (has_mbyte)
6882 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006883 oap->inclusive = TRUE;
6884 }
6885}
6886
6887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 * "0" and "^" commands.
6889 * cap->arg is the argument for beginline().
6890 */
6891 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006892nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 cap->oap->motion_type = MCHAR;
6895 cap->oap->inclusive = FALSE;
6896 beginline(cap->arg);
6897#ifdef FEAT_FOLDING
6898 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6899 foldOpenCursor();
6900#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006901 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6902 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903}
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905/*
6906 * In exclusive Visual mode, may include the last character.
6907 */
6908 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006909adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910{
6911 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006912 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (has_mbyte)
6915 inc_cursor();
6916 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 ++curwin->w_cursor.col;
6918 cap->oap->inclusive = FALSE;
6919 }
6920}
6921
6922/*
6923 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6924 * Should check VIsual_mode before calling this.
6925 * Returns TRUE when backed up to the previous line.
6926 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006927 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006928unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 pos_T *pp;
6931
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006932 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006934 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 pp = &curwin->w_cursor;
6936 else
6937 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 if (pp->coladd > 0)
6939 --pp->coladd;
6940 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 if (pp->col > 0)
6942 {
6943 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006944 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 }
6946 else if (pp->lnum > 1)
6947 {
6948 --pp->lnum;
6949 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6950 return TRUE;
6951 }
6952 }
6953 return FALSE;
6954}
6955
6956/*
6957 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6958 */
6959 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006960nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
6962 if (VIsual_active)
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 VIsual_select = TRUE;
Shougo Matsushita4ede01f2022-01-20 15:26:03 +00006965 VIsual_select_reg = 0;
6966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 else if (VIsual_reselect)
6968 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006969 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 cap->arg = TRUE;
6971 nv_g_cmd(cap);
6972 }
6973}
6974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
6976/*
6977 * "G", "gg", CTRL-END, CTRL-HOME.
6978 * cap->arg is TRUE for "G".
6979 */
6980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006981nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982{
6983 linenr_T lnum;
6984
6985 if (cap->arg)
6986 lnum = curbuf->b_ml.ml_line_count;
6987 else
6988 lnum = 1L;
6989 cap->oap->motion_type = MLINE;
6990 setpcmark();
6991
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006992 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (cap->count0 != 0)
6994 lnum = cap->count0;
6995 if (lnum < 1L)
6996 lnum = 1L;
6997 else if (lnum > curbuf->b_ml.ml_line_count)
6998 lnum = curbuf->b_ml.ml_line_count;
6999 curwin->w_cursor.lnum = lnum;
7000 beginline(BL_SOL | BL_FIX);
7001#ifdef FEAT_FOLDING
7002 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
7003 foldOpenCursor();
7004#endif
7005}
7006
7007/*
7008 * CTRL-\ in Normal mode.
7009 */
7010 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007011nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012{
7013 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
7014 {
7015 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00007016 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007017 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 restart_edit = 0;
7019#ifdef FEAT_CMDWIN
7020 if (cmdwin_type != 0)
7021 cmdwin_result = Ctrl_C;
7022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 if (VIsual_active)
7024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007025 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 redraw_curbuf_later(INVERTED);
7027 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007028 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 if (cap->nchar == Ctrl_G && p_im)
7030 restart_edit = 'a';
7031 }
7032 else
7033 clearopbeep(cap->oap);
7034}
7035
7036/*
7037 * ESC in Normal mode: beep, but don't flush buffers.
7038 * Don't even beep if we are canceling a command.
7039 */
7040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007041nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 int no_reason;
7044
7045 no_reason = (cap->oap->op_type == OP_NOP
7046 && cap->opcount == 0
7047 && cap->count0 == 0
7048 && cap->oap->regname == 0
7049 && !p_im);
7050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {
7053 if (restart_edit == 0
7054#ifdef FEAT_CMDWIN
7055 && cmdwin_type == 0
7056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007059 {
7060 if (anyBufIsChanged())
7061 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
7062 else
7063 msg(_("Type :qa and press <Enter> to exit Vim"));
7064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007066 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
7067 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 if (!p_im)
7069 restart_edit = 0;
7070#ifdef FEAT_CMDWIN
7071 if (cmdwin_type != 0)
7072 {
7073 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007074 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 return;
7076 }
7077#endif
7078 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01007079#ifdef FEAT_CMDWIN
7080 else if (cmdwin_type != 0 && ex_normal_busy)
7081 {
7082 // When :normal runs out of characters while in the command line window
7083 // vgetorpeek() will return ESC. Exit the cmdline window to break the
7084 // loop.
7085 cmdwin_result = K_IGNORE;
7086 return;
7087 }
7088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 if (VIsual_active)
7091 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007092 end_visual_mode(); // stop Visual
7093 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 curwin->w_set_curswant = TRUE;
7095 redraw_curbuf_later(INVERTED);
7096 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007097 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007098 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 clearop(cap->oap);
7100
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007101 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7102 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007103 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 restart_edit = 'a';
7105}
7106
7107/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007108 * Move the cursor for the "A" command.
7109 */
7110 void
7111set_cursor_for_append_to_line(void)
7112{
7113 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007114 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007115 {
7116 int save_State = State;
7117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007118 // Pretend Insert mode here to allow the cursor on the
7119 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007120 State = INSERT;
7121 coladvance((colnr_T)MAXCOL);
7122 State = save_State;
7123 }
7124 else
7125 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7126}
7127
7128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007130 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 */
7132 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007133nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007135 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7137 cap->cmdchar = 'i';
7138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007139 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007141 {
7142#ifdef FEAT_TERMINAL
7143 if (term_in_normal_mode())
7144 {
7145 end_visual_mode();
7146 clearop(cap->oap);
7147 term_enter_job_mode();
7148 return;
7149 }
7150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007154 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007155 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7156 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {
7158#ifdef FEAT_TEXTOBJ
7159 nv_object(cap);
7160#else
7161 clearopbeep(cap->oap);
7162#endif
7163 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007164#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007165 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007166 {
7167 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007168 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007169 return;
7170 }
7171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 else if (!curbuf->b_p_ma && !p_im)
7173 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007174 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007175 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007177 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007178 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007179 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007181 else if (cap->cmdchar == K_PS && VIsual_active)
7182 {
7183 pos_T old_pos = curwin->w_cursor;
7184 pos_T old_visual = VIsual;
7185
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007186 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007187 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7188 {
7189 shift_delete_registers();
7190 cap->oap->regname = '1';
7191 }
7192 else
7193 cap->oap->regname = '-';
7194 cap->cmdchar = 'd';
7195 cap->nchar = NUL;
7196 nv_operator(cap);
7197 do_pending_operator(cap, 0, FALSE);
7198 cap->cmdchar = K_PS;
7199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007200 // When the last char in the line was deleted then append. Detect this
7201 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007202 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7203 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007204 inc_cursor();
7205
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007206 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007207 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 else if (!checkclearopq(cap->oap))
7210 {
7211 switch (cap->cmdchar)
7212 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007213 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007214 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 break;
7216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007217 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007218 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7219 beginline(BL_WHITE);
7220 else
7221 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 break;
7223
Bram Moolenaara1891842017-02-04 21:34:31 +01007224 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007225 // Bracketed paste works like "a"ppend, unless the cursor is in
7226 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007227 if (curwin->w_cursor.col == 0)
7228 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007229 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007230
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007231 case 'a': // "a"ppend is like "i"nsert on the next character.
7232 // increment coladd when in virtual space, increment the
7233 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 if (virtual_active()
7235 && (curwin->w_cursor.coladd > 0
7236 || *ml_get_cursor() == NUL
7237 || *ml_get_cursor() == TAB))
7238 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007239 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 inc_cursor();
7241 break;
7242 }
7243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7245 {
7246 int save_State = State;
7247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007248 // Pretend Insert mode here to allow the cursor on the
7249 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 State = INSERT;
7251 coladvance(getviscol());
7252 State = save_State;
7253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
7255 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7256 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007257 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007258 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007259 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260}
7261
7262/*
7263 * Invoke edit() and take care of "restart_edit" and the return value.
7264 */
7265 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007266invoke_edit(
7267 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007268 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007269 int cmd,
7270 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 int restart_edit_save = 0;
7273
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007274 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7275 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7276 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 if (repl || !stuff_empty())
7278 restart_edit_save = restart_edit;
7279 else
7280 restart_edit_save = 0;
7281
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007282 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 restart_edit = 0;
7284
7285 if (edit(cmd, startln, cap->count1))
7286 cap->retval |= CA_COMMAND_BUSY;
7287
7288 if (restart_edit == 0)
7289 restart_edit = restart_edit_save;
7290}
7291
7292#ifdef FEAT_TEXTOBJ
7293/*
7294 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7295 */
7296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007297nv_object(
7298 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
7300 int flag;
7301 int include;
7302 char_u *mps_save;
7303
7304 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007305 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007307 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007309 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 mps_save = curbuf->b_p_mps;
7311 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7312
7313 switch (cap->nchar)
7314 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007315 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 flag = current_word(cap->oap, cap->count1, include, FALSE);
7317 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007318 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 flag = current_word(cap->oap, cap->count1, include, TRUE);
7320 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007321 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 case '(':
7323 case ')':
7324 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7325 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007326 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 case '{':
7328 case '}':
7329 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7330 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007331 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 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 't': // "at" = a tag block (xml and html)
7340 // Do not adjust oap->end in do_pending_operator()
7341 // otherwise there are different results for 'dit'
7342 // (note leading whitespace in last line):
7343 // 1) <b> 2) <b>
7344 // foobar foobar
7345 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007346 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007347 flag = current_tagblock(cap->oap, cap->count1, include);
7348 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007349 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 flag = current_par(cap->oap, cap->count1, include, 'p');
7351 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007352 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 flag = current_sent(cap->oap, cap->count1, include);
7354 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007355 case '"': // "a"" = a double quoted string
7356 case '\'': // "a'" = a single quoted string
7357 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007358 flag = current_quote(cap->oap, cap->count1, include,
7359 cap->nchar);
7360 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007361#if 0 // TODO
7362 case 'S': // "aS" = a section
7363 case 'f': // "af" = a filename
7364 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365#endif
7366 default:
7367 flag = FAIL;
7368 break;
7369 }
7370
7371 curbuf->b_p_mps = mps_save;
7372 if (flag == FAIL)
7373 clearopbeep(cap->oap);
7374 adjust_cursor_col();
7375 curwin->w_set_curswant = TRUE;
7376}
7377#endif
7378
7379/*
7380 * "q" command: Start/stop recording.
7381 * "q:", "q/", "q?": edit command-line in command-line window.
7382 */
7383 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007384nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
7386 if (cap->oap->op_type == OP_FORMAT)
7387 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007388 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 cap->cmdchar = 'g';
7390 cap->nchar = 'q';
7391 nv_operator(cap);
7392 }
7393 else if (!checkclearop(cap->oap))
7394 {
7395#ifdef FEAT_CMDWIN
7396 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7397 {
7398 stuffcharReadbuff(cap->nchar);
7399 stuffcharReadbuff(K_CMDWIN);
7400 }
7401 else
7402#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007403 // (stop) recording into a named register, unless executing a
7404 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007405 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 clearopbeep(cap->oap);
7407 }
7408}
7409
7410/*
7411 * Handle the "@r" command.
7412 */
7413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007414nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 if (checkclearop(cap->oap))
7417 return;
7418#ifdef FEAT_EVAL
7419 if (cap->nchar == '=')
7420 {
7421 if (get_expr_register() == NUL)
7422 return;
7423 }
7424#endif
7425 while (cap->count1-- && !got_int)
7426 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007427 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 {
7429 clearopbeep(cap->oap);
7430 break;
7431 }
7432 line_breakcheck();
7433 }
7434}
7435
7436/*
7437 * Handle the CTRL-U and CTRL-D commands.
7438 */
7439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007440nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
7442 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7443 || (cap->cmdchar == Ctrl_D
7444 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7445 clearopbeep(cap->oap);
7446 else if (!checkclearop(cap->oap))
7447 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7448}
7449
7450/*
7451 * Handle "J" or "gJ" command.
7452 */
7453 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007454nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007456 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007458 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 {
7460 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007461 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7463 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007465 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007466 if (cap->count0 <= 2)
7467 {
7468 clearopbeep(cap->oap);
7469 return;
7470 }
7471 cap->count0 = curbuf->b_ml.ml_line_count
7472 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007474
7475 prep_redo(cap->oap->regname, cap->count0,
7476 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7477 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
7479}
7480
7481/*
7482 * "P", "gP", "p" and "gp" commands.
7483 */
7484 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007485nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007487 nv_put_opt(cap, FALSE);
7488}
7489
7490/*
7491 * "P", "gP", "p" and "gp" commands.
7492 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7493 */
7494 static void
7495nv_put_opt(cmdarg_T *cap, int fix_indent)
7496{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 int regname = 0;
7498 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007499 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007500 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 int dir;
7502 int flags = 0;
7503
7504 if (cap->oap->op_type != OP_NOP)
7505 {
7506#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007507 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7509 {
7510 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007511 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
7513 else
7514#endif
7515 clearopbeep(cap->oap);
7516 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007517#ifdef FEAT_JOB_CHANNEL
7518 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7519 {
7520 clearopbeep(cap->oap);
7521 }
7522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 else
7524 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007525 if (fix_indent)
7526 {
7527 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7528 ? FORWARD : BACKWARD;
7529 flags |= PUT_FIXINDENT;
7530 }
7531 else
7532 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007533 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7534 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 prep_redo_cmd(cap);
7536 if (cap->cmdchar == 'g')
7537 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007538 else if (cap->cmdchar == 'z')
7539 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 if (VIsual_active)
7542 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007543 // Putting in Visual mode: The put text replaces the selected
7544 // text. First delete the selected text, then put the new text.
7545 // Need to save and restore the registers that the delete
7546 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007547 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007549#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007551#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007552 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007553 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007554#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
7558 )
7559 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007560 // The delete is going to overwrite the register we want to
7561 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 reg1 = get_register(regname, TRUE);
7563 }
7564
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007565 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 cap->cmdchar = 'd';
7567 cap->nchar = NUL;
7568 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007569 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 nv_operator(cap);
7571 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007572 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007573 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007575 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 cap->oap->regname = regname;
7577
7578 if (reg1 != NULL)
7579 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007580 // Delete probably changed the register we want to put, save
7581 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 reg2 = get_register(regname, FALSE);
7583 put_register(regname, reg1);
7584 }
7585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007586 // When deleted a linewise Visual area, put the register as
7587 // lines to avoid it joined with the next line. When deletion was
7588 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 if (VIsual_mode == 'V')
7590 flags |= PUT_LINE;
7591 else if (VIsual_mode == 'v')
7592 flags |= PUT_LINE_SPLIT;
7593 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7594 flags |= PUT_LINE_FORWARD;
7595 dir = BACKWARD;
7596 if ((VIsual_mode != 'V'
7597 && curwin->w_cursor.col < curbuf->b_op_start.col)
7598 || (VIsual_mode == 'V'
7599 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007600 // cursor is at the end of the line or end of file, put
7601 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007603 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007604 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007606 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007608 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 if (reg2 != NULL)
7610 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007611
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007612 // What to reselect with "gv"? Selecting the just put text seems to
7613 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007614 if (was_visual)
7615 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007616 curbuf->b_visual.vi_start = curbuf->b_op_start;
7617 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007618 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007619 if (*p_sel == 'e')
7620 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007621 }
7622
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007623 // When all lines were selected and deleted do_put() leaves an empty
7624 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007625 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007626 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007627 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007628 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007629
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007630 // If the cursor was in that line, move it to the end of the last
7631 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007632 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7633 {
7634 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7635 coladvance((colnr_T)MAXCOL);
7636 }
7637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 auto_format(FALSE, TRUE);
7639 }
7640}
7641
7642/*
7643 * "o" and "O" commands.
7644 */
7645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007646nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007649 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7651 {
7652 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007653 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655 else
7656#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007657 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007659#ifdef FEAT_JOB_CHANNEL
7660 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007661 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 n_opencmd(cap);
7665}
7666
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667#ifdef FEAT_NETBEANS_INTG
7668 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007669nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670{
7671 netbeans_keycommand(cap->nchar);
7672}
7673#endif
7674
7675#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007677nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007679 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680}
7681#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007682
Bram Moolenaar3918c952005-03-15 22:34:55 +00007683/*
7684 * Trigger CursorHold event.
7685 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7686 * input buffer. "did_cursorhold" is set to avoid retriggering.
7687 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007689nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007690{
7691 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7692 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007693 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007694}