blob: 0e1e11801c7e420b71df934e8e55b486e5c77fdb [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar792cf5e2019-09-30 23:12:16 +020017static int VIsual_mode_orig = NUL; // saved Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010019#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010021#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020022static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010023static void unshift_special(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010025static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#endif
27
28/*
29 * nv_*(): functions called to handle Normal and Visual mode commands.
30 * n_*(): functions called to handle Normal mode commands.
31 * v_*(): functions called to handle Visual mode commands.
32 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010033static void nv_ignore(cmdarg_T *cap);
34static void nv_nop(cmdarg_T *cap);
35static void nv_error(cmdarg_T *cap);
36static void nv_help(cmdarg_T *cap);
37static void nv_addsub(cmdarg_T *cap);
38static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010041static void nv_ver_scrollbar(cmdarg_T *cap);
42static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000044#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void nv_tabline(cmdarg_T *cap);
46static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000047#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void nv_exmode(cmdarg_T *cap);
49static void nv_colon(cmdarg_T *cap);
50static void nv_ctrlg(cmdarg_T *cap);
51static void nv_ctrlh(cmdarg_T *cap);
52static void nv_clear(cmdarg_T *cap);
53static void nv_ctrlo(cmdarg_T *cap);
54static void nv_hat(cmdarg_T *cap);
55static void nv_Zet(cmdarg_T *cap);
56static void nv_ident(cmdarg_T *cap);
57static void nv_tagpop(cmdarg_T *cap);
58static void nv_scroll(cmdarg_T *cap);
59static void nv_right(cmdarg_T *cap);
60static void nv_left(cmdarg_T *cap);
61static void nv_up(cmdarg_T *cap);
62static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010063static void nv_end(cmdarg_T *cap);
64static void nv_dollar(cmdarg_T *cap);
65static void nv_search(cmdarg_T *cap);
66static void nv_next(cmdarg_T *cap);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +020067static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt, int *wrapped);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010068static void nv_csearch(cmdarg_T *cap);
69static void nv_brackets(cmdarg_T *cap);
70static void nv_percent(cmdarg_T *cap);
71static void nv_brace(cmdarg_T *cap);
72static void nv_mark(cmdarg_T *cap);
73static void nv_findpar(cmdarg_T *cap);
74static void nv_undo(cmdarg_T *cap);
75static void nv_kundo(cmdarg_T *cap);
76static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010077static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010078static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
79static void v_visop(cmdarg_T *cap);
80static void nv_subst(cmdarg_T *cap);
81static void nv_abbrev(cmdarg_T *cap);
82static void nv_optrans(cmdarg_T *cap);
83static void nv_gomark(cmdarg_T *cap);
84static void nv_pcmark(cmdarg_T *cap);
85static void nv_regname(cmdarg_T *cap);
86static void nv_visual(cmdarg_T *cap);
87static void n_start_visual_mode(int c);
88static void nv_window(cmdarg_T *cap);
89static void nv_suspend(cmdarg_T *cap);
90static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010091static void nv_dot(cmdarg_T *cap);
92static void nv_redo(cmdarg_T *cap);
93static void nv_Undo(cmdarg_T *cap);
94static void nv_tilde(cmdarg_T *cap);
95static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000096#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010097static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +000098#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010099static void nv_lineop(cmdarg_T *cap);
100static void nv_home(cmdarg_T *cap);
101static void nv_pipe(cmdarg_T *cap);
102static void nv_bck_word(cmdarg_T *cap);
103static void nv_wordcmd(cmdarg_T *cap);
104static void nv_beginline(cmdarg_T *cap);
105static void adjust_cursor(oparg_T *oap);
106static void adjust_for_sel(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100107static void nv_select(cmdarg_T *cap);
108static void nv_goto(cmdarg_T *cap);
109static void nv_normal(cmdarg_T *cap);
110static void nv_esc(cmdarg_T *oap);
111static void nv_edit(cmdarg_T *cap);
112static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100114static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100116static void nv_record(cmdarg_T *cap);
117static void nv_at(cmdarg_T *cap);
118static void nv_halfpage(cmdarg_T *cap);
119static void nv_join(cmdarg_T *cap);
120static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200121static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100127static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100129static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000131static char *e_noident = N_("E349: No identifier under cursor");
132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133/*
134 * Function to be called for a Normal or Visual mode command.
135 * The argument is a cmdarg_T.
136 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100137typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100139// Values for cmd_flags.
140#define NV_NCH 0x01 // may need to get a second char
141#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
142#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
143#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100145#define NV_SS 0x10 // may start selection
146#define NV_SSS 0x20 // may start selection with shift modifier
147#define NV_STS 0x40 // may stop selection without shift modif.
148#define NV_RL 0x80 // 'rightleft' modifies command
149#define NV_KEEPREG 0x100 // don't clear regname
150#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152/*
153 * Generally speaking, every Normal mode command should either clear any
154 * pending operator (with *clearop*()), or set the motion type variable
155 * oap->motion_type.
156 *
157 * When a cursor motion command is made, it is marked as being a character or
158 * line oriented motion. Then, if an operator is in effect, the operation
159 * becomes character or line oriented accordingly.
160 */
161
162/*
163 * This table contains one entry for every Normal or Visual mode command.
164 * The order doesn't matter, init_normal_cmds() will create a sorted index.
165 * It is faster when all keys from zero to '~' are present.
166 */
167static const struct nv_cmd
168{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100169 int cmd_char; // (first) command character
170 nv_func_T cmd_func; // function for this command
171 short_u cmd_flags; // NV_ flags
172 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173} nv_cmds[] =
174{
175 {NUL, nv_error, 0, 0},
176 {Ctrl_A, nv_addsub, 0, 0},
177 {Ctrl_B, nv_page, NV_STS, BACKWARD},
178 {Ctrl_C, nv_esc, 0, TRUE},
179 {Ctrl_D, nv_halfpage, 0, 0},
180 {Ctrl_E, nv_scroll_line, 0, TRUE},
181 {Ctrl_F, nv_page, NV_STS, FORWARD},
182 {Ctrl_G, nv_ctrlg, 0, 0},
183 {Ctrl_H, nv_ctrlh, 0, 0},
184 {Ctrl_I, nv_pcmark, 0, 0},
185 {NL, nv_down, 0, FALSE},
186 {Ctrl_K, nv_error, 0, 0},
187 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100188 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 {Ctrl_N, nv_down, NV_STS, FALSE},
190 {Ctrl_O, nv_ctrlo, 0, 0},
191 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000192 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 {Ctrl_R, nv_redo, 0, 0},
194 {Ctrl_S, nv_ignore, 0, 0},
195 {Ctrl_T, nv_tagpop, NV_NCW, 0},
196 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 {Ctrl_V, nv_visual, 0, FALSE},
198 {'V', nv_visual, 0, FALSE},
199 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 {Ctrl_W, nv_window, 0, 0},
201 {Ctrl_X, nv_addsub, 0, 0},
202 {Ctrl_Y, nv_scroll_line, 0, FALSE},
203 {Ctrl_Z, nv_suspend, 0, 0},
204 {ESC, nv_esc, 0, FALSE},
205 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
206 {Ctrl_RSB, nv_ident, NV_NCW, 0},
207 {Ctrl_HAT, nv_hat, NV_NCW, 0},
208 {Ctrl__, nv_error, 0, 0},
209 {' ', nv_right, 0, 0},
210 {'!', nv_operator, 0, 0},
211 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
212 {'#', nv_ident, 0, 0},
213 {'$', nv_dollar, 0, 0},
214 {'%', nv_percent, 0, 0},
215 {'&', nv_optrans, 0, 0},
216 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
217 {'(', nv_brace, 0, BACKWARD},
218 {')', nv_brace, 0, FORWARD},
219 {'*', nv_ident, 0, 0},
220 {'+', nv_down, 0, TRUE},
221 {',', nv_csearch, 0, TRUE},
222 {'-', nv_up, 0, TRUE},
223 {'.', nv_dot, NV_KEEPREG, 0},
224 {'/', nv_search, 0, FALSE},
225 {'0', nv_beginline, 0, 0},
226 {'1', nv_ignore, 0, 0},
227 {'2', nv_ignore, 0, 0},
228 {'3', nv_ignore, 0, 0},
229 {'4', nv_ignore, 0, 0},
230 {'5', nv_ignore, 0, 0},
231 {'6', nv_ignore, 0, 0},
232 {'7', nv_ignore, 0, 0},
233 {'8', nv_ignore, 0, 0},
234 {'9', nv_ignore, 0, 0},
235 {':', nv_colon, 0, 0},
236 {';', nv_csearch, 0, FALSE},
237 {'<', nv_operator, NV_RL, 0},
238 {'=', nv_operator, 0, 0},
239 {'>', nv_operator, NV_RL, 0},
240 {'?', nv_search, 0, FALSE},
241 {'@', nv_at, NV_NCH_NOP, FALSE},
242 {'A', nv_edit, 0, 0},
243 {'B', nv_bck_word, 0, 1},
244 {'C', nv_abbrev, NV_KEEPREG, 0},
245 {'D', nv_abbrev, NV_KEEPREG, 0},
246 {'E', nv_wordcmd, 0, TRUE},
247 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
248 {'G', nv_goto, 0, TRUE},
249 {'H', nv_scroll, 0, 0},
250 {'I', nv_edit, 0, 0},
251 {'J', nv_join, 0, 0},
252 {'K', nv_ident, 0, 0},
253 {'L', nv_scroll, 0, 0},
254 {'M', nv_scroll, 0, 0},
255 {'N', nv_next, 0, SEARCH_REV},
256 {'O', nv_open, 0, 0},
257 {'P', nv_put, 0, 0},
258 {'Q', nv_exmode, NV_NCW, 0},
259 {'R', nv_Replace, 0, FALSE},
260 {'S', nv_subst, NV_KEEPREG, 0},
261 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
262 {'U', nv_Undo, 0, 0},
263 {'W', nv_wordcmd, 0, TRUE},
264 {'X', nv_abbrev, NV_KEEPREG, 0},
265 {'Y', nv_abbrev, NV_KEEPREG, 0},
266 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
267 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
268 {'\\', nv_error, 0, 0},
269 {']', nv_brackets, NV_NCH_ALW, FORWARD},
270 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
271 {'_', nv_lineop, 0, 0},
272 {'`', nv_gomark, NV_NCH_ALW, FALSE},
273 {'a', nv_edit, NV_NCH, 0},
274 {'b', nv_bck_word, 0, 0},
275 {'c', nv_operator, 0, 0},
276 {'d', nv_operator, 0, 0},
277 {'e', nv_wordcmd, 0, FALSE},
278 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
279 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
280 {'h', nv_left, NV_RL, 0},
281 {'i', nv_edit, NV_NCH, 0},
282 {'j', nv_down, 0, FALSE},
283 {'k', nv_up, 0, FALSE},
284 {'l', nv_right, NV_RL, 0},
285 {'m', nv_mark, NV_NCH_NOP, 0},
286 {'n', nv_next, 0, 0},
287 {'o', nv_open, 0, 0},
288 {'p', nv_put, 0, 0},
289 {'q', nv_record, NV_NCH, 0},
290 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
291 {'s', nv_subst, NV_KEEPREG, 0},
292 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
293 {'u', nv_undo, 0, 0},
294 {'w', nv_wordcmd, 0, FALSE},
295 {'x', nv_abbrev, NV_KEEPREG, 0},
296 {'y', nv_operator, 0, 0},
297 {'z', nv_zet, NV_NCH_ALW, 0},
298 {'{', nv_findpar, 0, BACKWARD},
299 {'|', nv_pipe, 0, 0},
300 {'}', nv_findpar, 0, FORWARD},
301 {'~', nv_tilde, 0, 0},
302
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100303 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200305 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
306 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
307 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
308 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 {K_LEFTMOUSE, nv_mouse, 0, 0},
310 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
311 {K_LEFTDRAG, nv_mouse, 0, 0},
312 {K_LEFTRELEASE, nv_mouse, 0, 0},
313 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100314 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
316 {K_MIDDLEDRAG, nv_mouse, 0, 0},
317 {K_MIDDLERELEASE, nv_mouse, 0, 0},
318 {K_RIGHTMOUSE, nv_mouse, 0, 0},
319 {K_RIGHTDRAG, nv_mouse, 0, 0},
320 {K_RIGHTRELEASE, nv_mouse, 0, 0},
321 {K_X1MOUSE, nv_mouse, 0, 0},
322 {K_X1DRAG, nv_mouse, 0, 0},
323 {K_X1RELEASE, nv_mouse, 0, 0},
324 {K_X2MOUSE, nv_mouse, 0, 0},
325 {K_X2DRAG, nv_mouse, 0, 0},
326 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000327 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000328 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {K_INS, nv_edit, 0, 0},
330 {K_KINS, nv_edit, 0, 0},
331 {K_BS, nv_ctrlh, 0, 0},
332 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
333 {K_S_UP, nv_page, NV_SS, BACKWARD},
334 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
335 {K_S_DOWN, nv_page, NV_SS, FORWARD},
336 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
337 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
338 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
339 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
340 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
341 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
342 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
343 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
344 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
345 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
346 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
347 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 {K_S_END, nv_end, NV_SS, FALSE},
349 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
350 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
351 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 {K_S_HOME, nv_home, NV_SS, 0},
353 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
354 {K_DEL, nv_abbrev, 0, 0},
355 {K_KDEL, nv_abbrev, 0, 0},
356 {K_UNDO, nv_kundo, 0, 0},
357 {K_HELP, nv_help, NV_NCW, 0},
358 {K_F1, nv_help, NV_NCW, 0},
359 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifdef FEAT_GUI
362 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
363 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
364#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000365#ifdef FEAT_GUI_TABLINE
366 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000367 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369#ifdef FEAT_NETBEANS_INTG
370 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
371#endif
372#ifdef FEAT_DND
373 {K_DROP, nv_drop, NV_STS, 0},
374#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000375 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100376 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100377 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378};
379
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100380// Number of commands in nv_cmds[].
K.Takataeeec2542021-06-02 13:28:16 +0200381#define NV_CMDS_SIZE ARRAY_LENGTH(nv_cmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static short nv_cmd_idx[NV_CMDS_SIZE];
385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386// The highest index for which
387// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static int nv_max_linear;
389
390/*
391 * Compare functions for qsort() below, that checks the command character
392 * through the index in nv_cmd_idx[].
393 */
394 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100395nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396{
397 int c1, c2;
398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100399 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 c1 = nv_cmds[*(const short *)s1].cmd_char;
401 c2 = nv_cmds[*(const short *)s2].cmd_char;
402 if (c1 < 0)
403 c1 = -c1;
404 if (c2 < 0)
405 c2 = -c2;
406 return c1 - c2;
407}
408
409/*
410 * Initialize the nv_cmd_idx[] table.
411 */
412 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100413init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
415 int i;
416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100417 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000418 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 nv_cmd_idx[i] = i;
420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100421 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100424 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000425 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
427 break;
428 nv_max_linear = i - 1;
429}
430
431/*
432 * Search for a command in the commands table.
433 * Returns -1 for invalid command.
434 */
435 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 int i;
439 int idx;
440 int top, bot;
441 int c;
442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100443 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (cmdchar >= 0x100)
445 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100447 // We use the absolute value of the character. Special keys have a
448 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (cmdchar < 0)
450 cmdchar = -cmdchar;
451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // If the character is in the first part: The character is the index into
453 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (cmdchar <= nv_max_linear)
455 return nv_cmd_idx[cmdchar];
456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100457 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 bot = nv_max_linear + 1;
459 top = NV_CMDS_SIZE - 1;
460 idx = -1;
461 while (bot <= top)
462 {
463 i = (top + bot) / 2;
464 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
465 if (c < 0)
466 c = -c;
467 if (cmdchar == c)
468 {
469 idx = nv_cmd_idx[i];
470 break;
471 }
472 if (cmdchar > c)
473 bot = i + 1;
474 else
475 top = i - 1;
476 }
477 return idx;
478}
479
480/*
481 * Execute a command in Normal mode.
482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100484normal_cmd(
485 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100486 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100490 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 int old_col = curwin->w_curswant;
492#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100493 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100495 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 static int old_mapped_len = 0;
498 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000499#ifdef FEAT_EVAL
500 int set_prevcount = FALSE;
501#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200502 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
Bram Moolenaara80faa82020-04-12 19:37:17 +0200504 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000506
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100507 // Use a count remembered from before entering an operator. After typing
508 // "3d" we return from normal_cmd() and come back here, the "3" is
509 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 ca.opcount = opcount;
511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 /*
513 * If there is an operator pending, then the command we take this time
514 * will terminate it. Finish_op tells us to finish the operation before
515 * returning this time (unless the operation was cancelled).
516 */
517#ifdef CURSOR_SHAPE
518 c = finish_op;
519#endif
520 finish_op = (oap->op_type != OP_NOP);
521#ifdef CURSOR_SHAPE
522 if (finish_op != c)
523 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100524 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525# ifdef FEAT_MOUSESHAPE
526 update_mouseshape(-1);
527# endif
528 }
529#endif
530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100531 // When not finishing an operator and no register name typed, reset the
532 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000536#ifdef FEAT_EVAL
537 set_prevcount = TRUE;
538#endif
539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100541 // Restore counts from before receiving K_CURSORHOLD. This means after
542 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
543 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000544 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
545 {
546 ca.opcount = oap->prev_opcount;
547 ca.count0 = oap->prev_count0;
548 oap->prev_opcount = 0;
549 oap->prev_count0 = 0;
550 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000551
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
554 State = NORMAL_BUSY;
555#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100556 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100559#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100560 // Set v:count here, when called from main() and not a stuffed
561 // command, so that v:count can be used in an expression mapping
562 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100563 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100564 set_vcount_ca(&ca, &set_prevcount);
565#endif
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 /*
568 * Get the command character from the user.
569 */
570 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100571 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
573 /*
574 * If a mapping was started in Visual or Select mode, remember the length
575 * of the mapping. This is used below to not return to Insert mode for as
576 * long as the mapping is being executed.
577 */
578 if (restart_edit == 0)
579 old_mapped_len = 0;
580 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000581 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 old_mapped_len = typebuf_maplen();
583
584 if (c == NUL)
585 c = K_ZERO;
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 /*
588 * In Select mode, typed text replaces the selection.
589 */
590 if (VIsual_active
591 && VIsual_select
592 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
593 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100594 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
595 // 'insertmode' is set) fake a "d"elete command, Insert mode will
596 // restart automatically.
597 // Insert the typed character in the typeahead buffer, so that it can
598 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200599 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000600 if (restart_edit != 0)
601 c = 'd';
602 else
603 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100604 msg_nowait = TRUE; // don't delay going to insert mode
605 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608#ifdef FEAT_CMDL_INFO
609 need_flushbuf = add_to_showcmd(c);
610#endif
611
612getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
615 /*
616 * Handle a count before a command and compute ca.count0.
617 * Note that '0' is a command and not the start of a count, but it's
618 * part of a count after other digits.
619 */
620 while ( (c >= '1' && c <= '9')
621 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
622 {
623 if (c == K_DEL || c == K_KDEL)
624 {
625 ca.count0 /= 10;
626#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100627 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#endif
629 }
630 else
631 ca.count0 = ca.count0 * 10 + (c - '0');
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100632 if (ca.count0 < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000634#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100635 // Set v:count here, when called from main() and not a stuffed
636 // command, so that v:count can be used in an expression mapping
637 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100638 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100639 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (ctrl_w)
642 {
643 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100644 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100646 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000647 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 --no_zero_mapping;
650 if (ctrl_w)
651 {
652 --no_mapping;
653 --allow_keys;
654 }
655#ifdef FEAT_CMDL_INFO
656 need_flushbuf |= add_to_showcmd(c);
657#endif
658 }
659
660 /*
661 * If we got CTRL-W there may be a/another count
662 */
663 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
664 {
665 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100666 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 ca.count0 = 0;
668 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100669 ++allow_keys; // no mapping for nchar, but keys
670 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 --no_mapping;
673 --allow_keys;
674#ifdef FEAT_CMDL_INFO
675 need_flushbuf |= add_to_showcmd(c);
676#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100677 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679 }
680
Bram Moolenaara983fe92008-07-31 20:04:27 +0000681 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100683 // Save the count values so that ca.opcount and ca.count0 are exactly
684 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000685 oap->prev_opcount = ca.opcount;
686 oap->prev_count0 = ca.count0;
687 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100688 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000689 {
690 /*
691 * If we're in the middle of an operator (including after entering a
692 * yank buffer with '"') AND we had a count before the operator, then
693 * that count overrides the current value of ca.count0.
694 * What this means effectively, is that commands like "3dw" get turned
695 * into "d3w" which makes things fall into place pretty neatly.
696 * If you give a count before AND after the operator, they are
697 * multiplied.
698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (ca.count0)
700 ca.count0 *= ca.opcount;
701 else
702 ca.count0 = ca.opcount;
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100703 if (ca.count0 < 0) // overflow
704 ca.count0 = 999999999L;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 }
706
707 /*
708 * Always remember the count. It will be set to zero (on the next call,
709 * above) when there is no pending operator.
710 * When called from main(), save the count for use by the "count" built-in
711 * variable.
712 */
713 ca.opcount = ca.count0;
714 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
715
716#ifdef FEAT_EVAL
717 /*
718 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100719 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100721 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000722 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#endif
724
725 /*
726 * Find the command character in the table of commands.
727 * For CTRL-W we already got nchar when looking for a count.
728 */
729 if (ctrl_w)
730 {
731 ca.nchar = c;
732 ca.cmdchar = Ctrl_W;
733 }
734 else
735 ca.cmdchar = c;
736 idx = find_command(ca.cmdchar);
737 if (idx < 0)
738 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100739 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 clearopbeep(oap);
741 goto normal_end;
742 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000743
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000744 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100746 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000748 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 goto normal_end;
750 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000751 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
752 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 /*
755 * In Visual/Select mode, a few keys are handled in a special way.
756 */
757 if (VIsual_active)
758 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100759 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (km_stopsel
761 && (nv_cmds[idx].cmd_flags & NV_STS)
762 && !(mod_mask & MOD_MASK_SHIFT))
763 {
764 end_visual_mode();
765 redraw_curbuf_later(INVERTED);
766 }
767
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100768 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (km_startsel)
770 {
771 if (nv_cmds[idx].cmd_flags & NV_SS)
772 {
773 unshift_special(&ca);
774 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000775 if (idx < 0)
776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100777 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000778 clearopbeep(oap);
779 goto normal_end;
780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
783 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788#ifdef FEAT_RIGHTLEFT
789 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
790 && (nv_cmds[idx].cmd_flags & NV_RL))
791 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100792 // Invert horizontal movements and operations. Only when typed by the
793 // user directly, not when the result of a mapping or "x" translated
794 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 switch (ca.cmdchar)
796 {
797 case 'l': ca.cmdchar = 'h'; break;
798 case K_RIGHT: ca.cmdchar = K_LEFT; break;
799 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
800 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
801 case 'h': ca.cmdchar = 'l'; break;
802 case K_LEFT: ca.cmdchar = K_RIGHT; break;
803 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
804 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
805 case '>': ca.cmdchar = '<'; break;
806 case '<': ca.cmdchar = '>'; break;
807 }
808 idx = find_command(ca.cmdchar);
809 }
810#endif
811
812 /*
813 * Get an additional character if we need one.
814 */
815 if ((nv_cmds[idx].cmd_flags & NV_NCH)
816 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
817 && oap->op_type == OP_NOP)
818 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
819 || (ca.cmdchar == 'q'
820 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200821 && reg_recording == 0
822 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100824 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 {
826 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100827 int repl = FALSE; // get character for replace mode
828 int lit = FALSE; // get extra character literally
829 int langmap_active = FALSE; // using :lmap mappings
830 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100831#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100832 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833#endif
834
835 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100836 ++allow_keys; // no mapping for nchar, but allow key codes
837 // Don't generate a CursorHold event here, most commands can't handle
838 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000839 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (ca.cmdchar == 'g')
841 {
842 /*
843 * For 'g' get the next character now, so that we can check for
844 * "gr", "g'" and "g`".
845 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000846 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#ifdef FEAT_CMDL_INFO
849 need_flushbuf |= add_to_showcmd(ca.nchar);
850#endif
851 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100852 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100854 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100856 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100858 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100861 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863 else
864 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100865 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 repl = TRUE;
867 cp = &ca.nchar;
868 }
869 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
870
871 /*
872 * Get a second or third character.
873 */
874 if (cp != NULL)
875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (repl)
877 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100879#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100880 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
884 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100885 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 --no_mapping;
887 --allow_keys;
888 if (repl)
889 State = LREPLACE;
890 else
891 State = LANGMAP;
892 langmap_active = TRUE;
893 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100894#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100896 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
898 im_set_active(TRUE);
899#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200900 if ((State & INSERT) && !p_ek)
901 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200902#ifdef FEAT_JOB_CHANNEL
903 ch_log_output = TRUE;
904#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200905 // Disable bracketed paste and modifyOtherKeys here, we won't
906 // recognize the escape sequences with 'esckeys' off.
907 out_str(T_BD);
908 out_str(T_CTE);
909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000911 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaarca774f62020-08-30 20:46:38 +0200913 if ((State & INSERT) && !p_ek)
914 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200915#ifdef FEAT_JOB_CHANNEL
916 ch_log_output = TRUE;
917#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200918 // Re-enable bracketed paste mode and modifyOtherKeys
919 out_str(T_BE);
920 out_str(T_CTI);
921 }
922
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 if (langmap_active)
924 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100925 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 ++no_mapping;
927 ++allow_keys;
928 State = NORMAL_BUSY;
929 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100930#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 if (lang)
932 {
933 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
934 im_save_status(&curbuf->b_p_iminsert);
935 im_set_active(FALSE);
936 }
937 p_smd = save_smd;
938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940#ifdef FEAT_CMDL_INFO
941 need_flushbuf |= add_to_showcmd(*cp);
942#endif
943
944 if (!lit)
945 {
946#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100947 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (*cp == Ctrl_K
949 && ((nv_cmds[idx].cmd_flags & NV_LANG)
950 || cp == &ca.extra_char)
951 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
952 {
953 c = get_digraph(FALSE);
954 if (c > 0)
955 {
956 *cp = c;
957# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100958 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 del_from_showcmd(3);
960 need_flushbuf |= add_to_showcmd(*cp);
961# endif
962 }
963 }
964#endif
965
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100966 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100969 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (p_hkmap && lang && KeyTyped)
971 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972#endif
973 }
974
975 /*
976 * When the next character is CTRL-\ a following CTRL-N means the
977 * command is aborted and we go to Normal mode.
978 */
979 if (cp == &ca.extra_char
980 && ca.nchar == Ctrl_BSL
981 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
982 {
983 ca.cmdchar = Ctrl_BSL;
984 ca.nchar = ca.extra_char;
985 idx = find_command(ca.cmdchar);
986 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200987 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200988 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 else if (*cp == Ctrl_BSL)
990 {
991 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100993 // There is a busy wait here when typing "f<C-\>" and then
994 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 while ((c = vpeekc()) <= 0 && towait > 0L)
996 {
Bram Moolenaare2edc2e2021-01-16 20:21:23 +0100997 do_sleep(towait > 50L ? 50L : towait, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 towait -= 50L;
999 }
1000 if (c > 0)
1001 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001002 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 if (c != Ctrl_N && c != Ctrl_G)
1004 vungetc(c);
1005 else
1006 {
1007 ca.cmdchar = Ctrl_BSL;
1008 ca.nchar = c;
1009 idx = find_command(ca.cmdchar);
1010 }
1011 }
1012 }
1013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 // When getting a text character and the next character is a
1015 // multi-byte character, it could be a composing character.
1016 // However, don't wait for it to arrive. Also, do enable mapping,
1017 // because if it's put back with vungetc() it's too late to apply
1018 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001019 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 while (enc_utf8 && lang && (c = vpeekc()) > 0
1021 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1022 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001023 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (!utf_iscomposing(c))
1025 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001026 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 break;
1028 }
1029 else if (ca.ncharC1 == 0)
1030 ca.ncharC1 = c;
1031 else
1032 ca.ncharC2 = c;
1033 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001034 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
1036 --no_mapping;
1037 --allow_keys;
1038 }
1039
1040#ifdef FEAT_CMDL_INFO
1041 /*
1042 * Flush the showcmd characters onto the screen so we can see them while
1043 * the command is being executed. Only do this when the shown command was
1044 * actually displayed, otherwise this will slow down a lot when executing
1045 * mappings.
1046 */
1047 if (need_flushbuf)
1048 out_flush();
1049#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001050 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001051 {
1052 if (ex_normal_busy)
1053 did_cursorhold = save_did_cursorhold;
1054 else
1055 did_cursorhold = FALSE;
1056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
1058 State = NORMAL;
1059
1060 if (ca.nchar == ESC)
1061 {
1062 clearop(oap);
1063 if (restart_edit == 0 && goto_im())
1064 restart_edit = 'a';
1065 goto normal_end;
1066 }
1067
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001068 if (ca.cmdchar != K_IGNORE)
1069 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001070 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001071 msg_col = 0;
1072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001074 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001076 // When 'keymodel' contains "startsel" some keys start Select/Visual
1077 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (!VIsual_active && km_startsel)
1079 {
1080 if (nv_cmds[idx].cmd_flags & NV_SS)
1081 {
1082 start_selection();
1083 unshift_special(&ca);
1084 idx = find_command(ca.cmdchar);
1085 }
1086 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1087 && (mod_mask & MOD_MASK_SHIFT))
1088 {
1089 start_selection();
1090 mod_mask &= ~MOD_MASK_SHIFT;
1091 }
1092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
1094 /*
1095 * Execute the command!
1096 * Call the command function found in the commands table.
1097 */
1098 ca.arg = nv_cmds[idx].cmd_arg;
1099 (nv_cmds[idx].cmd_func)(&ca);
1100
1101 /*
1102 * If we didn't start or finish an operator, reset oap->regname, unless we
1103 * need it later.
1104 */
1105 if (!finish_op
1106 && !oap->op_type
1107 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1108 {
1109 clearop(oap);
1110#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001111 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112#endif
1113 }
1114
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001115 // Get the length of mapped chars again after typing a count, second
1116 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001117 if (old_mapped_len > 0)
1118 old_mapped_len = typebuf_maplen();
1119
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 /*
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001121 * If an operation is pending, handle it. But not for K_IGNORE or
1122 * K_MOUSEMOVE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 */
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001124 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001125 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
1127 /*
1128 * Wait for a moment when a message is displayed that will be overwritten
1129 * by the mode message.
1130 * In Visual mode and with "^O" in Insert mode, a short message will be
1131 * overwritten by the mode message. Wait a bit, until a key is hit.
1132 * In Visual mode, it's more important to keep the Visual area updated
1133 * than keeping a message (e.g. from a /pat search).
1134 * Only do this if the command was typed, not from a mapping.
1135 * Don't wait when emsg_silent is non-zero.
1136 * Also wait a bit after an error message, e.g. for "^O:".
1137 * Don't redraw the screen, it would remove the message.
1138 */
1139 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001140 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 || (VIsual_active
1143 && old_pos.lnum == curwin->w_cursor.lnum
1144 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 )
1146 && (clear_cmdline
1147 || redraw_cmdline)
1148 && (msg_didout || (msg_didany && msg_scroll))
1149 && !msg_nowait
1150 && KeyTyped)
1151 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 && (msg_scroll
1154 || emsg_on_display)))
1155 && oap->regname == 0
1156 && !(ca.retval & CA_COMMAND_BUSY)
1157 && stuff_empty()
1158 && typebuf_typed()
1159 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001160 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 && !did_wait_return
1162 && oap->op_type == OP_NOP)
1163 {
1164 int save_State = State;
1165
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001166 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (restart_edit != 0)
1168 State = INSERT;
1169
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001170 // If need to redraw, and there is a "keep_msg", redraw before the
1171 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1173 {
1174 char_u *kmsg;
1175
1176 kmsg = keep_msg;
1177 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001178 // Showmode() will clear keep_msg, but we want to use it anyway.
1179 // First update w_topline.
1180 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001182 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001184
1185 kmsg = vim_strsave(keep_msg);
1186 if (kmsg != NULL)
1187 {
1188 msg_attr((char *)kmsg, keep_msg_attr);
1189 vim_free(kmsg);
1190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001193#ifdef CURSOR_SHAPE
1194 ui_cursor_shape(); // may show different cursor shape
1195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 cursor_on();
1197 out_flush();
1198 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001199 ui_delay(1003L, TRUE); // wait at least one second
1200 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 State = save_State;
1202
1203 msg_scroll = FALSE;
1204 emsg_on_display = FALSE;
1205 }
1206
1207 /*
1208 * Finish up after executing a Normal mode command.
1209 */
1210normal_end:
1211
1212 msg_nowait = FALSE;
1213
Bram Moolenaarcc613032020-06-07 21:31:18 +02001214#ifdef FEAT_EVAL
1215 if (finish_op)
1216 reset_reg_var();
1217#endif
1218
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001219 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef CURSOR_SHAPE
1221 c = finish_op;
1222#endif
1223 finish_op = FALSE;
1224#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001225 // Redraw the cursor with another shape, if we were in Operator-pending
1226 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (c || ca.cmdchar == 'r')
1228 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001229 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230# ifdef FEAT_MOUSESHAPE
1231 update_mouseshape(-1);
1232# endif
1233 }
1234#endif
1235
1236#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001237 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001238 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 clear_showcmd();
1240#endif
1241
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001242 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 vim_free(ca.searchbuf);
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (has_mbyte)
1246 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 if (curwin->w_p_scb && toplevel)
1249 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001250 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 do_check_scrollbind(TRUE);
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Bram Moolenaar860cae12010-06-05 23:22:07 +02001254 if (curwin->w_p_crb && toplevel)
1255 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001256 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001257 do_check_cursorbind();
1258 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001259
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001260#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001261 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001262 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001263 restart_edit = 0;
1264#endif
1265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 /*
1267 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1268 * if still inside a mapping that started in Visual mode).
1269 * May switch from Visual to Select mode after CTRL-O command.
1270 */
1271 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1273 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 && !(ca.retval & CA_COMMAND_BUSY)
1275 && stuff_empty()
1276 && oap->regname == 0)
1277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (restart_VIsual_select == 1)
1279 {
1280 VIsual_select = TRUE;
1281 showmode();
1282 restart_VIsual_select = 0;
1283 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001284 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 (void)edit(restart_edit, FALSE, 1L);
1286 }
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (restart_VIsual_select == 2)
1289 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001291 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 opcount = ca.opcount;
1293}
1294
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001295#ifdef FEAT_EVAL
1296/*
1297 * Set v:count and v:count1 according to "cap".
1298 * Set v:prevcount only when "set_prevcount" is TRUE.
1299 */
1300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001301set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001302{
1303 long count = cap->count0;
1304
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001305 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001306 if (cap->opcount != 0)
1307 count = cap->opcount * (count == 0 ? 1 : count);
1308 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001309 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001310}
1311#endif
1312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001314 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 * if not.
1316 */
1317 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001318check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 static int did_check = FALSE;
1321
1322 if (full_screen)
1323 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001324 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001325 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 did_check = TRUE;
1327 }
1328}
1329
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001330#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1331/*
1332 * Call yank_do_autocmd() for "regname".
1333 */
1334 static void
1335call_yank_do_autocmd(int regname)
1336{
1337 oparg_T oa;
1338 yankreg_T *reg;
1339
1340 clear_oparg(&oa);
1341 oa.regname = regname;
1342 oa.op_type = OP_YANK;
1343 oa.is_VIsual = TRUE;
1344 reg = get_register(regname, TRUE);
1345 yank_do_autocmd(&oa, reg);
1346 free_register(reg);
1347}
1348#endif
1349
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001351 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001352 * This function or the next should ALWAYS be called to end Visual mode, except
1353 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 */
1355 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001356end_visual_mode()
1357{
1358 end_visual_mode_keep_button();
1359 reset_held_button();
1360}
1361
1362 void
1363end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365#ifdef FEAT_CLIPBOARD
1366 /*
1367 * If we are using the clipboard, then remember what was selected in case
1368 * we need to paste it somewhere while we still own the selection.
1369 * Only do this when the clipboard is already owned. Don't want to grab
1370 * the selection when hitting ESC.
1371 */
1372 if (clip_star.available && clip_star.owned)
1373 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001374
1375# if defined(FEAT_EVAL)
1376 // Emit a TextYankPost for the automatic copy of the selection into the
1377 // star and/or plus register.
1378 if (has_textyankpost())
1379 {
1380 if (clip_isautosel_star())
1381 call_yank_do_autocmd('*');
1382 if (clip_isautosel_plus())
1383 call_yank_do_autocmd('+');
1384 }
1385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#endif
1387
1388 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 setmouse();
1390 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001392 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001393 curbuf->b_visual.vi_mode = VIsual_mode;
1394 curbuf->b_visual.vi_start = VIsual;
1395 curbuf->b_visual.vi_end = curwin->w_cursor;
1396 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#ifdef FEAT_EVAL
1398 curbuf->b_visual_mode_eval = VIsual_mode;
1399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (!virtual_active())
1401 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001402 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001404 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405}
1406
1407/*
1408 * Reset VIsual_active and VIsual_reselect.
1409 */
1410 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001411reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
1413 if (VIsual_active)
1414 {
1415 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001416 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 VIsual_reselect = FALSE;
1419}
1420
1421/*
1422 * Reset VIsual_active and VIsual_reselect if it's set.
1423 */
1424 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001425reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
1427 if (VIsual_active)
1428 {
1429 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001430 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 VIsual_reselect = FALSE;
1432 }
1433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001435 void
1436restore_visual_mode(void)
1437{
1438 if (VIsual_mode_orig != NUL)
1439 {
1440 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1441 VIsual_mode_orig = NUL;
1442 }
1443}
1444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445/*
1446 * Check for a balloon-eval special item to include when searching for an
1447 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1448 * Returns TRUE if the character at "*ptr" should be included.
1449 * "dir" is FORWARD or BACKWARD, the direction of searching.
1450 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1451 * "bnp" points to a counter for square brackets.
1452 */
1453 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001454find_is_eval_item(
1455 char_u *ptr,
1456 int *colp,
1457 int *bnp,
1458 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001460 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1462 ++*bnp;
1463 if (*bnp > 0)
1464 {
1465 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1466 --*bnp;
1467 return TRUE;
1468 }
1469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001470 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (*ptr == '.')
1472 return TRUE;
1473
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001474 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1476 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1477 {
1478 *colp += dir;
1479 return TRUE;
1480 }
1481 return FALSE;
1482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484/*
1485 * Find the identifier under or to the right of the cursor.
1486 * "find_type" can have one of three values:
1487 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001488 * FIND_STRING: find any non-white text
1489 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001490 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 *
1492 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001493 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001495 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 * This doesn't match the real Vi but I like it a little better and it
1497 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001498 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 * When FIND_IDENT isn't defined, we backup until a blank.
1500 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001501 * Returns the length of the text, or zero if no text is found.
1502 * If text is found, a pointer to the text is put in "*text". This
1503 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 */
1505 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001506find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
1508 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001509 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510}
1511
1512/*
1513 * Like find_ident_under_cursor(), but for any window and any position.
1514 * However: Uses 'iskeyword' from the current window!.
1515 */
1516 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001517find_ident_at_pos(
1518 win_T *wp,
1519 linenr_T lnum,
1520 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001521 char_u **text,
1522 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001523 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
1525 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001526 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 int this_class = 0;
1529 int prev_class;
1530 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001531 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
1533 /*
1534 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001535 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 */
1537 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1538 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1539 {
1540 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001541 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 */
1543 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 if (has_mbyte)
1545 {
1546 while (ptr[col] != NUL)
1547 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001548 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 this_class = mb_get_class(ptr + col);
1552 if (this_class != 0 && (i == 1 || this_class != 1))
1553 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001554 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 }
1556 }
1557 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001559 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 )
1562 ++col;
1563
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001564 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001568 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (has_mbyte)
1571 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001572 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1574 this_class = mb_get_class((char_u *)"a");
1575 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001577 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {
1579 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1580 prev_class = mb_get_class(ptr + prevcol);
1581 if (this_class != prev_class
1582 && (i == 0
1583 || prev_class == 0
1584 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 && (!(find_type & FIND_EVAL)
1586 || prevcol == 0
1587 || !find_is_eval_item(ptr + prevcol, &prevcol,
1588 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 )
1590 break;
1591 col = prevcol;
1592 }
1593
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001594 // If we don't want just any old text, or we've found an
1595 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (this_class > 2)
1597 this_class = 2;
1598 if (!(find_type & FIND_STRING) || this_class == 2)
1599 break;
1600 }
1601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 while (col > 0
1604 && ((i == 0
1605 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001606 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 && (!(find_type & FIND_IDENT)
1608 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 || ((find_type & FIND_EVAL)
1610 && col > 1
1611 && find_is_eval_item(ptr + col - 1, &col,
1612 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 ))
1614 --col;
1615
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001616 // If we don't want just any old text, or we've found an
1617 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1619 break;
1620 }
1621 }
1622
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001623 if (ptr[col] == NUL || (i == 0
1624 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001626 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001627 if ((find_type & FIND_NOERROR) == 0)
1628 {
1629 if (find_type & FIND_STRING)
1630 emsg(_("E348: No string under cursor"));
1631 else
1632 emsg(_(e_noident));
1633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 return 0;
1635 }
1636 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001637 *text = ptr;
1638 if (textcol != NULL)
1639 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001642 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 bn = 0;
1645 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (has_mbyte)
1648 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001649 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 this_class = mb_get_class(ptr);
1651 while (ptr[col] != NUL
1652 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1653 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 || ((find_type & FIND_EVAL)
1655 && col <= (int)startcol
1656 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001658 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
1660 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001662 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 || ((find_type & FIND_EVAL)
1664 && col <= (int)startcol
1665 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 return col;
1670}
1671
1672/*
1673 * Prepare for redo of a normal command.
1674 */
1675 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001676prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677{
1678 prep_redo(cap->oap->regname, cap->count0,
1679 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1680}
1681
1682/*
1683 * Prepare for redo of any command.
1684 * Note that only the last argument can be a multi-byte char.
1685 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001686 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001687prep_redo(
1688 int regname,
1689 long num,
1690 int cmd1,
1691 int cmd2,
1692 int cmd3,
1693 int cmd4,
1694 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001697 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 {
1699 AppendCharToRedobuff('"');
1700 AppendCharToRedobuff(regname);
1701 }
1702 if (num)
1703 AppendNumberToRedobuff(num);
1704
1705 if (cmd1 != NUL)
1706 AppendCharToRedobuff(cmd1);
1707 if (cmd2 != NUL)
1708 AppendCharToRedobuff(cmd2);
1709 if (cmd3 != NUL)
1710 AppendCharToRedobuff(cmd3);
1711 if (cmd4 != NUL)
1712 AppendCharToRedobuff(cmd4);
1713 if (cmd5 != NUL)
1714 AppendCharToRedobuff(cmd5);
1715}
1716
1717/*
1718 * check for operator active and clear it
1719 *
1720 * return TRUE if operator was active
1721 */
1722 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001723checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724{
1725 if (oap->op_type == OP_NOP)
1726 return FALSE;
1727 clearopbeep(oap);
1728 return TRUE;
1729}
1730
1731/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001732 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001734 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 */
1736 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001737checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001739 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 return FALSE;
1741 clearopbeep(oap);
1742 return TRUE;
1743}
1744
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001745 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001746clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 oap->op_type = OP_NOP;
1749 oap->regname = 0;
1750 oap->motion_force = NUL;
1751 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001752 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753}
1754
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001755 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001756clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 clearop(oap);
1759 beep_flush();
1760}
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762/*
1763 * Remove the shift modifier from a special key.
1764 */
1765 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001766unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767{
1768 switch (cap->cmdchar)
1769 {
1770 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1771 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1772 case K_S_UP: cap->cmdchar = K_UP; break;
1773 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1774 case K_S_HOME: cap->cmdchar = K_HOME; break;
1775 case K_S_END: cap->cmdchar = K_END; break;
1776 }
1777 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1778}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001780/*
1781 * If the mode is currently displayed clear the command line or update the
1782 * command displayed.
1783 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001784 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001785may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001786{
1787 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001788 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001789#ifdef FEAT_CMDL_INFO
1790 else
1791 clear_showcmd();
1792#endif
1793}
1794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1796/*
1797 * Routines for displaying a partly typed command
1798 */
1799
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001800#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001802static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803static int showcmd_is_clear = TRUE;
1804static int showcmd_visual = FALSE;
1805
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001806static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001809clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810{
1811 if (!p_sc)
1812 return;
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (VIsual_active && !char_avail())
1815 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001816 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 long lines;
1818 colnr_T leftcol, rightcol;
1819 linenr_T top, bot;
1820
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001821 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001822 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 top = VIsual.lnum;
1825 bot = curwin->w_cursor.lnum;
1826 }
1827 else
1828 {
1829 top = curwin->w_cursor.lnum;
1830 bot = VIsual.lnum;
1831 }
1832# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001833 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001834 (void)hasFolding(top, &top, NULL);
1835 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836# endif
1837 lines = bot - top + 1;
1838
1839 if (VIsual_mode == Ctrl_V)
1840 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001841# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001842 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001843 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001844
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001845 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001846 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001847 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001850# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001851 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001852 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1855 (long)(rightcol - leftcol + 1));
1856 }
1857 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1858 sprintf((char *)showcmd_buf, "%ld", lines);
1859 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001860 {
1861 char_u *s, *e;
1862 int l;
1863 int bytes = 0;
1864 int chars = 0;
1865
1866 if (cursor_bot)
1867 {
1868 s = ml_get_pos(&VIsual);
1869 e = ml_get_cursor();
1870 }
1871 else
1872 {
1873 s = ml_get_cursor();
1874 e = ml_get_pos(&VIsual);
1875 }
1876 while ((*p_sel != 'e') ? s <= e : s < e)
1877 {
1878 l = (*mb_ptr2len)(s);
1879 if (l == 0)
1880 {
1881 ++bytes;
1882 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001883 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001884 }
1885 bytes += l;
1886 ++chars;
1887 s += l;
1888 }
1889 if (bytes == chars)
1890 sprintf((char *)showcmd_buf, "%d", chars);
1891 else
1892 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1893 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001894 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 showcmd_visual = TRUE;
1896 }
1897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
1899 showcmd_buf[0] = NUL;
1900 showcmd_visual = FALSE;
1901
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001902 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (showcmd_is_clear)
1904 return;
1905 }
1906
1907 display_showcmd();
1908}
1909
1910/*
1911 * Add 'c' to string of shown command chars.
1912 * Return TRUE if output has been written (and setcursor() has been called).
1913 */
1914 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001915add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 char_u *p;
1918 int old_len;
1919 int extra_len;
1920 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 int i;
1922 static int ignore[] =
1923 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001924#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1926 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001927#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001928 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001929 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1931 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001932 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001934 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 0
1936 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
Bram Moolenaar09df3122006-01-23 22:23:09 +00001938 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 return FALSE;
1940
1941 if (showcmd_visual)
1942 {
1943 showcmd_buf[0] = NUL;
1944 showcmd_visual = FALSE;
1945 }
1946
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001947 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (IS_SPECIAL(c))
1949 for (i = 0; ignore[i] != 0; ++i)
1950 if (ignore[i] == c)
1951 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
1953 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001954 if (*p == ' ')
1955 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 old_len = (int)STRLEN(showcmd_buf);
1957 extra_len = (int)STRLEN(p);
1958 overflow = old_len + extra_len - SHOWCMD_COLS;
1959 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001960 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1961 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 STRCAT(showcmd_buf, p);
1963
1964 if (char_avail())
1965 return FALSE;
1966
1967 display_showcmd();
1968
1969 return TRUE;
1970}
1971
1972 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001973add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 if (!add_to_showcmd(c))
1976 setcursor();
1977}
1978
1979/*
1980 * Delete 'len' characters from the end of the shown command.
1981 */
1982 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001983del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984{
1985 int old_len;
1986
1987 if (!p_sc)
1988 return;
1989
1990 old_len = (int)STRLEN(showcmd_buf);
1991 if (len > old_len)
1992 len = old_len;
1993 showcmd_buf[old_len - len] = NUL;
1994
1995 if (!char_avail())
1996 display_showcmd();
1997}
1998
1999/*
2000 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2001 * something and there is a partial mapping.
2002 */
2003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002004push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 if (p_sc)
2007 STRCPY(old_showcmd_buf, showcmd_buf);
2008}
2009
2010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002011pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012{
2013 if (!p_sc)
2014 return;
2015
2016 STRCPY(showcmd_buf, old_showcmd_buf);
2017
2018 display_showcmd();
2019}
2020
2021 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002022display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
2024 int len;
2025
2026 cursor_off();
2027
2028 len = (int)STRLEN(showcmd_buf);
2029 if (len == 0)
2030 showcmd_is_clear = TRUE;
2031 else
2032 {
2033 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2034 showcmd_is_clear = FALSE;
2035 }
2036
2037 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002038 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2039 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 */
2041 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002043 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044}
2045#endif
2046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047/*
2048 * When "check" is FALSE, prepare for commands that scroll the window.
2049 * When "check" is TRUE, take care of scroll-binding after the window has
2050 * scrolled. Called from normal_cmd() and edit().
2051 */
2052 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
2055 static win_T *old_curwin = NULL;
2056 static linenr_T old_topline = 0;
2057#ifdef FEAT_DIFF
2058 static int old_topfill = 0;
2059#endif
2060 static buf_T *old_buf = NULL;
2061 static colnr_T old_leftcol = 0;
2062
2063 if (check && curwin->w_p_scb)
2064 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002065 // If a ":syncbind" command was just used, don't scroll, only reset
2066 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (did_syncbind)
2068 did_syncbind = FALSE;
2069 else if (curwin == old_curwin)
2070 {
2071 /*
2072 * Synchronize other windows, as necessary according to
2073 * 'scrollbind'. Don't do this after an ":edit" command, except
2074 * when 'diff' is set.
2075 */
2076 if ((curwin->w_buffer == old_buf
2077#ifdef FEAT_DIFF
2078 || curwin->w_p_diff
2079#endif
2080 )
2081 && (curwin->w_topline != old_topline
2082#ifdef FEAT_DIFF
2083 || curwin->w_topfill != old_topfill
2084#endif
2085 || curwin->w_leftcol != old_leftcol))
2086 {
2087 check_scrollbind(curwin->w_topline - old_topline,
2088 (long)(curwin->w_leftcol - old_leftcol));
2089 }
2090 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002091 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 /*
2094 * When switching between windows, make sure that the relative
2095 * vertical offset is valid for the new window. The relative
2096 * offset is invalid whenever another 'scrollbind' window has
2097 * scrolled to a point that would force the current window to
2098 * scroll past the beginning or end of its buffer. When the
2099 * resync is performed, some of the other 'scrollbind' windows may
2100 * need to jump so that the current window's relative position is
2101 * visible on-screen.
2102 */
2103 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2104 }
2105 curwin->w_scbind_pos = curwin->w_topline;
2106 }
2107
2108 old_curwin = curwin;
2109 old_topline = curwin->w_topline;
2110#ifdef FEAT_DIFF
2111 old_topfill = curwin->w_topfill;
2112#endif
2113 old_buf = curwin->w_buffer;
2114 old_leftcol = curwin->w_leftcol;
2115}
2116
2117/*
2118 * Synchronize any windows that have "scrollbind" set, based on the
2119 * number of rows by which the current window has changed
2120 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2121 */
2122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002123check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 int want_ver;
2126 int want_hor;
2127 win_T *old_curwin = curwin;
2128 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 int old_VIsual_select = VIsual_select;
2130 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 colnr_T tgt_leftcol = curwin->w_leftcol;
2132 long topline;
2133 long y;
2134
2135 /*
2136 * check 'scrollopt' string for vertical and horizontal scroll options
2137 */
2138 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2139#ifdef FEAT_DIFF
2140 want_ver |= old_curwin->w_p_diff;
2141#endif
2142 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2143
2144 /*
2145 * loop through the scrollbound windows and scroll accordingly
2146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002148 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
2150 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002151 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (curwin != old_curwin && curwin->w_p_scb)
2153 {
2154 /*
2155 * do the vertical scroll
2156 */
2157 if (want_ver)
2158 {
2159#ifdef FEAT_DIFF
2160 if (old_curwin->w_p_diff && curwin->w_p_diff)
2161 {
2162 diff_set_topline(old_curwin, curwin);
2163 }
2164 else
2165#endif
2166 {
2167 curwin->w_scbind_pos += topline_diff;
2168 topline = curwin->w_scbind_pos;
2169 if (topline > curbuf->b_ml.ml_line_count)
2170 topline = curbuf->b_ml.ml_line_count;
2171 if (topline < 1)
2172 topline = 1;
2173
2174 y = topline - curwin->w_topline;
2175 if (y > 0)
2176 scrollup(y, FALSE);
2177 else
2178 scrolldown(-y, FALSE);
2179 }
2180
2181 redraw_later(VALID);
2182 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185
2186 /*
2187 * do the horizontal scroll
2188 */
2189 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2190 {
2191 curwin->w_leftcol = tgt_leftcol;
2192 leftcol_changed();
2193 }
2194 }
2195 }
2196
2197 /*
2198 * reset current-window
2199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 VIsual_select = old_VIsual_select;
2201 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 curwin = old_curwin;
2203 curbuf = old_curbuf;
2204}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
2206/*
2207 * Command character that's ignored.
2208 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002209 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002212nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002214 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215}
2216
2217/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002218 * Command character that doesn't do anything, but unlike nv_ignore() does
2219 * start edit(). Used for "startinsert" executed while starting up.
2220 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002222nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002223{
2224}
2225
2226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * Command character doesn't exist.
2228 */
2229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002230nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232 clearopbeep(cap->oap);
2233}
2234
2235/*
2236 * <Help> and <F1> commands.
2237 */
2238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002239nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
2241 if (!checkclearopq(cap->oap))
2242 ex_help(NULL);
2243}
2244
2245/*
2246 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2247 */
2248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002249nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002251#ifdef FEAT_JOB_CHANNEL
2252 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2253 clearopbeep(cap->oap);
2254 else
2255#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002256 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002257 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002258 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002259 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2260 op_addsub(cap->oap, cap->count1, cap->arg);
2261 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002262 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002263 else if (VIsual_active)
2264 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002265 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002266 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267}
2268
2269/*
2270 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2271 */
2272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002273nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002276 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277 if (mod_mask & MOD_MASK_CTRL)
2278 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002279 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002280 if (cap->arg == BACKWARD)
2281 goto_tabpage(-(int)cap->count1);
2282 else
2283 goto_tabpage((int)cap->count0);
2284 }
2285 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002286 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288}
2289
2290/*
2291 * Implementation of "gd" and "gD" command.
2292 */
2293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002294nv_gd(
2295 oparg_T *oap,
2296 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002297 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 char_u *ptr;
2301
Bram Moolenaard9d30582005-05-18 22:10:28 +00002302 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002303 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
2304 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002306#ifdef FEAT_FOLDING
2307 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2308 foldOpenCursor();
2309#endif
2310}
2311
2312/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002313 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2314 * otherwise.
2315 */
2316 static int
2317is_ident(char_u *line, int offset)
2318{
2319 int i;
2320 int incomment = FALSE;
2321 int instring = 0;
2322 int prev = 0;
2323
2324 for (i = 0; i < offset && line[i] != NUL; i++)
2325 {
2326 if (instring != 0)
2327 {
2328 if (prev != '\\' && line[i] == instring)
2329 instring = 0;
2330 }
2331 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2332 {
2333 instring = line[i];
2334 }
2335 else
2336 {
2337 if (incomment)
2338 {
2339 if (prev == '*' && line[i] == '/')
2340 incomment = FALSE;
2341 }
2342 else if (prev == '/' && line[i] == '*')
2343 {
2344 incomment = TRUE;
2345 }
2346 else if (prev == '/' && line[i] == '/')
2347 {
2348 return FALSE;
2349 }
2350 }
2351
2352 prev = line[i];
2353 }
2354
2355 return incomment == FALSE && instring == 0;
2356}
2357
2358/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002359 * Search for variable declaration of "ptr[len]".
2360 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2361 * current file ("gD").
2362 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002363 * Return FAIL when not found.
2364 */
2365 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002366find_decl(
2367 char_u *ptr,
2368 int len,
2369 int locally,
2370 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002371 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002372{
2373 char_u *pat;
2374 pos_T old_pos;
2375 pos_T par_pos;
2376 pos_T found_pos;
2377 int t;
2378 int save_p_ws;
2379 int save_p_scs;
2380 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002381 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002382 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002383 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002384
2385 if ((pat = alloc(len + 7)) == NULL)
2386 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002388 // Put "\V" before the pattern to avoid that the special meaning of "."
2389 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002390 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2391 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 old_pos = curwin->w_cursor;
2393 save_p_ws = p_ws;
2394 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002395 p_ws = FALSE; // don't wrap around end of file now
2396 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /*
2399 * With "gD" go to line 1.
2400 * With "gd" Search back for the start of the current function, then go
2401 * back until a blank line. If this fails go to line 1.
2402 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002403 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002405 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002407 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 }
2409 else
2410 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002411 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2413 --curwin->w_cursor.lnum;
2414 }
2415 curwin->w_cursor.col = 0;
2416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002417 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002418 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002419 for (;;)
2420 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002421 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002422 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002423 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002424 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002425
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002426 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002427 {
2428 pos_T *pos;
2429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002430 // Check that the block the match is in doesn't end before the
2431 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002432 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2433 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2434 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002435 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002436 // There can't be a useful match before the end of this block.
2437 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002438 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002439 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002440 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002441 }
2442
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002443 if (t == FAIL)
2444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002446 if (found_pos.lnum != 0)
2447 {
2448 curwin->w_cursor = found_pos;
2449 t = OK;
2450 }
2451 break;
2452 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002453 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002455 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002456 ++curwin->w_cursor.lnum;
2457 curwin->w_cursor.col = 0;
2458 continue;
2459 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002460 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2461
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002462 // If the current position is not a valid identifier and a previous
2463 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002464 if (!valid && found_pos.lnum != 0)
2465 {
2466 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002467 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002468 }
2469
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002470 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002471 if (valid && !locally)
2472 break;
2473 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002474 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002475 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002476 if (found_pos.lnum != 0)
2477 curwin->w_cursor = found_pos;
2478 break;
2479 }
2480
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002481 // For finding a local variable and the match is before the "{" or
2482 // inside a comment, continue searching. For K&R style function
2483 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002484 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002485 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002486 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002487 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002488 // Remove SEARCH_START from flags to avoid getting stuck at one
2489 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002490 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002492
2493 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002495 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 curwin->w_cursor = old_pos;
2497 }
2498 else
2499 {
2500 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002501 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 reset_search_dir();
2503 }
2504
2505 vim_free(pat);
2506 p_ws = save_p_ws;
2507 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002508
2509 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510}
2511
2512/*
2513 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2514 * lines rather than lines in the file.
2515 * 'dist' must be positive.
2516 *
2517 * Return OK if able to move cursor, FAIL otherwise.
2518 */
2519 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002520nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
2522 int linelen = linetabsize(ml_get_curline());
2523 int retval = OK;
2524 int atend = FALSE;
2525 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002526 int col_off1; // margin offset for first screen line
2527 int col_off2; // margin offset for wrapped screen line
2528 int width1; // text width for first screen line
2529 int width2; // test width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002532 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
2534 col_off1 = curwin_col_off();
2535 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002536 width1 = curwin->w_width - col_off1;
2537 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002538 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002539 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /*
2544 * Instead of sticking at the last character of the buffer line we
2545 * try to stick in the last column of the screen.
2546 */
2547 if (curwin->w_curswant == MAXCOL)
2548 {
2549 atend = TRUE;
2550 validate_virtcol();
2551 if (width1 <= 0)
2552 curwin->w_curswant = 0;
2553 else
2554 {
2555 curwin->w_curswant = width1 - 1;
2556 if (curwin->w_virtcol > curwin->w_curswant)
2557 curwin->w_curswant += ((curwin->w_virtcol
2558 - curwin->w_curswant - 1) / width2 + 1) * width2;
2559 }
2560 }
2561 else
2562 {
2563 if (linelen > width1)
2564 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2565 else
2566 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002567 if (curwin->w_curswant >= (colnr_T)n)
2568 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570
2571 while (dist--)
2572 {
2573 if (dir == BACKWARD)
2574 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002575 if ((long)curwin->w_curswant >= width1
2576#ifdef FEAT_FOLDING
2577 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2578#endif
2579 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002580 // Move back within the line. This can give a negative value
2581 // for w_curswant if width1 < width2 (with cpoptions+=n),
2582 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 curwin->w_curswant -= width2;
2584 else
2585 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002586 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002588 // Move to the start of a closed fold. Don't do that when
2589 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (!(fdo_flags & FDO_ALL))
2591 (void)hasFolding(curwin->w_cursor.lnum,
2592 &curwin->w_cursor.lnum, NULL);
2593#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002594 if (curwin->w_cursor.lnum == 1)
2595 {
2596 retval = FAIL;
2597 break;
2598 }
2599 --curwin->w_cursor.lnum;
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 linelen = linetabsize(ml_get_curline());
2602 if (linelen > width1)
2603 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2604 + 1) * width2;
2605 }
2606 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002607 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 {
2609 if (linelen > width1)
2610 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2611 else
2612 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002613 if (curwin->w_curswant + width2 < (colnr_T)n
2614#ifdef FEAT_FOLDING
2615 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2616#endif
2617 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002618 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 curwin->w_curswant += width2;
2620 else
2621 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002622 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002624 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2626 &curwin->w_cursor.lnum);
2627#endif
2628 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2629 {
2630 retval = FAIL;
2631 break;
2632 }
2633 curwin->w_cursor.lnum++;
2634 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002635 // Check if the cursor has moved below the number display
2636 // when width1 < width2 (with cpoptions+=n). Subtract width2
2637 // to get a negative value for w_curswant, which will get
2638 // clipped to column 0.
2639 if (curwin->w_curswant >= width1)
2640 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002641 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643 }
2644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002647 if (virtual_active() && atend)
2648 coladvance(MAXCOL);
2649 else
2650 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2653 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002654 colnr_T virtcol;
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 /*
2657 * Check for landing on a character that got split at the end of the
2658 * last line. We want to advance a screenline, not end up in the same
2659 * screenline or move two screenlines.
2660 */
2661 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002662 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002663#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002664 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2665 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002666#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002667
2668 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 && (curwin->w_curswant < (colnr_T)width1
2670 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2671 : ((curwin->w_curswant - width1) % width2
2672 > (colnr_T)width2 / 2)))
2673 --curwin->w_cursor.col;
2674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002677 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 return retval;
2680}
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682/*
2683 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2684 * cap->arg must be TRUE for CTRL-E.
2685 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002686 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002687nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
2689 if (!checkclearop(cap->oap))
2690 scroll_redraw(cap->arg, cap->count1);
2691}
2692
2693/*
2694 * Scroll "count" lines up or down, and redraw.
2695 */
2696 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002697scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 linenr_T prev_topline = curwin->w_topline;
2700#ifdef FEAT_DIFF
2701 int prev_topfill = curwin->w_topfill;
2702#endif
2703 linenr_T prev_lnum = curwin->w_cursor.lnum;
2704
2705 if (up)
2706 scrollup(count, TRUE);
2707 else
2708 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002709 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002711 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2712 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 cursor_correct();
2714 check_cursor_moved(curwin);
2715 curwin->w_valid |= VALID_TOPLINE;
2716
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002717 // If moved back to where we were, at least move the cursor, otherwise
2718 // we get stuck at one position. Don't move the cursor up if the
2719 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 while (curwin->w_topline == prev_topline
2721#ifdef FEAT_DIFF
2722 && curwin->w_topfill == prev_topfill
2723#endif
2724 )
2725 {
2726 if (up)
2727 {
2728 if (curwin->w_cursor.lnum > prev_lnum
2729 || cursor_down(1L, FALSE) == FAIL)
2730 break;
2731 }
2732 else
2733 {
2734 if (curwin->w_cursor.lnum < prev_lnum
2735 || prev_topline == 1L
2736 || cursor_up(1L, FALSE) == FAIL)
2737 break;
2738 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002739 // Mark w_topline as valid, otherwise the screen jumps back at the
2740 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 check_cursor_moved(curwin);
2742 curwin->w_valid |= VALID_TOPLINE;
2743 }
2744 }
2745 if (curwin->w_cursor.lnum != prev_lnum)
2746 coladvance(curwin->w_curswant);
2747 redraw_later(VALID);
2748}
2749
2750/*
2751 * Commands that start with "z".
2752 */
2753 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002754nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 long n;
2757 colnr_T col;
2758 int nchar = cap->nchar;
2759#ifdef FEAT_FOLDING
2760 long old_fdl = curwin->w_p_fdl;
2761 int old_fen = curwin->w_p_fen;
2762#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002763#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002764 int undo = FALSE;
2765#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002766 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
2768 if (VIM_ISDIGIT(nchar))
2769 {
2770 /*
2771 * "z123{nchar}": edit the count before obtaining {nchar}
2772 */
2773 if (checkclearop(cap->oap))
2774 return;
2775 n = nchar - '0';
2776 for (;;)
2777 {
2778#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002779 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780#endif
2781 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002782 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002783 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 --no_mapping;
2786 --allow_keys;
2787#ifdef FEAT_CMDL_INFO
2788 (void)add_to_showcmd(nchar);
2789#endif
2790 if (nchar == K_DEL || nchar == K_KDEL)
2791 n /= 10;
2792 else if (VIM_ISDIGIT(nchar))
2793 n = n * 10 + (nchar - '0');
2794 else if (nchar == CAR)
2795 {
2796#ifdef FEAT_GUI
2797 need_mouse_correct = TRUE;
2798#endif
2799 win_setheight((int)n);
2800 break;
2801 }
2802 else if (nchar == 'l'
2803 || nchar == 'h'
2804 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002805 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 {
2807 cap->count1 = n ? n * cap->count1 : cap->count1;
2808 goto dozet;
2809 }
2810 else
2811 {
2812 clearopbeep(cap->oap);
2813 break;
2814 }
2815 }
2816 cap->oap->op_type = OP_NOP;
2817 return;
2818 }
2819
2820dozet:
2821 if (
2822#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002823 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2824 // and "zC" only in Visual mode. "zj" and "zk" are motion
2825 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 cap->nchar != 'f' && cap->nchar != 'F'
2827 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2828 && cap->nchar != 'j' && cap->nchar != 'k'
2829 &&
2830#endif
2831 checkclearop(cap->oap))
2832 return;
2833
2834 /*
2835 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2836 * If line number given, set cursor.
2837 */
2838 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2839 && cap->count0
2840 && cap->count0 != curwin->w_cursor.lnum)
2841 {
2842 setpcmark();
2843 if (cap->count0 > curbuf->b_ml.ml_line_count)
2844 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2845 else
2846 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002847 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849
2850 switch (nchar)
2851 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002852 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 case '+':
2854 if (cap->count0 == 0)
2855 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002856 // No count given: put cursor at the line below screen
2857 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2859 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2860 else
2861 curwin->w_cursor.lnum = curwin->w_botline;
2862 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002863 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 case NL:
2865 case CAR:
2866 case K_KENTER:
2867 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002868 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
2870 case 't': scroll_cursor_top(0, TRUE);
2871 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002872 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 break;
2874
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002875 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002877 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
2879 case 'z': scroll_cursor_halfway(TRUE);
2880 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002881 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 break;
2883
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002884 // "z^", "z-" and "zb": put cursor at bottom of screen
2885 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2886 // when <count> is at bottom of window, and puts that one at
2887 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (cap->count0 != 0)
2889 {
2890 scroll_cursor_bot(0, TRUE);
2891 curwin->w_cursor.lnum = curwin->w_topline;
2892 }
2893 else if (curwin->w_topline == 1)
2894 curwin->w_cursor.lnum = 1;
2895 else
2896 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002897 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 case '-':
2899 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002900 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
2902 case 'b': scroll_cursor_bot(0, TRUE);
2903 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002904 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 break;
2906
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002907 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002909 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002910 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case 'h':
2914 case K_LEFT:
2915 if (!curwin->w_p_wrap)
2916 {
2917 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2918 curwin->w_leftcol = 0;
2919 else
2920 curwin->w_leftcol -= (colnr_T)cap->count1;
2921 leftcol_changed();
2922 }
2923 break;
2924
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002925 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002926 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002927 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002929 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 case 'l':
2931 case K_RIGHT:
2932 if (!curwin->w_p_wrap)
2933 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 curwin->w_leftcol += (colnr_T)cap->count1;
2936 leftcol_changed();
2937 }
2938 break;
2939
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002940 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 case 's': if (!curwin->w_p_wrap)
2942 {
2943#ifdef FEAT_FOLDING
2944 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002945 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 else
2947#endif
2948 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002949 if ((long)col > siso)
2950 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 else
2952 col = 0;
2953 if (curwin->w_leftcol != col)
2954 {
2955 curwin->w_leftcol = col;
2956 redraw_later(NOT_VALID);
2957 }
2958 }
2959 break;
2960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 case 'e': if (!curwin->w_p_wrap)
2963 {
2964#ifdef FEAT_FOLDING
2965 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002966 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else
2968#endif
2969 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02002970 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01002971 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 col = 0;
2973 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01002974 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (curwin->w_leftcol != col)
2976 {
2977 curwin->w_leftcol = col;
2978 redraw_later(NOT_VALID);
2979 }
2980 }
2981 break;
2982
Christian Brabandt2fa93842021-05-30 22:17:25 +02002983 // "zp", "zP" in block mode put without addind trailing spaces
2984 case 'P':
2985 case 'p': nv_put(cap);
2986 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02002987 // "zy" Yank without trailing spaces
2988 case 'y': nv_operator(cap);
2989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 // "zF": create fold command
2992 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 case 'F':
2994 case 'f': if (foldManualAllowed(TRUE))
2995 {
2996 cap->nchar = 'f';
2997 nv_operator(cap);
2998 curwin->w_p_fen = TRUE;
2999
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003000 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3002 {
3003 nv_operator(cap);
3004 finish_op = TRUE;
3005 }
3006 }
3007 else
3008 clearopbeep(cap->oap);
3009 break;
3010
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003011 // "zd": delete fold at cursor
3012 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 case 'd':
3014 case 'D': if (foldManualAllowed(FALSE))
3015 {
3016 if (VIsual_active)
3017 nv_operator(cap);
3018 else
3019 deleteFold(curwin->w_cursor.lnum,
3020 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3021 }
3022 break;
3023
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003024 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 case 'E': if (foldmethodIsManual(curwin))
3026 {
3027 clearFolding(curwin);
3028 changed_window_setting();
3029 }
3030 else if (foldmethodIsMarker(curwin))
3031 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3032 TRUE, FALSE);
3033 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003034 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 break;
3036
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003037 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 case 'n': curwin->w_p_fen = FALSE;
3039 break;
3040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003041 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 case 'N': curwin->w_p_fen = TRUE;
3043 break;
3044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003045 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3047 break;
3048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003049 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3051 openFold(curwin->w_cursor.lnum, cap->count1);
3052 else
3053 {
3054 closeFold(curwin->w_cursor.lnum, cap->count1);
3055 curwin->w_p_fen = TRUE;
3056 }
3057 break;
3058
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003059 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3061 openFoldRecurse(curwin->w_cursor.lnum);
3062 else
3063 {
3064 closeFoldRecurse(curwin->w_cursor.lnum);
3065 curwin->w_p_fen = TRUE;
3066 }
3067 break;
3068
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003069 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 case 'o': if (VIsual_active)
3071 nv_operator(cap);
3072 else
3073 openFold(curwin->w_cursor.lnum, cap->count1);
3074 break;
3075
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003076 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 case 'O': if (VIsual_active)
3078 nv_operator(cap);
3079 else
3080 openFoldRecurse(curwin->w_cursor.lnum);
3081 break;
3082
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003083 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 case 'c': if (VIsual_active)
3085 nv_operator(cap);
3086 else
3087 closeFold(curwin->w_cursor.lnum, cap->count1);
3088 curwin->w_p_fen = TRUE;
3089 break;
3090
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003091 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 case 'C': if (VIsual_active)
3093 nv_operator(cap);
3094 else
3095 closeFoldRecurse(curwin->w_cursor.lnum);
3096 curwin->w_p_fen = TRUE;
3097 break;
3098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003099 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 case 'v': foldOpenCursor();
3101 break;
3102
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003103 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003105 curwin->w_foldinvalid = TRUE; // recompute folds
3106 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 foldOpenCursor();
3108 break;
3109
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003110 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003112 curwin->w_foldinvalid = TRUE; // recompute folds
3113 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 break;
3115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003116 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003118 {
3119 curwin->w_p_fdl -= cap->count1;
3120 if (curwin->w_p_fdl < 0)
3121 curwin->w_p_fdl = 0;
3122 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003123 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 curwin->w_p_fen = TRUE;
3125 break;
3126
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003127 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003129 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 curwin->w_p_fen = TRUE;
3131 break;
3132
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003134 case 'r': curwin->w_p_fdl += cap->count1;
3135 {
3136 int d = getDeepestNesting();
3137
3138 if (curwin->w_p_fdl >= d)
3139 curwin->w_p_fdl = d;
3140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 break;
3142
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003143 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003145 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 break;
3147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003148 case 'j': // "zj" move to next fold downwards
3149 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3151 cap->count1) == FAIL)
3152 clearopbeep(cap->oap);
3153 break;
3154
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003155#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003157#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003158 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003159 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003160 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003161 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003162 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003163 --no_mapping;
3164 --allow_keys;
3165#ifdef FEAT_CMDL_INFO
3166 (void)add_to_showcmd(nchar);
3167#endif
3168 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3169 {
3170 clearopbeep(cap->oap);
3171 break;
3172 }
3173 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003174 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003175
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003176 case 'g': // "zg": add good word to word list
3177 case 'w': // "zw": add wrong word to word list
3178 case 'G': // "zG": add good word to temp word list
3179 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003180 {
3181 char_u *ptr = NULL;
3182 int len;
3183
3184 if (checkclearop(cap->oap))
3185 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003186 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3187 == FAIL)
3188 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003189 if (ptr == NULL)
3190 {
3191 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003192
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003193 // Find bad word under the cursor. When 'spell' is
3194 // off this fails and find_ident_under_cursor() is
3195 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003196 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003197 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003198 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003199 if (len != 0 && curwin->w_cursor.col <= pos.col)
3200 ptr = ml_get_pos(&curwin->w_cursor);
3201 curwin->w_cursor = pos;
3202 }
3203
Bram Moolenaarb765d632005-06-07 21:00:02 +00003204 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3205 FIND_IDENT)) == 0)
3206 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003207 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3208 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003209 (nchar == 'G' || nchar == 'W')
3210 ? 0 : (int)cap->count1,
3211 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003212 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003213 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003214
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003215 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003216 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003217 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003218 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003219#endif
3220
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 default: clearopbeep(cap->oap);
3222 }
3223
3224#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003225 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 if (old_fen != curwin->w_p_fen)
3227 {
3228# ifdef FEAT_DIFF
3229 win_T *wp;
3230
3231 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3232 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 FOR_ALL_WINDOWS(wp)
3235 {
3236 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3237 {
3238 wp->w_p_fen = curwin->w_p_fen;
3239 changed_window_setting_win(wp);
3240 }
3241 }
3242 }
3243# endif
3244 changed_window_setting();
3245 }
3246
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003247 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (old_fdl != curwin->w_p_fdl)
3249 newFoldLevel();
3250#endif
3251}
3252
3253#ifdef FEAT_GUI
3254/*
3255 * Vertical scrollbar movement.
3256 */
3257 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003258nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 if (cap->oap->op_type != OP_NOP)
3261 clearopbeep(cap->oap);
3262
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003263 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 gui_do_scroll();
3265}
3266
3267/*
3268 * Horizontal scrollbar movement.
3269 */
3270 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003271nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 if (cap->oap->op_type != OP_NOP)
3274 clearopbeep(cap->oap);
3275
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003276 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003277 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278}
3279#endif
3280
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003281#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003282/*
3283 * Click in GUI tab.
3284 */
3285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003286nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003287{
3288 if (cap->oap->op_type != OP_NOP)
3289 clearopbeep(cap->oap);
3290
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003291 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003292 goto_tabpage(current_tab);
3293}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003294
3295/*
3296 * Selected item in tab line menu.
3297 */
3298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003299nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003300{
3301 if (cap->oap->op_type != OP_NOP)
3302 clearopbeep(cap->oap);
3303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003304 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003305 handle_tabmenu();
3306}
3307
3308/*
3309 * Handle selecting an item of the GUI tab line menu.
3310 * Used in Normal and Insert mode.
3311 */
3312 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003313handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003314{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003315 switch (current_tabmenu)
3316 {
3317 case TABLINE_MENU_CLOSE:
3318 if (current_tab == 0)
3319 do_cmdline_cmd((char_u *)"tabclose");
3320 else
3321 {
3322 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3323 current_tab);
3324 do_cmdline_cmd(IObuff);
3325 }
3326 break;
3327
3328 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003329 if (current_tab == 0)
3330 do_cmdline_cmd((char_u *)"$tabnew");
3331 else
3332 {
3333 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3334 current_tab - 1);
3335 do_cmdline_cmd(IObuff);
3336 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003337 break;
3338
3339 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003340 if (current_tab == 0)
3341 do_cmdline_cmd((char_u *)"browse $tabnew");
3342 else
3343 {
3344 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3345 current_tab - 1);
3346 do_cmdline_cmd(IObuff);
3347 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003348 break;
3349 }
3350}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003351#endif
3352
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353/*
3354 * "Q" command.
3355 */
3356 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003357nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 /*
3360 * Ignore 'Q' in Visual mode, just give a beep.
3361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003363 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003364 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 do_exmode(FALSE);
3366}
3367
3368/*
3369 * Handle a ":" command.
3370 */
3371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003372nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003374 int old_p_im;
3375 int cmd_result;
3376 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
Bram Moolenaar957cf672020-11-12 14:21:06 +01003378 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 nv_operator(cap);
3380 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
3382 if (cap->oap->op_type != OP_NOP)
3383 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003384 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 cap->oap->motion_type = MCHAR;
3386 cap->oap->inclusive = FALSE;
3387 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003388 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003390 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 stuffcharReadbuff('.');
3392 if (cap->count0 > 1)
3393 {
3394 stuffReadbuff((char_u *)",.+");
3395 stuffnumReadbuff((long)cap->count0 - 1L);
3396 }
3397 }
3398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003399 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (KeyTyped)
3401 compute_cmdrow();
3402
3403 old_p_im = p_im;
3404
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003405 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003406 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3408
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003409 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (p_im != old_p_im)
3411 {
3412 if (p_im)
3413 restart_edit = 'i';
3414 else
3415 restart_edit = 0;
3416 }
3417
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003418 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003419 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003420 clearop(cap->oap);
3421 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3423 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003424 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3425 || did_emsg
3426 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003427 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 clearopbeep(cap->oap);
3429 }
3430}
3431
3432/*
3433 * Handle CTRL-G command.
3434 */
3435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003436nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003438 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 VIsual_select = !VIsual_select;
3441 showmode();
3442 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003443 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003444 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 fileinfo((int)cap->count0, FALSE, TRUE);
3446}
3447
3448/*
3449 * Handle CTRL-H <Backspace> command.
3450 */
3451 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003452nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (VIsual_active && VIsual_select)
3455 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003456 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 v_visop(cap);
3458 }
3459 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 nv_left(cap);
3461}
3462
3463/*
3464 * CTRL-L: clear screen and redraw.
3465 */
3466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003467nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468{
3469 if (!checkclearop(cap->oap))
3470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003472 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003473 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003474# ifdef FEAT_RELTIME
3475 {
3476 win_T *wp;
3477
3478 FOR_ALL_WINDOWS(wp)
3479 wp->w_s->b_syn_slow = FALSE;
3480 }
3481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482#endif
3483 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003484#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3485# ifdef VIMDLL
3486 if (!gui.in_use)
3487# endif
3488 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491}
3492
3493/*
3494 * CTRL-O: In Select mode: switch to Visual mode for one command.
3495 * Otherwise: Go to older pcmark.
3496 */
3497 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003498nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if (VIsual_active && VIsual_select)
3501 {
3502 VIsual_select = FALSE;
3503 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003504 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
3506 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 cap->count1 = -cap->count1;
3509 nv_pcmark(cap);
3510 }
3511}
3512
3513/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003514 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3515 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 */
3517 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003518nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 if (!checkclearopq(cap->oap))
3521 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3522 GETF_SETMARK|GETF_ALT, FALSE);
3523}
3524
3525/*
3526 * "Z" commands.
3527 */
3528 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003529nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
3531 if (!checkclearopq(cap->oap))
3532 {
3533 switch (cap->nchar)
3534 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003535 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 case 'Z': do_cmdline_cmd((char_u *)"x");
3537 break;
3538
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003539 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 case 'Q': do_cmdline_cmd((char_u *)"q!");
3541 break;
3542
3543 default: clearopbeep(cap->oap);
3544 }
3545 }
3546}
3547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548/*
3549 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3550 */
3551 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003552do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
3554 oparg_T oa;
3555 cmdarg_T ca;
3556
3557 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003558 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 ca.oap = &oa;
3560 ca.cmdchar = c1;
3561 ca.nchar = c2;
3562 nv_ident(&ca);
3563}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565/*
3566 * Handle the commands that use the word under the cursor.
3567 * [g] CTRL-] :ta to current identifier
3568 * [g] 'K' run program for current identifier
3569 * [g] '*' / to current identifier or string
3570 * [g] '#' ? to current identifier or string
3571 * g ']' :tselect for current identifier
3572 */
3573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003574nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576 char_u *ptr = NULL;
3577 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003578 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003579 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003581 char_u *kp; // value of 'keywordprg'
3582 int kp_help; // 'keywordprg' is ":he"
3583 int kp_ex; // 'keywordprg' starts with ":"
3584 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003586 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003587 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *aux_ptr;
3589 int isman;
3590 int isman_s;
3591
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003592 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 cmdchar = cap->nchar;
3595 g_cmd = TRUE;
3596 }
3597 else
3598 {
3599 cmdchar = cap->cmdchar;
3600 g_cmd = FALSE;
3601 }
3602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003603 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 cmdchar = '#';
3605
3606 /*
3607 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3608 */
3609 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3612 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (checkclearopq(cap->oap))
3614 return;
3615 }
3616
3617 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3618 (cmdchar == '*' || cmdchar == '#')
3619 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3620 {
3621 clearop(cap->oap);
3622 return;
3623 }
3624
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003625 // Allocate buffer to put the command in. Inserting backslashes can
3626 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3627 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3629 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3630 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003631 if (kp_help && *skipwhite(ptr) == NUL)
3632 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003633 emsg(_(e_noident)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003634 return;
3635 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003636 kp_ex = (*kp == ':');
3637 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3638 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (buf == NULL)
3640 return;
3641 buf[0] = NUL;
3642
3643 switch (cmdchar)
3644 {
3645 case '*':
3646 case '#':
3647 /*
3648 * Put cursor at start of word, makes search skip the word
3649 * under the cursor.
3650 * Call setpcmark() first, so "*``" puts the cursor back where
3651 * it was.
3652 */
3653 setpcmark();
3654 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3655
3656 if (!g_cmd && vim_iswordp(ptr))
3657 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003658 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 break;
3660
3661 case 'K':
3662 if (kp_help)
3663 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003664 else if (kp_ex)
3665 {
3666 if (cap->count0 != 0)
3667 vim_snprintf((char *)buf, buflen, "%s %ld",
3668 kp, cap->count0);
3669 else
3670 STRCPY(buf, kp);
3671 STRCAT(buf, " ");
3672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 else
3674 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003675 // An external command will probably use an argument starting
3676 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003677 while (*ptr == '-' && n > 0)
3678 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003679 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003680 --n;
3681 }
3682 if (n == 0)
3683 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003684 emsg(_(e_noident)); // found dashes only
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003685 vim_free(buf);
3686 return;
3687 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003689 // When a count is given, turn it into a range. Is this
3690 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 isman = (STRCMP(kp, "man") == 0);
3692 isman_s = (STRCMP(kp, "man -s") == 0);
3693 if (cap->count0 != 0 && !(isman || isman_s))
3694 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3695
3696 STRCAT(buf, "! ");
3697 if (cap->count0 == 0 && isman_s)
3698 STRCAT(buf, "man");
3699 else
3700 STRCAT(buf, kp);
3701 STRCAT(buf, " ");
3702 if (cap->count0 != 0 && (isman || isman_s))
3703 {
3704 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3705 STRCAT(buf, " ");
3706 }
3707 }
3708 break;
3709
3710 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003711 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712#ifdef FEAT_CSCOPE
3713 if (p_cst)
3714 STRCPY(buf, "cstag ");
3715 else
3716#endif
3717 STRCPY(buf, "ts ");
3718 break;
3719
3720 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003721 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (curbuf->b_help)
3723 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003725 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003726 if (g_cmd)
3727 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003728 else if (cap->count0 == 0)
3729 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003730 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003731 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
3734
3735 /*
3736 * Now grab the chars in the identifier
3737 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003738 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003740 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003741 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003742 // Escape the argument properly for an Ex command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003743 p = vim_strsave_fnameescape(ptr, FALSE);
3744 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003745 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003746 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003747 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003748 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003750 vim_free(buf);
3751 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003753 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003754 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003755 {
3756 vim_free(buf);
3757 vim_free(p);
3758 return;
3759 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003760 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003761 STRCAT(buf, p);
3762 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003764 else
3765 {
3766 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003767 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003768 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003769 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003770 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003771 {
3772 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003773 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003774 aux_ptr = (char_u *)"";
3775 else
3776 aux_ptr = (char_u *)"\\|\"\n[";
3777 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003778 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003779 aux_ptr = (char_u *)"\\|\"\n*?[";
3780
3781 p = buf + STRLEN(buf);
3782 while (n-- > 0)
3783 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003784 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003785 if (vim_strchr(aux_ptr, *ptr) != NULL)
3786 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003787 // When current byte is a part of multibyte character, copy all
3788 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003789 if (has_mbyte)
3790 {
3791 int i;
3792 int len = (*mb_ptr2len)(ptr) - 1;
3793
3794 for (i = 0; i < len && n >= 1; ++i, --n)
3795 *p++ = *ptr++;
3796 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003797 *p++ = *ptr++;
3798 }
3799 *p = NUL;
3800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
3802 /*
3803 * Execute the command.
3804 */
3805 if (cmdchar == '*' || cmdchar == '#')
3806 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003807 if (!g_cmd && (has_mbyte
3808 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3809 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003811
3812 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003813 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003815
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003816 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003819 {
3820 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003822 g_tag_at_cursor = FALSE;
3823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
3825 vim_free(buf);
3826}
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828/*
3829 * Get visually selected text, within one line only.
3830 * Returns FAIL if more than one line selected.
3831 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003832 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003833get_visual_text(
3834 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003835 char_u **pp, // return: start of selected text
3836 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837{
3838 if (VIsual_mode != 'V')
3839 unadjust_for_sel();
3840 if (VIsual.lnum != curwin->w_cursor.lnum)
3841 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003842 if (cap != NULL)
3843 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 return FAIL;
3845 }
3846 if (VIsual_mode == 'V')
3847 {
3848 *pp = ml_get_curline();
3849 *lenp = (int)STRLEN(*pp);
3850 }
3851 else
3852 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003853 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
3855 *pp = ml_get_pos(&curwin->w_cursor);
3856 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3857 }
3858 else
3859 {
3860 *pp = ml_get_pos(&VIsual);
3861 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (has_mbyte)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003864 // Correct the length to include the whole last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003865 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 }
3867 reset_VIsual_and_resel();
3868 return OK;
3869}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870
3871/*
3872 * CTRL-T: backwards in tag stack
3873 */
3874 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003875nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
3877 if (!checkclearopq(cap->oap))
3878 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3879}
3880
3881/*
3882 * Handle scrolling command 'H', 'L' and 'M'.
3883 */
3884 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003885nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
3887 int used = 0;
3888 long n;
3889#ifdef FEAT_FOLDING
3890 linenr_T lnum;
3891#endif
3892 int half;
3893
3894 cap->oap->motion_type = MLINE;
3895 setpcmark();
3896
3897 if (cap->cmdchar == 'L')
3898 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003899 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 curwin->w_cursor.lnum = curwin->w_botline - 1;
3901 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3902 curwin->w_cursor.lnum = 1;
3903 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003904 {
3905#ifdef FEAT_FOLDING
3906 if (hasAnyFolding(curwin))
3907 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003908 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003909 for (n = cap->count1 - 1; n > 0
3910 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3911 {
3912 (void)hasFolding(curwin->w_cursor.lnum,
3913 &curwin->w_cursor.lnum, NULL);
3914 --curwin->w_cursor.lnum;
3915 }
3916 }
3917 else
3918#endif
3919 curwin->w_cursor.lnum -= cap->count1 - 1;
3920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 }
3922 else
3923 {
3924 if (cap->cmdchar == 'M')
3925 {
3926#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003927 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 used -= diff_check_fill(curwin, curwin->w_topline)
3929 - curwin->w_topfill;
3930#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003931 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3933 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3934 {
3935#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003936 // Count half he number of filler lines to be "below this
3937 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3939 + n) / 2 >= half)
3940 {
3941 --n;
3942 break;
3943 }
3944#endif
3945 used += plines(curwin->w_topline + n);
3946 if (used >= half)
3947 break;
3948#ifdef FEAT_FOLDING
3949 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
3950 n = lnum - curwin->w_topline;
3951#endif
3952 }
3953 if (n > 0 && used > curwin->w_height)
3954 --n;
3955 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003956 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003959#ifdef FEAT_FOLDING
3960 if (hasAnyFolding(curwin))
3961 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003962 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003963 lnum = curwin->w_topline;
3964 while (n-- > 0 && lnum < curwin->w_botline - 1)
3965 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02003966 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003967 ++lnum;
3968 }
3969 n = lnum - curwin->w_topline;
3970 }
3971#endif
3972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 curwin->w_cursor.lnum = curwin->w_topline + n;
3974 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3975 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3976 }
3977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003978 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02003979 if (cap->oap->op_type == OP_NOP)
3980 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 beginline(BL_SOL | BL_FIX);
3982}
3983
3984/*
3985 * Cursor right commands.
3986 */
3987 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003988nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
3990 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003991 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003993 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
3994 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003995 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003996 if (mod_mask & MOD_MASK_CTRL)
3997 cap->arg = TRUE;
3998 nv_wordcmd(cap);
3999 return;
4000 }
4001
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 cap->oap->motion_type = MCHAR;
4003 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004004 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004007 * In virtual edit mode, there's no such thing as "past_line", as lines
4008 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 */
4010 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004011 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 for (n = cap->count1; n > 0; --n)
4014 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004015 if ((!past_line && oneright() == FAIL)
4016 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004017 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 {
4019 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004020 * <Space> wraps to next line if 'whichwrap' has 's'.
4021 * 'l' wraps to next line if 'whichwrap' has 'l'.
4022 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 */
4024 if ( ((cap->cmdchar == ' '
4025 && vim_strchr(p_ww, 's') != NULL)
4026 || (cap->cmdchar == 'l'
4027 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004028 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 && vim_strchr(p_ww, '>') != NULL))
4030 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4031 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004032 // When deleting we also count the NL as a character.
4033 // Set cap->oap->inclusive when last char in the line is
4034 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004035 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004037 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 cap->oap->inclusive = TRUE;
4039 else
4040 {
4041 ++curwin->w_cursor.lnum;
4042 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 curwin->w_set_curswant = TRUE;
4045 cap->oap->inclusive = FALSE;
4046 }
4047 continue;
4048 }
4049 if (cap->oap->op_type == OP_NOP)
4050 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004051 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 if (n == cap->count1)
4053 beep_flush();
4054 }
4055 else
4056 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004057 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 cap->oap->inclusive = TRUE;
4059 }
4060 break;
4061 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004062 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
4064 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (virtual_active())
4066 oneright();
4067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004070 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 ++curwin->w_cursor.col;
4073 }
4074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076#ifdef FEAT_FOLDING
4077 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4078 && cap->oap->op_type == OP_NOP)
4079 foldOpenCursor();
4080#endif
4081}
4082
4083/*
4084 * Cursor left commands.
4085 *
4086 * Returns TRUE when operator end should not be adjusted.
4087 */
4088 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004089nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
4091 long n;
4092
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004093 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4094 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004095 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004096 if (mod_mask & MOD_MASK_CTRL)
4097 cap->arg = 1;
4098 nv_bck_word(cap);
4099 return;
4100 }
4101
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 cap->oap->motion_type = MCHAR;
4103 cap->oap->inclusive = FALSE;
4104 for (n = cap->count1; n > 0; --n)
4105 {
4106 if (oneleft() == FAIL)
4107 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004108 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4109 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4110 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if ( (((cap->cmdchar == K_BS
4112 || cap->cmdchar == Ctrl_H)
4113 && vim_strchr(p_ww, 'b') != NULL)
4114 || (cap->cmdchar == 'h'
4115 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004116 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 && vim_strchr(p_ww, '<') != NULL))
4118 && curwin->w_cursor.lnum > 1)
4119 {
4120 --(curwin->w_cursor.lnum);
4121 coladvance((colnr_T)MAXCOL);
4122 curwin->w_set_curswant = TRUE;
4123
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004124 // When the NL before the first char has to be deleted we
4125 // put the cursor on the NUL after the previous line.
4126 // This is a very special case, be careful!
4127 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if ( (cap->oap->op_type == OP_DELETE
4129 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004130 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004132 char_u *cp = ml_get_cursor();
4133
4134 if (*cp != NUL)
4135 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004136 if (has_mbyte)
4137 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4138 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004139 ++curwin->w_cursor.col;
4140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 cap->retval |= CA_NO_ADJ_OP_END;
4142 }
4143 continue;
4144 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004145 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4147 beep_flush();
4148 break;
4149 }
4150 }
4151#ifdef FEAT_FOLDING
4152 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4153 && cap->oap->op_type == OP_NOP)
4154 foldOpenCursor();
4155#endif
4156}
4157
4158/*
4159 * Cursor up commands.
4160 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4161 */
4162 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004163nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004165 if (mod_mask & MOD_MASK_SHIFT)
4166 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004167 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004168 cap->arg = BACKWARD;
4169 nv_page(cap);
4170 }
4171 else
4172 {
4173 cap->oap->motion_type = MLINE;
4174 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4175 clearopbeep(cap->oap);
4176 else if (cap->arg)
4177 beginline(BL_WHITE | BL_FIX);
4178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179}
4180
4181/*
4182 * Cursor down commands.
4183 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4184 */
4185 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004186nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004188 if (mod_mask & MOD_MASK_SHIFT)
4189 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004190 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004191 cap->arg = FORWARD;
4192 nv_page(cap);
4193 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004194#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004195 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004196 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4197 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
4201#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004202 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004203 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 cmdwin_result = CAR;
4205 else
4206#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004207#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004208 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004209 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4210 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4211 {
4212 invoke_prompt_callback();
4213 if (restart_edit == 0)
4214 restart_edit = 'a';
4215 }
4216 else
4217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
4219 cap->oap->motion_type = MLINE;
4220 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4221 clearopbeep(cap->oap);
4222 else if (cap->arg)
4223 beginline(BL_WHITE | BL_FIX);
4224 }
4225 }
4226}
4227
4228#ifdef FEAT_SEARCHPATH
4229/*
4230 * Grab the file name under the cursor and edit it.
4231 */
4232 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004233nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
4235 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004236 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004238 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
4240 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004241 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return;
4243 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004244 if (curbuf_locked())
4245 {
4246 clearop(cap->oap);
4247 return;
4248 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004249#ifdef FEAT_PROP_POPUP
4250 if (ERROR_IF_TERM_POPUP_WINDOW)
4251 return;
4252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004254 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
4256 if (ptr != NULL)
4257 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004258 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004259 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004260 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004262 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004263 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004264 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004265 {
4266 curwin->w_cursor.lnum = lnum;
4267 check_cursor_lnum();
4268 beginline(BL_SOL | BL_FIX);
4269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 vim_free(ptr);
4271 }
4272 else
4273 clearop(cap->oap);
4274}
4275#endif
4276
4277/*
4278 * <End> command: to end of current line or last line.
4279 */
4280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004281nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004283 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004285 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004287 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289 nv_dollar(cap);
4290}
4291
4292/*
4293 * Handle the "$" command.
4294 */
4295 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004296nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
4298 cap->oap->motion_type = MCHAR;
4299 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004300 // In virtual mode when off the edge of a line and an operator
4301 // is pending (whew!) keep the cursor where it is.
4302 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (!virtual_active() || gchar_cursor() != NUL
4304 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004305 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (cursor_down((long)(cap->count1 - 1),
4307 cap->oap->op_type == OP_NOP) == FAIL)
4308 clearopbeep(cap->oap);
4309#ifdef FEAT_FOLDING
4310 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4311 foldOpenCursor();
4312#endif
4313}
4314
4315/*
4316 * Implementation of '?' and '/' commands.
4317 * If cap->arg is TRUE don't set PC mark.
4318 */
4319 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004320nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321{
4322 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004323 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
4325 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4326 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004327 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 cap->cmdchar = 'g';
4329 cap->nchar = '?';
4330 nv_operator(cap);
4331 return;
4332 }
4333
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004334 // When using 'incsearch' the cursor may be moved to set a different search
4335 // start position.
Bram Moolenaare96a2492019-06-25 04:12:16 +02004336 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 if (cap->searchbuf == NULL)
4339 {
4340 clearop(oap);
4341 return;
4342 }
4343
Bram Moolenaar46539112015-02-17 15:43:57 +01004344 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004345 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004346 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347}
4348
4349/*
4350 * Handle "N" and "n" commands.
4351 * cap->arg is SEARCH_REV for "N", 0 for "n".
4352 */
4353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004354nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004356 pos_T old = curwin->w_cursor;
4357 int wrapped = FALSE;
4358 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004359
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004360 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004361 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004362 // Avoid getting stuck on the current cursor position, which can
4363 // happen when an offset is given and the cursor is on the last char
4364 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004365 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004366 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004367 cap->count1 -= 1;
4368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369}
4370
4371/*
4372 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4373 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004374 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004376 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004377normal_search(
4378 cmdarg_T *cap,
4379 int dir,
4380 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004381 int opt, // extra flags for do_search()
4382 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004385 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387 cap->oap->motion_type = MCHAR;
4388 cap->oap->inclusive = FALSE;
4389 cap->oap->use_reg_one = TRUE;
4390 curwin->w_set_curswant = TRUE;
4391
Bram Moolenaara80faa82020-04-12 19:37:17 +02004392 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004393 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004394 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4395 if (wrapped != NULL)
4396 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if (i == 0)
4398 clearop(cap->oap);
4399 else
4400 {
4401 if (i == 2)
4402 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404#ifdef FEAT_FOLDING
4405 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4406 foldOpenCursor();
4407#endif
4408 }
4409
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004410 // "/$" will put the cursor after the end of the line, may need to
4411 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004413 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414}
4415
4416/*
4417 * Character search commands.
4418 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4419 * ',' and FALSE for ';'.
4420 * cap->nchar is NUL for ',' and ';' (repeat the search)
4421 */
4422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004423nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 int t_cmd;
4426
4427 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4428 t_cmd = TRUE;
4429 else
4430 t_cmd = FALSE;
4431
4432 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4434 clearopbeep(cap->oap);
4435 else
4436 {
4437 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004438 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4440 && (t_cmd || cap->oap->op_type != OP_NOP))
4441 {
4442 colnr_T scol, ecol;
4443
4444 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4445 curwin->w_cursor.coladd = ecol - scol;
4446 }
4447 else
4448 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450#ifdef FEAT_FOLDING
4451 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4452 foldOpenCursor();
4453#endif
4454 }
4455}
4456
4457/*
4458 * "[" and "]" commands.
4459 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4460 */
4461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004462nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004464 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004466 pos_T *pos = NULL; // init for GCC
4467 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 int flag;
4469 long n;
4470 int findc;
4471 int c;
4472
4473 cap->oap->motion_type = MCHAR;
4474 cap->oap->inclusive = FALSE;
4475 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004476 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
4478#ifdef FEAT_SEARCHPATH
4479 /*
4480 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4481 */
4482 if (cap->nchar == 'f')
4483 nv_gotofile(cap);
4484 else
4485#endif
4486
4487#ifdef FEAT_FIND_ID
4488 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004489 * Find the occurrence(s) of the identifier or define under cursor
4490 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 *
4492 * search list jump
4493 * fwd bwd fwd bwd fwd bwd
4494 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4495 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4496 */
4497 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004498# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004500# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004502# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 cap->nchar) != NULL)
4504 {
4505 char_u *ptr;
4506 int len;
4507
4508 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4509 clearop(cap->oap);
4510 else
4511 {
4512 find_pattern_in_path(ptr, 0, len, TRUE,
4513 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4514 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4515 cap->count1,
4516 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4517 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4518 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4519 (linenr_T)MAXLNUM);
4520 curwin->w_set_curswant = TRUE;
4521 }
4522 }
4523 else
4524#endif
4525
4526 /*
4527 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4528 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4529 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4530 * "[m" or "]m" search for prev/next start of (Java) method.
4531 * "[M" or "]M" search for prev/next end of (Java) method.
4532 */
4533 if ( (cap->cmdchar == '['
4534 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4535 || (cap->cmdchar == ']'
4536 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4537 {
4538 if (cap->nchar == '*')
4539 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 prev_pos.lnum = 0;
4541 if (cap->nchar == 'm' || cap->nchar == 'M')
4542 {
4543 if (cap->cmdchar == '[')
4544 findc = '{';
4545 else
4546 findc = '}';
4547 n = 9999;
4548 }
4549 else
4550 {
4551 findc = cap->nchar;
4552 n = cap->count1;
4553 }
4554 for ( ; n > 0; --n)
4555 {
4556 if ((pos = findmatchlimit(cap->oap, findc,
4557 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4558 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004559 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
4561 if (cap->nchar != 'm' && cap->nchar != 'M')
4562 clearopbeep(cap->oap);
4563 }
4564 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004565 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567 }
4568 prev_pos = new_pos;
4569 curwin->w_cursor = *pos;
4570 new_pos = *pos;
4571 }
4572 curwin->w_cursor = old_pos;
4573
4574 /*
4575 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4576 * brought us to the match for "[m" and "]M" when inside a method.
4577 * Try finding the '{' or '}' we want to be at.
4578 * Also repeat for the given count.
4579 */
4580 if (cap->nchar == 'm' || cap->nchar == 'M')
4581 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004582 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 int norm = ((findc == '{') == (cap->nchar == 'm'));
4584
4585 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004586 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 if (prev_pos.lnum != 0)
4588 {
4589 pos = &prev_pos;
4590 curwin->w_cursor = prev_pos;
4591 if (norm)
4592 --n;
4593 }
4594 else
4595 pos = NULL;
4596 while (n > 0)
4597 {
4598 for (;;)
4599 {
4600 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4601 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004602 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (pos == NULL)
4604 clearopbeep(cap->oap);
4605 n = 0;
4606 break;
4607 }
4608 c = gchar_cursor();
4609 if (c == '{' || c == '}')
4610 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004611 // Must have found end/start of class: use it.
4612 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if ((c == findc && norm) || (n == 1 && !norm))
4614 {
4615 new_pos = curwin->w_cursor;
4616 pos = &new_pos;
4617 n = 0;
4618 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004619 // if no match found at all, we started outside of the
4620 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 else if (new_pos.lnum == 0)
4622 {
4623 new_pos = curwin->w_cursor;
4624 pos = &new_pos;
4625 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004626 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 else if ((pos = findmatchlimit(cap->oap, findc,
4628 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4629 0)) == NULL)
4630 n = 0;
4631 else
4632 curwin->w_cursor = *pos;
4633 break;
4634 }
4635 }
4636 --n;
4637 }
4638 curwin->w_cursor = old_pos;
4639 if (pos == NULL && new_pos.lnum != 0)
4640 clearopbeep(cap->oap);
4641 }
4642 if (pos != NULL)
4643 {
4644 setpcmark();
4645 curwin->w_cursor = *pos;
4646 curwin->w_set_curswant = TRUE;
4647#ifdef FEAT_FOLDING
4648 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4649 && cap->oap->op_type == OP_NOP)
4650 foldOpenCursor();
4651#endif
4652 }
4653 }
4654
4655 /*
4656 * "[[", "[]", "]]" and "][": move to start or end of function
4657 */
4658 else if (cap->nchar == '[' || cap->nchar == ']')
4659 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004660 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 flag = '{';
4662 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004663 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664
4665 curwin->w_set_curswant = TRUE;
4666 /*
4667 * Imitate strange Vi behaviour: When using "]]" with an operator
4668 * we also stop at '}'.
4669 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004670 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 (cap->oap->op_type != OP_NOP
4672 && cap->arg == FORWARD && flag == '{')))
4673 clearopbeep(cap->oap);
4674 else
4675 {
4676 if (cap->oap->op_type == OP_NOP)
4677 beginline(BL_WHITE | BL_FIX);
4678#ifdef FEAT_FOLDING
4679 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4680 foldOpenCursor();
4681#endif
4682 }
4683 }
4684
4685 /*
4686 * "[p", "[P", "]P" and "]p": put with indent adjustment
4687 */
4688 else if (cap->nchar == 'p' || cap->nchar == 'P')
4689 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004690 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
4692
4693 /*
4694 * "['", "[`", "]'" and "]`": jump to next mark
4695 */
4696 else if (cap->nchar == '\'' || cap->nchar == '`')
4697 {
4698 pos = &curwin->w_cursor;
4699 for (n = cap->count1; n > 0; --n)
4700 {
4701 prev_pos = *pos;
4702 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4703 cap->nchar == '\'');
4704 if (pos == NULL)
4705 break;
4706 }
4707 if (pos == NULL)
4708 pos = &prev_pos;
4709 nv_cursormark(cap, cap->nchar == '\'', pos);
4710 }
4711
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 /*
4713 * [ or ] followed by a middle mouse click: put selected text with
4714 * indent adjustment. Any other button just does as usual.
4715 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004716 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
4718 (void)do_mouse(cap->oap, cap->nchar,
4719 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4720 cap->count1, PUT_FIXINDENT);
4721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
4723#ifdef FEAT_FOLDING
4724 /*
4725 * "[z" and "]z": move to start or end of open fold.
4726 */
4727 else if (cap->nchar == 'z')
4728 {
4729 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4730 cap->count1) == FAIL)
4731 clearopbeep(cap->oap);
4732 }
4733#endif
4734
4735#ifdef FEAT_DIFF
4736 /*
4737 * "[c" and "]c": move to next or previous diff-change.
4738 */
4739 else if (cap->nchar == 'c')
4740 {
4741 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4742 cap->count1) == FAIL)
4743 clearopbeep(cap->oap);
4744 }
4745#endif
4746
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004747#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004748 /*
4749 * "[s", "[S", "]s" and "]S": move to next spell error.
4750 */
4751 else if (cap->nchar == 's' || cap->nchar == 'S')
4752 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004753 setpcmark();
4754 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004755 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4756 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004757 {
4758 clearopbeep(cap->oap);
4759 break;
4760 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004761 else
4762 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004763# ifdef FEAT_FOLDING
4764 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4765 foldOpenCursor();
4766# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004767 }
4768#endif
4769
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004770 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 else
4772 clearopbeep(cap->oap);
4773}
4774
4775/*
4776 * Handle Normal mode "%" command.
4777 */
4778 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004779nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004782#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 linenr_T lnum = curwin->w_cursor.lnum;
4784#endif
4785
4786 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004787 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 {
4789 if (cap->count0 > 100)
4790 clearopbeep(cap->oap);
4791 else
4792 {
4793 cap->oap->motion_type = MLINE;
4794 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004795 // Round up, so 'normal 100%' always jumps at the line line.
4796 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4797 // overflow on 32-bits, so use a formula with less accuracy
4798 // to avoid overflows.
4799 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4801 / 100L * cap->count0;
4802 else
4803 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4804 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004805 if (curwin->w_cursor.lnum < 1)
4806 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4808 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4809 beginline(BL_SOL | BL_FIX);
4810 }
4811 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004812 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814 cap->oap->motion_type = MCHAR;
4815 cap->oap->use_reg_one = TRUE;
4816 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4817 clearopbeep(cap->oap);
4818 else
4819 {
4820 setpcmark();
4821 curwin->w_cursor = *pos;
4822 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826 }
4827#ifdef FEAT_FOLDING
4828 if (cap->oap->op_type == OP_NOP
4829 && lnum != curwin->w_cursor.lnum
4830 && (fdo_flags & FDO_PERCENT)
4831 && KeyTyped)
4832 foldOpenCursor();
4833#endif
4834}
4835
4836/*
4837 * Handle "(" and ")" commands.
4838 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4839 */
4840 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004841nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
4843 cap->oap->motion_type = MCHAR;
4844 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004845 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004846 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 curwin->w_set_curswant = TRUE;
4848
4849 if (findsent(cap->arg, cap->count1) == FAIL)
4850 clearopbeep(cap->oap);
4851 else
4852 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004853 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004854 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856#ifdef FEAT_FOLDING
4857 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4858 foldOpenCursor();
4859#endif
4860 }
4861}
4862
4863/*
4864 * "m" command: Mark a position.
4865 */
4866 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004867nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
4869 if (!checkclearop(cap->oap))
4870 {
4871 if (setmark(cap->nchar) == FAIL)
4872 clearopbeep(cap->oap);
4873 }
4874}
4875
4876/*
4877 * "{" and "}" commands.
4878 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4879 */
4880 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004881nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882{
4883 cap->oap->motion_type = MCHAR;
4884 cap->oap->inclusive = FALSE;
4885 cap->oap->use_reg_one = TRUE;
4886 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004887 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 clearopbeep(cap->oap);
4889 else
4890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#ifdef FEAT_FOLDING
4893 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4894 foldOpenCursor();
4895#endif
4896 }
4897}
4898
4899/*
4900 * "u" command: Undo or make lower case.
4901 */
4902 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004903nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004905 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004907 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 cap->cmdchar = 'g';
4909 cap->nchar = 'u';
4910 nv_operator(cap);
4911 }
4912 else
4913 nv_kundo(cap);
4914}
4915
4916/*
4917 * <Undo> command.
4918 */
4919 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004920nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
4922 if (!checkclearopq(cap->oap))
4923 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004924#ifdef FEAT_JOB_CHANNEL
4925 if (bt_prompt(curbuf))
4926 {
4927 clearopbeep(cap->oap);
4928 return;
4929 }
4930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 u_undo((int)cap->count1);
4932 curwin->w_set_curswant = TRUE;
4933 }
4934}
4935
4936/*
4937 * Handle the "r" command.
4938 */
4939 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004940nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
4942 char_u *ptr;
4943 int had_ctrl_v;
4944 long n;
4945
4946 if (checkclearop(cap->oap))
4947 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02004948#ifdef FEAT_JOB_CHANNEL
4949 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4950 {
4951 clearopbeep(cap->oap);
4952 return;
4953 }
4954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004956 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 if (cap->nchar == Ctrl_V)
4958 {
4959 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01004960 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (cap->nchar > DEL)
4963 had_ctrl_v = NUL;
4964 }
4965 else
4966 had_ctrl_v = NUL;
4967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004968 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00004969 if (IS_SPECIAL(cap->nchar))
4970 {
4971 clearopbeep(cap->oap);
4972 return;
4973 }
4974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004975 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if (VIsual_active)
4977 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00004978 if (got_int)
4979 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01004980 if (had_ctrl_v)
4981 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004982 // Use a special (negative) number to make a difference between a
4983 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01004984 if (cap->nchar == CAR)
4985 cap->nchar = REPLACE_CR_NCHAR;
4986 else if (cap->nchar == NL)
4987 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01004988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 nv_operator(cap);
4990 return;
4991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004993 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 if (virtual_active())
4995 {
4996 if (u_save_cursor() == FAIL)
4997 return;
4998 if (gchar_cursor() == NUL)
4999 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005000 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 coladvance_force((colnr_T)(getviscol() + cap->count1));
5002 curwin->w_cursor.col -= cap->count1;
5003 }
5004 else if (gchar_cursor() == TAB)
5005 coladvance_force(getviscol());
5006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005008 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005010 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005011 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 clearopbeep(cap->oap);
5014 return;
5015 }
5016
5017 /*
5018 * Replacing with a TAB is done by edit() when it is complicated because
5019 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5020 * Other characters are done below to avoid problems with things like
5021 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5022 */
5023 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5024 {
5025 stuffnumReadbuff(cap->count1);
5026 stuffcharReadbuff('R');
5027 stuffcharReadbuff('\t');
5028 stuffcharReadbuff(ESC);
5029 return;
5030 }
5031
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005032 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (u_save_cursor() == FAIL)
5034 return;
5035
5036 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5037 {
5038 /*
5039 * Replace character(s) by a single newline.
5040 * Strange vi behaviour: Only one newline is inserted.
5041 * Delete the characters here.
5042 * Insert the newline with an insert command, takes care of
5043 * autoindent. The insert command depends on being on the last
5044 * character of a line or not.
5045 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005046 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 stuffcharReadbuff('\r');
5048 stuffcharReadbuff(ESC);
5049
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005050 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 invoke_edit(cap, TRUE, 'r', FALSE);
5052 }
5053 else
5054 {
5055 prep_redo(cap->oap->regname, cap->count1,
5056 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5057
5058 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (has_mbyte)
5060 {
5061 int old_State = State;
5062
5063 if (cap->ncharC1 != 0)
5064 AppendCharToRedobuff(cap->ncharC1);
5065 if (cap->ncharC2 != 0)
5066 AppendCharToRedobuff(cap->ncharC2);
5067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005068 // This is slow, but it handles replacing a single-byte with a
5069 // multi-byte and the other way around. Also handles adding
5070 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 for (n = cap->count1; n > 0; --n)
5072 {
5073 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005074 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5075 {
5076 int c = ins_copychar(curwin->w_cursor.lnum
5077 + (cap->nchar == Ctrl_Y ? -1 : 1));
5078 if (c != NUL)
5079 ins_char(c);
5080 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005081 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005082 ++curwin->w_cursor.col;
5083 }
5084 else
5085 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 State = old_State;
5087 if (cap->ncharC1 != 0)
5088 ins_char(cap->ncharC1);
5089 if (cap->ncharC2 != 0)
5090 ins_char(cap->ncharC2);
5091 }
5092 }
5093 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 /*
5096 * Replace the characters within one line.
5097 */
5098 for (n = cap->count1; n > 0; --n)
5099 {
5100 /*
5101 * Get ptr again, because u_save and/or showmatch() will have
5102 * released the line. At the same time we let know that the
5103 * line will be changed.
5104 */
5105 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005106 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5107 {
5108 int c = ins_copychar(curwin->w_cursor.lnum
5109 + (cap->nchar == Ctrl_Y ? -1 : 1));
5110 if (c != NUL)
5111 ptr[curwin->w_cursor.col] = c;
5112 }
5113 else
5114 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (p_sm && msg_silent == 0)
5116 showmatch(cap->nchar);
5117 ++curwin->w_cursor.col;
5118 }
5119#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005120 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005122 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
Bram Moolenaar009b2592004-10-24 19:18:58 +00005124 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005125 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005127 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129#endif
5130
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005131 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 changed_bytes(curwin->w_cursor.lnum,
5133 (colnr_T)(curwin->w_cursor.col - cap->count1));
5134 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005135 --curwin->w_cursor.col; // cursor on the last replaced char
5136 // if the character on the left of the current cursor is a multi-byte
5137 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 if (has_mbyte)
5139 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 curbuf->b_op_end = curwin->w_cursor;
5141 curwin->w_set_curswant = TRUE;
5142 set_last_insert(cap->nchar);
5143 }
5144}
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146/*
5147 * 'o': Exchange start and end of Visual area.
5148 * 'O': same, but in block mode exchange left and right corners.
5149 */
5150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005151v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 pos_T old_cursor;
5154 colnr_T left, right;
5155
5156 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5157 {
5158 old_cursor = curwin->w_cursor;
5159 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5160 curwin->w_cursor.lnum = VIsual.lnum;
5161 coladvance(left);
5162 VIsual = curwin->w_cursor;
5163
5164 curwin->w_cursor.lnum = old_cursor.lnum;
5165 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005166 // 'selection "exclusive" and cursor at right-bottom corner: move it
5167 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5169 ++curwin->w_curswant;
5170 coladvance(curwin->w_curswant);
5171 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005173 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
5175 curwin->w_cursor.lnum = VIsual.lnum;
5176 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5177 ++right;
5178 coladvance(right);
5179 VIsual = curwin->w_cursor;
5180
5181 curwin->w_cursor.lnum = old_cursor.lnum;
5182 coladvance(left);
5183 curwin->w_curswant = left;
5184 }
5185 }
5186 else
5187 {
5188 old_cursor = curwin->w_cursor;
5189 curwin->w_cursor = VIsual;
5190 VIsual = old_cursor;
5191 curwin->w_set_curswant = TRUE;
5192 }
5193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195/*
5196 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5197 */
5198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005199nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005201 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
5203 cap->cmdchar = 'c';
5204 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005205 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 VIsual_mode = 'V';
5207 nv_operator(cap);
5208 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005209 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
5211 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005212 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 else
5214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (virtual_active())
5216 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5218 }
5219 }
5220}
5221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222/*
5223 * "gr".
5224 */
5225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005226nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (VIsual_active)
5229 {
5230 cap->cmdchar = 'r';
5231 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005232 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005234 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
5236 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005237 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 else
5239 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005240 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005241 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 stuffcharReadbuff(cap->extra_char);
5243 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 if (virtual_active())
5245 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 invoke_edit(cap, TRUE, 'v', FALSE);
5247 }
5248 }
5249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
5251/*
5252 * Swap case for "~" command, when it does not work like an operator.
5253 */
5254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005255n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256{
5257 long n;
5258 pos_T startpos;
5259 int did_change = 0;
5260#ifdef FEAT_NETBEANS_INTG
5261 pos_T pos;
5262 char_u *ptr;
5263 int count;
5264#endif
5265
5266 if (checkclearopq(cap->oap))
5267 return;
5268
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005269 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
5271 clearopbeep(cap->oap);
5272 return;
5273 }
5274
5275 prep_redo_cmd(cap);
5276
5277 if (u_save_cursor() == FAIL)
5278 return;
5279
5280 startpos = curwin->w_cursor;
5281#ifdef FEAT_NETBEANS_INTG
5282 pos = startpos;
5283#endif
5284 for (n = cap->count1; n > 0; --n)
5285 {
5286 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5287 inc_cursor();
5288 if (gchar_cursor() == NUL)
5289 {
5290 if (vim_strchr(p_ww, '~') != NULL
5291 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5292 {
5293#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005294 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
5296 if (did_change)
5297 {
5298 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005299 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005300 netbeans_removed(curbuf, pos.lnum, pos.col,
5301 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005303 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 pos.col = 0;
5306 pos.lnum++;
5307 }
5308#endif
5309 ++curwin->w_cursor.lnum;
5310 curwin->w_cursor.col = 0;
5311 if (n > 1)
5312 {
5313 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5314 break;
5315 u_clearline();
5316 }
5317 }
5318 else
5319 break;
5320 }
5321 }
5322#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005323 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
5325 ptr = ml_get(pos.lnum);
5326 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005327 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5328 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330#endif
5331
5332
5333 check_cursor();
5334 curwin->w_set_curswant = TRUE;
5335 if (did_change)
5336 {
5337 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5338 0L);
5339 curbuf->b_op_start = startpos;
5340 curbuf->b_op_end = curwin->w_cursor;
5341 if (curbuf->b_op_end.col > 0)
5342 --curbuf->b_op_end.col;
5343 }
5344}
5345
5346/*
5347 * Move cursor to mark.
5348 */
5349 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005350nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
5352 if (check_mark(pos) == FAIL)
5353 clearop(cap->oap);
5354 else
5355 {
5356 if (cap->cmdchar == '\''
5357 || cap->cmdchar == '`'
5358 || cap->cmdchar == '['
5359 || cap->cmdchar == ']')
5360 setpcmark();
5361 curwin->w_cursor = *pos;
5362 if (flag)
5363 beginline(BL_WHITE | BL_FIX);
5364 else
5365 check_cursor();
5366 }
5367 cap->oap->motion_type = flag ? MLINE : MCHAR;
5368 if (cap->cmdchar == '`')
5369 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005370 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 curwin->w_set_curswant = TRUE;
5372}
5373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374/*
5375 * Handle commands that are operators in Visual mode.
5376 */
5377 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005378v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5381
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005382 // Uppercase means linewise, except in block mode, then "D" deletes till
5383 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 if (isupper(cap->cmdchar))
5385 {
5386 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005387 {
5388 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5392 curwin->w_curswant = MAXCOL;
5393 }
5394 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5395 nv_operator(cap);
5396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398/*
5399 * "s" and "S" commands.
5400 */
5401 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005402nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005404#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005405 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005406 if (term_swap_diff() == OK)
5407 return;
5408#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005409#ifdef FEAT_JOB_CHANNEL
5410 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5411 {
5412 clearopbeep(cap->oap);
5413 return;
5414 }
5415#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005416 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
5418 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005419 {
5420 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 cap->cmdchar = 'c';
5424 nv_operator(cap);
5425 }
5426 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 nv_optrans(cap);
5428}
5429
5430/*
5431 * Abbreviated commands.
5432 */
5433 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005434nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005437 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005439 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 if (VIsual_active)
5441 v_visop(cap);
5442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 nv_optrans(cap);
5444}
5445
5446/*
5447 * Translate a command into another command.
5448 */
5449 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005450nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5453 (char_u *)"d$", (char_u *)"c$",
5454 (char_u *)"cl", (char_u *)"cc",
5455 (char_u *)"yy", (char_u *)":s\r"};
5456 static char_u *str = (char_u *)"xXDCsSY&";
5457
5458 if (!checkclearopq(cap->oap))
5459 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005460 // In Vi "2D" doesn't delete the next line. Can't translate it
5461 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005462 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5463 {
5464 cap->oap->start = curwin->w_cursor;
5465 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005466#ifdef FEAT_EVAL
5467 set_op_var(OP_DELETE);
5468#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005469 cap->count1 = 1;
5470 nv_dollar(cap);
5471 finish_op = TRUE;
5472 ResetRedobuff();
5473 AppendCharToRedobuff('D');
5474 }
5475 else
5476 {
5477 if (cap->count0)
5478 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005479 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
5482 cap->opcount = 0;
5483}
5484
5485/*
5486 * "'" and "`" commands. Also for "g'" and "g`".
5487 * cap->arg is TRUE for "'" and "g'".
5488 */
5489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005490nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
5492 pos_T *pos;
5493 int c;
5494#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005495 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005496 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497#endif
5498
5499 if (cap->cmdchar == 'g')
5500 c = cap->extra_char;
5501 else
5502 c = cap->nchar;
5503 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005504 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 if (cap->arg)
5507 {
5508 check_cursor_lnum();
5509 beginline(BL_WHITE | BL_FIX);
5510 }
5511 else
5512 check_cursor();
5513 }
5514 else
5515 nv_cursormark(cap, cap->arg, pos);
5516
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005517 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (!virtual_active())
5519 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005520 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#ifdef FEAT_FOLDING
5522 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005523 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005524 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 && (fdo_flags & FDO_MARK)
5526 && old_KeyTyped)
5527 foldOpenCursor();
5528#endif
5529}
5530
5531/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005532 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 */
5534 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005535nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537#ifdef FEAT_JUMPLIST
5538 pos_T *pos;
5539# ifdef FEAT_FOLDING
5540 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005541 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542# endif
5543
5544 if (!checkclearopq(cap->oap))
5545 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005546 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5547 {
5548 if (goto_tabpage_lastused() == FAIL)
5549 clearopbeep(cap->oap);
5550 return;
5551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 if (cap->cmdchar == 'g')
5553 pos = movechangelist((int)cap->count1);
5554 else
5555 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005556 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
5558 curwin->w_set_curswant = TRUE;
5559 check_cursor();
5560 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005561 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 nv_cursormark(cap, FALSE, pos);
5563 else if (cap->cmdchar == 'g')
5564 {
5565 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005566 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005568 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005570 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572 else
5573 clearopbeep(cap->oap);
5574# ifdef FEAT_FOLDING
5575 if (cap->oap->op_type == OP_NOP
5576 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5577 && (fdo_flags & FDO_MARK)
5578 && old_KeyTyped)
5579 foldOpenCursor();
5580# endif
5581 }
5582#else
5583 clearopbeep(cap->oap);
5584#endif
5585}
5586
5587/*
5588 * Handle '"' command.
5589 */
5590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005591nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 if (checkclearop(cap->oap))
5594 return;
5595#ifdef FEAT_EVAL
5596 if (cap->nchar == '=')
5597 cap->nchar = get_expr_register();
5598#endif
5599 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5600 {
5601 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005602 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603#ifdef FEAT_EVAL
5604 set_reg_var(cap->oap->regname);
5605#endif
5606 }
5607 else
5608 clearopbeep(cap->oap);
5609}
5610
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611/*
5612 * Handle "v", "V" and "CTRL-V" commands.
5613 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5614 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005615 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 */
5617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005618nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005620 if (cap->cmdchar == Ctrl_Q)
5621 cap->cmdchar = Ctrl_V;
5622
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005623 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5624 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 if (cap->oap->op_type != OP_NOP)
5626 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005627 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005628 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 return;
5630 }
5631
5632 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005633 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005635 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005637 else // toggle char/block mode
5638 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 VIsual_mode = cap->cmdchar;
5640 showmode();
5641 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005642 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005644 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
5646 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005647 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005649 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 VIsual = curwin->w_cursor;
5651
5652 VIsual_active = TRUE;
5653 VIsual_reselect = TRUE;
5654 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005655 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005658 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005659 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 /*
5661 * For V and ^V, we multiply the number of lines even if there
5662 * was only one -- webb
5663 */
5664 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5665 {
5666 curwin->w_cursor.lnum +=
5667 resel_VIsual_line_count * cap->count0 - 1;
5668 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5669 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5670 }
5671 VIsual_mode = resel_VIsual_mode;
5672 if (VIsual_mode == 'v')
5673 {
5674 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005675 {
5676 validate_virtcol();
5677 curwin->w_curswant = curwin->w_virtcol
5678 + resel_VIsual_vcol * cap->count0 - 1;
5679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005681 curwin->w_curswant = resel_VIsual_vcol;
5682 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005684 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
5686 curwin->w_curswant = MAXCOL;
5687 coladvance((colnr_T)MAXCOL);
5688 }
5689 else if (VIsual_mode == Ctrl_V)
5690 {
5691 validate_virtcol();
5692 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005693 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 coladvance(curwin->w_curswant);
5695 }
5696 else
5697 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005698 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 else
5701 {
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');
5705 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005706 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005707 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005708 if (cap->count0 > 0 && --cap->count1 > 0)
5709 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005710 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005711 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5712 nv_right(cap);
5713 else if (VIsual_mode == 'V')
5714 nv_down(cap);
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 }
5718}
5719
5720/*
5721 * Start selection for Shift-movement keys.
5722 */
5723 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005724start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005726 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 may_start_select('k');
5728 n_start_visual_mode('v');
5729}
5730
5731/*
5732 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5733 */
5734 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005735may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737 VIsual_select = (stuff_empty() && typebuf_typed()
5738 && (vim_strchr(p_slm, c) != NULL));
5739}
5740
5741/*
5742 * Start Visual mode "c".
5743 * Should set VIsual_select before calling this.
5744 */
5745 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005746n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005748#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005749 int cursor_line_was_concealed = curwin->w_p_cole > 0
5750 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005751#endif
5752
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 VIsual_mode = c;
5754 VIsual_active = TRUE;
5755 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005756
5757 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005758 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005759 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005760 {
5761 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 VIsual = curwin->w_cursor;
5765
5766#ifdef FEAT_FOLDING
5767 foldAdjustVisual();
5768#endif
5769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005771#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005772 // Check if redraw is needed after changing the state.
5773 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005774#endif
5775
Bram Moolenaar09df3122006-01-23 22:23:09 +00005776 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005777 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005779 // Make sure the clipboard gets updated. Needed because start and
5780 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 clip_star.vmode = NUL;
5782#endif
5783
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005784 // Only need to redraw this line, unless still need to redraw an old
5785 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 if (curwin->w_redr_type < INVERTED)
5787 {
5788 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5789 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5790 }
5791}
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794/*
5795 * CTRL-W: Window commands
5796 */
5797 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005798nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005800 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005801 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005802 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005803 cap->cmdchar = ':';
5804 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005805 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005806 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005807 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005808 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809}
5810
5811/*
5812 * CTRL-Z: Suspend
5813 */
5814 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005815nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
5817 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005819 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005820 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821}
5822
5823/*
5824 * Commands starting with "g".
5825 */
5826 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005827nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
5829 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 int i;
5832 int flag = FALSE;
5833
5834 switch (cap->nchar)
5835 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005836 case Ctrl_A:
5837 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838#ifdef MEM_PROFILE
5839 /*
5840 * "g^A": dump log of used memory.
5841 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005842 if (!VIsual_active && cap->nchar == Ctrl_A)
5843 vim_mem_profile_dump();
5844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005846 /*
5847 * "g^A/g^X": sequentially increment visually selected region
5848 */
5849 if (VIsual_active)
5850 {
5851 cap->arg = TRUE;
5852 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005853 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005854 nv_addsub(cap);
5855 }
5856 else
5857 clearopbeep(oap);
5858 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 /*
5861 * "gR": Enter virtual replace mode.
5862 */
5863 case 'R':
5864 cap->arg = TRUE;
5865 nv_Replace(cap);
5866 break;
5867
5868 case 'r':
5869 nv_vreplace(cap);
5870 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
5872 case '&':
5873 do_cmdline_cmd((char_u *)"%s//~/&");
5874 break;
5875
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 /*
5877 * "gv": Reselect the previous Visual area. If Visual already active,
5878 * exchange previous and current Visual area.
5879 */
5880 case 'v':
5881 if (checkclearop(oap))
5882 break;
5883
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005884 if ( curbuf->b_visual.vi_start.lnum == 0
5885 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5886 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 beep_flush();
5888 else
5889 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005890 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 if (VIsual_active)
5892 {
5893 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005894 VIsual_mode = curbuf->b_visual.vi_mode;
5895 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896# ifdef FEAT_EVAL
5897 curbuf->b_visual_mode_eval = i;
5898# endif
5899 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005900 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5901 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005903 tpos = curbuf->b_visual.vi_end;
5904 curbuf->b_visual.vi_end = curwin->w_cursor;
5905 curwin->w_cursor = curbuf->b_visual.vi_start;
5906 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908 else
5909 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005910 VIsual_mode = curbuf->b_visual.vi_mode;
5911 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5912 tpos = curbuf->b_visual.vi_end;
5913 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
5915
5916 VIsual_active = TRUE;
5917 VIsual_reselect = TRUE;
5918
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005919 // Set Visual to the start and w_cursor to the end of the Visual
5920 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 check_cursor();
5922 VIsual = curwin->w_cursor;
5923 curwin->w_cursor = tpos;
5924 check_cursor();
5925 update_topline();
5926 /*
5927 * When called from normal "g" command: start Select mode when
5928 * 'selectmode' contains "cmd". When called for K_SELECT, always
5929 * start Select mode.
5930 */
5931 if (cap->arg)
5932 VIsual_select = TRUE;
5933 else
5934 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005937 // Make sure the clipboard gets updated. Needed because start and
5938 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 clip_star.vmode = NUL;
5940#endif
5941 redraw_curbuf_later(INVERTED);
5942 showmode();
5943 }
5944 break;
5945 /*
5946 * "gV": Don't reselect the previous Visual area after a Select mode
5947 * mapping of menu.
5948 */
5949 case 'V':
5950 VIsual_reselect = FALSE;
5951 break;
5952
5953 /*
5954 * "gh": start Select mode.
5955 * "gH": start Select line mode.
5956 * "g^H": start Select block mode.
5957 */
5958 case K_BS:
5959 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005960 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 case 'h':
5962 case 'H':
5963 case Ctrl_H:
5964# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005965 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 if (cap->nchar == Ctrl_H)
5967 cap->cmdchar = Ctrl_V;
5968 else
5969# endif
5970 cap->cmdchar = cap->nchar + ('v' - 'h');
5971 cap->arg = TRUE;
5972 nv_visual(cap);
5973 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02005974
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005975 // "gn", "gN" visually select next/previous search match
5976 // "gn" selects next match
5977 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02005978 case 'N':
5979 case 'n':
5980 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02005981 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02005982 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983
5984 /*
5985 * "gj" and "gk" two new funny movement keys -- up and down
5986 * movement based on *screen* line rather than *file* line.
5987 */
5988 case 'j':
5989 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01005990 // with 'nowrap' it works just like the normal "j" command.
5991 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {
5993 oap->motion_type = MLINE;
5994 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
5995 }
5996 else
5997 i = nv_screengo(oap, FORWARD, cap->count1);
5998 if (i == FAIL)
5999 clearopbeep(oap);
6000 break;
6001
6002 case 'k':
6003 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006004 // with 'nowrap' it works just like the normal "k" command.
6005 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 {
6007 oap->motion_type = MLINE;
6008 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6009 }
6010 else
6011 i = nv_screengo(oap, BACKWARD, cap->count1);
6012 if (i == FAIL)
6013 clearopbeep(oap);
6014 break;
6015
6016 /*
6017 * "gJ": join two lines without inserting a space.
6018 */
6019 case 'J':
6020 nv_join(cap);
6021 break;
6022
6023 /*
6024 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6025 * "gm": middle of "g0" and "g$".
6026 */
6027 case '^':
6028 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006029 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
6031 case '0':
6032 case 'm':
6033 case K_HOME:
6034 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 oap->motion_type = MCHAR;
6036 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006037 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006039 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 int width2 = width1 + curwin_col_off2();
6041
6042 validate_virtcol();
6043 i = 0;
6044 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6045 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6046 }
6047 else
6048 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006049 // Go to the middle of the screen line. When 'number' or
6050 // 'relativenumber' is on and lines are wrapping the middle can be more
6051 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006053 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 + ((curwin->w_p_wrap && i > 0)
6055 ? curwin_col_off2() : 0)) / 2;
6056 coladvance((colnr_T)i);
6057 if (flag)
6058 {
6059 do
6060 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006061 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006062 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 }
6064 curwin->w_set_curswant = TRUE;
6065 break;
6066
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006067 case 'M':
6068 {
6069 char_u *ptr = ml_get_curline();
6070
6071 oap->motion_type = MCHAR;
6072 oap->inclusive = FALSE;
6073 if (has_mbyte)
Bram Moolenaar69bf6342019-10-29 04:16:57 +01006074 i = mb_string2cells(ptr, (int)STRLEN(ptr));
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006075 else
6076 i = (int)STRLEN(ptr);
6077 if (cap->count0 > 0 && cap->count0 <= 100)
6078 coladvance((colnr_T)(i * cap->count0 / 100));
6079 else
6080 coladvance((colnr_T)(i / 2));
6081 curwin->w_set_curswant = TRUE;
6082 }
6083 break;
6084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006086 // "g_": to the last non-blank character in the line or <count> lines
6087 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 cap->oap->motion_type = MCHAR;
6089 cap->oap->inclusive = TRUE;
6090 curwin->w_curswant = MAXCOL;
6091 if (cursor_down((long)(cap->count1 - 1),
6092 cap->oap->op_type == OP_NOP) == FAIL)
6093 clearopbeep(cap->oap);
6094 else
6095 {
6096 char_u *ptr = ml_get_curline();
6097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006098 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6100 --curwin->w_cursor.col;
6101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006102 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006104 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 --curwin->w_cursor.col;
6106 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006107 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 }
6109 break;
6110
6111 case '$':
6112 case K_END:
6113 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 {
6115 int col_off = curwin_col_off();
6116
6117 oap->motion_type = MCHAR;
6118 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006119 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006121 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 if (cap->count1 == 1)
6123 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006124 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 int width2 = width1 + curwin_col_off2();
6126
6127 validate_virtcol();
6128 i = width1 - 1;
6129 if (curwin->w_virtcol >= (colnr_T)width1)
6130 i += ((curwin->w_virtcol - width1) / width2 + 1)
6131 * width2;
6132 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006133
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006134 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006135 validate_virtcol();
6136 curwin->w_curswant = curwin->w_virtcol;
6137 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6139 {
6140 /*
6141 * Check for landing on a character that got split at
6142 * the end of the line. We do not want to advance to
6143 * the next screen line.
6144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (curwin->w_virtcol > (colnr_T)i)
6146 --curwin->w_cursor.col;
6147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
6149 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6150 clearopbeep(oap);
6151 }
6152 else
6153 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006154 if (cap->count1 > 1)
6155 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006156 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006157
Bram Moolenaar02631462017-09-22 15:20:32 +02006158 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006160
Bram Moolenaar74ede802021-05-29 19:18:01 +02006161 // if the character doesn't fit move one back
6162 if (curwin->w_cursor.col > 0
6163 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6164 {
6165 colnr_T vcol;
6166
6167 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6168 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6169 --curwin->w_cursor.col;
6170 }
6171
Bram Moolenaard5c82342019-07-27 18:44:57 +02006172 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006173 validate_virtcol();
6174 curwin->w_curswant = curwin->w_virtcol;
6175 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 }
6177 }
6178 break;
6179
6180 /*
6181 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6182 */
6183 case '*':
6184 case '#':
6185#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006186 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006188 case Ctrl_RSB: // :tag or :tselect for current identifier
6189 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 nv_ident(cap);
6191 break;
6192
6193 /*
6194 * ge and gE: go back to end of word
6195 */
6196 case 'e':
6197 case 'E':
6198 oap->motion_type = MCHAR;
6199 curwin->w_set_curswant = TRUE;
6200 oap->inclusive = TRUE;
6201 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6202 clearopbeep(oap);
6203 break;
6204
6205 /*
6206 * "g CTRL-G": display info about cursor position
6207 */
6208 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006209 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 break;
6211
6212 /*
6213 * "gi": start Insert at the last position.
6214 */
6215 case 'i':
6216 if (curbuf->b_last_insert.lnum != 0)
6217 {
6218 curwin->w_cursor = curbuf->b_last_insert;
6219 check_cursor_lnum();
6220 i = (int)STRLEN(ml_get_curline());
6221 if (curwin->w_cursor.col > (colnr_T)i)
6222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (virtual_active())
6224 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 curwin->w_cursor.col = i;
6226 }
6227 }
6228 cap->cmdchar = 'i';
6229 nv_edit(cap);
6230 break;
6231
6232 /*
6233 * "gI": Start insert in column 1.
6234 */
6235 case 'I':
6236 beginline(0);
6237 if (!checkclearopq(oap))
6238 invoke_edit(cap, FALSE, 'g', FALSE);
6239 break;
6240
6241#ifdef FEAT_SEARCHPATH
6242 /*
6243 * "gf": goto file, edit file under cursor
6244 * "]f" and "[f": can also be used.
6245 */
6246 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006247 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 nv_gotofile(cap);
6249 break;
6250#endif
6251
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006252 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 case '\'':
6254 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006255 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 case '`':
6257 nv_gomark(cap);
6258 break;
6259
6260 /*
6261 * "gs": Goto sleep.
6262 */
6263 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006264 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 break;
6266
6267 /*
6268 * "ga": Display the ascii value of the character under the
6269 * cursor. It is displayed in decimal, hex, and octal. -- webb
6270 */
6271 case 'a':
6272 do_ascii(NULL);
6273 break;
6274
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 /*
6276 * "g8": Display the bytes used for the UTF-8 character under the
6277 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006278 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 */
6280 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006281 if (cap->count0 == 8)
6282 utf_find_illegal();
6283 else
6284 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006287 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006288 case '<':
6289 show_sb_text();
6290 break;
6291
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 /*
6293 * "gg": Goto the first line in file. With a count it goes to
6294 * that line number like for "G". -- webb
6295 */
6296 case 'g':
6297 cap->arg = FALSE;
6298 nv_goto(cap);
6299 break;
6300
6301 /*
6302 * Two-character operators:
6303 * "gq" Format text
6304 * "gw" Format text and keep cursor position
6305 * "g~" Toggle the case of the text.
6306 * "gu" Change text to lower case.
6307 * "gU" Change text to upper case.
6308 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006309 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 */
6311 case 'q':
6312 case 'w':
6313 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006314 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 case '~':
6316 case 'u':
6317 case 'U':
6318 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006319 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 nv_operator(cap);
6321 break;
6322
6323 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006324 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 * current function
6326 * "gD": idem, but in the current file.
6327 */
6328 case 'd':
6329 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006330 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 break;
6332
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 /*
6334 * g<*Mouse> : <C-*mouse>
6335 */
6336 case K_MIDDLEMOUSE:
6337 case K_MIDDLEDRAG:
6338 case K_MIDDLERELEASE:
6339 case K_LEFTMOUSE:
6340 case K_LEFTDRAG:
6341 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006342 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 case K_RIGHTMOUSE:
6344 case K_RIGHTDRAG:
6345 case K_RIGHTRELEASE:
6346 case K_X1MOUSE:
6347 case K_X1DRAG:
6348 case K_X1RELEASE:
6349 case K_X2MOUSE:
6350 case K_X2DRAG:
6351 case K_X2RELEASE:
6352 mod_mask = MOD_MASK_CTRL;
6353 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
6356 case K_IGNORE:
6357 break;
6358
6359 /*
6360 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6361 */
6362 case 'p':
6363 case 'P':
6364 nv_put(cap);
6365 break;
6366
6367#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006368 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 case 'o':
6370 goto_byte(cap->count0);
6371 break;
6372#endif
6373
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006374 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006376 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 {
6378 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006379 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 break;
6381 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006382
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (!checkclearopq(oap))
6384 do_exmode(TRUE);
6385 break;
6386
6387#ifdef FEAT_JUMPLIST
6388 case ',':
6389 nv_pcmark(cap);
6390 break;
6391
6392 case ';':
6393 cap->count1 = -cap->count1;
6394 nv_pcmark(cap);
6395 break;
6396#endif
6397
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006398 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006399 if (!checkclearop(oap))
6400 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006402 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006403 if (!checkclearop(oap))
6404 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006405 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006406
Bram Moolenaar62a23252020-08-09 14:04:42 +02006407 case TAB:
6408 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6409 clearopbeep(oap);
6410 break;
6411
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006412 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006413 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006414 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006415 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006416 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006417 break;
6418
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 default:
6420 clearopbeep(oap);
6421 break;
6422 }
6423}
6424
6425/*
6426 * Handle "o" and "O" commands.
6427 */
6428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006429n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006431#ifdef FEAT_CONCEAL
6432 linenr_T oldline = curwin->w_cursor.lnum;
6433#endif
6434
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 if (!checkclearopq(cap->oap))
6436 {
6437#ifdef FEAT_FOLDING
6438 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006439 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 (void)hasFolding(curwin->w_cursor.lnum,
6441 &curwin->w_cursor.lnum, NULL);
6442 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006443 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 (void)hasFolding(curwin->w_cursor.lnum,
6445 NULL, &curwin->w_cursor.lnum);
6446#endif
6447 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6448 (cap->cmdchar == 'O' ? 1 : 0)),
6449 (linenr_T)(curwin->w_cursor.lnum +
6450 (cap->cmdchar == 'o' ? 1 : 0))
6451 ) == OK
6452 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006453 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
6454 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006456#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006457 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006458 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006459#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006460#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006461 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006462 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006463 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006464#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006465 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006466 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6467 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6469 }
6470 }
6471}
6472
6473/*
6474 * "." command: redo last change.
6475 */
6476 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006477nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479 if (!checkclearopq(cap->oap))
6480 {
6481 /*
6482 * If "restart_edit" is TRUE, the last but one command is repeated
6483 * instead of the last command (inserting text). This is used for
6484 * CTRL-O <.> in insert mode.
6485 */
6486 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6487 clearopbeep(cap->oap);
6488 }
6489}
6490
6491/*
6492 * CTRL-R: undo undo
6493 */
6494 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006495nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 if (!checkclearopq(cap->oap))
6498 {
6499 u_redo((int)cap->count1);
6500 curwin->w_set_curswant = TRUE;
6501 }
6502}
6503
6504/*
6505 * Handle "U" command.
6506 */
6507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006508nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006510 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006511 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006513 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 cap->cmdchar = 'g';
6515 cap->nchar = 'U';
6516 nv_operator(cap);
6517 }
6518 else if (!checkclearopq(cap->oap))
6519 {
6520 u_undoline();
6521 curwin->w_set_curswant = TRUE;
6522 }
6523}
6524
6525/*
6526 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6527 * single character.
6528 */
6529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006530nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006532 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006533 {
6534#ifdef FEAT_JOB_CHANNEL
6535 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6536 {
6537 clearopbeep(cap->oap);
6538 return;
6539 }
6540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 else
6544 nv_operator(cap);
6545}
6546
6547/*
6548 * Handle an operator command.
6549 * The actual work is done by do_pending_operator().
6550 */
6551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006552nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
6554 int op_type;
6555
6556 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006557#ifdef FEAT_JOB_CHANNEL
6558 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6559 {
6560 clearopbeep(cap->oap);
6561 return;
6562 }
6563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006565 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 nv_lineop(cap);
6567 else if (!checkclearop(cap->oap))
6568 {
6569 cap->oap->start = curwin->w_cursor;
6570 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006571#ifdef FEAT_EVAL
6572 set_op_var(op_type);
6573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
6575}
6576
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006577#ifdef FEAT_EVAL
6578/*
6579 * Set v:operator to the characters for "optype".
6580 */
6581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006582set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006583{
6584 char_u opchars[3];
6585
6586 if (optype == OP_NOP)
6587 set_vim_var_string(VV_OP, NULL, 0);
6588 else
6589 {
6590 opchars[0] = get_op_char(optype);
6591 opchars[1] = get_extra_op_char(optype);
6592 opchars[2] = NUL;
6593 set_vim_var_string(VV_OP, opchars, -1);
6594 }
6595}
6596#endif
6597
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598/*
6599 * Handle linewise operator "dd", "yy", etc.
6600 *
6601 * "_" is is a strange motion command that helps make operators more logical.
6602 * It is actually implemented, but not documented in the real Vi. This motion
6603 * command actually refers to "the current line". Commands like "dd" and "yy"
6604 * are really an alternate form of "d_" and "y_". It does accept a count, so
6605 * "d3_" works to delete 3 lines.
6606 */
6607 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006608nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609{
6610 cap->oap->motion_type = MLINE;
6611 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6612 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006613 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006614 && cap->oap->motion_force != 'v'
6615 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 || cap->oap->op_type == OP_LSHIFT
6617 || cap->oap->op_type == OP_RSHIFT)
6618 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006619 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 beginline(BL_WHITE | BL_FIX);
6621}
6622
6623/*
6624 * <Home> command.
6625 */
6626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006627nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006629 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006630 if (mod_mask & MOD_MASK_CTRL)
6631 nv_goto(cap);
6632 else
6633 {
6634 cap->count0 = 1;
6635 nv_pipe(cap);
6636 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006637 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6638 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639}
6640
6641/*
6642 * "|" command.
6643 */
6644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006645nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 cap->oap->motion_type = MCHAR;
6648 cap->oap->inclusive = FALSE;
6649 beginline(0);
6650 if (cap->count0 > 0)
6651 {
6652 coladvance((colnr_T)(cap->count0 - 1));
6653 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6654 }
6655 else
6656 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006657 // keep curswant at the column where we wanted to go, not where
6658 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 curwin->w_set_curswant = FALSE;
6660}
6661
6662/*
6663 * Handle back-word command "b" and "B".
6664 * cap->arg is 1 for "B"
6665 */
6666 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006667nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 cap->oap->motion_type = MCHAR;
6670 cap->oap->inclusive = FALSE;
6671 curwin->w_set_curswant = TRUE;
6672 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6673 clearopbeep(cap->oap);
6674#ifdef FEAT_FOLDING
6675 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6676 foldOpenCursor();
6677#endif
6678}
6679
6680/*
6681 * Handle word motion commands "e", "E", "w" and "W".
6682 * cap->arg is TRUE for "E" and "W".
6683 */
6684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006685nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 int n;
6688 int word_end;
6689 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006690 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 /*
6693 * Set inclusive for the "E" and "e" command.
6694 */
6695 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6696 word_end = TRUE;
6697 else
6698 word_end = FALSE;
6699 cap->oap->inclusive = word_end;
6700
6701 /*
6702 * "cw" and "cW" are a special case.
6703 */
6704 if (!word_end && cap->oap->op_type == OP_CHANGE)
6705 {
6706 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006707 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006709 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 {
6711 /*
6712 * Reproduce a funny Vi behaviour: "cw" on a blank only
6713 * changes one character, not all blanks until the start of
6714 * the next word. Only do this when the 'w' flag is included
6715 * in 'cpoptions'.
6716 */
6717 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6718 {
6719 cap->oap->inclusive = TRUE;
6720 cap->oap->motion_type = MCHAR;
6721 return;
6722 }
6723 }
6724 else
6725 {
6726 /*
6727 * This is a little strange. To match what the real Vi does,
6728 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6729 * that we are not on a space or a TAB. This seems impolite
6730 * at first, but it's really more what we mean when we say
6731 * 'cw'.
6732 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006733 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 * will change only one character! This is done by setting
6735 * flag.
6736 */
6737 cap->oap->inclusive = TRUE;
6738 word_end = TRUE;
6739 flag = TRUE;
6740 }
6741 }
6742 }
6743
6744 cap->oap->motion_type = MCHAR;
6745 curwin->w_set_curswant = TRUE;
6746 if (word_end)
6747 n = end_word(cap->count1, cap->arg, flag, FALSE);
6748 else
6749 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6750
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006751 // Don't leave the cursor on the NUL past the end of line. Unless we
6752 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006753 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006754 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
6756 if (n == FAIL && cap->oap->op_type == OP_NOP)
6757 clearopbeep(cap->oap);
6758 else
6759 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761#ifdef FEAT_FOLDING
6762 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6763 foldOpenCursor();
6764#endif
6765 }
6766}
6767
6768/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006769 * Used after a movement command: If the cursor ends up on the NUL after the
6770 * end of the line, may move it back to the last character and make the motion
6771 * inclusive.
6772 */
6773 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006774adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006775{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006776 // The cursor cannot remain on the NUL when:
6777 // - the column is > 0
6778 // - not in Visual mode or 'selection' is "o"
6779 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006780 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006781 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006782 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006783 {
6784 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006785 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006786 if (has_mbyte)
6787 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006788 oap->inclusive = TRUE;
6789 }
6790}
6791
6792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 * "0" and "^" commands.
6794 * cap->arg is the argument for beginline().
6795 */
6796 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006797nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
6799 cap->oap->motion_type = MCHAR;
6800 cap->oap->inclusive = FALSE;
6801 beginline(cap->arg);
6802#ifdef FEAT_FOLDING
6803 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6804 foldOpenCursor();
6805#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006806 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6807 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808}
6809
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810/*
6811 * In exclusive Visual mode, may include the last character.
6812 */
6813 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006814adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815{
6816 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006817 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (has_mbyte)
6820 inc_cursor();
6821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 ++curwin->w_cursor.col;
6823 cap->oap->inclusive = FALSE;
6824 }
6825}
6826
6827/*
6828 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6829 * Should check VIsual_mode before calling this.
6830 * Returns TRUE when backed up to the previous line.
6831 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006832 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006833unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
6835 pos_T *pp;
6836
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006837 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006839 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 pp = &curwin->w_cursor;
6841 else
6842 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 if (pp->coladd > 0)
6844 --pp->coladd;
6845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 if (pp->col > 0)
6847 {
6848 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006849 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851 else if (pp->lnum > 1)
6852 {
6853 --pp->lnum;
6854 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6855 return TRUE;
6856 }
6857 }
6858 return FALSE;
6859}
6860
6861/*
6862 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6863 */
6864 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006865nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
6867 if (VIsual_active)
6868 VIsual_select = TRUE;
6869 else if (VIsual_reselect)
6870 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006871 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 cap->arg = TRUE;
6873 nv_g_cmd(cap);
6874 }
6875}
6876
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
6878/*
6879 * "G", "gg", CTRL-END, CTRL-HOME.
6880 * cap->arg is TRUE for "G".
6881 */
6882 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006883nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884{
6885 linenr_T lnum;
6886
6887 if (cap->arg)
6888 lnum = curbuf->b_ml.ml_line_count;
6889 else
6890 lnum = 1L;
6891 cap->oap->motion_type = MLINE;
6892 setpcmark();
6893
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006894 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (cap->count0 != 0)
6896 lnum = cap->count0;
6897 if (lnum < 1L)
6898 lnum = 1L;
6899 else if (lnum > curbuf->b_ml.ml_line_count)
6900 lnum = curbuf->b_ml.ml_line_count;
6901 curwin->w_cursor.lnum = lnum;
6902 beginline(BL_SOL | BL_FIX);
6903#ifdef FEAT_FOLDING
6904 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6905 foldOpenCursor();
6906#endif
6907}
6908
6909/*
6910 * CTRL-\ in Normal mode.
6911 */
6912 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006913nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6916 {
6917 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006918 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006919 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 restart_edit = 0;
6921#ifdef FEAT_CMDWIN
6922 if (cmdwin_type != 0)
6923 cmdwin_result = Ctrl_C;
6924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 if (VIsual_active)
6926 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006927 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 redraw_curbuf_later(INVERTED);
6929 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006930 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 if (cap->nchar == Ctrl_G && p_im)
6932 restart_edit = 'a';
6933 }
6934 else
6935 clearopbeep(cap->oap);
6936}
6937
6938/*
6939 * ESC in Normal mode: beep, but don't flush buffers.
6940 * Don't even beep if we are canceling a command.
6941 */
6942 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006943nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 int no_reason;
6946
6947 no_reason = (cap->oap->op_type == OP_NOP
6948 && cap->opcount == 0
6949 && cap->count0 == 0
6950 && cap->oap->regname == 0
6951 && !p_im);
6952
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006953 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 {
6955 if (restart_edit == 0
6956#ifdef FEAT_CMDWIN
6957 && cmdwin_type == 0
6958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01006961 {
6962 if (anyBufIsChanged())
6963 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
6964 else
6965 msg(_("Type :qa and press <Enter> to exit Vim"));
6966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006968 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
6969 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 if (!p_im)
6971 restart_edit = 0;
6972#ifdef FEAT_CMDWIN
6973 if (cmdwin_type != 0)
6974 {
6975 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006976 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 return;
6978 }
6979#endif
6980 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01006981#ifdef FEAT_CMDWIN
6982 else if (cmdwin_type != 0 && ex_normal_busy)
6983 {
6984 // When :normal runs out of characters while in the command line window
6985 // vgetorpeek() will return ESC. Exit the cmdline window to break the
6986 // loop.
6987 cmdwin_result = K_IGNORE;
6988 return;
6989 }
6990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (VIsual_active)
6993 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006994 end_visual_mode(); // stop Visual
6995 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 curwin->w_set_curswant = TRUE;
6997 redraw_curbuf_later(INVERTED);
6998 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006999 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007000 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 clearop(cap->oap);
7002
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007003 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7004 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007005 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 restart_edit = 'a';
7007}
7008
7009/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007010 * Move the cursor for the "A" command.
7011 */
7012 void
7013set_cursor_for_append_to_line(void)
7014{
7015 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007016 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007017 {
7018 int save_State = State;
7019
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007020 // Pretend Insert mode here to allow the cursor on the
7021 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007022 State = INSERT;
7023 coladvance((colnr_T)MAXCOL);
7024 State = save_State;
7025 }
7026 else
7027 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7028}
7029
7030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007032 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 */
7034 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007035nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007037 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7039 cap->cmdchar = 'i';
7040
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007041 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007043 {
7044#ifdef FEAT_TERMINAL
7045 if (term_in_normal_mode())
7046 {
7047 end_visual_mode();
7048 clearop(cap->oap);
7049 term_enter_job_mode();
7050 return;
7051 }
7052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007056 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007057 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7058 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
7060#ifdef FEAT_TEXTOBJ
7061 nv_object(cap);
7062#else
7063 clearopbeep(cap->oap);
7064#endif
7065 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007066#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007067 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007068 {
7069 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007070 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007071 return;
7072 }
7073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 else if (!curbuf->b_p_ma && !p_im)
7075 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007076 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007077 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007079 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007080 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007081 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007083 else if (cap->cmdchar == K_PS && VIsual_active)
7084 {
7085 pos_T old_pos = curwin->w_cursor;
7086 pos_T old_visual = VIsual;
7087
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007088 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007089 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7090 {
7091 shift_delete_registers();
7092 cap->oap->regname = '1';
7093 }
7094 else
7095 cap->oap->regname = '-';
7096 cap->cmdchar = 'd';
7097 cap->nchar = NUL;
7098 nv_operator(cap);
7099 do_pending_operator(cap, 0, FALSE);
7100 cap->cmdchar = K_PS;
7101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007102 // When the last char in the line was deleted then append. Detect this
7103 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007104 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7105 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007106 inc_cursor();
7107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007108 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007109 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 else if (!checkclearopq(cap->oap))
7112 {
7113 switch (cap->cmdchar)
7114 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007115 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007116 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 break;
7118
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007119 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007120 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7121 beginline(BL_WHITE);
7122 else
7123 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 break;
7125
Bram Moolenaara1891842017-02-04 21:34:31 +01007126 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007127 // Bracketed paste works like "a"ppend, unless the cursor is in
7128 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007129 if (curwin->w_cursor.col == 0)
7130 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007131 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007132
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007133 case 'a': // "a"ppend is like "i"nsert on the next character.
7134 // increment coladd when in virtual space, increment the
7135 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (virtual_active()
7137 && (curwin->w_cursor.coladd > 0
7138 || *ml_get_cursor() == NUL
7139 || *ml_get_cursor() == TAB))
7140 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007141 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 inc_cursor();
7143 break;
7144 }
7145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7147 {
7148 int save_State = State;
7149
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007150 // Pretend Insert mode here to allow the cursor on the
7151 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 State = INSERT;
7153 coladvance(getviscol());
7154 State = save_State;
7155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7158 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007159 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007160 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007161 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162}
7163
7164/*
7165 * Invoke edit() and take care of "restart_edit" and the return value.
7166 */
7167 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007168invoke_edit(
7169 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007170 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007171 int cmd,
7172 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
7174 int restart_edit_save = 0;
7175
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7177 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7178 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 if (repl || !stuff_empty())
7180 restart_edit_save = restart_edit;
7181 else
7182 restart_edit_save = 0;
7183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007184 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 restart_edit = 0;
7186
7187 if (edit(cmd, startln, cap->count1))
7188 cap->retval |= CA_COMMAND_BUSY;
7189
7190 if (restart_edit == 0)
7191 restart_edit = restart_edit_save;
7192}
7193
7194#ifdef FEAT_TEXTOBJ
7195/*
7196 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7197 */
7198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007199nv_object(
7200 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201{
7202 int flag;
7203 int include;
7204 char_u *mps_save;
7205
7206 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007207 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007209 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007211 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 mps_save = curbuf->b_p_mps;
7213 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7214
7215 switch (cap->nchar)
7216 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007217 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 flag = current_word(cap->oap, cap->count1, include, FALSE);
7219 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007220 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 flag = current_word(cap->oap, cap->count1, include, TRUE);
7222 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007223 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 case '(':
7225 case ')':
7226 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7227 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007228 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 case '{':
7230 case '}':
7231 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7232 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007233 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 case ']':
7235 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7236 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007237 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 case '>':
7239 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7240 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007241 case 't': // "at" = a tag block (xml and html)
7242 // Do not adjust oap->end in do_pending_operator()
7243 // otherwise there are different results for 'dit'
7244 // (note leading whitespace in last line):
7245 // 1) <b> 2) <b>
7246 // foobar foobar
7247 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007248 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007249 flag = current_tagblock(cap->oap, cap->count1, include);
7250 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007251 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 flag = current_par(cap->oap, cap->count1, include, 'p');
7253 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007254 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 flag = current_sent(cap->oap, cap->count1, include);
7256 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007257 case '"': // "a"" = a double quoted string
7258 case '\'': // "a'" = a single quoted string
7259 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007260 flag = current_quote(cap->oap, cap->count1, include,
7261 cap->nchar);
7262 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007263#if 0 // TODO
7264 case 'S': // "aS" = a section
7265 case 'f': // "af" = a filename
7266 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267#endif
7268 default:
7269 flag = FAIL;
7270 break;
7271 }
7272
7273 curbuf->b_p_mps = mps_save;
7274 if (flag == FAIL)
7275 clearopbeep(cap->oap);
7276 adjust_cursor_col();
7277 curwin->w_set_curswant = TRUE;
7278}
7279#endif
7280
7281/*
7282 * "q" command: Start/stop recording.
7283 * "q:", "q/", "q?": edit command-line in command-line window.
7284 */
7285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007286nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287{
7288 if (cap->oap->op_type == OP_FORMAT)
7289 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007290 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 cap->cmdchar = 'g';
7292 cap->nchar = 'q';
7293 nv_operator(cap);
7294 }
7295 else if (!checkclearop(cap->oap))
7296 {
7297#ifdef FEAT_CMDWIN
7298 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7299 {
7300 stuffcharReadbuff(cap->nchar);
7301 stuffcharReadbuff(K_CMDWIN);
7302 }
7303 else
7304#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007305 // (stop) recording into a named register, unless executing a
7306 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007307 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 clearopbeep(cap->oap);
7309 }
7310}
7311
7312/*
7313 * Handle the "@r" command.
7314 */
7315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007316nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317{
7318 if (checkclearop(cap->oap))
7319 return;
7320#ifdef FEAT_EVAL
7321 if (cap->nchar == '=')
7322 {
7323 if (get_expr_register() == NUL)
7324 return;
7325 }
7326#endif
7327 while (cap->count1-- && !got_int)
7328 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007329 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 {
7331 clearopbeep(cap->oap);
7332 break;
7333 }
7334 line_breakcheck();
7335 }
7336}
7337
7338/*
7339 * Handle the CTRL-U and CTRL-D commands.
7340 */
7341 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007342nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7345 || (cap->cmdchar == Ctrl_D
7346 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7347 clearopbeep(cap->oap);
7348 else if (!checkclearop(cap->oap))
7349 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7350}
7351
7352/*
7353 * Handle "J" or "gJ" command.
7354 */
7355 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007356nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007358 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007360 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
7362 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007363 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7365 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007367 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007368 if (cap->count0 <= 2)
7369 {
7370 clearopbeep(cap->oap);
7371 return;
7372 }
7373 cap->count0 = curbuf->b_ml.ml_line_count
7374 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007376
7377 prep_redo(cap->oap->regname, cap->count0,
7378 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7379 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381}
7382
7383/*
7384 * "P", "gP", "p" and "gp" commands.
7385 */
7386 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007387nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007389 nv_put_opt(cap, FALSE);
7390}
7391
7392/*
7393 * "P", "gP", "p" and "gp" commands.
7394 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7395 */
7396 static void
7397nv_put_opt(cmdarg_T *cap, int fix_indent)
7398{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 int regname = 0;
7400 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007401 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007402 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 int dir;
7404 int flags = 0;
7405
7406 if (cap->oap->op_type != OP_NOP)
7407 {
7408#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007409 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7411 {
7412 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007413 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 }
7415 else
7416#endif
7417 clearopbeep(cap->oap);
7418 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007419#ifdef FEAT_JOB_CHANNEL
7420 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7421 {
7422 clearopbeep(cap->oap);
7423 }
7424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 else
7426 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007427 if (fix_indent)
7428 {
7429 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7430 ? FORWARD : BACKWARD;
7431 flags |= PUT_FIXINDENT;
7432 }
7433 else
7434 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007435 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7436 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 prep_redo_cmd(cap);
7438 if (cap->cmdchar == 'g')
7439 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007440 else if (cap->cmdchar == 'z')
7441 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (VIsual_active)
7444 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007445 // Putting in Visual mode: The put text replaces the selected
7446 // text. First delete the selected text, then put the new text.
7447 // Need to save and restore the registers that the delete
7448 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007449 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007451#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007453#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007454 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007455 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007456#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
7460 )
7461 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007462 // The delete is going to overwrite the register we want to
7463 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 reg1 = get_register(regname, TRUE);
7465 }
7466
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007467 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 cap->cmdchar = 'd';
7469 cap->nchar = NUL;
7470 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007471 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 nv_operator(cap);
7473 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007474 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007475 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007477 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 cap->oap->regname = regname;
7479
7480 if (reg1 != NULL)
7481 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007482 // Delete probably changed the register we want to put, save
7483 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 reg2 = get_register(regname, FALSE);
7485 put_register(regname, reg1);
7486 }
7487
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007488 // When deleted a linewise Visual area, put the register as
7489 // lines to avoid it joined with the next line. When deletion was
7490 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if (VIsual_mode == 'V')
7492 flags |= PUT_LINE;
7493 else if (VIsual_mode == 'v')
7494 flags |= PUT_LINE_SPLIT;
7495 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7496 flags |= PUT_LINE_FORWARD;
7497 dir = BACKWARD;
7498 if ((VIsual_mode != 'V'
7499 && curwin->w_cursor.col < curbuf->b_op_start.col)
7500 || (VIsual_mode == 'V'
7501 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007502 // cursor is at the end of the line or end of file, put
7503 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007505 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007506 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007508 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007510 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 if (reg2 != NULL)
7512 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007513
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007514 // What to reselect with "gv"? Selecting the just put text seems to
7515 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007516 if (was_visual)
7517 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007518 curbuf->b_visual.vi_start = curbuf->b_op_start;
7519 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007520 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007521 if (*p_sel == 'e')
7522 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007523 }
7524
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007525 // When all lines were selected and deleted do_put() leaves an empty
7526 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007527 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007528 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007529 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007530 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007532 // If the cursor was in that line, move it to the end of the last
7533 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007534 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7535 {
7536 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7537 coladvance((colnr_T)MAXCOL);
7538 }
7539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 auto_format(FALSE, TRUE);
7541 }
7542}
7543
7544/*
7545 * "o" and "O" commands.
7546 */
7547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007548nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549{
7550#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007551 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7553 {
7554 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007555 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 }
7557 else
7558#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007559 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007561#ifdef FEAT_JOB_CHANNEL
7562 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007563 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 n_opencmd(cap);
7567}
7568
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569#ifdef FEAT_NETBEANS_INTG
7570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007571nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572{
7573 netbeans_keycommand(cap->nchar);
7574}
7575#endif
7576
7577#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007579nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007581 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582}
7583#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007584
Bram Moolenaar3918c952005-03-15 22:34:55 +00007585/*
7586 * Trigger CursorHold event.
7587 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7588 * input buffer. "did_cursorhold" is set to avoid retriggering.
7589 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007591nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007592{
7593 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7594 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007595 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007596}