blob: eafd1fddb628fdf2412fd4669ebedaef8bf4d34f [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);
92static void nv_redo(cmdarg_T *cap);
93static 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
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000131static char *e_noident = N_("E349: No identifier under cursor");
132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Function to be called for a Normal or Visual mode command.
135 * The argument is a cmdarg_T.
136 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100137typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100139// Values for cmd_flags.
140#define NV_NCH 0x01 // may need to get a second char
141#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
142#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
143#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145#define NV_SS 0x10 // may start selection
146#define NV_SSS 0x20 // may start selection with shift modifier
147#define NV_STS 0x40 // may stop selection without shift modif.
148#define NV_RL 0x80 // 'rightleft' modifies command
149#define NV_KEEPREG 0x100 // don't clear regname
150#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152/*
153 * Generally speaking, every Normal mode command should either clear any
154 * pending operator (with *clearop*()), or set the motion type variable
155 * oap->motion_type.
156 *
157 * When a cursor motion command is made, it is marked as being a character or
158 * line oriented motion. Then, if an operator is in effect, the operation
159 * becomes character or line oriented accordingly.
160 */
161
162/*
163 * This table contains one entry for every Normal or Visual mode command.
164 * The order doesn't matter, init_normal_cmds() will create a sorted index.
165 * It is faster when all keys from zero to '~' are present.
166 */
167static const struct nv_cmd
168{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100169 int cmd_char; // (first) command character
170 nv_func_T cmd_func; // function for this command
171 short_u cmd_flags; // NV_ flags
172 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173} nv_cmds[] =
174{
175 {NUL, nv_error, 0, 0},
176 {Ctrl_A, nv_addsub, 0, 0},
177 {Ctrl_B, nv_page, NV_STS, BACKWARD},
178 {Ctrl_C, nv_esc, 0, TRUE},
179 {Ctrl_D, nv_halfpage, 0, 0},
180 {Ctrl_E, nv_scroll_line, 0, TRUE},
181 {Ctrl_F, nv_page, NV_STS, FORWARD},
182 {Ctrl_G, nv_ctrlg, 0, 0},
183 {Ctrl_H, nv_ctrlh, 0, 0},
184 {Ctrl_I, nv_pcmark, 0, 0},
185 {NL, nv_down, 0, FALSE},
186 {Ctrl_K, nv_error, 0, 0},
187 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100188 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 {Ctrl_N, nv_down, NV_STS, FALSE},
190 {Ctrl_O, nv_ctrlo, 0, 0},
191 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000192 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 {Ctrl_R, nv_redo, 0, 0},
194 {Ctrl_S, nv_ignore, 0, 0},
195 {Ctrl_T, nv_tagpop, NV_NCW, 0},
196 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 {Ctrl_V, nv_visual, 0, FALSE},
198 {'V', nv_visual, 0, FALSE},
199 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 {Ctrl_W, nv_window, 0, 0},
201 {Ctrl_X, nv_addsub, 0, 0},
202 {Ctrl_Y, nv_scroll_line, 0, FALSE},
203 {Ctrl_Z, nv_suspend, 0, 0},
204 {ESC, nv_esc, 0, FALSE},
205 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
206 {Ctrl_RSB, nv_ident, NV_NCW, 0},
207 {Ctrl_HAT, nv_hat, NV_NCW, 0},
208 {Ctrl__, nv_error, 0, 0},
209 {' ', nv_right, 0, 0},
210 {'!', nv_operator, 0, 0},
211 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
212 {'#', nv_ident, 0, 0},
213 {'$', nv_dollar, 0, 0},
214 {'%', nv_percent, 0, 0},
215 {'&', nv_optrans, 0, 0},
216 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
217 {'(', nv_brace, 0, BACKWARD},
218 {')', nv_brace, 0, FORWARD},
219 {'*', nv_ident, 0, 0},
220 {'+', nv_down, 0, TRUE},
221 {',', nv_csearch, 0, TRUE},
222 {'-', nv_up, 0, TRUE},
223 {'.', nv_dot, NV_KEEPREG, 0},
224 {'/', nv_search, 0, FALSE},
225 {'0', nv_beginline, 0, 0},
226 {'1', nv_ignore, 0, 0},
227 {'2', nv_ignore, 0, 0},
228 {'3', nv_ignore, 0, 0},
229 {'4', nv_ignore, 0, 0},
230 {'5', nv_ignore, 0, 0},
231 {'6', nv_ignore, 0, 0},
232 {'7', nv_ignore, 0, 0},
233 {'8', nv_ignore, 0, 0},
234 {'9', nv_ignore, 0, 0},
235 {':', nv_colon, 0, 0},
236 {';', nv_csearch, 0, FALSE},
237 {'<', nv_operator, NV_RL, 0},
238 {'=', nv_operator, 0, 0},
239 {'>', nv_operator, NV_RL, 0},
240 {'?', nv_search, 0, FALSE},
241 {'@', nv_at, NV_NCH_NOP, FALSE},
242 {'A', nv_edit, 0, 0},
243 {'B', nv_bck_word, 0, 1},
244 {'C', nv_abbrev, NV_KEEPREG, 0},
245 {'D', nv_abbrev, NV_KEEPREG, 0},
246 {'E', nv_wordcmd, 0, TRUE},
247 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
248 {'G', nv_goto, 0, TRUE},
249 {'H', nv_scroll, 0, 0},
250 {'I', nv_edit, 0, 0},
251 {'J', nv_join, 0, 0},
252 {'K', nv_ident, 0, 0},
253 {'L', nv_scroll, 0, 0},
254 {'M', nv_scroll, 0, 0},
255 {'N', nv_next, 0, SEARCH_REV},
256 {'O', nv_open, 0, 0},
257 {'P', nv_put, 0, 0},
258 {'Q', nv_exmode, NV_NCW, 0},
259 {'R', nv_Replace, 0, FALSE},
260 {'S', nv_subst, NV_KEEPREG, 0},
261 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
262 {'U', nv_Undo, 0, 0},
263 {'W', nv_wordcmd, 0, TRUE},
264 {'X', nv_abbrev, NV_KEEPREG, 0},
265 {'Y', nv_abbrev, NV_KEEPREG, 0},
266 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
267 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
268 {'\\', nv_error, 0, 0},
269 {']', nv_brackets, NV_NCH_ALW, FORWARD},
270 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
271 {'_', nv_lineop, 0, 0},
272 {'`', nv_gomark, NV_NCH_ALW, FALSE},
273 {'a', nv_edit, NV_NCH, 0},
274 {'b', nv_bck_word, 0, 0},
275 {'c', nv_operator, 0, 0},
276 {'d', nv_operator, 0, 0},
277 {'e', nv_wordcmd, 0, FALSE},
278 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
279 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
280 {'h', nv_left, NV_RL, 0},
281 {'i', nv_edit, NV_NCH, 0},
282 {'j', nv_down, 0, FALSE},
283 {'k', nv_up, 0, FALSE},
284 {'l', nv_right, NV_RL, 0},
285 {'m', nv_mark, NV_NCH_NOP, 0},
286 {'n', nv_next, 0, 0},
287 {'o', nv_open, 0, 0},
288 {'p', nv_put, 0, 0},
289 {'q', nv_record, NV_NCH, 0},
290 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
291 {'s', nv_subst, NV_KEEPREG, 0},
292 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
293 {'u', nv_undo, 0, 0},
294 {'w', nv_wordcmd, 0, FALSE},
295 {'x', nv_abbrev, NV_KEEPREG, 0},
296 {'y', nv_operator, 0, 0},
297 {'z', nv_zet, NV_NCH_ALW, 0},
298 {'{', nv_findpar, 0, BACKWARD},
299 {'|', nv_pipe, 0, 0},
300 {'}', nv_findpar, 0, FORWARD},
301 {'~', nv_tilde, 0, 0},
302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100303 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200305 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
306 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
307 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
308 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {K_LEFTMOUSE, nv_mouse, 0, 0},
310 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
311 {K_LEFTDRAG, nv_mouse, 0, 0},
312 {K_LEFTRELEASE, nv_mouse, 0, 0},
313 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100314 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
316 {K_MIDDLEDRAG, nv_mouse, 0, 0},
317 {K_MIDDLERELEASE, nv_mouse, 0, 0},
318 {K_RIGHTMOUSE, nv_mouse, 0, 0},
319 {K_RIGHTDRAG, nv_mouse, 0, 0},
320 {K_RIGHTRELEASE, nv_mouse, 0, 0},
321 {K_X1MOUSE, nv_mouse, 0, 0},
322 {K_X1DRAG, nv_mouse, 0, 0},
323 {K_X1RELEASE, nv_mouse, 0, 0},
324 {K_X2MOUSE, nv_mouse, 0, 0},
325 {K_X2DRAG, nv_mouse, 0, 0},
326 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000327 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000328 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {K_INS, nv_edit, 0, 0},
330 {K_KINS, nv_edit, 0, 0},
331 {K_BS, nv_ctrlh, 0, 0},
332 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
333 {K_S_UP, nv_page, NV_SS, BACKWARD},
334 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
335 {K_S_DOWN, nv_page, NV_SS, FORWARD},
336 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
337 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
338 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
339 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
340 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
341 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
342 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
343 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
344 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
345 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
346 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
347 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 {K_S_END, nv_end, NV_SS, FALSE},
349 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
350 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
351 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 {K_S_HOME, nv_home, NV_SS, 0},
353 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
354 {K_DEL, nv_abbrev, 0, 0},
355 {K_KDEL, nv_abbrev, 0, 0},
356 {K_UNDO, nv_kundo, 0, 0},
357 {K_HELP, nv_help, NV_NCW, 0},
358 {K_F1, nv_help, NV_NCW, 0},
359 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifdef FEAT_GUI
362 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
363 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
364#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000365#ifdef FEAT_GUI_TABLINE
366 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000367 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369#ifdef FEAT_NETBEANS_INTG
370 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
371#endif
372#ifdef FEAT_DND
373 {K_DROP, nv_drop, NV_STS, 0},
374#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000375 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100376 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100377 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378};
379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100380// Number of commands in nv_cmds[].
K.Takataeeec2542021-06-02 13:28:16 +0200381#define NV_CMDS_SIZE ARRAY_LENGTH(nv_cmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static short nv_cmd_idx[NV_CMDS_SIZE];
385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386// The highest index for which
387// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static int nv_max_linear;
389
390/*
391 * Compare functions for qsort() below, that checks the command character
392 * through the index in nv_cmd_idx[].
393 */
394 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100395nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396{
397 int c1, c2;
398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100399 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 c1 = nv_cmds[*(const short *)s1].cmd_char;
401 c2 = nv_cmds[*(const short *)s2].cmd_char;
402 if (c1 < 0)
403 c1 = -c1;
404 if (c2 < 0)
405 c2 = -c2;
406 return c1 - c2;
407}
408
409/*
410 * Initialize the nv_cmd_idx[] table.
411 */
412 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100413init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
415 int i;
416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100417 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000418 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 nv_cmd_idx[i] = i;
420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100421 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100424 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000425 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
427 break;
428 nv_max_linear = i - 1;
429}
430
431/*
432 * Search for a command in the commands table.
433 * Returns -1 for invalid command.
434 */
435 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 int i;
439 int idx;
440 int top, bot;
441 int c;
442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100443 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (cmdchar >= 0x100)
445 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100447 // We use the absolute value of the character. Special keys have a
448 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (cmdchar < 0)
450 cmdchar = -cmdchar;
451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // If the character is in the first part: The character is the index into
453 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (cmdchar <= nv_max_linear)
455 return nv_cmd_idx[cmdchar];
456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100457 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 bot = nv_max_linear + 1;
459 top = NV_CMDS_SIZE - 1;
460 idx = -1;
461 while (bot <= top)
462 {
463 i = (top + bot) / 2;
464 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
465 if (c < 0)
466 c = -c;
467 if (cmdchar == c)
468 {
469 idx = nv_cmd_idx[i];
470 break;
471 }
472 if (cmdchar > c)
473 bot = i + 1;
474 else
475 top = i - 1;
476 }
477 return idx;
478}
479
480/*
481 * Execute a command in Normal mode.
482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100484normal_cmd(
485 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100486 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100490 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 int old_col = curwin->w_curswant;
492#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100493 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100495 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 static int old_mapped_len = 0;
498 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000499#ifdef FEAT_EVAL
500 int set_prevcount = FALSE;
501#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200502 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
Bram Moolenaara80faa82020-04-12 19:37:17 +0200504 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000506
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100507 // Use a count remembered from before entering an operator. After typing
508 // "3d" we return from normal_cmd() and come back here, the "3" is
509 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 ca.opcount = opcount;
511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 /*
513 * If there is an operator pending, then the command we take this time
514 * will terminate it. Finish_op tells us to finish the operation before
515 * returning this time (unless the operation was cancelled).
516 */
517#ifdef CURSOR_SHAPE
518 c = finish_op;
519#endif
520 finish_op = (oap->op_type != OP_NOP);
521#ifdef CURSOR_SHAPE
522 if (finish_op != c)
523 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100524 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525# ifdef FEAT_MOUSESHAPE
526 update_mouseshape(-1);
527# endif
528 }
529#endif
530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100531 // When not finishing an operator and no register name typed, reset the
532 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000536#ifdef FEAT_EVAL
537 set_prevcount = TRUE;
538#endif
539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100541 // Restore counts from before receiving K_CURSORHOLD. This means after
542 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
543 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000544 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
545 {
546 ca.opcount = oap->prev_opcount;
547 ca.count0 = oap->prev_count0;
548 oap->prev_opcount = 0;
549 oap->prev_count0 = 0;
550 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000551
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554 State = NORMAL_BUSY;
555#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100556 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100559#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100560 // Set v:count here, when called from main() and not a stuffed
561 // command, so that v:count can be used in an expression mapping
562 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100563 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100564 set_vcount_ca(&ca, &set_prevcount);
565#endif
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 /*
568 * Get the command character from the user.
569 */
570 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100571 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
573 /*
574 * If a mapping was started in Visual or Select mode, remember the length
575 * of the mapping. This is used below to not return to Insert mode for as
576 * long as the mapping is being executed.
577 */
578 if (restart_edit == 0)
579 old_mapped_len = 0;
580 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000581 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 old_mapped_len = typebuf_maplen();
583
584 if (c == NUL)
585 c = K_ZERO;
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 /*
588 * In Select mode, typed text replaces the selection.
589 */
590 if (VIsual_active
591 && VIsual_select
592 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
595 // 'insertmode' is set) fake a "d"elete command, Insert mode will
596 // restart automatically.
597 // Insert the typed character in the typeahead buffer, so that it can
598 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200599 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000600 if (restart_edit != 0)
601 c = 'd';
602 else
603 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 msg_nowait = TRUE; // don't delay going to insert mode
605 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608#ifdef FEAT_CMDL_INFO
609 need_flushbuf = add_to_showcmd(c);
610#endif
611
612getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
615 /*
616 * Handle a count before a command and compute ca.count0.
617 * Note that '0' is a command and not the start of a count, but it's
618 * part of a count after other digits.
619 */
620 while ( (c >= '1' && c <= '9')
621 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
622 {
623 if (c == K_DEL || c == K_KDEL)
624 {
625 ca.count0 /= 10;
626#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
629 }
630 else
631 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100632 if (ca.count0 < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000634#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100635 // Set v:count here, when called from main() and not a stuffed
636 // command, so that v:count can be used in an expression mapping
637 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100638 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100639 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (ctrl_w)
642 {
643 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100644 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100646 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000647 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 --no_zero_mapping;
650 if (ctrl_w)
651 {
652 --no_mapping;
653 --allow_keys;
654 }
655#ifdef FEAT_CMDL_INFO
656 need_flushbuf |= add_to_showcmd(c);
657#endif
658 }
659
660 /*
661 * If we got CTRL-W there may be a/another count
662 */
663 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
664 {
665 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 ca.count0 = 0;
668 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 ++allow_keys; // no mapping for nchar, but keys
670 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 --no_mapping;
673 --allow_keys;
674#ifdef FEAT_CMDL_INFO
675 need_flushbuf |= add_to_showcmd(c);
676#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679 }
680
Bram Moolenaara983fe92008-07-31 20:04:27 +0000681 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 // Save the count values so that ca.opcount and ca.count0 are exactly
684 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000685 oap->prev_opcount = ca.opcount;
686 oap->prev_count0 = ca.count0;
687 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100688 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000689 {
690 /*
691 * If we're in the middle of an operator (including after entering a
692 * yank buffer with '"') AND we had a count before the operator, then
693 * that count overrides the current value of ca.count0.
694 * What this means effectively, is that commands like "3dw" get turned
695 * into "d3w" which makes things fall into place pretty neatly.
696 * If you give a count before AND after the operator, they are
697 * multiplied.
698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (ca.count0)
700 ca.count0 *= ca.opcount;
701 else
702 ca.count0 = ca.opcount;
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100703 if (ca.count0 < 0) // overflow
704 ca.count0 = 999999999L;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
706
707 /*
708 * Always remember the count. It will be set to zero (on the next call,
709 * above) when there is no pending operator.
710 * When called from main(), save the count for use by the "count" built-in
711 * variable.
712 */
713 ca.opcount = ca.count0;
714 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
715
716#ifdef FEAT_EVAL
717 /*
718 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100719 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100721 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000722 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#endif
724
725 /*
726 * Find the command character in the table of commands.
727 * For CTRL-W we already got nchar when looking for a count.
728 */
729 if (ctrl_w)
730 {
731 ca.nchar = c;
732 ca.cmdchar = Ctrl_W;
733 }
734 else
735 ca.cmdchar = c;
736 idx = find_command(ca.cmdchar);
737 if (idx < 0)
738 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100739 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 clearopbeep(oap);
741 goto normal_end;
742 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000743
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000744 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100746 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000748 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 goto normal_end;
750 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000751 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
752 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 /*
755 * In Visual/Select mode, a few keys are handled in a special way.
756 */
757 if (VIsual_active)
758 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100759 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (km_stopsel
761 && (nv_cmds[idx].cmd_flags & NV_STS)
762 && !(mod_mask & MOD_MASK_SHIFT))
763 {
764 end_visual_mode();
765 redraw_curbuf_later(INVERTED);
766 }
767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100768 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (km_startsel)
770 {
771 if (nv_cmds[idx].cmd_flags & NV_SS)
772 {
773 unshift_special(&ca);
774 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000775 if (idx < 0)
776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100777 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000778 clearopbeep(oap);
779 goto normal_end;
780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
783 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788#ifdef FEAT_RIGHTLEFT
789 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
790 && (nv_cmds[idx].cmd_flags & NV_RL))
791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100792 // Invert horizontal movements and operations. Only when typed by the
793 // user directly, not when the result of a mapping or "x" translated
794 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 switch (ca.cmdchar)
796 {
797 case 'l': ca.cmdchar = 'h'; break;
798 case K_RIGHT: ca.cmdchar = K_LEFT; break;
799 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
800 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
801 case 'h': ca.cmdchar = 'l'; break;
802 case K_LEFT: ca.cmdchar = K_RIGHT; break;
803 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
804 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
805 case '>': ca.cmdchar = '<'; break;
806 case '<': ca.cmdchar = '>'; break;
807 }
808 idx = find_command(ca.cmdchar);
809 }
810#endif
811
812 /*
813 * Get an additional character if we need one.
814 */
815 if ((nv_cmds[idx].cmd_flags & NV_NCH)
816 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
817 && oap->op_type == OP_NOP)
818 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
819 || (ca.cmdchar == 'q'
820 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200821 && reg_recording == 0
822 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100824 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {
826 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 int repl = FALSE; // get character for replace mode
828 int lit = FALSE; // get extra character literally
829 int langmap_active = FALSE; // using :lmap mappings
830 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100831#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100832 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833#endif
834
835 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 ++allow_keys; // no mapping for nchar, but allow key codes
837 // Don't generate a CursorHold event here, most commands can't handle
838 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000839 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (ca.cmdchar == 'g')
841 {
842 /*
843 * For 'g' get the next character now, so that we can check for
844 * "gr", "g'" and "g`".
845 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000846 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#ifdef FEAT_CMDL_INFO
849 need_flushbuf |= add_to_showcmd(ca.nchar);
850#endif
851 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100852 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100858 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863 else
864 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 repl = TRUE;
867 cp = &ca.nchar;
868 }
869 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
870
871 /*
872 * Get a second or third character.
873 */
874 if (cp != NULL)
875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (repl)
877 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100879#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100880 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
884 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100885 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 --no_mapping;
887 --allow_keys;
888 if (repl)
889 State = LREPLACE;
890 else
891 State = LANGMAP;
892 langmap_active = TRUE;
893 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100894#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100896 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
898 im_set_active(TRUE);
899#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200900 if ((State & INSERT) && !p_ek)
901 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200902#ifdef FEAT_JOB_CHANNEL
903 ch_log_output = TRUE;
904#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200905 // Disable bracketed paste and modifyOtherKeys here, we won't
906 // recognize the escape sequences with 'esckeys' off.
907 out_str(T_BD);
908 out_str(T_CTE);
909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000911 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaarca774f62020-08-30 20:46:38 +0200913 if ((State & INSERT) && !p_ek)
914 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200915#ifdef FEAT_JOB_CHANNEL
916 ch_log_output = TRUE;
917#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200918 // Re-enable bracketed paste mode and modifyOtherKeys
919 out_str(T_BE);
920 out_str(T_CTI);
921 }
922
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (langmap_active)
924 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100925 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 ++no_mapping;
927 ++allow_keys;
928 State = NORMAL_BUSY;
929 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100930#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (lang)
932 {
933 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
934 im_save_status(&curbuf->b_p_iminsert);
935 im_set_active(FALSE);
936 }
937 p_smd = save_smd;
938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#ifdef FEAT_CMDL_INFO
941 need_flushbuf |= add_to_showcmd(*cp);
942#endif
943
944 if (!lit)
945 {
946#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100947 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (*cp == Ctrl_K
949 && ((nv_cmds[idx].cmd_flags & NV_LANG)
950 || cp == &ca.extra_char)
951 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
952 {
953 c = get_digraph(FALSE);
954 if (c > 0)
955 {
956 *cp = c;
957# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 del_from_showcmd(3);
960 need_flushbuf |= add_to_showcmd(*cp);
961# endif
962 }
963 }
964#endif
965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100966 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100969 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (p_hkmap && lang && KeyTyped)
971 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972#endif
973 }
974
975 /*
976 * When the next character is CTRL-\ a following CTRL-N means the
977 * command is aborted and we go to Normal mode.
978 */
979 if (cp == &ca.extra_char
980 && ca.nchar == Ctrl_BSL
981 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
982 {
983 ca.cmdchar = Ctrl_BSL;
984 ca.nchar = ca.extra_char;
985 idx = find_command(ca.cmdchar);
986 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200987 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200988 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 else if (*cp == Ctrl_BSL)
990 {
991 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100993 // There is a busy wait here when typing "f<C-\>" and then
994 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 while ((c = vpeekc()) <= 0 && towait > 0L)
996 {
Bram Moolenaare2edc2e2021-01-16 20:21:23 +0100997 do_sleep(towait > 50L ? 50L : towait, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 towait -= 50L;
999 }
1000 if (c > 0)
1001 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001002 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (c != Ctrl_N && c != Ctrl_G)
1004 vungetc(c);
1005 else
1006 {
1007 ca.cmdchar = Ctrl_BSL;
1008 ca.nchar = c;
1009 idx = find_command(ca.cmdchar);
1010 }
1011 }
1012 }
1013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 // When getting a text character and the next character is a
1015 // multi-byte character, it could be a composing character.
1016 // However, don't wait for it to arrive. Also, do enable mapping,
1017 // because if it's put back with vungetc() it's too late to apply
1018 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001019 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 while (enc_utf8 && lang && (c = vpeekc()) > 0
1021 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1022 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001023 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (!utf_iscomposing(c))
1025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001026 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 break;
1028 }
1029 else if (ca.ncharC1 == 0)
1030 ca.ncharC1 = c;
1031 else
1032 ca.ncharC2 = c;
1033 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001034 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
1036 --no_mapping;
1037 --allow_keys;
1038 }
1039
1040#ifdef FEAT_CMDL_INFO
1041 /*
1042 * Flush the showcmd characters onto the screen so we can see them while
1043 * the command is being executed. Only do this when the shown command was
1044 * actually displayed, otherwise this will slow down a lot when executing
1045 * mappings.
1046 */
1047 if (need_flushbuf)
1048 out_flush();
1049#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001050 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001051 {
1052 if (ex_normal_busy)
1053 did_cursorhold = save_did_cursorhold;
1054 else
1055 did_cursorhold = FALSE;
1056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
1058 State = NORMAL;
1059
1060 if (ca.nchar == ESC)
1061 {
1062 clearop(oap);
1063 if (restart_edit == 0 && goto_im())
1064 restart_edit = 'a';
1065 goto normal_end;
1066 }
1067
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001068 if (ca.cmdchar != K_IGNORE)
1069 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001070 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001071 msg_col = 0;
1072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001074 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001076 // When 'keymodel' contains "startsel" some keys start Select/Visual
1077 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (!VIsual_active && km_startsel)
1079 {
1080 if (nv_cmds[idx].cmd_flags & NV_SS)
1081 {
1082 start_selection();
1083 unshift_special(&ca);
1084 idx = find_command(ca.cmdchar);
1085 }
1086 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1087 && (mod_mask & MOD_MASK_SHIFT))
1088 {
1089 start_selection();
1090 mod_mask &= ~MOD_MASK_SHIFT;
1091 }
1092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
1094 /*
1095 * Execute the command!
1096 * Call the command function found in the commands table.
1097 */
1098 ca.arg = nv_cmds[idx].cmd_arg;
1099 (nv_cmds[idx].cmd_func)(&ca);
1100
1101 /*
1102 * If we didn't start or finish an operator, reset oap->regname, unless we
1103 * need it later.
1104 */
1105 if (!finish_op
1106 && !oap->op_type
1107 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1108 {
1109 clearop(oap);
1110#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001111 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112#endif
1113 }
1114
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001115 // Get the length of mapped chars again after typing a count, second
1116 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001117 if (old_mapped_len > 0)
1118 old_mapped_len = typebuf_maplen();
1119
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 /*
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001121 * If an operation is pending, handle it. But not for K_IGNORE or
1122 * K_MOUSEMOVE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 */
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001124 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001125 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
1127 /*
1128 * Wait for a moment when a message is displayed that will be overwritten
1129 * by the mode message.
1130 * In Visual mode and with "^O" in Insert mode, a short message will be
1131 * overwritten by the mode message. Wait a bit, until a key is hit.
1132 * In Visual mode, it's more important to keep the Visual area updated
1133 * than keeping a message (e.g. from a /pat search).
1134 * Only do this if the command was typed, not from a mapping.
1135 * Don't wait when emsg_silent is non-zero.
1136 * Also wait a bit after an error message, e.g. for "^O:".
1137 * Don't redraw the screen, it would remove the message.
1138 */
1139 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001140 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 || (VIsual_active
1143 && old_pos.lnum == curwin->w_cursor.lnum
1144 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 )
1146 && (clear_cmdline
1147 || redraw_cmdline)
1148 && (msg_didout || (msg_didany && msg_scroll))
1149 && !msg_nowait
1150 && KeyTyped)
1151 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 && (msg_scroll
1154 || emsg_on_display)))
1155 && oap->regname == 0
1156 && !(ca.retval & CA_COMMAND_BUSY)
1157 && stuff_empty()
1158 && typebuf_typed()
1159 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001160 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 && !did_wait_return
1162 && oap->op_type == OP_NOP)
1163 {
1164 int save_State = State;
1165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001166 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (restart_edit != 0)
1168 State = INSERT;
1169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001170 // If need to redraw, and there is a "keep_msg", redraw before the
1171 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1173 {
1174 char_u *kmsg;
1175
1176 kmsg = keep_msg;
1177 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001178 // Showmode() will clear keep_msg, but we want to use it anyway.
1179 // First update w_topline.
1180 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001182 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001184
1185 kmsg = vim_strsave(keep_msg);
1186 if (kmsg != NULL)
1187 {
1188 msg_attr((char *)kmsg, keep_msg_attr);
1189 vim_free(kmsg);
1190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001193#ifdef CURSOR_SHAPE
1194 ui_cursor_shape(); // may show different cursor shape
1195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 cursor_on();
1197 out_flush();
1198 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001199 ui_delay(1003L, TRUE); // wait at least one second
1200 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 State = save_State;
1202
1203 msg_scroll = FALSE;
1204 emsg_on_display = FALSE;
1205 }
1206
1207 /*
1208 * Finish up after executing a Normal mode command.
1209 */
1210normal_end:
1211
1212 msg_nowait = FALSE;
1213
Bram Moolenaarcc613032020-06-07 21:31:18 +02001214#ifdef FEAT_EVAL
1215 if (finish_op)
1216 reset_reg_var();
1217#endif
1218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001219 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef CURSOR_SHAPE
1221 c = finish_op;
1222#endif
1223 finish_op = FALSE;
1224#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001225 // Redraw the cursor with another shape, if we were in Operator-pending
1226 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (c || ca.cmdchar == 'r')
1228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230# ifdef FEAT_MOUSESHAPE
1231 update_mouseshape(-1);
1232# endif
1233 }
1234#endif
1235
1236#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001237 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001238 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 clear_showcmd();
1240#endif
1241
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001242 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 vim_free(ca.searchbuf);
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (has_mbyte)
1246 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 if (curwin->w_p_scb && toplevel)
1249 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 do_check_scrollbind(TRUE);
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Bram Moolenaar860cae12010-06-05 23:22:07 +02001254 if (curwin->w_p_crb && toplevel)
1255 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001256 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001257 do_check_cursorbind();
1258 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001259
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001260#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001261 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001262 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001263 restart_edit = 0;
1264#endif
1265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 /*
1267 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1268 * if still inside a mapping that started in Visual mode).
1269 * May switch from Visual to Select mode after CTRL-O command.
1270 */
1271 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1273 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 && !(ca.retval & CA_COMMAND_BUSY)
1275 && stuff_empty()
1276 && oap->regname == 0)
1277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (restart_VIsual_select == 1)
1279 {
1280 VIsual_select = TRUE;
1281 showmode();
1282 restart_VIsual_select = 0;
1283 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001284 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 (void)edit(restart_edit, FALSE, 1L);
1286 }
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (restart_VIsual_select == 2)
1289 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001291 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 opcount = ca.opcount;
1293}
1294
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001295#ifdef FEAT_EVAL
1296/*
1297 * Set v:count and v:count1 according to "cap".
1298 * Set v:prevcount only when "set_prevcount" is TRUE.
1299 */
1300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001301set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001302{
1303 long count = cap->count0;
1304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001305 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001306 if (cap->opcount != 0)
1307 count = cap->opcount * (count == 0 ? 1 : count);
1308 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001309 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001310}
1311#endif
1312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001314 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 * if not.
1316 */
1317 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001318check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 static int did_check = FALSE;
1321
1322 if (full_screen)
1323 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001324 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001325 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 did_check = TRUE;
1327 }
1328}
1329
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001330#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1331/*
1332 * Call yank_do_autocmd() for "regname".
1333 */
1334 static void
1335call_yank_do_autocmd(int regname)
1336{
1337 oparg_T oa;
1338 yankreg_T *reg;
1339
1340 clear_oparg(&oa);
1341 oa.regname = regname;
1342 oa.op_type = OP_YANK;
1343 oa.is_VIsual = TRUE;
1344 reg = get_register(regname, TRUE);
1345 yank_do_autocmd(&oa, reg);
1346 free_register(reg);
1347}
1348#endif
1349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001351 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001352 * This function or the next should ALWAYS be called to end Visual mode, except
1353 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 */
1355 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001356end_visual_mode()
1357{
1358 end_visual_mode_keep_button();
1359 reset_held_button();
1360}
1361
1362 void
1363end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365#ifdef FEAT_CLIPBOARD
1366 /*
1367 * If we are using the clipboard, then remember what was selected in case
1368 * we need to paste it somewhere while we still own the selection.
1369 * Only do this when the clipboard is already owned. Don't want to grab
1370 * the selection when hitting ESC.
1371 */
1372 if (clip_star.available && clip_star.owned)
1373 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001374
1375# if defined(FEAT_EVAL)
1376 // Emit a TextYankPost for the automatic copy of the selection into the
1377 // star and/or plus register.
1378 if (has_textyankpost())
1379 {
1380 if (clip_isautosel_star())
1381 call_yank_do_autocmd('*');
1382 if (clip_isautosel_plus())
1383 call_yank_do_autocmd('+');
1384 }
1385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#endif
1387
1388 VIsual_active = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02001389 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 setmouse();
1391 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001393 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001394 curbuf->b_visual.vi_mode = VIsual_mode;
1395 curbuf->b_visual.vi_start = VIsual;
1396 curbuf->b_visual.vi_end = curwin->w_cursor;
1397 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_EVAL
1399 curbuf->b_visual_mode_eval = VIsual_mode;
1400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (!virtual_active())
1402 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001403 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001405 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406}
1407
1408/*
1409 * Reset VIsual_active and VIsual_reselect.
1410 */
1411 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001412reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
1414 if (VIsual_active)
1415 {
1416 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001417 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 VIsual_reselect = FALSE;
1420}
1421
1422/*
1423 * Reset VIsual_active and VIsual_reselect if it's set.
1424 */
1425 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001426reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
1428 if (VIsual_active)
1429 {
1430 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001431 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 VIsual_reselect = FALSE;
1433 }
1434}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001436 void
1437restore_visual_mode(void)
1438{
1439 if (VIsual_mode_orig != NUL)
1440 {
1441 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1442 VIsual_mode_orig = NUL;
1443 }
1444}
1445
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446/*
1447 * Check for a balloon-eval special item to include when searching for an
1448 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1449 * Returns TRUE if the character at "*ptr" should be included.
1450 * "dir" is FORWARD or BACKWARD, the direction of searching.
1451 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1452 * "bnp" points to a counter for square brackets.
1453 */
1454 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001455find_is_eval_item(
1456 char_u *ptr,
1457 int *colp,
1458 int *bnp,
1459 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001461 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1463 ++*bnp;
1464 if (*bnp > 0)
1465 {
1466 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1467 --*bnp;
1468 return TRUE;
1469 }
1470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001471 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if (*ptr == '.')
1473 return TRUE;
1474
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001475 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1477 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1478 {
1479 *colp += dir;
1480 return TRUE;
1481 }
1482 return FALSE;
1483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485/*
1486 * Find the identifier under or to the right of the cursor.
1487 * "find_type" can have one of three values:
1488 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001489 * FIND_STRING: find any non-white text
1490 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001491 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 *
1493 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001494 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001496 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 * This doesn't match the real Vi but I like it a little better and it
1498 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001499 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * When FIND_IDENT isn't defined, we backup until a blank.
1501 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001502 * Returns the length of the text, or zero if no text is found.
1503 * If text is found, a pointer to the text is put in "*text". This
1504 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 */
1506 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001507find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
1509 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001510 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511}
1512
1513/*
1514 * Like find_ident_under_cursor(), but for any window and any position.
1515 * However: Uses 'iskeyword' from the current window!.
1516 */
1517 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001518find_ident_at_pos(
1519 win_T *wp,
1520 linenr_T lnum,
1521 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001522 char_u **text,
1523 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001524 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001527 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 int this_class = 0;
1530 int prev_class;
1531 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001532 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
1534 /*
1535 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001536 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 */
1538 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1539 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1540 {
1541 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001542 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 */
1544 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 if (has_mbyte)
1546 {
1547 while (ptr[col] != NUL)
1548 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001549 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1551 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 this_class = mb_get_class(ptr + col);
1553 if (this_class != 0 && (i == 1 || this_class != 1))
1554 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001555 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557 }
1558 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001560 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 )
1563 ++col;
1564
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001565 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001569 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (has_mbyte)
1572 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001573 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1575 this_class = mb_get_class((char_u *)"a");
1576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001578 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
1580 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1581 prev_class = mb_get_class(ptr + prevcol);
1582 if (this_class != prev_class
1583 && (i == 0
1584 || prev_class == 0
1585 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 && (!(find_type & FIND_EVAL)
1587 || prevcol == 0
1588 || !find_is_eval_item(ptr + prevcol, &prevcol,
1589 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 )
1591 break;
1592 col = prevcol;
1593 }
1594
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001595 // If we don't want just any old text, or we've found an
1596 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (this_class > 2)
1598 this_class = 2;
1599 if (!(find_type & FIND_STRING) || this_class == 2)
1600 break;
1601 }
1602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 while (col > 0
1605 && ((i == 0
1606 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001607 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 && (!(find_type & FIND_IDENT)
1609 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 || ((find_type & FIND_EVAL)
1611 && col > 1
1612 && find_is_eval_item(ptr + col - 1, &col,
1613 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 ))
1615 --col;
1616
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001617 // If we don't want just any old text, or we've found an
1618 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1620 break;
1621 }
1622 }
1623
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001624 if (ptr[col] == NUL || (i == 0
1625 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001627 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001628 if ((find_type & FIND_NOERROR) == 0)
1629 {
1630 if (find_type & FIND_STRING)
1631 emsg(_("E348: No string under cursor"));
1632 else
1633 emsg(_(e_noident));
1634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 return 0;
1636 }
1637 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001638 *text = ptr;
1639 if (textcol != NULL)
1640 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
1642 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001643 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 bn = 0;
1646 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (has_mbyte)
1649 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001650 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 this_class = mb_get_class(ptr);
1652 while (ptr[col] != NUL
1653 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1654 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 || ((find_type & FIND_EVAL)
1656 && col <= (int)startcol
1657 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001659 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001663 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 || ((find_type & FIND_EVAL)
1665 && col <= (int)startcol
1666 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670 return col;
1671}
1672
1673/*
1674 * Prepare for redo of a normal command.
1675 */
1676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001677prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
1679 prep_redo(cap->oap->regname, cap->count0,
1680 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1681}
1682
1683/*
1684 * Prepare for redo of any command.
1685 * Note that only the last argument can be a multi-byte char.
1686 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001687 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001688prep_redo(
1689 int regname,
1690 long num,
1691 int cmd1,
1692 int cmd2,
1693 int cmd3,
1694 int cmd4,
1695 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001698 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 {
1700 AppendCharToRedobuff('"');
1701 AppendCharToRedobuff(regname);
1702 }
1703 if (num)
1704 AppendNumberToRedobuff(num);
1705
1706 if (cmd1 != NUL)
1707 AppendCharToRedobuff(cmd1);
1708 if (cmd2 != NUL)
1709 AppendCharToRedobuff(cmd2);
1710 if (cmd3 != NUL)
1711 AppendCharToRedobuff(cmd3);
1712 if (cmd4 != NUL)
1713 AppendCharToRedobuff(cmd4);
1714 if (cmd5 != NUL)
1715 AppendCharToRedobuff(cmd5);
1716}
1717
1718/*
1719 * check for operator active and clear it
1720 *
1721 * return TRUE if operator was active
1722 */
1723 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001724checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725{
1726 if (oap->op_type == OP_NOP)
1727 return FALSE;
1728 clearopbeep(oap);
1729 return TRUE;
1730}
1731
1732/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001733 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001735 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 */
1737 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001738checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001740 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 return FALSE;
1742 clearopbeep(oap);
1743 return TRUE;
1744}
1745
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001747clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
1749 oap->op_type = OP_NOP;
1750 oap->regname = 0;
1751 oap->motion_force = NUL;
1752 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001753 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754}
1755
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001756 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001757clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
1759 clearop(oap);
1760 beep_flush();
1761}
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763/*
1764 * Remove the shift modifier from a special key.
1765 */
1766 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001767unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768{
1769 switch (cap->cmdchar)
1770 {
1771 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1772 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1773 case K_S_UP: cap->cmdchar = K_UP; break;
1774 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1775 case K_S_HOME: cap->cmdchar = K_HOME; break;
1776 case K_S_END: cap->cmdchar = K_END; break;
1777 }
1778 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1779}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001781/*
1782 * If the mode is currently displayed clear the command line or update the
1783 * command displayed.
1784 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001785 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001786may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001787{
1788 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001789 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001790#ifdef FEAT_CMDL_INFO
1791 else
1792 clear_showcmd();
1793#endif
1794}
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1797/*
1798 * Routines for displaying a partly typed command
1799 */
1800
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001801#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001803static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804static int showcmd_is_clear = TRUE;
1805static int showcmd_visual = FALSE;
1806
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001807static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001810clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
1812 if (!p_sc)
1813 return;
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (VIsual_active && !char_avail())
1816 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001817 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 long lines;
1819 colnr_T leftcol, rightcol;
1820 linenr_T top, bot;
1821
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001822 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001823 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
1825 top = VIsual.lnum;
1826 bot = curwin->w_cursor.lnum;
1827 }
1828 else
1829 {
1830 top = curwin->w_cursor.lnum;
1831 bot = VIsual.lnum;
1832 }
1833# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001834 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001835 (void)hasFolding(top, &top, NULL);
1836 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837# endif
1838 lines = bot - top + 1;
1839
1840 if (VIsual_mode == Ctrl_V)
1841 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001842# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001843 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001844 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001845
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001846 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001847 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001848 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001851# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001852 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001853 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1856 (long)(rightcol - leftcol + 1));
1857 }
1858 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1859 sprintf((char *)showcmd_buf, "%ld", lines);
1860 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001861 {
1862 char_u *s, *e;
1863 int l;
1864 int bytes = 0;
1865 int chars = 0;
1866
1867 if (cursor_bot)
1868 {
1869 s = ml_get_pos(&VIsual);
1870 e = ml_get_cursor();
1871 }
1872 else
1873 {
1874 s = ml_get_cursor();
1875 e = ml_get_pos(&VIsual);
1876 }
1877 while ((*p_sel != 'e') ? s <= e : s < e)
1878 {
1879 l = (*mb_ptr2len)(s);
1880 if (l == 0)
1881 {
1882 ++bytes;
1883 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001884 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001885 }
1886 bytes += l;
1887 ++chars;
1888 s += l;
1889 }
1890 if (bytes == chars)
1891 sprintf((char *)showcmd_buf, "%d", chars);
1892 else
1893 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1894 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001895 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 showcmd_visual = TRUE;
1897 }
1898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
1900 showcmd_buf[0] = NUL;
1901 showcmd_visual = FALSE;
1902
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001903 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (showcmd_is_clear)
1905 return;
1906 }
1907
1908 display_showcmd();
1909}
1910
1911/*
1912 * Add 'c' to string of shown command chars.
1913 * Return TRUE if output has been written (and setcursor() has been called).
1914 */
1915 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001916add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 char_u *p;
1919 int old_len;
1920 int extra_len;
1921 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 int i;
1923 static int ignore[] =
1924 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001925#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1927 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001928#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001929 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001930 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1932 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001933 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001935 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 0
1937 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaar09df3122006-01-23 22:23:09 +00001939 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 return FALSE;
1941
1942 if (showcmd_visual)
1943 {
1944 showcmd_buf[0] = NUL;
1945 showcmd_visual = FALSE;
1946 }
1947
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001948 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (IS_SPECIAL(c))
1950 for (i = 0; ignore[i] != 0; ++i)
1951 if (ignore[i] == c)
1952 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
1954 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001955 if (*p == ' ')
1956 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 old_len = (int)STRLEN(showcmd_buf);
1958 extra_len = (int)STRLEN(p);
1959 overflow = old_len + extra_len - SHOWCMD_COLS;
1960 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001961 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1962 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 STRCAT(showcmd_buf, p);
1964
1965 if (char_avail())
1966 return FALSE;
1967
1968 display_showcmd();
1969
1970 return TRUE;
1971}
1972
1973 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001974add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976 if (!add_to_showcmd(c))
1977 setcursor();
1978}
1979
1980/*
1981 * Delete 'len' characters from the end of the shown command.
1982 */
1983 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001984del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
1986 int old_len;
1987
1988 if (!p_sc)
1989 return;
1990
1991 old_len = (int)STRLEN(showcmd_buf);
1992 if (len > old_len)
1993 len = old_len;
1994 showcmd_buf[old_len - len] = NUL;
1995
1996 if (!char_avail())
1997 display_showcmd();
1998}
1999
2000/*
2001 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2002 * something and there is a partial mapping.
2003 */
2004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002005push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 if (p_sc)
2008 STRCPY(old_showcmd_buf, showcmd_buf);
2009}
2010
2011 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002012pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013{
2014 if (!p_sc)
2015 return;
2016
2017 STRCPY(showcmd_buf, old_showcmd_buf);
2018
2019 display_showcmd();
2020}
2021
2022 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002023display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024{
2025 int len;
2026
2027 cursor_off();
2028
2029 len = (int)STRLEN(showcmd_buf);
2030 if (len == 0)
2031 showcmd_is_clear = TRUE;
2032 else
2033 {
2034 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2035 showcmd_is_clear = FALSE;
2036 }
2037
2038 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002039 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2040 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 */
2042 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2043
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002044 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045}
2046#endif
2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048/*
2049 * When "check" is FALSE, prepare for commands that scroll the window.
2050 * When "check" is TRUE, take care of scroll-binding after the window has
2051 * scrolled. Called from normal_cmd() and edit().
2052 */
2053 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002054do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055{
2056 static win_T *old_curwin = NULL;
2057 static linenr_T old_topline = 0;
2058#ifdef FEAT_DIFF
2059 static int old_topfill = 0;
2060#endif
2061 static buf_T *old_buf = NULL;
2062 static colnr_T old_leftcol = 0;
2063
2064 if (check && curwin->w_p_scb)
2065 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002066 // If a ":syncbind" command was just used, don't scroll, only reset
2067 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (did_syncbind)
2069 did_syncbind = FALSE;
2070 else if (curwin == old_curwin)
2071 {
2072 /*
2073 * Synchronize other windows, as necessary according to
2074 * 'scrollbind'. Don't do this after an ":edit" command, except
2075 * when 'diff' is set.
2076 */
2077 if ((curwin->w_buffer == old_buf
2078#ifdef FEAT_DIFF
2079 || curwin->w_p_diff
2080#endif
2081 )
2082 && (curwin->w_topline != old_topline
2083#ifdef FEAT_DIFF
2084 || curwin->w_topfill != old_topfill
2085#endif
2086 || curwin->w_leftcol != old_leftcol))
2087 {
2088 check_scrollbind(curwin->w_topline - old_topline,
2089 (long)(curwin->w_leftcol - old_leftcol));
2090 }
2091 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002092 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 /*
2095 * When switching between windows, make sure that the relative
2096 * vertical offset is valid for the new window. The relative
2097 * offset is invalid whenever another 'scrollbind' window has
2098 * scrolled to a point that would force the current window to
2099 * scroll past the beginning or end of its buffer. When the
2100 * resync is performed, some of the other 'scrollbind' windows may
2101 * need to jump so that the current window's relative position is
2102 * visible on-screen.
2103 */
2104 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2105 }
2106 curwin->w_scbind_pos = curwin->w_topline;
2107 }
2108
2109 old_curwin = curwin;
2110 old_topline = curwin->w_topline;
2111#ifdef FEAT_DIFF
2112 old_topfill = curwin->w_topfill;
2113#endif
2114 old_buf = curwin->w_buffer;
2115 old_leftcol = curwin->w_leftcol;
2116}
2117
2118/*
2119 * Synchronize any windows that have "scrollbind" set, based on the
2120 * number of rows by which the current window has changed
2121 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2122 */
2123 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002124check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126 int want_ver;
2127 int want_hor;
2128 win_T *old_curwin = curwin;
2129 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 int old_VIsual_select = VIsual_select;
2131 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 colnr_T tgt_leftcol = curwin->w_leftcol;
2133 long topline;
2134 long y;
2135
2136 /*
2137 * check 'scrollopt' string for vertical and horizontal scroll options
2138 */
2139 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2140#ifdef FEAT_DIFF
2141 want_ver |= old_curwin->w_p_diff;
2142#endif
2143 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2144
2145 /*
2146 * loop through the scrollbound windows and scroll accordingly
2147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002149 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002152 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 if (curwin != old_curwin && curwin->w_p_scb)
2154 {
2155 /*
2156 * do the vertical scroll
2157 */
2158 if (want_ver)
2159 {
2160#ifdef FEAT_DIFF
2161 if (old_curwin->w_p_diff && curwin->w_p_diff)
2162 {
2163 diff_set_topline(old_curwin, curwin);
2164 }
2165 else
2166#endif
2167 {
2168 curwin->w_scbind_pos += topline_diff;
2169 topline = curwin->w_scbind_pos;
2170 if (topline > curbuf->b_ml.ml_line_count)
2171 topline = curbuf->b_ml.ml_line_count;
2172 if (topline < 1)
2173 topline = 1;
2174
2175 y = topline - curwin->w_topline;
2176 if (y > 0)
2177 scrollup(y, FALSE);
2178 else
2179 scrolldown(-y, FALSE);
2180 }
2181
2182 redraw_later(VALID);
2183 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186
2187 /*
2188 * do the horizontal scroll
2189 */
2190 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2191 {
2192 curwin->w_leftcol = tgt_leftcol;
2193 leftcol_changed();
2194 }
2195 }
2196 }
2197
2198 /*
2199 * reset current-window
2200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 VIsual_select = old_VIsual_select;
2202 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 curwin = old_curwin;
2204 curbuf = old_curbuf;
2205}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206
2207/*
2208 * Command character that's ignored.
2209 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002210 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002213nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002215 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216}
2217
2218/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002219 * Command character that doesn't do anything, but unlike nv_ignore() does
2220 * start edit(). Used for "startinsert" executed while starting up.
2221 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002223nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002224{
2225}
2226
2227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 * Command character doesn't exist.
2229 */
2230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002231nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 clearopbeep(cap->oap);
2234}
2235
2236/*
2237 * <Help> and <F1> commands.
2238 */
2239 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002240nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 if (!checkclearopq(cap->oap))
2243 ex_help(NULL);
2244}
2245
2246/*
2247 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2248 */
2249 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002250nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002252#ifdef FEAT_JOB_CHANNEL
2253 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2254 clearopbeep(cap->oap);
2255 else
2256#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002257 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002258 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002259 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002260 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2261 op_addsub(cap->oap, cap->count1, cap->arg);
2262 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002263 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002264 else if (VIsual_active)
2265 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002266 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002267 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268}
2269
2270/*
2271 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2272 */
2273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002274nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
2276 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002278 if (mod_mask & MOD_MASK_CTRL)
2279 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002280 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002281 if (cap->arg == BACKWARD)
2282 goto_tabpage(-(int)cap->count1);
2283 else
2284 goto_tabpage((int)cap->count0);
2285 }
2286 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002287 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289}
2290
2291/*
2292 * Implementation of "gd" and "gD" command.
2293 */
2294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002295nv_gd(
2296 oparg_T *oap,
2297 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002298 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299{
2300 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 char_u *ptr;
2302
Bram Moolenaard9d30582005-05-18 22:10:28 +00002303 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002304 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2305 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002307#ifdef FEAT_FOLDING
2308 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2309 foldOpenCursor();
2310#endif
2311}
2312
2313/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002314 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2315 * otherwise.
2316 */
2317 static int
2318is_ident(char_u *line, int offset)
2319{
2320 int i;
2321 int incomment = FALSE;
2322 int instring = 0;
2323 int prev = 0;
2324
2325 for (i = 0; i < offset && line[i] != NUL; i++)
2326 {
2327 if (instring != 0)
2328 {
2329 if (prev != '\\' && line[i] == instring)
2330 instring = 0;
2331 }
2332 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2333 {
2334 instring = line[i];
2335 }
2336 else
2337 {
2338 if (incomment)
2339 {
2340 if (prev == '*' && line[i] == '/')
2341 incomment = FALSE;
2342 }
2343 else if (prev == '/' && line[i] == '*')
2344 {
2345 incomment = TRUE;
2346 }
2347 else if (prev == '/' && line[i] == '/')
2348 {
2349 return FALSE;
2350 }
2351 }
2352
2353 prev = line[i];
2354 }
2355
2356 return incomment == FALSE && instring == 0;
2357}
2358
2359/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002360 * Search for variable declaration of "ptr[len]".
2361 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2362 * current file ("gD").
2363 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002364 * Return FAIL when not found.
2365 */
2366 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002367find_decl(
2368 char_u *ptr,
2369 int len,
2370 int locally,
2371 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002372 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002373{
2374 char_u *pat;
2375 pos_T old_pos;
2376 pos_T par_pos;
2377 pos_T found_pos;
2378 int t;
2379 int save_p_ws;
2380 int save_p_scs;
2381 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002382 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002383 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002384 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002385
2386 if ((pat = alloc(len + 7)) == NULL)
2387 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002388
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002389 // Put "\V" before the pattern to avoid that the special meaning of "."
2390 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002391 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2392 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 old_pos = curwin->w_cursor;
2394 save_p_ws = p_ws;
2395 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002396 p_ws = FALSE; // don't wrap around end of file now
2397 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 /*
2400 * With "gD" go to line 1.
2401 * With "gd" Search back for the start of the current function, then go
2402 * back until a blank line. If this fails go to line 1.
2403 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002404 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002406 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002408 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 else
2411 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002412 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2414 --curwin->w_cursor.lnum;
2415 }
2416 curwin->w_cursor.col = 0;
2417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002418 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002419 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002420 for (;;)
2421 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002422 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002423 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002424 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002425 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002426
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002427 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002428 {
2429 pos_T *pos;
2430
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002431 // Check that the block the match is in doesn't end before the
2432 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002433 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2434 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2435 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002436 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002437 // There can't be a useful match before the end of this block.
2438 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002439 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002440 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002441 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002442 }
2443
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002444 if (t == FAIL)
2445 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002446 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002447 if (found_pos.lnum != 0)
2448 {
2449 curwin->w_cursor = found_pos;
2450 t = OK;
2451 }
2452 break;
2453 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002454 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002456 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002457 ++curwin->w_cursor.lnum;
2458 curwin->w_cursor.col = 0;
2459 continue;
2460 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002461 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2462
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002463 // If the current position is not a valid identifier and a previous
2464 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002465 if (!valid && found_pos.lnum != 0)
2466 {
2467 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002468 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002469 }
2470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002471 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002472 if (valid && !locally)
2473 break;
2474 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002475 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002476 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002477 if (found_pos.lnum != 0)
2478 curwin->w_cursor = found_pos;
2479 break;
2480 }
2481
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002482 // For finding a local variable and the match is before the "{" or
2483 // inside a comment, continue searching. For K&R style function
2484 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002485 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002486 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002487 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002488 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002489 // Remove SEARCH_START from flags to avoid getting stuck at one
2490 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002491 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002493
2494 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002496 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 curwin->w_cursor = old_pos;
2498 }
2499 else
2500 {
2501 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002502 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 reset_search_dir();
2504 }
2505
2506 vim_free(pat);
2507 p_ws = save_p_ws;
2508 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002509
2510 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511}
2512
2513/*
2514 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2515 * lines rather than lines in the file.
2516 * 'dist' must be positive.
2517 *
2518 * Return OK if able to move cursor, FAIL otherwise.
2519 */
2520 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002521nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523 int linelen = linetabsize(ml_get_curline());
2524 int retval = OK;
2525 int atend = FALSE;
2526 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002527 int col_off1; // margin offset for first screen line
2528 int col_off2; // margin offset for wrapped screen line
2529 int width1; // text width for first screen line
2530 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002533 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535 col_off1 = curwin_col_off();
2536 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002537 width1 = curwin->w_width - col_off1;
2538 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002539 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002540 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 /*
2545 * Instead of sticking at the last character of the buffer line we
2546 * try to stick in the last column of the screen.
2547 */
2548 if (curwin->w_curswant == MAXCOL)
2549 {
2550 atend = TRUE;
2551 validate_virtcol();
2552 if (width1 <= 0)
2553 curwin->w_curswant = 0;
2554 else
2555 {
2556 curwin->w_curswant = width1 - 1;
2557 if (curwin->w_virtcol > curwin->w_curswant)
2558 curwin->w_curswant += ((curwin->w_virtcol
2559 - curwin->w_curswant - 1) / width2 + 1) * width2;
2560 }
2561 }
2562 else
2563 {
2564 if (linelen > width1)
2565 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2566 else
2567 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002568 if (curwin->w_curswant >= (colnr_T)n)
2569 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 }
2571
2572 while (dist--)
2573 {
2574 if (dir == BACKWARD)
2575 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002576 if ((long)curwin->w_curswant >= width1
2577#ifdef FEAT_FOLDING
2578 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2579#endif
2580 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002581 // Move back within the line. This can give a negative value
2582 // for w_curswant if width1 < width2 (with cpoptions+=n),
2583 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 curwin->w_curswant -= width2;
2585 else
2586 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002587 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002589 // Move to the start of a closed fold. Don't do that when
2590 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (!(fdo_flags & FDO_ALL))
2592 (void)hasFolding(curwin->w_cursor.lnum,
2593 &curwin->w_cursor.lnum, NULL);
2594#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002595 if (curwin->w_cursor.lnum == 1)
2596 {
2597 retval = FAIL;
2598 break;
2599 }
2600 --curwin->w_cursor.lnum;
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 linelen = linetabsize(ml_get_curline());
2603 if (linelen > width1)
2604 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2605 + 1) * width2;
2606 }
2607 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002608 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 if (linelen > width1)
2611 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2612 else
2613 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002614 if (curwin->w_curswant + width2 < (colnr_T)n
2615#ifdef FEAT_FOLDING
2616 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2617#endif
2618 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002619 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 curwin->w_curswant += width2;
2621 else
2622 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002623 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002625 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2627 &curwin->w_cursor.lnum);
2628#endif
2629 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2630 {
2631 retval = FAIL;
2632 break;
2633 }
2634 curwin->w_cursor.lnum++;
2635 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002636 // Check if the cursor has moved below the number display
2637 // when width1 < width2 (with cpoptions+=n). Subtract width2
2638 // to get a negative value for w_curswant, which will get
2639 // clipped to column 0.
2640 if (curwin->w_curswant >= width1)
2641 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002642 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644 }
2645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002648 if (virtual_active() && atend)
2649 coladvance(MAXCOL);
2650 else
2651 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2654 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002655 colnr_T virtcol;
2656
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 /*
2658 * Check for landing on a character that got split at the end of the
2659 * last line. We want to advance a screenline, not end up in the same
2660 * screenline or move two screenlines.
2661 */
2662 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002663 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002664#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002665 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2666 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002667#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002668
2669 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 && (curwin->w_curswant < (colnr_T)width1
2671 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2672 : ((curwin->w_curswant - width1) % width2
2673 > (colnr_T)width2 / 2)))
2674 --curwin->w_cursor.col;
2675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002678 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
2680 return retval;
2681}
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683/*
2684 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2685 * cap->arg must be TRUE for CTRL-E.
2686 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002687 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002688nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 if (!checkclearop(cap->oap))
2691 scroll_redraw(cap->arg, cap->count1);
2692}
2693
2694/*
2695 * Scroll "count" lines up or down, and redraw.
2696 */
2697 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002698scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
2700 linenr_T prev_topline = curwin->w_topline;
2701#ifdef FEAT_DIFF
2702 int prev_topfill = curwin->w_topfill;
2703#endif
2704 linenr_T prev_lnum = curwin->w_cursor.lnum;
2705
2706 if (up)
2707 scrollup(count, TRUE);
2708 else
2709 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002710 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002712 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2713 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 cursor_correct();
2715 check_cursor_moved(curwin);
2716 curwin->w_valid |= VALID_TOPLINE;
2717
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002718 // If moved back to where we were, at least move the cursor, otherwise
2719 // we get stuck at one position. Don't move the cursor up if the
2720 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 while (curwin->w_topline == prev_topline
2722#ifdef FEAT_DIFF
2723 && curwin->w_topfill == prev_topfill
2724#endif
2725 )
2726 {
2727 if (up)
2728 {
2729 if (curwin->w_cursor.lnum > prev_lnum
2730 || cursor_down(1L, FALSE) == FAIL)
2731 break;
2732 }
2733 else
2734 {
2735 if (curwin->w_cursor.lnum < prev_lnum
2736 || prev_topline == 1L
2737 || cursor_up(1L, FALSE) == FAIL)
2738 break;
2739 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002740 // Mark w_topline as valid, otherwise the screen jumps back at the
2741 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 check_cursor_moved(curwin);
2743 curwin->w_valid |= VALID_TOPLINE;
2744 }
2745 }
2746 if (curwin->w_cursor.lnum != prev_lnum)
2747 coladvance(curwin->w_curswant);
2748 redraw_later(VALID);
2749}
2750
2751/*
2752 * Commands that start with "z".
2753 */
2754 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002755nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 long n;
2758 colnr_T col;
2759 int nchar = cap->nchar;
2760#ifdef FEAT_FOLDING
2761 long old_fdl = curwin->w_p_fdl;
2762 int old_fen = curwin->w_p_fen;
2763#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002764#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002765 int undo = FALSE;
2766#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002767 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
2769 if (VIM_ISDIGIT(nchar))
2770 {
2771 /*
2772 * "z123{nchar}": edit the count before obtaining {nchar}
2773 */
2774 if (checkclearop(cap->oap))
2775 return;
2776 n = nchar - '0';
2777 for (;;)
2778 {
2779#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002780 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781#endif
2782 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002783 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002784 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 --no_mapping;
2787 --allow_keys;
2788#ifdef FEAT_CMDL_INFO
2789 (void)add_to_showcmd(nchar);
2790#endif
2791 if (nchar == K_DEL || nchar == K_KDEL)
2792 n /= 10;
2793 else if (VIM_ISDIGIT(nchar))
2794 n = n * 10 + (nchar - '0');
2795 else if (nchar == CAR)
2796 {
2797#ifdef FEAT_GUI
2798 need_mouse_correct = TRUE;
2799#endif
2800 win_setheight((int)n);
2801 break;
2802 }
2803 else if (nchar == 'l'
2804 || nchar == 'h'
2805 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002806 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
2808 cap->count1 = n ? n * cap->count1 : cap->count1;
2809 goto dozet;
2810 }
2811 else
2812 {
2813 clearopbeep(cap->oap);
2814 break;
2815 }
2816 }
2817 cap->oap->op_type = OP_NOP;
2818 return;
2819 }
2820
2821dozet:
2822 if (
2823#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002824 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2825 // and "zC" only in Visual mode. "zj" and "zk" are motion
2826 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 cap->nchar != 'f' && cap->nchar != 'F'
2828 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2829 && cap->nchar != 'j' && cap->nchar != 'k'
2830 &&
2831#endif
2832 checkclearop(cap->oap))
2833 return;
2834
2835 /*
2836 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2837 * If line number given, set cursor.
2838 */
2839 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2840 && cap->count0
2841 && cap->count0 != curwin->w_cursor.lnum)
2842 {
2843 setpcmark();
2844 if (cap->count0 > curbuf->b_ml.ml_line_count)
2845 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2846 else
2847 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002848 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
2850
2851 switch (nchar)
2852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002853 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 case '+':
2855 if (cap->count0 == 0)
2856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002857 // No count given: put cursor at the line below screen
2858 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2860 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2861 else
2862 curwin->w_cursor.lnum = curwin->w_botline;
2863 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002864 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 case NL:
2866 case CAR:
2867 case K_KENTER:
2868 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002869 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
2871 case 't': scroll_cursor_top(0, TRUE);
2872 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002873 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 break;
2875
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002876 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002878 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
2880 case 'z': scroll_cursor_halfway(TRUE);
2881 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002882 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 break;
2884
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002885 // "z^", "z-" and "zb": put cursor at bottom of screen
2886 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2887 // when <count> is at bottom of window, and puts that one at
2888 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 if (cap->count0 != 0)
2890 {
2891 scroll_cursor_bot(0, TRUE);
2892 curwin->w_cursor.lnum = curwin->w_topline;
2893 }
2894 else if (curwin->w_topline == 1)
2895 curwin->w_cursor.lnum = 1;
2896 else
2897 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002898 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 case '-':
2900 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
2903 case 'b': scroll_cursor_bot(0, TRUE);
2904 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002905 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 break;
2907
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002908 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002910 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002911 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002913 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 case 'h':
2915 case K_LEFT:
2916 if (!curwin->w_p_wrap)
2917 {
2918 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2919 curwin->w_leftcol = 0;
2920 else
2921 curwin->w_leftcol -= (colnr_T)cap->count1;
2922 leftcol_changed();
2923 }
2924 break;
2925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002927 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002928 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002930 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 case 'l':
2932 case K_RIGHT:
2933 if (!curwin->w_p_wrap)
2934 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002935 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 curwin->w_leftcol += (colnr_T)cap->count1;
2937 leftcol_changed();
2938 }
2939 break;
2940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 case 's': if (!curwin->w_p_wrap)
2943 {
2944#ifdef FEAT_FOLDING
2945 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 else
2948#endif
2949 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002950 if ((long)col > siso)
2951 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 else
2953 col = 0;
2954 if (curwin->w_leftcol != col)
2955 {
2956 curwin->w_leftcol = col;
2957 redraw_later(NOT_VALID);
2958 }
2959 }
2960 break;
2961
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002962 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 case 'e': if (!curwin->w_p_wrap)
2964 {
2965#ifdef FEAT_FOLDING
2966 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 else
2969#endif
2970 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002971 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002972 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 col = 0;
2974 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002975 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 if (curwin->w_leftcol != col)
2977 {
2978 curwin->w_leftcol = col;
2979 redraw_later(NOT_VALID);
2980 }
2981 }
2982 break;
2983
Christian Brabandt2fa93842021-05-30 22:17:25 +02002984 // "zp", "zP" in block mode put without addind trailing spaces
2985 case 'P':
2986 case 'p': nv_put(cap);
2987 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002988 // "zy" Yank without trailing spaces
2989 case 'y': nv_operator(cap);
2990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002992 // "zF": create fold command
2993 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 case 'F':
2995 case 'f': if (foldManualAllowed(TRUE))
2996 {
2997 cap->nchar = 'f';
2998 nv_operator(cap);
2999 curwin->w_p_fen = TRUE;
3000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003001 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3003 {
3004 nv_operator(cap);
3005 finish_op = TRUE;
3006 }
3007 }
3008 else
3009 clearopbeep(cap->oap);
3010 break;
3011
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003012 // "zd": delete fold at cursor
3013 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 case 'd':
3015 case 'D': if (foldManualAllowed(FALSE))
3016 {
3017 if (VIsual_active)
3018 nv_operator(cap);
3019 else
3020 deleteFold(curwin->w_cursor.lnum,
3021 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3022 }
3023 break;
3024
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003025 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 case 'E': if (foldmethodIsManual(curwin))
3027 {
3028 clearFolding(curwin);
3029 changed_window_setting();
3030 }
3031 else if (foldmethodIsMarker(curwin))
3032 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3033 TRUE, FALSE);
3034 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003035 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 break;
3037
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003038 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 case 'n': curwin->w_p_fen = FALSE;
3040 break;
3041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003042 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 case 'N': curwin->w_p_fen = TRUE;
3044 break;
3045
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003046 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3048 break;
3049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003050 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3052 openFold(curwin->w_cursor.lnum, cap->count1);
3053 else
3054 {
3055 closeFold(curwin->w_cursor.lnum, cap->count1);
3056 curwin->w_p_fen = TRUE;
3057 }
3058 break;
3059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003060 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3062 openFoldRecurse(curwin->w_cursor.lnum);
3063 else
3064 {
3065 closeFoldRecurse(curwin->w_cursor.lnum);
3066 curwin->w_p_fen = TRUE;
3067 }
3068 break;
3069
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003070 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 case 'o': if (VIsual_active)
3072 nv_operator(cap);
3073 else
3074 openFold(curwin->w_cursor.lnum, cap->count1);
3075 break;
3076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003077 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 case 'O': if (VIsual_active)
3079 nv_operator(cap);
3080 else
3081 openFoldRecurse(curwin->w_cursor.lnum);
3082 break;
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 case 'c': if (VIsual_active)
3086 nv_operator(cap);
3087 else
3088 closeFold(curwin->w_cursor.lnum, cap->count1);
3089 curwin->w_p_fen = TRUE;
3090 break;
3091
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003092 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 case 'C': if (VIsual_active)
3094 nv_operator(cap);
3095 else
3096 closeFoldRecurse(curwin->w_cursor.lnum);
3097 curwin->w_p_fen = TRUE;
3098 break;
3099
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003100 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 case 'v': foldOpenCursor();
3102 break;
3103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003104 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003106 curwin->w_foldinvalid = TRUE; // recompute folds
3107 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 foldOpenCursor();
3109 break;
3110
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003111 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003113 curwin->w_foldinvalid = TRUE; // recompute folds
3114 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 break;
3116
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003117 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003119 {
3120 curwin->w_p_fdl -= cap->count1;
3121 if (curwin->w_p_fdl < 0)
3122 curwin->w_p_fdl = 0;
3123 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003124 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 curwin->w_p_fen = TRUE;
3126 break;
3127
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003128 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003130 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 curwin->w_p_fen = TRUE;
3132 break;
3133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003134 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003135 case 'r': curwin->w_p_fdl += cap->count1;
3136 {
3137 int d = getDeepestNesting();
3138
3139 if (curwin->w_p_fdl >= d)
3140 curwin->w_p_fdl = d;
3141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 break;
3143
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003144 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003146 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 break;
3148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 case 'j': // "zj" move to next fold downwards
3150 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3152 cap->count1) == FAIL)
3153 clearopbeep(cap->oap);
3154 break;
3155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003156#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003158#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003159 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003160 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003161 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003162 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003163 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003164 --no_mapping;
3165 --allow_keys;
3166#ifdef FEAT_CMDL_INFO
3167 (void)add_to_showcmd(nchar);
3168#endif
3169 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3170 {
3171 clearopbeep(cap->oap);
3172 break;
3173 }
3174 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003175 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003176
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003177 case 'g': // "zg": add good word to word list
3178 case 'w': // "zw": add wrong word to word list
3179 case 'G': // "zG": add good word to temp word list
3180 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003181 {
3182 char_u *ptr = NULL;
3183 int len;
3184
3185 if (checkclearop(cap->oap))
3186 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003187 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3188 == FAIL)
3189 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003190 if (ptr == NULL)
3191 {
3192 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003194 // Find bad word under the cursor. When 'spell' is
3195 // off this fails and find_ident_under_cursor() is
3196 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003197 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003198 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003199 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003200 if (len != 0 && curwin->w_cursor.col <= pos.col)
3201 ptr = ml_get_pos(&curwin->w_cursor);
3202 curwin->w_cursor = pos;
3203 }
3204
Bram Moolenaarb765d632005-06-07 21:00:02 +00003205 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3206 FIND_IDENT)) == 0)
3207 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003208 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3209 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003210 (nchar == 'G' || nchar == 'W')
3211 ? 0 : (int)cap->count1,
3212 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003213 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003214 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003215
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003216 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003217 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003218 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003219 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003220#endif
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 default: clearopbeep(cap->oap);
3223 }
3224
3225#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003226 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 if (old_fen != curwin->w_p_fen)
3228 {
3229# ifdef FEAT_DIFF
3230 win_T *wp;
3231
3232 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3233 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003234 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 FOR_ALL_WINDOWS(wp)
3236 {
3237 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3238 {
3239 wp->w_p_fen = curwin->w_p_fen;
3240 changed_window_setting_win(wp);
3241 }
3242 }
3243 }
3244# endif
3245 changed_window_setting();
3246 }
3247
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003248 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 if (old_fdl != curwin->w_p_fdl)
3250 newFoldLevel();
3251#endif
3252}
3253
3254#ifdef FEAT_GUI
3255/*
3256 * Vertical scrollbar movement.
3257 */
3258 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003259nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 if (cap->oap->op_type != OP_NOP)
3262 clearopbeep(cap->oap);
3263
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003264 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 gui_do_scroll();
3266}
3267
3268/*
3269 * Horizontal scrollbar movement.
3270 */
3271 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003272nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 if (cap->oap->op_type != OP_NOP)
3275 clearopbeep(cap->oap);
3276
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003277 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003278 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279}
3280#endif
3281
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003282#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003283/*
3284 * Click in GUI tab.
3285 */
3286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003287nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003288{
3289 if (cap->oap->op_type != OP_NOP)
3290 clearopbeep(cap->oap);
3291
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003292 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003293 goto_tabpage(current_tab);
3294}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003295
3296/*
3297 * Selected item in tab line menu.
3298 */
3299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003300nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003301{
3302 if (cap->oap->op_type != OP_NOP)
3303 clearopbeep(cap->oap);
3304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003305 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003306 handle_tabmenu();
3307}
3308
3309/*
3310 * Handle selecting an item of the GUI tab line menu.
3311 * Used in Normal and Insert mode.
3312 */
3313 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003314handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003315{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003316 switch (current_tabmenu)
3317 {
3318 case TABLINE_MENU_CLOSE:
3319 if (current_tab == 0)
3320 do_cmdline_cmd((char_u *)"tabclose");
3321 else
3322 {
3323 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3324 current_tab);
3325 do_cmdline_cmd(IObuff);
3326 }
3327 break;
3328
3329 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003330 if (current_tab == 0)
3331 do_cmdline_cmd((char_u *)"$tabnew");
3332 else
3333 {
3334 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3335 current_tab - 1);
3336 do_cmdline_cmd(IObuff);
3337 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003338 break;
3339
3340 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003341 if (current_tab == 0)
3342 do_cmdline_cmd((char_u *)"browse $tabnew");
3343 else
3344 {
3345 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3346 current_tab - 1);
3347 do_cmdline_cmd(IObuff);
3348 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003349 break;
3350 }
3351}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003352#endif
3353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354/*
3355 * "Q" command.
3356 */
3357 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003358nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 /*
3361 * Ignore 'Q' in Visual mode, just give a beep.
3362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003364 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003365 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 do_exmode(FALSE);
3367}
3368
3369/*
3370 * Handle a ":" command.
3371 */
3372 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003373nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003375 int old_p_im;
3376 int cmd_result;
3377 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
Bram Moolenaar957cf672020-11-12 14:21:06 +01003379 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 nv_operator(cap);
3381 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 {
3383 if (cap->oap->op_type != OP_NOP)
3384 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003385 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 cap->oap->motion_type = MCHAR;
3387 cap->oap->inclusive = FALSE;
3388 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003389 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003391 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 stuffcharReadbuff('.');
3393 if (cap->count0 > 1)
3394 {
3395 stuffReadbuff((char_u *)",.+");
3396 stuffnumReadbuff((long)cap->count0 - 1L);
3397 }
3398 }
3399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003400 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (KeyTyped)
3402 compute_cmdrow();
3403
3404 old_p_im = p_im;
3405
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003406 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003407 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003410 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (p_im != old_p_im)
3412 {
3413 if (p_im)
3414 restart_edit = 'i';
3415 else
3416 restart_edit = 0;
3417 }
3418
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003419 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003420 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003421 clearop(cap->oap);
3422 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3424 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003425 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3426 || did_emsg
3427 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003428 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 clearopbeep(cap->oap);
3430 }
3431}
3432
3433/*
3434 * Handle CTRL-G command.
3435 */
3436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003437nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
3441 VIsual_select = !VIsual_select;
3442 showmode();
3443 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003444 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003445 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 fileinfo((int)cap->count0, FALSE, TRUE);
3447}
3448
3449/*
3450 * Handle CTRL-H <Backspace> command.
3451 */
3452 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003453nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (VIsual_active && VIsual_select)
3456 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003457 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 v_visop(cap);
3459 }
3460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 nv_left(cap);
3462}
3463
3464/*
3465 * CTRL-L: clear screen and redraw.
3466 */
3467 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003468nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
3470 if (!checkclearop(cap->oap))
3471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003473 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003474 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003475# ifdef FEAT_RELTIME
3476 {
3477 win_T *wp;
3478
3479 FOR_ALL_WINDOWS(wp)
3480 wp->w_s->b_syn_slow = FALSE;
3481 }
3482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483#endif
3484 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003485#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3486# ifdef VIMDLL
3487 if (!gui.in_use)
3488# endif
3489 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
3492}
3493
3494/*
3495 * CTRL-O: In Select mode: switch to Visual mode for one command.
3496 * Otherwise: Go to older pcmark.
3497 */
3498 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003499nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (VIsual_active && VIsual_select)
3502 {
3503 VIsual_select = FALSE;
3504 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003505 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 cap->count1 = -cap->count1;
3510 nv_pcmark(cap);
3511 }
3512}
3513
3514/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003515 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3516 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 */
3518 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003519nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
3521 if (!checkclearopq(cap->oap))
3522 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3523 GETF_SETMARK|GETF_ALT, FALSE);
3524}
3525
3526/*
3527 * "Z" commands.
3528 */
3529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003530nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532 if (!checkclearopq(cap->oap))
3533 {
3534 switch (cap->nchar)
3535 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003536 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 case 'Z': do_cmdline_cmd((char_u *)"x");
3538 break;
3539
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003540 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 case 'Q': do_cmdline_cmd((char_u *)"q!");
3542 break;
3543
3544 default: clearopbeep(cap->oap);
3545 }
3546 }
3547}
3548
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549/*
3550 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3551 */
3552 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003553do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
3555 oparg_T oa;
3556 cmdarg_T ca;
3557
3558 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003559 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 ca.oap = &oa;
3561 ca.cmdchar = c1;
3562 ca.nchar = c2;
3563 nv_ident(&ca);
3564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566/*
3567 * Handle the commands that use the word under the cursor.
3568 * [g] CTRL-] :ta to current identifier
3569 * [g] 'K' run program for current identifier
3570 * [g] '*' / to current identifier or string
3571 * [g] '#' ? to current identifier or string
3572 * g ']' :tselect for current identifier
3573 */
3574 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003575nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
3577 char_u *ptr = NULL;
3578 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003579 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003580 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003582 char_u *kp; // value of 'keywordprg'
3583 int kp_help; // 'keywordprg' is ":he"
3584 int kp_ex; // 'keywordprg' starts with ":"
3585 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003587 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003588 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 char_u *aux_ptr;
3590 int isman;
3591 int isman_s;
3592
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003593 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 cmdchar = cap->nchar;
3596 g_cmd = TRUE;
3597 }
3598 else
3599 {
3600 cmdchar = cap->cmdchar;
3601 g_cmd = FALSE;
3602 }
3603
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003604 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 cmdchar = '#';
3606
3607 /*
3608 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3609 */
3610 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3613 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (checkclearopq(cap->oap))
3615 return;
3616 }
3617
3618 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3619 (cmdchar == '*' || cmdchar == '#')
3620 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3621 {
3622 clearop(cap->oap);
3623 return;
3624 }
3625
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003626 // Allocate buffer to put the command in. Inserting backslashes can
3627 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3628 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3630 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3631 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003632 if (kp_help && *skipwhite(ptr) == NUL)
3633 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003634 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003635 return;
3636 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003637 kp_ex = (*kp == ':');
3638 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3639 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (buf == NULL)
3641 return;
3642 buf[0] = NUL;
3643
3644 switch (cmdchar)
3645 {
3646 case '*':
3647 case '#':
3648 /*
3649 * Put cursor at start of word, makes search skip the word
3650 * under the cursor.
3651 * Call setpcmark() first, so "*``" puts the cursor back where
3652 * it was.
3653 */
3654 setpcmark();
3655 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3656
3657 if (!g_cmd && vim_iswordp(ptr))
3658 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003659 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 break;
3661
3662 case 'K':
3663 if (kp_help)
3664 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003665 else if (kp_ex)
3666 {
3667 if (cap->count0 != 0)
3668 vim_snprintf((char *)buf, buflen, "%s %ld",
3669 kp, cap->count0);
3670 else
3671 STRCPY(buf, kp);
3672 STRCAT(buf, " ");
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 else
3675 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003676 // An external command will probably use an argument starting
3677 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003678 while (*ptr == '-' && n > 0)
3679 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003680 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003681 --n;
3682 }
3683 if (n == 0)
3684 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003685 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003686 vim_free(buf);
3687 return;
3688 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003689
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003690 // When a count is given, turn it into a range. Is this
3691 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 isman = (STRCMP(kp, "man") == 0);
3693 isman_s = (STRCMP(kp, "man -s") == 0);
3694 if (cap->count0 != 0 && !(isman || isman_s))
3695 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3696
3697 STRCAT(buf, "! ");
3698 if (cap->count0 == 0 && isman_s)
3699 STRCAT(buf, "man");
3700 else
3701 STRCAT(buf, kp);
3702 STRCAT(buf, " ");
3703 if (cap->count0 != 0 && (isman || isman_s))
3704 {
3705 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3706 STRCAT(buf, " ");
3707 }
3708 }
3709 break;
3710
3711 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003712 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713#ifdef FEAT_CSCOPE
3714 if (p_cst)
3715 STRCPY(buf, "cstag ");
3716 else
3717#endif
3718 STRCPY(buf, "ts ");
3719 break;
3720
3721 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003722 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (curbuf->b_help)
3724 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003726 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003727 if (g_cmd)
3728 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003729 else if (cap->count0 == 0)
3730 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003731 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003732 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 }
3735
3736 /*
3737 * Now grab the chars in the identifier
3738 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003739 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003741 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003742 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003743 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003744 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003745 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003746 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003747 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003748 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003749 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003751 vim_free(buf);
3752 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003754 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003755 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003756 {
3757 vim_free(buf);
3758 vim_free(p);
3759 return;
3760 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003761 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003762 STRCAT(buf, p);
3763 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003765 else
3766 {
3767 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003768 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003769 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003770 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003771 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003772 {
3773 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003774 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003775 aux_ptr = (char_u *)"";
3776 else
3777 aux_ptr = (char_u *)"\\|\"\n[";
3778 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003779 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003780 aux_ptr = (char_u *)"\\|\"\n*?[";
3781
3782 p = buf + STRLEN(buf);
3783 while (n-- > 0)
3784 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003785 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003786 if (vim_strchr(aux_ptr, *ptr) != NULL)
3787 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003788 // When current byte is a part of multibyte character, copy all
3789 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003790 if (has_mbyte)
3791 {
3792 int i;
3793 int len = (*mb_ptr2len)(ptr) - 1;
3794
3795 for (i = 0; i < len && n >= 1; ++i, --n)
3796 *p++ = *ptr++;
3797 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003798 *p++ = *ptr++;
3799 }
3800 *p = NUL;
3801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
3803 /*
3804 * Execute the command.
3805 */
3806 if (cmdchar == '*' || cmdchar == '#')
3807 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003808 if (!g_cmd && (has_mbyte
3809 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3810 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003812
3813 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003814 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003816
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003817 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003820 {
3821 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003823 g_tag_at_cursor = FALSE;
3824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 vim_free(buf);
3827}
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829/*
3830 * Get visually selected text, within one line only.
3831 * Returns FAIL if more than one line selected.
3832 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003833 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003834get_visual_text(
3835 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003836 char_u **pp, // return: start of selected text
3837 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838{
3839 if (VIsual_mode != 'V')
3840 unadjust_for_sel();
3841 if (VIsual.lnum != curwin->w_cursor.lnum)
3842 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003843 if (cap != NULL)
3844 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 return FAIL;
3846 }
3847 if (VIsual_mode == 'V')
3848 {
3849 *pp = ml_get_curline();
3850 *lenp = (int)STRLEN(*pp);
3851 }
3852 else
3853 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003854 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
3856 *pp = ml_get_pos(&curwin->w_cursor);
3857 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3858 }
3859 else
3860 {
3861 *pp = ml_get_pos(&VIsual);
3862 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003865 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003866 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868 reset_VIsual_and_resel();
3869 return OK;
3870}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
3872/*
3873 * CTRL-T: backwards in tag stack
3874 */
3875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003876nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 if (!checkclearopq(cap->oap))
3879 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3880}
3881
3882/*
3883 * Handle scrolling command 'H', 'L' and 'M'.
3884 */
3885 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003886nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887{
3888 int used = 0;
3889 long n;
3890#ifdef FEAT_FOLDING
3891 linenr_T lnum;
3892#endif
3893 int half;
3894
3895 cap->oap->motion_type = MLINE;
3896 setpcmark();
3897
3898 if (cap->cmdchar == 'L')
3899 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003900 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 curwin->w_cursor.lnum = curwin->w_botline - 1;
3902 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3903 curwin->w_cursor.lnum = 1;
3904 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003905 {
3906#ifdef FEAT_FOLDING
3907 if (hasAnyFolding(curwin))
3908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003909 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003910 for (n = cap->count1 - 1; n > 0
3911 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3912 {
3913 (void)hasFolding(curwin->w_cursor.lnum,
3914 &curwin->w_cursor.lnum, NULL);
3915 --curwin->w_cursor.lnum;
3916 }
3917 }
3918 else
3919#endif
3920 curwin->w_cursor.lnum -= cap->count1 - 1;
3921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923 else
3924 {
3925 if (cap->cmdchar == 'M')
3926 {
3927#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003928 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 used -= diff_check_fill(curwin, curwin->w_topline)
3930 - curwin->w_topfill;
3931#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003932 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3934 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3935 {
3936#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003937 // Count half he number of filler lines to be "below this
3938 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3940 + n) / 2 >= half)
3941 {
3942 --n;
3943 break;
3944 }
3945#endif
3946 used += plines(curwin->w_topline + n);
3947 if (used >= half)
3948 break;
3949#ifdef FEAT_FOLDING
3950 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3951 n = lnum - curwin->w_topline;
3952#endif
3953 }
3954 if (n > 0 && used > curwin->w_height)
3955 --n;
3956 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003957 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003960#ifdef FEAT_FOLDING
3961 if (hasAnyFolding(curwin))
3962 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003963 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003964 lnum = curwin->w_topline;
3965 while (n-- > 0 && lnum < curwin->w_botline - 1)
3966 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003967 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003968 ++lnum;
3969 }
3970 n = lnum - curwin->w_topline;
3971 }
3972#endif
3973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 curwin->w_cursor.lnum = curwin->w_topline + n;
3975 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3976 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3977 }
3978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003979 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003980 if (cap->oap->op_type == OP_NOP)
3981 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 beginline(BL_SOL | BL_FIX);
3983}
3984
3985/*
3986 * Cursor right commands.
3987 */
3988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003989nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
3991 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003992 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3995 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003996 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003997 if (mod_mask & MOD_MASK_CTRL)
3998 cap->arg = TRUE;
3999 nv_wordcmd(cap);
4000 return;
4001 }
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 cap->oap->motion_type = MCHAR;
4004 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004005 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004008 * In virtual edit mode, there's no such thing as "past_line", as lines
4009 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 */
4011 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004012 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
4014 for (n = cap->count1; n > 0; --n)
4015 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004016 if ((!past_line && oneright() == FAIL)
4017 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004018 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
4020 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004021 * <Space> wraps to next line if 'whichwrap' has 's'.
4022 * 'l' wraps to next line if 'whichwrap' has 'l'.
4023 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 */
4025 if ( ((cap->cmdchar == ' '
4026 && vim_strchr(p_ww, 's') != NULL)
4027 || (cap->cmdchar == 'l'
4028 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004029 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 && vim_strchr(p_ww, '>') != NULL))
4031 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4032 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004033 // When deleting we also count the NL as a character.
4034 // Set cap->oap->inclusive when last char in the line is
4035 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004036 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004038 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 cap->oap->inclusive = TRUE;
4040 else
4041 {
4042 ++curwin->w_cursor.lnum;
4043 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 curwin->w_set_curswant = TRUE;
4046 cap->oap->inclusive = FALSE;
4047 }
4048 continue;
4049 }
4050 if (cap->oap->op_type == OP_NOP)
4051 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004052 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (n == cap->count1)
4054 beep_flush();
4055 }
4056 else
4057 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004058 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 cap->oap->inclusive = TRUE;
4060 }
4061 break;
4062 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004063 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
4065 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (virtual_active())
4067 oneright();
4068 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004071 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 ++curwin->w_cursor.col;
4074 }
4075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077#ifdef FEAT_FOLDING
4078 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4079 && cap->oap->op_type == OP_NOP)
4080 foldOpenCursor();
4081#endif
4082}
4083
4084/*
4085 * Cursor left commands.
4086 *
4087 * Returns TRUE when operator end should not be adjusted.
4088 */
4089 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004090nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091{
4092 long n;
4093
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004094 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4095 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004096 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004097 if (mod_mask & MOD_MASK_CTRL)
4098 cap->arg = 1;
4099 nv_bck_word(cap);
4100 return;
4101 }
4102
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 cap->oap->motion_type = MCHAR;
4104 cap->oap->inclusive = FALSE;
4105 for (n = cap->count1; n > 0; --n)
4106 {
4107 if (oneleft() == FAIL)
4108 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004109 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4110 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4111 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 if ( (((cap->cmdchar == K_BS
4113 || cap->cmdchar == Ctrl_H)
4114 && vim_strchr(p_ww, 'b') != NULL)
4115 || (cap->cmdchar == 'h'
4116 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004117 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 && vim_strchr(p_ww, '<') != NULL))
4119 && curwin->w_cursor.lnum > 1)
4120 {
4121 --(curwin->w_cursor.lnum);
4122 coladvance((colnr_T)MAXCOL);
4123 curwin->w_set_curswant = TRUE;
4124
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004125 // When the NL before the first char has to be deleted we
4126 // put the cursor on the NUL after the previous line.
4127 // This is a very special case, be careful!
4128 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 if ( (cap->oap->op_type == OP_DELETE
4130 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004131 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004133 char_u *cp = ml_get_cursor();
4134
4135 if (*cp != NUL)
4136 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004137 if (has_mbyte)
4138 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4139 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004140 ++curwin->w_cursor.col;
4141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 cap->retval |= CA_NO_ADJ_OP_END;
4143 }
4144 continue;
4145 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004146 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4148 beep_flush();
4149 break;
4150 }
4151 }
4152#ifdef FEAT_FOLDING
4153 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4154 && cap->oap->op_type == OP_NOP)
4155 foldOpenCursor();
4156#endif
4157}
4158
4159/*
4160 * Cursor up commands.
4161 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4162 */
4163 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004164nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004166 if (mod_mask & MOD_MASK_SHIFT)
4167 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004168 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004169 cap->arg = BACKWARD;
4170 nv_page(cap);
4171 }
4172 else
4173 {
4174 cap->oap->motion_type = MLINE;
4175 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4176 clearopbeep(cap->oap);
4177 else if (cap->arg)
4178 beginline(BL_WHITE | BL_FIX);
4179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180}
4181
4182/*
4183 * Cursor down commands.
4184 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4185 */
4186 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004187nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004189 if (mod_mask & MOD_MASK_SHIFT)
4190 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004191 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004192 cap->arg = FORWARD;
4193 nv_page(cap);
4194 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004195#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004196 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004197 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4198 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004200 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
4202#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004203 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004204 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 cmdwin_result = CAR;
4206 else
4207#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004208#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004209 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004210 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4211 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4212 {
4213 invoke_prompt_callback();
4214 if (restart_edit == 0)
4215 restart_edit = 'a';
4216 }
4217 else
4218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
4220 cap->oap->motion_type = MLINE;
4221 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4222 clearopbeep(cap->oap);
4223 else if (cap->arg)
4224 beginline(BL_WHITE | BL_FIX);
4225 }
4226 }
4227}
4228
4229#ifdef FEAT_SEARCHPATH
4230/*
4231 * Grab the file name under the cursor and edit it.
4232 */
4233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004234nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
4236 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004237 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004239 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
4241 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004242 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 return;
4244 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004245 if (curbuf_locked())
4246 {
4247 clearop(cap->oap);
4248 return;
4249 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004250#ifdef FEAT_PROP_POPUP
4251 if (ERROR_IF_TERM_POPUP_WINDOW)
4252 return;
4253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004255 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 if (ptr != NULL)
4258 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004259 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004260 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004261 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004263 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004264 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004265 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004266 {
4267 curwin->w_cursor.lnum = lnum;
4268 check_cursor_lnum();
4269 beginline(BL_SOL | BL_FIX);
4270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 vim_free(ptr);
4272 }
4273 else
4274 clearop(cap->oap);
4275}
4276#endif
4277
4278/*
4279 * <End> command: to end of current line or last line.
4280 */
4281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004282nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004284 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004286 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004288 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 nv_dollar(cap);
4291}
4292
4293/*
4294 * Handle the "$" command.
4295 */
4296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004297nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
4299 cap->oap->motion_type = MCHAR;
4300 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004301 // In virtual mode when off the edge of a line and an operator
4302 // is pending (whew!) keep the cursor where it is.
4303 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (!virtual_active() || gchar_cursor() != NUL
4305 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004306 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 if (cursor_down((long)(cap->count1 - 1),
4308 cap->oap->op_type == OP_NOP) == FAIL)
4309 clearopbeep(cap->oap);
4310#ifdef FEAT_FOLDING
4311 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4312 foldOpenCursor();
4313#endif
4314}
4315
4316/*
4317 * Implementation of '?' and '/' commands.
4318 * If cap->arg is TRUE don't set PC mark.
4319 */
4320 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004321nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
4323 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004324 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
4326 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4327 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004328 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 cap->cmdchar = 'g';
4330 cap->nchar = '?';
4331 nv_operator(cap);
4332 return;
4333 }
4334
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004335 // When using 'incsearch' the cursor may be moved to set a different search
4336 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004337 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 if (cap->searchbuf == NULL)
4340 {
4341 clearop(oap);
4342 return;
4343 }
4344
Bram Moolenaar46539112015-02-17 15:43:57 +01004345 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004346 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004347 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348}
4349
4350/*
4351 * Handle "N" and "n" commands.
4352 * cap->arg is SEARCH_REV for "N", 0 for "n".
4353 */
4354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004355nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004357 pos_T old = curwin->w_cursor;
4358 int wrapped = FALSE;
4359 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004360
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004361 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004362 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004363 // Avoid getting stuck on the current cursor position, which can
4364 // happen when an offset is given and the cursor is on the last char
4365 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004366 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004367 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004368 cap->count1 -= 1;
4369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370}
4371
4372/*
4373 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4374 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004375 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004377 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004378normal_search(
4379 cmdarg_T *cap,
4380 int dir,
4381 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004382 int opt, // extra flags for do_search()
4383 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004386 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388 cap->oap->motion_type = MCHAR;
4389 cap->oap->inclusive = FALSE;
4390 cap->oap->use_reg_one = TRUE;
4391 curwin->w_set_curswant = TRUE;
4392
Bram Moolenaara80faa82020-04-12 19:37:17 +02004393 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004394 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004395 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4396 if (wrapped != NULL)
4397 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 if (i == 0)
4399 clearop(cap->oap);
4400 else
4401 {
4402 if (i == 2)
4403 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405#ifdef FEAT_FOLDING
4406 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4407 foldOpenCursor();
4408#endif
4409 }
4410
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004411 // "/$" will put the cursor after the end of the line, may need to
4412 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004414 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415}
4416
4417/*
4418 * Character search commands.
4419 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4420 * ',' and FALSE for ';'.
4421 * cap->nchar is NUL for ',' and ';' (repeat the search)
4422 */
4423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004424nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
4426 int t_cmd;
4427
4428 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4429 t_cmd = TRUE;
4430 else
4431 t_cmd = FALSE;
4432
4433 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4435 clearopbeep(cap->oap);
4436 else
4437 {
4438 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004439 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4441 && (t_cmd || cap->oap->op_type != OP_NOP))
4442 {
4443 colnr_T scol, ecol;
4444
4445 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4446 curwin->w_cursor.coladd = ecol - scol;
4447 }
4448 else
4449 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#ifdef FEAT_FOLDING
4452 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4453 foldOpenCursor();
4454#endif
4455 }
4456}
4457
4458/*
4459 * "[" and "]" commands.
4460 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4461 */
4462 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004463nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004465 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004467 pos_T *pos = NULL; // init for GCC
4468 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 int flag;
4470 long n;
4471 int findc;
4472 int c;
4473
4474 cap->oap->motion_type = MCHAR;
4475 cap->oap->inclusive = FALSE;
4476 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004477 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479#ifdef FEAT_SEARCHPATH
4480 /*
4481 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4482 */
4483 if (cap->nchar == 'f')
4484 nv_gotofile(cap);
4485 else
4486#endif
4487
4488#ifdef FEAT_FIND_ID
4489 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004490 * Find the occurrence(s) of the identifier or define under cursor
4491 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 *
4493 * search list jump
4494 * fwd bwd fwd bwd fwd bwd
4495 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4496 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4497 */
4498 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004499# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004501# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004503# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 cap->nchar) != NULL)
4505 {
4506 char_u *ptr;
4507 int len;
4508
4509 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4510 clearop(cap->oap);
4511 else
4512 {
4513 find_pattern_in_path(ptr, 0, len, TRUE,
4514 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4515 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4516 cap->count1,
4517 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4518 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4519 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4520 (linenr_T)MAXLNUM);
4521 curwin->w_set_curswant = TRUE;
4522 }
4523 }
4524 else
4525#endif
4526
4527 /*
4528 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4529 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4530 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4531 * "[m" or "]m" search for prev/next start of (Java) method.
4532 * "[M" or "]M" search for prev/next end of (Java) method.
4533 */
4534 if ( (cap->cmdchar == '['
4535 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4536 || (cap->cmdchar == ']'
4537 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4538 {
4539 if (cap->nchar == '*')
4540 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 prev_pos.lnum = 0;
4542 if (cap->nchar == 'm' || cap->nchar == 'M')
4543 {
4544 if (cap->cmdchar == '[')
4545 findc = '{';
4546 else
4547 findc = '}';
4548 n = 9999;
4549 }
4550 else
4551 {
4552 findc = cap->nchar;
4553 n = cap->count1;
4554 }
4555 for ( ; n > 0; --n)
4556 {
4557 if ((pos = findmatchlimit(cap->oap, findc,
4558 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4559 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004560 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
4562 if (cap->nchar != 'm' && cap->nchar != 'M')
4563 clearopbeep(cap->oap);
4564 }
4565 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004566 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 break;
4568 }
4569 prev_pos = new_pos;
4570 curwin->w_cursor = *pos;
4571 new_pos = *pos;
4572 }
4573 curwin->w_cursor = old_pos;
4574
4575 /*
4576 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4577 * brought us to the match for "[m" and "]M" when inside a method.
4578 * Try finding the '{' or '}' we want to be at.
4579 * Also repeat for the given count.
4580 */
4581 if (cap->nchar == 'm' || cap->nchar == 'M')
4582 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004583 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 int norm = ((findc == '{') == (cap->nchar == 'm'));
4585
4586 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004587 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (prev_pos.lnum != 0)
4589 {
4590 pos = &prev_pos;
4591 curwin->w_cursor = prev_pos;
4592 if (norm)
4593 --n;
4594 }
4595 else
4596 pos = NULL;
4597 while (n > 0)
4598 {
4599 for (;;)
4600 {
4601 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4602 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004603 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (pos == NULL)
4605 clearopbeep(cap->oap);
4606 n = 0;
4607 break;
4608 }
4609 c = gchar_cursor();
4610 if (c == '{' || c == '}')
4611 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004612 // Must have found end/start of class: use it.
4613 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if ((c == findc && norm) || (n == 1 && !norm))
4615 {
4616 new_pos = curwin->w_cursor;
4617 pos = &new_pos;
4618 n = 0;
4619 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004620 // if no match found at all, we started outside of the
4621 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 else if (new_pos.lnum == 0)
4623 {
4624 new_pos = curwin->w_cursor;
4625 pos = &new_pos;
4626 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004627 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 else if ((pos = findmatchlimit(cap->oap, findc,
4629 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4630 0)) == NULL)
4631 n = 0;
4632 else
4633 curwin->w_cursor = *pos;
4634 break;
4635 }
4636 }
4637 --n;
4638 }
4639 curwin->w_cursor = old_pos;
4640 if (pos == NULL && new_pos.lnum != 0)
4641 clearopbeep(cap->oap);
4642 }
4643 if (pos != NULL)
4644 {
4645 setpcmark();
4646 curwin->w_cursor = *pos;
4647 curwin->w_set_curswant = TRUE;
4648#ifdef FEAT_FOLDING
4649 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4650 && cap->oap->op_type == OP_NOP)
4651 foldOpenCursor();
4652#endif
4653 }
4654 }
4655
4656 /*
4657 * "[[", "[]", "]]" and "][": move to start or end of function
4658 */
4659 else if (cap->nchar == '[' || cap->nchar == ']')
4660 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004661 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 flag = '{';
4663 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004664 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
4666 curwin->w_set_curswant = TRUE;
4667 /*
4668 * Imitate strange Vi behaviour: When using "]]" with an operator
4669 * we also stop at '}'.
4670 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004671 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 (cap->oap->op_type != OP_NOP
4673 && cap->arg == FORWARD && flag == '{')))
4674 clearopbeep(cap->oap);
4675 else
4676 {
4677 if (cap->oap->op_type == OP_NOP)
4678 beginline(BL_WHITE | BL_FIX);
4679#ifdef FEAT_FOLDING
4680 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4681 foldOpenCursor();
4682#endif
4683 }
4684 }
4685
4686 /*
4687 * "[p", "[P", "]P" and "]p": put with indent adjustment
4688 */
4689 else if (cap->nchar == 'p' || cap->nchar == 'P')
4690 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004691 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
4693
4694 /*
4695 * "['", "[`", "]'" and "]`": jump to next mark
4696 */
4697 else if (cap->nchar == '\'' || cap->nchar == '`')
4698 {
4699 pos = &curwin->w_cursor;
4700 for (n = cap->count1; n > 0; --n)
4701 {
4702 prev_pos = *pos;
4703 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4704 cap->nchar == '\'');
4705 if (pos == NULL)
4706 break;
4707 }
4708 if (pos == NULL)
4709 pos = &prev_pos;
4710 nv_cursormark(cap, cap->nchar == '\'', pos);
4711 }
4712
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 /*
4714 * [ or ] followed by a middle mouse click: put selected text with
4715 * indent adjustment. Any other button just does as usual.
4716 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004717 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
4719 (void)do_mouse(cap->oap, cap->nchar,
4720 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4721 cap->count1, PUT_FIXINDENT);
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
4724#ifdef FEAT_FOLDING
4725 /*
4726 * "[z" and "]z": move to start or end of open fold.
4727 */
4728 else if (cap->nchar == 'z')
4729 {
4730 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4731 cap->count1) == FAIL)
4732 clearopbeep(cap->oap);
4733 }
4734#endif
4735
4736#ifdef FEAT_DIFF
4737 /*
4738 * "[c" and "]c": move to next or previous diff-change.
4739 */
4740 else if (cap->nchar == 'c')
4741 {
4742 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4743 cap->count1) == FAIL)
4744 clearopbeep(cap->oap);
4745 }
4746#endif
4747
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004748#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004749 /*
4750 * "[s", "[S", "]s" and "]S": move to next spell error.
4751 */
4752 else if (cap->nchar == 's' || cap->nchar == 'S')
4753 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004754 setpcmark();
4755 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004756 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4757 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004758 {
4759 clearopbeep(cap->oap);
4760 break;
4761 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004762 else
4763 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004764# ifdef FEAT_FOLDING
4765 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4766 foldOpenCursor();
4767# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004768 }
4769#endif
4770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004771 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 else
4773 clearopbeep(cap->oap);
4774}
4775
4776/*
4777 * Handle Normal mode "%" command.
4778 */
4779 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004780nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
4782 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004783#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 linenr_T lnum = curwin->w_cursor.lnum;
4785#endif
4786
4787 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004788 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
4790 if (cap->count0 > 100)
4791 clearopbeep(cap->oap);
4792 else
4793 {
4794 cap->oap->motion_type = MLINE;
4795 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004796 // Round up, so 'normal 100%' always jumps at the line line.
4797 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4798 // overflow on 32-bits, so use a formula with less accuracy
4799 // to avoid overflows.
4800 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4802 / 100L * cap->count0;
4803 else
4804 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4805 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004806 if (curwin->w_cursor.lnum < 1)
4807 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4809 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4810 beginline(BL_SOL | BL_FIX);
4811 }
4812 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
4815 cap->oap->motion_type = MCHAR;
4816 cap->oap->use_reg_one = TRUE;
4817 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4818 clearopbeep(cap->oap);
4819 else
4820 {
4821 setpcmark();
4822 curwin->w_cursor = *pos;
4823 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827 }
4828#ifdef FEAT_FOLDING
4829 if (cap->oap->op_type == OP_NOP
4830 && lnum != curwin->w_cursor.lnum
4831 && (fdo_flags & FDO_PERCENT)
4832 && KeyTyped)
4833 foldOpenCursor();
4834#endif
4835}
4836
4837/*
4838 * Handle "(" and ")" commands.
4839 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4840 */
4841 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004842nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843{
4844 cap->oap->motion_type = MCHAR;
4845 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004846 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004847 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 curwin->w_set_curswant = TRUE;
4849
4850 if (findsent(cap->arg, cap->count1) == FAIL)
4851 clearopbeep(cap->oap);
4852 else
4853 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004854 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004855 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#ifdef FEAT_FOLDING
4858 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4859 foldOpenCursor();
4860#endif
4861 }
4862}
4863
4864/*
4865 * "m" command: Mark a position.
4866 */
4867 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004868nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869{
4870 if (!checkclearop(cap->oap))
4871 {
4872 if (setmark(cap->nchar) == FAIL)
4873 clearopbeep(cap->oap);
4874 }
4875}
4876
4877/*
4878 * "{" and "}" commands.
4879 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4880 */
4881 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004882nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
4884 cap->oap->motion_type = MCHAR;
4885 cap->oap->inclusive = FALSE;
4886 cap->oap->use_reg_one = TRUE;
4887 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004888 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 clearopbeep(cap->oap);
4890 else
4891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#ifdef FEAT_FOLDING
4894 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4895 foldOpenCursor();
4896#endif
4897 }
4898}
4899
4900/*
4901 * "u" command: Undo or make lower case.
4902 */
4903 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004904nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004906 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004908 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 cap->cmdchar = 'g';
4910 cap->nchar = 'u';
4911 nv_operator(cap);
4912 }
4913 else
4914 nv_kundo(cap);
4915}
4916
4917/*
4918 * <Undo> command.
4919 */
4920 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004921nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
4923 if (!checkclearopq(cap->oap))
4924 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004925#ifdef FEAT_JOB_CHANNEL
4926 if (bt_prompt(curbuf))
4927 {
4928 clearopbeep(cap->oap);
4929 return;
4930 }
4931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 u_undo((int)cap->count1);
4933 curwin->w_set_curswant = TRUE;
4934 }
4935}
4936
4937/*
4938 * Handle the "r" command.
4939 */
4940 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004941nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942{
4943 char_u *ptr;
4944 int had_ctrl_v;
4945 long n;
4946
4947 if (checkclearop(cap->oap))
4948 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004949#ifdef FEAT_JOB_CHANNEL
4950 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4951 {
4952 clearopbeep(cap->oap);
4953 return;
4954 }
4955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004957 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (cap->nchar == Ctrl_V)
4959 {
4960 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004961 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004962 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (cap->nchar > DEL)
4964 had_ctrl_v = NUL;
4965 }
4966 else
4967 had_ctrl_v = NUL;
4968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004969 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004970 if (IS_SPECIAL(cap->nchar))
4971 {
4972 clearopbeep(cap->oap);
4973 return;
4974 }
4975
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004976 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (VIsual_active)
4978 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004979 if (got_int)
4980 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004981 if (had_ctrl_v)
4982 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004983 // Use a special (negative) number to make a difference between a
4984 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004985 if (cap->nchar == CAR)
4986 cap->nchar = REPLACE_CR_NCHAR;
4987 else if (cap->nchar == NL)
4988 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 nv_operator(cap);
4991 return;
4992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004994 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (virtual_active())
4996 {
4997 if (u_save_cursor() == FAIL)
4998 return;
4999 if (gchar_cursor() == NUL)
5000 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005001 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 coladvance_force((colnr_T)(getviscol() + cap->count1));
5003 curwin->w_cursor.col -= cap->count1;
5004 }
5005 else if (gchar_cursor() == TAB)
5006 coladvance_force(getviscol());
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005009 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005011 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005012 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
5014 clearopbeep(cap->oap);
5015 return;
5016 }
5017
5018 /*
5019 * Replacing with a TAB is done by edit() when it is complicated because
5020 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5021 * Other characters are done below to avoid problems with things like
5022 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5023 */
5024 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5025 {
5026 stuffnumReadbuff(cap->count1);
5027 stuffcharReadbuff('R');
5028 stuffcharReadbuff('\t');
5029 stuffcharReadbuff(ESC);
5030 return;
5031 }
5032
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005033 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (u_save_cursor() == FAIL)
5035 return;
5036
5037 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5038 {
5039 /*
5040 * Replace character(s) by a single newline.
5041 * Strange vi behaviour: Only one newline is inserted.
5042 * Delete the characters here.
5043 * Insert the newline with an insert command, takes care of
5044 * autoindent. The insert command depends on being on the last
5045 * character of a line or not.
5046 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005047 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 stuffcharReadbuff('\r');
5049 stuffcharReadbuff(ESC);
5050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005051 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 invoke_edit(cap, TRUE, 'r', FALSE);
5053 }
5054 else
5055 {
5056 prep_redo(cap->oap->regname, cap->count1,
5057 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5058
5059 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (has_mbyte)
5061 {
5062 int old_State = State;
5063
5064 if (cap->ncharC1 != 0)
5065 AppendCharToRedobuff(cap->ncharC1);
5066 if (cap->ncharC2 != 0)
5067 AppendCharToRedobuff(cap->ncharC2);
5068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005069 // This is slow, but it handles replacing a single-byte with a
5070 // multi-byte and the other way around. Also handles adding
5071 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 for (n = cap->count1; n > 0; --n)
5073 {
5074 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005075 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5076 {
5077 int c = ins_copychar(curwin->w_cursor.lnum
5078 + (cap->nchar == Ctrl_Y ? -1 : 1));
5079 if (c != NUL)
5080 ins_char(c);
5081 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005082 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005083 ++curwin->w_cursor.col;
5084 }
5085 else
5086 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 State = old_State;
5088 if (cap->ncharC1 != 0)
5089 ins_char(cap->ncharC1);
5090 if (cap->ncharC2 != 0)
5091 ins_char(cap->ncharC2);
5092 }
5093 }
5094 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
5096 /*
5097 * Replace the characters within one line.
5098 */
5099 for (n = cap->count1; n > 0; --n)
5100 {
5101 /*
5102 * Get ptr again, because u_save and/or showmatch() will have
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005103 * released the line. This may also happen in ins_copychar().
5104 * At the same time we let know that the line will be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005106 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5107 {
5108 int c = ins_copychar(curwin->w_cursor.lnum
5109 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005110
5111 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005112 if (c != NUL)
5113 ptr[curwin->w_cursor.col] = c;
5114 }
5115 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005116 {
5117 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005118 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if (p_sm && msg_silent == 0)
5121 showmatch(cap->nchar);
5122 ++curwin->w_cursor.col;
5123 }
5124#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005125 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005127 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
Bram Moolenaar009b2592004-10-24 19:18:58 +00005129 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005130 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005132 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134#endif
5135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005136 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 changed_bytes(curwin->w_cursor.lnum,
5138 (colnr_T)(curwin->w_cursor.col - cap->count1));
5139 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005140 --curwin->w_cursor.col; // cursor on the last replaced char
5141 // if the character on the left of the current cursor is a multi-byte
5142 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (has_mbyte)
5144 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 curbuf->b_op_end = curwin->w_cursor;
5146 curwin->w_set_curswant = TRUE;
5147 set_last_insert(cap->nchar);
5148 }
5149}
5150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151/*
5152 * 'o': Exchange start and end of Visual area.
5153 * 'O': same, but in block mode exchange left and right corners.
5154 */
5155 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005156v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157{
5158 pos_T old_cursor;
5159 colnr_T left, right;
5160
5161 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5162 {
5163 old_cursor = curwin->w_cursor;
5164 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5165 curwin->w_cursor.lnum = VIsual.lnum;
5166 coladvance(left);
5167 VIsual = curwin->w_cursor;
5168
5169 curwin->w_cursor.lnum = old_cursor.lnum;
5170 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005171 // 'selection "exclusive" and cursor at right-bottom corner: move it
5172 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5174 ++curwin->w_curswant;
5175 coladvance(curwin->w_curswant);
5176 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005178 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
5180 curwin->w_cursor.lnum = VIsual.lnum;
5181 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5182 ++right;
5183 coladvance(right);
5184 VIsual = curwin->w_cursor;
5185
5186 curwin->w_cursor.lnum = old_cursor.lnum;
5187 coladvance(left);
5188 curwin->w_curswant = left;
5189 }
5190 }
5191 else
5192 {
5193 old_cursor = curwin->w_cursor;
5194 curwin->w_cursor = VIsual;
5195 VIsual = old_cursor;
5196 curwin->w_set_curswant = TRUE;
5197 }
5198}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
5200/*
5201 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5202 */
5203 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005204nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005206 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 cap->cmdchar = 'c';
5209 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005210 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 VIsual_mode = 'V';
5212 nv_operator(cap);
5213 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005214 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005217 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 else
5219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (virtual_active())
5221 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5223 }
5224 }
5225}
5226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227/*
5228 * "gr".
5229 */
5230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005231nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 if (VIsual_active)
5234 {
5235 cap->cmdchar = 'r';
5236 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005237 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005239 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
5241 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005242 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else
5244 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005245 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005246 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 stuffcharReadbuff(cap->extra_char);
5248 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 if (virtual_active())
5250 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 invoke_edit(cap, TRUE, 'v', FALSE);
5252 }
5253 }
5254}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256/*
5257 * Swap case for "~" command, when it does not work like an operator.
5258 */
5259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005260n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 long n;
5263 pos_T startpos;
5264 int did_change = 0;
5265#ifdef FEAT_NETBEANS_INTG
5266 pos_T pos;
5267 char_u *ptr;
5268 int count;
5269#endif
5270
5271 if (checkclearopq(cap->oap))
5272 return;
5273
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005274 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
5276 clearopbeep(cap->oap);
5277 return;
5278 }
5279
5280 prep_redo_cmd(cap);
5281
5282 if (u_save_cursor() == FAIL)
5283 return;
5284
5285 startpos = curwin->w_cursor;
5286#ifdef FEAT_NETBEANS_INTG
5287 pos = startpos;
5288#endif
5289 for (n = cap->count1; n > 0; --n)
5290 {
5291 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5292 inc_cursor();
5293 if (gchar_cursor() == NUL)
5294 {
5295 if (vim_strchr(p_ww, '~') != NULL
5296 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5297 {
5298#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005299 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
5301 if (did_change)
5302 {
5303 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005304 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005305 netbeans_removed(curbuf, pos.lnum, pos.col,
5306 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005308 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310 pos.col = 0;
5311 pos.lnum++;
5312 }
5313#endif
5314 ++curwin->w_cursor.lnum;
5315 curwin->w_cursor.col = 0;
5316 if (n > 1)
5317 {
5318 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5319 break;
5320 u_clearline();
5321 }
5322 }
5323 else
5324 break;
5325 }
5326 }
5327#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005328 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
5330 ptr = ml_get(pos.lnum);
5331 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005332 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5333 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335#endif
5336
5337
5338 check_cursor();
5339 curwin->w_set_curswant = TRUE;
5340 if (did_change)
5341 {
5342 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5343 0L);
5344 curbuf->b_op_start = startpos;
5345 curbuf->b_op_end = curwin->w_cursor;
5346 if (curbuf->b_op_end.col > 0)
5347 --curbuf->b_op_end.col;
5348 }
5349}
5350
5351/*
5352 * Move cursor to mark.
5353 */
5354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005355nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
5357 if (check_mark(pos) == FAIL)
5358 clearop(cap->oap);
5359 else
5360 {
5361 if (cap->cmdchar == '\''
5362 || cap->cmdchar == '`'
5363 || cap->cmdchar == '['
5364 || cap->cmdchar == ']')
5365 setpcmark();
5366 curwin->w_cursor = *pos;
5367 if (flag)
5368 beginline(BL_WHITE | BL_FIX);
5369 else
5370 check_cursor();
5371 }
5372 cap->oap->motion_type = flag ? MLINE : MCHAR;
5373 if (cap->cmdchar == '`')
5374 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005375 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 curwin->w_set_curswant = TRUE;
5377}
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379/*
5380 * Handle commands that are operators in Visual mode.
5381 */
5382 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005383v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
5385 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005387 // Uppercase means linewise, except in block mode, then "D" deletes till
5388 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 if (isupper(cap->cmdchar))
5390 {
5391 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005392 {
5393 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5397 curwin->w_curswant = MAXCOL;
5398 }
5399 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5400 nv_operator(cap);
5401}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403/*
5404 * "s" and "S" commands.
5405 */
5406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005407nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005409#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005410 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005411 if (term_swap_diff() == OK)
5412 return;
5413#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005414#ifdef FEAT_JOB_CHANNEL
5415 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5416 {
5417 clearopbeep(cap->oap);
5418 return;
5419 }
5420#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005421 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
5423 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005424 {
5425 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 cap->cmdchar = 'c';
5429 nv_operator(cap);
5430 }
5431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 nv_optrans(cap);
5433}
5434
5435/*
5436 * Abbreviated commands.
5437 */
5438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005439nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005442 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005444 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 if (VIsual_active)
5446 v_visop(cap);
5447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 nv_optrans(cap);
5449}
5450
5451/*
5452 * Translate a command into another command.
5453 */
5454 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005455nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
5457 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5458 (char_u *)"d$", (char_u *)"c$",
5459 (char_u *)"cl", (char_u *)"cc",
5460 (char_u *)"yy", (char_u *)":s\r"};
5461 static char_u *str = (char_u *)"xXDCsSY&";
5462
5463 if (!checkclearopq(cap->oap))
5464 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005465 // In Vi "2D" doesn't delete the next line. Can't translate it
5466 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005467 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5468 {
5469 cap->oap->start = curwin->w_cursor;
5470 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005471#ifdef FEAT_EVAL
5472 set_op_var(OP_DELETE);
5473#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005474 cap->count1 = 1;
5475 nv_dollar(cap);
5476 finish_op = TRUE;
5477 ResetRedobuff();
5478 AppendCharToRedobuff('D');
5479 }
5480 else
5481 {
5482 if (cap->count0)
5483 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005484 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 cap->opcount = 0;
5488}
5489
5490/*
5491 * "'" and "`" commands. Also for "g'" and "g`".
5492 * cap->arg is TRUE for "'" and "g'".
5493 */
5494 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005495nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
5497 pos_T *pos;
5498 int c;
5499#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005500 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005501 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502#endif
5503
5504 if (cap->cmdchar == 'g')
5505 c = cap->extra_char;
5506 else
5507 c = cap->nchar;
5508 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005509 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
5511 if (cap->arg)
5512 {
5513 check_cursor_lnum();
5514 beginline(BL_WHITE | BL_FIX);
5515 }
5516 else
5517 check_cursor();
5518 }
5519 else
5520 nv_cursormark(cap, cap->arg, pos);
5521
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005522 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 if (!virtual_active())
5524 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005525 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526#ifdef FEAT_FOLDING
5527 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005528 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005529 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 && (fdo_flags & FDO_MARK)
5531 && old_KeyTyped)
5532 foldOpenCursor();
5533#endif
5534}
5535
5536/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005537 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 */
5539 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005540nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542#ifdef FEAT_JUMPLIST
5543 pos_T *pos;
5544# ifdef FEAT_FOLDING
5545 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005546 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547# endif
5548
5549 if (!checkclearopq(cap->oap))
5550 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005551 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5552 {
5553 if (goto_tabpage_lastused() == FAIL)
5554 clearopbeep(cap->oap);
5555 return;
5556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 if (cap->cmdchar == 'g')
5558 pos = movechangelist((int)cap->count1);
5559 else
5560 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005561 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
5563 curwin->w_set_curswant = TRUE;
5564 check_cursor();
5565 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005566 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 nv_cursormark(cap, FALSE, pos);
5568 else if (cap->cmdchar == 'g')
5569 {
5570 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005571 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005573 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005575 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 else
5578 clearopbeep(cap->oap);
5579# ifdef FEAT_FOLDING
5580 if (cap->oap->op_type == OP_NOP
5581 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5582 && (fdo_flags & FDO_MARK)
5583 && old_KeyTyped)
5584 foldOpenCursor();
5585# endif
5586 }
5587#else
5588 clearopbeep(cap->oap);
5589#endif
5590}
5591
5592/*
5593 * Handle '"' command.
5594 */
5595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005596nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
5598 if (checkclearop(cap->oap))
5599 return;
5600#ifdef FEAT_EVAL
5601 if (cap->nchar == '=')
5602 cap->nchar = get_expr_register();
5603#endif
5604 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5605 {
5606 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005607 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608#ifdef FEAT_EVAL
5609 set_reg_var(cap->oap->regname);
5610#endif
5611 }
5612 else
5613 clearopbeep(cap->oap);
5614}
5615
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616/*
5617 * Handle "v", "V" and "CTRL-V" commands.
5618 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5619 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005620 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 */
5622 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005623nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005625 if (cap->cmdchar == Ctrl_Q)
5626 cap->cmdchar = Ctrl_V;
5627
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005628 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5629 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (cap->oap->op_type != OP_NOP)
5631 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005632 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005633 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 return;
5635 }
5636
5637 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005638 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005640 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005642 else // toggle char/block mode
5643 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 VIsual_mode = cap->cmdchar;
5645 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005646 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005648 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005650 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
5652 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005653 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005655 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 VIsual = curwin->w_cursor;
5657
5658 VIsual_active = TRUE;
5659 VIsual_reselect = TRUE;
5660 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005661 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005664 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005665 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 /*
5667 * For V and ^V, we multiply the number of lines even if there
5668 * was only one -- webb
5669 */
5670 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5671 {
5672 curwin->w_cursor.lnum +=
5673 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005674 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676 VIsual_mode = resel_VIsual_mode;
5677 if (VIsual_mode == 'v')
5678 {
5679 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005680 {
5681 validate_virtcol();
5682 curwin->w_curswant = curwin->w_virtcol
5683 + resel_VIsual_vcol * cap->count0 - 1;
5684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005686 curwin->w_curswant = resel_VIsual_vcol;
5687 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005689 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
5691 curwin->w_curswant = MAXCOL;
5692 coladvance((colnr_T)MAXCOL);
5693 }
5694 else if (VIsual_mode == Ctrl_V)
5695 {
5696 validate_virtcol();
5697 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005698 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 coladvance(curwin->w_curswant);
5700 }
5701 else
5702 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005703 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 }
5705 else
5706 {
5707 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005708 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 may_start_select('c');
5710 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005711 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005712 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005713 if (cap->count0 > 0 && --cap->count1 > 0)
5714 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005715 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005716 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5717 nv_right(cap);
5718 else if (VIsual_mode == 'V')
5719 nv_down(cap);
5720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 }
5723}
5724
5725/*
5726 * Start selection for Shift-movement keys.
5727 */
5728 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005729start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005731 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 may_start_select('k');
5733 n_start_visual_mode('v');
5734}
5735
5736/*
5737 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5738 */
5739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005740may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 VIsual_select = (stuff_empty() && typebuf_typed()
5743 && (vim_strchr(p_slm, c) != NULL));
5744}
5745
5746/*
5747 * Start Visual mode "c".
5748 * Should set VIsual_select before calling this.
5749 */
5750 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005751n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005753#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005754 int cursor_line_was_concealed = curwin->w_p_cole > 0
5755 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005756#endif
5757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 VIsual_mode = c;
5759 VIsual_active = TRUE;
5760 VIsual_reselect = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005761 trigger_modechanged();
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005762
5763 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005764 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005765 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005766 {
5767 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 VIsual = curwin->w_cursor;
5771
5772#ifdef FEAT_FOLDING
5773 foldAdjustVisual();
5774#endif
5775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005777#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005778 // Check if redraw is needed after changing the state.
5779 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005780#endif
5781
Bram Moolenaar09df3122006-01-23 22:23:09 +00005782 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005783 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005785 // Make sure the clipboard gets updated. Needed because start and
5786 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 clip_star.vmode = NUL;
5788#endif
5789
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005790 // Only need to redraw this line, unless still need to redraw an old
5791 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 if (curwin->w_redr_type < INVERTED)
5793 {
5794 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5795 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5796 }
5797}
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
5800/*
5801 * CTRL-W: Window commands
5802 */
5803 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005804nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005806 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005807 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005808 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005809 cap->cmdchar = ':';
5810 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005811 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005812 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005813 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005814 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815}
5816
5817/*
5818 * CTRL-Z: Suspend
5819 */
5820 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005821nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822{
5823 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005825 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005826 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827}
5828
5829/*
5830 * Commands starting with "g".
5831 */
5832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005833nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834{
5835 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 int i;
5838 int flag = FALSE;
5839
5840 switch (cap->nchar)
5841 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005842 case Ctrl_A:
5843 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844#ifdef MEM_PROFILE
5845 /*
5846 * "g^A": dump log of used memory.
5847 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005848 if (!VIsual_active && cap->nchar == Ctrl_A)
5849 vim_mem_profile_dump();
5850 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005852 /*
5853 * "g^A/g^X": sequentially increment visually selected region
5854 */
5855 if (VIsual_active)
5856 {
5857 cap->arg = TRUE;
5858 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005859 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005860 nv_addsub(cap);
5861 }
5862 else
5863 clearopbeep(oap);
5864 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 /*
5867 * "gR": Enter virtual replace mode.
5868 */
5869 case 'R':
5870 cap->arg = TRUE;
5871 nv_Replace(cap);
5872 break;
5873
5874 case 'r':
5875 nv_vreplace(cap);
5876 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
5878 case '&':
5879 do_cmdline_cmd((char_u *)"%s//~/&");
5880 break;
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 /*
5883 * "gv": Reselect the previous Visual area. If Visual already active,
5884 * exchange previous and current Visual area.
5885 */
5886 case 'v':
5887 if (checkclearop(oap))
5888 break;
5889
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005890 if ( curbuf->b_visual.vi_start.lnum == 0
5891 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5892 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 beep_flush();
5894 else
5895 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005896 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 if (VIsual_active)
5898 {
5899 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005900 VIsual_mode = curbuf->b_visual.vi_mode;
5901 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902# ifdef FEAT_EVAL
5903 curbuf->b_visual_mode_eval = i;
5904# endif
5905 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005906 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5907 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005909 tpos = curbuf->b_visual.vi_end;
5910 curbuf->b_visual.vi_end = curwin->w_cursor;
5911 curwin->w_cursor = curbuf->b_visual.vi_start;
5912 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
5914 else
5915 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005916 VIsual_mode = curbuf->b_visual.vi_mode;
5917 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5918 tpos = curbuf->b_visual.vi_end;
5919 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 }
5921
5922 VIsual_active = TRUE;
5923 VIsual_reselect = TRUE;
5924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005925 // Set Visual to the start and w_cursor to the end of the Visual
5926 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 check_cursor();
5928 VIsual = curwin->w_cursor;
5929 curwin->w_cursor = tpos;
5930 check_cursor();
5931 update_topline();
5932 /*
5933 * When called from normal "g" command: start Select mode when
5934 * 'selectmode' contains "cmd". When called for K_SELECT, always
5935 * start Select mode.
5936 */
5937 if (cap->arg)
5938 VIsual_select = TRUE;
5939 else
5940 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005943 // Make sure the clipboard gets updated. Needed because start and
5944 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 clip_star.vmode = NUL;
5946#endif
5947 redraw_curbuf_later(INVERTED);
5948 showmode();
5949 }
5950 break;
5951 /*
5952 * "gV": Don't reselect the previous Visual area after a Select mode
5953 * mapping of menu.
5954 */
5955 case 'V':
5956 VIsual_reselect = FALSE;
5957 break;
5958
5959 /*
5960 * "gh": start Select mode.
5961 * "gH": start Select line mode.
5962 * "g^H": start Select block mode.
5963 */
5964 case K_BS:
5965 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005966 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 case 'h':
5968 case 'H':
5969 case Ctrl_H:
5970# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005971 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 if (cap->nchar == Ctrl_H)
5973 cap->cmdchar = Ctrl_V;
5974 else
5975# endif
5976 cap->cmdchar = cap->nchar + ('v' - 'h');
5977 cap->arg = TRUE;
5978 nv_visual(cap);
5979 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005980
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005981 // "gn", "gN" visually select next/previous search match
5982 // "gn" selects next match
5983 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005984 case 'N':
5985 case 'n':
5986 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005987 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
5990 /*
5991 * "gj" and "gk" two new funny movement keys -- up and down
5992 * movement based on *screen* line rather than *file* line.
5993 */
5994 case 'j':
5995 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005996 // with 'nowrap' it works just like the normal "j" command.
5997 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 {
5999 oap->motion_type = MLINE;
6000 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6001 }
6002 else
6003 i = nv_screengo(oap, FORWARD, cap->count1);
6004 if (i == FAIL)
6005 clearopbeep(oap);
6006 break;
6007
6008 case 'k':
6009 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006010 // with 'nowrap' it works just like the normal "k" command.
6011 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 {
6013 oap->motion_type = MLINE;
6014 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6015 }
6016 else
6017 i = nv_screengo(oap, BACKWARD, cap->count1);
6018 if (i == FAIL)
6019 clearopbeep(oap);
6020 break;
6021
6022 /*
6023 * "gJ": join two lines without inserting a space.
6024 */
6025 case 'J':
6026 nv_join(cap);
6027 break;
6028
6029 /*
6030 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6031 * "gm": middle of "g0" and "g$".
6032 */
6033 case '^':
6034 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006035 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036
6037 case '0':
6038 case 'm':
6039 case K_HOME:
6040 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 oap->motion_type = MCHAR;
6042 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006043 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006045 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 int width2 = width1 + curwin_col_off2();
6047
6048 validate_virtcol();
6049 i = 0;
6050 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6051 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6052 }
6053 else
6054 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006055 // Go to the middle of the screen line. When 'number' or
6056 // 'relativenumber' is on and lines are wrapping the middle can be more
6057 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006059 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 + ((curwin->w_p_wrap && i > 0)
6061 ? curwin_col_off2() : 0)) / 2;
6062 coladvance((colnr_T)i);
6063 if (flag)
6064 {
6065 do
6066 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006067 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006068 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 }
6070 curwin->w_set_curswant = TRUE;
6071 break;
6072
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006073 case 'M':
6074 {
6075 char_u *ptr = ml_get_curline();
6076
6077 oap->motion_type = MCHAR;
6078 oap->inclusive = FALSE;
6079 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006080 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006081 else
6082 i = (int)STRLEN(ptr);
6083 if (cap->count0 > 0 && cap->count0 <= 100)
6084 coladvance((colnr_T)(i * cap->count0 / 100));
6085 else
6086 coladvance((colnr_T)(i / 2));
6087 curwin->w_set_curswant = TRUE;
6088 }
6089 break;
6090
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006092 // "g_": to the last non-blank character in the line or <count> lines
6093 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 cap->oap->motion_type = MCHAR;
6095 cap->oap->inclusive = TRUE;
6096 curwin->w_curswant = MAXCOL;
6097 if (cursor_down((long)(cap->count1 - 1),
6098 cap->oap->op_type == OP_NOP) == FAIL)
6099 clearopbeep(cap->oap);
6100 else
6101 {
6102 char_u *ptr = ml_get_curline();
6103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006104 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6106 --curwin->w_cursor.col;
6107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006108 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006110 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 --curwin->w_cursor.col;
6112 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006113 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 }
6115 break;
6116
6117 case '$':
6118 case K_END:
6119 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 {
6121 int col_off = curwin_col_off();
6122
6123 oap->motion_type = MCHAR;
6124 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006125 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006127 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if (cap->count1 == 1)
6129 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006130 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 int width2 = width1 + curwin_col_off2();
6132
6133 validate_virtcol();
6134 i = width1 - 1;
6135 if (curwin->w_virtcol >= (colnr_T)width1)
6136 i += ((curwin->w_virtcol - width1) / width2 + 1)
6137 * width2;
6138 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006140 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006141 validate_virtcol();
6142 curwin->w_curswant = curwin->w_virtcol;
6143 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6145 {
6146 /*
6147 * Check for landing on a character that got split at
6148 * the end of the line. We do not want to advance to
6149 * the next screen line.
6150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (curwin->w_virtcol > (colnr_T)i)
6152 --curwin->w_cursor.col;
6153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 }
6155 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6156 clearopbeep(oap);
6157 }
6158 else
6159 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006160 if (cap->count1 > 1)
6161 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006162 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006163
Bram Moolenaar02631462017-09-22 15:20:32 +02006164 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006166
Bram Moolenaar74ede802021-05-29 19:18:01 +02006167 // if the character doesn't fit move one back
6168 if (curwin->w_cursor.col > 0
6169 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6170 {
6171 colnr_T vcol;
6172
6173 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6174 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6175 --curwin->w_cursor.col;
6176 }
6177
Bram Moolenaard5c82342019-07-27 18:44:57 +02006178 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006179 validate_virtcol();
6180 curwin->w_curswant = curwin->w_virtcol;
6181 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 }
6183 }
6184 break;
6185
6186 /*
6187 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6188 */
6189 case '*':
6190 case '#':
6191#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006192 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006194 case Ctrl_RSB: // :tag or :tselect for current identifier
6195 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 nv_ident(cap);
6197 break;
6198
6199 /*
6200 * ge and gE: go back to end of word
6201 */
6202 case 'e':
6203 case 'E':
6204 oap->motion_type = MCHAR;
6205 curwin->w_set_curswant = TRUE;
6206 oap->inclusive = TRUE;
6207 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6208 clearopbeep(oap);
6209 break;
6210
6211 /*
6212 * "g CTRL-G": display info about cursor position
6213 */
6214 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006215 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 break;
6217
6218 /*
6219 * "gi": start Insert at the last position.
6220 */
6221 case 'i':
6222 if (curbuf->b_last_insert.lnum != 0)
6223 {
6224 curwin->w_cursor = curbuf->b_last_insert;
6225 check_cursor_lnum();
6226 i = (int)STRLEN(ml_get_curline());
6227 if (curwin->w_cursor.col > (colnr_T)i)
6228 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 if (virtual_active())
6230 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 curwin->w_cursor.col = i;
6232 }
6233 }
6234 cap->cmdchar = 'i';
6235 nv_edit(cap);
6236 break;
6237
6238 /*
6239 * "gI": Start insert in column 1.
6240 */
6241 case 'I':
6242 beginline(0);
6243 if (!checkclearopq(oap))
6244 invoke_edit(cap, FALSE, 'g', FALSE);
6245 break;
6246
6247#ifdef FEAT_SEARCHPATH
6248 /*
6249 * "gf": goto file, edit file under cursor
6250 * "]f" and "[f": can also be used.
6251 */
6252 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006253 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 nv_gotofile(cap);
6255 break;
6256#endif
6257
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006258 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 case '\'':
6260 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006261 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 case '`':
6263 nv_gomark(cap);
6264 break;
6265
6266 /*
6267 * "gs": Goto sleep.
6268 */
6269 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006270 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 break;
6272
6273 /*
6274 * "ga": Display the ascii value of the character under the
6275 * cursor. It is displayed in decimal, hex, and octal. -- webb
6276 */
6277 case 'a':
6278 do_ascii(NULL);
6279 break;
6280
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 /*
6282 * "g8": Display the bytes used for the UTF-8 character under the
6283 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006284 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 */
6286 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006287 if (cap->count0 == 8)
6288 utf_find_illegal();
6289 else
6290 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006293 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006294 case '<':
6295 show_sb_text();
6296 break;
6297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 /*
6299 * "gg": Goto the first line in file. With a count it goes to
6300 * that line number like for "G". -- webb
6301 */
6302 case 'g':
6303 cap->arg = FALSE;
6304 nv_goto(cap);
6305 break;
6306
6307 /*
6308 * Two-character operators:
6309 * "gq" Format text
6310 * "gw" Format text and keep cursor position
6311 * "g~" Toggle the case of the text.
6312 * "gu" Change text to lower case.
6313 * "gU" Change text to upper case.
6314 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006315 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 */
6317 case 'q':
6318 case 'w':
6319 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006320 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 case '~':
6322 case 'u':
6323 case 'U':
6324 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006325 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 nv_operator(cap);
6327 break;
6328
6329 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006330 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 * current function
6332 * "gD": idem, but in the current file.
6333 */
6334 case 'd':
6335 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006336 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 break;
6338
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 /*
6340 * g<*Mouse> : <C-*mouse>
6341 */
6342 case K_MIDDLEMOUSE:
6343 case K_MIDDLEDRAG:
6344 case K_MIDDLERELEASE:
6345 case K_LEFTMOUSE:
6346 case K_LEFTDRAG:
6347 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006348 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 case K_RIGHTMOUSE:
6350 case K_RIGHTDRAG:
6351 case K_RIGHTRELEASE:
6352 case K_X1MOUSE:
6353 case K_X1DRAG:
6354 case K_X1RELEASE:
6355 case K_X2MOUSE:
6356 case K_X2DRAG:
6357 case K_X2RELEASE:
6358 mod_mask = MOD_MASK_CTRL;
6359 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 case K_IGNORE:
6363 break;
6364
6365 /*
6366 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6367 */
6368 case 'p':
6369 case 'P':
6370 nv_put(cap);
6371 break;
6372
6373#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006374 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 case 'o':
6376 goto_byte(cap->count0);
6377 break;
6378#endif
6379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006380 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006382 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
6384 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006385 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 break;
6387 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (!checkclearopq(oap))
6390 do_exmode(TRUE);
6391 break;
6392
6393#ifdef FEAT_JUMPLIST
6394 case ',':
6395 nv_pcmark(cap);
6396 break;
6397
6398 case ';':
6399 cap->count1 = -cap->count1;
6400 nv_pcmark(cap);
6401 break;
6402#endif
6403
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006404 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006405 if (!checkclearop(oap))
6406 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006407 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006408 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006409 if (!checkclearop(oap))
6410 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006411 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006412
Bram Moolenaar62a23252020-08-09 14:04:42 +02006413 case TAB:
6414 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6415 clearopbeep(oap);
6416 break;
6417
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006418 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006419 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006420 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006421 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006422 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006423 break;
6424
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 default:
6426 clearopbeep(oap);
6427 break;
6428 }
6429}
6430
6431/*
6432 * Handle "o" and "O" commands.
6433 */
6434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006435n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006437#ifdef FEAT_CONCEAL
6438 linenr_T oldline = curwin->w_cursor.lnum;
6439#endif
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (!checkclearopq(cap->oap))
6442 {
6443#ifdef FEAT_FOLDING
6444 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006445 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 (void)hasFolding(curwin->w_cursor.lnum,
6447 &curwin->w_cursor.lnum, NULL);
6448 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006449 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 (void)hasFolding(curwin->w_cursor.lnum,
6451 NULL, &curwin->w_cursor.lnum);
6452#endif
6453 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6454 (cap->cmdchar == 'O' ? 1 : 0)),
6455 (linenr_T)(curwin->w_cursor.lnum +
6456 (cap->cmdchar == 'o' ? 1 : 0))
6457 ) == OK
6458 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006459 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6460 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006462#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006463 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006464 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006465#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006466#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006467 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006468 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006469 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006470#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006471 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006472 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6473 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6475 }
6476 }
6477}
6478
6479/*
6480 * "." command: redo last change.
6481 */
6482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006483nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
6485 if (!checkclearopq(cap->oap))
6486 {
6487 /*
6488 * If "restart_edit" is TRUE, the last but one command is repeated
6489 * instead of the last command (inserting text). This is used for
6490 * CTRL-O <.> in insert mode.
6491 */
6492 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6493 clearopbeep(cap->oap);
6494 }
6495}
6496
6497/*
6498 * CTRL-R: undo undo
6499 */
6500 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006501nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 if (!checkclearopq(cap->oap))
6504 {
6505 u_redo((int)cap->count1);
6506 curwin->w_set_curswant = TRUE;
6507 }
6508}
6509
6510/*
6511 * Handle "U" command.
6512 */
6513 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006514nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006516 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006517 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006519 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 cap->cmdchar = 'g';
6521 cap->nchar = 'U';
6522 nv_operator(cap);
6523 }
6524 else if (!checkclearopq(cap->oap))
6525 {
6526 u_undoline();
6527 curwin->w_set_curswant = TRUE;
6528 }
6529}
6530
6531/*
6532 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6533 * single character.
6534 */
6535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006536nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006538 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006539 {
6540#ifdef FEAT_JOB_CHANNEL
6541 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6542 {
6543 clearopbeep(cap->oap);
6544 return;
6545 }
6546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 else
6550 nv_operator(cap);
6551}
6552
6553/*
6554 * Handle an operator command.
6555 * The actual work is done by do_pending_operator().
6556 */
6557 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006558nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 int op_type;
6561
6562 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006563#ifdef FEAT_JOB_CHANNEL
6564 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6565 {
6566 clearopbeep(cap->oap);
6567 return;
6568 }
6569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006571 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 nv_lineop(cap);
6573 else if (!checkclearop(cap->oap))
6574 {
6575 cap->oap->start = curwin->w_cursor;
6576 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006577#ifdef FEAT_EVAL
6578 set_op_var(op_type);
6579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 }
6581}
6582
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006583#ifdef FEAT_EVAL
6584/*
6585 * Set v:operator to the characters for "optype".
6586 */
6587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006588set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006589{
6590 char_u opchars[3];
6591
6592 if (optype == OP_NOP)
6593 set_vim_var_string(VV_OP, NULL, 0);
6594 else
6595 {
6596 opchars[0] = get_op_char(optype);
6597 opchars[1] = get_extra_op_char(optype);
6598 opchars[2] = NUL;
6599 set_vim_var_string(VV_OP, opchars, -1);
6600 }
6601}
6602#endif
6603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604/*
6605 * Handle linewise operator "dd", "yy", etc.
6606 *
6607 * "_" is is a strange motion command that helps make operators more logical.
6608 * It is actually implemented, but not documented in the real Vi. This motion
6609 * command actually refers to "the current line". Commands like "dd" and "yy"
6610 * are really an alternate form of "d_" and "y_". It does accept a count, so
6611 * "d3_" works to delete 3 lines.
6612 */
6613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006614nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 cap->oap->motion_type = MLINE;
6617 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6618 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006619 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006620 && cap->oap->motion_force != 'v'
6621 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 || cap->oap->op_type == OP_LSHIFT
6623 || cap->oap->op_type == OP_RSHIFT)
6624 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006625 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 beginline(BL_WHITE | BL_FIX);
6627}
6628
6629/*
6630 * <Home> command.
6631 */
6632 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006633nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006635 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006636 if (mod_mask & MOD_MASK_CTRL)
6637 nv_goto(cap);
6638 else
6639 {
6640 cap->count0 = 1;
6641 nv_pipe(cap);
6642 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006643 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6644 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645}
6646
6647/*
6648 * "|" command.
6649 */
6650 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006651nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
6653 cap->oap->motion_type = MCHAR;
6654 cap->oap->inclusive = FALSE;
6655 beginline(0);
6656 if (cap->count0 > 0)
6657 {
6658 coladvance((colnr_T)(cap->count0 - 1));
6659 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6660 }
6661 else
6662 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006663 // keep curswant at the column where we wanted to go, not where
6664 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 curwin->w_set_curswant = FALSE;
6666}
6667
6668/*
6669 * Handle back-word command "b" and "B".
6670 * cap->arg is 1 for "B"
6671 */
6672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006673nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
6675 cap->oap->motion_type = MCHAR;
6676 cap->oap->inclusive = FALSE;
6677 curwin->w_set_curswant = TRUE;
6678 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6679 clearopbeep(cap->oap);
6680#ifdef FEAT_FOLDING
6681 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6682 foldOpenCursor();
6683#endif
6684}
6685
6686/*
6687 * Handle word motion commands "e", "E", "w" and "W".
6688 * cap->arg is TRUE for "E" and "W".
6689 */
6690 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006691nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 int n;
6694 int word_end;
6695 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006696 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697
6698 /*
6699 * Set inclusive for the "E" and "e" command.
6700 */
6701 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6702 word_end = TRUE;
6703 else
6704 word_end = FALSE;
6705 cap->oap->inclusive = word_end;
6706
6707 /*
6708 * "cw" and "cW" are a special case.
6709 */
6710 if (!word_end && cap->oap->op_type == OP_CHANGE)
6711 {
6712 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006713 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006715 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 {
6717 /*
6718 * Reproduce a funny Vi behaviour: "cw" on a blank only
6719 * changes one character, not all blanks until the start of
6720 * the next word. Only do this when the 'w' flag is included
6721 * in 'cpoptions'.
6722 */
6723 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6724 {
6725 cap->oap->inclusive = TRUE;
6726 cap->oap->motion_type = MCHAR;
6727 return;
6728 }
6729 }
6730 else
6731 {
6732 /*
6733 * This is a little strange. To match what the real Vi does,
6734 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6735 * that we are not on a space or a TAB. This seems impolite
6736 * at first, but it's really more what we mean when we say
6737 * 'cw'.
6738 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006739 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 * will change only one character! This is done by setting
6741 * flag.
6742 */
6743 cap->oap->inclusive = TRUE;
6744 word_end = TRUE;
6745 flag = TRUE;
6746 }
6747 }
6748 }
6749
6750 cap->oap->motion_type = MCHAR;
6751 curwin->w_set_curswant = TRUE;
6752 if (word_end)
6753 n = end_word(cap->count1, cap->arg, flag, FALSE);
6754 else
6755 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6756
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006757 // Don't leave the cursor on the NUL past the end of line. Unless we
6758 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006759 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006760 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
6762 if (n == FAIL && cap->oap->op_type == OP_NOP)
6763 clearopbeep(cap->oap);
6764 else
6765 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767#ifdef FEAT_FOLDING
6768 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6769 foldOpenCursor();
6770#endif
6771 }
6772}
6773
6774/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006775 * Used after a movement command: If the cursor ends up on the NUL after the
6776 * end of the line, may move it back to the last character and make the motion
6777 * inclusive.
6778 */
6779 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006780adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006781{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006782 // The cursor cannot remain on the NUL when:
6783 // - the column is > 0
6784 // - not in Visual mode or 'selection' is "o"
6785 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006786 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006787 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006788 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006789 {
6790 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006791 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006792 if (has_mbyte)
6793 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006794 oap->inclusive = TRUE;
6795 }
6796}
6797
6798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 * "0" and "^" commands.
6800 * cap->arg is the argument for beginline().
6801 */
6802 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006803nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805 cap->oap->motion_type = MCHAR;
6806 cap->oap->inclusive = FALSE;
6807 beginline(cap->arg);
6808#ifdef FEAT_FOLDING
6809 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6810 foldOpenCursor();
6811#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006812 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6813 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
6815
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816/*
6817 * In exclusive Visual mode, may include the last character.
6818 */
6819 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006820adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006823 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 if (has_mbyte)
6826 inc_cursor();
6827 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 ++curwin->w_cursor.col;
6829 cap->oap->inclusive = FALSE;
6830 }
6831}
6832
6833/*
6834 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6835 * Should check VIsual_mode before calling this.
6836 * Returns TRUE when backed up to the previous line.
6837 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006838 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006839unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840{
6841 pos_T *pp;
6842
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006843 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006845 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 pp = &curwin->w_cursor;
6847 else
6848 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 if (pp->coladd > 0)
6850 --pp->coladd;
6851 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 if (pp->col > 0)
6853 {
6854 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006855 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 }
6857 else if (pp->lnum > 1)
6858 {
6859 --pp->lnum;
6860 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6861 return TRUE;
6862 }
6863 }
6864 return FALSE;
6865}
6866
6867/*
6868 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6869 */
6870 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006871nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
6873 if (VIsual_active)
6874 VIsual_select = TRUE;
6875 else if (VIsual_reselect)
6876 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006877 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 cap->arg = TRUE;
6879 nv_g_cmd(cap);
6880 }
6881}
6882
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884/*
6885 * "G", "gg", CTRL-END, CTRL-HOME.
6886 * cap->arg is TRUE for "G".
6887 */
6888 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006889nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
6891 linenr_T lnum;
6892
6893 if (cap->arg)
6894 lnum = curbuf->b_ml.ml_line_count;
6895 else
6896 lnum = 1L;
6897 cap->oap->motion_type = MLINE;
6898 setpcmark();
6899
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006900 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (cap->count0 != 0)
6902 lnum = cap->count0;
6903 if (lnum < 1L)
6904 lnum = 1L;
6905 else if (lnum > curbuf->b_ml.ml_line_count)
6906 lnum = curbuf->b_ml.ml_line_count;
6907 curwin->w_cursor.lnum = lnum;
6908 beginline(BL_SOL | BL_FIX);
6909#ifdef FEAT_FOLDING
6910 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6911 foldOpenCursor();
6912#endif
6913}
6914
6915/*
6916 * CTRL-\ in Normal mode.
6917 */
6918 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006919nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6922 {
6923 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006924 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006925 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 restart_edit = 0;
6927#ifdef FEAT_CMDWIN
6928 if (cmdwin_type != 0)
6929 cmdwin_result = Ctrl_C;
6930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 if (VIsual_active)
6932 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006933 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 redraw_curbuf_later(INVERTED);
6935 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006936 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 if (cap->nchar == Ctrl_G && p_im)
6938 restart_edit = 'a';
6939 }
6940 else
6941 clearopbeep(cap->oap);
6942}
6943
6944/*
6945 * ESC in Normal mode: beep, but don't flush buffers.
6946 * Don't even beep if we are canceling a command.
6947 */
6948 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006949nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950{
6951 int no_reason;
6952
6953 no_reason = (cap->oap->op_type == OP_NOP
6954 && cap->opcount == 0
6955 && cap->count0 == 0
6956 && cap->oap->regname == 0
6957 && !p_im);
6958
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006959 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {
6961 if (restart_edit == 0
6962#ifdef FEAT_CMDWIN
6963 && cmdwin_type == 0
6964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006967 {
6968 if (anyBufIsChanged())
6969 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6970 else
6971 msg(_("Type :qa and press <Enter> to exit Vim"));
6972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006974 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6975 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if (!p_im)
6977 restart_edit = 0;
6978#ifdef FEAT_CMDWIN
6979 if (cmdwin_type != 0)
6980 {
6981 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006982 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 return;
6984 }
6985#endif
6986 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006987#ifdef FEAT_CMDWIN
6988 else if (cmdwin_type != 0 && ex_normal_busy)
6989 {
6990 // When :normal runs out of characters while in the command line window
6991 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6992 // loop.
6993 cmdwin_result = K_IGNORE;
6994 return;
6995 }
6996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (VIsual_active)
6999 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007000 end_visual_mode(); // stop Visual
7001 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 curwin->w_set_curswant = TRUE;
7003 redraw_curbuf_later(INVERTED);
7004 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007005 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007006 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 clearop(cap->oap);
7008
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007009 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7010 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007011 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 restart_edit = 'a';
7013}
7014
7015/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007016 * Move the cursor for the "A" command.
7017 */
7018 void
7019set_cursor_for_append_to_line(void)
7020{
7021 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007022 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007023 {
7024 int save_State = State;
7025
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007026 // Pretend Insert mode here to allow the cursor on the
7027 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007028 State = INSERT;
7029 coladvance((colnr_T)MAXCOL);
7030 State = save_State;
7031 }
7032 else
7033 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7034}
7035
7036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007038 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 */
7040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007041nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007043 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7045 cap->cmdchar = 'i';
7046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007047 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007049 {
7050#ifdef FEAT_TERMINAL
7051 if (term_in_normal_mode())
7052 {
7053 end_visual_mode();
7054 clearop(cap->oap);
7055 term_enter_job_mode();
7056 return;
7057 }
7058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007062 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007063 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7064 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
7066#ifdef FEAT_TEXTOBJ
7067 nv_object(cap);
7068#else
7069 clearopbeep(cap->oap);
7070#endif
7071 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007072#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007073 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007074 {
7075 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007076 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007077 return;
7078 }
7079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 else if (!curbuf->b_p_ma && !p_im)
7081 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007082 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007083 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007085 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007086 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007087 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007089 else if (cap->cmdchar == K_PS && VIsual_active)
7090 {
7091 pos_T old_pos = curwin->w_cursor;
7092 pos_T old_visual = VIsual;
7093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007094 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007095 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7096 {
7097 shift_delete_registers();
7098 cap->oap->regname = '1';
7099 }
7100 else
7101 cap->oap->regname = '-';
7102 cap->cmdchar = 'd';
7103 cap->nchar = NUL;
7104 nv_operator(cap);
7105 do_pending_operator(cap, 0, FALSE);
7106 cap->cmdchar = K_PS;
7107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 // When the last char in the line was deleted then append. Detect this
7109 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007110 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7111 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007112 inc_cursor();
7113
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007114 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007115 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 else if (!checkclearopq(cap->oap))
7118 {
7119 switch (cap->cmdchar)
7120 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007121 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007122 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 break;
7124
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007125 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007126 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7127 beginline(BL_WHITE);
7128 else
7129 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 break;
7131
Bram Moolenaara1891842017-02-04 21:34:31 +01007132 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007133 // Bracketed paste works like "a"ppend, unless the cursor is in
7134 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007135 if (curwin->w_cursor.col == 0)
7136 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007137 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007139 case 'a': // "a"ppend is like "i"nsert on the next character.
7140 // increment coladd when in virtual space, increment the
7141 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 if (virtual_active()
7143 && (curwin->w_cursor.coladd > 0
7144 || *ml_get_cursor() == NUL
7145 || *ml_get_cursor() == TAB))
7146 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007147 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 inc_cursor();
7149 break;
7150 }
7151
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7153 {
7154 int save_State = State;
7155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007156 // Pretend Insert mode here to allow the cursor on the
7157 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 State = INSERT;
7159 coladvance(getviscol());
7160 State = save_State;
7161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
7163 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7164 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007165 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007166 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007167 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168}
7169
7170/*
7171 * Invoke edit() and take care of "restart_edit" and the return value.
7172 */
7173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007174invoke_edit(
7175 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007177 int cmd,
7178 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
7180 int restart_edit_save = 0;
7181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007182 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7183 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7184 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 if (repl || !stuff_empty())
7186 restart_edit_save = restart_edit;
7187 else
7188 restart_edit_save = 0;
7189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007190 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 restart_edit = 0;
7192
7193 if (edit(cmd, startln, cap->count1))
7194 cap->retval |= CA_COMMAND_BUSY;
7195
7196 if (restart_edit == 0)
7197 restart_edit = restart_edit_save;
7198}
7199
7200#ifdef FEAT_TEXTOBJ
7201/*
7202 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7203 */
7204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007205nv_object(
7206 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208 int flag;
7209 int include;
7210 char_u *mps_save;
7211
7212 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007213 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007215 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007217 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 mps_save = curbuf->b_p_mps;
7219 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7220
7221 switch (cap->nchar)
7222 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007223 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 flag = current_word(cap->oap, cap->count1, include, FALSE);
7225 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007226 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 flag = current_word(cap->oap, cap->count1, include, TRUE);
7228 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007229 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 case '(':
7231 case ')':
7232 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7233 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007234 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 case '{':
7236 case '}':
7237 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7238 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007239 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 case ']':
7241 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7242 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007243 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 case '>':
7245 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7246 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007247 case 't': // "at" = a tag block (xml and html)
7248 // Do not adjust oap->end in do_pending_operator()
7249 // otherwise there are different results for 'dit'
7250 // (note leading whitespace in last line):
7251 // 1) <b> 2) <b>
7252 // foobar foobar
7253 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007254 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007255 flag = current_tagblock(cap->oap, cap->count1, include);
7256 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007257 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 flag = current_par(cap->oap, cap->count1, include, 'p');
7259 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007260 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 flag = current_sent(cap->oap, cap->count1, include);
7262 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007263 case '"': // "a"" = a double quoted string
7264 case '\'': // "a'" = a single quoted string
7265 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007266 flag = current_quote(cap->oap, cap->count1, include,
7267 cap->nchar);
7268 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007269#if 0 // TODO
7270 case 'S': // "aS" = a section
7271 case 'f': // "af" = a filename
7272 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273#endif
7274 default:
7275 flag = FAIL;
7276 break;
7277 }
7278
7279 curbuf->b_p_mps = mps_save;
7280 if (flag == FAIL)
7281 clearopbeep(cap->oap);
7282 adjust_cursor_col();
7283 curwin->w_set_curswant = TRUE;
7284}
7285#endif
7286
7287/*
7288 * "q" command: Start/stop recording.
7289 * "q:", "q/", "q?": edit command-line in command-line window.
7290 */
7291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007292nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294 if (cap->oap->op_type == OP_FORMAT)
7295 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007296 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 cap->cmdchar = 'g';
7298 cap->nchar = 'q';
7299 nv_operator(cap);
7300 }
7301 else if (!checkclearop(cap->oap))
7302 {
7303#ifdef FEAT_CMDWIN
7304 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7305 {
7306 stuffcharReadbuff(cap->nchar);
7307 stuffcharReadbuff(K_CMDWIN);
7308 }
7309 else
7310#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007311 // (stop) recording into a named register, unless executing a
7312 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007313 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 clearopbeep(cap->oap);
7315 }
7316}
7317
7318/*
7319 * Handle the "@r" command.
7320 */
7321 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007322nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323{
7324 if (checkclearop(cap->oap))
7325 return;
7326#ifdef FEAT_EVAL
7327 if (cap->nchar == '=')
7328 {
7329 if (get_expr_register() == NUL)
7330 return;
7331 }
7332#endif
7333 while (cap->count1-- && !got_int)
7334 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007335 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
7337 clearopbeep(cap->oap);
7338 break;
7339 }
7340 line_breakcheck();
7341 }
7342}
7343
7344/*
7345 * Handle the CTRL-U and CTRL-D commands.
7346 */
7347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007348nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
7350 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7351 || (cap->cmdchar == Ctrl_D
7352 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7353 clearopbeep(cap->oap);
7354 else if (!checkclearop(cap->oap))
7355 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7356}
7357
7358/*
7359 * Handle "J" or "gJ" command.
7360 */
7361 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007362nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007364 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007366 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
7368 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007369 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7371 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007373 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007374 if (cap->count0 <= 2)
7375 {
7376 clearopbeep(cap->oap);
7377 return;
7378 }
7379 cap->count0 = curbuf->b_ml.ml_line_count
7380 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007382
7383 prep_redo(cap->oap->regname, cap->count0,
7384 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7385 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 }
7387}
7388
7389/*
7390 * "P", "gP", "p" and "gp" commands.
7391 */
7392 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007393nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007395 nv_put_opt(cap, FALSE);
7396}
7397
7398/*
7399 * "P", "gP", "p" and "gp" commands.
7400 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7401 */
7402 static void
7403nv_put_opt(cmdarg_T *cap, int fix_indent)
7404{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 int regname = 0;
7406 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007407 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007408 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 int dir;
7410 int flags = 0;
7411
7412 if (cap->oap->op_type != OP_NOP)
7413 {
7414#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007415 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7417 {
7418 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007419 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
7421 else
7422#endif
7423 clearopbeep(cap->oap);
7424 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007425#ifdef FEAT_JOB_CHANNEL
7426 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7427 {
7428 clearopbeep(cap->oap);
7429 }
7430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 else
7432 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007433 if (fix_indent)
7434 {
7435 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7436 ? FORWARD : BACKWARD;
7437 flags |= PUT_FIXINDENT;
7438 }
7439 else
7440 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007441 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7442 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 prep_redo_cmd(cap);
7444 if (cap->cmdchar == 'g')
7445 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007446 else if (cap->cmdchar == 'z')
7447 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (VIsual_active)
7450 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007451 // Putting in Visual mode: The put text replaces the selected
7452 // text. First delete the selected text, then put the new text.
7453 // Need to save and restore the registers that the delete
7454 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007455 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007457#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007459#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007460 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007461 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007462#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465
7466 )
7467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007468 // The delete is going to overwrite the register we want to
7469 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 reg1 = get_register(regname, TRUE);
7471 }
7472
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007473 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 cap->cmdchar = 'd';
7475 cap->nchar = NUL;
7476 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007477 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 nv_operator(cap);
7479 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007480 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007481 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007483 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 cap->oap->regname = regname;
7485
7486 if (reg1 != NULL)
7487 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007488 // Delete probably changed the register we want to put, save
7489 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 reg2 = get_register(regname, FALSE);
7491 put_register(regname, reg1);
7492 }
7493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007494 // When deleted a linewise Visual area, put the register as
7495 // lines to avoid it joined with the next line. When deletion was
7496 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 if (VIsual_mode == 'V')
7498 flags |= PUT_LINE;
7499 else if (VIsual_mode == 'v')
7500 flags |= PUT_LINE_SPLIT;
7501 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7502 flags |= PUT_LINE_FORWARD;
7503 dir = BACKWARD;
7504 if ((VIsual_mode != 'V'
7505 && curwin->w_cursor.col < curbuf->b_op_start.col)
7506 || (VIsual_mode == 'V'
7507 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007508 // cursor is at the end of the line or end of file, put
7509 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007511 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007512 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007514 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007516 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 if (reg2 != NULL)
7518 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007519
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007520 // What to reselect with "gv"? Selecting the just put text seems to
7521 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007522 if (was_visual)
7523 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007524 curbuf->b_visual.vi_start = curbuf->b_op_start;
7525 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007526 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007527 if (*p_sel == 'e')
7528 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007529 }
7530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007531 // When all lines were selected and deleted do_put() leaves an empty
7532 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007533 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007534 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007535 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007536 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007537
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007538 // If the cursor was in that line, move it to the end of the last
7539 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007540 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7541 {
7542 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7543 coladvance((colnr_T)MAXCOL);
7544 }
7545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 auto_format(FALSE, TRUE);
7547 }
7548}
7549
7550/*
7551 * "o" and "O" commands.
7552 */
7553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007554nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555{
7556#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007557 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7559 {
7560 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007561 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 }
7563 else
7564#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007565 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007567#ifdef FEAT_JOB_CHANNEL
7568 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007569 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 n_opencmd(cap);
7573}
7574
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575#ifdef FEAT_NETBEANS_INTG
7576 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007577nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578{
7579 netbeans_keycommand(cap->nchar);
7580}
7581#endif
7582
7583#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007585nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007587 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588}
7589#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007590
Bram Moolenaar3918c952005-03-15 22:34:55 +00007591/*
7592 * Trigger CursorHold event.
7593 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7594 * input buffer. "did_cursorhold" is set to avoid retriggering.
7595 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007597nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007598{
7599 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7600 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007601 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007602}