blob: efc7cfd8a09b7f99923cc172916d1c0aa8d77b18 [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
131/*
132 * Function to be called for a Normal or Visual mode command.
133 * The argument is a cmdarg_T.
134 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100135typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100137// Values for cmd_flags.
138#define NV_NCH 0x01 // may need to get a second char
139#define NV_NCH_NOP (0x02|NV_NCH) // get second char when no operator pending
140#define NV_NCH_ALW (0x04|NV_NCH) // always get a second char
141#define NV_LANG 0x08 // second char needs language adjustment
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100143#define NV_SS 0x10 // may start selection
144#define NV_SSS 0x20 // may start selection with shift modifier
145#define NV_STS 0x40 // may stop selection without shift modif.
146#define NV_RL 0x80 // 'rightleft' modifies command
147#define NV_KEEPREG 0x100 // don't clear regname
148#define NV_NCW 0x200 // not allowed in command-line window
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150/*
151 * Generally speaking, every Normal mode command should either clear any
152 * pending operator (with *clearop*()), or set the motion type variable
153 * oap->motion_type.
154 *
155 * When a cursor motion command is made, it is marked as being a character or
156 * line oriented motion. Then, if an operator is in effect, the operation
157 * becomes character or line oriented accordingly.
158 */
159
160/*
161 * This table contains one entry for every Normal or Visual mode command.
162 * The order doesn't matter, init_normal_cmds() will create a sorted index.
163 * It is faster when all keys from zero to '~' are present.
164 */
165static const struct nv_cmd
166{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100167 int cmd_char; // (first) command character
168 nv_func_T cmd_func; // function for this command
169 short_u cmd_flags; // NV_ flags
170 short cmd_arg; // value for ca.arg
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171} nv_cmds[] =
172{
173 {NUL, nv_error, 0, 0},
174 {Ctrl_A, nv_addsub, 0, 0},
175 {Ctrl_B, nv_page, NV_STS, BACKWARD},
176 {Ctrl_C, nv_esc, 0, TRUE},
177 {Ctrl_D, nv_halfpage, 0, 0},
178 {Ctrl_E, nv_scroll_line, 0, TRUE},
179 {Ctrl_F, nv_page, NV_STS, FORWARD},
180 {Ctrl_G, nv_ctrlg, 0, 0},
181 {Ctrl_H, nv_ctrlh, 0, 0},
182 {Ctrl_I, nv_pcmark, 0, 0},
183 {NL, nv_down, 0, FALSE},
184 {Ctrl_K, nv_error, 0, 0},
185 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100186 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {Ctrl_N, nv_down, NV_STS, FALSE},
188 {Ctrl_O, nv_ctrlo, 0, 0},
189 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000190 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 {Ctrl_R, nv_redo, 0, 0},
192 {Ctrl_S, nv_ignore, 0, 0},
193 {Ctrl_T, nv_tagpop, NV_NCW, 0},
194 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 {Ctrl_V, nv_visual, 0, FALSE},
196 {'V', nv_visual, 0, FALSE},
197 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 {Ctrl_W, nv_window, 0, 0},
199 {Ctrl_X, nv_addsub, 0, 0},
200 {Ctrl_Y, nv_scroll_line, 0, FALSE},
201 {Ctrl_Z, nv_suspend, 0, 0},
202 {ESC, nv_esc, 0, FALSE},
203 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
204 {Ctrl_RSB, nv_ident, NV_NCW, 0},
205 {Ctrl_HAT, nv_hat, NV_NCW, 0},
206 {Ctrl__, nv_error, 0, 0},
207 {' ', nv_right, 0, 0},
208 {'!', nv_operator, 0, 0},
209 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
210 {'#', nv_ident, 0, 0},
211 {'$', nv_dollar, 0, 0},
212 {'%', nv_percent, 0, 0},
213 {'&', nv_optrans, 0, 0},
214 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
215 {'(', nv_brace, 0, BACKWARD},
216 {')', nv_brace, 0, FORWARD},
217 {'*', nv_ident, 0, 0},
218 {'+', nv_down, 0, TRUE},
219 {',', nv_csearch, 0, TRUE},
220 {'-', nv_up, 0, TRUE},
221 {'.', nv_dot, NV_KEEPREG, 0},
222 {'/', nv_search, 0, FALSE},
223 {'0', nv_beginline, 0, 0},
224 {'1', nv_ignore, 0, 0},
225 {'2', nv_ignore, 0, 0},
226 {'3', nv_ignore, 0, 0},
227 {'4', nv_ignore, 0, 0},
228 {'5', nv_ignore, 0, 0},
229 {'6', nv_ignore, 0, 0},
230 {'7', nv_ignore, 0, 0},
231 {'8', nv_ignore, 0, 0},
232 {'9', nv_ignore, 0, 0},
233 {':', nv_colon, 0, 0},
234 {';', nv_csearch, 0, FALSE},
235 {'<', nv_operator, NV_RL, 0},
236 {'=', nv_operator, 0, 0},
237 {'>', nv_operator, NV_RL, 0},
238 {'?', nv_search, 0, FALSE},
239 {'@', nv_at, NV_NCH_NOP, FALSE},
240 {'A', nv_edit, 0, 0},
241 {'B', nv_bck_word, 0, 1},
242 {'C', nv_abbrev, NV_KEEPREG, 0},
243 {'D', nv_abbrev, NV_KEEPREG, 0},
244 {'E', nv_wordcmd, 0, TRUE},
245 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
246 {'G', nv_goto, 0, TRUE},
247 {'H', nv_scroll, 0, 0},
248 {'I', nv_edit, 0, 0},
249 {'J', nv_join, 0, 0},
250 {'K', nv_ident, 0, 0},
251 {'L', nv_scroll, 0, 0},
252 {'M', nv_scroll, 0, 0},
253 {'N', nv_next, 0, SEARCH_REV},
254 {'O', nv_open, 0, 0},
255 {'P', nv_put, 0, 0},
256 {'Q', nv_exmode, NV_NCW, 0},
257 {'R', nv_Replace, 0, FALSE},
258 {'S', nv_subst, NV_KEEPREG, 0},
259 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
260 {'U', nv_Undo, 0, 0},
261 {'W', nv_wordcmd, 0, TRUE},
262 {'X', nv_abbrev, NV_KEEPREG, 0},
263 {'Y', nv_abbrev, NV_KEEPREG, 0},
264 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
265 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
266 {'\\', nv_error, 0, 0},
267 {']', nv_brackets, NV_NCH_ALW, FORWARD},
268 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
269 {'_', nv_lineop, 0, 0},
270 {'`', nv_gomark, NV_NCH_ALW, FALSE},
271 {'a', nv_edit, NV_NCH, 0},
272 {'b', nv_bck_word, 0, 0},
273 {'c', nv_operator, 0, 0},
274 {'d', nv_operator, 0, 0},
275 {'e', nv_wordcmd, 0, FALSE},
276 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
277 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
278 {'h', nv_left, NV_RL, 0},
279 {'i', nv_edit, NV_NCH, 0},
280 {'j', nv_down, 0, FALSE},
281 {'k', nv_up, 0, FALSE},
282 {'l', nv_right, NV_RL, 0},
283 {'m', nv_mark, NV_NCH_NOP, 0},
284 {'n', nv_next, 0, 0},
285 {'o', nv_open, 0, 0},
286 {'p', nv_put, 0, 0},
287 {'q', nv_record, NV_NCH, 0},
288 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
289 {'s', nv_subst, NV_KEEPREG, 0},
290 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
291 {'u', nv_undo, 0, 0},
292 {'w', nv_wordcmd, 0, FALSE},
293 {'x', nv_abbrev, NV_KEEPREG, 0},
294 {'y', nv_operator, 0, 0},
295 {'z', nv_zet, NV_NCH_ALW, 0},
296 {'{', nv_findpar, 0, BACKWARD},
297 {'|', nv_pipe, 0, 0},
298 {'}', nv_findpar, 0, FORWARD},
299 {'~', nv_tilde, 0, 0},
300
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100301 // pound sign
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 {POUND, nv_ident, 0, 0},
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200303 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
304 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
305 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
306 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {K_LEFTMOUSE, nv_mouse, 0, 0},
308 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
309 {K_LEFTDRAG, nv_mouse, 0, 0},
310 {K_LEFTRELEASE, nv_mouse, 0, 0},
311 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100312 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
314 {K_MIDDLEDRAG, nv_mouse, 0, 0},
315 {K_MIDDLERELEASE, nv_mouse, 0, 0},
316 {K_RIGHTMOUSE, nv_mouse, 0, 0},
317 {K_RIGHTDRAG, nv_mouse, 0, 0},
318 {K_RIGHTRELEASE, nv_mouse, 0, 0},
319 {K_X1MOUSE, nv_mouse, 0, 0},
320 {K_X1DRAG, nv_mouse, 0, 0},
321 {K_X1RELEASE, nv_mouse, 0, 0},
322 {K_X2MOUSE, nv_mouse, 0, 0},
323 {K_X2DRAG, nv_mouse, 0, 0},
324 {K_X2RELEASE, nv_mouse, 0, 0},
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000325 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000326 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {K_INS, nv_edit, 0, 0},
328 {K_KINS, nv_edit, 0, 0},
329 {K_BS, nv_ctrlh, 0, 0},
330 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
331 {K_S_UP, nv_page, NV_SS, BACKWARD},
332 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
333 {K_S_DOWN, nv_page, NV_SS, FORWARD},
334 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
335 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
336 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
337 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
338 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
339 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
340 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
341 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
342 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
343 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
344 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
345 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {K_S_END, nv_end, NV_SS, FALSE},
347 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
348 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
349 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 {K_S_HOME, nv_home, NV_SS, 0},
351 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
352 {K_DEL, nv_abbrev, 0, 0},
353 {K_KDEL, nv_abbrev, 0, 0},
354 {K_UNDO, nv_kundo, 0, 0},
355 {K_HELP, nv_help, NV_NCW, 0},
356 {K_F1, nv_help, NV_NCW, 0},
357 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef FEAT_GUI
360 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
361 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
362#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000363#ifdef FEAT_GUI_TABLINE
364 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000365 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#ifdef FEAT_NETBEANS_INTG
368 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
369#endif
370#ifdef FEAT_DND
371 {K_DROP, nv_drop, NV_STS, 0},
372#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000373 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100374 {K_PS, nv_edit, 0, 0},
Bram Moolenaar957cf672020-11-12 14:21:06 +0100375 {K_COMMAND, nv_colon, 0, 0},
Bram Moolenaare32c3c42022-01-15 18:26:04 +0000376 {K_SCRIPT_COMMAND, nv_colon, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377};
378
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100379// Number of commands in nv_cmds[].
K.Takataeeec2542021-06-02 13:28:16 +0200380#define NV_CMDS_SIZE ARRAY_LENGTH(nv_cmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000382#ifndef PROTO // cproto doesn't like this
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100383// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static short nv_cmd_idx[NV_CMDS_SIZE];
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100387// The highest index for which
388// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static int nv_max_linear;
390
391/*
392 * Compare functions for qsort() below, that checks the command character
393 * through the index in nv_cmd_idx[].
394 */
395 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100396nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397{
398 int c1, c2;
399
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100400 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 c1 = nv_cmds[*(const short *)s1].cmd_char;
402 c2 = nv_cmds[*(const short *)s2].cmd_char;
403 if (c1 < 0)
404 c1 = -c1;
405 if (c2 < 0)
406 c2 = -c2;
407 return c1 - c2;
408}
409
410/*
411 * Initialize the nv_cmd_idx[] table.
412 */
413 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100414init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
416 int i;
417
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100418 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000419 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 nv_cmd_idx[i] = i;
421
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100422 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
424
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100425 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000426 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
428 break;
429 nv_max_linear = i - 1;
430}
431
432/*
433 * Search for a command in the commands table.
434 * Returns -1 for invalid command.
435 */
436 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100437find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
439 int i;
440 int idx;
441 int top, bot;
442 int c;
443
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100444 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 if (cmdchar >= 0x100)
446 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100448 // We use the absolute value of the character. Special keys have a
449 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 if (cmdchar < 0)
451 cmdchar = -cmdchar;
452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100453 // If the character is in the first part: The character is the index into
454 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 if (cmdchar <= nv_max_linear)
456 return nv_cmd_idx[cmdchar];
457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100458 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 bot = nv_max_linear + 1;
460 top = NV_CMDS_SIZE - 1;
461 idx = -1;
462 while (bot <= top)
463 {
464 i = (top + bot) / 2;
465 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
466 if (c < 0)
467 c = -c;
468 if (cmdchar == c)
469 {
470 idx = nv_cmd_idx[i];
471 break;
472 }
473 if (cmdchar > c)
474 bot = i + 1;
475 else
476 top = i - 1;
477 }
478 return idx;
479}
480
481/*
482 * Execute a command in Normal mode.
483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100485normal_cmd(
486 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100487 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100489 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100491 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 int old_col = curwin->w_curswant;
493#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100494 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100496 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 static int old_mapped_len = 0;
499 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000500#ifdef FEAT_EVAL
501 int set_prevcount = FALSE;
502#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200503 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504
Bram Moolenaara80faa82020-04-12 19:37:17 +0200505 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000507
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100508 // Use a count remembered from before entering an operator. After typing
509 // "3d" we return from normal_cmd() and come back here, the "3" is
510 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 ca.opcount = opcount;
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 /*
514 * If there is an operator pending, then the command we take this time
515 * will terminate it. Finish_op tells us to finish the operation before
516 * returning this time (unless the operation was cancelled).
517 */
518#ifdef CURSOR_SHAPE
519 c = finish_op;
520#endif
521 finish_op = (oap->op_type != OP_NOP);
522#ifdef CURSOR_SHAPE
523 if (finish_op != c)
524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100525 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526# ifdef FEAT_MOUSESHAPE
527 update_mouseshape(-1);
528# endif
529 }
530#endif
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100531 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100533 // When not finishing an operator and no register name typed, reset the
534 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000538#ifdef FEAT_EVAL
539 set_prevcount = TRUE;
540#endif
541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100543 // Restore counts from before receiving K_CURSORHOLD. This means after
544 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
545 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000546 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
547 {
548 ca.opcount = oap->prev_opcount;
549 ca.count0 = oap->prev_count0;
550 oap->prev_opcount = 0;
551 oap->prev_count0 = 0;
552 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000553
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
556 State = NORMAL_BUSY;
557#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100558 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#endif
560
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100561#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100562 // Set v:count here, when called from main() and not a stuffed
563 // command, so that v:count can be used in an expression mapping
564 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100565 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100566 set_vcount_ca(&ca, &set_prevcount);
567#endif
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 /*
570 * Get the command character from the user.
571 */
572 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100573 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
575 /*
576 * If a mapping was started in Visual or Select mode, remember the length
577 * of the mapping. This is used below to not return to Insert mode for as
578 * long as the mapping is being executed.
579 */
580 if (restart_edit == 0)
581 old_mapped_len = 0;
582 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000583 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 old_mapped_len = typebuf_maplen();
585
586 if (c == NUL)
587 c = K_ZERO;
588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 /*
590 * In Select mode, typed text replaces the selection.
591 */
592 if (VIsual_active
593 && VIsual_select
594 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
595 {
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000596 int len;
597
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100598 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
599 // 'insertmode' is set) fake a "d"elete command, Insert mode will
600 // restart automatically.
601 // Insert the typed character in the typeahead buffer, so that it can
602 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarc88e9772022-01-03 13:47:50 +0000603 len = ins_char_typebuf(vgetc_char, vgetc_mod_mask);
604
605 // When recording the character will be recorded again, remove the
606 // previously recording.
607 ungetchars(len);
608
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000609 if (restart_edit != 0)
610 c = 'd';
611 else
612 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100613 msg_nowait = TRUE; // don't delay going to insert mode
614 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000617 // If the window was made so small that nothing shows, make it at least one
618 // line and one column when typing a command.
619 if (KeyTyped && !KeyStuffed)
620 win_ensure_size();
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#ifdef FEAT_CMDL_INFO
623 need_flushbuf = add_to_showcmd(c);
624#endif
625
626getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
629 /*
630 * Handle a count before a command and compute ca.count0.
631 * Note that '0' is a command and not the start of a count, but it's
632 * part of a count after other digits.
633 */
634 while ( (c >= '1' && c <= '9')
635 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
636 {
637 if (c == K_DEL || c == K_KDEL)
638 {
639 ca.count0 /= 10;
640#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100641 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#endif
643 }
Bram Moolenaar9b0e82f2021-11-24 13:40:29 +0000644 else if (ca.count0 > 99999999L)
Bram Moolenaar03725c52021-11-24 12:17:53 +0000645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 ca.count0 = 999999999L;
Bram Moolenaar03725c52021-11-24 12:17:53 +0000647 }
648 else
649 {
650 ca.count0 = ca.count0 * 10 + (c - '0');
651 }
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000652#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100653 // Set v:count here, when called from main() and not a stuffed
654 // command, so that v:count can be used in an expression mapping
655 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100656 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100657 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (ctrl_w)
660 {
661 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100662 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100664 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000665 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 --no_zero_mapping;
668 if (ctrl_w)
669 {
670 --no_mapping;
671 --allow_keys;
672 }
673#ifdef FEAT_CMDL_INFO
674 need_flushbuf |= add_to_showcmd(c);
675#endif
676 }
677
678 /*
679 * If we got CTRL-W there may be a/another count
680 */
681 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
682 {
683 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100684 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 ca.count0 = 0;
686 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100687 ++allow_keys; // no mapping for nchar, but keys
688 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 --no_mapping;
691 --allow_keys;
692#ifdef FEAT_CMDL_INFO
693 need_flushbuf |= add_to_showcmd(c);
694#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100695 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 }
697 }
698
Bram Moolenaara983fe92008-07-31 20:04:27 +0000699 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100701 // Save the count values so that ca.opcount and ca.count0 are exactly
702 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000703 oap->prev_opcount = ca.opcount;
704 oap->prev_count0 = ca.count0;
705 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100706 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000707 {
708 /*
709 * If we're in the middle of an operator (including after entering a
710 * yank buffer with '"') AND we had a count before the operator, then
711 * that count overrides the current value of ca.count0.
712 * What this means effectively, is that commands like "3dw" get turned
713 * into "d3w" which makes things fall into place pretty neatly.
714 * If you give a count before AND after the operator, they are
715 * multiplied.
716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (ca.count0)
Bram Moolenaar03725c52021-11-24 12:17:53 +0000718 {
719 if (ca.opcount >= 999999999L / ca.count0)
720 ca.count0 = 999999999L;
721 else
722 ca.count0 *= ca.opcount;
723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 else
725 ca.count0 = ca.opcount;
726 }
727
728 /*
729 * Always remember the count. It will be set to zero (on the next call,
730 * above) when there is no pending operator.
731 * When called from main(), save the count for use by the "count" built-in
732 * variable.
733 */
734 ca.opcount = ca.count0;
735 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
736
737#ifdef FEAT_EVAL
738 /*
739 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100740 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100742 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000743 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#endif
745
746 /*
747 * Find the command character in the table of commands.
748 * For CTRL-W we already got nchar when looking for a count.
749 */
750 if (ctrl_w)
751 {
752 ca.nchar = c;
753 ca.cmdchar = Ctrl_W;
754 }
755 else
756 ca.cmdchar = c;
757 idx = find_command(ca.cmdchar);
758 if (idx < 0)
759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100760 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 clearopbeep(oap);
762 goto normal_end;
763 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000764
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000765 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100767 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000769 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 goto normal_end;
771 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000772 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
773 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 /*
776 * In Visual/Select mode, a few keys are handled in a special way.
777 */
778 if (VIsual_active)
779 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100780 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (km_stopsel
782 && (nv_cmds[idx].cmd_flags & NV_STS)
783 && !(mod_mask & MOD_MASK_SHIFT))
784 {
785 end_visual_mode();
786 redraw_curbuf_later(INVERTED);
787 }
788
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100789 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 if (km_startsel)
791 {
792 if (nv_cmds[idx].cmd_flags & NV_SS)
793 {
794 unshift_special(&ca);
795 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000796 if (idx < 0)
797 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100798 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000799 clearopbeep(oap);
800 goto normal_end;
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
803 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
804 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809#ifdef FEAT_RIGHTLEFT
810 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
811 && (nv_cmds[idx].cmd_flags & NV_RL))
812 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100813 // Invert horizontal movements and operations. Only when typed by the
814 // user directly, not when the result of a mapping or "x" translated
815 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 switch (ca.cmdchar)
817 {
818 case 'l': ca.cmdchar = 'h'; break;
819 case K_RIGHT: ca.cmdchar = K_LEFT; break;
820 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
821 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
822 case 'h': ca.cmdchar = 'l'; break;
823 case K_LEFT: ca.cmdchar = K_RIGHT; break;
824 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
825 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
826 case '>': ca.cmdchar = '<'; break;
827 case '<': ca.cmdchar = '>'; break;
828 }
829 idx = find_command(ca.cmdchar);
830 }
831#endif
832
833 /*
834 * Get an additional character if we need one.
835 */
836 if ((nv_cmds[idx].cmd_flags & NV_NCH)
837 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
838 && oap->op_type == OP_NOP)
839 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
840 || (ca.cmdchar == 'q'
841 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200842 && reg_recording == 0
843 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100845 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
847 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100848 int repl = FALSE; // get character for replace mode
849 int lit = FALSE; // get extra character literally
850 int langmap_active = FALSE; // using :lmap mappings
851 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100852#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100853 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855
856 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100857 ++allow_keys; // no mapping for nchar, but allow key codes
858 // Don't generate a CursorHold event here, most commands can't handle
859 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000860 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (ca.cmdchar == 'g')
862 {
863 /*
864 * For 'g' get the next character now, so that we can check for
865 * "gr", "g'" and "g`".
866 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000867 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#ifdef FEAT_CMDL_INFO
870 need_flushbuf |= add_to_showcmd(ca.nchar);
871#endif
872 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100873 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100875 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100877 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100879 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100882 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884 else
885 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100886 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 repl = TRUE;
888 cp = &ca.nchar;
889 }
890 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
891
892 /*
893 * Get a second or third character.
894 */
895 if (cp != NULL)
896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (repl)
898 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100899 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100900#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100901 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
905 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100906 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 --no_mapping;
908 --allow_keys;
909 if (repl)
910 State = LREPLACE;
911 else
912 State = LANGMAP;
913 langmap_active = TRUE;
914 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100915#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100917 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
919 im_set_active(TRUE);
920#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200921 if ((State & INSERT) && !p_ek)
922 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200923#ifdef FEAT_JOB_CHANNEL
924 ch_log_output = TRUE;
925#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200926 // Disable bracketed paste and modifyOtherKeys here, we won't
927 // recognize the escape sequences with 'esckeys' off.
928 out_str(T_BD);
929 out_str(T_CTE);
930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000932 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
Bram Moolenaarca774f62020-08-30 20:46:38 +0200934 if ((State & INSERT) && !p_ek)
935 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200936#ifdef FEAT_JOB_CHANNEL
937 ch_log_output = TRUE;
938#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200939 // Re-enable bracketed paste mode and modifyOtherKeys
940 out_str(T_BE);
941 out_str(T_CTI);
942 }
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (langmap_active)
945 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100946 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 ++no_mapping;
948 ++allow_keys;
949 State = NORMAL_BUSY;
950 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100951#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 if (lang)
953 {
954 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
955 im_save_status(&curbuf->b_p_iminsert);
956 im_set_active(FALSE);
957 }
958 p_smd = save_smd;
959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#ifdef FEAT_CMDL_INFO
962 need_flushbuf |= add_to_showcmd(*cp);
963#endif
964
965 if (!lit)
966 {
967#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100968 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (*cp == Ctrl_K
970 && ((nv_cmds[idx].cmd_flags & NV_LANG)
971 || cp == &ca.extra_char)
972 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
973 {
974 c = get_digraph(FALSE);
975 if (c > 0)
976 {
977 *cp = c;
978# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100979 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 del_from_showcmd(3);
981 need_flushbuf |= add_to_showcmd(*cp);
982# endif
983 }
984 }
985#endif
986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100987 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100990 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (p_hkmap && lang && KeyTyped)
992 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994 }
995
996 /*
997 * When the next character is CTRL-\ a following CTRL-N means the
998 * command is aborted and we go to Normal mode.
999 */
1000 if (cp == &ca.extra_char
1001 && ca.nchar == Ctrl_BSL
1002 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1003 {
1004 ca.cmdchar = Ctrl_BSL;
1005 ca.nchar = ca.extra_char;
1006 idx = find_command(ca.cmdchar);
1007 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001008 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001009 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 else if (*cp == Ctrl_BSL)
1011 {
1012 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001014 // There is a busy wait here when typing "f<C-\>" and then
1015 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 while ((c = vpeekc()) <= 0 && towait > 0L)
1017 {
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01001018 do_sleep(towait > 50L ? 50L : towait, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 towait -= 50L;
1020 }
1021 if (c > 0)
1022 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001023 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (c != Ctrl_N && c != Ctrl_G)
1025 vungetc(c);
1026 else
1027 {
1028 ca.cmdchar = Ctrl_BSL;
1029 ca.nchar = c;
1030 idx = find_command(ca.cmdchar);
1031 }
1032 }
1033 }
1034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001035 // When getting a text character and the next character is a
1036 // multi-byte character, it could be a composing character.
1037 // However, don't wait for it to arrive. Also, do enable mapping,
1038 // because if it's put back with vungetc() it's too late to apply
1039 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001040 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 while (enc_utf8 && lang && (c = vpeekc()) > 0
1042 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1043 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001044 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (!utf_iscomposing(c))
1046 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001047 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 break;
1049 }
1050 else if (ca.ncharC1 == 0)
1051 ca.ncharC1 = c;
1052 else
1053 ca.ncharC2 = c;
1054 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001055 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057 --no_mapping;
1058 --allow_keys;
1059 }
1060
1061#ifdef FEAT_CMDL_INFO
1062 /*
1063 * Flush the showcmd characters onto the screen so we can see them while
1064 * the command is being executed. Only do this when the shown command was
1065 * actually displayed, otherwise this will slow down a lot when executing
1066 * mappings.
1067 */
1068 if (need_flushbuf)
1069 out_flush();
1070#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001071 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001072 {
1073 if (ex_normal_busy)
1074 did_cursorhold = save_did_cursorhold;
1075 else
1076 did_cursorhold = FALSE;
1077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
1079 State = NORMAL;
1080
1081 if (ca.nchar == ESC)
1082 {
1083 clearop(oap);
1084 if (restart_edit == 0 && goto_im())
1085 restart_edit = 'a';
1086 goto normal_end;
1087 }
1088
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001089 if (ca.cmdchar != K_IGNORE)
1090 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001091 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001092 msg_col = 0;
1093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001095 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001097 // When 'keymodel' contains "startsel" some keys start Select/Visual
1098 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (!VIsual_active && km_startsel)
1100 {
1101 if (nv_cmds[idx].cmd_flags & NV_SS)
1102 {
1103 start_selection();
1104 unshift_special(&ca);
1105 idx = find_command(ca.cmdchar);
1106 }
1107 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1108 && (mod_mask & MOD_MASK_SHIFT))
1109 {
1110 start_selection();
1111 mod_mask &= ~MOD_MASK_SHIFT;
1112 }
1113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
1115 /*
1116 * Execute the command!
1117 * Call the command function found in the commands table.
1118 */
1119 ca.arg = nv_cmds[idx].cmd_arg;
1120 (nv_cmds[idx].cmd_func)(&ca);
1121
1122 /*
1123 * If we didn't start or finish an operator, reset oap->regname, unless we
1124 * need it later.
1125 */
1126 if (!finish_op
1127 && !oap->op_type
1128 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1129 {
1130 clearop(oap);
1131#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001132 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133#endif
1134 }
1135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001136 // Get the length of mapped chars again after typing a count, second
1137 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001138 if (old_mapped_len > 0)
1139 old_mapped_len = typebuf_maplen();
1140
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 /*
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001142 * If an operation is pending, handle it. But not for K_IGNORE or
1143 * K_MOUSEMOVE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 */
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001145 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001146 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147
1148 /*
1149 * Wait for a moment when a message is displayed that will be overwritten
1150 * by the mode message.
1151 * In Visual mode and with "^O" in Insert mode, a short message will be
1152 * overwritten by the mode message. Wait a bit, until a key is hit.
1153 * In Visual mode, it's more important to keep the Visual area updated
1154 * than keeping a message (e.g. from a /pat search).
1155 * Only do this if the command was typed, not from a mapping.
1156 * Don't wait when emsg_silent is non-zero.
1157 * Also wait a bit after an error message, e.g. for "^O:".
1158 * Don't redraw the screen, it would remove the message.
1159 */
1160 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001161 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 || (VIsual_active
1164 && old_pos.lnum == curwin->w_cursor.lnum
1165 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 )
1167 && (clear_cmdline
1168 || redraw_cmdline)
1169 && (msg_didout || (msg_didany && msg_scroll))
1170 && !msg_nowait
1171 && KeyTyped)
1172 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && (msg_scroll
1175 || emsg_on_display)))
1176 && oap->regname == 0
1177 && !(ca.retval & CA_COMMAND_BUSY)
1178 && stuff_empty()
1179 && typebuf_typed()
1180 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001181 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 && !did_wait_return
1183 && oap->op_type == OP_NOP)
1184 {
1185 int save_State = State;
1186
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001187 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (restart_edit != 0)
1189 State = INSERT;
1190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001191 // If need to redraw, and there is a "keep_msg", redraw before the
1192 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1194 {
1195 char_u *kmsg;
1196
1197 kmsg = keep_msg;
1198 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001199 // Showmode() will clear keep_msg, but we want to use it anyway.
1200 // First update w_topline.
1201 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001203 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001205
1206 kmsg = vim_strsave(keep_msg);
1207 if (kmsg != NULL)
1208 {
1209 msg_attr((char *)kmsg, keep_msg_attr);
1210 vim_free(kmsg);
1211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001214#ifdef CURSOR_SHAPE
1215 ui_cursor_shape(); // may show different cursor shape
1216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 cursor_on();
1218 out_flush();
1219 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001220 ui_delay(1003L, TRUE); // wait at least one second
1221 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 State = save_State;
1223
1224 msg_scroll = FALSE;
1225 emsg_on_display = FALSE;
1226 }
1227
1228 /*
1229 * Finish up after executing a Normal mode command.
1230 */
1231normal_end:
1232
1233 msg_nowait = FALSE;
1234
Bram Moolenaarcc613032020-06-07 21:31:18 +02001235#ifdef FEAT_EVAL
1236 if (finish_op)
1237 reset_reg_var();
1238#endif
1239
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001240 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241#ifdef CURSOR_SHAPE
1242 c = finish_op;
1243#endif
1244 finish_op = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001245 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001247 // Redraw the cursor with another shape, if we were in Operator-pending
1248 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 if (c || ca.cmdchar == 'r')
1250 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001251 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252# ifdef FEAT_MOUSESHAPE
1253 update_mouseshape(-1);
1254# endif
1255 }
1256#endif
1257
1258#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001259 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001260 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 clear_showcmd();
1262#endif
1263
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001264 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 vim_free(ca.searchbuf);
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (has_mbyte)
1268 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (curwin->w_p_scb && toplevel)
1271 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001272 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 do_check_scrollbind(TRUE);
1274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
Bram Moolenaar860cae12010-06-05 23:22:07 +02001276 if (curwin->w_p_crb && toplevel)
1277 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001278 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001279 do_check_cursorbind();
1280 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001281
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001282#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001283 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001284 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001285 restart_edit = 0;
1286#endif
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 /*
1289 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1290 * if still inside a mapping that started in Visual mode).
1291 * May switch from Visual to Select mode after CTRL-O command.
1292 */
1293 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1295 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 && !(ca.retval & CA_COMMAND_BUSY)
1297 && stuff_empty()
1298 && oap->regname == 0)
1299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (restart_VIsual_select == 1)
1301 {
1302 VIsual_select = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001303 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 showmode();
1305 restart_VIsual_select = 0;
1306 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001307 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 (void)edit(restart_edit, FALSE, 1L);
1309 }
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (restart_VIsual_select == 2)
1312 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001314 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 opcount = ca.opcount;
1316}
1317
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001318#ifdef FEAT_EVAL
1319/*
1320 * Set v:count and v:count1 according to "cap".
1321 * Set v:prevcount only when "set_prevcount" is TRUE.
1322 */
1323 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001324set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001325{
1326 long count = cap->count0;
1327
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001328 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001329 if (cap->opcount != 0)
1330 count = cap->opcount * (count == 0 ? 1 : count);
1331 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001332 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001333}
1334#endif
1335
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001337 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 * if not.
1339 */
1340 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001341check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
1343 static int did_check = FALSE;
1344
1345 if (full_screen)
1346 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001347 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001348 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 did_check = TRUE;
1350 }
1351}
1352
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001353#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1354/*
1355 * Call yank_do_autocmd() for "regname".
1356 */
1357 static void
1358call_yank_do_autocmd(int regname)
1359{
1360 oparg_T oa;
1361 yankreg_T *reg;
1362
1363 clear_oparg(&oa);
1364 oa.regname = regname;
1365 oa.op_type = OP_YANK;
1366 oa.is_VIsual = TRUE;
1367 reg = get_register(regname, TRUE);
1368 yank_do_autocmd(&oa, reg);
1369 free_register(reg);
1370}
1371#endif
1372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001374 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001375 * This function or the next should ALWAYS be called to end Visual mode, except
1376 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 */
1378 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001379end_visual_mode()
1380{
1381 end_visual_mode_keep_button();
1382 reset_held_button();
1383}
1384
1385 void
1386end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388#ifdef FEAT_CLIPBOARD
1389 /*
1390 * If we are using the clipboard, then remember what was selected in case
1391 * we need to paste it somewhere while we still own the selection.
1392 * Only do this when the clipboard is already owned. Don't want to grab
1393 * the selection when hitting ESC.
1394 */
1395 if (clip_star.available && clip_star.owned)
1396 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001397
1398# if defined(FEAT_EVAL)
1399 // Emit a TextYankPost for the automatic copy of the selection into the
1400 // star and/or plus register.
1401 if (has_textyankpost())
1402 {
1403 if (clip_isautosel_star())
1404 call_yank_do_autocmd('*');
1405 if (clip_isautosel_plus())
1406 call_yank_do_autocmd('+');
1407 }
1408# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409#endif
1410
1411 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 setmouse();
1413 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001415 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001416 curbuf->b_visual.vi_mode = VIsual_mode;
1417 curbuf->b_visual.vi_start = VIsual;
1418 curbuf->b_visual.vi_end = curwin->w_cursor;
1419 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420#ifdef FEAT_EVAL
1421 curbuf->b_visual_mode_eval = VIsual_mode;
1422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (!virtual_active())
1424 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001425 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001427 adjust_cursor_eol();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001428 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429}
1430
1431/*
1432 * Reset VIsual_active and VIsual_reselect.
1433 */
1434 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001435reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436{
1437 if (VIsual_active)
1438 {
1439 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001440 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 }
1442 VIsual_reselect = FALSE;
1443}
1444
1445/*
1446 * Reset VIsual_active and VIsual_reselect if it's set.
1447 */
1448 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001449reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 if (VIsual_active)
1452 {
1453 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001454 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 VIsual_reselect = FALSE;
1456 }
1457}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001459 void
1460restore_visual_mode(void)
1461{
1462 if (VIsual_mode_orig != NUL)
1463 {
1464 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1465 VIsual_mode_orig = NUL;
1466 }
1467}
1468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469/*
1470 * Check for a balloon-eval special item to include when searching for an
1471 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1472 * Returns TRUE if the character at "*ptr" should be included.
1473 * "dir" is FORWARD or BACKWARD, the direction of searching.
1474 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1475 * "bnp" points to a counter for square brackets.
1476 */
1477 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001478find_is_eval_item(
1479 char_u *ptr,
1480 int *colp,
1481 int *bnp,
1482 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001484 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1486 ++*bnp;
1487 if (*bnp > 0)
1488 {
1489 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1490 --*bnp;
1491 return TRUE;
1492 }
1493
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001494 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (*ptr == '.')
1496 return TRUE;
1497
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001498 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1500 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1501 {
1502 *colp += dir;
1503 return TRUE;
1504 }
1505 return FALSE;
1506}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507
1508/*
1509 * Find the identifier under or to the right of the cursor.
1510 * "find_type" can have one of three values:
1511 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001512 * FIND_STRING: find any non-white text
1513 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001514 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 *
1516 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001517 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001519 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 * This doesn't match the real Vi but I like it a little better and it
1521 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001522 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 * When FIND_IDENT isn't defined, we backup until a blank.
1524 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001525 * Returns the length of the text, or zero if no text is found.
1526 * If text is found, a pointer to the text is put in "*text". This
1527 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 */
1529 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001530find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531{
1532 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001533 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534}
1535
1536/*
1537 * Like find_ident_under_cursor(), but for any window and any position.
1538 * However: Uses 'iskeyword' from the current window!.
1539 */
1540 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001541find_ident_at_pos(
1542 win_T *wp,
1543 linenr_T lnum,
1544 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001545 char_u **text,
1546 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001547 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001550 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 int this_class = 0;
1553 int prev_class;
1554 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001555 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
1557 /*
1558 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001559 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
1561 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1562 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1563 {
1564 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001565 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
1567 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (has_mbyte)
1569 {
1570 while (ptr[col] != NUL)
1571 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001572 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 this_class = mb_get_class(ptr + col);
1576 if (this_class != 0 && (i == 1 || this_class != 1))
1577 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001578 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 }
1581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001583 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 )
1586 ++col;
1587
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001588 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001592 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (has_mbyte)
1595 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001596 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1598 this_class = mb_get_class((char_u *)"a");
1599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001601 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 {
1603 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1604 prev_class = mb_get_class(ptr + prevcol);
1605 if (this_class != prev_class
1606 && (i == 0
1607 || prev_class == 0
1608 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 && (!(find_type & FIND_EVAL)
1610 || prevcol == 0
1611 || !find_is_eval_item(ptr + prevcol, &prevcol,
1612 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 )
1614 break;
1615 col = prevcol;
1616 }
1617
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001618 // If we don't want just any old text, or we've found an
1619 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 if (this_class > 2)
1621 this_class = 2;
1622 if (!(find_type & FIND_STRING) || this_class == 2)
1623 break;
1624 }
1625 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 {
1627 while (col > 0
1628 && ((i == 0
1629 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001630 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 && (!(find_type & FIND_IDENT)
1632 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 || ((find_type & FIND_EVAL)
1634 && col > 1
1635 && find_is_eval_item(ptr + col - 1, &col,
1636 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 ))
1638 --col;
1639
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001640 // If we don't want just any old text, or we've found an
1641 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1643 break;
1644 }
1645 }
1646
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001647 if (ptr[col] == NUL || (i == 0
1648 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001650 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001651 if ((find_type & FIND_NOERROR) == 0)
1652 {
1653 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001654 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001655 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001656 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 return 0;
1659 }
1660 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001661 *text = ptr;
1662 if (textcol != NULL)
1663 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001666 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 bn = 0;
1669 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (has_mbyte)
1672 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001673 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 this_class = mb_get_class(ptr);
1675 while (ptr[col] != NUL
1676 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1677 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 || ((find_type & FIND_EVAL)
1679 && col <= (int)startcol
1680 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001682 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001686 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 || ((find_type & FIND_EVAL)
1688 && col <= (int)startcol
1689 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 return col;
1694}
1695
1696/*
1697 * Prepare for redo of a normal command.
1698 */
1699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001700prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 prep_redo(cap->oap->regname, cap->count0,
1703 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1704}
1705
1706/*
1707 * Prepare for redo of any command.
1708 * Note that only the last argument can be a multi-byte char.
1709 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001710 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001711prep_redo(
1712 int regname,
1713 long num,
1714 int cmd1,
1715 int cmd2,
1716 int cmd3,
1717 int cmd4,
1718 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001720 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1721}
1722
1723/*
1724 * Prepare for redo of any command with extra count after "cmd2".
1725 */
1726 void
1727prep_redo_num2(
1728 int regname,
1729 long num1,
1730 int cmd1,
1731 int cmd2,
1732 long num2,
1733 int cmd3,
1734 int cmd4,
1735 int cmd5)
1736{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001738 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 {
1740 AppendCharToRedobuff('"');
1741 AppendCharToRedobuff(regname);
1742 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001743 if (num1 != 0)
1744 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 if (cmd1 != NUL)
1746 AppendCharToRedobuff(cmd1);
1747 if (cmd2 != NUL)
1748 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001749 if (num2 != 0)
1750 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 if (cmd3 != NUL)
1752 AppendCharToRedobuff(cmd3);
1753 if (cmd4 != NUL)
1754 AppendCharToRedobuff(cmd4);
1755 if (cmd5 != NUL)
1756 AppendCharToRedobuff(cmd5);
1757}
1758
1759/*
1760 * check for operator active and clear it
1761 *
1762 * return TRUE if operator was active
1763 */
1764 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001765checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 if (oap->op_type == OP_NOP)
1768 return FALSE;
1769 clearopbeep(oap);
1770 return TRUE;
1771}
1772
1773/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001774 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001776 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 */
1778 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001779checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001781 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 return FALSE;
1783 clearopbeep(oap);
1784 return TRUE;
1785}
1786
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001787 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001788clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 oap->op_type = OP_NOP;
1791 oap->regname = 0;
1792 oap->motion_force = NUL;
1793 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001794 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795}
1796
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001798clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
1800 clearop(oap);
1801 beep_flush();
1802}
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804/*
1805 * Remove the shift modifier from a special key.
1806 */
1807 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001808unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
1810 switch (cap->cmdchar)
1811 {
1812 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1813 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1814 case K_S_UP: cap->cmdchar = K_UP; break;
1815 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1816 case K_S_HOME: cap->cmdchar = K_HOME; break;
1817 case K_S_END: cap->cmdchar = K_END; break;
1818 }
1819 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1820}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001822/*
1823 * If the mode is currently displayed clear the command line or update the
1824 * command displayed.
1825 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001827may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001828{
1829 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001830 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001831#ifdef FEAT_CMDL_INFO
1832 else
1833 clear_showcmd();
1834#endif
1835}
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1838/*
1839 * Routines for displaying a partly typed command
1840 */
1841
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001842#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001844static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845static int showcmd_is_clear = TRUE;
1846static int showcmd_visual = FALSE;
1847
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001848static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
1850 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001851clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
1853 if (!p_sc)
1854 return;
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 if (VIsual_active && !char_avail())
1857 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001858 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 long lines;
1860 colnr_T leftcol, rightcol;
1861 linenr_T top, bot;
1862
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001863 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001864 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
1866 top = VIsual.lnum;
1867 bot = curwin->w_cursor.lnum;
1868 }
1869 else
1870 {
1871 top = curwin->w_cursor.lnum;
1872 bot = VIsual.lnum;
1873 }
1874# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001875 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001876 (void)hasFolding(top, &top, NULL);
1877 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878# endif
1879 lines = bot - top + 1;
1880
1881 if (VIsual_mode == Ctrl_V)
1882 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001883# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001884 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001885 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001886
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001887 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001888 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001889 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001892# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001893 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001894 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1897 (long)(rightcol - leftcol + 1));
1898 }
1899 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1900 sprintf((char *)showcmd_buf, "%ld", lines);
1901 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001902 {
1903 char_u *s, *e;
1904 int l;
1905 int bytes = 0;
1906 int chars = 0;
1907
1908 if (cursor_bot)
1909 {
1910 s = ml_get_pos(&VIsual);
1911 e = ml_get_cursor();
1912 }
1913 else
1914 {
1915 s = ml_get_cursor();
1916 e = ml_get_pos(&VIsual);
1917 }
1918 while ((*p_sel != 'e') ? s <= e : s < e)
1919 {
1920 l = (*mb_ptr2len)(s);
1921 if (l == 0)
1922 {
1923 ++bytes;
1924 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001925 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001926 }
1927 bytes += l;
1928 ++chars;
1929 s += l;
1930 }
1931 if (bytes == chars)
1932 sprintf((char *)showcmd_buf, "%d", chars);
1933 else
1934 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1935 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001936 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 showcmd_visual = TRUE;
1938 }
1939 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 showcmd_buf[0] = NUL;
1942 showcmd_visual = FALSE;
1943
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001944 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (showcmd_is_clear)
1946 return;
1947 }
1948
1949 display_showcmd();
1950}
1951
1952/*
1953 * Add 'c' to string of shown command chars.
1954 * Return TRUE if output has been written (and setcursor() has been called).
1955 */
1956 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001957add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 char_u *p;
1960 int old_len;
1961 int extra_len;
1962 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 int i;
1964 static int ignore[] =
1965 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001966#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1968 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001969#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001970 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001971 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1973 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001974 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001976 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 0
1978 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
Bram Moolenaar09df3122006-01-23 22:23:09 +00001980 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 return FALSE;
1982
1983 if (showcmd_visual)
1984 {
1985 showcmd_buf[0] = NUL;
1986 showcmd_visual = FALSE;
1987 }
1988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001989 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (IS_SPECIAL(c))
1991 for (i = 0; ignore[i] != 0; ++i)
1992 if (ignore[i] == c)
1993 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
1995 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001996 if (*p == ' ')
1997 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 old_len = (int)STRLEN(showcmd_buf);
1999 extra_len = (int)STRLEN(p);
2000 overflow = old_len + extra_len - SHOWCMD_COLS;
2001 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00002002 mch_memmove(showcmd_buf, showcmd_buf + overflow,
2003 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 STRCAT(showcmd_buf, p);
2005
2006 if (char_avail())
2007 return FALSE;
2008
2009 display_showcmd();
2010
2011 return TRUE;
2012}
2013
2014 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002015add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016{
2017 if (!add_to_showcmd(c))
2018 setcursor();
2019}
2020
2021/*
2022 * Delete 'len' characters from the end of the shown command.
2023 */
2024 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002025del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026{
2027 int old_len;
2028
2029 if (!p_sc)
2030 return;
2031
2032 old_len = (int)STRLEN(showcmd_buf);
2033 if (len > old_len)
2034 len = old_len;
2035 showcmd_buf[old_len - len] = NUL;
2036
2037 if (!char_avail())
2038 display_showcmd();
2039}
2040
2041/*
2042 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2043 * something and there is a partial mapping.
2044 */
2045 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002046push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047{
2048 if (p_sc)
2049 STRCPY(old_showcmd_buf, showcmd_buf);
2050}
2051
2052 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002053pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054{
2055 if (!p_sc)
2056 return;
2057
2058 STRCPY(showcmd_buf, old_showcmd_buf);
2059
2060 display_showcmd();
2061}
2062
2063 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002064display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
2066 int len;
2067
2068 cursor_off();
2069
2070 len = (int)STRLEN(showcmd_buf);
2071 if (len == 0)
2072 showcmd_is_clear = TRUE;
2073 else
2074 {
2075 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2076 showcmd_is_clear = FALSE;
2077 }
2078
2079 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002080 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2081 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 */
2083 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2084
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002085 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086}
2087#endif
2088
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089/*
2090 * When "check" is FALSE, prepare for commands that scroll the window.
2091 * When "check" is TRUE, take care of scroll-binding after the window has
2092 * scrolled. Called from normal_cmd() and edit().
2093 */
2094 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002095do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
2097 static win_T *old_curwin = NULL;
2098 static linenr_T old_topline = 0;
2099#ifdef FEAT_DIFF
2100 static int old_topfill = 0;
2101#endif
2102 static buf_T *old_buf = NULL;
2103 static colnr_T old_leftcol = 0;
2104
2105 if (check && curwin->w_p_scb)
2106 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002107 // If a ":syncbind" command was just used, don't scroll, only reset
2108 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (did_syncbind)
2110 did_syncbind = FALSE;
2111 else if (curwin == old_curwin)
2112 {
2113 /*
2114 * Synchronize other windows, as necessary according to
2115 * 'scrollbind'. Don't do this after an ":edit" command, except
2116 * when 'diff' is set.
2117 */
2118 if ((curwin->w_buffer == old_buf
2119#ifdef FEAT_DIFF
2120 || curwin->w_p_diff
2121#endif
2122 )
2123 && (curwin->w_topline != old_topline
2124#ifdef FEAT_DIFF
2125 || curwin->w_topfill != old_topfill
2126#endif
2127 || curwin->w_leftcol != old_leftcol))
2128 {
2129 check_scrollbind(curwin->w_topline - old_topline,
2130 (long)(curwin->w_leftcol - old_leftcol));
2131 }
2132 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002133 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
2135 /*
2136 * When switching between windows, make sure that the relative
2137 * vertical offset is valid for the new window. The relative
2138 * offset is invalid whenever another 'scrollbind' window has
2139 * scrolled to a point that would force the current window to
2140 * scroll past the beginning or end of its buffer. When the
2141 * resync is performed, some of the other 'scrollbind' windows may
2142 * need to jump so that the current window's relative position is
2143 * visible on-screen.
2144 */
2145 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2146 }
2147 curwin->w_scbind_pos = curwin->w_topline;
2148 }
2149
2150 old_curwin = curwin;
2151 old_topline = curwin->w_topline;
2152#ifdef FEAT_DIFF
2153 old_topfill = curwin->w_topfill;
2154#endif
2155 old_buf = curwin->w_buffer;
2156 old_leftcol = curwin->w_leftcol;
2157}
2158
2159/*
2160 * Synchronize any windows that have "scrollbind" set, based on the
2161 * number of rows by which the current window has changed
2162 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2163 */
2164 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002165check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
2167 int want_ver;
2168 int want_hor;
2169 win_T *old_curwin = curwin;
2170 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 int old_VIsual_select = VIsual_select;
2172 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 colnr_T tgt_leftcol = curwin->w_leftcol;
2174 long topline;
2175 long y;
2176
2177 /*
2178 * check 'scrollopt' string for vertical and horizontal scroll options
2179 */
2180 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2181#ifdef FEAT_DIFF
2182 want_ver |= old_curwin->w_p_diff;
2183#endif
2184 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2185
2186 /*
2187 * loop through the scrollbound windows and scroll accordingly
2188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002190 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
2192 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002193 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 if (curwin != old_curwin && curwin->w_p_scb)
2195 {
2196 /*
2197 * do the vertical scroll
2198 */
2199 if (want_ver)
2200 {
2201#ifdef FEAT_DIFF
2202 if (old_curwin->w_p_diff && curwin->w_p_diff)
2203 {
2204 diff_set_topline(old_curwin, curwin);
2205 }
2206 else
2207#endif
2208 {
2209 curwin->w_scbind_pos += topline_diff;
2210 topline = curwin->w_scbind_pos;
2211 if (topline > curbuf->b_ml.ml_line_count)
2212 topline = curbuf->b_ml.ml_line_count;
2213 if (topline < 1)
2214 topline = 1;
2215
2216 y = topline - curwin->w_topline;
2217 if (y > 0)
2218 scrollup(y, FALSE);
2219 else
2220 scrolldown(-y, FALSE);
2221 }
2222
2223 redraw_later(VALID);
2224 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
2227
2228 /*
2229 * do the horizontal scroll
2230 */
2231 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2232 {
2233 curwin->w_leftcol = tgt_leftcol;
2234 leftcol_changed();
2235 }
2236 }
2237 }
2238
2239 /*
2240 * reset current-window
2241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 VIsual_select = old_VIsual_select;
2243 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 curwin = old_curwin;
2245 curbuf = old_curbuf;
2246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
2248/*
2249 * Command character that's ignored.
2250 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002251 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002254nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002256 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257}
2258
2259/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002260 * Command character that doesn't do anything, but unlike nv_ignore() does
2261 * start edit(). Used for "startinsert" executed while starting up.
2262 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002264nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002265{
2266}
2267
2268/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 * Command character doesn't exist.
2270 */
2271 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002272nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
2274 clearopbeep(cap->oap);
2275}
2276
2277/*
2278 * <Help> and <F1> commands.
2279 */
2280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002281nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 if (!checkclearopq(cap->oap))
2284 ex_help(NULL);
2285}
2286
2287/*
2288 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2289 */
2290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002291nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002293#ifdef FEAT_JOB_CHANNEL
2294 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2295 clearopbeep(cap->oap);
2296 else
2297#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002298 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002299 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002300 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002301 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2302 op_addsub(cap->oap, cap->count1, cap->arg);
2303 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002304 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002305 else if (VIsual_active)
2306 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002307 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002308 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309}
2310
2311/*
2312 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2313 */
2314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002315nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
2317 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002318 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002319 if (mod_mask & MOD_MASK_CTRL)
2320 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002321 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002322 if (cap->arg == BACKWARD)
2323 goto_tabpage(-(int)cap->count1);
2324 else
2325 goto_tabpage((int)cap->count0);
2326 }
2327 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002328 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330}
2331
2332/*
2333 * Implementation of "gd" and "gD" command.
2334 */
2335 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002336nv_gd(
2337 oparg_T *oap,
2338 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002339 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340{
2341 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 char_u *ptr;
2343
Bram Moolenaard9d30582005-05-18 22:10:28 +00002344 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002345 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002346 == FAIL)
2347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002349 }
2350 else
2351 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002352#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002353 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2354 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002355#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002356 // clear any search statistics
2357 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2358 clear_cmdline = TRUE;
2359 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002360}
2361
2362/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002363 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2364 * otherwise.
2365 */
2366 static int
2367is_ident(char_u *line, int offset)
2368{
2369 int i;
2370 int incomment = FALSE;
2371 int instring = 0;
2372 int prev = 0;
2373
2374 for (i = 0; i < offset && line[i] != NUL; i++)
2375 {
2376 if (instring != 0)
2377 {
2378 if (prev != '\\' && line[i] == instring)
2379 instring = 0;
2380 }
2381 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2382 {
2383 instring = line[i];
2384 }
2385 else
2386 {
2387 if (incomment)
2388 {
2389 if (prev == '*' && line[i] == '/')
2390 incomment = FALSE;
2391 }
2392 else if (prev == '/' && line[i] == '*')
2393 {
2394 incomment = TRUE;
2395 }
2396 else if (prev == '/' && line[i] == '/')
2397 {
2398 return FALSE;
2399 }
2400 }
2401
2402 prev = line[i];
2403 }
2404
2405 return incomment == FALSE && instring == 0;
2406}
2407
2408/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002409 * Search for variable declaration of "ptr[len]".
2410 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2411 * current file ("gD").
2412 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002413 * Return FAIL when not found.
2414 */
2415 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002416find_decl(
2417 char_u *ptr,
2418 int len,
2419 int locally,
2420 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002421 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002422{
2423 char_u *pat;
2424 pos_T old_pos;
2425 pos_T par_pos;
2426 pos_T found_pos;
2427 int t;
2428 int save_p_ws;
2429 int save_p_scs;
2430 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002431 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002432 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002433 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002434
2435 if ((pat = alloc(len + 7)) == NULL)
2436 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002437
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002438 // Put "\V" before the pattern to avoid that the special meaning of "."
2439 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002440 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2441 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 old_pos = curwin->w_cursor;
2443 save_p_ws = p_ws;
2444 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002445 p_ws = FALSE; // don't wrap around end of file now
2446 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
2448 /*
2449 * With "gD" go to line 1.
2450 * With "gd" Search back for the start of the current function, then go
2451 * back until a blank line. If this fails go to line 1.
2452 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002453 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002455 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002457 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 }
2459 else
2460 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002461 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2463 --curwin->w_cursor.lnum;
2464 }
2465 curwin->w_cursor.col = 0;
2466
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002467 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002468 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002469 for (;;)
2470 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002471 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002472 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002473 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002474 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002475
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002476 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002477 {
2478 pos_T *pos;
2479
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002480 // Check that the block the match is in doesn't end before the
2481 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002482 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2483 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2484 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002485 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002486 // There can't be a useful match before the end of this block.
2487 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002488 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002489 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002490 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002491 }
2492
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002493 if (t == FAIL)
2494 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002495 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002496 if (found_pos.lnum != 0)
2497 {
2498 curwin->w_cursor = found_pos;
2499 t = OK;
2500 }
2501 break;
2502 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002503 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002504 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002505 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002506 ++curwin->w_cursor.lnum;
2507 curwin->w_cursor.col = 0;
2508 continue;
2509 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002510 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2511
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002512 // If the current position is not a valid identifier and a previous
2513 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002514 if (!valid && found_pos.lnum != 0)
2515 {
2516 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002517 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002518 }
2519
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002520 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002521 if (valid && !locally)
2522 break;
2523 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002524 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002525 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002526 if (found_pos.lnum != 0)
2527 curwin->w_cursor = found_pos;
2528 break;
2529 }
2530
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002531 // For finding a local variable and the match is before the "{" or
2532 // inside a comment, continue searching. For K&R style function
2533 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002534 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002535 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002536 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002537 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002538 // Remove SEARCH_START from flags to avoid getting stuck at one
2539 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002540 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002542
2543 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002545 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 curwin->w_cursor = old_pos;
2547 }
2548 else
2549 {
2550 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002551 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 reset_search_dir();
2553 }
2554
2555 vim_free(pat);
2556 p_ws = save_p_ws;
2557 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002558
2559 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560}
2561
2562/*
2563 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2564 * lines rather than lines in the file.
2565 * 'dist' must be positive.
2566 *
2567 * Return OK if able to move cursor, FAIL otherwise.
2568 */
2569 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002570nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 int linelen = linetabsize(ml_get_curline());
2573 int retval = OK;
2574 int atend = FALSE;
2575 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002576 int col_off1; // margin offset for first screen line
2577 int col_off2; // margin offset for wrapped screen line
2578 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002579 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002582 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584 col_off1 = curwin_col_off();
2585 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002586 width1 = curwin->w_width - col_off1;
2587 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002588 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002589 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002592 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 /*
2594 * Instead of sticking at the last character of the buffer line we
2595 * try to stick in the last column of the screen.
2596 */
2597 if (curwin->w_curswant == MAXCOL)
2598 {
2599 atend = TRUE;
2600 validate_virtcol();
2601 if (width1 <= 0)
2602 curwin->w_curswant = 0;
2603 else
2604 {
2605 curwin->w_curswant = width1 - 1;
2606 if (curwin->w_virtcol > curwin->w_curswant)
2607 curwin->w_curswant += ((curwin->w_virtcol
2608 - curwin->w_curswant - 1) / width2 + 1) * width2;
2609 }
2610 }
2611 else
2612 {
2613 if (linelen > width1)
2614 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2615 else
2616 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002617 if (curwin->w_curswant >= (colnr_T)n)
2618 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
2620
2621 while (dist--)
2622 {
2623 if (dir == BACKWARD)
2624 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002625 if ((long)curwin->w_curswant >= width1
2626#ifdef FEAT_FOLDING
2627 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2628#endif
2629 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002630 // Move back within the line. This can give a negative value
2631 // for w_curswant if width1 < width2 (with cpoptions+=n),
2632 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 curwin->w_curswant -= width2;
2634 else
2635 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002636 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002638 // Move to the start of a closed fold. Don't do that when
2639 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (!(fdo_flags & FDO_ALL))
2641 (void)hasFolding(curwin->w_cursor.lnum,
2642 &curwin->w_cursor.lnum, NULL);
2643#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002644 if (curwin->w_cursor.lnum == 1)
2645 {
2646 retval = FAIL;
2647 break;
2648 }
2649 --curwin->w_cursor.lnum;
2650
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 linelen = linetabsize(ml_get_curline());
2652 if (linelen > width1)
2653 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2654 + 1) * width2;
2655 }
2656 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002657 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 if (linelen > width1)
2660 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2661 else
2662 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002663 if (curwin->w_curswant + width2 < (colnr_T)n
2664#ifdef FEAT_FOLDING
2665 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2666#endif
2667 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002668 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 curwin->w_curswant += width2;
2670 else
2671 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002672 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002674 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2676 &curwin->w_cursor.lnum);
2677#endif
2678 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2679 {
2680 retval = FAIL;
2681 break;
2682 }
2683 curwin->w_cursor.lnum++;
2684 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002685 // Check if the cursor has moved below the number display
2686 // when width1 < width2 (with cpoptions+=n). Subtract width2
2687 // to get a negative value for w_curswant, which will get
2688 // clipped to column 0.
2689 if (curwin->w_curswant >= width1)
2690 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002691 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
2693 }
2694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002697 if (virtual_active() && atend)
2698 coladvance(MAXCOL);
2699 else
2700 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2703 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002704 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002705 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002706
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 /*
2708 * Check for landing on a character that got split at the end of the
2709 * last line. We want to advance a screenline, not end up in the same
2710 * screenline or move two screenlines.
2711 */
2712 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002713 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002714#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002715 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2716 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002717#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002718
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002719 c = (*mb_ptr2char)(ml_get_cursor());
2720 if (dir == FORWARD && virtcol < curwin->w_curswant
2721 && (curwin->w_curswant <= (colnr_T)width1)
2722 && !vim_isprintc(c) && c > 255)
2723 oneright();
2724
Bram Moolenaar773b1582014-08-29 14:20:51 +02002725 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 && (curwin->w_curswant < (colnr_T)width1
2727 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2728 : ((curwin->w_curswant - width1) % width2
2729 > (colnr_T)width2 / 2)))
2730 --curwin->w_cursor.col;
2731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
2733 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002734 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 return retval;
2737}
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739/*
2740 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2741 * cap->arg must be TRUE for CTRL-E.
2742 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002743 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002744nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 if (!checkclearop(cap->oap))
2747 scroll_redraw(cap->arg, cap->count1);
2748}
2749
2750/*
2751 * Scroll "count" lines up or down, and redraw.
2752 */
2753 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002754scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 linenr_T prev_topline = curwin->w_topline;
2757#ifdef FEAT_DIFF
2758 int prev_topfill = curwin->w_topfill;
2759#endif
2760 linenr_T prev_lnum = curwin->w_cursor.lnum;
2761
2762 if (up)
2763 scrollup(count, TRUE);
2764 else
2765 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002766 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002768 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2769 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 cursor_correct();
2771 check_cursor_moved(curwin);
2772 curwin->w_valid |= VALID_TOPLINE;
2773
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002774 // If moved back to where we were, at least move the cursor, otherwise
2775 // we get stuck at one position. Don't move the cursor up if the
2776 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 while (curwin->w_topline == prev_topline
2778#ifdef FEAT_DIFF
2779 && curwin->w_topfill == prev_topfill
2780#endif
2781 )
2782 {
2783 if (up)
2784 {
2785 if (curwin->w_cursor.lnum > prev_lnum
2786 || cursor_down(1L, FALSE) == FAIL)
2787 break;
2788 }
2789 else
2790 {
2791 if (curwin->w_cursor.lnum < prev_lnum
2792 || prev_topline == 1L
2793 || cursor_up(1L, FALSE) == FAIL)
2794 break;
2795 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002796 // Mark w_topline as valid, otherwise the screen jumps back at the
2797 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 check_cursor_moved(curwin);
2799 curwin->w_valid |= VALID_TOPLINE;
2800 }
2801 }
2802 if (curwin->w_cursor.lnum != prev_lnum)
2803 coladvance(curwin->w_curswant);
2804 redraw_later(VALID);
2805}
2806
2807/*
2808 * Commands that start with "z".
2809 */
2810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002811nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
2813 long n;
2814 colnr_T col;
2815 int nchar = cap->nchar;
2816#ifdef FEAT_FOLDING
2817 long old_fdl = curwin->w_p_fdl;
2818 int old_fen = curwin->w_p_fen;
2819#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002820#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002821 int undo = FALSE;
2822#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002823 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
2825 if (VIM_ISDIGIT(nchar))
2826 {
2827 /*
2828 * "z123{nchar}": edit the count before obtaining {nchar}
2829 */
2830 if (checkclearop(cap->oap))
2831 return;
2832 n = nchar - '0';
2833 for (;;)
2834 {
2835#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002836 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837#endif
2838 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002839 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002840 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 --no_mapping;
2843 --allow_keys;
2844#ifdef FEAT_CMDL_INFO
2845 (void)add_to_showcmd(nchar);
2846#endif
2847 if (nchar == K_DEL || nchar == K_KDEL)
2848 n /= 10;
2849 else if (VIM_ISDIGIT(nchar))
2850 n = n * 10 + (nchar - '0');
2851 else if (nchar == CAR)
2852 {
2853#ifdef FEAT_GUI
2854 need_mouse_correct = TRUE;
2855#endif
2856 win_setheight((int)n);
2857 break;
2858 }
2859 else if (nchar == 'l'
2860 || nchar == 'h'
2861 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002862 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
2864 cap->count1 = n ? n * cap->count1 : cap->count1;
2865 goto dozet;
2866 }
2867 else
2868 {
2869 clearopbeep(cap->oap);
2870 break;
2871 }
2872 }
2873 cap->oap->op_type = OP_NOP;
2874 return;
2875 }
2876
2877dozet:
2878 if (
2879#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002880 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2881 // and "zC" only in Visual mode. "zj" and "zk" are motion
2882 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 cap->nchar != 'f' && cap->nchar != 'F'
2884 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2885 && cap->nchar != 'j' && cap->nchar != 'k'
2886 &&
2887#endif
2888 checkclearop(cap->oap))
2889 return;
2890
2891 /*
2892 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2893 * If line number given, set cursor.
2894 */
2895 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2896 && cap->count0
2897 && cap->count0 != curwin->w_cursor.lnum)
2898 {
2899 setpcmark();
2900 if (cap->count0 > curbuf->b_ml.ml_line_count)
2901 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2902 else
2903 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002904 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 }
2906
2907 switch (nchar)
2908 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002909 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 case '+':
2911 if (cap->count0 == 0)
2912 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002913 // No count given: put cursor at the line below screen
2914 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2916 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2917 else
2918 curwin->w_cursor.lnum = curwin->w_botline;
2919 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002920 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 case NL:
2922 case CAR:
2923 case K_KENTER:
2924 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002925 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
2927 case 't': scroll_cursor_top(0, TRUE);
2928 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002929 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 break;
2931
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002932 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002934 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
2936 case 'z': scroll_cursor_halfway(TRUE);
2937 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002938 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 break;
2940
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002941 // "z^", "z-" and "zb": put cursor at bottom of screen
2942 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2943 // when <count> is at bottom of window, and puts that one at
2944 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 if (cap->count0 != 0)
2946 {
2947 scroll_cursor_bot(0, TRUE);
2948 curwin->w_cursor.lnum = curwin->w_topline;
2949 }
2950 else if (curwin->w_topline == 1)
2951 curwin->w_cursor.lnum = 1;
2952 else
2953 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002954 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 case '-':
2956 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002957 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 case 'b': scroll_cursor_bot(0, TRUE);
2960 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002961 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 break;
2963
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002964 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002966 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002967 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002969 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 case 'h':
2971 case K_LEFT:
2972 if (!curwin->w_p_wrap)
2973 {
2974 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2975 curwin->w_leftcol = 0;
2976 else
2977 curwin->w_leftcol -= (colnr_T)cap->count1;
2978 leftcol_changed();
2979 }
2980 break;
2981
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002982 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002983 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002984 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002986 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 case 'l':
2988 case K_RIGHT:
2989 if (!curwin->w_p_wrap)
2990 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002991 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 curwin->w_leftcol += (colnr_T)cap->count1;
2993 leftcol_changed();
2994 }
2995 break;
2996
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002997 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 case 's': if (!curwin->w_p_wrap)
2999 {
3000#ifdef FEAT_FOLDING
3001 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003002 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 else
3004#endif
3005 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01003006 if ((long)col > siso)
3007 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 else
3009 col = 0;
3010 if (curwin->w_leftcol != col)
3011 {
3012 curwin->w_leftcol = col;
3013 redraw_later(NOT_VALID);
3014 }
3015 }
3016 break;
3017
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003018 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 case 'e': if (!curwin->w_p_wrap)
3020 {
3021#ifdef FEAT_FOLDING
3022 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003023 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 else
3025#endif
3026 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02003027 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01003028 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 col = 0;
3030 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01003031 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (curwin->w_leftcol != col)
3033 {
3034 curwin->w_leftcol = col;
3035 redraw_later(NOT_VALID);
3036 }
3037 }
3038 break;
3039
Christian Brabandt2fa93842021-05-30 22:17:25 +02003040 // "zp", "zP" in block mode put without addind trailing spaces
3041 case 'P':
3042 case 'p': nv_put(cap);
3043 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02003044 // "zy" Yank without trailing spaces
3045 case 'y': nv_operator(cap);
3046 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003048 // "zF": create fold command
3049 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 case 'F':
3051 case 'f': if (foldManualAllowed(TRUE))
3052 {
3053 cap->nchar = 'f';
3054 nv_operator(cap);
3055 curwin->w_p_fen = TRUE;
3056
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003057 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3059 {
3060 nv_operator(cap);
3061 finish_op = TRUE;
3062 }
3063 }
3064 else
3065 clearopbeep(cap->oap);
3066 break;
3067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003068 // "zd": delete fold at cursor
3069 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 case 'd':
3071 case 'D': if (foldManualAllowed(FALSE))
3072 {
3073 if (VIsual_active)
3074 nv_operator(cap);
3075 else
3076 deleteFold(curwin->w_cursor.lnum,
3077 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3078 }
3079 break;
3080
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003081 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 case 'E': if (foldmethodIsManual(curwin))
3083 {
3084 clearFolding(curwin);
3085 changed_window_setting();
3086 }
3087 else if (foldmethodIsMarker(curwin))
3088 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3089 TRUE, FALSE);
3090 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003091 emsg(_(e_cannot_erase_folds_with_current_foldmethod));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 break;
3093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 case 'n': curwin->w_p_fen = FALSE;
3096 break;
3097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 case 'N': curwin->w_p_fen = TRUE;
3100 break;
3101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003102 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3104 break;
3105
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003106 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3108 openFold(curwin->w_cursor.lnum, cap->count1);
3109 else
3110 {
3111 closeFold(curwin->w_cursor.lnum, cap->count1);
3112 curwin->w_p_fen = TRUE;
3113 }
3114 break;
3115
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003116 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3118 openFoldRecurse(curwin->w_cursor.lnum);
3119 else
3120 {
3121 closeFoldRecurse(curwin->w_cursor.lnum);
3122 curwin->w_p_fen = TRUE;
3123 }
3124 break;
3125
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003126 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 case 'o': if (VIsual_active)
3128 nv_operator(cap);
3129 else
3130 openFold(curwin->w_cursor.lnum, cap->count1);
3131 break;
3132
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003133 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 case 'O': if (VIsual_active)
3135 nv_operator(cap);
3136 else
3137 openFoldRecurse(curwin->w_cursor.lnum);
3138 break;
3139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003140 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 case 'c': if (VIsual_active)
3142 nv_operator(cap);
3143 else
3144 closeFold(curwin->w_cursor.lnum, cap->count1);
3145 curwin->w_p_fen = TRUE;
3146 break;
3147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003148 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 case 'C': if (VIsual_active)
3150 nv_operator(cap);
3151 else
3152 closeFoldRecurse(curwin->w_cursor.lnum);
3153 curwin->w_p_fen = TRUE;
3154 break;
3155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003156 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 case 'v': foldOpenCursor();
3158 break;
3159
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003160 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003162 curwin->w_foldinvalid = TRUE; // recompute folds
3163 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 foldOpenCursor();
3165 break;
3166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003167 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003169 curwin->w_foldinvalid = TRUE; // recompute folds
3170 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 break;
3172
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003173 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003175 {
3176 curwin->w_p_fdl -= cap->count1;
3177 if (curwin->w_p_fdl < 0)
3178 curwin->w_p_fdl = 0;
3179 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003180 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 curwin->w_p_fen = TRUE;
3182 break;
3183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003184 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003186 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 curwin->w_p_fen = TRUE;
3188 break;
3189
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003190 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003191 case 'r': curwin->w_p_fdl += cap->count1;
3192 {
3193 int d = getDeepestNesting();
3194
3195 if (curwin->w_p_fdl >= d)
3196 curwin->w_p_fdl = d;
3197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 break;
3199
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003200 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003202 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 break;
3204
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003205 case 'j': // "zj" move to next fold downwards
3206 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3208 cap->count1) == FAIL)
3209 clearopbeep(cap->oap);
3210 break;
3211
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003212#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003214#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003215 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003216 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003217 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003218 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003219 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003220 --no_mapping;
3221 --allow_keys;
3222#ifdef FEAT_CMDL_INFO
3223 (void)add_to_showcmd(nchar);
3224#endif
3225 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3226 {
3227 clearopbeep(cap->oap);
3228 break;
3229 }
3230 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003231 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003232
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003233 case 'g': // "zg": add good word to word list
3234 case 'w': // "zw": add wrong word to word list
3235 case 'G': // "zG": add good word to temp word list
3236 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003237 {
3238 char_u *ptr = NULL;
3239 int len;
3240
3241 if (checkclearop(cap->oap))
3242 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003243 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3244 == FAIL)
3245 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003246 if (ptr == NULL)
3247 {
3248 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003249
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003250 // Find bad word under the cursor. When 'spell' is
3251 // off this fails and find_ident_under_cursor() is
3252 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003253 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003254 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003255 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003256 if (len != 0 && curwin->w_cursor.col <= pos.col)
3257 ptr = ml_get_pos(&curwin->w_cursor);
3258 curwin->w_cursor = pos;
3259 }
3260
Bram Moolenaarb765d632005-06-07 21:00:02 +00003261 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3262 FIND_IDENT)) == 0)
3263 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003264 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3265 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003266 (nchar == 'G' || nchar == 'W')
3267 ? 0 : (int)cap->count1,
3268 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003269 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003270 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003271
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003272 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003273 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003274 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003275 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003276#endif
3277
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 default: clearopbeep(cap->oap);
3279 }
3280
3281#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (old_fen != curwin->w_p_fen)
3284 {
3285# ifdef FEAT_DIFF
3286 win_T *wp;
3287
3288 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3289 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003290 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 FOR_ALL_WINDOWS(wp)
3292 {
3293 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3294 {
3295 wp->w_p_fen = curwin->w_p_fen;
3296 changed_window_setting_win(wp);
3297 }
3298 }
3299 }
3300# endif
3301 changed_window_setting();
3302 }
3303
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003304 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (old_fdl != curwin->w_p_fdl)
3306 newFoldLevel();
3307#endif
3308}
3309
3310#ifdef FEAT_GUI
3311/*
3312 * Vertical scrollbar movement.
3313 */
3314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003315nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 if (cap->oap->op_type != OP_NOP)
3318 clearopbeep(cap->oap);
3319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003320 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 gui_do_scroll();
3322}
3323
3324/*
3325 * Horizontal scrollbar movement.
3326 */
3327 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003328nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330 if (cap->oap->op_type != OP_NOP)
3331 clearopbeep(cap->oap);
3332
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003333 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003334 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335}
3336#endif
3337
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003338#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003339/*
3340 * Click in GUI tab.
3341 */
3342 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003343nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003344{
3345 if (cap->oap->op_type != OP_NOP)
3346 clearopbeep(cap->oap);
3347
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003348 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003349 goto_tabpage(current_tab);
3350}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003351
3352/*
3353 * Selected item in tab line menu.
3354 */
3355 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003356nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003357{
3358 if (cap->oap->op_type != OP_NOP)
3359 clearopbeep(cap->oap);
3360
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003361 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003362 handle_tabmenu();
3363}
3364
3365/*
3366 * Handle selecting an item of the GUI tab line menu.
3367 * Used in Normal and Insert mode.
3368 */
3369 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003370handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003371{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003372 switch (current_tabmenu)
3373 {
3374 case TABLINE_MENU_CLOSE:
3375 if (current_tab == 0)
3376 do_cmdline_cmd((char_u *)"tabclose");
3377 else
3378 {
3379 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3380 current_tab);
3381 do_cmdline_cmd(IObuff);
3382 }
3383 break;
3384
3385 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003386 if (current_tab == 0)
3387 do_cmdline_cmd((char_u *)"$tabnew");
3388 else
3389 {
3390 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3391 current_tab - 1);
3392 do_cmdline_cmd(IObuff);
3393 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003394 break;
3395
3396 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003397 if (current_tab == 0)
3398 do_cmdline_cmd((char_u *)"browse $tabnew");
3399 else
3400 {
3401 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3402 current_tab - 1);
3403 do_cmdline_cmd(IObuff);
3404 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003405 break;
3406 }
3407}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003408#endif
3409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410/*
3411 * "Q" command.
3412 */
3413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003414nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
3416 /*
3417 * Ignore 'Q' in Visual mode, just give a beep.
3418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003420 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003421 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 do_exmode(FALSE);
3423}
3424
3425/*
3426 * Handle a ":" command.
3427 */
3428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003429nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003431 int old_p_im;
3432 int cmd_result;
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003433 int is_cmdkey = cap->cmdchar == K_COMMAND
3434 || cap->cmdchar == K_SCRIPT_COMMAND;
3435 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar957cf672020-11-12 14:21:06 +01003437 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 nv_operator(cap);
3439 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
3441 if (cap->oap->op_type != OP_NOP)
3442 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003443 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 cap->oap->motion_type = MCHAR;
3445 cap->oap->inclusive = FALSE;
3446 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003447 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003449 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 stuffcharReadbuff('.');
3451 if (cap->count0 > 1)
3452 {
3453 stuffReadbuff((char_u *)",.+");
3454 stuffnumReadbuff((long)cap->count0 - 1L);
3455 }
3456 }
3457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (KeyTyped)
3460 compute_cmdrow();
3461
3462 old_p_im = p_im;
3463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003464 // get a command line and execute it
Bram Moolenaare32c3c42022-01-15 18:26:04 +00003465 flags = cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0;
3466 if (is_cmdkey)
3467 cmd_result = do_cmdkey_command(cap->cmdchar, flags);
3468 else
3469 cmd_result = do_cmdline(NULL, getexline, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003471 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 if (p_im != old_p_im)
3473 {
3474 if (p_im)
3475 restart_edit = 'i';
3476 else
3477 restart_edit = 0;
3478 }
3479
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003480 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003481 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003482 clearop(cap->oap);
3483 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3485 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003486 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3487 || did_emsg
3488 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003489 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 clearopbeep(cap->oap);
3491 }
3492}
3493
3494/*
3495 * Handle CTRL-G command.
3496 */
3497 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003498nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003500 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 VIsual_select = !VIsual_select;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003503 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 showmode();
3505 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003506 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003507 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 fileinfo((int)cap->count0, FALSE, TRUE);
3509}
3510
3511/*
3512 * Handle CTRL-H <Backspace> command.
3513 */
3514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003515nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (VIsual_active && VIsual_select)
3518 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003519 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 v_visop(cap);
3521 }
3522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 nv_left(cap);
3524}
3525
3526/*
3527 * CTRL-L: clear screen and redraw.
3528 */
3529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003530nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532 if (!checkclearop(cap->oap))
3533 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003535 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003536 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003537# ifdef FEAT_RELTIME
3538 {
3539 win_T *wp;
3540
3541 FOR_ALL_WINDOWS(wp)
3542 wp->w_s->b_syn_slow = FALSE;
3543 }
3544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545#endif
3546 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003547#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3548# ifdef VIMDLL
3549 if (!gui.in_use)
3550# endif
3551 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
3554}
3555
3556/*
3557 * CTRL-O: In Select mode: switch to Visual mode for one command.
3558 * Otherwise: Go to older pcmark.
3559 */
3560 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003561nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 if (VIsual_active && VIsual_select)
3564 {
3565 VIsual_select = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003566 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003568 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
3570 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 cap->count1 = -cap->count1;
3573 nv_pcmark(cap);
3574 }
3575}
3576
3577/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003578 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3579 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 */
3581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003582nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583{
3584 if (!checkclearopq(cap->oap))
3585 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3586 GETF_SETMARK|GETF_ALT, FALSE);
3587}
3588
3589/*
3590 * "Z" commands.
3591 */
3592 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003593nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594{
3595 if (!checkclearopq(cap->oap))
3596 {
3597 switch (cap->nchar)
3598 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003599 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 case 'Z': do_cmdline_cmd((char_u *)"x");
3601 break;
3602
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003603 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 case 'Q': do_cmdline_cmd((char_u *)"q!");
3605 break;
3606
3607 default: clearopbeep(cap->oap);
3608 }
3609 }
3610}
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612/*
3613 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3614 */
3615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003616do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 oparg_T oa;
3619 cmdarg_T ca;
3620
3621 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003622 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 ca.oap = &oa;
3624 ca.cmdchar = c1;
3625 ca.nchar = c2;
3626 nv_ident(&ca);
3627}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628
3629/*
3630 * Handle the commands that use the word under the cursor.
3631 * [g] CTRL-] :ta to current identifier
3632 * [g] 'K' run program for current identifier
3633 * [g] '*' / to current identifier or string
3634 * [g] '#' ? to current identifier or string
3635 * g ']' :tselect for current identifier
3636 */
3637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003638nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
3640 char_u *ptr = NULL;
3641 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003642 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003643 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003645 char_u *kp; // value of 'keywordprg'
3646 int kp_help; // 'keywordprg' is ":he"
3647 int kp_ex; // 'keywordprg' starts with ":"
3648 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003650 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003651 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 char_u *aux_ptr;
3653 int isman;
3654 int isman_s;
3655
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003656 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 cmdchar = cap->nchar;
3659 g_cmd = TRUE;
3660 }
3661 else
3662 {
3663 cmdchar = cap->cmdchar;
3664 g_cmd = FALSE;
3665 }
3666
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003667 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 cmdchar = '#';
3669
3670 /*
3671 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3672 */
3673 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (checkclearopq(cap->oap))
3678 return;
3679 }
3680
3681 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3682 (cmdchar == '*' || cmdchar == '#')
3683 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3684 {
3685 clearop(cap->oap);
3686 return;
3687 }
3688
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003689 // Allocate buffer to put the command in. Inserting backslashes can
3690 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3691 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3693 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3694 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003695 if (kp_help && *skipwhite(ptr) == NUL)
3696 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003697 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003698 return;
3699 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003700 kp_ex = (*kp == ':');
3701 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3702 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 if (buf == NULL)
3704 return;
3705 buf[0] = NUL;
3706
3707 switch (cmdchar)
3708 {
3709 case '*':
3710 case '#':
3711 /*
3712 * Put cursor at start of word, makes search skip the word
3713 * under the cursor.
3714 * Call setpcmark() first, so "*``" puts the cursor back where
3715 * it was.
3716 */
3717 setpcmark();
3718 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3719
3720 if (!g_cmd && vim_iswordp(ptr))
3721 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003722 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 break;
3724
3725 case 'K':
3726 if (kp_help)
3727 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003728 else if (kp_ex)
3729 {
3730 if (cap->count0 != 0)
3731 vim_snprintf((char *)buf, buflen, "%s %ld",
3732 kp, cap->count0);
3733 else
3734 STRCPY(buf, kp);
3735 STRCAT(buf, " ");
3736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 else
3738 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003739 // An external command will probably use an argument starting
3740 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003741 while (*ptr == '-' && n > 0)
3742 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003743 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003744 --n;
3745 }
3746 if (n == 0)
3747 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003748 // found dashes only
3749 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003750 vim_free(buf);
3751 return;
3752 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003753
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003754 // When a count is given, turn it into a range. Is this
3755 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 isman = (STRCMP(kp, "man") == 0);
3757 isman_s = (STRCMP(kp, "man -s") == 0);
3758 if (cap->count0 != 0 && !(isman || isman_s))
3759 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3760
3761 STRCAT(buf, "! ");
3762 if (cap->count0 == 0 && isman_s)
3763 STRCAT(buf, "man");
3764 else
3765 STRCAT(buf, kp);
3766 STRCAT(buf, " ");
3767 if (cap->count0 != 0 && (isman || isman_s))
3768 {
3769 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3770 STRCAT(buf, " ");
3771 }
3772 }
3773 break;
3774
3775 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003776 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777#ifdef FEAT_CSCOPE
3778 if (p_cst)
3779 STRCPY(buf, "cstag ");
3780 else
3781#endif
3782 STRCPY(buf, "ts ");
3783 break;
3784
3785 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003786 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (curbuf->b_help)
3788 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003790 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003791 if (g_cmd)
3792 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003793 else if (cap->count0 == 0)
3794 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003795 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003796 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799
3800 /*
3801 * Now grab the chars in the identifier
3802 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003803 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003805 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003806 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003807 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003808 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003809 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003810 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003811 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003812 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003813 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003815 vim_free(buf);
3816 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003818 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003819 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003820 {
3821 vim_free(buf);
3822 vim_free(p);
3823 return;
3824 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003825 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003826 STRCAT(buf, p);
3827 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003829 else
3830 {
3831 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003832 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003833 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003834 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003835 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003836 {
3837 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003838 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003839 aux_ptr = (char_u *)"";
3840 else
3841 aux_ptr = (char_u *)"\\|\"\n[";
3842 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003843 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003844 aux_ptr = (char_u *)"\\|\"\n*?[";
3845
3846 p = buf + STRLEN(buf);
3847 while (n-- > 0)
3848 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003849 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003850 if (vim_strchr(aux_ptr, *ptr) != NULL)
3851 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003852 // When current byte is a part of multibyte character, copy all
3853 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003854 if (has_mbyte)
3855 {
3856 int i;
3857 int len = (*mb_ptr2len)(ptr) - 1;
3858
3859 for (i = 0; i < len && n >= 1; ++i, --n)
3860 *p++ = *ptr++;
3861 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003862 *p++ = *ptr++;
3863 }
3864 *p = NUL;
3865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
3867 /*
3868 * Execute the command.
3869 */
3870 if (cmdchar == '*' || cmdchar == '#')
3871 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003872 if (!g_cmd && (has_mbyte
3873 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3874 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003876
3877 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003878 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003880
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003881 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003884 {
3885 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003887 g_tag_at_cursor = FALSE;
3888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 vim_free(buf);
3891}
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893/*
3894 * Get visually selected text, within one line only.
3895 * Returns FAIL if more than one line selected.
3896 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003897 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003898get_visual_text(
3899 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003900 char_u **pp, // return: start of selected text
3901 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
3903 if (VIsual_mode != 'V')
3904 unadjust_for_sel();
3905 if (VIsual.lnum != curwin->w_cursor.lnum)
3906 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003907 if (cap != NULL)
3908 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return FAIL;
3910 }
3911 if (VIsual_mode == 'V')
3912 {
3913 *pp = ml_get_curline();
3914 *lenp = (int)STRLEN(*pp);
3915 }
3916 else
3917 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003918 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
3920 *pp = ml_get_pos(&curwin->w_cursor);
3921 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3922 }
3923 else
3924 {
3925 *pp = ml_get_pos(&VIsual);
3926 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3927 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003928 if (**pp == NUL)
3929 *lenp = 0;
3930 if (has_mbyte && *lenp > 0)
3931 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003932 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934 reset_VIsual_and_resel();
3935 return OK;
3936}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938/*
3939 * CTRL-T: backwards in tag stack
3940 */
3941 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003942nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943{
3944 if (!checkclearopq(cap->oap))
3945 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3946}
3947
3948/*
3949 * Handle scrolling command 'H', 'L' and 'M'.
3950 */
3951 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003952nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
3954 int used = 0;
3955 long n;
3956#ifdef FEAT_FOLDING
3957 linenr_T lnum;
3958#endif
3959 int half;
3960
3961 cap->oap->motion_type = MLINE;
3962 setpcmark();
3963
3964 if (cap->cmdchar == 'L')
3965 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003966 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 curwin->w_cursor.lnum = curwin->w_botline - 1;
3968 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3969 curwin->w_cursor.lnum = 1;
3970 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003971 {
3972#ifdef FEAT_FOLDING
3973 if (hasAnyFolding(curwin))
3974 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003975 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003976 for (n = cap->count1 - 1; n > 0
3977 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3978 {
3979 (void)hasFolding(curwin->w_cursor.lnum,
3980 &curwin->w_cursor.lnum, NULL);
3981 --curwin->w_cursor.lnum;
3982 }
3983 }
3984 else
3985#endif
3986 curwin->w_cursor.lnum -= cap->count1 - 1;
3987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 }
3989 else
3990 {
3991 if (cap->cmdchar == 'M')
3992 {
3993#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003994 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 used -= diff_check_fill(curwin, curwin->w_topline)
3996 - curwin->w_topfill;
3997#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003998 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
4000 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
4001 {
4002#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004003 // Count half he number of filler lines to be "below this
4004 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
4006 + n) / 2 >= half)
4007 {
4008 --n;
4009 break;
4010 }
4011#endif
4012 used += plines(curwin->w_topline + n);
4013 if (used >= half)
4014 break;
4015#ifdef FEAT_FOLDING
4016 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
4017 n = lnum - curwin->w_topline;
4018#endif
4019 }
4020 if (n > 0 && used > curwin->w_height)
4021 --n;
4022 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004023 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004026#ifdef FEAT_FOLDING
4027 if (hasAnyFolding(curwin))
4028 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004029 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004030 lnum = curwin->w_topline;
4031 while (n-- > 0 && lnum < curwin->w_botline - 1)
4032 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02004033 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004034 ++lnum;
4035 }
4036 n = lnum - curwin->w_topline;
4037 }
4038#endif
4039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 curwin->w_cursor.lnum = curwin->w_topline + n;
4041 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4042 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4043 }
4044
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004045 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02004046 if (cap->oap->op_type == OP_NOP)
4047 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 beginline(BL_SOL | BL_FIX);
4049}
4050
4051/*
4052 * Cursor right commands.
4053 */
4054 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004055nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056{
4057 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004058 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004060 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4061 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004062 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004063 if (mod_mask & MOD_MASK_CTRL)
4064 cap->arg = TRUE;
4065 nv_wordcmd(cap);
4066 return;
4067 }
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 cap->oap->motion_type = MCHAR;
4070 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004071 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004074 * In virtual edit mode, there's no such thing as "past_line", as lines
4075 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 */
4077 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004078 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
4080 for (n = cap->count1; n > 0; --n)
4081 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004082 if ((!past_line && oneright() == FAIL)
4083 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004084 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
4086 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004087 * <Space> wraps to next line if 'whichwrap' has 's'.
4088 * 'l' wraps to next line if 'whichwrap' has 'l'.
4089 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 */
4091 if ( ((cap->cmdchar == ' '
4092 && vim_strchr(p_ww, 's') != NULL)
4093 || (cap->cmdchar == 'l'
4094 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004095 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 && vim_strchr(p_ww, '>') != NULL))
4097 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4098 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004099 // When deleting we also count the NL as a character.
4100 // Set cap->oap->inclusive when last char in the line is
4101 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004102 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004104 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 cap->oap->inclusive = TRUE;
4106 else
4107 {
4108 ++curwin->w_cursor.lnum;
4109 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 curwin->w_set_curswant = TRUE;
4112 cap->oap->inclusive = FALSE;
4113 }
4114 continue;
4115 }
4116 if (cap->oap->op_type == OP_NOP)
4117 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004118 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 if (n == cap->count1)
4120 beep_flush();
4121 }
4122 else
4123 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004124 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 cap->oap->inclusive = TRUE;
4126 }
4127 break;
4128 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004129 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
4131 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (virtual_active())
4133 oneright();
4134 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004137 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 ++curwin->w_cursor.col;
4140 }
4141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143#ifdef FEAT_FOLDING
4144 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4145 && cap->oap->op_type == OP_NOP)
4146 foldOpenCursor();
4147#endif
4148}
4149
4150/*
4151 * Cursor left commands.
4152 *
4153 * Returns TRUE when operator end should not be adjusted.
4154 */
4155 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004156nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157{
4158 long n;
4159
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004160 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4161 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004162 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004163 if (mod_mask & MOD_MASK_CTRL)
4164 cap->arg = 1;
4165 nv_bck_word(cap);
4166 return;
4167 }
4168
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 cap->oap->motion_type = MCHAR;
4170 cap->oap->inclusive = FALSE;
4171 for (n = cap->count1; n > 0; --n)
4172 {
4173 if (oneleft() == FAIL)
4174 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004175 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4176 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4177 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 if ( (((cap->cmdchar == K_BS
4179 || cap->cmdchar == Ctrl_H)
4180 && vim_strchr(p_ww, 'b') != NULL)
4181 || (cap->cmdchar == 'h'
4182 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004183 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 && vim_strchr(p_ww, '<') != NULL))
4185 && curwin->w_cursor.lnum > 1)
4186 {
4187 --(curwin->w_cursor.lnum);
4188 coladvance((colnr_T)MAXCOL);
4189 curwin->w_set_curswant = TRUE;
4190
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004191 // When the NL before the first char has to be deleted we
4192 // put the cursor on the NUL after the previous line.
4193 // This is a very special case, be careful!
4194 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if ( (cap->oap->op_type == OP_DELETE
4196 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004197 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004199 char_u *cp = ml_get_cursor();
4200
4201 if (*cp != NUL)
4202 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004203 if (has_mbyte)
4204 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4205 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004206 ++curwin->w_cursor.col;
4207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 cap->retval |= CA_NO_ADJ_OP_END;
4209 }
4210 continue;
4211 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004212 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4214 beep_flush();
4215 break;
4216 }
4217 }
4218#ifdef FEAT_FOLDING
4219 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4220 && cap->oap->op_type == OP_NOP)
4221 foldOpenCursor();
4222#endif
4223}
4224
4225/*
4226 * Cursor up commands.
4227 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4228 */
4229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004230nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004232 if (mod_mask & MOD_MASK_SHIFT)
4233 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004234 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004235 cap->arg = BACKWARD;
4236 nv_page(cap);
4237 }
4238 else
4239 {
4240 cap->oap->motion_type = MLINE;
4241 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4242 clearopbeep(cap->oap);
4243 else if (cap->arg)
4244 beginline(BL_WHITE | BL_FIX);
4245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246}
4247
4248/*
4249 * Cursor down commands.
4250 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4251 */
4252 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004253nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004255 if (mod_mask & MOD_MASK_SHIFT)
4256 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004257 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004258 cap->arg = FORWARD;
4259 nv_page(cap);
4260 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004261#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004262 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004263 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4264 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004266 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 {
4268#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004269 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004270 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 cmdwin_result = CAR;
4272 else
4273#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004274#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004275 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004276 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4277 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4278 {
4279 invoke_prompt_callback();
4280 if (restart_edit == 0)
4281 restart_edit = 'a';
4282 }
4283 else
4284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
4286 cap->oap->motion_type = MLINE;
4287 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4288 clearopbeep(cap->oap);
4289 else if (cap->arg)
4290 beginline(BL_WHITE | BL_FIX);
4291 }
4292 }
4293}
4294
4295#ifdef FEAT_SEARCHPATH
4296/*
4297 * Grab the file name under the cursor and edit it.
4298 */
4299 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004300nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301{
4302 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004303 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004305 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
4307 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004308 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return;
4310 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004311 if (curbuf_locked())
4312 {
4313 clearop(cap->oap);
4314 return;
4315 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004316#ifdef FEAT_PROP_POPUP
4317 if (ERROR_IF_TERM_POPUP_WINDOW)
4318 return;
4319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004321 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 if (ptr != NULL)
4324 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004325 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004326 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004327 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004329 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004330 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004331 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004332 {
4333 curwin->w_cursor.lnum = lnum;
4334 check_cursor_lnum();
4335 beginline(BL_SOL | BL_FIX);
4336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 vim_free(ptr);
4338 }
4339 else
4340 clearop(cap->oap);
4341}
4342#endif
4343
4344/*
4345 * <End> command: to end of current line or last line.
4346 */
4347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004348nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004350 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004352 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004354 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 nv_dollar(cap);
4357}
4358
4359/*
4360 * Handle the "$" command.
4361 */
4362 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004363nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
4365 cap->oap->motion_type = MCHAR;
4366 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004367 // In virtual mode when off the edge of a line and an operator
4368 // is pending (whew!) keep the cursor where it is.
4369 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (!virtual_active() || gchar_cursor() != NUL
4371 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004372 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (cursor_down((long)(cap->count1 - 1),
4374 cap->oap->op_type == OP_NOP) == FAIL)
4375 clearopbeep(cap->oap);
4376#ifdef FEAT_FOLDING
4377 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4378 foldOpenCursor();
4379#endif
4380}
4381
4382/*
4383 * Implementation of '?' and '/' commands.
4384 * If cap->arg is TRUE don't set PC mark.
4385 */
4386 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004387nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
4389 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004390 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4393 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004394 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 cap->cmdchar = 'g';
4396 cap->nchar = '?';
4397 nv_operator(cap);
4398 return;
4399 }
4400
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004401 // When using 'incsearch' the cursor may be moved to set a different search
4402 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004403 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404
4405 if (cap->searchbuf == NULL)
4406 {
4407 clearop(oap);
4408 return;
4409 }
4410
Bram Moolenaar46539112015-02-17 15:43:57 +01004411 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004412 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004413 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414}
4415
4416/*
4417 * Handle "N" and "n" commands.
4418 * cap->arg is SEARCH_REV for "N", 0 for "n".
4419 */
4420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004421nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004423 pos_T old = curwin->w_cursor;
4424 int wrapped = FALSE;
4425 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004426
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004427 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004428 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004429 // Avoid getting stuck on the current cursor position, which can
4430 // happen when an offset is given and the cursor is on the last char
4431 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004432 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004433 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004434 cap->count1 -= 1;
4435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436}
4437
4438/*
4439 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4440 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004441 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004443 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004444normal_search(
4445 cmdarg_T *cap,
4446 int dir,
4447 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004448 int opt, // extra flags for do_search()
4449 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450{
4451 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004452 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 cap->oap->motion_type = MCHAR;
4455 cap->oap->inclusive = FALSE;
4456 cap->oap->use_reg_one = TRUE;
4457 curwin->w_set_curswant = TRUE;
4458
Bram Moolenaara80faa82020-04-12 19:37:17 +02004459 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004460 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004461 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4462 if (wrapped != NULL)
4463 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 if (i == 0)
4465 clearop(cap->oap);
4466 else
4467 {
4468 if (i == 2)
4469 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471#ifdef FEAT_FOLDING
4472 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4473 foldOpenCursor();
4474#endif
4475 }
4476
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004477 // "/$" will put the cursor after the end of the line, may need to
4478 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004480 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481}
4482
4483/*
4484 * Character search commands.
4485 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4486 * ',' and FALSE for ';'.
4487 * cap->nchar is NUL for ',' and ';' (repeat the search)
4488 */
4489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004490nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491{
4492 int t_cmd;
4493
4494 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4495 t_cmd = TRUE;
4496 else
4497 t_cmd = FALSE;
4498
4499 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4501 clearopbeep(cap->oap);
4502 else
4503 {
4504 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004505 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4507 && (t_cmd || cap->oap->op_type != OP_NOP))
4508 {
4509 colnr_T scol, ecol;
4510
4511 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4512 curwin->w_cursor.coladd = ecol - scol;
4513 }
4514 else
4515 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517#ifdef FEAT_FOLDING
4518 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4519 foldOpenCursor();
4520#endif
4521 }
4522}
4523
4524/*
4525 * "[" and "]" commands.
4526 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4527 */
4528 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004529nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004531 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004533 pos_T *pos = NULL; // init for GCC
4534 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 int flag;
4536 long n;
4537 int findc;
4538 int c;
4539
4540 cap->oap->motion_type = MCHAR;
4541 cap->oap->inclusive = FALSE;
4542 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004543 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545#ifdef FEAT_SEARCHPATH
4546 /*
4547 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4548 */
4549 if (cap->nchar == 'f')
4550 nv_gotofile(cap);
4551 else
4552#endif
4553
4554#ifdef FEAT_FIND_ID
4555 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004556 * Find the occurrence(s) of the identifier or define under cursor
4557 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 *
4559 * search list jump
4560 * fwd bwd fwd bwd fwd bwd
4561 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4562 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4563 */
4564 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004565# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004567# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 cap->nchar) != NULL)
4571 {
4572 char_u *ptr;
4573 int len;
4574
4575 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4576 clearop(cap->oap);
4577 else
4578 {
4579 find_pattern_in_path(ptr, 0, len, TRUE,
4580 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4581 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4582 cap->count1,
4583 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4584 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4585 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4586 (linenr_T)MAXLNUM);
4587 curwin->w_set_curswant = TRUE;
4588 }
4589 }
4590 else
4591#endif
4592
4593 /*
4594 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4595 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4596 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4597 * "[m" or "]m" search for prev/next start of (Java) method.
4598 * "[M" or "]M" search for prev/next end of (Java) method.
4599 */
4600 if ( (cap->cmdchar == '['
4601 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4602 || (cap->cmdchar == ']'
4603 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4604 {
4605 if (cap->nchar == '*')
4606 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 prev_pos.lnum = 0;
4608 if (cap->nchar == 'm' || cap->nchar == 'M')
4609 {
4610 if (cap->cmdchar == '[')
4611 findc = '{';
4612 else
4613 findc = '}';
4614 n = 9999;
4615 }
4616 else
4617 {
4618 findc = cap->nchar;
4619 n = cap->count1;
4620 }
4621 for ( ; n > 0; --n)
4622 {
4623 if ((pos = findmatchlimit(cap->oap, findc,
4624 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4625 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004626 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
4628 if (cap->nchar != 'm' && cap->nchar != 'M')
4629 clearopbeep(cap->oap);
4630 }
4631 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004632 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 break;
4634 }
4635 prev_pos = new_pos;
4636 curwin->w_cursor = *pos;
4637 new_pos = *pos;
4638 }
4639 curwin->w_cursor = old_pos;
4640
4641 /*
4642 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4643 * brought us to the match for "[m" and "]M" when inside a method.
4644 * Try finding the '{' or '}' we want to be at.
4645 * Also repeat for the given count.
4646 */
4647 if (cap->nchar == 'm' || cap->nchar == 'M')
4648 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004649 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 int norm = ((findc == '{') == (cap->nchar == 'm'));
4651
4652 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004653 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (prev_pos.lnum != 0)
4655 {
4656 pos = &prev_pos;
4657 curwin->w_cursor = prev_pos;
4658 if (norm)
4659 --n;
4660 }
4661 else
4662 pos = NULL;
4663 while (n > 0)
4664 {
4665 for (;;)
4666 {
4667 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4668 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004669 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (pos == NULL)
4671 clearopbeep(cap->oap);
4672 n = 0;
4673 break;
4674 }
4675 c = gchar_cursor();
4676 if (c == '{' || c == '}')
4677 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004678 // Must have found end/start of class: use it.
4679 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 if ((c == findc && norm) || (n == 1 && !norm))
4681 {
4682 new_pos = curwin->w_cursor;
4683 pos = &new_pos;
4684 n = 0;
4685 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004686 // if no match found at all, we started outside of the
4687 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 else if (new_pos.lnum == 0)
4689 {
4690 new_pos = curwin->w_cursor;
4691 pos = &new_pos;
4692 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004693 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 else if ((pos = findmatchlimit(cap->oap, findc,
4695 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4696 0)) == NULL)
4697 n = 0;
4698 else
4699 curwin->w_cursor = *pos;
4700 break;
4701 }
4702 }
4703 --n;
4704 }
4705 curwin->w_cursor = old_pos;
4706 if (pos == NULL && new_pos.lnum != 0)
4707 clearopbeep(cap->oap);
4708 }
4709 if (pos != NULL)
4710 {
4711 setpcmark();
4712 curwin->w_cursor = *pos;
4713 curwin->w_set_curswant = TRUE;
4714#ifdef FEAT_FOLDING
4715 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4716 && cap->oap->op_type == OP_NOP)
4717 foldOpenCursor();
4718#endif
4719 }
4720 }
4721
4722 /*
4723 * "[[", "[]", "]]" and "][": move to start or end of function
4724 */
4725 else if (cap->nchar == '[' || cap->nchar == ']')
4726 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004727 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 flag = '{';
4729 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004730 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
4732 curwin->w_set_curswant = TRUE;
4733 /*
4734 * Imitate strange Vi behaviour: When using "]]" with an operator
4735 * we also stop at '}'.
4736 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004737 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 (cap->oap->op_type != OP_NOP
4739 && cap->arg == FORWARD && flag == '{')))
4740 clearopbeep(cap->oap);
4741 else
4742 {
4743 if (cap->oap->op_type == OP_NOP)
4744 beginline(BL_WHITE | BL_FIX);
4745#ifdef FEAT_FOLDING
4746 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4747 foldOpenCursor();
4748#endif
4749 }
4750 }
4751
4752 /*
4753 * "[p", "[P", "]P" and "]p": put with indent adjustment
4754 */
4755 else if (cap->nchar == 'p' || cap->nchar == 'P')
4756 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004757 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
4759
4760 /*
4761 * "['", "[`", "]'" and "]`": jump to next mark
4762 */
4763 else if (cap->nchar == '\'' || cap->nchar == '`')
4764 {
4765 pos = &curwin->w_cursor;
4766 for (n = cap->count1; n > 0; --n)
4767 {
4768 prev_pos = *pos;
4769 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4770 cap->nchar == '\'');
4771 if (pos == NULL)
4772 break;
4773 }
4774 if (pos == NULL)
4775 pos = &prev_pos;
4776 nv_cursormark(cap, cap->nchar == '\'', pos);
4777 }
4778
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 /*
4780 * [ or ] followed by a middle mouse click: put selected text with
4781 * indent adjustment. Any other button just does as usual.
4782 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004783 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
4785 (void)do_mouse(cap->oap, cap->nchar,
4786 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4787 cap->count1, PUT_FIXINDENT);
4788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
4790#ifdef FEAT_FOLDING
4791 /*
4792 * "[z" and "]z": move to start or end of open fold.
4793 */
4794 else if (cap->nchar == 'z')
4795 {
4796 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4797 cap->count1) == FAIL)
4798 clearopbeep(cap->oap);
4799 }
4800#endif
4801
4802#ifdef FEAT_DIFF
4803 /*
4804 * "[c" and "]c": move to next or previous diff-change.
4805 */
4806 else if (cap->nchar == 'c')
4807 {
4808 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4809 cap->count1) == FAIL)
4810 clearopbeep(cap->oap);
4811 }
4812#endif
4813
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004814#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004815 /*
4816 * "[s", "[S", "]s" and "]S": move to next spell error.
4817 */
4818 else if (cap->nchar == 's' || cap->nchar == 'S')
4819 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004820 setpcmark();
4821 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004822 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4823 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004824 {
4825 clearopbeep(cap->oap);
4826 break;
4827 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004828 else
4829 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004830# ifdef FEAT_FOLDING
4831 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4832 foldOpenCursor();
4833# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004834 }
4835#endif
4836
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004837 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 else
4839 clearopbeep(cap->oap);
4840}
4841
4842/*
4843 * Handle Normal mode "%" command.
4844 */
4845 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004846nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847{
4848 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004849#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 linenr_T lnum = curwin->w_cursor.lnum;
4851#endif
4852
4853 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004854 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
4856 if (cap->count0 > 100)
4857 clearopbeep(cap->oap);
4858 else
4859 {
4860 cap->oap->motion_type = MLINE;
4861 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004862 // Round up, so 'normal 100%' always jumps at the line line.
4863 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4864 // overflow on 32-bits, so use a formula with less accuracy
4865 // to avoid overflows.
4866 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4868 / 100L * cap->count0;
4869 else
4870 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4871 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004872 if (curwin->w_cursor.lnum < 1)
4873 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4875 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4876 beginline(BL_SOL | BL_FIX);
4877 }
4878 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004879 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 cap->oap->motion_type = MCHAR;
4882 cap->oap->use_reg_one = TRUE;
4883 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4884 clearopbeep(cap->oap);
4885 else
4886 {
4887 setpcmark();
4888 curwin->w_cursor = *pos;
4889 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
4893 }
4894#ifdef FEAT_FOLDING
4895 if (cap->oap->op_type == OP_NOP
4896 && lnum != curwin->w_cursor.lnum
4897 && (fdo_flags & FDO_PERCENT)
4898 && KeyTyped)
4899 foldOpenCursor();
4900#endif
4901}
4902
4903/*
4904 * Handle "(" and ")" commands.
4905 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4906 */
4907 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004908nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
4910 cap->oap->motion_type = MCHAR;
4911 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004912 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004913 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 curwin->w_set_curswant = TRUE;
4915
4916 if (findsent(cap->arg, cap->count1) == FAIL)
4917 clearopbeep(cap->oap);
4918 else
4919 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004920 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004921 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923#ifdef FEAT_FOLDING
4924 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4925 foldOpenCursor();
4926#endif
4927 }
4928}
4929
4930/*
4931 * "m" command: Mark a position.
4932 */
4933 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004934nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936 if (!checkclearop(cap->oap))
4937 {
4938 if (setmark(cap->nchar) == FAIL)
4939 clearopbeep(cap->oap);
4940 }
4941}
4942
4943/*
4944 * "{" and "}" commands.
4945 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4946 */
4947 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004948nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
4950 cap->oap->motion_type = MCHAR;
4951 cap->oap->inclusive = FALSE;
4952 cap->oap->use_reg_one = TRUE;
4953 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004954 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 clearopbeep(cap->oap);
4956 else
4957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959#ifdef FEAT_FOLDING
4960 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4961 foldOpenCursor();
4962#endif
4963 }
4964}
4965
4966/*
4967 * "u" command: Undo or make lower case.
4968 */
4969 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004970nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004972 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004974 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 cap->cmdchar = 'g';
4976 cap->nchar = 'u';
4977 nv_operator(cap);
4978 }
4979 else
4980 nv_kundo(cap);
4981}
4982
4983/*
4984 * <Undo> command.
4985 */
4986 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004987nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
4989 if (!checkclearopq(cap->oap))
4990 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004991#ifdef FEAT_JOB_CHANNEL
4992 if (bt_prompt(curbuf))
4993 {
4994 clearopbeep(cap->oap);
4995 return;
4996 }
4997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 u_undo((int)cap->count1);
4999 curwin->w_set_curswant = TRUE;
5000 }
5001}
5002
5003/*
5004 * Handle the "r" command.
5005 */
5006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005007nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
5009 char_u *ptr;
5010 int had_ctrl_v;
5011 long n;
5012
5013 if (checkclearop(cap->oap))
5014 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02005015#ifdef FEAT_JOB_CHANNEL
5016 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5017 {
5018 clearopbeep(cap->oap);
5019 return;
5020 }
5021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005023 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (cap->nchar == Ctrl_V)
5025 {
5026 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01005027 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005028 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (cap->nchar > DEL)
5030 had_ctrl_v = NUL;
5031 }
5032 else
5033 had_ctrl_v = NUL;
5034
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005035 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005036 if (IS_SPECIAL(cap->nchar))
5037 {
5038 clearopbeep(cap->oap);
5039 return;
5040 }
5041
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005042 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 if (VIsual_active)
5044 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00005045 if (got_int)
5046 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01005047 if (had_ctrl_v)
5048 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005049 // Use a special (negative) number to make a difference between a
5050 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01005051 if (cap->nchar == CAR)
5052 cap->nchar = REPLACE_CR_NCHAR;
5053 else if (cap->nchar == NL)
5054 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01005055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 nv_operator(cap);
5057 return;
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005060 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (virtual_active())
5062 {
5063 if (u_save_cursor() == FAIL)
5064 return;
5065 if (gchar_cursor() == NUL)
5066 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005067 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 coladvance_force((colnr_T)(getviscol() + cap->count1));
5069 curwin->w_cursor.col -= cap->count1;
5070 }
5071 else if (gchar_cursor() == TAB)
5072 coladvance_force(getviscol());
5073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005075 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005077 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005078 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 clearopbeep(cap->oap);
5081 return;
5082 }
5083
5084 /*
5085 * Replacing with a TAB is done by edit() when it is complicated because
5086 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5087 * Other characters are done below to avoid problems with things like
5088 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5089 */
5090 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5091 {
5092 stuffnumReadbuff(cap->count1);
5093 stuffcharReadbuff('R');
5094 stuffcharReadbuff('\t');
5095 stuffcharReadbuff(ESC);
5096 return;
5097 }
5098
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005099 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (u_save_cursor() == FAIL)
5101 return;
5102
5103 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5104 {
5105 /*
5106 * Replace character(s) by a single newline.
5107 * Strange vi behaviour: Only one newline is inserted.
5108 * Delete the characters here.
5109 * Insert the newline with an insert command, takes care of
5110 * autoindent. The insert command depends on being on the last
5111 * character of a line or not.
5112 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005113 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 stuffcharReadbuff('\r');
5115 stuffcharReadbuff(ESC);
5116
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005117 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 invoke_edit(cap, TRUE, 'r', FALSE);
5119 }
5120 else
5121 {
5122 prep_redo(cap->oap->regname, cap->count1,
5123 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5124
5125 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (has_mbyte)
5127 {
5128 int old_State = State;
5129
5130 if (cap->ncharC1 != 0)
5131 AppendCharToRedobuff(cap->ncharC1);
5132 if (cap->ncharC2 != 0)
5133 AppendCharToRedobuff(cap->ncharC2);
5134
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005135 // This is slow, but it handles replacing a single-byte with a
5136 // multi-byte and the other way around. Also handles adding
5137 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 for (n = cap->count1; n > 0; --n)
5139 {
5140 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005141 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5142 {
5143 int c = ins_copychar(curwin->w_cursor.lnum
5144 + (cap->nchar == Ctrl_Y ? -1 : 1));
5145 if (c != NUL)
5146 ins_char(c);
5147 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005148 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005149 ++curwin->w_cursor.col;
5150 }
5151 else
5152 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 State = old_State;
5154 if (cap->ncharC1 != 0)
5155 ins_char(cap->ncharC1);
5156 if (cap->ncharC2 != 0)
5157 ins_char(cap->ncharC2);
5158 }
5159 }
5160 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
5162 /*
5163 * Replace the characters within one line.
5164 */
5165 for (n = cap->count1; n > 0; --n)
5166 {
5167 /*
5168 * Get ptr again, because u_save and/or showmatch() will have
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005169 * released the line. This may also happen in ins_copychar().
5170 * At the same time we let know that the line will be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005172 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5173 {
5174 int c = ins_copychar(curwin->w_cursor.lnum
5175 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005176
5177 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005178 if (c != NUL)
5179 ptr[curwin->w_cursor.col] = c;
5180 }
5181 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005182 {
5183 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005184 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 if (p_sm && msg_silent == 0)
5187 showmatch(cap->nchar);
5188 ++curwin->w_cursor.col;
5189 }
5190#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005191 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005193 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
Bram Moolenaar009b2592004-10-24 19:18:58 +00005195 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005196 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005198 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200#endif
5201
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005202 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 changed_bytes(curwin->w_cursor.lnum,
5204 (colnr_T)(curwin->w_cursor.col - cap->count1));
5205 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005206 --curwin->w_cursor.col; // cursor on the last replaced char
5207 // if the character on the left of the current cursor is a multi-byte
5208 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (has_mbyte)
5210 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 curbuf->b_op_end = curwin->w_cursor;
5212 curwin->w_set_curswant = TRUE;
5213 set_last_insert(cap->nchar);
5214 }
5215}
5216
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217/*
5218 * 'o': Exchange start and end of Visual area.
5219 * 'O': same, but in block mode exchange left and right corners.
5220 */
5221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005222v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
5224 pos_T old_cursor;
5225 colnr_T left, right;
5226
5227 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5228 {
5229 old_cursor = curwin->w_cursor;
5230 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5231 curwin->w_cursor.lnum = VIsual.lnum;
5232 coladvance(left);
5233 VIsual = curwin->w_cursor;
5234
5235 curwin->w_cursor.lnum = old_cursor.lnum;
5236 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005237 // 'selection "exclusive" and cursor at right-bottom corner: move it
5238 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5240 ++curwin->w_curswant;
5241 coladvance(curwin->w_curswant);
5242 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005244 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 {
5246 curwin->w_cursor.lnum = VIsual.lnum;
5247 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5248 ++right;
5249 coladvance(right);
5250 VIsual = curwin->w_cursor;
5251
5252 curwin->w_cursor.lnum = old_cursor.lnum;
5253 coladvance(left);
5254 curwin->w_curswant = left;
5255 }
5256 }
5257 else
5258 {
5259 old_cursor = curwin->w_cursor;
5260 curwin->w_cursor = VIsual;
5261 VIsual = old_cursor;
5262 curwin->w_set_curswant = TRUE;
5263 }
5264}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
5266/*
5267 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5268 */
5269 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005270nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005272 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 cap->cmdchar = 'c';
5275 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005276 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 VIsual_mode = 'V';
5278 nv_operator(cap);
5279 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005280 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
5282 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005283 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 else
5285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (virtual_active())
5287 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5289 }
5290 }
5291}
5292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293/*
5294 * "gr".
5295 */
5296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005297nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (VIsual_active)
5300 {
5301 cap->cmdchar = 'r';
5302 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005303 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005305 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
5307 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005308 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 else
5310 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005311 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005312 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 stuffcharReadbuff(cap->extra_char);
5314 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (virtual_active())
5316 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 invoke_edit(cap, TRUE, 'v', FALSE);
5318 }
5319 }
5320}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
5322/*
5323 * Swap case for "~" command, when it does not work like an operator.
5324 */
5325 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005326n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
5328 long n;
5329 pos_T startpos;
5330 int did_change = 0;
5331#ifdef FEAT_NETBEANS_INTG
5332 pos_T pos;
5333 char_u *ptr;
5334 int count;
5335#endif
5336
5337 if (checkclearopq(cap->oap))
5338 return;
5339
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005340 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
5342 clearopbeep(cap->oap);
5343 return;
5344 }
5345
5346 prep_redo_cmd(cap);
5347
5348 if (u_save_cursor() == FAIL)
5349 return;
5350
5351 startpos = curwin->w_cursor;
5352#ifdef FEAT_NETBEANS_INTG
5353 pos = startpos;
5354#endif
5355 for (n = cap->count1; n > 0; --n)
5356 {
5357 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5358 inc_cursor();
5359 if (gchar_cursor() == NUL)
5360 {
5361 if (vim_strchr(p_ww, '~') != NULL
5362 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5363 {
5364#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005365 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
5367 if (did_change)
5368 {
5369 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005370 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005371 netbeans_removed(curbuf, pos.lnum, pos.col,
5372 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005374 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
5376 pos.col = 0;
5377 pos.lnum++;
5378 }
5379#endif
5380 ++curwin->w_cursor.lnum;
5381 curwin->w_cursor.col = 0;
5382 if (n > 1)
5383 {
5384 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5385 break;
5386 u_clearline();
5387 }
5388 }
5389 else
5390 break;
5391 }
5392 }
5393#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005394 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 ptr = ml_get(pos.lnum);
5397 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005398 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5399 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
5401#endif
5402
5403
5404 check_cursor();
5405 curwin->w_set_curswant = TRUE;
5406 if (did_change)
5407 {
5408 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5409 0L);
5410 curbuf->b_op_start = startpos;
5411 curbuf->b_op_end = curwin->w_cursor;
5412 if (curbuf->b_op_end.col > 0)
5413 --curbuf->b_op_end.col;
5414 }
5415}
5416
5417/*
5418 * Move cursor to mark.
5419 */
5420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005421nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423 if (check_mark(pos) == FAIL)
5424 clearop(cap->oap);
5425 else
5426 {
5427 if (cap->cmdchar == '\''
5428 || cap->cmdchar == '`'
5429 || cap->cmdchar == '['
5430 || cap->cmdchar == ']')
5431 setpcmark();
5432 curwin->w_cursor = *pos;
5433 if (flag)
5434 beginline(BL_WHITE | BL_FIX);
5435 else
5436 check_cursor();
5437 }
5438 cap->oap->motion_type = flag ? MLINE : MCHAR;
5439 if (cap->cmdchar == '`')
5440 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005441 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 curwin->w_set_curswant = TRUE;
5443}
5444
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445/*
5446 * Handle commands that are operators in Visual mode.
5447 */
5448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005449v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
5451 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5452
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005453 // Uppercase means linewise, except in block mode, then "D" deletes till
5454 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 if (isupper(cap->cmdchar))
5456 {
5457 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005458 {
5459 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5463 curwin->w_curswant = MAXCOL;
5464 }
5465 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5466 nv_operator(cap);
5467}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
5469/*
5470 * "s" and "S" commands.
5471 */
5472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005473nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005475#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005476 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005477 if (term_swap_diff() == OK)
5478 return;
5479#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005480#ifdef FEAT_JOB_CHANNEL
5481 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5482 {
5483 clearopbeep(cap->oap);
5484 return;
5485 }
5486#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005487 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
5489 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005490 {
5491 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 cap->cmdchar = 'c';
5495 nv_operator(cap);
5496 }
5497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 nv_optrans(cap);
5499}
5500
5501/*
5502 * Abbreviated commands.
5503 */
5504 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005505nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005508 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005510 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 if (VIsual_active)
5512 v_visop(cap);
5513 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 nv_optrans(cap);
5515}
5516
5517/*
5518 * Translate a command into another command.
5519 */
5520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005521nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5524 (char_u *)"d$", (char_u *)"c$",
5525 (char_u *)"cl", (char_u *)"cc",
5526 (char_u *)"yy", (char_u *)":s\r"};
5527 static char_u *str = (char_u *)"xXDCsSY&";
5528
5529 if (!checkclearopq(cap->oap))
5530 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005531 // In Vi "2D" doesn't delete the next line. Can't translate it
5532 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005533 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5534 {
5535 cap->oap->start = curwin->w_cursor;
5536 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005537#ifdef FEAT_EVAL
5538 set_op_var(OP_DELETE);
5539#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005540 cap->count1 = 1;
5541 nv_dollar(cap);
5542 finish_op = TRUE;
5543 ResetRedobuff();
5544 AppendCharToRedobuff('D');
5545 }
5546 else
5547 {
5548 if (cap->count0)
5549 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005550 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553 cap->opcount = 0;
5554}
5555
5556/*
5557 * "'" and "`" commands. Also for "g'" and "g`".
5558 * cap->arg is TRUE for "'" and "g'".
5559 */
5560 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005561nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563 pos_T *pos;
5564 int c;
5565#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005566 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005567 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568#endif
5569
5570 if (cap->cmdchar == 'g')
5571 c = cap->extra_char;
5572 else
5573 c = cap->nchar;
5574 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005575 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
5577 if (cap->arg)
5578 {
5579 check_cursor_lnum();
5580 beginline(BL_WHITE | BL_FIX);
5581 }
5582 else
5583 check_cursor();
5584 }
5585 else
5586 nv_cursormark(cap, cap->arg, pos);
5587
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005588 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 if (!virtual_active())
5590 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005591 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592#ifdef FEAT_FOLDING
5593 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005594 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005595 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 && (fdo_flags & FDO_MARK)
5597 && old_KeyTyped)
5598 foldOpenCursor();
5599#endif
5600}
5601
5602/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005603 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
5605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005606nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005609#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005611 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 if (!checkclearopq(cap->oap))
5615 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005616 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5617 {
5618 if (goto_tabpage_lastused() == FAIL)
5619 clearopbeep(cap->oap);
5620 return;
5621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 if (cap->cmdchar == 'g')
5623 pos = movechangelist((int)cap->count1);
5624 else
5625 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005626 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
5628 curwin->w_set_curswant = TRUE;
5629 check_cursor();
5630 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005631 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 nv_cursormark(cap, FALSE, pos);
5633 else if (cap->cmdchar == 'g')
5634 {
5635 if (curbuf->b_changelistlen == 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005636 emsg(_(e_changelist_is_empty));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 else if (cap->count1 < 0)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005638 emsg(_(e_at_start_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00005640 emsg(_(e_at_end_of_changelist));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642 else
5643 clearopbeep(cap->oap);
5644# ifdef FEAT_FOLDING
5645 if (cap->oap->op_type == OP_NOP
5646 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5647 && (fdo_flags & FDO_MARK)
5648 && old_KeyTyped)
5649 foldOpenCursor();
5650# endif
5651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652}
5653
5654/*
5655 * Handle '"' command.
5656 */
5657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005658nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
5660 if (checkclearop(cap->oap))
5661 return;
5662#ifdef FEAT_EVAL
5663 if (cap->nchar == '=')
5664 cap->nchar = get_expr_register();
5665#endif
5666 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5667 {
5668 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005669 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670#ifdef FEAT_EVAL
5671 set_reg_var(cap->oap->regname);
5672#endif
5673 }
5674 else
5675 clearopbeep(cap->oap);
5676}
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678/*
5679 * Handle "v", "V" and "CTRL-V" commands.
5680 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5681 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005682 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 */
5684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005685nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005687 if (cap->cmdchar == Ctrl_Q)
5688 cap->cmdchar = Ctrl_V;
5689
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005690 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5691 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (cap->oap->op_type != OP_NOP)
5693 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005694 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005695 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 return;
5697 }
5698
5699 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005700 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005702 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005704 else // toggle char/block mode
5705 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 VIsual_mode = cap->cmdchar;
5707 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005708 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005710 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005712 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005715 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005717 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 VIsual = curwin->w_cursor;
5719
5720 VIsual_active = TRUE;
5721 VIsual_reselect = TRUE;
5722 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005723 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005726 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005727 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 /*
5729 * For V and ^V, we multiply the number of lines even if there
5730 * was only one -- webb
5731 */
5732 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5733 {
5734 curwin->w_cursor.lnum +=
5735 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005736 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738 VIsual_mode = resel_VIsual_mode;
5739 if (VIsual_mode == 'v')
5740 {
5741 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005742 {
5743 validate_virtcol();
5744 curwin->w_curswant = curwin->w_virtcol
5745 + resel_VIsual_vcol * cap->count0 - 1;
5746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005748 curwin->w_curswant = resel_VIsual_vcol;
5749 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005751 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 curwin->w_curswant = MAXCOL;
5754 coladvance((colnr_T)MAXCOL);
5755 }
5756 else if (VIsual_mode == Ctrl_V)
5757 {
5758 validate_virtcol();
5759 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005760 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 coladvance(curwin->w_curswant);
5762 }
5763 else
5764 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005765 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 else
5768 {
5769 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005770 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 may_start_select('c');
5772 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005773 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005774 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005775 if (cap->count0 > 0 && --cap->count1 > 0)
5776 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005777 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005778 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5779 nv_right(cap);
5780 else if (VIsual_mode == 'V')
5781 nv_down(cap);
5782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
5784 }
5785}
5786
5787/*
5788 * Start selection for Shift-movement keys.
5789 */
5790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005791start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005793 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 may_start_select('k');
5795 n_start_visual_mode('v');
5796}
5797
5798/*
5799 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5800 */
5801 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005802may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
5804 VIsual_select = (stuff_empty() && typebuf_typed()
5805 && (vim_strchr(p_slm, c) != NULL));
5806}
5807
5808/*
5809 * Start Visual mode "c".
5810 * Should set VIsual_select before calling this.
5811 */
5812 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005813n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005815#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005816 int cursor_line_was_concealed = curwin->w_p_cole > 0
5817 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005818#endif
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 VIsual_mode = c;
5821 VIsual_active = TRUE;
5822 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005823
5824 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005825 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005826 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005827 {
5828 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 VIsual = curwin->w_cursor;
5832
5833#ifdef FEAT_FOLDING
5834 foldAdjustVisual();
5835#endif
5836
Bram Moolenaara0620062021-11-17 16:52:40 +00005837 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005839#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005840 // Check if redraw is needed after changing the state.
5841 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005842#endif
5843
Bram Moolenaar09df3122006-01-23 22:23:09 +00005844 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005845 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005847 // Make sure the clipboard gets updated. Needed because start and
5848 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 clip_star.vmode = NUL;
5850#endif
5851
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005852 // Only need to redraw this line, unless still need to redraw an old
5853 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 if (curwin->w_redr_type < INVERTED)
5855 {
5856 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5857 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5858 }
5859}
5860
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
5862/*
5863 * CTRL-W: Window commands
5864 */
5865 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005866nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005868 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005869 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005870 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005871 cap->cmdchar = ':';
5872 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005873 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005874 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005875 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005876 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877}
5878
5879/*
5880 * CTRL-Z: Suspend
5881 */
5882 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005883nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884{
5885 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005887 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005888 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889}
5890
5891/*
5892 * Commands starting with "g".
5893 */
5894 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005895nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 int i;
5900 int flag = FALSE;
5901
5902 switch (cap->nchar)
5903 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005904 case Ctrl_A:
5905 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#ifdef MEM_PROFILE
5907 /*
5908 * "g^A": dump log of used memory.
5909 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005910 if (!VIsual_active && cap->nchar == Ctrl_A)
5911 vim_mem_profile_dump();
5912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005914 /*
5915 * "g^A/g^X": sequentially increment visually selected region
5916 */
5917 if (VIsual_active)
5918 {
5919 cap->arg = TRUE;
5920 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005921 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005922 nv_addsub(cap);
5923 }
5924 else
5925 clearopbeep(oap);
5926 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 /*
5929 * "gR": Enter virtual replace mode.
5930 */
5931 case 'R':
5932 cap->arg = TRUE;
5933 nv_Replace(cap);
5934 break;
5935
5936 case 'r':
5937 nv_vreplace(cap);
5938 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
5940 case '&':
5941 do_cmdline_cmd((char_u *)"%s//~/&");
5942 break;
5943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 /*
5945 * "gv": Reselect the previous Visual area. If Visual already active,
5946 * exchange previous and current Visual area.
5947 */
5948 case 'v':
5949 if (checkclearop(oap))
5950 break;
5951
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005952 if ( curbuf->b_visual.vi_start.lnum == 0
5953 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5954 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 beep_flush();
5956 else
5957 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005958 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 if (VIsual_active)
5960 {
5961 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005962 VIsual_mode = curbuf->b_visual.vi_mode;
5963 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964# ifdef FEAT_EVAL
5965 curbuf->b_visual_mode_eval = i;
5966# endif
5967 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005968 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5969 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005971 tpos = curbuf->b_visual.vi_end;
5972 curbuf->b_visual.vi_end = curwin->w_cursor;
5973 curwin->w_cursor = curbuf->b_visual.vi_start;
5974 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 }
5976 else
5977 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005978 VIsual_mode = curbuf->b_visual.vi_mode;
5979 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5980 tpos = curbuf->b_visual.vi_end;
5981 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
5983
5984 VIsual_active = TRUE;
5985 VIsual_reselect = TRUE;
5986
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005987 // Set Visual to the start and w_cursor to the end of the Visual
5988 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 check_cursor();
5990 VIsual = curwin->w_cursor;
5991 curwin->w_cursor = tpos;
5992 check_cursor();
5993 update_topline();
5994 /*
5995 * When called from normal "g" command: start Select mode when
5996 * 'selectmode' contains "cmd". When called for K_SELECT, always
5997 * start Select mode.
5998 */
5999 if (cap->arg)
6000 VIsual_select = TRUE;
6001 else
6002 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006005 // Make sure the clipboard gets updated. Needed because start and
6006 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 clip_star.vmode = NUL;
6008#endif
6009 redraw_curbuf_later(INVERTED);
6010 showmode();
6011 }
6012 break;
6013 /*
6014 * "gV": Don't reselect the previous Visual area after a Select mode
6015 * mapping of menu.
6016 */
6017 case 'V':
6018 VIsual_reselect = FALSE;
6019 break;
6020
6021 /*
6022 * "gh": start Select mode.
6023 * "gH": start Select line mode.
6024 * "g^H": start Select block mode.
6025 */
6026 case K_BS:
6027 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006028 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 case 'h':
6030 case 'H':
6031 case Ctrl_H:
6032# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006033 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if (cap->nchar == Ctrl_H)
6035 cap->cmdchar = Ctrl_V;
6036 else
6037# endif
6038 cap->cmdchar = cap->nchar + ('v' - 'h');
6039 cap->arg = TRUE;
6040 nv_visual(cap);
6041 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006042
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006043 // "gn", "gN" visually select next/previous search match
6044 // "gn" selects next match
6045 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006046 case 'N':
6047 case 'n':
6048 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006049 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006050 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051
6052 /*
6053 * "gj" and "gk" two new funny movement keys -- up and down
6054 * movement based on *screen* line rather than *file* line.
6055 */
6056 case 'j':
6057 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006058 // with 'nowrap' it works just like the normal "j" command.
6059 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 {
6061 oap->motion_type = MLINE;
6062 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6063 }
6064 else
6065 i = nv_screengo(oap, FORWARD, cap->count1);
6066 if (i == FAIL)
6067 clearopbeep(oap);
6068 break;
6069
6070 case 'k':
6071 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006072 // with 'nowrap' it works just like the normal "k" command.
6073 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 {
6075 oap->motion_type = MLINE;
6076 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6077 }
6078 else
6079 i = nv_screengo(oap, BACKWARD, cap->count1);
6080 if (i == FAIL)
6081 clearopbeep(oap);
6082 break;
6083
6084 /*
6085 * "gJ": join two lines without inserting a space.
6086 */
6087 case 'J':
6088 nv_join(cap);
6089 break;
6090
6091 /*
6092 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6093 * "gm": middle of "g0" and "g$".
6094 */
6095 case '^':
6096 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006097 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
6099 case '0':
6100 case 'm':
6101 case K_HOME:
6102 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 oap->motion_type = MCHAR;
6104 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006105 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006107 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 int width2 = width1 + curwin_col_off2();
6109
6110 validate_virtcol();
6111 i = 0;
6112 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6113 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6114 }
6115 else
6116 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006117 // Go to the middle of the screen line. When 'number' or
6118 // 'relativenumber' is on and lines are wrapping the middle can be more
6119 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006121 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 + ((curwin->w_p_wrap && i > 0)
6123 ? curwin_col_off2() : 0)) / 2;
6124 coladvance((colnr_T)i);
6125 if (flag)
6126 {
6127 do
6128 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006129 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006130 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 }
6132 curwin->w_set_curswant = TRUE;
6133 break;
6134
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006135 case 'M':
6136 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006137 oap->motion_type = MCHAR;
6138 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006139 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006140 if (cap->count0 > 0 && cap->count0 <= 100)
6141 coladvance((colnr_T)(i * cap->count0 / 100));
6142 else
6143 coladvance((colnr_T)(i / 2));
6144 curwin->w_set_curswant = TRUE;
6145 }
6146 break;
6147
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006149 // "g_": to the last non-blank character in the line or <count> lines
6150 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 cap->oap->motion_type = MCHAR;
6152 cap->oap->inclusive = TRUE;
6153 curwin->w_curswant = MAXCOL;
6154 if (cursor_down((long)(cap->count1 - 1),
6155 cap->oap->op_type == OP_NOP) == FAIL)
6156 clearopbeep(cap->oap);
6157 else
6158 {
6159 char_u *ptr = ml_get_curline();
6160
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006161 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6163 --curwin->w_cursor.col;
6164
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006165 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006167 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 --curwin->w_cursor.col;
6169 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006170 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 }
6172 break;
6173
6174 case '$':
6175 case K_END:
6176 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 {
6178 int col_off = curwin_col_off();
6179
6180 oap->motion_type = MCHAR;
6181 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006182 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006184 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 if (cap->count1 == 1)
6186 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006187 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 int width2 = width1 + curwin_col_off2();
6189
6190 validate_virtcol();
6191 i = width1 - 1;
6192 if (curwin->w_virtcol >= (colnr_T)width1)
6193 i += ((curwin->w_virtcol - width1) / width2 + 1)
6194 * width2;
6195 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006197 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006198 validate_virtcol();
6199 curwin->w_curswant = curwin->w_virtcol;
6200 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6202 {
6203 /*
6204 * Check for landing on a character that got split at
6205 * the end of the line. We do not want to advance to
6206 * the next screen line.
6207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 if (curwin->w_virtcol > (colnr_T)i)
6209 --curwin->w_cursor.col;
6210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 }
6212 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6213 clearopbeep(oap);
6214 }
6215 else
6216 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006217 if (cap->count1 > 1)
6218 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006219 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006220
Bram Moolenaar02631462017-09-22 15:20:32 +02006221 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006223
Bram Moolenaar74ede802021-05-29 19:18:01 +02006224 // if the character doesn't fit move one back
6225 if (curwin->w_cursor.col > 0
6226 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6227 {
6228 colnr_T vcol;
6229
6230 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6231 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6232 --curwin->w_cursor.col;
6233 }
6234
Bram Moolenaard5c82342019-07-27 18:44:57 +02006235 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006236 validate_virtcol();
6237 curwin->w_curswant = curwin->w_virtcol;
6238 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
6240 }
6241 break;
6242
6243 /*
6244 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6245 */
6246 case '*':
6247 case '#':
6248#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006249 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006251 case Ctrl_RSB: // :tag or :tselect for current identifier
6252 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 nv_ident(cap);
6254 break;
6255
6256 /*
6257 * ge and gE: go back to end of word
6258 */
6259 case 'e':
6260 case 'E':
6261 oap->motion_type = MCHAR;
6262 curwin->w_set_curswant = TRUE;
6263 oap->inclusive = TRUE;
6264 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6265 clearopbeep(oap);
6266 break;
6267
6268 /*
6269 * "g CTRL-G": display info about cursor position
6270 */
6271 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006272 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 break;
6274
6275 /*
6276 * "gi": start Insert at the last position.
6277 */
6278 case 'i':
6279 if (curbuf->b_last_insert.lnum != 0)
6280 {
6281 curwin->w_cursor = curbuf->b_last_insert;
6282 check_cursor_lnum();
6283 i = (int)STRLEN(ml_get_curline());
6284 if (curwin->w_cursor.col > (colnr_T)i)
6285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 if (virtual_active())
6287 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 curwin->w_cursor.col = i;
6289 }
6290 }
6291 cap->cmdchar = 'i';
6292 nv_edit(cap);
6293 break;
6294
6295 /*
6296 * "gI": Start insert in column 1.
6297 */
6298 case 'I':
6299 beginline(0);
6300 if (!checkclearopq(oap))
6301 invoke_edit(cap, FALSE, 'g', FALSE);
6302 break;
6303
6304#ifdef FEAT_SEARCHPATH
6305 /*
6306 * "gf": goto file, edit file under cursor
6307 * "]f" and "[f": can also be used.
6308 */
6309 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006310 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 nv_gotofile(cap);
6312 break;
6313#endif
6314
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006315 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 case '\'':
6317 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006318 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 case '`':
6320 nv_gomark(cap);
6321 break;
6322
6323 /*
6324 * "gs": Goto sleep.
6325 */
6326 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006327 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 break;
6329
6330 /*
6331 * "ga": Display the ascii value of the character under the
6332 * cursor. It is displayed in decimal, hex, and octal. -- webb
6333 */
6334 case 'a':
6335 do_ascii(NULL);
6336 break;
6337
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 /*
6339 * "g8": Display the bytes used for the UTF-8 character under the
6340 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006341 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 */
6343 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006344 if (cap->count0 == 8)
6345 utf_find_illegal();
6346 else
6347 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006350 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006351 case '<':
6352 show_sb_text();
6353 break;
6354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 /*
6356 * "gg": Goto the first line in file. With a count it goes to
6357 * that line number like for "G". -- webb
6358 */
6359 case 'g':
6360 cap->arg = FALSE;
6361 nv_goto(cap);
6362 break;
6363
6364 /*
6365 * Two-character operators:
6366 * "gq" Format text
6367 * "gw" Format text and keep cursor position
6368 * "g~" Toggle the case of the text.
6369 * "gu" Change text to lower case.
6370 * "gU" Change text to upper case.
6371 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006372 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 */
6374 case 'q':
6375 case 'w':
6376 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006377 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 case '~':
6379 case 'u':
6380 case 'U':
6381 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006382 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 nv_operator(cap);
6384 break;
6385
6386 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006387 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 * current function
6389 * "gD": idem, but in the current file.
6390 */
6391 case 'd':
6392 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006393 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 break;
6395
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 /*
6397 * g<*Mouse> : <C-*mouse>
6398 */
6399 case K_MIDDLEMOUSE:
6400 case K_MIDDLEDRAG:
6401 case K_MIDDLERELEASE:
6402 case K_LEFTMOUSE:
6403 case K_LEFTDRAG:
6404 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006405 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 case K_RIGHTMOUSE:
6407 case K_RIGHTDRAG:
6408 case K_RIGHTRELEASE:
6409 case K_X1MOUSE:
6410 case K_X1DRAG:
6411 case K_X1RELEASE:
6412 case K_X2MOUSE:
6413 case K_X2DRAG:
6414 case K_X2RELEASE:
6415 mod_mask = MOD_MASK_CTRL;
6416 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
6419 case K_IGNORE:
6420 break;
6421
6422 /*
6423 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6424 */
6425 case 'p':
6426 case 'P':
6427 nv_put(cap);
6428 break;
6429
6430#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006431 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 case 'o':
6433 goto_byte(cap->count0);
6434 break;
6435#endif
6436
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006437 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006439 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 {
6441 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006442 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 break;
6444 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (!checkclearopq(oap))
6447 do_exmode(TRUE);
6448 break;
6449
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 case ',':
6451 nv_pcmark(cap);
6452 break;
6453
6454 case ';':
6455 cap->count1 = -cap->count1;
6456 nv_pcmark(cap);
6457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006459 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006460 if (!checkclearop(oap))
6461 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006462 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006463 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006464 if (!checkclearop(oap))
6465 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006466 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006467
Bram Moolenaar62a23252020-08-09 14:04:42 +02006468 case TAB:
6469 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6470 clearopbeep(oap);
6471 break;
6472
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006473 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006474 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006475 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006476 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006477 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006478 break;
6479
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 default:
6481 clearopbeep(oap);
6482 break;
6483 }
6484}
6485
6486/*
6487 * Handle "o" and "O" commands.
6488 */
6489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006490n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006492#ifdef FEAT_CONCEAL
6493 linenr_T oldline = curwin->w_cursor.lnum;
6494#endif
6495
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (!checkclearopq(cap->oap))
6497 {
6498#ifdef FEAT_FOLDING
6499 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006500 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 (void)hasFolding(curwin->w_cursor.lnum,
6502 &curwin->w_cursor.lnum, NULL);
6503 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006504 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 (void)hasFolding(curwin->w_cursor.lnum,
6506 NULL, &curwin->w_cursor.lnum);
6507#endif
6508 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6509 (cap->cmdchar == 'O' ? 1 : 0)),
6510 (linenr_T)(curwin->w_cursor.lnum +
6511 (cap->cmdchar == 'o' ? 1 : 0))
6512 ) == OK
6513 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006514 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006515 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006517#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006518 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006519 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006520#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006521#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006522 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006523 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006524 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006525#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006526 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006527 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6528 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6530 }
6531 }
6532}
6533
6534/*
6535 * "." command: redo last change.
6536 */
6537 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006538nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 if (!checkclearopq(cap->oap))
6541 {
6542 /*
6543 * If "restart_edit" is TRUE, the last but one command is repeated
6544 * instead of the last command (inserting text). This is used for
6545 * CTRL-O <.> in insert mode.
6546 */
6547 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6548 clearopbeep(cap->oap);
6549 }
6550}
6551
6552/*
6553 * CTRL-R: undo undo
6554 */
6555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006556nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558 if (!checkclearopq(cap->oap))
6559 {
6560 u_redo((int)cap->count1);
6561 curwin->w_set_curswant = TRUE;
6562 }
6563}
6564
6565/*
6566 * Handle "U" command.
6567 */
6568 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006569nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006571 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006572 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006574 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 cap->cmdchar = 'g';
6576 cap->nchar = 'U';
6577 nv_operator(cap);
6578 }
6579 else if (!checkclearopq(cap->oap))
6580 {
6581 u_undoline();
6582 curwin->w_set_curswant = TRUE;
6583 }
6584}
6585
6586/*
6587 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6588 * single character.
6589 */
6590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006591nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006593 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006594 {
6595#ifdef FEAT_JOB_CHANNEL
6596 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6597 {
6598 clearopbeep(cap->oap);
6599 return;
6600 }
6601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 else
6605 nv_operator(cap);
6606}
6607
6608/*
6609 * Handle an operator command.
6610 * The actual work is done by do_pending_operator().
6611 */
6612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006613nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614{
6615 int op_type;
6616
6617 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006618#ifdef FEAT_JOB_CHANNEL
6619 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6620 {
6621 clearopbeep(cap->oap);
6622 return;
6623 }
6624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006626 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 nv_lineop(cap);
6628 else if (!checkclearop(cap->oap))
6629 {
6630 cap->oap->start = curwin->w_cursor;
6631 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006632#ifdef FEAT_EVAL
6633 set_op_var(op_type);
6634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 }
6636}
6637
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006638#ifdef FEAT_EVAL
6639/*
6640 * Set v:operator to the characters for "optype".
6641 */
6642 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006643set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006644{
6645 char_u opchars[3];
6646
6647 if (optype == OP_NOP)
6648 set_vim_var_string(VV_OP, NULL, 0);
6649 else
6650 {
6651 opchars[0] = get_op_char(optype);
6652 opchars[1] = get_extra_op_char(optype);
6653 opchars[2] = NUL;
6654 set_vim_var_string(VV_OP, opchars, -1);
6655 }
6656}
6657#endif
6658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659/*
6660 * Handle linewise operator "dd", "yy", etc.
6661 *
6662 * "_" is is a strange motion command that helps make operators more logical.
6663 * It is actually implemented, but not documented in the real Vi. This motion
6664 * command actually refers to "the current line". Commands like "dd" and "yy"
6665 * are really an alternate form of "d_" and "y_". It does accept a count, so
6666 * "d3_" works to delete 3 lines.
6667 */
6668 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006669nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 cap->oap->motion_type = MLINE;
6672 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6673 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006674 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006675 && cap->oap->motion_force != 'v'
6676 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 || cap->oap->op_type == OP_LSHIFT
6678 || cap->oap->op_type == OP_RSHIFT)
6679 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006680 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 beginline(BL_WHITE | BL_FIX);
6682}
6683
6684/*
6685 * <Home> command.
6686 */
6687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006688nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006690 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006691 if (mod_mask & MOD_MASK_CTRL)
6692 nv_goto(cap);
6693 else
6694 {
6695 cap->count0 = 1;
6696 nv_pipe(cap);
6697 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006698 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6699 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700}
6701
6702/*
6703 * "|" command.
6704 */
6705 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006706nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707{
6708 cap->oap->motion_type = MCHAR;
6709 cap->oap->inclusive = FALSE;
6710 beginline(0);
6711 if (cap->count0 > 0)
6712 {
6713 coladvance((colnr_T)(cap->count0 - 1));
6714 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6715 }
6716 else
6717 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006718 // keep curswant at the column where we wanted to go, not where
6719 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 curwin->w_set_curswant = FALSE;
6721}
6722
6723/*
6724 * Handle back-word command "b" and "B".
6725 * cap->arg is 1 for "B"
6726 */
6727 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006728nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 cap->oap->motion_type = MCHAR;
6731 cap->oap->inclusive = FALSE;
6732 curwin->w_set_curswant = TRUE;
6733 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6734 clearopbeep(cap->oap);
6735#ifdef FEAT_FOLDING
6736 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6737 foldOpenCursor();
6738#endif
6739}
6740
6741/*
6742 * Handle word motion commands "e", "E", "w" and "W".
6743 * cap->arg is TRUE for "E" and "W".
6744 */
6745 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006746nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
6748 int n;
6749 int word_end;
6750 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006751 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
6753 /*
6754 * Set inclusive for the "E" and "e" command.
6755 */
6756 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6757 word_end = TRUE;
6758 else
6759 word_end = FALSE;
6760 cap->oap->inclusive = word_end;
6761
6762 /*
6763 * "cw" and "cW" are a special case.
6764 */
6765 if (!word_end && cap->oap->op_type == OP_CHANGE)
6766 {
6767 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006768 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006770 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
6772 /*
6773 * Reproduce a funny Vi behaviour: "cw" on a blank only
6774 * changes one character, not all blanks until the start of
6775 * the next word. Only do this when the 'w' flag is included
6776 * in 'cpoptions'.
6777 */
6778 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6779 {
6780 cap->oap->inclusive = TRUE;
6781 cap->oap->motion_type = MCHAR;
6782 return;
6783 }
6784 }
6785 else
6786 {
6787 /*
6788 * This is a little strange. To match what the real Vi does,
6789 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6790 * that we are not on a space or a TAB. This seems impolite
6791 * at first, but it's really more what we mean when we say
6792 * 'cw'.
6793 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006794 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 * will change only one character! This is done by setting
6796 * flag.
6797 */
6798 cap->oap->inclusive = TRUE;
6799 word_end = TRUE;
6800 flag = TRUE;
6801 }
6802 }
6803 }
6804
6805 cap->oap->motion_type = MCHAR;
6806 curwin->w_set_curswant = TRUE;
6807 if (word_end)
6808 n = end_word(cap->count1, cap->arg, flag, FALSE);
6809 else
6810 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6811
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006812 // Don't leave the cursor on the NUL past the end of line. Unless we
6813 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006814 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006815 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
6817 if (n == FAIL && cap->oap->op_type == OP_NOP)
6818 clearopbeep(cap->oap);
6819 else
6820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822#ifdef FEAT_FOLDING
6823 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6824 foldOpenCursor();
6825#endif
6826 }
6827}
6828
6829/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006830 * Used after a movement command: If the cursor ends up on the NUL after the
6831 * end of the line, may move it back to the last character and make the motion
6832 * inclusive.
6833 */
6834 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006835adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006836{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006837 // The cursor cannot remain on the NUL when:
6838 // - the column is > 0
6839 // - not in Visual mode or 'selection' is "o"
6840 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006841 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006842 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006843 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006844 {
6845 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006846 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006847 if (has_mbyte)
6848 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006849 oap->inclusive = TRUE;
6850 }
6851}
6852
6853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 * "0" and "^" commands.
6855 * cap->arg is the argument for beginline().
6856 */
6857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006858nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859{
6860 cap->oap->motion_type = MCHAR;
6861 cap->oap->inclusive = FALSE;
6862 beginline(cap->arg);
6863#ifdef FEAT_FOLDING
6864 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6865 foldOpenCursor();
6866#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006867 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6868 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869}
6870
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871/*
6872 * In exclusive Visual mode, may include the last character.
6873 */
6874 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006875adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876{
6877 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006878 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 if (has_mbyte)
6881 inc_cursor();
6882 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 ++curwin->w_cursor.col;
6884 cap->oap->inclusive = FALSE;
6885 }
6886}
6887
6888/*
6889 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6890 * Should check VIsual_mode before calling this.
6891 * Returns TRUE when backed up to the previous line.
6892 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006893 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006894unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
6896 pos_T *pp;
6897
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006898 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006900 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 pp = &curwin->w_cursor;
6902 else
6903 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (pp->coladd > 0)
6905 --pp->coladd;
6906 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (pp->col > 0)
6908 {
6909 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006910 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 }
6912 else if (pp->lnum > 1)
6913 {
6914 --pp->lnum;
6915 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6916 return TRUE;
6917 }
6918 }
6919 return FALSE;
6920}
6921
6922/*
6923 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6924 */
6925 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006926nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927{
6928 if (VIsual_active)
6929 VIsual_select = TRUE;
6930 else if (VIsual_reselect)
6931 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006932 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 cap->arg = TRUE;
6934 nv_g_cmd(cap);
6935 }
6936}
6937
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
6939/*
6940 * "G", "gg", CTRL-END, CTRL-HOME.
6941 * cap->arg is TRUE for "G".
6942 */
6943 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006944nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945{
6946 linenr_T lnum;
6947
6948 if (cap->arg)
6949 lnum = curbuf->b_ml.ml_line_count;
6950 else
6951 lnum = 1L;
6952 cap->oap->motion_type = MLINE;
6953 setpcmark();
6954
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006955 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (cap->count0 != 0)
6957 lnum = cap->count0;
6958 if (lnum < 1L)
6959 lnum = 1L;
6960 else if (lnum > curbuf->b_ml.ml_line_count)
6961 lnum = curbuf->b_ml.ml_line_count;
6962 curwin->w_cursor.lnum = lnum;
6963 beginline(BL_SOL | BL_FIX);
6964#ifdef FEAT_FOLDING
6965 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6966 foldOpenCursor();
6967#endif
6968}
6969
6970/*
6971 * CTRL-\ in Normal mode.
6972 */
6973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006974nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6977 {
6978 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006979 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006980 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 restart_edit = 0;
6982#ifdef FEAT_CMDWIN
6983 if (cmdwin_type != 0)
6984 cmdwin_result = Ctrl_C;
6985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (VIsual_active)
6987 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006988 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 redraw_curbuf_later(INVERTED);
6990 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006991 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (cap->nchar == Ctrl_G && p_im)
6993 restart_edit = 'a';
6994 }
6995 else
6996 clearopbeep(cap->oap);
6997}
6998
6999/*
7000 * ESC in Normal mode: beep, but don't flush buffers.
7001 * Don't even beep if we are canceling a command.
7002 */
7003 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007004nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006 int no_reason;
7007
7008 no_reason = (cap->oap->op_type == OP_NOP
7009 && cap->opcount == 0
7010 && cap->count0 == 0
7011 && cap->oap->regname == 0
7012 && !p_im);
7013
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007014 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {
7016 if (restart_edit == 0
7017#ifdef FEAT_CMDWIN
7018 && cmdwin_type == 0
7019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007022 {
7023 if (anyBufIsChanged())
7024 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
7025 else
7026 msg(_("Type :qa and press <Enter> to exit Vim"));
7027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007029 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
7030 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (!p_im)
7032 restart_edit = 0;
7033#ifdef FEAT_CMDWIN
7034 if (cmdwin_type != 0)
7035 {
7036 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007037 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 return;
7039 }
7040#endif
7041 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01007042#ifdef FEAT_CMDWIN
7043 else if (cmdwin_type != 0 && ex_normal_busy)
7044 {
7045 // When :normal runs out of characters while in the command line window
7046 // vgetorpeek() will return ESC. Exit the cmdline window to break the
7047 // loop.
7048 cmdwin_result = K_IGNORE;
7049 return;
7050 }
7051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 if (VIsual_active)
7054 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007055 end_visual_mode(); // stop Visual
7056 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 curwin->w_set_curswant = TRUE;
7058 redraw_curbuf_later(INVERTED);
7059 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007060 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007061 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 clearop(cap->oap);
7063
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007064 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7065 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007066 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 restart_edit = 'a';
7068}
7069
7070/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007071 * Move the cursor for the "A" command.
7072 */
7073 void
7074set_cursor_for_append_to_line(void)
7075{
7076 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007077 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007078 {
7079 int save_State = State;
7080
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007081 // Pretend Insert mode here to allow the cursor on the
7082 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007083 State = INSERT;
7084 coladvance((colnr_T)MAXCOL);
7085 State = save_State;
7086 }
7087 else
7088 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7089}
7090
7091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007093 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 */
7095 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007096nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007098 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7100 cap->cmdchar = 'i';
7101
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007102 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007104 {
7105#ifdef FEAT_TERMINAL
7106 if (term_in_normal_mode())
7107 {
7108 end_visual_mode();
7109 clearop(cap->oap);
7110 term_enter_job_mode();
7111 return;
7112 }
7113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007117 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007118 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7119 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {
7121#ifdef FEAT_TEXTOBJ
7122 nv_object(cap);
7123#else
7124 clearopbeep(cap->oap);
7125#endif
7126 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007127#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007128 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007129 {
7130 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007131 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007132 return;
7133 }
7134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 else if (!curbuf->b_p_ma && !p_im)
7136 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007137 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007138 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007140 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007141 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007142 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007144 else if (cap->cmdchar == K_PS && VIsual_active)
7145 {
7146 pos_T old_pos = curwin->w_cursor;
7147 pos_T old_visual = VIsual;
7148
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007149 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007150 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7151 {
7152 shift_delete_registers();
7153 cap->oap->regname = '1';
7154 }
7155 else
7156 cap->oap->regname = '-';
7157 cap->cmdchar = 'd';
7158 cap->nchar = NUL;
7159 nv_operator(cap);
7160 do_pending_operator(cap, 0, FALSE);
7161 cap->cmdchar = K_PS;
7162
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007163 // When the last char in the line was deleted then append. Detect this
7164 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007165 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7166 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007167 inc_cursor();
7168
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007169 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007170 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 else if (!checkclearopq(cap->oap))
7173 {
7174 switch (cap->cmdchar)
7175 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007176 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007177 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 break;
7179
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007180 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007181 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7182 beginline(BL_WHITE);
7183 else
7184 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 break;
7186
Bram Moolenaara1891842017-02-04 21:34:31 +01007187 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007188 // Bracketed paste works like "a"ppend, unless the cursor is in
7189 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007190 if (curwin->w_cursor.col == 0)
7191 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007192 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007193
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007194 case 'a': // "a"ppend is like "i"nsert on the next character.
7195 // increment coladd when in virtual space, increment the
7196 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 if (virtual_active()
7198 && (curwin->w_cursor.coladd > 0
7199 || *ml_get_cursor() == NUL
7200 || *ml_get_cursor() == TAB))
7201 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007202 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 inc_cursor();
7204 break;
7205 }
7206
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7208 {
7209 int save_State = State;
7210
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007211 // Pretend Insert mode here to allow the cursor on the
7212 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 State = INSERT;
7214 coladvance(getviscol());
7215 State = save_State;
7216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
7218 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7219 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007220 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007221 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007222 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223}
7224
7225/*
7226 * Invoke edit() and take care of "restart_edit" and the return value.
7227 */
7228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007229invoke_edit(
7230 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007231 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007232 int cmd,
7233 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
7235 int restart_edit_save = 0;
7236
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007237 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7238 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7239 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 if (repl || !stuff_empty())
7241 restart_edit_save = restart_edit;
7242 else
7243 restart_edit_save = 0;
7244
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007245 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 restart_edit = 0;
7247
7248 if (edit(cmd, startln, cap->count1))
7249 cap->retval |= CA_COMMAND_BUSY;
7250
7251 if (restart_edit == 0)
7252 restart_edit = restart_edit_save;
7253}
7254
7255#ifdef FEAT_TEXTOBJ
7256/*
7257 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7258 */
7259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007260nv_object(
7261 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
7263 int flag;
7264 int include;
7265 char_u *mps_save;
7266
7267 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007268 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007270 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007272 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 mps_save = curbuf->b_p_mps;
7274 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7275
7276 switch (cap->nchar)
7277 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007278 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 flag = current_word(cap->oap, cap->count1, include, FALSE);
7280 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007281 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 flag = current_word(cap->oap, cap->count1, include, TRUE);
7283 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007284 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 case '(':
7286 case ')':
7287 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7288 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007289 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 case '{':
7291 case '}':
7292 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7293 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007294 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 case ']':
7296 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7297 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007298 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 case '>':
7300 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7301 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007302 case 't': // "at" = a tag block (xml and html)
7303 // Do not adjust oap->end in do_pending_operator()
7304 // otherwise there are different results for 'dit'
7305 // (note leading whitespace in last line):
7306 // 1) <b> 2) <b>
7307 // foobar foobar
7308 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007309 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007310 flag = current_tagblock(cap->oap, cap->count1, include);
7311 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007312 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 flag = current_par(cap->oap, cap->count1, include, 'p');
7314 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007315 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 flag = current_sent(cap->oap, cap->count1, include);
7317 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007318 case '"': // "a"" = a double quoted string
7319 case '\'': // "a'" = a single quoted string
7320 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007321 flag = current_quote(cap->oap, cap->count1, include,
7322 cap->nchar);
7323 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007324#if 0 // TODO
7325 case 'S': // "aS" = a section
7326 case 'f': // "af" = a filename
7327 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328#endif
7329 default:
7330 flag = FAIL;
7331 break;
7332 }
7333
7334 curbuf->b_p_mps = mps_save;
7335 if (flag == FAIL)
7336 clearopbeep(cap->oap);
7337 adjust_cursor_col();
7338 curwin->w_set_curswant = TRUE;
7339}
7340#endif
7341
7342/*
7343 * "q" command: Start/stop recording.
7344 * "q:", "q/", "q?": edit command-line in command-line window.
7345 */
7346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007347nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
7349 if (cap->oap->op_type == OP_FORMAT)
7350 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007351 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 cap->cmdchar = 'g';
7353 cap->nchar = 'q';
7354 nv_operator(cap);
7355 }
7356 else if (!checkclearop(cap->oap))
7357 {
7358#ifdef FEAT_CMDWIN
7359 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7360 {
7361 stuffcharReadbuff(cap->nchar);
7362 stuffcharReadbuff(K_CMDWIN);
7363 }
7364 else
7365#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007366 // (stop) recording into a named register, unless executing a
7367 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007368 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 clearopbeep(cap->oap);
7370 }
7371}
7372
7373/*
7374 * Handle the "@r" command.
7375 */
7376 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007377nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
7379 if (checkclearop(cap->oap))
7380 return;
7381#ifdef FEAT_EVAL
7382 if (cap->nchar == '=')
7383 {
7384 if (get_expr_register() == NUL)
7385 return;
7386 }
7387#endif
7388 while (cap->count1-- && !got_int)
7389 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007390 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 {
7392 clearopbeep(cap->oap);
7393 break;
7394 }
7395 line_breakcheck();
7396 }
7397}
7398
7399/*
7400 * Handle the CTRL-U and CTRL-D commands.
7401 */
7402 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007403nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7406 || (cap->cmdchar == Ctrl_D
7407 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7408 clearopbeep(cap->oap);
7409 else if (!checkclearop(cap->oap))
7410 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7411}
7412
7413/*
7414 * Handle "J" or "gJ" command.
7415 */
7416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007417nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007419 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007421 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
7423 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007424 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7426 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007428 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007429 if (cap->count0 <= 2)
7430 {
7431 clearopbeep(cap->oap);
7432 return;
7433 }
7434 cap->count0 = curbuf->b_ml.ml_line_count
7435 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007437
7438 prep_redo(cap->oap->regname, cap->count0,
7439 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7440 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 }
7442}
7443
7444/*
7445 * "P", "gP", "p" and "gp" commands.
7446 */
7447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007448nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007450 nv_put_opt(cap, FALSE);
7451}
7452
7453/*
7454 * "P", "gP", "p" and "gp" commands.
7455 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7456 */
7457 static void
7458nv_put_opt(cmdarg_T *cap, int fix_indent)
7459{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 int regname = 0;
7461 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007462 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007463 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 int dir;
7465 int flags = 0;
7466
7467 if (cap->oap->op_type != OP_NOP)
7468 {
7469#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007470 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7472 {
7473 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007474 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476 else
7477#endif
7478 clearopbeep(cap->oap);
7479 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007480#ifdef FEAT_JOB_CHANNEL
7481 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7482 {
7483 clearopbeep(cap->oap);
7484 }
7485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 else
7487 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007488 if (fix_indent)
7489 {
7490 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7491 ? FORWARD : BACKWARD;
7492 flags |= PUT_FIXINDENT;
7493 }
7494 else
7495 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007496 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7497 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 prep_redo_cmd(cap);
7499 if (cap->cmdchar == 'g')
7500 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007501 else if (cap->cmdchar == 'z')
7502 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 if (VIsual_active)
7505 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007506 // Putting in Visual mode: The put text replaces the selected
7507 // text. First delete the selected text, then put the new text.
7508 // Need to save and restore the registers that the delete
7509 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007510 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007512#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007514#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007515 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007516 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007517#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
7521 )
7522 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007523 // The delete is going to overwrite the register we want to
7524 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 reg1 = get_register(regname, TRUE);
7526 }
7527
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007528 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 cap->cmdchar = 'd';
7530 cap->nchar = NUL;
7531 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007532 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 nv_operator(cap);
7534 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007535 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007536 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007538 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 cap->oap->regname = regname;
7540
7541 if (reg1 != NULL)
7542 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007543 // Delete probably changed the register we want to put, save
7544 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 reg2 = get_register(regname, FALSE);
7546 put_register(regname, reg1);
7547 }
7548
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007549 // When deleted a linewise Visual area, put the register as
7550 // lines to avoid it joined with the next line. When deletion was
7551 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 if (VIsual_mode == 'V')
7553 flags |= PUT_LINE;
7554 else if (VIsual_mode == 'v')
7555 flags |= PUT_LINE_SPLIT;
7556 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7557 flags |= PUT_LINE_FORWARD;
7558 dir = BACKWARD;
7559 if ((VIsual_mode != 'V'
7560 && curwin->w_cursor.col < curbuf->b_op_start.col)
7561 || (VIsual_mode == 'V'
7562 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007563 // cursor is at the end of the line or end of file, put
7564 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007566 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007567 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007569 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007571 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 if (reg2 != NULL)
7573 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007575 // What to reselect with "gv"? Selecting the just put text seems to
7576 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007577 if (was_visual)
7578 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007579 curbuf->b_visual.vi_start = curbuf->b_op_start;
7580 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007581 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007582 if (*p_sel == 'e')
7583 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007584 }
7585
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007586 // When all lines were selected and deleted do_put() leaves an empty
7587 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007588 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007589 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007590 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007591 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007592
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007593 // If the cursor was in that line, move it to the end of the last
7594 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007595 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7596 {
7597 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7598 coladvance((colnr_T)MAXCOL);
7599 }
7600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 auto_format(FALSE, TRUE);
7602 }
7603}
7604
7605/*
7606 * "o" and "O" commands.
7607 */
7608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007609nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610{
7611#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007612 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7614 {
7615 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007616 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 }
7618 else
7619#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007620 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007622#ifdef FEAT_JOB_CHANNEL
7623 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007624 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 n_opencmd(cap);
7628}
7629
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630#ifdef FEAT_NETBEANS_INTG
7631 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007632nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634 netbeans_keycommand(cap->nchar);
7635}
7636#endif
7637
7638#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007640nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007642 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643}
7644#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007645
Bram Moolenaar3918c952005-03-15 22:34:55 +00007646/*
7647 * Trigger CursorHold event.
7648 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7649 * input buffer. "did_cursorhold" is set to avoid retriggering.
7650 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007651 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007652nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007653{
7654 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7655 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007656 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007657}