blob: e297a2e77bea6f79344a78d9cefef4484e330942 [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 Moolenaarf7ff6e82014-03-23 15:13:05 +010033#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void find_start_of_word(pos_T *);
35static void find_end_of_word(pos_T *);
36static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void prep_redo(int regname, long, int, int, int, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void clearop(oparg_T *oap);
40static void clearopbeep(oparg_T *oap);
41static void unshift_special(cmdarg_T *cap);
42static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045#endif
46
47/*
48 * nv_*(): functions called to handle Normal and Visual mode commands.
49 * n_*(): functions called to handle Normal mode commands.
50 * v_*(): functions called to handle Visual mode commands.
51 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010052static void nv_ignore(cmdarg_T *cap);
53static void nv_nop(cmdarg_T *cap);
54static void nv_error(cmdarg_T *cap);
55static void nv_help(cmdarg_T *cap);
56static void nv_addsub(cmdarg_T *cap);
57static void nv_page(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010059static void nv_mousescroll(cmdarg_T *cap);
60static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010062static void nv_scroll_line(cmdarg_T *cap);
63static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_ver_scrollbar(cmdarg_T *cap);
66static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000068#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_tabline(cmdarg_T *cap);
70static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000071#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010072static void nv_exmode(cmdarg_T *cap);
73static void nv_colon(cmdarg_T *cap);
74static void nv_ctrlg(cmdarg_T *cap);
75static void nv_ctrlh(cmdarg_T *cap);
76static void nv_clear(cmdarg_T *cap);
77static void nv_ctrlo(cmdarg_T *cap);
78static void nv_hat(cmdarg_T *cap);
79static void nv_Zet(cmdarg_T *cap);
80static void nv_ident(cmdarg_T *cap);
81static void nv_tagpop(cmdarg_T *cap);
82static void nv_scroll(cmdarg_T *cap);
83static void nv_right(cmdarg_T *cap);
84static void nv_left(cmdarg_T *cap);
85static void nv_up(cmdarg_T *cap);
86static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010087static void nv_end(cmdarg_T *cap);
88static void nv_dollar(cmdarg_T *cap);
89static void nv_search(cmdarg_T *cap);
90static void nv_next(cmdarg_T *cap);
91static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
92static void nv_csearch(cmdarg_T *cap);
93static void nv_brackets(cmdarg_T *cap);
94static void nv_percent(cmdarg_T *cap);
95static void nv_brace(cmdarg_T *cap);
96static void nv_mark(cmdarg_T *cap);
97static void nv_findpar(cmdarg_T *cap);
98static void nv_undo(cmdarg_T *cap);
99static void nv_kundo(cmdarg_T *cap);
100static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100101static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100102static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
103static void v_visop(cmdarg_T *cap);
104static void nv_subst(cmdarg_T *cap);
105static void nv_abbrev(cmdarg_T *cap);
106static void nv_optrans(cmdarg_T *cap);
107static void nv_gomark(cmdarg_T *cap);
108static void nv_pcmark(cmdarg_T *cap);
109static void nv_regname(cmdarg_T *cap);
110static void nv_visual(cmdarg_T *cap);
111static void n_start_visual_mode(int c);
112static void nv_window(cmdarg_T *cap);
113static void nv_suspend(cmdarg_T *cap);
114static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_dot(cmdarg_T *cap);
116static void nv_redo(cmdarg_T *cap);
117static void nv_Undo(cmdarg_T *cap);
118static void nv_tilde(cmdarg_T *cap);
119static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000120#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000122#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_lineop(cmdarg_T *cap);
124static void nv_home(cmdarg_T *cap);
125static void nv_pipe(cmdarg_T *cap);
126static void nv_bck_word(cmdarg_T *cap);
127static void nv_wordcmd(cmdarg_T *cap);
128static void nv_beginline(cmdarg_T *cap);
129static void adjust_cursor(oparg_T *oap);
130static void adjust_for_sel(cmdarg_T *cap);
131static int unadjust_for_sel(void);
132static void nv_select(cmdarg_T *cap);
133static void nv_goto(cmdarg_T *cap);
134static void nv_normal(cmdarg_T *cap);
135static void nv_esc(cmdarg_T *oap);
136static void nv_edit(cmdarg_T *cap);
137static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100139static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100141static void nv_record(cmdarg_T *cap);
142static void nv_at(cmdarg_T *cap);
143static void nv_halfpage(cmdarg_T *cap);
144static void nv_join(cmdarg_T *cap);
145static void nv_put(cmdarg_T *cap);
146static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100148static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif
150#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100151static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100153static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100154static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000156static char *e_noident = N_("E349: No identifier under cursor");
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158/*
159 * Function to be called for a Normal or Visual mode command.
160 * The argument is a cmdarg_T.
161 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100162typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
164/* Values for cmd_flags. */
165#define NV_NCH 0x01 /* may need to get a second char */
166#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
167#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
168#define NV_LANG 0x08 /* second char needs language adjustment */
169
170#define NV_SS 0x10 /* may start selection */
171#define NV_SSS 0x20 /* may start selection with shift modifier */
172#define NV_STS 0x40 /* may stop selection without shift modif. */
173#define NV_RL 0x80 /* 'rightleft' modifies command */
174#define NV_KEEPREG 0x100 /* don't clear regname */
175#define NV_NCW 0x200 /* not allowed in command-line window */
176
177/*
178 * Generally speaking, every Normal mode command should either clear any
179 * pending operator (with *clearop*()), or set the motion type variable
180 * oap->motion_type.
181 *
182 * When a cursor motion command is made, it is marked as being a character or
183 * line oriented motion. Then, if an operator is in effect, the operation
184 * becomes character or line oriented accordingly.
185 */
186
187/*
188 * This table contains one entry for every Normal or Visual mode command.
189 * The order doesn't matter, init_normal_cmds() will create a sorted index.
190 * It is faster when all keys from zero to '~' are present.
191 */
192static const struct nv_cmd
193{
194 int cmd_char; /* (first) command character */
195 nv_func_T cmd_func; /* function for this command */
196 short_u cmd_flags; /* NV_ flags */
197 short cmd_arg; /* value for ca.arg */
198} nv_cmds[] =
199{
200 {NUL, nv_error, 0, 0},
201 {Ctrl_A, nv_addsub, 0, 0},
202 {Ctrl_B, nv_page, NV_STS, BACKWARD},
203 {Ctrl_C, nv_esc, 0, TRUE},
204 {Ctrl_D, nv_halfpage, 0, 0},
205 {Ctrl_E, nv_scroll_line, 0, TRUE},
206 {Ctrl_F, nv_page, NV_STS, FORWARD},
207 {Ctrl_G, nv_ctrlg, 0, 0},
208 {Ctrl_H, nv_ctrlh, 0, 0},
209 {Ctrl_I, nv_pcmark, 0, 0},
210 {NL, nv_down, 0, FALSE},
211 {Ctrl_K, nv_error, 0, 0},
212 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100213 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 {Ctrl_N, nv_down, NV_STS, FALSE},
215 {Ctrl_O, nv_ctrlo, 0, 0},
216 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000217 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 {Ctrl_R, nv_redo, 0, 0},
219 {Ctrl_S, nv_ignore, 0, 0},
220 {Ctrl_T, nv_tagpop, NV_NCW, 0},
221 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 {Ctrl_V, nv_visual, 0, FALSE},
223 {'V', nv_visual, 0, FALSE},
224 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 {Ctrl_W, nv_window, 0, 0},
226 {Ctrl_X, nv_addsub, 0, 0},
227 {Ctrl_Y, nv_scroll_line, 0, FALSE},
228 {Ctrl_Z, nv_suspend, 0, 0},
229 {ESC, nv_esc, 0, FALSE},
230 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
231 {Ctrl_RSB, nv_ident, NV_NCW, 0},
232 {Ctrl_HAT, nv_hat, NV_NCW, 0},
233 {Ctrl__, nv_error, 0, 0},
234 {' ', nv_right, 0, 0},
235 {'!', nv_operator, 0, 0},
236 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
237 {'#', nv_ident, 0, 0},
238 {'$', nv_dollar, 0, 0},
239 {'%', nv_percent, 0, 0},
240 {'&', nv_optrans, 0, 0},
241 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
242 {'(', nv_brace, 0, BACKWARD},
243 {')', nv_brace, 0, FORWARD},
244 {'*', nv_ident, 0, 0},
245 {'+', nv_down, 0, TRUE},
246 {',', nv_csearch, 0, TRUE},
247 {'-', nv_up, 0, TRUE},
248 {'.', nv_dot, NV_KEEPREG, 0},
249 {'/', nv_search, 0, FALSE},
250 {'0', nv_beginline, 0, 0},
251 {'1', nv_ignore, 0, 0},
252 {'2', nv_ignore, 0, 0},
253 {'3', nv_ignore, 0, 0},
254 {'4', nv_ignore, 0, 0},
255 {'5', nv_ignore, 0, 0},
256 {'6', nv_ignore, 0, 0},
257 {'7', nv_ignore, 0, 0},
258 {'8', nv_ignore, 0, 0},
259 {'9', nv_ignore, 0, 0},
260 {':', nv_colon, 0, 0},
261 {';', nv_csearch, 0, FALSE},
262 {'<', nv_operator, NV_RL, 0},
263 {'=', nv_operator, 0, 0},
264 {'>', nv_operator, NV_RL, 0},
265 {'?', nv_search, 0, FALSE},
266 {'@', nv_at, NV_NCH_NOP, FALSE},
267 {'A', nv_edit, 0, 0},
268 {'B', nv_bck_word, 0, 1},
269 {'C', nv_abbrev, NV_KEEPREG, 0},
270 {'D', nv_abbrev, NV_KEEPREG, 0},
271 {'E', nv_wordcmd, 0, TRUE},
272 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
273 {'G', nv_goto, 0, TRUE},
274 {'H', nv_scroll, 0, 0},
275 {'I', nv_edit, 0, 0},
276 {'J', nv_join, 0, 0},
277 {'K', nv_ident, 0, 0},
278 {'L', nv_scroll, 0, 0},
279 {'M', nv_scroll, 0, 0},
280 {'N', nv_next, 0, SEARCH_REV},
281 {'O', nv_open, 0, 0},
282 {'P', nv_put, 0, 0},
283 {'Q', nv_exmode, NV_NCW, 0},
284 {'R', nv_Replace, 0, FALSE},
285 {'S', nv_subst, NV_KEEPREG, 0},
286 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
287 {'U', nv_Undo, 0, 0},
288 {'W', nv_wordcmd, 0, TRUE},
289 {'X', nv_abbrev, NV_KEEPREG, 0},
290 {'Y', nv_abbrev, NV_KEEPREG, 0},
291 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
292 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
293 {'\\', nv_error, 0, 0},
294 {']', nv_brackets, NV_NCH_ALW, FORWARD},
295 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
296 {'_', nv_lineop, 0, 0},
297 {'`', nv_gomark, NV_NCH_ALW, FALSE},
298 {'a', nv_edit, NV_NCH, 0},
299 {'b', nv_bck_word, 0, 0},
300 {'c', nv_operator, 0, 0},
301 {'d', nv_operator, 0, 0},
302 {'e', nv_wordcmd, 0, FALSE},
303 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
304 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
305 {'h', nv_left, NV_RL, 0},
306 {'i', nv_edit, NV_NCH, 0},
307 {'j', nv_down, 0, FALSE},
308 {'k', nv_up, 0, FALSE},
309 {'l', nv_right, NV_RL, 0},
310 {'m', nv_mark, NV_NCH_NOP, 0},
311 {'n', nv_next, 0, 0},
312 {'o', nv_open, 0, 0},
313 {'p', nv_put, 0, 0},
314 {'q', nv_record, NV_NCH, 0},
315 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
316 {'s', nv_subst, NV_KEEPREG, 0},
317 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
318 {'u', nv_undo, 0, 0},
319 {'w', nv_wordcmd, 0, FALSE},
320 {'x', nv_abbrev, NV_KEEPREG, 0},
321 {'y', nv_operator, 0, 0},
322 {'z', nv_zet, NV_NCH_ALW, 0},
323 {'{', nv_findpar, 0, BACKWARD},
324 {'|', nv_pipe, 0, 0},
325 {'}', nv_findpar, 0, FORWARD},
326 {'~', nv_tilde, 0, 0},
327
328 /* pound sign */
329 {POUND, nv_ident, 0, 0},
330#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200331 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
332 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
333 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
334 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {K_LEFTMOUSE, nv_mouse, 0, 0},
336 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
337 {K_LEFTDRAG, nv_mouse, 0, 0},
338 {K_LEFTRELEASE, nv_mouse, 0, 0},
339 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100340 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
342 {K_MIDDLEDRAG, nv_mouse, 0, 0},
343 {K_MIDDLERELEASE, nv_mouse, 0, 0},
344 {K_RIGHTMOUSE, nv_mouse, 0, 0},
345 {K_RIGHTDRAG, nv_mouse, 0, 0},
346 {K_RIGHTRELEASE, nv_mouse, 0, 0},
347 {K_X1MOUSE, nv_mouse, 0, 0},
348 {K_X1DRAG, nv_mouse, 0, 0},
349 {K_X1RELEASE, nv_mouse, 0, 0},
350 {K_X2MOUSE, nv_mouse, 0, 0},
351 {K_X2DRAG, nv_mouse, 0, 0},
352 {K_X2RELEASE, nv_mouse, 0, 0},
353#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000354 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000355 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {K_INS, nv_edit, 0, 0},
357 {K_KINS, nv_edit, 0, 0},
358 {K_BS, nv_ctrlh, 0, 0},
359 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
360 {K_S_UP, nv_page, NV_SS, BACKWARD},
361 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
362 {K_S_DOWN, nv_page, NV_SS, FORWARD},
363 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
364 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
365 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
366 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
367 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
368 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
369 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
370 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
371 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
372 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
373 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
374 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {K_S_END, nv_end, NV_SS, FALSE},
376 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
377 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
378 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {K_S_HOME, nv_home, NV_SS, 0},
380 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
381 {K_DEL, nv_abbrev, 0, 0},
382 {K_KDEL, nv_abbrev, 0, 0},
383 {K_UNDO, nv_kundo, 0, 0},
384 {K_HELP, nv_help, NV_NCW, 0},
385 {K_F1, nv_help, NV_NCW, 0},
386 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#ifdef FEAT_GUI
389 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
390 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
391#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000392#ifdef FEAT_GUI_TABLINE
393 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000394 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#ifdef FEAT_NETBEANS_INTG
397 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
398#endif
399#ifdef FEAT_DND
400 {K_DROP, nv_drop, NV_STS, 0},
401#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100403 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404};
405
406/* Number of commands in nv_cmds[]. */
407#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
408
409/* Sorted index of commands in nv_cmds[]. */
410static short nv_cmd_idx[NV_CMDS_SIZE];
411
412/* The highest index for which
413 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
414static int nv_max_linear;
415
416/*
417 * Compare functions for qsort() below, that checks the command character
418 * through the index in nv_cmd_idx[].
419 */
420 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100421nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422{
423 int c1, c2;
424
425 /* The commands are sorted on absolute value. */
426 c1 = nv_cmds[*(const short *)s1].cmd_char;
427 c2 = nv_cmds[*(const short *)s2].cmd_char;
428 if (c1 < 0)
429 c1 = -c1;
430 if (c2 < 0)
431 c2 = -c2;
432 return c1 - c2;
433}
434
435/*
436 * Initialize the nv_cmd_idx[] table.
437 */
438 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100439init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
441 int i;
442
443 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000444 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 nv_cmd_idx[i] = i;
446
447 /* Sort the commands by the command character. */
448 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
449
450 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000451 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
453 break;
454 nv_max_linear = i - 1;
455}
456
457/*
458 * Search for a command in the commands table.
459 * Returns -1 for invalid command.
460 */
461 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100462find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463{
464 int i;
465 int idx;
466 int top, bot;
467 int c;
468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 /* A multi-byte character is never a command. */
470 if (cmdchar >= 0x100)
471 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472
473 /* We use the absolute value of the character. Special keys have a
474 * negative value, but are sorted on their absolute value. */
475 if (cmdchar < 0)
476 cmdchar = -cmdchar;
477
478 /* If the character is in the first part: The character is the index into
479 * nv_cmd_idx[]. */
480 if (cmdchar <= nv_max_linear)
481 return nv_cmd_idx[cmdchar];
482
483 /* Perform a binary search. */
484 bot = nv_max_linear + 1;
485 top = NV_CMDS_SIZE - 1;
486 idx = -1;
487 while (bot <= top)
488 {
489 i = (top + bot) / 2;
490 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
491 if (c < 0)
492 c = -c;
493 if (cmdchar == c)
494 {
495 idx = nv_cmd_idx[i];
496 break;
497 }
498 if (cmdchar > c)
499 bot = i + 1;
500 else
501 top = i - 1;
502 }
503 return idx;
504}
505
506/*
507 * Execute a command in Normal mode.
508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100510normal_cmd(
511 oparg_T *oap,
512 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 cmdarg_T ca; /* command arguments */
515 int c;
516 int ctrl_w = FALSE; /* got CTRL-W command */
517 int old_col = curwin->w_curswant;
518#ifdef FEAT_CMDL_INFO
519 int need_flushbuf; /* need to call out_flush() */
520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 pos_T old_pos; /* cursor position before command */
522 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 static int old_mapped_len = 0;
524 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000525#ifdef FEAT_EVAL
526 int set_prevcount = FALSE;
527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
529 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
530 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000531
532 /* Use a count remembered from before entering an operator. After typing
533 * "3d" we return from normal_cmd() and come back here, the "3" is
534 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 ca.opcount = opcount;
536
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 /*
538 * If there is an operator pending, then the command we take this time
539 * will terminate it. Finish_op tells us to finish the operation before
540 * returning this time (unless the operation was cancelled).
541 */
542#ifdef CURSOR_SHAPE
543 c = finish_op;
544#endif
545 finish_op = (oap->op_type != OP_NOP);
546#ifdef CURSOR_SHAPE
547 if (finish_op != c)
548 {
549 ui_cursor_shape(); /* may show different cursor shape */
550# ifdef FEAT_MOUSESHAPE
551 update_mouseshape(-1);
552# endif
553 }
554#endif
555
Bram Moolenaara983fe92008-07-31 20:04:27 +0000556 /* When not finishing an operator and no register name typed, reset the
557 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000559 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000561#ifdef FEAT_EVAL
562 set_prevcount = TRUE;
563#endif
564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaara983fe92008-07-31 20:04:27 +0000566 /* Restore counts from before receiving K_CURSORHOLD. This means after
567 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
568 * "3 * 2". */
569 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
570 {
571 ca.opcount = oap->prev_opcount;
572 ca.count0 = oap->prev_count0;
573 oap->prev_opcount = 0;
574 oap->prev_count0 = 0;
575 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000576
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578
579 State = NORMAL_BUSY;
580#ifdef USE_ON_FLY_SCROLL
581 dont_scroll = FALSE; /* allow scrolling here */
582#endif
583
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100584#ifdef FEAT_EVAL
585 /* Set v:count here, when called from main() and not a stuffed
586 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100587 * when there is no count. Do set it for redo. */
588 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100589 set_vcount_ca(&ca, &set_prevcount);
590#endif
591
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 /*
593 * Get the command character from the user.
594 */
595 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100596 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
598 /*
599 * If a mapping was started in Visual or Select mode, remember the length
600 * of the mapping. This is used below to not return to Insert mode for as
601 * long as the mapping is being executed.
602 */
603 if (restart_edit == 0)
604 old_mapped_len = 0;
605 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000606 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 old_mapped_len = typebuf_maplen();
608
609 if (c == NUL)
610 c = K_ZERO;
611
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 /*
613 * In Select mode, typed text replaces the selection.
614 */
615 if (VIsual_active
616 && VIsual_select
617 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
618 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000619 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
620 * 'insertmode' is set) fake a "d"elete command, Insert mode will
621 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000622 * Insert the typed character in the typeahead buffer, so that it can
623 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000624 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000625 if (restart_edit != 0)
626 c = 'd';
627 else
628 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000629 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200630 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
633#ifdef FEAT_CMDL_INFO
634 need_flushbuf = add_to_showcmd(c);
635#endif
636
637getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 {
640 /*
641 * Handle a count before a command and compute ca.count0.
642 * Note that '0' is a command and not the start of a count, but it's
643 * part of a count after other digits.
644 */
645 while ( (c >= '1' && c <= '9')
646 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
647 {
648 if (c == K_DEL || c == K_KDEL)
649 {
650 ca.count0 /= 10;
651#ifdef FEAT_CMDL_INFO
652 del_from_showcmd(4); /* delete the digit and ~@% */
653#endif
654 }
655 else
656 ca.count0 = ca.count0 * 10 + (c - '0');
657 if (ca.count0 < 0) /* got too large! */
658 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000659#ifdef FEAT_EVAL
660 /* Set v:count here, when called from main() and not a stuffed
661 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100662 * right after the count. Do set it for redo. */
663 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100664 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (ctrl_w)
667 {
668 ++no_mapping;
669 ++allow_keys; /* no mapping for nchar, but keys */
670 }
671 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000672 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 --no_zero_mapping;
675 if (ctrl_w)
676 {
677 --no_mapping;
678 --allow_keys;
679 }
680#ifdef FEAT_CMDL_INFO
681 need_flushbuf |= add_to_showcmd(c);
682#endif
683 }
684
685 /*
686 * If we got CTRL-W there may be a/another count
687 */
688 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
689 {
690 ctrl_w = TRUE;
691 ca.opcount = ca.count0; /* remember first count */
692 ca.count0 = 0;
693 ++no_mapping;
694 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000695 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 --no_mapping;
698 --allow_keys;
699#ifdef FEAT_CMDL_INFO
700 need_flushbuf |= add_to_showcmd(c);
701#endif
702 goto getcount; /* jump back */
703 }
704 }
705
Bram Moolenaara983fe92008-07-31 20:04:27 +0000706 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000708 /* Save the count values so that ca.opcount and ca.count0 are exactly
709 * the same when coming back here after handling K_CURSORHOLD. */
710 oap->prev_opcount = ca.opcount;
711 oap->prev_count0 = ca.count0;
712 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100713 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000714 {
715 /*
716 * If we're in the middle of an operator (including after entering a
717 * yank buffer with '"') AND we had a count before the operator, then
718 * that count overrides the current value of ca.count0.
719 * What this means effectively, is that commands like "3dw" get turned
720 * into "d3w" which makes things fall into place pretty neatly.
721 * If you give a count before AND after the operator, they are
722 * multiplied.
723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 if (ca.count0)
725 ca.count0 *= ca.opcount;
726 else
727 ca.count0 = ca.opcount;
728 }
729
730 /*
731 * Always remember the count. It will be set to zero (on the next call,
732 * above) when there is no pending operator.
733 * When called from main(), save the count for use by the "count" built-in
734 * variable.
735 */
736 ca.opcount = ca.count0;
737 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
738
739#ifdef FEAT_EVAL
740 /*
741 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100742 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100744 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000745 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746#endif
747
748 /*
749 * Find the command character in the table of commands.
750 * For CTRL-W we already got nchar when looking for a count.
751 */
752 if (ctrl_w)
753 {
754 ca.nchar = c;
755 ca.cmdchar = Ctrl_W;
756 }
757 else
758 ca.cmdchar = c;
759 idx = find_command(ca.cmdchar);
760 if (idx < 0)
761 {
762 /* Not a known command: beep. */
763 clearopbeep(oap);
764 goto normal_end;
765 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000766
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000767 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200769 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000771 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 goto normal_end;
773 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000774 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
775 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 /*
778 * In Visual/Select mode, a few keys are handled in a special way.
779 */
780 if (VIsual_active)
781 {
782 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
783 if (km_stopsel
784 && (nv_cmds[idx].cmd_flags & NV_STS)
785 && !(mod_mask & MOD_MASK_SHIFT))
786 {
787 end_visual_mode();
788 redraw_curbuf_later(INVERTED);
789 }
790
791 /* Keys that work different when 'keymodel' contains "startsel" */
792 if (km_startsel)
793 {
794 if (nv_cmds[idx].cmd_flags & NV_SS)
795 {
796 unshift_special(&ca);
797 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000798 if (idx < 0)
799 {
800 /* Just in case */
801 clearopbeep(oap);
802 goto normal_end;
803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
806 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811#ifdef FEAT_RIGHTLEFT
812 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
813 && (nv_cmds[idx].cmd_flags & NV_RL))
814 {
815 /* Invert horizontal movements and operations. Only when typed by the
816 * user directly, not when the result of a mapping or "x" translated
817 * to "dl". */
818 switch (ca.cmdchar)
819 {
820 case 'l': ca.cmdchar = 'h'; break;
821 case K_RIGHT: ca.cmdchar = K_LEFT; break;
822 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
823 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
824 case 'h': ca.cmdchar = 'l'; break;
825 case K_LEFT: ca.cmdchar = K_RIGHT; break;
826 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
827 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
828 case '>': ca.cmdchar = '<'; break;
829 case '<': ca.cmdchar = '>'; break;
830 }
831 idx = find_command(ca.cmdchar);
832 }
833#endif
834
835 /*
836 * Get an additional character if we need one.
837 */
838 if ((nv_cmds[idx].cmd_flags & NV_NCH)
839 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
840 && oap->op_type == OP_NOP)
841 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
842 || (ca.cmdchar == 'q'
843 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200844 && reg_recording == 0
845 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100847 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
849 int *cp;
850 int repl = FALSE; /* get character for replace mode */
851 int lit = FALSE; /* get extra character literally */
852 int langmap_active = FALSE; /* using :lmap mappings */
853 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100854#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 int save_smd; /* saved value of p_smd */
856#endif
857
858 ++no_mapping;
859 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000860 /* Don't generate a CursorHold event here, most commands can't handle
861 * it, e.g., nv_replace(), nv_csearch(). */
862 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 if (ca.cmdchar == 'g')
864 {
865 /*
866 * For 'g' get the next character now, so that we can check for
867 * "gr", "g'" and "g`".
868 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000869 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#ifdef FEAT_CMDL_INFO
872 need_flushbuf |= add_to_showcmd(ca.nchar);
873#endif
874 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100875 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 {
877 cp = &ca.extra_char; /* need to get a third character */
878 if (ca.nchar != 'r')
879 lit = TRUE; /* get it literally */
880 else
881 repl = TRUE; /* get it in replace mode */
882 }
883 else
884 cp = NULL; /* no third character needed */
885 }
886 else
887 {
888 if (ca.cmdchar == 'r') /* get it in replace mode */
889 repl = TRUE;
890 cp = &ca.nchar;
891 }
892 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
893
894 /*
895 * Get a second or third character.
896 */
897 if (cp != NULL)
898 {
899#ifdef CURSOR_SHAPE
900 if (repl)
901 {
902 State = REPLACE; /* pretend Replace mode */
903 ui_cursor_shape(); /* show different cursor shape */
904 }
905#endif
906 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
907 {
908 /* Allow mappings defined with ":lmap". */
909 --no_mapping;
910 --allow_keys;
911 if (repl)
912 State = LREPLACE;
913 else
914 State = LANGMAP;
915 langmap_active = TRUE;
916 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100917#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 save_smd = p_smd;
919 p_smd = FALSE; /* Don't let the IM code show the mode here */
920 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
921 im_set_active(TRUE);
922#endif
923
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000924 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
926 if (langmap_active)
927 {
928 /* Undo the decrement done above */
929 ++no_mapping;
930 ++allow_keys;
931 State = NORMAL_BUSY;
932 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100933#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (lang)
935 {
936 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
937 im_save_status(&curbuf->b_p_iminsert);
938 im_set_active(FALSE);
939 }
940 p_smd = save_smd;
941#endif
942#ifdef CURSOR_SHAPE
943 State = NORMAL_BUSY;
944#endif
945#ifdef FEAT_CMDL_INFO
946 need_flushbuf |= add_to_showcmd(*cp);
947#endif
948
949 if (!lit)
950 {
951#ifdef FEAT_DIGRAPHS
952 /* Typing CTRL-K gets a digraph. */
953 if (*cp == Ctrl_K
954 && ((nv_cmds[idx].cmd_flags & NV_LANG)
955 || cp == &ca.extra_char)
956 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
957 {
958 c = get_digraph(FALSE);
959 if (c > 0)
960 {
961 *cp = c;
962# ifdef FEAT_CMDL_INFO
963 /* Guessing how to update showcmd here... */
964 del_from_showcmd(3);
965 need_flushbuf |= add_to_showcmd(*cp);
966# endif
967 }
968 }
969#endif
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 /* adjust chars > 127, except after "tTfFr" commands */
972 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973#ifdef FEAT_RIGHTLEFT
974 /* adjust Hebrew mapped char */
975 if (p_hkmap && lang && KeyTyped)
976 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977#endif
978 }
979
980 /*
981 * When the next character is CTRL-\ a following CTRL-N means the
982 * command is aborted and we go to Normal mode.
983 */
984 if (cp == &ca.extra_char
985 && ca.nchar == Ctrl_BSL
986 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
987 {
988 ca.cmdchar = Ctrl_BSL;
989 ca.nchar = ca.extra_char;
990 idx = find_command(ca.cmdchar);
991 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200992 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200993 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 else if (*cp == Ctrl_BSL)
995 {
996 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
997
998 /* There is a busy wait here when typing "f<C-\>" and then
999 * something different from CTRL-N. Can't be avoided. */
1000 while ((c = vpeekc()) <= 0 && towait > 0L)
1001 {
1002 do_sleep(towait > 50L ? 50L : towait);
1003 towait -= 50L;
1004 }
1005 if (c > 0)
1006 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001007 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (c != Ctrl_N && c != Ctrl_G)
1009 vungetc(c);
1010 else
1011 {
1012 ca.cmdchar = Ctrl_BSL;
1013 ca.nchar = c;
1014 idx = find_command(ca.cmdchar);
1015 }
1016 }
1017 }
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 /* When getting a text character and the next character is a
1020 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001021 * However, don't wait for it to arrive. Also, do enable mapping,
1022 * because if it's put back with vungetc() it's too late to apply
1023 * mapping. */
1024 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 while (enc_utf8 && lang && (c = vpeekc()) > 0
1026 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1027 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001028 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if (!utf_iscomposing(c))
1030 {
1031 vungetc(c); /* it wasn't, put it back */
1032 break;
1033 }
1034 else if (ca.ncharC1 == 0)
1035 ca.ncharC1 = c;
1036 else
1037 ca.ncharC2 = c;
1038 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001039 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041 --no_mapping;
1042 --allow_keys;
1043 }
1044
1045#ifdef FEAT_CMDL_INFO
1046 /*
1047 * Flush the showcmd characters onto the screen so we can see them while
1048 * the command is being executed. Only do this when the shown command was
1049 * actually displayed, otherwise this will slow down a lot when executing
1050 * mappings.
1051 */
1052 if (need_flushbuf)
1053 out_flush();
1054#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001055 if (ca.cmdchar != K_IGNORE)
1056 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
1058 State = NORMAL;
1059
1060 if (ca.nchar == ESC)
1061 {
1062 clearop(oap);
1063 if (restart_edit == 0 && goto_im())
1064 restart_edit = 'a';
1065 goto normal_end;
1066 }
1067
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001068 if (ca.cmdchar != K_IGNORE)
1069 {
1070 msg_didout = FALSE; /* don't scroll screen up for normal command */
1071 msg_col = 0;
1072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 old_pos = curwin->w_cursor; /* remember where cursor was */
1075
1076 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1077 * mode. */
1078 if (!VIsual_active && km_startsel)
1079 {
1080 if (nv_cmds[idx].cmd_flags & NV_SS)
1081 {
1082 start_selection();
1083 unshift_special(&ca);
1084 idx = find_command(ca.cmdchar);
1085 }
1086 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1087 && (mod_mask & MOD_MASK_SHIFT))
1088 {
1089 start_selection();
1090 mod_mask &= ~MOD_MASK_SHIFT;
1091 }
1092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
1094 /*
1095 * Execute the command!
1096 * Call the command function found in the commands table.
1097 */
1098 ca.arg = nv_cmds[idx].cmd_arg;
1099 (nv_cmds[idx].cmd_func)(&ca);
1100
1101 /*
1102 * If we didn't start or finish an operator, reset oap->regname, unless we
1103 * need it later.
1104 */
1105 if (!finish_op
1106 && !oap->op_type
1107 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1108 {
1109 clearop(oap);
1110#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001111 {
1112 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001113
Bram Moolenaar536681b2011-05-10 16:12:45 +02001114 /* Adjust the register according to 'clipboard', so that when
1115 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001116# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001117 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001118# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001119 set_reg_var(regname);
1120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121#endif
1122 }
1123
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001124 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001125 * character or "z333<cr>". */
1126 if (old_mapped_len > 0)
1127 old_mapped_len = typebuf_maplen();
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 /*
1130 * If an operation is pending, handle it...
1131 */
1132 do_pending_operator(&ca, old_col, FALSE);
1133
1134 /*
1135 * Wait for a moment when a message is displayed that will be overwritten
1136 * by the mode message.
1137 * In Visual mode and with "^O" in Insert mode, a short message will be
1138 * overwritten by the mode message. Wait a bit, until a key is hit.
1139 * In Visual mode, it's more important to keep the Visual area updated
1140 * than keeping a message (e.g. from a /pat search).
1141 * Only do this if the command was typed, not from a mapping.
1142 * Don't wait when emsg_silent is non-zero.
1143 * Also wait a bit after an error message, e.g. for "^O:".
1144 * Don't redraw the screen, it would remove the message.
1145 */
1146 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001147 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 || (VIsual_active
1150 && old_pos.lnum == curwin->w_cursor.lnum
1151 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 )
1153 && (clear_cmdline
1154 || redraw_cmdline)
1155 && (msg_didout || (msg_didany && msg_scroll))
1156 && !msg_nowait
1157 && KeyTyped)
1158 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 && (msg_scroll
1161 || emsg_on_display)))
1162 && oap->regname == 0
1163 && !(ca.retval & CA_COMMAND_BUSY)
1164 && stuff_empty()
1165 && typebuf_typed()
1166 && emsg_silent == 0
1167 && !did_wait_return
1168 && oap->op_type == OP_NOP)
1169 {
1170 int save_State = State;
1171
1172 /* Draw the cursor with the right shape here */
1173 if (restart_edit != 0)
1174 State = INSERT;
1175
1176 /* If need to redraw, and there is a "keep_msg", redraw before the
1177 * delay */
1178 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1179 {
1180 char_u *kmsg;
1181
1182 kmsg = keep_msg;
1183 keep_msg = NULL;
1184 /* showmode() will clear keep_msg, but we want to use it anyway */
1185 update_screen(0);
1186 /* now reset it, otherwise it's put in the history again */
1187 keep_msg = kmsg;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001188 msg_attr((char *)kmsg, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 vim_free(kmsg);
1190 }
1191 setcursor();
1192 cursor_on();
1193 out_flush();
1194 if (msg_scroll || emsg_on_display)
1195 ui_delay(1000L, TRUE); /* wait at least one second */
1196 ui_delay(3000L, FALSE); /* wait up to three seconds */
1197 State = save_State;
1198
1199 msg_scroll = FALSE;
1200 emsg_on_display = FALSE;
1201 }
1202
1203 /*
1204 * Finish up after executing a Normal mode command.
1205 */
1206normal_end:
1207
1208 msg_nowait = FALSE;
1209
1210 /* Reset finish_op, in case it was set */
1211#ifdef CURSOR_SHAPE
1212 c = finish_op;
1213#endif
1214 finish_op = FALSE;
1215#ifdef CURSOR_SHAPE
1216 /* Redraw the cursor with another shape, if we were in Operator-pending
1217 * mode or did a replace command. */
1218 if (c || ca.cmdchar == 'r')
1219 {
1220 ui_cursor_shape(); /* may show different cursor shape */
1221# ifdef FEAT_MOUSESHAPE
1222 update_mouseshape(-1);
1223# endif
1224 }
1225#endif
1226
1227#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001228 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001229 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 clear_showcmd();
1231#endif
1232
1233 checkpcmark(); /* check if we moved since setting pcmark */
1234 vim_free(ca.searchbuf);
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (has_mbyte)
1237 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (curwin->w_p_scb && toplevel)
1240 {
1241 validate_cursor(); /* may need to update w_leftcol */
1242 do_check_scrollbind(TRUE);
1243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaar860cae12010-06-05 23:22:07 +02001245 if (curwin->w_p_crb && toplevel)
1246 {
1247 validate_cursor(); /* may need to update w_leftcol */
1248 do_check_cursorbind();
1249 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001250
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001251#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001252 /* don't go to Insert mode if a terminal has a running job */
1253 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001254 restart_edit = 0;
1255#endif
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 /*
1258 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1259 * if still inside a mapping that started in Visual mode).
1260 * May switch from Visual to Select mode after CTRL-O command.
1261 */
1262 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1264 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 && !(ca.retval & CA_COMMAND_BUSY)
1266 && stuff_empty()
1267 && oap->regname == 0)
1268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (restart_VIsual_select == 1)
1270 {
1271 VIsual_select = TRUE;
1272 showmode();
1273 restart_VIsual_select = 0;
1274 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001275 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 (void)edit(restart_edit, FALSE, 1L);
1277 }
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (restart_VIsual_select == 2)
1280 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282 /* Save count before an operator for next time. */
1283 opcount = ca.opcount;
1284}
1285
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001286#ifdef FEAT_EVAL
1287/*
1288 * Set v:count and v:count1 according to "cap".
1289 * Set v:prevcount only when "set_prevcount" is TRUE.
1290 */
1291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001292set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001293{
1294 long count = cap->count0;
1295
1296 /* multiply with cap->opcount the same way as above */
1297 if (cap->opcount != 0)
1298 count = cap->opcount * (count == 0 ? 1 : count);
1299 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1300 *set_prevcount = FALSE; /* only set v:prevcount once */
1301}
1302#endif
1303
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304/*
Bram Moolenaar5823f842019-01-03 22:58:08 +01001305 * Handle an operator after Visual mode or when the movement is finished.
1306 * "gui_yank" is true when yanking text for the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 */
1308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001309do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 oparg_T *oap = cap->oap;
1312 pos_T old_cursor;
1313 int empty_region_error;
1314 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001315#ifdef FEAT_LINEBREAK
1316 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 /* The visual area is remembered for redo */
1320 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1321 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001322 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001324 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327#if defined(FEAT_CLIPBOARD)
1328 /*
1329 * Yank the visual area into the GUI selection register before we operate
1330 * on it and lose it forever.
1331 * Don't do it if a specific register was specified, so that ""x"*P works.
1332 * This could call do_pending_operator() recursively, but that's OK
1333 * because gui_yank will be TRUE for the nested call.
1334 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001335 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 && oap->op_type != OP_NOP
1337 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 && VIsual_active
1339 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && oap->regname == 0)
1341 clip_auto_select();
1342#endif
1343 old_cursor = curwin->w_cursor;
1344
1345 /*
1346 * If an operation is pending, handle it...
1347 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001348 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 {
Bram Moolenaar5823f842019-01-03 22:58:08 +01001350 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
1351 // for the clipboard.
1352 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
1353
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001354#ifdef FEAT_LINEBREAK
1355 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001356 if (curwin->w_p_lbr)
1357 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001358 curwin->w_p_lbr = FALSE;
1359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 oap->is_VIsual = VIsual_active;
1361 if (oap->motion_force == 'V')
1362 oap->motion_type = MLINE;
1363 else if (oap->motion_force == 'v')
1364 {
1365 /* If the motion was linewise, "inclusive" will not have been set.
1366 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1367 if (oap->motion_type == MLINE)
1368 oap->inclusive = FALSE;
1369 /* If the motion already was characterwise, toggle "inclusive" */
1370 else if (oap->motion_type == MCHAR)
1371 oap->inclusive = !oap->inclusive;
1372 oap->motion_type = MCHAR;
1373 }
1374 else if (oap->motion_force == Ctrl_V)
1375 {
1376 /* Change line- or characterwise motion into Visual block mode. */
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01001377 if (!VIsual_active)
1378 {
1379 VIsual_active = TRUE;
1380 VIsual = oap->start;
1381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 VIsual_mode = Ctrl_V;
1383 VIsual_select = FALSE;
1384 VIsual_reselect = FALSE;
1385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaar7afea822013-04-24 18:34:45 +02001387 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1388 /* Never redo "zf" (define fold). */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001389 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001390 && ((!VIsual_active || oap->motion_force)
1391 /* Also redo Operator-pending Visual mode mappings */
1392 || (VIsual_active && cap->cmdchar == ':'
1393 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001394 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#ifdef FEAT_FOLDING
1396 && oap->op_type != OP_FOLD
1397 && oap->op_type != OP_FOLDOPEN
1398 && oap->op_type != OP_FOLDOPENREC
1399 && oap->op_type != OP_FOLDCLOSE
1400 && oap->op_type != OP_FOLDCLOSEREC
1401 && oap->op_type != OP_FOLDDEL
1402 && oap->op_type != OP_FOLDDELREC
1403#endif
1404 )
1405 {
1406 prep_redo(oap->regname, cap->count0,
1407 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1408 oap->motion_force, cap->cmdchar, cap->nchar);
1409 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1410 {
1411 /*
1412 * If 'cpoptions' does not contain 'r', insert the search
1413 * pattern to really repeat the same command.
1414 */
1415 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001416 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 AppendToRedobuff(NL_STR);
1418 }
1419 else if (cap->cmdchar == ':')
1420 {
1421 /* do_cmdline() has stored the first typed line in
1422 * "repeat_cmdline". When several lines are typed repeating
1423 * won't be possible. */
1424 if (repeat_cmdline == NULL)
1425 ResetRedobuff();
1426 else
1427 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001428 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001430 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 }
1432 }
1433 }
1434
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (redo_VIsual_busy)
1436 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001437 /* Redo of an operation on a Visual area. Use the same size from
1438 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 oap->start = curwin->w_cursor;
1440 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1441 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1442 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1443 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001444 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001446 if (VIsual_mode == 'v')
1447 {
1448 if (redo_VIsual_line_count <= 1)
1449 {
1450 validate_virtcol();
1451 curwin->w_curswant =
1452 curwin->w_virtcol + redo_VIsual_vcol - 1;
1453 }
1454 else
1455 curwin->w_curswant = redo_VIsual_vcol;
1456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001458 {
1459 curwin->w_curswant = MAXCOL;
1460 }
1461 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463 cap->count0 = redo_VIsual_count;
1464 if (redo_VIsual_count != 0)
1465 cap->count1 = redo_VIsual_count;
1466 else
1467 cap->count1 = 1;
1468 }
1469 else if (VIsual_active)
1470 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001471 if (!gui_yank)
1472 {
1473 /* Save the current VIsual area for '< and '> marks, and "gv" */
1474 curbuf->b_visual.vi_start = VIsual;
1475 curbuf->b_visual.vi_end = curwin->w_cursor;
1476 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001477 if (VIsual_mode_orig != NUL)
1478 {
1479 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1480 VIsual_mode_orig = NUL;
1481 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001482 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001484 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
1488 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001489 * characterwise selection.
1490 * Special case: gH<Del> deletes the last line. */
1491 if (VIsual_select && VIsual_mode == 'V'
1492 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001494 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 VIsual.col = 0;
1497 curwin->w_cursor.col =
1498 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1499 }
1500 else
1501 {
1502 curwin->w_cursor.col = 0;
1503 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1504 }
1505 VIsual_mode = 'v';
1506 }
1507 /* If 'selection' is "exclusive", backup one character for
1508 * charwise selections. */
1509 else if (VIsual_mode == 'v')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001510 include_line_break = unadjust_for_sel();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511
1512 oap->start = VIsual;
1513 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001516 oap->start.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
1520 /*
1521 * Set oap->start to the first position of the operated text, oap->end
1522 * to the end of the operated text. w_cursor is equal to oap->start.
1523 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001524 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526#ifdef FEAT_FOLDING
1527 /* Include folded lines completely. */
1528 if (!VIsual_active)
1529 {
1530 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1531 oap->start.col = 0;
1532 if (hasFolding(curwin->w_cursor.lnum, NULL,
1533 &curwin->w_cursor.lnum))
1534 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1535 }
1536#endif
1537 oap->end = curwin->w_cursor;
1538 curwin->w_cursor = oap->start;
1539
1540 /* w_virtcol may have been updated; if the cursor goes back to its
1541 * previous position w_virtcol becomes invalid and isn't updated
1542 * automatically. */
1543 curwin->w_valid &= ~VALID_VIRTCOL;
1544 }
1545 else
1546 {
1547#ifdef FEAT_FOLDING
1548 /* Include folded lines completely. */
1549 if (!VIsual_active && oap->motion_type == MLINE)
1550 {
1551 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1552 NULL))
1553 curwin->w_cursor.col = 0;
1554 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1555 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1556 }
1557#endif
1558 oap->end = oap->start;
1559 oap->start = curwin->w_cursor;
1560 }
1561
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001562 /* Just in case lines were deleted that make the position invalid. */
1563 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 /* Set "virtual_op" before resetting VIsual_active. */
1567 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (VIsual_active || redo_VIsual_busy)
1570 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001571 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
1573 if (!redo_VIsual_busy && !gui_yank)
1574 {
1575 /*
1576 * Prepare to reselect and redo Visual: this is based on the
1577 * size of the Visual text
1578 */
1579 resel_VIsual_mode = VIsual_mode;
1580 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001581 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001583 {
1584 if (VIsual_mode != Ctrl_V)
1585 getvvcol(curwin, &(oap->end),
1586 NULL, NULL, &oap->end_vcol);
1587 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1588 {
1589 if (VIsual_mode != Ctrl_V)
1590 getvvcol(curwin, &(oap->start),
1591 &oap->start_vcol, NULL, NULL);
1592 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1593 }
1594 else
1595 resel_VIsual_vcol = oap->end_vcol;
1596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 resel_VIsual_line_count = oap->line_count;
1598 }
1599
1600 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001601 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 && oap->op_type != OP_COLON
1603#ifdef FEAT_FOLDING
1604 && oap->op_type != OP_FOLD
1605 && oap->op_type != OP_FOLDOPEN
1606 && oap->op_type != OP_FOLDOPENREC
1607 && oap->op_type != OP_FOLDCLOSE
1608 && oap->op_type != OP_FOLDCLOSEREC
1609 && oap->op_type != OP_FOLDDEL
1610 && oap->op_type != OP_FOLDDELREC
1611#endif
1612 && oap->motion_force == NUL
1613 )
1614 {
1615 /* Prepare for redoing. Only use the nchar field for "r",
1616 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001617 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1618 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001619 prep_redo(oap->regname, cap->count0,
1620 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1621 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001622 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001623 {
1624 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1625
1626 /* reverse what nv_replace() did */
1627 if (nchar == REPLACE_CR_NCHAR)
1628 nchar = CAR;
1629 else if (nchar == REPLACE_NL_NCHAR)
1630 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001631 prep_redo(oap->regname, 0L, NUL, 'v',
1632 get_op_char(oap->op_type),
1633 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001634 nchar);
1635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (!redo_VIsual_busy)
1637 {
1638 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001639 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 redo_VIsual_line_count = resel_VIsual_line_count;
1641 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001642 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 }
1644 }
1645
1646 /*
1647 * oap->inclusive defaults to TRUE.
1648 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1649 * FALSE. This makes "d}P" and "v}dP" work the same.
1650 */
1651 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1652 oap->inclusive = TRUE;
1653 if (VIsual_mode == 'V')
1654 oap->motion_type = MLINE;
1655 else
1656 {
1657 oap->motion_type = MCHAR;
1658 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001659 && (include_line_break || !virtual_op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
1661 oap->inclusive = FALSE;
1662 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001663 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001664 if (*p_sel != 'o'
1665 && !op_on_lines(oap->op_type)
1666 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001668 ++oap->end.lnum;
1669 oap->end.col = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001670 oap->end.coladd = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001671 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 }
1673 }
1674 }
1675
1676 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001677
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 /*
1679 * Switch Visual off now, so screen updating does
1680 * not show inverted text when the screen is redrawn.
1681 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1682 * no screen redraw, so it is done here to remove the inverted
1683 * part.
1684 */
1685 if (!gui_yank)
1686 {
1687 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001688#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 setmouse();
1690 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001691#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001692 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if ((oap->op_type == OP_YANK
1694 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001695 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 || oap->op_type == OP_FILTER)
1697 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001698 {
1699#ifdef FEAT_LINEBREAK
1700 /* make sure redrawing is correct */
1701 curwin->w_p_lbr = lbr_saved;
1702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
1706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 /* Include the trailing byte of a multi-byte char. */
1709 if (has_mbyte && oap->inclusive)
1710 {
1711 int l;
1712
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001713 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (l > 1)
1715 oap->end.col += l - 1;
1716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 curwin->w_set_curswant = TRUE;
1718
1719 /*
1720 * oap->empty is set when start and end are the same. The inclusive
1721 * flag affects this too, unless yanking and the end is on a NUL.
1722 */
1723 oap->empty = (oap->motion_type == MCHAR
1724 && (!oap->inclusive
1725 || (oap->op_type == OP_YANK
1726 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001727 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001728 && !(virtual_op && oap->start.coladd != oap->end.coladd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /*
1730 * For delete, change and yank, it's an error to operate on an
1731 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1732 */
1733 empty_region_error = (oap->empty
1734 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1735
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 /* Force a redraw when operating on an empty Visual region, when
1737 * 'modifiable is off or creating a fold. */
1738 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001739#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001743 {
1744#ifdef FEAT_LINEBREAK
1745 curwin->w_p_lbr = lbr_saved;
1746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
1750 /*
1751 * If the end of an operator is in column one while oap->motion_type
1752 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1753 * character in the previous line. If op_start is on or before the
1754 * first non-blank in the line, the operator becomes linewise
1755 * (strange, but that's the way vi does it).
1756 */
1757 if ( oap->motion_type == MCHAR
1758 && oap->inclusive == FALSE
1759 && !(cap->retval & CA_NO_ADJ_OP_END)
1760 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001762 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 && oap->line_count > 1)
1764 {
1765 oap->end_adjusted = TRUE; /* remember that we did this */
1766 --oap->line_count;
1767 --oap->end.lnum;
1768 if (inindent(0))
1769 oap->motion_type = MLINE;
1770 else
1771 {
1772 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1773 if (oap->end.col)
1774 {
1775 --oap->end.col;
1776 oap->inclusive = TRUE;
1777 }
1778 }
1779 }
1780 else
1781 oap->end_adjusted = FALSE;
1782
1783 switch (oap->op_type)
1784 {
1785 case OP_LSHIFT:
1786 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001787 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 auto_format(FALSE, TRUE);
1789 break;
1790
1791 case OP_JOIN_NS:
1792 case OP_JOIN:
1793 if (oap->line_count < 2)
1794 oap->line_count = 2;
1795 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1796 curbuf->b_ml.ml_line_count)
1797 beep_flush();
1798 else
1799 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001800 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001801 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 auto_format(FALSE, TRUE);
1803 }
1804 break;
1805
1806 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001809 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001810 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001811 CancelRedo();
1812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 else
1814 {
1815 (void)op_delete(oap);
1816 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1817 u_save_cursor(); /* cursor line wasn't saved yet */
1818 auto_format(FALSE, TRUE);
1819 }
1820 break;
1821
1822 case OP_YANK:
1823 if (empty_region_error)
1824 {
1825 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001826 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001827 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001828 CancelRedo();
1829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 }
1831 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001832 {
1833#ifdef FEAT_LINEBREAK
1834 curwin->w_p_lbr = lbr_saved;
1835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 check_cursor_col();
1839 break;
1840
1841 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001844 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001845 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001846 CancelRedo();
1847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 else
1849 {
1850 /* This is a new edit command, not a restart. Need to
1851 * remember it to make 'insertmode' work with mappings for
1852 * Visual mode. But do this only once and not when typed and
1853 * 'insertmode' isn't set. */
1854 if (p_im || !KeyTyped)
1855 restart_edit_save = restart_edit;
1856 else
1857 restart_edit_save = 0;
1858 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001859#ifdef FEAT_LINEBREAK
1860 /* Restore linebreak, so that when the user edits it looks as
1861 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001862 if (curwin->w_p_lbr != lbr_saved)
1863 {
1864 curwin->w_p_lbr = lbr_saved;
1865 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1866 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 /* Reset finish_op now, don't want it set inside edit(). */
1869 finish_op = FALSE;
1870 if (op_change(oap)) /* will call edit() */
1871 cap->retval |= CA_COMMAND_BUSY;
1872 if (restart_edit == 0)
1873 restart_edit = restart_edit_save;
1874 }
1875 break;
1876
1877 case OP_FILTER:
1878 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1879 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1880 else
1881 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001882 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 case OP_INDENT:
1885 case OP_COLON:
1886
1887#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1888 /*
1889 * If 'equalprg' is empty, do the indenting internally.
1890 */
1891 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1892 {
1893# ifdef FEAT_LISP
1894 if (curbuf->b_p_lisp)
1895 {
1896 op_reindent(oap, get_lisp_indent);
1897 break;
1898 }
1899# endif
1900# ifdef FEAT_CINDENT
1901 op_reindent(oap,
1902# ifdef FEAT_EVAL
1903 *curbuf->b_p_inde != NUL ? get_expr_indent :
1904# endif
1905 get_c_indent);
1906 break;
1907# endif
1908 }
1909#endif
1910
1911 op_colon(oap);
1912 break;
1913
1914 case OP_TILDE:
1915 case OP_UPPER:
1916 case OP_LOWER:
1917 case OP_ROT13:
1918 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001919 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001920 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001921 CancelRedo();
1922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 op_tilde(oap);
1925 check_cursor_col();
1926 break;
1927
1928 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001929#if defined(FEAT_EVAL)
1930 if (*curbuf->b_p_fex != NUL)
1931 op_formatexpr(oap); /* use expression */
1932 else
1933#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001934 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 op_colon(oap); /* use external command */
1936 else
1937 op_format(oap, FALSE); /* use internal function */
1938 break;
1939
1940 case OP_FORMAT2:
1941 op_format(oap, TRUE); /* use internal function */
1942 break;
1943
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001944 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001945#ifdef FEAT_LINEBREAK
1946 /* Restore linebreak, so that when the user edits it looks as
1947 * before. */
1948 curwin->w_p_lbr = lbr_saved;
1949#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001950 op_function(oap); /* call 'operatorfunc' */
1951 break;
1952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 case OP_INSERT:
1954 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001957 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001958 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001959 CancelRedo();
1960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 else
1962 {
1963 /* This is a new edit command, not a restart. Need to
1964 * remember it to make 'insertmode' work with mappings for
1965 * Visual mode. But do this only once. */
1966 restart_edit_save = restart_edit;
1967 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001968#ifdef FEAT_LINEBREAK
1969 /* Restore linebreak, so that when the user edits it looks as
1970 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001971 if (curwin->w_p_lbr != lbr_saved)
1972 {
1973 curwin->w_p_lbr = lbr_saved;
1974 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1975 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001978#ifdef FEAT_LINEBREAK
1979 /* Reset linebreak, so that formatting works correctly. */
1980 curwin->w_p_lbr = FALSE;
1981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
1983 /* TODO: when inserting in several lines, should format all
1984 * the lines. */
1985 auto_format(FALSE, TRUE);
1986
1987 if (restart_edit == 0)
1988 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01001989 else
1990 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 break;
1993
1994 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001997 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001998 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001999 CancelRedo();
2000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002002 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002003#ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002004 /* Restore linebreak, so that when the user edits it looks as
2005 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002006 if (curwin->w_p_lbr != lbr_saved)
2007 {
2008 curwin->w_p_lbr = lbr_saved;
2009 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2010 }
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 break;
2015
2016#ifdef FEAT_FOLDING
2017 case OP_FOLD:
2018 VIsual_reselect = FALSE; /* don't reselect now */
2019 foldCreate(oap->start.lnum, oap->end.lnum);
2020 break;
2021
2022 case OP_FOLDOPEN:
2023 case OP_FOLDOPENREC:
2024 case OP_FOLDCLOSE:
2025 case OP_FOLDCLOSEREC:
2026 VIsual_reselect = FALSE; /* don't reselect now */
2027 opFoldRange(oap->start.lnum, oap->end.lnum,
2028 oap->op_type == OP_FOLDOPEN
2029 || oap->op_type == OP_FOLDOPENREC,
2030 oap->op_type == OP_FOLDOPENREC
2031 || oap->op_type == OP_FOLDCLOSEREC,
2032 oap->is_VIsual);
2033 break;
2034
2035 case OP_FOLDDEL:
2036 case OP_FOLDDELREC:
2037 VIsual_reselect = FALSE; /* don't reselect now */
2038 deleteFold(oap->start.lnum, oap->end.lnum,
2039 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2040 break;
2041#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002042 case OP_NR_ADD:
2043 case OP_NR_SUB:
2044 if (empty_region_error)
2045 {
2046 vim_beep(BO_OPER);
2047 CancelRedo();
2048 }
2049 else
2050 {
2051 VIsual_active = TRUE;
2052#ifdef FEAT_LINEBREAK
2053 curwin->w_p_lbr = lbr_saved;
2054#endif
2055 op_addsub(oap, cap->count1, redo_VIsual_arg);
2056 VIsual_active = FALSE;
2057 }
2058 check_cursor_col();
2059 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 default:
2061 clearopbeep(oap);
2062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 virtual_op = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (!gui_yank)
2065 {
2066 /*
2067 * if 'sol' not set, go back to old column for some commands
2068 */
2069 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2070 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2071 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002072 {
2073#ifdef FEAT_LINEBREAK
2074 curwin->w_p_lbr = FALSE;
2075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079 else
2080 {
2081 curwin->w_cursor = old_cursor;
2082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 clearop(oap);
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01002085 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002087#ifdef FEAT_LINEBREAK
2088 curwin->w_p_lbr = lbr_saved;
2089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090}
2091
2092/*
2093 * Handle indent and format operators and visual mode ":".
2094 */
2095 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002096op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (oap->is_VIsual)
2100 stuffReadbuff((char_u *)"'<,'>");
2101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
2103 /*
2104 * Make the range look nice, so it can be repeated.
2105 */
2106 if (oap->start.lnum == curwin->w_cursor.lnum)
2107 stuffcharReadbuff('.');
2108 else
2109 stuffnumReadbuff((long)oap->start.lnum);
2110 if (oap->end.lnum != oap->start.lnum)
2111 {
2112 stuffcharReadbuff(',');
2113 if (oap->end.lnum == curwin->w_cursor.lnum)
2114 stuffcharReadbuff('.');
2115 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2116 stuffcharReadbuff('$');
2117 else if (oap->start.lnum == curwin->w_cursor.lnum)
2118 {
2119 stuffReadbuff((char_u *)".+");
2120 stuffnumReadbuff((long)oap->line_count - 1);
2121 }
2122 else
2123 stuffnumReadbuff((long)oap->end.lnum);
2124 }
2125 }
2126 if (oap->op_type != OP_COLON)
2127 stuffReadbuff((char_u *)"!");
2128 if (oap->op_type == OP_INDENT)
2129 {
2130#ifndef FEAT_CINDENT
2131 if (*get_equalprg() == NUL)
2132 stuffReadbuff((char_u *)"indent");
2133 else
2134#endif
2135 stuffReadbuff(get_equalprg());
2136 stuffReadbuff((char_u *)"\n");
2137 }
2138 else if (oap->op_type == OP_FORMAT)
2139 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002140 if (*curbuf->b_p_fp != NUL)
2141 stuffReadbuff(curbuf->b_p_fp);
2142 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002144 else
2145 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002146 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
2148
2149 /*
2150 * do_cmdline() does the rest
2151 */
2152}
2153
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002154/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002155 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002156 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002158op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002159{
2160#ifdef FEAT_EVAL
Bram Moolenaarffa96842018-06-12 22:05:14 +02002161 typval_T argv[2];
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002162 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002163
2164 if (*p_opfunc == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002165 emsg(_("E774: 'operatorfunc' is empty"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002166 else
2167 {
2168 /* Set '[ and '] marks to text to be operated on. */
2169 curbuf->b_op_start = oap->start;
2170 curbuf->b_op_end = oap->end;
2171 if (oap->motion_type != MLINE && !oap->inclusive)
2172 /* Exclude the end position. */
2173 decl(&curbuf->b_op_end);
2174
Bram Moolenaarffa96842018-06-12 22:05:14 +02002175 argv[0].v_type = VAR_STRING;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002176 if (oap->block_mode)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002177 argv[0].vval.v_string = (char_u *)"block";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002178 else if (oap->motion_type == MLINE)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002179 argv[0].vval.v_string = (char_u *)"line";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002180 else
Bram Moolenaarffa96842018-06-12 22:05:14 +02002181 argv[0].vval.v_string = (char_u *)"char";
2182 argv[1].v_type = VAR_UNKNOWN;
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002183
2184 /* Reset virtual_op so that 'virtualedit' can be changed in the
2185 * function. */
2186 virtual_op = MAYBE;
2187
Bram Moolenaarded27a12018-08-01 19:06:03 +02002188 (void)call_func_retnr(p_opfunc, 1, argv);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002189
2190 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002191 }
2192#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002193 emsg(_("E775: Eval feature not available"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002194#endif
2195}
2196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197#if defined(FEAT_MOUSE) || defined(PROTO)
2198/*
2199 * Do the appropriate action for the current mouse click in the current mode.
2200 * Not used for Command-line mode.
2201 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002202 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 * event modi- position visual change action
2204 * fier cursor window
2205 * left press - yes end yes
2206 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002207 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 * left drag - yes start if moved no
2209 * left relse - yes start if moved no
2210 * middle press - yes if not active no put register
2211 * middle press - yes if active no yank and put
2212 * right press - yes start or extend yes
2213 * right press S yes no change yes "#" (2)
2214 * right drag - yes extend no
2215 * right relse - yes extend no
2216 *
2217 * Insert or Replace Mode:
2218 * event modi- position visual change action
2219 * fier cursor window
2220 * left press - yes (cannot be active) yes
2221 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2222 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2223 * left drag - yes start or extend (1) no CTRL-O (1)
2224 * left relse - yes start or extend (1) no CTRL-O (1)
2225 * middle press - no (cannot be active) no put register
2226 * right press - yes start or extend yes CTRL-O
2227 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2228 *
2229 * (1) only if mouse pointer moved since press
2230 * (2) only if click is in same buffer
2231 *
2232 * Return TRUE if start_arrow() should be called for edit mode.
2233 */
2234 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002235do_mouse(
2236 oparg_T *oap, /* operator argument, can be NULL */
2237 int c, /* K_LEFTMOUSE, etc */
2238 int dir, /* Direction to 'put' if necessary */
2239 long count,
2240 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2243 static int got_click = FALSE; /* got a click some time back */
2244
2245 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2246 int is_click; /* If FALSE it's a drag or release event */
2247 int is_drag; /* If TRUE it's a drag event */
2248 int jump_flags = 0; /* flags for jump_to_mouse() */
2249 pos_T start_visual;
2250 int moved; /* Has cursor moved? */
2251 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002252 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 int c1, c2;
2255#if defined(FEAT_FOLDING)
2256 pos_T save_cursor;
2257#endif
2258 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 static pos_T orig_cursor;
2260 colnr_T leftcol, rightcol;
2261 pos_T end_visual;
2262 int diff;
2263 int old_active = VIsual_active;
2264 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 int regname;
2266
2267#if defined(FEAT_FOLDING)
2268 save_cursor = curwin->w_cursor;
2269#endif
2270
2271 /*
2272 * When GUI is active, always recognize mouse events, otherwise:
2273 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2274 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2275 * - For command line and insert mode 'mouse' is checked before calling
2276 * do_mouse().
2277 */
2278 if (do_always)
2279 do_always = FALSE;
2280 else
2281#ifdef FEAT_GUI
2282 if (!gui.in_use)
2283#endif
2284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (VIsual_active)
2286 {
2287 if (!mouse_has(MOUSE_VISUAL))
2288 return FALSE;
2289 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002290 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 return FALSE;
2292 }
2293
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002294 for (;;)
2295 {
2296 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2297 if (is_drag)
2298 {
2299 /* If the next character is the same mouse event then use that
2300 * one. Speeds up dragging the status line. */
2301 if (vpeekc() != NUL)
2302 {
2303 int nc;
2304 int save_mouse_row = mouse_row;
2305 int save_mouse_col = mouse_col;
2306
2307 /* Need to get the character, peeking doesn't get the actual
2308 * one. */
2309 nc = safe_vgetc();
2310 if (c == nc)
2311 continue;
2312 vungetc(nc);
2313 mouse_row = save_mouse_row;
2314 mouse_col = save_mouse_col;
2315 }
2316 }
2317 break;
2318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002320 if (c == K_MOUSEMOVE)
2321 {
Bram Moolenaar06bd8242019-05-08 22:55:16 +02002322 // Mouse moved without a button pressed.
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002323#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002324 ui_may_remove_balloon();
Bram Moolenaar06bd8242019-05-08 22:55:16 +02002325 if (p_bevalterm)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002326 {
2327 profile_setlimit(p_bdlay, &bevalexpr_due);
2328 bevalexpr_due_set = TRUE;
2329 }
2330#endif
2331 return FALSE;
2332 }
2333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#ifdef FEAT_MOUSESHAPE
2335 /* May have stopped dragging the status or separator line. The pointer is
2336 * most likely still on the status or separator line. */
2337 if (!is_drag && drag_status_line)
2338 {
2339 drag_status_line = FALSE;
2340 update_mouseshape(SHAPE_IDX_STATUS);
2341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (!is_drag && drag_sep_line)
2343 {
2344 drag_sep_line = FALSE;
2345 update_mouseshape(SHAPE_IDX_VSEP);
2346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347#endif
2348
2349 /*
2350 * Ignore drag and release events if we didn't get a click.
2351 */
2352 if (is_click)
2353 got_click = TRUE;
2354 else
2355 {
2356 if (!got_click) /* didn't get click, ignore */
2357 return FALSE;
2358 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002361 if (in_tab_line)
2362 {
2363 in_tab_line = FALSE;
2364 return FALSE;
2365 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 /*
2370 * CTRL right mouse button does CTRL-T
2371 */
2372 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2373 {
2374 if (State & INSERT)
2375 stuffcharReadbuff(Ctrl_O);
2376 if (count > 1)
2377 stuffnumReadbuff(count);
2378 stuffcharReadbuff(Ctrl_T);
2379 got_click = FALSE; /* ignore drag&release now */
2380 return FALSE;
2381 }
2382
2383 /*
2384 * CTRL only works with left mouse button
2385 */
2386 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2387 return FALSE;
2388
2389 /*
2390 * When a modifier is down, ignore drag and release events, as well as
2391 * multiple clicks and the middle mouse button.
2392 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2393 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002394 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2395 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 && (!is_click
2397 || (mod_mask & MOD_MASK_MULTI_CLICK)
2398 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002399 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 && mouse_model_popup()
2401 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002402 && !((mod_mask & MOD_MASK_ALT)
2403 && !mouse_model_popup()
2404 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 )
2406 return FALSE;
2407
2408 /*
2409 * If the button press was used as the movement command for an operator
2410 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2411 * drag/release events.
2412 */
2413 if (!is_click && which_button == MOUSE_MIDDLE)
2414 return FALSE;
2415
2416 if (oap != NULL)
2417 regname = oap->regname;
2418 else
2419 regname = 0;
2420
2421 /*
2422 * Middle mouse button does a 'put' of the selected text
2423 */
2424 if (which_button == MOUSE_MIDDLE)
2425 {
2426 if (State == NORMAL)
2427 {
2428 /*
2429 * If an operator was pending, we don't know what the user wanted
2430 * to do. Go back to normal mode: Clear the operator and beep().
2431 */
2432 if (oap != NULL && oap->op_type != OP_NOP)
2433 {
2434 clearopbeep(oap);
2435 return FALSE;
2436 }
2437
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 /*
2439 * If visual was active, yank the highlighted text and put it
2440 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002441 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 */
2443 if (VIsual_active)
2444 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002445 if (VIsual_select)
2446 {
2447 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002448 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002449 }
2450 else
2451 {
2452 stuffcharReadbuff('y');
2453 stuffcharReadbuff(K_MIDDLEMOUSE);
2454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 do_always = TRUE; /* ignore 'mouse' setting next time */
2456 return FALSE;
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 /*
2459 * The rest is below jump_to_mouse()
2460 */
2461 }
2462
2463 else if ((State & INSERT) == 0)
2464 return FALSE;
2465
2466 /*
2467 * Middle click in insert mode doesn't move the mouse, just insert the
2468 * contents of a register. '.' register is special, can't insert that
2469 * with do_put().
2470 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2471 * happens for the GUI).
2472 */
2473 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2474 {
2475 if (regname == '.')
2476 insert_reg(regname, TRUE);
2477 else
2478 {
2479#ifdef FEAT_CLIPBOARD
2480 if (clip_star.available && regname == 0)
2481 regname = '*';
2482#endif
2483 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2484 insert_reg(regname, TRUE);
2485 else
2486 {
2487 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2488
2489 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2490 AppendCharToRedobuff(Ctrl_R);
2491 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2492 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2493 }
2494 }
2495 return FALSE;
2496 }
2497 }
2498
2499 /* When dragging or button-up stay in the same window. */
2500 if (!is_click)
2501 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2502
2503 start_visual.lnum = 0;
2504
Bram Moolenaarf740b292006-02-16 22:11:02 +00002505 /* Check for clicking in the tab page line. */
2506 if (mouse_row == 0 && firstwin->w_winrow > 0)
2507 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002508 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002509 {
2510 if (in_tab_line)
2511 {
2512 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002513 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2514 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002515 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002516 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002517 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002518
Bram Moolenaarf740b292006-02-16 22:11:02 +00002519 /* click in a tab selects that tab page */
2520 if (is_click
2521# ifdef FEAT_CMDWIN
2522 && cmdwin_type == 0
2523# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002524 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002525 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002526 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002527 c1 = TabPageIdxs[mouse_col];
2528 if (c1 >= 0)
2529 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002530 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2531 {
2532 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002533 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002534 tabpage_new();
2535 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2536 }
2537 else
2538 {
2539 /* Go to specified tab page, or next one if not clicking
2540 * on a label. */
2541 goto_tabpage(c1);
2542
2543 /* It's like clicking on the status line of a window. */
2544 if (curwin != old_curwin)
2545 end_visual_mode();
2546 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002547 }
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002548 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002549 {
2550 tabpage_T *tp;
2551
2552 /* Close the current or specified tab page. */
2553 if (c1 == -999)
2554 tp = curtab;
2555 else
2556 tp = find_tabpage(-c1);
2557 if (tp == curtab)
2558 {
2559 if (first_tabpage->tp_next != NULL)
2560 tabpage_close(FALSE);
2561 }
2562 else if (tp != NULL)
2563 tabpage_close_other(tp, FALSE);
2564 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002565 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002566 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002567 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002568 else if (is_drag && in_tab_line)
2569 {
2570 c1 = TabPageIdxs[mouse_col];
2571 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2572 return FALSE;
2573 }
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 /*
2576 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2577 * right button up -> pop-up menu
2578 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002579 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 */
2581 if (mouse_model_popup())
2582 {
2583 if (which_button == MOUSE_RIGHT
2584 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2585 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002586#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002588 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2589 || defined(FEAT_TERM_POPUP_MENU)
2590# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (gui.in_use)
2592 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002593# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2594 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2595 if (!is_click)
2596 /* Ignore right button release events, only shows the popup
2597 * menu on the button down event. */
2598 return FALSE;
2599# endif
2600# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2601 if (is_click || is_drag)
2602 /* Ignore right button down and drag mouse events. Windows
2603 * only shows the popup menu on the button up event. */
2604 return FALSE;
2605# endif
2606 }
2607# endif
2608# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2609 else
2610# endif
2611# if defined(FEAT_TERM_POPUP_MENU)
2612 if (!is_click)
2613 /* Ignore right button release events, only shows the popup
2614 * menu on the button down event. */
2615 return FALSE;
2616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002618 jump_flags = 0;
2619 if (STRCMP(p_mousem, "popup_setpos") == 0)
2620 {
2621 /* First set the cursor position before showing the popup
2622 * menu. */
2623 if (VIsual_active)
2624 {
2625 pos_T m_pos;
2626
2627 /*
2628 * set MOUSE_MAY_STOP_VIS if we are outside the
2629 * selection or the current window (might have false
2630 * negative here)
2631 */
2632 if (mouse_row < curwin->w_winrow
2633 || mouse_row
2634 > (curwin->w_winrow + curwin->w_height))
2635 jump_flags = MOUSE_MAY_STOP_VIS;
2636 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2637 jump_flags = MOUSE_MAY_STOP_VIS;
2638 else
2639 {
2640 if ((LT_POS(curwin->w_cursor, VIsual)
2641 && (LT_POS(m_pos, curwin->w_cursor)
2642 || LT_POS(VIsual, m_pos)))
2643 || (LT_POS(VIsual, curwin->w_cursor)
2644 && (LT_POS(m_pos, VIsual)
2645 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002647 jump_flags = MOUSE_MAY_STOP_VIS;
2648 }
2649 else if (VIsual_mode == Ctrl_V)
2650 {
2651 getvcols(curwin, &curwin->w_cursor, &VIsual,
2652 &leftcol, &rightcol);
2653 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2654 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 }
2657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002659 else
2660 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002662 if (jump_flags)
2663 {
2664 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2665 update_curbuf(VIsual_active ? INVERTED : VALID);
2666 setcursor();
2667 out_flush(); /* Update before showing popup menu */
2668 }
2669# ifdef FEAT_MENU
2670 show_popupmenu();
2671 got_click = FALSE; /* ignore release events */
2672# endif
2673 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674#else
2675 return FALSE;
2676#endif
2677 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002678 if (which_button == MOUSE_LEFT
2679 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
2681 which_button = MOUSE_RIGHT;
2682 mod_mask &= ~MOD_MASK_SHIFT;
2683 }
2684 }
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if ((State & (NORMAL | INSERT))
2687 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2688 {
2689 if (which_button == MOUSE_LEFT)
2690 {
2691 if (is_click)
2692 {
2693 /* stop Visual mode for a left click in a window, but not when
2694 * on a status line */
2695 if (VIsual_active)
2696 jump_flags |= MOUSE_MAY_STOP_VIS;
2697 }
2698 else if (mouse_has(MOUSE_VISUAL))
2699 jump_flags |= MOUSE_MAY_VIS;
2700 }
2701 else if (which_button == MOUSE_RIGHT)
2702 {
2703 if (is_click && VIsual_active)
2704 {
2705 /*
2706 * Remember the start and end of visual before moving the
2707 * cursor.
2708 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002709 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
2711 start_visual = curwin->w_cursor;
2712 end_visual = VIsual;
2713 }
2714 else
2715 {
2716 start_visual = VIsual;
2717 end_visual = curwin->w_cursor;
2718 }
2719 }
2720 jump_flags |= MOUSE_FOCUS;
2721 if (mouse_has(MOUSE_VISUAL))
2722 jump_flags |= MOUSE_MAY_VIS;
2723 }
2724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 /*
2727 * If an operator is pending, ignore all drags and releases until the
2728 * next mouse click.
2729 */
2730 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2731 {
2732 got_click = FALSE;
2733 oap->motion_type = MCHAR;
2734 }
2735
2736 /* When releasing the button let jump_to_mouse() know. */
2737 if (!is_click && !is_drag)
2738 jump_flags |= MOUSE_RELEASED;
2739
2740 /*
2741 * JUMP!
2742 */
2743 jump_flags = jump_to_mouse(jump_flags,
2744 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002745
2746#ifdef FEAT_MENU
2747 /* A click in the window toolbar has no side effects. */
2748 if (jump_flags & MOUSE_WINBAR)
2749 return FALSE;
2750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 moved = (jump_flags & CURSOR_MOVED);
2752 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002756 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2758 {
2759 int key = KEY2TERMCAP1(c);
2760
2761 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2762 || key == (int)KE_RIGHTRELEASE)
2763 netbeans_button_release(which_button);
2764 }
2765#endif
2766
2767 /* When jumping to another window, clear a pending operator. That's a bit
2768 * friendlier than beeping and not jumping to that window. */
2769 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2770 clearop(oap);
2771
2772#ifdef FEAT_FOLDING
2773 if (mod_mask == 0
2774 && !is_drag
2775 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2776 && which_button == MOUSE_LEFT)
2777 {
2778 /* open or close a fold at this line */
2779 if (jump_flags & MOUSE_FOLD_OPEN)
2780 openFold(curwin->w_cursor.lnum, 1L);
2781 else
2782 closeFold(curwin->w_cursor.lnum, 1L);
2783 /* don't move the cursor if still in the same window */
2784 if (curwin == old_curwin)
2785 curwin->w_cursor = save_cursor;
2786 }
2787#endif
2788
2789#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2790 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2791 {
2792 clip_modeless(which_button, is_click, is_drag);
2793 return FALSE;
2794 }
2795#endif
2796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002798 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002799 if (VIsual_active && is_drag && get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
2801 /* In the very first line, allow scrolling one line */
2802 if (mouse_row == 0)
2803 mouse_dragging = 2;
2804 else
2805 mouse_dragging = 1;
2806 }
2807
2808 /* When dragging the mouse above the window, scroll down. */
2809 if (is_drag && mouse_row < 0 && !in_status_line)
2810 {
2811 scroll_redraw(FALSE, 1L);
2812 mouse_row = 0;
2813 }
2814
2815 if (start_visual.lnum) /* right click in visual mode */
2816 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002817 /* When ALT is pressed make Visual mode blockwise. */
2818 if (mod_mask & MOD_MASK_ALT)
2819 VIsual_mode = Ctrl_V;
2820
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 /*
2822 * In Visual-block mode, divide the area in four, pick up the corner
2823 * that is in the quarter that the cursor is in.
2824 */
2825 if (VIsual_mode == Ctrl_V)
2826 {
2827 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2828 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2829 end_visual.col = leftcol;
2830 else
2831 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002832 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 end_visual.lnum = start_visual.lnum;
2835
2836 /* move VIsual to the right column */
2837 start_visual = curwin->w_cursor; /* save the cursor pos */
2838 curwin->w_cursor = end_visual;
2839 coladvance(end_visual.col);
2840 VIsual = curwin->w_cursor;
2841 curwin->w_cursor = start_visual; /* restore the cursor */
2842 }
2843 else
2844 {
2845 /*
2846 * If the click is before the start of visual, change the start.
2847 * If the click is after the end of visual, change the end. If
2848 * the click is inside the visual, change the closest side.
2849 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002850 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002852 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 VIsual = start_visual;
2854 else
2855 {
2856 /* In the same line, compare column number */
2857 if (end_visual.lnum == start_visual.lnum)
2858 {
2859 if (curwin->w_cursor.col - start_visual.col >
2860 end_visual.col - curwin->w_cursor.col)
2861 VIsual = start_visual;
2862 else
2863 VIsual = end_visual;
2864 }
2865
2866 /* In different lines, compare line number */
2867 else
2868 {
2869 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2870 (end_visual.lnum - curwin->w_cursor.lnum);
2871
2872 if (diff > 0) /* closest to end */
2873 VIsual = start_visual;
2874 else if (diff < 0) /* closest to start */
2875 VIsual = end_visual;
2876 else /* in the middle line */
2877 {
2878 if (curwin->w_cursor.col <
2879 (start_visual.col + end_visual.col) / 2)
2880 VIsual = end_visual;
2881 else
2882 VIsual = start_visual;
2883 }
2884 }
2885 }
2886 }
2887 }
2888 /*
2889 * If Visual mode started in insert mode, execute "CTRL-O"
2890 */
2891 else if ((State & INSERT) && VIsual_active)
2892 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894 /*
2895 * Middle mouse click: Put text before cursor.
2896 */
2897 if (which_button == MOUSE_MIDDLE)
2898 {
2899#ifdef FEAT_CLIPBOARD
2900 if (clip_star.available && regname == 0)
2901 regname = '*';
2902#endif
2903 if (yank_register_mline(regname))
2904 {
2905 if (mouse_past_bottom)
2906 dir = FORWARD;
2907 }
2908 else if (mouse_past_eol)
2909 dir = FORWARD;
2910
2911 if (fixindent)
2912 {
2913 c1 = (dir == BACKWARD) ? '[' : ']';
2914 c2 = 'p';
2915 }
2916 else
2917 {
2918 c1 = (dir == FORWARD) ? 'p' : 'P';
2919 c2 = NUL;
2920 }
2921 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2922
2923 /*
2924 * Remember where the paste started, so in edit() Insstart can be set
2925 * to this position
2926 */
2927 if (restart_edit != 0)
2928 where_paste_started = curwin->w_cursor;
2929 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2930 }
2931
Bram Moolenaar4033c552017-09-16 20:54:51 +02002932#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 /*
2934 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2935 * error under the mouse pointer.
2936 */
2937 else if (((mod_mask & MOD_MASK_CTRL)
2938 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2939 && bt_quickfix(curbuf))
2940 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002941 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002942 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002943 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002944 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 got_click = FALSE; /* ignore drag&release now */
2946 }
2947#endif
2948
2949 /*
2950 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2951 * under the mouse pointer.
2952 */
2953 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2954 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2955 {
2956 if (State & INSERT)
2957 stuffcharReadbuff(Ctrl_O);
2958 stuffcharReadbuff(Ctrl_RSB);
2959 got_click = FALSE; /* ignore drag&release now */
2960 }
2961
2962 /*
2963 * Shift-Mouse click searches for the next occurrence of the word under
2964 * the mouse pointer
2965 */
2966 else if ((mod_mask & MOD_MASK_SHIFT))
2967 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002968 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 stuffcharReadbuff(Ctrl_O);
2970 if (which_button == MOUSE_LEFT)
2971 stuffcharReadbuff('*');
2972 else /* MOUSE_RIGHT */
2973 stuffcharReadbuff('#');
2974 }
2975
2976 /* Handle double clicks, unless on status line */
2977 else if (in_status_line)
2978 {
2979#ifdef FEAT_MOUSESHAPE
2980 if ((is_drag || is_click) && !drag_status_line)
2981 {
2982 drag_status_line = TRUE;
2983 update_mouseshape(-1);
2984 }
2985#endif
2986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 else if (in_sep_line)
2988 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02002989#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if ((is_drag || is_click) && !drag_sep_line)
2991 {
2992 drag_sep_line = TRUE;
2993 update_mouseshape(-1);
2994 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01002995#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02002996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
2998 && mouse_has(MOUSE_VISUAL))
2999 {
3000 if (is_click || !VIsual_active)
3001 {
3002 if (VIsual_active)
3003 orig_cursor = VIsual;
3004 else
3005 {
3006 check_visual_highlight();
3007 VIsual = curwin->w_cursor;
3008 orig_cursor = VIsual;
3009 VIsual_active = TRUE;
3010 VIsual_reselect = TRUE;
3011 /* start Select mode if 'selectmode' contains "mouse" */
3012 may_start_select('o');
3013 setmouse();
3014 }
3015 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003016 {
3017 /* Double click with ALT pressed makes it blockwise. */
3018 if (mod_mask & MOD_MASK_ALT)
3019 VIsual_mode = Ctrl_V;
3020 else
3021 VIsual_mode = 'v';
3022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3024 VIsual_mode = 'V';
3025 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3026 VIsual_mode = Ctrl_V;
3027#ifdef FEAT_CLIPBOARD
3028 /* Make sure the clipboard gets updated. Needed because start and
3029 * end may still be the same, and the selection needs to be owned */
3030 clip_star.vmode = NUL;
3031#endif
3032 }
3033 /*
3034 * A double click selects a word or a block.
3035 */
3036 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3037 {
3038 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003039 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041 if (is_click)
3042 {
3043 /* If the character under the cursor (skipping white space) is
3044 * not a word character, try finding a match and select a (),
3045 * {}, [], #if/#endif, etc. block. */
3046 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003047 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 inc(&end_visual);
3049 if (oap != NULL)
3050 oap->motion_type = MCHAR;
3051 if (oap != NULL
3052 && VIsual_mode == 'v'
3053 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003054 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 && (pos = findmatch(oap, NUL)) != NULL)
3056 {
3057 curwin->w_cursor = *pos;
3058 if (oap->motion_type == MLINE)
3059 VIsual_mode = 'V';
3060 else if (*p_sel == 'e')
3061 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003062 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 ++VIsual.col;
3064 else
3065 ++curwin->w_cursor.col;
3066 }
3067 }
3068 }
3069
3070 if (pos == NULL && (is_click || is_drag))
3071 {
3072 /* When not found a match or when dragging: extend to include
3073 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003074 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
3076 find_start_of_word(&curwin->w_cursor);
3077 find_end_of_word(&VIsual);
3078 }
3079 else
3080 {
3081 find_start_of_word(&VIsual);
3082 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003084 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 find_end_of_word(&curwin->w_cursor);
3086 }
3087 }
3088 curwin->w_set_curswant = TRUE;
3089 }
3090 if (is_click)
3091 redraw_curbuf_later(INVERTED); /* update the inversion */
3092 }
3093 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003094 {
3095 if (mod_mask & MOD_MASK_ALT)
3096 VIsual_mode = Ctrl_V;
3097 else
3098 VIsual_mode = 'v';
3099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003102 if ((!VIsual_active && old_active && mode_displayed)
3103 || (VIsual_active && p_smd && msg_silent == 0
3104 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
3107 return moved;
3108}
3109
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110/*
3111 * Move "pos" back to the start of the word it's in.
3112 */
3113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003114find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 char_u *line;
3117 int cclass;
3118 int col;
3119
3120 line = ml_get(pos->lnum);
3121 cclass = get_mouse_class(line + pos->col);
3122
3123 while (pos->col > 0)
3124 {
3125 col = pos->col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 col -= (*mb_head_off)(line, line + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (get_mouse_class(line + col) != cclass)
3128 break;
3129 pos->col = col;
3130 }
3131}
3132
3133/*
3134 * Move "pos" forward to the end of the word it's in.
3135 * When 'selection' is "exclusive", the position is just after the word.
3136 */
3137 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003138find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 char_u *line;
3141 int cclass;
3142 int col;
3143
3144 line = ml_get(pos->lnum);
3145 if (*p_sel == 'e' && pos->col > 0)
3146 {
3147 --pos->col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 pos->col -= (*mb_head_off)(line, line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 }
3150 cclass = get_mouse_class(line + pos->col);
3151 while (line[pos->col] != NUL)
3152 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003153 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (get_mouse_class(line + col) != cclass)
3155 {
3156 if (*p_sel == 'e')
3157 pos->col = col;
3158 break;
3159 }
3160 pos->col = col;
3161 }
3162}
3163
3164/*
3165 * Get class of a character for selection: same class means same word.
3166 * 0: blank
3167 * 1: punctuation groups
3168 * 2: normal word character
3169 * >2: multi-byte word character.
3170 */
3171 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003172get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 int c;
3175
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3177 return mb_get_class(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 c = *p;
3180 if (c == ' ' || c == '\t')
3181 return 0;
3182
3183 if (vim_iswordc(c))
3184 return 2;
3185
3186 /*
3187 * There are a few special cases where we want certain combinations of
3188 * characters to be considered as a single word. These are things like
3189 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003190 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 */
3192 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3193 return 1;
3194 return c;
3195}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#endif /* FEAT_MOUSE */
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198/*
3199 * Check if highlighting for visual mode is possible, give a warning message
3200 * if not.
3201 */
3202 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003203check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204{
3205 static int did_check = FALSE;
3206
3207 if (full_screen)
3208 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003209 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003210 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 did_check = TRUE;
3212 }
3213}
3214
3215/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003216 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 * This function should ALWAYS be called to end Visual mode, except from
3218 * do_pending_operator().
3219 */
3220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003221end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223#ifdef FEAT_CLIPBOARD
3224 /*
3225 * If we are using the clipboard, then remember what was selected in case
3226 * we need to paste it somewhere while we still own the selection.
3227 * Only do this when the clipboard is already owned. Don't want to grab
3228 * the selection when hitting ESC.
3229 */
3230 if (clip_star.available && clip_star.owned)
3231 clip_auto_select();
3232#endif
3233
3234 VIsual_active = FALSE;
3235#ifdef FEAT_MOUSE
3236 setmouse();
3237 mouse_dragging = 0;
3238#endif
3239
3240 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003241 curbuf->b_visual.vi_mode = VIsual_mode;
3242 curbuf->b_visual.vi_start = VIsual;
3243 curbuf->b_visual.vi_end = curwin->w_cursor;
3244 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245#ifdef FEAT_EVAL
3246 curbuf->b_visual_mode_eval = VIsual_mode;
3247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 if (!virtual_active())
3249 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003250 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003252 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253}
3254
3255/*
3256 * Reset VIsual_active and VIsual_reselect.
3257 */
3258 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003259reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 if (VIsual_active)
3262 {
3263 end_visual_mode();
3264 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3265 }
3266 VIsual_reselect = FALSE;
3267}
3268
3269/*
3270 * Reset VIsual_active and VIsual_reselect if it's set.
3271 */
3272 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003273reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
3275 if (VIsual_active)
3276 {
3277 end_visual_mode();
3278 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3279 VIsual_reselect = FALSE;
3280 }
3281}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283/*
3284 * Check for a balloon-eval special item to include when searching for an
3285 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3286 * Returns TRUE if the character at "*ptr" should be included.
3287 * "dir" is FORWARD or BACKWARD, the direction of searching.
3288 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3289 * "bnp" points to a counter for square brackets.
3290 */
3291 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003292find_is_eval_item(
3293 char_u *ptr,
3294 int *colp,
3295 int *bnp,
3296 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 /* Accept everything inside []. */
3299 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3300 ++*bnp;
3301 if (*bnp > 0)
3302 {
3303 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3304 --*bnp;
3305 return TRUE;
3306 }
3307
3308 /* skip over "s.var" */
3309 if (*ptr == '.')
3310 return TRUE;
3311
3312 /* two-character item: s->var */
3313 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3314 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3315 {
3316 *colp += dir;
3317 return TRUE;
3318 }
3319 return FALSE;
3320}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
3322/*
3323 * Find the identifier under or to the right of the cursor.
3324 * "find_type" can have one of three values:
3325 * FIND_IDENT: find an identifier (keyword)
3326 * FIND_STRING: find any non-white string
3327 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003328 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 *
3330 * There are three steps:
3331 * 1. Search forward for the start of an identifier/string. Doesn't move if
3332 * already on one.
3333 * 2. Search backward for the start of this identifier/string.
3334 * This doesn't match the real Vi but I like it a little better and it
3335 * shouldn't bother anyone.
3336 * 3. Search forward to the end of this identifier/string.
3337 * When FIND_IDENT isn't defined, we backup until a blank.
3338 *
3339 * Returns the length of the string, or zero if no string is found.
3340 * If a string is found, a pointer to the string is put in "*string". This
3341 * string is not always NUL terminated.
3342 */
3343 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003344find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3347 curwin->w_cursor.col, string, find_type);
3348}
3349
3350/*
3351 * Like find_ident_under_cursor(), but for any window and any position.
3352 * However: Uses 'iskeyword' from the current window!.
3353 */
3354 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003355find_ident_at_pos(
3356 win_T *wp,
3357 linenr_T lnum,
3358 colnr_T startcol,
3359 char_u **string,
3360 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361{
3362 char_u *ptr;
3363 int col = 0; /* init to shut up GCC */
3364 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 int this_class = 0;
3366 int prev_class;
3367 int prevcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
3370 /*
3371 * if i == 0: try to find an identifier
3372 * if i == 1: try to find any non-white string
3373 */
3374 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3375 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3376 {
3377 /*
3378 * 1. skip to start of identifier/string
3379 */
3380 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (has_mbyte)
3382 {
3383 while (ptr[col] != NUL)
3384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 /* Stop at a ']' to evaluate "a[x]". */
3386 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3387 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 this_class = mb_get_class(ptr + col);
3389 if (this_class != 0 && (i == 1 || this_class != 1))
3390 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003391 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 }
3393 }
3394 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003396 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 )
3399 ++col;
3400
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 /* When starting on a ']' count it, so that we include the '['. */
3402 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403
3404 /*
3405 * 2. Back up to start of identifier/string.
3406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 if (has_mbyte)
3408 {
3409 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3411 this_class = mb_get_class((char_u *)"a");
3412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003414 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 {
3416 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3417 prev_class = mb_get_class(ptr + prevcol);
3418 if (this_class != prev_class
3419 && (i == 0
3420 || prev_class == 0
3421 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 && (!(find_type & FIND_EVAL)
3423 || prevcol == 0
3424 || !find_is_eval_item(ptr + prevcol, &prevcol,
3425 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 )
3427 break;
3428 col = prevcol;
3429 }
3430
3431 /* If we don't want just any old string, or we've found an
3432 * identifier, stop searching. */
3433 if (this_class > 2)
3434 this_class = 2;
3435 if (!(find_type & FIND_STRING) || this_class == 2)
3436 break;
3437 }
3438 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 while (col > 0
3441 && ((i == 0
3442 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003443 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 && (!(find_type & FIND_IDENT)
3445 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 || ((find_type & FIND_EVAL)
3447 && col > 1
3448 && find_is_eval_item(ptr + col - 1, &col,
3449 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 ))
3451 --col;
3452
3453 /* If we don't want just any old string, or we've found an
3454 * identifier, stop searching. */
3455 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3456 break;
3457 }
3458 }
3459
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003460 if (ptr[col] == NUL || (i == 0
3461 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 /*
3464 * didn't find an identifier or string
3465 */
3466 if (find_type & FIND_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003467 emsg(_("E348: No string under cursor"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003469 emsg(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 return 0;
3471 }
3472 ptr += col;
3473 *string = ptr;
3474
3475 /*
3476 * 3. Find the end if the identifier/string.
3477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 bn = 0;
3479 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (has_mbyte)
3482 {
3483 /* Search for point of changing multibyte character class. */
3484 this_class = mb_get_class(ptr);
3485 while (ptr[col] != NUL
3486 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3487 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 || ((find_type & FIND_EVAL)
3489 && col <= (int)startcol
3490 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003492 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003496 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 || ((find_type & FIND_EVAL)
3498 && col <= (int)startcol
3499 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 return col;
3504}
3505
3506/*
3507 * Prepare for redo of a normal command.
3508 */
3509 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003510prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
3512 prep_redo(cap->oap->regname, cap->count0,
3513 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3514}
3515
3516/*
3517 * Prepare for redo of any command.
3518 * Note that only the last argument can be a multi-byte char.
3519 */
3520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003521prep_redo(
3522 int regname,
3523 long num,
3524 int cmd1,
3525 int cmd2,
3526 int cmd3,
3527 int cmd4,
3528 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
3530 ResetRedobuff();
3531 if (regname != 0) /* yank from specified buffer */
3532 {
3533 AppendCharToRedobuff('"');
3534 AppendCharToRedobuff(regname);
3535 }
3536 if (num)
3537 AppendNumberToRedobuff(num);
3538
3539 if (cmd1 != NUL)
3540 AppendCharToRedobuff(cmd1);
3541 if (cmd2 != NUL)
3542 AppendCharToRedobuff(cmd2);
3543 if (cmd3 != NUL)
3544 AppendCharToRedobuff(cmd3);
3545 if (cmd4 != NUL)
3546 AppendCharToRedobuff(cmd4);
3547 if (cmd5 != NUL)
3548 AppendCharToRedobuff(cmd5);
3549}
3550
3551/*
3552 * check for operator active and clear it
3553 *
3554 * return TRUE if operator was active
3555 */
3556 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003557checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
3559 if (oap->op_type == OP_NOP)
3560 return FALSE;
3561 clearopbeep(oap);
3562 return TRUE;
3563}
3564
3565/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003566 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003568 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 */
3570 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003571checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003573 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 return FALSE;
3575 clearopbeep(oap);
3576 return TRUE;
3577}
3578
3579 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003580clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 oap->op_type = OP_NOP;
3583 oap->regname = 0;
3584 oap->motion_force = NUL;
3585 oap->use_reg_one = FALSE;
3586}
3587
3588 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003589clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
3591 clearop(oap);
3592 beep_flush();
3593}
3594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595/*
3596 * Remove the shift modifier from a special key.
3597 */
3598 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003599unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600{
3601 switch (cap->cmdchar)
3602 {
3603 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3604 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3605 case K_S_UP: cap->cmdchar = K_UP; break;
3606 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3607 case K_S_HOME: cap->cmdchar = K_HOME; break;
3608 case K_S_END: cap->cmdchar = K_END; break;
3609 }
3610 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3611}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003613/*
3614 * If the mode is currently displayed clear the command line or update the
3615 * command displayed.
3616 */
3617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003618may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003619{
3620 if (mode_displayed)
3621 clear_cmdline = TRUE; /* unshow visual mode later */
3622#ifdef FEAT_CMDL_INFO
3623 else
3624 clear_showcmd();
3625#endif
3626}
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3629/*
3630 * Routines for displaying a partly typed command
3631 */
3632
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003633#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634static char_u showcmd_buf[SHOWCMD_BUFLEN];
3635static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3636static int showcmd_is_clear = TRUE;
3637static int showcmd_visual = FALSE;
3638
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003639static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
3641 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003642clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 if (!p_sc)
3645 return;
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 if (VIsual_active && !char_avail())
3648 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003649 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 long lines;
3651 colnr_T leftcol, rightcol;
3652 linenr_T top, bot;
3653
3654 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003655 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
3657 top = VIsual.lnum;
3658 bot = curwin->w_cursor.lnum;
3659 }
3660 else
3661 {
3662 top = curwin->w_cursor.lnum;
3663 bot = VIsual.lnum;
3664 }
3665# ifdef FEAT_FOLDING
3666 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003667 (void)hasFolding(top, &top, NULL);
3668 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669# endif
3670 lines = bot - top + 1;
3671
3672 if (VIsual_mode == Ctrl_V)
3673 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003674# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003675 char_u *saved_sbr = p_sbr;
3676
3677 /* Make 'sbr' empty for a moment to get the correct size. */
3678 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003679# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003681# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003682 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3685 (long)(rightcol - leftcol + 1));
3686 }
3687 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3688 sprintf((char *)showcmd_buf, "%ld", lines);
3689 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003690 {
3691 char_u *s, *e;
3692 int l;
3693 int bytes = 0;
3694 int chars = 0;
3695
3696 if (cursor_bot)
3697 {
3698 s = ml_get_pos(&VIsual);
3699 e = ml_get_cursor();
3700 }
3701 else
3702 {
3703 s = ml_get_cursor();
3704 e = ml_get_pos(&VIsual);
3705 }
3706 while ((*p_sel != 'e') ? s <= e : s < e)
3707 {
3708 l = (*mb_ptr2len)(s);
3709 if (l == 0)
3710 {
3711 ++bytes;
3712 ++chars;
3713 break; /* end of line */
3714 }
3715 bytes += l;
3716 ++chars;
3717 s += l;
3718 }
3719 if (bytes == chars)
3720 sprintf((char *)showcmd_buf, "%d", chars);
3721 else
3722 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3725 showcmd_visual = TRUE;
3726 }
3727 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 {
3729 showcmd_buf[0] = NUL;
3730 showcmd_visual = FALSE;
3731
3732 /* Don't actually display something if there is nothing to clear. */
3733 if (showcmd_is_clear)
3734 return;
3735 }
3736
3737 display_showcmd();
3738}
3739
3740/*
3741 * Add 'c' to string of shown command chars.
3742 * Return TRUE if output has been written (and setcursor() has been called).
3743 */
3744 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003745add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746{
3747 char_u *p;
3748 int old_len;
3749 int extra_len;
3750 int overflow;
3751#if defined(FEAT_MOUSE)
3752 int i;
3753 static int ignore[] =
3754 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003755# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3757 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003758# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003759 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003760 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3762 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003763 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003765 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 0
3767 };
3768#endif
3769
Bram Moolenaar09df3122006-01-23 22:23:09 +00003770 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 return FALSE;
3772
3773 if (showcmd_visual)
3774 {
3775 showcmd_buf[0] = NUL;
3776 showcmd_visual = FALSE;
3777 }
3778
3779#if defined(FEAT_MOUSE)
3780 /* Ignore keys that are scrollbar updates and mouse clicks */
3781 if (IS_SPECIAL(c))
3782 for (i = 0; ignore[i] != 0; ++i)
3783 if (ignore[i] == c)
3784 return FALSE;
3785#endif
3786
3787 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003788 if (*p == ' ')
3789 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 old_len = (int)STRLEN(showcmd_buf);
3791 extra_len = (int)STRLEN(p);
3792 overflow = old_len + extra_len - SHOWCMD_COLS;
3793 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003794 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3795 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 STRCAT(showcmd_buf, p);
3797
3798 if (char_avail())
3799 return FALSE;
3800
3801 display_showcmd();
3802
3803 return TRUE;
3804}
3805
3806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003807add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808{
3809 if (!add_to_showcmd(c))
3810 setcursor();
3811}
3812
3813/*
3814 * Delete 'len' characters from the end of the shown command.
3815 */
3816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003817del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 int old_len;
3820
3821 if (!p_sc)
3822 return;
3823
3824 old_len = (int)STRLEN(showcmd_buf);
3825 if (len > old_len)
3826 len = old_len;
3827 showcmd_buf[old_len - len] = NUL;
3828
3829 if (!char_avail())
3830 display_showcmd();
3831}
3832
3833/*
3834 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3835 * something and there is a partial mapping.
3836 */
3837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003838push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
3840 if (p_sc)
3841 STRCPY(old_showcmd_buf, showcmd_buf);
3842}
3843
3844 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003845pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
3847 if (!p_sc)
3848 return;
3849
3850 STRCPY(showcmd_buf, old_showcmd_buf);
3851
3852 display_showcmd();
3853}
3854
3855 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003856display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
3858 int len;
3859
3860 cursor_off();
3861
3862 len = (int)STRLEN(showcmd_buf);
3863 if (len == 0)
3864 showcmd_is_clear = TRUE;
3865 else
3866 {
3867 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3868 showcmd_is_clear = FALSE;
3869 }
3870
3871 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003872 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3873 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 */
3875 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3876
3877 setcursor(); /* put cursor back where it belongs */
3878}
3879#endif
3880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881/*
3882 * When "check" is FALSE, prepare for commands that scroll the window.
3883 * When "check" is TRUE, take care of scroll-binding after the window has
3884 * scrolled. Called from normal_cmd() and edit().
3885 */
3886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003887do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
3889 static win_T *old_curwin = NULL;
3890 static linenr_T old_topline = 0;
3891#ifdef FEAT_DIFF
3892 static int old_topfill = 0;
3893#endif
3894 static buf_T *old_buf = NULL;
3895 static colnr_T old_leftcol = 0;
3896
3897 if (check && curwin->w_p_scb)
3898 {
3899 /* If a ":syncbind" command was just used, don't scroll, only reset
3900 * the values. */
3901 if (did_syncbind)
3902 did_syncbind = FALSE;
3903 else if (curwin == old_curwin)
3904 {
3905 /*
3906 * Synchronize other windows, as necessary according to
3907 * 'scrollbind'. Don't do this after an ":edit" command, except
3908 * when 'diff' is set.
3909 */
3910 if ((curwin->w_buffer == old_buf
3911#ifdef FEAT_DIFF
3912 || curwin->w_p_diff
3913#endif
3914 )
3915 && (curwin->w_topline != old_topline
3916#ifdef FEAT_DIFF
3917 || curwin->w_topfill != old_topfill
3918#endif
3919 || curwin->w_leftcol != old_leftcol))
3920 {
3921 check_scrollbind(curwin->w_topline - old_topline,
3922 (long)(curwin->w_leftcol - old_leftcol));
3923 }
3924 }
3925 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3926 {
3927 /*
3928 * When switching between windows, make sure that the relative
3929 * vertical offset is valid for the new window. The relative
3930 * offset is invalid whenever another 'scrollbind' window has
3931 * scrolled to a point that would force the current window to
3932 * scroll past the beginning or end of its buffer. When the
3933 * resync is performed, some of the other 'scrollbind' windows may
3934 * need to jump so that the current window's relative position is
3935 * visible on-screen.
3936 */
3937 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3938 }
3939 curwin->w_scbind_pos = curwin->w_topline;
3940 }
3941
3942 old_curwin = curwin;
3943 old_topline = curwin->w_topline;
3944#ifdef FEAT_DIFF
3945 old_topfill = curwin->w_topfill;
3946#endif
3947 old_buf = curwin->w_buffer;
3948 old_leftcol = curwin->w_leftcol;
3949}
3950
3951/*
3952 * Synchronize any windows that have "scrollbind" set, based on the
3953 * number of rows by which the current window has changed
3954 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3955 */
3956 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003957check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
3959 int want_ver;
3960 int want_hor;
3961 win_T *old_curwin = curwin;
3962 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 int old_VIsual_select = VIsual_select;
3964 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 colnr_T tgt_leftcol = curwin->w_leftcol;
3966 long topline;
3967 long y;
3968
3969 /*
3970 * check 'scrollopt' string for vertical and horizontal scroll options
3971 */
3972 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3973#ifdef FEAT_DIFF
3974 want_ver |= old_curwin->w_p_diff;
3975#endif
3976 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3977
3978 /*
3979 * loop through the scrollbound windows and scroll accordingly
3980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003982 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
3984 curbuf = curwin->w_buffer;
3985 /* skip original window and windows with 'noscrollbind' */
3986 if (curwin != old_curwin && curwin->w_p_scb)
3987 {
3988 /*
3989 * do the vertical scroll
3990 */
3991 if (want_ver)
3992 {
3993#ifdef FEAT_DIFF
3994 if (old_curwin->w_p_diff && curwin->w_p_diff)
3995 {
3996 diff_set_topline(old_curwin, curwin);
3997 }
3998 else
3999#endif
4000 {
4001 curwin->w_scbind_pos += topline_diff;
4002 topline = curwin->w_scbind_pos;
4003 if (topline > curbuf->b_ml.ml_line_count)
4004 topline = curbuf->b_ml.ml_line_count;
4005 if (topline < 1)
4006 topline = 1;
4007
4008 y = topline - curwin->w_topline;
4009 if (y > 0)
4010 scrollup(y, FALSE);
4011 else
4012 scrolldown(-y, FALSE);
4013 }
4014
4015 redraw_later(VALID);
4016 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019
4020 /*
4021 * do the horizontal scroll
4022 */
4023 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4024 {
4025 curwin->w_leftcol = tgt_leftcol;
4026 leftcol_changed();
4027 }
4028 }
4029 }
4030
4031 /*
4032 * reset current-window
4033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 VIsual_select = old_VIsual_select;
4035 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 curwin = old_curwin;
4037 curbuf = old_curbuf;
4038}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
4040/*
4041 * Command character that's ignored.
4042 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004043 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004046nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004048 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049}
4050
4051/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004052 * Command character that doesn't do anything, but unlike nv_ignore() does
4053 * start edit(). Used for "startinsert" executed while starting up.
4054 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004055 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004056nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004057{
4058}
4059
4060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 * Command character doesn't exist.
4062 */
4063 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004064nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
4066 clearopbeep(cap->oap);
4067}
4068
4069/*
4070 * <Help> and <F1> commands.
4071 */
4072 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004073nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
4075 if (!checkclearopq(cap->oap))
4076 ex_help(NULL);
4077}
4078
4079/*
4080 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4081 */
4082 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004083nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084{
Bram Moolenaarf2732452018-06-03 14:47:35 +02004085#ifdef FEAT_JOB_CHANNEL
4086 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4087 clearopbeep(cap->oap);
4088 else
4089#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01004090 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004091 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004092 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004093 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4094 op_addsub(cap->oap, cap->count1, cap->arg);
4095 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004096 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004097 else if (VIsual_active)
4098 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004099 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004100 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101}
4102
4103/*
4104 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4105 */
4106 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004107nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108{
4109 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004110 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004111 if (mod_mask & MOD_MASK_CTRL)
4112 {
4113 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4114 if (cap->arg == BACKWARD)
4115 goto_tabpage(-(int)cap->count1);
4116 else
4117 goto_tabpage((int)cap->count0);
4118 }
4119 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004120 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122}
4123
4124/*
4125 * Implementation of "gd" and "gD" command.
4126 */
4127 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004128nv_gd(
4129 oparg_T *oap,
4130 int nchar,
4131 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 char_u *ptr;
4135
Bram Moolenaard9d30582005-05-18 22:10:28 +00004136 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004137 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4138 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004140#ifdef FEAT_FOLDING
4141 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4142 foldOpenCursor();
4143#endif
4144}
4145
4146/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004147 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4148 * otherwise.
4149 */
4150 static int
4151is_ident(char_u *line, int offset)
4152{
4153 int i;
4154 int incomment = FALSE;
4155 int instring = 0;
4156 int prev = 0;
4157
4158 for (i = 0; i < offset && line[i] != NUL; i++)
4159 {
4160 if (instring != 0)
4161 {
4162 if (prev != '\\' && line[i] == instring)
4163 instring = 0;
4164 }
4165 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4166 {
4167 instring = line[i];
4168 }
4169 else
4170 {
4171 if (incomment)
4172 {
4173 if (prev == '*' && line[i] == '/')
4174 incomment = FALSE;
4175 }
4176 else if (prev == '/' && line[i] == '*')
4177 {
4178 incomment = TRUE;
4179 }
4180 else if (prev == '/' && line[i] == '/')
4181 {
4182 return FALSE;
4183 }
4184 }
4185
4186 prev = line[i];
4187 }
4188
4189 return incomment == FALSE && instring == 0;
4190}
4191
4192/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004193 * Search for variable declaration of "ptr[len]".
4194 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4195 * current file ("gD").
4196 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004197 * Return FAIL when not found.
4198 */
4199 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004200find_decl(
4201 char_u *ptr,
4202 int len,
4203 int locally,
4204 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004205 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004206{
4207 char_u *pat;
4208 pos_T old_pos;
4209 pos_T par_pos;
4210 pos_T found_pos;
4211 int t;
4212 int save_p_ws;
4213 int save_p_scs;
4214 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004215 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004216 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004217 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004218
4219 if ((pat = alloc(len + 7)) == NULL)
4220 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004221
4222 /* Put "\V" before the pattern to avoid that the special meaning of "."
4223 * and "~" causes trouble. */
4224 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4225 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 old_pos = curwin->w_cursor;
4227 save_p_ws = p_ws;
4228 save_p_scs = p_scs;
4229 p_ws = FALSE; /* don't wrap around end of file now */
4230 p_scs = FALSE; /* don't switch ignorecase off now */
4231
4232 /*
4233 * With "gD" go to line 1.
4234 * With "gd" Search back for the start of the current function, then go
4235 * back until a blank line. If this fails go to line 1.
4236 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004237 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
4239 setpcmark(); /* Set in findpar() otherwise */
4240 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004241 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243 else
4244 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004245 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4247 --curwin->w_cursor.lnum;
4248 }
4249 curwin->w_cursor.col = 0;
4250
4251 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004252 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004253 for (;;)
4254 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004255 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004256 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004257 if (curwin->w_cursor.lnum >= old_pos.lnum)
4258 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004259
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004260 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004261 {
4262 pos_T *pos;
4263
4264 /* Check that the block the match is in doesn't end before the
4265 * position where we started the search from. */
4266 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4267 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4268 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004269 {
4270 /* There can't be a useful match before the end of this block.
4271 * Skip to the end. */
4272 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004273 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004274 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004275 }
4276
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004277 if (t == FAIL)
4278 {
4279 /* If we previously found a valid position, use it. */
4280 if (found_pos.lnum != 0)
4281 {
4282 curwin->w_cursor = found_pos;
4283 t = OK;
4284 }
4285 break;
4286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004288 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004289 {
4290 /* Ignore this line, continue at start of next line. */
4291 ++curwin->w_cursor.lnum;
4292 curwin->w_cursor.col = 0;
4293 continue;
4294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004296 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4297
4298 /* If the current position is not a valid identifier and a previous
4299 * match is present, favor that one instead. */
4300 if (!valid && found_pos.lnum != 0)
4301 {
4302 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004303 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004304 }
4305
4306 /* Global search: use first valid match found */
4307 if (valid && !locally)
4308 break;
4309 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004310 {
4311 /* If we previously found a valid position, use it. */
4312 if (found_pos.lnum != 0)
4313 curwin->w_cursor = found_pos;
4314 break;
4315 }
4316
Bram Moolenaar226630a2016-10-08 19:21:31 +02004317 /* For finding a local variable and the match is before the "{" or
4318 * inside a comment, continue searching. For K&R style function
4319 * declarations this skips the function header without types. */
4320 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004321 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004322 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004323 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004324 /* Remove SEARCH_START from flags to avoid getting stuck at one
4325 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004326 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004328
4329 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004331 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 curwin->w_cursor = old_pos;
4333 }
4334 else
4335 {
4336 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 /* "n" searches forward now */
4338 reset_search_dir();
4339 }
4340
4341 vim_free(pat);
4342 p_ws = save_p_ws;
4343 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004344
4345 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346}
4347
4348/*
4349 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4350 * lines rather than lines in the file.
4351 * 'dist' must be positive.
4352 *
4353 * Return OK if able to move cursor, FAIL otherwise.
4354 */
4355 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004356nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
4358 int linelen = linetabsize(ml_get_curline());
4359 int retval = OK;
4360 int atend = FALSE;
4361 int n;
4362 int col_off1; /* margin offset for first screen line */
4363 int col_off2; /* margin offset for wrapped screen line */
4364 int width1; /* text width for first screen line */
4365 int width2; /* test width for wrapped screen line */
4366
4367 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004368 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
4370 col_off1 = curwin_col_off();
4371 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004372 width1 = curwin->w_width - col_off1;
4373 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004374 if (width2 == 0)
4375 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /*
4380 * Instead of sticking at the last character of the buffer line we
4381 * try to stick in the last column of the screen.
4382 */
4383 if (curwin->w_curswant == MAXCOL)
4384 {
4385 atend = TRUE;
4386 validate_virtcol();
4387 if (width1 <= 0)
4388 curwin->w_curswant = 0;
4389 else
4390 {
4391 curwin->w_curswant = width1 - 1;
4392 if (curwin->w_virtcol > curwin->w_curswant)
4393 curwin->w_curswant += ((curwin->w_virtcol
4394 - curwin->w_curswant - 1) / width2 + 1) * width2;
4395 }
4396 }
4397 else
4398 {
4399 if (linelen > width1)
4400 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4401 else
4402 n = width1;
4403 if (curwin->w_curswant > (colnr_T)n + 1)
4404 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4405 * width2;
4406 }
4407
4408 while (dist--)
4409 {
4410 if (dir == BACKWARD)
4411 {
4412 if ((long)curwin->w_curswant >= width2)
4413 /* move back within line */
4414 curwin->w_curswant -= width2;
4415 else
4416 {
4417 /* to previous line */
4418 if (curwin->w_cursor.lnum == 1)
4419 {
4420 retval = FAIL;
4421 break;
4422 }
4423 --curwin->w_cursor.lnum;
4424#ifdef FEAT_FOLDING
4425 /* Move to the start of a closed fold. Don't do that when
4426 * 'foldopen' contains "all": it will open in a moment. */
4427 if (!(fdo_flags & FDO_ALL))
4428 (void)hasFolding(curwin->w_cursor.lnum,
4429 &curwin->w_cursor.lnum, NULL);
4430#endif
4431 linelen = linetabsize(ml_get_curline());
4432 if (linelen > width1)
4433 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4434 + 1) * width2;
4435 }
4436 }
4437 else /* dir == FORWARD */
4438 {
4439 if (linelen > width1)
4440 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4441 else
4442 n = width1;
4443 if (curwin->w_curswant + width2 < (colnr_T)n)
4444 /* move forward within line */
4445 curwin->w_curswant += width2;
4446 else
4447 {
4448 /* to next line */
4449#ifdef FEAT_FOLDING
4450 /* Move to the end of a closed fold. */
4451 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4452 &curwin->w_cursor.lnum);
4453#endif
4454 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4455 {
4456 retval = FAIL;
4457 break;
4458 }
4459 curwin->w_cursor.lnum++;
4460 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004461 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 }
4463 }
4464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004467 if (virtual_active() && atend)
4468 coladvance(MAXCOL);
4469 else
4470 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4473 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004474 colnr_T virtcol;
4475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 /*
4477 * Check for landing on a character that got split at the end of the
4478 * last line. We want to advance a screenline, not end up in the same
4479 * screenline or move two screenlines.
4480 */
4481 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004482 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004483#if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004484 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4485 virtcol -= vim_strsize(p_sbr);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004486#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004487
4488 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 && (curwin->w_curswant < (colnr_T)width1
4490 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4491 : ((curwin->w_curswant - width1) % width2
4492 > (colnr_T)width2 / 2)))
4493 --curwin->w_cursor.col;
4494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
4496 if (atend)
4497 curwin->w_curswant = MAXCOL; /* stick in the last column */
4498
4499 return retval;
4500}
4501
4502#ifdef FEAT_MOUSE
4503/*
4504 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4505 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004506 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4507 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 */
4509 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004510nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004512 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004514 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
4516 int row, col;
4517
4518 row = mouse_row;
4519 col = mouse_col;
4520
4521 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004522 wp = mouse_find_win(&row, &col);
4523 if (wp == NULL)
4524 return;
4525 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 curbuf = curwin->w_buffer;
4527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004529 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004531# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004532 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004533 /* This window is a terminal window, send the mouse event there.
4534 * Set "typed" to FALSE to avoid an endless loop. */
4535 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004536 else
4537# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004538 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4539 {
4540 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4541 }
4542 else
4543 {
4544 cap->count1 = 3;
4545 cap->count0 = 3;
4546 nv_scroll_line(cap);
4547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004549# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 else
4551 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004552 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4553 if (!curwin->w_p_wrap)
4554 {
4555 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004556
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004557 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004558 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004559 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4560 if (val < 0)
4561 val = 0;
4562
4563 gui_do_horiz_scroll(val, TRUE);
4564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004566# endif
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +01004567# ifdef FEAT_SYN_HL
4568 if (curwin != old_curwin && curwin->w_p_cul)
4569 redraw_for_cursorline(curwin);
4570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 curwin->w_redr_status = TRUE;
4573
4574 curwin = old_curwin;
4575 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576}
4577
4578/*
4579 * Mouse clicks and drags.
4580 */
4581 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004582nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583{
4584 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4585}
4586#endif
4587
4588/*
4589 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4590 * cap->arg must be TRUE for CTRL-E.
4591 */
4592 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004593nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594{
4595 if (!checkclearop(cap->oap))
4596 scroll_redraw(cap->arg, cap->count1);
4597}
4598
4599/*
4600 * Scroll "count" lines up or down, and redraw.
4601 */
4602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004603scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 linenr_T prev_topline = curwin->w_topline;
4606#ifdef FEAT_DIFF
4607 int prev_topfill = curwin->w_topfill;
4608#endif
4609 linenr_T prev_lnum = curwin->w_cursor.lnum;
4610
4611 if (up)
4612 scrollup(count, TRUE);
4613 else
4614 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01004615 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4618 * valid, otherwise the screen jumps back at the end of the file. */
4619 cursor_correct();
4620 check_cursor_moved(curwin);
4621 curwin->w_valid |= VALID_TOPLINE;
4622
4623 /* If moved back to where we were, at least move the cursor, otherwise
4624 * we get stuck at one position. Don't move the cursor up if the
4625 * first line of the buffer is already on the screen */
4626 while (curwin->w_topline == prev_topline
4627#ifdef FEAT_DIFF
4628 && curwin->w_topfill == prev_topfill
4629#endif
4630 )
4631 {
4632 if (up)
4633 {
4634 if (curwin->w_cursor.lnum > prev_lnum
4635 || cursor_down(1L, FALSE) == FAIL)
4636 break;
4637 }
4638 else
4639 {
4640 if (curwin->w_cursor.lnum < prev_lnum
4641 || prev_topline == 1L
4642 || cursor_up(1L, FALSE) == FAIL)
4643 break;
4644 }
4645 /* Mark w_topline as valid, otherwise the screen jumps back at the
4646 * end of the file. */
4647 check_cursor_moved(curwin);
4648 curwin->w_valid |= VALID_TOPLINE;
4649 }
4650 }
4651 if (curwin->w_cursor.lnum != prev_lnum)
4652 coladvance(curwin->w_curswant);
4653 redraw_later(VALID);
4654}
4655
4656/*
4657 * Commands that start with "z".
4658 */
4659 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004660nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
4662 long n;
4663 colnr_T col;
4664 int nchar = cap->nchar;
4665#ifdef FEAT_FOLDING
4666 long old_fdl = curwin->w_p_fdl;
4667 int old_fen = curwin->w_p_fen;
4668#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004669#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004670 int undo = FALSE;
4671#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01004672 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 if (VIM_ISDIGIT(nchar))
4675 {
4676 /*
4677 * "z123{nchar}": edit the count before obtaining {nchar}
4678 */
4679 if (checkclearop(cap->oap))
4680 return;
4681 n = nchar - '0';
4682 for (;;)
4683 {
4684#ifdef USE_ON_FLY_SCROLL
4685 dont_scroll = TRUE; /* disallow scrolling here */
4686#endif
4687 ++no_mapping;
4688 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004689 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 --no_mapping;
4692 --allow_keys;
4693#ifdef FEAT_CMDL_INFO
4694 (void)add_to_showcmd(nchar);
4695#endif
4696 if (nchar == K_DEL || nchar == K_KDEL)
4697 n /= 10;
4698 else if (VIM_ISDIGIT(nchar))
4699 n = n * 10 + (nchar - '0');
4700 else if (nchar == CAR)
4701 {
4702#ifdef FEAT_GUI
4703 need_mouse_correct = TRUE;
4704#endif
4705 win_setheight((int)n);
4706 break;
4707 }
4708 else if (nchar == 'l'
4709 || nchar == 'h'
4710 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004711 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
4713 cap->count1 = n ? n * cap->count1 : cap->count1;
4714 goto dozet;
4715 }
4716 else
4717 {
4718 clearopbeep(cap->oap);
4719 break;
4720 }
4721 }
4722 cap->oap->op_type = OP_NOP;
4723 return;
4724 }
4725
4726dozet:
4727 if (
4728#ifdef FEAT_FOLDING
4729 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4730 * and "zC" only in Visual mode. "zj" and "zk" are motion
4731 * commands. */
4732 cap->nchar != 'f' && cap->nchar != 'F'
4733 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4734 && cap->nchar != 'j' && cap->nchar != 'k'
4735 &&
4736#endif
4737 checkclearop(cap->oap))
4738 return;
4739
4740 /*
4741 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4742 * If line number given, set cursor.
4743 */
4744 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4745 && cap->count0
4746 && cap->count0 != curwin->w_cursor.lnum)
4747 {
4748 setpcmark();
4749 if (cap->count0 > curbuf->b_ml.ml_line_count)
4750 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4751 else
4752 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004753 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
4755
4756 switch (nchar)
4757 {
4758 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4759 case '+':
4760 if (cap->count0 == 0)
4761 {
4762 /* No count given: put cursor at the line below screen */
4763 validate_botline(); /* make sure w_botline is valid */
4764 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4765 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4766 else
4767 curwin->w_cursor.lnum = curwin->w_botline;
4768 }
4769 /* FALLTHROUGH */
4770 case NL:
4771 case CAR:
4772 case K_KENTER:
4773 beginline(BL_WHITE | BL_FIX);
4774 /* FALLTHROUGH */
4775
4776 case 't': scroll_cursor_top(0, TRUE);
4777 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004778 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 break;
4780
4781 /* "z." and "zz": put cursor in middle of screen */
4782 case '.': beginline(BL_WHITE | BL_FIX);
4783 /* FALLTHROUGH */
4784
4785 case 'z': scroll_cursor_halfway(TRUE);
4786 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004787 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 break;
4789
4790 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4791 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4792 * when <count> is at bottom of window, and puts that one at
4793 * bottom of window. */
4794 if (cap->count0 != 0)
4795 {
4796 scroll_cursor_bot(0, TRUE);
4797 curwin->w_cursor.lnum = curwin->w_topline;
4798 }
4799 else if (curwin->w_topline == 1)
4800 curwin->w_cursor.lnum = 1;
4801 else
4802 curwin->w_cursor.lnum = curwin->w_topline - 1;
4803 /* FALLTHROUGH */
4804 case '-':
4805 beginline(BL_WHITE | BL_FIX);
4806 /* FALLTHROUGH */
4807
4808 case 'b': scroll_cursor_bot(0, TRUE);
4809 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004810 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 break;
4812
4813 /* "zH" - scroll screen right half-page */
4814 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004815 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 /* FALLTHROUGH */
4817
4818 /* "zh" - scroll screen to the right */
4819 case 'h':
4820 case K_LEFT:
4821 if (!curwin->w_p_wrap)
4822 {
4823 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4824 curwin->w_leftcol = 0;
4825 else
4826 curwin->w_leftcol -= (colnr_T)cap->count1;
4827 leftcol_changed();
4828 }
4829 break;
4830
4831 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004832 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 /* FALLTHROUGH */
4834
4835 /* "zl" - scroll screen to the left */
4836 case 'l':
4837 case K_RIGHT:
4838 if (!curwin->w_p_wrap)
4839 {
4840 /* scroll the window left */
4841 curwin->w_leftcol += (colnr_T)cap->count1;
4842 leftcol_changed();
4843 }
4844 break;
4845
4846 /* "zs" - scroll screen, cursor at the start */
4847 case 's': if (!curwin->w_p_wrap)
4848 {
4849#ifdef FEAT_FOLDING
4850 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4851 col = 0; /* like the cursor is in col 0 */
4852 else
4853#endif
4854 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01004855 if ((long)col > siso)
4856 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 else
4858 col = 0;
4859 if (curwin->w_leftcol != col)
4860 {
4861 curwin->w_leftcol = col;
4862 redraw_later(NOT_VALID);
4863 }
4864 }
4865 break;
4866
4867 /* "ze" - scroll screen, cursor at the end */
4868 case 'e': if (!curwin->w_p_wrap)
4869 {
4870#ifdef FEAT_FOLDING
4871 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4872 col = 0; /* like the cursor is in col 0 */
4873 else
4874#endif
4875 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004876 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01004877 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 col = 0;
4879 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01004880 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (curwin->w_leftcol != col)
4882 {
4883 curwin->w_leftcol = col;
4884 redraw_later(NOT_VALID);
4885 }
4886 }
4887 break;
4888
4889#ifdef FEAT_FOLDING
4890 /* "zF": create fold command */
4891 /* "zf": create fold operator */
4892 case 'F':
4893 case 'f': if (foldManualAllowed(TRUE))
4894 {
4895 cap->nchar = 'f';
4896 nv_operator(cap);
4897 curwin->w_p_fen = TRUE;
4898
4899 /* "zF" is like "zfzf" */
4900 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4901 {
4902 nv_operator(cap);
4903 finish_op = TRUE;
4904 }
4905 }
4906 else
4907 clearopbeep(cap->oap);
4908 break;
4909
4910 /* "zd": delete fold at cursor */
4911 /* "zD": delete fold at cursor recursively */
4912 case 'd':
4913 case 'D': if (foldManualAllowed(FALSE))
4914 {
4915 if (VIsual_active)
4916 nv_operator(cap);
4917 else
4918 deleteFold(curwin->w_cursor.lnum,
4919 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4920 }
4921 break;
4922
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004923 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 case 'E': if (foldmethodIsManual(curwin))
4925 {
4926 clearFolding(curwin);
4927 changed_window_setting();
4928 }
4929 else if (foldmethodIsMarker(curwin))
4930 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4931 TRUE, FALSE);
4932 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004933 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 break;
4935
4936 /* "zn": fold none: reset 'foldenable' */
4937 case 'n': curwin->w_p_fen = FALSE;
4938 break;
4939
4940 /* "zN": fold Normal: set 'foldenable' */
4941 case 'N': curwin->w_p_fen = TRUE;
4942 break;
4943
4944 /* "zi": invert folding: toggle 'foldenable' */
4945 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4946 break;
4947
4948 /* "za": open closed fold or close open fold at cursor */
4949 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4950 openFold(curwin->w_cursor.lnum, cap->count1);
4951 else
4952 {
4953 closeFold(curwin->w_cursor.lnum, cap->count1);
4954 curwin->w_p_fen = TRUE;
4955 }
4956 break;
4957
4958 /* "zA": open fold at cursor recursively */
4959 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4960 openFoldRecurse(curwin->w_cursor.lnum);
4961 else
4962 {
4963 closeFoldRecurse(curwin->w_cursor.lnum);
4964 curwin->w_p_fen = TRUE;
4965 }
4966 break;
4967
4968 /* "zo": open fold at cursor or Visual area */
4969 case 'o': if (VIsual_active)
4970 nv_operator(cap);
4971 else
4972 openFold(curwin->w_cursor.lnum, cap->count1);
4973 break;
4974
4975 /* "zO": open fold recursively */
4976 case 'O': if (VIsual_active)
4977 nv_operator(cap);
4978 else
4979 openFoldRecurse(curwin->w_cursor.lnum);
4980 break;
4981
4982 /* "zc": close fold at cursor or Visual area */
4983 case 'c': if (VIsual_active)
4984 nv_operator(cap);
4985 else
4986 closeFold(curwin->w_cursor.lnum, cap->count1);
4987 curwin->w_p_fen = TRUE;
4988 break;
4989
4990 /* "zC": close fold recursively */
4991 case 'C': if (VIsual_active)
4992 nv_operator(cap);
4993 else
4994 closeFoldRecurse(curwin->w_cursor.lnum);
4995 curwin->w_p_fen = TRUE;
4996 break;
4997
4998 /* "zv": open folds at the cursor */
4999 case 'v': foldOpenCursor();
5000 break;
5001
5002 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5003 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005004 curwin->w_foldinvalid = TRUE; /* recompute folds */
5005 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 foldOpenCursor();
5007 break;
5008
5009 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5010 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005011 curwin->w_foldinvalid = TRUE; /* recompute folds */
5012 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 break;
5014
5015 /* "zm": fold more */
5016 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005017 {
5018 curwin->w_p_fdl -= cap->count1;
5019 if (curwin->w_p_fdl < 0)
5020 curwin->w_p_fdl = 0;
5021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 old_fdl = -1; /* force an update */
5023 curwin->w_p_fen = TRUE;
5024 break;
5025
5026 /* "zM": close all folds */
5027 case 'M': curwin->w_p_fdl = 0;
5028 old_fdl = -1; /* force an update */
5029 curwin->w_p_fen = TRUE;
5030 break;
5031
5032 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005033 case 'r': curwin->w_p_fdl += cap->count1;
5034 {
5035 int d = getDeepestNesting();
5036
5037 if (curwin->w_p_fdl >= d)
5038 curwin->w_p_fdl = d;
5039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 break;
5041
5042 /* "zR": open all folds */
5043 case 'R': curwin->w_p_fdl = getDeepestNesting();
5044 old_fdl = -1; /* force an update */
5045 break;
5046
5047 case 'j': /* "zj" move to next fold downwards */
5048 case 'k': /* "zk" move to next fold upwards */
5049 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5050 cap->count1) == FAIL)
5051 clearopbeep(cap->oap);
5052 break;
5053
5054#endif /* FEAT_FOLDING */
5055
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005056#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005057 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5058 ++no_mapping;
5059 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005060 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005061 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005062 --no_mapping;
5063 --allow_keys;
5064#ifdef FEAT_CMDL_INFO
5065 (void)add_to_showcmd(nchar);
5066#endif
5067 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5068 {
5069 clearopbeep(cap->oap);
5070 break;
5071 }
5072 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005073 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005074
Bram Moolenaarb765d632005-06-07 21:00:02 +00005075 case 'g': /* "zg": add good word to word list */
5076 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005077 case 'G': /* "zG": add good word to temp word list */
5078 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005079 {
5080 char_u *ptr = NULL;
5081 int len;
5082
5083 if (checkclearop(cap->oap))
5084 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005085 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5086 == FAIL)
5087 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005088 if (ptr == NULL)
5089 {
5090 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005091
Bram Moolenaar134bf072013-09-25 18:54:24 +02005092 /* Find bad word under the cursor. When 'spell' is
5093 * off this fails and find_ident_under_cursor() is
5094 * used below. */
5095 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005096 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005097 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005098 if (len != 0 && curwin->w_cursor.col <= pos.col)
5099 ptr = ml_get_pos(&curwin->w_cursor);
5100 curwin->w_cursor = pos;
5101 }
5102
Bram Moolenaarb765d632005-06-07 21:00:02 +00005103 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5104 FIND_IDENT)) == 0)
5105 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005106 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005107 (nchar == 'G' || nchar == 'W')
5108 ? 0 : (int)cap->count1,
5109 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005110 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005111 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005112
Bram Moolenaar43abc522005-12-10 20:15:02 +00005113 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005114 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005115 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005116 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005117#endif
5118
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 default: clearopbeep(cap->oap);
5120 }
5121
5122#ifdef FEAT_FOLDING
5123 /* Redraw when 'foldenable' changed */
5124 if (old_fen != curwin->w_p_fen)
5125 {
5126# ifdef FEAT_DIFF
5127 win_T *wp;
5128
5129 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5130 {
5131 /* Adjust 'foldenable' in diff-synced windows. */
5132 FOR_ALL_WINDOWS(wp)
5133 {
5134 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5135 {
5136 wp->w_p_fen = curwin->w_p_fen;
5137 changed_window_setting_win(wp);
5138 }
5139 }
5140 }
5141# endif
5142 changed_window_setting();
5143 }
5144
5145 /* Redraw when 'foldlevel' changed. */
5146 if (old_fdl != curwin->w_p_fdl)
5147 newFoldLevel();
5148#endif
5149}
5150
5151#ifdef FEAT_GUI
5152/*
5153 * Vertical scrollbar movement.
5154 */
5155 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005156nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157{
5158 if (cap->oap->op_type != OP_NOP)
5159 clearopbeep(cap->oap);
5160
5161 /* Even if an operator was pending, we still want to scroll */
5162 gui_do_scroll();
5163}
5164
5165/*
5166 * Horizontal scrollbar movement.
5167 */
5168 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005169nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170{
5171 if (cap->oap->op_type != OP_NOP)
5172 clearopbeep(cap->oap);
5173
5174 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005175 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176}
5177#endif
5178
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005179#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005180/*
5181 * Click in GUI tab.
5182 */
5183 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005184nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005185{
5186 if (cap->oap->op_type != OP_NOP)
5187 clearopbeep(cap->oap);
5188
5189 /* Even if an operator was pending, we still want to jump tabs. */
5190 goto_tabpage(current_tab);
5191}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005192
5193/*
5194 * Selected item in tab line menu.
5195 */
5196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005197nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005198{
5199 if (cap->oap->op_type != OP_NOP)
5200 clearopbeep(cap->oap);
5201
5202 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005203 handle_tabmenu();
5204}
5205
5206/*
5207 * Handle selecting an item of the GUI tab line menu.
5208 * Used in Normal and Insert mode.
5209 */
5210 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005211handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005212{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005213 switch (current_tabmenu)
5214 {
5215 case TABLINE_MENU_CLOSE:
5216 if (current_tab == 0)
5217 do_cmdline_cmd((char_u *)"tabclose");
5218 else
5219 {
5220 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5221 current_tab);
5222 do_cmdline_cmd(IObuff);
5223 }
5224 break;
5225
5226 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005227 if (current_tab == 0)
5228 do_cmdline_cmd((char_u *)"$tabnew");
5229 else
5230 {
5231 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5232 current_tab - 1);
5233 do_cmdline_cmd(IObuff);
5234 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005235 break;
5236
5237 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005238 if (current_tab == 0)
5239 do_cmdline_cmd((char_u *)"browse $tabnew");
5240 else
5241 {
5242 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5243 current_tab - 1);
5244 do_cmdline_cmd(IObuff);
5245 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005246 break;
5247 }
5248}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005249#endif
5250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251/*
5252 * "Q" command.
5253 */
5254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005255nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256{
5257 /*
5258 * Ignore 'Q' in Visual mode, just give a beep.
5259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005261 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005262 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 do_exmode(FALSE);
5264}
5265
5266/*
5267 * Handle a ":" command.
5268 */
5269 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005270nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005273 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if (VIsual_active)
5276 nv_operator(cap);
5277 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
5279 if (cap->oap->op_type != OP_NOP)
5280 {
5281 /* Using ":" as a movement is characterwise exclusive. */
5282 cap->oap->motion_type = MCHAR;
5283 cap->oap->inclusive = FALSE;
5284 }
5285 else if (cap->count0)
5286 {
5287 /* translate "count:" into ":.,.+(count - 1)" */
5288 stuffcharReadbuff('.');
5289 if (cap->count0 > 1)
5290 {
5291 stuffReadbuff((char_u *)",.+");
5292 stuffnumReadbuff((long)cap->count0 - 1L);
5293 }
5294 }
5295
5296 /* When typing, don't type below an old message */
5297 if (KeyTyped)
5298 compute_cmdrow();
5299
5300 old_p_im = p_im;
5301
5302 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005303 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5305
5306 /* If 'insertmode' changed, enter or exit Insert mode */
5307 if (p_im != old_p_im)
5308 {
5309 if (p_im)
5310 restart_edit = 'i';
5311 else
5312 restart_edit = 0;
5313 }
5314
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005315 if (cmd_result == FAIL)
5316 /* The Ex command failed, do not execute the operator. */
5317 clearop(cap->oap);
5318 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5320 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005321 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5322 || did_emsg
5323 ))
5324 /* The start of the operator has become invalid by the Ex command.
5325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 clearopbeep(cap->oap);
5327 }
5328}
5329
5330/*
5331 * Handle CTRL-G command.
5332 */
5333 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005334nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 if (VIsual_active) /* toggle Selection/Visual mode */
5337 {
5338 VIsual_select = !VIsual_select;
5339 showmode();
5340 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005341 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 /* print full name if count given or :cd used */
5343 fileinfo((int)cap->count0, FALSE, TRUE);
5344}
5345
5346/*
5347 * Handle CTRL-H <Backspace> command.
5348 */
5349 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005350nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 if (VIsual_active && VIsual_select)
5353 {
5354 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5355 v_visop(cap);
5356 }
5357 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 nv_left(cap);
5359}
5360
5361/*
5362 * CTRL-L: clear screen and redraw.
5363 */
5364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005365nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 if (!checkclearop(cap->oap))
5368 {
5369#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5370 /*
5371 * Right now, the BeBox doesn't seem to have an easy way to detect
5372 * window resizing, so we cheat and make the user detect it
5373 * manually with CTRL-L instead
5374 */
5375 ui_get_shellsize();
5376#endif
5377#ifdef FEAT_SYN_HL
5378 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005379 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005380# ifdef FEAT_RELTIME
5381 {
5382 win_T *wp;
5383
5384 FOR_ALL_WINDOWS(wp)
5385 wp->w_s->b_syn_slow = FALSE;
5386 }
5387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388#endif
5389 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005390#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
5391# ifdef VIMDLL
5392 if (!gui.in_use)
5393# endif
5394 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01005395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397}
5398
5399/*
5400 * CTRL-O: In Select mode: switch to Visual mode for one command.
5401 * Otherwise: Go to older pcmark.
5402 */
5403 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005404nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 if (VIsual_active && VIsual_select)
5407 {
5408 VIsual_select = FALSE;
5409 showmode();
5410 restart_VIsual_select = 2; /* restart Select mode later */
5411 }
5412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 cap->count1 = -cap->count1;
5415 nv_pcmark(cap);
5416 }
5417}
5418
5419/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01005420 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
5421 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 */
5423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005424nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425{
5426 if (!checkclearopq(cap->oap))
5427 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5428 GETF_SETMARK|GETF_ALT, FALSE);
5429}
5430
5431/*
5432 * "Z" commands.
5433 */
5434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005435nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 if (!checkclearopq(cap->oap))
5438 {
5439 switch (cap->nchar)
5440 {
5441 /* "ZZ": equivalent to ":x". */
5442 case 'Z': do_cmdline_cmd((char_u *)"x");
5443 break;
5444
5445 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5446 case 'Q': do_cmdline_cmd((char_u *)"q!");
5447 break;
5448
5449 default: clearopbeep(cap->oap);
5450 }
5451 }
5452}
5453
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454/*
5455 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5456 */
5457 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005458do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 oparg_T oa;
5461 cmdarg_T ca;
5462
5463 clear_oparg(&oa);
5464 vim_memset(&ca, 0, sizeof(ca));
5465 ca.oap = &oa;
5466 ca.cmdchar = c1;
5467 ca.nchar = c2;
5468 nv_ident(&ca);
5469}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
5471/*
5472 * Handle the commands that use the word under the cursor.
5473 * [g] CTRL-] :ta to current identifier
5474 * [g] 'K' run program for current identifier
5475 * [g] '*' / to current identifier or string
5476 * [g] '#' ? to current identifier or string
5477 * g ']' :tselect for current identifier
5478 */
5479 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005480nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481{
5482 char_u *ptr = NULL;
5483 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005484 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005485 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 char_u *p;
5487 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005488 int kp_help; /* 'keywordprg' is ":he" */
5489 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 int n = 0; /* init for GCC */
5491 int cmdchar;
5492 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005493 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 char_u *aux_ptr;
5495 int isman;
5496 int isman_s;
5497
5498 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5499 {
5500 cmdchar = cap->nchar;
5501 g_cmd = TRUE;
5502 }
5503 else
5504 {
5505 cmdchar = cap->cmdchar;
5506 g_cmd = FALSE;
5507 }
5508
5509 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5510 cmdchar = '#';
5511
5512 /*
5513 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5514 */
5515 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5518 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 if (checkclearopq(cap->oap))
5520 return;
5521 }
5522
5523 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5524 (cmdchar == '*' || cmdchar == '#')
5525 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5526 {
5527 clearop(cap->oap);
5528 return;
5529 }
5530
5531 /* Allocate buffer to put the command in. Inserting backslashes can
5532 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5533 * and some numbers. */
5534 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5535 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5536 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005537 if (kp_help && *skipwhite(ptr) == NUL)
5538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005539 emsg(_(e_noident)); /* found white space only */
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005540 return;
5541 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005542 kp_ex = (*kp == ':');
5543 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5544 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 if (buf == NULL)
5546 return;
5547 buf[0] = NUL;
5548
5549 switch (cmdchar)
5550 {
5551 case '*':
5552 case '#':
5553 /*
5554 * Put cursor at start of word, makes search skip the word
5555 * under the cursor.
5556 * Call setpcmark() first, so "*``" puts the cursor back where
5557 * it was.
5558 */
5559 setpcmark();
5560 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5561
5562 if (!g_cmd && vim_iswordp(ptr))
5563 STRCPY(buf, "\\<");
5564 no_smartcase = TRUE; /* don't use 'smartcase' now */
5565 break;
5566
5567 case 'K':
5568 if (kp_help)
5569 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005570 else if (kp_ex)
5571 {
5572 if (cap->count0 != 0)
5573 vim_snprintf((char *)buf, buflen, "%s %ld",
5574 kp, cap->count0);
5575 else
5576 STRCPY(buf, kp);
5577 STRCAT(buf, " ");
5578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 else
5580 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005581 /* An external command will probably use an argument starting
5582 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005583 while (*ptr == '-' && n > 0)
5584 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005585 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005586 --n;
5587 }
5588 if (n == 0)
5589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005590 emsg(_(e_noident)); /* found dashes only */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005591 vim_free(buf);
5592 return;
5593 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005594
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 /* When a count is given, turn it into a range. Is this
5596 * really what we want? */
5597 isman = (STRCMP(kp, "man") == 0);
5598 isman_s = (STRCMP(kp, "man -s") == 0);
5599 if (cap->count0 != 0 && !(isman || isman_s))
5600 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5601
5602 STRCAT(buf, "! ");
5603 if (cap->count0 == 0 && isman_s)
5604 STRCAT(buf, "man");
5605 else
5606 STRCAT(buf, kp);
5607 STRCAT(buf, " ");
5608 if (cap->count0 != 0 && (isman || isman_s))
5609 {
5610 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5611 STRCAT(buf, " ");
5612 }
5613 }
5614 break;
5615
5616 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005617 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618#ifdef FEAT_CSCOPE
5619 if (p_cst)
5620 STRCPY(buf, "cstag ");
5621 else
5622#endif
5623 STRCPY(buf, "ts ");
5624 break;
5625
5626 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005627 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 if (curbuf->b_help)
5629 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005631 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005632 if (g_cmd)
5633 STRCPY(buf, "tj ");
5634 else
5635 sprintf((char *)buf, "%ldta ", cap->count0);
5636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638
5639 /*
5640 * Now grab the chars in the identifier
5641 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005642 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005644 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005645 if (kp_ex)
5646 /* Escape the argument properly for an Ex command */
5647 p = vim_strsave_fnameescape(ptr, FALSE);
5648 else
5649 /* Escape the argument properly for a shell command */
5650 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005651 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005652 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005654 vim_free(buf);
5655 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005657 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5658 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005659 {
5660 vim_free(buf);
5661 vim_free(p);
5662 return;
5663 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005664 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005665 STRCAT(buf, p);
5666 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005668 else
5669 {
5670 if (cmdchar == '*')
5671 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5672 else if (cmdchar == '#')
5673 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005674 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005675 {
5676 if (curbuf->b_help)
5677 /* ":help" handles unescaped argument */
5678 aux_ptr = (char_u *)"";
5679 else
5680 aux_ptr = (char_u *)"\\|\"\n[";
5681 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005682 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005683 aux_ptr = (char_u *)"\\|\"\n*?[";
5684
5685 p = buf + STRLEN(buf);
5686 while (n-- > 0)
5687 {
5688 /* put a backslash before \ and some others */
5689 if (vim_strchr(aux_ptr, *ptr) != NULL)
5690 *p++ = '\\';
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005691 /* When current byte is a part of multibyte character, copy all
5692 * bytes of that character. */
5693 if (has_mbyte)
5694 {
5695 int i;
5696 int len = (*mb_ptr2len)(ptr) - 1;
5697
5698 for (i = 0; i < len && n >= 1; ++i, --n)
5699 *p++ = *ptr++;
5700 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005701 *p++ = *ptr++;
5702 }
5703 *p = NUL;
5704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
5706 /*
5707 * Execute the command.
5708 */
5709 if (cmdchar == '*' || cmdchar == '#')
5710 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005711 if (!g_cmd && (has_mbyte
5712 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
5713 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 STRCAT(buf, "\\>");
5715#ifdef FEAT_CMDHIST
5716 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005717 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5719#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005720 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005723 {
5724 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005726 g_tag_at_cursor = FALSE;
5727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729 vim_free(buf);
5730}
5731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732/*
5733 * Get visually selected text, within one line only.
5734 * Returns FAIL if more than one line selected.
5735 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005736 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005737get_visual_text(
5738 cmdarg_T *cap,
5739 char_u **pp, /* return: start of selected text */
5740 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 if (VIsual_mode != 'V')
5743 unadjust_for_sel();
5744 if (VIsual.lnum != curwin->w_cursor.lnum)
5745 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005746 if (cap != NULL)
5747 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 return FAIL;
5749 }
5750 if (VIsual_mode == 'V')
5751 {
5752 *pp = ml_get_curline();
5753 *lenp = (int)STRLEN(*pp);
5754 }
5755 else
5756 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005757 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {
5759 *pp = ml_get_pos(&curwin->w_cursor);
5760 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5761 }
5762 else
5763 {
5764 *pp = ml_get_pos(&VIsual);
5765 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 if (has_mbyte)
5768 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005769 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771 reset_VIsual_and_resel();
5772 return OK;
5773}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774
5775/*
5776 * CTRL-T: backwards in tag stack
5777 */
5778 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005779nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 if (!checkclearopq(cap->oap))
5782 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5783}
5784
5785/*
5786 * Handle scrolling command 'H', 'L' and 'M'.
5787 */
5788 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005789nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 int used = 0;
5792 long n;
5793#ifdef FEAT_FOLDING
5794 linenr_T lnum;
5795#endif
5796 int half;
5797
5798 cap->oap->motion_type = MLINE;
5799 setpcmark();
5800
5801 if (cap->cmdchar == 'L')
5802 {
5803 validate_botline(); /* make sure curwin->w_botline is valid */
5804 curwin->w_cursor.lnum = curwin->w_botline - 1;
5805 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5806 curwin->w_cursor.lnum = 1;
5807 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005808 {
5809#ifdef FEAT_FOLDING
5810 if (hasAnyFolding(curwin))
5811 {
5812 /* Count a fold for one screen line. */
5813 for (n = cap->count1 - 1; n > 0
5814 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5815 {
5816 (void)hasFolding(curwin->w_cursor.lnum,
5817 &curwin->w_cursor.lnum, NULL);
5818 --curwin->w_cursor.lnum;
5819 }
5820 }
5821 else
5822#endif
5823 curwin->w_cursor.lnum -= cap->count1 - 1;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826 else
5827 {
5828 if (cap->cmdchar == 'M')
5829 {
5830#ifdef FEAT_DIFF
5831 /* Don't count filler lines above the window. */
5832 used -= diff_check_fill(curwin, curwin->w_topline)
5833 - curwin->w_topfill;
5834#endif
5835 validate_botline(); /* make sure w_empty_rows is valid */
5836 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5837 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5838 {
5839#ifdef FEAT_DIFF
5840 /* Count half he number of filler lines to be "below this
5841 * line" and half to be "above the next line". */
5842 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5843 + n) / 2 >= half)
5844 {
5845 --n;
5846 break;
5847 }
5848#endif
5849 used += plines(curwin->w_topline + n);
5850 if (used >= half)
5851 break;
5852#ifdef FEAT_FOLDING
5853 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5854 n = lnum - curwin->w_topline;
5855#endif
5856 }
5857 if (n > 0 && used > curwin->w_height)
5858 --n;
5859 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005860 else /* (cap->cmdchar == 'H') */
5861 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005863#ifdef FEAT_FOLDING
5864 if (hasAnyFolding(curwin))
5865 {
5866 /* Count a fold for one screen line. */
5867 lnum = curwin->w_topline;
5868 while (n-- > 0 && lnum < curwin->w_botline - 1)
5869 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005870 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005871 ++lnum;
5872 }
5873 n = lnum - curwin->w_topline;
5874 }
5875#endif
5876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 curwin->w_cursor.lnum = curwin->w_topline + n;
5878 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5879 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5880 }
5881
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005882 /* Correct for 'so', except when an operator is pending. */
5883 if (cap->oap->op_type == OP_NOP)
5884 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 beginline(BL_SOL | BL_FIX);
5886}
5887
5888/*
5889 * Cursor right commands.
5890 */
5891 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005892nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005895 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005897 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5898 {
5899 /* <C-Right> and <S-Right> move a word or WORD right */
5900 if (mod_mask & MOD_MASK_CTRL)
5901 cap->arg = TRUE;
5902 nv_wordcmd(cap);
5903 return;
5904 }
5905
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 cap->oap->motion_type = MCHAR;
5907 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005908 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005911 * In virtual edit mode, there's no such thing as "past_line", as lines
5912 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 */
5914 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005915 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
5917 for (n = cap->count1; n > 0; --n)
5918 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005919 if ((!past_line && oneright() == FAIL)
5920 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005921 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
5923 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005924 * <Space> wraps to next line if 'whichwrap' has 's'.
5925 * 'l' wraps to next line if 'whichwrap' has 'l'.
5926 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 */
5928 if ( ((cap->cmdchar == ' '
5929 && vim_strchr(p_ww, 's') != NULL)
5930 || (cap->cmdchar == 'l'
5931 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005932 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 && vim_strchr(p_ww, '>') != NULL))
5934 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5935 {
5936 /* When deleting we also count the NL as a character.
5937 * Set cap->oap->inclusive when last char in the line is
5938 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005939 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005941 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 cap->oap->inclusive = TRUE;
5943 else
5944 {
5945 ++curwin->w_cursor.lnum;
5946 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 curwin->w_set_curswant = TRUE;
5949 cap->oap->inclusive = FALSE;
5950 }
5951 continue;
5952 }
5953 if (cap->oap->op_type == OP_NOP)
5954 {
5955 /* Only beep and flush if not moved at all */
5956 if (n == cap->count1)
5957 beep_flush();
5958 }
5959 else
5960 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005961 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 cap->oap->inclusive = TRUE;
5963 }
5964 break;
5965 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005966 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 {
5968 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 if (virtual_active())
5970 oneright();
5971 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 if (has_mbyte)
5974 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005975 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 ++curwin->w_cursor.col;
5978 }
5979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 }
5981#ifdef FEAT_FOLDING
5982 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5983 && cap->oap->op_type == OP_NOP)
5984 foldOpenCursor();
5985#endif
5986}
5987
5988/*
5989 * Cursor left commands.
5990 *
5991 * Returns TRUE when operator end should not be adjusted.
5992 */
5993 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005994nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
5996 long n;
5997
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005998 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5999 {
6000 /* <C-Left> and <S-Left> move a word or WORD left */
6001 if (mod_mask & MOD_MASK_CTRL)
6002 cap->arg = 1;
6003 nv_bck_word(cap);
6004 return;
6005 }
6006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 cap->oap->motion_type = MCHAR;
6008 cap->oap->inclusive = FALSE;
6009 for (n = cap->count1; n > 0; --n)
6010 {
6011 if (oneleft() == FAIL)
6012 {
6013 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6014 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6015 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6016 */
6017 if ( (((cap->cmdchar == K_BS
6018 || cap->cmdchar == Ctrl_H)
6019 && vim_strchr(p_ww, 'b') != NULL)
6020 || (cap->cmdchar == 'h'
6021 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006022 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 && vim_strchr(p_ww, '<') != NULL))
6024 && curwin->w_cursor.lnum > 1)
6025 {
6026 --(curwin->w_cursor.lnum);
6027 coladvance((colnr_T)MAXCOL);
6028 curwin->w_set_curswant = TRUE;
6029
6030 /* When the NL before the first char has to be deleted we
6031 * put the cursor on the NUL after the previous line.
6032 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006033 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if ( (cap->oap->op_type == OP_DELETE
6035 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006036 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006038 char_u *cp = ml_get_cursor();
6039
6040 if (*cp != NUL)
6041 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006042 if (has_mbyte)
6043 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6044 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006045 ++curwin->w_cursor.col;
6046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 cap->retval |= CA_NO_ADJ_OP_END;
6048 }
6049 continue;
6050 }
6051 /* Only beep and flush if not moved at all */
6052 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6053 beep_flush();
6054 break;
6055 }
6056 }
6057#ifdef FEAT_FOLDING
6058 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6059 && cap->oap->op_type == OP_NOP)
6060 foldOpenCursor();
6061#endif
6062}
6063
6064/*
6065 * Cursor up commands.
6066 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6067 */
6068 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006069nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006071 if (mod_mask & MOD_MASK_SHIFT)
6072 {
6073 /* <S-Up> is page up */
6074 cap->arg = BACKWARD;
6075 nv_page(cap);
6076 }
6077 else
6078 {
6079 cap->oap->motion_type = MLINE;
6080 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6081 clearopbeep(cap->oap);
6082 else if (cap->arg)
6083 beginline(BL_WHITE | BL_FIX);
6084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085}
6086
6087/*
6088 * Cursor down commands.
6089 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6090 */
6091 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006092nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006094 if (mod_mask & MOD_MASK_SHIFT)
6095 {
6096 /* <S-Down> is page down */
6097 cap->arg = FORWARD;
6098 nv_page(cap);
6099 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006100#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006101 /* Quickfix window only: view the result under the cursor. */
6102 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6103 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
6107#ifdef FEAT_CMDWIN
6108 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006109 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 cmdwin_result = CAR;
6111 else
6112#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006113#ifdef FEAT_JOB_CHANNEL
6114 /* In a prompt buffer a <CR> in the last line invokes the callback. */
6115 if (bt_prompt(curbuf) && cap->cmdchar == CAR
6116 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
6117 {
6118 invoke_prompt_callback();
6119 if (restart_edit == 0)
6120 restart_edit = 'a';
6121 }
6122 else
6123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
6125 cap->oap->motion_type = MLINE;
6126 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6127 clearopbeep(cap->oap);
6128 else if (cap->arg)
6129 beginline(BL_WHITE | BL_FIX);
6130 }
6131 }
6132}
6133
6134#ifdef FEAT_SEARCHPATH
6135/*
6136 * Grab the file name under the cursor and edit it.
6137 */
6138 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006139nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
6141 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006142 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006144 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
6146 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006147 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 return;
6149 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006150 if (curbuf_locked())
6151 {
6152 clearop(cap->oap);
6153 return;
6154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006156 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158 if (ptr != NULL)
6159 {
6160 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006161 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006162 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006164 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006165 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006166 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006167 {
6168 curwin->w_cursor.lnum = lnum;
6169 check_cursor_lnum();
6170 beginline(BL_SOL | BL_FIX);
6171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 vim_free(ptr);
6173 }
6174 else
6175 clearop(cap->oap);
6176}
6177#endif
6178
6179/*
6180 * <End> command: to end of current line or last line.
6181 */
6182 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006183nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006185 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006187 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 nv_goto(cap);
6189 cap->count1 = 1; /* to end of current line */
6190 }
6191 nv_dollar(cap);
6192}
6193
6194/*
6195 * Handle the "$" command.
6196 */
6197 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006198nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 cap->oap->motion_type = MCHAR;
6201 cap->oap->inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 /* In virtual mode when off the edge of a line and an operator
6203 * is pending (whew!) keep the cursor where it is.
6204 * Otherwise, send it to the end of the line. */
6205 if (!virtual_active() || gchar_cursor() != NUL
6206 || cap->oap->op_type == OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6208 if (cursor_down((long)(cap->count1 - 1),
6209 cap->oap->op_type == OP_NOP) == FAIL)
6210 clearopbeep(cap->oap);
6211#ifdef FEAT_FOLDING
6212 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6213 foldOpenCursor();
6214#endif
6215}
6216
6217/*
6218 * Implementation of '?' and '/' commands.
6219 * If cap->arg is TRUE don't set PC mark.
6220 */
6221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006222nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006225 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
6227 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6228 {
6229 /* Translate "g??" to "g?g?" */
6230 cap->cmdchar = 'g';
6231 cap->nchar = '?';
6232 nv_operator(cap);
6233 return;
6234 }
6235
Bram Moolenaardda933d2016-09-03 21:04:58 +02006236 /* When using 'incsearch' the cursor may be moved to set a different search
6237 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6239
6240 if (cap->searchbuf == NULL)
6241 {
6242 clearop(oap);
6243 return;
6244 }
6245
Bram Moolenaar46539112015-02-17 15:43:57 +01006246 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006247 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006248 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249}
6250
6251/*
6252 * Handle "N" and "n" commands.
6253 * cap->arg is SEARCH_REV for "N", 0 for "n".
6254 */
6255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006256nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
Bram Moolenaar46539112015-02-17 15:43:57 +01006258 pos_T old = curwin->w_cursor;
6259 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6260
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006261 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006262 {
6263 /* Avoid getting stuck on the current cursor position, which can
6264 * happen when an offset is given and the cursor is on the last char
6265 * in the buffer: Repeat with count + 1. */
6266 cap->count1 += 1;
6267 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6268 cap->count1 -= 1;
6269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270}
6271
6272/*
6273 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6274 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006275 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006277 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006278normal_search(
6279 cmdarg_T *cap,
6280 int dir,
6281 char_u *pat,
6282 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
6284 int i;
6285
6286 cap->oap->motion_type = MCHAR;
6287 cap->oap->inclusive = FALSE;
6288 cap->oap->use_reg_one = TRUE;
6289 curwin->w_set_curswant = TRUE;
6290
6291 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006292 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (i == 0)
6294 clearop(cap->oap);
6295 else
6296 {
6297 if (i == 2)
6298 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300#ifdef FEAT_FOLDING
6301 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6302 foldOpenCursor();
6303#endif
6304 }
6305
6306 /* "/$" will put the cursor after the end of the line, may need to
6307 * correct that here */
6308 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006309 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310}
6311
6312/*
6313 * Character search commands.
6314 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6315 * ',' and FALSE for ';'.
6316 * cap->nchar is NUL for ',' and ';' (repeat the search)
6317 */
6318 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006319nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 int t_cmd;
6322
6323 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6324 t_cmd = TRUE;
6325 else
6326 t_cmd = FALSE;
6327
6328 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6330 clearopbeep(cap->oap);
6331 else
6332 {
6333 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 /* Include a Tab for "tx" and for "dfx". */
6335 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6336 && (t_cmd || cap->oap->op_type != OP_NOP))
6337 {
6338 colnr_T scol, ecol;
6339
6340 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6341 curwin->w_cursor.coladd = ecol - scol;
6342 }
6343 else
6344 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346#ifdef FEAT_FOLDING
6347 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6348 foldOpenCursor();
6349#endif
6350 }
6351}
6352
6353/*
6354 * "[" and "]" commands.
6355 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6356 */
6357 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006358nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006360 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 pos_T prev_pos;
6362 pos_T *pos = NULL; /* init for GCC */
6363 pos_T old_pos; /* cursor position before command */
6364 int flag;
6365 long n;
6366 int findc;
6367 int c;
6368
6369 cap->oap->motion_type = MCHAR;
6370 cap->oap->inclusive = FALSE;
6371 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01006372 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
6374#ifdef FEAT_SEARCHPATH
6375 /*
6376 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6377 */
6378 if (cap->nchar == 'f')
6379 nv_gotofile(cap);
6380 else
6381#endif
6382
6383#ifdef FEAT_FIND_ID
6384 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006385 * Find the occurrence(s) of the identifier or define under cursor
6386 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 *
6388 * search list jump
6389 * fwd bwd fwd bwd fwd bwd
6390 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6391 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6392 */
6393 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006394# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006396# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 cap->nchar) != NULL)
6400 {
6401 char_u *ptr;
6402 int len;
6403
6404 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6405 clearop(cap->oap);
6406 else
6407 {
6408 find_pattern_in_path(ptr, 0, len, TRUE,
6409 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6410 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6411 cap->count1,
6412 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6413 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6414 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6415 (linenr_T)MAXLNUM);
6416 curwin->w_set_curswant = TRUE;
6417 }
6418 }
6419 else
6420#endif
6421
6422 /*
6423 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6424 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6425 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6426 * "[m" or "]m" search for prev/next start of (Java) method.
6427 * "[M" or "]M" search for prev/next end of (Java) method.
6428 */
6429 if ( (cap->cmdchar == '['
6430 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6431 || (cap->cmdchar == ']'
6432 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6433 {
6434 if (cap->nchar == '*')
6435 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 prev_pos.lnum = 0;
6437 if (cap->nchar == 'm' || cap->nchar == 'M')
6438 {
6439 if (cap->cmdchar == '[')
6440 findc = '{';
6441 else
6442 findc = '}';
6443 n = 9999;
6444 }
6445 else
6446 {
6447 findc = cap->nchar;
6448 n = cap->count1;
6449 }
6450 for ( ; n > 0; --n)
6451 {
6452 if ((pos = findmatchlimit(cap->oap, findc,
6453 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6454 {
6455 if (new_pos.lnum == 0) /* nothing found */
6456 {
6457 if (cap->nchar != 'm' && cap->nchar != 'M')
6458 clearopbeep(cap->oap);
6459 }
6460 else
6461 pos = &new_pos; /* use last one found */
6462 break;
6463 }
6464 prev_pos = new_pos;
6465 curwin->w_cursor = *pos;
6466 new_pos = *pos;
6467 }
6468 curwin->w_cursor = old_pos;
6469
6470 /*
6471 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6472 * brought us to the match for "[m" and "]M" when inside a method.
6473 * Try finding the '{' or '}' we want to be at.
6474 * Also repeat for the given count.
6475 */
6476 if (cap->nchar == 'm' || cap->nchar == 'M')
6477 {
6478 /* norm is TRUE for "]M" and "[m" */
6479 int norm = ((findc == '{') == (cap->nchar == 'm'));
6480
6481 n = cap->count1;
6482 /* found a match: we were inside a method */
6483 if (prev_pos.lnum != 0)
6484 {
6485 pos = &prev_pos;
6486 curwin->w_cursor = prev_pos;
6487 if (norm)
6488 --n;
6489 }
6490 else
6491 pos = NULL;
6492 while (n > 0)
6493 {
6494 for (;;)
6495 {
6496 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6497 {
6498 /* if not found anything, that's an error */
6499 if (pos == NULL)
6500 clearopbeep(cap->oap);
6501 n = 0;
6502 break;
6503 }
6504 c = gchar_cursor();
6505 if (c == '{' || c == '}')
6506 {
6507 /* Must have found end/start of class: use it.
6508 * Or found the place to be at. */
6509 if ((c == findc && norm) || (n == 1 && !norm))
6510 {
6511 new_pos = curwin->w_cursor;
6512 pos = &new_pos;
6513 n = 0;
6514 }
6515 /* if no match found at all, we started outside of the
6516 * class and we're inside now. Just go on. */
6517 else if (new_pos.lnum == 0)
6518 {
6519 new_pos = curwin->w_cursor;
6520 pos = &new_pos;
6521 }
6522 /* found start/end of other method: go to match */
6523 else if ((pos = findmatchlimit(cap->oap, findc,
6524 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6525 0)) == NULL)
6526 n = 0;
6527 else
6528 curwin->w_cursor = *pos;
6529 break;
6530 }
6531 }
6532 --n;
6533 }
6534 curwin->w_cursor = old_pos;
6535 if (pos == NULL && new_pos.lnum != 0)
6536 clearopbeep(cap->oap);
6537 }
6538 if (pos != NULL)
6539 {
6540 setpcmark();
6541 curwin->w_cursor = *pos;
6542 curwin->w_set_curswant = TRUE;
6543#ifdef FEAT_FOLDING
6544 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6545 && cap->oap->op_type == OP_NOP)
6546 foldOpenCursor();
6547#endif
6548 }
6549 }
6550
6551 /*
6552 * "[[", "[]", "]]" and "][": move to start or end of function
6553 */
6554 else if (cap->nchar == '[' || cap->nchar == ']')
6555 {
6556 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6557 flag = '{';
6558 else
6559 flag = '}'; /* "][" or "[]" */
6560
6561 curwin->w_set_curswant = TRUE;
6562 /*
6563 * Imitate strange Vi behaviour: When using "]]" with an operator
6564 * we also stop at '}'.
6565 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006566 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 (cap->oap->op_type != OP_NOP
6568 && cap->arg == FORWARD && flag == '{')))
6569 clearopbeep(cap->oap);
6570 else
6571 {
6572 if (cap->oap->op_type == OP_NOP)
6573 beginline(BL_WHITE | BL_FIX);
6574#ifdef FEAT_FOLDING
6575 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6576 foldOpenCursor();
6577#endif
6578 }
6579 }
6580
6581 /*
6582 * "[p", "[P", "]P" and "]p": put with indent adjustment
6583 */
6584 else if (cap->nchar == 'p' || cap->nchar == 'P')
6585 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006586 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006588 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6589 ? FORWARD : BACKWARD;
6590 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006591 int was_visual = VIsual_active;
6592 int line_count = curbuf->b_ml.ml_line_count;
6593 pos_T start, end;
6594
6595 if (VIsual_active)
6596 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006597 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006598 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006599 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006600 curwin->w_cursor = (dir == BACKWARD ? start : end);
6601 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006602# ifdef FEAT_CLIPBOARD
6603 adjust_clip_reg(&regname);
6604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006606
6607 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006608 if (was_visual)
6609 {
6610 VIsual = start;
6611 curwin->w_cursor = end;
6612 if (dir == BACKWARD)
6613 {
6614 /* adjust lines */
6615 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6616 curwin->w_cursor.lnum +=
6617 curbuf->b_ml.ml_line_count - line_count;
6618 }
6619
6620 VIsual_active = TRUE;
6621 if (VIsual_mode == 'V')
6622 {
6623 /* delete visually selected lines */
6624 cap->cmdchar = 'd';
6625 cap->nchar = NUL;
6626 cap->oap->regname = regname;
6627 nv_operator(cap);
6628 do_pending_operator(cap, 0, FALSE);
6629 }
6630 if (VIsual_active)
6631 {
6632 end_visual_mode();
6633 redraw_later(SOME_VALID);
6634 }
6635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 }
6637 }
6638
6639 /*
6640 * "['", "[`", "]'" and "]`": jump to next mark
6641 */
6642 else if (cap->nchar == '\'' || cap->nchar == '`')
6643 {
6644 pos = &curwin->w_cursor;
6645 for (n = cap->count1; n > 0; --n)
6646 {
6647 prev_pos = *pos;
6648 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6649 cap->nchar == '\'');
6650 if (pos == NULL)
6651 break;
6652 }
6653 if (pos == NULL)
6654 pos = &prev_pos;
6655 nv_cursormark(cap, cap->nchar == '\'', pos);
6656 }
6657
6658#ifdef FEAT_MOUSE
6659 /*
6660 * [ or ] followed by a middle mouse click: put selected text with
6661 * indent adjustment. Any other button just does as usual.
6662 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006663 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 {
6665 (void)do_mouse(cap->oap, cap->nchar,
6666 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6667 cap->count1, PUT_FIXINDENT);
6668 }
6669#endif /* FEAT_MOUSE */
6670
6671#ifdef FEAT_FOLDING
6672 /*
6673 * "[z" and "]z": move to start or end of open fold.
6674 */
6675 else if (cap->nchar == 'z')
6676 {
6677 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6678 cap->count1) == FAIL)
6679 clearopbeep(cap->oap);
6680 }
6681#endif
6682
6683#ifdef FEAT_DIFF
6684 /*
6685 * "[c" and "]c": move to next or previous diff-change.
6686 */
6687 else if (cap->nchar == 'c')
6688 {
6689 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6690 cap->count1) == FAIL)
6691 clearopbeep(cap->oap);
6692 }
6693#endif
6694
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006695#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006696 /*
6697 * "[s", "[S", "]s" and "]S": move to next spell error.
6698 */
6699 else if (cap->nchar == 's' || cap->nchar == 'S')
6700 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006701 setpcmark();
6702 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006703 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6704 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006705 {
6706 clearopbeep(cap->oap);
6707 break;
6708 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006709 else
6710 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006711# ifdef FEAT_FOLDING
6712 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6713 foldOpenCursor();
6714# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006715 }
6716#endif
6717
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 /* Not a valid cap->nchar. */
6719 else
6720 clearopbeep(cap->oap);
6721}
6722
6723/*
6724 * Handle Normal mode "%" command.
6725 */
6726 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006727nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
6729 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006730#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 linenr_T lnum = curwin->w_cursor.lnum;
6732#endif
6733
6734 cap->oap->inclusive = TRUE;
6735 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6736 {
6737 if (cap->count0 > 100)
6738 clearopbeep(cap->oap);
6739 else
6740 {
6741 cap->oap->motion_type = MLINE;
6742 setpcmark();
6743 /* Round up, so CTRL-G will give same value. Watch out for a
6744 * large line count, the line number must not go negative! */
6745 if (curbuf->b_ml.ml_line_count > 1000000)
6746 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6747 / 100L * cap->count0;
6748 else
6749 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6750 cap->count0 + 99L) / 100L;
6751 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6752 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6753 beginline(BL_SOL | BL_FIX);
6754 }
6755 }
6756 else /* "%" : go to matching paren */
6757 {
6758 cap->oap->motion_type = MCHAR;
6759 cap->oap->use_reg_one = TRUE;
6760 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6761 clearopbeep(cap->oap);
6762 else
6763 {
6764 setpcmark();
6765 curwin->w_cursor = *pos;
6766 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 }
6770 }
6771#ifdef FEAT_FOLDING
6772 if (cap->oap->op_type == OP_NOP
6773 && lnum != curwin->w_cursor.lnum
6774 && (fdo_flags & FDO_PERCENT)
6775 && KeyTyped)
6776 foldOpenCursor();
6777#endif
6778}
6779
6780/*
6781 * Handle "(" and ")" commands.
6782 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6783 */
6784 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006785nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786{
6787 cap->oap->motion_type = MCHAR;
6788 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006789 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6790 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 curwin->w_set_curswant = TRUE;
6792
6793 if (findsent(cap->arg, cap->count1) == FAIL)
6794 clearopbeep(cap->oap);
6795 else
6796 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006797 /* Don't leave the cursor on the NUL past end of line. */
6798 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800#ifdef FEAT_FOLDING
6801 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6802 foldOpenCursor();
6803#endif
6804 }
6805}
6806
6807/*
6808 * "m" command: Mark a position.
6809 */
6810 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006811nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
6813 if (!checkclearop(cap->oap))
6814 {
6815 if (setmark(cap->nchar) == FAIL)
6816 clearopbeep(cap->oap);
6817 }
6818}
6819
6820/*
6821 * "{" and "}" commands.
6822 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6823 */
6824 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006825nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826{
6827 cap->oap->motion_type = MCHAR;
6828 cap->oap->inclusive = FALSE;
6829 cap->oap->use_reg_one = TRUE;
6830 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006831 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 clearopbeep(cap->oap);
6833 else
6834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836#ifdef FEAT_FOLDING
6837 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6838 foldOpenCursor();
6839#endif
6840 }
6841}
6842
6843/*
6844 * "u" command: Undo or make lower case.
6845 */
6846 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006847nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006849 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 {
6851 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6852 cap->cmdchar = 'g';
6853 cap->nchar = 'u';
6854 nv_operator(cap);
6855 }
6856 else
6857 nv_kundo(cap);
6858}
6859
6860/*
6861 * <Undo> command.
6862 */
6863 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006864nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 if (!checkclearopq(cap->oap))
6867 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02006868#ifdef FEAT_JOB_CHANNEL
6869 if (bt_prompt(curbuf))
6870 {
6871 clearopbeep(cap->oap);
6872 return;
6873 }
6874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 u_undo((int)cap->count1);
6876 curwin->w_set_curswant = TRUE;
6877 }
6878}
6879
6880/*
6881 * Handle the "r" command.
6882 */
6883 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006884nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
6886 char_u *ptr;
6887 int had_ctrl_v;
6888 long n;
6889
6890 if (checkclearop(cap->oap))
6891 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006892#ifdef FEAT_JOB_CHANNEL
6893 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6894 {
6895 clearopbeep(cap->oap);
6896 return;
6897 }
6898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899
6900 /* get another character */
6901 if (cap->nchar == Ctrl_V)
6902 {
6903 had_ctrl_v = Ctrl_V;
6904 cap->nchar = get_literal();
6905 /* Don't redo a multibyte character with CTRL-V. */
6906 if (cap->nchar > DEL)
6907 had_ctrl_v = NUL;
6908 }
6909 else
6910 had_ctrl_v = NUL;
6911
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006912 /* Abort if the character is a special key. */
6913 if (IS_SPECIAL(cap->nchar))
6914 {
6915 clearopbeep(cap->oap);
6916 return;
6917 }
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 /* Visual mode "r" */
6920 if (VIsual_active)
6921 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006922 if (got_int)
6923 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006924 if (had_ctrl_v)
6925 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006926 /* Use a special (negative) number to make a difference between a
6927 * literal CR or NL and a line break. */
6928 if (cap->nchar == CAR)
6929 cap->nchar = REPLACE_CR_NCHAR;
6930 else if (cap->nchar == NL)
6931 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01006932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 nv_operator(cap);
6934 return;
6935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 /* Break tabs, etc. */
6938 if (virtual_active())
6939 {
6940 if (u_save_cursor() == FAIL)
6941 return;
6942 if (gchar_cursor() == NUL)
6943 {
6944 /* Add extra space and put the cursor on the first one. */
6945 coladvance_force((colnr_T)(getviscol() + cap->count1));
6946 curwin->w_cursor.col -= cap->count1;
6947 }
6948 else if (gchar_cursor() == TAB)
6949 coladvance_force(getviscol());
6950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006952 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006954 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006955 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
6957 clearopbeep(cap->oap);
6958 return;
6959 }
6960
6961 /*
6962 * Replacing with a TAB is done by edit() when it is complicated because
6963 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6964 * Other characters are done below to avoid problems with things like
6965 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6966 */
6967 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6968 {
6969 stuffnumReadbuff(cap->count1);
6970 stuffcharReadbuff('R');
6971 stuffcharReadbuff('\t');
6972 stuffcharReadbuff(ESC);
6973 return;
6974 }
6975
6976 /* save line for undo */
6977 if (u_save_cursor() == FAIL)
6978 return;
6979
6980 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6981 {
6982 /*
6983 * Replace character(s) by a single newline.
6984 * Strange vi behaviour: Only one newline is inserted.
6985 * Delete the characters here.
6986 * Insert the newline with an insert command, takes care of
6987 * autoindent. The insert command depends on being on the last
6988 * character of a line or not.
6989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 (void)del_chars(cap->count1, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 stuffcharReadbuff('\r');
6992 stuffcharReadbuff(ESC);
6993
6994 /* Give 'r' to edit(), to get the redo command right. */
6995 invoke_edit(cap, TRUE, 'r', FALSE);
6996 }
6997 else
6998 {
6999 prep_redo(cap->oap->regname, cap->count1,
7000 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7001
7002 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 if (has_mbyte)
7004 {
7005 int old_State = State;
7006
7007 if (cap->ncharC1 != 0)
7008 AppendCharToRedobuff(cap->ncharC1);
7009 if (cap->ncharC2 != 0)
7010 AppendCharToRedobuff(cap->ncharC2);
7011
7012 /* This is slow, but it handles replacing a single-byte with a
7013 * multi-byte and the other way around. Also handles adding
7014 * composing characters for utf-8. */
7015 for (n = cap->count1; n > 0; --n)
7016 {
7017 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007018 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7019 {
7020 int c = ins_copychar(curwin->w_cursor.lnum
7021 + (cap->nchar == Ctrl_Y ? -1 : 1));
7022 if (c != NUL)
7023 ins_char(c);
7024 else
7025 /* will be decremented further down */
7026 ++curwin->w_cursor.col;
7027 }
7028 else
7029 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 State = old_State;
7031 if (cap->ncharC1 != 0)
7032 ins_char(cap->ncharC1);
7033 if (cap->ncharC2 != 0)
7034 ins_char(cap->ncharC2);
7035 }
7036 }
7037 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 {
7039 /*
7040 * Replace the characters within one line.
7041 */
7042 for (n = cap->count1; n > 0; --n)
7043 {
7044 /*
7045 * Get ptr again, because u_save and/or showmatch() will have
7046 * released the line. At the same time we let know that the
7047 * line will be changed.
7048 */
7049 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007050 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7051 {
7052 int c = ins_copychar(curwin->w_cursor.lnum
7053 + (cap->nchar == Ctrl_Y ? -1 : 1));
7054 if (c != NUL)
7055 ptr[curwin->w_cursor.col] = c;
7056 }
7057 else
7058 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 if (p_sm && msg_silent == 0)
7060 showmatch(cap->nchar);
7061 ++curwin->w_cursor.col;
7062 }
7063#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007064 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007066 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaar009b2592004-10-24 19:18:58 +00007068 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007069 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007071 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 }
7073#endif
7074
7075 /* mark the buffer as changed and prepare for displaying */
7076 changed_bytes(curwin->w_cursor.lnum,
7077 (colnr_T)(curwin->w_cursor.col - cap->count1));
7078 }
7079 --curwin->w_cursor.col; /* cursor on the last replaced char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 /* if the character on the left of the current cursor is a multi-byte
7081 * character, move two characters left */
7082 if (has_mbyte)
7083 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 curbuf->b_op_end = curwin->w_cursor;
7085 curwin->w_set_curswant = TRUE;
7086 set_last_insert(cap->nchar);
7087 }
7088}
7089
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090/*
7091 * 'o': Exchange start and end of Visual area.
7092 * 'O': same, but in block mode exchange left and right corners.
7093 */
7094 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007095v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 pos_T old_cursor;
7098 colnr_T left, right;
7099
7100 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7101 {
7102 old_cursor = curwin->w_cursor;
7103 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7104 curwin->w_cursor.lnum = VIsual.lnum;
7105 coladvance(left);
7106 VIsual = curwin->w_cursor;
7107
7108 curwin->w_cursor.lnum = old_cursor.lnum;
7109 curwin->w_curswant = right;
7110 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7111 * right one column */
7112 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7113 ++curwin->w_curswant;
7114 coladvance(curwin->w_curswant);
7115 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007117 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {
7119 curwin->w_cursor.lnum = VIsual.lnum;
7120 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7121 ++right;
7122 coladvance(right);
7123 VIsual = curwin->w_cursor;
7124
7125 curwin->w_cursor.lnum = old_cursor.lnum;
7126 coladvance(left);
7127 curwin->w_curswant = left;
7128 }
7129 }
7130 else
7131 {
7132 old_cursor = curwin->w_cursor;
7133 curwin->w_cursor = VIsual;
7134 VIsual = old_cursor;
7135 curwin->w_set_curswant = TRUE;
7136 }
7137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139/*
7140 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7141 */
7142 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007143nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 if (VIsual_active) /* "R" is replace lines */
7146 {
7147 cap->cmdchar = 'c';
7148 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007149 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 VIsual_mode = 'V';
7151 nv_operator(cap);
7152 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007153 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 {
7155 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007156 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 else
7158 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 if (virtual_active())
7160 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7162 }
7163 }
7164}
7165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166/*
7167 * "gr".
7168 */
7169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007170nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (VIsual_active)
7173 {
7174 cap->cmdchar = 'r';
7175 cap->nchar = cap->extra_char;
7176 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7177 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007178 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
7180 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007181 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 else
7183 {
7184 if (cap->extra_char == Ctrl_V) /* get another character */
7185 cap->extra_char = get_literal();
7186 stuffcharReadbuff(cap->extra_char);
7187 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 if (virtual_active())
7189 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 invoke_edit(cap, TRUE, 'v', FALSE);
7191 }
7192 }
7193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
7195/*
7196 * Swap case for "~" command, when it does not work like an operator.
7197 */
7198 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007199n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 long n;
7202 pos_T startpos;
7203 int did_change = 0;
7204#ifdef FEAT_NETBEANS_INTG
7205 pos_T pos;
7206 char_u *ptr;
7207 int count;
7208#endif
7209
7210 if (checkclearopq(cap->oap))
7211 return;
7212
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007213 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {
7215 clearopbeep(cap->oap);
7216 return;
7217 }
7218
7219 prep_redo_cmd(cap);
7220
7221 if (u_save_cursor() == FAIL)
7222 return;
7223
7224 startpos = curwin->w_cursor;
7225#ifdef FEAT_NETBEANS_INTG
7226 pos = startpos;
7227#endif
7228 for (n = cap->count1; n > 0; --n)
7229 {
7230 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7231 inc_cursor();
7232 if (gchar_cursor() == NUL)
7233 {
7234 if (vim_strchr(p_ww, '~') != NULL
7235 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7236 {
7237#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007238 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 {
7240 if (did_change)
7241 {
7242 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007243 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007244 netbeans_removed(curbuf, pos.lnum, pos.col,
7245 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007247 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 }
7249 pos.col = 0;
7250 pos.lnum++;
7251 }
7252#endif
7253 ++curwin->w_cursor.lnum;
7254 curwin->w_cursor.col = 0;
7255 if (n > 1)
7256 {
7257 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7258 break;
7259 u_clearline();
7260 }
7261 }
7262 else
7263 break;
7264 }
7265 }
7266#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007267 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 {
7269 ptr = ml_get(pos.lnum);
7270 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007271 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7272 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274#endif
7275
7276
7277 check_cursor();
7278 curwin->w_set_curswant = TRUE;
7279 if (did_change)
7280 {
7281 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7282 0L);
7283 curbuf->b_op_start = startpos;
7284 curbuf->b_op_end = curwin->w_cursor;
7285 if (curbuf->b_op_end.col > 0)
7286 --curbuf->b_op_end.col;
7287 }
7288}
7289
7290/*
7291 * Move cursor to mark.
7292 */
7293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007294nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 if (check_mark(pos) == FAIL)
7297 clearop(cap->oap);
7298 else
7299 {
7300 if (cap->cmdchar == '\''
7301 || cap->cmdchar == '`'
7302 || cap->cmdchar == '['
7303 || cap->cmdchar == ']')
7304 setpcmark();
7305 curwin->w_cursor = *pos;
7306 if (flag)
7307 beginline(BL_WHITE | BL_FIX);
7308 else
7309 check_cursor();
7310 }
7311 cap->oap->motion_type = flag ? MLINE : MCHAR;
7312 if (cap->cmdchar == '`')
7313 cap->oap->use_reg_one = TRUE;
7314 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7315 curwin->w_set_curswant = TRUE;
7316}
7317
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318/*
7319 * Handle commands that are operators in Visual mode.
7320 */
7321 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007322v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323{
7324 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7325
7326 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007327 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 if (isupper(cap->cmdchar))
7329 {
7330 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007331 {
7332 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7336 curwin->w_curswant = MAXCOL;
7337 }
7338 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7339 nv_operator(cap);
7340}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341
7342/*
7343 * "s" and "S" commands.
7344 */
7345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007346nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007348#ifdef FEAT_TERMINAL
7349 /* When showing output of term_dumpdiff() swap the top and botom. */
7350 if (term_swap_diff() == OK)
7351 return;
7352#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02007353#ifdef FEAT_JOB_CHANNEL
7354 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7355 {
7356 clearopbeep(cap->oap);
7357 return;
7358 }
7359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7361 {
7362 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007363 {
7364 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 cap->cmdchar = 'c';
7368 nv_operator(cap);
7369 }
7370 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 nv_optrans(cap);
7372}
7373
7374/*
7375 * Abbreviated commands.
7376 */
7377 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007378nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379{
7380 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7381 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7382
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 /* in Visual mode these commands are operators */
7384 if (VIsual_active)
7385 v_visop(cap);
7386 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 nv_optrans(cap);
7388}
7389
7390/*
7391 * Translate a command into another command.
7392 */
7393 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007394nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395{
7396 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7397 (char_u *)"d$", (char_u *)"c$",
7398 (char_u *)"cl", (char_u *)"cc",
7399 (char_u *)"yy", (char_u *)":s\r"};
7400 static char_u *str = (char_u *)"xXDCsSY&";
7401
7402 if (!checkclearopq(cap->oap))
7403 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007404 /* In Vi "2D" doesn't delete the next line. Can't translate it
7405 * either, because "2." should also not use the count. */
7406 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7407 {
7408 cap->oap->start = curwin->w_cursor;
7409 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007410#ifdef FEAT_EVAL
7411 set_op_var(OP_DELETE);
7412#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007413 cap->count1 = 1;
7414 nv_dollar(cap);
7415 finish_op = TRUE;
7416 ResetRedobuff();
7417 AppendCharToRedobuff('D');
7418 }
7419 else
7420 {
7421 if (cap->count0)
7422 stuffnumReadbuff(cap->count0);
7423 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 }
7426 cap->opcount = 0;
7427}
7428
7429/*
7430 * "'" and "`" commands. Also for "g'" and "g`".
7431 * cap->arg is TRUE for "'" and "g'".
7432 */
7433 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007434nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435{
7436 pos_T *pos;
7437 int c;
7438#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007439 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7441#endif
7442
7443 if (cap->cmdchar == 'g')
7444 c = cap->extra_char;
7445 else
7446 c = cap->nchar;
7447 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7448 if (pos == (pos_T *)-1) /* jumped to other file */
7449 {
7450 if (cap->arg)
7451 {
7452 check_cursor_lnum();
7453 beginline(BL_WHITE | BL_FIX);
7454 }
7455 else
7456 check_cursor();
7457 }
7458 else
7459 nv_cursormark(cap, cap->arg, pos);
7460
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 /* May need to clear the coladd that a mark includes. */
7462 if (!virtual_active())
7463 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007464 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465#ifdef FEAT_FOLDING
7466 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007467 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007468 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 && (fdo_flags & FDO_MARK)
7470 && old_KeyTyped)
7471 foldOpenCursor();
7472#endif
7473}
7474
7475/*
7476 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7477 */
7478 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007479nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481#ifdef FEAT_JUMPLIST
7482 pos_T *pos;
7483# ifdef FEAT_FOLDING
7484 linenr_T lnum = curwin->w_cursor.lnum;
7485 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7486# endif
7487
7488 if (!checkclearopq(cap->oap))
7489 {
7490 if (cap->cmdchar == 'g')
7491 pos = movechangelist((int)cap->count1);
7492 else
7493 pos = movemark((int)cap->count1);
7494 if (pos == (pos_T *)-1) /* jump to other file */
7495 {
7496 curwin->w_set_curswant = TRUE;
7497 check_cursor();
7498 }
7499 else if (pos != NULL) /* can jump */
7500 nv_cursormark(cap, FALSE, pos);
7501 else if (cap->cmdchar == 'g')
7502 {
7503 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007504 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007506 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007508 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 }
7510 else
7511 clearopbeep(cap->oap);
7512# ifdef FEAT_FOLDING
7513 if (cap->oap->op_type == OP_NOP
7514 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7515 && (fdo_flags & FDO_MARK)
7516 && old_KeyTyped)
7517 foldOpenCursor();
7518# endif
7519 }
7520#else
7521 clearopbeep(cap->oap);
7522#endif
7523}
7524
7525/*
7526 * Handle '"' command.
7527 */
7528 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007529nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530{
7531 if (checkclearop(cap->oap))
7532 return;
7533#ifdef FEAT_EVAL
7534 if (cap->nchar == '=')
7535 cap->nchar = get_expr_register();
7536#endif
7537 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7538 {
7539 cap->oap->regname = cap->nchar;
7540 cap->opcount = cap->count0; /* remember count before '"' */
7541#ifdef FEAT_EVAL
7542 set_reg_var(cap->oap->regname);
7543#endif
7544 }
7545 else
7546 clearopbeep(cap->oap);
7547}
7548
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549/*
7550 * Handle "v", "V" and "CTRL-V" commands.
7551 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7552 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007553 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 */
7555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007556nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007558 if (cap->cmdchar == Ctrl_Q)
7559 cap->cmdchar = Ctrl_V;
7560
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7562 * characterwise, linewise, or blockwise. */
7563 if (cap->oap->op_type != OP_NOP)
7564 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007565 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 finish_op = FALSE; /* operator doesn't finish now but later */
7567 return;
7568 }
7569
7570 VIsual_select = cap->arg;
7571 if (VIsual_active) /* change Visual mode */
7572 {
7573 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7574 end_visual_mode();
7575 else /* toggle char/block mode */
7576 { /* or char/line mode */
7577 VIsual_mode = cap->cmdchar;
7578 showmode();
7579 }
7580 redraw_curbuf_later(INVERTED); /* update the inversion */
7581 }
7582 else /* start Visual mode */
7583 {
7584 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007585 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007587 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 VIsual = curwin->w_cursor;
7589
7590 VIsual_active = TRUE;
7591 VIsual_reselect = TRUE;
7592 if (!cap->arg)
7593 /* start Select mode when 'selectmode' contains "cmd" */
7594 may_start_select('c');
7595#ifdef FEAT_MOUSE
7596 setmouse();
7597#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007598 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 redraw_cmdline = TRUE; /* show visual mode later */
7600 /*
7601 * For V and ^V, we multiply the number of lines even if there
7602 * was only one -- webb
7603 */
7604 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7605 {
7606 curwin->w_cursor.lnum +=
7607 resel_VIsual_line_count * cap->count0 - 1;
7608 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7609 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7610 }
7611 VIsual_mode = resel_VIsual_mode;
7612 if (VIsual_mode == 'v')
7613 {
7614 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007615 {
7616 validate_virtcol();
7617 curwin->w_curswant = curwin->w_virtcol
7618 + resel_VIsual_vcol * cap->count0 - 1;
7619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007621 curwin->w_curswant = resel_VIsual_vcol;
7622 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007624 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 {
7626 curwin->w_curswant = MAXCOL;
7627 coladvance((colnr_T)MAXCOL);
7628 }
7629 else if (VIsual_mode == Ctrl_V)
7630 {
7631 validate_virtcol();
7632 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007633 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 coladvance(curwin->w_curswant);
7635 }
7636 else
7637 curwin->w_set_curswant = TRUE;
7638 redraw_curbuf_later(INVERTED); /* show the inversion */
7639 }
7640 else
7641 {
7642 if (!cap->arg)
7643 /* start Select mode when 'selectmode' contains "cmd" */
7644 may_start_select('c');
7645 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007646 if (VIsual_mode != 'V' && *p_sel == 'e')
7647 ++cap->count1; /* include one more char */
7648 if (cap->count0 > 0 && --cap->count1 > 0)
7649 {
7650 /* With a count select that many characters or lines. */
7651 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7652 nv_right(cap);
7653 else if (VIsual_mode == 'V')
7654 nv_down(cap);
7655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 }
7657 }
7658}
7659
7660/*
7661 * Start selection for Shift-movement keys.
7662 */
7663 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007664start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665{
7666 /* if 'selectmode' contains "key", start Select mode */
7667 may_start_select('k');
7668 n_start_visual_mode('v');
7669}
7670
7671/*
7672 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7673 */
7674 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007675may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677 VIsual_select = (stuff_empty() && typebuf_typed()
7678 && (vim_strchr(p_slm, c) != NULL));
7679}
7680
7681/*
7682 * Start Visual mode "c".
7683 * Should set VIsual_select before calling this.
7684 */
7685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007686n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007688#ifdef FEAT_CONCEAL
7689 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007690 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007691#endif
7692
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 VIsual_mode = c;
7694 VIsual_active = TRUE;
7695 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007696
7697 // Corner case: the 0 position in a tab may change when going into
7698 // virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007700 {
7701 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 VIsual = curwin->w_cursor;
7705
7706#ifdef FEAT_FOLDING
7707 foldAdjustVisual();
7708#endif
7709
7710#ifdef FEAT_MOUSE
7711 setmouse();
7712#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007713#ifdef FEAT_CONCEAL
7714 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007715 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007716#endif
7717
Bram Moolenaar09df3122006-01-23 22:23:09 +00007718 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 redraw_cmdline = TRUE; /* show visual mode later */
7720#ifdef FEAT_CLIPBOARD
7721 /* Make sure the clipboard gets updated. Needed because start and
7722 * end may still be the same, and the selection needs to be owned */
7723 clip_star.vmode = NUL;
7724#endif
7725
7726 /* Only need to redraw this line, unless still need to redraw an old
7727 * Visual area (when 'lazyredraw' is set). */
7728 if (curwin->w_redr_type < INVERTED)
7729 {
7730 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7731 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7732 }
7733}
7734
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
7736/*
7737 * CTRL-W: Window commands
7738 */
7739 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007740nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007742 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007743 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007744 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007745 cap->cmdchar = ':';
7746 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007747 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007748 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007749 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751}
7752
7753/*
7754 * CTRL-Z: Suspend
7755 */
7756 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007757nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
7759 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 if (VIsual_active)
7761 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 do_cmdline_cmd((char_u *)"st");
7763}
7764
7765/*
7766 * Commands starting with "g".
7767 */
7768 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007769nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 int i;
7774 int flag = FALSE;
7775
7776 switch (cap->nchar)
7777 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007778 case Ctrl_A:
7779 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780#ifdef MEM_PROFILE
7781 /*
7782 * "g^A": dump log of used memory.
7783 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007784 if (!VIsual_active && cap->nchar == Ctrl_A)
7785 vim_mem_profile_dump();
7786 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007788 /*
7789 * "g^A/g^X": sequentially increment visually selected region
7790 */
7791 if (VIsual_active)
7792 {
7793 cap->arg = TRUE;
7794 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007795 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007796 nv_addsub(cap);
7797 }
7798 else
7799 clearopbeep(oap);
7800 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 /*
7803 * "gR": Enter virtual replace mode.
7804 */
7805 case 'R':
7806 cap->arg = TRUE;
7807 nv_Replace(cap);
7808 break;
7809
7810 case 'r':
7811 nv_vreplace(cap);
7812 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813
7814 case '&':
7815 do_cmdline_cmd((char_u *)"%s//~/&");
7816 break;
7817
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 /*
7819 * "gv": Reselect the previous Visual area. If Visual already active,
7820 * exchange previous and current Visual area.
7821 */
7822 case 'v':
7823 if (checkclearop(oap))
7824 break;
7825
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007826 if ( curbuf->b_visual.vi_start.lnum == 0
7827 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7828 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 beep_flush();
7830 else
7831 {
7832 /* set w_cursor to the start of the Visual area, tpos to the end */
7833 if (VIsual_active)
7834 {
7835 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007836 VIsual_mode = curbuf->b_visual.vi_mode;
7837 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838# ifdef FEAT_EVAL
7839 curbuf->b_visual_mode_eval = i;
7840# endif
7841 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007842 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7843 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007845 tpos = curbuf->b_visual.vi_end;
7846 curbuf->b_visual.vi_end = curwin->w_cursor;
7847 curwin->w_cursor = curbuf->b_visual.vi_start;
7848 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 }
7850 else
7851 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007852 VIsual_mode = curbuf->b_visual.vi_mode;
7853 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7854 tpos = curbuf->b_visual.vi_end;
7855 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
7857
7858 VIsual_active = TRUE;
7859 VIsual_reselect = TRUE;
7860
7861 /* Set Visual to the start and w_cursor to the end of the Visual
7862 * area. Make sure they are on an existing character. */
7863 check_cursor();
7864 VIsual = curwin->w_cursor;
7865 curwin->w_cursor = tpos;
7866 check_cursor();
7867 update_topline();
7868 /*
7869 * When called from normal "g" command: start Select mode when
7870 * 'selectmode' contains "cmd". When called for K_SELECT, always
7871 * start Select mode.
7872 */
7873 if (cap->arg)
7874 VIsual_select = TRUE;
7875 else
7876 may_start_select('c');
7877#ifdef FEAT_MOUSE
7878 setmouse();
7879#endif
7880#ifdef FEAT_CLIPBOARD
7881 /* Make sure the clipboard gets updated. Needed because start and
7882 * end are still the same, and the selection needs to be owned */
7883 clip_star.vmode = NUL;
7884#endif
7885 redraw_curbuf_later(INVERTED);
7886 showmode();
7887 }
7888 break;
7889 /*
7890 * "gV": Don't reselect the previous Visual area after a Select mode
7891 * mapping of menu.
7892 */
7893 case 'V':
7894 VIsual_reselect = FALSE;
7895 break;
7896
7897 /*
7898 * "gh": start Select mode.
7899 * "gH": start Select line mode.
7900 * "g^H": start Select block mode.
7901 */
7902 case K_BS:
7903 cap->nchar = Ctrl_H;
7904 /* FALLTHROUGH */
7905 case 'h':
7906 case 'H':
7907 case Ctrl_H:
7908# ifdef EBCDIC
7909 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7910 if (cap->nchar == Ctrl_H)
7911 cap->cmdchar = Ctrl_V;
7912 else
7913# endif
7914 cap->cmdchar = cap->nchar + ('v' - 'h');
7915 cap->arg = TRUE;
7916 nv_visual(cap);
7917 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007918
7919 /* "gn", "gN" visually select next/previous search match
7920 * "gn" selects next match
7921 * "gN" selects previous match
7922 */
7923 case 'N':
7924 case 'n':
7925 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007926 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007927 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928
7929 /*
7930 * "gj" and "gk" two new funny movement keys -- up and down
7931 * movement based on *screen* line rather than *file* line.
7932 */
7933 case 'j':
7934 case K_DOWN:
7935 /* with 'nowrap' it works just like the normal "j" command; also when
7936 * in a closed fold */
7937 if (!curwin->w_p_wrap
7938#ifdef FEAT_FOLDING
7939 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7940#endif
7941 )
7942 {
7943 oap->motion_type = MLINE;
7944 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7945 }
7946 else
7947 i = nv_screengo(oap, FORWARD, cap->count1);
7948 if (i == FAIL)
7949 clearopbeep(oap);
7950 break;
7951
7952 case 'k':
7953 case K_UP:
7954 /* with 'nowrap' it works just like the normal "k" command; also when
7955 * in a closed fold */
7956 if (!curwin->w_p_wrap
7957#ifdef FEAT_FOLDING
7958 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7959#endif
7960 )
7961 {
7962 oap->motion_type = MLINE;
7963 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7964 }
7965 else
7966 i = nv_screengo(oap, BACKWARD, cap->count1);
7967 if (i == FAIL)
7968 clearopbeep(oap);
7969 break;
7970
7971 /*
7972 * "gJ": join two lines without inserting a space.
7973 */
7974 case 'J':
7975 nv_join(cap);
7976 break;
7977
7978 /*
7979 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7980 * "gm": middle of "g0" and "g$".
7981 */
7982 case '^':
7983 flag = TRUE;
7984 /* FALLTHROUGH */
7985
7986 case '0':
7987 case 'm':
7988 case K_HOME:
7989 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 oap->motion_type = MCHAR;
7991 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007992 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007994 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 int width2 = width1 + curwin_col_off2();
7996
7997 validate_virtcol();
7998 i = 0;
7999 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8000 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8001 }
8002 else
8003 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008004 /* Go to the middle of the screen line. When 'number' or
8005 * 'relativenumber' is on and lines are wrapping the middle can be more
8006 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008008 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 + ((curwin->w_p_wrap && i > 0)
8010 ? curwin_col_off2() : 0)) / 2;
8011 coladvance((colnr_T)i);
8012 if (flag)
8013 {
8014 do
8015 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008016 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008017 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 }
8019 curwin->w_set_curswant = TRUE;
8020 break;
8021
8022 case '_':
8023 /* "g_": to the last non-blank character in the line or <count> lines
8024 * downward. */
8025 cap->oap->motion_type = MCHAR;
8026 cap->oap->inclusive = TRUE;
8027 curwin->w_curswant = MAXCOL;
8028 if (cursor_down((long)(cap->count1 - 1),
8029 cap->oap->op_type == OP_NOP) == FAIL)
8030 clearopbeep(cap->oap);
8031 else
8032 {
8033 char_u *ptr = ml_get_curline();
8034
8035 /* In Visual mode we may end up after the line. */
8036 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8037 --curwin->w_cursor.col;
8038
8039 /* Decrease the cursor column until it's on a non-blank. */
8040 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008041 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 --curwin->w_cursor.col;
8043 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008044 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 }
8046 break;
8047
8048 case '$':
8049 case K_END:
8050 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {
8052 int col_off = curwin_col_off();
8053
8054 oap->motion_type = MCHAR;
8055 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008056 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {
8058 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8059 if (cap->count1 == 1)
8060 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008061 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 int width2 = width1 + curwin_col_off2();
8063
8064 validate_virtcol();
8065 i = width1 - 1;
8066 if (curwin->w_virtcol >= (colnr_T)width1)
8067 i += ((curwin->w_virtcol - width1) / width2 + 1)
8068 * width2;
8069 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008070
8071 /* Make sure we stick in this column. */
8072 validate_virtcol();
8073 curwin->w_curswant = curwin->w_virtcol;
8074 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8076 {
8077 /*
8078 * Check for landing on a character that got split at
8079 * the end of the line. We do not want to advance to
8080 * the next screen line.
8081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 if (curwin->w_virtcol > (colnr_T)i)
8083 --curwin->w_cursor.col;
8084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 }
8086 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8087 clearopbeep(oap);
8088 }
8089 else
8090 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008091 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008093
8094 /* Make sure we stick in this column. */
8095 validate_virtcol();
8096 curwin->w_curswant = curwin->w_virtcol;
8097 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 }
8099 }
8100 break;
8101
8102 /*
8103 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8104 */
8105 case '*':
8106 case '#':
8107#if POUND != '#'
8108 case POUND: /* pound sign (sometimes equal to '#') */
8109#endif
8110 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8111 case ']': /* :tselect for current identifier */
8112 nv_ident(cap);
8113 break;
8114
8115 /*
8116 * ge and gE: go back to end of word
8117 */
8118 case 'e':
8119 case 'E':
8120 oap->motion_type = MCHAR;
8121 curwin->w_set_curswant = TRUE;
8122 oap->inclusive = TRUE;
8123 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8124 clearopbeep(oap);
8125 break;
8126
8127 /*
8128 * "g CTRL-G": display info about cursor position
8129 */
8130 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008131 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 break;
8133
8134 /*
8135 * "gi": start Insert at the last position.
8136 */
8137 case 'i':
8138 if (curbuf->b_last_insert.lnum != 0)
8139 {
8140 curwin->w_cursor = curbuf->b_last_insert;
8141 check_cursor_lnum();
8142 i = (int)STRLEN(ml_get_curline());
8143 if (curwin->w_cursor.col > (colnr_T)i)
8144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (virtual_active())
8146 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 curwin->w_cursor.col = i;
8148 }
8149 }
8150 cap->cmdchar = 'i';
8151 nv_edit(cap);
8152 break;
8153
8154 /*
8155 * "gI": Start insert in column 1.
8156 */
8157 case 'I':
8158 beginline(0);
8159 if (!checkclearopq(oap))
8160 invoke_edit(cap, FALSE, 'g', FALSE);
8161 break;
8162
8163#ifdef FEAT_SEARCHPATH
8164 /*
8165 * "gf": goto file, edit file under cursor
8166 * "]f" and "[f": can also be used.
8167 */
8168 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008169 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 nv_gotofile(cap);
8171 break;
8172#endif
8173
8174 /* "g'm" and "g`m": jump to mark without setting pcmark */
8175 case '\'':
8176 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008177 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 case '`':
8179 nv_gomark(cap);
8180 break;
8181
8182 /*
8183 * "gs": Goto sleep.
8184 */
8185 case 's':
8186 do_sleep(cap->count1 * 1000L);
8187 break;
8188
8189 /*
8190 * "ga": Display the ascii value of the character under the
8191 * cursor. It is displayed in decimal, hex, and octal. -- webb
8192 */
8193 case 'a':
8194 do_ascii(NULL);
8195 break;
8196
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 /*
8198 * "g8": Display the bytes used for the UTF-8 character under the
8199 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008200 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 */
8202 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008203 if (cap->count0 == 8)
8204 utf_find_illegal();
8205 else
8206 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208
Bram Moolenaar60402d62017-04-20 18:54:50 +02008209 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008210 case '<':
8211 show_sb_text();
8212 break;
8213
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 /*
8215 * "gg": Goto the first line in file. With a count it goes to
8216 * that line number like for "G". -- webb
8217 */
8218 case 'g':
8219 cap->arg = FALSE;
8220 nv_goto(cap);
8221 break;
8222
8223 /*
8224 * Two-character operators:
8225 * "gq" Format text
8226 * "gw" Format text and keep cursor position
8227 * "g~" Toggle the case of the text.
8228 * "gu" Change text to lower case.
8229 * "gU" Change text to upper case.
8230 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008231 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 */
8233 case 'q':
8234 case 'w':
8235 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008236 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 case '~':
8238 case 'u':
8239 case 'U':
8240 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008241 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 nv_operator(cap);
8243 break;
8244
8245 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008246 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 * current function
8248 * "gD": idem, but in the current file.
8249 */
8250 case 'd':
8251 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008252 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 break;
8254
8255#ifdef FEAT_MOUSE
8256 /*
8257 * g<*Mouse> : <C-*mouse>
8258 */
8259 case K_MIDDLEMOUSE:
8260 case K_MIDDLEDRAG:
8261 case K_MIDDLERELEASE:
8262 case K_LEFTMOUSE:
8263 case K_LEFTDRAG:
8264 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008265 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 case K_RIGHTMOUSE:
8267 case K_RIGHTDRAG:
8268 case K_RIGHTRELEASE:
8269 case K_X1MOUSE:
8270 case K_X1DRAG:
8271 case K_X1RELEASE:
8272 case K_X2MOUSE:
8273 case K_X2DRAG:
8274 case K_X2RELEASE:
8275 mod_mask = MOD_MASK_CTRL;
8276 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8277 break;
8278#endif
8279
8280 case K_IGNORE:
8281 break;
8282
8283 /*
8284 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8285 */
8286 case 'p':
8287 case 'P':
8288 nv_put(cap);
8289 break;
8290
8291#ifdef FEAT_BYTEOFF
8292 /* "go": goto byte count from start of buffer */
8293 case 'o':
8294 goto_byte(cap->count0);
8295 break;
8296#endif
8297
8298 /* "gQ": improved Ex mode */
8299 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008300 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {
8302 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008303 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 break;
8305 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008306
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 if (!checkclearopq(oap))
8308 do_exmode(TRUE);
8309 break;
8310
8311#ifdef FEAT_JUMPLIST
8312 case ',':
8313 nv_pcmark(cap);
8314 break;
8315
8316 case ';':
8317 cap->count1 = -cap->count1;
8318 nv_pcmark(cap);
8319 break;
8320#endif
8321
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008322 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008323 if (!checkclearop(oap))
8324 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008325 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008326 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008327 if (!checkclearop(oap))
8328 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008329 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008330
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008331 case '+':
8332 case '-': /* "g+" and "g-": undo or redo along the timeline */
8333 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008334 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008335 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008336 break;
8337
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 default:
8339 clearopbeep(oap);
8340 break;
8341 }
8342}
8343
8344/*
8345 * Handle "o" and "O" commands.
8346 */
8347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008348n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008350#ifdef FEAT_CONCEAL
8351 linenr_T oldline = curwin->w_cursor.lnum;
8352#endif
8353
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 if (!checkclearopq(cap->oap))
8355 {
8356#ifdef FEAT_FOLDING
8357 if (cap->cmdchar == 'O')
8358 /* Open above the first line of a folded sequence of lines */
8359 (void)hasFolding(curwin->w_cursor.lnum,
8360 &curwin->w_cursor.lnum, NULL);
8361 else
8362 /* Open below the last line of a folded sequence of lines */
8363 (void)hasFolding(curwin->w_cursor.lnum,
8364 NULL, &curwin->w_cursor.lnum);
8365#endif
8366 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8367 (cap->cmdchar == 'O' ? 1 : 0)),
8368 (linenr_T)(curwin->w_cursor.lnum +
8369 (cap->cmdchar == 'o' ? 1 : 0))
8370 ) == OK
8371 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8372#ifdef FEAT_COMMENTS
8373 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8374#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02008375 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008377#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008378 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01008379 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008380#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008381#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008382 if (curwin->w_p_cul)
8383 /* force redraw of cursorline */
8384 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008385#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008386 /* When '#' is in 'cpoptions' ignore the count. */
8387 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8388 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8390 }
8391 }
8392}
8393
8394/*
8395 * "." command: redo last change.
8396 */
8397 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008398nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399{
8400 if (!checkclearopq(cap->oap))
8401 {
8402 /*
8403 * If "restart_edit" is TRUE, the last but one command is repeated
8404 * instead of the last command (inserting text). This is used for
8405 * CTRL-O <.> in insert mode.
8406 */
8407 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8408 clearopbeep(cap->oap);
8409 }
8410}
8411
8412/*
8413 * CTRL-R: undo undo
8414 */
8415 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008416nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
8418 if (!checkclearopq(cap->oap))
8419 {
8420 u_redo((int)cap->count1);
8421 curwin->w_set_curswant = TRUE;
8422 }
8423}
8424
8425/*
8426 * Handle "U" command.
8427 */
8428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008429nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430{
8431 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008432 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {
8434 /* translate "gUU" to "gUgU" */
8435 cap->cmdchar = 'g';
8436 cap->nchar = 'U';
8437 nv_operator(cap);
8438 }
8439 else if (!checkclearopq(cap->oap))
8440 {
8441 u_undoline();
8442 curwin->w_set_curswant = TRUE;
8443 }
8444}
8445
8446/*
8447 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8448 * single character.
8449 */
8450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008451nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008453 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02008454 {
8455#ifdef FEAT_JOB_CHANNEL
8456 if (bt_prompt(curbuf) && !prompt_curpos_editable())
8457 {
8458 clearopbeep(cap->oap);
8459 return;
8460 }
8461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 else
8465 nv_operator(cap);
8466}
8467
8468/*
8469 * Handle an operator command.
8470 * The actual work is done by do_pending_operator().
8471 */
8472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008473nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474{
8475 int op_type;
8476
8477 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008478#ifdef FEAT_JOB_CHANNEL
8479 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
8480 {
8481 clearopbeep(cap->oap);
8482 return;
8483 }
8484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485
8486 if (op_type == cap->oap->op_type) /* double operator works on lines */
8487 nv_lineop(cap);
8488 else if (!checkclearop(cap->oap))
8489 {
8490 cap->oap->start = curwin->w_cursor;
8491 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008492#ifdef FEAT_EVAL
8493 set_op_var(op_type);
8494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 }
8496}
8497
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008498#ifdef FEAT_EVAL
8499/*
8500 * Set v:operator to the characters for "optype".
8501 */
8502 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008503set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008504{
8505 char_u opchars[3];
8506
8507 if (optype == OP_NOP)
8508 set_vim_var_string(VV_OP, NULL, 0);
8509 else
8510 {
8511 opchars[0] = get_op_char(optype);
8512 opchars[1] = get_extra_op_char(optype);
8513 opchars[2] = NUL;
8514 set_vim_var_string(VV_OP, opchars, -1);
8515 }
8516}
8517#endif
8518
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519/*
8520 * Handle linewise operator "dd", "yy", etc.
8521 *
8522 * "_" is is a strange motion command that helps make operators more logical.
8523 * It is actually implemented, but not documented in the real Vi. This motion
8524 * command actually refers to "the current line". Commands like "dd" and "yy"
8525 * are really an alternate form of "d_" and "y_". It does accept a count, so
8526 * "d3_" works to delete 3 lines.
8527 */
8528 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008529nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 cap->oap->motion_type = MLINE;
8532 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8533 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008534 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8535 && cap->oap->motion_force != 'v'
8536 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 || cap->oap->op_type == OP_LSHIFT
8538 || cap->oap->op_type == OP_RSHIFT)
8539 beginline(BL_SOL | BL_FIX);
8540 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8541 beginline(BL_WHITE | BL_FIX);
8542}
8543
8544/*
8545 * <Home> command.
8546 */
8547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008548nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008550 /* CTRL-HOME is like "gg" */
8551 if (mod_mask & MOD_MASK_CTRL)
8552 nv_goto(cap);
8553 else
8554 {
8555 cap->count0 = 1;
8556 nv_pipe(cap);
8557 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008558 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8559 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560}
8561
8562/*
8563 * "|" command.
8564 */
8565 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008566nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
8568 cap->oap->motion_type = MCHAR;
8569 cap->oap->inclusive = FALSE;
8570 beginline(0);
8571 if (cap->count0 > 0)
8572 {
8573 coladvance((colnr_T)(cap->count0 - 1));
8574 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8575 }
8576 else
8577 curwin->w_curswant = 0;
8578 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008579 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 curwin->w_set_curswant = FALSE;
8581}
8582
8583/*
8584 * Handle back-word command "b" and "B".
8585 * cap->arg is 1 for "B"
8586 */
8587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008588nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589{
8590 cap->oap->motion_type = MCHAR;
8591 cap->oap->inclusive = FALSE;
8592 curwin->w_set_curswant = TRUE;
8593 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8594 clearopbeep(cap->oap);
8595#ifdef FEAT_FOLDING
8596 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8597 foldOpenCursor();
8598#endif
8599}
8600
8601/*
8602 * Handle word motion commands "e", "E", "w" and "W".
8603 * cap->arg is TRUE for "E" and "W".
8604 */
8605 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008606nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
8608 int n;
8609 int word_end;
8610 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008611 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612
8613 /*
8614 * Set inclusive for the "E" and "e" command.
8615 */
8616 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8617 word_end = TRUE;
8618 else
8619 word_end = FALSE;
8620 cap->oap->inclusive = word_end;
8621
8622 /*
8623 * "cw" and "cW" are a special case.
8624 */
8625 if (!word_end && cap->oap->op_type == OP_CHANGE)
8626 {
8627 n = gchar_cursor();
8628 if (n != NUL) /* not an empty line */
8629 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008630 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 {
8632 /*
8633 * Reproduce a funny Vi behaviour: "cw" on a blank only
8634 * changes one character, not all blanks until the start of
8635 * the next word. Only do this when the 'w' flag is included
8636 * in 'cpoptions'.
8637 */
8638 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8639 {
8640 cap->oap->inclusive = TRUE;
8641 cap->oap->motion_type = MCHAR;
8642 return;
8643 }
8644 }
8645 else
8646 {
8647 /*
8648 * This is a little strange. To match what the real Vi does,
8649 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8650 * that we are not on a space or a TAB. This seems impolite
8651 * at first, but it's really more what we mean when we say
8652 * 'cw'.
8653 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008654 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 * will change only one character! This is done by setting
8656 * flag.
8657 */
8658 cap->oap->inclusive = TRUE;
8659 word_end = TRUE;
8660 flag = TRUE;
8661 }
8662 }
8663 }
8664
8665 cap->oap->motion_type = MCHAR;
8666 curwin->w_set_curswant = TRUE;
8667 if (word_end)
8668 n = end_word(cap->count1, cap->arg, flag, FALSE);
8669 else
8670 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8671
Bram Moolenaardfefb982008-04-01 10:06:39 +00008672 /* Don't leave the cursor on the NUL past the end of line. Unless we
8673 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008674 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008675 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676
8677 if (n == FAIL && cap->oap->op_type == OP_NOP)
8678 clearopbeep(cap->oap);
8679 else
8680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682#ifdef FEAT_FOLDING
8683 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8684 foldOpenCursor();
8685#endif
8686 }
8687}
8688
8689/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008690 * Used after a movement command: If the cursor ends up on the NUL after the
8691 * end of the line, may move it back to the last character and make the motion
8692 * inclusive.
8693 */
8694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008695adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008696{
8697 /* The cursor cannot remain on the NUL when:
8698 * - the column is > 0
8699 * - not in Visual mode or 'selection' is "o"
8700 * - 'virtualedit' is not "all" and not "onemore".
8701 */
8702 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008703 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008704 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008705 {
8706 --curwin->w_cursor.col;
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008707 /* prevent cursor from moving on the trail byte */
8708 if (has_mbyte)
8709 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008710 oap->inclusive = TRUE;
8711 }
8712}
8713
8714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 * "0" and "^" commands.
8716 * cap->arg is the argument for beginline().
8717 */
8718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008719nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
8721 cap->oap->motion_type = MCHAR;
8722 cap->oap->inclusive = FALSE;
8723 beginline(cap->arg);
8724#ifdef FEAT_FOLDING
8725 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8726 foldOpenCursor();
8727#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008728 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8729 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730}
8731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732/*
8733 * In exclusive Visual mode, may include the last character.
8734 */
8735 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008736adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737{
8738 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008739 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 if (has_mbyte)
8742 inc_cursor();
8743 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 ++curwin->w_cursor.col;
8745 cap->oap->inclusive = FALSE;
8746 }
8747}
8748
8749/*
8750 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8751 * Should check VIsual_mode before calling this.
8752 * Returns TRUE when backed up to the previous line.
8753 */
8754 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008755unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757 pos_T *pp;
8758
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008759 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008761 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 pp = &curwin->w_cursor;
8763 else
8764 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 if (pp->coladd > 0)
8766 --pp->coladd;
8767 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 if (pp->col > 0)
8769 {
8770 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008771 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 }
8773 else if (pp->lnum > 1)
8774 {
8775 --pp->lnum;
8776 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8777 return TRUE;
8778 }
8779 }
8780 return FALSE;
8781}
8782
8783/*
8784 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8785 */
8786 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008787nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
8789 if (VIsual_active)
8790 VIsual_select = TRUE;
8791 else if (VIsual_reselect)
8792 {
8793 cap->nchar = 'v'; /* fake "gv" command */
8794 cap->arg = TRUE;
8795 nv_g_cmd(cap);
8796 }
8797}
8798
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799
8800/*
8801 * "G", "gg", CTRL-END, CTRL-HOME.
8802 * cap->arg is TRUE for "G".
8803 */
8804 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008805nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806{
8807 linenr_T lnum;
8808
8809 if (cap->arg)
8810 lnum = curbuf->b_ml.ml_line_count;
8811 else
8812 lnum = 1L;
8813 cap->oap->motion_type = MLINE;
8814 setpcmark();
8815
8816 /* When a count is given, use it instead of the default lnum */
8817 if (cap->count0 != 0)
8818 lnum = cap->count0;
8819 if (lnum < 1L)
8820 lnum = 1L;
8821 else if (lnum > curbuf->b_ml.ml_line_count)
8822 lnum = curbuf->b_ml.ml_line_count;
8823 curwin->w_cursor.lnum = lnum;
8824 beginline(BL_SOL | BL_FIX);
8825#ifdef FEAT_FOLDING
8826 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8827 foldOpenCursor();
8828#endif
8829}
8830
8831/*
8832 * CTRL-\ in Normal mode.
8833 */
8834 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008835nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836{
8837 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8838 {
8839 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008840 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 clear_cmdline = TRUE; /* unshow mode later */
8842 restart_edit = 0;
8843#ifdef FEAT_CMDWIN
8844 if (cmdwin_type != 0)
8845 cmdwin_result = Ctrl_C;
8846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 if (VIsual_active)
8848 {
8849 end_visual_mode(); /* stop Visual */
8850 redraw_curbuf_later(INVERTED);
8851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8853 if (cap->nchar == Ctrl_G && p_im)
8854 restart_edit = 'a';
8855 }
8856 else
8857 clearopbeep(cap->oap);
8858}
8859
8860/*
8861 * ESC in Normal mode: beep, but don't flush buffers.
8862 * Don't even beep if we are canceling a command.
8863 */
8864 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008865nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 int no_reason;
8868
8869 no_reason = (cap->oap->op_type == OP_NOP
8870 && cap->opcount == 0
8871 && cap->count0 == 0
8872 && cap->oap->regname == 0
8873 && !p_im);
8874
8875 if (cap->arg) /* TRUE for CTRL-C */
8876 {
8877 if (restart_edit == 0
8878#ifdef FEAT_CMDWIN
8879 && cmdwin_type == 0
8880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01008883 {
8884 if (anyBufIsChanged())
8885 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
8886 else
8887 msg(_("Type :qa and press <Enter> to exit Vim"));
8888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889
8890 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8891 * set again below when halfway a mapping. */
8892 if (!p_im)
8893 restart_edit = 0;
8894#ifdef FEAT_CMDWIN
8895 if (cmdwin_type != 0)
8896 {
8897 cmdwin_result = K_IGNORE;
8898 got_int = FALSE; /* don't stop executing autocommands et al. */
8899 return;
8900 }
8901#endif
8902 }
8903
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 if (VIsual_active)
8905 {
8906 end_visual_mode(); /* stop Visual */
8907 check_cursor_col(); /* make sure cursor is not beyond EOL */
8908 curwin->w_set_curswant = TRUE;
8909 redraw_curbuf_later(INVERTED);
8910 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008911 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008912 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 clearop(cap->oap);
8914
8915 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8916 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01008917 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 restart_edit = 'a';
8919}
8920
8921/*
8922 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01008923 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 */
8925 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008926nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
8928 /* <Insert> is equal to "i" */
8929 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8930 cap->cmdchar = 'i';
8931
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 /* in Visual mode "A" and "I" are an operator */
8933 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02008934 {
8935#ifdef FEAT_TERMINAL
8936 if (term_in_normal_mode())
8937 {
8938 end_visual_mode();
8939 clearop(cap->oap);
8940 term_enter_job_mode();
8941 return;
8942 }
8943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02008945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946
8947 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008948 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8949 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 {
8951#ifdef FEAT_TEXTOBJ
8952 nv_object(cap);
8953#else
8954 clearopbeep(cap->oap);
8955#endif
8956 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02008957#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02008958 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02008959 {
8960 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02008961 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02008962 return;
8963 }
8964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 else if (!curbuf->b_p_ma && !p_im)
8966 {
8967 /* Only give this error when 'insertmode' is off. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008968 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01008970 if (cap->cmdchar == K_PS)
8971 /* drop the pasted text */
8972 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 }
Bram Moolenaara1891842017-02-04 21:34:31 +01008974 else if (cap->cmdchar == K_PS && VIsual_active)
8975 {
8976 pos_T old_pos = curwin->w_cursor;
8977 pos_T old_visual = VIsual;
8978
8979 /* In Visual mode the selected text is deleted. */
8980 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
8981 {
8982 shift_delete_registers();
8983 cap->oap->regname = '1';
8984 }
8985 else
8986 cap->oap->regname = '-';
8987 cap->cmdchar = 'd';
8988 cap->nchar = NUL;
8989 nv_operator(cap);
8990 do_pending_operator(cap, 0, FALSE);
8991 cap->cmdchar = K_PS;
8992
8993 /* When the last char in the line was deleted then append. Detect this
8994 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008995 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
8996 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01008997 inc_cursor();
8998
8999 /* Insert to replace the deleted text with the pasted text. */
9000 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 else if (!checkclearopq(cap->oap))
9003 {
9004 switch (cap->cmdchar)
9005 {
9006 case 'A': /* "A"ppend after the line */
9007 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 if (ve_flags == VE_ALL)
9009 {
9010 int save_State = State;
9011
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009012 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 * character past the end of the line */
9014 State = INSERT;
9015 coladvance((colnr_T)MAXCOL);
9016 State = save_State;
9017 }
9018 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9020 break;
9021
9022 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009023 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9024 beginline(BL_WHITE);
9025 else
9026 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 break;
9028
Bram Moolenaara1891842017-02-04 21:34:31 +01009029 case K_PS:
9030 /* Bracketed paste works like "a"ppend, unless the cursor is in
9031 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009032 if (curwin->w_cursor.col == 0)
9033 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009034 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009035
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 case 'a': /* "a"ppend is like "i"nsert on the next character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 /* increment coladd when in virtual space, increment the
9038 * column otherwise, also to append after an unprintable char */
9039 if (virtual_active()
9040 && (curwin->w_cursor.coladd > 0
9041 || *ml_get_cursor() == NUL
9042 || *ml_get_cursor() == TAB))
9043 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009044 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 inc_cursor();
9046 break;
9047 }
9048
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9050 {
9051 int save_State = State;
9052
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009053 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 * character past the end of the line */
9055 State = INSERT;
9056 coladvance(getviscol());
9057 State = save_State;
9058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059
9060 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9061 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009062 else if (cap->cmdchar == K_PS)
9063 /* drop the pasted text */
9064 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065}
9066
9067/*
9068 * Invoke edit() and take care of "restart_edit" and the return value.
9069 */
9070 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009071invoke_edit(
9072 cmdarg_T *cap,
9073 int repl, /* "r" or "gr" command */
9074 int cmd,
9075 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
9077 int restart_edit_save = 0;
9078
9079 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9080 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9081 * it. */
9082 if (repl || !stuff_empty())
9083 restart_edit_save = restart_edit;
9084 else
9085 restart_edit_save = 0;
9086
9087 /* Always reset "restart_edit", this is not a restarted edit. */
9088 restart_edit = 0;
9089
9090 if (edit(cmd, startln, cap->count1))
9091 cap->retval |= CA_COMMAND_BUSY;
9092
9093 if (restart_edit == 0)
9094 restart_edit = restart_edit_save;
9095}
9096
9097#ifdef FEAT_TEXTOBJ
9098/*
9099 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9100 */
9101 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009102nv_object(
9103 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
9105 int flag;
9106 int include;
9107 char_u *mps_save;
9108
9109 if (cap->cmdchar == 'i')
9110 include = FALSE; /* "ix" = inner object: exclude white space */
9111 else
9112 include = TRUE; /* "ax" = an object: include white space */
9113
9114 /* Make sure (), [], {} and <> are in 'matchpairs' */
9115 mps_save = curbuf->b_p_mps;
9116 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9117
9118 switch (cap->nchar)
9119 {
9120 case 'w': /* "aw" = a word */
9121 flag = current_word(cap->oap, cap->count1, include, FALSE);
9122 break;
9123 case 'W': /* "aW" = a WORD */
9124 flag = current_word(cap->oap, cap->count1, include, TRUE);
9125 break;
9126 case 'b': /* "ab" = a braces block */
9127 case '(':
9128 case ')':
9129 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9130 break;
9131 case 'B': /* "aB" = a Brackets block */
9132 case '{':
9133 case '}':
9134 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9135 break;
9136 case '[': /* "a[" = a [] block */
9137 case ']':
9138 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9139 break;
9140 case '<': /* "a<" = a <> block */
9141 case '>':
9142 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9143 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009144 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009145 /* Do not adjust oap->end in do_pending_operator()
9146 * otherwise there are different results for 'dit'
9147 * (note leading whitespace in last line):
9148 * 1) <b> 2) <b>
9149 * foobar foobar
9150 * </b> </b>
9151 */
9152 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009153 flag = current_tagblock(cap->oap, cap->count1, include);
9154 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 case 'p': /* "ap" = a paragraph */
9156 flag = current_par(cap->oap, cap->count1, include, 'p');
9157 break;
9158 case 's': /* "as" = a sentence */
9159 flag = current_sent(cap->oap, cap->count1, include);
9160 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009161 case '"': /* "a"" = a double quoted string */
9162 case '\'': /* "a'" = a single quoted string */
9163 case '`': /* "a`" = a backtick quoted string */
9164 flag = current_quote(cap->oap, cap->count1, include,
9165 cap->nchar);
9166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167#if 0 /* TODO */
9168 case 'S': /* "aS" = a section */
9169 case 'f': /* "af" = a filename */
9170 case 'u': /* "au" = a URL */
9171#endif
9172 default:
9173 flag = FAIL;
9174 break;
9175 }
9176
9177 curbuf->b_p_mps = mps_save;
9178 if (flag == FAIL)
9179 clearopbeep(cap->oap);
9180 adjust_cursor_col();
9181 curwin->w_set_curswant = TRUE;
9182}
9183#endif
9184
9185/*
9186 * "q" command: Start/stop recording.
9187 * "q:", "q/", "q?": edit command-line in command-line window.
9188 */
9189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009190nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191{
9192 if (cap->oap->op_type == OP_FORMAT)
9193 {
9194 /* "gqq" is the same as "gqgq": format line */
9195 cap->cmdchar = 'g';
9196 cap->nchar = 'q';
9197 nv_operator(cap);
9198 }
9199 else if (!checkclearop(cap->oap))
9200 {
9201#ifdef FEAT_CMDWIN
9202 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9203 {
9204 stuffcharReadbuff(cap->nchar);
9205 stuffcharReadbuff(K_CMDWIN);
9206 }
9207 else
9208#endif
9209 /* (stop) recording into a named register, unless executing a
9210 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009211 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 clearopbeep(cap->oap);
9213 }
9214}
9215
9216/*
9217 * Handle the "@r" command.
9218 */
9219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009220nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221{
9222 if (checkclearop(cap->oap))
9223 return;
9224#ifdef FEAT_EVAL
9225 if (cap->nchar == '=')
9226 {
9227 if (get_expr_register() == NUL)
9228 return;
9229 }
9230#endif
9231 while (cap->count1-- && !got_int)
9232 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009233 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 {
9235 clearopbeep(cap->oap);
9236 break;
9237 }
9238 line_breakcheck();
9239 }
9240}
9241
9242/*
9243 * Handle the CTRL-U and CTRL-D commands.
9244 */
9245 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009246nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247{
9248 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9249 || (cap->cmdchar == Ctrl_D
9250 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9251 clearopbeep(cap->oap);
9252 else if (!checkclearop(cap->oap))
9253 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9254}
9255
9256/*
9257 * Handle "J" or "gJ" command.
9258 */
9259 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009260nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 if (VIsual_active) /* join the visual lines */
9263 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009264 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 {
9266 if (cap->count0 <= 1)
9267 cap->count0 = 2; /* default for join is two lines! */
9268 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9269 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009271 /* can't join when on the last line */
9272 if (cap->count0 <= 2)
9273 {
9274 clearopbeep(cap->oap);
9275 return;
9276 }
9277 cap->count0 = curbuf->b_ml.ml_line_count
9278 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009280
9281 prep_redo(cap->oap->regname, cap->count0,
9282 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9283 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 }
9285}
9286
9287/*
9288 * "P", "gP", "p" and "gp" commands.
9289 */
9290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009291nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 int regname = 0;
9294 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009295 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009296 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 int dir;
9298 int flags = 0;
9299
9300 if (cap->oap->op_type != OP_NOP)
9301 {
9302#ifdef FEAT_DIFF
9303 /* "dp" is ":diffput" */
9304 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9305 {
9306 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009307 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 }
9309 else
9310#endif
9311 clearopbeep(cap->oap);
9312 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02009313#ifdef FEAT_JOB_CHANNEL
9314 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
9315 {
9316 clearopbeep(cap->oap);
9317 }
9318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 else
9320 {
9321 dir = (cap->cmdchar == 'P'
9322 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9323 ? BACKWARD : FORWARD;
9324 prep_redo_cmd(cap);
9325 if (cap->cmdchar == 'g')
9326 flags |= PUT_CURSEND;
9327
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 if (VIsual_active)
9329 {
9330 /* Putting in Visual mode: The put text replaces the selected
9331 * text. First delete the selected text, then put the new text.
9332 * Need to save and restore the registers that the delete
9333 * overwrites if the old contents is being put.
9334 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009335 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009337#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009339#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009340 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009341 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009342#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345
9346 )
9347 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009348 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 * put, save it first. */
9350 reg1 = get_register(regname, TRUE);
9351 }
9352
9353 /* Now delete the selected text. */
9354 cap->cmdchar = 'd';
9355 cap->nchar = NUL;
9356 cap->oap->regname = NUL;
9357 nv_operator(cap);
9358 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009359 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360
9361 /* delete PUT_LINE_BACKWARD; */
9362 cap->oap->regname = regname;
9363
9364 if (reg1 != NULL)
9365 {
9366 /* Delete probably changed the register we want to put, save
9367 * it first. Then put back what was there before the delete. */
9368 reg2 = get_register(regname, FALSE);
9369 put_register(regname, reg1);
9370 }
9371
9372 /* When deleted a linewise Visual area, put the register as
9373 * lines to avoid it joined with the next line. When deletion was
9374 * characterwise, split a line when putting lines. */
9375 if (VIsual_mode == 'V')
9376 flags |= PUT_LINE;
9377 else if (VIsual_mode == 'v')
9378 flags |= PUT_LINE_SPLIT;
9379 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9380 flags |= PUT_LINE_FORWARD;
9381 dir = BACKWARD;
9382 if ((VIsual_mode != 'V'
9383 && curwin->w_cursor.col < curbuf->b_op_start.col)
9384 || (VIsual_mode == 'V'
9385 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9386 /* cursor is at the end of the line or end of file, put
9387 * forward. */
9388 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009389 /* May have been reset in do_put(). */
9390 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 do_put(cap->oap->regname, dir, cap->count1, flags);
9393
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 /* If a register was saved, put it back now. */
9395 if (reg2 != NULL)
9396 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009397
9398 /* What to reselect with "gv"? Selecting the just put text seems to
9399 * be the most useful, since the original text was removed. */
9400 if (was_visual)
9401 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009402 curbuf->b_visual.vi_start = curbuf->b_op_start;
9403 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009404 /* need to adjust cursor position */
9405 if (*p_sel == 'e')
9406 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009407 }
9408
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009409 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009410 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009411 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009412 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009413 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009414
9415 /* If the cursor was in that line, move it to the end of the last
9416 * line. */
9417 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9418 {
9419 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9420 coladvance((colnr_T)MAXCOL);
9421 }
9422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 auto_format(FALSE, TRUE);
9424 }
9425}
9426
9427/*
9428 * "o" and "O" commands.
9429 */
9430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009431nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432{
9433#ifdef FEAT_DIFF
9434 /* "do" is ":diffget" */
9435 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9436 {
9437 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009438 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 }
9440 else
9441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 if (VIsual_active) /* switch start and end of visual */
9443 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009444#ifdef FEAT_JOB_CHANNEL
9445 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02009446 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 n_opencmd(cap);
9450}
9451
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452#ifdef FEAT_NETBEANS_INTG
9453 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009454nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456 netbeans_keycommand(cap->nchar);
9457}
9458#endif
9459
9460#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009462nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463{
9464 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9465}
9466#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009467
Bram Moolenaar3918c952005-03-15 22:34:55 +00009468/*
9469 * Trigger CursorHold event.
9470 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9471 * input buffer. "did_cursorhold" is set to avoid retriggering.
9472 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009473 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009474nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009475{
9476 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9477 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009478 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009479}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009480
9481/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009482 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009483 */
9484 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009485get_op_vcol(
9486 oparg_T *oap,
9487 colnr_T redo_VIsual_vcol,
9488 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009489{
9490 colnr_T start, end;
9491
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009492 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009493 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009494 return;
9495
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009496 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009497
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009498 /* prevent from moving onto a trail byte */
9499 if (has_mbyte)
9500 mb_adjustpos(curwin->w_buffer, &oap->end);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009501
9502 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009503
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009504 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009505 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009506 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9507
9508 if (start < oap->start_vcol)
9509 oap->start_vcol = start;
9510 if (end > oap->end_vcol)
9511 {
9512 if (initial && *p_sel == 'e' && start >= 1
9513 && start - 1 >= oap->end_vcol)
9514 oap->end_vcol = start - 1;
9515 else
9516 oap->end_vcol = end;
9517 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009518 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009519
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009520 /* if '$' was used, get oap->end_vcol from longest line */
9521 if (curwin->w_curswant == MAXCOL)
9522 {
9523 curwin->w_cursor.col = MAXCOL;
9524 oap->end_vcol = 0;
9525 for (curwin->w_cursor.lnum = oap->start.lnum;
9526 curwin->w_cursor.lnum <= oap->end.lnum;
9527 ++curwin->w_cursor.lnum)
9528 {
9529 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9530 if (end > oap->end_vcol)
9531 oap->end_vcol = end;
9532 }
9533 }
9534 else if (redo_VIsual_busy)
9535 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9536 /*
9537 * Correct oap->end.col and oap->start.col to be the
9538 * upper-left and lower-right corner of the block area.
9539 *
9540 * (Actually, this does convert column positions into character
9541 * positions)
9542 */
9543 curwin->w_cursor.lnum = oap->end.lnum;
9544 coladvance(oap->end_vcol);
9545 oap->end = curwin->w_cursor;
9546
9547 curwin->w_cursor = oap->start;
9548 coladvance(oap->start_vcol);
9549 oap->start = curwin->w_cursor;
9550}