blob: 4db90d658fb136aa1208eeb7f1f0060a603de6fe [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 Moolenaar071d4272004-06-13 20:20:40 +000017/*
18 * The Visual area is remembered for reselection.
19 */
20static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
21static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020022static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar7d311c52014-02-22 23:49:35 +010023static int VIsual_mode_orig = NUL; /* saved Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010027#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010029#endif
Bram Moolenaareae1b912019-05-09 15:12:55 +020030static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static void op_colon(oparg_T *oap);
32static void op_function(oparg_T *oap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010033static void unshift_special(cmdarg_T *cap);
34static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#endif
38
39/*
40 * nv_*(): functions called to handle Normal and Visual mode commands.
41 * n_*(): functions called to handle Normal mode commands.
42 * v_*(): functions called to handle Visual mode commands.
43 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void nv_ignore(cmdarg_T *cap);
45static void nv_nop(cmdarg_T *cap);
46static void nv_error(cmdarg_T *cap);
47static void nv_help(cmdarg_T *cap);
48static void nv_addsub(cmdarg_T *cap);
49static void nv_page(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010050static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010052static void nv_ver_scrollbar(cmdarg_T *cap);
53static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000055#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010056static void nv_tabline(cmdarg_T *cap);
57static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000058#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010059static void nv_exmode(cmdarg_T *cap);
60static void nv_colon(cmdarg_T *cap);
61static void nv_ctrlg(cmdarg_T *cap);
62static void nv_ctrlh(cmdarg_T *cap);
63static void nv_clear(cmdarg_T *cap);
64static void nv_ctrlo(cmdarg_T *cap);
65static void nv_hat(cmdarg_T *cap);
66static void nv_Zet(cmdarg_T *cap);
67static void nv_ident(cmdarg_T *cap);
68static void nv_tagpop(cmdarg_T *cap);
69static void nv_scroll(cmdarg_T *cap);
70static void nv_right(cmdarg_T *cap);
71static void nv_left(cmdarg_T *cap);
72static void nv_up(cmdarg_T *cap);
73static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010074static void nv_end(cmdarg_T *cap);
75static void nv_dollar(cmdarg_T *cap);
76static void nv_search(cmdarg_T *cap);
77static void nv_next(cmdarg_T *cap);
78static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
79static void nv_csearch(cmdarg_T *cap);
80static void nv_brackets(cmdarg_T *cap);
81static void nv_percent(cmdarg_T *cap);
82static void nv_brace(cmdarg_T *cap);
83static void nv_mark(cmdarg_T *cap);
84static void nv_findpar(cmdarg_T *cap);
85static void nv_undo(cmdarg_T *cap);
86static void nv_kundo(cmdarg_T *cap);
87static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010088static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010089static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
90static void v_visop(cmdarg_T *cap);
91static void nv_subst(cmdarg_T *cap);
92static void nv_abbrev(cmdarg_T *cap);
93static void nv_optrans(cmdarg_T *cap);
94static void nv_gomark(cmdarg_T *cap);
95static void nv_pcmark(cmdarg_T *cap);
96static void nv_regname(cmdarg_T *cap);
97static void nv_visual(cmdarg_T *cap);
98static void n_start_visual_mode(int c);
99static void nv_window(cmdarg_T *cap);
100static void nv_suspend(cmdarg_T *cap);
101static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100102static void nv_dot(cmdarg_T *cap);
103static void nv_redo(cmdarg_T *cap);
104static void nv_Undo(cmdarg_T *cap);
105static void nv_tilde(cmdarg_T *cap);
106static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000107#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100108static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000109#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void nv_lineop(cmdarg_T *cap);
111static void nv_home(cmdarg_T *cap);
112static void nv_pipe(cmdarg_T *cap);
113static void nv_bck_word(cmdarg_T *cap);
114static void nv_wordcmd(cmdarg_T *cap);
115static void nv_beginline(cmdarg_T *cap);
116static void adjust_cursor(oparg_T *oap);
117static void adjust_for_sel(cmdarg_T *cap);
118static int unadjust_for_sel(void);
119static void nv_select(cmdarg_T *cap);
120static void nv_goto(cmdarg_T *cap);
121static void nv_normal(cmdarg_T *cap);
122static void nv_esc(cmdarg_T *oap);
123static void nv_edit(cmdarg_T *cap);
124static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100126static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100128static void nv_record(cmdarg_T *cap);
129static void nv_at(cmdarg_T *cap);
130static void nv_halfpage(cmdarg_T *cap);
131static void nv_join(cmdarg_T *cap);
132static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200133static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100134static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100136static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#endif
138#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100139static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100141static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100142static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000144static char *e_noident = N_("E349: No identifier under cursor");
145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146/*
147 * Function to be called for a Normal or Visual mode command.
148 * The argument is a cmdarg_T.
149 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100150typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151
152/* Values for cmd_flags. */
153#define NV_NCH 0x01 /* may need to get a second char */
154#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
155#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
156#define NV_LANG 0x08 /* second char needs language adjustment */
157
158#define NV_SS 0x10 /* may start selection */
159#define NV_SSS 0x20 /* may start selection with shift modifier */
160#define NV_STS 0x40 /* may stop selection without shift modif. */
161#define NV_RL 0x80 /* 'rightleft' modifies command */
162#define NV_KEEPREG 0x100 /* don't clear regname */
163#define NV_NCW 0x200 /* not allowed in command-line window */
164
165/*
166 * Generally speaking, every Normal mode command should either clear any
167 * pending operator (with *clearop*()), or set the motion type variable
168 * oap->motion_type.
169 *
170 * When a cursor motion command is made, it is marked as being a character or
171 * line oriented motion. Then, if an operator is in effect, the operation
172 * becomes character or line oriented accordingly.
173 */
174
175/*
176 * This table contains one entry for every Normal or Visual mode command.
177 * The order doesn't matter, init_normal_cmds() will create a sorted index.
178 * It is faster when all keys from zero to '~' are present.
179 */
180static const struct nv_cmd
181{
182 int cmd_char; /* (first) command character */
183 nv_func_T cmd_func; /* function for this command */
184 short_u cmd_flags; /* NV_ flags */
185 short cmd_arg; /* value for ca.arg */
186} nv_cmds[] =
187{
188 {NUL, nv_error, 0, 0},
189 {Ctrl_A, nv_addsub, 0, 0},
190 {Ctrl_B, nv_page, NV_STS, BACKWARD},
191 {Ctrl_C, nv_esc, 0, TRUE},
192 {Ctrl_D, nv_halfpage, 0, 0},
193 {Ctrl_E, nv_scroll_line, 0, TRUE},
194 {Ctrl_F, nv_page, NV_STS, FORWARD},
195 {Ctrl_G, nv_ctrlg, 0, 0},
196 {Ctrl_H, nv_ctrlh, 0, 0},
197 {Ctrl_I, nv_pcmark, 0, 0},
198 {NL, nv_down, 0, FALSE},
199 {Ctrl_K, nv_error, 0, 0},
200 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100201 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 {Ctrl_N, nv_down, NV_STS, FALSE},
203 {Ctrl_O, nv_ctrlo, 0, 0},
204 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000205 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {Ctrl_R, nv_redo, 0, 0},
207 {Ctrl_S, nv_ignore, 0, 0},
208 {Ctrl_T, nv_tagpop, NV_NCW, 0},
209 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 {Ctrl_V, nv_visual, 0, FALSE},
211 {'V', nv_visual, 0, FALSE},
212 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 {Ctrl_W, nv_window, 0, 0},
214 {Ctrl_X, nv_addsub, 0, 0},
215 {Ctrl_Y, nv_scroll_line, 0, FALSE},
216 {Ctrl_Z, nv_suspend, 0, 0},
217 {ESC, nv_esc, 0, FALSE},
218 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
219 {Ctrl_RSB, nv_ident, NV_NCW, 0},
220 {Ctrl_HAT, nv_hat, NV_NCW, 0},
221 {Ctrl__, nv_error, 0, 0},
222 {' ', nv_right, 0, 0},
223 {'!', nv_operator, 0, 0},
224 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
225 {'#', nv_ident, 0, 0},
226 {'$', nv_dollar, 0, 0},
227 {'%', nv_percent, 0, 0},
228 {'&', nv_optrans, 0, 0},
229 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
230 {'(', nv_brace, 0, BACKWARD},
231 {')', nv_brace, 0, FORWARD},
232 {'*', nv_ident, 0, 0},
233 {'+', nv_down, 0, TRUE},
234 {',', nv_csearch, 0, TRUE},
235 {'-', nv_up, 0, TRUE},
236 {'.', nv_dot, NV_KEEPREG, 0},
237 {'/', nv_search, 0, FALSE},
238 {'0', nv_beginline, 0, 0},
239 {'1', nv_ignore, 0, 0},
240 {'2', nv_ignore, 0, 0},
241 {'3', nv_ignore, 0, 0},
242 {'4', nv_ignore, 0, 0},
243 {'5', nv_ignore, 0, 0},
244 {'6', nv_ignore, 0, 0},
245 {'7', nv_ignore, 0, 0},
246 {'8', nv_ignore, 0, 0},
247 {'9', nv_ignore, 0, 0},
248 {':', nv_colon, 0, 0},
249 {';', nv_csearch, 0, FALSE},
250 {'<', nv_operator, NV_RL, 0},
251 {'=', nv_operator, 0, 0},
252 {'>', nv_operator, NV_RL, 0},
253 {'?', nv_search, 0, FALSE},
254 {'@', nv_at, NV_NCH_NOP, FALSE},
255 {'A', nv_edit, 0, 0},
256 {'B', nv_bck_word, 0, 1},
257 {'C', nv_abbrev, NV_KEEPREG, 0},
258 {'D', nv_abbrev, NV_KEEPREG, 0},
259 {'E', nv_wordcmd, 0, TRUE},
260 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
261 {'G', nv_goto, 0, TRUE},
262 {'H', nv_scroll, 0, 0},
263 {'I', nv_edit, 0, 0},
264 {'J', nv_join, 0, 0},
265 {'K', nv_ident, 0, 0},
266 {'L', nv_scroll, 0, 0},
267 {'M', nv_scroll, 0, 0},
268 {'N', nv_next, 0, SEARCH_REV},
269 {'O', nv_open, 0, 0},
270 {'P', nv_put, 0, 0},
271 {'Q', nv_exmode, NV_NCW, 0},
272 {'R', nv_Replace, 0, FALSE},
273 {'S', nv_subst, NV_KEEPREG, 0},
274 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
275 {'U', nv_Undo, 0, 0},
276 {'W', nv_wordcmd, 0, TRUE},
277 {'X', nv_abbrev, NV_KEEPREG, 0},
278 {'Y', nv_abbrev, NV_KEEPREG, 0},
279 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
280 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
281 {'\\', nv_error, 0, 0},
282 {']', nv_brackets, NV_NCH_ALW, FORWARD},
283 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
284 {'_', nv_lineop, 0, 0},
285 {'`', nv_gomark, NV_NCH_ALW, FALSE},
286 {'a', nv_edit, NV_NCH, 0},
287 {'b', nv_bck_word, 0, 0},
288 {'c', nv_operator, 0, 0},
289 {'d', nv_operator, 0, 0},
290 {'e', nv_wordcmd, 0, FALSE},
291 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
292 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
293 {'h', nv_left, NV_RL, 0},
294 {'i', nv_edit, NV_NCH, 0},
295 {'j', nv_down, 0, FALSE},
296 {'k', nv_up, 0, FALSE},
297 {'l', nv_right, NV_RL, 0},
298 {'m', nv_mark, NV_NCH_NOP, 0},
299 {'n', nv_next, 0, 0},
300 {'o', nv_open, 0, 0},
301 {'p', nv_put, 0, 0},
302 {'q', nv_record, NV_NCH, 0},
303 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
304 {'s', nv_subst, NV_KEEPREG, 0},
305 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
306 {'u', nv_undo, 0, 0},
307 {'w', nv_wordcmd, 0, FALSE},
308 {'x', nv_abbrev, NV_KEEPREG, 0},
309 {'y', nv_operator, 0, 0},
310 {'z', nv_zet, NV_NCH_ALW, 0},
311 {'{', nv_findpar, 0, BACKWARD},
312 {'|', nv_pipe, 0, 0},
313 {'}', nv_findpar, 0, FORWARD},
314 {'~', nv_tilde, 0, 0},
315
316 /* pound sign */
317 {POUND, nv_ident, 0, 0},
318#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200319 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
320 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
321 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
322 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {K_LEFTMOUSE, nv_mouse, 0, 0},
324 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
325 {K_LEFTDRAG, nv_mouse, 0, 0},
326 {K_LEFTRELEASE, nv_mouse, 0, 0},
327 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100328 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
330 {K_MIDDLEDRAG, nv_mouse, 0, 0},
331 {K_MIDDLERELEASE, nv_mouse, 0, 0},
332 {K_RIGHTMOUSE, nv_mouse, 0, 0},
333 {K_RIGHTDRAG, nv_mouse, 0, 0},
334 {K_RIGHTRELEASE, nv_mouse, 0, 0},
335 {K_X1MOUSE, nv_mouse, 0, 0},
336 {K_X1DRAG, nv_mouse, 0, 0},
337 {K_X1RELEASE, nv_mouse, 0, 0},
338 {K_X2MOUSE, nv_mouse, 0, 0},
339 {K_X2DRAG, nv_mouse, 0, 0},
340 {K_X2RELEASE, nv_mouse, 0, 0},
341#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000342 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000343 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 {K_INS, nv_edit, 0, 0},
345 {K_KINS, nv_edit, 0, 0},
346 {K_BS, nv_ctrlh, 0, 0},
347 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
348 {K_S_UP, nv_page, NV_SS, BACKWARD},
349 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
350 {K_S_DOWN, nv_page, NV_SS, FORWARD},
351 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
352 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
353 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
354 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
355 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
356 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
357 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
358 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
359 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
360 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
361 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
362 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {K_S_END, nv_end, NV_SS, FALSE},
364 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
365 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
366 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 {K_S_HOME, nv_home, NV_SS, 0},
368 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
369 {K_DEL, nv_abbrev, 0, 0},
370 {K_KDEL, nv_abbrev, 0, 0},
371 {K_UNDO, nv_kundo, 0, 0},
372 {K_HELP, nv_help, NV_NCW, 0},
373 {K_F1, nv_help, NV_NCW, 0},
374 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#ifdef FEAT_GUI
377 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
378 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
379#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000380#ifdef FEAT_GUI_TABLINE
381 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000382 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384#ifdef FEAT_NETBEANS_INTG
385 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
386#endif
387#ifdef FEAT_DND
388 {K_DROP, nv_drop, NV_STS, 0},
389#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100391 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392};
393
394/* Number of commands in nv_cmds[]. */
395#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
396
397/* Sorted index of commands in nv_cmds[]. */
398static short nv_cmd_idx[NV_CMDS_SIZE];
399
400/* The highest index for which
401 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
402static int nv_max_linear;
403
404/*
405 * Compare functions for qsort() below, that checks the command character
406 * through the index in nv_cmd_idx[].
407 */
408 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100409nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
411 int c1, c2;
412
413 /* The commands are sorted on absolute value. */
414 c1 = nv_cmds[*(const short *)s1].cmd_char;
415 c2 = nv_cmds[*(const short *)s2].cmd_char;
416 if (c1 < 0)
417 c1 = -c1;
418 if (c2 < 0)
419 c2 = -c2;
420 return c1 - c2;
421}
422
423/*
424 * Initialize the nv_cmd_idx[] table.
425 */
426 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100427init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429 int i;
430
431 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000432 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 nv_cmd_idx[i] = i;
434
435 /* Sort the commands by the command character. */
436 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
437
438 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000439 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
441 break;
442 nv_max_linear = i - 1;
443}
444
445/*
446 * Search for a command in the commands table.
447 * Returns -1 for invalid command.
448 */
449 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100450find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
452 int i;
453 int idx;
454 int top, bot;
455 int c;
456
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 /* A multi-byte character is never a command. */
458 if (cmdchar >= 0x100)
459 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
461 /* We use the absolute value of the character. Special keys have a
462 * negative value, but are sorted on their absolute value. */
463 if (cmdchar < 0)
464 cmdchar = -cmdchar;
465
466 /* If the character is in the first part: The character is the index into
467 * nv_cmd_idx[]. */
468 if (cmdchar <= nv_max_linear)
469 return nv_cmd_idx[cmdchar];
470
471 /* Perform a binary search. */
472 bot = nv_max_linear + 1;
473 top = NV_CMDS_SIZE - 1;
474 idx = -1;
475 while (bot <= top)
476 {
477 i = (top + bot) / 2;
478 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
479 if (c < 0)
480 c = -c;
481 if (cmdchar == c)
482 {
483 idx = nv_cmd_idx[i];
484 break;
485 }
486 if (cmdchar > c)
487 bot = i + 1;
488 else
489 top = i - 1;
490 }
491 return idx;
492}
493
494/*
495 * Execute a command in Normal mode.
496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100498normal_cmd(
499 oparg_T *oap,
500 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 cmdarg_T ca; /* command arguments */
503 int c;
504 int ctrl_w = FALSE; /* got CTRL-W command */
505 int old_col = curwin->w_curswant;
506#ifdef FEAT_CMDL_INFO
507 int need_flushbuf; /* need to call out_flush() */
508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 pos_T old_pos; /* cursor position before command */
510 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 static int old_mapped_len = 0;
512 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000513#ifdef FEAT_EVAL
514 int set_prevcount = FALSE;
515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
517 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
518 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000519
520 /* Use a count remembered from before entering an operator. After typing
521 * "3d" we return from normal_cmd() and come back here, the "3" is
522 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 ca.opcount = opcount;
524
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 /*
526 * If there is an operator pending, then the command we take this time
527 * will terminate it. Finish_op tells us to finish the operation before
528 * returning this time (unless the operation was cancelled).
529 */
530#ifdef CURSOR_SHAPE
531 c = finish_op;
532#endif
533 finish_op = (oap->op_type != OP_NOP);
534#ifdef CURSOR_SHAPE
535 if (finish_op != c)
536 {
537 ui_cursor_shape(); /* may show different cursor shape */
538# ifdef FEAT_MOUSESHAPE
539 update_mouseshape(-1);
540# endif
541 }
542#endif
543
Bram Moolenaara983fe92008-07-31 20:04:27 +0000544 /* When not finishing an operator and no register name typed, reset the
545 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000549#ifdef FEAT_EVAL
550 set_prevcount = TRUE;
551#endif
552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553
Bram Moolenaara983fe92008-07-31 20:04:27 +0000554 /* Restore counts from before receiving K_CURSORHOLD. This means after
555 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
556 * "3 * 2". */
557 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
558 {
559 ca.opcount = oap->prev_opcount;
560 ca.count0 = oap->prev_count0;
561 oap->prev_opcount = 0;
562 oap->prev_count0 = 0;
563 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
567 State = NORMAL_BUSY;
568#ifdef USE_ON_FLY_SCROLL
569 dont_scroll = FALSE; /* allow scrolling here */
570#endif
571
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100572#ifdef FEAT_EVAL
573 /* Set v:count here, when called from main() and not a stuffed
574 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100575 * when there is no count. Do set it for redo. */
576 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100577 set_vcount_ca(&ca, &set_prevcount);
578#endif
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 /*
581 * Get the command character from the user.
582 */
583 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100584 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
586 /*
587 * If a mapping was started in Visual or Select mode, remember the length
588 * of the mapping. This is used below to not return to Insert mode for as
589 * long as the mapping is being executed.
590 */
591 if (restart_edit == 0)
592 old_mapped_len = 0;
593 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000594 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 old_mapped_len = typebuf_maplen();
596
597 if (c == NUL)
598 c = K_ZERO;
599
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 /*
601 * In Select mode, typed text replaces the selection.
602 */
603 if (VIsual_active
604 && VIsual_select
605 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
606 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000607 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
608 * 'insertmode' is set) fake a "d"elete command, Insert mode will
609 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000610 * Insert the typed character in the typeahead buffer, so that it can
611 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000612 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000613 if (restart_edit != 0)
614 c = 'd';
615 else
616 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000617 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200618 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
621#ifdef FEAT_CMDL_INFO
622 need_flushbuf = add_to_showcmd(c);
623#endif
624
625getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {
628 /*
629 * Handle a count before a command and compute ca.count0.
630 * Note that '0' is a command and not the start of a count, but it's
631 * part of a count after other digits.
632 */
633 while ( (c >= '1' && c <= '9')
634 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
635 {
636 if (c == K_DEL || c == K_KDEL)
637 {
638 ca.count0 /= 10;
639#ifdef FEAT_CMDL_INFO
640 del_from_showcmd(4); /* delete the digit and ~@% */
641#endif
642 }
643 else
644 ca.count0 = ca.count0 * 10 + (c - '0');
645 if (ca.count0 < 0) /* got too large! */
646 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000647#ifdef FEAT_EVAL
648 /* Set v:count here, when called from main() and not a stuffed
649 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100650 * right after the count. Do set it for redo. */
651 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100652 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 if (ctrl_w)
655 {
656 ++no_mapping;
657 ++allow_keys; /* no mapping for nchar, but keys */
658 }
659 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000660 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 --no_zero_mapping;
663 if (ctrl_w)
664 {
665 --no_mapping;
666 --allow_keys;
667 }
668#ifdef FEAT_CMDL_INFO
669 need_flushbuf |= add_to_showcmd(c);
670#endif
671 }
672
673 /*
674 * If we got CTRL-W there may be a/another count
675 */
676 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
677 {
678 ctrl_w = TRUE;
679 ca.opcount = ca.count0; /* remember first count */
680 ca.count0 = 0;
681 ++no_mapping;
682 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000683 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 --no_mapping;
686 --allow_keys;
687#ifdef FEAT_CMDL_INFO
688 need_flushbuf |= add_to_showcmd(c);
689#endif
690 goto getcount; /* jump back */
691 }
692 }
693
Bram Moolenaara983fe92008-07-31 20:04:27 +0000694 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000696 /* Save the count values so that ca.opcount and ca.count0 are exactly
697 * the same when coming back here after handling K_CURSORHOLD. */
698 oap->prev_opcount = ca.opcount;
699 oap->prev_count0 = ca.count0;
700 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100701 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000702 {
703 /*
704 * If we're in the middle of an operator (including after entering a
705 * yank buffer with '"') AND we had a count before the operator, then
706 * that count overrides the current value of ca.count0.
707 * What this means effectively, is that commands like "3dw" get turned
708 * into "d3w" which makes things fall into place pretty neatly.
709 * If you give a count before AND after the operator, they are
710 * multiplied.
711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 if (ca.count0)
713 ca.count0 *= ca.opcount;
714 else
715 ca.count0 = ca.opcount;
716 }
717
718 /*
719 * Always remember the count. It will be set to zero (on the next call,
720 * above) when there is no pending operator.
721 * When called from main(), save the count for use by the "count" built-in
722 * variable.
723 */
724 ca.opcount = ca.count0;
725 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
726
727#ifdef FEAT_EVAL
728 /*
729 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100730 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100732 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000733 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#endif
735
736 /*
737 * Find the command character in the table of commands.
738 * For CTRL-W we already got nchar when looking for a count.
739 */
740 if (ctrl_w)
741 {
742 ca.nchar = c;
743 ca.cmdchar = Ctrl_W;
744 }
745 else
746 ca.cmdchar = c;
747 idx = find_command(ca.cmdchar);
748 if (idx < 0)
749 {
750 /* Not a known command: beep. */
751 clearopbeep(oap);
752 goto normal_end;
753 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000754
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000755 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200757 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000759 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 goto normal_end;
761 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000762 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
763 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 /*
766 * In Visual/Select mode, a few keys are handled in a special way.
767 */
768 if (VIsual_active)
769 {
770 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
771 if (km_stopsel
772 && (nv_cmds[idx].cmd_flags & NV_STS)
773 && !(mod_mask & MOD_MASK_SHIFT))
774 {
775 end_visual_mode();
776 redraw_curbuf_later(INVERTED);
777 }
778
779 /* Keys that work different when 'keymodel' contains "startsel" */
780 if (km_startsel)
781 {
782 if (nv_cmds[idx].cmd_flags & NV_SS)
783 {
784 unshift_special(&ca);
785 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000786 if (idx < 0)
787 {
788 /* Just in case */
789 clearopbeep(oap);
790 goto normal_end;
791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
794 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
799#ifdef FEAT_RIGHTLEFT
800 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
801 && (nv_cmds[idx].cmd_flags & NV_RL))
802 {
803 /* Invert horizontal movements and operations. Only when typed by the
804 * user directly, not when the result of a mapping or "x" translated
805 * to "dl". */
806 switch (ca.cmdchar)
807 {
808 case 'l': ca.cmdchar = 'h'; break;
809 case K_RIGHT: ca.cmdchar = K_LEFT; break;
810 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
811 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
812 case 'h': ca.cmdchar = 'l'; break;
813 case K_LEFT: ca.cmdchar = K_RIGHT; break;
814 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
815 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
816 case '>': ca.cmdchar = '<'; break;
817 case '<': ca.cmdchar = '>'; break;
818 }
819 idx = find_command(ca.cmdchar);
820 }
821#endif
822
823 /*
824 * Get an additional character if we need one.
825 */
826 if ((nv_cmds[idx].cmd_flags & NV_NCH)
827 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
828 && oap->op_type == OP_NOP)
829 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
830 || (ca.cmdchar == 'q'
831 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200832 && reg_recording == 0
833 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100835 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
837 int *cp;
838 int repl = FALSE; /* get character for replace mode */
839 int lit = FALSE; /* get extra character literally */
840 int langmap_active = FALSE; /* using :lmap mappings */
841 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100842#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 int save_smd; /* saved value of p_smd */
844#endif
845
846 ++no_mapping;
847 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000848 /* Don't generate a CursorHold event here, most commands can't handle
849 * it, e.g., nv_replace(), nv_csearch(). */
850 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (ca.cmdchar == 'g')
852 {
853 /*
854 * For 'g' get the next character now, so that we can check for
855 * "gr", "g'" and "g`".
856 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000857 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859#ifdef FEAT_CMDL_INFO
860 need_flushbuf |= add_to_showcmd(ca.nchar);
861#endif
862 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100863 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 {
865 cp = &ca.extra_char; /* need to get a third character */
866 if (ca.nchar != 'r')
867 lit = TRUE; /* get it literally */
868 else
869 repl = TRUE; /* get it in replace mode */
870 }
871 else
872 cp = NULL; /* no third character needed */
873 }
874 else
875 {
876 if (ca.cmdchar == 'r') /* get it in replace mode */
877 repl = TRUE;
878 cp = &ca.nchar;
879 }
880 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
881
882 /*
883 * Get a second or third character.
884 */
885 if (cp != NULL)
886 {
887#ifdef CURSOR_SHAPE
888 if (repl)
889 {
890 State = REPLACE; /* pretend Replace mode */
891 ui_cursor_shape(); /* show different cursor shape */
892 }
893#endif
894 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
895 {
896 /* Allow mappings defined with ":lmap". */
897 --no_mapping;
898 --allow_keys;
899 if (repl)
900 State = LREPLACE;
901 else
902 State = LANGMAP;
903 langmap_active = TRUE;
904 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100905#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 save_smd = p_smd;
907 p_smd = FALSE; /* Don't let the IM code show the mode here */
908 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
909 im_set_active(TRUE);
910#endif
911
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000912 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
914 if (langmap_active)
915 {
916 /* Undo the decrement done above */
917 ++no_mapping;
918 ++allow_keys;
919 State = NORMAL_BUSY;
920 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100921#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (lang)
923 {
924 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
925 im_save_status(&curbuf->b_p_iminsert);
926 im_set_active(FALSE);
927 }
928 p_smd = save_smd;
929#endif
930#ifdef CURSOR_SHAPE
931 State = NORMAL_BUSY;
932#endif
933#ifdef FEAT_CMDL_INFO
934 need_flushbuf |= add_to_showcmd(*cp);
935#endif
936
937 if (!lit)
938 {
939#ifdef FEAT_DIGRAPHS
940 /* Typing CTRL-K gets a digraph. */
941 if (*cp == Ctrl_K
942 && ((nv_cmds[idx].cmd_flags & NV_LANG)
943 || cp == &ca.extra_char)
944 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
945 {
946 c = get_digraph(FALSE);
947 if (c > 0)
948 {
949 *cp = c;
950# ifdef FEAT_CMDL_INFO
951 /* Guessing how to update showcmd here... */
952 del_from_showcmd(3);
953 need_flushbuf |= add_to_showcmd(*cp);
954# endif
955 }
956 }
957#endif
958
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 /* adjust chars > 127, except after "tTfFr" commands */
960 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#ifdef FEAT_RIGHTLEFT
962 /* adjust Hebrew mapped char */
963 if (p_hkmap && lang && KeyTyped)
964 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965#endif
966 }
967
968 /*
969 * When the next character is CTRL-\ a following CTRL-N means the
970 * command is aborted and we go to Normal mode.
971 */
972 if (cp == &ca.extra_char
973 && ca.nchar == Ctrl_BSL
974 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
975 {
976 ca.cmdchar = Ctrl_BSL;
977 ca.nchar = ca.extra_char;
978 idx = find_command(ca.cmdchar);
979 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200980 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200981 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 else if (*cp == Ctrl_BSL)
983 {
984 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
985
986 /* There is a busy wait here when typing "f<C-\>" and then
987 * something different from CTRL-N. Can't be avoided. */
988 while ((c = vpeekc()) <= 0 && towait > 0L)
989 {
990 do_sleep(towait > 50L ? 50L : towait);
991 towait -= 50L;
992 }
993 if (c > 0)
994 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000995 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (c != Ctrl_N && c != Ctrl_G)
997 vungetc(c);
998 else
999 {
1000 ca.cmdchar = Ctrl_BSL;
1001 ca.nchar = c;
1002 idx = find_command(ca.cmdchar);
1003 }
1004 }
1005 }
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 /* When getting a text character and the next character is a
1008 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001009 * However, don't wait for it to arrive. Also, do enable mapping,
1010 * because if it's put back with vungetc() it's too late to apply
1011 * mapping. */
1012 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 while (enc_utf8 && lang && (c = vpeekc()) > 0
1014 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1015 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001016 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (!utf_iscomposing(c))
1018 {
1019 vungetc(c); /* it wasn't, put it back */
1020 break;
1021 }
1022 else if (ca.ncharC1 == 0)
1023 ca.ncharC1 = c;
1024 else
1025 ca.ncharC2 = c;
1026 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001027 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 --no_mapping;
1030 --allow_keys;
1031 }
1032
1033#ifdef FEAT_CMDL_INFO
1034 /*
1035 * Flush the showcmd characters onto the screen so we can see them while
1036 * the command is being executed. Only do this when the shown command was
1037 * actually displayed, otherwise this will slow down a lot when executing
1038 * mappings.
1039 */
1040 if (need_flushbuf)
1041 out_flush();
1042#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001043 if (ca.cmdchar != K_IGNORE)
1044 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046 State = NORMAL;
1047
1048 if (ca.nchar == ESC)
1049 {
1050 clearop(oap);
1051 if (restart_edit == 0 && goto_im())
1052 restart_edit = 'a';
1053 goto normal_end;
1054 }
1055
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001056 if (ca.cmdchar != K_IGNORE)
1057 {
1058 msg_didout = FALSE; /* don't scroll screen up for normal command */
1059 msg_col = 0;
1060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 old_pos = curwin->w_cursor; /* remember where cursor was */
1063
1064 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1065 * mode. */
1066 if (!VIsual_active && km_startsel)
1067 {
1068 if (nv_cmds[idx].cmd_flags & NV_SS)
1069 {
1070 start_selection();
1071 unshift_special(&ca);
1072 idx = find_command(ca.cmdchar);
1073 }
1074 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1075 && (mod_mask & MOD_MASK_SHIFT))
1076 {
1077 start_selection();
1078 mod_mask &= ~MOD_MASK_SHIFT;
1079 }
1080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
1082 /*
1083 * Execute the command!
1084 * Call the command function found in the commands table.
1085 */
1086 ca.arg = nv_cmds[idx].cmd_arg;
1087 (nv_cmds[idx].cmd_func)(&ca);
1088
1089 /*
1090 * If we didn't start or finish an operator, reset oap->regname, unless we
1091 * need it later.
1092 */
1093 if (!finish_op
1094 && !oap->op_type
1095 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1096 {
1097 clearop(oap);
1098#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001099 {
1100 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001101
Bram Moolenaar536681b2011-05-10 16:12:45 +02001102 /* Adjust the register according to 'clipboard', so that when
1103 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001104# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001105 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001106# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001107 set_reg_var(regname);
1108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109#endif
1110 }
1111
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001112 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001113 * character or "z333<cr>". */
1114 if (old_mapped_len > 0)
1115 old_mapped_len = typebuf_maplen();
1116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 /*
1118 * If an operation is pending, handle it...
1119 */
1120 do_pending_operator(&ca, old_col, FALSE);
1121
1122 /*
1123 * Wait for a moment when a message is displayed that will be overwritten
1124 * by the mode message.
1125 * In Visual mode and with "^O" in Insert mode, a short message will be
1126 * overwritten by the mode message. Wait a bit, until a key is hit.
1127 * In Visual mode, it's more important to keep the Visual area updated
1128 * than keeping a message (e.g. from a /pat search).
1129 * Only do this if the command was typed, not from a mapping.
1130 * Don't wait when emsg_silent is non-zero.
1131 * Also wait a bit after an error message, e.g. for "^O:".
1132 * Don't redraw the screen, it would remove the message.
1133 */
1134 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001135 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 || (VIsual_active
1138 && old_pos.lnum == curwin->w_cursor.lnum
1139 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 )
1141 && (clear_cmdline
1142 || redraw_cmdline)
1143 && (msg_didout || (msg_didany && msg_scroll))
1144 && !msg_nowait
1145 && KeyTyped)
1146 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 && (msg_scroll
1149 || emsg_on_display)))
1150 && oap->regname == 0
1151 && !(ca.retval & CA_COMMAND_BUSY)
1152 && stuff_empty()
1153 && typebuf_typed()
1154 && emsg_silent == 0
1155 && !did_wait_return
1156 && oap->op_type == OP_NOP)
1157 {
1158 int save_State = State;
1159
1160 /* Draw the cursor with the right shape here */
1161 if (restart_edit != 0)
1162 State = INSERT;
1163
1164 /* If need to redraw, and there is a "keep_msg", redraw before the
1165 * delay */
1166 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1167 {
1168 char_u *kmsg;
1169
1170 kmsg = keep_msg;
1171 keep_msg = NULL;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001172 // showmode() will clear keep_msg, but we want to use it anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001174 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001176
1177 kmsg = vim_strsave(keep_msg);
1178 if (kmsg != NULL)
1179 {
1180 msg_attr((char *)kmsg, keep_msg_attr);
1181 vim_free(kmsg);
1182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 setcursor();
1185 cursor_on();
1186 out_flush();
1187 if (msg_scroll || emsg_on_display)
1188 ui_delay(1000L, TRUE); /* wait at least one second */
1189 ui_delay(3000L, FALSE); /* wait up to three seconds */
1190 State = save_State;
1191
1192 msg_scroll = FALSE;
1193 emsg_on_display = FALSE;
1194 }
1195
1196 /*
1197 * Finish up after executing a Normal mode command.
1198 */
1199normal_end:
1200
1201 msg_nowait = FALSE;
1202
1203 /* Reset finish_op, in case it was set */
1204#ifdef CURSOR_SHAPE
1205 c = finish_op;
1206#endif
1207 finish_op = FALSE;
1208#ifdef CURSOR_SHAPE
1209 /* Redraw the cursor with another shape, if we were in Operator-pending
1210 * mode or did a replace command. */
1211 if (c || ca.cmdchar == 'r')
1212 {
1213 ui_cursor_shape(); /* may show different cursor shape */
1214# ifdef FEAT_MOUSESHAPE
1215 update_mouseshape(-1);
1216# endif
1217 }
1218#endif
1219
1220#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001221 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001222 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 clear_showcmd();
1224#endif
1225
1226 checkpcmark(); /* check if we moved since setting pcmark */
1227 vim_free(ca.searchbuf);
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (has_mbyte)
1230 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (curwin->w_p_scb && toplevel)
1233 {
1234 validate_cursor(); /* may need to update w_leftcol */
1235 do_check_scrollbind(TRUE);
1236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
Bram Moolenaar860cae12010-06-05 23:22:07 +02001238 if (curwin->w_p_crb && toplevel)
1239 {
1240 validate_cursor(); /* may need to update w_leftcol */
1241 do_check_cursorbind();
1242 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001243
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001244#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001245 /* don't go to Insert mode if a terminal has a running job */
1246 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001247 restart_edit = 0;
1248#endif
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 /*
1251 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1252 * if still inside a mapping that started in Visual mode).
1253 * May switch from Visual to Select mode after CTRL-O command.
1254 */
1255 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1257 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 && !(ca.retval & CA_COMMAND_BUSY)
1259 && stuff_empty()
1260 && oap->regname == 0)
1261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (restart_VIsual_select == 1)
1263 {
1264 VIsual_select = TRUE;
1265 showmode();
1266 restart_VIsual_select = 0;
1267 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001268 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 (void)edit(restart_edit, FALSE, 1L);
1270 }
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (restart_VIsual_select == 2)
1273 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
1275 /* Save count before an operator for next time. */
1276 opcount = ca.opcount;
1277}
1278
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001279#ifdef FEAT_EVAL
1280/*
1281 * Set v:count and v:count1 according to "cap".
1282 * Set v:prevcount only when "set_prevcount" is TRUE.
1283 */
1284 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001285set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001286{
1287 long count = cap->count0;
1288
1289 /* multiply with cap->opcount the same way as above */
1290 if (cap->opcount != 0)
1291 count = cap->opcount * (count == 0 ? 1 : count);
1292 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1293 *set_prevcount = FALSE; /* only set v:prevcount once */
1294}
1295#endif
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297/*
Bram Moolenaar5823f842019-01-03 22:58:08 +01001298 * Handle an operator after Visual mode or when the movement is finished.
1299 * "gui_yank" is true when yanking text for the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 */
1301 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001302do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303{
1304 oparg_T *oap = cap->oap;
1305 pos_T old_cursor;
1306 int empty_region_error;
1307 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001308#ifdef FEAT_LINEBREAK
1309 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 /* The visual area is remembered for redo */
1313 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1314 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001315 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001317 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320#if defined(FEAT_CLIPBOARD)
1321 /*
1322 * Yank the visual area into the GUI selection register before we operate
1323 * on it and lose it forever.
1324 * Don't do it if a specific register was specified, so that ""x"*P works.
1325 * This could call do_pending_operator() recursively, but that's OK
1326 * because gui_yank will be TRUE for the nested call.
1327 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001328 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && oap->op_type != OP_NOP
1330 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && VIsual_active
1332 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 && oap->regname == 0)
1334 clip_auto_select();
1335#endif
1336 old_cursor = curwin->w_cursor;
1337
1338 /*
1339 * If an operation is pending, handle it...
1340 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001341 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
Bram Moolenaar5823f842019-01-03 22:58:08 +01001343 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
1344 // for the clipboard.
1345 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
1346
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001347#ifdef FEAT_LINEBREAK
1348 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001349 if (curwin->w_p_lbr)
1350 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001351 curwin->w_p_lbr = FALSE;
1352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 oap->is_VIsual = VIsual_active;
1354 if (oap->motion_force == 'V')
1355 oap->motion_type = MLINE;
1356 else if (oap->motion_force == 'v')
1357 {
1358 /* If the motion was linewise, "inclusive" will not have been set.
1359 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1360 if (oap->motion_type == MLINE)
1361 oap->inclusive = FALSE;
1362 /* If the motion already was characterwise, toggle "inclusive" */
1363 else if (oap->motion_type == MCHAR)
1364 oap->inclusive = !oap->inclusive;
1365 oap->motion_type = MCHAR;
1366 }
1367 else if (oap->motion_force == Ctrl_V)
1368 {
1369 /* Change line- or characterwise motion into Visual block mode. */
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01001370 if (!VIsual_active)
1371 {
1372 VIsual_active = TRUE;
1373 VIsual = oap->start;
1374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 VIsual_mode = Ctrl_V;
1376 VIsual_select = FALSE;
1377 VIsual_reselect = FALSE;
1378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
Bram Moolenaar7afea822013-04-24 18:34:45 +02001380 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1381 /* Never redo "zf" (define fold). */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001382 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001383 && ((!VIsual_active || oap->motion_force)
1384 /* Also redo Operator-pending Visual mode mappings */
1385 || (VIsual_active && cap->cmdchar == ':'
1386 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001387 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388#ifdef FEAT_FOLDING
1389 && oap->op_type != OP_FOLD
1390 && oap->op_type != OP_FOLDOPEN
1391 && oap->op_type != OP_FOLDOPENREC
1392 && oap->op_type != OP_FOLDCLOSE
1393 && oap->op_type != OP_FOLDCLOSEREC
1394 && oap->op_type != OP_FOLDDEL
1395 && oap->op_type != OP_FOLDDELREC
1396#endif
1397 )
1398 {
1399 prep_redo(oap->regname, cap->count0,
1400 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1401 oap->motion_force, cap->cmdchar, cap->nchar);
1402 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1403 {
1404 /*
1405 * If 'cpoptions' does not contain 'r', insert the search
1406 * pattern to really repeat the same command.
1407 */
1408 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001409 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 AppendToRedobuff(NL_STR);
1411 }
1412 else if (cap->cmdchar == ':')
1413 {
1414 /* do_cmdline() has stored the first typed line in
1415 * "repeat_cmdline". When several lines are typed repeating
1416 * won't be possible. */
1417 if (repeat_cmdline == NULL)
1418 ResetRedobuff();
1419 else
1420 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001421 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001423 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 }
1426 }
1427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (redo_VIsual_busy)
1429 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001430 /* Redo of an operation on a Visual area. Use the same size from
1431 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 oap->start = curwin->w_cursor;
1433 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1434 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1435 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1436 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001437 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001439 if (VIsual_mode == 'v')
1440 {
1441 if (redo_VIsual_line_count <= 1)
1442 {
1443 validate_virtcol();
1444 curwin->w_curswant =
1445 curwin->w_virtcol + redo_VIsual_vcol - 1;
1446 }
1447 else
1448 curwin->w_curswant = redo_VIsual_vcol;
1449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001451 {
1452 curwin->w_curswant = MAXCOL;
1453 }
1454 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
1456 cap->count0 = redo_VIsual_count;
1457 if (redo_VIsual_count != 0)
1458 cap->count1 = redo_VIsual_count;
1459 else
1460 cap->count1 = 1;
1461 }
1462 else if (VIsual_active)
1463 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001464 if (!gui_yank)
1465 {
1466 /* Save the current VIsual area for '< and '> marks, and "gv" */
1467 curbuf->b_visual.vi_start = VIsual;
1468 curbuf->b_visual.vi_end = curwin->w_cursor;
1469 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001470 if (VIsual_mode_orig != NUL)
1471 {
1472 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1473 VIsual_mode_orig = NUL;
1474 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001475 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001477 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
1481 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001482 * characterwise selection.
1483 * Special case: gH<Del> deletes the last line. */
1484 if (VIsual_select && VIsual_mode == 'V'
1485 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001487 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
1489 VIsual.col = 0;
1490 curwin->w_cursor.col =
1491 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1492 }
1493 else
1494 {
1495 curwin->w_cursor.col = 0;
1496 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1497 }
1498 VIsual_mode = 'v';
1499 }
1500 /* If 'selection' is "exclusive", backup one character for
1501 * charwise selections. */
1502 else if (VIsual_mode == 'v')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001503 include_line_break = unadjust_for_sel();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505 oap->start = VIsual;
1506 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001509 oap->start.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
1513 /*
1514 * Set oap->start to the first position of the operated text, oap->end
1515 * to the end of the operated text. w_cursor is equal to oap->start.
1516 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001517 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
1519#ifdef FEAT_FOLDING
1520 /* Include folded lines completely. */
1521 if (!VIsual_active)
1522 {
1523 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1524 oap->start.col = 0;
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02001525 if ((curwin->w_cursor.col > 0 || oap->inclusive)
1526 && hasFolding(curwin->w_cursor.lnum, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 &curwin->w_cursor.lnum))
1528 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1529 }
1530#endif
1531 oap->end = curwin->w_cursor;
1532 curwin->w_cursor = oap->start;
1533
1534 /* w_virtcol may have been updated; if the cursor goes back to its
1535 * previous position w_virtcol becomes invalid and isn't updated
1536 * automatically. */
1537 curwin->w_valid &= ~VALID_VIRTCOL;
1538 }
1539 else
1540 {
1541#ifdef FEAT_FOLDING
1542 /* Include folded lines completely. */
1543 if (!VIsual_active && oap->motion_type == MLINE)
1544 {
1545 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1546 NULL))
1547 curwin->w_cursor.col = 0;
1548 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1549 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1550 }
1551#endif
1552 oap->end = oap->start;
1553 oap->start = curwin->w_cursor;
1554 }
1555
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001556 /* Just in case lines were deleted that make the position invalid. */
1557 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 /* Set "virtual_op" before resetting VIsual_active. */
1561 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 if (VIsual_active || redo_VIsual_busy)
1564 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001565 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 if (!redo_VIsual_busy && !gui_yank)
1568 {
1569 /*
1570 * Prepare to reselect and redo Visual: this is based on the
1571 * size of the Visual text
1572 */
1573 resel_VIsual_mode = VIsual_mode;
1574 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001575 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001577 {
1578 if (VIsual_mode != Ctrl_V)
1579 getvvcol(curwin, &(oap->end),
1580 NULL, NULL, &oap->end_vcol);
1581 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1582 {
1583 if (VIsual_mode != Ctrl_V)
1584 getvvcol(curwin, &(oap->start),
1585 &oap->start_vcol, NULL, NULL);
1586 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1587 }
1588 else
1589 resel_VIsual_vcol = oap->end_vcol;
1590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 resel_VIsual_line_count = oap->line_count;
1592 }
1593
1594 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001595 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 && oap->op_type != OP_COLON
1597#ifdef FEAT_FOLDING
1598 && oap->op_type != OP_FOLD
1599 && oap->op_type != OP_FOLDOPEN
1600 && oap->op_type != OP_FOLDOPENREC
1601 && oap->op_type != OP_FOLDCLOSE
1602 && oap->op_type != OP_FOLDCLOSEREC
1603 && oap->op_type != OP_FOLDDEL
1604 && oap->op_type != OP_FOLDDELREC
1605#endif
1606 && oap->motion_force == NUL
1607 )
1608 {
1609 /* Prepare for redoing. Only use the nchar field for "r",
1610 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001611 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1612 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001613 prep_redo(oap->regname, cap->count0,
1614 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1615 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001616 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001617 {
1618 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1619
1620 /* reverse what nv_replace() did */
1621 if (nchar == REPLACE_CR_NCHAR)
1622 nchar = CAR;
1623 else if (nchar == REPLACE_NL_NCHAR)
1624 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001625 prep_redo(oap->regname, 0L, NUL, 'v',
1626 get_op_char(oap->op_type),
1627 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001628 nchar);
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (!redo_VIsual_busy)
1631 {
1632 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001633 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 redo_VIsual_line_count = resel_VIsual_line_count;
1635 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001636 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 }
1638 }
1639
1640 /*
1641 * oap->inclusive defaults to TRUE.
1642 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1643 * FALSE. This makes "d}P" and "v}dP" work the same.
1644 */
1645 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1646 oap->inclusive = TRUE;
1647 if (VIsual_mode == 'V')
1648 oap->motion_type = MLINE;
1649 else
1650 {
1651 oap->motion_type = MCHAR;
1652 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001653 && (include_line_break || !virtual_op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 {
1655 oap->inclusive = FALSE;
1656 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001657 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001658 if (*p_sel != 'o'
1659 && !op_on_lines(oap->op_type)
1660 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001662 ++oap->end.lnum;
1663 oap->end.col = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001664 oap->end.coladd = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001665 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
1667 }
1668 }
1669
1670 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001671
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 /*
1673 * Switch Visual off now, so screen updating does
1674 * not show inverted text when the screen is redrawn.
1675 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1676 * no screen redraw, so it is done here to remove the inverted
1677 * part.
1678 */
1679 if (!gui_yank)
1680 {
1681 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001682#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 setmouse();
1684 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001685#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001686 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 if ((oap->op_type == OP_YANK
1688 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001689 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 || oap->op_type == OP_FILTER)
1691 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001692 {
1693#ifdef FEAT_LINEBREAK
1694 /* make sure redrawing is correct */
1695 curwin->w_p_lbr = lbr_saved;
1696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
1700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 /* Include the trailing byte of a multi-byte char. */
1703 if (has_mbyte && oap->inclusive)
1704 {
1705 int l;
1706
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001707 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (l > 1)
1709 oap->end.col += l - 1;
1710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 curwin->w_set_curswant = TRUE;
1712
1713 /*
1714 * oap->empty is set when start and end are the same. The inclusive
1715 * flag affects this too, unless yanking and the end is on a NUL.
1716 */
1717 oap->empty = (oap->motion_type == MCHAR
1718 && (!oap->inclusive
1719 || (oap->op_type == OP_YANK
1720 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001721 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001722 && !(virtual_op && oap->start.coladd != oap->end.coladd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 /*
1724 * For delete, change and yank, it's an error to operate on an
1725 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1726 */
1727 empty_region_error = (oap->empty
1728 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 /* Force a redraw when operating on an empty Visual region, when
1731 * 'modifiable is off or creating a fold. */
1732 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001733#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001737 {
1738#ifdef FEAT_LINEBREAK
1739 curwin->w_p_lbr = lbr_saved;
1740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
1744 /*
1745 * If the end of an operator is in column one while oap->motion_type
1746 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1747 * character in the previous line. If op_start is on or before the
1748 * first non-blank in the line, the operator becomes linewise
1749 * (strange, but that's the way vi does it).
1750 */
1751 if ( oap->motion_type == MCHAR
1752 && oap->inclusive == FALSE
1753 && !(cap->retval & CA_NO_ADJ_OP_END)
1754 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001756 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 && oap->line_count > 1)
1758 {
1759 oap->end_adjusted = TRUE; /* remember that we did this */
1760 --oap->line_count;
1761 --oap->end.lnum;
1762 if (inindent(0))
1763 oap->motion_type = MLINE;
1764 else
1765 {
1766 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1767 if (oap->end.col)
1768 {
1769 --oap->end.col;
1770 oap->inclusive = TRUE;
1771 }
1772 }
1773 }
1774 else
1775 oap->end_adjusted = FALSE;
1776
1777 switch (oap->op_type)
1778 {
1779 case OP_LSHIFT:
1780 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001781 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 auto_format(FALSE, TRUE);
1783 break;
1784
1785 case OP_JOIN_NS:
1786 case OP_JOIN:
1787 if (oap->line_count < 2)
1788 oap->line_count = 2;
1789 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1790 curbuf->b_ml.ml_line_count)
1791 beep_flush();
1792 else
1793 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001794 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001795 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 auto_format(FALSE, TRUE);
1797 }
1798 break;
1799
1800 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001803 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001804 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001805 CancelRedo();
1806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 else
1808 {
1809 (void)op_delete(oap);
1810 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1811 u_save_cursor(); /* cursor line wasn't saved yet */
1812 auto_format(FALSE, TRUE);
1813 }
1814 break;
1815
1816 case OP_YANK:
1817 if (empty_region_error)
1818 {
1819 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001820 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001821 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001822 CancelRedo();
1823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 }
1825 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001826 {
1827#ifdef FEAT_LINEBREAK
1828 curwin->w_p_lbr = lbr_saved;
1829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 check_cursor_col();
1833 break;
1834
1835 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001838 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001839 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001840 CancelRedo();
1841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 else
1843 {
1844 /* This is a new edit command, not a restart. Need to
1845 * remember it to make 'insertmode' work with mappings for
1846 * Visual mode. But do this only once and not when typed and
1847 * 'insertmode' isn't set. */
1848 if (p_im || !KeyTyped)
1849 restart_edit_save = restart_edit;
1850 else
1851 restart_edit_save = 0;
1852 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001853#ifdef FEAT_LINEBREAK
1854 /* Restore linebreak, so that when the user edits it looks as
1855 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001856 if (curwin->w_p_lbr != lbr_saved)
1857 {
1858 curwin->w_p_lbr = lbr_saved;
1859 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1860 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 /* Reset finish_op now, don't want it set inside edit(). */
1863 finish_op = FALSE;
1864 if (op_change(oap)) /* will call edit() */
1865 cap->retval |= CA_COMMAND_BUSY;
1866 if (restart_edit == 0)
1867 restart_edit = restart_edit_save;
1868 }
1869 break;
1870
1871 case OP_FILTER:
1872 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1873 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1874 else
1875 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001876 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 case OP_INDENT:
1879 case OP_COLON:
1880
1881#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1882 /*
1883 * If 'equalprg' is empty, do the indenting internally.
1884 */
1885 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1886 {
1887# ifdef FEAT_LISP
1888 if (curbuf->b_p_lisp)
1889 {
1890 op_reindent(oap, get_lisp_indent);
1891 break;
1892 }
1893# endif
1894# ifdef FEAT_CINDENT
1895 op_reindent(oap,
1896# ifdef FEAT_EVAL
1897 *curbuf->b_p_inde != NUL ? get_expr_indent :
1898# endif
1899 get_c_indent);
1900 break;
1901# endif
1902 }
1903#endif
1904
1905 op_colon(oap);
1906 break;
1907
1908 case OP_TILDE:
1909 case OP_UPPER:
1910 case OP_LOWER:
1911 case OP_ROT13:
1912 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001913 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001914 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001915 CancelRedo();
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 else
1918 op_tilde(oap);
1919 check_cursor_col();
1920 break;
1921
1922 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001923#if defined(FEAT_EVAL)
1924 if (*curbuf->b_p_fex != NUL)
1925 op_formatexpr(oap); /* use expression */
1926 else
1927#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001928 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 op_colon(oap); /* use external command */
1930 else
1931 op_format(oap, FALSE); /* use internal function */
1932 break;
1933
1934 case OP_FORMAT2:
1935 op_format(oap, TRUE); /* use internal function */
1936 break;
1937
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001938 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001939#ifdef FEAT_LINEBREAK
1940 /* Restore linebreak, so that when the user edits it looks as
1941 * before. */
1942 curwin->w_p_lbr = lbr_saved;
1943#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001944 op_function(oap); /* call 'operatorfunc' */
1945 break;
1946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 case OP_INSERT:
1948 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001951 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001952 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001953 CancelRedo();
1954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 else
1956 {
1957 /* This is a new edit command, not a restart. Need to
1958 * remember it to make 'insertmode' work with mappings for
1959 * Visual mode. But do this only once. */
1960 restart_edit_save = restart_edit;
1961 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001962#ifdef FEAT_LINEBREAK
1963 /* Restore linebreak, so that when the user edits it looks as
1964 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001965 if (curwin->w_p_lbr != lbr_saved)
1966 {
1967 curwin->w_p_lbr = lbr_saved;
1968 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1969 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001972#ifdef FEAT_LINEBREAK
1973 /* Reset linebreak, so that formatting works correctly. */
1974 curwin->w_p_lbr = FALSE;
1975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
1977 /* TODO: when inserting in several lines, should format all
1978 * the lines. */
1979 auto_format(FALSE, TRUE);
1980
1981 if (restart_edit == 0)
1982 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01001983 else
1984 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 break;
1987
1988 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001991 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001992 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001993 CancelRedo();
1994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001996 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01001997#ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001998 /* Restore linebreak, so that when the user edits it looks as
1999 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002000 if (curwin->w_p_lbr != lbr_saved)
2001 {
2002 curwin->w_p_lbr = lbr_saved;
2003 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2004 }
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 break;
2009
2010#ifdef FEAT_FOLDING
2011 case OP_FOLD:
2012 VIsual_reselect = FALSE; /* don't reselect now */
2013 foldCreate(oap->start.lnum, oap->end.lnum);
2014 break;
2015
2016 case OP_FOLDOPEN:
2017 case OP_FOLDOPENREC:
2018 case OP_FOLDCLOSE:
2019 case OP_FOLDCLOSEREC:
2020 VIsual_reselect = FALSE; /* don't reselect now */
2021 opFoldRange(oap->start.lnum, oap->end.lnum,
2022 oap->op_type == OP_FOLDOPEN
2023 || oap->op_type == OP_FOLDOPENREC,
2024 oap->op_type == OP_FOLDOPENREC
2025 || oap->op_type == OP_FOLDCLOSEREC,
2026 oap->is_VIsual);
2027 break;
2028
2029 case OP_FOLDDEL:
2030 case OP_FOLDDELREC:
2031 VIsual_reselect = FALSE; /* don't reselect now */
2032 deleteFold(oap->start.lnum, oap->end.lnum,
2033 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2034 break;
2035#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002036 case OP_NR_ADD:
2037 case OP_NR_SUB:
2038 if (empty_region_error)
2039 {
2040 vim_beep(BO_OPER);
2041 CancelRedo();
2042 }
2043 else
2044 {
2045 VIsual_active = TRUE;
2046#ifdef FEAT_LINEBREAK
2047 curwin->w_p_lbr = lbr_saved;
2048#endif
2049 op_addsub(oap, cap->count1, redo_VIsual_arg);
2050 VIsual_active = FALSE;
2051 }
2052 check_cursor_col();
2053 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 default:
2055 clearopbeep(oap);
2056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 virtual_op = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (!gui_yank)
2059 {
2060 /*
2061 * if 'sol' not set, go back to old column for some commands
2062 */
2063 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2064 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2065 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002066 {
2067#ifdef FEAT_LINEBREAK
2068 curwin->w_p_lbr = FALSE;
2069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073 else
2074 {
2075 curwin->w_cursor = old_cursor;
2076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 clearop(oap);
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01002079 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002081#ifdef FEAT_LINEBREAK
2082 curwin->w_p_lbr = lbr_saved;
2083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084}
2085
2086/*
2087 * Handle indent and format operators and visual mode ":".
2088 */
2089 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002090op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091{
2092 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 if (oap->is_VIsual)
2094 stuffReadbuff((char_u *)"'<,'>");
2095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 /*
2098 * Make the range look nice, so it can be repeated.
2099 */
2100 if (oap->start.lnum == curwin->w_cursor.lnum)
2101 stuffcharReadbuff('.');
2102 else
2103 stuffnumReadbuff((long)oap->start.lnum);
2104 if (oap->end.lnum != oap->start.lnum)
2105 {
2106 stuffcharReadbuff(',');
2107 if (oap->end.lnum == curwin->w_cursor.lnum)
2108 stuffcharReadbuff('.');
2109 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2110 stuffcharReadbuff('$');
2111 else if (oap->start.lnum == curwin->w_cursor.lnum)
2112 {
2113 stuffReadbuff((char_u *)".+");
2114 stuffnumReadbuff((long)oap->line_count - 1);
2115 }
2116 else
2117 stuffnumReadbuff((long)oap->end.lnum);
2118 }
2119 }
2120 if (oap->op_type != OP_COLON)
2121 stuffReadbuff((char_u *)"!");
2122 if (oap->op_type == OP_INDENT)
2123 {
2124#ifndef FEAT_CINDENT
2125 if (*get_equalprg() == NUL)
2126 stuffReadbuff((char_u *)"indent");
2127 else
2128#endif
2129 stuffReadbuff(get_equalprg());
2130 stuffReadbuff((char_u *)"\n");
2131 }
2132 else if (oap->op_type == OP_FORMAT)
2133 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002134 if (*curbuf->b_p_fp != NUL)
2135 stuffReadbuff(curbuf->b_p_fp);
2136 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002138 else
2139 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002140 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142
2143 /*
2144 * do_cmdline() does the rest
2145 */
2146}
2147
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002148/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002149 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002150 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002151 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002152op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002153{
2154#ifdef FEAT_EVAL
Bram Moolenaarffa96842018-06-12 22:05:14 +02002155 typval_T argv[2];
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002156 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002157
2158 if (*p_opfunc == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002159 emsg(_("E774: 'operatorfunc' is empty"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002160 else
2161 {
2162 /* Set '[ and '] marks to text to be operated on. */
2163 curbuf->b_op_start = oap->start;
2164 curbuf->b_op_end = oap->end;
2165 if (oap->motion_type != MLINE && !oap->inclusive)
2166 /* Exclude the end position. */
2167 decl(&curbuf->b_op_end);
2168
Bram Moolenaarffa96842018-06-12 22:05:14 +02002169 argv[0].v_type = VAR_STRING;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002170 if (oap->block_mode)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002171 argv[0].vval.v_string = (char_u *)"block";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002172 else if (oap->motion_type == MLINE)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002173 argv[0].vval.v_string = (char_u *)"line";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002174 else
Bram Moolenaarffa96842018-06-12 22:05:14 +02002175 argv[0].vval.v_string = (char_u *)"char";
2176 argv[1].v_type = VAR_UNKNOWN;
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002177
2178 /* Reset virtual_op so that 'virtualedit' can be changed in the
2179 * function. */
2180 virtual_op = MAYBE;
2181
Bram Moolenaarded27a12018-08-01 19:06:03 +02002182 (void)call_func_retnr(p_opfunc, 1, argv);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002183
2184 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002185 }
2186#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002187 emsg(_("E775: Eval feature not available"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002188#endif
2189}
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191/*
2192 * Check if highlighting for visual mode is possible, give a warning message
2193 * if not.
2194 */
2195 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002196check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197{
2198 static int did_check = FALSE;
2199
2200 if (full_screen)
2201 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01002202 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002203 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 did_check = TRUE;
2205 }
2206}
2207
2208/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00002209 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 * This function should ALWAYS be called to end Visual mode, except from
2211 * do_pending_operator().
2212 */
2213 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002214end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215{
2216#ifdef FEAT_CLIPBOARD
2217 /*
2218 * If we are using the clipboard, then remember what was selected in case
2219 * we need to paste it somewhere while we still own the selection.
2220 * Only do this when the clipboard is already owned. Don't want to grab
2221 * the selection when hitting ESC.
2222 */
2223 if (clip_star.available && clip_star.owned)
2224 clip_auto_select();
2225#endif
2226
2227 VIsual_active = FALSE;
2228#ifdef FEAT_MOUSE
2229 setmouse();
2230 mouse_dragging = 0;
2231#endif
2232
2233 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002234 curbuf->b_visual.vi_mode = VIsual_mode;
2235 curbuf->b_visual.vi_start = VIsual;
2236 curbuf->b_visual.vi_end = curwin->w_cursor;
2237 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#ifdef FEAT_EVAL
2239 curbuf->b_visual_mode_eval = VIsual_mode;
2240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if (!virtual_active())
2242 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02002243 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002245 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246}
2247
2248/*
2249 * Reset VIsual_active and VIsual_reselect.
2250 */
2251 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002252reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253{
2254 if (VIsual_active)
2255 {
2256 end_visual_mode();
2257 redraw_curbuf_later(INVERTED); /* delete the inversion later */
2258 }
2259 VIsual_reselect = FALSE;
2260}
2261
2262/*
2263 * Reset VIsual_active and VIsual_reselect if it's set.
2264 */
2265 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002266reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267{
2268 if (VIsual_active)
2269 {
2270 end_visual_mode();
2271 redraw_curbuf_later(INVERTED); /* delete the inversion later */
2272 VIsual_reselect = FALSE;
2273 }
2274}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276/*
2277 * Check for a balloon-eval special item to include when searching for an
2278 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
2279 * Returns TRUE if the character at "*ptr" should be included.
2280 * "dir" is FORWARD or BACKWARD, the direction of searching.
2281 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
2282 * "bnp" points to a counter for square brackets.
2283 */
2284 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002285find_is_eval_item(
2286 char_u *ptr,
2287 int *colp,
2288 int *bnp,
2289 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 /* Accept everything inside []. */
2292 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
2293 ++*bnp;
2294 if (*bnp > 0)
2295 {
2296 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
2297 --*bnp;
2298 return TRUE;
2299 }
2300
2301 /* skip over "s.var" */
2302 if (*ptr == '.')
2303 return TRUE;
2304
2305 /* two-character item: s->var */
2306 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
2307 && ptr[dir == BACKWARD ? -1 : 0] == '-')
2308 {
2309 *colp += dir;
2310 return TRUE;
2311 }
2312 return FALSE;
2313}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
2315/*
2316 * Find the identifier under or to the right of the cursor.
2317 * "find_type" can have one of three values:
2318 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002319 * FIND_STRING: find any non-white text
2320 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002321 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 *
2323 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002324 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002326 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 * This doesn't match the real Vi but I like it a little better and it
2328 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002329 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 * When FIND_IDENT isn't defined, we backup until a blank.
2331 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002332 * Returns the length of the text, or zero if no text is found.
2333 * If text is found, a pointer to the text is put in "*text". This
2334 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 */
2336 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002337find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002340 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341}
2342
2343/*
2344 * Like find_ident_under_cursor(), but for any window and any position.
2345 * However: Uses 'iskeyword' from the current window!.
2346 */
2347 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002348find_ident_at_pos(
2349 win_T *wp,
2350 linenr_T lnum,
2351 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002352 char_u **text,
2353 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01002354 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
2356 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002357 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 int this_class = 0;
2360 int prev_class;
2361 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002362 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
2364 /*
2365 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002366 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 */
2368 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
2369 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
2370 {
2371 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002372 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 */
2374 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 if (has_mbyte)
2376 {
2377 while (ptr[col] != NUL)
2378 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002379 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if ((find_type & FIND_EVAL) && ptr[col] == ']')
2381 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 this_class = mb_get_class(ptr + col);
2383 if (this_class != 0 && (i == 1 || this_class != 1))
2384 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002385 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
2387 }
2388 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002390 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 )
2393 ++col;
2394
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002395 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002399 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (has_mbyte)
2402 {
2403 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if ((find_type & FIND_EVAL) && ptr[col] == ']')
2405 this_class = mb_get_class((char_u *)"a");
2406 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002408 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
2411 prev_class = mb_get_class(ptr + prevcol);
2412 if (this_class != prev_class
2413 && (i == 0
2414 || prev_class == 0
2415 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 && (!(find_type & FIND_EVAL)
2417 || prevcol == 0
2418 || !find_is_eval_item(ptr + prevcol, &prevcol,
2419 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 )
2421 break;
2422 col = prevcol;
2423 }
2424
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002425 // If we don't want just any old text, or we've found an
2426 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (this_class > 2)
2428 this_class = 2;
2429 if (!(find_type & FIND_STRING) || this_class == 2)
2430 break;
2431 }
2432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 while (col > 0
2435 && ((i == 0
2436 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01002437 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 && (!(find_type & FIND_IDENT)
2439 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 || ((find_type & FIND_EVAL)
2441 && col > 1
2442 && find_is_eval_item(ptr + col - 1, &col,
2443 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 ))
2445 --col;
2446
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002447 // If we don't want just any old text, or we've found an
2448 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
2450 break;
2451 }
2452 }
2453
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002454 if (ptr[col] == NUL || (i == 0
2455 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002457 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02002458 if ((find_type & FIND_NOERROR) == 0)
2459 {
2460 if (find_type & FIND_STRING)
2461 emsg(_("E348: No string under cursor"));
2462 else
2463 emsg(_(e_noident));
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 return 0;
2466 }
2467 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002468 *text = ptr;
2469 if (textcol != NULL)
2470 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002473 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 bn = 0;
2476 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 if (has_mbyte)
2479 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02002480 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 this_class = mb_get_class(ptr);
2482 while (ptr[col] != NUL
2483 && ((i == 0 ? mb_get_class(ptr + col) == this_class
2484 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 || ((find_type & FIND_EVAL)
2486 && col <= (int)startcol
2487 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002489 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01002493 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 || ((find_type & FIND_EVAL)
2495 && col <= (int)startcol
2496 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 return col;
2501}
2502
2503/*
2504 * Prepare for redo of a normal command.
2505 */
2506 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002507prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
2509 prep_redo(cap->oap->regname, cap->count0,
2510 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
2511}
2512
2513/*
2514 * Prepare for redo of any command.
2515 * Note that only the last argument can be a multi-byte char.
2516 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002517 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002518prep_redo(
2519 int regname,
2520 long num,
2521 int cmd1,
2522 int cmd2,
2523 int cmd3,
2524 int cmd4,
2525 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526{
2527 ResetRedobuff();
2528 if (regname != 0) /* yank from specified buffer */
2529 {
2530 AppendCharToRedobuff('"');
2531 AppendCharToRedobuff(regname);
2532 }
2533 if (num)
2534 AppendNumberToRedobuff(num);
2535
2536 if (cmd1 != NUL)
2537 AppendCharToRedobuff(cmd1);
2538 if (cmd2 != NUL)
2539 AppendCharToRedobuff(cmd2);
2540 if (cmd3 != NUL)
2541 AppendCharToRedobuff(cmd3);
2542 if (cmd4 != NUL)
2543 AppendCharToRedobuff(cmd4);
2544 if (cmd5 != NUL)
2545 AppendCharToRedobuff(cmd5);
2546}
2547
2548/*
2549 * check for operator active and clear it
2550 *
2551 * return TRUE if operator was active
2552 */
2553 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002554checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 if (oap->op_type == OP_NOP)
2557 return FALSE;
2558 clearopbeep(oap);
2559 return TRUE;
2560}
2561
2562/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00002563 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00002565 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 */
2567 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002568checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002570 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 return FALSE;
2572 clearopbeep(oap);
2573 return TRUE;
2574}
2575
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002576 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002577clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578{
2579 oap->op_type = OP_NOP;
2580 oap->regname = 0;
2581 oap->motion_force = NUL;
2582 oap->use_reg_one = FALSE;
2583}
2584
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02002585 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002586clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
2588 clearop(oap);
2589 beep_flush();
2590}
2591
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592/*
2593 * Remove the shift modifier from a special key.
2594 */
2595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002596unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597{
2598 switch (cap->cmdchar)
2599 {
2600 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
2601 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
2602 case K_S_UP: cap->cmdchar = K_UP; break;
2603 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
2604 case K_S_HOME: cap->cmdchar = K_HOME; break;
2605 case K_S_END: cap->cmdchar = K_END; break;
2606 }
2607 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
2608}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02002610/*
2611 * If the mode is currently displayed clear the command line or update the
2612 * command displayed.
2613 */
2614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002615may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02002616{
2617 if (mode_displayed)
2618 clear_cmdline = TRUE; /* unshow visual mode later */
2619#ifdef FEAT_CMDL_INFO
2620 else
2621 clear_showcmd();
2622#endif
2623}
2624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625#if defined(FEAT_CMDL_INFO) || defined(PROTO)
2626/*
2627 * Routines for displaying a partly typed command
2628 */
2629
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002630#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631static char_u showcmd_buf[SHOWCMD_BUFLEN];
2632static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
2633static int showcmd_is_clear = TRUE;
2634static int showcmd_visual = FALSE;
2635
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002636static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002639clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 if (!p_sc)
2642 return;
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (VIsual_active && !char_avail())
2645 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002646 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 long lines;
2648 colnr_T leftcol, rightcol;
2649 linenr_T top, bot;
2650
2651 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00002652 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
2654 top = VIsual.lnum;
2655 bot = curwin->w_cursor.lnum;
2656 }
2657 else
2658 {
2659 top = curwin->w_cursor.lnum;
2660 bot = VIsual.lnum;
2661 }
2662# ifdef FEAT_FOLDING
2663 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002664 (void)hasFolding(top, &top, NULL);
2665 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666# endif
2667 lines = bot - top + 1;
2668
2669 if (VIsual_mode == Ctrl_V)
2670 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02002671# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00002672 char_u *saved_sbr = p_sbr;
2673
2674 /* Make 'sbr' empty for a moment to get the correct size. */
2675 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02002676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02002678# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00002679 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02002680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
2682 (long)(rightcol - leftcol + 1));
2683 }
2684 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
2685 sprintf((char *)showcmd_buf, "%ld", lines);
2686 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02002687 {
2688 char_u *s, *e;
2689 int l;
2690 int bytes = 0;
2691 int chars = 0;
2692
2693 if (cursor_bot)
2694 {
2695 s = ml_get_pos(&VIsual);
2696 e = ml_get_cursor();
2697 }
2698 else
2699 {
2700 s = ml_get_cursor();
2701 e = ml_get_pos(&VIsual);
2702 }
2703 while ((*p_sel != 'e') ? s <= e : s < e)
2704 {
2705 l = (*mb_ptr2len)(s);
2706 if (l == 0)
2707 {
2708 ++bytes;
2709 ++chars;
2710 break; /* end of line */
2711 }
2712 bytes += l;
2713 ++chars;
2714 s += l;
2715 }
2716 if (bytes == chars)
2717 sprintf((char *)showcmd_buf, "%d", chars);
2718 else
2719 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
2720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
2722 showcmd_visual = TRUE;
2723 }
2724 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
2726 showcmd_buf[0] = NUL;
2727 showcmd_visual = FALSE;
2728
2729 /* Don't actually display something if there is nothing to clear. */
2730 if (showcmd_is_clear)
2731 return;
2732 }
2733
2734 display_showcmd();
2735}
2736
2737/*
2738 * Add 'c' to string of shown command chars.
2739 * Return TRUE if output has been written (and setcursor() has been called).
2740 */
2741 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002742add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 char_u *p;
2745 int old_len;
2746 int extra_len;
2747 int overflow;
2748#if defined(FEAT_MOUSE)
2749 int i;
2750 static int ignore[] =
2751 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00002752# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
2754 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00002755# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01002756 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002757 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
2759 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002760 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002762 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 0
2764 };
2765#endif
2766
Bram Moolenaar09df3122006-01-23 22:23:09 +00002767 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 return FALSE;
2769
2770 if (showcmd_visual)
2771 {
2772 showcmd_buf[0] = NUL;
2773 showcmd_visual = FALSE;
2774 }
2775
2776#if defined(FEAT_MOUSE)
2777 /* Ignore keys that are scrollbar updates and mouse clicks */
2778 if (IS_SPECIAL(c))
2779 for (i = 0; ignore[i] != 0; ++i)
2780 if (ignore[i] == c)
2781 return FALSE;
2782#endif
2783
2784 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01002785 if (*p == ' ')
2786 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 old_len = (int)STRLEN(showcmd_buf);
2788 extra_len = (int)STRLEN(p);
2789 overflow = old_len + extra_len - SHOWCMD_COLS;
2790 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00002791 mch_memmove(showcmd_buf, showcmd_buf + overflow,
2792 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 STRCAT(showcmd_buf, p);
2794
2795 if (char_avail())
2796 return FALSE;
2797
2798 display_showcmd();
2799
2800 return TRUE;
2801}
2802
2803 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002804add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805{
2806 if (!add_to_showcmd(c))
2807 setcursor();
2808}
2809
2810/*
2811 * Delete 'len' characters from the end of the shown command.
2812 */
2813 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002814del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816 int old_len;
2817
2818 if (!p_sc)
2819 return;
2820
2821 old_len = (int)STRLEN(showcmd_buf);
2822 if (len > old_len)
2823 len = old_len;
2824 showcmd_buf[old_len - len] = NUL;
2825
2826 if (!char_avail())
2827 display_showcmd();
2828}
2829
2830/*
2831 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
2832 * something and there is a partial mapping.
2833 */
2834 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002835push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
2837 if (p_sc)
2838 STRCPY(old_showcmd_buf, showcmd_buf);
2839}
2840
2841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002842pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844 if (!p_sc)
2845 return;
2846
2847 STRCPY(showcmd_buf, old_showcmd_buf);
2848
2849 display_showcmd();
2850}
2851
2852 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002853display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 int len;
2856
2857 cursor_off();
2858
2859 len = (int)STRLEN(showcmd_buf);
2860 if (len == 0)
2861 showcmd_is_clear = TRUE;
2862 else
2863 {
2864 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
2865 showcmd_is_clear = FALSE;
2866 }
2867
2868 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002869 * clear the rest of an old message by outputting up to SHOWCMD_COLS
2870 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 */
2872 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
2873
2874 setcursor(); /* put cursor back where it belongs */
2875}
2876#endif
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878/*
2879 * When "check" is FALSE, prepare for commands that scroll the window.
2880 * When "check" is TRUE, take care of scroll-binding after the window has
2881 * scrolled. Called from normal_cmd() and edit().
2882 */
2883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002884do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 static win_T *old_curwin = NULL;
2887 static linenr_T old_topline = 0;
2888#ifdef FEAT_DIFF
2889 static int old_topfill = 0;
2890#endif
2891 static buf_T *old_buf = NULL;
2892 static colnr_T old_leftcol = 0;
2893
2894 if (check && curwin->w_p_scb)
2895 {
2896 /* If a ":syncbind" command was just used, don't scroll, only reset
2897 * the values. */
2898 if (did_syncbind)
2899 did_syncbind = FALSE;
2900 else if (curwin == old_curwin)
2901 {
2902 /*
2903 * Synchronize other windows, as necessary according to
2904 * 'scrollbind'. Don't do this after an ":edit" command, except
2905 * when 'diff' is set.
2906 */
2907 if ((curwin->w_buffer == old_buf
2908#ifdef FEAT_DIFF
2909 || curwin->w_p_diff
2910#endif
2911 )
2912 && (curwin->w_topline != old_topline
2913#ifdef FEAT_DIFF
2914 || curwin->w_topfill != old_topfill
2915#endif
2916 || curwin->w_leftcol != old_leftcol))
2917 {
2918 check_scrollbind(curwin->w_topline - old_topline,
2919 (long)(curwin->w_leftcol - old_leftcol));
2920 }
2921 }
2922 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
2923 {
2924 /*
2925 * When switching between windows, make sure that the relative
2926 * vertical offset is valid for the new window. The relative
2927 * offset is invalid whenever another 'scrollbind' window has
2928 * scrolled to a point that would force the current window to
2929 * scroll past the beginning or end of its buffer. When the
2930 * resync is performed, some of the other 'scrollbind' windows may
2931 * need to jump so that the current window's relative position is
2932 * visible on-screen.
2933 */
2934 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
2935 }
2936 curwin->w_scbind_pos = curwin->w_topline;
2937 }
2938
2939 old_curwin = curwin;
2940 old_topline = curwin->w_topline;
2941#ifdef FEAT_DIFF
2942 old_topfill = curwin->w_topfill;
2943#endif
2944 old_buf = curwin->w_buffer;
2945 old_leftcol = curwin->w_leftcol;
2946}
2947
2948/*
2949 * Synchronize any windows that have "scrollbind" set, based on the
2950 * number of rows by which the current window has changed
2951 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
2952 */
2953 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002954check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 int want_ver;
2957 int want_hor;
2958 win_T *old_curwin = curwin;
2959 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 int old_VIsual_select = VIsual_select;
2961 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 colnr_T tgt_leftcol = curwin->w_leftcol;
2963 long topline;
2964 long y;
2965
2966 /*
2967 * check 'scrollopt' string for vertical and horizontal scroll options
2968 */
2969 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
2970#ifdef FEAT_DIFF
2971 want_ver |= old_curwin->w_p_diff;
2972#endif
2973 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
2974
2975 /*
2976 * loop through the scrollbound windows and scroll accordingly
2977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002979 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
2981 curbuf = curwin->w_buffer;
2982 /* skip original window and windows with 'noscrollbind' */
2983 if (curwin != old_curwin && curwin->w_p_scb)
2984 {
2985 /*
2986 * do the vertical scroll
2987 */
2988 if (want_ver)
2989 {
2990#ifdef FEAT_DIFF
2991 if (old_curwin->w_p_diff && curwin->w_p_diff)
2992 {
2993 diff_set_topline(old_curwin, curwin);
2994 }
2995 else
2996#endif
2997 {
2998 curwin->w_scbind_pos += topline_diff;
2999 topline = curwin->w_scbind_pos;
3000 if (topline > curbuf->b_ml.ml_line_count)
3001 topline = curbuf->b_ml.ml_line_count;
3002 if (topline < 1)
3003 topline = 1;
3004
3005 y = topline - curwin->w_topline;
3006 if (y > 0)
3007 scrollup(y, FALSE);
3008 else
3009 scrolldown(-y, FALSE);
3010 }
3011
3012 redraw_later(VALID);
3013 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 }
3016
3017 /*
3018 * do the horizontal scroll
3019 */
3020 if (want_hor && curwin->w_leftcol != tgt_leftcol)
3021 {
3022 curwin->w_leftcol = tgt_leftcol;
3023 leftcol_changed();
3024 }
3025 }
3026 }
3027
3028 /*
3029 * reset current-window
3030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 VIsual_select = old_VIsual_select;
3032 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 curwin = old_curwin;
3034 curbuf = old_curbuf;
3035}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
3037/*
3038 * Command character that's ignored.
3039 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003040 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003043nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
Bram Moolenaarfc735152005-03-22 22:54:12 +00003045 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046}
3047
3048/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00003049 * Command character that doesn't do anything, but unlike nv_ignore() does
3050 * start edit(). Used for "startinsert" executed while starting up.
3051 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00003052 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003053nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00003054{
3055}
3056
3057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 * Command character doesn't exist.
3059 */
3060 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003061nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 clearopbeep(cap->oap);
3064}
3065
3066/*
3067 * <Help> and <F1> commands.
3068 */
3069 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003070nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072 if (!checkclearopq(cap->oap))
3073 ex_help(NULL);
3074}
3075
3076/*
3077 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
3078 */
3079 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003080nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
Bram Moolenaarf2732452018-06-03 14:47:35 +02003082#ifdef FEAT_JOB_CHANNEL
3083 if (bt_prompt(curbuf) && !prompt_curpos_editable())
3084 clearopbeep(cap->oap);
3085 else
3086#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01003087 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02003088 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01003089 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01003090 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
3091 op_addsub(cap->oap, cap->count1, cap->arg);
3092 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02003093 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01003094 else if (VIsual_active)
3095 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02003096 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01003097 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098}
3099
3100/*
3101 * CTRL-F, CTRL-B, etc: Scroll page up or down.
3102 */
3103 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003104nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003107 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003108 if (mod_mask & MOD_MASK_CTRL)
3109 {
3110 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
3111 if (cap->arg == BACKWARD)
3112 goto_tabpage(-(int)cap->count1);
3113 else
3114 goto_tabpage((int)cap->count0);
3115 }
3116 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003117 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119}
3120
3121/*
3122 * Implementation of "gd" and "gD" command.
3123 */
3124 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003125nv_gd(
3126 oparg_T *oap,
3127 int nchar,
3128 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 char_u *ptr;
3132
Bram Moolenaard9d30582005-05-18 22:10:28 +00003133 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02003134 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
3135 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003137#ifdef FEAT_FOLDING
3138 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
3139 foldOpenCursor();
3140#endif
3141}
3142
3143/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02003144 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
3145 * otherwise.
3146 */
3147 static int
3148is_ident(char_u *line, int offset)
3149{
3150 int i;
3151 int incomment = FALSE;
3152 int instring = 0;
3153 int prev = 0;
3154
3155 for (i = 0; i < offset && line[i] != NUL; i++)
3156 {
3157 if (instring != 0)
3158 {
3159 if (prev != '\\' && line[i] == instring)
3160 instring = 0;
3161 }
3162 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
3163 {
3164 instring = line[i];
3165 }
3166 else
3167 {
3168 if (incomment)
3169 {
3170 if (prev == '*' && line[i] == '/')
3171 incomment = FALSE;
3172 }
3173 else if (prev == '/' && line[i] == '*')
3174 {
3175 incomment = TRUE;
3176 }
3177 else if (prev == '/' && line[i] == '/')
3178 {
3179 return FALSE;
3180 }
3181 }
3182
3183 prev = line[i];
3184 }
3185
3186 return incomment == FALSE && instring == 0;
3187}
3188
3189/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003190 * Search for variable declaration of "ptr[len]".
3191 * When "locally" is TRUE in the current function ("gd"), otherwise in the
3192 * current file ("gD").
3193 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003194 * Return FAIL when not found.
3195 */
3196 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003197find_decl(
3198 char_u *ptr,
3199 int len,
3200 int locally,
3201 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02003202 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003203{
3204 char_u *pat;
3205 pos_T old_pos;
3206 pos_T par_pos;
3207 pos_T found_pos;
3208 int t;
3209 int save_p_ws;
3210 int save_p_scs;
3211 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003212 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02003213 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02003214 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003215
3216 if ((pat = alloc(len + 7)) == NULL)
3217 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00003218
3219 /* Put "\V" before the pattern to avoid that the special meaning of "."
3220 * and "~" causes trouble. */
3221 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
3222 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 old_pos = curwin->w_cursor;
3224 save_p_ws = p_ws;
3225 save_p_scs = p_scs;
3226 p_ws = FALSE; /* don't wrap around end of file now */
3227 p_scs = FALSE; /* don't switch ignorecase off now */
3228
3229 /*
3230 * With "gD" go to line 1.
3231 * With "gd" Search back for the start of the current function, then go
3232 * back until a blank line. If this fails go to line 1.
3233 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00003234 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
3236 setpcmark(); /* Set in findpar() otherwise */
3237 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003238 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
3240 else
3241 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00003242 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
3244 --curwin->w_cursor.lnum;
3245 }
3246 curwin->w_cursor.col = 0;
3247
3248 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003249 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003250 for (;;)
3251 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003252 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003253 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003254 if (curwin->w_cursor.lnum >= old_pos.lnum)
3255 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003256
Bram Moolenaar0fd92892006-03-09 22:27:48 +00003257 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003258 {
3259 pos_T *pos;
3260
3261 /* Check that the block the match is in doesn't end before the
3262 * position where we started the search from. */
3263 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
3264 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
3265 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02003266 {
3267 /* There can't be a useful match before the end of this block.
3268 * Skip to the end. */
3269 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003270 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02003271 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003272 }
3273
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003274 if (t == FAIL)
3275 {
3276 /* If we previously found a valid position, use it. */
3277 if (found_pos.lnum != 0)
3278 {
3279 curwin->w_cursor = found_pos;
3280 t = OK;
3281 }
3282 break;
3283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02003285 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003286 {
3287 /* Ignore this line, continue at start of next line. */
3288 ++curwin->w_cursor.lnum;
3289 curwin->w_cursor.col = 0;
3290 continue;
3291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02003293 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
3294
3295 /* If the current position is not a valid identifier and a previous
3296 * match is present, favor that one instead. */
3297 if (!valid && found_pos.lnum != 0)
3298 {
3299 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003300 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02003301 }
3302
3303 /* Global search: use first valid match found */
3304 if (valid && !locally)
3305 break;
3306 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003307 {
3308 /* If we previously found a valid position, use it. */
3309 if (found_pos.lnum != 0)
3310 curwin->w_cursor = found_pos;
3311 break;
3312 }
3313
Bram Moolenaar226630a2016-10-08 19:21:31 +02003314 /* For finding a local variable and the match is before the "{" or
3315 * inside a comment, continue searching. For K&R style function
3316 * declarations this skips the function header without types. */
3317 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003318 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02003319 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02003320 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02003321 /* Remove SEARCH_START from flags to avoid getting stuck at one
3322 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02003323 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003325
3326 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003328 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 curwin->w_cursor = old_pos;
3330 }
3331 else
3332 {
3333 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 /* "n" searches forward now */
3335 reset_search_dir();
3336 }
3337
3338 vim_free(pat);
3339 p_ws = save_p_ws;
3340 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003341
3342 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343}
3344
3345/*
3346 * Move 'dist' lines in direction 'dir', counting lines by *screen*
3347 * lines rather than lines in the file.
3348 * 'dist' must be positive.
3349 *
3350 * Return OK if able to move cursor, FAIL otherwise.
3351 */
3352 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003353nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 int linelen = linetabsize(ml_get_curline());
3356 int retval = OK;
3357 int atend = FALSE;
3358 int n;
3359 int col_off1; /* margin offset for first screen line */
3360 int col_off2; /* margin offset for wrapped screen line */
3361 int width1; /* text width for first screen line */
3362 int width2; /* test width for wrapped screen line */
3363
3364 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02003365 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367 col_off1 = curwin_col_off();
3368 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02003369 width1 = curwin->w_width - col_off1;
3370 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01003371 if (width2 == 0)
3372 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01003375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 /*
3377 * Instead of sticking at the last character of the buffer line we
3378 * try to stick in the last column of the screen.
3379 */
3380 if (curwin->w_curswant == MAXCOL)
3381 {
3382 atend = TRUE;
3383 validate_virtcol();
3384 if (width1 <= 0)
3385 curwin->w_curswant = 0;
3386 else
3387 {
3388 curwin->w_curswant = width1 - 1;
3389 if (curwin->w_virtcol > curwin->w_curswant)
3390 curwin->w_curswant += ((curwin->w_virtcol
3391 - curwin->w_curswant - 1) / width2 + 1) * width2;
3392 }
3393 }
3394 else
3395 {
3396 if (linelen > width1)
3397 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
3398 else
3399 n = width1;
3400 if (curwin->w_curswant > (colnr_T)n + 1)
3401 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
3402 * width2;
3403 }
3404
3405 while (dist--)
3406 {
3407 if (dir == BACKWARD)
3408 {
Bram Moolenaar03ac52f2019-09-24 22:47:46 +02003409 if ((long)curwin->w_curswant > width2)
3410 // move back within line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 curwin->w_curswant -= width2;
3412 else
3413 {
3414 /* to previous line */
3415 if (curwin->w_cursor.lnum == 1)
3416 {
3417 retval = FAIL;
3418 break;
3419 }
3420 --curwin->w_cursor.lnum;
3421#ifdef FEAT_FOLDING
3422 /* Move to the start of a closed fold. Don't do that when
3423 * 'foldopen' contains "all": it will open in a moment. */
3424 if (!(fdo_flags & FDO_ALL))
3425 (void)hasFolding(curwin->w_cursor.lnum,
3426 &curwin->w_cursor.lnum, NULL);
3427#endif
3428 linelen = linetabsize(ml_get_curline());
3429 if (linelen > width1)
3430 curwin->w_curswant += (((linelen - width1 - 1) / width2)
3431 + 1) * width2;
3432 }
3433 }
3434 else /* dir == FORWARD */
3435 {
3436 if (linelen > width1)
3437 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
3438 else
3439 n = width1;
3440 if (curwin->w_curswant + width2 < (colnr_T)n)
3441 /* move forward within line */
3442 curwin->w_curswant += width2;
3443 else
3444 {
3445 /* to next line */
3446#ifdef FEAT_FOLDING
3447 /* Move to the end of a closed fold. */
3448 (void)hasFolding(curwin->w_cursor.lnum, NULL,
3449 &curwin->w_cursor.lnum);
3450#endif
3451 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
3452 {
3453 retval = FAIL;
3454 break;
3455 }
3456 curwin->w_cursor.lnum++;
3457 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02003458 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 }
3461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01003464 if (virtual_active() && atend)
3465 coladvance(MAXCOL);
3466 else
3467 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
3470 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02003471 colnr_T virtcol;
3472
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 /*
3474 * Check for landing on a character that got split at the end of the
3475 * last line. We want to advance a screenline, not end up in the same
3476 * screenline or move two screenlines.
3477 */
3478 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02003479 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003480#if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02003481 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
3482 virtcol -= vim_strsize(p_sbr);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003483#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02003484
3485 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 && (curwin->w_curswant < (colnr_T)width1
3487 ? (curwin->w_curswant > (colnr_T)width1 / 2)
3488 : ((curwin->w_curswant - width1) % width2
3489 > (colnr_T)width2 / 2)))
3490 --curwin->w_cursor.col;
3491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493 if (atend)
3494 curwin->w_curswant = MAXCOL; /* stick in the last column */
3495
3496 return retval;
3497}
3498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499/*
3500 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
3501 * cap->arg must be TRUE for CTRL-E.
3502 */
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02003503 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003504nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505{
3506 if (!checkclearop(cap->oap))
3507 scroll_redraw(cap->arg, cap->count1);
3508}
3509
3510/*
3511 * Scroll "count" lines up or down, and redraw.
3512 */
3513 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003514scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 linenr_T prev_topline = curwin->w_topline;
3517#ifdef FEAT_DIFF
3518 int prev_topfill = curwin->w_topfill;
3519#endif
3520 linenr_T prev_lnum = curwin->w_cursor.lnum;
3521
3522 if (up)
3523 scrollup(count, TRUE);
3524 else
3525 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01003526 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
3529 * valid, otherwise the screen jumps back at the end of the file. */
3530 cursor_correct();
3531 check_cursor_moved(curwin);
3532 curwin->w_valid |= VALID_TOPLINE;
3533
3534 /* If moved back to where we were, at least move the cursor, otherwise
3535 * we get stuck at one position. Don't move the cursor up if the
3536 * first line of the buffer is already on the screen */
3537 while (curwin->w_topline == prev_topline
3538#ifdef FEAT_DIFF
3539 && curwin->w_topfill == prev_topfill
3540#endif
3541 )
3542 {
3543 if (up)
3544 {
3545 if (curwin->w_cursor.lnum > prev_lnum
3546 || cursor_down(1L, FALSE) == FAIL)
3547 break;
3548 }
3549 else
3550 {
3551 if (curwin->w_cursor.lnum < prev_lnum
3552 || prev_topline == 1L
3553 || cursor_up(1L, FALSE) == FAIL)
3554 break;
3555 }
3556 /* Mark w_topline as valid, otherwise the screen jumps back at the
3557 * end of the file. */
3558 check_cursor_moved(curwin);
3559 curwin->w_valid |= VALID_TOPLINE;
3560 }
3561 }
3562 if (curwin->w_cursor.lnum != prev_lnum)
3563 coladvance(curwin->w_curswant);
3564 redraw_later(VALID);
3565}
3566
3567/*
3568 * Commands that start with "z".
3569 */
3570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003571nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
3573 long n;
3574 colnr_T col;
3575 int nchar = cap->nchar;
3576#ifdef FEAT_FOLDING
3577 long old_fdl = curwin->w_p_fdl;
3578 int old_fen = curwin->w_p_fen;
3579#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003580#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00003581 int undo = FALSE;
3582#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01003583 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
3585 if (VIM_ISDIGIT(nchar))
3586 {
3587 /*
3588 * "z123{nchar}": edit the count before obtaining {nchar}
3589 */
3590 if (checkclearop(cap->oap))
3591 return;
3592 n = nchar - '0';
3593 for (;;)
3594 {
3595#ifdef USE_ON_FLY_SCROLL
3596 dont_scroll = TRUE; /* disallow scrolling here */
3597#endif
3598 ++no_mapping;
3599 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003600 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 --no_mapping;
3603 --allow_keys;
3604#ifdef FEAT_CMDL_INFO
3605 (void)add_to_showcmd(nchar);
3606#endif
3607 if (nchar == K_DEL || nchar == K_KDEL)
3608 n /= 10;
3609 else if (VIM_ISDIGIT(nchar))
3610 n = n * 10 + (nchar - '0');
3611 else if (nchar == CAR)
3612 {
3613#ifdef FEAT_GUI
3614 need_mouse_correct = TRUE;
3615#endif
3616 win_setheight((int)n);
3617 break;
3618 }
3619 else if (nchar == 'l'
3620 || nchar == 'h'
3621 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00003622 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
3624 cap->count1 = n ? n * cap->count1 : cap->count1;
3625 goto dozet;
3626 }
3627 else
3628 {
3629 clearopbeep(cap->oap);
3630 break;
3631 }
3632 }
3633 cap->oap->op_type = OP_NOP;
3634 return;
3635 }
3636
3637dozet:
3638 if (
3639#ifdef FEAT_FOLDING
3640 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
3641 * and "zC" only in Visual mode. "zj" and "zk" are motion
3642 * commands. */
3643 cap->nchar != 'f' && cap->nchar != 'F'
3644 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
3645 && cap->nchar != 'j' && cap->nchar != 'k'
3646 &&
3647#endif
3648 checkclearop(cap->oap))
3649 return;
3650
3651 /*
3652 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
3653 * If line number given, set cursor.
3654 */
3655 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
3656 && cap->count0
3657 && cap->count0 != curwin->w_cursor.lnum)
3658 {
3659 setpcmark();
3660 if (cap->count0 > curbuf->b_ml.ml_line_count)
3661 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3662 else
3663 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003664 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666
3667 switch (nchar)
3668 {
3669 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
3670 case '+':
3671 if (cap->count0 == 0)
3672 {
3673 /* No count given: put cursor at the line below screen */
3674 validate_botline(); /* make sure w_botline is valid */
3675 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
3676 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
3677 else
3678 curwin->w_cursor.lnum = curwin->w_botline;
3679 }
3680 /* FALLTHROUGH */
3681 case NL:
3682 case CAR:
3683 case K_KENTER:
3684 beginline(BL_WHITE | BL_FIX);
3685 /* FALLTHROUGH */
3686
3687 case 't': scroll_cursor_top(0, TRUE);
3688 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003689 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 break;
3691
3692 /* "z." and "zz": put cursor in middle of screen */
3693 case '.': beginline(BL_WHITE | BL_FIX);
3694 /* FALLTHROUGH */
3695
3696 case 'z': scroll_cursor_halfway(TRUE);
3697 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003698 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 break;
3700
3701 /* "z^", "z-" and "zb": put cursor at bottom of screen */
3702 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
3703 * when <count> is at bottom of window, and puts that one at
3704 * bottom of window. */
3705 if (cap->count0 != 0)
3706 {
3707 scroll_cursor_bot(0, TRUE);
3708 curwin->w_cursor.lnum = curwin->w_topline;
3709 }
3710 else if (curwin->w_topline == 1)
3711 curwin->w_cursor.lnum = 1;
3712 else
3713 curwin->w_cursor.lnum = curwin->w_topline - 1;
3714 /* FALLTHROUGH */
3715 case '-':
3716 beginline(BL_WHITE | BL_FIX);
3717 /* FALLTHROUGH */
3718
3719 case 'b': scroll_cursor_bot(0, TRUE);
3720 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01003721 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 break;
3723
3724 /* "zH" - scroll screen right half-page */
3725 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02003726 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 /* FALLTHROUGH */
3728
3729 /* "zh" - scroll screen to the right */
3730 case 'h':
3731 case K_LEFT:
3732 if (!curwin->w_p_wrap)
3733 {
3734 if ((colnr_T)cap->count1 > curwin->w_leftcol)
3735 curwin->w_leftcol = 0;
3736 else
3737 curwin->w_leftcol -= (colnr_T)cap->count1;
3738 leftcol_changed();
3739 }
3740 break;
3741
3742 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02003743 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 /* FALLTHROUGH */
3745
3746 /* "zl" - scroll screen to the left */
3747 case 'l':
3748 case K_RIGHT:
3749 if (!curwin->w_p_wrap)
3750 {
3751 /* scroll the window left */
3752 curwin->w_leftcol += (colnr_T)cap->count1;
3753 leftcol_changed();
3754 }
3755 break;
3756
3757 /* "zs" - scroll screen, cursor at the start */
3758 case 's': if (!curwin->w_p_wrap)
3759 {
3760#ifdef FEAT_FOLDING
3761 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3762 col = 0; /* like the cursor is in col 0 */
3763 else
3764#endif
3765 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01003766 if ((long)col > siso)
3767 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 else
3769 col = 0;
3770 if (curwin->w_leftcol != col)
3771 {
3772 curwin->w_leftcol = col;
3773 redraw_later(NOT_VALID);
3774 }
3775 }
3776 break;
3777
3778 /* "ze" - scroll screen, cursor at the end */
3779 case 'e': if (!curwin->w_p_wrap)
3780 {
3781#ifdef FEAT_FOLDING
3782 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3783 col = 0; /* like the cursor is in col 0 */
3784 else
3785#endif
3786 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02003787 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01003788 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 col = 0;
3790 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01003791 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (curwin->w_leftcol != col)
3793 {
3794 curwin->w_leftcol = col;
3795 redraw_later(NOT_VALID);
3796 }
3797 }
3798 break;
3799
3800#ifdef FEAT_FOLDING
3801 /* "zF": create fold command */
3802 /* "zf": create fold operator */
3803 case 'F':
3804 case 'f': if (foldManualAllowed(TRUE))
3805 {
3806 cap->nchar = 'f';
3807 nv_operator(cap);
3808 curwin->w_p_fen = TRUE;
3809
3810 /* "zF" is like "zfzf" */
3811 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
3812 {
3813 nv_operator(cap);
3814 finish_op = TRUE;
3815 }
3816 }
3817 else
3818 clearopbeep(cap->oap);
3819 break;
3820
3821 /* "zd": delete fold at cursor */
3822 /* "zD": delete fold at cursor recursively */
3823 case 'd':
3824 case 'D': if (foldManualAllowed(FALSE))
3825 {
3826 if (VIsual_active)
3827 nv_operator(cap);
3828 else
3829 deleteFold(curwin->w_cursor.lnum,
3830 curwin->w_cursor.lnum, nchar == 'D', FALSE);
3831 }
3832 break;
3833
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003834 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 case 'E': if (foldmethodIsManual(curwin))
3836 {
3837 clearFolding(curwin);
3838 changed_window_setting();
3839 }
3840 else if (foldmethodIsMarker(curwin))
3841 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
3842 TRUE, FALSE);
3843 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003844 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 break;
3846
3847 /* "zn": fold none: reset 'foldenable' */
3848 case 'n': curwin->w_p_fen = FALSE;
3849 break;
3850
3851 /* "zN": fold Normal: set 'foldenable' */
3852 case 'N': curwin->w_p_fen = TRUE;
3853 break;
3854
3855 /* "zi": invert folding: toggle 'foldenable' */
3856 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
3857 break;
3858
3859 /* "za": open closed fold or close open fold at cursor */
3860 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3861 openFold(curwin->w_cursor.lnum, cap->count1);
3862 else
3863 {
3864 closeFold(curwin->w_cursor.lnum, cap->count1);
3865 curwin->w_p_fen = TRUE;
3866 }
3867 break;
3868
3869 /* "zA": open fold at cursor recursively */
3870 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
3871 openFoldRecurse(curwin->w_cursor.lnum);
3872 else
3873 {
3874 closeFoldRecurse(curwin->w_cursor.lnum);
3875 curwin->w_p_fen = TRUE;
3876 }
3877 break;
3878
3879 /* "zo": open fold at cursor or Visual area */
3880 case 'o': if (VIsual_active)
3881 nv_operator(cap);
3882 else
3883 openFold(curwin->w_cursor.lnum, cap->count1);
3884 break;
3885
3886 /* "zO": open fold recursively */
3887 case 'O': if (VIsual_active)
3888 nv_operator(cap);
3889 else
3890 openFoldRecurse(curwin->w_cursor.lnum);
3891 break;
3892
3893 /* "zc": close fold at cursor or Visual area */
3894 case 'c': if (VIsual_active)
3895 nv_operator(cap);
3896 else
3897 closeFold(curwin->w_cursor.lnum, cap->count1);
3898 curwin->w_p_fen = TRUE;
3899 break;
3900
3901 /* "zC": close fold recursively */
3902 case 'C': if (VIsual_active)
3903 nv_operator(cap);
3904 else
3905 closeFoldRecurse(curwin->w_cursor.lnum);
3906 curwin->w_p_fen = TRUE;
3907 break;
3908
3909 /* "zv": open folds at the cursor */
3910 case 'v': foldOpenCursor();
3911 break;
3912
3913 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
3914 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02003915 curwin->w_foldinvalid = TRUE; /* recompute folds */
3916 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 foldOpenCursor();
3918 break;
3919
3920 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
3921 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02003922 curwin->w_foldinvalid = TRUE; /* recompute folds */
3923 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 break;
3925
3926 /* "zm": fold more */
3927 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003928 {
3929 curwin->w_p_fdl -= cap->count1;
3930 if (curwin->w_p_fdl < 0)
3931 curwin->w_p_fdl = 0;
3932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 old_fdl = -1; /* force an update */
3934 curwin->w_p_fen = TRUE;
3935 break;
3936
3937 /* "zM": close all folds */
3938 case 'M': curwin->w_p_fdl = 0;
3939 old_fdl = -1; /* force an update */
3940 curwin->w_p_fen = TRUE;
3941 break;
3942
3943 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02003944 case 'r': curwin->w_p_fdl += cap->count1;
3945 {
3946 int d = getDeepestNesting();
3947
3948 if (curwin->w_p_fdl >= d)
3949 curwin->w_p_fdl = d;
3950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 break;
3952
3953 /* "zR": open all folds */
3954 case 'R': curwin->w_p_fdl = getDeepestNesting();
3955 old_fdl = -1; /* force an update */
3956 break;
3957
3958 case 'j': /* "zj" move to next fold downwards */
3959 case 'k': /* "zk" move to next fold upwards */
3960 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
3961 cap->count1) == FAIL)
3962 clearopbeep(cap->oap);
3963 break;
3964
3965#endif /* FEAT_FOLDING */
3966
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00003967#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00003968 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
3969 ++no_mapping;
3970 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00003971 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00003972 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00003973 --no_mapping;
3974 --allow_keys;
3975#ifdef FEAT_CMDL_INFO
3976 (void)add_to_showcmd(nchar);
3977#endif
3978 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
3979 {
3980 clearopbeep(cap->oap);
3981 break;
3982 }
3983 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003984 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00003985
Bram Moolenaarb765d632005-06-07 21:00:02 +00003986 case 'g': /* "zg": add good word to word list */
3987 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00003988 case 'G': /* "zG": add good word to temp word list */
3989 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003990 {
3991 char_u *ptr = NULL;
3992 int len;
3993
3994 if (checkclearop(cap->oap))
3995 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003996 if (VIsual_active && get_visual_text(cap, &ptr, &len)
3997 == FAIL)
3998 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003999 if (ptr == NULL)
4000 {
4001 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004002
Bram Moolenaar134bf072013-09-25 18:54:24 +02004003 /* Find bad word under the cursor. When 'spell' is
4004 * off this fails and find_ident_under_cursor() is
4005 * used below. */
4006 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004007 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02004008 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004009 if (len != 0 && curwin->w_cursor.col <= pos.col)
4010 ptr = ml_get_pos(&curwin->w_cursor);
4011 curwin->w_cursor = pos;
4012 }
4013
Bram Moolenaarb765d632005-06-07 21:00:02 +00004014 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
4015 FIND_IDENT)) == 0)
4016 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02004017 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
4018 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00004019 (nchar == 'G' || nchar == 'W')
4020 ? 0 : (int)cap->count1,
4021 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004022 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00004023 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004024
Bram Moolenaar43abc522005-12-10 20:15:02 +00004025 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00004026 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00004027 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004028 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004029#endif
4030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 default: clearopbeep(cap->oap);
4032 }
4033
4034#ifdef FEAT_FOLDING
4035 /* Redraw when 'foldenable' changed */
4036 if (old_fen != curwin->w_p_fen)
4037 {
4038# ifdef FEAT_DIFF
4039 win_T *wp;
4040
4041 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
4042 {
4043 /* Adjust 'foldenable' in diff-synced windows. */
4044 FOR_ALL_WINDOWS(wp)
4045 {
4046 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
4047 {
4048 wp->w_p_fen = curwin->w_p_fen;
4049 changed_window_setting_win(wp);
4050 }
4051 }
4052 }
4053# endif
4054 changed_window_setting();
4055 }
4056
4057 /* Redraw when 'foldlevel' changed. */
4058 if (old_fdl != curwin->w_p_fdl)
4059 newFoldLevel();
4060#endif
4061}
4062
4063#ifdef FEAT_GUI
4064/*
4065 * Vertical scrollbar movement.
4066 */
4067 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004068nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069{
4070 if (cap->oap->op_type != OP_NOP)
4071 clearopbeep(cap->oap);
4072
4073 /* Even if an operator was pending, we still want to scroll */
4074 gui_do_scroll();
4075}
4076
4077/*
4078 * Horizontal scrollbar movement.
4079 */
4080 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004081nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
4083 if (cap->oap->op_type != OP_NOP)
4084 clearopbeep(cap->oap);
4085
4086 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004087 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088}
4089#endif
4090
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004091#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004092/*
4093 * Click in GUI tab.
4094 */
4095 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004096nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004097{
4098 if (cap->oap->op_type != OP_NOP)
4099 clearopbeep(cap->oap);
4100
4101 /* Even if an operator was pending, we still want to jump tabs. */
4102 goto_tabpage(current_tab);
4103}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004104
4105/*
4106 * Selected item in tab line menu.
4107 */
4108 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004109nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004110{
4111 if (cap->oap->op_type != OP_NOP)
4112 clearopbeep(cap->oap);
4113
4114 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004115 handle_tabmenu();
4116}
4117
4118/*
4119 * Handle selecting an item of the GUI tab line menu.
4120 * Used in Normal and Insert mode.
4121 */
4122 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004123handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004124{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004125 switch (current_tabmenu)
4126 {
4127 case TABLINE_MENU_CLOSE:
4128 if (current_tab == 0)
4129 do_cmdline_cmd((char_u *)"tabclose");
4130 else
4131 {
4132 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
4133 current_tab);
4134 do_cmdline_cmd(IObuff);
4135 }
4136 break;
4137
4138 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01004139 if (current_tab == 0)
4140 do_cmdline_cmd((char_u *)"$tabnew");
4141 else
4142 {
4143 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
4144 current_tab - 1);
4145 do_cmdline_cmd(IObuff);
4146 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004147 break;
4148
4149 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01004150 if (current_tab == 0)
4151 do_cmdline_cmd((char_u *)"browse $tabnew");
4152 else
4153 {
4154 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
4155 current_tab - 1);
4156 do_cmdline_cmd(IObuff);
4157 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004158 break;
4159 }
4160}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004161#endif
4162
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163/*
4164 * "Q" command.
4165 */
4166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004167nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
4169 /*
4170 * Ignore 'Q' in Visual mode, just give a beep.
4171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02004173 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004174 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 do_exmode(FALSE);
4176}
4177
4178/*
4179 * Handle a ":" command.
4180 */
4181 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004182nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
4184 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02004185 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (VIsual_active)
4188 nv_operator(cap);
4189 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 {
4191 if (cap->oap->op_type != OP_NOP)
4192 {
4193 /* Using ":" as a movement is characterwise exclusive. */
4194 cap->oap->motion_type = MCHAR;
4195 cap->oap->inclusive = FALSE;
4196 }
4197 else if (cap->count0)
4198 {
4199 /* translate "count:" into ":.,.+(count - 1)" */
4200 stuffcharReadbuff('.');
4201 if (cap->count0 > 1)
4202 {
4203 stuffReadbuff((char_u *)",.+");
4204 stuffnumReadbuff((long)cap->count0 - 1L);
4205 }
4206 }
4207
4208 /* When typing, don't type below an old message */
4209 if (KeyTyped)
4210 compute_cmdrow();
4211
4212 old_p_im = p_im;
4213
4214 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02004215 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
4217
4218 /* If 'insertmode' changed, enter or exit Insert mode */
4219 if (p_im != old_p_im)
4220 {
4221 if (p_im)
4222 restart_edit = 'i';
4223 else
4224 restart_edit = 0;
4225 }
4226
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02004227 if (cmd_result == FAIL)
4228 /* The Ex command failed, do not execute the operator. */
4229 clearop(cap->oap);
4230 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
4232 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02004233 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
4234 || did_emsg
4235 ))
4236 /* The start of the operator has become invalid by the Ex command.
4237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 clearopbeep(cap->oap);
4239 }
4240}
4241
4242/*
4243 * Handle CTRL-G command.
4244 */
4245 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004246nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (VIsual_active) /* toggle Selection/Visual mode */
4249 {
4250 VIsual_select = !VIsual_select;
4251 showmode();
4252 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004253 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 /* print full name if count given or :cd used */
4255 fileinfo((int)cap->count0, FALSE, TRUE);
4256}
4257
4258/*
4259 * Handle CTRL-H <Backspace> command.
4260 */
4261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004262nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (VIsual_active && VIsual_select)
4265 {
4266 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
4267 v_visop(cap);
4268 }
4269 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 nv_left(cap);
4271}
4272
4273/*
4274 * CTRL-L: clear screen and redraw.
4275 */
4276 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004277nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278{
4279 if (!checkclearop(cap->oap))
4280 {
4281#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
4282 /*
4283 * Right now, the BeBox doesn't seem to have an easy way to detect
4284 * window resizing, so we cheat and make the user detect it
4285 * manually with CTRL-L instead
4286 */
4287 ui_get_shellsize();
4288#endif
4289#ifdef FEAT_SYN_HL
4290 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004291 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02004292# ifdef FEAT_RELTIME
4293 {
4294 win_T *wp;
4295
4296 FOR_ALL_WINDOWS(wp)
4297 wp->w_s->b_syn_slow = FALSE;
4298 }
4299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300#endif
4301 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004302#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
4303# ifdef VIMDLL
4304 if (!gui.in_use)
4305# endif
4306 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01004307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309}
4310
4311/*
4312 * CTRL-O: In Select mode: switch to Visual mode for one command.
4313 * Otherwise: Go to older pcmark.
4314 */
4315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004316nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (VIsual_active && VIsual_select)
4319 {
4320 VIsual_select = FALSE;
4321 showmode();
4322 restart_VIsual_select = 2; /* restart Select mode later */
4323 }
4324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 {
4326 cap->count1 = -cap->count1;
4327 nv_pcmark(cap);
4328 }
4329}
4330
4331/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01004332 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
4333 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 */
4335 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004336nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337{
4338 if (!checkclearopq(cap->oap))
4339 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
4340 GETF_SETMARK|GETF_ALT, FALSE);
4341}
4342
4343/*
4344 * "Z" commands.
4345 */
4346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004347nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348{
4349 if (!checkclearopq(cap->oap))
4350 {
4351 switch (cap->nchar)
4352 {
4353 /* "ZZ": equivalent to ":x". */
4354 case 'Z': do_cmdline_cmd((char_u *)"x");
4355 break;
4356
4357 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
4358 case 'Q': do_cmdline_cmd((char_u *)"q!");
4359 break;
4360
4361 default: clearopbeep(cap->oap);
4362 }
4363 }
4364}
4365
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366/*
4367 * Call nv_ident() as if "c1" was used, with "c2" as next character.
4368 */
4369 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004370do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
4372 oparg_T oa;
4373 cmdarg_T ca;
4374
4375 clear_oparg(&oa);
4376 vim_memset(&ca, 0, sizeof(ca));
4377 ca.oap = &oa;
4378 ca.cmdchar = c1;
4379 ca.nchar = c2;
4380 nv_ident(&ca);
4381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383/*
4384 * Handle the commands that use the word under the cursor.
4385 * [g] CTRL-] :ta to current identifier
4386 * [g] 'K' run program for current identifier
4387 * [g] '*' / to current identifier or string
4388 * [g] '#' ? to current identifier or string
4389 * g ']' :tselect for current identifier
4390 */
4391 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004392nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
4394 char_u *ptr = NULL;
4395 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02004396 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02004397 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 char_u *p;
4399 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02004400 int kp_help; /* 'keywordprg' is ":he" */
4401 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int n = 0; /* init for GCC */
4403 int cmdchar;
4404 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01004405 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 char_u *aux_ptr;
4407 int isman;
4408 int isman_s;
4409
4410 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
4411 {
4412 cmdchar = cap->nchar;
4413 g_cmd = TRUE;
4414 }
4415 else
4416 {
4417 cmdchar = cap->cmdchar;
4418 g_cmd = FALSE;
4419 }
4420
4421 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
4422 cmdchar = '#';
4423
4424 /*
4425 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
4426 */
4427 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
4428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
4430 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (checkclearopq(cap->oap))
4432 return;
4433 }
4434
4435 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
4436 (cmdchar == '*' || cmdchar == '#')
4437 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
4438 {
4439 clearop(cap->oap);
4440 return;
4441 }
4442
4443 /* Allocate buffer to put the command in. Inserting backslashes can
4444 * double the length of the word. p_kp / curbuf->b_p_kp could be added
4445 * and some numbers. */
4446 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
4447 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
4448 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02004449 if (kp_help && *skipwhite(ptr) == NUL)
4450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004451 emsg(_(e_noident)); /* found white space only */
Bram Moolenaara4f99f52017-08-26 16:25:32 +02004452 return;
4453 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02004454 kp_ex = (*kp == ':');
4455 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
4456 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 if (buf == NULL)
4458 return;
4459 buf[0] = NUL;
4460
4461 switch (cmdchar)
4462 {
4463 case '*':
4464 case '#':
4465 /*
4466 * Put cursor at start of word, makes search skip the word
4467 * under the cursor.
4468 * Call setpcmark() first, so "*``" puts the cursor back where
4469 * it was.
4470 */
4471 setpcmark();
4472 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
4473
4474 if (!g_cmd && vim_iswordp(ptr))
4475 STRCPY(buf, "\\<");
4476 no_smartcase = TRUE; /* don't use 'smartcase' now */
4477 break;
4478
4479 case 'K':
4480 if (kp_help)
4481 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02004482 else if (kp_ex)
4483 {
4484 if (cap->count0 != 0)
4485 vim_snprintf((char *)buf, buflen, "%s %ld",
4486 kp, cap->count0);
4487 else
4488 STRCPY(buf, kp);
4489 STRCAT(buf, " ");
4490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 else
4492 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004493 /* An external command will probably use an argument starting
4494 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00004495 while (*ptr == '-' && n > 0)
4496 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004497 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00004498 --n;
4499 }
4500 if (n == 0)
4501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004502 emsg(_(e_noident)); /* found dashes only */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00004503 vim_free(buf);
4504 return;
4505 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 /* When a count is given, turn it into a range. Is this
4508 * really what we want? */
4509 isman = (STRCMP(kp, "man") == 0);
4510 isman_s = (STRCMP(kp, "man -s") == 0);
4511 if (cap->count0 != 0 && !(isman || isman_s))
4512 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
4513
4514 STRCAT(buf, "! ");
4515 if (cap->count0 == 0 && isman_s)
4516 STRCAT(buf, "man");
4517 else
4518 STRCAT(buf, kp);
4519 STRCAT(buf, " ");
4520 if (cap->count0 != 0 && (isman || isman_s))
4521 {
4522 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
4523 STRCAT(buf, " ");
4524 }
4525 }
4526 break;
4527
4528 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01004529 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530#ifdef FEAT_CSCOPE
4531 if (p_cst)
4532 STRCPY(buf, "cstag ");
4533 else
4534#endif
4535 STRCPY(buf, "ts ");
4536 break;
4537
4538 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01004539 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (curbuf->b_help)
4541 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01004543 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01004544 if (g_cmd)
4545 STRCPY(buf, "tj ");
4546 else
4547 sprintf((char *)buf, "%ldta ", cap->count0);
4548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
4550
4551 /*
4552 * Now grab the chars in the identifier
4553 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004554 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00004556 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01004557 if (kp_ex)
4558 /* Escape the argument properly for an Ex command */
4559 p = vim_strsave_fnameescape(ptr, FALSE);
4560 else
4561 /* Escape the argument properly for a shell command */
4562 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00004563 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004564 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004566 vim_free(buf);
4567 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004569 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02004570 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004571 {
4572 vim_free(buf);
4573 vim_free(p);
4574 return;
4575 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02004576 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004577 STRCAT(buf, p);
4578 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004580 else
4581 {
4582 if (cmdchar == '*')
4583 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
4584 else if (cmdchar == '#')
4585 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01004586 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02004587 {
4588 if (curbuf->b_help)
4589 /* ":help" handles unescaped argument */
4590 aux_ptr = (char_u *)"";
4591 else
4592 aux_ptr = (char_u *)"\\|\"\n[";
4593 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01004594 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004595 aux_ptr = (char_u *)"\\|\"\n*?[";
4596
4597 p = buf + STRLEN(buf);
4598 while (n-- > 0)
4599 {
4600 /* put a backslash before \ and some others */
4601 if (vim_strchr(aux_ptr, *ptr) != NULL)
4602 *p++ = '\\';
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004603 /* When current byte is a part of multibyte character, copy all
4604 * bytes of that character. */
4605 if (has_mbyte)
4606 {
4607 int i;
4608 int len = (*mb_ptr2len)(ptr) - 1;
4609
4610 for (i = 0; i < len && n >= 1; ++i, --n)
4611 *p++ = *ptr++;
4612 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00004613 *p++ = *ptr++;
4614 }
4615 *p = NUL;
4616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
4618 /*
4619 * Execute the command.
4620 */
4621 if (cmdchar == '*' || cmdchar == '#')
4622 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004623 if (!g_cmd && (has_mbyte
4624 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
4625 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02004627
4628 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00004629 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02004631
Bram Moolenaar46539112015-02-17 15:43:57 +01004632 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
4634 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02004635 {
4636 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02004638 g_tag_at_cursor = FALSE;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
4641 vim_free(buf);
4642}
4643
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644/*
4645 * Get visually selected text, within one line only.
4646 * Returns FAIL if more than one line selected.
4647 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004649get_visual_text(
4650 cmdarg_T *cap,
4651 char_u **pp, /* return: start of selected text */
4652 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
4654 if (VIsual_mode != 'V')
4655 unadjust_for_sel();
4656 if (VIsual.lnum != curwin->w_cursor.lnum)
4657 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004658 if (cap != NULL)
4659 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return FAIL;
4661 }
4662 if (VIsual_mode == 'V')
4663 {
4664 *pp = ml_get_curline();
4665 *lenp = (int)STRLEN(*pp);
4666 }
4667 else
4668 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004669 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
4671 *pp = ml_get_pos(&curwin->w_cursor);
4672 *lenp = VIsual.col - curwin->w_cursor.col + 1;
4673 }
4674 else
4675 {
4676 *pp = ml_get_pos(&VIsual);
4677 *lenp = curwin->w_cursor.col - VIsual.col + 1;
4678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (has_mbyte)
4680 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004681 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683 reset_VIsual_and_resel();
4684 return OK;
4685}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
4687/*
4688 * CTRL-T: backwards in tag stack
4689 */
4690 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004691nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 if (!checkclearopq(cap->oap))
4694 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
4695}
4696
4697/*
4698 * Handle scrolling command 'H', 'L' and 'M'.
4699 */
4700 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004701nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702{
4703 int used = 0;
4704 long n;
4705#ifdef FEAT_FOLDING
4706 linenr_T lnum;
4707#endif
4708 int half;
4709
4710 cap->oap->motion_type = MLINE;
4711 setpcmark();
4712
4713 if (cap->cmdchar == 'L')
4714 {
4715 validate_botline(); /* make sure curwin->w_botline is valid */
4716 curwin->w_cursor.lnum = curwin->w_botline - 1;
4717 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
4718 curwin->w_cursor.lnum = 1;
4719 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004720 {
4721#ifdef FEAT_FOLDING
4722 if (hasAnyFolding(curwin))
4723 {
4724 /* Count a fold for one screen line. */
4725 for (n = cap->count1 - 1; n > 0
4726 && curwin->w_cursor.lnum > curwin->w_topline; --n)
4727 {
4728 (void)hasFolding(curwin->w_cursor.lnum,
4729 &curwin->w_cursor.lnum, NULL);
4730 --curwin->w_cursor.lnum;
4731 }
4732 }
4733 else
4734#endif
4735 curwin->w_cursor.lnum -= cap->count1 - 1;
4736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
4738 else
4739 {
4740 if (cap->cmdchar == 'M')
4741 {
4742#ifdef FEAT_DIFF
4743 /* Don't count filler lines above the window. */
4744 used -= diff_check_fill(curwin, curwin->w_topline)
4745 - curwin->w_topfill;
4746#endif
4747 validate_botline(); /* make sure w_empty_rows is valid */
4748 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
4749 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
4750 {
4751#ifdef FEAT_DIFF
4752 /* Count half he number of filler lines to be "below this
4753 * line" and half to be "above the next line". */
4754 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
4755 + n) / 2 >= half)
4756 {
4757 --n;
4758 break;
4759 }
4760#endif
4761 used += plines(curwin->w_topline + n);
4762 if (used >= half)
4763 break;
4764#ifdef FEAT_FOLDING
4765 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
4766 n = lnum - curwin->w_topline;
4767#endif
4768 }
4769 if (n > 0 && used > curwin->w_height)
4770 --n;
4771 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004772 else /* (cap->cmdchar == 'H') */
4773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004775#ifdef FEAT_FOLDING
4776 if (hasAnyFolding(curwin))
4777 {
4778 /* Count a fold for one screen line. */
4779 lnum = curwin->w_topline;
4780 while (n-- > 0 && lnum < curwin->w_botline - 1)
4781 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02004782 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004783 ++lnum;
4784 }
4785 n = lnum - curwin->w_topline;
4786 }
4787#endif
4788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 curwin->w_cursor.lnum = curwin->w_topline + n;
4790 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
4791 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4792 }
4793
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02004794 /* Correct for 'so', except when an operator is pending. */
4795 if (cap->oap->op_type == OP_NOP)
4796 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 beginline(BL_SOL | BL_FIX);
4798}
4799
4800/*
4801 * Cursor right commands.
4802 */
4803 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004804nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
4806 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004807 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004809 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4810 {
4811 /* <C-Right> and <S-Right> move a word or WORD right */
4812 if (mod_mask & MOD_MASK_CTRL)
4813 cap->arg = TRUE;
4814 nv_wordcmd(cap);
4815 return;
4816 }
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 cap->oap->motion_type = MCHAR;
4819 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004820 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004823 * In virtual edit mode, there's no such thing as "past_line", as lines
4824 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 */
4826 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004827 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828
4829 for (n = cap->count1; n > 0; --n)
4830 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004831 if ((!past_line && oneright() == FAIL)
4832 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00004833 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
4835 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004836 * <Space> wraps to next line if 'whichwrap' has 's'.
4837 * 'l' wraps to next line if 'whichwrap' has 'l'.
4838 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 */
4840 if ( ((cap->cmdchar == ' '
4841 && vim_strchr(p_ww, 's') != NULL)
4842 || (cap->cmdchar == 'l'
4843 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004844 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 && vim_strchr(p_ww, '>') != NULL))
4846 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
4847 {
4848 /* When deleting we also count the NL as a character.
4849 * Set cap->oap->inclusive when last char in the line is
4850 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004851 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004853 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 cap->oap->inclusive = TRUE;
4855 else
4856 {
4857 ++curwin->w_cursor.lnum;
4858 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 curwin->w_set_curswant = TRUE;
4861 cap->oap->inclusive = FALSE;
4862 }
4863 continue;
4864 }
4865 if (cap->oap->op_type == OP_NOP)
4866 {
4867 /* Only beep and flush if not moved at all */
4868 if (n == cap->count1)
4869 beep_flush();
4870 }
4871 else
4872 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004873 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 cap->oap->inclusive = TRUE;
4875 }
4876 break;
4877 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004878 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (virtual_active())
4882 oneright();
4883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02004886 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 ++curwin->w_cursor.col;
4889 }
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892#ifdef FEAT_FOLDING
4893 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4894 && cap->oap->op_type == OP_NOP)
4895 foldOpenCursor();
4896#endif
4897}
4898
4899/*
4900 * Cursor left commands.
4901 *
4902 * Returns TRUE when operator end should not be adjusted.
4903 */
4904 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004905nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906{
4907 long n;
4908
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004909 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4910 {
4911 /* <C-Left> and <S-Left> move a word or WORD left */
4912 if (mod_mask & MOD_MASK_CTRL)
4913 cap->arg = 1;
4914 nv_bck_word(cap);
4915 return;
4916 }
4917
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 cap->oap->motion_type = MCHAR;
4919 cap->oap->inclusive = FALSE;
4920 for (n = cap->count1; n > 0; --n)
4921 {
4922 if (oneleft() == FAIL)
4923 {
4924 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
4925 * 'h' wraps to previous line if 'whichwrap' has 'h'.
4926 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
4927 */
4928 if ( (((cap->cmdchar == K_BS
4929 || cap->cmdchar == Ctrl_H)
4930 && vim_strchr(p_ww, 'b') != NULL)
4931 || (cap->cmdchar == 'h'
4932 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00004933 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 && vim_strchr(p_ww, '<') != NULL))
4935 && curwin->w_cursor.lnum > 1)
4936 {
4937 --(curwin->w_cursor.lnum);
4938 coladvance((colnr_T)MAXCOL);
4939 curwin->w_set_curswant = TRUE;
4940
4941 /* When the NL before the first char has to be deleted we
4942 * put the cursor on the NUL after the previous line.
4943 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00004944 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if ( (cap->oap->op_type == OP_DELETE
4946 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004947 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004949 char_u *cp = ml_get_cursor();
4950
4951 if (*cp != NUL)
4952 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004953 if (has_mbyte)
4954 curwin->w_cursor.col += (*mb_ptr2len)(cp);
4955 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01004956 ++curwin->w_cursor.col;
4957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 cap->retval |= CA_NO_ADJ_OP_END;
4959 }
4960 continue;
4961 }
4962 /* Only beep and flush if not moved at all */
4963 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
4964 beep_flush();
4965 break;
4966 }
4967 }
4968#ifdef FEAT_FOLDING
4969 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
4970 && cap->oap->op_type == OP_NOP)
4971 foldOpenCursor();
4972#endif
4973}
4974
4975/*
4976 * Cursor up commands.
4977 * cap->arg is TRUE for "-": Move cursor to first non-blank.
4978 */
4979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004980nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004982 if (mod_mask & MOD_MASK_SHIFT)
4983 {
4984 /* <S-Up> is page up */
4985 cap->arg = BACKWARD;
4986 nv_page(cap);
4987 }
4988 else
4989 {
4990 cap->oap->motion_type = MLINE;
4991 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
4992 clearopbeep(cap->oap);
4993 else if (cap->arg)
4994 beginline(BL_WHITE | BL_FIX);
4995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996}
4997
4998/*
4999 * Cursor down commands.
5000 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5001 */
5002 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02005003nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005005 if (mod_mask & MOD_MASK_SHIFT)
5006 {
5007 /* <S-Down> is page down */
5008 cap->arg = FORWARD;
5009 nv_page(cap);
5010 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005011#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02005012 /* Quickfix window only: view the result under the cursor. */
5013 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
5014 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02005016 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
5018#ifdef FEAT_CMDWIN
5019 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005020 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 cmdwin_result = CAR;
5022 else
5023#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005024#ifdef FEAT_JOB_CHANNEL
5025 /* In a prompt buffer a <CR> in the last line invokes the callback. */
5026 if (bt_prompt(curbuf) && cap->cmdchar == CAR
5027 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
5028 {
5029 invoke_prompt_callback();
5030 if (restart_edit == 0)
5031 restart_edit = 'a';
5032 }
5033 else
5034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
5036 cap->oap->motion_type = MLINE;
5037 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
5038 clearopbeep(cap->oap);
5039 else if (cap->arg)
5040 beginline(BL_WHITE | BL_FIX);
5041 }
5042 }
5043}
5044
5045#ifdef FEAT_SEARCHPATH
5046/*
5047 * Grab the file name under the cursor and edit it.
5048 */
5049 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005050nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
5052 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005053 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005055 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005058 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 return;
5060 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005061 if (curbuf_locked())
5062 {
5063 clearop(cap->oap);
5064 return;
5065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005067 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069 if (ptr != NULL)
5070 {
5071 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005072 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02005073 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02005075 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005076 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02005077 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005078 {
5079 curwin->w_cursor.lnum = lnum;
5080 check_cursor_lnum();
5081 beginline(BL_SOL | BL_FIX);
5082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 vim_free(ptr);
5084 }
5085 else
5086 clearop(cap->oap);
5087}
5088#endif
5089
5090/*
5091 * <End> command: to end of current line or last line.
5092 */
5093 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005094nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005096 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005098 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 nv_goto(cap);
5100 cap->count1 = 1; /* to end of current line */
5101 }
5102 nv_dollar(cap);
5103}
5104
5105/*
5106 * Handle the "$" command.
5107 */
5108 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005109nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
5111 cap->oap->motion_type = MCHAR;
5112 cap->oap->inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 /* In virtual mode when off the edge of a line and an operator
5114 * is pending (whew!) keep the cursor where it is.
5115 * Otherwise, send it to the end of the line. */
5116 if (!virtual_active() || gchar_cursor() != NUL
5117 || cap->oap->op_type == OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 curwin->w_curswant = MAXCOL; /* so we stay at the end */
5119 if (cursor_down((long)(cap->count1 - 1),
5120 cap->oap->op_type == OP_NOP) == FAIL)
5121 clearopbeep(cap->oap);
5122#ifdef FEAT_FOLDING
5123 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5124 foldOpenCursor();
5125#endif
5126}
5127
5128/*
5129 * Implementation of '?' and '/' commands.
5130 * If cap->arg is TRUE don't set PC mark.
5131 */
5132 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005133nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
5135 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02005136 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137
5138 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
5139 {
5140 /* Translate "g??" to "g?g?" */
5141 cap->cmdchar = 'g';
5142 cap->nchar = '?';
5143 nv_operator(cap);
5144 return;
5145 }
5146
Bram Moolenaardda933d2016-09-03 21:04:58 +02005147 /* When using 'incsearch' the cursor may be moved to set a different search
5148 * start position. */
Bram Moolenaare96a2492019-06-25 04:12:16 +02005149 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150
5151 if (cap->searchbuf == NULL)
5152 {
5153 clearop(oap);
5154 return;
5155 }
5156
Bram Moolenaar46539112015-02-17 15:43:57 +01005157 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005158 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02005159 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160}
5161
5162/*
5163 * Handle "N" and "n" commands.
5164 * cap->arg is SEARCH_REV for "N", 0 for "n".
5165 */
5166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005167nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
Bram Moolenaar46539112015-02-17 15:43:57 +01005169 pos_T old = curwin->w_cursor;
5170 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
5171
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005172 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01005173 {
5174 /* Avoid getting stuck on the current cursor position, which can
5175 * happen when an offset is given and the cursor is on the last char
5176 * in the buffer: Repeat with count + 1. */
5177 cap->count1 += 1;
5178 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
5179 cap->count1 -= 1;
5180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181}
5182
5183/*
5184 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
5185 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01005186 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 */
Bram Moolenaar46539112015-02-17 15:43:57 +01005188 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005189normal_search(
5190 cmdarg_T *cap,
5191 int dir,
5192 char_u *pat,
5193 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 int i;
5196
5197 cap->oap->motion_type = MCHAR;
5198 cap->oap->inclusive = FALSE;
5199 cap->oap->use_reg_one = TRUE;
5200 curwin->w_set_curswant = TRUE;
5201
5202 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02005203 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (i == 0)
5205 clearop(cap->oap);
5206 else
5207 {
5208 if (i == 2)
5209 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#ifdef FEAT_FOLDING
5212 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
5213 foldOpenCursor();
5214#endif
5215 }
5216
5217 /* "/$" will put the cursor after the end of the line, may need to
5218 * correct that here */
5219 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01005220 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221}
5222
5223/*
5224 * Character search commands.
5225 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
5226 * ',' and FALSE for ';'.
5227 * cap->nchar is NUL for ',' and ';' (repeat the search)
5228 */
5229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005230nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
5232 int t_cmd;
5233
5234 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
5235 t_cmd = TRUE;
5236 else
5237 t_cmd = FALSE;
5238
5239 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
5241 clearopbeep(cap->oap);
5242 else
5243 {
5244 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 /* Include a Tab for "tx" and for "dfx". */
5246 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
5247 && (t_cmd || cap->oap->op_type != OP_NOP))
5248 {
5249 colnr_T scol, ecol;
5250
5251 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
5252 curwin->w_cursor.coladd = ecol - scol;
5253 }
5254 else
5255 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257#ifdef FEAT_FOLDING
5258 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
5259 foldOpenCursor();
5260#endif
5261 }
5262}
5263
5264/*
5265 * "[" and "]" commands.
5266 * cap->arg is BACKWARD for "[" and FORWARD for "]".
5267 */
5268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005269nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005271 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 pos_T prev_pos;
5273 pos_T *pos = NULL; /* init for GCC */
5274 pos_T old_pos; /* cursor position before command */
5275 int flag;
5276 long n;
5277 int findc;
5278 int c;
5279
5280 cap->oap->motion_type = MCHAR;
5281 cap->oap->inclusive = FALSE;
5282 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005283 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
5285#ifdef FEAT_SEARCHPATH
5286 /*
5287 * "[f" or "]f" : Edit file under the cursor (same as "gf")
5288 */
5289 if (cap->nchar == 'f')
5290 nv_gotofile(cap);
5291 else
5292#endif
5293
5294#ifdef FEAT_FIND_ID
5295 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00005296 * Find the occurrence(s) of the identifier or define under cursor
5297 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 *
5299 * search list jump
5300 * fwd bwd fwd bwd fwd bwd
5301 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
5302 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
5303 */
5304 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005305# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01005307# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01005309# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 cap->nchar) != NULL)
5311 {
5312 char_u *ptr;
5313 int len;
5314
5315 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
5316 clearop(cap->oap);
5317 else
5318 {
5319 find_pattern_in_path(ptr, 0, len, TRUE,
5320 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
5321 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
5322 cap->count1,
5323 isupper(cap->nchar) ? ACTION_SHOW_ALL :
5324 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
5325 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
5326 (linenr_T)MAXLNUM);
5327 curwin->w_set_curswant = TRUE;
5328 }
5329 }
5330 else
5331#endif
5332
5333 /*
5334 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
5335 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
5336 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
5337 * "[m" or "]m" search for prev/next start of (Java) method.
5338 * "[M" or "]M" search for prev/next end of (Java) method.
5339 */
5340 if ( (cap->cmdchar == '['
5341 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
5342 || (cap->cmdchar == ']'
5343 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
5344 {
5345 if (cap->nchar == '*')
5346 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 prev_pos.lnum = 0;
5348 if (cap->nchar == 'm' || cap->nchar == 'M')
5349 {
5350 if (cap->cmdchar == '[')
5351 findc = '{';
5352 else
5353 findc = '}';
5354 n = 9999;
5355 }
5356 else
5357 {
5358 findc = cap->nchar;
5359 n = cap->count1;
5360 }
5361 for ( ; n > 0; --n)
5362 {
5363 if ((pos = findmatchlimit(cap->oap, findc,
5364 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
5365 {
5366 if (new_pos.lnum == 0) /* nothing found */
5367 {
5368 if (cap->nchar != 'm' && cap->nchar != 'M')
5369 clearopbeep(cap->oap);
5370 }
5371 else
5372 pos = &new_pos; /* use last one found */
5373 break;
5374 }
5375 prev_pos = new_pos;
5376 curwin->w_cursor = *pos;
5377 new_pos = *pos;
5378 }
5379 curwin->w_cursor = old_pos;
5380
5381 /*
5382 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
5383 * brought us to the match for "[m" and "]M" when inside a method.
5384 * Try finding the '{' or '}' we want to be at.
5385 * Also repeat for the given count.
5386 */
5387 if (cap->nchar == 'm' || cap->nchar == 'M')
5388 {
5389 /* norm is TRUE for "]M" and "[m" */
5390 int norm = ((findc == '{') == (cap->nchar == 'm'));
5391
5392 n = cap->count1;
5393 /* found a match: we were inside a method */
5394 if (prev_pos.lnum != 0)
5395 {
5396 pos = &prev_pos;
5397 curwin->w_cursor = prev_pos;
5398 if (norm)
5399 --n;
5400 }
5401 else
5402 pos = NULL;
5403 while (n > 0)
5404 {
5405 for (;;)
5406 {
5407 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
5408 {
5409 /* if not found anything, that's an error */
5410 if (pos == NULL)
5411 clearopbeep(cap->oap);
5412 n = 0;
5413 break;
5414 }
5415 c = gchar_cursor();
5416 if (c == '{' || c == '}')
5417 {
5418 /* Must have found end/start of class: use it.
5419 * Or found the place to be at. */
5420 if ((c == findc && norm) || (n == 1 && !norm))
5421 {
5422 new_pos = curwin->w_cursor;
5423 pos = &new_pos;
5424 n = 0;
5425 }
5426 /* if no match found at all, we started outside of the
5427 * class and we're inside now. Just go on. */
5428 else if (new_pos.lnum == 0)
5429 {
5430 new_pos = curwin->w_cursor;
5431 pos = &new_pos;
5432 }
5433 /* found start/end of other method: go to match */
5434 else if ((pos = findmatchlimit(cap->oap, findc,
5435 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
5436 0)) == NULL)
5437 n = 0;
5438 else
5439 curwin->w_cursor = *pos;
5440 break;
5441 }
5442 }
5443 --n;
5444 }
5445 curwin->w_cursor = old_pos;
5446 if (pos == NULL && new_pos.lnum != 0)
5447 clearopbeep(cap->oap);
5448 }
5449 if (pos != NULL)
5450 {
5451 setpcmark();
5452 curwin->w_cursor = *pos;
5453 curwin->w_set_curswant = TRUE;
5454#ifdef FEAT_FOLDING
5455 if ((fdo_flags & FDO_BLOCK) && KeyTyped
5456 && cap->oap->op_type == OP_NOP)
5457 foldOpenCursor();
5458#endif
5459 }
5460 }
5461
5462 /*
5463 * "[[", "[]", "]]" and "][": move to start or end of function
5464 */
5465 else if (cap->nchar == '[' || cap->nchar == ']')
5466 {
5467 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
5468 flag = '{';
5469 else
5470 flag = '}'; /* "][" or "[]" */
5471
5472 curwin->w_set_curswant = TRUE;
5473 /*
5474 * Imitate strange Vi behaviour: When using "]]" with an operator
5475 * we also stop at '}'.
5476 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00005477 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 (cap->oap->op_type != OP_NOP
5479 && cap->arg == FORWARD && flag == '{')))
5480 clearopbeep(cap->oap);
5481 else
5482 {
5483 if (cap->oap->op_type == OP_NOP)
5484 beginline(BL_WHITE | BL_FIX);
5485#ifdef FEAT_FOLDING
5486 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
5487 foldOpenCursor();
5488#endif
5489 }
5490 }
5491
5492 /*
5493 * "[p", "[P", "]P" and "]p": put with indent adjustment
5494 */
5495 else if (cap->nchar == 'p' || cap->nchar == 'P')
5496 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02005497 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
5499
5500 /*
5501 * "['", "[`", "]'" and "]`": jump to next mark
5502 */
5503 else if (cap->nchar == '\'' || cap->nchar == '`')
5504 {
5505 pos = &curwin->w_cursor;
5506 for (n = cap->count1; n > 0; --n)
5507 {
5508 prev_pos = *pos;
5509 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
5510 cap->nchar == '\'');
5511 if (pos == NULL)
5512 break;
5513 }
5514 if (pos == NULL)
5515 pos = &prev_pos;
5516 nv_cursormark(cap, cap->nchar == '\'', pos);
5517 }
5518
5519#ifdef FEAT_MOUSE
5520 /*
5521 * [ or ] followed by a middle mouse click: put selected text with
5522 * indent adjustment. Any other button just does as usual.
5523 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02005524 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 {
5526 (void)do_mouse(cap->oap, cap->nchar,
5527 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
5528 cap->count1, PUT_FIXINDENT);
5529 }
5530#endif /* FEAT_MOUSE */
5531
5532#ifdef FEAT_FOLDING
5533 /*
5534 * "[z" and "]z": move to start or end of open fold.
5535 */
5536 else if (cap->nchar == 'z')
5537 {
5538 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
5539 cap->count1) == FAIL)
5540 clearopbeep(cap->oap);
5541 }
5542#endif
5543
5544#ifdef FEAT_DIFF
5545 /*
5546 * "[c" and "]c": move to next or previous diff-change.
5547 */
5548 else if (cap->nchar == 'c')
5549 {
5550 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
5551 cap->count1) == FAIL)
5552 clearopbeep(cap->oap);
5553 }
5554#endif
5555
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005556#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005557 /*
5558 * "[s", "[S", "]s" and "]S": move to next spell error.
5559 */
5560 else if (cap->nchar == 's' || cap->nchar == 'S')
5561 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005562 setpcmark();
5563 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00005564 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
5565 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005566 {
5567 clearopbeep(cap->oap);
5568 break;
5569 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01005570 else
5571 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005572# ifdef FEAT_FOLDING
5573 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
5574 foldOpenCursor();
5575# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005576 }
5577#endif
5578
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 /* Not a valid cap->nchar. */
5580 else
5581 clearopbeep(cap->oap);
5582}
5583
5584/*
5585 * Handle Normal mode "%" command.
5586 */
5587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005588nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
5590 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02005591#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 linenr_T lnum = curwin->w_cursor.lnum;
5593#endif
5594
5595 cap->oap->inclusive = TRUE;
5596 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
5597 {
5598 if (cap->count0 > 100)
5599 clearopbeep(cap->oap);
5600 else
5601 {
5602 cap->oap->motion_type = MLINE;
5603 setpcmark();
5604 /* Round up, so CTRL-G will give same value. Watch out for a
5605 * large line count, the line number must not go negative! */
5606 if (curbuf->b_ml.ml_line_count > 1000000)
5607 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
5608 / 100L * cap->count0;
5609 else
5610 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
5611 cap->count0 + 99L) / 100L;
5612 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5613 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5614 beginline(BL_SOL | BL_FIX);
5615 }
5616 }
5617 else /* "%" : go to matching paren */
5618 {
5619 cap->oap->motion_type = MCHAR;
5620 cap->oap->use_reg_one = TRUE;
5621 if ((pos = findmatch(cap->oap, NUL)) == NULL)
5622 clearopbeep(cap->oap);
5623 else
5624 {
5625 setpcmark();
5626 curwin->w_cursor = *pos;
5627 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 }
5631 }
5632#ifdef FEAT_FOLDING
5633 if (cap->oap->op_type == OP_NOP
5634 && lnum != curwin->w_cursor.lnum
5635 && (fdo_flags & FDO_PERCENT)
5636 && KeyTyped)
5637 foldOpenCursor();
5638#endif
5639}
5640
5641/*
5642 * Handle "(" and ")" commands.
5643 * cap->arg is BACKWARD for "(" and FORWARD for ")".
5644 */
5645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005646nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648 cap->oap->motion_type = MCHAR;
5649 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00005650 /* The motion used to be inclusive for "(", but that is not what Vi does. */
5651 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 curwin->w_set_curswant = TRUE;
5653
5654 if (findsent(cap->arg, cap->count1) == FAIL)
5655 clearopbeep(cap->oap);
5656 else
5657 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00005658 /* Don't leave the cursor on the NUL past end of line. */
5659 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661#ifdef FEAT_FOLDING
5662 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
5663 foldOpenCursor();
5664#endif
5665 }
5666}
5667
5668/*
5669 * "m" command: Mark a position.
5670 */
5671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005672nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
5674 if (!checkclearop(cap->oap))
5675 {
5676 if (setmark(cap->nchar) == FAIL)
5677 clearopbeep(cap->oap);
5678 }
5679}
5680
5681/*
5682 * "{" and "}" commands.
5683 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
5684 */
5685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005686nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 cap->oap->motion_type = MCHAR;
5689 cap->oap->inclusive = FALSE;
5690 cap->oap->use_reg_one = TRUE;
5691 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00005692 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 clearopbeep(cap->oap);
5694 else
5695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697#ifdef FEAT_FOLDING
5698 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
5699 foldOpenCursor();
5700#endif
5701 }
5702}
5703
5704/*
5705 * "u" command: Undo or make lower case.
5706 */
5707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005708nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005710 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
5712 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
5713 cap->cmdchar = 'g';
5714 cap->nchar = 'u';
5715 nv_operator(cap);
5716 }
5717 else
5718 nv_kundo(cap);
5719}
5720
5721/*
5722 * <Undo> command.
5723 */
5724 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005725nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
5727 if (!checkclearopq(cap->oap))
5728 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02005729#ifdef FEAT_JOB_CHANNEL
5730 if (bt_prompt(curbuf))
5731 {
5732 clearopbeep(cap->oap);
5733 return;
5734 }
5735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 u_undo((int)cap->count1);
5737 curwin->w_set_curswant = TRUE;
5738 }
5739}
5740
5741/*
5742 * Handle the "r" command.
5743 */
5744 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005745nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
5747 char_u *ptr;
5748 int had_ctrl_v;
5749 long n;
5750
5751 if (checkclearop(cap->oap))
5752 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02005753#ifdef FEAT_JOB_CHANNEL
5754 if (bt_prompt(curbuf) && !prompt_curpos_editable())
5755 {
5756 clearopbeep(cap->oap);
5757 return;
5758 }
5759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 /* get another character */
5762 if (cap->nchar == Ctrl_V)
5763 {
5764 had_ctrl_v = Ctrl_V;
5765 cap->nchar = get_literal();
5766 /* Don't redo a multibyte character with CTRL-V. */
5767 if (cap->nchar > DEL)
5768 had_ctrl_v = NUL;
5769 }
5770 else
5771 had_ctrl_v = NUL;
5772
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005773 /* Abort if the character is a special key. */
5774 if (IS_SPECIAL(cap->nchar))
5775 {
5776 clearopbeep(cap->oap);
5777 return;
5778 }
5779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 /* Visual mode "r" */
5781 if (VIsual_active)
5782 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00005783 if (got_int)
5784 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01005785 if (had_ctrl_v)
5786 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01005787 /* Use a special (negative) number to make a difference between a
5788 * literal CR or NL and a line break. */
5789 if (cap->nchar == CAR)
5790 cap->nchar = REPLACE_CR_NCHAR;
5791 else if (cap->nchar == NL)
5792 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01005793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 nv_operator(cap);
5795 return;
5796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 /* Break tabs, etc. */
5799 if (virtual_active())
5800 {
5801 if (u_save_cursor() == FAIL)
5802 return;
5803 if (gchar_cursor() == NUL)
5804 {
5805 /* Add extra space and put the cursor on the first one. */
5806 coladvance_force((colnr_T)(getviscol() + cap->count1));
5807 curwin->w_cursor.col -= cap->count1;
5808 }
5809 else if (gchar_cursor() == TAB)
5810 coladvance_force(getviscol());
5811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005813 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00005815 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005816 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 {
5818 clearopbeep(cap->oap);
5819 return;
5820 }
5821
5822 /*
5823 * Replacing with a TAB is done by edit() when it is complicated because
5824 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
5825 * Other characters are done below to avoid problems with things like
5826 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
5827 */
5828 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
5829 {
5830 stuffnumReadbuff(cap->count1);
5831 stuffcharReadbuff('R');
5832 stuffcharReadbuff('\t');
5833 stuffcharReadbuff(ESC);
5834 return;
5835 }
5836
5837 /* save line for undo */
5838 if (u_save_cursor() == FAIL)
5839 return;
5840
5841 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
5842 {
5843 /*
5844 * Replace character(s) by a single newline.
5845 * Strange vi behaviour: Only one newline is inserted.
5846 * Delete the characters here.
5847 * Insert the newline with an insert command, takes care of
5848 * autoindent. The insert command depends on being on the last
5849 * character of a line or not.
5850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 (void)del_chars(cap->count1, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 stuffcharReadbuff('\r');
5853 stuffcharReadbuff(ESC);
5854
5855 /* Give 'r' to edit(), to get the redo command right. */
5856 invoke_edit(cap, TRUE, 'r', FALSE);
5857 }
5858 else
5859 {
5860 prep_redo(cap->oap->regname, cap->count1,
5861 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
5862
5863 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (has_mbyte)
5865 {
5866 int old_State = State;
5867
5868 if (cap->ncharC1 != 0)
5869 AppendCharToRedobuff(cap->ncharC1);
5870 if (cap->ncharC2 != 0)
5871 AppendCharToRedobuff(cap->ncharC2);
5872
5873 /* This is slow, but it handles replacing a single-byte with a
5874 * multi-byte and the other way around. Also handles adding
5875 * composing characters for utf-8. */
5876 for (n = cap->count1; n > 0; --n)
5877 {
5878 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02005879 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5880 {
5881 int c = ins_copychar(curwin->w_cursor.lnum
5882 + (cap->nchar == Ctrl_Y ? -1 : 1));
5883 if (c != NUL)
5884 ins_char(c);
5885 else
5886 /* will be decremented further down */
5887 ++curwin->w_cursor.col;
5888 }
5889 else
5890 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 State = old_State;
5892 if (cap->ncharC1 != 0)
5893 ins_char(cap->ncharC1);
5894 if (cap->ncharC2 != 0)
5895 ins_char(cap->ncharC2);
5896 }
5897 }
5898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 /*
5901 * Replace the characters within one line.
5902 */
5903 for (n = cap->count1; n > 0; --n)
5904 {
5905 /*
5906 * Get ptr again, because u_save and/or showmatch() will have
5907 * released the line. At the same time we let know that the
5908 * line will be changed.
5909 */
5910 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02005911 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
5912 {
5913 int c = ins_copychar(curwin->w_cursor.lnum
5914 + (cap->nchar == Ctrl_Y ? -1 : 1));
5915 if (c != NUL)
5916 ptr[curwin->w_cursor.col] = c;
5917 }
5918 else
5919 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 if (p_sm && msg_silent == 0)
5921 showmatch(cap->nchar);
5922 ++curwin->w_cursor.col;
5923 }
5924#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005925 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005927 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
Bram Moolenaar009b2592004-10-24 19:18:58 +00005929 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005930 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00005932 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 }
5934#endif
5935
5936 /* mark the buffer as changed and prepare for displaying */
5937 changed_bytes(curwin->w_cursor.lnum,
5938 (colnr_T)(curwin->w_cursor.col - cap->count1));
5939 }
5940 --curwin->w_cursor.col; /* cursor on the last replaced char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 /* if the character on the left of the current cursor is a multi-byte
5942 * character, move two characters left */
5943 if (has_mbyte)
5944 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 curbuf->b_op_end = curwin->w_cursor;
5946 curwin->w_set_curswant = TRUE;
5947 set_last_insert(cap->nchar);
5948 }
5949}
5950
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951/*
5952 * 'o': Exchange start and end of Visual area.
5953 * 'O': same, but in block mode exchange left and right corners.
5954 */
5955 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005956v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957{
5958 pos_T old_cursor;
5959 colnr_T left, right;
5960
5961 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
5962 {
5963 old_cursor = curwin->w_cursor;
5964 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
5965 curwin->w_cursor.lnum = VIsual.lnum;
5966 coladvance(left);
5967 VIsual = curwin->w_cursor;
5968
5969 curwin->w_cursor.lnum = old_cursor.lnum;
5970 curwin->w_curswant = right;
5971 /* 'selection "exclusive" and cursor at right-bottom corner: move it
5972 * right one column */
5973 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
5974 ++curwin->w_curswant;
5975 coladvance(curwin->w_curswant);
5976 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005978 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
5980 curwin->w_cursor.lnum = VIsual.lnum;
5981 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
5982 ++right;
5983 coladvance(right);
5984 VIsual = curwin->w_cursor;
5985
5986 curwin->w_cursor.lnum = old_cursor.lnum;
5987 coladvance(left);
5988 curwin->w_curswant = left;
5989 }
5990 }
5991 else
5992 {
5993 old_cursor = curwin->w_cursor;
5994 curwin->w_cursor = VIsual;
5995 VIsual = old_cursor;
5996 curwin->w_set_curswant = TRUE;
5997 }
5998}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
6000/*
6001 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6002 */
6003 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006004nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 if (VIsual_active) /* "R" is replace lines */
6007 {
6008 cap->cmdchar = 'c';
6009 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01006010 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 VIsual_mode = 'V';
6012 nv_operator(cap);
6013 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006014 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 {
6016 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006017 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 else
6019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 if (virtual_active())
6021 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
6023 }
6024 }
6025}
6026
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027/*
6028 * "gr".
6029 */
6030 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006031nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 if (VIsual_active)
6034 {
6035 cap->cmdchar = 'r';
6036 cap->nchar = cap->extra_char;
6037 nv_replace(cap); /* Do same as "r" in Visual mode for now */
6038 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006039 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 {
6041 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006042 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 else
6044 {
6045 if (cap->extra_char == Ctrl_V) /* get another character */
6046 cap->extra_char = get_literal();
6047 stuffcharReadbuff(cap->extra_char);
6048 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 if (virtual_active())
6050 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 invoke_edit(cap, TRUE, 'v', FALSE);
6052 }
6053 }
6054}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
6056/*
6057 * Swap case for "~" command, when it does not work like an operator.
6058 */
6059 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006060n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
6062 long n;
6063 pos_T startpos;
6064 int did_change = 0;
6065#ifdef FEAT_NETBEANS_INTG
6066 pos_T pos;
6067 char_u *ptr;
6068 int count;
6069#endif
6070
6071 if (checkclearopq(cap->oap))
6072 return;
6073
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006074 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 {
6076 clearopbeep(cap->oap);
6077 return;
6078 }
6079
6080 prep_redo_cmd(cap);
6081
6082 if (u_save_cursor() == FAIL)
6083 return;
6084
6085 startpos = curwin->w_cursor;
6086#ifdef FEAT_NETBEANS_INTG
6087 pos = startpos;
6088#endif
6089 for (n = cap->count1; n > 0; --n)
6090 {
6091 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
6092 inc_cursor();
6093 if (gchar_cursor() == NUL)
6094 {
6095 if (vim_strchr(p_ww, '~') != NULL
6096 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6097 {
6098#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006099 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
6101 if (did_change)
6102 {
6103 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006104 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006105 netbeans_removed(curbuf, pos.lnum, pos.col,
6106 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00006108 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 }
6110 pos.col = 0;
6111 pos.lnum++;
6112 }
6113#endif
6114 ++curwin->w_cursor.lnum;
6115 curwin->w_cursor.col = 0;
6116 if (n > 1)
6117 {
6118 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
6119 break;
6120 u_clearline();
6121 }
6122 }
6123 else
6124 break;
6125 }
6126 }
6127#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006128 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {
6130 ptr = ml_get(pos.lnum);
6131 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00006132 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
6133 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135#endif
6136
6137
6138 check_cursor();
6139 curwin->w_set_curswant = TRUE;
6140 if (did_change)
6141 {
6142 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
6143 0L);
6144 curbuf->b_op_start = startpos;
6145 curbuf->b_op_end = curwin->w_cursor;
6146 if (curbuf->b_op_end.col > 0)
6147 --curbuf->b_op_end.col;
6148 }
6149}
6150
6151/*
6152 * Move cursor to mark.
6153 */
6154 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006155nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
6157 if (check_mark(pos) == FAIL)
6158 clearop(cap->oap);
6159 else
6160 {
6161 if (cap->cmdchar == '\''
6162 || cap->cmdchar == '`'
6163 || cap->cmdchar == '['
6164 || cap->cmdchar == ']')
6165 setpcmark();
6166 curwin->w_cursor = *pos;
6167 if (flag)
6168 beginline(BL_WHITE | BL_FIX);
6169 else
6170 check_cursor();
6171 }
6172 cap->oap->motion_type = flag ? MLINE : MCHAR;
6173 if (cap->cmdchar == '`')
6174 cap->oap->use_reg_one = TRUE;
6175 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
6176 curwin->w_set_curswant = TRUE;
6177}
6178
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179/*
6180 * Handle commands that are operators in Visual mode.
6181 */
6182 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006183v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
6185 static char_u trans[] = "YyDdCcxdXdAAIIrr";
6186
6187 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006188 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 if (isupper(cap->cmdchar))
6190 {
6191 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01006192 {
6193 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01006195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
6197 curwin->w_curswant = MAXCOL;
6198 }
6199 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
6200 nv_operator(cap);
6201}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
6203/*
6204 * "s" and "S" commands.
6205 */
6206 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006207nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
Bram Moolenaard96ff162018-02-18 22:13:29 +01006209#ifdef FEAT_TERMINAL
6210 /* When showing output of term_dumpdiff() swap the top and botom. */
6211 if (term_swap_diff() == OK)
6212 return;
6213#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006214#ifdef FEAT_JOB_CHANNEL
6215 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6216 {
6217 clearopbeep(cap->oap);
6218 return;
6219 }
6220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
6222 {
6223 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01006224 {
6225 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01006227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 cap->cmdchar = 'c';
6229 nv_operator(cap);
6230 }
6231 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 nv_optrans(cap);
6233}
6234
6235/*
6236 * Abbreviated commands.
6237 */
6238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006239nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
6242 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
6243
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 /* in Visual mode these commands are operators */
6245 if (VIsual_active)
6246 v_visop(cap);
6247 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 nv_optrans(cap);
6249}
6250
6251/*
6252 * Translate a command into another command.
6253 */
6254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006255nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
6258 (char_u *)"d$", (char_u *)"c$",
6259 (char_u *)"cl", (char_u *)"cc",
6260 (char_u *)"yy", (char_u *)":s\r"};
6261 static char_u *str = (char_u *)"xXDCsSY&";
6262
6263 if (!checkclearopq(cap->oap))
6264 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02006265 // In Vi "2D" doesn't delete the next line. Can't translate it
6266 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006267 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
6268 {
6269 cap->oap->start = curwin->w_cursor;
6270 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00006271#ifdef FEAT_EVAL
6272 set_op_var(OP_DELETE);
6273#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006274 cap->count1 = 1;
6275 nv_dollar(cap);
6276 finish_op = TRUE;
6277 ResetRedobuff();
6278 AppendCharToRedobuff('D');
6279 }
6280 else
6281 {
6282 if (cap->count0)
6283 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02006284 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 }
6287 cap->opcount = 0;
6288}
6289
6290/*
6291 * "'" and "`" commands. Also for "g'" and "g`".
6292 * cap->arg is TRUE for "'" and "g'".
6293 */
6294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006295nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296{
6297 pos_T *pos;
6298 int c;
6299#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01006300 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6302#endif
6303
6304 if (cap->cmdchar == 'g')
6305 c = cap->extra_char;
6306 else
6307 c = cap->nchar;
6308 pos = getmark(c, (cap->oap->op_type == OP_NOP));
6309 if (pos == (pos_T *)-1) /* jumped to other file */
6310 {
6311 if (cap->arg)
6312 {
6313 check_cursor_lnum();
6314 beginline(BL_WHITE | BL_FIX);
6315 }
6316 else
6317 check_cursor();
6318 }
6319 else
6320 nv_cursormark(cap, cap->arg, pos);
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 /* May need to clear the coladd that a mark includes. */
6323 if (!virtual_active())
6324 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02006325 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326#ifdef FEAT_FOLDING
6327 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01006328 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006329 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 && (fdo_flags & FDO_MARK)
6331 && old_KeyTyped)
6332 foldOpenCursor();
6333#endif
6334}
6335
6336/*
6337 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
6338 */
6339 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006340nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342#ifdef FEAT_JUMPLIST
6343 pos_T *pos;
6344# ifdef FEAT_FOLDING
6345 linenr_T lnum = curwin->w_cursor.lnum;
6346 int old_KeyTyped = KeyTyped; /* getting file may reset it */
6347# endif
6348
6349 if (!checkclearopq(cap->oap))
6350 {
6351 if (cap->cmdchar == 'g')
6352 pos = movechangelist((int)cap->count1);
6353 else
6354 pos = movemark((int)cap->count1);
6355 if (pos == (pos_T *)-1) /* jump to other file */
6356 {
6357 curwin->w_set_curswant = TRUE;
6358 check_cursor();
6359 }
6360 else if (pos != NULL) /* can jump */
6361 nv_cursormark(cap, FALSE, pos);
6362 else if (cap->cmdchar == 'g')
6363 {
6364 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006365 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006367 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006369 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 }
6371 else
6372 clearopbeep(cap->oap);
6373# ifdef FEAT_FOLDING
6374 if (cap->oap->op_type == OP_NOP
6375 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
6376 && (fdo_flags & FDO_MARK)
6377 && old_KeyTyped)
6378 foldOpenCursor();
6379# endif
6380 }
6381#else
6382 clearopbeep(cap->oap);
6383#endif
6384}
6385
6386/*
6387 * Handle '"' command.
6388 */
6389 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006390nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391{
6392 if (checkclearop(cap->oap))
6393 return;
6394#ifdef FEAT_EVAL
6395 if (cap->nchar == '=')
6396 cap->nchar = get_expr_register();
6397#endif
6398 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
6399 {
6400 cap->oap->regname = cap->nchar;
6401 cap->opcount = cap->count0; /* remember count before '"' */
6402#ifdef FEAT_EVAL
6403 set_reg_var(cap->oap->regname);
6404#endif
6405 }
6406 else
6407 clearopbeep(cap->oap);
6408}
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410/*
6411 * Handle "v", "V" and "CTRL-V" commands.
6412 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
6413 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006414 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 */
6416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006417nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006419 if (cap->cmdchar == Ctrl_Q)
6420 cap->cmdchar = Ctrl_V;
6421
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
6423 * characterwise, linewise, or blockwise. */
6424 if (cap->oap->op_type != OP_NOP)
6425 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01006426 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 finish_op = FALSE; /* operator doesn't finish now but later */
6428 return;
6429 }
6430
6431 VIsual_select = cap->arg;
6432 if (VIsual_active) /* change Visual mode */
6433 {
6434 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
6435 end_visual_mode();
6436 else /* toggle char/block mode */
6437 { /* or char/line mode */
6438 VIsual_mode = cap->cmdchar;
6439 showmode();
6440 }
6441 redraw_curbuf_later(INVERTED); /* update the inversion */
6442 }
6443 else /* start Visual mode */
6444 {
6445 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02006446 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02006448 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 VIsual = curwin->w_cursor;
6450
6451 VIsual_active = TRUE;
6452 VIsual_reselect = TRUE;
6453 if (!cap->arg)
6454 /* start Select mode when 'selectmode' contains "cmd" */
6455 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 setmouse();
Bram Moolenaar09df3122006-01-23 22:23:09 +00006457 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 redraw_cmdline = TRUE; /* show visual mode later */
6459 /*
6460 * For V and ^V, we multiply the number of lines even if there
6461 * was only one -- webb
6462 */
6463 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
6464 {
6465 curwin->w_cursor.lnum +=
6466 resel_VIsual_line_count * cap->count0 - 1;
6467 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6468 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6469 }
6470 VIsual_mode = resel_VIsual_mode;
6471 if (VIsual_mode == 'v')
6472 {
6473 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02006474 {
6475 validate_virtcol();
6476 curwin->w_curswant = curwin->w_virtcol
6477 + resel_VIsual_vcol * cap->count0 - 1;
6478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02006480 curwin->w_curswant = resel_VIsual_vcol;
6481 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02006483 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 {
6485 curwin->w_curswant = MAXCOL;
6486 coladvance((colnr_T)MAXCOL);
6487 }
6488 else if (VIsual_mode == Ctrl_V)
6489 {
6490 validate_virtcol();
6491 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02006492 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 coladvance(curwin->w_curswant);
6494 }
6495 else
6496 curwin->w_set_curswant = TRUE;
6497 redraw_curbuf_later(INVERTED); /* show the inversion */
6498 }
6499 else
6500 {
6501 if (!cap->arg)
6502 /* start Select mode when 'selectmode' contains "cmd" */
6503 may_start_select('c');
6504 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02006505 if (VIsual_mode != 'V' && *p_sel == 'e')
6506 ++cap->count1; /* include one more char */
6507 if (cap->count0 > 0 && --cap->count1 > 0)
6508 {
6509 /* With a count select that many characters or lines. */
6510 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
6511 nv_right(cap);
6512 else if (VIsual_mode == 'V')
6513 nv_down(cap);
6514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 }
6516 }
6517}
6518
6519/*
6520 * Start selection for Shift-movement keys.
6521 */
6522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006523start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 /* if 'selectmode' contains "key", start Select mode */
6526 may_start_select('k');
6527 n_start_visual_mode('v');
6528}
6529
6530/*
6531 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
6532 */
6533 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006534may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 VIsual_select = (stuff_empty() && typebuf_typed()
6537 && (vim_strchr(p_slm, c) != NULL));
6538}
6539
6540/*
6541 * Start Visual mode "c".
6542 * Should set VIsual_select before calling this.
6543 */
6544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006545n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006547#ifdef FEAT_CONCEAL
6548 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02006549 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006550#endif
6551
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 VIsual_mode = c;
6553 VIsual_active = TRUE;
6554 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006555
6556 // Corner case: the 0 position in a tab may change when going into
6557 // virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02006559 {
6560 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02006562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 VIsual = curwin->w_cursor;
6564
6565#ifdef FEAT_FOLDING
6566 foldAdjustVisual();
6567#endif
6568
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 setmouse();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006570#ifdef FEAT_CONCEAL
6571 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02006572 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02006573#endif
6574
Bram Moolenaar09df3122006-01-23 22:23:09 +00006575 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 redraw_cmdline = TRUE; /* show visual mode later */
6577#ifdef FEAT_CLIPBOARD
6578 /* Make sure the clipboard gets updated. Needed because start and
6579 * end may still be the same, and the selection needs to be owned */
6580 clip_star.vmode = NUL;
6581#endif
6582
6583 /* Only need to redraw this line, unless still need to redraw an old
6584 * Visual area (when 'lazyredraw' is set). */
6585 if (curwin->w_redr_type < INVERTED)
6586 {
6587 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
6588 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
6589 }
6590}
6591
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593/*
6594 * CTRL-W: Window commands
6595 */
6596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006597nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
Bram Moolenaar938783d2017-07-16 20:13:26 +02006599 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01006600 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02006601 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01006602 cap->cmdchar = ':';
6603 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02006604 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01006605 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02006606 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608}
6609
6610/*
6611 * CTRL-Z: Suspend
6612 */
6613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006614nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 if (VIsual_active)
6618 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 do_cmdline_cmd((char_u *)"st");
6620}
6621
6622/*
6623 * Commands starting with "g".
6624 */
6625 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006626nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 int i;
6631 int flag = FALSE;
6632
6633 switch (cap->nchar)
6634 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006635 case Ctrl_A:
6636 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637#ifdef MEM_PROFILE
6638 /*
6639 * "g^A": dump log of used memory.
6640 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006641 if (!VIsual_active && cap->nchar == Ctrl_A)
6642 vim_mem_profile_dump();
6643 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006645 /*
6646 * "g^A/g^X": sequentially increment visually selected region
6647 */
6648 if (VIsual_active)
6649 {
6650 cap->arg = TRUE;
6651 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01006652 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02006653 nv_addsub(cap);
6654 }
6655 else
6656 clearopbeep(oap);
6657 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 /*
6660 * "gR": Enter virtual replace mode.
6661 */
6662 case 'R':
6663 cap->arg = TRUE;
6664 nv_Replace(cap);
6665 break;
6666
6667 case 'r':
6668 nv_vreplace(cap);
6669 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670
6671 case '&':
6672 do_cmdline_cmd((char_u *)"%s//~/&");
6673 break;
6674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 /*
6676 * "gv": Reselect the previous Visual area. If Visual already active,
6677 * exchange previous and current Visual area.
6678 */
6679 case 'v':
6680 if (checkclearop(oap))
6681 break;
6682
Bram Moolenaara226a6d2006-02-26 23:59:20 +00006683 if ( curbuf->b_visual.vi_start.lnum == 0
6684 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
6685 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 beep_flush();
6687 else
6688 {
6689 /* set w_cursor to the start of the Visual area, tpos to the end */
6690 if (VIsual_active)
6691 {
6692 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00006693 VIsual_mode = curbuf->b_visual.vi_mode;
6694 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695# ifdef FEAT_EVAL
6696 curbuf->b_visual_mode_eval = i;
6697# endif
6698 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00006699 curwin->w_curswant = curbuf->b_visual.vi_curswant;
6700 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
Bram Moolenaara226a6d2006-02-26 23:59:20 +00006702 tpos = curbuf->b_visual.vi_end;
6703 curbuf->b_visual.vi_end = curwin->w_cursor;
6704 curwin->w_cursor = curbuf->b_visual.vi_start;
6705 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 }
6707 else
6708 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00006709 VIsual_mode = curbuf->b_visual.vi_mode;
6710 curwin->w_curswant = curbuf->b_visual.vi_curswant;
6711 tpos = curbuf->b_visual.vi_end;
6712 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 }
6714
6715 VIsual_active = TRUE;
6716 VIsual_reselect = TRUE;
6717
6718 /* Set Visual to the start and w_cursor to the end of the Visual
6719 * area. Make sure they are on an existing character. */
6720 check_cursor();
6721 VIsual = curwin->w_cursor;
6722 curwin->w_cursor = tpos;
6723 check_cursor();
6724 update_topline();
6725 /*
6726 * When called from normal "g" command: start Select mode when
6727 * 'selectmode' contains "cmd". When called for K_SELECT, always
6728 * start Select mode.
6729 */
6730 if (cap->arg)
6731 VIsual_select = TRUE;
6732 else
6733 may_start_select('c');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 setmouse();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735#ifdef FEAT_CLIPBOARD
6736 /* Make sure the clipboard gets updated. Needed because start and
6737 * end are still the same, and the selection needs to be owned */
6738 clip_star.vmode = NUL;
6739#endif
6740 redraw_curbuf_later(INVERTED);
6741 showmode();
6742 }
6743 break;
6744 /*
6745 * "gV": Don't reselect the previous Visual area after a Select mode
6746 * mapping of menu.
6747 */
6748 case 'V':
6749 VIsual_reselect = FALSE;
6750 break;
6751
6752 /*
6753 * "gh": start Select mode.
6754 * "gH": start Select line mode.
6755 * "g^H": start Select block mode.
6756 */
6757 case K_BS:
6758 cap->nchar = Ctrl_H;
6759 /* FALLTHROUGH */
6760 case 'h':
6761 case 'H':
6762 case Ctrl_H:
6763# ifdef EBCDIC
6764 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
6765 if (cap->nchar == Ctrl_H)
6766 cap->cmdchar = Ctrl_V;
6767 else
6768# endif
6769 cap->cmdchar = cap->nchar + ('v' - 'h');
6770 cap->arg = TRUE;
6771 nv_visual(cap);
6772 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02006773
6774 /* "gn", "gN" visually select next/previous search match
6775 * "gn" selects next match
6776 * "gN" selects previous match
6777 */
6778 case 'N':
6779 case 'n':
6780 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02006781 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02006782 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783
6784 /*
6785 * "gj" and "gk" two new funny movement keys -- up and down
6786 * movement based on *screen* line rather than *file* line.
6787 */
6788 case 'j':
6789 case K_DOWN:
6790 /* with 'nowrap' it works just like the normal "j" command; also when
6791 * in a closed fold */
6792 if (!curwin->w_p_wrap
6793#ifdef FEAT_FOLDING
6794 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
6795#endif
6796 )
6797 {
6798 oap->motion_type = MLINE;
6799 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
6800 }
6801 else
6802 i = nv_screengo(oap, FORWARD, cap->count1);
6803 if (i == FAIL)
6804 clearopbeep(oap);
6805 break;
6806
6807 case 'k':
6808 case K_UP:
6809 /* with 'nowrap' it works just like the normal "k" command; also when
6810 * in a closed fold */
6811 if (!curwin->w_p_wrap
6812#ifdef FEAT_FOLDING
6813 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
6814#endif
6815 )
6816 {
6817 oap->motion_type = MLINE;
6818 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
6819 }
6820 else
6821 i = nv_screengo(oap, BACKWARD, cap->count1);
6822 if (i == FAIL)
6823 clearopbeep(oap);
6824 break;
6825
6826 /*
6827 * "gJ": join two lines without inserting a space.
6828 */
6829 case 'J':
6830 nv_join(cap);
6831 break;
6832
6833 /*
6834 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
6835 * "gm": middle of "g0" and "g$".
6836 */
6837 case '^':
6838 flag = TRUE;
6839 /* FALLTHROUGH */
6840
6841 case '0':
6842 case 'm':
6843 case K_HOME:
6844 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 oap->motion_type = MCHAR;
6846 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006847 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006849 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 int width2 = width1 + curwin_col_off2();
6851
6852 validate_virtcol();
6853 i = 0;
6854 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
6855 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
6856 }
6857 else
6858 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02006859 /* Go to the middle of the screen line. When 'number' or
6860 * 'relativenumber' is on and lines are wrapping the middle can be more
6861 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02006863 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 + ((curwin->w_p_wrap && i > 0)
6865 ? curwin_col_off2() : 0)) / 2;
6866 coladvance((colnr_T)i);
6867 if (flag)
6868 {
6869 do
6870 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01006871 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01006872 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 }
6874 curwin->w_set_curswant = TRUE;
6875 break;
6876
6877 case '_':
6878 /* "g_": to the last non-blank character in the line or <count> lines
6879 * downward. */
6880 cap->oap->motion_type = MCHAR;
6881 cap->oap->inclusive = TRUE;
6882 curwin->w_curswant = MAXCOL;
6883 if (cursor_down((long)(cap->count1 - 1),
6884 cap->oap->op_type == OP_NOP) == FAIL)
6885 clearopbeep(cap->oap);
6886 else
6887 {
6888 char_u *ptr = ml_get_curline();
6889
6890 /* In Visual mode we may end up after the line. */
6891 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
6892 --curwin->w_cursor.col;
6893
6894 /* Decrease the cursor column until it's on a non-blank. */
6895 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01006896 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 --curwin->w_cursor.col;
6898 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01006899 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 }
6901 break;
6902
6903 case '$':
6904 case K_END:
6905 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 {
6907 int col_off = curwin_col_off();
6908
6909 oap->motion_type = MCHAR;
6910 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006911 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {
6913 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6914 if (cap->count1 == 1)
6915 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006916 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 int width2 = width1 + curwin_col_off2();
6918
6919 validate_virtcol();
6920 i = width1 - 1;
6921 if (curwin->w_virtcol >= (colnr_T)width1)
6922 i += ((curwin->w_virtcol - width1) / width2 + 1)
6923 * width2;
6924 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02006925
6926 /* Make sure we stick in this column. */
6927 validate_virtcol();
6928 curwin->w_curswant = curwin->w_virtcol;
6929 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
6931 {
6932 /*
6933 * Check for landing on a character that got split at
6934 * the end of the line. We do not want to advance to
6935 * the next screen line.
6936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 if (curwin->w_virtcol > (colnr_T)i)
6938 --curwin->w_cursor.col;
6939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
6942 clearopbeep(oap);
6943 }
6944 else
6945 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02006946 if (cap->count1 > 1)
6947 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02006948 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02006949
Bram Moolenaar02631462017-09-22 15:20:32 +02006950 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006952
Bram Moolenaard5c82342019-07-27 18:44:57 +02006953 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00006954 validate_virtcol();
6955 curwin->w_curswant = curwin->w_virtcol;
6956 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958 }
6959 break;
6960
6961 /*
6962 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
6963 */
6964 case '*':
6965 case '#':
6966#if POUND != '#'
6967 case POUND: /* pound sign (sometimes equal to '#') */
6968#endif
6969 case Ctrl_RSB: /* :tag or :tselect for current identifier */
6970 case ']': /* :tselect for current identifier */
6971 nv_ident(cap);
6972 break;
6973
6974 /*
6975 * ge and gE: go back to end of word
6976 */
6977 case 'e':
6978 case 'E':
6979 oap->motion_type = MCHAR;
6980 curwin->w_set_curswant = TRUE;
6981 oap->inclusive = TRUE;
6982 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
6983 clearopbeep(oap);
6984 break;
6985
6986 /*
6987 * "g CTRL-G": display info about cursor position
6988 */
6989 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01006990 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 break;
6992
6993 /*
6994 * "gi": start Insert at the last position.
6995 */
6996 case 'i':
6997 if (curbuf->b_last_insert.lnum != 0)
6998 {
6999 curwin->w_cursor = curbuf->b_last_insert;
7000 check_cursor_lnum();
7001 i = (int)STRLEN(ml_get_curline());
7002 if (curwin->w_cursor.col > (colnr_T)i)
7003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (virtual_active())
7005 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 curwin->w_cursor.col = i;
7007 }
7008 }
7009 cap->cmdchar = 'i';
7010 nv_edit(cap);
7011 break;
7012
7013 /*
7014 * "gI": Start insert in column 1.
7015 */
7016 case 'I':
7017 beginline(0);
7018 if (!checkclearopq(oap))
7019 invoke_edit(cap, FALSE, 'g', FALSE);
7020 break;
7021
7022#ifdef FEAT_SEARCHPATH
7023 /*
7024 * "gf": goto file, edit file under cursor
7025 * "]f" and "[f": can also be used.
7026 */
7027 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00007028 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 nv_gotofile(cap);
7030 break;
7031#endif
7032
7033 /* "g'm" and "g`m": jump to mark without setting pcmark */
7034 case '\'':
7035 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007036 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 case '`':
7038 nv_gomark(cap);
7039 break;
7040
7041 /*
7042 * "gs": Goto sleep.
7043 */
7044 case 's':
7045 do_sleep(cap->count1 * 1000L);
7046 break;
7047
7048 /*
7049 * "ga": Display the ascii value of the character under the
7050 * cursor. It is displayed in decimal, hex, and octal. -- webb
7051 */
7052 case 'a':
7053 do_ascii(NULL);
7054 break;
7055
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 /*
7057 * "g8": Display the bytes used for the UTF-8 character under the
7058 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00007059 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 */
7061 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00007062 if (cap->count0 == 8)
7063 utf_find_illegal();
7064 else
7065 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaar60402d62017-04-20 18:54:50 +02007068 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007069 case '<':
7070 show_sb_text();
7071 break;
7072
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 /*
7074 * "gg": Goto the first line in file. With a count it goes to
7075 * that line number like for "G". -- webb
7076 */
7077 case 'g':
7078 cap->arg = FALSE;
7079 nv_goto(cap);
7080 break;
7081
7082 /*
7083 * Two-character operators:
7084 * "gq" Format text
7085 * "gw" Format text and keep cursor position
7086 * "g~" Toggle the case of the text.
7087 * "gu" Change text to lower case.
7088 * "gU" Change text to upper case.
7089 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007090 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 */
7092 case 'q':
7093 case 'w':
7094 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007095 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 case '~':
7097 case 'u':
7098 case 'U':
7099 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00007100 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 nv_operator(cap);
7102 break;
7103
7104 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00007105 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 * current function
7107 * "gD": idem, but in the current file.
7108 */
7109 case 'd':
7110 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00007111 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 break;
7113
7114#ifdef FEAT_MOUSE
7115 /*
7116 * g<*Mouse> : <C-*mouse>
7117 */
7118 case K_MIDDLEMOUSE:
7119 case K_MIDDLEDRAG:
7120 case K_MIDDLERELEASE:
7121 case K_LEFTMOUSE:
7122 case K_LEFTDRAG:
7123 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01007124 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 case K_RIGHTMOUSE:
7126 case K_RIGHTDRAG:
7127 case K_RIGHTRELEASE:
7128 case K_X1MOUSE:
7129 case K_X1DRAG:
7130 case K_X1RELEASE:
7131 case K_X2MOUSE:
7132 case K_X2DRAG:
7133 case K_X2RELEASE:
7134 mod_mask = MOD_MASK_CTRL;
7135 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
7136 break;
7137#endif
7138
7139 case K_IGNORE:
7140 break;
7141
7142 /*
7143 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
7144 */
7145 case 'p':
7146 case 'P':
7147 nv_put(cap);
7148 break;
7149
7150#ifdef FEAT_BYTEOFF
7151 /* "go": goto byte count from start of buffer */
7152 case 'o':
7153 goto_byte(cap->count0);
7154 break;
7155#endif
7156
7157 /* "gQ": improved Ex mode */
7158 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007159 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 {
7161 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007162 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 break;
7164 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 if (!checkclearopq(oap))
7167 do_exmode(TRUE);
7168 break;
7169
7170#ifdef FEAT_JUMPLIST
7171 case ',':
7172 nv_pcmark(cap);
7173 break;
7174
7175 case ';':
7176 cap->count1 = -cap->count1;
7177 nv_pcmark(cap);
7178 break;
7179#endif
7180
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007181 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02007182 if (!checkclearop(oap))
7183 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007184 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007185 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02007186 if (!checkclearop(oap))
7187 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007188 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007189
Bram Moolenaar35a2e192006-03-13 22:07:11 +00007190 case '+':
7191 case '-': /* "g+" and "g-": undo or redo along the timeline */
7192 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00007193 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02007194 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00007195 break;
7196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 default:
7198 clearopbeep(oap);
7199 break;
7200 }
7201}
7202
7203/*
7204 * Handle "o" and "O" commands.
7205 */
7206 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007207n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
Bram Moolenaar860cae12010-06-05 23:22:07 +02007209#ifdef FEAT_CONCEAL
7210 linenr_T oldline = curwin->w_cursor.lnum;
7211#endif
7212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 if (!checkclearopq(cap->oap))
7214 {
7215#ifdef FEAT_FOLDING
7216 if (cap->cmdchar == 'O')
7217 /* Open above the first line of a folded sequence of lines */
7218 (void)hasFolding(curwin->w_cursor.lnum,
7219 &curwin->w_cursor.lnum, NULL);
7220 else
7221 /* Open below the last line of a folded sequence of lines */
7222 (void)hasFolding(curwin->w_cursor.lnum,
7223 NULL, &curwin->w_cursor.lnum);
7224#endif
7225 if (u_save((linenr_T)(curwin->w_cursor.lnum -
7226 (cap->cmdchar == 'O' ? 1 : 0)),
7227 (linenr_T)(curwin->w_cursor.lnum +
7228 (cap->cmdchar == 'o' ? 1 : 0))
7229 ) == OK
7230 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
7231#ifdef FEAT_COMMENTS
7232 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
7233#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02007234 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02007236#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007237 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01007238 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02007239#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02007240#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02007241 if (curwin->w_p_cul)
7242 /* force redraw of cursorline */
7243 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02007244#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007245 /* When '#' is in 'cpoptions' ignore the count. */
7246 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
7247 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
7249 }
7250 }
7251}
7252
7253/*
7254 * "." command: redo last change.
7255 */
7256 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007257nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
7259 if (!checkclearopq(cap->oap))
7260 {
7261 /*
7262 * If "restart_edit" is TRUE, the last but one command is repeated
7263 * instead of the last command (inserting text). This is used for
7264 * CTRL-O <.> in insert mode.
7265 */
7266 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
7267 clearopbeep(cap->oap);
7268 }
7269}
7270
7271/*
7272 * CTRL-R: undo undo
7273 */
7274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007275nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
7277 if (!checkclearopq(cap->oap))
7278 {
7279 u_redo((int)cap->count1);
7280 curwin->w_set_curswant = TRUE;
7281 }
7282}
7283
7284/*
7285 * Handle "U" command.
7286 */
7287 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007288nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
7290 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007291 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
7293 /* translate "gUU" to "gUgU" */
7294 cap->cmdchar = 'g';
7295 cap->nchar = 'U';
7296 nv_operator(cap);
7297 }
7298 else if (!checkclearopq(cap->oap))
7299 {
7300 u_undoline();
7301 curwin->w_set_curswant = TRUE;
7302 }
7303}
7304
7305/*
7306 * '~' command: If tilde is not an operator and Visual is off: swap case of a
7307 * single character.
7308 */
7309 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007310nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007312 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02007313 {
7314#ifdef FEAT_JOB_CHANNEL
7315 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7316 {
7317 clearopbeep(cap->oap);
7318 return;
7319 }
7320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 else
7324 nv_operator(cap);
7325}
7326
7327/*
7328 * Handle an operator command.
7329 * The actual work is done by do_pending_operator().
7330 */
7331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007332nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
7334 int op_type;
7335
7336 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02007337#ifdef FEAT_JOB_CHANNEL
7338 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
7339 {
7340 clearopbeep(cap->oap);
7341 return;
7342 }
7343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
7345 if (op_type == cap->oap->op_type) /* double operator works on lines */
7346 nv_lineop(cap);
7347 else if (!checkclearop(cap->oap))
7348 {
7349 cap->oap->start = curwin->w_cursor;
7350 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007351#ifdef FEAT_EVAL
7352 set_op_var(op_type);
7353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 }
7355}
7356
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007357#ifdef FEAT_EVAL
7358/*
7359 * Set v:operator to the characters for "optype".
7360 */
7361 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007362set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007363{
7364 char_u opchars[3];
7365
7366 if (optype == OP_NOP)
7367 set_vim_var_string(VV_OP, NULL, 0);
7368 else
7369 {
7370 opchars[0] = get_op_char(optype);
7371 opchars[1] = get_extra_op_char(optype);
7372 opchars[2] = NUL;
7373 set_vim_var_string(VV_OP, opchars, -1);
7374 }
7375}
7376#endif
7377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378/*
7379 * Handle linewise operator "dd", "yy", etc.
7380 *
7381 * "_" is is a strange motion command that helps make operators more logical.
7382 * It is actually implemented, but not documented in the real Vi. This motion
7383 * command actually refers to "the current line". Commands like "dd" and "yy"
7384 * are really an alternate form of "d_" and "y_". It does accept a count, so
7385 * "d3_" works to delete 3 lines.
7386 */
7387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007388nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389{
7390 cap->oap->motion_type = MLINE;
7391 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
7392 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01007393 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
7394 && cap->oap->motion_force != 'v'
7395 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 || cap->oap->op_type == OP_LSHIFT
7397 || cap->oap->op_type == OP_RSHIFT)
7398 beginline(BL_SOL | BL_FIX);
7399 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
7400 beginline(BL_WHITE | BL_FIX);
7401}
7402
7403/*
7404 * <Home> command.
7405 */
7406 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007407nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00007409 /* CTRL-HOME is like "gg" */
7410 if (mod_mask & MOD_MASK_CTRL)
7411 nv_goto(cap);
7412 else
7413 {
7414 cap->count0 = 1;
7415 nv_pipe(cap);
7416 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00007417 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
7418 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419}
7420
7421/*
7422 * "|" command.
7423 */
7424 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007425nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
7427 cap->oap->motion_type = MCHAR;
7428 cap->oap->inclusive = FALSE;
7429 beginline(0);
7430 if (cap->count0 > 0)
7431 {
7432 coladvance((colnr_T)(cap->count0 - 1));
7433 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
7434 }
7435 else
7436 curwin->w_curswant = 0;
7437 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01007438 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 curwin->w_set_curswant = FALSE;
7440}
7441
7442/*
7443 * Handle back-word command "b" and "B".
7444 * cap->arg is 1 for "B"
7445 */
7446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007447nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449 cap->oap->motion_type = MCHAR;
7450 cap->oap->inclusive = FALSE;
7451 curwin->w_set_curswant = TRUE;
7452 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
7453 clearopbeep(cap->oap);
7454#ifdef FEAT_FOLDING
7455 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7456 foldOpenCursor();
7457#endif
7458}
7459
7460/*
7461 * Handle word motion commands "e", "E", "w" and "W".
7462 * cap->arg is TRUE for "E" and "W".
7463 */
7464 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007465nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466{
7467 int n;
7468 int word_end;
7469 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00007470 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
7472 /*
7473 * Set inclusive for the "E" and "e" command.
7474 */
7475 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
7476 word_end = TRUE;
7477 else
7478 word_end = FALSE;
7479 cap->oap->inclusive = word_end;
7480
7481 /*
7482 * "cw" and "cW" are a special case.
7483 */
7484 if (!word_end && cap->oap->op_type == OP_CHANGE)
7485 {
7486 n = gchar_cursor();
7487 if (n != NUL) /* not an empty line */
7488 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01007489 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 {
7491 /*
7492 * Reproduce a funny Vi behaviour: "cw" on a blank only
7493 * changes one character, not all blanks until the start of
7494 * the next word. Only do this when the 'w' flag is included
7495 * in 'cpoptions'.
7496 */
7497 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
7498 {
7499 cap->oap->inclusive = TRUE;
7500 cap->oap->motion_type = MCHAR;
7501 return;
7502 }
7503 }
7504 else
7505 {
7506 /*
7507 * This is a little strange. To match what the real Vi does,
7508 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
7509 * that we are not on a space or a TAB. This seems impolite
7510 * at first, but it's really more what we mean when we say
7511 * 'cw'.
7512 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007513 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 * will change only one character! This is done by setting
7515 * flag.
7516 */
7517 cap->oap->inclusive = TRUE;
7518 word_end = TRUE;
7519 flag = TRUE;
7520 }
7521 }
7522 }
7523
7524 cap->oap->motion_type = MCHAR;
7525 curwin->w_set_curswant = TRUE;
7526 if (word_end)
7527 n = end_word(cap->count1, cap->arg, flag, FALSE);
7528 else
7529 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
7530
Bram Moolenaardfefb982008-04-01 10:06:39 +00007531 /* Don't leave the cursor on the NUL past the end of line. Unless we
7532 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007533 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007534 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535
7536 if (n == FAIL && cap->oap->op_type == OP_NOP)
7537 clearopbeep(cap->oap);
7538 else
7539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541#ifdef FEAT_FOLDING
7542 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7543 foldOpenCursor();
7544#endif
7545 }
7546}
7547
7548/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007549 * Used after a movement command: If the cursor ends up on the NUL after the
7550 * end of the line, may move it back to the last character and make the motion
7551 * inclusive.
7552 */
7553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007554adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007555{
7556 /* The cursor cannot remain on the NUL when:
7557 * - the column is > 0
7558 * - not in Visual mode or 'selection' is "o"
7559 * - 'virtualedit' is not "all" and not "onemore".
7560 */
7561 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007562 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007563 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007564 {
7565 --curwin->w_cursor.col;
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007566 /* prevent cursor from moving on the trail byte */
7567 if (has_mbyte)
7568 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00007569 oap->inclusive = TRUE;
7570 }
7571}
7572
7573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 * "0" and "^" commands.
7575 * cap->arg is the argument for beginline().
7576 */
7577 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007578nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
7580 cap->oap->motion_type = MCHAR;
7581 cap->oap->inclusive = FALSE;
7582 beginline(cap->arg);
7583#ifdef FEAT_FOLDING
7584 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
7585 foldOpenCursor();
7586#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00007587 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
7588 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589}
7590
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591/*
7592 * In exclusive Visual mode, may include the last character.
7593 */
7594 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007595adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007598 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 if (has_mbyte)
7601 inc_cursor();
7602 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 ++curwin->w_cursor.col;
7604 cap->oap->inclusive = FALSE;
7605 }
7606}
7607
7608/*
7609 * Exclude last character at end of Visual area for 'selection' == "exclusive".
7610 * Should check VIsual_mode before calling this.
7611 * Returns TRUE when backed up to the previous line.
7612 */
7613 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007614unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
7616 pos_T *pp;
7617
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007618 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007620 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 pp = &curwin->w_cursor;
7622 else
7623 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 if (pp->coladd > 0)
7625 --pp->coladd;
7626 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 if (pp->col > 0)
7628 {
7629 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02007630 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632 else if (pp->lnum > 1)
7633 {
7634 --pp->lnum;
7635 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
7636 return TRUE;
7637 }
7638 }
7639 return FALSE;
7640}
7641
7642/*
7643 * SELECT key in Normal or Visual mode: end of Select mode mapping.
7644 */
7645 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007646nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 if (VIsual_active)
7649 VIsual_select = TRUE;
7650 else if (VIsual_reselect)
7651 {
7652 cap->nchar = 'v'; /* fake "gv" command */
7653 cap->arg = TRUE;
7654 nv_g_cmd(cap);
7655 }
7656}
7657
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
7659/*
7660 * "G", "gg", CTRL-END, CTRL-HOME.
7661 * cap->arg is TRUE for "G".
7662 */
7663 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007664nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
7666 linenr_T lnum;
7667
7668 if (cap->arg)
7669 lnum = curbuf->b_ml.ml_line_count;
7670 else
7671 lnum = 1L;
7672 cap->oap->motion_type = MLINE;
7673 setpcmark();
7674
7675 /* When a count is given, use it instead of the default lnum */
7676 if (cap->count0 != 0)
7677 lnum = cap->count0;
7678 if (lnum < 1L)
7679 lnum = 1L;
7680 else if (lnum > curbuf->b_ml.ml_line_count)
7681 lnum = curbuf->b_ml.ml_line_count;
7682 curwin->w_cursor.lnum = lnum;
7683 beginline(BL_SOL | BL_FIX);
7684#ifdef FEAT_FOLDING
7685 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
7686 foldOpenCursor();
7687#endif
7688}
7689
7690/*
7691 * CTRL-\ in Normal mode.
7692 */
7693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007694nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
7697 {
7698 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00007699 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 clear_cmdline = TRUE; /* unshow mode later */
7701 restart_edit = 0;
7702#ifdef FEAT_CMDWIN
7703 if (cmdwin_type != 0)
7704 cmdwin_result = Ctrl_C;
7705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 if (VIsual_active)
7707 {
7708 end_visual_mode(); /* stop Visual */
7709 redraw_curbuf_later(INVERTED);
7710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
7712 if (cap->nchar == Ctrl_G && p_im)
7713 restart_edit = 'a';
7714 }
7715 else
7716 clearopbeep(cap->oap);
7717}
7718
7719/*
7720 * ESC in Normal mode: beep, but don't flush buffers.
7721 * Don't even beep if we are canceling a command.
7722 */
7723 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007724nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
7726 int no_reason;
7727
7728 no_reason = (cap->oap->op_type == OP_NOP
7729 && cap->opcount == 0
7730 && cap->count0 == 0
7731 && cap->oap->regname == 0
7732 && !p_im);
7733
7734 if (cap->arg) /* TRUE for CTRL-C */
7735 {
7736 if (restart_edit == 0
7737#ifdef FEAT_CMDWIN
7738 && cmdwin_type == 0
7739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01007742 {
7743 if (anyBufIsChanged())
7744 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
7745 else
7746 msg(_("Type :qa and press <Enter> to exit Vim"));
7747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748
7749 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
7750 * set again below when halfway a mapping. */
7751 if (!p_im)
7752 restart_edit = 0;
7753#ifdef FEAT_CMDWIN
7754 if (cmdwin_type != 0)
7755 {
7756 cmdwin_result = K_IGNORE;
7757 got_int = FALSE; /* don't stop executing autocommands et al. */
7758 return;
7759 }
7760#endif
7761 }
7762
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (VIsual_active)
7764 {
7765 end_visual_mode(); /* stop Visual */
7766 check_cursor_col(); /* make sure cursor is not beyond EOL */
7767 curwin->w_set_curswant = TRUE;
7768 redraw_curbuf_later(INVERTED);
7769 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007770 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02007771 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 clearop(cap->oap);
7773
7774 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
7775 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01007776 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 restart_edit = 'a';
7778}
7779
7780/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007781 * Move the cursor for the "A" command.
7782 */
7783 void
7784set_cursor_for_append_to_line(void)
7785{
7786 curwin->w_set_curswant = TRUE;
7787 if (ve_flags == VE_ALL)
7788 {
7789 int save_State = State;
7790
7791 /* Pretend Insert mode here to allow the cursor on the
7792 * character past the end of the line */
7793 State = INSERT;
7794 coladvance((colnr_T)MAXCOL);
7795 State = save_State;
7796 }
7797 else
7798 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
7799}
7800
7801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01007803 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 */
7805 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007806nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
7808 /* <Insert> is equal to "i" */
7809 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
7810 cap->cmdchar = 'i';
7811
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 /* in Visual mode "A" and "I" are an operator */
7813 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02007814 {
7815#ifdef FEAT_TERMINAL
7816 if (term_in_normal_mode())
7817 {
7818 end_visual_mode();
7819 clearop(cap->oap);
7820 term_enter_job_mode();
7821 return;
7822 }
7823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02007825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826
7827 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007828 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
7829 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
7831#ifdef FEAT_TEXTOBJ
7832 nv_object(cap);
7833#else
7834 clearopbeep(cap->oap);
7835#endif
7836 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02007837#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02007838 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02007839 {
7840 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02007841 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02007842 return;
7843 }
7844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 else if (!curbuf->b_p_ma && !p_im)
7846 {
7847 /* Only give this error when 'insertmode' is off. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007848 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01007850 if (cap->cmdchar == K_PS)
7851 /* drop the pasted text */
7852 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 }
Bram Moolenaara1891842017-02-04 21:34:31 +01007854 else if (cap->cmdchar == K_PS && VIsual_active)
7855 {
7856 pos_T old_pos = curwin->w_cursor;
7857 pos_T old_visual = VIsual;
7858
7859 /* In Visual mode the selected text is deleted. */
7860 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
7861 {
7862 shift_delete_registers();
7863 cap->oap->regname = '1';
7864 }
7865 else
7866 cap->oap->regname = '-';
7867 cap->cmdchar = 'd';
7868 cap->nchar = NUL;
7869 nv_operator(cap);
7870 do_pending_operator(cap, 0, FALSE);
7871 cap->cmdchar = K_PS;
7872
7873 /* When the last char in the line was deleted then append. Detect this
7874 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007875 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
7876 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01007877 inc_cursor();
7878
7879 /* Insert to replace the deleted text with the pasted text. */
7880 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 else if (!checkclearopq(cap->oap))
7883 {
7884 switch (cap->cmdchar)
7885 {
7886 case 'A': /* "A"ppend after the line */
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007887 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 break;
7889
7890 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007891 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
7892 beginline(BL_WHITE);
7893 else
7894 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 break;
7896
Bram Moolenaara1891842017-02-04 21:34:31 +01007897 case K_PS:
7898 /* Bracketed paste works like "a"ppend, unless the cursor is in
7899 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007900 if (curwin->w_cursor.col == 0)
7901 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007902 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01007903
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 case 'a': /* "a"ppend is like "i"nsert on the next character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 /* increment coladd when in virtual space, increment the
7906 * column otherwise, also to append after an unprintable char */
7907 if (virtual_active()
7908 && (curwin->w_cursor.coladd > 0
7909 || *ml_get_cursor() == NUL
7910 || *ml_get_cursor() == TAB))
7911 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007912 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 inc_cursor();
7914 break;
7915 }
7916
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
7918 {
7919 int save_State = State;
7920
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007921 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 * character past the end of the line */
7923 State = INSERT;
7924 coladvance(getviscol());
7925 State = save_State;
7926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
7928 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
7929 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01007930 else if (cap->cmdchar == K_PS)
7931 /* drop the pasted text */
7932 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933}
7934
7935/*
7936 * Invoke edit() and take care of "restart_edit" and the return value.
7937 */
7938 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007939invoke_edit(
7940 cmdarg_T *cap,
7941 int repl, /* "r" or "gr" command */
7942 int cmd,
7943 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 int restart_edit_save = 0;
7946
7947 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
7948 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
7949 * it. */
7950 if (repl || !stuff_empty())
7951 restart_edit_save = restart_edit;
7952 else
7953 restart_edit_save = 0;
7954
7955 /* Always reset "restart_edit", this is not a restarted edit. */
7956 restart_edit = 0;
7957
7958 if (edit(cmd, startln, cap->count1))
7959 cap->retval |= CA_COMMAND_BUSY;
7960
7961 if (restart_edit == 0)
7962 restart_edit = restart_edit_save;
7963}
7964
7965#ifdef FEAT_TEXTOBJ
7966/*
7967 * "a" or "i" while an operator is pending or in Visual mode: object motion.
7968 */
7969 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007970nv_object(
7971 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
7973 int flag;
7974 int include;
7975 char_u *mps_save;
7976
7977 if (cap->cmdchar == 'i')
7978 include = FALSE; /* "ix" = inner object: exclude white space */
7979 else
7980 include = TRUE; /* "ax" = an object: include white space */
7981
7982 /* Make sure (), [], {} and <> are in 'matchpairs' */
7983 mps_save = curbuf->b_p_mps;
7984 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
7985
7986 switch (cap->nchar)
7987 {
7988 case 'w': /* "aw" = a word */
7989 flag = current_word(cap->oap, cap->count1, include, FALSE);
7990 break;
7991 case 'W': /* "aW" = a WORD */
7992 flag = current_word(cap->oap, cap->count1, include, TRUE);
7993 break;
7994 case 'b': /* "ab" = a braces block */
7995 case '(':
7996 case ')':
7997 flag = current_block(cap->oap, cap->count1, include, '(', ')');
7998 break;
7999 case 'B': /* "aB" = a Brackets block */
8000 case '{':
8001 case '}':
8002 flag = current_block(cap->oap, cap->count1, include, '{', '}');
8003 break;
8004 case '[': /* "a[" = a [] block */
8005 case ']':
8006 flag = current_block(cap->oap, cap->count1, include, '[', ']');
8007 break;
8008 case '<': /* "a<" = a <> block */
8009 case '>':
8010 flag = current_block(cap->oap, cap->count1, include, '<', '>');
8011 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00008012 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01008013 /* Do not adjust oap->end in do_pending_operator()
8014 * otherwise there are different results for 'dit'
8015 * (note leading whitespace in last line):
8016 * 1) <b> 2) <b>
8017 * foobar foobar
8018 * </b> </b>
8019 */
8020 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00008021 flag = current_tagblock(cap->oap, cap->count1, include);
8022 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 case 'p': /* "ap" = a paragraph */
8024 flag = current_par(cap->oap, cap->count1, include, 'p');
8025 break;
8026 case 's': /* "as" = a sentence */
8027 flag = current_sent(cap->oap, cap->count1, include);
8028 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00008029 case '"': /* "a"" = a double quoted string */
8030 case '\'': /* "a'" = a single quoted string */
8031 case '`': /* "a`" = a backtick quoted string */
8032 flag = current_quote(cap->oap, cap->count1, include,
8033 cap->nchar);
8034 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035#if 0 /* TODO */
8036 case 'S': /* "aS" = a section */
8037 case 'f': /* "af" = a filename */
8038 case 'u': /* "au" = a URL */
8039#endif
8040 default:
8041 flag = FAIL;
8042 break;
8043 }
8044
8045 curbuf->b_p_mps = mps_save;
8046 if (flag == FAIL)
8047 clearopbeep(cap->oap);
8048 adjust_cursor_col();
8049 curwin->w_set_curswant = TRUE;
8050}
8051#endif
8052
8053/*
8054 * "q" command: Start/stop recording.
8055 * "q:", "q/", "q?": edit command-line in command-line window.
8056 */
8057 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008058nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 if (cap->oap->op_type == OP_FORMAT)
8061 {
8062 /* "gqq" is the same as "gqgq": format line */
8063 cap->cmdchar = 'g';
8064 cap->nchar = 'q';
8065 nv_operator(cap);
8066 }
8067 else if (!checkclearop(cap->oap))
8068 {
8069#ifdef FEAT_CMDWIN
8070 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
8071 {
8072 stuffcharReadbuff(cap->nchar);
8073 stuffcharReadbuff(K_CMDWIN);
8074 }
8075 else
8076#endif
8077 /* (stop) recording into a named register, unless executing a
8078 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02008079 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 clearopbeep(cap->oap);
8081 }
8082}
8083
8084/*
8085 * Handle the "@r" command.
8086 */
8087 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008088nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
8090 if (checkclearop(cap->oap))
8091 return;
8092#ifdef FEAT_EVAL
8093 if (cap->nchar == '=')
8094 {
8095 if (get_expr_register() == NUL)
8096 return;
8097 }
8098#endif
8099 while (cap->count1-- && !got_int)
8100 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008101 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {
8103 clearopbeep(cap->oap);
8104 break;
8105 }
8106 line_breakcheck();
8107 }
8108}
8109
8110/*
8111 * Handle the CTRL-U and CTRL-D commands.
8112 */
8113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008114nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115{
8116 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
8117 || (cap->cmdchar == Ctrl_D
8118 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
8119 clearopbeep(cap->oap);
8120 else if (!checkclearop(cap->oap))
8121 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
8122}
8123
8124/*
8125 * Handle "J" or "gJ" command.
8126 */
8127 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008128nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 if (VIsual_active) /* join the visual lines */
8131 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008132 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {
8134 if (cap->count0 <= 1)
8135 cap->count0 = 2; /* default for join is two lines! */
8136 if (curwin->w_cursor.lnum + cap->count0 - 1 >
8137 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01008139 /* can't join when on the last line */
8140 if (cap->count0 <= 2)
8141 {
8142 clearopbeep(cap->oap);
8143 return;
8144 }
8145 cap->count0 = curbuf->b_ml.ml_line_count
8146 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01008148
8149 prep_redo(cap->oap->regname, cap->count0,
8150 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
8151 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 }
8153}
8154
8155/*
8156 * "P", "gP", "p" and "gp" commands.
8157 */
8158 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008159nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02008161 nv_put_opt(cap, FALSE);
8162}
8163
8164/*
8165 * "P", "gP", "p" and "gp" commands.
8166 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
8167 */
8168 static void
8169nv_put_opt(cmdarg_T *cap, int fix_indent)
8170{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 int regname = 0;
8172 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008173 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008174 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 int dir;
8176 int flags = 0;
8177
8178 if (cap->oap->op_type != OP_NOP)
8179 {
8180#ifdef FEAT_DIFF
8181 /* "dp" is ":diffput" */
8182 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
8183 {
8184 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01008185 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 }
8187 else
8188#endif
8189 clearopbeep(cap->oap);
8190 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02008191#ifdef FEAT_JOB_CHANNEL
8192 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
8193 {
8194 clearopbeep(cap->oap);
8195 }
8196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 else
8198 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02008199 if (fix_indent)
8200 {
8201 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
8202 ? FORWARD : BACKWARD;
8203 flags |= PUT_FIXINDENT;
8204 }
8205 else
8206 dir = (cap->cmdchar == 'P'
8207 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 ? BACKWARD : FORWARD;
8209 prep_redo_cmd(cap);
8210 if (cap->cmdchar == 'g')
8211 flags |= PUT_CURSEND;
8212
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 if (VIsual_active)
8214 {
8215 /* Putting in Visual mode: The put text replaces the selected
8216 * text. First delete the selected text, then put the new text.
8217 * Need to save and restore the registers that the delete
8218 * overwrites if the old contents is being put.
8219 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008220 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008222#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008224#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01008225 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01008226 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008227#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230
8231 )
8232 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01008233 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 * put, save it first. */
8235 reg1 = get_register(regname, TRUE);
8236 }
8237
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02008238 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 cap->cmdchar = 'd';
8240 cap->nchar = NUL;
8241 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02008242 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 nv_operator(cap);
8244 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008245 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02008246 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247
8248 /* delete PUT_LINE_BACKWARD; */
8249 cap->oap->regname = regname;
8250
8251 if (reg1 != NULL)
8252 {
8253 /* Delete probably changed the register we want to put, save
8254 * it first. Then put back what was there before the delete. */
8255 reg2 = get_register(regname, FALSE);
8256 put_register(regname, reg1);
8257 }
8258
8259 /* When deleted a linewise Visual area, put the register as
8260 * lines to avoid it joined with the next line. When deletion was
8261 * characterwise, split a line when putting lines. */
8262 if (VIsual_mode == 'V')
8263 flags |= PUT_LINE;
8264 else if (VIsual_mode == 'v')
8265 flags |= PUT_LINE_SPLIT;
8266 if (VIsual_mode == Ctrl_V && dir == FORWARD)
8267 flags |= PUT_LINE_FORWARD;
8268 dir = BACKWARD;
8269 if ((VIsual_mode != 'V'
8270 && curwin->w_cursor.col < curbuf->b_op_start.col)
8271 || (VIsual_mode == 'V'
8272 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
8273 /* cursor is at the end of the line or end of file, put
8274 * forward. */
8275 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02008276 /* May have been reset in do_put(). */
8277 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 do_put(cap->oap->regname, dir, cap->count1, flags);
8280
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 /* If a register was saved, put it back now. */
8282 if (reg2 != NULL)
8283 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008284
8285 /* What to reselect with "gv"? Selecting the just put text seems to
8286 * be the most useful, since the original text was removed. */
8287 if (was_visual)
8288 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00008289 curbuf->b_visual.vi_start = curbuf->b_op_start;
8290 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01008291 /* need to adjust cursor position */
8292 if (*p_sel == 'e')
8293 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008294 }
8295
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008296 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008297 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008298 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00008299 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00008300 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02008301 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00008302
8303 /* If the cursor was in that line, move it to the end of the last
8304 * line. */
8305 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
8306 {
8307 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8308 coladvance((colnr_T)MAXCOL);
8309 }
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 auto_format(FALSE, TRUE);
8312 }
8313}
8314
8315/*
8316 * "o" and "O" commands.
8317 */
8318 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008319nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
8321#ifdef FEAT_DIFF
8322 /* "do" is ":diffget" */
8323 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
8324 {
8325 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01008326 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 else
8329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (VIsual_active) /* switch start and end of visual */
8331 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008332#ifdef FEAT_JOB_CHANNEL
8333 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02008334 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 n_opencmd(cap);
8338}
8339
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340#ifdef FEAT_NETBEANS_INTG
8341 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008342nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344 netbeans_keycommand(cap->nchar);
8345}
8346#endif
8347
8348#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008350nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
8352 do_put('~', BACKWARD, 1L, PUT_CURSEND);
8353}
8354#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00008355
Bram Moolenaar3918c952005-03-15 22:34:55 +00008356/*
8357 * Trigger CursorHold event.
8358 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
8359 * input buffer. "did_cursorhold" is set to avoid retriggering.
8360 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00008361 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008362nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00008363{
8364 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
8365 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00008366 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00008367}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008368
8369/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02008370 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008371 */
8372 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008373get_op_vcol(
8374 oparg_T *oap,
8375 colnr_T redo_VIsual_vcol,
8376 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008377{
8378 colnr_T start, end;
8379
Bram Moolenaar89c17c02015-08-11 17:46:36 +02008380 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02008381 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008382 return;
8383
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02008384 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008385
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008386 /* prevent from moving onto a trail byte */
8387 if (has_mbyte)
8388 mb_adjustpos(curwin->w_buffer, &oap->end);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008389
8390 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008391
Bram Moolenaar31b259b2015-07-28 11:21:32 +02008392 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008393 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02008394 getvvcol(curwin, &(oap->end), &start, NULL, &end);
8395
8396 if (start < oap->start_vcol)
8397 oap->start_vcol = start;
8398 if (end > oap->end_vcol)
8399 {
8400 if (initial && *p_sel == 'e' && start >= 1
8401 && start - 1 >= oap->end_vcol)
8402 oap->end_vcol = start - 1;
8403 else
8404 oap->end_vcol = end;
8405 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008406 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02008407
Bram Moolenaar74db34c2015-06-25 13:30:46 +02008408 /* if '$' was used, get oap->end_vcol from longest line */
8409 if (curwin->w_curswant == MAXCOL)
8410 {
8411 curwin->w_cursor.col = MAXCOL;
8412 oap->end_vcol = 0;
8413 for (curwin->w_cursor.lnum = oap->start.lnum;
8414 curwin->w_cursor.lnum <= oap->end.lnum;
8415 ++curwin->w_cursor.lnum)
8416 {
8417 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
8418 if (end > oap->end_vcol)
8419 oap->end_vcol = end;
8420 }
8421 }
8422 else if (redo_VIsual_busy)
8423 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
8424 /*
8425 * Correct oap->end.col and oap->start.col to be the
8426 * upper-left and lower-right corner of the block area.
8427 *
8428 * (Actually, this does convert column positions into character
8429 * positions)
8430 */
8431 curwin->w_cursor.lnum = oap->end.lnum;
8432 coladvance(oap->end_vcol);
8433 oap->end = curwin->w_cursor;
8434
8435 curwin->w_cursor = oap->start;
8436 coladvance(oap->start_vcol);
8437 oap->start = curwin->w_cursor;
8438}