blob: 56fae51dacf25c750a97aed79859e327a92bc2b0 [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 Moolenaar2228cd72021-11-22 14:16:08 +0000383#ifndef PROTO // cproto doesn't like this
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100384// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static short nv_cmd_idx[NV_CMDS_SIZE];
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100388// The highest index for which
389// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390static int nv_max_linear;
391
392/*
393 * Compare functions for qsort() below, that checks the command character
394 * through the index in nv_cmd_idx[].
395 */
396 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100397nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
399 int c1, c2;
400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100401 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 c1 = nv_cmds[*(const short *)s1].cmd_char;
403 c2 = nv_cmds[*(const short *)s2].cmd_char;
404 if (c1 < 0)
405 c1 = -c1;
406 if (c2 < 0)
407 c2 = -c2;
408 return c1 - c2;
409}
410
411/*
412 * Initialize the nv_cmd_idx[] table.
413 */
414 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100415init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416{
417 int i;
418
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100419 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000420 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 nv_cmd_idx[i] = i;
422
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100423 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
425
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100426 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000427 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
429 break;
430 nv_max_linear = i - 1;
431}
432
433/*
434 * Search for a command in the commands table.
435 * Returns -1 for invalid command.
436 */
437 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100438find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439{
440 int i;
441 int idx;
442 int top, bot;
443 int c;
444
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100445 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 if (cmdchar >= 0x100)
447 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100449 // We use the absolute value of the character. Special keys have a
450 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 if (cmdchar < 0)
452 cmdchar = -cmdchar;
453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100454 // If the character is in the first part: The character is the index into
455 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 if (cmdchar <= nv_max_linear)
457 return nv_cmd_idx[cmdchar];
458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100459 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 bot = nv_max_linear + 1;
461 top = NV_CMDS_SIZE - 1;
462 idx = -1;
463 while (bot <= top)
464 {
465 i = (top + bot) / 2;
466 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
467 if (c < 0)
468 c = -c;
469 if (cmdchar == c)
470 {
471 idx = nv_cmd_idx[i];
472 break;
473 }
474 if (cmdchar > c)
475 bot = i + 1;
476 else
477 top = i - 1;
478 }
479 return idx;
480}
481
482/*
483 * Execute a command in Normal mode.
484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100486normal_cmd(
487 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100490 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100492 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 int old_col = curwin->w_curswant;
494#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100495 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100497 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 static int old_mapped_len = 0;
500 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000501#ifdef FEAT_EVAL
502 int set_prevcount = FALSE;
503#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200504 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505
Bram Moolenaara80faa82020-04-12 19:37:17 +0200506 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000508
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100509 // Use a count remembered from before entering an operator. After typing
510 // "3d" we return from normal_cmd() and come back here, the "3" is
511 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 ca.opcount = opcount;
513
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 /*
515 * If there is an operator pending, then the command we take this time
516 * will terminate it. Finish_op tells us to finish the operation before
517 * returning this time (unless the operation was cancelled).
518 */
519#ifdef CURSOR_SHAPE
520 c = finish_op;
521#endif
522 finish_op = (oap->op_type != OP_NOP);
523#ifdef CURSOR_SHAPE
524 if (finish_op != c)
525 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100526 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527# ifdef FEAT_MOUSESHAPE
528 update_mouseshape(-1);
529# endif
530 }
531#endif
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100532 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100534 // When not finishing an operator and no register name typed, reset the
535 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000539#ifdef FEAT_EVAL
540 set_prevcount = TRUE;
541#endif
542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100544 // Restore counts from before receiving K_CURSORHOLD. This means after
545 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
546 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000547 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
548 {
549 ca.opcount = oap->prev_opcount;
550 ca.count0 = oap->prev_count0;
551 oap->prev_opcount = 0;
552 oap->prev_count0 = 0;
553 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000554
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 State = NORMAL_BUSY;
558#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100559 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
561
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100562#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100563 // Set v:count here, when called from main() and not a stuffed
564 // command, so that v:count can be used in an expression mapping
565 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100566 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100567 set_vcount_ca(&ca, &set_prevcount);
568#endif
569
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 /*
571 * Get the command character from the user.
572 */
573 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100574 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576 /*
577 * If a mapping was started in Visual or Select mode, remember the length
578 * of the mapping. This is used below to not return to Insert mode for as
579 * long as the mapping is being executed.
580 */
581 if (restart_edit == 0)
582 old_mapped_len = 0;
583 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000584 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 old_mapped_len = typebuf_maplen();
586
587 if (c == NUL)
588 c = K_ZERO;
589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 /*
591 * In Select mode, typed text replaces the selection.
592 */
593 if (VIsual_active
594 && VIsual_select
595 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
596 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100597 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
598 // 'insertmode' is set) fake a "d"elete command, Insert mode will
599 // restart automatically.
600 // Insert the typed character in the typeahead buffer, so that it can
601 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200602 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000603 if (restart_edit != 0)
604 c = 'd';
605 else
606 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100607 msg_nowait = TRUE; // don't delay going to insert mode
608 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611#ifdef FEAT_CMDL_INFO
612 need_flushbuf = add_to_showcmd(c);
613#endif
614
615getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 {
618 /*
619 * Handle a count before a command and compute ca.count0.
620 * Note that '0' is a command and not the start of a count, but it's
621 * part of a count after other digits.
622 */
623 while ( (c >= '1' && c <= '9')
624 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
625 {
626 if (c == K_DEL || c == K_KDEL)
627 {
628 ca.count0 /= 10;
629#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100630 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#endif
632 }
633 else
634 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100635 if (ca.count0 < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000637#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100638 // Set v:count here, when called from main() and not a stuffed
639 // command, so that v:count can be used in an expression mapping
640 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100641 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100642 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if (ctrl_w)
645 {
646 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100647 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100649 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000650 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 --no_zero_mapping;
653 if (ctrl_w)
654 {
655 --no_mapping;
656 --allow_keys;
657 }
658#ifdef FEAT_CMDL_INFO
659 need_flushbuf |= add_to_showcmd(c);
660#endif
661 }
662
663 /*
664 * If we got CTRL-W there may be a/another count
665 */
666 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
667 {
668 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 ca.count0 = 0;
671 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100672 ++allow_keys; // no mapping for nchar, but keys
673 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 --no_mapping;
676 --allow_keys;
677#ifdef FEAT_CMDL_INFO
678 need_flushbuf |= add_to_showcmd(c);
679#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100680 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 }
682 }
683
Bram Moolenaara983fe92008-07-31 20:04:27 +0000684 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100686 // Save the count values so that ca.opcount and ca.count0 are exactly
687 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000688 oap->prev_opcount = ca.opcount;
689 oap->prev_count0 = ca.count0;
690 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100691 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000692 {
693 /*
694 * If we're in the middle of an operator (including after entering a
695 * yank buffer with '"') AND we had a count before the operator, then
696 * that count overrides the current value of ca.count0.
697 * What this means effectively, is that commands like "3dw" get turned
698 * into "d3w" which makes things fall into place pretty neatly.
699 * If you give a count before AND after the operator, they are
700 * multiplied.
701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 if (ca.count0)
703 ca.count0 *= ca.opcount;
704 else
705 ca.count0 = ca.opcount;
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100706 if (ca.count0 < 0) // overflow
707 ca.count0 = 999999999L;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 }
709
710 /*
711 * Always remember the count. It will be set to zero (on the next call,
712 * above) when there is no pending operator.
713 * When called from main(), save the count for use by the "count" built-in
714 * variable.
715 */
716 ca.opcount = ca.count0;
717 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
718
719#ifdef FEAT_EVAL
720 /*
721 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100722 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100724 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000725 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726#endif
727
728 /*
729 * Find the command character in the table of commands.
730 * For CTRL-W we already got nchar when looking for a count.
731 */
732 if (ctrl_w)
733 {
734 ca.nchar = c;
735 ca.cmdchar = Ctrl_W;
736 }
737 else
738 ca.cmdchar = c;
739 idx = find_command(ca.cmdchar);
740 if (idx < 0)
741 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100742 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 clearopbeep(oap);
744 goto normal_end;
745 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000746
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000747 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100749 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000751 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 goto normal_end;
753 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000754 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
755 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 /*
758 * In Visual/Select mode, a few keys are handled in a special way.
759 */
760 if (VIsual_active)
761 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100762 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (km_stopsel
764 && (nv_cmds[idx].cmd_flags & NV_STS)
765 && !(mod_mask & MOD_MASK_SHIFT))
766 {
767 end_visual_mode();
768 redraw_curbuf_later(INVERTED);
769 }
770
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100771 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (km_startsel)
773 {
774 if (nv_cmds[idx].cmd_flags & NV_SS)
775 {
776 unshift_special(&ca);
777 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000778 if (idx < 0)
779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100780 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000781 clearopbeep(oap);
782 goto normal_end;
783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
786 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791#ifdef FEAT_RIGHTLEFT
792 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
793 && (nv_cmds[idx].cmd_flags & NV_RL))
794 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100795 // Invert horizontal movements and operations. Only when typed by the
796 // user directly, not when the result of a mapping or "x" translated
797 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 switch (ca.cmdchar)
799 {
800 case 'l': ca.cmdchar = 'h'; break;
801 case K_RIGHT: ca.cmdchar = K_LEFT; break;
802 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
803 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
804 case 'h': ca.cmdchar = 'l'; break;
805 case K_LEFT: ca.cmdchar = K_RIGHT; break;
806 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
807 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
808 case '>': ca.cmdchar = '<'; break;
809 case '<': ca.cmdchar = '>'; break;
810 }
811 idx = find_command(ca.cmdchar);
812 }
813#endif
814
815 /*
816 * Get an additional character if we need one.
817 */
818 if ((nv_cmds[idx].cmd_flags & NV_NCH)
819 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
820 && oap->op_type == OP_NOP)
821 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
822 || (ca.cmdchar == 'q'
823 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200824 && reg_recording == 0
825 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100827 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
829 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100830 int repl = FALSE; // get character for replace mode
831 int lit = FALSE; // get extra character literally
832 int langmap_active = FALSE; // using :lmap mappings
833 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100834#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100835 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#endif
837
838 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100839 ++allow_keys; // no mapping for nchar, but allow key codes
840 // Don't generate a CursorHold event here, most commands can't handle
841 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000842 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (ca.cmdchar == 'g')
844 {
845 /*
846 * For 'g' get the next character now, so that we can check for
847 * "gr", "g'" and "g`".
848 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000849 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851#ifdef FEAT_CMDL_INFO
852 need_flushbuf |= add_to_showcmd(ca.nchar);
853#endif
854 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100855 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100859 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100864 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866 else
867 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100868 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 repl = TRUE;
870 cp = &ca.nchar;
871 }
872 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
873
874 /*
875 * Get a second or third character.
876 */
877 if (cp != NULL)
878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (repl)
880 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100881 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100882#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100883 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
887 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100888 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 --no_mapping;
890 --allow_keys;
891 if (repl)
892 State = LREPLACE;
893 else
894 State = LANGMAP;
895 langmap_active = TRUE;
896 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100897#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100899 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
901 im_set_active(TRUE);
902#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200903 if ((State & INSERT) && !p_ek)
904 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200905#ifdef FEAT_JOB_CHANNEL
906 ch_log_output = TRUE;
907#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200908 // Disable bracketed paste and modifyOtherKeys here, we won't
909 // recognize the escape sequences with 'esckeys' off.
910 out_str(T_BD);
911 out_str(T_CTE);
912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000914 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Bram Moolenaarca774f62020-08-30 20:46:38 +0200916 if ((State & INSERT) && !p_ek)
917 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200918#ifdef FEAT_JOB_CHANNEL
919 ch_log_output = TRUE;
920#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200921 // Re-enable bracketed paste mode and modifyOtherKeys
922 out_str(T_BE);
923 out_str(T_CTI);
924 }
925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (langmap_active)
927 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100928 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 ++no_mapping;
930 ++allow_keys;
931 State = NORMAL_BUSY;
932 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100933#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (lang)
935 {
936 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
937 im_save_status(&curbuf->b_p_iminsert);
938 im_set_active(FALSE);
939 }
940 p_smd = save_smd;
941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943#ifdef FEAT_CMDL_INFO
944 need_flushbuf |= add_to_showcmd(*cp);
945#endif
946
947 if (!lit)
948 {
949#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100950 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (*cp == Ctrl_K
952 && ((nv_cmds[idx].cmd_flags & NV_LANG)
953 || cp == &ca.extra_char)
954 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
955 {
956 c = get_digraph(FALSE);
957 if (c > 0)
958 {
959 *cp = c;
960# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100961 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 del_from_showcmd(3);
963 need_flushbuf |= add_to_showcmd(*cp);
964# endif
965 }
966 }
967#endif
968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100969 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100972 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 if (p_hkmap && lang && KeyTyped)
974 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975#endif
976 }
977
978 /*
979 * When the next character is CTRL-\ a following CTRL-N means the
980 * command is aborted and we go to Normal mode.
981 */
982 if (cp == &ca.extra_char
983 && ca.nchar == Ctrl_BSL
984 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
985 {
986 ca.cmdchar = Ctrl_BSL;
987 ca.nchar = ca.extra_char;
988 idx = find_command(ca.cmdchar);
989 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200990 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200991 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 else if (*cp == Ctrl_BSL)
993 {
994 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
995
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100996 // There is a busy wait here when typing "f<C-\>" and then
997 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 while ((c = vpeekc()) <= 0 && towait > 0L)
999 {
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01001000 do_sleep(towait > 50L ? 50L : towait, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 towait -= 50L;
1002 }
1003 if (c > 0)
1004 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001005 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (c != Ctrl_N && c != Ctrl_G)
1007 vungetc(c);
1008 else
1009 {
1010 ca.cmdchar = Ctrl_BSL;
1011 ca.nchar = c;
1012 idx = find_command(ca.cmdchar);
1013 }
1014 }
1015 }
1016
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001017 // When getting a text character and the next character is a
1018 // multi-byte character, it could be a composing character.
1019 // However, don't wait for it to arrive. Also, do enable mapping,
1020 // because if it's put back with vungetc() it's too late to apply
1021 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001022 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 while (enc_utf8 && lang && (c = vpeekc()) > 0
1024 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1025 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001026 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (!utf_iscomposing(c))
1028 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001029 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 break;
1031 }
1032 else if (ca.ncharC1 == 0)
1033 ca.ncharC1 = c;
1034 else
1035 ca.ncharC2 = c;
1036 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001037 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
1039 --no_mapping;
1040 --allow_keys;
1041 }
1042
1043#ifdef FEAT_CMDL_INFO
1044 /*
1045 * Flush the showcmd characters onto the screen so we can see them while
1046 * the command is being executed. Only do this when the shown command was
1047 * actually displayed, otherwise this will slow down a lot when executing
1048 * mappings.
1049 */
1050 if (need_flushbuf)
1051 out_flush();
1052#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001053 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001054 {
1055 if (ex_normal_busy)
1056 did_cursorhold = save_did_cursorhold;
1057 else
1058 did_cursorhold = FALSE;
1059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
1061 State = NORMAL;
1062
1063 if (ca.nchar == ESC)
1064 {
1065 clearop(oap);
1066 if (restart_edit == 0 && goto_im())
1067 restart_edit = 'a';
1068 goto normal_end;
1069 }
1070
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001071 if (ca.cmdchar != K_IGNORE)
1072 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001073 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001074 msg_col = 0;
1075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001077 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001079 // When 'keymodel' contains "startsel" some keys start Select/Visual
1080 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 if (!VIsual_active && km_startsel)
1082 {
1083 if (nv_cmds[idx].cmd_flags & NV_SS)
1084 {
1085 start_selection();
1086 unshift_special(&ca);
1087 idx = find_command(ca.cmdchar);
1088 }
1089 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1090 && (mod_mask & MOD_MASK_SHIFT))
1091 {
1092 start_selection();
1093 mod_mask &= ~MOD_MASK_SHIFT;
1094 }
1095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
1097 /*
1098 * Execute the command!
1099 * Call the command function found in the commands table.
1100 */
1101 ca.arg = nv_cmds[idx].cmd_arg;
1102 (nv_cmds[idx].cmd_func)(&ca);
1103
1104 /*
1105 * If we didn't start or finish an operator, reset oap->regname, unless we
1106 * need it later.
1107 */
1108 if (!finish_op
1109 && !oap->op_type
1110 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1111 {
1112 clearop(oap);
1113#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001114 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#endif
1116 }
1117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001118 // Get the length of mapped chars again after typing a count, second
1119 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001120 if (old_mapped_len > 0)
1121 old_mapped_len = typebuf_maplen();
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 /*
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001124 * If an operation is pending, handle it. But not for K_IGNORE or
1125 * K_MOUSEMOVE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001127 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001128 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
1130 /*
1131 * Wait for a moment when a message is displayed that will be overwritten
1132 * by the mode message.
1133 * In Visual mode and with "^O" in Insert mode, a short message will be
1134 * overwritten by the mode message. Wait a bit, until a key is hit.
1135 * In Visual mode, it's more important to keep the Visual area updated
1136 * than keeping a message (e.g. from a /pat search).
1137 * Only do this if the command was typed, not from a mapping.
1138 * Don't wait when emsg_silent is non-zero.
1139 * Also wait a bit after an error message, e.g. for "^O:".
1140 * Don't redraw the screen, it would remove the message.
1141 */
1142 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001143 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 || (VIsual_active
1146 && old_pos.lnum == curwin->w_cursor.lnum
1147 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 )
1149 && (clear_cmdline
1150 || redraw_cmdline)
1151 && (msg_didout || (msg_didany && msg_scroll))
1152 && !msg_nowait
1153 && KeyTyped)
1154 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 && (msg_scroll
1157 || emsg_on_display)))
1158 && oap->regname == 0
1159 && !(ca.retval & CA_COMMAND_BUSY)
1160 && stuff_empty()
1161 && typebuf_typed()
1162 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001163 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 && !did_wait_return
1165 && oap->op_type == OP_NOP)
1166 {
1167 int save_State = State;
1168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001169 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 if (restart_edit != 0)
1171 State = INSERT;
1172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001173 // If need to redraw, and there is a "keep_msg", redraw before the
1174 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1176 {
1177 char_u *kmsg;
1178
1179 kmsg = keep_msg;
1180 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001181 // Showmode() will clear keep_msg, but we want to use it anyway.
1182 // First update w_topline.
1183 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001185 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001187
1188 kmsg = vim_strsave(keep_msg);
1189 if (kmsg != NULL)
1190 {
1191 msg_attr((char *)kmsg, keep_msg_attr);
1192 vim_free(kmsg);
1193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001196#ifdef CURSOR_SHAPE
1197 ui_cursor_shape(); // may show different cursor shape
1198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 cursor_on();
1200 out_flush();
1201 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001202 ui_delay(1003L, TRUE); // wait at least one second
1203 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 State = save_State;
1205
1206 msg_scroll = FALSE;
1207 emsg_on_display = FALSE;
1208 }
1209
1210 /*
1211 * Finish up after executing a Normal mode command.
1212 */
1213normal_end:
1214
1215 msg_nowait = FALSE;
1216
Bram Moolenaarcc613032020-06-07 21:31:18 +02001217#ifdef FEAT_EVAL
1218 if (finish_op)
1219 reset_reg_var();
1220#endif
1221
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001222 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#ifdef CURSOR_SHAPE
1224 c = finish_op;
1225#endif
1226 finish_op = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001227 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 // Redraw the cursor with another shape, if we were in Operator-pending
1230 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 if (c || ca.cmdchar == 'r')
1232 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001233 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234# ifdef FEAT_MOUSESHAPE
1235 update_mouseshape(-1);
1236# endif
1237 }
1238#endif
1239
1240#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001241 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001242 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 clear_showcmd();
1244#endif
1245
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001246 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 vim_free(ca.searchbuf);
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (has_mbyte)
1250 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (curwin->w_p_scb && toplevel)
1253 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001254 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 do_check_scrollbind(TRUE);
1256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
Bram Moolenaar860cae12010-06-05 23:22:07 +02001258 if (curwin->w_p_crb && toplevel)
1259 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001260 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001261 do_check_cursorbind();
1262 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001263
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001264#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001265 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001266 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001267 restart_edit = 0;
1268#endif
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 /*
1271 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1272 * if still inside a mapping that started in Visual mode).
1273 * May switch from Visual to Select mode after CTRL-O command.
1274 */
1275 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1277 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 && !(ca.retval & CA_COMMAND_BUSY)
1279 && stuff_empty()
1280 && oap->regname == 0)
1281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 if (restart_VIsual_select == 1)
1283 {
1284 VIsual_select = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001285 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 showmode();
1287 restart_VIsual_select = 0;
1288 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001289 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 (void)edit(restart_edit, FALSE, 1L);
1291 }
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (restart_VIsual_select == 2)
1294 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001296 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 opcount = ca.opcount;
1298}
1299
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001300#ifdef FEAT_EVAL
1301/*
1302 * Set v:count and v:count1 according to "cap".
1303 * Set v:prevcount only when "set_prevcount" is TRUE.
1304 */
1305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001306set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001307{
1308 long count = cap->count0;
1309
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001310 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001311 if (cap->opcount != 0)
1312 count = cap->opcount * (count == 0 ? 1 : count);
1313 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001314 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001315}
1316#endif
1317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001319 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 * if not.
1321 */
1322 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001323check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324{
1325 static int did_check = FALSE;
1326
1327 if (full_screen)
1328 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001329 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001330 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 did_check = TRUE;
1332 }
1333}
1334
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001335#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1336/*
1337 * Call yank_do_autocmd() for "regname".
1338 */
1339 static void
1340call_yank_do_autocmd(int regname)
1341{
1342 oparg_T oa;
1343 yankreg_T *reg;
1344
1345 clear_oparg(&oa);
1346 oa.regname = regname;
1347 oa.op_type = OP_YANK;
1348 oa.is_VIsual = TRUE;
1349 reg = get_register(regname, TRUE);
1350 yank_do_autocmd(&oa, reg);
1351 free_register(reg);
1352}
1353#endif
1354
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001356 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001357 * This function or the next should ALWAYS be called to end Visual mode, except
1358 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 */
1360 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001361end_visual_mode()
1362{
1363 end_visual_mode_keep_button();
1364 reset_held_button();
1365}
1366
1367 void
1368end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370#ifdef FEAT_CLIPBOARD
1371 /*
1372 * If we are using the clipboard, then remember what was selected in case
1373 * we need to paste it somewhere while we still own the selection.
1374 * Only do this when the clipboard is already owned. Don't want to grab
1375 * the selection when hitting ESC.
1376 */
1377 if (clip_star.available && clip_star.owned)
1378 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001379
1380# if defined(FEAT_EVAL)
1381 // Emit a TextYankPost for the automatic copy of the selection into the
1382 // star and/or plus register.
1383 if (has_textyankpost())
1384 {
1385 if (clip_isautosel_star())
1386 call_yank_do_autocmd('*');
1387 if (clip_isautosel_plus())
1388 call_yank_do_autocmd('+');
1389 }
1390# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#endif
1392
1393 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 setmouse();
1395 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001397 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001398 curbuf->b_visual.vi_mode = VIsual_mode;
1399 curbuf->b_visual.vi_start = VIsual;
1400 curbuf->b_visual.vi_end = curwin->w_cursor;
1401 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#ifdef FEAT_EVAL
1403 curbuf->b_visual_mode_eval = VIsual_mode;
1404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (!virtual_active())
1406 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001407 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001409 adjust_cursor_eol();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001410 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411}
1412
1413/*
1414 * Reset VIsual_active and VIsual_reselect.
1415 */
1416 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001417reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 if (VIsual_active)
1420 {
1421 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001422 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 VIsual_reselect = FALSE;
1425}
1426
1427/*
1428 * Reset VIsual_active and VIsual_reselect if it's set.
1429 */
1430 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001431reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 if (VIsual_active)
1434 {
1435 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001436 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 VIsual_reselect = FALSE;
1438 }
1439}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001441 void
1442restore_visual_mode(void)
1443{
1444 if (VIsual_mode_orig != NUL)
1445 {
1446 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1447 VIsual_mode_orig = NUL;
1448 }
1449}
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451/*
1452 * Check for a balloon-eval special item to include when searching for an
1453 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1454 * Returns TRUE if the character at "*ptr" should be included.
1455 * "dir" is FORWARD or BACKWARD, the direction of searching.
1456 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1457 * "bnp" points to a counter for square brackets.
1458 */
1459 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001460find_is_eval_item(
1461 char_u *ptr,
1462 int *colp,
1463 int *bnp,
1464 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001466 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1468 ++*bnp;
1469 if (*bnp > 0)
1470 {
1471 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1472 --*bnp;
1473 return TRUE;
1474 }
1475
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if (*ptr == '.')
1478 return TRUE;
1479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001480 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1482 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1483 {
1484 *colp += dir;
1485 return TRUE;
1486 }
1487 return FALSE;
1488}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
1490/*
1491 * Find the identifier under or to the right of the cursor.
1492 * "find_type" can have one of three values:
1493 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001494 * FIND_STRING: find any non-white text
1495 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001496 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 *
1498 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001499 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001501 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 * This doesn't match the real Vi but I like it a little better and it
1503 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001504 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 * When FIND_IDENT isn't defined, we backup until a blank.
1506 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001507 * Returns the length of the text, or zero if no text is found.
1508 * If text is found, a pointer to the text is put in "*text". This
1509 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
1511 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001512find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001515 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516}
1517
1518/*
1519 * Like find_ident_under_cursor(), but for any window and any position.
1520 * However: Uses 'iskeyword' from the current window!.
1521 */
1522 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001523find_ident_at_pos(
1524 win_T *wp,
1525 linenr_T lnum,
1526 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001527 char_u **text,
1528 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001529 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
1531 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001532 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 int this_class = 0;
1535 int prev_class;
1536 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001537 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
1539 /*
1540 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001541 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 */
1543 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1544 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1545 {
1546 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001547 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 */
1549 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (has_mbyte)
1551 {
1552 while (ptr[col] != NUL)
1553 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001554 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1556 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 this_class = mb_get_class(ptr + col);
1558 if (this_class != 0 && (i == 1 || this_class != 1))
1559 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001560 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 }
1562 }
1563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001565 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 )
1568 ++col;
1569
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001570 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
1573 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001574 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (has_mbyte)
1577 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001578 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1580 this_class = mb_get_class((char_u *)"a");
1581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001583 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1586 prev_class = mb_get_class(ptr + prevcol);
1587 if (this_class != prev_class
1588 && (i == 0
1589 || prev_class == 0
1590 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 && (!(find_type & FIND_EVAL)
1592 || prevcol == 0
1593 || !find_is_eval_item(ptr + prevcol, &prevcol,
1594 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 )
1596 break;
1597 col = prevcol;
1598 }
1599
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001600 // If we don't want just any old text, or we've found an
1601 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (this_class > 2)
1603 this_class = 2;
1604 if (!(find_type & FIND_STRING) || this_class == 2)
1605 break;
1606 }
1607 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
1609 while (col > 0
1610 && ((i == 0
1611 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001612 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 && (!(find_type & FIND_IDENT)
1614 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 || ((find_type & FIND_EVAL)
1616 && col > 1
1617 && find_is_eval_item(ptr + col - 1, &col,
1618 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 ))
1620 --col;
1621
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001622 // If we don't want just any old text, or we've found an
1623 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1625 break;
1626 }
1627 }
1628
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001629 if (ptr[col] == NUL || (i == 0
1630 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001632 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001633 if ((find_type & FIND_NOERROR) == 0)
1634 {
1635 if (find_type & FIND_STRING)
1636 emsg(_("E348: No string under cursor"));
1637 else
1638 emsg(_(e_noident));
1639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 return 0;
1641 }
1642 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001643 *text = ptr;
1644 if (textcol != NULL)
1645 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001648 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 bn = 0;
1651 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (has_mbyte)
1654 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001655 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 this_class = mb_get_class(ptr);
1657 while (ptr[col] != NUL
1658 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1659 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 || ((find_type & FIND_EVAL)
1661 && col <= (int)startcol
1662 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001664 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001668 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 || ((find_type & FIND_EVAL)
1670 && col <= (int)startcol
1671 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675 return col;
1676}
1677
1678/*
1679 * Prepare for redo of a normal command.
1680 */
1681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001682prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 prep_redo(cap->oap->regname, cap->count0,
1685 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1686}
1687
1688/*
1689 * Prepare for redo of any command.
1690 * Note that only the last argument can be a multi-byte char.
1691 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001692 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001693prep_redo(
1694 int regname,
1695 long num,
1696 int cmd1,
1697 int cmd2,
1698 int cmd3,
1699 int cmd4,
1700 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001702 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1703}
1704
1705/*
1706 * Prepare for redo of any command with extra count after "cmd2".
1707 */
1708 void
1709prep_redo_num2(
1710 int regname,
1711 long num1,
1712 int cmd1,
1713 int cmd2,
1714 long num2,
1715 int cmd3,
1716 int cmd4,
1717 int cmd5)
1718{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001720 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 {
1722 AppendCharToRedobuff('"');
1723 AppendCharToRedobuff(regname);
1724 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001725 if (num1 != 0)
1726 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (cmd1 != NUL)
1728 AppendCharToRedobuff(cmd1);
1729 if (cmd2 != NUL)
1730 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001731 if (num2 != 0)
1732 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (cmd3 != NUL)
1734 AppendCharToRedobuff(cmd3);
1735 if (cmd4 != NUL)
1736 AppendCharToRedobuff(cmd4);
1737 if (cmd5 != NUL)
1738 AppendCharToRedobuff(cmd5);
1739}
1740
1741/*
1742 * check for operator active and clear it
1743 *
1744 * return TRUE if operator was active
1745 */
1746 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001747checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
1749 if (oap->op_type == OP_NOP)
1750 return FALSE;
1751 clearopbeep(oap);
1752 return TRUE;
1753}
1754
1755/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001756 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001758 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 */
1760 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001761checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001763 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 return FALSE;
1765 clearopbeep(oap);
1766 return TRUE;
1767}
1768
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001770clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 oap->op_type = OP_NOP;
1773 oap->regname = 0;
1774 oap->motion_force = NUL;
1775 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001776 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777}
1778
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001780clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 clearop(oap);
1783 beep_flush();
1784}
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786/*
1787 * Remove the shift modifier from a special key.
1788 */
1789 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001790unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791{
1792 switch (cap->cmdchar)
1793 {
1794 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1795 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1796 case K_S_UP: cap->cmdchar = K_UP; break;
1797 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1798 case K_S_HOME: cap->cmdchar = K_HOME; break;
1799 case K_S_END: cap->cmdchar = K_END; break;
1800 }
1801 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1802}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001804/*
1805 * If the mode is currently displayed clear the command line or update the
1806 * command displayed.
1807 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001809may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001810{
1811 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001812 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001813#ifdef FEAT_CMDL_INFO
1814 else
1815 clear_showcmd();
1816#endif
1817}
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1820/*
1821 * Routines for displaying a partly typed command
1822 */
1823
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001824#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001826static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827static int showcmd_is_clear = TRUE;
1828static int showcmd_visual = FALSE;
1829
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001830static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
1832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001833clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
1835 if (!p_sc)
1836 return;
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (VIsual_active && !char_avail())
1839 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001840 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 long lines;
1842 colnr_T leftcol, rightcol;
1843 linenr_T top, bot;
1844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001845 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001846 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
1848 top = VIsual.lnum;
1849 bot = curwin->w_cursor.lnum;
1850 }
1851 else
1852 {
1853 top = curwin->w_cursor.lnum;
1854 bot = VIsual.lnum;
1855 }
1856# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001857 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001858 (void)hasFolding(top, &top, NULL);
1859 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860# endif
1861 lines = bot - top + 1;
1862
1863 if (VIsual_mode == Ctrl_V)
1864 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001865# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001866 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001867 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001868
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001869 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001870 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001871 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001874# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001875 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001876 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1879 (long)(rightcol - leftcol + 1));
1880 }
1881 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1882 sprintf((char *)showcmd_buf, "%ld", lines);
1883 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001884 {
1885 char_u *s, *e;
1886 int l;
1887 int bytes = 0;
1888 int chars = 0;
1889
1890 if (cursor_bot)
1891 {
1892 s = ml_get_pos(&VIsual);
1893 e = ml_get_cursor();
1894 }
1895 else
1896 {
1897 s = ml_get_cursor();
1898 e = ml_get_pos(&VIsual);
1899 }
1900 while ((*p_sel != 'e') ? s <= e : s < e)
1901 {
1902 l = (*mb_ptr2len)(s);
1903 if (l == 0)
1904 {
1905 ++bytes;
1906 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001907 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001908 }
1909 bytes += l;
1910 ++chars;
1911 s += l;
1912 }
1913 if (bytes == chars)
1914 sprintf((char *)showcmd_buf, "%d", chars);
1915 else
1916 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1917 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001918 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 showcmd_visual = TRUE;
1920 }
1921 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 showcmd_buf[0] = NUL;
1924 showcmd_visual = FALSE;
1925
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001926 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (showcmd_is_clear)
1928 return;
1929 }
1930
1931 display_showcmd();
1932}
1933
1934/*
1935 * Add 'c' to string of shown command chars.
1936 * Return TRUE if output has been written (and setcursor() has been called).
1937 */
1938 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001939add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940{
1941 char_u *p;
1942 int old_len;
1943 int extra_len;
1944 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 int i;
1946 static int ignore[] =
1947 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001948#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1950 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001951#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001952 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001953 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1955 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001956 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001958 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 0
1960 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961
Bram Moolenaar09df3122006-01-23 22:23:09 +00001962 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 return FALSE;
1964
1965 if (showcmd_visual)
1966 {
1967 showcmd_buf[0] = NUL;
1968 showcmd_visual = FALSE;
1969 }
1970
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001971 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (IS_SPECIAL(c))
1973 for (i = 0; ignore[i] != 0; ++i)
1974 if (ignore[i] == c)
1975 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
1977 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001978 if (*p == ' ')
1979 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 old_len = (int)STRLEN(showcmd_buf);
1981 extra_len = (int)STRLEN(p);
1982 overflow = old_len + extra_len - SHOWCMD_COLS;
1983 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001984 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1985 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 STRCAT(showcmd_buf, p);
1987
1988 if (char_avail())
1989 return FALSE;
1990
1991 display_showcmd();
1992
1993 return TRUE;
1994}
1995
1996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001997add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998{
1999 if (!add_to_showcmd(c))
2000 setcursor();
2001}
2002
2003/*
2004 * Delete 'len' characters from the end of the shown command.
2005 */
2006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002007del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
2009 int old_len;
2010
2011 if (!p_sc)
2012 return;
2013
2014 old_len = (int)STRLEN(showcmd_buf);
2015 if (len > old_len)
2016 len = old_len;
2017 showcmd_buf[old_len - len] = NUL;
2018
2019 if (!char_avail())
2020 display_showcmd();
2021}
2022
2023/*
2024 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2025 * something and there is a partial mapping.
2026 */
2027 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002028push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
2030 if (p_sc)
2031 STRCPY(old_showcmd_buf, showcmd_buf);
2032}
2033
2034 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002035pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036{
2037 if (!p_sc)
2038 return;
2039
2040 STRCPY(showcmd_buf, old_showcmd_buf);
2041
2042 display_showcmd();
2043}
2044
2045 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002046display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047{
2048 int len;
2049
2050 cursor_off();
2051
2052 len = (int)STRLEN(showcmd_buf);
2053 if (len == 0)
2054 showcmd_is_clear = TRUE;
2055 else
2056 {
2057 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2058 showcmd_is_clear = FALSE;
2059 }
2060
2061 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002062 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2063 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 */
2065 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2066
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002067 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068}
2069#endif
2070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071/*
2072 * When "check" is FALSE, prepare for commands that scroll the window.
2073 * When "check" is TRUE, take care of scroll-binding after the window has
2074 * scrolled. Called from normal_cmd() and edit().
2075 */
2076 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002077do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078{
2079 static win_T *old_curwin = NULL;
2080 static linenr_T old_topline = 0;
2081#ifdef FEAT_DIFF
2082 static int old_topfill = 0;
2083#endif
2084 static buf_T *old_buf = NULL;
2085 static colnr_T old_leftcol = 0;
2086
2087 if (check && curwin->w_p_scb)
2088 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002089 // If a ":syncbind" command was just used, don't scroll, only reset
2090 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 if (did_syncbind)
2092 did_syncbind = FALSE;
2093 else if (curwin == old_curwin)
2094 {
2095 /*
2096 * Synchronize other windows, as necessary according to
2097 * 'scrollbind'. Don't do this after an ":edit" command, except
2098 * when 'diff' is set.
2099 */
2100 if ((curwin->w_buffer == old_buf
2101#ifdef FEAT_DIFF
2102 || curwin->w_p_diff
2103#endif
2104 )
2105 && (curwin->w_topline != old_topline
2106#ifdef FEAT_DIFF
2107 || curwin->w_topfill != old_topfill
2108#endif
2109 || curwin->w_leftcol != old_leftcol))
2110 {
2111 check_scrollbind(curwin->w_topline - old_topline,
2112 (long)(curwin->w_leftcol - old_leftcol));
2113 }
2114 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002115 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 {
2117 /*
2118 * When switching between windows, make sure that the relative
2119 * vertical offset is valid for the new window. The relative
2120 * offset is invalid whenever another 'scrollbind' window has
2121 * scrolled to a point that would force the current window to
2122 * scroll past the beginning or end of its buffer. When the
2123 * resync is performed, some of the other 'scrollbind' windows may
2124 * need to jump so that the current window's relative position is
2125 * visible on-screen.
2126 */
2127 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2128 }
2129 curwin->w_scbind_pos = curwin->w_topline;
2130 }
2131
2132 old_curwin = curwin;
2133 old_topline = curwin->w_topline;
2134#ifdef FEAT_DIFF
2135 old_topfill = curwin->w_topfill;
2136#endif
2137 old_buf = curwin->w_buffer;
2138 old_leftcol = curwin->w_leftcol;
2139}
2140
2141/*
2142 * Synchronize any windows that have "scrollbind" set, based on the
2143 * number of rows by which the current window has changed
2144 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2145 */
2146 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 int want_ver;
2150 int want_hor;
2151 win_T *old_curwin = curwin;
2152 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 int old_VIsual_select = VIsual_select;
2154 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 colnr_T tgt_leftcol = curwin->w_leftcol;
2156 long topline;
2157 long y;
2158
2159 /*
2160 * check 'scrollopt' string for vertical and horizontal scroll options
2161 */
2162 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2163#ifdef FEAT_DIFF
2164 want_ver |= old_curwin->w_p_diff;
2165#endif
2166 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2167
2168 /*
2169 * loop through the scrollbound windows and scroll accordingly
2170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002172 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
2174 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002175 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (curwin != old_curwin && curwin->w_p_scb)
2177 {
2178 /*
2179 * do the vertical scroll
2180 */
2181 if (want_ver)
2182 {
2183#ifdef FEAT_DIFF
2184 if (old_curwin->w_p_diff && curwin->w_p_diff)
2185 {
2186 diff_set_topline(old_curwin, curwin);
2187 }
2188 else
2189#endif
2190 {
2191 curwin->w_scbind_pos += topline_diff;
2192 topline = curwin->w_scbind_pos;
2193 if (topline > curbuf->b_ml.ml_line_count)
2194 topline = curbuf->b_ml.ml_line_count;
2195 if (topline < 1)
2196 topline = 1;
2197
2198 y = topline - curwin->w_topline;
2199 if (y > 0)
2200 scrollup(y, FALSE);
2201 else
2202 scrolldown(-y, FALSE);
2203 }
2204
2205 redraw_later(VALID);
2206 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209
2210 /*
2211 * do the horizontal scroll
2212 */
2213 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2214 {
2215 curwin->w_leftcol = tgt_leftcol;
2216 leftcol_changed();
2217 }
2218 }
2219 }
2220
2221 /*
2222 * reset current-window
2223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 VIsual_select = old_VIsual_select;
2225 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 curwin = old_curwin;
2227 curbuf = old_curbuf;
2228}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230/*
2231 * Command character that's ignored.
2232 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002233 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002236nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002238 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239}
2240
2241/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002242 * Command character that doesn't do anything, but unlike nv_ignore() does
2243 * start edit(). Used for "startinsert" executed while starting up.
2244 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002245 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002246nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002247{
2248}
2249
2250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 * Command character doesn't exist.
2252 */
2253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002254nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
2256 clearopbeep(cap->oap);
2257}
2258
2259/*
2260 * <Help> and <F1> commands.
2261 */
2262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002263nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264{
2265 if (!checkclearopq(cap->oap))
2266 ex_help(NULL);
2267}
2268
2269/*
2270 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2271 */
2272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002273nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002275#ifdef FEAT_JOB_CHANNEL
2276 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2277 clearopbeep(cap->oap);
2278 else
2279#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002280 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002281 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002282 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002283 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2284 op_addsub(cap->oap, cap->count1, cap->arg);
2285 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002286 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002287 else if (VIsual_active)
2288 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002289 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002290 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291}
2292
2293/*
2294 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2295 */
2296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002297nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002300 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301 if (mod_mask & MOD_MASK_CTRL)
2302 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002303 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002304 if (cap->arg == BACKWARD)
2305 goto_tabpage(-(int)cap->count1);
2306 else
2307 goto_tabpage((int)cap->count0);
2308 }
2309 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002310 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312}
2313
2314/*
2315 * Implementation of "gd" and "gD" command.
2316 */
2317 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002318nv_gd(
2319 oparg_T *oap,
2320 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002321 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
2323 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 char_u *ptr;
2325
Bram Moolenaard9d30582005-05-18 22:10:28 +00002326 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002327 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002328 == FAIL)
2329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002331 }
2332 else
2333 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002334#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002335 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2336 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002337#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002338 // clear any search statistics
2339 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2340 clear_cmdline = TRUE;
2341 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002342}
2343
2344/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002345 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2346 * otherwise.
2347 */
2348 static int
2349is_ident(char_u *line, int offset)
2350{
2351 int i;
2352 int incomment = FALSE;
2353 int instring = 0;
2354 int prev = 0;
2355
2356 for (i = 0; i < offset && line[i] != NUL; i++)
2357 {
2358 if (instring != 0)
2359 {
2360 if (prev != '\\' && line[i] == instring)
2361 instring = 0;
2362 }
2363 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2364 {
2365 instring = line[i];
2366 }
2367 else
2368 {
2369 if (incomment)
2370 {
2371 if (prev == '*' && line[i] == '/')
2372 incomment = FALSE;
2373 }
2374 else if (prev == '/' && line[i] == '*')
2375 {
2376 incomment = TRUE;
2377 }
2378 else if (prev == '/' && line[i] == '/')
2379 {
2380 return FALSE;
2381 }
2382 }
2383
2384 prev = line[i];
2385 }
2386
2387 return incomment == FALSE && instring == 0;
2388}
2389
2390/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002391 * Search for variable declaration of "ptr[len]".
2392 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2393 * current file ("gD").
2394 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002395 * Return FAIL when not found.
2396 */
2397 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002398find_decl(
2399 char_u *ptr,
2400 int len,
2401 int locally,
2402 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002403 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002404{
2405 char_u *pat;
2406 pos_T old_pos;
2407 pos_T par_pos;
2408 pos_T found_pos;
2409 int t;
2410 int save_p_ws;
2411 int save_p_scs;
2412 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002413 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002414 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002415 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002416
2417 if ((pat = alloc(len + 7)) == NULL)
2418 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002419
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002420 // Put "\V" before the pattern to avoid that the special meaning of "."
2421 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002422 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2423 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 old_pos = curwin->w_cursor;
2425 save_p_ws = p_ws;
2426 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002427 p_ws = FALSE; // don't wrap around end of file now
2428 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 /*
2431 * With "gD" go to line 1.
2432 * With "gd" Search back for the start of the current function, then go
2433 * back until a blank line. If this fails go to line 1.
2434 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002435 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002437 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002439 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 }
2441 else
2442 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002443 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2445 --curwin->w_cursor.lnum;
2446 }
2447 curwin->w_cursor.col = 0;
2448
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002449 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002450 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002451 for (;;)
2452 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002453 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002454 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002455 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002456 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002457
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002458 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002459 {
2460 pos_T *pos;
2461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002462 // Check that the block the match is in doesn't end before the
2463 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002464 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2465 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2466 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002467 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002468 // There can't be a useful match before the end of this block.
2469 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002470 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002471 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002472 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002473 }
2474
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002475 if (t == FAIL)
2476 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002477 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002478 if (found_pos.lnum != 0)
2479 {
2480 curwin->w_cursor = found_pos;
2481 t = OK;
2482 }
2483 break;
2484 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002485 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002486 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002487 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002488 ++curwin->w_cursor.lnum;
2489 curwin->w_cursor.col = 0;
2490 continue;
2491 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002492 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002494 // If the current position is not a valid identifier and a previous
2495 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002496 if (!valid && found_pos.lnum != 0)
2497 {
2498 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002499 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002500 }
2501
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002502 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002503 if (valid && !locally)
2504 break;
2505 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002506 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002507 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002508 if (found_pos.lnum != 0)
2509 curwin->w_cursor = found_pos;
2510 break;
2511 }
2512
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002513 // For finding a local variable and the match is before the "{" or
2514 // inside a comment, continue searching. For K&R style function
2515 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002516 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002517 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002518 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002519 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002520 // Remove SEARCH_START from flags to avoid getting stuck at one
2521 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002522 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002524
2525 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002527 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 curwin->w_cursor = old_pos;
2529 }
2530 else
2531 {
2532 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002533 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 reset_search_dir();
2535 }
2536
2537 vim_free(pat);
2538 p_ws = save_p_ws;
2539 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002540
2541 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542}
2543
2544/*
2545 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2546 * lines rather than lines in the file.
2547 * 'dist' must be positive.
2548 *
2549 * Return OK if able to move cursor, FAIL otherwise.
2550 */
2551 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002552nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
2554 int linelen = linetabsize(ml_get_curline());
2555 int retval = OK;
2556 int atend = FALSE;
2557 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002558 int col_off1; // margin offset for first screen line
2559 int col_off2; // margin offset for wrapped screen line
2560 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002561 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
2563 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002564 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566 col_off1 = curwin_col_off();
2567 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002568 width1 = curwin->w_width - col_off1;
2569 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002570 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002571 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 /*
2576 * Instead of sticking at the last character of the buffer line we
2577 * try to stick in the last column of the screen.
2578 */
2579 if (curwin->w_curswant == MAXCOL)
2580 {
2581 atend = TRUE;
2582 validate_virtcol();
2583 if (width1 <= 0)
2584 curwin->w_curswant = 0;
2585 else
2586 {
2587 curwin->w_curswant = width1 - 1;
2588 if (curwin->w_virtcol > curwin->w_curswant)
2589 curwin->w_curswant += ((curwin->w_virtcol
2590 - curwin->w_curswant - 1) / width2 + 1) * width2;
2591 }
2592 }
2593 else
2594 {
2595 if (linelen > width1)
2596 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2597 else
2598 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002599 if (curwin->w_curswant >= (colnr_T)n)
2600 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
2602
2603 while (dist--)
2604 {
2605 if (dir == BACKWARD)
2606 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002607 if ((long)curwin->w_curswant >= width1
2608#ifdef FEAT_FOLDING
2609 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2610#endif
2611 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002612 // Move back within the line. This can give a negative value
2613 // for w_curswant if width1 < width2 (with cpoptions+=n),
2614 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 curwin->w_curswant -= width2;
2616 else
2617 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002618 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002620 // Move to the start of a closed fold. Don't do that when
2621 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (!(fdo_flags & FDO_ALL))
2623 (void)hasFolding(curwin->w_cursor.lnum,
2624 &curwin->w_cursor.lnum, NULL);
2625#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002626 if (curwin->w_cursor.lnum == 1)
2627 {
2628 retval = FAIL;
2629 break;
2630 }
2631 --curwin->w_cursor.lnum;
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 linelen = linetabsize(ml_get_curline());
2634 if (linelen > width1)
2635 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2636 + 1) * width2;
2637 }
2638 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002639 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 {
2641 if (linelen > width1)
2642 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2643 else
2644 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002645 if (curwin->w_curswant + width2 < (colnr_T)n
2646#ifdef FEAT_FOLDING
2647 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2648#endif
2649 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002650 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 curwin->w_curswant += width2;
2652 else
2653 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002654 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002656 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2658 &curwin->w_cursor.lnum);
2659#endif
2660 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2661 {
2662 retval = FAIL;
2663 break;
2664 }
2665 curwin->w_cursor.lnum++;
2666 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002667 // Check if the cursor has moved below the number display
2668 // when width1 < width2 (with cpoptions+=n). Subtract width2
2669 // to get a negative value for w_curswant, which will get
2670 // clipped to column 0.
2671 if (curwin->w_curswant >= width1)
2672 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002673 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675 }
2676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002679 if (virtual_active() && atend)
2680 coladvance(MAXCOL);
2681 else
2682 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2685 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002686 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002687 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002688
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 /*
2690 * Check for landing on a character that got split at the end of the
2691 * last line. We want to advance a screenline, not end up in the same
2692 * screenline or move two screenlines.
2693 */
2694 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002695 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002696#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002697 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2698 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002699#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002700
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002701 c = (*mb_ptr2char)(ml_get_cursor());
2702 if (dir == FORWARD && virtcol < curwin->w_curswant
2703 && (curwin->w_curswant <= (colnr_T)width1)
2704 && !vim_isprintc(c) && c > 255)
2705 oneright();
2706
Bram Moolenaar773b1582014-08-29 14:20:51 +02002707 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 && (curwin->w_curswant < (colnr_T)width1
2709 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2710 : ((curwin->w_curswant - width1) % width2
2711 > (colnr_T)width2 / 2)))
2712 --curwin->w_cursor.col;
2713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
2715 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002716 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 return retval;
2719}
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721/*
2722 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2723 * cap->arg must be TRUE for CTRL-E.
2724 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002725 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002726nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727{
2728 if (!checkclearop(cap->oap))
2729 scroll_redraw(cap->arg, cap->count1);
2730}
2731
2732/*
2733 * Scroll "count" lines up or down, and redraw.
2734 */
2735 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002736scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737{
2738 linenr_T prev_topline = curwin->w_topline;
2739#ifdef FEAT_DIFF
2740 int prev_topfill = curwin->w_topfill;
2741#endif
2742 linenr_T prev_lnum = curwin->w_cursor.lnum;
2743
2744 if (up)
2745 scrollup(count, TRUE);
2746 else
2747 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002748 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002750 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2751 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 cursor_correct();
2753 check_cursor_moved(curwin);
2754 curwin->w_valid |= VALID_TOPLINE;
2755
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002756 // If moved back to where we were, at least move the cursor, otherwise
2757 // we get stuck at one position. Don't move the cursor up if the
2758 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 while (curwin->w_topline == prev_topline
2760#ifdef FEAT_DIFF
2761 && curwin->w_topfill == prev_topfill
2762#endif
2763 )
2764 {
2765 if (up)
2766 {
2767 if (curwin->w_cursor.lnum > prev_lnum
2768 || cursor_down(1L, FALSE) == FAIL)
2769 break;
2770 }
2771 else
2772 {
2773 if (curwin->w_cursor.lnum < prev_lnum
2774 || prev_topline == 1L
2775 || cursor_up(1L, FALSE) == FAIL)
2776 break;
2777 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002778 // Mark w_topline as valid, otherwise the screen jumps back at the
2779 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 check_cursor_moved(curwin);
2781 curwin->w_valid |= VALID_TOPLINE;
2782 }
2783 }
2784 if (curwin->w_cursor.lnum != prev_lnum)
2785 coladvance(curwin->w_curswant);
2786 redraw_later(VALID);
2787}
2788
2789/*
2790 * Commands that start with "z".
2791 */
2792 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002793nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 long n;
2796 colnr_T col;
2797 int nchar = cap->nchar;
2798#ifdef FEAT_FOLDING
2799 long old_fdl = curwin->w_p_fdl;
2800 int old_fen = curwin->w_p_fen;
2801#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002802#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002803 int undo = FALSE;
2804#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002805 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806
2807 if (VIM_ISDIGIT(nchar))
2808 {
2809 /*
2810 * "z123{nchar}": edit the count before obtaining {nchar}
2811 */
2812 if (checkclearop(cap->oap))
2813 return;
2814 n = nchar - '0';
2815 for (;;)
2816 {
2817#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002818 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819#endif
2820 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002821 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002822 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 --no_mapping;
2825 --allow_keys;
2826#ifdef FEAT_CMDL_INFO
2827 (void)add_to_showcmd(nchar);
2828#endif
2829 if (nchar == K_DEL || nchar == K_KDEL)
2830 n /= 10;
2831 else if (VIM_ISDIGIT(nchar))
2832 n = n * 10 + (nchar - '0');
2833 else if (nchar == CAR)
2834 {
2835#ifdef FEAT_GUI
2836 need_mouse_correct = TRUE;
2837#endif
2838 win_setheight((int)n);
2839 break;
2840 }
2841 else if (nchar == 'l'
2842 || nchar == 'h'
2843 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002844 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
2846 cap->count1 = n ? n * cap->count1 : cap->count1;
2847 goto dozet;
2848 }
2849 else
2850 {
2851 clearopbeep(cap->oap);
2852 break;
2853 }
2854 }
2855 cap->oap->op_type = OP_NOP;
2856 return;
2857 }
2858
2859dozet:
2860 if (
2861#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002862 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2863 // and "zC" only in Visual mode. "zj" and "zk" are motion
2864 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 cap->nchar != 'f' && cap->nchar != 'F'
2866 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2867 && cap->nchar != 'j' && cap->nchar != 'k'
2868 &&
2869#endif
2870 checkclearop(cap->oap))
2871 return;
2872
2873 /*
2874 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2875 * If line number given, set cursor.
2876 */
2877 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2878 && cap->count0
2879 && cap->count0 != curwin->w_cursor.lnum)
2880 {
2881 setpcmark();
2882 if (cap->count0 > curbuf->b_ml.ml_line_count)
2883 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2884 else
2885 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002886 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888
2889 switch (nchar)
2890 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002891 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 case '+':
2893 if (cap->count0 == 0)
2894 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002895 // No count given: put cursor at the line below screen
2896 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2898 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2899 else
2900 curwin->w_cursor.lnum = curwin->w_botline;
2901 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002902 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 case NL:
2904 case CAR:
2905 case K_KENTER:
2906 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
2909 case 't': scroll_cursor_top(0, TRUE);
2910 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002911 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 break;
2913
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002914 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002916 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 case 'z': scroll_cursor_halfway(TRUE);
2919 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002920 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 break;
2922
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002923 // "z^", "z-" and "zb": put cursor at bottom of screen
2924 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2925 // when <count> is at bottom of window, and puts that one at
2926 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 if (cap->count0 != 0)
2928 {
2929 scroll_cursor_bot(0, TRUE);
2930 curwin->w_cursor.lnum = curwin->w_topline;
2931 }
2932 else if (curwin->w_topline == 1)
2933 curwin->w_cursor.lnum = 1;
2934 else
2935 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002936 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 case '-':
2938 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002939 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940
2941 case 'b': scroll_cursor_bot(0, TRUE);
2942 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002943 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 break;
2945
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002948 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002949 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002951 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 case 'h':
2953 case K_LEFT:
2954 if (!curwin->w_p_wrap)
2955 {
2956 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2957 curwin->w_leftcol = 0;
2958 else
2959 curwin->w_leftcol -= (colnr_T)cap->count1;
2960 leftcol_changed();
2961 }
2962 break;
2963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002965 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002968 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 case 'l':
2970 case K_RIGHT:
2971 if (!curwin->w_p_wrap)
2972 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002973 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 curwin->w_leftcol += (colnr_T)cap->count1;
2975 leftcol_changed();
2976 }
2977 break;
2978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002979 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 case 's': if (!curwin->w_p_wrap)
2981 {
2982#ifdef FEAT_FOLDING
2983 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 else
2986#endif
2987 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002988 if ((long)col > siso)
2989 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 else
2991 col = 0;
2992 if (curwin->w_leftcol != col)
2993 {
2994 curwin->w_leftcol = col;
2995 redraw_later(NOT_VALID);
2996 }
2997 }
2998 break;
2999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 case 'e': if (!curwin->w_p_wrap)
3002 {
3003#ifdef FEAT_FOLDING
3004 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003005 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 else
3007#endif
3008 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02003009 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01003010 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 col = 0;
3012 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01003013 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 if (curwin->w_leftcol != col)
3015 {
3016 curwin->w_leftcol = col;
3017 redraw_later(NOT_VALID);
3018 }
3019 }
3020 break;
3021
Christian Brabandt2fa93842021-05-30 22:17:25 +02003022 // "zp", "zP" in block mode put without addind trailing spaces
3023 case 'P':
3024 case 'p': nv_put(cap);
3025 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02003026 // "zy" Yank without trailing spaces
3027 case 'y': nv_operator(cap);
3028 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003030 // "zF": create fold command
3031 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 case 'F':
3033 case 'f': if (foldManualAllowed(TRUE))
3034 {
3035 cap->nchar = 'f';
3036 nv_operator(cap);
3037 curwin->w_p_fen = TRUE;
3038
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003039 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3041 {
3042 nv_operator(cap);
3043 finish_op = TRUE;
3044 }
3045 }
3046 else
3047 clearopbeep(cap->oap);
3048 break;
3049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003050 // "zd": delete fold at cursor
3051 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 case 'd':
3053 case 'D': if (foldManualAllowed(FALSE))
3054 {
3055 if (VIsual_active)
3056 nv_operator(cap);
3057 else
3058 deleteFold(curwin->w_cursor.lnum,
3059 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3060 }
3061 break;
3062
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003063 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 case 'E': if (foldmethodIsManual(curwin))
3065 {
3066 clearFolding(curwin);
3067 changed_window_setting();
3068 }
3069 else if (foldmethodIsMarker(curwin))
3070 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3071 TRUE, FALSE);
3072 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003073 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 break;
3075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003076 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 case 'n': curwin->w_p_fen = FALSE;
3078 break;
3079
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003080 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 case 'N': curwin->w_p_fen = TRUE;
3082 break;
3083
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003084 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3086 break;
3087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003088 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3090 openFold(curwin->w_cursor.lnum, cap->count1);
3091 else
3092 {
3093 closeFold(curwin->w_cursor.lnum, cap->count1);
3094 curwin->w_p_fen = TRUE;
3095 }
3096 break;
3097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3100 openFoldRecurse(curwin->w_cursor.lnum);
3101 else
3102 {
3103 closeFoldRecurse(curwin->w_cursor.lnum);
3104 curwin->w_p_fen = TRUE;
3105 }
3106 break;
3107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 case 'o': if (VIsual_active)
3110 nv_operator(cap);
3111 else
3112 openFold(curwin->w_cursor.lnum, cap->count1);
3113 break;
3114
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003115 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 case 'O': if (VIsual_active)
3117 nv_operator(cap);
3118 else
3119 openFoldRecurse(curwin->w_cursor.lnum);
3120 break;
3121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003122 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 case 'c': if (VIsual_active)
3124 nv_operator(cap);
3125 else
3126 closeFold(curwin->w_cursor.lnum, cap->count1);
3127 curwin->w_p_fen = TRUE;
3128 break;
3129
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003130 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 case 'C': if (VIsual_active)
3132 nv_operator(cap);
3133 else
3134 closeFoldRecurse(curwin->w_cursor.lnum);
3135 curwin->w_p_fen = TRUE;
3136 break;
3137
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003138 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 case 'v': foldOpenCursor();
3140 break;
3141
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003142 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003144 curwin->w_foldinvalid = TRUE; // recompute folds
3145 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 foldOpenCursor();
3147 break;
3148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003149 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003151 curwin->w_foldinvalid = TRUE; // recompute folds
3152 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 break;
3154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003155 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003157 {
3158 curwin->w_p_fdl -= cap->count1;
3159 if (curwin->w_p_fdl < 0)
3160 curwin->w_p_fdl = 0;
3161 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003162 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 curwin->w_p_fen = TRUE;
3164 break;
3165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003166 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003168 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 curwin->w_p_fen = TRUE;
3170 break;
3171
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003172 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003173 case 'r': curwin->w_p_fdl += cap->count1;
3174 {
3175 int d = getDeepestNesting();
3176
3177 if (curwin->w_p_fdl >= d)
3178 curwin->w_p_fdl = d;
3179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 break;
3181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003184 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 break;
3186
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003187 case 'j': // "zj" move to next fold downwards
3188 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3190 cap->count1) == FAIL)
3191 clearopbeep(cap->oap);
3192 break;
3193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003194#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003196#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003198 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003199 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003200 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003201 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003202 --no_mapping;
3203 --allow_keys;
3204#ifdef FEAT_CMDL_INFO
3205 (void)add_to_showcmd(nchar);
3206#endif
3207 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3208 {
3209 clearopbeep(cap->oap);
3210 break;
3211 }
3212 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003213 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003215 case 'g': // "zg": add good word to word list
3216 case 'w': // "zw": add wrong word to word list
3217 case 'G': // "zG": add good word to temp word list
3218 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003219 {
3220 char_u *ptr = NULL;
3221 int len;
3222
3223 if (checkclearop(cap->oap))
3224 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003225 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3226 == FAIL)
3227 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003228 if (ptr == NULL)
3229 {
3230 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003232 // Find bad word under the cursor. When 'spell' is
3233 // off this fails and find_ident_under_cursor() is
3234 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003235 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003236 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003237 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003238 if (len != 0 && curwin->w_cursor.col <= pos.col)
3239 ptr = ml_get_pos(&curwin->w_cursor);
3240 curwin->w_cursor = pos;
3241 }
3242
Bram Moolenaarb765d632005-06-07 21:00:02 +00003243 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3244 FIND_IDENT)) == 0)
3245 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003246 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3247 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003248 (nchar == 'G' || nchar == 'W')
3249 ? 0 : (int)cap->count1,
3250 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003251 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003252 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003253
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003254 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003255 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003256 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003257 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003258#endif
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 default: clearopbeep(cap->oap);
3261 }
3262
3263#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003264 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (old_fen != curwin->w_p_fen)
3266 {
3267# ifdef FEAT_DIFF
3268 win_T *wp;
3269
3270 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3271 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003272 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 FOR_ALL_WINDOWS(wp)
3274 {
3275 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3276 {
3277 wp->w_p_fen = curwin->w_p_fen;
3278 changed_window_setting_win(wp);
3279 }
3280 }
3281 }
3282# endif
3283 changed_window_setting();
3284 }
3285
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003286 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (old_fdl != curwin->w_p_fdl)
3288 newFoldLevel();
3289#endif
3290}
3291
3292#ifdef FEAT_GUI
3293/*
3294 * Vertical scrollbar movement.
3295 */
3296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003297nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 if (cap->oap->op_type != OP_NOP)
3300 clearopbeep(cap->oap);
3301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003302 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 gui_do_scroll();
3304}
3305
3306/*
3307 * Horizontal scrollbar movement.
3308 */
3309 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003310nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
3312 if (cap->oap->op_type != OP_NOP)
3313 clearopbeep(cap->oap);
3314
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003315 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003316 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317}
3318#endif
3319
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003320#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003321/*
3322 * Click in GUI tab.
3323 */
3324 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003325nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003326{
3327 if (cap->oap->op_type != OP_NOP)
3328 clearopbeep(cap->oap);
3329
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003330 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003331 goto_tabpage(current_tab);
3332}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003333
3334/*
3335 * Selected item in tab line menu.
3336 */
3337 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003338nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003339{
3340 if (cap->oap->op_type != OP_NOP)
3341 clearopbeep(cap->oap);
3342
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003343 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003344 handle_tabmenu();
3345}
3346
3347/*
3348 * Handle selecting an item of the GUI tab line menu.
3349 * Used in Normal and Insert mode.
3350 */
3351 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003352handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003353{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003354 switch (current_tabmenu)
3355 {
3356 case TABLINE_MENU_CLOSE:
3357 if (current_tab == 0)
3358 do_cmdline_cmd((char_u *)"tabclose");
3359 else
3360 {
3361 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3362 current_tab);
3363 do_cmdline_cmd(IObuff);
3364 }
3365 break;
3366
3367 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003368 if (current_tab == 0)
3369 do_cmdline_cmd((char_u *)"$tabnew");
3370 else
3371 {
3372 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3373 current_tab - 1);
3374 do_cmdline_cmd(IObuff);
3375 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003376 break;
3377
3378 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003379 if (current_tab == 0)
3380 do_cmdline_cmd((char_u *)"browse $tabnew");
3381 else
3382 {
3383 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3384 current_tab - 1);
3385 do_cmdline_cmd(IObuff);
3386 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003387 break;
3388 }
3389}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003390#endif
3391
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392/*
3393 * "Q" command.
3394 */
3395 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003396nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
3398 /*
3399 * Ignore 'Q' in Visual mode, just give a beep.
3400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003402 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003403 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 do_exmode(FALSE);
3405}
3406
3407/*
3408 * Handle a ":" command.
3409 */
3410 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003411nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003413 int old_p_im;
3414 int cmd_result;
3415 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
Bram Moolenaar957cf672020-11-12 14:21:06 +01003417 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 nv_operator(cap);
3419 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 {
3421 if (cap->oap->op_type != OP_NOP)
3422 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003423 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 cap->oap->motion_type = MCHAR;
3425 cap->oap->inclusive = FALSE;
3426 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003427 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003429 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 stuffcharReadbuff('.');
3431 if (cap->count0 > 1)
3432 {
3433 stuffReadbuff((char_u *)",.+");
3434 stuffnumReadbuff((long)cap->count0 - 1L);
3435 }
3436 }
3437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003438 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (KeyTyped)
3440 compute_cmdrow();
3441
3442 old_p_im = p_im;
3443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003445 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (p_im != old_p_im)
3450 {
3451 if (p_im)
3452 restart_edit = 'i';
3453 else
3454 restart_edit = 0;
3455 }
3456
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003457 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003459 clearop(cap->oap);
3460 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3462 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003463 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3464 || did_emsg
3465 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003466 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 clearopbeep(cap->oap);
3468 }
3469}
3470
3471/*
3472 * Handle CTRL-G command.
3473 */
3474 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003475nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003477 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
3479 VIsual_select = !VIsual_select;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003480 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 showmode();
3482 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003483 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003484 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 fileinfo((int)cap->count0, FALSE, TRUE);
3486}
3487
3488/*
3489 * Handle CTRL-H <Backspace> command.
3490 */
3491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003492nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (VIsual_active && VIsual_select)
3495 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003496 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 v_visop(cap);
3498 }
3499 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 nv_left(cap);
3501}
3502
3503/*
3504 * CTRL-L: clear screen and redraw.
3505 */
3506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003507nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
3509 if (!checkclearop(cap->oap))
3510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003512 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003513 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003514# ifdef FEAT_RELTIME
3515 {
3516 win_T *wp;
3517
3518 FOR_ALL_WINDOWS(wp)
3519 wp->w_s->b_syn_slow = FALSE;
3520 }
3521# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#endif
3523 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003524#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3525# ifdef VIMDLL
3526 if (!gui.in_use)
3527# endif
3528 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531}
3532
3533/*
3534 * CTRL-O: In Select mode: switch to Visual mode for one command.
3535 * Otherwise: Go to older pcmark.
3536 */
3537 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003538nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (VIsual_active && VIsual_select)
3541 {
3542 VIsual_select = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003543 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003545 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 cap->count1 = -cap->count1;
3550 nv_pcmark(cap);
3551 }
3552}
3553
3554/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003555 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3556 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 */
3558 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003559nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 if (!checkclearopq(cap->oap))
3562 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3563 GETF_SETMARK|GETF_ALT, FALSE);
3564}
3565
3566/*
3567 * "Z" commands.
3568 */
3569 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003570nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
3572 if (!checkclearopq(cap->oap))
3573 {
3574 switch (cap->nchar)
3575 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003576 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 case 'Z': do_cmdline_cmd((char_u *)"x");
3578 break;
3579
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003580 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 case 'Q': do_cmdline_cmd((char_u *)"q!");
3582 break;
3583
3584 default: clearopbeep(cap->oap);
3585 }
3586 }
3587}
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589/*
3590 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3591 */
3592 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003593do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594{
3595 oparg_T oa;
3596 cmdarg_T ca;
3597
3598 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003599 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 ca.oap = &oa;
3601 ca.cmdchar = c1;
3602 ca.nchar = c2;
3603 nv_ident(&ca);
3604}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
3606/*
3607 * Handle the commands that use the word under the cursor.
3608 * [g] CTRL-] :ta to current identifier
3609 * [g] 'K' run program for current identifier
3610 * [g] '*' / to current identifier or string
3611 * [g] '#' ? to current identifier or string
3612 * g ']' :tselect for current identifier
3613 */
3614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003615nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
3617 char_u *ptr = NULL;
3618 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003619 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003620 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003622 char_u *kp; // value of 'keywordprg'
3623 int kp_help; // 'keywordprg' is ":he"
3624 int kp_ex; // 'keywordprg' starts with ":"
3625 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003627 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003628 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 char_u *aux_ptr;
3630 int isman;
3631 int isman_s;
3632
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003633 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 cmdchar = cap->nchar;
3636 g_cmd = TRUE;
3637 }
3638 else
3639 {
3640 cmdchar = cap->cmdchar;
3641 g_cmd = FALSE;
3642 }
3643
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003644 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 cmdchar = '#';
3646
3647 /*
3648 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3649 */
3650 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3653 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 if (checkclearopq(cap->oap))
3655 return;
3656 }
3657
3658 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3659 (cmdchar == '*' || cmdchar == '#')
3660 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3661 {
3662 clearop(cap->oap);
3663 return;
3664 }
3665
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003666 // Allocate buffer to put the command in. Inserting backslashes can
3667 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3668 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3670 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3671 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003672 if (kp_help && *skipwhite(ptr) == NUL)
3673 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003674 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003675 return;
3676 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003677 kp_ex = (*kp == ':');
3678 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3679 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (buf == NULL)
3681 return;
3682 buf[0] = NUL;
3683
3684 switch (cmdchar)
3685 {
3686 case '*':
3687 case '#':
3688 /*
3689 * Put cursor at start of word, makes search skip the word
3690 * under the cursor.
3691 * Call setpcmark() first, so "*``" puts the cursor back where
3692 * it was.
3693 */
3694 setpcmark();
3695 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3696
3697 if (!g_cmd && vim_iswordp(ptr))
3698 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003699 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 break;
3701
3702 case 'K':
3703 if (kp_help)
3704 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003705 else if (kp_ex)
3706 {
3707 if (cap->count0 != 0)
3708 vim_snprintf((char *)buf, buflen, "%s %ld",
3709 kp, cap->count0);
3710 else
3711 STRCPY(buf, kp);
3712 STRCAT(buf, " ");
3713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 else
3715 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003716 // An external command will probably use an argument starting
3717 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003718 while (*ptr == '-' && n > 0)
3719 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003720 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003721 --n;
3722 }
3723 if (n == 0)
3724 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003725 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003726 vim_free(buf);
3727 return;
3728 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003729
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003730 // When a count is given, turn it into a range. Is this
3731 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 isman = (STRCMP(kp, "man") == 0);
3733 isman_s = (STRCMP(kp, "man -s") == 0);
3734 if (cap->count0 != 0 && !(isman || isman_s))
3735 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3736
3737 STRCAT(buf, "! ");
3738 if (cap->count0 == 0 && isman_s)
3739 STRCAT(buf, "man");
3740 else
3741 STRCAT(buf, kp);
3742 STRCAT(buf, " ");
3743 if (cap->count0 != 0 && (isman || isman_s))
3744 {
3745 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3746 STRCAT(buf, " ");
3747 }
3748 }
3749 break;
3750
3751 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003752 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753#ifdef FEAT_CSCOPE
3754 if (p_cst)
3755 STRCPY(buf, "cstag ");
3756 else
3757#endif
3758 STRCPY(buf, "ts ");
3759 break;
3760
3761 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003762 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (curbuf->b_help)
3764 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003766 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003767 if (g_cmd)
3768 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003769 else if (cap->count0 == 0)
3770 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003771 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003772 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775
3776 /*
3777 * Now grab the chars in the identifier
3778 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003779 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003781 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003782 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003783 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003784 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003785 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003786 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003787 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003788 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003789 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003791 vim_free(buf);
3792 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003794 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003795 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003796 {
3797 vim_free(buf);
3798 vim_free(p);
3799 return;
3800 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003801 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003802 STRCAT(buf, p);
3803 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003805 else
3806 {
3807 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003808 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003809 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003810 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003811 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003812 {
3813 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003814 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003815 aux_ptr = (char_u *)"";
3816 else
3817 aux_ptr = (char_u *)"\\|\"\n[";
3818 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003819 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003820 aux_ptr = (char_u *)"\\|\"\n*?[";
3821
3822 p = buf + STRLEN(buf);
3823 while (n-- > 0)
3824 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003825 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003826 if (vim_strchr(aux_ptr, *ptr) != NULL)
3827 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003828 // When current byte is a part of multibyte character, copy all
3829 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003830 if (has_mbyte)
3831 {
3832 int i;
3833 int len = (*mb_ptr2len)(ptr) - 1;
3834
3835 for (i = 0; i < len && n >= 1; ++i, --n)
3836 *p++ = *ptr++;
3837 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003838 *p++ = *ptr++;
3839 }
3840 *p = NUL;
3841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 /*
3844 * Execute the command.
3845 */
3846 if (cmdchar == '*' || cmdchar == '#')
3847 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003848 if (!g_cmd && (has_mbyte
3849 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3850 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003852
3853 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003854 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003856
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003857 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003860 {
3861 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003863 g_tag_at_cursor = FALSE;
3864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 vim_free(buf);
3867}
3868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869/*
3870 * Get visually selected text, within one line only.
3871 * Returns FAIL if more than one line selected.
3872 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003873 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003874get_visual_text(
3875 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003876 char_u **pp, // return: start of selected text
3877 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878{
3879 if (VIsual_mode != 'V')
3880 unadjust_for_sel();
3881 if (VIsual.lnum != curwin->w_cursor.lnum)
3882 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003883 if (cap != NULL)
3884 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 return FAIL;
3886 }
3887 if (VIsual_mode == 'V')
3888 {
3889 *pp = ml_get_curline();
3890 *lenp = (int)STRLEN(*pp);
3891 }
3892 else
3893 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003894 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
3896 *pp = ml_get_pos(&curwin->w_cursor);
3897 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3898 }
3899 else
3900 {
3901 *pp = ml_get_pos(&VIsual);
3902 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3903 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003904 if (**pp == NUL)
3905 *lenp = 0;
3906 if (has_mbyte && *lenp > 0)
3907 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003908 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910 reset_VIsual_and_resel();
3911 return OK;
3912}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914/*
3915 * CTRL-T: backwards in tag stack
3916 */
3917 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003918nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
3920 if (!checkclearopq(cap->oap))
3921 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3922}
3923
3924/*
3925 * Handle scrolling command 'H', 'L' and 'M'.
3926 */
3927 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003928nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930 int used = 0;
3931 long n;
3932#ifdef FEAT_FOLDING
3933 linenr_T lnum;
3934#endif
3935 int half;
3936
3937 cap->oap->motion_type = MLINE;
3938 setpcmark();
3939
3940 if (cap->cmdchar == 'L')
3941 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003942 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 curwin->w_cursor.lnum = curwin->w_botline - 1;
3944 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3945 curwin->w_cursor.lnum = 1;
3946 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003947 {
3948#ifdef FEAT_FOLDING
3949 if (hasAnyFolding(curwin))
3950 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003951 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003952 for (n = cap->count1 - 1; n > 0
3953 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3954 {
3955 (void)hasFolding(curwin->w_cursor.lnum,
3956 &curwin->w_cursor.lnum, NULL);
3957 --curwin->w_cursor.lnum;
3958 }
3959 }
3960 else
3961#endif
3962 curwin->w_cursor.lnum -= cap->count1 - 1;
3963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 }
3965 else
3966 {
3967 if (cap->cmdchar == 'M')
3968 {
3969#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003970 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 used -= diff_check_fill(curwin, curwin->w_topline)
3972 - curwin->w_topfill;
3973#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003974 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3976 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3977 {
3978#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003979 // Count half he number of filler lines to be "below this
3980 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3982 + n) / 2 >= half)
3983 {
3984 --n;
3985 break;
3986 }
3987#endif
3988 used += plines(curwin->w_topline + n);
3989 if (used >= half)
3990 break;
3991#ifdef FEAT_FOLDING
3992 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3993 n = lnum - curwin->w_topline;
3994#endif
3995 }
3996 if (n > 0 && used > curwin->w_height)
3997 --n;
3998 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003999 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004002#ifdef FEAT_FOLDING
4003 if (hasAnyFolding(curwin))
4004 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004005 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004006 lnum = curwin->w_topline;
4007 while (n-- > 0 && lnum < curwin->w_botline - 1)
4008 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02004009 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004010 ++lnum;
4011 }
4012 n = lnum - curwin->w_topline;
4013 }
4014#endif
4015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 curwin->w_cursor.lnum = curwin->w_topline + n;
4017 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4018 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4019 }
4020
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004021 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02004022 if (cap->oap->op_type == OP_NOP)
4023 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 beginline(BL_SOL | BL_FIX);
4025}
4026
4027/*
4028 * Cursor right commands.
4029 */
4030 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004031nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032{
4033 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004034 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004036 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4037 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004038 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004039 if (mod_mask & MOD_MASK_CTRL)
4040 cap->arg = TRUE;
4041 nv_wordcmd(cap);
4042 return;
4043 }
4044
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 cap->oap->motion_type = MCHAR;
4046 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004047 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004050 * In virtual edit mode, there's no such thing as "past_line", as lines
4051 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 */
4053 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004054 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056 for (n = cap->count1; n > 0; --n)
4057 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004058 if ((!past_line && oneright() == FAIL)
4059 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004060 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
4062 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004063 * <Space> wraps to next line if 'whichwrap' has 's'.
4064 * 'l' wraps to next line if 'whichwrap' has 'l'.
4065 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 */
4067 if ( ((cap->cmdchar == ' '
4068 && vim_strchr(p_ww, 's') != NULL)
4069 || (cap->cmdchar == 'l'
4070 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004071 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 && vim_strchr(p_ww, '>') != NULL))
4073 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4074 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004075 // When deleting we also count the NL as a character.
4076 // Set cap->oap->inclusive when last char in the line is
4077 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004078 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004080 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 cap->oap->inclusive = TRUE;
4082 else
4083 {
4084 ++curwin->w_cursor.lnum;
4085 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 curwin->w_set_curswant = TRUE;
4088 cap->oap->inclusive = FALSE;
4089 }
4090 continue;
4091 }
4092 if (cap->oap->op_type == OP_NOP)
4093 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004094 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (n == cap->count1)
4096 beep_flush();
4097 }
4098 else
4099 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004100 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 cap->oap->inclusive = TRUE;
4102 }
4103 break;
4104 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004105 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
4107 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (virtual_active())
4109 oneright();
4110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004113 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 ++curwin->w_cursor.col;
4116 }
4117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
4119#ifdef FEAT_FOLDING
4120 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4121 && cap->oap->op_type == OP_NOP)
4122 foldOpenCursor();
4123#endif
4124}
4125
4126/*
4127 * Cursor left commands.
4128 *
4129 * Returns TRUE when operator end should not be adjusted.
4130 */
4131 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004132nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133{
4134 long n;
4135
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004136 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4137 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004138 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004139 if (mod_mask & MOD_MASK_CTRL)
4140 cap->arg = 1;
4141 nv_bck_word(cap);
4142 return;
4143 }
4144
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 cap->oap->motion_type = MCHAR;
4146 cap->oap->inclusive = FALSE;
4147 for (n = cap->count1; n > 0; --n)
4148 {
4149 if (oneleft() == FAIL)
4150 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004151 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4152 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4153 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 if ( (((cap->cmdchar == K_BS
4155 || cap->cmdchar == Ctrl_H)
4156 && vim_strchr(p_ww, 'b') != NULL)
4157 || (cap->cmdchar == 'h'
4158 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004159 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 && vim_strchr(p_ww, '<') != NULL))
4161 && curwin->w_cursor.lnum > 1)
4162 {
4163 --(curwin->w_cursor.lnum);
4164 coladvance((colnr_T)MAXCOL);
4165 curwin->w_set_curswant = TRUE;
4166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004167 // When the NL before the first char has to be deleted we
4168 // put the cursor on the NUL after the previous line.
4169 // This is a very special case, be careful!
4170 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if ( (cap->oap->op_type == OP_DELETE
4172 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004173 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004175 char_u *cp = ml_get_cursor();
4176
4177 if (*cp != NUL)
4178 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004179 if (has_mbyte)
4180 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4181 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004182 ++curwin->w_cursor.col;
4183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 cap->retval |= CA_NO_ADJ_OP_END;
4185 }
4186 continue;
4187 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004188 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4190 beep_flush();
4191 break;
4192 }
4193 }
4194#ifdef FEAT_FOLDING
4195 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4196 && cap->oap->op_type == OP_NOP)
4197 foldOpenCursor();
4198#endif
4199}
4200
4201/*
4202 * Cursor up commands.
4203 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4204 */
4205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004206nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004208 if (mod_mask & MOD_MASK_SHIFT)
4209 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004210 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004211 cap->arg = BACKWARD;
4212 nv_page(cap);
4213 }
4214 else
4215 {
4216 cap->oap->motion_type = MLINE;
4217 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4218 clearopbeep(cap->oap);
4219 else if (cap->arg)
4220 beginline(BL_WHITE | BL_FIX);
4221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222}
4223
4224/*
4225 * Cursor down commands.
4226 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4227 */
4228 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004229nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004231 if (mod_mask & MOD_MASK_SHIFT)
4232 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004233 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004234 cap->arg = FORWARD;
4235 nv_page(cap);
4236 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004237#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004238 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004239 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4240 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
4244#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004245 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004246 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 cmdwin_result = CAR;
4248 else
4249#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004250#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004251 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004252 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4253 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4254 {
4255 invoke_prompt_callback();
4256 if (restart_edit == 0)
4257 restart_edit = 'a';
4258 }
4259 else
4260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 {
4262 cap->oap->motion_type = MLINE;
4263 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4264 clearopbeep(cap->oap);
4265 else if (cap->arg)
4266 beginline(BL_WHITE | BL_FIX);
4267 }
4268 }
4269}
4270
4271#ifdef FEAT_SEARCHPATH
4272/*
4273 * Grab the file name under the cursor and edit it.
4274 */
4275 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004276nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
4278 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004279 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004281 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
4283 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004284 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return;
4286 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004287 if (curbuf_locked())
4288 {
4289 clearop(cap->oap);
4290 return;
4291 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004292#ifdef FEAT_PROP_POPUP
4293 if (ERROR_IF_TERM_POPUP_WINDOW)
4294 return;
4295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004297 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 if (ptr != NULL)
4300 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004301 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004302 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004303 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004305 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004306 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004307 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004308 {
4309 curwin->w_cursor.lnum = lnum;
4310 check_cursor_lnum();
4311 beginline(BL_SOL | BL_FIX);
4312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 vim_free(ptr);
4314 }
4315 else
4316 clearop(cap->oap);
4317}
4318#endif
4319
4320/*
4321 * <End> command: to end of current line or last line.
4322 */
4323 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004324nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004326 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004328 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004330 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 nv_dollar(cap);
4333}
4334
4335/*
4336 * Handle the "$" command.
4337 */
4338 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004339nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 cap->oap->motion_type = MCHAR;
4342 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004343 // In virtual mode when off the edge of a line and an operator
4344 // is pending (whew!) keep the cursor where it is.
4345 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (!virtual_active() || gchar_cursor() != NUL
4347 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004348 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 if (cursor_down((long)(cap->count1 - 1),
4350 cap->oap->op_type == OP_NOP) == FAIL)
4351 clearopbeep(cap->oap);
4352#ifdef FEAT_FOLDING
4353 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4354 foldOpenCursor();
4355#endif
4356}
4357
4358/*
4359 * Implementation of '?' and '/' commands.
4360 * If cap->arg is TRUE don't set PC mark.
4361 */
4362 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004363nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
4365 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004366 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4369 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004370 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 cap->cmdchar = 'g';
4372 cap->nchar = '?';
4373 nv_operator(cap);
4374 return;
4375 }
4376
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004377 // When using 'incsearch' the cursor may be moved to set a different search
4378 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004379 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381 if (cap->searchbuf == NULL)
4382 {
4383 clearop(oap);
4384 return;
4385 }
4386
Bram Moolenaar46539112015-02-17 15:43:57 +01004387 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004388 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004389 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390}
4391
4392/*
4393 * Handle "N" and "n" commands.
4394 * cap->arg is SEARCH_REV for "N", 0 for "n".
4395 */
4396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004397nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004399 pos_T old = curwin->w_cursor;
4400 int wrapped = FALSE;
4401 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004402
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004403 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004404 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004405 // Avoid getting stuck on the current cursor position, which can
4406 // happen when an offset is given and the cursor is on the last char
4407 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004408 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004409 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004410 cap->count1 -= 1;
4411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412}
4413
4414/*
4415 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4416 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004417 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004419 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004420normal_search(
4421 cmdarg_T *cap,
4422 int dir,
4423 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004424 int opt, // extra flags for do_search()
4425 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426{
4427 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004428 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
4430 cap->oap->motion_type = MCHAR;
4431 cap->oap->inclusive = FALSE;
4432 cap->oap->use_reg_one = TRUE;
4433 curwin->w_set_curswant = TRUE;
4434
Bram Moolenaara80faa82020-04-12 19:37:17 +02004435 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004436 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004437 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4438 if (wrapped != NULL)
4439 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (i == 0)
4441 clearop(cap->oap);
4442 else
4443 {
4444 if (i == 2)
4445 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447#ifdef FEAT_FOLDING
4448 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4449 foldOpenCursor();
4450#endif
4451 }
4452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004453 // "/$" will put the cursor after the end of the line, may need to
4454 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004456 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457}
4458
4459/*
4460 * Character search commands.
4461 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4462 * ',' and FALSE for ';'.
4463 * cap->nchar is NUL for ',' and ';' (repeat the search)
4464 */
4465 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004466nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467{
4468 int t_cmd;
4469
4470 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4471 t_cmd = TRUE;
4472 else
4473 t_cmd = FALSE;
4474
4475 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4477 clearopbeep(cap->oap);
4478 else
4479 {
4480 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004481 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4483 && (t_cmd || cap->oap->op_type != OP_NOP))
4484 {
4485 colnr_T scol, ecol;
4486
4487 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4488 curwin->w_cursor.coladd = ecol - scol;
4489 }
4490 else
4491 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493#ifdef FEAT_FOLDING
4494 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4495 foldOpenCursor();
4496#endif
4497 }
4498}
4499
4500/*
4501 * "[" and "]" commands.
4502 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4503 */
4504 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004505nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004507 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004509 pos_T *pos = NULL; // init for GCC
4510 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 int flag;
4512 long n;
4513 int findc;
4514 int c;
4515
4516 cap->oap->motion_type = MCHAR;
4517 cap->oap->inclusive = FALSE;
4518 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004519 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
4521#ifdef FEAT_SEARCHPATH
4522 /*
4523 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4524 */
4525 if (cap->nchar == 'f')
4526 nv_gotofile(cap);
4527 else
4528#endif
4529
4530#ifdef FEAT_FIND_ID
4531 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004532 * Find the occurrence(s) of the identifier or define under cursor
4533 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 *
4535 * search list jump
4536 * fwd bwd fwd bwd fwd bwd
4537 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4538 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4539 */
4540 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004541# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004543# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004545# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 cap->nchar) != NULL)
4547 {
4548 char_u *ptr;
4549 int len;
4550
4551 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4552 clearop(cap->oap);
4553 else
4554 {
4555 find_pattern_in_path(ptr, 0, len, TRUE,
4556 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4557 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4558 cap->count1,
4559 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4560 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4561 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4562 (linenr_T)MAXLNUM);
4563 curwin->w_set_curswant = TRUE;
4564 }
4565 }
4566 else
4567#endif
4568
4569 /*
4570 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4571 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4572 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4573 * "[m" or "]m" search for prev/next start of (Java) method.
4574 * "[M" or "]M" search for prev/next end of (Java) method.
4575 */
4576 if ( (cap->cmdchar == '['
4577 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4578 || (cap->cmdchar == ']'
4579 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4580 {
4581 if (cap->nchar == '*')
4582 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 prev_pos.lnum = 0;
4584 if (cap->nchar == 'm' || cap->nchar == 'M')
4585 {
4586 if (cap->cmdchar == '[')
4587 findc = '{';
4588 else
4589 findc = '}';
4590 n = 9999;
4591 }
4592 else
4593 {
4594 findc = cap->nchar;
4595 n = cap->count1;
4596 }
4597 for ( ; n > 0; --n)
4598 {
4599 if ((pos = findmatchlimit(cap->oap, findc,
4600 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4601 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004602 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
4604 if (cap->nchar != 'm' && cap->nchar != 'M')
4605 clearopbeep(cap->oap);
4606 }
4607 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004608 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 break;
4610 }
4611 prev_pos = new_pos;
4612 curwin->w_cursor = *pos;
4613 new_pos = *pos;
4614 }
4615 curwin->w_cursor = old_pos;
4616
4617 /*
4618 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4619 * brought us to the match for "[m" and "]M" when inside a method.
4620 * Try finding the '{' or '}' we want to be at.
4621 * Also repeat for the given count.
4622 */
4623 if (cap->nchar == 'm' || cap->nchar == 'M')
4624 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004625 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 int norm = ((findc == '{') == (cap->nchar == 'm'));
4627
4628 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004629 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (prev_pos.lnum != 0)
4631 {
4632 pos = &prev_pos;
4633 curwin->w_cursor = prev_pos;
4634 if (norm)
4635 --n;
4636 }
4637 else
4638 pos = NULL;
4639 while (n > 0)
4640 {
4641 for (;;)
4642 {
4643 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4644 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004645 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if (pos == NULL)
4647 clearopbeep(cap->oap);
4648 n = 0;
4649 break;
4650 }
4651 c = gchar_cursor();
4652 if (c == '{' || c == '}')
4653 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004654 // Must have found end/start of class: use it.
4655 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if ((c == findc && norm) || (n == 1 && !norm))
4657 {
4658 new_pos = curwin->w_cursor;
4659 pos = &new_pos;
4660 n = 0;
4661 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004662 // if no match found at all, we started outside of the
4663 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 else if (new_pos.lnum == 0)
4665 {
4666 new_pos = curwin->w_cursor;
4667 pos = &new_pos;
4668 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004669 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 else if ((pos = findmatchlimit(cap->oap, findc,
4671 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4672 0)) == NULL)
4673 n = 0;
4674 else
4675 curwin->w_cursor = *pos;
4676 break;
4677 }
4678 }
4679 --n;
4680 }
4681 curwin->w_cursor = old_pos;
4682 if (pos == NULL && new_pos.lnum != 0)
4683 clearopbeep(cap->oap);
4684 }
4685 if (pos != NULL)
4686 {
4687 setpcmark();
4688 curwin->w_cursor = *pos;
4689 curwin->w_set_curswant = TRUE;
4690#ifdef FEAT_FOLDING
4691 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4692 && cap->oap->op_type == OP_NOP)
4693 foldOpenCursor();
4694#endif
4695 }
4696 }
4697
4698 /*
4699 * "[[", "[]", "]]" and "][": move to start or end of function
4700 */
4701 else if (cap->nchar == '[' || cap->nchar == ']')
4702 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004703 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 flag = '{';
4705 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004706 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
4708 curwin->w_set_curswant = TRUE;
4709 /*
4710 * Imitate strange Vi behaviour: When using "]]" with an operator
4711 * we also stop at '}'.
4712 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004713 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 (cap->oap->op_type != OP_NOP
4715 && cap->arg == FORWARD && flag == '{')))
4716 clearopbeep(cap->oap);
4717 else
4718 {
4719 if (cap->oap->op_type == OP_NOP)
4720 beginline(BL_WHITE | BL_FIX);
4721#ifdef FEAT_FOLDING
4722 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4723 foldOpenCursor();
4724#endif
4725 }
4726 }
4727
4728 /*
4729 * "[p", "[P", "]P" and "]p": put with indent adjustment
4730 */
4731 else if (cap->nchar == 'p' || cap->nchar == 'P')
4732 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004733 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735
4736 /*
4737 * "['", "[`", "]'" and "]`": jump to next mark
4738 */
4739 else if (cap->nchar == '\'' || cap->nchar == '`')
4740 {
4741 pos = &curwin->w_cursor;
4742 for (n = cap->count1; n > 0; --n)
4743 {
4744 prev_pos = *pos;
4745 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4746 cap->nchar == '\'');
4747 if (pos == NULL)
4748 break;
4749 }
4750 if (pos == NULL)
4751 pos = &prev_pos;
4752 nv_cursormark(cap, cap->nchar == '\'', pos);
4753 }
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 /*
4756 * [ or ] followed by a middle mouse click: put selected text with
4757 * indent adjustment. Any other button just does as usual.
4758 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004759 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
4761 (void)do_mouse(cap->oap, cap->nchar,
4762 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4763 cap->count1, PUT_FIXINDENT);
4764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766#ifdef FEAT_FOLDING
4767 /*
4768 * "[z" and "]z": move to start or end of open fold.
4769 */
4770 else if (cap->nchar == 'z')
4771 {
4772 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4773 cap->count1) == FAIL)
4774 clearopbeep(cap->oap);
4775 }
4776#endif
4777
4778#ifdef FEAT_DIFF
4779 /*
4780 * "[c" and "]c": move to next or previous diff-change.
4781 */
4782 else if (cap->nchar == 'c')
4783 {
4784 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4785 cap->count1) == FAIL)
4786 clearopbeep(cap->oap);
4787 }
4788#endif
4789
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004790#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004791 /*
4792 * "[s", "[S", "]s" and "]S": move to next spell error.
4793 */
4794 else if (cap->nchar == 's' || cap->nchar == 'S')
4795 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004796 setpcmark();
4797 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004798 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4799 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004800 {
4801 clearopbeep(cap->oap);
4802 break;
4803 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004804 else
4805 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004806# ifdef FEAT_FOLDING
4807 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4808 foldOpenCursor();
4809# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004810 }
4811#endif
4812
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004813 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 else
4815 clearopbeep(cap->oap);
4816}
4817
4818/*
4819 * Handle Normal mode "%" command.
4820 */
4821 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004822nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823{
4824 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004825#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 linenr_T lnum = curwin->w_cursor.lnum;
4827#endif
4828
4829 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004830 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
4832 if (cap->count0 > 100)
4833 clearopbeep(cap->oap);
4834 else
4835 {
4836 cap->oap->motion_type = MLINE;
4837 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004838 // Round up, so 'normal 100%' always jumps at the line line.
4839 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4840 // overflow on 32-bits, so use a formula with less accuracy
4841 // to avoid overflows.
4842 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4844 / 100L * cap->count0;
4845 else
4846 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4847 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004848 if (curwin->w_cursor.lnum < 1)
4849 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4851 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4852 beginline(BL_SOL | BL_FIX);
4853 }
4854 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004855 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
4857 cap->oap->motion_type = MCHAR;
4858 cap->oap->use_reg_one = TRUE;
4859 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4860 clearopbeep(cap->oap);
4861 else
4862 {
4863 setpcmark();
4864 curwin->w_cursor = *pos;
4865 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
4869 }
4870#ifdef FEAT_FOLDING
4871 if (cap->oap->op_type == OP_NOP
4872 && lnum != curwin->w_cursor.lnum
4873 && (fdo_flags & FDO_PERCENT)
4874 && KeyTyped)
4875 foldOpenCursor();
4876#endif
4877}
4878
4879/*
4880 * Handle "(" and ")" commands.
4881 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4882 */
4883 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004884nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
4886 cap->oap->motion_type = MCHAR;
4887 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004888 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004889 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 curwin->w_set_curswant = TRUE;
4891
4892 if (findsent(cap->arg, cap->count1) == FAIL)
4893 clearopbeep(cap->oap);
4894 else
4895 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004896 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004897 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#ifdef FEAT_FOLDING
4900 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4901 foldOpenCursor();
4902#endif
4903 }
4904}
4905
4906/*
4907 * "m" command: Mark a position.
4908 */
4909 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004910nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911{
4912 if (!checkclearop(cap->oap))
4913 {
4914 if (setmark(cap->nchar) == FAIL)
4915 clearopbeep(cap->oap);
4916 }
4917}
4918
4919/*
4920 * "{" and "}" commands.
4921 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4922 */
4923 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004924nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 cap->oap->motion_type = MCHAR;
4927 cap->oap->inclusive = FALSE;
4928 cap->oap->use_reg_one = TRUE;
4929 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004930 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 clearopbeep(cap->oap);
4932 else
4933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935#ifdef FEAT_FOLDING
4936 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4937 foldOpenCursor();
4938#endif
4939 }
4940}
4941
4942/*
4943 * "u" command: Undo or make lower case.
4944 */
4945 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004946nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004948 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004950 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 cap->cmdchar = 'g';
4952 cap->nchar = 'u';
4953 nv_operator(cap);
4954 }
4955 else
4956 nv_kundo(cap);
4957}
4958
4959/*
4960 * <Undo> command.
4961 */
4962 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004963nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
4965 if (!checkclearopq(cap->oap))
4966 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004967#ifdef FEAT_JOB_CHANNEL
4968 if (bt_prompt(curbuf))
4969 {
4970 clearopbeep(cap->oap);
4971 return;
4972 }
4973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 u_undo((int)cap->count1);
4975 curwin->w_set_curswant = TRUE;
4976 }
4977}
4978
4979/*
4980 * Handle the "r" command.
4981 */
4982 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004983nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
4985 char_u *ptr;
4986 int had_ctrl_v;
4987 long n;
4988
4989 if (checkclearop(cap->oap))
4990 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004991#ifdef FEAT_JOB_CHANNEL
4992 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4993 {
4994 clearopbeep(cap->oap);
4995 return;
4996 }
4997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004999 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 if (cap->nchar == Ctrl_V)
5001 {
5002 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01005003 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005004 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 if (cap->nchar > DEL)
5006 had_ctrl_v = NUL;
5007 }
5008 else
5009 had_ctrl_v = NUL;
5010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005011 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005012 if (IS_SPECIAL(cap->nchar))
5013 {
5014 clearopbeep(cap->oap);
5015 return;
5016 }
5017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005018 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (VIsual_active)
5020 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00005021 if (got_int)
5022 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01005023 if (had_ctrl_v)
5024 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005025 // Use a special (negative) number to make a difference between a
5026 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01005027 if (cap->nchar == CAR)
5028 cap->nchar = REPLACE_CR_NCHAR;
5029 else if (cap->nchar == NL)
5030 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01005031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 nv_operator(cap);
5033 return;
5034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005036 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (virtual_active())
5038 {
5039 if (u_save_cursor() == FAIL)
5040 return;
5041 if (gchar_cursor() == NUL)
5042 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005043 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 coladvance_force((colnr_T)(getviscol() + cap->count1));
5045 curwin->w_cursor.col -= cap->count1;
5046 }
5047 else if (gchar_cursor() == TAB)
5048 coladvance_force(getviscol());
5049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005051 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005053 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005054 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 clearopbeep(cap->oap);
5057 return;
5058 }
5059
5060 /*
5061 * Replacing with a TAB is done by edit() when it is complicated because
5062 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5063 * Other characters are done below to avoid problems with things like
5064 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5065 */
5066 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5067 {
5068 stuffnumReadbuff(cap->count1);
5069 stuffcharReadbuff('R');
5070 stuffcharReadbuff('\t');
5071 stuffcharReadbuff(ESC);
5072 return;
5073 }
5074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005075 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (u_save_cursor() == FAIL)
5077 return;
5078
5079 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5080 {
5081 /*
5082 * Replace character(s) by a single newline.
5083 * Strange vi behaviour: Only one newline is inserted.
5084 * Delete the characters here.
5085 * Insert the newline with an insert command, takes care of
5086 * autoindent. The insert command depends on being on the last
5087 * character of a line or not.
5088 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005089 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 stuffcharReadbuff('\r');
5091 stuffcharReadbuff(ESC);
5092
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005093 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 invoke_edit(cap, TRUE, 'r', FALSE);
5095 }
5096 else
5097 {
5098 prep_redo(cap->oap->regname, cap->count1,
5099 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5100
5101 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (has_mbyte)
5103 {
5104 int old_State = State;
5105
5106 if (cap->ncharC1 != 0)
5107 AppendCharToRedobuff(cap->ncharC1);
5108 if (cap->ncharC2 != 0)
5109 AppendCharToRedobuff(cap->ncharC2);
5110
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005111 // This is slow, but it handles replacing a single-byte with a
5112 // multi-byte and the other way around. Also handles adding
5113 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 for (n = cap->count1; n > 0; --n)
5115 {
5116 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005117 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5118 {
5119 int c = ins_copychar(curwin->w_cursor.lnum
5120 + (cap->nchar == Ctrl_Y ? -1 : 1));
5121 if (c != NUL)
5122 ins_char(c);
5123 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005124 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005125 ++curwin->w_cursor.col;
5126 }
5127 else
5128 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 State = old_State;
5130 if (cap->ncharC1 != 0)
5131 ins_char(cap->ncharC1);
5132 if (cap->ncharC2 != 0)
5133 ins_char(cap->ncharC2);
5134 }
5135 }
5136 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 /*
5139 * Replace the characters within one line.
5140 */
5141 for (n = cap->count1; n > 0; --n)
5142 {
5143 /*
5144 * Get ptr again, because u_save and/or showmatch() will have
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005145 * released the line. This may also happen in ins_copychar().
5146 * At the same time we let know that the line will be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005148 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5149 {
5150 int c = ins_copychar(curwin->w_cursor.lnum
5151 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005152
5153 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005154 if (c != NUL)
5155 ptr[curwin->w_cursor.col] = c;
5156 }
5157 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005158 {
5159 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005160 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 if (p_sm && msg_silent == 0)
5163 showmatch(cap->nchar);
5164 ++curwin->w_cursor.col;
5165 }
5166#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005167 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005169 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
Bram Moolenaar009b2592004-10-24 19:18:58 +00005171 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005172 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005174 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176#endif
5177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005178 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 changed_bytes(curwin->w_cursor.lnum,
5180 (colnr_T)(curwin->w_cursor.col - cap->count1));
5181 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005182 --curwin->w_cursor.col; // cursor on the last replaced char
5183 // if the character on the left of the current cursor is a multi-byte
5184 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (has_mbyte)
5186 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 curbuf->b_op_end = curwin->w_cursor;
5188 curwin->w_set_curswant = TRUE;
5189 set_last_insert(cap->nchar);
5190 }
5191}
5192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193/*
5194 * 'o': Exchange start and end of Visual area.
5195 * 'O': same, but in block mode exchange left and right corners.
5196 */
5197 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005198v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 pos_T old_cursor;
5201 colnr_T left, right;
5202
5203 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5204 {
5205 old_cursor = curwin->w_cursor;
5206 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5207 curwin->w_cursor.lnum = VIsual.lnum;
5208 coladvance(left);
5209 VIsual = curwin->w_cursor;
5210
5211 curwin->w_cursor.lnum = old_cursor.lnum;
5212 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005213 // 'selection "exclusive" and cursor at right-bottom corner: move it
5214 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5216 ++curwin->w_curswant;
5217 coladvance(curwin->w_curswant);
5218 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005220 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
5222 curwin->w_cursor.lnum = VIsual.lnum;
5223 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5224 ++right;
5225 coladvance(right);
5226 VIsual = curwin->w_cursor;
5227
5228 curwin->w_cursor.lnum = old_cursor.lnum;
5229 coladvance(left);
5230 curwin->w_curswant = left;
5231 }
5232 }
5233 else
5234 {
5235 old_cursor = curwin->w_cursor;
5236 curwin->w_cursor = VIsual;
5237 VIsual = old_cursor;
5238 curwin->w_set_curswant = TRUE;
5239 }
5240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
5242/*
5243 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5244 */
5245 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005246nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005248 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
5250 cap->cmdchar = 'c';
5251 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005252 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 VIsual_mode = 'V';
5254 nv_operator(cap);
5255 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005256 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005259 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 else
5261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 if (virtual_active())
5263 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5265 }
5266 }
5267}
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269/*
5270 * "gr".
5271 */
5272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005273nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if (VIsual_active)
5276 {
5277 cap->cmdchar = 'r';
5278 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005279 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005281 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
5283 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005284 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 else
5286 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005287 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005288 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 stuffcharReadbuff(cap->extra_char);
5290 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 if (virtual_active())
5292 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 invoke_edit(cap, TRUE, 'v', FALSE);
5294 }
5295 }
5296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
5298/*
5299 * Swap case for "~" command, when it does not work like an operator.
5300 */
5301 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005302n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
5304 long n;
5305 pos_T startpos;
5306 int did_change = 0;
5307#ifdef FEAT_NETBEANS_INTG
5308 pos_T pos;
5309 char_u *ptr;
5310 int count;
5311#endif
5312
5313 if (checkclearopq(cap->oap))
5314 return;
5315
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005316 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
5318 clearopbeep(cap->oap);
5319 return;
5320 }
5321
5322 prep_redo_cmd(cap);
5323
5324 if (u_save_cursor() == FAIL)
5325 return;
5326
5327 startpos = curwin->w_cursor;
5328#ifdef FEAT_NETBEANS_INTG
5329 pos = startpos;
5330#endif
5331 for (n = cap->count1; n > 0; --n)
5332 {
5333 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5334 inc_cursor();
5335 if (gchar_cursor() == NUL)
5336 {
5337 if (vim_strchr(p_ww, '~') != NULL
5338 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5339 {
5340#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005341 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 if (did_change)
5344 {
5345 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005346 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005347 netbeans_removed(curbuf, pos.lnum, pos.col,
5348 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005350 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352 pos.col = 0;
5353 pos.lnum++;
5354 }
5355#endif
5356 ++curwin->w_cursor.lnum;
5357 curwin->w_cursor.col = 0;
5358 if (n > 1)
5359 {
5360 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5361 break;
5362 u_clearline();
5363 }
5364 }
5365 else
5366 break;
5367 }
5368 }
5369#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005370 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 {
5372 ptr = ml_get(pos.lnum);
5373 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005374 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5375 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377#endif
5378
5379
5380 check_cursor();
5381 curwin->w_set_curswant = TRUE;
5382 if (did_change)
5383 {
5384 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5385 0L);
5386 curbuf->b_op_start = startpos;
5387 curbuf->b_op_end = curwin->w_cursor;
5388 if (curbuf->b_op_end.col > 0)
5389 --curbuf->b_op_end.col;
5390 }
5391}
5392
5393/*
5394 * Move cursor to mark.
5395 */
5396 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005397nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 if (check_mark(pos) == FAIL)
5400 clearop(cap->oap);
5401 else
5402 {
5403 if (cap->cmdchar == '\''
5404 || cap->cmdchar == '`'
5405 || cap->cmdchar == '['
5406 || cap->cmdchar == ']')
5407 setpcmark();
5408 curwin->w_cursor = *pos;
5409 if (flag)
5410 beginline(BL_WHITE | BL_FIX);
5411 else
5412 check_cursor();
5413 }
5414 cap->oap->motion_type = flag ? MLINE : MCHAR;
5415 if (cap->cmdchar == '`')
5416 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005417 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 curwin->w_set_curswant = TRUE;
5419}
5420
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421/*
5422 * Handle commands that are operators in Visual mode.
5423 */
5424 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005425v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426{
5427 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5428
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005429 // Uppercase means linewise, except in block mode, then "D" deletes till
5430 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 if (isupper(cap->cmdchar))
5432 {
5433 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005434 {
5435 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5439 curwin->w_curswant = MAXCOL;
5440 }
5441 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5442 nv_operator(cap);
5443}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445/*
5446 * "s" and "S" commands.
5447 */
5448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005449nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005451#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005452 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005453 if (term_swap_diff() == OK)
5454 return;
5455#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005456#ifdef FEAT_JOB_CHANNEL
5457 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5458 {
5459 clearopbeep(cap->oap);
5460 return;
5461 }
5462#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005463 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 {
5465 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005466 {
5467 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 cap->cmdchar = 'c';
5471 nv_operator(cap);
5472 }
5473 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 nv_optrans(cap);
5475}
5476
5477/*
5478 * Abbreviated commands.
5479 */
5480 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005481nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005484 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005486 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 if (VIsual_active)
5488 v_visop(cap);
5489 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 nv_optrans(cap);
5491}
5492
5493/*
5494 * Translate a command into another command.
5495 */
5496 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005497nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
5499 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5500 (char_u *)"d$", (char_u *)"c$",
5501 (char_u *)"cl", (char_u *)"cc",
5502 (char_u *)"yy", (char_u *)":s\r"};
5503 static char_u *str = (char_u *)"xXDCsSY&";
5504
5505 if (!checkclearopq(cap->oap))
5506 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005507 // In Vi "2D" doesn't delete the next line. Can't translate it
5508 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005509 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5510 {
5511 cap->oap->start = curwin->w_cursor;
5512 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005513#ifdef FEAT_EVAL
5514 set_op_var(OP_DELETE);
5515#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005516 cap->count1 = 1;
5517 nv_dollar(cap);
5518 finish_op = TRUE;
5519 ResetRedobuff();
5520 AppendCharToRedobuff('D');
5521 }
5522 else
5523 {
5524 if (cap->count0)
5525 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005526 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 }
5529 cap->opcount = 0;
5530}
5531
5532/*
5533 * "'" and "`" commands. Also for "g'" and "g`".
5534 * cap->arg is TRUE for "'" and "g'".
5535 */
5536 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005537nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 pos_T *pos;
5540 int c;
5541#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005542 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005543 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544#endif
5545
5546 if (cap->cmdchar == 'g')
5547 c = cap->extra_char;
5548 else
5549 c = cap->nchar;
5550 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005551 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 {
5553 if (cap->arg)
5554 {
5555 check_cursor_lnum();
5556 beginline(BL_WHITE | BL_FIX);
5557 }
5558 else
5559 check_cursor();
5560 }
5561 else
5562 nv_cursormark(cap, cap->arg, pos);
5563
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005564 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 if (!virtual_active())
5566 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005567 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568#ifdef FEAT_FOLDING
5569 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005570 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005571 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 && (fdo_flags & FDO_MARK)
5573 && old_KeyTyped)
5574 foldOpenCursor();
5575#endif
5576}
5577
5578/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005579 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 */
5581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005582nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584#ifdef FEAT_JUMPLIST
5585 pos_T *pos;
5586# ifdef FEAT_FOLDING
5587 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005588 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589# endif
5590
5591 if (!checkclearopq(cap->oap))
5592 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005593 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5594 {
5595 if (goto_tabpage_lastused() == FAIL)
5596 clearopbeep(cap->oap);
5597 return;
5598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (cap->cmdchar == 'g')
5600 pos = movechangelist((int)cap->count1);
5601 else
5602 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005603 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
5605 curwin->w_set_curswant = TRUE;
5606 check_cursor();
5607 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005608 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 nv_cursormark(cap, FALSE, pos);
5610 else if (cap->cmdchar == 'g')
5611 {
5612 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005613 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005615 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005617 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
5619 else
5620 clearopbeep(cap->oap);
5621# ifdef FEAT_FOLDING
5622 if (cap->oap->op_type == OP_NOP
5623 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5624 && (fdo_flags & FDO_MARK)
5625 && old_KeyTyped)
5626 foldOpenCursor();
5627# endif
5628 }
5629#else
5630 clearopbeep(cap->oap);
5631#endif
5632}
5633
5634/*
5635 * Handle '"' command.
5636 */
5637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005638nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 if (checkclearop(cap->oap))
5641 return;
5642#ifdef FEAT_EVAL
5643 if (cap->nchar == '=')
5644 cap->nchar = get_expr_register();
5645#endif
5646 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5647 {
5648 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005649 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650#ifdef FEAT_EVAL
5651 set_reg_var(cap->oap->regname);
5652#endif
5653 }
5654 else
5655 clearopbeep(cap->oap);
5656}
5657
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658/*
5659 * Handle "v", "V" and "CTRL-V" commands.
5660 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5661 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005662 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 */
5664 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005665nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005667 if (cap->cmdchar == Ctrl_Q)
5668 cap->cmdchar = Ctrl_V;
5669
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005670 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5671 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 if (cap->oap->op_type != OP_NOP)
5673 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005674 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005675 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 return;
5677 }
5678
5679 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005680 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005682 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005684 else // toggle char/block mode
5685 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 VIsual_mode = cap->cmdchar;
5687 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005688 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005690 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005692 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
5694 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005695 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005697 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 VIsual = curwin->w_cursor;
5699
5700 VIsual_active = TRUE;
5701 VIsual_reselect = TRUE;
5702 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005703 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005706 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005707 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 /*
5709 * For V and ^V, we multiply the number of lines even if there
5710 * was only one -- webb
5711 */
5712 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5713 {
5714 curwin->w_cursor.lnum +=
5715 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005716 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 VIsual_mode = resel_VIsual_mode;
5719 if (VIsual_mode == 'v')
5720 {
5721 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005722 {
5723 validate_virtcol();
5724 curwin->w_curswant = curwin->w_virtcol
5725 + resel_VIsual_vcol * cap->count0 - 1;
5726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005728 curwin->w_curswant = resel_VIsual_vcol;
5729 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005731 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
5733 curwin->w_curswant = MAXCOL;
5734 coladvance((colnr_T)MAXCOL);
5735 }
5736 else if (VIsual_mode == Ctrl_V)
5737 {
5738 validate_virtcol();
5739 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005740 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 coladvance(curwin->w_curswant);
5742 }
5743 else
5744 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005745 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
5747 else
5748 {
5749 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005750 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 may_start_select('c');
5752 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005753 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005754 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005755 if (cap->count0 > 0 && --cap->count1 > 0)
5756 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005757 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005758 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5759 nv_right(cap);
5760 else if (VIsual_mode == 'V')
5761 nv_down(cap);
5762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
5764 }
5765}
5766
5767/*
5768 * Start selection for Shift-movement keys.
5769 */
5770 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005771start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005773 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 may_start_select('k');
5775 n_start_visual_mode('v');
5776}
5777
5778/*
5779 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5780 */
5781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005782may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
5784 VIsual_select = (stuff_empty() && typebuf_typed()
5785 && (vim_strchr(p_slm, c) != NULL));
5786}
5787
5788/*
5789 * Start Visual mode "c".
5790 * Should set VIsual_select before calling this.
5791 */
5792 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005793n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005795#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005796 int cursor_line_was_concealed = curwin->w_p_cole > 0
5797 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005798#endif
5799
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 VIsual_mode = c;
5801 VIsual_active = TRUE;
5802 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005803
5804 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005805 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005806 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005807 {
5808 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 VIsual = curwin->w_cursor;
5812
5813#ifdef FEAT_FOLDING
5814 foldAdjustVisual();
5815#endif
5816
Bram Moolenaara0620062021-11-17 16:52:40 +00005817 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005819#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005820 // Check if redraw is needed after changing the state.
5821 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005822#endif
5823
Bram Moolenaar09df3122006-01-23 22:23:09 +00005824 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005825 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005827 // Make sure the clipboard gets updated. Needed because start and
5828 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 clip_star.vmode = NUL;
5830#endif
5831
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005832 // Only need to redraw this line, unless still need to redraw an old
5833 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (curwin->w_redr_type < INVERTED)
5835 {
5836 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5837 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5838 }
5839}
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842/*
5843 * CTRL-W: Window commands
5844 */
5845 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005846nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005848 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005849 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005850 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005851 cap->cmdchar = ':';
5852 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005853 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005854 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005855 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005856 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857}
5858
5859/*
5860 * CTRL-Z: Suspend
5861 */
5862 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005863nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005867 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005868 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869}
5870
5871/*
5872 * Commands starting with "g".
5873 */
5874 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005875nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 int i;
5880 int flag = FALSE;
5881
5882 switch (cap->nchar)
5883 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005884 case Ctrl_A:
5885 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886#ifdef MEM_PROFILE
5887 /*
5888 * "g^A": dump log of used memory.
5889 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005890 if (!VIsual_active && cap->nchar == Ctrl_A)
5891 vim_mem_profile_dump();
5892 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005894 /*
5895 * "g^A/g^X": sequentially increment visually selected region
5896 */
5897 if (VIsual_active)
5898 {
5899 cap->arg = TRUE;
5900 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005901 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005902 nv_addsub(cap);
5903 }
5904 else
5905 clearopbeep(oap);
5906 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 /*
5909 * "gR": Enter virtual replace mode.
5910 */
5911 case 'R':
5912 cap->arg = TRUE;
5913 nv_Replace(cap);
5914 break;
5915
5916 case 'r':
5917 nv_vreplace(cap);
5918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919
5920 case '&':
5921 do_cmdline_cmd((char_u *)"%s//~/&");
5922 break;
5923
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 /*
5925 * "gv": Reselect the previous Visual area. If Visual already active,
5926 * exchange previous and current Visual area.
5927 */
5928 case 'v':
5929 if (checkclearop(oap))
5930 break;
5931
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005932 if ( curbuf->b_visual.vi_start.lnum == 0
5933 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5934 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 beep_flush();
5936 else
5937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005938 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 if (VIsual_active)
5940 {
5941 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005942 VIsual_mode = curbuf->b_visual.vi_mode;
5943 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944# ifdef FEAT_EVAL
5945 curbuf->b_visual_mode_eval = i;
5946# endif
5947 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005948 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5949 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005951 tpos = curbuf->b_visual.vi_end;
5952 curbuf->b_visual.vi_end = curwin->w_cursor;
5953 curwin->w_cursor = curbuf->b_visual.vi_start;
5954 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 }
5956 else
5957 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005958 VIsual_mode = curbuf->b_visual.vi_mode;
5959 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5960 tpos = curbuf->b_visual.vi_end;
5961 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 }
5963
5964 VIsual_active = TRUE;
5965 VIsual_reselect = TRUE;
5966
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005967 // Set Visual to the start and w_cursor to the end of the Visual
5968 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 check_cursor();
5970 VIsual = curwin->w_cursor;
5971 curwin->w_cursor = tpos;
5972 check_cursor();
5973 update_topline();
5974 /*
5975 * When called from normal "g" command: start Select mode when
5976 * 'selectmode' contains "cmd". When called for K_SELECT, always
5977 * start Select mode.
5978 */
5979 if (cap->arg)
5980 VIsual_select = TRUE;
5981 else
5982 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005985 // Make sure the clipboard gets updated. Needed because start and
5986 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 clip_star.vmode = NUL;
5988#endif
5989 redraw_curbuf_later(INVERTED);
5990 showmode();
5991 }
5992 break;
5993 /*
5994 * "gV": Don't reselect the previous Visual area after a Select mode
5995 * mapping of menu.
5996 */
5997 case 'V':
5998 VIsual_reselect = FALSE;
5999 break;
6000
6001 /*
6002 * "gh": start Select mode.
6003 * "gH": start Select line mode.
6004 * "g^H": start Select block mode.
6005 */
6006 case K_BS:
6007 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006008 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 case 'h':
6010 case 'H':
6011 case Ctrl_H:
6012# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006013 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 if (cap->nchar == Ctrl_H)
6015 cap->cmdchar = Ctrl_V;
6016 else
6017# endif
6018 cap->cmdchar = cap->nchar + ('v' - 'h');
6019 cap->arg = TRUE;
6020 nv_visual(cap);
6021 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006023 // "gn", "gN" visually select next/previous search match
6024 // "gn" selects next match
6025 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006026 case 'N':
6027 case 'n':
6028 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006029 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006030 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031
6032 /*
6033 * "gj" and "gk" two new funny movement keys -- up and down
6034 * movement based on *screen* line rather than *file* line.
6035 */
6036 case 'j':
6037 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006038 // with 'nowrap' it works just like the normal "j" command.
6039 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 {
6041 oap->motion_type = MLINE;
6042 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6043 }
6044 else
6045 i = nv_screengo(oap, FORWARD, cap->count1);
6046 if (i == FAIL)
6047 clearopbeep(oap);
6048 break;
6049
6050 case 'k':
6051 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006052 // with 'nowrap' it works just like the normal "k" command.
6053 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 {
6055 oap->motion_type = MLINE;
6056 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6057 }
6058 else
6059 i = nv_screengo(oap, BACKWARD, cap->count1);
6060 if (i == FAIL)
6061 clearopbeep(oap);
6062 break;
6063
6064 /*
6065 * "gJ": join two lines without inserting a space.
6066 */
6067 case 'J':
6068 nv_join(cap);
6069 break;
6070
6071 /*
6072 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6073 * "gm": middle of "g0" and "g$".
6074 */
6075 case '^':
6076 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006077 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078
6079 case '0':
6080 case 'm':
6081 case K_HOME:
6082 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 oap->motion_type = MCHAR;
6084 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006085 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006087 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 int width2 = width1 + curwin_col_off2();
6089
6090 validate_virtcol();
6091 i = 0;
6092 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6093 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6094 }
6095 else
6096 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006097 // Go to the middle of the screen line. When 'number' or
6098 // 'relativenumber' is on and lines are wrapping the middle can be more
6099 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006101 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 + ((curwin->w_p_wrap && i > 0)
6103 ? curwin_col_off2() : 0)) / 2;
6104 coladvance((colnr_T)i);
6105 if (flag)
6106 {
6107 do
6108 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006109 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006110 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 }
6112 curwin->w_set_curswant = TRUE;
6113 break;
6114
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006115 case 'M':
6116 {
6117 char_u *ptr = ml_get_curline();
6118
6119 oap->motion_type = MCHAR;
6120 oap->inclusive = FALSE;
6121 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006122 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006123 else
6124 i = (int)STRLEN(ptr);
6125 if (cap->count0 > 0 && cap->count0 <= 100)
6126 coladvance((colnr_T)(i * cap->count0 / 100));
6127 else
6128 coladvance((colnr_T)(i / 2));
6129 curwin->w_set_curswant = TRUE;
6130 }
6131 break;
6132
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006134 // "g_": to the last non-blank character in the line or <count> lines
6135 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 cap->oap->motion_type = MCHAR;
6137 cap->oap->inclusive = TRUE;
6138 curwin->w_curswant = MAXCOL;
6139 if (cursor_down((long)(cap->count1 - 1),
6140 cap->oap->op_type == OP_NOP) == FAIL)
6141 clearopbeep(cap->oap);
6142 else
6143 {
6144 char_u *ptr = ml_get_curline();
6145
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006146 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6148 --curwin->w_cursor.col;
6149
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006150 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006152 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 --curwin->w_cursor.col;
6154 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006155 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 }
6157 break;
6158
6159 case '$':
6160 case K_END:
6161 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 {
6163 int col_off = curwin_col_off();
6164
6165 oap->motion_type = MCHAR;
6166 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006167 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006169 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 if (cap->count1 == 1)
6171 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006172 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 int width2 = width1 + curwin_col_off2();
6174
6175 validate_virtcol();
6176 i = width1 - 1;
6177 if (curwin->w_virtcol >= (colnr_T)width1)
6178 i += ((curwin->w_virtcol - width1) / width2 + 1)
6179 * width2;
6180 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006182 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006183 validate_virtcol();
6184 curwin->w_curswant = curwin->w_virtcol;
6185 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6187 {
6188 /*
6189 * Check for landing on a character that got split at
6190 * the end of the line. We do not want to advance to
6191 * the next screen line.
6192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (curwin->w_virtcol > (colnr_T)i)
6194 --curwin->w_cursor.col;
6195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6198 clearopbeep(oap);
6199 }
6200 else
6201 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006202 if (cap->count1 > 1)
6203 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006204 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006205
Bram Moolenaar02631462017-09-22 15:20:32 +02006206 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006208
Bram Moolenaar74ede802021-05-29 19:18:01 +02006209 // if the character doesn't fit move one back
6210 if (curwin->w_cursor.col > 0
6211 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6212 {
6213 colnr_T vcol;
6214
6215 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6216 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6217 --curwin->w_cursor.col;
6218 }
6219
Bram Moolenaard5c82342019-07-27 18:44:57 +02006220 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006221 validate_virtcol();
6222 curwin->w_curswant = curwin->w_virtcol;
6223 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
6225 }
6226 break;
6227
6228 /*
6229 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6230 */
6231 case '*':
6232 case '#':
6233#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006234 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006236 case Ctrl_RSB: // :tag or :tselect for current identifier
6237 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 nv_ident(cap);
6239 break;
6240
6241 /*
6242 * ge and gE: go back to end of word
6243 */
6244 case 'e':
6245 case 'E':
6246 oap->motion_type = MCHAR;
6247 curwin->w_set_curswant = TRUE;
6248 oap->inclusive = TRUE;
6249 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6250 clearopbeep(oap);
6251 break;
6252
6253 /*
6254 * "g CTRL-G": display info about cursor position
6255 */
6256 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006257 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 break;
6259
6260 /*
6261 * "gi": start Insert at the last position.
6262 */
6263 case 'i':
6264 if (curbuf->b_last_insert.lnum != 0)
6265 {
6266 curwin->w_cursor = curbuf->b_last_insert;
6267 check_cursor_lnum();
6268 i = (int)STRLEN(ml_get_curline());
6269 if (curwin->w_cursor.col > (colnr_T)i)
6270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 if (virtual_active())
6272 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 curwin->w_cursor.col = i;
6274 }
6275 }
6276 cap->cmdchar = 'i';
6277 nv_edit(cap);
6278 break;
6279
6280 /*
6281 * "gI": Start insert in column 1.
6282 */
6283 case 'I':
6284 beginline(0);
6285 if (!checkclearopq(oap))
6286 invoke_edit(cap, FALSE, 'g', FALSE);
6287 break;
6288
6289#ifdef FEAT_SEARCHPATH
6290 /*
6291 * "gf": goto file, edit file under cursor
6292 * "]f" and "[f": can also be used.
6293 */
6294 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006295 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 nv_gotofile(cap);
6297 break;
6298#endif
6299
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006300 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 case '\'':
6302 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006303 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 case '`':
6305 nv_gomark(cap);
6306 break;
6307
6308 /*
6309 * "gs": Goto sleep.
6310 */
6311 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006312 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 break;
6314
6315 /*
6316 * "ga": Display the ascii value of the character under the
6317 * cursor. It is displayed in decimal, hex, and octal. -- webb
6318 */
6319 case 'a':
6320 do_ascii(NULL);
6321 break;
6322
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 /*
6324 * "g8": Display the bytes used for the UTF-8 character under the
6325 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006326 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 */
6328 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006329 if (cap->count0 == 8)
6330 utf_find_illegal();
6331 else
6332 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006335 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006336 case '<':
6337 show_sb_text();
6338 break;
6339
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 /*
6341 * "gg": Goto the first line in file. With a count it goes to
6342 * that line number like for "G". -- webb
6343 */
6344 case 'g':
6345 cap->arg = FALSE;
6346 nv_goto(cap);
6347 break;
6348
6349 /*
6350 * Two-character operators:
6351 * "gq" Format text
6352 * "gw" Format text and keep cursor position
6353 * "g~" Toggle the case of the text.
6354 * "gu" Change text to lower case.
6355 * "gU" Change text to upper case.
6356 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006357 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 */
6359 case 'q':
6360 case 'w':
6361 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006362 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 case '~':
6364 case 'u':
6365 case 'U':
6366 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006367 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 nv_operator(cap);
6369 break;
6370
6371 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006372 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 * current function
6374 * "gD": idem, but in the current file.
6375 */
6376 case 'd':
6377 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006378 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 break;
6380
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 /*
6382 * g<*Mouse> : <C-*mouse>
6383 */
6384 case K_MIDDLEMOUSE:
6385 case K_MIDDLEDRAG:
6386 case K_MIDDLERELEASE:
6387 case K_LEFTMOUSE:
6388 case K_LEFTDRAG:
6389 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006390 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 case K_RIGHTMOUSE:
6392 case K_RIGHTDRAG:
6393 case K_RIGHTRELEASE:
6394 case K_X1MOUSE:
6395 case K_X1DRAG:
6396 case K_X1RELEASE:
6397 case K_X2MOUSE:
6398 case K_X2DRAG:
6399 case K_X2RELEASE:
6400 mod_mask = MOD_MASK_CTRL;
6401 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404 case K_IGNORE:
6405 break;
6406
6407 /*
6408 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6409 */
6410 case 'p':
6411 case 'P':
6412 nv_put(cap);
6413 break;
6414
6415#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006416 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 case 'o':
6418 goto_byte(cap->count0);
6419 break;
6420#endif
6421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006422 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006424 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 {
6426 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006427 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 break;
6429 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006430
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (!checkclearopq(oap))
6432 do_exmode(TRUE);
6433 break;
6434
6435#ifdef FEAT_JUMPLIST
6436 case ',':
6437 nv_pcmark(cap);
6438 break;
6439
6440 case ';':
6441 cap->count1 = -cap->count1;
6442 nv_pcmark(cap);
6443 break;
6444#endif
6445
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006446 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006447 if (!checkclearop(oap))
6448 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006450 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006451 if (!checkclearop(oap))
6452 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006453 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006454
Bram Moolenaar62a23252020-08-09 14:04:42 +02006455 case TAB:
6456 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6457 clearopbeep(oap);
6458 break;
6459
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006460 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006461 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006462 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006463 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006464 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006465 break;
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 default:
6468 clearopbeep(oap);
6469 break;
6470 }
6471}
6472
6473/*
6474 * Handle "o" and "O" commands.
6475 */
6476 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006477n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006479#ifdef FEAT_CONCEAL
6480 linenr_T oldline = curwin->w_cursor.lnum;
6481#endif
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (!checkclearopq(cap->oap))
6484 {
6485#ifdef FEAT_FOLDING
6486 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006487 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 (void)hasFolding(curwin->w_cursor.lnum,
6489 &curwin->w_cursor.lnum, NULL);
6490 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006491 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 (void)hasFolding(curwin->w_cursor.lnum,
6493 NULL, &curwin->w_cursor.lnum);
6494#endif
6495 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6496 (cap->cmdchar == 'O' ? 1 : 0)),
6497 (linenr_T)(curwin->w_cursor.lnum +
6498 (cap->cmdchar == 'o' ? 1 : 0))
6499 ) == OK
6500 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006501 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6502 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006504#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006505 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006506 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006507#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006508#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006509 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006510 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006511 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006512#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006513 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006514 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6515 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6517 }
6518 }
6519}
6520
6521/*
6522 * "." command: redo last change.
6523 */
6524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006525nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 if (!checkclearopq(cap->oap))
6528 {
6529 /*
6530 * If "restart_edit" is TRUE, the last but one command is repeated
6531 * instead of the last command (inserting text). This is used for
6532 * CTRL-O <.> in insert mode.
6533 */
6534 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6535 clearopbeep(cap->oap);
6536 }
6537}
6538
6539/*
6540 * CTRL-R: undo undo
6541 */
6542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006543nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
6545 if (!checkclearopq(cap->oap))
6546 {
6547 u_redo((int)cap->count1);
6548 curwin->w_set_curswant = TRUE;
6549 }
6550}
6551
6552/*
6553 * Handle "U" command.
6554 */
6555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006556nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006558 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006559 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006561 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 cap->cmdchar = 'g';
6563 cap->nchar = 'U';
6564 nv_operator(cap);
6565 }
6566 else if (!checkclearopq(cap->oap))
6567 {
6568 u_undoline();
6569 curwin->w_set_curswant = TRUE;
6570 }
6571}
6572
6573/*
6574 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6575 * single character.
6576 */
6577 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006578nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006580 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006581 {
6582#ifdef FEAT_JOB_CHANNEL
6583 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6584 {
6585 clearopbeep(cap->oap);
6586 return;
6587 }
6588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 else
6592 nv_operator(cap);
6593}
6594
6595/*
6596 * Handle an operator command.
6597 * The actual work is done by do_pending_operator().
6598 */
6599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006600nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602 int op_type;
6603
6604 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006605#ifdef FEAT_JOB_CHANNEL
6606 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6607 {
6608 clearopbeep(cap->oap);
6609 return;
6610 }
6611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006613 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 nv_lineop(cap);
6615 else if (!checkclearop(cap->oap))
6616 {
6617 cap->oap->start = curwin->w_cursor;
6618 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006619#ifdef FEAT_EVAL
6620 set_op_var(op_type);
6621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 }
6623}
6624
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006625#ifdef FEAT_EVAL
6626/*
6627 * Set v:operator to the characters for "optype".
6628 */
6629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006631{
6632 char_u opchars[3];
6633
6634 if (optype == OP_NOP)
6635 set_vim_var_string(VV_OP, NULL, 0);
6636 else
6637 {
6638 opchars[0] = get_op_char(optype);
6639 opchars[1] = get_extra_op_char(optype);
6640 opchars[2] = NUL;
6641 set_vim_var_string(VV_OP, opchars, -1);
6642 }
6643}
6644#endif
6645
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646/*
6647 * Handle linewise operator "dd", "yy", etc.
6648 *
6649 * "_" is is a strange motion command that helps make operators more logical.
6650 * It is actually implemented, but not documented in the real Vi. This motion
6651 * command actually refers to "the current line". Commands like "dd" and "yy"
6652 * are really an alternate form of "d_" and "y_". It does accept a count, so
6653 * "d3_" works to delete 3 lines.
6654 */
6655 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006656nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 cap->oap->motion_type = MLINE;
6659 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6660 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006661 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006662 && cap->oap->motion_force != 'v'
6663 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 || cap->oap->op_type == OP_LSHIFT
6665 || cap->oap->op_type == OP_RSHIFT)
6666 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006667 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 beginline(BL_WHITE | BL_FIX);
6669}
6670
6671/*
6672 * <Home> command.
6673 */
6674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006675nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006677 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006678 if (mod_mask & MOD_MASK_CTRL)
6679 nv_goto(cap);
6680 else
6681 {
6682 cap->count0 = 1;
6683 nv_pipe(cap);
6684 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006685 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6686 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687}
6688
6689/*
6690 * "|" command.
6691 */
6692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006693nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 cap->oap->motion_type = MCHAR;
6696 cap->oap->inclusive = FALSE;
6697 beginline(0);
6698 if (cap->count0 > 0)
6699 {
6700 coladvance((colnr_T)(cap->count0 - 1));
6701 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6702 }
6703 else
6704 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006705 // keep curswant at the column where we wanted to go, not where
6706 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 curwin->w_set_curswant = FALSE;
6708}
6709
6710/*
6711 * Handle back-word command "b" and "B".
6712 * cap->arg is 1 for "B"
6713 */
6714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006715nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717 cap->oap->motion_type = MCHAR;
6718 cap->oap->inclusive = FALSE;
6719 curwin->w_set_curswant = TRUE;
6720 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6721 clearopbeep(cap->oap);
6722#ifdef FEAT_FOLDING
6723 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6724 foldOpenCursor();
6725#endif
6726}
6727
6728/*
6729 * Handle word motion commands "e", "E", "w" and "W".
6730 * cap->arg is TRUE for "E" and "W".
6731 */
6732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006733nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 int n;
6736 int word_end;
6737 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006738 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739
6740 /*
6741 * Set inclusive for the "E" and "e" command.
6742 */
6743 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6744 word_end = TRUE;
6745 else
6746 word_end = FALSE;
6747 cap->oap->inclusive = word_end;
6748
6749 /*
6750 * "cw" and "cW" are a special case.
6751 */
6752 if (!word_end && cap->oap->op_type == OP_CHANGE)
6753 {
6754 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006757 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
6759 /*
6760 * Reproduce a funny Vi behaviour: "cw" on a blank only
6761 * changes one character, not all blanks until the start of
6762 * the next word. Only do this when the 'w' flag is included
6763 * in 'cpoptions'.
6764 */
6765 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6766 {
6767 cap->oap->inclusive = TRUE;
6768 cap->oap->motion_type = MCHAR;
6769 return;
6770 }
6771 }
6772 else
6773 {
6774 /*
6775 * This is a little strange. To match what the real Vi does,
6776 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6777 * that we are not on a space or a TAB. This seems impolite
6778 * at first, but it's really more what we mean when we say
6779 * 'cw'.
6780 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006781 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 * will change only one character! This is done by setting
6783 * flag.
6784 */
6785 cap->oap->inclusive = TRUE;
6786 word_end = TRUE;
6787 flag = TRUE;
6788 }
6789 }
6790 }
6791
6792 cap->oap->motion_type = MCHAR;
6793 curwin->w_set_curswant = TRUE;
6794 if (word_end)
6795 n = end_word(cap->count1, cap->arg, flag, FALSE);
6796 else
6797 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006799 // Don't leave the cursor on the NUL past the end of line. Unless we
6800 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006801 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006802 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
6804 if (n == FAIL && cap->oap->op_type == OP_NOP)
6805 clearopbeep(cap->oap);
6806 else
6807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809#ifdef FEAT_FOLDING
6810 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6811 foldOpenCursor();
6812#endif
6813 }
6814}
6815
6816/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006817 * Used after a movement command: If the cursor ends up on the NUL after the
6818 * end of the line, may move it back to the last character and make the motion
6819 * inclusive.
6820 */
6821 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006822adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006823{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006824 // The cursor cannot remain on the NUL when:
6825 // - the column is > 0
6826 // - not in Visual mode or 'selection' is "o"
6827 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006828 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006829 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006830 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006831 {
6832 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006833 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006834 if (has_mbyte)
6835 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006836 oap->inclusive = TRUE;
6837 }
6838}
6839
6840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 * "0" and "^" commands.
6842 * cap->arg is the argument for beginline().
6843 */
6844 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006845nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 cap->oap->motion_type = MCHAR;
6848 cap->oap->inclusive = FALSE;
6849 beginline(cap->arg);
6850#ifdef FEAT_FOLDING
6851 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6852 foldOpenCursor();
6853#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006854 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6855 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856}
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
6859 * In exclusive Visual mode, may include the last character.
6860 */
6861 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006862adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006865 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (has_mbyte)
6868 inc_cursor();
6869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 ++curwin->w_cursor.col;
6871 cap->oap->inclusive = FALSE;
6872 }
6873}
6874
6875/*
6876 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6877 * Should check VIsual_mode before calling this.
6878 * Returns TRUE when backed up to the previous line.
6879 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006880 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006881unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882{
6883 pos_T *pp;
6884
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006885 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006887 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 pp = &curwin->w_cursor;
6889 else
6890 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 if (pp->coladd > 0)
6892 --pp->coladd;
6893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 if (pp->col > 0)
6895 {
6896 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006897 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 }
6899 else if (pp->lnum > 1)
6900 {
6901 --pp->lnum;
6902 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6903 return TRUE;
6904 }
6905 }
6906 return FALSE;
6907}
6908
6909/*
6910 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6911 */
6912 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006913nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 if (VIsual_active)
6916 VIsual_select = TRUE;
6917 else if (VIsual_reselect)
6918 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006919 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 cap->arg = TRUE;
6921 nv_g_cmd(cap);
6922 }
6923}
6924
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
6926/*
6927 * "G", "gg", CTRL-END, CTRL-HOME.
6928 * cap->arg is TRUE for "G".
6929 */
6930 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006931nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932{
6933 linenr_T lnum;
6934
6935 if (cap->arg)
6936 lnum = curbuf->b_ml.ml_line_count;
6937 else
6938 lnum = 1L;
6939 cap->oap->motion_type = MLINE;
6940 setpcmark();
6941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006942 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 if (cap->count0 != 0)
6944 lnum = cap->count0;
6945 if (lnum < 1L)
6946 lnum = 1L;
6947 else if (lnum > curbuf->b_ml.ml_line_count)
6948 lnum = curbuf->b_ml.ml_line_count;
6949 curwin->w_cursor.lnum = lnum;
6950 beginline(BL_SOL | BL_FIX);
6951#ifdef FEAT_FOLDING
6952 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6953 foldOpenCursor();
6954#endif
6955}
6956
6957/*
6958 * CTRL-\ in Normal mode.
6959 */
6960 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006961nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
6963 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6964 {
6965 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006966 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 restart_edit = 0;
6969#ifdef FEAT_CMDWIN
6970 if (cmdwin_type != 0)
6971 cmdwin_result = Ctrl_C;
6972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (VIsual_active)
6974 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006975 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 redraw_curbuf_later(INVERTED);
6977 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 if (cap->nchar == Ctrl_G && p_im)
6980 restart_edit = 'a';
6981 }
6982 else
6983 clearopbeep(cap->oap);
6984}
6985
6986/*
6987 * ESC in Normal mode: beep, but don't flush buffers.
6988 * Don't even beep if we are canceling a command.
6989 */
6990 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006991nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 int no_reason;
6994
6995 no_reason = (cap->oap->op_type == OP_NOP
6996 && cap->opcount == 0
6997 && cap->count0 == 0
6998 && cap->oap->regname == 0
6999 && !p_im);
7000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
7003 if (restart_edit == 0
7004#ifdef FEAT_CMDWIN
7005 && cmdwin_type == 0
7006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007009 {
7010 if (anyBufIsChanged())
7011 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
7012 else
7013 msg(_("Type :qa and press <Enter> to exit Vim"));
7014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007016 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
7017 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 if (!p_im)
7019 restart_edit = 0;
7020#ifdef FEAT_CMDWIN
7021 if (cmdwin_type != 0)
7022 {
7023 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007024 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 return;
7026 }
7027#endif
7028 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01007029#ifdef FEAT_CMDWIN
7030 else if (cmdwin_type != 0 && ex_normal_busy)
7031 {
7032 // When :normal runs out of characters while in the command line window
7033 // vgetorpeek() will return ESC. Exit the cmdline window to break the
7034 // loop.
7035 cmdwin_result = K_IGNORE;
7036 return;
7037 }
7038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 if (VIsual_active)
7041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007042 end_visual_mode(); // stop Visual
7043 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 curwin->w_set_curswant = TRUE;
7045 redraw_curbuf_later(INVERTED);
7046 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007047 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007048 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 clearop(cap->oap);
7050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7052 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007053 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 restart_edit = 'a';
7055}
7056
7057/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007058 * Move the cursor for the "A" command.
7059 */
7060 void
7061set_cursor_for_append_to_line(void)
7062{
7063 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007064 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007065 {
7066 int save_State = State;
7067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 // Pretend Insert mode here to allow the cursor on the
7069 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007070 State = INSERT;
7071 coladvance((colnr_T)MAXCOL);
7072 State = save_State;
7073 }
7074 else
7075 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7076}
7077
7078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007080 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 */
7082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007083nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007085 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7087 cap->cmdchar = 'i';
7088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007089 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007091 {
7092#ifdef FEAT_TERMINAL
7093 if (term_in_normal_mode())
7094 {
7095 end_visual_mode();
7096 clearop(cap->oap);
7097 term_enter_job_mode();
7098 return;
7099 }
7100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007105 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7106 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
7108#ifdef FEAT_TEXTOBJ
7109 nv_object(cap);
7110#else
7111 clearopbeep(cap->oap);
7112#endif
7113 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007114#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007115 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007116 {
7117 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007118 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007119 return;
7120 }
7121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 else if (!curbuf->b_p_ma && !p_im)
7123 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007124 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007125 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007127 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007128 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007129 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007131 else if (cap->cmdchar == K_PS && VIsual_active)
7132 {
7133 pos_T old_pos = curwin->w_cursor;
7134 pos_T old_visual = VIsual;
7135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007136 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007137 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7138 {
7139 shift_delete_registers();
7140 cap->oap->regname = '1';
7141 }
7142 else
7143 cap->oap->regname = '-';
7144 cap->cmdchar = 'd';
7145 cap->nchar = NUL;
7146 nv_operator(cap);
7147 do_pending_operator(cap, 0, FALSE);
7148 cap->cmdchar = K_PS;
7149
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007150 // When the last char in the line was deleted then append. Detect this
7151 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007152 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7153 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007154 inc_cursor();
7155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007156 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007157 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 else if (!checkclearopq(cap->oap))
7160 {
7161 switch (cap->cmdchar)
7162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007163 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007164 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 break;
7166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007167 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007168 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7169 beginline(BL_WHITE);
7170 else
7171 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 break;
7173
Bram Moolenaara1891842017-02-04 21:34:31 +01007174 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007175 // Bracketed paste works like "a"ppend, unless the cursor is in
7176 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007177 if (curwin->w_cursor.col == 0)
7178 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007179 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181 case 'a': // "a"ppend is like "i"nsert on the next character.
7182 // increment coladd when in virtual space, increment the
7183 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (virtual_active()
7185 && (curwin->w_cursor.coladd > 0
7186 || *ml_get_cursor() == NUL
7187 || *ml_get_cursor() == TAB))
7188 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007189 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 inc_cursor();
7191 break;
7192 }
7193
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7195 {
7196 int save_State = State;
7197
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007198 // Pretend Insert mode here to allow the cursor on the
7199 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 State = INSERT;
7201 coladvance(getviscol());
7202 State = save_State;
7203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204
7205 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7206 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007207 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007208 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007209 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210}
7211
7212/*
7213 * Invoke edit() and take care of "restart_edit" and the return value.
7214 */
7215 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007216invoke_edit(
7217 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007218 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007219 int cmd,
7220 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 int restart_edit_save = 0;
7223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007224 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7225 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7226 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 if (repl || !stuff_empty())
7228 restart_edit_save = restart_edit;
7229 else
7230 restart_edit_save = 0;
7231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007232 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 restart_edit = 0;
7234
7235 if (edit(cmd, startln, cap->count1))
7236 cap->retval |= CA_COMMAND_BUSY;
7237
7238 if (restart_edit == 0)
7239 restart_edit = restart_edit_save;
7240}
7241
7242#ifdef FEAT_TEXTOBJ
7243/*
7244 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7245 */
7246 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007247nv_object(
7248 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
7250 int flag;
7251 int include;
7252 char_u *mps_save;
7253
7254 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007255 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007257 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007259 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 mps_save = curbuf->b_p_mps;
7261 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7262
7263 switch (cap->nchar)
7264 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007265 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 flag = current_word(cap->oap, cap->count1, include, FALSE);
7267 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007268 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 flag = current_word(cap->oap, cap->count1, include, TRUE);
7270 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007271 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 case '(':
7273 case ')':
7274 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7275 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007276 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 case '{':
7278 case '}':
7279 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7280 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007281 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 case ']':
7283 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7284 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007285 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 case '>':
7287 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7288 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007289 case 't': // "at" = a tag block (xml and html)
7290 // Do not adjust oap->end in do_pending_operator()
7291 // otherwise there are different results for 'dit'
7292 // (note leading whitespace in last line):
7293 // 1) <b> 2) <b>
7294 // foobar foobar
7295 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007296 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007297 flag = current_tagblock(cap->oap, cap->count1, include);
7298 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007299 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 flag = current_par(cap->oap, cap->count1, include, 'p');
7301 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007302 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 flag = current_sent(cap->oap, cap->count1, include);
7304 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007305 case '"': // "a"" = a double quoted string
7306 case '\'': // "a'" = a single quoted string
7307 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007308 flag = current_quote(cap->oap, cap->count1, include,
7309 cap->nchar);
7310 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007311#if 0 // TODO
7312 case 'S': // "aS" = a section
7313 case 'f': // "af" = a filename
7314 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315#endif
7316 default:
7317 flag = FAIL;
7318 break;
7319 }
7320
7321 curbuf->b_p_mps = mps_save;
7322 if (flag == FAIL)
7323 clearopbeep(cap->oap);
7324 adjust_cursor_col();
7325 curwin->w_set_curswant = TRUE;
7326}
7327#endif
7328
7329/*
7330 * "q" command: Start/stop recording.
7331 * "q:", "q/", "q?": edit command-line in command-line window.
7332 */
7333 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007334nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
7336 if (cap->oap->op_type == OP_FORMAT)
7337 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007338 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 cap->cmdchar = 'g';
7340 cap->nchar = 'q';
7341 nv_operator(cap);
7342 }
7343 else if (!checkclearop(cap->oap))
7344 {
7345#ifdef FEAT_CMDWIN
7346 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7347 {
7348 stuffcharReadbuff(cap->nchar);
7349 stuffcharReadbuff(K_CMDWIN);
7350 }
7351 else
7352#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007353 // (stop) recording into a named register, unless executing a
7354 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007355 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 clearopbeep(cap->oap);
7357 }
7358}
7359
7360/*
7361 * Handle the "@r" command.
7362 */
7363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007364nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365{
7366 if (checkclearop(cap->oap))
7367 return;
7368#ifdef FEAT_EVAL
7369 if (cap->nchar == '=')
7370 {
7371 if (get_expr_register() == NUL)
7372 return;
7373 }
7374#endif
7375 while (cap->count1-- && !got_int)
7376 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007377 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
7379 clearopbeep(cap->oap);
7380 break;
7381 }
7382 line_breakcheck();
7383 }
7384}
7385
7386/*
7387 * Handle the CTRL-U and CTRL-D commands.
7388 */
7389 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007390nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7393 || (cap->cmdchar == Ctrl_D
7394 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7395 clearopbeep(cap->oap);
7396 else if (!checkclearop(cap->oap))
7397 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7398}
7399
7400/*
7401 * Handle "J" or "gJ" command.
7402 */
7403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007404nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007406 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007408 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 {
7410 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007411 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7413 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007415 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007416 if (cap->count0 <= 2)
7417 {
7418 clearopbeep(cap->oap);
7419 return;
7420 }
7421 cap->count0 = curbuf->b_ml.ml_line_count
7422 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007424
7425 prep_redo(cap->oap->regname, cap->count0,
7426 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7427 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429}
7430
7431/*
7432 * "P", "gP", "p" and "gp" commands.
7433 */
7434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007435nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007437 nv_put_opt(cap, FALSE);
7438}
7439
7440/*
7441 * "P", "gP", "p" and "gp" commands.
7442 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7443 */
7444 static void
7445nv_put_opt(cmdarg_T *cap, int fix_indent)
7446{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 int regname = 0;
7448 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007449 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007450 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 int dir;
7452 int flags = 0;
7453
7454 if (cap->oap->op_type != OP_NOP)
7455 {
7456#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007457 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7459 {
7460 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007461 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
7463 else
7464#endif
7465 clearopbeep(cap->oap);
7466 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007467#ifdef FEAT_JOB_CHANNEL
7468 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7469 {
7470 clearopbeep(cap->oap);
7471 }
7472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 else
7474 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007475 if (fix_indent)
7476 {
7477 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7478 ? FORWARD : BACKWARD;
7479 flags |= PUT_FIXINDENT;
7480 }
7481 else
7482 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007483 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7484 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 prep_redo_cmd(cap);
7486 if (cap->cmdchar == 'g')
7487 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007488 else if (cap->cmdchar == 'z')
7489 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if (VIsual_active)
7492 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007493 // Putting in Visual mode: The put text replaces the selected
7494 // text. First delete the selected text, then put the new text.
7495 // Need to save and restore the registers that the delete
7496 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007497 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007499#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007501#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007502 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007503 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007504#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507
7508 )
7509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007510 // The delete is going to overwrite the register we want to
7511 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 reg1 = get_register(regname, TRUE);
7513 }
7514
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007515 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 cap->cmdchar = 'd';
7517 cap->nchar = NUL;
7518 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007519 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 nv_operator(cap);
7521 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007522 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007523 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007525 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 cap->oap->regname = regname;
7527
7528 if (reg1 != NULL)
7529 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007530 // Delete probably changed the register we want to put, save
7531 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 reg2 = get_register(regname, FALSE);
7533 put_register(regname, reg1);
7534 }
7535
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007536 // When deleted a linewise Visual area, put the register as
7537 // lines to avoid it joined with the next line. When deletion was
7538 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 if (VIsual_mode == 'V')
7540 flags |= PUT_LINE;
7541 else if (VIsual_mode == 'v')
7542 flags |= PUT_LINE_SPLIT;
7543 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7544 flags |= PUT_LINE_FORWARD;
7545 dir = BACKWARD;
7546 if ((VIsual_mode != 'V'
7547 && curwin->w_cursor.col < curbuf->b_op_start.col)
7548 || (VIsual_mode == 'V'
7549 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007550 // cursor is at the end of the line or end of file, put
7551 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007553 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007554 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007556 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007558 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 if (reg2 != NULL)
7560 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007562 // What to reselect with "gv"? Selecting the just put text seems to
7563 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007564 if (was_visual)
7565 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007566 curbuf->b_visual.vi_start = curbuf->b_op_start;
7567 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007568 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007569 if (*p_sel == 'e')
7570 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007571 }
7572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007573 // When all lines were selected and deleted do_put() leaves an empty
7574 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007575 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007576 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007577 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007578 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007579
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007580 // If the cursor was in that line, move it to the end of the last
7581 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007582 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7583 {
7584 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7585 coladvance((colnr_T)MAXCOL);
7586 }
7587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 auto_format(FALSE, TRUE);
7589 }
7590}
7591
7592/*
7593 * "o" and "O" commands.
7594 */
7595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007596nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007599 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7601 {
7602 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007603 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 }
7605 else
7606#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007607 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007609#ifdef FEAT_JOB_CHANNEL
7610 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007611 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 n_opencmd(cap);
7615}
7616
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617#ifdef FEAT_NETBEANS_INTG
7618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007619nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
7621 netbeans_keycommand(cap->nchar);
7622}
7623#endif
7624
7625#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007627nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007629 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630}
7631#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007632
Bram Moolenaar3918c952005-03-15 22:34:55 +00007633/*
7634 * Trigger CursorHold event.
7635 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7636 * input buffer. "did_cursorhold" is set to avoid retriggering.
7637 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007638 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007639nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007640{
7641 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7642 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007643 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007644}