blob: 79e47a69a52fba31153222a27372e7302275a26f [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 Moolenaar071d4272004-06-13 20:20:40 +0000376};
377
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100378// Number of commands in nv_cmds[].
K.Takataeeec2542021-06-02 13:28:16 +0200379#define NV_CMDS_SIZE ARRAY_LENGTH(nv_cmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000381#ifndef PROTO // cproto doesn't like this
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100382// Sorted index of commands in nv_cmds[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383static short nv_cmd_idx[NV_CMDS_SIZE];
Bram Moolenaar2228cd72021-11-22 14:16:08 +0000384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100386// The highest index for which
387// nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static int nv_max_linear;
389
390/*
391 * Compare functions for qsort() below, that checks the command character
392 * through the index in nv_cmd_idx[].
393 */
394 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100395nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396{
397 int c1, c2;
398
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100399 // The commands are sorted on absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 c1 = nv_cmds[*(const short *)s1].cmd_char;
401 c2 = nv_cmds[*(const short *)s2].cmd_char;
402 if (c1 < 0)
403 c1 = -c1;
404 if (c2 < 0)
405 c2 = -c2;
406 return c1 - c2;
407}
408
409/*
410 * Initialize the nv_cmd_idx[] table.
411 */
412 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100413init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414{
415 int i;
416
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100417 // Fill the index table with a one to one relation.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000418 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 nv_cmd_idx[i] = i;
420
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100421 // Sort the commands by the command character.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100424 // Find the first entry that can't be indexed by the command character.
Bram Moolenaar78a15312009-05-15 19:33:18 +0000425 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
427 break;
428 nv_max_linear = i - 1;
429}
430
431/*
432 * Search for a command in the commands table.
433 * Returns -1 for invalid command.
434 */
435 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100436find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437{
438 int i;
439 int idx;
440 int top, bot;
441 int c;
442
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100443 // A multi-byte character is never a command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (cmdchar >= 0x100)
445 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100447 // We use the absolute value of the character. Special keys have a
448 // negative value, but are sorted on their absolute value.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 if (cmdchar < 0)
450 cmdchar = -cmdchar;
451
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100452 // If the character is in the first part: The character is the index into
453 // nv_cmd_idx[].
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (cmdchar <= nv_max_linear)
455 return nv_cmd_idx[cmdchar];
456
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100457 // Perform a binary search.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 bot = nv_max_linear + 1;
459 top = NV_CMDS_SIZE - 1;
460 idx = -1;
461 while (bot <= top)
462 {
463 i = (top + bot) / 2;
464 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
465 if (c < 0)
466 c = -c;
467 if (cmdchar == c)
468 {
469 idx = nv_cmd_idx[i];
470 break;
471 }
472 if (cmdchar > c)
473 bot = i + 1;
474 else
475 top = i - 1;
476 }
477 return idx;
478}
479
480/*
481 * Execute a command in Normal mode.
482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100484normal_cmd(
485 oparg_T *oap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100486 int toplevel UNUSED) // TRUE when called from main()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100488 cmdarg_T ca; // command arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 int c;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100490 int ctrl_w = FALSE; // got CTRL-W command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 int old_col = curwin->w_curswant;
492#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100493 int need_flushbuf; // need to call out_flush()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100495 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 static int old_mapped_len = 0;
498 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000499#ifdef FEAT_EVAL
500 int set_prevcount = FALSE;
501#endif
Bram Moolenaarb146e012020-07-19 23:06:05 +0200502 int save_did_cursorhold = did_cursorhold;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503
Bram Moolenaara80faa82020-04-12 19:37:17 +0200504 CLEAR_FIELD(ca); // also resets ca.retval
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000506
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100507 // Use a count remembered from before entering an operator. After typing
508 // "3d" we return from normal_cmd() and come back here, the "3" is
509 // remembered in "opcount".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 ca.opcount = opcount;
511
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 /*
513 * If there is an operator pending, then the command we take this time
514 * will terminate it. Finish_op tells us to finish the operation before
515 * returning this time (unless the operation was cancelled).
516 */
517#ifdef CURSOR_SHAPE
518 c = finish_op;
519#endif
520 finish_op = (oap->op_type != OP_NOP);
521#ifdef CURSOR_SHAPE
522 if (finish_op != c)
523 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100524 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525# ifdef FEAT_MOUSESHAPE
526 update_mouseshape(-1);
527# endif
528 }
529#endif
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +0100530 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100532 // When not finishing an operator and no register name typed, reset the
533 // count.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000537#ifdef FEAT_EVAL
538 set_prevcount = TRUE;
539#endif
540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100542 // Restore counts from before receiving K_CURSORHOLD. This means after
543 // typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
544 // "3 * 2".
Bram Moolenaara983fe92008-07-31 20:04:27 +0000545 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
546 {
547 ca.opcount = oap->prev_opcount;
548 ca.count0 = oap->prev_count0;
549 oap->prev_opcount = 0;
550 oap->prev_count0 = 0;
551 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000552
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554
555 State = NORMAL_BUSY;
556#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100557 dont_scroll = FALSE; // allow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100560#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100561 // Set v:count here, when called from main() and not a stuffed
562 // command, so that v:count can be used in an expression mapping
563 // when there is no count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100564 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100565 set_vcount_ca(&ca, &set_prevcount);
566#endif
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /*
569 * Get the command character from the user.
570 */
571 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100572 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 /*
575 * If a mapping was started in Visual or Select mode, remember the length
576 * of the mapping. This is used below to not return to Insert mode for as
577 * long as the mapping is being executed.
578 */
579 if (restart_edit == 0)
580 old_mapped_len = 0;
581 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000582 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 old_mapped_len = typebuf_maplen();
584
585 if (c == NUL)
586 c = K_ZERO;
587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 /*
589 * In Select mode, typed text replaces the selection.
590 */
591 if (VIsual_active
592 && VIsual_select
593 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
594 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100595 // Fake a "c"hange command. When "restart_edit" is set (e.g., because
596 // 'insertmode' is set) fake a "d"elete command, Insert mode will
597 // restart automatically.
598 // Insert the typed character in the typeahead buffer, so that it can
599 // be mapped in Insert mode. Required for ":lmap" to work.
Bram Moolenaarb42c0d52020-05-29 22:41:41 +0200600 ins_char_typebuf(vgetc_char, vgetc_mod_mask);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000601 if (restart_edit != 0)
602 c = 'd';
603 else
604 c = 'c';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100605 msg_nowait = TRUE; // don't delay going to insert mode
606 old_mapped_len = 0; // do go to Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaard0fb9072021-12-09 11:57:22 +0000609 // If the window was made so small that nothing shows, make it at least one
610 // line and one column when typing a command.
611 if (KeyTyped && !KeyStuffed)
612 win_ensure_size();
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef FEAT_CMDL_INFO
615 need_flushbuf = add_to_showcmd(c);
616#endif
617
618getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 {
621 /*
622 * Handle a count before a command and compute ca.count0.
623 * Note that '0' is a command and not the start of a count, but it's
624 * part of a count after other digits.
625 */
626 while ( (c >= '1' && c <= '9')
627 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
628 {
629 if (c == K_DEL || c == K_KDEL)
630 {
631 ca.count0 /= 10;
632#ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100633 del_from_showcmd(4); // delete the digit and ~@%
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#endif
635 }
Bram Moolenaar9b0e82f2021-11-24 13:40:29 +0000636 else if (ca.count0 > 99999999L)
Bram Moolenaar03725c52021-11-24 12:17:53 +0000637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ca.count0 = 999999999L;
Bram Moolenaar03725c52021-11-24 12:17:53 +0000639 }
640 else
641 {
642 ca.count0 = ca.count0 * 10 + (c - '0');
643 }
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000644#ifdef FEAT_EVAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100645 // Set v:count here, when called from main() and not a stuffed
646 // command, so that v:count can be used in an expression mapping
647 // right after the count. Do set it for redo.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100648 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100649 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (ctrl_w)
652 {
653 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100654 ++allow_keys; // no mapping for nchar, but keys
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100656 ++no_zero_mapping; // don't map zero here
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000657 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 --no_zero_mapping;
660 if (ctrl_w)
661 {
662 --no_mapping;
663 --allow_keys;
664 }
665#ifdef FEAT_CMDL_INFO
666 need_flushbuf |= add_to_showcmd(c);
667#endif
668 }
669
670 /*
671 * If we got CTRL-W there may be a/another count
672 */
673 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
674 {
675 ctrl_w = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100676 ca.opcount = ca.count0; // remember first count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 ca.count0 = 0;
678 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100679 ++allow_keys; // no mapping for nchar, but keys
680 c = plain_vgetc(); // get next character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 --no_mapping;
683 --allow_keys;
684#ifdef FEAT_CMDL_INFO
685 need_flushbuf |= add_to_showcmd(c);
686#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100687 goto getcount; // jump back
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 }
689 }
690
Bram Moolenaara983fe92008-07-31 20:04:27 +0000691 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100693 // Save the count values so that ca.opcount and ca.count0 are exactly
694 // the same when coming back here after handling K_CURSORHOLD.
Bram Moolenaara983fe92008-07-31 20:04:27 +0000695 oap->prev_opcount = ca.opcount;
696 oap->prev_count0 = ca.count0;
697 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100698 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000699 {
700 /*
701 * If we're in the middle of an operator (including after entering a
702 * yank buffer with '"') AND we had a count before the operator, then
703 * that count overrides the current value of ca.count0.
704 * What this means effectively, is that commands like "3dw" get turned
705 * into "d3w" which makes things fall into place pretty neatly.
706 * If you give a count before AND after the operator, they are
707 * multiplied.
708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (ca.count0)
Bram Moolenaar03725c52021-11-24 12:17:53 +0000710 {
711 if (ca.opcount >= 999999999L / ca.count0)
712 ca.count0 = 999999999L;
713 else
714 ca.count0 *= ca.opcount;
715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 else
717 ca.count0 = ca.opcount;
718 }
719
720 /*
721 * Always remember the count. It will be set to zero (on the next call,
722 * above) when there is no pending operator.
723 * When called from main(), save the count for use by the "count" built-in
724 * variable.
725 */
726 ca.opcount = ca.count0;
727 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
728
729#ifdef FEAT_EVAL
730 /*
731 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100732 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100734 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000735 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#endif
737
738 /*
739 * Find the command character in the table of commands.
740 * For CTRL-W we already got nchar when looking for a count.
741 */
742 if (ctrl_w)
743 {
744 ca.nchar = c;
745 ca.cmdchar = Ctrl_W;
746 }
747 else
748 ca.cmdchar = c;
749 idx = find_command(ca.cmdchar);
750 if (idx < 0)
751 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100752 // Not a known command: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 clearopbeep(oap);
754 goto normal_end;
755 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000756
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000757 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100759 // This command is not allowed while editing a cmdline: beep.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000761 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 goto normal_end;
763 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000764 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
765 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 /*
768 * In Visual/Select mode, a few keys are handled in a special way.
769 */
770 if (VIsual_active)
771 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100772 // when 'keymodel' contains "stopsel" may stop Select/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (km_stopsel
774 && (nv_cmds[idx].cmd_flags & NV_STS)
775 && !(mod_mask & MOD_MASK_SHIFT))
776 {
777 end_visual_mode();
778 redraw_curbuf_later(INVERTED);
779 }
780
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100781 // Keys that work different when 'keymodel' contains "startsel"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (km_startsel)
783 {
784 if (nv_cmds[idx].cmd_flags & NV_SS)
785 {
786 unshift_special(&ca);
787 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000788 if (idx < 0)
789 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100790 // Just in case
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000791 clearopbeep(oap);
792 goto normal_end;
793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 }
795 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
796 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 }
799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801#ifdef FEAT_RIGHTLEFT
802 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
803 && (nv_cmds[idx].cmd_flags & NV_RL))
804 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100805 // Invert horizontal movements and operations. Only when typed by the
806 // user directly, not when the result of a mapping or "x" translated
807 // to "dl".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 switch (ca.cmdchar)
809 {
810 case 'l': ca.cmdchar = 'h'; break;
811 case K_RIGHT: ca.cmdchar = K_LEFT; break;
812 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
813 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
814 case 'h': ca.cmdchar = 'l'; break;
815 case K_LEFT: ca.cmdchar = K_RIGHT; break;
816 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
817 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
818 case '>': ca.cmdchar = '<'; break;
819 case '<': ca.cmdchar = '>'; break;
820 }
821 idx = find_command(ca.cmdchar);
822 }
823#endif
824
825 /*
826 * Get an additional character if we need one.
827 */
828 if ((nv_cmds[idx].cmd_flags & NV_NCH)
829 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
830 && oap->op_type == OP_NOP)
831 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
832 || (ca.cmdchar == 'q'
833 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200834 && reg_recording == 0
835 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100837 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
839 int *cp;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100840 int repl = FALSE; // get character for replace mode
841 int lit = FALSE; // get extra character literally
842 int langmap_active = FALSE; // using :lmap mappings
843 int lang; // getting a text character
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100844#ifdef HAVE_INPUT_METHOD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100845 int save_smd; // saved value of p_smd
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#endif
847
848 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100849 ++allow_keys; // no mapping for nchar, but allow key codes
850 // Don't generate a CursorHold event here, most commands can't handle
851 // it, e.g., nv_replace(), nv_csearch().
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000852 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 if (ca.cmdchar == 'g')
854 {
855 /*
856 * For 'g' get the next character now, so that we can check for
857 * "gr", "g'" and "g`".
858 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000859 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861#ifdef FEAT_CMDL_INFO
862 need_flushbuf |= add_to_showcmd(ca.nchar);
863#endif
864 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100865 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100867 cp = &ca.extra_char; // need to get a third character
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (ca.nchar != 'r')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100869 lit = TRUE; // get it literally
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100871 repl = TRUE; // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100874 cp = NULL; // no third character needed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
876 else
877 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100878 if (ca.cmdchar == 'r') // get it in replace mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 repl = TRUE;
880 cp = &ca.nchar;
881 }
882 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
883
884 /*
885 * Get a second or third character.
886 */
887 if (cp != NULL)
888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (repl)
890 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100891 State = REPLACE; // pretend Replace mode
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100892#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100893 ui_cursor_shape(); // show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#endif
Bram Moolenaar7a641ca2019-10-31 19:55:55 +0100895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
897 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100898 // Allow mappings defined with ":lmap".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 --no_mapping;
900 --allow_keys;
901 if (repl)
902 State = LREPLACE;
903 else
904 State = LANGMAP;
905 langmap_active = TRUE;
906 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100907#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 save_smd = p_smd;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100909 p_smd = FALSE; // Don't let the IM code show the mode here
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
911 im_set_active(TRUE);
912#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200913 if ((State & INSERT) && !p_ek)
914 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200915#ifdef FEAT_JOB_CHANNEL
916 ch_log_output = TRUE;
917#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200918 // Disable bracketed paste and modifyOtherKeys here, we won't
919 // recognize the escape sequences with 'esckeys' off.
920 out_str(T_BD);
921 out_str(T_CTE);
922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000924 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
Bram Moolenaarca774f62020-08-30 20:46:38 +0200926 if ((State & INSERT) && !p_ek)
927 {
Bram Moolenaar86394aa2020-09-05 14:27:24 +0200928#ifdef FEAT_JOB_CHANNEL
929 ch_log_output = TRUE;
930#endif
Bram Moolenaarca774f62020-08-30 20:46:38 +0200931 // Re-enable bracketed paste mode and modifyOtherKeys
932 out_str(T_BE);
933 out_str(T_CTI);
934 }
935
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (langmap_active)
937 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100938 // Undo the decrement done above
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 ++no_mapping;
940 ++allow_keys;
941 State = NORMAL_BUSY;
942 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100943#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (lang)
945 {
946 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
947 im_save_status(&curbuf->b_p_iminsert);
948 im_set_active(FALSE);
949 }
950 p_smd = save_smd;
951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 State = NORMAL_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953#ifdef FEAT_CMDL_INFO
954 need_flushbuf |= add_to_showcmd(*cp);
955#endif
956
957 if (!lit)
958 {
959#ifdef FEAT_DIGRAPHS
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100960 // Typing CTRL-K gets a digraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (*cp == Ctrl_K
962 && ((nv_cmds[idx].cmd_flags & NV_LANG)
963 || cp == &ca.extra_char)
964 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
965 {
966 c = get_digraph(FALSE);
967 if (c > 0)
968 {
969 *cp = c;
970# ifdef FEAT_CMDL_INFO
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100971 // Guessing how to update showcmd here...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 del_from_showcmd(3);
973 need_flushbuf |= add_to_showcmd(*cp);
974# endif
975 }
976 }
977#endif
978
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100979 // adjust chars > 127, except after "tTfFr" commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e0ce172019-12-05 20:12:41 +0100982 // adjust Hebrew mapped char
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (p_hkmap && lang && KeyTyped)
984 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#endif
986 }
987
988 /*
989 * When the next character is CTRL-\ a following CTRL-N means the
990 * command is aborted and we go to Normal mode.
991 */
992 if (cp == &ca.extra_char
993 && ca.nchar == Ctrl_BSL
994 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
995 {
996 ca.cmdchar = Ctrl_BSL;
997 ca.nchar = ca.extra_char;
998 idx = find_command(ca.cmdchar);
999 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001000 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001001 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 else if (*cp == Ctrl_BSL)
1003 {
1004 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1005
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001006 // There is a busy wait here when typing "f<C-\>" and then
1007 // something different from CTRL-N. Can't be avoided.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 while ((c = vpeekc()) <= 0 && towait > 0L)
1009 {
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01001010 do_sleep(towait > 50L ? 50L : towait, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 towait -= 50L;
1012 }
1013 if (c > 0)
1014 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001015 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (c != Ctrl_N && c != Ctrl_G)
1017 vungetc(c);
1018 else
1019 {
1020 ca.cmdchar = Ctrl_BSL;
1021 ca.nchar = c;
1022 idx = find_command(ca.cmdchar);
1023 }
1024 }
1025 }
1026
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001027 // When getting a text character and the next character is a
1028 // multi-byte character, it could be a composing character.
1029 // However, don't wait for it to arrive. Also, do enable mapping,
1030 // because if it's put back with vungetc() it's too late to apply
1031 // mapping.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001032 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 while (enc_utf8 && lang && (c = vpeekc()) > 0
1034 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1035 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001036 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (!utf_iscomposing(c))
1038 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001039 vungetc(c); // it wasn't, put it back
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 break;
1041 }
1042 else if (ca.ncharC1 == 0)
1043 ca.ncharC1 = c;
1044 else
1045 ca.ncharC2 = c;
1046 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001047 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049 --no_mapping;
1050 --allow_keys;
1051 }
1052
1053#ifdef FEAT_CMDL_INFO
1054 /*
1055 * Flush the showcmd characters onto the screen so we can see them while
1056 * the command is being executed. Only do this when the shown command was
1057 * actually displayed, otherwise this will slow down a lot when executing
1058 * mappings.
1059 */
1060 if (need_flushbuf)
1061 out_flush();
1062#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001063 if (ca.cmdchar != K_IGNORE)
Bram Moolenaar08815a12020-07-20 23:10:56 +02001064 {
1065 if (ex_normal_busy)
1066 did_cursorhold = save_did_cursorhold;
1067 else
1068 did_cursorhold = FALSE;
1069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
1071 State = NORMAL;
1072
1073 if (ca.nchar == ESC)
1074 {
1075 clearop(oap);
1076 if (restart_edit == 0 && goto_im())
1077 restart_edit = 'a';
1078 goto normal_end;
1079 }
1080
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001081 if (ca.cmdchar != K_IGNORE)
1082 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001083 msg_didout = FALSE; // don't scroll screen up for normal command
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001084 msg_col = 0;
1085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001087 old_pos = curwin->w_cursor; // remember where cursor was
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001089 // When 'keymodel' contains "startsel" some keys start Select/Visual
1090 // mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (!VIsual_active && km_startsel)
1092 {
1093 if (nv_cmds[idx].cmd_flags & NV_SS)
1094 {
1095 start_selection();
1096 unshift_special(&ca);
1097 idx = find_command(ca.cmdchar);
1098 }
1099 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1100 && (mod_mask & MOD_MASK_SHIFT))
1101 {
1102 start_selection();
1103 mod_mask &= ~MOD_MASK_SHIFT;
1104 }
1105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
1107 /*
1108 * Execute the command!
1109 * Call the command function found in the commands table.
1110 */
1111 ca.arg = nv_cmds[idx].cmd_arg;
1112 (nv_cmds[idx].cmd_func)(&ca);
1113
1114 /*
1115 * If we didn't start or finish an operator, reset oap->regname, unless we
1116 * need it later.
1117 */
1118 if (!finish_op
1119 && !oap->op_type
1120 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1121 {
1122 clearop(oap);
1123#ifdef FEAT_EVAL
Bram Moolenaar439c0362020-06-06 15:58:03 +02001124 reset_reg_var();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#endif
1126 }
1127
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001128 // Get the length of mapped chars again after typing a count, second
1129 // character or "z333<cr>".
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001130 if (old_mapped_len > 0)
1131 old_mapped_len = typebuf_maplen();
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 /*
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001134 * If an operation is pending, handle it. But not for K_IGNORE or
1135 * K_MOUSEMOVE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 */
Bram Moolenaar1ad72c82021-05-04 21:56:28 +02001137 if (ca.cmdchar != K_IGNORE && ca.cmdchar != K_MOUSEMOVE)
Bram Moolenaarfa5612c2019-12-01 19:37:07 +01001138 do_pending_operator(&ca, old_col, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
1140 /*
1141 * Wait for a moment when a message is displayed that will be overwritten
1142 * by the mode message.
1143 * In Visual mode and with "^O" in Insert mode, a short message will be
1144 * overwritten by the mode message. Wait a bit, until a key is hit.
1145 * In Visual mode, it's more important to keep the Visual area updated
1146 * than keeping a message (e.g. from a /pat search).
1147 * Only do this if the command was typed, not from a mapping.
1148 * Don't wait when emsg_silent is non-zero.
1149 * Also wait a bit after an error message, e.g. for "^O:".
1150 * Don't redraw the screen, it would remove the message.
1151 */
1152 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001153 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 || (VIsual_active
1156 && old_pos.lnum == curwin->w_cursor.lnum
1157 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 )
1159 && (clear_cmdline
1160 || redraw_cmdline)
1161 && (msg_didout || (msg_didany && msg_scroll))
1162 && !msg_nowait
1163 && KeyTyped)
1164 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 && (msg_scroll
1167 || emsg_on_display)))
1168 && oap->regname == 0
1169 && !(ca.retval & CA_COMMAND_BUSY)
1170 && stuff_empty()
1171 && typebuf_typed()
1172 && emsg_silent == 0
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001173 && !in_assert_fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && !did_wait_return
1175 && oap->op_type == OP_NOP)
1176 {
1177 int save_State = State;
1178
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001179 // Draw the cursor with the right shape here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (restart_edit != 0)
1181 State = INSERT;
1182
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001183 // If need to redraw, and there is a "keep_msg", redraw before the
1184 // delay
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1186 {
1187 char_u *kmsg;
1188
1189 kmsg = keep_msg;
1190 keep_msg = NULL;
Bram Moolenaar5715b312020-03-16 22:08:45 +01001191 // Showmode() will clear keep_msg, but we want to use it anyway.
1192 // First update w_topline.
1193 setcursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001195 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001197
1198 kmsg = vim_strsave(keep_msg);
1199 if (kmsg != NULL)
1200 {
1201 msg_attr((char *)kmsg, keep_msg_attr);
1202 vim_free(kmsg);
1203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 setcursor();
Bram Moolenaar5715b312020-03-16 22:08:45 +01001206#ifdef CURSOR_SHAPE
1207 ui_cursor_shape(); // may show different cursor shape
1208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 cursor_on();
1210 out_flush();
1211 if (msg_scroll || emsg_on_display)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001212 ui_delay(1003L, TRUE); // wait at least one second
1213 ui_delay(3003L, FALSE); // wait up to three seconds
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 State = save_State;
1215
1216 msg_scroll = FALSE;
1217 emsg_on_display = FALSE;
1218 }
1219
1220 /*
1221 * Finish up after executing a Normal mode command.
1222 */
1223normal_end:
1224
1225 msg_nowait = FALSE;
1226
Bram Moolenaarcc613032020-06-07 21:31:18 +02001227#ifdef FEAT_EVAL
1228 if (finish_op)
1229 reset_reg_var();
1230#endif
1231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001232 // Reset finish_op, in case it was set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233#ifdef CURSOR_SHAPE
1234 c = finish_op;
1235#endif
1236 finish_op = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001237 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238#ifdef CURSOR_SHAPE
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001239 // Redraw the cursor with another shape, if we were in Operator-pending
1240 // mode or did a replace command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (c || ca.cmdchar == 'r')
1242 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001243 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244# ifdef FEAT_MOUSESHAPE
1245 update_mouseshape(-1);
1246# endif
1247 }
1248#endif
1249
1250#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001251 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001252 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 clear_showcmd();
1254#endif
1255
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001256 checkpcmark(); // check if we moved since setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 vim_free(ca.searchbuf);
1258
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (has_mbyte)
1260 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (curwin->w_p_scb && toplevel)
1263 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001264 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 do_check_scrollbind(TRUE);
1266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267
Bram Moolenaar860cae12010-06-05 23:22:07 +02001268 if (curwin->w_p_crb && toplevel)
1269 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001270 validate_cursor(); // may need to update w_leftcol
Bram Moolenaar860cae12010-06-05 23:22:07 +02001271 do_check_cursorbind();
1272 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001273
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001274#ifdef FEAT_TERMINAL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001275 // don't go to Insert mode if a terminal has a running job
Bram Moolenaareef9add2017-09-16 15:38:04 +02001276 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001277 restart_edit = 0;
1278#endif
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 /*
1281 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1282 * if still inside a mapping that started in Visual mode).
1283 * May switch from Visual to Select mode after CTRL-O command.
1284 */
1285 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1287 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 && !(ca.retval & CA_COMMAND_BUSY)
1289 && stuff_empty()
1290 && oap->regname == 0)
1291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 if (restart_VIsual_select == 1)
1293 {
1294 VIsual_select = TRUE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001295 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 showmode();
1297 restart_VIsual_select = 0;
1298 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001299 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 (void)edit(restart_edit, FALSE, 1L);
1301 }
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (restart_VIsual_select == 2)
1304 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001306 // Save count before an operator for next time.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 opcount = ca.opcount;
1308}
1309
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001310#ifdef FEAT_EVAL
1311/*
1312 * Set v:count and v:count1 according to "cap".
1313 * Set v:prevcount only when "set_prevcount" is TRUE.
1314 */
1315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001316set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001317{
1318 long count = cap->count0;
1319
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001320 // multiply with cap->opcount the same way as above
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001321 if (cap->opcount != 0)
1322 count = cap->opcount * (count == 0 ? 1 : count);
1323 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001324 *set_prevcount = FALSE; // only set v:prevcount once
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001325}
1326#endif
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328/*
Bram Moolenaar5715b312020-03-16 22:08:45 +01001329 * Check if highlighting for Visual mode is possible, give a warning message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * if not.
1331 */
1332 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001333check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 static int did_check = FALSE;
1336
1337 if (full_screen)
1338 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01001339 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001340 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 did_check = TRUE;
1342 }
1343}
1344
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001345#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
1346/*
1347 * Call yank_do_autocmd() for "regname".
1348 */
1349 static void
1350call_yank_do_autocmd(int regname)
1351{
1352 oparg_T oa;
1353 yankreg_T *reg;
1354
1355 clear_oparg(&oa);
1356 oa.regname = regname;
1357 oa.op_type = OP_YANK;
1358 oa.is_VIsual = TRUE;
1359 reg = get_register(regname, TRUE);
1360 yank_do_autocmd(&oa, reg);
1361 free_register(reg);
1362}
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00001366 * End Visual mode.
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001367 * This function or the next should ALWAYS be called to end Visual mode, except
1368 * from do_pending_operator().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 */
1370 void
Bram Moolenaar4f3c57f2021-06-03 22:11:08 +02001371end_visual_mode()
1372{
1373 end_visual_mode_keep_button();
1374 reset_held_button();
1375}
1376
1377 void
1378end_visual_mode_keep_button()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379{
1380#ifdef FEAT_CLIPBOARD
1381 /*
1382 * If we are using the clipboard, then remember what was selected in case
1383 * we need to paste it somewhere while we still own the selection.
1384 * Only do this when the clipboard is already owned. Don't want to grab
1385 * the selection when hitting ESC.
1386 */
1387 if (clip_star.available && clip_star.owned)
1388 clip_auto_select();
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001389
1390# if defined(FEAT_EVAL)
1391 // Emit a TextYankPost for the automatic copy of the selection into the
1392 // star and/or plus register.
1393 if (has_textyankpost())
1394 {
1395 if (clip_isautosel_star())
1396 call_yank_do_autocmd('*');
1397 if (clip_isautosel_plus())
1398 call_yank_do_autocmd('+');
1399 }
1400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#endif
1402
1403 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 setmouse();
1405 mouse_dragging = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001407 // Save the current VIsual area for '< and '> marks, and "gv"
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001408 curbuf->b_visual.vi_mode = VIsual_mode;
1409 curbuf->b_visual.vi_start = VIsual;
1410 curbuf->b_visual.vi_end = curwin->w_cursor;
1411 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#ifdef FEAT_EVAL
1413 curbuf->b_visual_mode_eval = VIsual_mode;
1414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (!virtual_active())
1416 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001417 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001419 adjust_cursor_eol();
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001420 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421}
1422
1423/*
1424 * Reset VIsual_active and VIsual_reselect.
1425 */
1426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001427reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
1429 if (VIsual_active)
1430 {
1431 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001432 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 }
1434 VIsual_reselect = FALSE;
1435}
1436
1437/*
1438 * Reset VIsual_active and VIsual_reselect if it's set.
1439 */
1440 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001441reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442{
1443 if (VIsual_active)
1444 {
1445 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001446 redraw_curbuf_later(INVERTED); // delete the inversion later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 VIsual_reselect = FALSE;
1448 }
1449}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001451 void
1452restore_visual_mode(void)
1453{
1454 if (VIsual_mode_orig != NUL)
1455 {
1456 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1457 VIsual_mode_orig = NUL;
1458 }
1459}
1460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461/*
1462 * Check for a balloon-eval special item to include when searching for an
1463 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
1464 * Returns TRUE if the character at "*ptr" should be included.
1465 * "dir" is FORWARD or BACKWARD, the direction of searching.
1466 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
1467 * "bnp" points to a counter for square brackets.
1468 */
1469 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001470find_is_eval_item(
1471 char_u *ptr,
1472 int *colp,
1473 int *bnp,
1474 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001476 // Accept everything inside [].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
1478 ++*bnp;
1479 if (*bnp > 0)
1480 {
1481 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
1482 --*bnp;
1483 return TRUE;
1484 }
1485
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001486 // skip over "s.var"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (*ptr == '.')
1488 return TRUE;
1489
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001490 // two-character item: s->var
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
1492 && ptr[dir == BACKWARD ? -1 : 0] == '-')
1493 {
1494 *colp += dir;
1495 return TRUE;
1496 }
1497 return FALSE;
1498}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
1500/*
1501 * Find the identifier under or to the right of the cursor.
1502 * "find_type" can have one of three values:
1503 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001504 * FIND_STRING: find any non-white text
1505 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00001506 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 *
1508 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001509 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001511 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 * This doesn't match the real Vi but I like it a little better and it
1513 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001514 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 * When FIND_IDENT isn't defined, we backup until a blank.
1516 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001517 * Returns the length of the text, or zero if no text is found.
1518 * If text is found, a pointer to the text is put in "*text". This
1519 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 */
1521 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001522find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001525 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526}
1527
1528/*
1529 * Like find_ident_under_cursor(), but for any window and any position.
1530 * However: Uses 'iskeyword' from the current window!.
1531 */
1532 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001533find_ident_at_pos(
1534 win_T *wp,
1535 linenr_T lnum,
1536 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001537 char_u **text,
1538 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01001539 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540{
1541 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001542 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 int this_class = 0;
1545 int prev_class;
1546 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001547 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
1549 /*
1550 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001551 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 */
1553 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
1554 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
1555 {
1556 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001557 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
1559 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 if (has_mbyte)
1561 {
1562 while (ptr[col] != NUL)
1563 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001564 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1566 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 this_class = mb_get_class(ptr + col);
1568 if (this_class != 0 && (i == 1 || this_class != 1))
1569 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001570 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572 }
1573 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001575 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 )
1578 ++col;
1579
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001580 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001584 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (has_mbyte)
1587 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001588 // Remember class of character under cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if ((find_type & FIND_EVAL) && ptr[col] == ']')
1590 this_class = mb_get_class((char_u *)"a");
1591 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001593 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
1595 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
1596 prev_class = mb_get_class(ptr + prevcol);
1597 if (this_class != prev_class
1598 && (i == 0
1599 || prev_class == 0
1600 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 && (!(find_type & FIND_EVAL)
1602 || prevcol == 0
1603 || !find_is_eval_item(ptr + prevcol, &prevcol,
1604 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 )
1606 break;
1607 col = prevcol;
1608 }
1609
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001610 // If we don't want just any old text, or we've found an
1611 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 if (this_class > 2)
1613 this_class = 2;
1614 if (!(find_type & FIND_STRING) || this_class == 2)
1615 break;
1616 }
1617 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
1619 while (col > 0
1620 && ((i == 0
1621 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001622 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 && (!(find_type & FIND_IDENT)
1624 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 || ((find_type & FIND_EVAL)
1626 && col > 1
1627 && find_is_eval_item(ptr + col - 1, &col,
1628 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 ))
1630 --col;
1631
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001632 // If we don't want just any old text, or we've found an
1633 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
1635 break;
1636 }
1637 }
1638
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001639 if (ptr[col] == NUL || (i == 0
1640 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001642 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02001643 if ((find_type & FIND_NOERROR) == 0)
1644 {
1645 if (find_type & FIND_STRING)
Bram Moolenaareaaac012022-01-02 17:00:40 +00001646 emsg(_(e_no_string_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001647 else
Bram Moolenaareaaac012022-01-02 17:00:40 +00001648 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar17627312019-06-02 19:53:44 +02001649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 return 0;
1651 }
1652 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001653 *text = ptr;
1654 if (textcol != NULL)
1655 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
1657 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001658 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 bn = 0;
1661 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (has_mbyte)
1664 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02001665 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 this_class = mb_get_class(ptr);
1667 while (ptr[col] != NUL
1668 && ((i == 0 ? mb_get_class(ptr + col) == this_class
1669 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 || ((find_type & FIND_EVAL)
1671 && col <= (int)startcol
1672 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001674 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
1676 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01001678 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 || ((find_type & FIND_EVAL)
1680 && col <= (int)startcol
1681 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
1685 return col;
1686}
1687
1688/*
1689 * Prepare for redo of a normal command.
1690 */
1691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001692prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693{
1694 prep_redo(cap->oap->regname, cap->count0,
1695 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
1696}
1697
1698/*
1699 * Prepare for redo of any command.
1700 * Note that only the last argument can be a multi-byte char.
1701 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001702 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001703prep_redo(
1704 int regname,
1705 long num,
1706 int cmd1,
1707 int cmd2,
1708 int cmd3,
1709 int cmd4,
1710 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001712 prep_redo_num2(regname, num, cmd1, cmd2, 0L, cmd3, cmd4, cmd5);
1713}
1714
1715/*
1716 * Prepare for redo of any command with extra count after "cmd2".
1717 */
1718 void
1719prep_redo_num2(
1720 int regname,
1721 long num1,
1722 int cmd1,
1723 int cmd2,
1724 long num2,
1725 int cmd3,
1726 int cmd4,
1727 int cmd5)
1728{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 ResetRedobuff();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001730 if (regname != 0) // yank from specified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 {
1732 AppendCharToRedobuff('"');
1733 AppendCharToRedobuff(regname);
1734 }
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001735 if (num1 != 0)
1736 AppendNumberToRedobuff(num1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (cmd1 != NUL)
1738 AppendCharToRedobuff(cmd1);
1739 if (cmd2 != NUL)
1740 AppendCharToRedobuff(cmd2);
Bram Moolenaar2228cd72021-11-22 14:16:08 +00001741 if (num2 != 0)
1742 AppendNumberToRedobuff(num2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (cmd3 != NUL)
1744 AppendCharToRedobuff(cmd3);
1745 if (cmd4 != NUL)
1746 AppendCharToRedobuff(cmd4);
1747 if (cmd5 != NUL)
1748 AppendCharToRedobuff(cmd5);
1749}
1750
1751/*
1752 * check for operator active and clear it
1753 *
1754 * return TRUE if operator was active
1755 */
1756 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001757checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
1759 if (oap->op_type == OP_NOP)
1760 return FALSE;
1761 clearopbeep(oap);
1762 return TRUE;
1763}
1764
1765/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00001766 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00001768 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 */
1770 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001771checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001773 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 return FALSE;
1775 clearopbeep(oap);
1776 return TRUE;
1777}
1778
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001780clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 oap->op_type = OP_NOP;
1783 oap->regname = 0;
1784 oap->motion_force = NUL;
1785 oap->use_reg_one = FALSE;
Bram Moolenaar21492742021-06-04 21:57:57 +02001786 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787}
1788
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02001789 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001790clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791{
1792 clearop(oap);
1793 beep_flush();
1794}
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796/*
1797 * Remove the shift modifier from a special key.
1798 */
1799 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001800unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
1802 switch (cap->cmdchar)
1803 {
1804 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
1805 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
1806 case K_S_UP: cap->cmdchar = K_UP; break;
1807 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
1808 case K_S_HOME: cap->cmdchar = K_HOME; break;
1809 case K_S_END: cap->cmdchar = K_END; break;
1810 }
1811 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
1812}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001814/*
1815 * If the mode is currently displayed clear the command line or update the
1816 * command displayed.
1817 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02001818 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001819may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001820{
1821 if (mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001822 clear_cmdline = TRUE; // unshow visual mode later
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001823#ifdef FEAT_CMDL_INFO
1824 else
1825 clear_showcmd();
1826#endif
1827}
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829#if defined(FEAT_CMDL_INFO) || defined(PROTO)
1830/*
1831 * Routines for displaying a partly typed command
1832 */
1833
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001834#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835static char_u showcmd_buf[SHOWCMD_BUFLEN];
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001836static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; // For push_showcmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837static int showcmd_is_clear = TRUE;
1838static int showcmd_visual = FALSE;
1839
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01001840static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
1842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001843clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
1845 if (!p_sc)
1846 return;
1847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (VIsual_active && !char_avail())
1849 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001850 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 long lines;
1852 colnr_T leftcol, rightcol;
1853 linenr_T top, bot;
1854
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001855 // Show the size of the Visual area.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001856 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 {
1858 top = VIsual.lnum;
1859 bot = curwin->w_cursor.lnum;
1860 }
1861 else
1862 {
1863 top = curwin->w_cursor.lnum;
1864 bot = VIsual.lnum;
1865 }
1866# ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001867 // Include closed folds as a whole.
Bram Moolenaarcde88542015-08-11 19:14:00 +02001868 (void)hasFolding(top, &top, NULL);
1869 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870# endif
1871 lines = bot - top + 1;
1872
1873 if (VIsual_mode == Ctrl_V)
1874 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001875# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001876 char_u *saved_sbr = p_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001877 char_u *saved_w_sbr = curwin->w_p_sbr;
Bram Moolenaar81d00072009-04-29 15:41:40 +00001878
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001879 // Make 'sbr' empty for a moment to get the correct size.
Bram Moolenaar81d00072009-04-29 15:41:40 +00001880 p_sbr = empty_option;
Bram Moolenaaree857022019-11-09 23:26:40 +01001881 curwin->w_p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001884# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00001885 p_sbr = saved_sbr;
Bram Moolenaaree857022019-11-09 23:26:40 +01001886 curwin->w_p_sbr = saved_w_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02001887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
1889 (long)(rightcol - leftcol + 1));
1890 }
1891 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
1892 sprintf((char *)showcmd_buf, "%ld", lines);
1893 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001894 {
1895 char_u *s, *e;
1896 int l;
1897 int bytes = 0;
1898 int chars = 0;
1899
1900 if (cursor_bot)
1901 {
1902 s = ml_get_pos(&VIsual);
1903 e = ml_get_cursor();
1904 }
1905 else
1906 {
1907 s = ml_get_cursor();
1908 e = ml_get_pos(&VIsual);
1909 }
1910 while ((*p_sel != 'e') ? s <= e : s < e)
1911 {
1912 l = (*mb_ptr2len)(s);
1913 if (l == 0)
1914 {
1915 ++bytes;
1916 ++chars;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001917 break; // end of line
Bram Moolenaarf91787c2010-07-17 12:47:16 +02001918 }
1919 bytes += l;
1920 ++chars;
1921 s += l;
1922 }
1923 if (bytes == chars)
1924 sprintf((char *)showcmd_buf, "%d", chars);
1925 else
1926 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
1927 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001928 showcmd_buf[SHOWCMD_COLS] = NUL; // truncate
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 showcmd_visual = TRUE;
1930 }
1931 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 showcmd_buf[0] = NUL;
1934 showcmd_visual = FALSE;
1935
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001936 // Don't actually display something if there is nothing to clear.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (showcmd_is_clear)
1938 return;
1939 }
1940
1941 display_showcmd();
1942}
1943
1944/*
1945 * Add 'c' to string of shown command chars.
1946 * Return TRUE if output has been written (and setcursor() has been called).
1947 */
1948 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001949add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
1951 char_u *p;
1952 int old_len;
1953 int extra_len;
1954 int overflow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 int i;
1956 static int ignore[] =
1957 {
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001958#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
1960 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaara1cb1d12019-10-17 23:00:07 +02001961#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001962 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001963 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
1965 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001966 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001968 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 0
1970 };
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
Bram Moolenaar09df3122006-01-23 22:23:09 +00001972 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 return FALSE;
1974
1975 if (showcmd_visual)
1976 {
1977 showcmd_buf[0] = NUL;
1978 showcmd_visual = FALSE;
1979 }
1980
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01001981 // Ignore keys that are scrollbar updates and mouse clicks
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (IS_SPECIAL(c))
1983 for (i = 0; ignore[i] != 0; ++i)
1984 if (ignore[i] == c)
1985 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
1987 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01001988 if (*p == ' ')
1989 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 old_len = (int)STRLEN(showcmd_buf);
1991 extra_len = (int)STRLEN(p);
1992 overflow = old_len + extra_len - SHOWCMD_COLS;
1993 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00001994 mch_memmove(showcmd_buf, showcmd_buf + overflow,
1995 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 STRCAT(showcmd_buf, p);
1997
1998 if (char_avail())
1999 return FALSE;
2000
2001 display_showcmd();
2002
2003 return TRUE;
2004}
2005
2006 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002007add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008{
2009 if (!add_to_showcmd(c))
2010 setcursor();
2011}
2012
2013/*
2014 * Delete 'len' characters from the end of the shown command.
2015 */
2016 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002017del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018{
2019 int old_len;
2020
2021 if (!p_sc)
2022 return;
2023
2024 old_len = (int)STRLEN(showcmd_buf);
2025 if (len > old_len)
2026 len = old_len;
2027 showcmd_buf[old_len - len] = NUL;
2028
2029 if (!char_avail())
2030 display_showcmd();
2031}
2032
2033/*
2034 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2035 * something and there is a partial mapping.
2036 */
2037 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002038push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
2040 if (p_sc)
2041 STRCPY(old_showcmd_buf, showcmd_buf);
2042}
2043
2044 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002045pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
2047 if (!p_sc)
2048 return;
2049
2050 STRCPY(showcmd_buf, old_showcmd_buf);
2051
2052 display_showcmd();
2053}
2054
2055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002056display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057{
2058 int len;
2059
2060 cursor_off();
2061
2062 len = (int)STRLEN(showcmd_buf);
2063 if (len == 0)
2064 showcmd_is_clear = TRUE;
2065 else
2066 {
2067 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2068 showcmd_is_clear = FALSE;
2069 }
2070
2071 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002072 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2073 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 */
2075 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2076
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002077 setcursor(); // put cursor back where it belongs
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078}
2079#endif
2080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081/*
2082 * When "check" is FALSE, prepare for commands that scroll the window.
2083 * When "check" is TRUE, take care of scroll-binding after the window has
2084 * scrolled. Called from normal_cmd() and edit().
2085 */
2086 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002087do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
2089 static win_T *old_curwin = NULL;
2090 static linenr_T old_topline = 0;
2091#ifdef FEAT_DIFF
2092 static int old_topfill = 0;
2093#endif
2094 static buf_T *old_buf = NULL;
2095 static colnr_T old_leftcol = 0;
2096
2097 if (check && curwin->w_p_scb)
2098 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002099 // If a ":syncbind" command was just used, don't scroll, only reset
2100 // the values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 if (did_syncbind)
2102 did_syncbind = FALSE;
2103 else if (curwin == old_curwin)
2104 {
2105 /*
2106 * Synchronize other windows, as necessary according to
2107 * 'scrollbind'. Don't do this after an ":edit" command, except
2108 * when 'diff' is set.
2109 */
2110 if ((curwin->w_buffer == old_buf
2111#ifdef FEAT_DIFF
2112 || curwin->w_p_diff
2113#endif
2114 )
2115 && (curwin->w_topline != old_topline
2116#ifdef FEAT_DIFF
2117 || curwin->w_topfill != old_topfill
2118#endif
2119 || curwin->w_leftcol != old_leftcol))
2120 {
2121 check_scrollbind(curwin->w_topline - old_topline,
2122 (long)(curwin->w_leftcol - old_leftcol));
2123 }
2124 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002125 else if (vim_strchr(p_sbo, 'j')) // jump flag set in 'scrollopt'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
2127 /*
2128 * When switching between windows, make sure that the relative
2129 * vertical offset is valid for the new window. The relative
2130 * offset is invalid whenever another 'scrollbind' window has
2131 * scrolled to a point that would force the current window to
2132 * scroll past the beginning or end of its buffer. When the
2133 * resync is performed, some of the other 'scrollbind' windows may
2134 * need to jump so that the current window's relative position is
2135 * visible on-screen.
2136 */
2137 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2138 }
2139 curwin->w_scbind_pos = curwin->w_topline;
2140 }
2141
2142 old_curwin = curwin;
2143 old_topline = curwin->w_topline;
2144#ifdef FEAT_DIFF
2145 old_topfill = curwin->w_topfill;
2146#endif
2147 old_buf = curwin->w_buffer;
2148 old_leftcol = curwin->w_leftcol;
2149}
2150
2151/*
2152 * Synchronize any windows that have "scrollbind" set, based on the
2153 * number of rows by which the current window has changed
2154 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2155 */
2156 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002157check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158{
2159 int want_ver;
2160 int want_hor;
2161 win_T *old_curwin = curwin;
2162 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 int old_VIsual_select = VIsual_select;
2164 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 colnr_T tgt_leftcol = curwin->w_leftcol;
2166 long topline;
2167 long y;
2168
2169 /*
2170 * check 'scrollopt' string for vertical and horizontal scroll options
2171 */
2172 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2173#ifdef FEAT_DIFF
2174 want_ver |= old_curwin->w_p_diff;
2175#endif
2176 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2177
2178 /*
2179 * loop through the scrollbound windows and scroll accordingly
2180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002182 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
2184 curbuf = curwin->w_buffer;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002185 // skip original window and windows with 'noscrollbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 if (curwin != old_curwin && curwin->w_p_scb)
2187 {
2188 /*
2189 * do the vertical scroll
2190 */
2191 if (want_ver)
2192 {
2193#ifdef FEAT_DIFF
2194 if (old_curwin->w_p_diff && curwin->w_p_diff)
2195 {
2196 diff_set_topline(old_curwin, curwin);
2197 }
2198 else
2199#endif
2200 {
2201 curwin->w_scbind_pos += topline_diff;
2202 topline = curwin->w_scbind_pos;
2203 if (topline > curbuf->b_ml.ml_line_count)
2204 topline = curbuf->b_ml.ml_line_count;
2205 if (topline < 1)
2206 topline = 1;
2207
2208 y = topline - curwin->w_topline;
2209 if (y > 0)
2210 scrollup(y, FALSE);
2211 else
2212 scrolldown(-y, FALSE);
2213 }
2214
2215 redraw_later(VALID);
2216 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
2219
2220 /*
2221 * do the horizontal scroll
2222 */
2223 if (want_hor && curwin->w_leftcol != tgt_leftcol)
2224 {
2225 curwin->w_leftcol = tgt_leftcol;
2226 leftcol_changed();
2227 }
2228 }
2229 }
2230
2231 /*
2232 * reset current-window
2233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 VIsual_select = old_VIsual_select;
2235 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 curwin = old_curwin;
2237 curbuf = old_curbuf;
2238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240/*
2241 * Command character that's ignored.
2242 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02002243 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002246nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002248 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249}
2250
2251/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00002252 * Command character that doesn't do anything, but unlike nv_ignore() does
2253 * start edit(). Used for "startinsert" executed while starting up.
2254 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00002255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002256nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00002257{
2258}
2259
2260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 * Command character doesn't exist.
2262 */
2263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002264nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266 clearopbeep(cap->oap);
2267}
2268
2269/*
2270 * <Help> and <F1> commands.
2271 */
2272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002273nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 if (!checkclearopq(cap->oap))
2276 ex_help(NULL);
2277}
2278
2279/*
2280 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
2281 */
2282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002283nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
Bram Moolenaarf2732452018-06-03 14:47:35 +02002285#ifdef FEAT_JOB_CHANNEL
2286 if (bt_prompt(curbuf) && !prompt_curpos_editable())
2287 clearopbeep(cap->oap);
2288 else
2289#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002290 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002291 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01002292 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01002293 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
2294 op_addsub(cap->oap, cap->count1, cap->arg);
2295 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02002296 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01002297 else if (VIsual_active)
2298 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02002299 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01002300 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301}
2302
2303/*
2304 * CTRL-F, CTRL-B, etc: Scroll page up or down.
2305 */
2306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002307nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002310 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002311 if (mod_mask & MOD_MASK_CTRL)
2312 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002313 // <C-PageUp>: tab page back; <C-PageDown>: tab page forward
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002314 if (cap->arg == BACKWARD)
2315 goto_tabpage(-(int)cap->count1);
2316 else
2317 goto_tabpage((int)cap->count0);
2318 }
2319 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02002320 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322}
2323
2324/*
2325 * Implementation of "gd" and "gD" command.
2326 */
2327 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002328nv_gd(
2329 oparg_T *oap,
2330 int nchar,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002331 int thisblock) // 1 for "1gd" and "1gD"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
2333 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 char_u *ptr;
2335
Bram Moolenaard9d30582005-05-18 22:10:28 +00002336 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02002337 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
Bram Moolenaar0c711142021-11-12 10:30:04 +00002338 == FAIL)
2339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 clearopbeep(oap);
Bram Moolenaar0c711142021-11-12 10:30:04 +00002341 }
2342 else
2343 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002344#ifdef FEAT_FOLDING
Bram Moolenaar0c711142021-11-12 10:30:04 +00002345 if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
2346 foldOpenCursor();
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002347#endif
Bram Moolenaar0c711142021-11-12 10:30:04 +00002348 // clear any search statistics
2349 if (messaging() && !msg_silent && !shortmess(SHM_SEARCHCOUNT))
2350 clear_cmdline = TRUE;
2351 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002352}
2353
2354/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02002355 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
2356 * otherwise.
2357 */
2358 static int
2359is_ident(char_u *line, int offset)
2360{
2361 int i;
2362 int incomment = FALSE;
2363 int instring = 0;
2364 int prev = 0;
2365
2366 for (i = 0; i < offset && line[i] != NUL; i++)
2367 {
2368 if (instring != 0)
2369 {
2370 if (prev != '\\' && line[i] == instring)
2371 instring = 0;
2372 }
2373 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
2374 {
2375 instring = line[i];
2376 }
2377 else
2378 {
2379 if (incomment)
2380 {
2381 if (prev == '*' && line[i] == '/')
2382 incomment = FALSE;
2383 }
2384 else if (prev == '/' && line[i] == '*')
2385 {
2386 incomment = TRUE;
2387 }
2388 else if (prev == '/' && line[i] == '/')
2389 {
2390 return FALSE;
2391 }
2392 }
2393
2394 prev = line[i];
2395 }
2396
2397 return incomment == FALSE && instring == 0;
2398}
2399
2400/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002401 * Search for variable declaration of "ptr[len]".
2402 * When "locally" is TRUE in the current function ("gd"), otherwise in the
2403 * current file ("gD").
2404 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002405 * Return FAIL when not found.
2406 */
2407 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002408find_decl(
2409 char_u *ptr,
2410 int len,
2411 int locally,
2412 int thisblock,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002413 int flags_arg) // flags passed to searchit()
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002414{
2415 char_u *pat;
2416 pos_T old_pos;
2417 pos_T par_pos;
2418 pos_T found_pos;
2419 int t;
2420 int save_p_ws;
2421 int save_p_scs;
2422 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002423 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002424 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002425 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002426
2427 if ((pat = alloc(len + 7)) == NULL)
2428 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00002429
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002430 // Put "\V" before the pattern to avoid that the special meaning of "."
2431 // and "~" causes trouble.
Bram Moolenaard9d30582005-05-18 22:10:28 +00002432 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
2433 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 old_pos = curwin->w_cursor;
2435 save_p_ws = p_ws;
2436 save_p_scs = p_scs;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002437 p_ws = FALSE; // don't wrap around end of file now
2438 p_scs = FALSE; // don't switch ignorecase off now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 /*
2441 * With "gD" go to line 1.
2442 * With "gd" Search back for the start of the current function, then go
2443 * back until a blank line. If this fails go to line 1.
2444 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002447 setpcmark(); // Set in findpar() otherwise
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002449 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451 else
2452 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00002453 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
2455 --curwin->w_cursor.lnum;
2456 }
2457 curwin->w_cursor.col = 0;
2458
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002459 // Search forward for the identifier, ignore comment lines.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002460 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002461 for (;;)
2462 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002463 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002464 pat, 1L, searchflags, RE_LAST, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002465 if (curwin->w_cursor.lnum >= old_pos.lnum)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002466 t = FAIL; // match after start is failure too
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002467
Bram Moolenaar0fd92892006-03-09 22:27:48 +00002468 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002469 {
2470 pos_T *pos;
2471
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002472 // Check that the block the match is in doesn't end before the
2473 // position where we started the search from.
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002474 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
2475 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
2476 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02002477 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002478 // There can't be a useful match before the end of this block.
2479 // Skip to the end.
Bram Moolenaar60402d62017-04-20 18:54:50 +02002480 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002481 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02002482 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002483 }
2484
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002485 if (t == FAIL)
2486 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002487 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002488 if (found_pos.lnum != 0)
2489 {
2490 curwin->w_cursor = found_pos;
2491 t = OK;
2492 }
2493 break;
2494 }
Bram Moolenaar81340392012-06-06 16:12:59 +02002495 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002496 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002497 // Ignore this line, continue at start of next line.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002498 ++curwin->w_cursor.lnum;
2499 curwin->w_cursor.col = 0;
2500 continue;
2501 }
Bram Moolenaar226630a2016-10-08 19:21:31 +02002502 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
2503
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002504 // If the current position is not a valid identifier and a previous
2505 // match is present, favor that one instead.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002506 if (!valid && found_pos.lnum != 0)
2507 {
2508 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002509 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02002510 }
2511
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002512 // Global search: use first valid match found
Bram Moolenaar226630a2016-10-08 19:21:31 +02002513 if (valid && !locally)
2514 break;
2515 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002516 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002517 // If we previously found a valid position, use it.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002518 if (found_pos.lnum != 0)
2519 curwin->w_cursor = found_pos;
2520 break;
2521 }
2522
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002523 // For finding a local variable and the match is before the "{" or
2524 // inside a comment, continue searching. For K&R style function
2525 // declarations this skips the function header without types.
Bram Moolenaar226630a2016-10-08 19:21:31 +02002526 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002527 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02002528 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02002529 found_pos = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002530 // Remove SEARCH_START from flags to avoid getting stuck at one
2531 // position.
Bram Moolenaar23c60f22016-06-15 22:03:48 +02002532 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002534
2535 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002537 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 curwin->w_cursor = old_pos;
2539 }
2540 else
2541 {
2542 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002543 // "n" searches forward now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 reset_search_dir();
2545 }
2546
2547 vim_free(pat);
2548 p_ws = save_p_ws;
2549 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002550
2551 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552}
2553
2554/*
2555 * Move 'dist' lines in direction 'dir', counting lines by *screen*
2556 * lines rather than lines in the file.
2557 * 'dist' must be positive.
2558 *
2559 * Return OK if able to move cursor, FAIL otherwise.
2560 */
2561 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002562nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 int linelen = linetabsize(ml_get_curline());
2565 int retval = OK;
2566 int atend = FALSE;
2567 int n;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002568 int col_off1; // margin offset for first screen line
2569 int col_off2; // margin offset for wrapped screen line
2570 int width1; // text width for first screen line
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002571 int width2; // text width for wrapped screen line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02002574 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576 col_off1 = curwin_col_off();
2577 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02002578 width1 = curwin->w_width - col_off1;
2579 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01002580 if (width2 == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002581 width2 = 1; // avoid divide by zero
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /*
2586 * Instead of sticking at the last character of the buffer line we
2587 * try to stick in the last column of the screen.
2588 */
2589 if (curwin->w_curswant == MAXCOL)
2590 {
2591 atend = TRUE;
2592 validate_virtcol();
2593 if (width1 <= 0)
2594 curwin->w_curswant = 0;
2595 else
2596 {
2597 curwin->w_curswant = width1 - 1;
2598 if (curwin->w_virtcol > curwin->w_curswant)
2599 curwin->w_curswant += ((curwin->w_virtcol
2600 - curwin->w_curswant - 1) / width2 + 1) * width2;
2601 }
2602 }
2603 else
2604 {
2605 if (linelen > width1)
2606 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2607 else
2608 n = width1;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002609 if (curwin->w_curswant >= (colnr_T)n)
2610 curwin->w_curswant = n - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
2612
2613 while (dist--)
2614 {
2615 if (dir == BACKWARD)
2616 {
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002617 if ((long)curwin->w_curswant >= width1
2618#ifdef FEAT_FOLDING
2619 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2620#endif
2621 )
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002622 // Move back within the line. This can give a negative value
2623 // for w_curswant if width1 < width2 (with cpoptions+=n),
2624 // which will get clipped to column 0.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 curwin->w_curswant -= width2;
2626 else
2627 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002628 // to previous line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002630 // Move to the start of a closed fold. Don't do that when
2631 // 'foldopen' contains "all": it will open in a moment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if (!(fdo_flags & FDO_ALL))
2633 (void)hasFolding(curwin->w_cursor.lnum,
2634 &curwin->w_cursor.lnum, NULL);
2635#endif
Bram Moolenaare71996b2021-01-21 17:03:07 +01002636 if (curwin->w_cursor.lnum == 1)
2637 {
2638 retval = FAIL;
2639 break;
2640 }
2641 --curwin->w_cursor.lnum;
2642
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 linelen = linetabsize(ml_get_curline());
2644 if (linelen > width1)
2645 curwin->w_curswant += (((linelen - width1 - 1) / width2)
2646 + 1) * width2;
2647 }
2648 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002649 else // dir == FORWARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
2651 if (linelen > width1)
2652 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
2653 else
2654 n = width1;
Bram Moolenaar3c49e742021-04-04 21:26:04 +02002655 if (curwin->w_curswant + width2 < (colnr_T)n
2656#ifdef FEAT_FOLDING
2657 && !hasFolding(curwin->w_cursor.lnum, NULL, NULL)
2658#endif
2659 )
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002660 // move forward within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 curwin->w_curswant += width2;
2662 else
2663 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002664 // to next line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002666 // Move to the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 (void)hasFolding(curwin->w_cursor.lnum, NULL,
2668 &curwin->w_cursor.lnum);
2669#endif
2670 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
2671 {
2672 retval = FAIL;
2673 break;
2674 }
2675 curwin->w_cursor.lnum++;
2676 curwin->w_curswant %= width2;
Bram Moolenaarceba3dd2019-10-12 16:12:54 +02002677 // Check if the cursor has moved below the number display
2678 // when width1 < width2 (with cpoptions+=n). Subtract width2
2679 // to get a negative value for w_curswant, which will get
2680 // clipped to column 0.
2681 if (curwin->w_curswant >= width1)
2682 curwin->w_curswant -= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02002683 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 }
2685 }
2686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01002689 if (virtual_active() && atend)
2690 coladvance(MAXCOL);
2691 else
2692 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
2695 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02002696 colnr_T virtcol;
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002697 int c;
Bram Moolenaar773b1582014-08-29 14:20:51 +02002698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 /*
2700 * Check for landing on a character that got split at the end of the
2701 * last line. We want to advance a screenline, not end up in the same
2702 * screenline or move two screenlines.
2703 */
2704 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02002705 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002706#if defined(FEAT_LINEBREAK)
Bram Moolenaaree857022019-11-09 23:26:40 +01002707 if (virtcol > (colnr_T)width1 && *get_showbreak_value(curwin) != NUL)
2708 virtcol -= vim_strsize(get_showbreak_value(curwin));
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002709#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02002710
Christian Brabandtaaec1d42021-11-04 13:28:29 +00002711 c = (*mb_ptr2char)(ml_get_cursor());
2712 if (dir == FORWARD && virtcol < curwin->w_curswant
2713 && (curwin->w_curswant <= (colnr_T)width1)
2714 && !vim_isprintc(c) && c > 255)
2715 oneright();
2716
Bram Moolenaar773b1582014-08-29 14:20:51 +02002717 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 && (curwin->w_curswant < (colnr_T)width1
2719 ? (curwin->w_curswant > (colnr_T)width1 / 2)
2720 : ((curwin->w_curswant - width1) % width2
2721 > (colnr_T)width2 / 2)))
2722 --curwin->w_cursor.col;
2723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 if (atend)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002726 curwin->w_curswant = MAXCOL; // stick in the last column
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 return retval;
2729}
2730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731/*
2732 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
2733 * cap->arg must be TRUE for CTRL-E.
2734 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002735 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002736nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737{
2738 if (!checkclearop(cap->oap))
2739 scroll_redraw(cap->arg, cap->count1);
2740}
2741
2742/*
2743 * Scroll "count" lines up or down, and redraw.
2744 */
2745 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002746scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 linenr_T prev_topline = curwin->w_topline;
2749#ifdef FEAT_DIFF
2750 int prev_topfill = curwin->w_topfill;
2751#endif
2752 linenr_T prev_lnum = curwin->w_cursor.lnum;
2753
2754 if (up)
2755 scrollup(count, TRUE);
2756 else
2757 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002758 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002760 // Adjust the cursor position for 'scrolloff'. Mark w_topline as
2761 // valid, otherwise the screen jumps back at the end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 cursor_correct();
2763 check_cursor_moved(curwin);
2764 curwin->w_valid |= VALID_TOPLINE;
2765
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002766 // If moved back to where we were, at least move the cursor, otherwise
2767 // we get stuck at one position. Don't move the cursor up if the
2768 // first line of the buffer is already on the screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 while (curwin->w_topline == prev_topline
2770#ifdef FEAT_DIFF
2771 && curwin->w_topfill == prev_topfill
2772#endif
2773 )
2774 {
2775 if (up)
2776 {
2777 if (curwin->w_cursor.lnum > prev_lnum
2778 || cursor_down(1L, FALSE) == FAIL)
2779 break;
2780 }
2781 else
2782 {
2783 if (curwin->w_cursor.lnum < prev_lnum
2784 || prev_topline == 1L
2785 || cursor_up(1L, FALSE) == FAIL)
2786 break;
2787 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002788 // Mark w_topline as valid, otherwise the screen jumps back at the
2789 // end of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 check_cursor_moved(curwin);
2791 curwin->w_valid |= VALID_TOPLINE;
2792 }
2793 }
2794 if (curwin->w_cursor.lnum != prev_lnum)
2795 coladvance(curwin->w_curswant);
2796 redraw_later(VALID);
2797}
2798
2799/*
2800 * Commands that start with "z".
2801 */
2802 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002803nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
2805 long n;
2806 colnr_T col;
2807 int nchar = cap->nchar;
2808#ifdef FEAT_FOLDING
2809 long old_fdl = curwin->w_p_fdl;
2810 int old_fen = curwin->w_p_fen;
2811#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002812#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00002813 int undo = FALSE;
2814#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01002815 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
2817 if (VIM_ISDIGIT(nchar))
2818 {
2819 /*
2820 * "z123{nchar}": edit the count before obtaining {nchar}
2821 */
2822 if (checkclearop(cap->oap))
2823 return;
2824 n = nchar - '0';
2825 for (;;)
2826 {
2827#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002828 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829#endif
2830 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002831 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00002832 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 --no_mapping;
2835 --allow_keys;
2836#ifdef FEAT_CMDL_INFO
2837 (void)add_to_showcmd(nchar);
2838#endif
2839 if (nchar == K_DEL || nchar == K_KDEL)
2840 n /= 10;
2841 else if (VIM_ISDIGIT(nchar))
2842 n = n * 10 + (nchar - '0');
2843 else if (nchar == CAR)
2844 {
2845#ifdef FEAT_GUI
2846 need_mouse_correct = TRUE;
2847#endif
2848 win_setheight((int)n);
2849 break;
2850 }
2851 else if (nchar == 'l'
2852 || nchar == 'h'
2853 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00002854 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {
2856 cap->count1 = n ? n * cap->count1 : cap->count1;
2857 goto dozet;
2858 }
2859 else
2860 {
2861 clearopbeep(cap->oap);
2862 break;
2863 }
2864 }
2865 cap->oap->op_type = OP_NOP;
2866 return;
2867 }
2868
2869dozet:
2870 if (
2871#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002872 // "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
2873 // and "zC" only in Visual mode. "zj" and "zk" are motion
2874 // commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 cap->nchar != 'f' && cap->nchar != 'F'
2876 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
2877 && cap->nchar != 'j' && cap->nchar != 'k'
2878 &&
2879#endif
2880 checkclearop(cap->oap))
2881 return;
2882
2883 /*
2884 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
2885 * If line number given, set cursor.
2886 */
2887 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
2888 && cap->count0
2889 && cap->count0 != curwin->w_cursor.lnum)
2890 {
2891 setpcmark();
2892 if (cap->count0 > curbuf->b_ml.ml_line_count)
2893 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2894 else
2895 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002896 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898
2899 switch (nchar)
2900 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002901 // "z+", "z<CR>" and "zt": put cursor at top of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 case '+':
2903 if (cap->count0 == 0)
2904 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002905 // No count given: put cursor at the line below screen
2906 validate_botline(); // make sure w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
2908 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
2909 else
2910 curwin->w_cursor.lnum = curwin->w_botline;
2911 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002912 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 case NL:
2914 case CAR:
2915 case K_KENTER:
2916 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002917 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
2919 case 't': scroll_cursor_top(0, TRUE);
2920 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002921 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 break;
2923
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002924 // "z." and "zz": put cursor in middle of screen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 case '.': beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002926 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927
2928 case 'z': scroll_cursor_halfway(TRUE);
2929 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002930 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 break;
2932
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002933 // "z^", "z-" and "zb": put cursor at bottom of screen
2934 case '^': // Strange Vi behavior: <count>z^ finds line at top of window
2935 // when <count> is at bottom of window, and puts that one at
2936 // bottom of window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 if (cap->count0 != 0)
2938 {
2939 scroll_cursor_bot(0, TRUE);
2940 curwin->w_cursor.lnum = curwin->w_topline;
2941 }
2942 else if (curwin->w_topline == 1)
2943 curwin->w_cursor.lnum = 1;
2944 else
2945 curwin->w_cursor.lnum = curwin->w_topline - 1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002946 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 case '-':
2948 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002949 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951 case 'b': scroll_cursor_bot(0, TRUE);
2952 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01002953 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 break;
2955
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002956 // "zH" - scroll screen right half-page
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02002958 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002959 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002961 // "zh" - scroll screen to the right
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 case 'h':
2963 case K_LEFT:
2964 if (!curwin->w_p_wrap)
2965 {
2966 if ((colnr_T)cap->count1 > curwin->w_leftcol)
2967 curwin->w_leftcol = 0;
2968 else
2969 curwin->w_leftcol -= (colnr_T)cap->count1;
2970 leftcol_changed();
2971 }
2972 break;
2973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002974 // "zL" - scroll screen left half-page
Bram Moolenaar02631462017-09-22 15:20:32 +02002975 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002976 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002978 // "zl" - scroll screen to the left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 case 'l':
2980 case K_RIGHT:
2981 if (!curwin->w_p_wrap)
2982 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002983 // scroll the window left
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 curwin->w_leftcol += (colnr_T)cap->count1;
2985 leftcol_changed();
2986 }
2987 break;
2988
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002989 // "zs" - scroll screen, cursor at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 case 's': if (!curwin->w_p_wrap)
2991 {
2992#ifdef FEAT_FOLDING
2993 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01002994 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 else
2996#endif
2997 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01002998 if ((long)col > siso)
2999 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 else
3001 col = 0;
3002 if (curwin->w_leftcol != col)
3003 {
3004 curwin->w_leftcol = col;
3005 redraw_later(NOT_VALID);
3006 }
3007 }
3008 break;
3009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003010 // "ze" - scroll screen, cursor at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 case 'e': if (!curwin->w_p_wrap)
3012 {
3013#ifdef FEAT_FOLDING
3014 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003015 col = 0; // like the cursor is in col 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 else
3017#endif
3018 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02003019 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01003020 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 col = 0;
3022 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01003023 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (curwin->w_leftcol != col)
3025 {
3026 curwin->w_leftcol = col;
3027 redraw_later(NOT_VALID);
3028 }
3029 }
3030 break;
3031
Christian Brabandt2fa93842021-05-30 22:17:25 +02003032 // "zp", "zP" in block mode put without addind trailing spaces
3033 case 'P':
3034 case 'p': nv_put(cap);
3035 break;
Christian Brabandt544a38e2021-06-10 19:39:11 +02003036 // "zy" Yank without trailing spaces
3037 case 'y': nv_operator(cap);
3038 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003040 // "zF": create fold command
3041 // "zf": create fold operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 case 'F':
3043 case 'f': if (foldManualAllowed(TRUE))
3044 {
3045 cap->nchar = 'f';
3046 nv_operator(cap);
3047 curwin->w_p_fen = TRUE;
3048
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003049 // "zF" is like "zfzf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3051 {
3052 nv_operator(cap);
3053 finish_op = TRUE;
3054 }
3055 }
3056 else
3057 clearopbeep(cap->oap);
3058 break;
3059
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003060 // "zd": delete fold at cursor
3061 // "zD": delete fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 case 'd':
3063 case 'D': if (foldManualAllowed(FALSE))
3064 {
3065 if (VIsual_active)
3066 nv_operator(cap);
3067 else
3068 deleteFold(curwin->w_cursor.lnum,
3069 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3070 }
3071 break;
3072
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003073 // "zE": erase all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 case 'E': if (foldmethodIsManual(curwin))
3075 {
3076 clearFolding(curwin);
3077 changed_window_setting();
3078 }
3079 else if (foldmethodIsMarker(curwin))
3080 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3081 TRUE, FALSE);
3082 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003083 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 break;
3085
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003086 // "zn": fold none: reset 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 case 'n': curwin->w_p_fen = FALSE;
3088 break;
3089
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003090 // "zN": fold Normal: set 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 case 'N': curwin->w_p_fen = TRUE;
3092 break;
3093
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003094 // "zi": invert folding: toggle 'foldenable'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3096 break;
3097
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003098 // "za": open closed fold or close open fold at cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3100 openFold(curwin->w_cursor.lnum, cap->count1);
3101 else
3102 {
3103 closeFold(curwin->w_cursor.lnum, cap->count1);
3104 curwin->w_p_fen = TRUE;
3105 }
3106 break;
3107
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003108 // "zA": open fold at cursor recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3110 openFoldRecurse(curwin->w_cursor.lnum);
3111 else
3112 {
3113 closeFoldRecurse(curwin->w_cursor.lnum);
3114 curwin->w_p_fen = TRUE;
3115 }
3116 break;
3117
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003118 // "zo": open fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 case 'o': if (VIsual_active)
3120 nv_operator(cap);
3121 else
3122 openFold(curwin->w_cursor.lnum, cap->count1);
3123 break;
3124
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003125 // "zO": open fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 case 'O': if (VIsual_active)
3127 nv_operator(cap);
3128 else
3129 openFoldRecurse(curwin->w_cursor.lnum);
3130 break;
3131
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003132 // "zc": close fold at cursor or Visual area
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 case 'c': if (VIsual_active)
3134 nv_operator(cap);
3135 else
3136 closeFold(curwin->w_cursor.lnum, cap->count1);
3137 curwin->w_p_fen = TRUE;
3138 break;
3139
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003140 // "zC": close fold recursively
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 case 'C': if (VIsual_active)
3142 nv_operator(cap);
3143 else
3144 closeFoldRecurse(curwin->w_cursor.lnum);
3145 curwin->w_p_fen = TRUE;
3146 break;
3147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003148 // "zv": open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 case 'v': foldOpenCursor();
3150 break;
3151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003152 // "zx": re-apply 'foldlevel' and open folds at the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003154 curwin->w_foldinvalid = TRUE; // recompute folds
3155 newFoldLevel(); // update right now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 foldOpenCursor();
3157 break;
3158
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003159 // "zX": undo manual opens/closes, re-apply 'foldlevel'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003161 curwin->w_foldinvalid = TRUE; // recompute folds
3162 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 break;
3164
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003165 // "zm": fold more
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003167 {
3168 curwin->w_p_fdl -= cap->count1;
3169 if (curwin->w_p_fdl < 0)
3170 curwin->w_p_fdl = 0;
3171 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003172 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 curwin->w_p_fen = TRUE;
3174 break;
3175
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003176 // "zM": close all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 case 'M': curwin->w_p_fdl = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003178 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 curwin->w_p_fen = TRUE;
3180 break;
3181
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003182 // "zr": reduce folding
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003183 case 'r': curwin->w_p_fdl += cap->count1;
3184 {
3185 int d = getDeepestNesting();
3186
3187 if (curwin->w_p_fdl >= d)
3188 curwin->w_p_fdl = d;
3189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 break;
3191
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003192 // "zR": open all folds
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 case 'R': curwin->w_p_fdl = getDeepestNesting();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003194 old_fdl = -1; // force an update
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 break;
3196
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003197 case 'j': // "zj" move to next fold downwards
3198 case 'k': // "zk" move to next fold upwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3200 cap->count1) == FAIL)
3201 clearopbeep(cap->oap);
3202 break;
3203
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003204#endif // FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003206#ifdef FEAT_SPELL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003207 case 'u': // "zug" and "zuw": undo "zg" and "zw"
Bram Moolenaard0131a82006-03-04 21:46:13 +00003208 ++no_mapping;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003209 ++allow_keys; // no mapping for nchar, but allow key codes
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003210 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003211 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003212 --no_mapping;
3213 --allow_keys;
3214#ifdef FEAT_CMDL_INFO
3215 (void)add_to_showcmd(nchar);
3216#endif
3217 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3218 {
3219 clearopbeep(cap->oap);
3220 break;
3221 }
3222 undo = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003223 // FALLTHROUGH
Bram Moolenaard0131a82006-03-04 21:46:13 +00003224
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003225 case 'g': // "zg": add good word to word list
3226 case 'w': // "zw": add wrong word to word list
3227 case 'G': // "zG": add good word to temp word list
3228 case 'W': // "zW": add wrong word to temp word list
Bram Moolenaarb765d632005-06-07 21:00:02 +00003229 {
3230 char_u *ptr = NULL;
3231 int len;
3232
3233 if (checkclearop(cap->oap))
3234 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003235 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3236 == FAIL)
3237 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003238 if (ptr == NULL)
3239 {
3240 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003241
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003242 // Find bad word under the cursor. When 'spell' is
3243 // off this fails and find_ident_under_cursor() is
3244 // used below.
Bram Moolenaar134bf072013-09-25 18:54:24 +02003245 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003246 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02003247 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003248 if (len != 0 && curwin->w_cursor.col <= pos.col)
3249 ptr = ml_get_pos(&curwin->w_cursor);
3250 curwin->w_cursor = pos;
3251 }
3252
Bram Moolenaarb765d632005-06-07 21:00:02 +00003253 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
3254 FIND_IDENT)) == 0)
3255 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02003256 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
3257 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00003258 (nchar == 'G' || nchar == 'W')
3259 ? 0 : (int)cap->count1,
3260 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003261 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00003262 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003263
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003264 case '=': // "z=": suggestions for a badly spelled word
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003265 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003266 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003267 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003268#endif
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 default: clearopbeep(cap->oap);
3271 }
3272
3273#ifdef FEAT_FOLDING
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003274 // Redraw when 'foldenable' changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 if (old_fen != curwin->w_p_fen)
3276 {
3277# ifdef FEAT_DIFF
3278 win_T *wp;
3279
3280 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
3281 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003282 // Adjust 'foldenable' in diff-synced windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 FOR_ALL_WINDOWS(wp)
3284 {
3285 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
3286 {
3287 wp->w_p_fen = curwin->w_p_fen;
3288 changed_window_setting_win(wp);
3289 }
3290 }
3291 }
3292# endif
3293 changed_window_setting();
3294 }
3295
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003296 // Redraw when 'foldlevel' changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (old_fdl != curwin->w_p_fdl)
3298 newFoldLevel();
3299#endif
3300}
3301
3302#ifdef FEAT_GUI
3303/*
3304 * Vertical scrollbar movement.
3305 */
3306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003307nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 if (cap->oap->op_type != OP_NOP)
3310 clearopbeep(cap->oap);
3311
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003312 // Even if an operator was pending, we still want to scroll
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 gui_do_scroll();
3314}
3315
3316/*
3317 * Horizontal scrollbar movement.
3318 */
3319 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003320nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 if (cap->oap->op_type != OP_NOP)
3323 clearopbeep(cap->oap);
3324
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003325 // Even if an operator was pending, we still want to scroll
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003326 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327}
3328#endif
3329
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003330#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003331/*
3332 * Click in GUI tab.
3333 */
3334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003335nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003336{
3337 if (cap->oap->op_type != OP_NOP)
3338 clearopbeep(cap->oap);
3339
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003340 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003341 goto_tabpage(current_tab);
3342}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003343
3344/*
3345 * Selected item in tab line menu.
3346 */
3347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003348nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003349{
3350 if (cap->oap->op_type != OP_NOP)
3351 clearopbeep(cap->oap);
3352
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003353 // Even if an operator was pending, we still want to jump tabs.
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003354 handle_tabmenu();
3355}
3356
3357/*
3358 * Handle selecting an item of the GUI tab line menu.
3359 * Used in Normal and Insert mode.
3360 */
3361 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003362handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003363{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003364 switch (current_tabmenu)
3365 {
3366 case TABLINE_MENU_CLOSE:
3367 if (current_tab == 0)
3368 do_cmdline_cmd((char_u *)"tabclose");
3369 else
3370 {
3371 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
3372 current_tab);
3373 do_cmdline_cmd(IObuff);
3374 }
3375 break;
3376
3377 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003378 if (current_tab == 0)
3379 do_cmdline_cmd((char_u *)"$tabnew");
3380 else
3381 {
3382 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
3383 current_tab - 1);
3384 do_cmdline_cmd(IObuff);
3385 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003386 break;
3387
3388 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01003389 if (current_tab == 0)
3390 do_cmdline_cmd((char_u *)"browse $tabnew");
3391 else
3392 {
3393 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
3394 current_tab - 1);
3395 do_cmdline_cmd(IObuff);
3396 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003397 break;
3398 }
3399}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003400#endif
3401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402/*
3403 * "Q" command.
3404 */
3405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003406nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407{
3408 /*
3409 * Ignore 'Q' in Visual mode, just give a beep.
3410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02003412 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003413 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 do_exmode(FALSE);
3415}
3416
3417/*
3418 * Handle a ":" command.
3419 */
3420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003421nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
Bram Moolenaar957cf672020-11-12 14:21:06 +01003423 int old_p_im;
3424 int cmd_result;
3425 int is_cmdkey = cap->cmdchar == K_COMMAND;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar957cf672020-11-12 14:21:06 +01003427 if (VIsual_active && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 nv_operator(cap);
3429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
3431 if (cap->oap->op_type != OP_NOP)
3432 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003433 // Using ":" as a movement is characterwise exclusive.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 cap->oap->motion_type = MCHAR;
3435 cap->oap->inclusive = FALSE;
3436 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01003437 else if (cap->count0 && !is_cmdkey)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003439 // translate "count:" into ":.,.+(count - 1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 stuffcharReadbuff('.');
3441 if (cap->count0 > 1)
3442 {
3443 stuffReadbuff((char_u *)",.+");
3444 stuffnumReadbuff((long)cap->count0 - 1L);
3445 }
3446 }
3447
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003448 // When typing, don't type below an old message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (KeyTyped)
3450 compute_cmdrow();
3451
3452 old_p_im = p_im;
3453
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003454 // get a command line and execute it
Bram Moolenaar957cf672020-11-12 14:21:06 +01003455 cmd_result = do_cmdline(NULL, is_cmdkey ? getcmdkeycmd : getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
3457
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003458 // If 'insertmode' changed, enter or exit Insert mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 if (p_im != old_p_im)
3460 {
3461 if (p_im)
3462 restart_edit = 'i';
3463 else
3464 restart_edit = 0;
3465 }
3466
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003467 if (cmd_result == FAIL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003468 // The Ex command failed, do not execute the operator.
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003469 clearop(cap->oap);
3470 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
3472 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02003473 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
3474 || did_emsg
3475 ))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003476 // The start of the operator has become invalid by the Ex command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 clearopbeep(cap->oap);
3478 }
3479}
3480
3481/*
3482 * Handle CTRL-G command.
3483 */
3484 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003485nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003487 if (VIsual_active) // toggle Selection/Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 {
3489 VIsual_select = !VIsual_select;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003490 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 showmode();
3492 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003493 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003494 // print full name if count given or :cd used
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 fileinfo((int)cap->count0, FALSE, TRUE);
3496}
3497
3498/*
3499 * Handle CTRL-H <Backspace> command.
3500 */
3501 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003502nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (VIsual_active && VIsual_select)
3505 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003506 cap->cmdchar = 'x'; // BS key behaves like 'x' in Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 v_visop(cap);
3508 }
3509 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 nv_left(cap);
3511}
3512
3513/*
3514 * CTRL-L: clear screen and redraw.
3515 */
3516 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003517nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
3519 if (!checkclearop(cap->oap))
3520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521#ifdef FEAT_SYN_HL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003522 // Clear all syntax states to force resyncing.
Bram Moolenaar860cae12010-06-05 23:22:07 +02003523 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02003524# ifdef FEAT_RELTIME
3525 {
3526 win_T *wp;
3527
3528 FOR_ALL_WINDOWS(wp)
3529 wp->w_s->b_syn_slow = FALSE;
3530 }
3531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532#endif
3533 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003534#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3535# ifdef VIMDLL
3536 if (!gui.in_use)
3537# endif
3538 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01003539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541}
3542
3543/*
3544 * CTRL-O: In Select mode: switch to Visual mode for one command.
3545 * Otherwise: Go to older pcmark.
3546 */
3547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003548nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (VIsual_active && VIsual_select)
3551 {
3552 VIsual_select = FALSE;
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01003553 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 showmode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003555 restart_VIsual_select = 2; // restart Select mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 }
3557 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
3559 cap->count1 = -cap->count1;
3560 nv_pcmark(cap);
3561 }
3562}
3563
3564/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01003565 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
3566 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 */
3568 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003569nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571 if (!checkclearopq(cap->oap))
3572 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
3573 GETF_SETMARK|GETF_ALT, FALSE);
3574}
3575
3576/*
3577 * "Z" commands.
3578 */
3579 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003580nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 if (!checkclearopq(cap->oap))
3583 {
3584 switch (cap->nchar)
3585 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003586 // "ZZ": equivalent to ":x".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 case 'Z': do_cmdline_cmd((char_u *)"x");
3588 break;
3589
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003590 // "ZQ": equivalent to ":q!" (Elvis compatible).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 case 'Q': do_cmdline_cmd((char_u *)"q!");
3592 break;
3593
3594 default: clearopbeep(cap->oap);
3595 }
3596 }
3597}
3598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599/*
3600 * Call nv_ident() as if "c1" was used, with "c2" as next character.
3601 */
3602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003603do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 oparg_T oa;
3606 cmdarg_T ca;
3607
3608 clear_oparg(&oa);
Bram Moolenaara80faa82020-04-12 19:37:17 +02003609 CLEAR_FIELD(ca);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 ca.oap = &oa;
3611 ca.cmdchar = c1;
3612 ca.nchar = c2;
3613 nv_ident(&ca);
3614}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616/*
3617 * Handle the commands that use the word under the cursor.
3618 * [g] CTRL-] :ta to current identifier
3619 * [g] 'K' run program for current identifier
3620 * [g] '*' / to current identifier or string
3621 * [g] '#' ? to current identifier or string
3622 * g ']' :tselect for current identifier
3623 */
3624 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003625nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 char_u *ptr = NULL;
3628 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003629 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003630 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 char_u *p;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003632 char_u *kp; // value of 'keywordprg'
3633 int kp_help; // 'keywordprg' is ":he"
3634 int kp_ex; // 'keywordprg' starts with ":"
3635 int n = 0; // init for GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 int cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003637 int g_cmd; // "g" command
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003638 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 char_u *aux_ptr;
3640 int isman;
3641 int isman_s;
3642
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003643 if (cap->cmdchar == 'g') // "g*", "g#", "g]" and "gCTRL-]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 {
3645 cmdchar = cap->nchar;
3646 g_cmd = TRUE;
3647 }
3648 else
3649 {
3650 cmdchar = cap->cmdchar;
3651 g_cmd = FALSE;
3652 }
3653
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003654 if (cmdchar == POUND) // the pound sign, '#' for English keyboards
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 cmdchar = '#';
3656
3657 /*
3658 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
3659 */
3660 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
3661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
3663 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 if (checkclearopq(cap->oap))
3665 return;
3666 }
3667
3668 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
3669 (cmdchar == '*' || cmdchar == '#')
3670 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
3671 {
3672 clearop(cap->oap);
3673 return;
3674 }
3675
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003676 // Allocate buffer to put the command in. Inserting backslashes can
3677 // double the length of the word. p_kp / curbuf->b_p_kp could be added
3678 // and some numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
3680 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
3681 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003682 if (kp_help && *skipwhite(ptr) == NUL)
3683 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003684 emsg(_(e_no_identifier_under_cursor)); // found white space only
Bram Moolenaara4f99f52017-08-26 16:25:32 +02003685 return;
3686 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003687 kp_ex = (*kp == ':');
3688 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
3689 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (buf == NULL)
3691 return;
3692 buf[0] = NUL;
3693
3694 switch (cmdchar)
3695 {
3696 case '*':
3697 case '#':
3698 /*
3699 * Put cursor at start of word, makes search skip the word
3700 * under the cursor.
3701 * Call setpcmark() first, so "*``" puts the cursor back where
3702 * it was.
3703 */
3704 setpcmark();
3705 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
3706
3707 if (!g_cmd && vim_iswordp(ptr))
3708 STRCPY(buf, "\\<");
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003709 no_smartcase = TRUE; // don't use 'smartcase' now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 break;
3711
3712 case 'K':
3713 if (kp_help)
3714 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02003715 else if (kp_ex)
3716 {
3717 if (cap->count0 != 0)
3718 vim_snprintf((char *)buf, buflen, "%s %ld",
3719 kp, cap->count0);
3720 else
3721 STRCPY(buf, kp);
3722 STRCAT(buf, " ");
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 else
3725 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003726 // An external command will probably use an argument starting
3727 // with "-" as an option. To avoid trouble we skip the "-".
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003728 while (*ptr == '-' && n > 0)
3729 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003730 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003731 --n;
3732 }
3733 if (n == 0)
3734 {
Bram Moolenaareaaac012022-01-02 17:00:40 +00003735 // found dashes only
3736 emsg(_(e_no_identifier_under_cursor));
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003737 vim_free(buf);
3738 return;
3739 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003740
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003741 // When a count is given, turn it into a range. Is this
3742 // really what we want?
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 isman = (STRCMP(kp, "man") == 0);
3744 isman_s = (STRCMP(kp, "man -s") == 0);
3745 if (cap->count0 != 0 && !(isman || isman_s))
3746 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
3747
3748 STRCAT(buf, "! ");
3749 if (cap->count0 == 0 && isman_s)
3750 STRCAT(buf, "man");
3751 else
3752 STRCAT(buf, kp);
3753 STRCAT(buf, " ");
3754 if (cap->count0 != 0 && (isman || isman_s))
3755 {
3756 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
3757 STRCAT(buf, " ");
3758 }
3759 }
3760 break;
3761
3762 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003763 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764#ifdef FEAT_CSCOPE
3765 if (p_cst)
3766 STRCPY(buf, "cstag ");
3767 else
3768#endif
3769 STRCPY(buf, "ts ");
3770 break;
3771
3772 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01003773 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (curbuf->b_help)
3775 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003777 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003778 if (g_cmd)
3779 STRCPY(buf, "tj ");
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003780 else if (cap->count0 == 0)
3781 STRCPY(buf, "ta ");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003782 else
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +02003783 sprintf((char *)buf, ":%ldta ", cap->count0);
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786
3787 /*
3788 * Now grab the chars in the identifier
3789 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003790 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003792 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003793 if (kp_ex)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003794 // Escape the argument properly for an Ex command
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01003795 p = vim_strsave_fnameescape(ptr, VSE_NONE);
Bram Moolenaar426f3752016-11-04 21:22:37 +01003796 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003797 // Escape the argument properly for a shell command
Bram Moolenaar426f3752016-11-04 21:22:37 +01003798 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003799 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003800 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003802 vim_free(buf);
3803 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003805 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003806 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003807 {
3808 vim_free(buf);
3809 vim_free(p);
3810 return;
3811 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02003812 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003813 STRCAT(buf, p);
3814 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003816 else
3817 {
3818 if (cmdchar == '*')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003819 aux_ptr = (char_u *)(magic_isset() ? "/.*~[^$\\" : "/^$\\");
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003820 else if (cmdchar == '#')
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003821 aux_ptr = (char_u *)(magic_isset() ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003822 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003823 {
3824 if (curbuf->b_help)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003825 // ":help" handles unescaped argument
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02003826 aux_ptr = (char_u *)"";
3827 else
3828 aux_ptr = (char_u *)"\\|\"\n[";
3829 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01003830 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003831 aux_ptr = (char_u *)"\\|\"\n*?[";
3832
3833 p = buf + STRLEN(buf);
3834 while (n-- > 0)
3835 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003836 // put a backslash before \ and some others
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003837 if (vim_strchr(aux_ptr, *ptr) != NULL)
3838 *p++ = '\\';
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003839 // When current byte is a part of multibyte character, copy all
3840 // bytes of that character.
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003841 if (has_mbyte)
3842 {
3843 int i;
3844 int len = (*mb_ptr2len)(ptr) - 1;
3845
3846 for (i = 0; i < len && n >= 1; ++i, --n)
3847 *p++ = *ptr++;
3848 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00003849 *p++ = *ptr++;
3850 }
3851 *p = NUL;
3852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
3854 /*
3855 * Execute the command.
3856 */
3857 if (cmdchar == '*' || cmdchar == '#')
3858 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003859 if (!g_cmd && (has_mbyte
3860 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
3861 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02003863
3864 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00003865 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02003867
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003868 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003871 {
3872 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02003874 g_tag_at_cursor = FALSE;
3875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
3877 vim_free(buf);
3878}
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880/*
3881 * Get visually selected text, within one line only.
3882 * Returns FAIL if more than one line selected.
3883 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003884 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003885get_visual_text(
3886 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003887 char_u **pp, // return: start of selected text
3888 int *lenp) // return: length of selected text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889{
3890 if (VIsual_mode != 'V')
3891 unadjust_for_sel();
3892 if (VIsual.lnum != curwin->w_cursor.lnum)
3893 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003894 if (cap != NULL)
3895 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return FAIL;
3897 }
3898 if (VIsual_mode == 'V')
3899 {
3900 *pp = ml_get_curline();
3901 *lenp = (int)STRLEN(*pp);
3902 }
3903 else
3904 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003905 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
3907 *pp = ml_get_pos(&curwin->w_cursor);
3908 *lenp = VIsual.col - curwin->w_cursor.col + 1;
3909 }
3910 else
3911 {
3912 *pp = ml_get_pos(&VIsual);
3913 *lenp = curwin->w_cursor.col - VIsual.col + 1;
3914 }
Bram Moolenaar615ddd52021-11-17 18:00:31 +00003915 if (**pp == NUL)
3916 *lenp = 0;
3917 if (has_mbyte && *lenp > 0)
3918 // Correct the length to include all bytes of the last character.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003919 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 }
3921 reset_VIsual_and_resel();
3922 return OK;
3923}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925/*
3926 * CTRL-T: backwards in tag stack
3927 */
3928 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003929nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930{
3931 if (!checkclearopq(cap->oap))
3932 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
3933}
3934
3935/*
3936 * Handle scrolling command 'H', 'L' and 'M'.
3937 */
3938 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003939nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941 int used = 0;
3942 long n;
3943#ifdef FEAT_FOLDING
3944 linenr_T lnum;
3945#endif
3946 int half;
3947
3948 cap->oap->motion_type = MLINE;
3949 setpcmark();
3950
3951 if (cap->cmdchar == 'L')
3952 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003953 validate_botline(); // make sure curwin->w_botline is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 curwin->w_cursor.lnum = curwin->w_botline - 1;
3955 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
3956 curwin->w_cursor.lnum = 1;
3957 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003958 {
3959#ifdef FEAT_FOLDING
3960 if (hasAnyFolding(curwin))
3961 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003962 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003963 for (n = cap->count1 - 1; n > 0
3964 && curwin->w_cursor.lnum > curwin->w_topline; --n)
3965 {
3966 (void)hasFolding(curwin->w_cursor.lnum,
3967 &curwin->w_cursor.lnum, NULL);
3968 --curwin->w_cursor.lnum;
3969 }
3970 }
3971 else
3972#endif
3973 curwin->w_cursor.lnum -= cap->count1 - 1;
3974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976 else
3977 {
3978 if (cap->cmdchar == 'M')
3979 {
3980#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003981 // Don't count filler lines above the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 used -= diff_check_fill(curwin, curwin->w_topline)
3983 - curwin->w_topfill;
3984#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003985 validate_botline(); // make sure w_empty_rows is valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
3987 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
3988 {
3989#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01003990 // Count half he number of filler lines to be "below this
3991 // line" and half to be "above the next line".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
3993 + n) / 2 >= half)
3994 {
3995 --n;
3996 break;
3997 }
3998#endif
3999 used += plines(curwin->w_topline + n);
4000 if (used >= half)
4001 break;
4002#ifdef FEAT_FOLDING
4003 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
4004 n = lnum - curwin->w_topline;
4005#endif
4006 }
4007 if (n > 0 && used > curwin->w_height)
4008 --n;
4009 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004010 else // (cap->cmdchar == 'H')
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004013#ifdef FEAT_FOLDING
4014 if (hasAnyFolding(curwin))
4015 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004016 // Count a fold for one screen line.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004017 lnum = curwin->w_topline;
4018 while (n-- > 0 && lnum < curwin->w_botline - 1)
4019 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02004020 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004021 ++lnum;
4022 }
4023 n = lnum - curwin->w_topline;
4024 }
4025#endif
4026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 curwin->w_cursor.lnum = curwin->w_topline + n;
4028 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4029 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4030 }
4031
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004032 // Correct for 'so', except when an operator is pending.
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02004033 if (cap->oap->op_type == OP_NOP)
4034 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 beginline(BL_SOL | BL_FIX);
4036}
4037
4038/*
4039 * Cursor right commands.
4040 */
4041 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004042nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043{
4044 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004045 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004047 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4048 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004049 // <C-Right> and <S-Right> move a word or WORD right
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004050 if (mod_mask & MOD_MASK_CTRL)
4051 cap->arg = TRUE;
4052 nv_wordcmd(cap);
4053 return;
4054 }
4055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 cap->oap->motion_type = MCHAR;
4057 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004058 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004061 * In virtual edit mode, there's no such thing as "past_line", as lines
4062 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 */
4064 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004065 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 for (n = cap->count1; n > 0; --n)
4068 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004069 if ((!past_line && oneright() == FAIL)
4070 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004071 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 {
4073 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004074 * <Space> wraps to next line if 'whichwrap' has 's'.
4075 * 'l' wraps to next line if 'whichwrap' has 'l'.
4076 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 */
4078 if ( ((cap->cmdchar == ' '
4079 && vim_strchr(p_ww, 's') != NULL)
4080 || (cap->cmdchar == 'l'
4081 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004082 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 && vim_strchr(p_ww, '>') != NULL))
4084 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4085 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004086 // When deleting we also count the NL as a character.
4087 // Set cap->oap->inclusive when last char in the line is
4088 // included, move to next line after that
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004089 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004091 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 cap->oap->inclusive = TRUE;
4093 else
4094 {
4095 ++curwin->w_cursor.lnum;
4096 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 curwin->w_set_curswant = TRUE;
4099 cap->oap->inclusive = FALSE;
4100 }
4101 continue;
4102 }
4103 if (cap->oap->op_type == OP_NOP)
4104 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004105 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (n == cap->count1)
4107 beep_flush();
4108 }
4109 else
4110 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004111 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 cap->oap->inclusive = TRUE;
4113 }
4114 break;
4115 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004116 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
4118 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 if (virtual_active())
4120 oneright();
4121 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004124 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 ++curwin->w_cursor.col;
4127 }
4128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130#ifdef FEAT_FOLDING
4131 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4132 && cap->oap->op_type == OP_NOP)
4133 foldOpenCursor();
4134#endif
4135}
4136
4137/*
4138 * Cursor left commands.
4139 *
4140 * Returns TRUE when operator end should not be adjusted.
4141 */
4142 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004143nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
4145 long n;
4146
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4148 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004149 // <C-Left> and <S-Left> move a word or WORD left
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004150 if (mod_mask & MOD_MASK_CTRL)
4151 cap->arg = 1;
4152 nv_bck_word(cap);
4153 return;
4154 }
4155
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 cap->oap->motion_type = MCHAR;
4157 cap->oap->inclusive = FALSE;
4158 for (n = cap->count1; n > 0; --n)
4159 {
4160 if (oneleft() == FAIL)
4161 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004162 // <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4163 // 'h' wraps to previous line if 'whichwrap' has 'h'.
4164 // CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 if ( (((cap->cmdchar == K_BS
4166 || cap->cmdchar == Ctrl_H)
4167 && vim_strchr(p_ww, 'b') != NULL)
4168 || (cap->cmdchar == 'h'
4169 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004170 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 && vim_strchr(p_ww, '<') != NULL))
4172 && curwin->w_cursor.lnum > 1)
4173 {
4174 --(curwin->w_cursor.lnum);
4175 coladvance((colnr_T)MAXCOL);
4176 curwin->w_set_curswant = TRUE;
4177
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004178 // When the NL before the first char has to be deleted we
4179 // put the cursor on the NUL after the previous line.
4180 // This is a very special case, be careful!
4181 // Don't adjust op_end now, otherwise it won't work.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if ( (cap->oap->op_type == OP_DELETE
4183 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004184 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004186 char_u *cp = ml_get_cursor();
4187
4188 if (*cp != NUL)
4189 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004190 if (has_mbyte)
4191 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4192 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004193 ++curwin->w_cursor.col;
4194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 cap->retval |= CA_NO_ADJ_OP_END;
4196 }
4197 continue;
4198 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004199 // Only beep and flush if not moved at all
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4201 beep_flush();
4202 break;
4203 }
4204 }
4205#ifdef FEAT_FOLDING
4206 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4207 && cap->oap->op_type == OP_NOP)
4208 foldOpenCursor();
4209#endif
4210}
4211
4212/*
4213 * Cursor up commands.
4214 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4215 */
4216 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004217nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004219 if (mod_mask & MOD_MASK_SHIFT)
4220 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004221 // <S-Up> is page up
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004222 cap->arg = BACKWARD;
4223 nv_page(cap);
4224 }
4225 else
4226 {
4227 cap->oap->motion_type = MLINE;
4228 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4229 clearopbeep(cap->oap);
4230 else if (cap->arg)
4231 beginline(BL_WHITE | BL_FIX);
4232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233}
4234
4235/*
4236 * Cursor down commands.
4237 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
4238 */
4239 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02004240nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004242 if (mod_mask & MOD_MASK_SHIFT)
4243 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004244 // <S-Down> is page down
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004245 cap->arg = FORWARD;
4246 nv_page(cap);
4247 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02004248#if defined(FEAT_QUICKFIX)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004249 // Quickfix window only: view the result under the cursor.
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004250 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
4251 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255#ifdef FEAT_CMDWIN
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004256 // In the cmdline window a <CR> executes the command.
Bram Moolenaar05159a02005-02-26 23:04:13 +00004257 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 cmdwin_result = CAR;
4259 else
4260#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02004261#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004262 // In a prompt buffer a <CR> in the last line invokes the callback.
Bram Moolenaarf2732452018-06-03 14:47:35 +02004263 if (bt_prompt(curbuf) && cap->cmdchar == CAR
4264 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4265 {
4266 invoke_prompt_callback();
4267 if (restart_edit == 0)
4268 restart_edit = 'a';
4269 }
4270 else
4271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 {
4273 cap->oap->motion_type = MLINE;
4274 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4275 clearopbeep(cap->oap);
4276 else if (cap->arg)
4277 beginline(BL_WHITE | BL_FIX);
4278 }
4279 }
4280}
4281
4282#ifdef FEAT_SEARCHPATH
4283/*
4284 * Grab the file name under the cursor and edit it.
4285 */
4286 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004287nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
4289 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004290 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004292 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 {
4294 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004295 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 return;
4297 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004298 if (curbuf_locked())
4299 {
4300 clearop(cap->oap);
4301 return;
4302 }
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02004303#ifdef FEAT_PROP_POPUP
4304 if (ERROR_IF_TERM_POPUP_WINDOW)
4305 return;
4306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004308 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 if (ptr != NULL)
4311 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004312 // do autowrite if necessary
Bram Moolenaareb44a682017-08-03 22:44:55 +02004313 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02004314 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004316 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02004317 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02004318 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004319 {
4320 curwin->w_cursor.lnum = lnum;
4321 check_cursor_lnum();
4322 beginline(BL_SOL | BL_FIX);
4323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 vim_free(ptr);
4325 }
4326 else
4327 clearop(cap->oap);
4328}
4329#endif
4330
4331/*
4332 * <End> command: to end of current line or last line.
4333 */
4334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004335nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004337 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) // CTRL-END = goto last line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004339 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 nv_goto(cap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004341 cap->count1 = 1; // to end of current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343 nv_dollar(cap);
4344}
4345
4346/*
4347 * Handle the "$" command.
4348 */
4349 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004350nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351{
4352 cap->oap->motion_type = MCHAR;
4353 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004354 // In virtual mode when off the edge of a line and an operator
4355 // is pending (whew!) keep the cursor where it is.
4356 // Otherwise, send it to the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 if (!virtual_active() || gchar_cursor() != NUL
4358 || cap->oap->op_type == OP_NOP)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004359 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 if (cursor_down((long)(cap->count1 - 1),
4361 cap->oap->op_type == OP_NOP) == FAIL)
4362 clearopbeep(cap->oap);
4363#ifdef FEAT_FOLDING
4364 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4365 foldOpenCursor();
4366#endif
4367}
4368
4369/*
4370 * Implementation of '?' and '/' commands.
4371 * If cap->arg is TRUE don't set PC mark.
4372 */
4373 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004374nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
4376 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02004377 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
4380 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004381 // Translate "g??" to "g?g?"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 cap->cmdchar = 'g';
4383 cap->nchar = '?';
4384 nv_operator(cap);
4385 return;
4386 }
4387
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004388 // When using 'incsearch' the cursor may be moved to set a different search
4389 // start position.
Bram Moolenaarc97f9a52021-12-28 20:59:56 +00004390 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392 if (cap->searchbuf == NULL)
4393 {
4394 clearop(oap);
4395 return;
4396 }
4397
Bram Moolenaar46539112015-02-17 15:43:57 +01004398 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004399 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004400 ? 0 : SEARCH_MARK, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401}
4402
4403/*
4404 * Handle "N" and "n" commands.
4405 * cap->arg is SEARCH_REV for "N", 0 for "n".
4406 */
4407 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004408nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004410 pos_T old = curwin->w_cursor;
4411 int wrapped = FALSE;
4412 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, &wrapped);
Bram Moolenaar46539112015-02-17 15:43:57 +01004413
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004414 if (i == 1 && !wrapped && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01004415 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004416 // Avoid getting stuck on the current cursor position, which can
4417 // happen when an offset is given and the cursor is on the last char
4418 // in the buffer: Repeat with count + 1.
Bram Moolenaar46539112015-02-17 15:43:57 +01004419 cap->count1 += 1;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004420 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg, NULL);
Bram Moolenaar46539112015-02-17 15:43:57 +01004421 cap->count1 -= 1;
4422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423}
4424
4425/*
4426 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
4427 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01004428 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 */
Bram Moolenaar46539112015-02-17 15:43:57 +01004430 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004431normal_search(
4432 cmdarg_T *cap,
4433 int dir,
4434 char_u *pat,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004435 int opt, // extra flags for do_search()
4436 int *wrapped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
4438 int i;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004439 searchit_arg_T sia;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 cap->oap->motion_type = MCHAR;
4442 cap->oap->inclusive = FALSE;
4443 cap->oap->use_reg_one = TRUE;
4444 curwin->w_set_curswant = TRUE;
4445
Bram Moolenaara80faa82020-04-12 19:37:17 +02004446 CLEAR_FIELD(sia);
Bram Moolenaarc036e872020-02-21 21:30:52 +01004447 i = do_search(cap->oap, dir, dir, pat, cap->count1,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004448 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, &sia);
4449 if (wrapped != NULL)
4450 *wrapped = sia.sa_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 if (i == 0)
4452 clearop(cap->oap);
4453 else
4454 {
4455 if (i == 2)
4456 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458#ifdef FEAT_FOLDING
4459 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4460 foldOpenCursor();
4461#endif
4462 }
4463
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004464 // "/$" will put the cursor after the end of the line, may need to
4465 // correct that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01004467 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468}
4469
4470/*
4471 * Character search commands.
4472 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
4473 * ',' and FALSE for ';'.
4474 * cap->nchar is NUL for ',' and ';' (repeat the search)
4475 */
4476 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004477nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
4479 int t_cmd;
4480
4481 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
4482 t_cmd = TRUE;
4483 else
4484 t_cmd = FALSE;
4485
4486 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
4488 clearopbeep(cap->oap);
4489 else
4490 {
4491 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004492 // Include a Tab for "tx" and for "dfx".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
4494 && (t_cmd || cap->oap->op_type != OP_NOP))
4495 {
4496 colnr_T scol, ecol;
4497
4498 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
4499 curwin->w_cursor.coladd = ecol - scol;
4500 }
4501 else
4502 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504#ifdef FEAT_FOLDING
4505 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
4506 foldOpenCursor();
4507#endif
4508 }
4509}
4510
4511/*
4512 * "[" and "]" commands.
4513 * cap->arg is BACKWARD for "[" and FORWARD for "]".
4514 */
4515 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004516nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01004518 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 pos_T prev_pos;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004520 pos_T *pos = NULL; // init for GCC
4521 pos_T old_pos; // cursor position before command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 int flag;
4523 long n;
4524 int findc;
4525 int c;
4526
4527 cap->oap->motion_type = MCHAR;
4528 cap->oap->inclusive = FALSE;
4529 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004530 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532#ifdef FEAT_SEARCHPATH
4533 /*
4534 * "[f" or "]f" : Edit file under the cursor (same as "gf")
4535 */
4536 if (cap->nchar == 'f')
4537 nv_gotofile(cap);
4538 else
4539#endif
4540
4541#ifdef FEAT_FIND_ID
4542 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00004543 * Find the occurrence(s) of the identifier or define under cursor
4544 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 *
4546 * search list jump
4547 * fwd bwd fwd bwd fwd bwd
4548 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
4549 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
4550 */
4551 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004552# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004554# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01004556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 cap->nchar) != NULL)
4558 {
4559 char_u *ptr;
4560 int len;
4561
4562 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
4563 clearop(cap->oap);
4564 else
4565 {
4566 find_pattern_in_path(ptr, 0, len, TRUE,
4567 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
4568 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
4569 cap->count1,
4570 isupper(cap->nchar) ? ACTION_SHOW_ALL :
4571 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
4572 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
4573 (linenr_T)MAXLNUM);
4574 curwin->w_set_curswant = TRUE;
4575 }
4576 }
4577 else
4578#endif
4579
4580 /*
4581 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
4582 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
4583 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
4584 * "[m" or "]m" search for prev/next start of (Java) method.
4585 * "[M" or "]M" search for prev/next end of (Java) method.
4586 */
4587 if ( (cap->cmdchar == '['
4588 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
4589 || (cap->cmdchar == ']'
4590 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
4591 {
4592 if (cap->nchar == '*')
4593 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 prev_pos.lnum = 0;
4595 if (cap->nchar == 'm' || cap->nchar == 'M')
4596 {
4597 if (cap->cmdchar == '[')
4598 findc = '{';
4599 else
4600 findc = '}';
4601 n = 9999;
4602 }
4603 else
4604 {
4605 findc = cap->nchar;
4606 n = cap->count1;
4607 }
4608 for ( ; n > 0; --n)
4609 {
4610 if ((pos = findmatchlimit(cap->oap, findc,
4611 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
4612 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004613 if (new_pos.lnum == 0) // nothing found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
4615 if (cap->nchar != 'm' && cap->nchar != 'M')
4616 clearopbeep(cap->oap);
4617 }
4618 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004619 pos = &new_pos; // use last one found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 break;
4621 }
4622 prev_pos = new_pos;
4623 curwin->w_cursor = *pos;
4624 new_pos = *pos;
4625 }
4626 curwin->w_cursor = old_pos;
4627
4628 /*
4629 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
4630 * brought us to the match for "[m" and "]M" when inside a method.
4631 * Try finding the '{' or '}' we want to be at.
4632 * Also repeat for the given count.
4633 */
4634 if (cap->nchar == 'm' || cap->nchar == 'M')
4635 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004636 // norm is TRUE for "]M" and "[m"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 int norm = ((findc == '{') == (cap->nchar == 'm'));
4638
4639 n = cap->count1;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004640 // found a match: we were inside a method
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (prev_pos.lnum != 0)
4642 {
4643 pos = &prev_pos;
4644 curwin->w_cursor = prev_pos;
4645 if (norm)
4646 --n;
4647 }
4648 else
4649 pos = NULL;
4650 while (n > 0)
4651 {
4652 for (;;)
4653 {
4654 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
4655 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004656 // if not found anything, that's an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if (pos == NULL)
4658 clearopbeep(cap->oap);
4659 n = 0;
4660 break;
4661 }
4662 c = gchar_cursor();
4663 if (c == '{' || c == '}')
4664 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004665 // Must have found end/start of class: use it.
4666 // Or found the place to be at.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if ((c == findc && norm) || (n == 1 && !norm))
4668 {
4669 new_pos = curwin->w_cursor;
4670 pos = &new_pos;
4671 n = 0;
4672 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004673 // if no match found at all, we started outside of the
4674 // class and we're inside now. Just go on.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 else if (new_pos.lnum == 0)
4676 {
4677 new_pos = curwin->w_cursor;
4678 pos = &new_pos;
4679 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004680 // found start/end of other method: go to match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 else if ((pos = findmatchlimit(cap->oap, findc,
4682 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
4683 0)) == NULL)
4684 n = 0;
4685 else
4686 curwin->w_cursor = *pos;
4687 break;
4688 }
4689 }
4690 --n;
4691 }
4692 curwin->w_cursor = old_pos;
4693 if (pos == NULL && new_pos.lnum != 0)
4694 clearopbeep(cap->oap);
4695 }
4696 if (pos != NULL)
4697 {
4698 setpcmark();
4699 curwin->w_cursor = *pos;
4700 curwin->w_set_curswant = TRUE;
4701#ifdef FEAT_FOLDING
4702 if ((fdo_flags & FDO_BLOCK) && KeyTyped
4703 && cap->oap->op_type == OP_NOP)
4704 foldOpenCursor();
4705#endif
4706 }
4707 }
4708
4709 /*
4710 * "[[", "[]", "]]" and "][": move to start or end of function
4711 */
4712 else if (cap->nchar == '[' || cap->nchar == ']')
4713 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004714 if (cap->nchar == cap->cmdchar) // "]]" or "[["
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 flag = '{';
4716 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004717 flag = '}'; // "][" or "[]"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718
4719 curwin->w_set_curswant = TRUE;
4720 /*
4721 * Imitate strange Vi behaviour: When using "]]" with an operator
4722 * we also stop at '}'.
4723 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004724 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 (cap->oap->op_type != OP_NOP
4726 && cap->arg == FORWARD && flag == '{')))
4727 clearopbeep(cap->oap);
4728 else
4729 {
4730 if (cap->oap->op_type == OP_NOP)
4731 beginline(BL_WHITE | BL_FIX);
4732#ifdef FEAT_FOLDING
4733 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4734 foldOpenCursor();
4735#endif
4736 }
4737 }
4738
4739 /*
4740 * "[p", "[P", "]P" and "]p": put with indent adjustment
4741 */
4742 else if (cap->nchar == 'p' || cap->nchar == 'P')
4743 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02004744 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
4746
4747 /*
4748 * "['", "[`", "]'" and "]`": jump to next mark
4749 */
4750 else if (cap->nchar == '\'' || cap->nchar == '`')
4751 {
4752 pos = &curwin->w_cursor;
4753 for (n = cap->count1; n > 0; --n)
4754 {
4755 prev_pos = *pos;
4756 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
4757 cap->nchar == '\'');
4758 if (pos == NULL)
4759 break;
4760 }
4761 if (pos == NULL)
4762 pos = &prev_pos;
4763 nv_cursormark(cap, cap->nchar == '\'', pos);
4764 }
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 /*
4767 * [ or ] followed by a middle mouse click: put selected text with
4768 * indent adjustment. Any other button just does as usual.
4769 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004770 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
4772 (void)do_mouse(cap->oap, cap->nchar,
4773 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
4774 cap->count1, PUT_FIXINDENT);
4775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777#ifdef FEAT_FOLDING
4778 /*
4779 * "[z" and "]z": move to start or end of open fold.
4780 */
4781 else if (cap->nchar == 'z')
4782 {
4783 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4784 cap->count1) == FAIL)
4785 clearopbeep(cap->oap);
4786 }
4787#endif
4788
4789#ifdef FEAT_DIFF
4790 /*
4791 * "[c" and "]c": move to next or previous diff-change.
4792 */
4793 else if (cap->nchar == 'c')
4794 {
4795 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
4796 cap->count1) == FAIL)
4797 clearopbeep(cap->oap);
4798 }
4799#endif
4800
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004801#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004802 /*
4803 * "[s", "[S", "]s" and "]S": move to next spell error.
4804 */
4805 else if (cap->nchar == 's' || cap->nchar == 'S')
4806 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004807 setpcmark();
4808 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00004809 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
4810 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00004811 {
4812 clearopbeep(cap->oap);
4813 break;
4814 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01004815 else
4816 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004817# ifdef FEAT_FOLDING
4818 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
4819 foldOpenCursor();
4820# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004821 }
4822#endif
4823
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004824 // Not a valid cap->nchar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 else
4826 clearopbeep(cap->oap);
4827}
4828
4829/*
4830 * Handle Normal mode "%" command.
4831 */
4832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004833nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
4835 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02004836#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 linenr_T lnum = curwin->w_cursor.lnum;
4838#endif
4839
4840 cap->oap->inclusive = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004841 if (cap->count0) // {cnt}% : goto {cnt} percentage in file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
4843 if (cap->count0 > 100)
4844 clearopbeep(cap->oap);
4845 else
4846 {
4847 cap->oap->motion_type = MLINE;
4848 setpcmark();
Bram Moolenaar2c655342021-02-23 19:32:03 +01004849 // Round up, so 'normal 100%' always jumps at the line line.
4850 // Beyond 21474836 lines, (ml_line_count * 100 + 99) would
4851 // overflow on 32-bits, so use a formula with less accuracy
4852 // to avoid overflows.
4853 if (curbuf->b_ml.ml_line_count >= 21474836)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
4855 / 100L * cap->count0;
4856 else
4857 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
4858 cap->count0 + 99L) / 100L;
Bram Moolenaar1d859e22021-01-28 17:24:58 +01004859 if (curwin->w_cursor.lnum < 1)
4860 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4862 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4863 beginline(BL_SOL | BL_FIX);
4864 }
4865 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004866 else // "%" : go to matching paren
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 cap->oap->motion_type = MCHAR;
4869 cap->oap->use_reg_one = TRUE;
4870 if ((pos = findmatch(cap->oap, NUL)) == NULL)
4871 clearopbeep(cap->oap);
4872 else
4873 {
4874 setpcmark();
4875 curwin->w_cursor = *pos;
4876 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
4880 }
4881#ifdef FEAT_FOLDING
4882 if (cap->oap->op_type == OP_NOP
4883 && lnum != curwin->w_cursor.lnum
4884 && (fdo_flags & FDO_PERCENT)
4885 && KeyTyped)
4886 foldOpenCursor();
4887#endif
4888}
4889
4890/*
4891 * Handle "(" and ")" commands.
4892 * cap->arg is BACKWARD for "(" and FORWARD for ")".
4893 */
4894 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004895nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
4897 cap->oap->motion_type = MCHAR;
4898 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004899 // The motion used to be inclusive for "(", but that is not what Vi does.
Bram Moolenaarebefac62005-12-28 22:39:57 +00004900 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 curwin->w_set_curswant = TRUE;
4902
4903 if (findsent(cap->arg, cap->count1) == FAIL)
4904 clearopbeep(cap->oap);
4905 else
4906 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004907 // Don't leave the cursor on the NUL past end of line.
Bram Moolenaar1f14d572008-01-12 16:12:10 +00004908 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#ifdef FEAT_FOLDING
4911 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4912 foldOpenCursor();
4913#endif
4914 }
4915}
4916
4917/*
4918 * "m" command: Mark a position.
4919 */
4920 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004921nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
4923 if (!checkclearop(cap->oap))
4924 {
4925 if (setmark(cap->nchar) == FAIL)
4926 clearopbeep(cap->oap);
4927 }
4928}
4929
4930/*
4931 * "{" and "}" commands.
4932 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
4933 */
4934 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004935nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 cap->oap->motion_type = MCHAR;
4938 cap->oap->inclusive = FALSE;
4939 cap->oap->use_reg_one = TRUE;
4940 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004941 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 clearopbeep(cap->oap);
4943 else
4944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#ifdef FEAT_FOLDING
4947 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
4948 foldOpenCursor();
4949#endif
4950 }
4951}
4952
4953/*
4954 * "u" command: Undo or make lower case.
4955 */
4956 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004957nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004959 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01004961 // translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 cap->cmdchar = 'g';
4963 cap->nchar = 'u';
4964 nv_operator(cap);
4965 }
4966 else
4967 nv_kundo(cap);
4968}
4969
4970/*
4971 * <Undo> command.
4972 */
4973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004974nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
4976 if (!checkclearopq(cap->oap))
4977 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02004978#ifdef FEAT_JOB_CHANNEL
4979 if (bt_prompt(curbuf))
4980 {
4981 clearopbeep(cap->oap);
4982 return;
4983 }
4984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 u_undo((int)cap->count1);
4986 curwin->w_set_curswant = TRUE;
4987 }
4988}
4989
4990/*
4991 * Handle the "r" command.
4992 */
4993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004994nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
4996 char_u *ptr;
4997 int had_ctrl_v;
4998 long n;
4999
5000 if (checkclearop(cap->oap))
5001 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02005002#ifdef FEAT_JOB_CHANNEL
5003 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5004 {
5005 clearopbeep(cap->oap);
5006 return;
5007 }
5008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005010 // get another character
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (cap->nchar == Ctrl_V)
5012 {
5013 had_ctrl_v = Ctrl_V;
Bram Moolenaar0684e362020-12-03 19:54:42 +01005014 cap->nchar = get_literal(FALSE);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005015 // Don't redo a multibyte character with CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (cap->nchar > DEL)
5017 had_ctrl_v = NUL;
5018 }
5019 else
5020 had_ctrl_v = NUL;
5021
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005022 // Abort if the character is a special key.
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005023 if (IS_SPECIAL(cap->nchar))
5024 {
5025 clearopbeep(cap->oap);
5026 return;
5027 }
5028
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005029 // Visual mode "r"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (VIsual_active)
5031 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00005032 if (got_int)
5033 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01005034 if (had_ctrl_v)
5035 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005036 // Use a special (negative) number to make a difference between a
5037 // literal CR or NL and a line break.
Bram Moolenaarf12519d2018-02-06 22:52:49 +01005038 if (cap->nchar == CAR)
5039 cap->nchar = REPLACE_CR_NCHAR;
5040 else if (cap->nchar == NL)
5041 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01005042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 nv_operator(cap);
5044 return;
5045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005047 // Break tabs, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (virtual_active())
5049 {
5050 if (u_save_cursor() == FAIL)
5051 return;
5052 if (gchar_cursor() == NUL)
5053 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005054 // Add extra space and put the cursor on the first one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 coladvance_force((colnr_T)(getviscol() + cap->count1));
5056 curwin->w_cursor.col -= cap->count1;
5057 }
5058 else if (gchar_cursor() == TAB)
5059 coladvance_force(getviscol());
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005062 // Abort if not enough characters to replace.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005064 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005065 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 clearopbeep(cap->oap);
5068 return;
5069 }
5070
5071 /*
5072 * Replacing with a TAB is done by edit() when it is complicated because
5073 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5074 * Other characters are done below to avoid problems with things like
5075 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5076 */
5077 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5078 {
5079 stuffnumReadbuff(cap->count1);
5080 stuffcharReadbuff('R');
5081 stuffcharReadbuff('\t');
5082 stuffcharReadbuff(ESC);
5083 return;
5084 }
5085
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005086 // save line for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (u_save_cursor() == FAIL)
5088 return;
5089
5090 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5091 {
5092 /*
5093 * Replace character(s) by a single newline.
5094 * Strange vi behaviour: Only one newline is inserted.
5095 * Delete the characters here.
5096 * Insert the newline with an insert command, takes care of
5097 * autoindent. The insert command depends on being on the last
5098 * character of a line or not.
5099 */
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005100 (void)del_chars(cap->count1, FALSE); // delete the characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 stuffcharReadbuff('\r');
5102 stuffcharReadbuff(ESC);
5103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005104 // Give 'r' to edit(), to get the redo command right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 invoke_edit(cap, TRUE, 'r', FALSE);
5106 }
5107 else
5108 {
5109 prep_redo(cap->oap->regname, cap->count1,
5110 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5111
5112 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (has_mbyte)
5114 {
5115 int old_State = State;
5116
5117 if (cap->ncharC1 != 0)
5118 AppendCharToRedobuff(cap->ncharC1);
5119 if (cap->ncharC2 != 0)
5120 AppendCharToRedobuff(cap->ncharC2);
5121
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005122 // This is slow, but it handles replacing a single-byte with a
5123 // multi-byte and the other way around. Also handles adding
5124 // composing characters for utf-8.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 for (n = cap->count1; n > 0; --n)
5126 {
5127 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005128 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5129 {
5130 int c = ins_copychar(curwin->w_cursor.lnum
5131 + (cap->nchar == Ctrl_Y ? -1 : 1));
5132 if (c != NUL)
5133 ins_char(c);
5134 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005135 // will be decremented further down
Bram Moolenaar8320da42012-04-30 18:18:47 +02005136 ++curwin->w_cursor.col;
5137 }
5138 else
5139 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 State = old_State;
5141 if (cap->ncharC1 != 0)
5142 ins_char(cap->ncharC1);
5143 if (cap->ncharC2 != 0)
5144 ins_char(cap->ncharC2);
5145 }
5146 }
5147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 /*
5150 * Replace the characters within one line.
5151 */
5152 for (n = cap->count1; n > 0; --n)
5153 {
5154 /*
5155 * Get ptr again, because u_save and/or showmatch() will have
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005156 * released the line. This may also happen in ins_copychar().
5157 * At the same time we let know that the line will be changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02005159 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5160 {
5161 int c = ins_copychar(curwin->w_cursor.lnum
5162 + (cap->nchar == Ctrl_Y ? -1 : 1));
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005163
5164 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005165 if (c != NUL)
5166 ptr[curwin->w_cursor.col] = c;
5167 }
5168 else
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005169 {
5170 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005171 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar35a9a002021-09-11 21:14:20 +02005172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (p_sm && msg_silent == 0)
5174 showmatch(cap->nchar);
5175 ++curwin->w_cursor.col;
5176 }
5177#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005178 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005180 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181
Bram Moolenaar009b2592004-10-24 19:18:58 +00005182 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005183 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005185 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187#endif
5188
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005189 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 changed_bytes(curwin->w_cursor.lnum,
5191 (colnr_T)(curwin->w_cursor.col - cap->count1));
5192 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005193 --curwin->w_cursor.col; // cursor on the last replaced char
5194 // if the character on the left of the current cursor is a multi-byte
5195 // character, move two characters left
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 if (has_mbyte)
5197 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 curbuf->b_op_end = curwin->w_cursor;
5199 curwin->w_set_curswant = TRUE;
5200 set_last_insert(cap->nchar);
5201 }
5202}
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204/*
5205 * 'o': Exchange start and end of Visual area.
5206 * 'O': same, but in block mode exchange left and right corners.
5207 */
5208 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005209v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
5211 pos_T old_cursor;
5212 colnr_T left, right;
5213
5214 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5215 {
5216 old_cursor = curwin->w_cursor;
5217 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5218 curwin->w_cursor.lnum = VIsual.lnum;
5219 coladvance(left);
5220 VIsual = curwin->w_cursor;
5221
5222 curwin->w_cursor.lnum = old_cursor.lnum;
5223 curwin->w_curswant = right;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005224 // 'selection "exclusive" and cursor at right-bottom corner: move it
5225 // right one column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5227 ++curwin->w_curswant;
5228 coladvance(curwin->w_curswant);
5229 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005231 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
5233 curwin->w_cursor.lnum = VIsual.lnum;
5234 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5235 ++right;
5236 coladvance(right);
5237 VIsual = curwin->w_cursor;
5238
5239 curwin->w_cursor.lnum = old_cursor.lnum;
5240 coladvance(left);
5241 curwin->w_curswant = left;
5242 }
5243 }
5244 else
5245 {
5246 old_cursor = curwin->w_cursor;
5247 curwin->w_cursor = VIsual;
5248 VIsual = old_cursor;
5249 curwin->w_set_curswant = TRUE;
5250 }
5251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253/*
5254 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
5255 */
5256 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005257nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005259 if (VIsual_active) // "R" is replace lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
5261 cap->cmdchar = 'c';
5262 cap->nchar = NUL;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005263 VIsual_mode_orig = VIsual_mode; // remember original area for gv
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 VIsual_mode = 'V';
5265 nv_operator(cap);
5266 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005267 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
5269 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005270 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 else
5272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 if (virtual_active())
5274 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
5276 }
5277 }
5278}
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280/*
5281 * "gr".
5282 */
5283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005284nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (VIsual_active)
5287 {
5288 cap->cmdchar = 'r';
5289 cap->nchar = cap->extra_char;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005290 nv_replace(cap); // Do same as "r" in Visual mode for now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005292 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
5294 if (!curbuf->b_p_ma)
Bram Moolenaar108010a2021-06-27 22:03:33 +02005295 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 else
5297 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005298 if (cap->extra_char == Ctrl_V) // get another character
Bram Moolenaar0684e362020-12-03 19:54:42 +01005299 cap->extra_char = get_literal(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 stuffcharReadbuff(cap->extra_char);
5301 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (virtual_active())
5303 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 invoke_edit(cap, TRUE, 'v', FALSE);
5305 }
5306 }
5307}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
5309/*
5310 * Swap case for "~" command, when it does not work like an operator.
5311 */
5312 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005313n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 long n;
5316 pos_T startpos;
5317 int did_change = 0;
5318#ifdef FEAT_NETBEANS_INTG
5319 pos_T pos;
5320 char_u *ptr;
5321 int count;
5322#endif
5323
5324 if (checkclearopq(cap->oap))
5325 return;
5326
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005327 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
5329 clearopbeep(cap->oap);
5330 return;
5331 }
5332
5333 prep_redo_cmd(cap);
5334
5335 if (u_save_cursor() == FAIL)
5336 return;
5337
5338 startpos = curwin->w_cursor;
5339#ifdef FEAT_NETBEANS_INTG
5340 pos = startpos;
5341#endif
5342 for (n = cap->count1; n > 0; --n)
5343 {
5344 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
5345 inc_cursor();
5346 if (gchar_cursor() == NUL)
5347 {
5348 if (vim_strchr(p_ww, '~') != NULL
5349 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5350 {
5351#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005352 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
5354 if (did_change)
5355 {
5356 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005357 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005358 netbeans_removed(curbuf, pos.lnum, pos.col,
5359 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005361 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363 pos.col = 0;
5364 pos.lnum++;
5365 }
5366#endif
5367 ++curwin->w_cursor.lnum;
5368 curwin->w_cursor.col = 0;
5369 if (n > 1)
5370 {
5371 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
5372 break;
5373 u_clearline();
5374 }
5375 }
5376 else
5377 break;
5378 }
5379 }
5380#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005381 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
5383 ptr = ml_get(pos.lnum);
5384 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00005385 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
5386 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388#endif
5389
5390
5391 check_cursor();
5392 curwin->w_set_curswant = TRUE;
5393 if (did_change)
5394 {
5395 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
5396 0L);
5397 curbuf->b_op_start = startpos;
5398 curbuf->b_op_end = curwin->w_cursor;
5399 if (curbuf->b_op_end.col > 0)
5400 --curbuf->b_op_end.col;
5401 }
5402}
5403
5404/*
5405 * Move cursor to mark.
5406 */
5407 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005408nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 if (check_mark(pos) == FAIL)
5411 clearop(cap->oap);
5412 else
5413 {
5414 if (cap->cmdchar == '\''
5415 || cap->cmdchar == '`'
5416 || cap->cmdchar == '['
5417 || cap->cmdchar == ']')
5418 setpcmark();
5419 curwin->w_cursor = *pos;
5420 if (flag)
5421 beginline(BL_WHITE | BL_FIX);
5422 else
5423 check_cursor();
5424 }
5425 cap->oap->motion_type = flag ? MLINE : MCHAR;
5426 if (cap->cmdchar == '`')
5427 cap->oap->use_reg_one = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005428 cap->oap->inclusive = FALSE; // ignored if not MCHAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 curwin->w_set_curswant = TRUE;
5430}
5431
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432/*
5433 * Handle commands that are operators in Visual mode.
5434 */
5435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005436v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437{
5438 static char_u trans[] = "YyDdCcxdXdAAIIrr";
5439
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005440 // Uppercase means linewise, except in block mode, then "D" deletes till
5441 // the end of the line, and "C" replaces till EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 if (isupper(cap->cmdchar))
5443 {
5444 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01005445 {
5446 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
5450 curwin->w_curswant = MAXCOL;
5451 }
5452 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
5453 nv_operator(cap);
5454}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
5456/*
5457 * "s" and "S" commands.
5458 */
5459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005460nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
Bram Moolenaard96ff162018-02-18 22:13:29 +01005462#ifdef FEAT_TERMINAL
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005463 // When showing output of term_dumpdiff() swap the top and bottom.
Bram Moolenaard96ff162018-02-18 22:13:29 +01005464 if (term_swap_diff() == OK)
5465 return;
5466#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005467#ifdef FEAT_JOB_CHANNEL
5468 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5469 {
5470 clearopbeep(cap->oap);
5471 return;
5472 }
5473#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005474 if (VIsual_active) // "vs" and "vS" are the same as "vc"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
5476 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01005477 {
5478 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01005480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 cap->cmdchar = 'c';
5482 nv_operator(cap);
5483 }
5484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 nv_optrans(cap);
5486}
5487
5488/*
5489 * Abbreviated commands.
5490 */
5491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005492nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005495 cap->cmdchar = 'x'; // DEL key behaves like 'x'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005497 // in Visual mode these commands are operators
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 if (VIsual_active)
5499 v_visop(cap);
5500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 nv_optrans(cap);
5502}
5503
5504/*
5505 * Translate a command into another command.
5506 */
5507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005508nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
5510 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
5511 (char_u *)"d$", (char_u *)"c$",
5512 (char_u *)"cl", (char_u *)"cc",
5513 (char_u *)"yy", (char_u *)":s\r"};
5514 static char_u *str = (char_u *)"xXDCsSY&";
5515
5516 if (!checkclearopq(cap->oap))
5517 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02005518 // In Vi "2D" doesn't delete the next line. Can't translate it
5519 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005520 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
5521 {
5522 cap->oap->start = curwin->w_cursor;
5523 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00005524#ifdef FEAT_EVAL
5525 set_op_var(OP_DELETE);
5526#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005527 cap->count1 = 1;
5528 nv_dollar(cap);
5529 finish_op = TRUE;
5530 ResetRedobuff();
5531 AppendCharToRedobuff('D');
5532 }
5533 else
5534 {
5535 if (cap->count0)
5536 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02005537 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
5540 cap->opcount = 0;
5541}
5542
5543/*
5544 * "'" and "`" commands. Also for "g'" and "g`".
5545 * cap->arg is TRUE for "'" and "g'".
5546 */
5547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005548nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549{
5550 pos_T *pos;
5551 int c;
5552#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01005553 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005554 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555#endif
5556
5557 if (cap->cmdchar == 'g')
5558 c = cap->extra_char;
5559 else
5560 c = cap->nchar;
5561 pos = getmark(c, (cap->oap->op_type == OP_NOP));
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005562 if (pos == (pos_T *)-1) // jumped to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
5564 if (cap->arg)
5565 {
5566 check_cursor_lnum();
5567 beginline(BL_WHITE | BL_FIX);
5568 }
5569 else
5570 check_cursor();
5571 }
5572 else
5573 nv_cursormark(cap, cap->arg, pos);
5574
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005575 // May need to clear the coladd that a mark includes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 if (!virtual_active())
5577 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02005578 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579#ifdef FEAT_FOLDING
5580 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01005581 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005582 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 && (fdo_flags & FDO_MARK)
5584 && old_KeyTyped)
5585 foldOpenCursor();
5586#endif
5587}
5588
5589/*
Bram Moolenaar62a23252020-08-09 14:04:42 +02005590 * Handle CTRL-O, CTRL-I, "g;", "g," and "CTRL-Tab" commands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 */
5592 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005593nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 pos_T *pos;
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005596#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005598 int old_KeyTyped = KeyTyped; // getting file may reset it
Bram Moolenaar739f13a2021-12-13 13:12:53 +00005599#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600
5601 if (!checkclearopq(cap->oap))
5602 {
Bram Moolenaar62a23252020-08-09 14:04:42 +02005603 if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL)
5604 {
5605 if (goto_tabpage_lastused() == FAIL)
5606 clearopbeep(cap->oap);
5607 return;
5608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if (cap->cmdchar == 'g')
5610 pos = movechangelist((int)cap->count1);
5611 else
5612 pos = movemark((int)cap->count1);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005613 if (pos == (pos_T *)-1) // jump to other file
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
5615 curwin->w_set_curswant = TRUE;
5616 check_cursor();
5617 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005618 else if (pos != NULL) // can jump
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 nv_cursormark(cap, FALSE, pos);
5620 else if (cap->cmdchar == 'g')
5621 {
5622 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005623 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005625 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005627 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 }
5629 else
5630 clearopbeep(cap->oap);
5631# ifdef FEAT_FOLDING
5632 if (cap->oap->op_type == OP_NOP
5633 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
5634 && (fdo_flags & FDO_MARK)
5635 && old_KeyTyped)
5636 foldOpenCursor();
5637# endif
5638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639}
5640
5641/*
5642 * Handle '"' command.
5643 */
5644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005645nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
5647 if (checkclearop(cap->oap))
5648 return;
5649#ifdef FEAT_EVAL
5650 if (cap->nchar == '=')
5651 cap->nchar = get_expr_register();
5652#endif
5653 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
5654 {
5655 cap->oap->regname = cap->nchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005656 cap->opcount = cap->count0; // remember count before '"'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657#ifdef FEAT_EVAL
5658 set_reg_var(cap->oap->regname);
5659#endif
5660 }
5661 else
5662 clearopbeep(cap->oap);
5663}
5664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665/*
5666 * Handle "v", "V" and "CTRL-V" commands.
5667 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
5668 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00005669 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 */
5671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005672nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005674 if (cap->cmdchar == Ctrl_Q)
5675 cap->cmdchar = Ctrl_V;
5676
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005677 // 'v', 'V' and CTRL-V can be used while an operator is pending to make it
5678 // characterwise, linewise, or blockwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (cap->oap->op_type != OP_NOP)
5680 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01005681 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005682 finish_op = FALSE; // operator doesn't finish now but later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 return;
5684 }
5685
5686 VIsual_select = cap->arg;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005687 if (VIsual_active) // change Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005689 if (VIsual_mode == cap->cmdchar) // stop visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 end_visual_mode();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005691 else // toggle char/block mode
5692 { // or char/line mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 VIsual_mode = cap->cmdchar;
5694 showmode();
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02005695 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005697 redraw_curbuf_later(INVERTED); // update the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005699 else // start Visual mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
5701 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005702 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005704 // use previously selected part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 VIsual = curwin->w_cursor;
5706
5707 VIsual_active = TRUE;
5708 VIsual_reselect = TRUE;
5709 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005710 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00005713 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005714 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 /*
5716 * For V and ^V, we multiply the number of lines even if there
5717 * was only one -- webb
5718 */
5719 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
5720 {
5721 curwin->w_cursor.lnum +=
5722 resel_VIsual_line_count * cap->count0 - 1;
Bram Moolenaarb07626d2021-10-11 15:40:43 +01005723 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725 VIsual_mode = resel_VIsual_mode;
5726 if (VIsual_mode == 'v')
5727 {
5728 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005729 {
5730 validate_virtcol();
5731 curwin->w_curswant = curwin->w_virtcol
5732 + resel_VIsual_vcol * cap->count0 - 1;
5733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005735 curwin->w_curswant = resel_VIsual_vcol;
5736 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005738 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
5740 curwin->w_curswant = MAXCOL;
5741 coladvance((colnr_T)MAXCOL);
5742 }
5743 else if (VIsual_mode == Ctrl_V)
5744 {
5745 validate_virtcol();
5746 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02005747 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 coladvance(curwin->w_curswant);
5749 }
5750 else
5751 curwin->w_set_curswant = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005752 redraw_curbuf_later(INVERTED); // show the inversion
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754 else
5755 {
5756 if (!cap->arg)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005757 // start Select mode when 'selectmode' contains "cmd"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 may_start_select('c');
5759 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005760 if (VIsual_mode != 'V' && *p_sel == 'e')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005761 ++cap->count1; // include one more char
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005762 if (cap->count0 > 0 && --cap->count1 > 0)
5763 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005764 // With a count select that many characters or lines.
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02005765 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
5766 nv_right(cap);
5767 else if (VIsual_mode == 'V')
5768 nv_down(cap);
5769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771 }
5772}
5773
5774/*
5775 * Start selection for Shift-movement keys.
5776 */
5777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005778start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005780 // if 'selectmode' contains "key", start Select mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 may_start_select('k');
5782 n_start_visual_mode('v');
5783}
5784
5785/*
5786 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
5787 */
5788 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005789may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 VIsual_select = (stuff_empty() && typebuf_typed()
5792 && (vim_strchr(p_slm, c) != NULL));
5793}
5794
5795/*
5796 * Start Visual mode "c".
5797 * Should set VIsual_select before calling this.
5798 */
5799 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005800n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005802#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005803 int cursor_line_was_concealed = curwin->w_p_cole > 0
5804 && conceal_cursor_line(curwin);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005805#endif
5806
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 VIsual_mode = c;
5808 VIsual_active = TRUE;
5809 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005810
5811 // Corner case: the 0 position in a tab may change when going into
Bram Moolenaar4b96df52020-01-26 22:00:26 +01005812 // virtualedit. Recalculate curwin->w_cursor to avoid bad highlighting.
Gary Johnson53ba05b2021-07-26 22:19:10 +02005813 if (c == Ctrl_V && (get_ve_flags() & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005814 {
5815 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02005817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 VIsual = curwin->w_cursor;
5819
5820#ifdef FEAT_FOLDING
5821 foldAdjustVisual();
5822#endif
5823
Bram Moolenaara0620062021-11-17 16:52:40 +00005824 trigger_modechanged();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005826#ifdef FEAT_CONCEAL
Bram Moolenaarea042672021-06-29 20:22:32 +02005827 // Check if redraw is needed after changing the state.
5828 conceal_check_cursor_line(cursor_line_was_concealed);
Bram Moolenaarf5963f72010-07-23 22:10:27 +02005829#endif
5830
Bram Moolenaar09df3122006-01-23 22:23:09 +00005831 if (p_smd && msg_silent == 0)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005832 redraw_cmdline = TRUE; // show visual mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005834 // Make sure the clipboard gets updated. Needed because start and
5835 // end may still be the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 clip_star.vmode = NUL;
5837#endif
5838
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005839 // Only need to redraw this line, unless still need to redraw an old
5840 // Visual area (when 'lazyredraw' is set).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 if (curwin->w_redr_type < INVERTED)
5842 {
5843 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
5844 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
5845 }
5846}
5847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849/*
5850 * CTRL-W: Window commands
5851 */
5852 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005853nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
Bram Moolenaar938783d2017-07-16 20:13:26 +02005855 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005856 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005857 // "CTRL-W :" is the same as typing ":"; useful in a terminal window
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005858 cap->cmdchar = ':';
5859 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02005860 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01005861 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02005862 else if (!checkclearop(cap->oap))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005863 do_window(cap->nchar, cap->count0, NUL); // everything is in window.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864}
5865
5866/*
5867 * CTRL-Z: Suspend
5868 */
5869 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005870nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
5872 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (VIsual_active)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005874 end_visual_mode(); // stop Visual mode
Bram Moolenaar100118c2020-12-11 19:30:34 +01005875 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876}
5877
5878/*
5879 * Commands starting with "g".
5880 */
5881 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005882nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 int i;
5887 int flag = FALSE;
5888
5889 switch (cap->nchar)
5890 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005891 case Ctrl_A:
5892 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#ifdef MEM_PROFILE
5894 /*
5895 * "g^A": dump log of used memory.
5896 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005897 if (!VIsual_active && cap->nchar == Ctrl_A)
5898 vim_mem_profile_dump();
5899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005901 /*
5902 * "g^A/g^X": sequentially increment visually selected region
5903 */
5904 if (VIsual_active)
5905 {
5906 cap->arg = TRUE;
5907 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01005908 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02005909 nv_addsub(cap);
5910 }
5911 else
5912 clearopbeep(oap);
5913 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 /*
5916 * "gR": Enter virtual replace mode.
5917 */
5918 case 'R':
5919 cap->arg = TRUE;
5920 nv_Replace(cap);
5921 break;
5922
5923 case 'r':
5924 nv_vreplace(cap);
5925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926
5927 case '&':
5928 do_cmdline_cmd((char_u *)"%s//~/&");
5929 break;
5930
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 /*
5932 * "gv": Reselect the previous Visual area. If Visual already active,
5933 * exchange previous and current Visual area.
5934 */
5935 case 'v':
5936 if (checkclearop(oap))
5937 break;
5938
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005939 if ( curbuf->b_visual.vi_start.lnum == 0
5940 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
5941 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 beep_flush();
5943 else
5944 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005945 // set w_cursor to the start of the Visual area, tpos to the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 if (VIsual_active)
5947 {
5948 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005949 VIsual_mode = curbuf->b_visual.vi_mode;
5950 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951# ifdef FEAT_EVAL
5952 curbuf->b_visual_mode_eval = i;
5953# endif
5954 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005955 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5956 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005958 tpos = curbuf->b_visual.vi_end;
5959 curbuf->b_visual.vi_end = curwin->w_cursor;
5960 curwin->w_cursor = curbuf->b_visual.vi_start;
5961 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 }
5963 else
5964 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005965 VIsual_mode = curbuf->b_visual.vi_mode;
5966 curwin->w_curswant = curbuf->b_visual.vi_curswant;
5967 tpos = curbuf->b_visual.vi_end;
5968 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 }
5970
5971 VIsual_active = TRUE;
5972 VIsual_reselect = TRUE;
5973
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005974 // Set Visual to the start and w_cursor to the end of the Visual
5975 // area. Make sure they are on an existing character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 check_cursor();
5977 VIsual = curwin->w_cursor;
5978 curwin->w_cursor = tpos;
5979 check_cursor();
5980 update_topline();
5981 /*
5982 * When called from normal "g" command: start Select mode when
5983 * 'selectmode' contains "cmd". When called for K_SELECT, always
5984 * start Select mode.
5985 */
5986 if (cap->arg)
5987 VIsual_select = TRUE;
5988 else
5989 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991#ifdef FEAT_CLIPBOARD
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01005992 // Make sure the clipboard gets updated. Needed because start and
5993 // end are still the same, and the selection needs to be owned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 clip_star.vmode = NUL;
5995#endif
5996 redraw_curbuf_later(INVERTED);
5997 showmode();
5998 }
5999 break;
6000 /*
6001 * "gV": Don't reselect the previous Visual area after a Select mode
6002 * mapping of menu.
6003 */
6004 case 'V':
6005 VIsual_reselect = FALSE;
6006 break;
6007
6008 /*
6009 * "gh": start Select mode.
6010 * "gH": start Select line mode.
6011 * "g^H": start Select block mode.
6012 */
6013 case K_BS:
6014 cap->nchar = Ctrl_H;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006015 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 case 'h':
6017 case 'H':
6018 case Ctrl_H:
6019# ifdef EBCDIC
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006020 // EBCDIC: 'v'-'h' != '^v'-'^h'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 if (cap->nchar == Ctrl_H)
6022 cap->cmdchar = Ctrl_V;
6023 else
6024# endif
6025 cap->cmdchar = cap->nchar + ('v' - 'h');
6026 cap->arg = TRUE;
6027 nv_visual(cap);
6028 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006029
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006030 // "gn", "gN" visually select next/previous search match
6031 // "gn" selects next match
6032 // "gN" selects previous match
Bram Moolenaar641e2862012-07-25 15:06:34 +02006033 case 'N':
6034 case 'n':
6035 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006036 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006037 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038
6039 /*
6040 * "gj" and "gk" two new funny movement keys -- up and down
6041 * movement based on *screen* line rather than *file* line.
6042 */
6043 case 'j':
6044 case K_DOWN:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006045 // with 'nowrap' it works just like the normal "j" command.
6046 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 {
6048 oap->motion_type = MLINE;
6049 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6050 }
6051 else
6052 i = nv_screengo(oap, FORWARD, cap->count1);
6053 if (i == FAIL)
6054 clearopbeep(oap);
6055 break;
6056
6057 case 'k':
6058 case K_UP:
Bram Moolenaare71996b2021-01-21 17:03:07 +01006059 // with 'nowrap' it works just like the normal "k" command.
6060 if (!curwin->w_p_wrap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 {
6062 oap->motion_type = MLINE;
6063 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6064 }
6065 else
6066 i = nv_screengo(oap, BACKWARD, cap->count1);
6067 if (i == FAIL)
6068 clearopbeep(oap);
6069 break;
6070
6071 /*
6072 * "gJ": join two lines without inserting a space.
6073 */
6074 case 'J':
6075 nv_join(cap);
6076 break;
6077
6078 /*
6079 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6080 * "gm": middle of "g0" and "g$".
6081 */
6082 case '^':
6083 flag = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006084 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085
6086 case '0':
6087 case 'm':
6088 case K_HOME:
6089 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 oap->motion_type = MCHAR;
6091 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006092 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006094 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 int width2 = width1 + curwin_col_off2();
6096
6097 validate_virtcol();
6098 i = 0;
6099 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6100 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6101 }
6102 else
6103 i = curwin->w_leftcol;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006104 // Go to the middle of the screen line. When 'number' or
6105 // 'relativenumber' is on and lines are wrapping the middle can be more
6106 // to the left.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006108 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 + ((curwin->w_p_wrap && i > 0)
6110 ? curwin_col_off2() : 0)) / 2;
6111 coladvance((colnr_T)i);
6112 if (flag)
6113 {
6114 do
6115 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006116 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006117 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
6119 curwin->w_set_curswant = TRUE;
6120 break;
6121
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006122 case 'M':
6123 {
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006124 oap->motion_type = MCHAR;
6125 oap->inclusive = FALSE;
Bram Moolenaar71c41252021-12-26 15:00:07 +00006126 i = linetabsize(ml_get_curline());
Bram Moolenaar8b530c12019-10-28 02:13:05 +01006127 if (cap->count0 > 0 && cap->count0 <= 100)
6128 coladvance((colnr_T)(i * cap->count0 / 100));
6129 else
6130 coladvance((colnr_T)(i / 2));
6131 curwin->w_set_curswant = TRUE;
6132 }
6133 break;
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 case '_':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006136 // "g_": to the last non-blank character in the line or <count> lines
6137 // downward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 cap->oap->motion_type = MCHAR;
6139 cap->oap->inclusive = TRUE;
6140 curwin->w_curswant = MAXCOL;
6141 if (cursor_down((long)(cap->count1 - 1),
6142 cap->oap->op_type == OP_NOP) == FAIL)
6143 clearopbeep(cap->oap);
6144 else
6145 {
6146 char_u *ptr = ml_get_curline();
6147
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006148 // In Visual mode we may end up after the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6150 --curwin->w_cursor.col;
6151
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006152 // Decrease the cursor column until it's on a non-blank.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006154 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 --curwin->w_cursor.col;
6156 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006157 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
6159 break;
6160
6161 case '$':
6162 case K_END:
6163 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 {
6165 int col_off = curwin_col_off();
6166
6167 oap->motion_type = MCHAR;
6168 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006169 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006171 curwin->w_curswant = MAXCOL; // so we stay at the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (cap->count1 == 1)
6173 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006174 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 int width2 = width1 + curwin_col_off2();
6176
6177 validate_virtcol();
6178 i = width1 - 1;
6179 if (curwin->w_virtcol >= (colnr_T)width1)
6180 i += ((curwin->w_virtcol - width1) / width2 + 1)
6181 * width2;
6182 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006183
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006184 // Make sure we stick in this column.
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006185 validate_virtcol();
6186 curwin->w_curswant = curwin->w_virtcol;
6187 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6189 {
6190 /*
6191 * Check for landing on a character that got split at
6192 * the end of the line. We do not want to advance to
6193 * the next screen line.
6194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 if (curwin->w_virtcol > (colnr_T)i)
6196 --curwin->w_cursor.col;
6197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
6199 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6200 clearopbeep(oap);
6201 }
6202 else
6203 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006204 if (cap->count1 > 1)
6205 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006206 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006207
Bram Moolenaar02631462017-09-22 15:20:32 +02006208 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006210
Bram Moolenaar74ede802021-05-29 19:18:01 +02006211 // if the character doesn't fit move one back
6212 if (curwin->w_cursor.col > 0
6213 && (*mb_ptr2cells)(ml_get_cursor()) > 1)
6214 {
6215 colnr_T vcol;
6216
6217 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &vcol);
6218 if (vcol >= curwin->w_leftcol + curwin->w_width - col_off)
6219 --curwin->w_cursor.col;
6220 }
6221
Bram Moolenaard5c82342019-07-27 18:44:57 +02006222 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006223 validate_virtcol();
6224 curwin->w_curswant = curwin->w_virtcol;
6225 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 }
6227 }
6228 break;
6229
6230 /*
6231 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6232 */
6233 case '*':
6234 case '#':
6235#if POUND != '#'
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006236 case POUND: // pound sign (sometimes equal to '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006238 case Ctrl_RSB: // :tag or :tselect for current identifier
6239 case ']': // :tselect for current identifier
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 nv_ident(cap);
6241 break;
6242
6243 /*
6244 * ge and gE: go back to end of word
6245 */
6246 case 'e':
6247 case 'E':
6248 oap->motion_type = MCHAR;
6249 curwin->w_set_curswant = TRUE;
6250 oap->inclusive = TRUE;
6251 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6252 clearopbeep(oap);
6253 break;
6254
6255 /*
6256 * "g CTRL-G": display info about cursor position
6257 */
6258 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006259 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 break;
6261
6262 /*
6263 * "gi": start Insert at the last position.
6264 */
6265 case 'i':
6266 if (curbuf->b_last_insert.lnum != 0)
6267 {
6268 curwin->w_cursor = curbuf->b_last_insert;
6269 check_cursor_lnum();
6270 i = (int)STRLEN(ml_get_curline());
6271 if (curwin->w_cursor.col > (colnr_T)i)
6272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 if (virtual_active())
6274 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 curwin->w_cursor.col = i;
6276 }
6277 }
6278 cap->cmdchar = 'i';
6279 nv_edit(cap);
6280 break;
6281
6282 /*
6283 * "gI": Start insert in column 1.
6284 */
6285 case 'I':
6286 beginline(0);
6287 if (!checkclearopq(oap))
6288 invoke_edit(cap, FALSE, 'g', FALSE);
6289 break;
6290
6291#ifdef FEAT_SEARCHPATH
6292 /*
6293 * "gf": goto file, edit file under cursor
6294 * "]f" and "[f": can also be used.
6295 */
6296 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006297 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 nv_gotofile(cap);
6299 break;
6300#endif
6301
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006302 // "g'm" and "g`m": jump to mark without setting pcmark
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 case '\'':
6304 cap->arg = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006305 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 case '`':
6307 nv_gomark(cap);
6308 break;
6309
6310 /*
6311 * "gs": Goto sleep.
6312 */
6313 case 's':
Bram Moolenaare2edc2e2021-01-16 20:21:23 +01006314 do_sleep(cap->count1 * 1000L, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 break;
6316
6317 /*
6318 * "ga": Display the ascii value of the character under the
6319 * cursor. It is displayed in decimal, hex, and octal. -- webb
6320 */
6321 case 'a':
6322 do_ascii(NULL);
6323 break;
6324
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 /*
6326 * "g8": Display the bytes used for the UTF-8 character under the
6327 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006328 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 */
6330 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00006331 if (cap->count0 == 8)
6332 utf_find_illegal();
6333 else
6334 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006337 // "g<": show scrollback text
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006338 case '<':
6339 show_sb_text();
6340 break;
6341
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 /*
6343 * "gg": Goto the first line in file. With a count it goes to
6344 * that line number like for "G". -- webb
6345 */
6346 case 'g':
6347 cap->arg = FALSE;
6348 nv_goto(cap);
6349 break;
6350
6351 /*
6352 * Two-character operators:
6353 * "gq" Format text
6354 * "gw" Format text and keep cursor position
6355 * "g~" Toggle the case of the text.
6356 * "gu" Change text to lower case.
6357 * "gU" Change text to upper case.
6358 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006359 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 */
6361 case 'q':
6362 case 'w':
6363 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006364 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 case '~':
6366 case 'u':
6367 case 'U':
6368 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00006369 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 nv_operator(cap);
6371 break;
6372
6373 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006374 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 * current function
6376 * "gD": idem, but in the current file.
6377 */
6378 case 'd':
6379 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00006380 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 break;
6382
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 /*
6384 * g<*Mouse> : <C-*mouse>
6385 */
6386 case K_MIDDLEMOUSE:
6387 case K_MIDDLEDRAG:
6388 case K_MIDDLERELEASE:
6389 case K_LEFTMOUSE:
6390 case K_LEFTDRAG:
6391 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006392 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 case K_RIGHTMOUSE:
6394 case K_RIGHTDRAG:
6395 case K_RIGHTRELEASE:
6396 case K_X1MOUSE:
6397 case K_X1DRAG:
6398 case K_X1RELEASE:
6399 case K_X2MOUSE:
6400 case K_X2DRAG:
6401 case K_X2RELEASE:
6402 mod_mask = MOD_MASK_CTRL;
6403 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
6404 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
6406 case K_IGNORE:
6407 break;
6408
6409 /*
6410 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
6411 */
6412 case 'p':
6413 case 'P':
6414 nv_put(cap);
6415 break;
6416
6417#ifdef FEAT_BYTEOFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006418 // "go": goto byte count from start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 case 'o':
6420 goto_byte(cap->count0);
6421 break;
6422#endif
6423
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006424 // "gQ": improved Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006426 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 {
6428 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006429 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 break;
6431 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006432
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 if (!checkclearopq(oap))
6434 do_exmode(TRUE);
6435 break;
6436
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 case ',':
6438 nv_pcmark(cap);
6439 break;
6440
6441 case ';':
6442 cap->count1 = -cap->count1;
6443 nv_pcmark(cap);
6444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006446 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006447 if (!checkclearop(oap))
6448 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006450 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02006451 if (!checkclearop(oap))
6452 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006453 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006454
Bram Moolenaar62a23252020-08-09 14:04:42 +02006455 case TAB:
6456 if (!checkclearop(oap) && goto_tabpage_lastused() == FAIL)
6457 clearopbeep(oap);
6458 break;
6459
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006460 case '+':
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006461 case '-': // "g+" and "g-": undo or redo along the timeline
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006462 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00006463 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02006464 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00006465 break;
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 default:
6468 clearopbeep(oap);
6469 break;
6470 }
6471}
6472
6473/*
6474 * Handle "o" and "O" commands.
6475 */
6476 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006477n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
Bram Moolenaar860cae12010-06-05 23:22:07 +02006479#ifdef FEAT_CONCEAL
6480 linenr_T oldline = curwin->w_cursor.lnum;
6481#endif
6482
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (!checkclearopq(cap->oap))
6484 {
6485#ifdef FEAT_FOLDING
6486 if (cap->cmdchar == 'O')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006487 // Open above the first line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 (void)hasFolding(curwin->w_cursor.lnum,
6489 &curwin->w_cursor.lnum, NULL);
6490 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006491 // Open below the last line of a folded sequence of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 (void)hasFolding(curwin->w_cursor.lnum,
6493 NULL, &curwin->w_cursor.lnum);
6494#endif
6495 if (u_save((linenr_T)(curwin->w_cursor.lnum -
6496 (cap->cmdchar == 'O' ? 1 : 0)),
6497 (linenr_T)(curwin->w_cursor.lnum +
6498 (cap->cmdchar == 'o' ? 1 : 0))
6499 ) == OK
6500 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
Bram Moolenaar8c96af92019-09-28 19:05:57 +02006501 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM : 0,
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00006502 0, NULL) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02006504#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006505 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01006506 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02006507#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006508#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006509 if (curwin->w_p_cul)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006510 // force redraw of cursorline
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02006511 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02006512#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006513 // When '#' is in 'cpoptions' ignore the count.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006514 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
6515 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
6517 }
6518 }
6519}
6520
6521/*
6522 * "." command: redo last change.
6523 */
6524 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006525nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 if (!checkclearopq(cap->oap))
6528 {
6529 /*
6530 * If "restart_edit" is TRUE, the last but one command is repeated
6531 * instead of the last command (inserting text). This is used for
6532 * CTRL-O <.> in insert mode.
6533 */
6534 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
6535 clearopbeep(cap->oap);
6536 }
6537}
6538
6539/*
6540 * CTRL-R: undo undo
6541 */
6542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006543nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544{
6545 if (!checkclearopq(cap->oap))
6546 {
6547 u_redo((int)cap->count1);
6548 curwin->w_set_curswant = TRUE;
6549 }
6550}
6551
6552/*
6553 * Handle "U" command.
6554 */
6555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006556nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006558 // In Visual mode and typing "gUU" triggers an operator
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006559 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006561 // translate "gUU" to "gUgU"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 cap->cmdchar = 'g';
6563 cap->nchar = 'U';
6564 nv_operator(cap);
6565 }
6566 else if (!checkclearopq(cap->oap))
6567 {
6568 u_undoline();
6569 curwin->w_set_curswant = TRUE;
6570 }
6571}
6572
6573/*
6574 * '~' command: If tilde is not an operator and Visual is off: swap case of a
6575 * single character.
6576 */
6577 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006578nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006580 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02006581 {
6582#ifdef FEAT_JOB_CHANNEL
6583 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6584 {
6585 clearopbeep(cap->oap);
6586 return;
6587 }
6588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 else
6592 nv_operator(cap);
6593}
6594
6595/*
6596 * Handle an operator command.
6597 * The actual work is done by do_pending_operator().
6598 */
6599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006600nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602 int op_type;
6603
6604 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02006605#ifdef FEAT_JOB_CHANNEL
6606 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
6607 {
6608 clearopbeep(cap->oap);
6609 return;
6610 }
6611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006613 if (op_type == cap->oap->op_type) // double operator works on lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 nv_lineop(cap);
6615 else if (!checkclearop(cap->oap))
6616 {
6617 cap->oap->start = curwin->w_cursor;
6618 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006619#ifdef FEAT_EVAL
6620 set_op_var(op_type);
6621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 }
6623}
6624
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006625#ifdef FEAT_EVAL
6626/*
6627 * Set v:operator to the characters for "optype".
6628 */
6629 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006631{
6632 char_u opchars[3];
6633
6634 if (optype == OP_NOP)
6635 set_vim_var_string(VV_OP, NULL, 0);
6636 else
6637 {
6638 opchars[0] = get_op_char(optype);
6639 opchars[1] = get_extra_op_char(optype);
6640 opchars[2] = NUL;
6641 set_vim_var_string(VV_OP, opchars, -1);
6642 }
6643}
6644#endif
6645
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646/*
6647 * Handle linewise operator "dd", "yy", etc.
6648 *
6649 * "_" is is a strange motion command that helps make operators more logical.
6650 * It is actually implemented, but not documented in the real Vi. This motion
6651 * command actually refers to "the current line". Commands like "dd" and "yy"
6652 * are really an alternate form of "d_" and "y_". It does accept a count, so
6653 * "d3_" works to delete 3 lines.
6654 */
6655 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006656nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
6658 cap->oap->motion_type = MLINE;
6659 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
6660 clearopbeep(cap->oap);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006661 else if ( (cap->oap->op_type == OP_DELETE // only with linewise motions
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01006662 && cap->oap->motion_force != 'v'
6663 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 || cap->oap->op_type == OP_LSHIFT
6665 || cap->oap->op_type == OP_RSHIFT)
6666 beginline(BL_SOL | BL_FIX);
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006667 else if (cap->oap->op_type != OP_YANK) // 'Y' does not move cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 beginline(BL_WHITE | BL_FIX);
6669}
6670
6671/*
6672 * <Home> command.
6673 */
6674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006675nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006677 // CTRL-HOME is like "gg"
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006678 if (mod_mask & MOD_MASK_CTRL)
6679 nv_goto(cap);
6680 else
6681 {
6682 cap->count0 = 1;
6683 nv_pipe(cap);
6684 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006685 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6686 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687}
6688
6689/*
6690 * "|" command.
6691 */
6692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006693nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 cap->oap->motion_type = MCHAR;
6696 cap->oap->inclusive = FALSE;
6697 beginline(0);
6698 if (cap->count0 > 0)
6699 {
6700 coladvance((colnr_T)(cap->count0 - 1));
6701 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
6702 }
6703 else
6704 curwin->w_curswant = 0;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006705 // keep curswant at the column where we wanted to go, not where
6706 // we ended; differs if line is too short
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 curwin->w_set_curswant = FALSE;
6708}
6709
6710/*
6711 * Handle back-word command "b" and "B".
6712 * cap->arg is 1 for "B"
6713 */
6714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006715nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717 cap->oap->motion_type = MCHAR;
6718 cap->oap->inclusive = FALSE;
6719 curwin->w_set_curswant = TRUE;
6720 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
6721 clearopbeep(cap->oap);
6722#ifdef FEAT_FOLDING
6723 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6724 foldOpenCursor();
6725#endif
6726}
6727
6728/*
6729 * Handle word motion commands "e", "E", "w" and "W".
6730 * cap->arg is TRUE for "E" and "W".
6731 */
6732 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006733nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 int n;
6736 int word_end;
6737 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00006738 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739
6740 /*
6741 * Set inclusive for the "E" and "e" command.
6742 */
6743 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
6744 word_end = TRUE;
6745 else
6746 word_end = FALSE;
6747 cap->oap->inclusive = word_end;
6748
6749 /*
6750 * "cw" and "cW" are a special case.
6751 */
6752 if (!word_end && cap->oap->op_type == OP_CHANGE)
6753 {
6754 n = gchar_cursor();
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006755 if (n != NUL) // not an empty line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006757 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
6759 /*
6760 * Reproduce a funny Vi behaviour: "cw" on a blank only
6761 * changes one character, not all blanks until the start of
6762 * the next word. Only do this when the 'w' flag is included
6763 * in 'cpoptions'.
6764 */
6765 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
6766 {
6767 cap->oap->inclusive = TRUE;
6768 cap->oap->motion_type = MCHAR;
6769 return;
6770 }
6771 }
6772 else
6773 {
6774 /*
6775 * This is a little strange. To match what the real Vi does,
6776 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
6777 * that we are not on a space or a TAB. This seems impolite
6778 * at first, but it's really more what we mean when we say
6779 * 'cw'.
6780 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006781 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 * will change only one character! This is done by setting
6783 * flag.
6784 */
6785 cap->oap->inclusive = TRUE;
6786 word_end = TRUE;
6787 flag = TRUE;
6788 }
6789 }
6790 }
6791
6792 cap->oap->motion_type = MCHAR;
6793 curwin->w_set_curswant = TRUE;
6794 if (word_end)
6795 n = end_word(cap->count1, cap->arg, flag, FALSE);
6796 else
6797 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
6798
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006799 // Don't leave the cursor on the NUL past the end of line. Unless we
6800 // didn't move it forward.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006801 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006802 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
6804 if (n == FAIL && cap->oap->op_type == OP_NOP)
6805 clearopbeep(cap->oap);
6806 else
6807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809#ifdef FEAT_FOLDING
6810 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6811 foldOpenCursor();
6812#endif
6813 }
6814}
6815
6816/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006817 * Used after a movement command: If the cursor ends up on the NUL after the
6818 * end of the line, may move it back to the last character and make the motion
6819 * inclusive.
6820 */
6821 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006822adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006823{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006824 // The cursor cannot remain on the NUL when:
6825 // - the column is > 0
6826 // - not in Visual mode or 'selection' is "o"
6827 // - 'virtualedit' is not "all" and not "onemore".
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006828 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006829 && (!VIsual_active || *p_sel == 'o')
Gary Johnson53ba05b2021-07-26 22:19:10 +02006830 && !virtual_active() && (get_ve_flags() & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006831 {
6832 --curwin->w_cursor.col;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006833 // prevent cursor from moving on the trail byte
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006834 if (has_mbyte)
6835 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006836 oap->inclusive = TRUE;
6837 }
6838}
6839
6840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 * "0" and "^" commands.
6842 * cap->arg is the argument for beginline().
6843 */
6844 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006845nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 cap->oap->motion_type = MCHAR;
6848 cap->oap->inclusive = FALSE;
6849 beginline(cap->arg);
6850#ifdef FEAT_FOLDING
6851 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6852 foldOpenCursor();
6853#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006854 ins_at_eol = FALSE; // Don't move cursor past eol (only necessary in a
6855 // one-character line).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856}
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
6859 * In exclusive Visual mode, may include the last character.
6860 */
6861 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006862adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006865 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (has_mbyte)
6868 inc_cursor();
6869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 ++curwin->w_cursor.col;
6871 cap->oap->inclusive = FALSE;
6872 }
6873}
6874
6875/*
6876 * Exclude last character at end of Visual area for 'selection' == "exclusive".
6877 * Should check VIsual_mode before calling this.
6878 * Returns TRUE when backed up to the previous line.
6879 */
Bram Moolenaar792cf5e2019-09-30 23:12:16 +02006880 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006881unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882{
6883 pos_T *pp;
6884
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006885 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006887 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 pp = &curwin->w_cursor;
6889 else
6890 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 if (pp->coladd > 0)
6892 --pp->coladd;
6893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 if (pp->col > 0)
6895 {
6896 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02006897 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 }
6899 else if (pp->lnum > 1)
6900 {
6901 --pp->lnum;
6902 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
6903 return TRUE;
6904 }
6905 }
6906 return FALSE;
6907}
6908
6909/*
6910 * SELECT key in Normal or Visual mode: end of Select mode mapping.
6911 */
6912 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006913nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 if (VIsual_active)
6916 VIsual_select = TRUE;
6917 else if (VIsual_reselect)
6918 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006919 cap->nchar = 'v'; // fake "gv" command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 cap->arg = TRUE;
6921 nv_g_cmd(cap);
6922 }
6923}
6924
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
6926/*
6927 * "G", "gg", CTRL-END, CTRL-HOME.
6928 * cap->arg is TRUE for "G".
6929 */
6930 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006931nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932{
6933 linenr_T lnum;
6934
6935 if (cap->arg)
6936 lnum = curbuf->b_ml.ml_line_count;
6937 else
6938 lnum = 1L;
6939 cap->oap->motion_type = MLINE;
6940 setpcmark();
6941
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006942 // When a count is given, use it instead of the default lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 if (cap->count0 != 0)
6944 lnum = cap->count0;
6945 if (lnum < 1L)
6946 lnum = 1L;
6947 else if (lnum > curbuf->b_ml.ml_line_count)
6948 lnum = curbuf->b_ml.ml_line_count;
6949 curwin->w_cursor.lnum = lnum;
6950 beginline(BL_SOL | BL_FIX);
6951#ifdef FEAT_FOLDING
6952 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
6953 foldOpenCursor();
6954#endif
6955}
6956
6957/*
6958 * CTRL-\ in Normal mode.
6959 */
6960 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006961nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
6963 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
6964 {
6965 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006966 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006967 clear_cmdline = TRUE; // unshow mode later
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 restart_edit = 0;
6969#ifdef FEAT_CMDWIN
6970 if (cmdwin_type != 0)
6971 cmdwin_result = Ctrl_C;
6972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (VIsual_active)
6974 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006975 end_visual_mode(); // stop Visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 redraw_curbuf_later(INVERTED);
6977 }
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01006978 // CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 if (cap->nchar == Ctrl_G && p_im)
6980 restart_edit = 'a';
6981 }
6982 else
6983 clearopbeep(cap->oap);
6984}
6985
6986/*
6987 * ESC in Normal mode: beep, but don't flush buffers.
6988 * Don't even beep if we are canceling a command.
6989 */
6990 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006991nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
6993 int no_reason;
6994
6995 no_reason = (cap->oap->op_type == OP_NOP
6996 && cap->opcount == 0
6997 && cap->count0 == 0
6998 && cap->oap->regname == 0
6999 && !p_im);
7000
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007001 if (cap->arg) // TRUE for CTRL-C
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
7003 if (restart_edit == 0
7004#ifdef FEAT_CMDWIN
7005 && cmdwin_type == 0
7006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007009 {
7010 if (anyBufIsChanged())
7011 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
7012 else
7013 msg(_("Type :qa and press <Enter> to exit Vim"));
7014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007016 // Don't reset "restart_edit" when 'insertmode' is set, it won't be
7017 // set again below when halfway a mapping.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 if (!p_im)
7019 restart_edit = 0;
7020#ifdef FEAT_CMDWIN
7021 if (cmdwin_type != 0)
7022 {
7023 cmdwin_result = K_IGNORE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007024 got_int = FALSE; // don't stop executing autocommands et al.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 return;
7026 }
7027#endif
7028 }
Bram Moolenaar7d414102021-02-23 19:39:20 +01007029#ifdef FEAT_CMDWIN
7030 else if (cmdwin_type != 0 && ex_normal_busy)
7031 {
7032 // When :normal runs out of characters while in the command line window
7033 // vgetorpeek() will return ESC. Exit the cmdline window to break the
7034 // loop.
7035 cmdwin_result = K_IGNORE;
7036 return;
7037 }
7038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 if (VIsual_active)
7041 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007042 end_visual_mode(); // stop Visual
7043 check_cursor_col(); // make sure cursor is not beyond EOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 curwin->w_set_curswant = TRUE;
7045 redraw_curbuf_later(INVERTED);
7046 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007047 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007048 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 clearop(cap->oap);
7050
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007051 // A CTRL-C is often used at the start of a menu. When 'insertmode' is
7052 // set return to Insert mode afterwards.
Bram Moolenaare2c38102016-01-31 14:55:40 +01007053 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 restart_edit = 'a';
7055}
7056
7057/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007058 * Move the cursor for the "A" command.
7059 */
7060 void
7061set_cursor_for_append_to_line(void)
7062{
7063 curwin->w_set_curswant = TRUE;
Gary Johnson53ba05b2021-07-26 22:19:10 +02007064 if (get_ve_flags() == VE_ALL)
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007065 {
7066 int save_State = State;
7067
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007068 // Pretend Insert mode here to allow the cursor on the
7069 // character past the end of the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007070 State = INSERT;
7071 coladvance((colnr_T)MAXCOL);
7072 State = save_State;
7073 }
7074 else
7075 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7076}
7077
7078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007080 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 */
7082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007083nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007085 // <Insert> is equal to "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7087 cap->cmdchar = 'i';
7088
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007089 // in Visual mode "A" and "I" are an operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007091 {
7092#ifdef FEAT_TERMINAL
7093 if (term_in_normal_mode())
7094 {
7095 end_visual_mode();
7096 clearop(cap->oap);
7097 term_enter_job_mode();
7098 return;
7099 }
7100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007104 // in Visual mode and after an operator "a" and "i" are for text objects
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007105 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7106 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 {
7108#ifdef FEAT_TEXTOBJ
7109 nv_object(cap);
7110#else
7111 clearopbeep(cap->oap);
7112#endif
7113 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007114#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007115 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007116 {
7117 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007118 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007119 return;
7120 }
7121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 else if (!curbuf->b_p_ma && !p_im)
7123 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007124 // Only give this error when 'insertmode' is off.
Bram Moolenaar108010a2021-06-27 22:03:33 +02007125 emsg(_(e_cannot_make_changes_modifiable_is_off));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007127 if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007128 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007129 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007131 else if (cap->cmdchar == K_PS && VIsual_active)
7132 {
7133 pos_T old_pos = curwin->w_cursor;
7134 pos_T old_visual = VIsual;
7135
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007136 // In Visual mode the selected text is deleted.
Bram Moolenaara1891842017-02-04 21:34:31 +01007137 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7138 {
7139 shift_delete_registers();
7140 cap->oap->regname = '1';
7141 }
7142 else
7143 cap->oap->regname = '-';
7144 cap->cmdchar = 'd';
7145 cap->nchar = NUL;
7146 nv_operator(cap);
7147 do_pending_operator(cap, 0, FALSE);
7148 cap->cmdchar = K_PS;
7149
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007150 // When the last char in the line was deleted then append. Detect this
7151 // by checking if the cursor moved to before the Visual area.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007152 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7153 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007154 inc_cursor();
7155
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007156 // Insert to replace the deleted text with the pasted text.
Bram Moolenaara1891842017-02-04 21:34:31 +01007157 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 else if (!checkclearopq(cap->oap))
7160 {
7161 switch (cap->cmdchar)
7162 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007163 case 'A': // "A"ppend after the line
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007164 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 break;
7166
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007167 case 'I': // "I"nsert before the first non-blank
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007168 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7169 beginline(BL_WHITE);
7170 else
7171 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 break;
7173
Bram Moolenaara1891842017-02-04 21:34:31 +01007174 case K_PS:
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007175 // Bracketed paste works like "a"ppend, unless the cursor is in
7176 // the first column, then it inserts.
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007177 if (curwin->w_cursor.col == 0)
7178 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007179 // FALLTHROUGH
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007180
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007181 case 'a': // "a"ppend is like "i"nsert on the next character.
7182 // increment coladd when in virtual space, increment the
7183 // column otherwise, also to append after an unprintable char
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (virtual_active()
7185 && (curwin->w_cursor.coladd > 0
7186 || *ml_get_cursor() == NUL
7187 || *ml_get_cursor() == TAB))
7188 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007189 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 inc_cursor();
7191 break;
7192 }
7193
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7195 {
7196 int save_State = State;
7197
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007198 // Pretend Insert mode here to allow the cursor on the
7199 // character past the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 State = INSERT;
7201 coladvance(getviscol());
7202 State = save_State;
7203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204
7205 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7206 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007207 else if (cap->cmdchar == K_PS)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007208 // drop the pasted text
Bram Moolenaarec2da362017-01-21 20:04:22 +01007209 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210}
7211
7212/*
7213 * Invoke edit() and take care of "restart_edit" and the return value.
7214 */
7215 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007216invoke_edit(
7217 cmdarg_T *cap,
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007218 int repl, // "r" or "gr" command
Bram Moolenaar9b578142016-01-30 19:39:49 +01007219 int cmd,
7220 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222 int restart_edit_save = 0;
7223
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007224 // Complicated: When the user types "a<C-O>a" we don't want to do Insert
7225 // mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7226 // it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 if (repl || !stuff_empty())
7228 restart_edit_save = restart_edit;
7229 else
7230 restart_edit_save = 0;
7231
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007232 // Always reset "restart_edit", this is not a restarted edit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 restart_edit = 0;
7234
7235 if (edit(cmd, startln, cap->count1))
7236 cap->retval |= CA_COMMAND_BUSY;
7237
7238 if (restart_edit == 0)
7239 restart_edit = restart_edit_save;
7240}
7241
7242#ifdef FEAT_TEXTOBJ
7243/*
7244 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7245 */
7246 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007247nv_object(
7248 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
7250 int flag;
7251 int include;
7252 char_u *mps_save;
7253
7254 if (cap->cmdchar == 'i')
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007255 include = FALSE; // "ix" = inner object: exclude white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 else
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007257 include = TRUE; // "ax" = an object: include white space
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007259 // Make sure (), [], {} and <> are in 'matchpairs'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 mps_save = curbuf->b_p_mps;
7261 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7262
7263 switch (cap->nchar)
7264 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007265 case 'w': // "aw" = a word
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 flag = current_word(cap->oap, cap->count1, include, FALSE);
7267 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007268 case 'W': // "aW" = a WORD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 flag = current_word(cap->oap, cap->count1, include, TRUE);
7270 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007271 case 'b': // "ab" = a braces block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 case '(':
7273 case ')':
7274 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7275 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007276 case 'B': // "aB" = a Brackets block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 case '{':
7278 case '}':
7279 flag = current_block(cap->oap, cap->count1, include, '{', '}');
7280 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007281 case '[': // "a[" = a [] block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 case ']':
7283 flag = current_block(cap->oap, cap->count1, include, '[', ']');
7284 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007285 case '<': // "a<" = a <> block
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 case '>':
7287 flag = current_block(cap->oap, cap->count1, include, '<', '>');
7288 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007289 case 't': // "at" = a tag block (xml and html)
7290 // Do not adjust oap->end in do_pending_operator()
7291 // otherwise there are different results for 'dit'
7292 // (note leading whitespace in last line):
7293 // 1) <b> 2) <b>
7294 // foobar foobar
7295 // </b> </b>
Bram Moolenaarb6c27352015-03-05 19:57:49 +01007296 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00007297 flag = current_tagblock(cap->oap, cap->count1, include);
7298 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007299 case 'p': // "ap" = a paragraph
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 flag = current_par(cap->oap, cap->count1, include, 'p');
7301 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007302 case 's': // "as" = a sentence
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 flag = current_sent(cap->oap, cap->count1, include);
7304 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007305 case '"': // "a"" = a double quoted string
7306 case '\'': // "a'" = a single quoted string
7307 case '`': // "a`" = a backtick quoted string
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007308 flag = current_quote(cap->oap, cap->count1, include,
7309 cap->nchar);
7310 break;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007311#if 0 // TODO
7312 case 'S': // "aS" = a section
7313 case 'f': // "af" = a filename
7314 case 'u': // "au" = a URL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315#endif
7316 default:
7317 flag = FAIL;
7318 break;
7319 }
7320
7321 curbuf->b_p_mps = mps_save;
7322 if (flag == FAIL)
7323 clearopbeep(cap->oap);
7324 adjust_cursor_col();
7325 curwin->w_set_curswant = TRUE;
7326}
7327#endif
7328
7329/*
7330 * "q" command: Start/stop recording.
7331 * "q:", "q/", "q?": edit command-line in command-line window.
7332 */
7333 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007334nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
7336 if (cap->oap->op_type == OP_FORMAT)
7337 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007338 // "gqq" is the same as "gqgq": format line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 cap->cmdchar = 'g';
7340 cap->nchar = 'q';
7341 nv_operator(cap);
7342 }
7343 else if (!checkclearop(cap->oap))
7344 {
7345#ifdef FEAT_CMDWIN
7346 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
7347 {
7348 stuffcharReadbuff(cap->nchar);
7349 stuffcharReadbuff(K_CMDWIN);
7350 }
7351 else
7352#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007353 // (stop) recording into a named register, unless executing a
7354 // register
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02007355 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 clearopbeep(cap->oap);
7357 }
7358}
7359
7360/*
7361 * Handle the "@r" command.
7362 */
7363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007364nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365{
7366 if (checkclearop(cap->oap))
7367 return;
7368#ifdef FEAT_EVAL
7369 if (cap->nchar == '=')
7370 {
7371 if (get_expr_register() == NUL)
7372 return;
7373 }
7374#endif
7375 while (cap->count1-- && !got_int)
7376 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007377 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
7379 clearopbeep(cap->oap);
7380 break;
7381 }
7382 line_breakcheck();
7383 }
7384}
7385
7386/*
7387 * Handle the CTRL-U and CTRL-D commands.
7388 */
7389 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007390nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
7393 || (cap->cmdchar == Ctrl_D
7394 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
7395 clearopbeep(cap->oap);
7396 else if (!checkclearop(cap->oap))
7397 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
7398}
7399
7400/*
7401 * Handle "J" or "gJ" command.
7402 */
7403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007404nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007406 if (VIsual_active) // join the visual lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007408 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 {
7410 if (cap->count0 <= 1)
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007411 cap->count0 = 2; // default for join is two lines!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 if (curwin->w_cursor.lnum + cap->count0 - 1 >
7413 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007415 // can't join when on the last line
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007416 if (cap->count0 <= 2)
7417 {
7418 clearopbeep(cap->oap);
7419 return;
7420 }
7421 cap->count0 = curbuf->b_ml.ml_line_count
7422 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01007424
7425 prep_redo(cap->oap->regname, cap->count0,
7426 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
7427 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429}
7430
7431/*
7432 * "P", "gP", "p" and "gp" commands.
7433 */
7434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007435nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007437 nv_put_opt(cap, FALSE);
7438}
7439
7440/*
7441 * "P", "gP", "p" and "gp" commands.
7442 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
7443 */
7444 static void
7445nv_put_opt(cmdarg_T *cap, int fix_indent)
7446{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 int regname = 0;
7448 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007449 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007450 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 int dir;
7452 int flags = 0;
7453
7454 if (cap->oap->op_type != OP_NOP)
7455 {
7456#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007457 // "dp" is ":diffput"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
7459 {
7460 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007461 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 }
7463 else
7464#endif
7465 clearopbeep(cap->oap);
7466 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02007467#ifdef FEAT_JOB_CHANNEL
7468 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
7469 {
7470 clearopbeep(cap->oap);
7471 }
7472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 else
7474 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02007475 if (fix_indent)
7476 {
7477 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
7478 ? FORWARD : BACKWARD;
7479 flags |= PUT_FIXINDENT;
7480 }
7481 else
7482 dir = (cap->cmdchar == 'P'
Christian Brabandt2fa93842021-05-30 22:17:25 +02007483 || ((cap->cmdchar == 'g' || cap->cmdchar == 'z')
7484 && cap->nchar == 'P')) ? BACKWARD : FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 prep_redo_cmd(cap);
7486 if (cap->cmdchar == 'g')
7487 flags |= PUT_CURSEND;
Christian Brabandt2fa93842021-05-30 22:17:25 +02007488 else if (cap->cmdchar == 'z')
7489 flags |= PUT_BLOCK_INNER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if (VIsual_active)
7492 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007493 // Putting in Visual mode: The put text replaces the selected
7494 // text. First delete the selected text, then put the new text.
7495 // Need to save and restore the registers that the delete
7496 // overwrites if the old contents is being put.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007497 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007499#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007501#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01007502 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01007503 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007504#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507
7508 )
7509 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007510 // The delete is going to overwrite the register we want to
7511 // put, save it first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 reg1 = get_register(regname, TRUE);
7513 }
7514
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007515 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 cap->cmdchar = 'd';
7517 cap->nchar = NUL;
7518 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007519 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 nv_operator(cap);
7521 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007522 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007523 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007525 // delete PUT_LINE_BACKWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 cap->oap->regname = regname;
7527
7528 if (reg1 != NULL)
7529 {
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007530 // Delete probably changed the register we want to put, save
7531 // it first. Then put back what was there before the delete.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 reg2 = get_register(regname, FALSE);
7533 put_register(regname, reg1);
7534 }
7535
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007536 // When deleted a linewise Visual area, put the register as
7537 // lines to avoid it joined with the next line. When deletion was
7538 // characterwise, split a line when putting lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 if (VIsual_mode == 'V')
7540 flags |= PUT_LINE;
7541 else if (VIsual_mode == 'v')
7542 flags |= PUT_LINE_SPLIT;
7543 if (VIsual_mode == Ctrl_V && dir == FORWARD)
7544 flags |= PUT_LINE_FORWARD;
7545 dir = BACKWARD;
7546 if ((VIsual_mode != 'V'
7547 && curwin->w_cursor.col < curbuf->b_op_start.col)
7548 || (VIsual_mode == 'V'
7549 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007550 // cursor is at the end of the line or end of file, put
7551 // forward.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 dir = FORWARD;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007553 // May have been reset in do_put().
Bram Moolenaarec11aef2013-09-22 15:23:44 +02007554 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007556 do_put(cap->oap->regname, NULL, dir, cap->count1, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007558 // If a register was saved, put it back now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 if (reg2 != NULL)
7560 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007561
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007562 // What to reselect with "gv"? Selecting the just put text seems to
7563 // be the most useful, since the original text was removed.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007564 if (was_visual)
7565 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007566 curbuf->b_visual.vi_start = curbuf->b_op_start;
7567 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007568 // need to adjust cursor position
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01007569 if (*p_sel == 'e')
7570 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007571 }
7572
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007573 // When all lines were selected and deleted do_put() leaves an empty
7574 // line that needs to be deleted now.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00007575 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007576 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02007577 ml_delete_flags(curbuf->b_ml.ml_line_count, ML_DEL_MESSAGE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02007578 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007579
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007580 // If the cursor was in that line, move it to the end of the last
7581 // line.
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00007582 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7583 {
7584 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7585 coladvance((colnr_T)MAXCOL);
7586 }
7587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 auto_format(FALSE, TRUE);
7589 }
7590}
7591
7592/*
7593 * "o" and "O" commands.
7594 */
7595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007596nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
7598#ifdef FEAT_DIFF
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007599 // "do" is ":diffget"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
7601 {
7602 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01007603 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 }
7605 else
7606#endif
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007607 if (VIsual_active) // switch start and end of visual
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007609#ifdef FEAT_JOB_CHANNEL
7610 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02007611 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 n_opencmd(cap);
7615}
7616
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617#ifdef FEAT_NETBEANS_INTG
7618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007619nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
7621 netbeans_keycommand(cap->nchar);
7622}
7623#endif
7624
7625#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007627nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007629 do_put('~', NULL, BACKWARD, 1L, PUT_CURSEND);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630}
7631#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00007632
Bram Moolenaar3918c952005-03-15 22:34:55 +00007633/*
7634 * Trigger CursorHold event.
7635 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
7636 * input buffer. "did_cursorhold" is set to avoid retriggering.
7637 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00007638 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007639nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00007640{
7641 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
7642 did_cursorhold = TRUE;
Bram Moolenaar6e0ce172019-12-05 20:12:41 +01007643 cap->retval |= CA_COMMAND_BUSY; // don't call edit() now
Bram Moolenaar3918c952005-03-15 22:34:55 +00007644}