blob: 5ae9ff1aeec7b1d7f4605c9fca27599b095b87ed [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 Moolenaar071d4272004-06-13 20:20:40 +000030static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020031#ifdef __BORLANDC__
32 _RTLENTRYF
33#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034 nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void op_colon(oparg_T *oap);
36static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010037#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void find_start_of_word(pos_T *);
39static void find_end_of_word(pos_T *);
40static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void prep_redo(int regname, long, int, int, int, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void clearop(oparg_T *oap);
44static void clearopbeep(oparg_T *oap);
45static void unshift_special(cmdarg_T *cap);
46static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
50
51/*
52 * nv_*(): functions called to handle Normal and Visual mode commands.
53 * n_*(): functions called to handle Normal mode commands.
54 * v_*(): functions called to handle Visual mode commands.
55 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010056static void nv_ignore(cmdarg_T *cap);
57static void nv_nop(cmdarg_T *cap);
58static void nv_error(cmdarg_T *cap);
59static void nv_help(cmdarg_T *cap);
60static void nv_addsub(cmdarg_T *cap);
61static void nv_page(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010063static void nv_mousescroll(cmdarg_T *cap);
64static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010066static void nv_scroll_line(cmdarg_T *cap);
67static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_ver_scrollbar(cmdarg_T *cap);
70static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000072#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010073static void nv_tabline(cmdarg_T *cap);
74static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000075#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010076static void nv_exmode(cmdarg_T *cap);
77static void nv_colon(cmdarg_T *cap);
78static void nv_ctrlg(cmdarg_T *cap);
79static void nv_ctrlh(cmdarg_T *cap);
80static void nv_clear(cmdarg_T *cap);
81static void nv_ctrlo(cmdarg_T *cap);
82static void nv_hat(cmdarg_T *cap);
83static void nv_Zet(cmdarg_T *cap);
84static void nv_ident(cmdarg_T *cap);
85static void nv_tagpop(cmdarg_T *cap);
86static void nv_scroll(cmdarg_T *cap);
87static void nv_right(cmdarg_T *cap);
88static void nv_left(cmdarg_T *cap);
89static void nv_up(cmdarg_T *cap);
90static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010091static void nv_end(cmdarg_T *cap);
92static void nv_dollar(cmdarg_T *cap);
93static void nv_search(cmdarg_T *cap);
94static void nv_next(cmdarg_T *cap);
95static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
96static void nv_csearch(cmdarg_T *cap);
97static void nv_brackets(cmdarg_T *cap);
98static void nv_percent(cmdarg_T *cap);
99static void nv_brace(cmdarg_T *cap);
100static void nv_mark(cmdarg_T *cap);
101static void nv_findpar(cmdarg_T *cap);
102static void nv_undo(cmdarg_T *cap);
103static void nv_kundo(cmdarg_T *cap);
104static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100105static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100106static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
107static void v_visop(cmdarg_T *cap);
108static void nv_subst(cmdarg_T *cap);
109static void nv_abbrev(cmdarg_T *cap);
110static void nv_optrans(cmdarg_T *cap);
111static void nv_gomark(cmdarg_T *cap);
112static void nv_pcmark(cmdarg_T *cap);
113static void nv_regname(cmdarg_T *cap);
114static void nv_visual(cmdarg_T *cap);
115static void n_start_visual_mode(int c);
116static void nv_window(cmdarg_T *cap);
117static void nv_suspend(cmdarg_T *cap);
118static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_dot(cmdarg_T *cap);
120static void nv_redo(cmdarg_T *cap);
121static void nv_Undo(cmdarg_T *cap);
122static void nv_tilde(cmdarg_T *cap);
123static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000124#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000126#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100127static void nv_lineop(cmdarg_T *cap);
128static void nv_home(cmdarg_T *cap);
129static void nv_pipe(cmdarg_T *cap);
130static void nv_bck_word(cmdarg_T *cap);
131static void nv_wordcmd(cmdarg_T *cap);
132static void nv_beginline(cmdarg_T *cap);
133static void adjust_cursor(oparg_T *oap);
134static void adjust_for_sel(cmdarg_T *cap);
135static int unadjust_for_sel(void);
136static void nv_select(cmdarg_T *cap);
137static void nv_goto(cmdarg_T *cap);
138static void nv_normal(cmdarg_T *cap);
139static void nv_esc(cmdarg_T *oap);
140static void nv_edit(cmdarg_T *cap);
141static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100143static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100145static void nv_record(cmdarg_T *cap);
146static void nv_at(cmdarg_T *cap);
147static void nv_halfpage(cmdarg_T *cap);
148static void nv_join(cmdarg_T *cap);
149static void nv_put(cmdarg_T *cap);
150static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100152static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
154#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100155static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100157static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100158static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000160static char *e_noident = N_("E349: No identifier under cursor");
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Function to be called for a Normal or Visual mode command.
164 * The argument is a cmdarg_T.
165 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100166typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168/* Values for cmd_flags. */
169#define NV_NCH 0x01 /* may need to get a second char */
170#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
171#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
172#define NV_LANG 0x08 /* second char needs language adjustment */
173
174#define NV_SS 0x10 /* may start selection */
175#define NV_SSS 0x20 /* may start selection with shift modifier */
176#define NV_STS 0x40 /* may stop selection without shift modif. */
177#define NV_RL 0x80 /* 'rightleft' modifies command */
178#define NV_KEEPREG 0x100 /* don't clear regname */
179#define NV_NCW 0x200 /* not allowed in command-line window */
180
181/*
182 * Generally speaking, every Normal mode command should either clear any
183 * pending operator (with *clearop*()), or set the motion type variable
184 * oap->motion_type.
185 *
186 * When a cursor motion command is made, it is marked as being a character or
187 * line oriented motion. Then, if an operator is in effect, the operation
188 * becomes character or line oriented accordingly.
189 */
190
191/*
192 * This table contains one entry for every Normal or Visual mode command.
193 * The order doesn't matter, init_normal_cmds() will create a sorted index.
194 * It is faster when all keys from zero to '~' are present.
195 */
196static const struct nv_cmd
197{
198 int cmd_char; /* (first) command character */
199 nv_func_T cmd_func; /* function for this command */
200 short_u cmd_flags; /* NV_ flags */
201 short cmd_arg; /* value for ca.arg */
202} nv_cmds[] =
203{
204 {NUL, nv_error, 0, 0},
205 {Ctrl_A, nv_addsub, 0, 0},
206 {Ctrl_B, nv_page, NV_STS, BACKWARD},
207 {Ctrl_C, nv_esc, 0, TRUE},
208 {Ctrl_D, nv_halfpage, 0, 0},
209 {Ctrl_E, nv_scroll_line, 0, TRUE},
210 {Ctrl_F, nv_page, NV_STS, FORWARD},
211 {Ctrl_G, nv_ctrlg, 0, 0},
212 {Ctrl_H, nv_ctrlh, 0, 0},
213 {Ctrl_I, nv_pcmark, 0, 0},
214 {NL, nv_down, 0, FALSE},
215 {Ctrl_K, nv_error, 0, 0},
216 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000217 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 {Ctrl_N, nv_down, NV_STS, FALSE},
219 {Ctrl_O, nv_ctrlo, 0, 0},
220 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000221 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 {Ctrl_R, nv_redo, 0, 0},
223 {Ctrl_S, nv_ignore, 0, 0},
224 {Ctrl_T, nv_tagpop, NV_NCW, 0},
225 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 {Ctrl_V, nv_visual, 0, FALSE},
227 {'V', nv_visual, 0, FALSE},
228 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 {Ctrl_W, nv_window, 0, 0},
230 {Ctrl_X, nv_addsub, 0, 0},
231 {Ctrl_Y, nv_scroll_line, 0, FALSE},
232 {Ctrl_Z, nv_suspend, 0, 0},
233 {ESC, nv_esc, 0, FALSE},
234 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
235 {Ctrl_RSB, nv_ident, NV_NCW, 0},
236 {Ctrl_HAT, nv_hat, NV_NCW, 0},
237 {Ctrl__, nv_error, 0, 0},
238 {' ', nv_right, 0, 0},
239 {'!', nv_operator, 0, 0},
240 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
241 {'#', nv_ident, 0, 0},
242 {'$', nv_dollar, 0, 0},
243 {'%', nv_percent, 0, 0},
244 {'&', nv_optrans, 0, 0},
245 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
246 {'(', nv_brace, 0, BACKWARD},
247 {')', nv_brace, 0, FORWARD},
248 {'*', nv_ident, 0, 0},
249 {'+', nv_down, 0, TRUE},
250 {',', nv_csearch, 0, TRUE},
251 {'-', nv_up, 0, TRUE},
252 {'.', nv_dot, NV_KEEPREG, 0},
253 {'/', nv_search, 0, FALSE},
254 {'0', nv_beginline, 0, 0},
255 {'1', nv_ignore, 0, 0},
256 {'2', nv_ignore, 0, 0},
257 {'3', nv_ignore, 0, 0},
258 {'4', nv_ignore, 0, 0},
259 {'5', nv_ignore, 0, 0},
260 {'6', nv_ignore, 0, 0},
261 {'7', nv_ignore, 0, 0},
262 {'8', nv_ignore, 0, 0},
263 {'9', nv_ignore, 0, 0},
264 {':', nv_colon, 0, 0},
265 {';', nv_csearch, 0, FALSE},
266 {'<', nv_operator, NV_RL, 0},
267 {'=', nv_operator, 0, 0},
268 {'>', nv_operator, NV_RL, 0},
269 {'?', nv_search, 0, FALSE},
270 {'@', nv_at, NV_NCH_NOP, FALSE},
271 {'A', nv_edit, 0, 0},
272 {'B', nv_bck_word, 0, 1},
273 {'C', nv_abbrev, NV_KEEPREG, 0},
274 {'D', nv_abbrev, NV_KEEPREG, 0},
275 {'E', nv_wordcmd, 0, TRUE},
276 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
277 {'G', nv_goto, 0, TRUE},
278 {'H', nv_scroll, 0, 0},
279 {'I', nv_edit, 0, 0},
280 {'J', nv_join, 0, 0},
281 {'K', nv_ident, 0, 0},
282 {'L', nv_scroll, 0, 0},
283 {'M', nv_scroll, 0, 0},
284 {'N', nv_next, 0, SEARCH_REV},
285 {'O', nv_open, 0, 0},
286 {'P', nv_put, 0, 0},
287 {'Q', nv_exmode, NV_NCW, 0},
288 {'R', nv_Replace, 0, FALSE},
289 {'S', nv_subst, NV_KEEPREG, 0},
290 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
291 {'U', nv_Undo, 0, 0},
292 {'W', nv_wordcmd, 0, TRUE},
293 {'X', nv_abbrev, NV_KEEPREG, 0},
294 {'Y', nv_abbrev, NV_KEEPREG, 0},
295 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
296 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
297 {'\\', nv_error, 0, 0},
298 {']', nv_brackets, NV_NCH_ALW, FORWARD},
299 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
300 {'_', nv_lineop, 0, 0},
301 {'`', nv_gomark, NV_NCH_ALW, FALSE},
302 {'a', nv_edit, NV_NCH, 0},
303 {'b', nv_bck_word, 0, 0},
304 {'c', nv_operator, 0, 0},
305 {'d', nv_operator, 0, 0},
306 {'e', nv_wordcmd, 0, FALSE},
307 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
308 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
309 {'h', nv_left, NV_RL, 0},
310 {'i', nv_edit, NV_NCH, 0},
311 {'j', nv_down, 0, FALSE},
312 {'k', nv_up, 0, FALSE},
313 {'l', nv_right, NV_RL, 0},
314 {'m', nv_mark, NV_NCH_NOP, 0},
315 {'n', nv_next, 0, 0},
316 {'o', nv_open, 0, 0},
317 {'p', nv_put, 0, 0},
318 {'q', nv_record, NV_NCH, 0},
319 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
320 {'s', nv_subst, NV_KEEPREG, 0},
321 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
322 {'u', nv_undo, 0, 0},
323 {'w', nv_wordcmd, 0, FALSE},
324 {'x', nv_abbrev, NV_KEEPREG, 0},
325 {'y', nv_operator, 0, 0},
326 {'z', nv_zet, NV_NCH_ALW, 0},
327 {'{', nv_findpar, 0, BACKWARD},
328 {'|', nv_pipe, 0, 0},
329 {'}', nv_findpar, 0, FORWARD},
330 {'~', nv_tilde, 0, 0},
331
332 /* pound sign */
333 {POUND, nv_ident, 0, 0},
334#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200335 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
336 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
337 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
338 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {K_LEFTMOUSE, nv_mouse, 0, 0},
340 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
341 {K_LEFTDRAG, nv_mouse, 0, 0},
342 {K_LEFTRELEASE, nv_mouse, 0, 0},
343 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100344 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
346 {K_MIDDLEDRAG, nv_mouse, 0, 0},
347 {K_MIDDLERELEASE, nv_mouse, 0, 0},
348 {K_RIGHTMOUSE, nv_mouse, 0, 0},
349 {K_RIGHTDRAG, nv_mouse, 0, 0},
350 {K_RIGHTRELEASE, nv_mouse, 0, 0},
351 {K_X1MOUSE, nv_mouse, 0, 0},
352 {K_X1DRAG, nv_mouse, 0, 0},
353 {K_X1RELEASE, nv_mouse, 0, 0},
354 {K_X2MOUSE, nv_mouse, 0, 0},
355 {K_X2DRAG, nv_mouse, 0, 0},
356 {K_X2RELEASE, nv_mouse, 0, 0},
357#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000358 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000359 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {K_INS, nv_edit, 0, 0},
361 {K_KINS, nv_edit, 0, 0},
362 {K_BS, nv_ctrlh, 0, 0},
363 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
364 {K_S_UP, nv_page, NV_SS, BACKWARD},
365 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
366 {K_S_DOWN, nv_page, NV_SS, FORWARD},
367 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
368 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
369 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
370 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
371 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
372 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
373 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
374 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
375 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
376 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
377 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
378 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {K_S_END, nv_end, NV_SS, FALSE},
380 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
381 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
382 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {K_S_HOME, nv_home, NV_SS, 0},
384 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
385 {K_DEL, nv_abbrev, 0, 0},
386 {K_KDEL, nv_abbrev, 0, 0},
387 {K_UNDO, nv_kundo, 0, 0},
388 {K_HELP, nv_help, NV_NCW, 0},
389 {K_F1, nv_help, NV_NCW, 0},
390 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#ifdef FEAT_GUI
393 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
394 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
395#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000396#ifdef FEAT_GUI_TABLINE
397 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000398 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#ifdef FEAT_NETBEANS_INTG
401 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
402#endif
403#ifdef FEAT_DND
404 {K_DROP, nv_drop, NV_STS, 0},
405#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000406 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100407 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408};
409
410/* Number of commands in nv_cmds[]. */
411#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
412
413/* Sorted index of commands in nv_cmds[]. */
414static short nv_cmd_idx[NV_CMDS_SIZE];
415
416/* The highest index for which
417 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
418static int nv_max_linear;
419
420/*
421 * Compare functions for qsort() below, that checks the command character
422 * through the index in nv_cmd_idx[].
423 */
424 static int
425#ifdef __BORLANDC__
426_RTLENTRYF
427#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100428nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430 int c1, c2;
431
432 /* The commands are sorted on absolute value. */
433 c1 = nv_cmds[*(const short *)s1].cmd_char;
434 c2 = nv_cmds[*(const short *)s2].cmd_char;
435 if (c1 < 0)
436 c1 = -c1;
437 if (c2 < 0)
438 c2 = -c2;
439 return c1 - c2;
440}
441
442/*
443 * Initialize the nv_cmd_idx[] table.
444 */
445 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100446init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447{
448 int i;
449
450 /* Fill the index table with a one to one relation. */
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 nv_cmd_idx[i] = i;
453
454 /* Sort the commands by the command character. */
455 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
456
457 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000458 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
460 break;
461 nv_max_linear = i - 1;
462}
463
464/*
465 * Search for a command in the commands table.
466 * Returns -1 for invalid command.
467 */
468 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100469find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int i;
472 int idx;
473 int top, bot;
474 int c;
475
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 /* A multi-byte character is never a command. */
477 if (cmdchar >= 0x100)
478 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
480 /* We use the absolute value of the character. Special keys have a
481 * negative value, but are sorted on their absolute value. */
482 if (cmdchar < 0)
483 cmdchar = -cmdchar;
484
485 /* If the character is in the first part: The character is the index into
486 * nv_cmd_idx[]. */
487 if (cmdchar <= nv_max_linear)
488 return nv_cmd_idx[cmdchar];
489
490 /* Perform a binary search. */
491 bot = nv_max_linear + 1;
492 top = NV_CMDS_SIZE - 1;
493 idx = -1;
494 while (bot <= top)
495 {
496 i = (top + bot) / 2;
497 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
498 if (c < 0)
499 c = -c;
500 if (cmdchar == c)
501 {
502 idx = nv_cmd_idx[i];
503 break;
504 }
505 if (cmdchar > c)
506 bot = i + 1;
507 else
508 top = i - 1;
509 }
510 return idx;
511}
512
513/*
514 * Execute a command in Normal mode.
515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100517normal_cmd(
518 oparg_T *oap,
519 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 cmdarg_T ca; /* command arguments */
522 int c;
523 int ctrl_w = FALSE; /* got CTRL-W command */
524 int old_col = curwin->w_curswant;
525#ifdef FEAT_CMDL_INFO
526 int need_flushbuf; /* need to call out_flush() */
527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 pos_T old_pos; /* cursor position before command */
529 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 static int old_mapped_len = 0;
531 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000532#ifdef FEAT_EVAL
533 int set_prevcount = FALSE;
534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
536 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
537 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000538
539 /* Use a count remembered from before entering an operator. After typing
540 * "3d" we return from normal_cmd() and come back here, the "3" is
541 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 ca.opcount = opcount;
543
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 /*
545 * If there is an operator pending, then the command we take this time
546 * will terminate it. Finish_op tells us to finish the operation before
547 * returning this time (unless the operation was cancelled).
548 */
549#ifdef CURSOR_SHAPE
550 c = finish_op;
551#endif
552 finish_op = (oap->op_type != OP_NOP);
553#ifdef CURSOR_SHAPE
554 if (finish_op != c)
555 {
556 ui_cursor_shape(); /* may show different cursor shape */
557# ifdef FEAT_MOUSESHAPE
558 update_mouseshape(-1);
559# endif
560 }
561#endif
562
Bram Moolenaara983fe92008-07-31 20:04:27 +0000563 /* When not finishing an operator and no register name typed, reset the
564 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000568#ifdef FEAT_EVAL
569 set_prevcount = TRUE;
570#endif
571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaara983fe92008-07-31 20:04:27 +0000573 /* Restore counts from before receiving K_CURSORHOLD. This means after
574 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
575 * "3 * 2". */
576 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
577 {
578 ca.opcount = oap->prev_opcount;
579 ca.count0 = oap->prev_count0;
580 oap->prev_opcount = 0;
581 oap->prev_count0 = 0;
582 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000583
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585
586 State = NORMAL_BUSY;
587#ifdef USE_ON_FLY_SCROLL
588 dont_scroll = FALSE; /* allow scrolling here */
589#endif
590
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100591#ifdef FEAT_EVAL
592 /* Set v:count here, when called from main() and not a stuffed
593 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100594 * when there is no count. Do set it for redo. */
595 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100596 set_vcount_ca(&ca, &set_prevcount);
597#endif
598
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 /*
600 * Get the command character from the user.
601 */
602 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100603 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
605 /*
606 * If a mapping was started in Visual or Select mode, remember the length
607 * of the mapping. This is used below to not return to Insert mode for as
608 * long as the mapping is being executed.
609 */
610 if (restart_edit == 0)
611 old_mapped_len = 0;
612 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000613 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 old_mapped_len = typebuf_maplen();
615
616 if (c == NUL)
617 c = K_ZERO;
618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 /*
620 * In Select mode, typed text replaces the selection.
621 */
622 if (VIsual_active
623 && VIsual_select
624 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
625 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000626 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
627 * 'insertmode' is set) fake a "d"elete command, Insert mode will
628 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000629 * Insert the typed character in the typeahead buffer, so that it can
630 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000631 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000632 if (restart_edit != 0)
633 c = 'd';
634 else
635 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000636 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200637 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
640#ifdef FEAT_CMDL_INFO
641 need_flushbuf = add_to_showcmd(c);
642#endif
643
644getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 {
647 /*
648 * Handle a count before a command and compute ca.count0.
649 * Note that '0' is a command and not the start of a count, but it's
650 * part of a count after other digits.
651 */
652 while ( (c >= '1' && c <= '9')
653 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
654 {
655 if (c == K_DEL || c == K_KDEL)
656 {
657 ca.count0 /= 10;
658#ifdef FEAT_CMDL_INFO
659 del_from_showcmd(4); /* delete the digit and ~@% */
660#endif
661 }
662 else
663 ca.count0 = ca.count0 * 10 + (c - '0');
664 if (ca.count0 < 0) /* got too large! */
665 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000666#ifdef FEAT_EVAL
667 /* Set v:count here, when called from main() and not a stuffed
668 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100669 * right after the count. Do set it for redo. */
670 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100671 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 if (ctrl_w)
674 {
675 ++no_mapping;
676 ++allow_keys; /* no mapping for nchar, but keys */
677 }
678 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000679 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 --no_zero_mapping;
682 if (ctrl_w)
683 {
684 --no_mapping;
685 --allow_keys;
686 }
687#ifdef FEAT_CMDL_INFO
688 need_flushbuf |= add_to_showcmd(c);
689#endif
690 }
691
692 /*
693 * If we got CTRL-W there may be a/another count
694 */
695 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
696 {
697 ctrl_w = TRUE;
698 ca.opcount = ca.count0; /* remember first count */
699 ca.count0 = 0;
700 ++no_mapping;
701 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000702 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 --no_mapping;
705 --allow_keys;
706#ifdef FEAT_CMDL_INFO
707 need_flushbuf |= add_to_showcmd(c);
708#endif
709 goto getcount; /* jump back */
710 }
711 }
712
Bram Moolenaara983fe92008-07-31 20:04:27 +0000713 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000715 /* Save the count values so that ca.opcount and ca.count0 are exactly
716 * the same when coming back here after handling K_CURSORHOLD. */
717 oap->prev_opcount = ca.opcount;
718 oap->prev_count0 = ca.count0;
719 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100720 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000721 {
722 /*
723 * If we're in the middle of an operator (including after entering a
724 * yank buffer with '"') AND we had a count before the operator, then
725 * that count overrides the current value of ca.count0.
726 * What this means effectively, is that commands like "3dw" get turned
727 * into "d3w" which makes things fall into place pretty neatly.
728 * If you give a count before AND after the operator, they are
729 * multiplied.
730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (ca.count0)
732 ca.count0 *= ca.opcount;
733 else
734 ca.count0 = ca.opcount;
735 }
736
737 /*
738 * Always remember the count. It will be set to zero (on the next call,
739 * above) when there is no pending operator.
740 * When called from main(), save the count for use by the "count" built-in
741 * variable.
742 */
743 ca.opcount = ca.count0;
744 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
745
746#ifdef FEAT_EVAL
747 /*
748 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100749 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100751 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000752 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#endif
754
755 /*
756 * Find the command character in the table of commands.
757 * For CTRL-W we already got nchar when looking for a count.
758 */
759 if (ctrl_w)
760 {
761 ca.nchar = c;
762 ca.cmdchar = Ctrl_W;
763 }
764 else
765 ca.cmdchar = c;
766 idx = find_command(ca.cmdchar);
767 if (idx < 0)
768 {
769 /* Not a known command: beep. */
770 clearopbeep(oap);
771 goto normal_end;
772 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000773
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000774 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200776 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000778 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 goto normal_end;
780 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000781 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
782 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 /*
785 * In Visual/Select mode, a few keys are handled in a special way.
786 */
787 if (VIsual_active)
788 {
789 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
790 if (km_stopsel
791 && (nv_cmds[idx].cmd_flags & NV_STS)
792 && !(mod_mask & MOD_MASK_SHIFT))
793 {
794 end_visual_mode();
795 redraw_curbuf_later(INVERTED);
796 }
797
798 /* Keys that work different when 'keymodel' contains "startsel" */
799 if (km_startsel)
800 {
801 if (nv_cmds[idx].cmd_flags & NV_SS)
802 {
803 unshift_special(&ca);
804 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000805 if (idx < 0)
806 {
807 /* Just in case */
808 clearopbeep(oap);
809 goto normal_end;
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
813 && (mod_mask & MOD_MASK_SHIFT))
814 {
815 mod_mask &= ~MOD_MASK_SHIFT;
816 }
817 }
818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
820#ifdef FEAT_RIGHTLEFT
821 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
822 && (nv_cmds[idx].cmd_flags & NV_RL))
823 {
824 /* Invert horizontal movements and operations. Only when typed by the
825 * user directly, not when the result of a mapping or "x" translated
826 * to "dl". */
827 switch (ca.cmdchar)
828 {
829 case 'l': ca.cmdchar = 'h'; break;
830 case K_RIGHT: ca.cmdchar = K_LEFT; break;
831 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
832 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
833 case 'h': ca.cmdchar = 'l'; break;
834 case K_LEFT: ca.cmdchar = K_RIGHT; break;
835 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
836 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
837 case '>': ca.cmdchar = '<'; break;
838 case '<': ca.cmdchar = '>'; break;
839 }
840 idx = find_command(ca.cmdchar);
841 }
842#endif
843
844 /*
845 * Get an additional character if we need one.
846 */
847 if ((nv_cmds[idx].cmd_flags & NV_NCH)
848 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
849 && oap->op_type == OP_NOP)
850 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
851 || (ca.cmdchar == 'q'
852 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200853 && reg_recording == 0
854 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100856 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
858 int *cp;
859 int repl = FALSE; /* get character for replace mode */
860 int lit = FALSE; /* get extra character literally */
861 int langmap_active = FALSE; /* using :lmap mappings */
862 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100863#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 int save_smd; /* saved value of p_smd */
865#endif
866
867 ++no_mapping;
868 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000869 /* Don't generate a CursorHold event here, most commands can't handle
870 * it, e.g., nv_replace(), nv_csearch(). */
871 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 if (ca.cmdchar == 'g')
873 {
874 /*
875 * For 'g' get the next character now, so that we can check for
876 * "gr", "g'" and "g`".
877 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000878 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880#ifdef FEAT_CMDL_INFO
881 need_flushbuf |= add_to_showcmd(ca.nchar);
882#endif
883 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100884 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
886 cp = &ca.extra_char; /* need to get a third character */
887 if (ca.nchar != 'r')
888 lit = TRUE; /* get it literally */
889 else
890 repl = TRUE; /* get it in replace mode */
891 }
892 else
893 cp = NULL; /* no third character needed */
894 }
895 else
896 {
897 if (ca.cmdchar == 'r') /* get it in replace mode */
898 repl = TRUE;
899 cp = &ca.nchar;
900 }
901 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
902
903 /*
904 * Get a second or third character.
905 */
906 if (cp != NULL)
907 {
908#ifdef CURSOR_SHAPE
909 if (repl)
910 {
911 State = REPLACE; /* pretend Replace mode */
912 ui_cursor_shape(); /* show different cursor shape */
913 }
914#endif
915 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
916 {
917 /* Allow mappings defined with ":lmap". */
918 --no_mapping;
919 --allow_keys;
920 if (repl)
921 State = LREPLACE;
922 else
923 State = LANGMAP;
924 langmap_active = TRUE;
925 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100926#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 save_smd = p_smd;
928 p_smd = FALSE; /* Don't let the IM code show the mode here */
929 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
930 im_set_active(TRUE);
931#endif
932
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000933 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
935 if (langmap_active)
936 {
937 /* Undo the decrement done above */
938 ++no_mapping;
939 ++allow_keys;
940 State = NORMAL_BUSY;
941 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100942#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (lang)
944 {
945 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
946 im_save_status(&curbuf->b_p_iminsert);
947 im_set_active(FALSE);
948 }
949 p_smd = save_smd;
950#endif
951#ifdef CURSOR_SHAPE
952 State = NORMAL_BUSY;
953#endif
954#ifdef FEAT_CMDL_INFO
955 need_flushbuf |= add_to_showcmd(*cp);
956#endif
957
958 if (!lit)
959 {
960#ifdef FEAT_DIGRAPHS
961 /* Typing CTRL-K gets a digraph. */
962 if (*cp == Ctrl_K
963 && ((nv_cmds[idx].cmd_flags & NV_LANG)
964 || cp == &ca.extra_char)
965 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
966 {
967 c = get_digraph(FALSE);
968 if (c > 0)
969 {
970 *cp = c;
971# ifdef FEAT_CMDL_INFO
972 /* Guessing how to update showcmd here... */
973 del_from_showcmd(3);
974 need_flushbuf |= add_to_showcmd(*cp);
975# endif
976 }
977 }
978#endif
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 /* adjust chars > 127, except after "tTfFr" commands */
981 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982#ifdef FEAT_RIGHTLEFT
983 /* adjust Hebrew mapped char */
984 if (p_hkmap && lang && KeyTyped)
985 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#endif
987 }
988
989 /*
990 * When the next character is CTRL-\ a following CTRL-N means the
991 * command is aborted and we go to Normal mode.
992 */
993 if (cp == &ca.extra_char
994 && ca.nchar == Ctrl_BSL
995 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
996 {
997 ca.cmdchar = Ctrl_BSL;
998 ca.nchar = ca.extra_char;
999 idx = find_command(ca.cmdchar);
1000 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001001 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001002 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 else if (*cp == Ctrl_BSL)
1004 {
1005 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1006
1007 /* There is a busy wait here when typing "f<C-\>" and then
1008 * something different from CTRL-N. Can't be avoided. */
1009 while ((c = vpeekc()) <= 0 && towait > 0L)
1010 {
1011 do_sleep(towait > 50L ? 50L : towait);
1012 towait -= 50L;
1013 }
1014 if (c > 0)
1015 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001016 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (c != Ctrl_N && c != Ctrl_G)
1018 vungetc(c);
1019 else
1020 {
1021 ca.cmdchar = Ctrl_BSL;
1022 ca.nchar = c;
1023 idx = find_command(ca.cmdchar);
1024 }
1025 }
1026 }
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 /* When getting a text character and the next character is a
1029 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001030 * However, don't wait for it to arrive. Also, do enable mapping,
1031 * because if it's put back with vungetc() it's too late to apply
1032 * mapping. */
1033 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 while (enc_utf8 && lang && (c = vpeekc()) > 0
1035 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1036 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001037 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (!utf_iscomposing(c))
1039 {
1040 vungetc(c); /* it wasn't, put it back */
1041 break;
1042 }
1043 else if (ca.ncharC1 == 0)
1044 ca.ncharC1 = c;
1045 else
1046 ca.ncharC2 = c;
1047 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001048 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050 --no_mapping;
1051 --allow_keys;
1052 }
1053
1054#ifdef FEAT_CMDL_INFO
1055 /*
1056 * Flush the showcmd characters onto the screen so we can see them while
1057 * the command is being executed. Only do this when the shown command was
1058 * actually displayed, otherwise this will slow down a lot when executing
1059 * mappings.
1060 */
1061 if (need_flushbuf)
1062 out_flush();
1063#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001064 if (ca.cmdchar != K_IGNORE)
1065 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067 State = NORMAL;
1068
1069 if (ca.nchar == ESC)
1070 {
1071 clearop(oap);
1072 if (restart_edit == 0 && goto_im())
1073 restart_edit = 'a';
1074 goto normal_end;
1075 }
1076
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001077 if (ca.cmdchar != K_IGNORE)
1078 {
1079 msg_didout = FALSE; /* don't scroll screen up for normal command */
1080 msg_col = 0;
1081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 old_pos = curwin->w_cursor; /* remember where cursor was */
1084
1085 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1086 * mode. */
1087 if (!VIsual_active && km_startsel)
1088 {
1089 if (nv_cmds[idx].cmd_flags & NV_SS)
1090 {
1091 start_selection();
1092 unshift_special(&ca);
1093 idx = find_command(ca.cmdchar);
1094 }
1095 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1096 && (mod_mask & MOD_MASK_SHIFT))
1097 {
1098 start_selection();
1099 mod_mask &= ~MOD_MASK_SHIFT;
1100 }
1101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
1103 /*
1104 * Execute the command!
1105 * Call the command function found in the commands table.
1106 */
1107 ca.arg = nv_cmds[idx].cmd_arg;
1108 (nv_cmds[idx].cmd_func)(&ca);
1109
1110 /*
1111 * If we didn't start or finish an operator, reset oap->regname, unless we
1112 * need it later.
1113 */
1114 if (!finish_op
1115 && !oap->op_type
1116 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1117 {
1118 clearop(oap);
1119#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001120 {
1121 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001122
Bram Moolenaar536681b2011-05-10 16:12:45 +02001123 /* Adjust the register according to 'clipboard', so that when
1124 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001125# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001126 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001127# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001128 set_reg_var(regname);
1129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130#endif
1131 }
1132
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001133 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001134 * character or "z333<cr>". */
1135 if (old_mapped_len > 0)
1136 old_mapped_len = typebuf_maplen();
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 /*
1139 * If an operation is pending, handle it...
1140 */
1141 do_pending_operator(&ca, old_col, FALSE);
1142
1143 /*
1144 * Wait for a moment when a message is displayed that will be overwritten
1145 * by the mode message.
1146 * In Visual mode and with "^O" in Insert mode, a short message will be
1147 * overwritten by the mode message. Wait a bit, until a key is hit.
1148 * In Visual mode, it's more important to keep the Visual area updated
1149 * than keeping a message (e.g. from a /pat search).
1150 * Only do this if the command was typed, not from a mapping.
1151 * Don't wait when emsg_silent is non-zero.
1152 * Also wait a bit after an error message, e.g. for "^O:".
1153 * Don't redraw the screen, it would remove the message.
1154 */
1155 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001156 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 || (VIsual_active
1159 && old_pos.lnum == curwin->w_cursor.lnum
1160 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 )
1162 && (clear_cmdline
1163 || redraw_cmdline)
1164 && (msg_didout || (msg_didany && msg_scroll))
1165 && !msg_nowait
1166 && KeyTyped)
1167 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 && (msg_scroll
1170 || emsg_on_display)))
1171 && oap->regname == 0
1172 && !(ca.retval & CA_COMMAND_BUSY)
1173 && stuff_empty()
1174 && typebuf_typed()
1175 && emsg_silent == 0
1176 && !did_wait_return
1177 && oap->op_type == OP_NOP)
1178 {
1179 int save_State = State;
1180
1181 /* Draw the cursor with the right shape here */
1182 if (restart_edit != 0)
1183 State = INSERT;
1184
1185 /* If need to redraw, and there is a "keep_msg", redraw before the
1186 * delay */
1187 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1188 {
1189 char_u *kmsg;
1190
1191 kmsg = keep_msg;
1192 keep_msg = NULL;
1193 /* showmode() will clear keep_msg, but we want to use it anyway */
1194 update_screen(0);
1195 /* now reset it, otherwise it's put in the history again */
1196 keep_msg = kmsg;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001197 msg_attr((char *)kmsg, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 vim_free(kmsg);
1199 }
1200 setcursor();
1201 cursor_on();
1202 out_flush();
1203 if (msg_scroll || emsg_on_display)
1204 ui_delay(1000L, TRUE); /* wait at least one second */
1205 ui_delay(3000L, FALSE); /* wait up to three seconds */
1206 State = save_State;
1207
1208 msg_scroll = FALSE;
1209 emsg_on_display = FALSE;
1210 }
1211
1212 /*
1213 * Finish up after executing a Normal mode command.
1214 */
1215normal_end:
1216
1217 msg_nowait = FALSE;
1218
1219 /* Reset finish_op, in case it was set */
1220#ifdef CURSOR_SHAPE
1221 c = finish_op;
1222#endif
1223 finish_op = FALSE;
1224#ifdef CURSOR_SHAPE
1225 /* Redraw the cursor with another shape, if we were in Operator-pending
1226 * mode or did a replace command. */
1227 if (c || ca.cmdchar == 'r')
1228 {
1229 ui_cursor_shape(); /* may show different cursor shape */
1230# ifdef FEAT_MOUSESHAPE
1231 update_mouseshape(-1);
1232# endif
1233 }
1234#endif
1235
1236#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001237 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001238 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 clear_showcmd();
1240#endif
1241
1242 checkpcmark(); /* check if we moved since setting pcmark */
1243 vim_free(ca.searchbuf);
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (has_mbyte)
1246 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 if (curwin->w_p_scb && toplevel)
1249 {
1250 validate_cursor(); /* may need to update w_leftcol */
1251 do_check_scrollbind(TRUE);
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Bram Moolenaar860cae12010-06-05 23:22:07 +02001254 if (curwin->w_p_crb && toplevel)
1255 {
1256 validate_cursor(); /* may need to update w_leftcol */
1257 do_check_cursorbind();
1258 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001259
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001260#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001261 /* don't go to Insert mode if a terminal has a running job */
1262 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001263 restart_edit = 0;
1264#endif
1265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 /*
1267 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1268 * if still inside a mapping that started in Visual mode).
1269 * May switch from Visual to Select mode after CTRL-O command.
1270 */
1271 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1273 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 && !(ca.retval & CA_COMMAND_BUSY)
1275 && stuff_empty()
1276 && oap->regname == 0)
1277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (restart_VIsual_select == 1)
1279 {
1280 VIsual_select = TRUE;
1281 showmode();
1282 restart_VIsual_select = 0;
1283 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001284 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 (void)edit(restart_edit, FALSE, 1L);
1286 }
1287
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 if (restart_VIsual_select == 2)
1289 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291 /* Save count before an operator for next time. */
1292 opcount = ca.opcount;
1293}
1294
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001295#ifdef FEAT_EVAL
1296/*
1297 * Set v:count and v:count1 according to "cap".
1298 * Set v:prevcount only when "set_prevcount" is TRUE.
1299 */
1300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001301set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001302{
1303 long count = cap->count0;
1304
1305 /* multiply with cap->opcount the same way as above */
1306 if (cap->opcount != 0)
1307 count = cap->opcount * (count == 0 ? 1 : count);
1308 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1309 *set_prevcount = FALSE; /* only set v:prevcount once */
1310}
1311#endif
1312
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313/*
Bram Moolenaar5823f842019-01-03 22:58:08 +01001314 * Handle an operator after Visual mode or when the movement is finished.
1315 * "gui_yank" is true when yanking text for the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 */
1317 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001318do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 oparg_T *oap = cap->oap;
1321 pos_T old_cursor;
1322 int empty_region_error;
1323 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001324#ifdef FEAT_LINEBREAK
1325 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 /* The visual area is remembered for redo */
1329 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1330 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001331 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001333 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336#if defined(FEAT_CLIPBOARD)
1337 /*
1338 * Yank the visual area into the GUI selection register before we operate
1339 * on it and lose it forever.
1340 * Don't do it if a specific register was specified, so that ""x"*P works.
1341 * This could call do_pending_operator() recursively, but that's OK
1342 * because gui_yank will be TRUE for the nested call.
1343 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001344 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 && oap->op_type != OP_NOP
1346 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 && VIsual_active
1348 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 && oap->regname == 0)
1350 clip_auto_select();
1351#endif
1352 old_cursor = curwin->w_cursor;
1353
1354 /*
1355 * If an operation is pending, handle it...
1356 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001357 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
Bram Moolenaar5823f842019-01-03 22:58:08 +01001359 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
1360 // for the clipboard.
1361 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
1362
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001363#ifdef FEAT_LINEBREAK
1364 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001365 if (curwin->w_p_lbr)
1366 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001367 curwin->w_p_lbr = FALSE;
1368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 oap->is_VIsual = VIsual_active;
1370 if (oap->motion_force == 'V')
1371 oap->motion_type = MLINE;
1372 else if (oap->motion_force == 'v')
1373 {
1374 /* If the motion was linewise, "inclusive" will not have been set.
1375 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1376 if (oap->motion_type == MLINE)
1377 oap->inclusive = FALSE;
1378 /* If the motion already was characterwise, toggle "inclusive" */
1379 else if (oap->motion_type == MCHAR)
1380 oap->inclusive = !oap->inclusive;
1381 oap->motion_type = MCHAR;
1382 }
1383 else if (oap->motion_force == Ctrl_V)
1384 {
1385 /* Change line- or characterwise motion into Visual block mode. */
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01001386 if (!VIsual_active)
1387 {
1388 VIsual_active = TRUE;
1389 VIsual = oap->start;
1390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 VIsual_mode = Ctrl_V;
1392 VIsual_select = FALSE;
1393 VIsual_reselect = FALSE;
1394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
Bram Moolenaar7afea822013-04-24 18:34:45 +02001396 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1397 /* Never redo "zf" (define fold). */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001398 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001399 && ((!VIsual_active || oap->motion_force)
1400 /* Also redo Operator-pending Visual mode mappings */
1401 || (VIsual_active && cap->cmdchar == ':'
1402 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001403 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_FOLDING
1405 && oap->op_type != OP_FOLD
1406 && oap->op_type != OP_FOLDOPEN
1407 && oap->op_type != OP_FOLDOPENREC
1408 && oap->op_type != OP_FOLDCLOSE
1409 && oap->op_type != OP_FOLDCLOSEREC
1410 && oap->op_type != OP_FOLDDEL
1411 && oap->op_type != OP_FOLDDELREC
1412#endif
1413 )
1414 {
1415 prep_redo(oap->regname, cap->count0,
1416 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1417 oap->motion_force, cap->cmdchar, cap->nchar);
1418 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1419 {
1420 /*
1421 * If 'cpoptions' does not contain 'r', insert the search
1422 * pattern to really repeat the same command.
1423 */
1424 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001425 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 AppendToRedobuff(NL_STR);
1427 }
1428 else if (cap->cmdchar == ':')
1429 {
1430 /* do_cmdline() has stored the first typed line in
1431 * "repeat_cmdline". When several lines are typed repeating
1432 * won't be possible. */
1433 if (repeat_cmdline == NULL)
1434 ResetRedobuff();
1435 else
1436 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001437 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001439 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 }
1441 }
1442 }
1443
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (redo_VIsual_busy)
1445 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001446 /* Redo of an operation on a Visual area. Use the same size from
1447 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 oap->start = curwin->w_cursor;
1449 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1450 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1451 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1452 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001453 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001455 if (VIsual_mode == 'v')
1456 {
1457 if (redo_VIsual_line_count <= 1)
1458 {
1459 validate_virtcol();
1460 curwin->w_curswant =
1461 curwin->w_virtcol + redo_VIsual_vcol - 1;
1462 }
1463 else
1464 curwin->w_curswant = redo_VIsual_vcol;
1465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001467 {
1468 curwin->w_curswant = MAXCOL;
1469 }
1470 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
1472 cap->count0 = redo_VIsual_count;
1473 if (redo_VIsual_count != 0)
1474 cap->count1 = redo_VIsual_count;
1475 else
1476 cap->count1 = 1;
1477 }
1478 else if (VIsual_active)
1479 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001480 if (!gui_yank)
1481 {
1482 /* Save the current VIsual area for '< and '> marks, and "gv" */
1483 curbuf->b_visual.vi_start = VIsual;
1484 curbuf->b_visual.vi_end = curwin->w_cursor;
1485 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001486 if (VIsual_mode_orig != NUL)
1487 {
1488 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1489 VIsual_mode_orig = NUL;
1490 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001491 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001493 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001498 * characterwise selection.
1499 * Special case: gH<Del> deletes the last line. */
1500 if (VIsual_select && VIsual_mode == 'V'
1501 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001503 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 {
1505 VIsual.col = 0;
1506 curwin->w_cursor.col =
1507 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1508 }
1509 else
1510 {
1511 curwin->w_cursor.col = 0;
1512 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1513 }
1514 VIsual_mode = 'v';
1515 }
1516 /* If 'selection' is "exclusive", backup one character for
1517 * charwise selections. */
1518 else if (VIsual_mode == 'v')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001519 include_line_break = unadjust_for_sel();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
1521 oap->start = VIsual;
1522 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001525 oap->start.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
1529 /*
1530 * Set oap->start to the first position of the operated text, oap->end
1531 * to the end of the operated text. w_cursor is equal to oap->start.
1532 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001533 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 {
1535#ifdef FEAT_FOLDING
1536 /* Include folded lines completely. */
1537 if (!VIsual_active)
1538 {
1539 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1540 oap->start.col = 0;
1541 if (hasFolding(curwin->w_cursor.lnum, NULL,
1542 &curwin->w_cursor.lnum))
1543 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1544 }
1545#endif
1546 oap->end = curwin->w_cursor;
1547 curwin->w_cursor = oap->start;
1548
1549 /* w_virtcol may have been updated; if the cursor goes back to its
1550 * previous position w_virtcol becomes invalid and isn't updated
1551 * automatically. */
1552 curwin->w_valid &= ~VALID_VIRTCOL;
1553 }
1554 else
1555 {
1556#ifdef FEAT_FOLDING
1557 /* Include folded lines completely. */
1558 if (!VIsual_active && oap->motion_type == MLINE)
1559 {
1560 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1561 NULL))
1562 curwin->w_cursor.col = 0;
1563 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1564 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1565 }
1566#endif
1567 oap->end = oap->start;
1568 oap->start = curwin->w_cursor;
1569 }
1570
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001571 /* Just in case lines were deleted that make the position invalid. */
1572 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1574
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 /* Set "virtual_op" before resetting VIsual_active. */
1576 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (VIsual_active || redo_VIsual_busy)
1579 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001580 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 if (!redo_VIsual_busy && !gui_yank)
1583 {
1584 /*
1585 * Prepare to reselect and redo Visual: this is based on the
1586 * size of the Visual text
1587 */
1588 resel_VIsual_mode = VIsual_mode;
1589 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001590 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001592 {
1593 if (VIsual_mode != Ctrl_V)
1594 getvvcol(curwin, &(oap->end),
1595 NULL, NULL, &oap->end_vcol);
1596 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1597 {
1598 if (VIsual_mode != Ctrl_V)
1599 getvvcol(curwin, &(oap->start),
1600 &oap->start_vcol, NULL, NULL);
1601 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1602 }
1603 else
1604 resel_VIsual_vcol = oap->end_vcol;
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 resel_VIsual_line_count = oap->line_count;
1607 }
1608
1609 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001610 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 && oap->op_type != OP_COLON
1612#ifdef FEAT_FOLDING
1613 && oap->op_type != OP_FOLD
1614 && oap->op_type != OP_FOLDOPEN
1615 && oap->op_type != OP_FOLDOPENREC
1616 && oap->op_type != OP_FOLDCLOSE
1617 && oap->op_type != OP_FOLDCLOSEREC
1618 && oap->op_type != OP_FOLDDEL
1619 && oap->op_type != OP_FOLDDELREC
1620#endif
1621 && oap->motion_force == NUL
1622 )
1623 {
1624 /* Prepare for redoing. Only use the nchar field for "r",
1625 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001626 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1627 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001628 prep_redo(oap->regname, cap->count0,
1629 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1630 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001631 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001632 {
1633 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1634
1635 /* reverse what nv_replace() did */
1636 if (nchar == REPLACE_CR_NCHAR)
1637 nchar = CAR;
1638 else if (nchar == REPLACE_NL_NCHAR)
1639 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001640 prep_redo(oap->regname, 0L, NUL, 'v',
1641 get_op_char(oap->op_type),
1642 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001643 nchar);
1644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (!redo_VIsual_busy)
1646 {
1647 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001648 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 redo_VIsual_line_count = resel_VIsual_line_count;
1650 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001651 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 }
1653 }
1654
1655 /*
1656 * oap->inclusive defaults to TRUE.
1657 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1658 * FALSE. This makes "d}P" and "v}dP" work the same.
1659 */
1660 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1661 oap->inclusive = TRUE;
1662 if (VIsual_mode == 'V')
1663 oap->motion_type = MLINE;
1664 else
1665 {
1666 oap->motion_type = MCHAR;
1667 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001668 && (include_line_break || !virtual_op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 {
1670 oap->inclusive = FALSE;
1671 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001672 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001673 if (*p_sel != 'o'
1674 && !op_on_lines(oap->op_type)
1675 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001677 ++oap->end.lnum;
1678 oap->end.col = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001679 oap->end.coladd = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001680 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
1682 }
1683 }
1684
1685 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001686
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 /*
1688 * Switch Visual off now, so screen updating does
1689 * not show inverted text when the screen is redrawn.
1690 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1691 * no screen redraw, so it is done here to remove the inverted
1692 * part.
1693 */
1694 if (!gui_yank)
1695 {
1696 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001697#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 setmouse();
1699 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001700#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001701 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 if ((oap->op_type == OP_YANK
1703 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001704 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 || oap->op_type == OP_FILTER)
1706 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001707 {
1708#ifdef FEAT_LINEBREAK
1709 /* make sure redrawing is correct */
1710 curwin->w_p_lbr = lbr_saved;
1711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 /* Include the trailing byte of a multi-byte char. */
1718 if (has_mbyte && oap->inclusive)
1719 {
1720 int l;
1721
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001722 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 if (l > 1)
1724 oap->end.col += l - 1;
1725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 curwin->w_set_curswant = TRUE;
1727
1728 /*
1729 * oap->empty is set when start and end are the same. The inclusive
1730 * flag affects this too, unless yanking and the end is on a NUL.
1731 */
1732 oap->empty = (oap->motion_type == MCHAR
1733 && (!oap->inclusive
1734 || (oap->op_type == OP_YANK
1735 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001736 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001737 && !(virtual_op && oap->start.coladd != oap->end.coladd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 /*
1739 * For delete, change and yank, it's an error to operate on an
1740 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1741 */
1742 empty_region_error = (oap->empty
1743 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 /* Force a redraw when operating on an empty Visual region, when
1746 * 'modifiable is off or creating a fold. */
1747 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001748#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001752 {
1753#ifdef FEAT_LINEBREAK
1754 curwin->w_p_lbr = lbr_saved;
1755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
1759 /*
1760 * If the end of an operator is in column one while oap->motion_type
1761 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1762 * character in the previous line. If op_start is on or before the
1763 * first non-blank in the line, the operator becomes linewise
1764 * (strange, but that's the way vi does it).
1765 */
1766 if ( oap->motion_type == MCHAR
1767 && oap->inclusive == FALSE
1768 && !(cap->retval & CA_NO_ADJ_OP_END)
1769 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001771 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 && oap->line_count > 1)
1773 {
1774 oap->end_adjusted = TRUE; /* remember that we did this */
1775 --oap->line_count;
1776 --oap->end.lnum;
1777 if (inindent(0))
1778 oap->motion_type = MLINE;
1779 else
1780 {
1781 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1782 if (oap->end.col)
1783 {
1784 --oap->end.col;
1785 oap->inclusive = TRUE;
1786 }
1787 }
1788 }
1789 else
1790 oap->end_adjusted = FALSE;
1791
1792 switch (oap->op_type)
1793 {
1794 case OP_LSHIFT:
1795 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001796 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 auto_format(FALSE, TRUE);
1798 break;
1799
1800 case OP_JOIN_NS:
1801 case OP_JOIN:
1802 if (oap->line_count < 2)
1803 oap->line_count = 2;
1804 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1805 curbuf->b_ml.ml_line_count)
1806 beep_flush();
1807 else
1808 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001809 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001810 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 auto_format(FALSE, TRUE);
1812 }
1813 break;
1814
1815 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001818 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001819 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001820 CancelRedo();
1821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 else
1823 {
1824 (void)op_delete(oap);
1825 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1826 u_save_cursor(); /* cursor line wasn't saved yet */
1827 auto_format(FALSE, TRUE);
1828 }
1829 break;
1830
1831 case OP_YANK:
1832 if (empty_region_error)
1833 {
1834 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001835 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001836 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001837 CancelRedo();
1838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 }
1840 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001841 {
1842#ifdef FEAT_LINEBREAK
1843 curwin->w_p_lbr = lbr_saved;
1844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 check_cursor_col();
1848 break;
1849
1850 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001853 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001854 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001855 CancelRedo();
1856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 else
1858 {
1859 /* This is a new edit command, not a restart. Need to
1860 * remember it to make 'insertmode' work with mappings for
1861 * Visual mode. But do this only once and not when typed and
1862 * 'insertmode' isn't set. */
1863 if (p_im || !KeyTyped)
1864 restart_edit_save = restart_edit;
1865 else
1866 restart_edit_save = 0;
1867 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001868#ifdef FEAT_LINEBREAK
1869 /* Restore linebreak, so that when the user edits it looks as
1870 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001871 if (curwin->w_p_lbr != lbr_saved)
1872 {
1873 curwin->w_p_lbr = lbr_saved;
1874 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1875 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 /* Reset finish_op now, don't want it set inside edit(). */
1878 finish_op = FALSE;
1879 if (op_change(oap)) /* will call edit() */
1880 cap->retval |= CA_COMMAND_BUSY;
1881 if (restart_edit == 0)
1882 restart_edit = restart_edit_save;
1883 }
1884 break;
1885
1886 case OP_FILTER:
1887 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1888 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1889 else
1890 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001891 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
1893 case OP_INDENT:
1894 case OP_COLON:
1895
1896#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1897 /*
1898 * If 'equalprg' is empty, do the indenting internally.
1899 */
1900 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1901 {
1902# ifdef FEAT_LISP
1903 if (curbuf->b_p_lisp)
1904 {
1905 op_reindent(oap, get_lisp_indent);
1906 break;
1907 }
1908# endif
1909# ifdef FEAT_CINDENT
1910 op_reindent(oap,
1911# ifdef FEAT_EVAL
1912 *curbuf->b_p_inde != NUL ? get_expr_indent :
1913# endif
1914 get_c_indent);
1915 break;
1916# endif
1917 }
1918#endif
1919
1920 op_colon(oap);
1921 break;
1922
1923 case OP_TILDE:
1924 case OP_UPPER:
1925 case OP_LOWER:
1926 case OP_ROT13:
1927 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001928 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001929 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001930 CancelRedo();
1931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 else
1933 op_tilde(oap);
1934 check_cursor_col();
1935 break;
1936
1937 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001938#if defined(FEAT_EVAL)
1939 if (*curbuf->b_p_fex != NUL)
1940 op_formatexpr(oap); /* use expression */
1941 else
1942#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001943 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 op_colon(oap); /* use external command */
1945 else
1946 op_format(oap, FALSE); /* use internal function */
1947 break;
1948
1949 case OP_FORMAT2:
1950 op_format(oap, TRUE); /* use internal function */
1951 break;
1952
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001953 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001954#ifdef FEAT_LINEBREAK
1955 /* Restore linebreak, so that when the user edits it looks as
1956 * before. */
1957 curwin->w_p_lbr = lbr_saved;
1958#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001959 op_function(oap); /* call 'operatorfunc' */
1960 break;
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 case OP_INSERT:
1963 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001966 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001967 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001968 CancelRedo();
1969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 else
1971 {
1972 /* This is a new edit command, not a restart. Need to
1973 * remember it to make 'insertmode' work with mappings for
1974 * Visual mode. But do this only once. */
1975 restart_edit_save = restart_edit;
1976 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001977#ifdef FEAT_LINEBREAK
1978 /* Restore linebreak, so that when the user edits it looks as
1979 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001980 if (curwin->w_p_lbr != lbr_saved)
1981 {
1982 curwin->w_p_lbr = lbr_saved;
1983 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1984 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001987#ifdef FEAT_LINEBREAK
1988 /* Reset linebreak, so that formatting works correctly. */
1989 curwin->w_p_lbr = FALSE;
1990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
1992 /* TODO: when inserting in several lines, should format all
1993 * the lines. */
1994 auto_format(FALSE, TRUE);
1995
1996 if (restart_edit == 0)
1997 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01001998 else
1999 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 break;
2002
2003 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002006 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002007 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002008 CancelRedo();
2009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002011 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002012#ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002013 /* Restore linebreak, so that when the user edits it looks as
2014 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002015 if (curwin->w_p_lbr != lbr_saved)
2016 {
2017 curwin->w_p_lbr = lbr_saved;
2018 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2019 }
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 break;
2024
2025#ifdef FEAT_FOLDING
2026 case OP_FOLD:
2027 VIsual_reselect = FALSE; /* don't reselect now */
2028 foldCreate(oap->start.lnum, oap->end.lnum);
2029 break;
2030
2031 case OP_FOLDOPEN:
2032 case OP_FOLDOPENREC:
2033 case OP_FOLDCLOSE:
2034 case OP_FOLDCLOSEREC:
2035 VIsual_reselect = FALSE; /* don't reselect now */
2036 opFoldRange(oap->start.lnum, oap->end.lnum,
2037 oap->op_type == OP_FOLDOPEN
2038 || oap->op_type == OP_FOLDOPENREC,
2039 oap->op_type == OP_FOLDOPENREC
2040 || oap->op_type == OP_FOLDCLOSEREC,
2041 oap->is_VIsual);
2042 break;
2043
2044 case OP_FOLDDEL:
2045 case OP_FOLDDELREC:
2046 VIsual_reselect = FALSE; /* don't reselect now */
2047 deleteFold(oap->start.lnum, oap->end.lnum,
2048 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2049 break;
2050#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002051 case OP_NR_ADD:
2052 case OP_NR_SUB:
2053 if (empty_region_error)
2054 {
2055 vim_beep(BO_OPER);
2056 CancelRedo();
2057 }
2058 else
2059 {
2060 VIsual_active = TRUE;
2061#ifdef FEAT_LINEBREAK
2062 curwin->w_p_lbr = lbr_saved;
2063#endif
2064 op_addsub(oap, cap->count1, redo_VIsual_arg);
2065 VIsual_active = FALSE;
2066 }
2067 check_cursor_col();
2068 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 default:
2070 clearopbeep(oap);
2071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 virtual_op = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 if (!gui_yank)
2074 {
2075 /*
2076 * if 'sol' not set, go back to old column for some commands
2077 */
2078 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2079 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2080 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002081 {
2082#ifdef FEAT_LINEBREAK
2083 curwin->w_p_lbr = FALSE;
2084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088 else
2089 {
2090 curwin->w_cursor = old_cursor;
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 clearop(oap);
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01002094 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002096#ifdef FEAT_LINEBREAK
2097 curwin->w_p_lbr = lbr_saved;
2098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099}
2100
2101/*
2102 * Handle indent and format operators and visual mode ":".
2103 */
2104 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002105op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106{
2107 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (oap->is_VIsual)
2109 stuffReadbuff((char_u *)"'<,'>");
2110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
2112 /*
2113 * Make the range look nice, so it can be repeated.
2114 */
2115 if (oap->start.lnum == curwin->w_cursor.lnum)
2116 stuffcharReadbuff('.');
2117 else
2118 stuffnumReadbuff((long)oap->start.lnum);
2119 if (oap->end.lnum != oap->start.lnum)
2120 {
2121 stuffcharReadbuff(',');
2122 if (oap->end.lnum == curwin->w_cursor.lnum)
2123 stuffcharReadbuff('.');
2124 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2125 stuffcharReadbuff('$');
2126 else if (oap->start.lnum == curwin->w_cursor.lnum)
2127 {
2128 stuffReadbuff((char_u *)".+");
2129 stuffnumReadbuff((long)oap->line_count - 1);
2130 }
2131 else
2132 stuffnumReadbuff((long)oap->end.lnum);
2133 }
2134 }
2135 if (oap->op_type != OP_COLON)
2136 stuffReadbuff((char_u *)"!");
2137 if (oap->op_type == OP_INDENT)
2138 {
2139#ifndef FEAT_CINDENT
2140 if (*get_equalprg() == NUL)
2141 stuffReadbuff((char_u *)"indent");
2142 else
2143#endif
2144 stuffReadbuff(get_equalprg());
2145 stuffReadbuff((char_u *)"\n");
2146 }
2147 else if (oap->op_type == OP_FORMAT)
2148 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002149 if (*curbuf->b_p_fp != NUL)
2150 stuffReadbuff(curbuf->b_p_fp);
2151 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002153 else
2154 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002155 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
2157
2158 /*
2159 * do_cmdline() does the rest
2160 */
2161}
2162
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002163/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002164 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002165 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002167op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002168{
2169#ifdef FEAT_EVAL
Bram Moolenaarffa96842018-06-12 22:05:14 +02002170 typval_T argv[2];
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002171 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002172
2173 if (*p_opfunc == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002174 emsg(_("E774: 'operatorfunc' is empty"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002175 else
2176 {
2177 /* Set '[ and '] marks to text to be operated on. */
2178 curbuf->b_op_start = oap->start;
2179 curbuf->b_op_end = oap->end;
2180 if (oap->motion_type != MLINE && !oap->inclusive)
2181 /* Exclude the end position. */
2182 decl(&curbuf->b_op_end);
2183
Bram Moolenaarffa96842018-06-12 22:05:14 +02002184 argv[0].v_type = VAR_STRING;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002185 if (oap->block_mode)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002186 argv[0].vval.v_string = (char_u *)"block";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002187 else if (oap->motion_type == MLINE)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002188 argv[0].vval.v_string = (char_u *)"line";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002189 else
Bram Moolenaarffa96842018-06-12 22:05:14 +02002190 argv[0].vval.v_string = (char_u *)"char";
2191 argv[1].v_type = VAR_UNKNOWN;
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002192
2193 /* Reset virtual_op so that 'virtualedit' can be changed in the
2194 * function. */
2195 virtual_op = MAYBE;
2196
Bram Moolenaarded27a12018-08-01 19:06:03 +02002197 (void)call_func_retnr(p_opfunc, 1, argv);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002198
2199 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002200 }
2201#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002202 emsg(_("E775: Eval feature not available"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002203#endif
2204}
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206#if defined(FEAT_MOUSE) || defined(PROTO)
2207/*
2208 * Do the appropriate action for the current mouse click in the current mode.
2209 * Not used for Command-line mode.
2210 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002211 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 * event modi- position visual change action
2213 * fier cursor window
2214 * left press - yes end yes
2215 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002216 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 * left drag - yes start if moved no
2218 * left relse - yes start if moved no
2219 * middle press - yes if not active no put register
2220 * middle press - yes if active no yank and put
2221 * right press - yes start or extend yes
2222 * right press S yes no change yes "#" (2)
2223 * right drag - yes extend no
2224 * right relse - yes extend no
2225 *
2226 * Insert or Replace Mode:
2227 * event modi- position visual change action
2228 * fier cursor window
2229 * left press - yes (cannot be active) yes
2230 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2231 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2232 * left drag - yes start or extend (1) no CTRL-O (1)
2233 * left relse - yes start or extend (1) no CTRL-O (1)
2234 * middle press - no (cannot be active) no put register
2235 * right press - yes start or extend yes CTRL-O
2236 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2237 *
2238 * (1) only if mouse pointer moved since press
2239 * (2) only if click is in same buffer
2240 *
2241 * Return TRUE if start_arrow() should be called for edit mode.
2242 */
2243 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002244do_mouse(
2245 oparg_T *oap, /* operator argument, can be NULL */
2246 int c, /* K_LEFTMOUSE, etc */
2247 int dir, /* Direction to 'put' if necessary */
2248 long count,
2249 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
2251 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2252 static int got_click = FALSE; /* got a click some time back */
2253
2254 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2255 int is_click; /* If FALSE it's a drag or release event */
2256 int is_drag; /* If TRUE it's a drag event */
2257 int jump_flags = 0; /* flags for jump_to_mouse() */
2258 pos_T start_visual;
2259 int moved; /* Has cursor moved? */
2260 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002261 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 int c1, c2;
2264#if defined(FEAT_FOLDING)
2265 pos_T save_cursor;
2266#endif
2267 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 static pos_T orig_cursor;
2269 colnr_T leftcol, rightcol;
2270 pos_T end_visual;
2271 int diff;
2272 int old_active = VIsual_active;
2273 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 int regname;
2275
2276#if defined(FEAT_FOLDING)
2277 save_cursor = curwin->w_cursor;
2278#endif
2279
2280 /*
2281 * When GUI is active, always recognize mouse events, otherwise:
2282 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2283 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2284 * - For command line and insert mode 'mouse' is checked before calling
2285 * do_mouse().
2286 */
2287 if (do_always)
2288 do_always = FALSE;
2289 else
2290#ifdef FEAT_GUI
2291 if (!gui.in_use)
2292#endif
2293 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if (VIsual_active)
2295 {
2296 if (!mouse_has(MOUSE_VISUAL))
2297 return FALSE;
2298 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002299 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 return FALSE;
2301 }
2302
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002303 for (;;)
2304 {
2305 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2306 if (is_drag)
2307 {
2308 /* If the next character is the same mouse event then use that
2309 * one. Speeds up dragging the status line. */
2310 if (vpeekc() != NUL)
2311 {
2312 int nc;
2313 int save_mouse_row = mouse_row;
2314 int save_mouse_col = mouse_col;
2315
2316 /* Need to get the character, peeking doesn't get the actual
2317 * one. */
2318 nc = safe_vgetc();
2319 if (c == nc)
2320 continue;
2321 vungetc(nc);
2322 mouse_row = save_mouse_row;
2323 mouse_col = save_mouse_col;
2324 }
2325 }
2326 break;
2327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002329 if (c == K_MOUSEMOVE)
2330 {
2331 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002332#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002333 ui_may_remove_balloon();
2334 if (p_bevalterm && !VIsual_active)
2335 {
2336 profile_setlimit(p_bdlay, &bevalexpr_due);
2337 bevalexpr_due_set = TRUE;
2338 }
2339#endif
2340 return FALSE;
2341 }
2342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343#ifdef FEAT_MOUSESHAPE
2344 /* May have stopped dragging the status or separator line. The pointer is
2345 * most likely still on the status or separator line. */
2346 if (!is_drag && drag_status_line)
2347 {
2348 drag_status_line = FALSE;
2349 update_mouseshape(SHAPE_IDX_STATUS);
2350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 if (!is_drag && drag_sep_line)
2352 {
2353 drag_sep_line = FALSE;
2354 update_mouseshape(SHAPE_IDX_VSEP);
2355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#endif
2357
2358 /*
2359 * Ignore drag and release events if we didn't get a click.
2360 */
2361 if (is_click)
2362 got_click = TRUE;
2363 else
2364 {
2365 if (!got_click) /* didn't get click, ignore */
2366 return FALSE;
2367 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002368 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002370 if (in_tab_line)
2371 {
2372 in_tab_line = FALSE;
2373 return FALSE;
2374 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 /*
2379 * CTRL right mouse button does CTRL-T
2380 */
2381 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2382 {
2383 if (State & INSERT)
2384 stuffcharReadbuff(Ctrl_O);
2385 if (count > 1)
2386 stuffnumReadbuff(count);
2387 stuffcharReadbuff(Ctrl_T);
2388 got_click = FALSE; /* ignore drag&release now */
2389 return FALSE;
2390 }
2391
2392 /*
2393 * CTRL only works with left mouse button
2394 */
2395 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2396 return FALSE;
2397
2398 /*
2399 * When a modifier is down, ignore drag and release events, as well as
2400 * multiple clicks and the middle mouse button.
2401 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2402 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002403 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2404 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 && (!is_click
2406 || (mod_mask & MOD_MASK_MULTI_CLICK)
2407 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002408 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 && mouse_model_popup()
2410 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002411 && !((mod_mask & MOD_MASK_ALT)
2412 && !mouse_model_popup()
2413 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 )
2415 return FALSE;
2416
2417 /*
2418 * If the button press was used as the movement command for an operator
2419 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2420 * drag/release events.
2421 */
2422 if (!is_click && which_button == MOUSE_MIDDLE)
2423 return FALSE;
2424
2425 if (oap != NULL)
2426 regname = oap->regname;
2427 else
2428 regname = 0;
2429
2430 /*
2431 * Middle mouse button does a 'put' of the selected text
2432 */
2433 if (which_button == MOUSE_MIDDLE)
2434 {
2435 if (State == NORMAL)
2436 {
2437 /*
2438 * If an operator was pending, we don't know what the user wanted
2439 * to do. Go back to normal mode: Clear the operator and beep().
2440 */
2441 if (oap != NULL && oap->op_type != OP_NOP)
2442 {
2443 clearopbeep(oap);
2444 return FALSE;
2445 }
2446
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 /*
2448 * If visual was active, yank the highlighted text and put it
2449 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002450 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
2452 if (VIsual_active)
2453 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002454 if (VIsual_select)
2455 {
2456 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002457 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002458 }
2459 else
2460 {
2461 stuffcharReadbuff('y');
2462 stuffcharReadbuff(K_MIDDLEMOUSE);
2463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 do_always = TRUE; /* ignore 'mouse' setting next time */
2465 return FALSE;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 /*
2468 * The rest is below jump_to_mouse()
2469 */
2470 }
2471
2472 else if ((State & INSERT) == 0)
2473 return FALSE;
2474
2475 /*
2476 * Middle click in insert mode doesn't move the mouse, just insert the
2477 * contents of a register. '.' register is special, can't insert that
2478 * with do_put().
2479 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2480 * happens for the GUI).
2481 */
2482 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2483 {
2484 if (regname == '.')
2485 insert_reg(regname, TRUE);
2486 else
2487 {
2488#ifdef FEAT_CLIPBOARD
2489 if (clip_star.available && regname == 0)
2490 regname = '*';
2491#endif
2492 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2493 insert_reg(regname, TRUE);
2494 else
2495 {
2496 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2497
2498 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2499 AppendCharToRedobuff(Ctrl_R);
2500 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2501 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2502 }
2503 }
2504 return FALSE;
2505 }
2506 }
2507
2508 /* When dragging or button-up stay in the same window. */
2509 if (!is_click)
2510 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2511
2512 start_visual.lnum = 0;
2513
Bram Moolenaarf740b292006-02-16 22:11:02 +00002514 /* Check for clicking in the tab page line. */
2515 if (mouse_row == 0 && firstwin->w_winrow > 0)
2516 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002517 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002518 {
2519 if (in_tab_line)
2520 {
2521 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002522 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2523 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002524 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002525 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002526 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002527
Bram Moolenaarf740b292006-02-16 22:11:02 +00002528 /* click in a tab selects that tab page */
2529 if (is_click
2530# ifdef FEAT_CMDWIN
2531 && cmdwin_type == 0
2532# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002533 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002534 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002535 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002536 c1 = TabPageIdxs[mouse_col];
2537 if (c1 >= 0)
2538 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002539 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2540 {
2541 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002542 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002543 tabpage_new();
2544 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2545 }
2546 else
2547 {
2548 /* Go to specified tab page, or next one if not clicking
2549 * on a label. */
2550 goto_tabpage(c1);
2551
2552 /* It's like clicking on the status line of a window. */
2553 if (curwin != old_curwin)
2554 end_visual_mode();
2555 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002556 }
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002557 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002558 {
2559 tabpage_T *tp;
2560
2561 /* Close the current or specified tab page. */
2562 if (c1 == -999)
2563 tp = curtab;
2564 else
2565 tp = find_tabpage(-c1);
2566 if (tp == curtab)
2567 {
2568 if (first_tabpage->tp_next != NULL)
2569 tabpage_close(FALSE);
2570 }
2571 else if (tp != NULL)
2572 tabpage_close_other(tp, FALSE);
2573 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002574 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002575 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002576 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002577 else if (is_drag && in_tab_line)
2578 {
2579 c1 = TabPageIdxs[mouse_col];
2580 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2581 return FALSE;
2582 }
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 /*
2585 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2586 * right button up -> pop-up menu
2587 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002588 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
2590 if (mouse_model_popup())
2591 {
2592 if (which_button == MOUSE_RIGHT
2593 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2594 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002595#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002597 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2598 || defined(FEAT_TERM_POPUP_MENU)
2599# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (gui.in_use)
2601 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002602# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2603 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2604 if (!is_click)
2605 /* Ignore right button release events, only shows the popup
2606 * menu on the button down event. */
2607 return FALSE;
2608# endif
2609# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2610 if (is_click || is_drag)
2611 /* Ignore right button down and drag mouse events. Windows
2612 * only shows the popup menu on the button up event. */
2613 return FALSE;
2614# endif
2615 }
2616# endif
2617# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2618 else
2619# endif
2620# if defined(FEAT_TERM_POPUP_MENU)
2621 if (!is_click)
2622 /* Ignore right button release events, only shows the popup
2623 * menu on the button down event. */
2624 return FALSE;
2625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002627 jump_flags = 0;
2628 if (STRCMP(p_mousem, "popup_setpos") == 0)
2629 {
2630 /* First set the cursor position before showing the popup
2631 * menu. */
2632 if (VIsual_active)
2633 {
2634 pos_T m_pos;
2635
2636 /*
2637 * set MOUSE_MAY_STOP_VIS if we are outside the
2638 * selection or the current window (might have false
2639 * negative here)
2640 */
2641 if (mouse_row < curwin->w_winrow
2642 || mouse_row
2643 > (curwin->w_winrow + curwin->w_height))
2644 jump_flags = MOUSE_MAY_STOP_VIS;
2645 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2646 jump_flags = MOUSE_MAY_STOP_VIS;
2647 else
2648 {
2649 if ((LT_POS(curwin->w_cursor, VIsual)
2650 && (LT_POS(m_pos, curwin->w_cursor)
2651 || LT_POS(VIsual, m_pos)))
2652 || (LT_POS(VIsual, curwin->w_cursor)
2653 && (LT_POS(m_pos, VIsual)
2654 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002656 jump_flags = MOUSE_MAY_STOP_VIS;
2657 }
2658 else if (VIsual_mode == Ctrl_V)
2659 {
2660 getvcols(curwin, &curwin->w_cursor, &VIsual,
2661 &leftcol, &rightcol);
2662 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2663 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
2666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002668 else
2669 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002671 if (jump_flags)
2672 {
2673 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2674 update_curbuf(VIsual_active ? INVERTED : VALID);
2675 setcursor();
2676 out_flush(); /* Update before showing popup menu */
2677 }
2678# ifdef FEAT_MENU
2679 show_popupmenu();
2680 got_click = FALSE; /* ignore release events */
2681# endif
2682 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683#else
2684 return FALSE;
2685#endif
2686 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002687 if (which_button == MOUSE_LEFT
2688 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
2690 which_button = MOUSE_RIGHT;
2691 mod_mask &= ~MOD_MASK_SHIFT;
2692 }
2693 }
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 if ((State & (NORMAL | INSERT))
2696 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2697 {
2698 if (which_button == MOUSE_LEFT)
2699 {
2700 if (is_click)
2701 {
2702 /* stop Visual mode for a left click in a window, but not when
2703 * on a status line */
2704 if (VIsual_active)
2705 jump_flags |= MOUSE_MAY_STOP_VIS;
2706 }
2707 else if (mouse_has(MOUSE_VISUAL))
2708 jump_flags |= MOUSE_MAY_VIS;
2709 }
2710 else if (which_button == MOUSE_RIGHT)
2711 {
2712 if (is_click && VIsual_active)
2713 {
2714 /*
2715 * Remember the start and end of visual before moving the
2716 * cursor.
2717 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002718 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
2720 start_visual = curwin->w_cursor;
2721 end_visual = VIsual;
2722 }
2723 else
2724 {
2725 start_visual = VIsual;
2726 end_visual = curwin->w_cursor;
2727 }
2728 }
2729 jump_flags |= MOUSE_FOCUS;
2730 if (mouse_has(MOUSE_VISUAL))
2731 jump_flags |= MOUSE_MAY_VIS;
2732 }
2733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
2735 /*
2736 * If an operator is pending, ignore all drags and releases until the
2737 * next mouse click.
2738 */
2739 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2740 {
2741 got_click = FALSE;
2742 oap->motion_type = MCHAR;
2743 }
2744
2745 /* When releasing the button let jump_to_mouse() know. */
2746 if (!is_click && !is_drag)
2747 jump_flags |= MOUSE_RELEASED;
2748
2749 /*
2750 * JUMP!
2751 */
2752 jump_flags = jump_to_mouse(jump_flags,
2753 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002754
2755#ifdef FEAT_MENU
2756 /* A click in the window toolbar has no side effects. */
2757 if (jump_flags & MOUSE_WINBAR)
2758 return FALSE;
2759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 moved = (jump_flags & CURSOR_MOVED);
2761 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
2764#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002765 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2767 {
2768 int key = KEY2TERMCAP1(c);
2769
2770 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2771 || key == (int)KE_RIGHTRELEASE)
2772 netbeans_button_release(which_button);
2773 }
2774#endif
2775
2776 /* When jumping to another window, clear a pending operator. That's a bit
2777 * friendlier than beeping and not jumping to that window. */
2778 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2779 clearop(oap);
2780
2781#ifdef FEAT_FOLDING
2782 if (mod_mask == 0
2783 && !is_drag
2784 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2785 && which_button == MOUSE_LEFT)
2786 {
2787 /* open or close a fold at this line */
2788 if (jump_flags & MOUSE_FOLD_OPEN)
2789 openFold(curwin->w_cursor.lnum, 1L);
2790 else
2791 closeFold(curwin->w_cursor.lnum, 1L);
2792 /* don't move the cursor if still in the same window */
2793 if (curwin == old_curwin)
2794 curwin->w_cursor = save_cursor;
2795 }
2796#endif
2797
2798#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2799 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2800 {
2801 clip_modeless(which_button, is_click, is_drag);
2802 return FALSE;
2803 }
2804#endif
2805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002807 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002808 if (VIsual_active && is_drag && get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
2810 /* In the very first line, allow scrolling one line */
2811 if (mouse_row == 0)
2812 mouse_dragging = 2;
2813 else
2814 mouse_dragging = 1;
2815 }
2816
2817 /* When dragging the mouse above the window, scroll down. */
2818 if (is_drag && mouse_row < 0 && !in_status_line)
2819 {
2820 scroll_redraw(FALSE, 1L);
2821 mouse_row = 0;
2822 }
2823
2824 if (start_visual.lnum) /* right click in visual mode */
2825 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002826 /* When ALT is pressed make Visual mode blockwise. */
2827 if (mod_mask & MOD_MASK_ALT)
2828 VIsual_mode = Ctrl_V;
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 /*
2831 * In Visual-block mode, divide the area in four, pick up the corner
2832 * that is in the quarter that the cursor is in.
2833 */
2834 if (VIsual_mode == Ctrl_V)
2835 {
2836 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2837 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2838 end_visual.col = leftcol;
2839 else
2840 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002841 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 end_visual.lnum = start_visual.lnum;
2844
2845 /* move VIsual to the right column */
2846 start_visual = curwin->w_cursor; /* save the cursor pos */
2847 curwin->w_cursor = end_visual;
2848 coladvance(end_visual.col);
2849 VIsual = curwin->w_cursor;
2850 curwin->w_cursor = start_visual; /* restore the cursor */
2851 }
2852 else
2853 {
2854 /*
2855 * If the click is before the start of visual, change the start.
2856 * If the click is after the end of visual, change the end. If
2857 * the click is inside the visual, change the closest side.
2858 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002859 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002861 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 VIsual = start_visual;
2863 else
2864 {
2865 /* In the same line, compare column number */
2866 if (end_visual.lnum == start_visual.lnum)
2867 {
2868 if (curwin->w_cursor.col - start_visual.col >
2869 end_visual.col - curwin->w_cursor.col)
2870 VIsual = start_visual;
2871 else
2872 VIsual = end_visual;
2873 }
2874
2875 /* In different lines, compare line number */
2876 else
2877 {
2878 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2879 (end_visual.lnum - curwin->w_cursor.lnum);
2880
2881 if (diff > 0) /* closest to end */
2882 VIsual = start_visual;
2883 else if (diff < 0) /* closest to start */
2884 VIsual = end_visual;
2885 else /* in the middle line */
2886 {
2887 if (curwin->w_cursor.col <
2888 (start_visual.col + end_visual.col) / 2)
2889 VIsual = end_visual;
2890 else
2891 VIsual = start_visual;
2892 }
2893 }
2894 }
2895 }
2896 }
2897 /*
2898 * If Visual mode started in insert mode, execute "CTRL-O"
2899 */
2900 else if ((State & INSERT) && VIsual_active)
2901 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
2903 /*
2904 * Middle mouse click: Put text before cursor.
2905 */
2906 if (which_button == MOUSE_MIDDLE)
2907 {
2908#ifdef FEAT_CLIPBOARD
2909 if (clip_star.available && regname == 0)
2910 regname = '*';
2911#endif
2912 if (yank_register_mline(regname))
2913 {
2914 if (mouse_past_bottom)
2915 dir = FORWARD;
2916 }
2917 else if (mouse_past_eol)
2918 dir = FORWARD;
2919
2920 if (fixindent)
2921 {
2922 c1 = (dir == BACKWARD) ? '[' : ']';
2923 c2 = 'p';
2924 }
2925 else
2926 {
2927 c1 = (dir == FORWARD) ? 'p' : 'P';
2928 c2 = NUL;
2929 }
2930 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2931
2932 /*
2933 * Remember where the paste started, so in edit() Insstart can be set
2934 * to this position
2935 */
2936 if (restart_edit != 0)
2937 where_paste_started = curwin->w_cursor;
2938 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2939 }
2940
Bram Moolenaar4033c552017-09-16 20:54:51 +02002941#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 /*
2943 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2944 * error under the mouse pointer.
2945 */
2946 else if (((mod_mask & MOD_MASK_CTRL)
2947 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2948 && bt_quickfix(curbuf))
2949 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002950 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002951 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002952 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002953 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 got_click = FALSE; /* ignore drag&release now */
2955 }
2956#endif
2957
2958 /*
2959 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2960 * under the mouse pointer.
2961 */
2962 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2963 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2964 {
2965 if (State & INSERT)
2966 stuffcharReadbuff(Ctrl_O);
2967 stuffcharReadbuff(Ctrl_RSB);
2968 got_click = FALSE; /* ignore drag&release now */
2969 }
2970
2971 /*
2972 * Shift-Mouse click searches for the next occurrence of the word under
2973 * the mouse pointer
2974 */
2975 else if ((mod_mask & MOD_MASK_SHIFT))
2976 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002977 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 stuffcharReadbuff(Ctrl_O);
2979 if (which_button == MOUSE_LEFT)
2980 stuffcharReadbuff('*');
2981 else /* MOUSE_RIGHT */
2982 stuffcharReadbuff('#');
2983 }
2984
2985 /* Handle double clicks, unless on status line */
2986 else if (in_status_line)
2987 {
2988#ifdef FEAT_MOUSESHAPE
2989 if ((is_drag || is_click) && !drag_status_line)
2990 {
2991 drag_status_line = TRUE;
2992 update_mouseshape(-1);
2993 }
2994#endif
2995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 else if (in_sep_line)
2997 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02002998#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 if ((is_drag || is_click) && !drag_sep_line)
3000 {
3001 drag_sep_line = TRUE;
3002 update_mouseshape(-1);
3003 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003004#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3007 && mouse_has(MOUSE_VISUAL))
3008 {
3009 if (is_click || !VIsual_active)
3010 {
3011 if (VIsual_active)
3012 orig_cursor = VIsual;
3013 else
3014 {
3015 check_visual_highlight();
3016 VIsual = curwin->w_cursor;
3017 orig_cursor = VIsual;
3018 VIsual_active = TRUE;
3019 VIsual_reselect = TRUE;
3020 /* start Select mode if 'selectmode' contains "mouse" */
3021 may_start_select('o');
3022 setmouse();
3023 }
3024 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003025 {
3026 /* Double click with ALT pressed makes it blockwise. */
3027 if (mod_mask & MOD_MASK_ALT)
3028 VIsual_mode = Ctrl_V;
3029 else
3030 VIsual_mode = 'v';
3031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3033 VIsual_mode = 'V';
3034 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3035 VIsual_mode = Ctrl_V;
3036#ifdef FEAT_CLIPBOARD
3037 /* Make sure the clipboard gets updated. Needed because start and
3038 * end may still be the same, and the selection needs to be owned */
3039 clip_star.vmode = NUL;
3040#endif
3041 }
3042 /*
3043 * A double click selects a word or a block.
3044 */
3045 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3046 {
3047 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003048 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
3050 if (is_click)
3051 {
3052 /* If the character under the cursor (skipping white space) is
3053 * not a word character, try finding a match and select a (),
3054 * {}, [], #if/#endif, etc. block. */
3055 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003056 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 inc(&end_visual);
3058 if (oap != NULL)
3059 oap->motion_type = MCHAR;
3060 if (oap != NULL
3061 && VIsual_mode == 'v'
3062 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003063 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 && (pos = findmatch(oap, NUL)) != NULL)
3065 {
3066 curwin->w_cursor = *pos;
3067 if (oap->motion_type == MLINE)
3068 VIsual_mode = 'V';
3069 else if (*p_sel == 'e')
3070 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003071 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 ++VIsual.col;
3073 else
3074 ++curwin->w_cursor.col;
3075 }
3076 }
3077 }
3078
3079 if (pos == NULL && (is_click || is_drag))
3080 {
3081 /* When not found a match or when dragging: extend to include
3082 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003083 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 {
3085 find_start_of_word(&curwin->w_cursor);
3086 find_end_of_word(&VIsual);
3087 }
3088 else
3089 {
3090 find_start_of_word(&VIsual);
3091 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003093 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 find_end_of_word(&curwin->w_cursor);
3095 }
3096 }
3097 curwin->w_set_curswant = TRUE;
3098 }
3099 if (is_click)
3100 redraw_curbuf_later(INVERTED); /* update the inversion */
3101 }
3102 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003103 {
3104 if (mod_mask & MOD_MASK_ALT)
3105 VIsual_mode = Ctrl_V;
3106 else
3107 VIsual_mode = 'v';
3108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003111 if ((!VIsual_active && old_active && mode_displayed)
3112 || (VIsual_active && p_smd && msg_silent == 0
3113 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 return moved;
3117}
3118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119/*
3120 * Move "pos" back to the start of the word it's in.
3121 */
3122 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003123find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 char_u *line;
3126 int cclass;
3127 int col;
3128
3129 line = ml_get(pos->lnum);
3130 cclass = get_mouse_class(line + pos->col);
3131
3132 while (pos->col > 0)
3133 {
3134 col = pos->col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 col -= (*mb_head_off)(line, line + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 if (get_mouse_class(line + col) != cclass)
3137 break;
3138 pos->col = col;
3139 }
3140}
3141
3142/*
3143 * Move "pos" forward to the end of the word it's in.
3144 * When 'selection' is "exclusive", the position is just after the word.
3145 */
3146 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003147find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148{
3149 char_u *line;
3150 int cclass;
3151 int col;
3152
3153 line = ml_get(pos->lnum);
3154 if (*p_sel == 'e' && pos->col > 0)
3155 {
3156 --pos->col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 pos->col -= (*mb_head_off)(line, line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
3159 cclass = get_mouse_class(line + pos->col);
3160 while (line[pos->col] != NUL)
3161 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003162 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 if (get_mouse_class(line + col) != cclass)
3164 {
3165 if (*p_sel == 'e')
3166 pos->col = col;
3167 break;
3168 }
3169 pos->col = col;
3170 }
3171}
3172
3173/*
3174 * Get class of a character for selection: same class means same word.
3175 * 0: blank
3176 * 1: punctuation groups
3177 * 2: normal word character
3178 * >2: multi-byte word character.
3179 */
3180 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003181get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 int c;
3184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3186 return mb_get_class(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 c = *p;
3189 if (c == ' ' || c == '\t')
3190 return 0;
3191
3192 if (vim_iswordc(c))
3193 return 2;
3194
3195 /*
3196 * There are a few special cases where we want certain combinations of
3197 * characters to be considered as a single word. These are things like
3198 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003199 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 */
3201 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3202 return 1;
3203 return c;
3204}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205#endif /* FEAT_MOUSE */
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207/*
3208 * Check if highlighting for visual mode is possible, give a warning message
3209 * if not.
3210 */
3211 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003212check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
3214 static int did_check = FALSE;
3215
3216 if (full_screen)
3217 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003218 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003219 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 did_check = TRUE;
3221 }
3222}
3223
3224/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003225 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 * This function should ALWAYS be called to end Visual mode, except from
3227 * do_pending_operator().
3228 */
3229 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003230end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231{
3232#ifdef FEAT_CLIPBOARD
3233 /*
3234 * If we are using the clipboard, then remember what was selected in case
3235 * we need to paste it somewhere while we still own the selection.
3236 * Only do this when the clipboard is already owned. Don't want to grab
3237 * the selection when hitting ESC.
3238 */
3239 if (clip_star.available && clip_star.owned)
3240 clip_auto_select();
3241#endif
3242
3243 VIsual_active = FALSE;
3244#ifdef FEAT_MOUSE
3245 setmouse();
3246 mouse_dragging = 0;
3247#endif
3248
3249 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003250 curbuf->b_visual.vi_mode = VIsual_mode;
3251 curbuf->b_visual.vi_start = VIsual;
3252 curbuf->b_visual.vi_end = curwin->w_cursor;
3253 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254#ifdef FEAT_EVAL
3255 curbuf->b_visual_mode_eval = VIsual_mode;
3256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (!virtual_active())
3258 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003259 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003261 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262}
3263
3264/*
3265 * Reset VIsual_active and VIsual_reselect.
3266 */
3267 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003268reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 if (VIsual_active)
3271 {
3272 end_visual_mode();
3273 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3274 }
3275 VIsual_reselect = FALSE;
3276}
3277
3278/*
3279 * Reset VIsual_active and VIsual_reselect if it's set.
3280 */
3281 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003282reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 if (VIsual_active)
3285 {
3286 end_visual_mode();
3287 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3288 VIsual_reselect = FALSE;
3289 }
3290}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292/*
3293 * Check for a balloon-eval special item to include when searching for an
3294 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3295 * Returns TRUE if the character at "*ptr" should be included.
3296 * "dir" is FORWARD or BACKWARD, the direction of searching.
3297 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3298 * "bnp" points to a counter for square brackets.
3299 */
3300 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003301find_is_eval_item(
3302 char_u *ptr,
3303 int *colp,
3304 int *bnp,
3305 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 /* Accept everything inside []. */
3308 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3309 ++*bnp;
3310 if (*bnp > 0)
3311 {
3312 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3313 --*bnp;
3314 return TRUE;
3315 }
3316
3317 /* skip over "s.var" */
3318 if (*ptr == '.')
3319 return TRUE;
3320
3321 /* two-character item: s->var */
3322 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3323 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3324 {
3325 *colp += dir;
3326 return TRUE;
3327 }
3328 return FALSE;
3329}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330
3331/*
3332 * Find the identifier under or to the right of the cursor.
3333 * "find_type" can have one of three values:
3334 * FIND_IDENT: find an identifier (keyword)
3335 * FIND_STRING: find any non-white string
3336 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003337 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 *
3339 * There are three steps:
3340 * 1. Search forward for the start of an identifier/string. Doesn't move if
3341 * already on one.
3342 * 2. Search backward for the start of this identifier/string.
3343 * This doesn't match the real Vi but I like it a little better and it
3344 * shouldn't bother anyone.
3345 * 3. Search forward to the end of this identifier/string.
3346 * When FIND_IDENT isn't defined, we backup until a blank.
3347 *
3348 * Returns the length of the string, or zero if no string is found.
3349 * If a string is found, a pointer to the string is put in "*string". This
3350 * string is not always NUL terminated.
3351 */
3352 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003353find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3356 curwin->w_cursor.col, string, find_type);
3357}
3358
3359/*
3360 * Like find_ident_under_cursor(), but for any window and any position.
3361 * However: Uses 'iskeyword' from the current window!.
3362 */
3363 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003364find_ident_at_pos(
3365 win_T *wp,
3366 linenr_T lnum,
3367 colnr_T startcol,
3368 char_u **string,
3369 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
3371 char_u *ptr;
3372 int col = 0; /* init to shut up GCC */
3373 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 int this_class = 0;
3375 int prev_class;
3376 int prevcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
3379 /*
3380 * if i == 0: try to find an identifier
3381 * if i == 1: try to find any non-white string
3382 */
3383 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3384 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3385 {
3386 /*
3387 * 1. skip to start of identifier/string
3388 */
3389 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 if (has_mbyte)
3391 {
3392 while (ptr[col] != NUL)
3393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 /* Stop at a ']' to evaluate "a[x]". */
3395 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3396 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 this_class = mb_get_class(ptr + col);
3398 if (this_class != 0 && (i == 1 || this_class != 1))
3399 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003400 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402 }
3403 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003405 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 )
3408 ++col;
3409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 /* When starting on a ']' count it, so that we include the '['. */
3411 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412
3413 /*
3414 * 2. Back up to start of identifier/string.
3415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (has_mbyte)
3417 {
3418 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3420 this_class = mb_get_class((char_u *)"a");
3421 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003423 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3426 prev_class = mb_get_class(ptr + prevcol);
3427 if (this_class != prev_class
3428 && (i == 0
3429 || prev_class == 0
3430 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 && (!(find_type & FIND_EVAL)
3432 || prevcol == 0
3433 || !find_is_eval_item(ptr + prevcol, &prevcol,
3434 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 )
3436 break;
3437 col = prevcol;
3438 }
3439
3440 /* If we don't want just any old string, or we've found an
3441 * identifier, stop searching. */
3442 if (this_class > 2)
3443 this_class = 2;
3444 if (!(find_type & FIND_STRING) || this_class == 2)
3445 break;
3446 }
3447 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
3449 while (col > 0
3450 && ((i == 0
3451 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003452 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 && (!(find_type & FIND_IDENT)
3454 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 || ((find_type & FIND_EVAL)
3456 && col > 1
3457 && find_is_eval_item(ptr + col - 1, &col,
3458 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 ))
3460 --col;
3461
3462 /* If we don't want just any old string, or we've found an
3463 * identifier, stop searching. */
3464 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3465 break;
3466 }
3467 }
3468
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003469 if (ptr[col] == NUL || (i == 0
3470 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
3472 /*
3473 * didn't find an identifier or string
3474 */
3475 if (find_type & FIND_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003476 emsg(_("E348: No string under cursor"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003478 emsg(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 return 0;
3480 }
3481 ptr += col;
3482 *string = ptr;
3483
3484 /*
3485 * 3. Find the end if the identifier/string.
3486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 bn = 0;
3488 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (has_mbyte)
3491 {
3492 /* Search for point of changing multibyte character class. */
3493 this_class = mb_get_class(ptr);
3494 while (ptr[col] != NUL
3495 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3496 : mb_get_class(ptr + col) != 0)
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 Moolenaar0fa313a2005-08-10 21:07:57 +00003501 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
3503 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003505 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 || ((find_type & FIND_EVAL)
3507 && col <= (int)startcol
3508 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 )
3510 {
3511 ++col;
3512 }
3513
3514 return col;
3515}
3516
3517/*
3518 * Prepare for redo of a normal command.
3519 */
3520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003521prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
3523 prep_redo(cap->oap->regname, cap->count0,
3524 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3525}
3526
3527/*
3528 * Prepare for redo of any command.
3529 * Note that only the last argument can be a multi-byte char.
3530 */
3531 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003532prep_redo(
3533 int regname,
3534 long num,
3535 int cmd1,
3536 int cmd2,
3537 int cmd3,
3538 int cmd4,
3539 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540{
3541 ResetRedobuff();
3542 if (regname != 0) /* yank from specified buffer */
3543 {
3544 AppendCharToRedobuff('"');
3545 AppendCharToRedobuff(regname);
3546 }
3547 if (num)
3548 AppendNumberToRedobuff(num);
3549
3550 if (cmd1 != NUL)
3551 AppendCharToRedobuff(cmd1);
3552 if (cmd2 != NUL)
3553 AppendCharToRedobuff(cmd2);
3554 if (cmd3 != NUL)
3555 AppendCharToRedobuff(cmd3);
3556 if (cmd4 != NUL)
3557 AppendCharToRedobuff(cmd4);
3558 if (cmd5 != NUL)
3559 AppendCharToRedobuff(cmd5);
3560}
3561
3562/*
3563 * check for operator active and clear it
3564 *
3565 * return TRUE if operator was active
3566 */
3567 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003568checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 if (oap->op_type == OP_NOP)
3571 return FALSE;
3572 clearopbeep(oap);
3573 return TRUE;
3574}
3575
3576/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003577 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003579 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 */
3581 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003582checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003584 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 return FALSE;
3586 clearopbeep(oap);
3587 return TRUE;
3588}
3589
3590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003591clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
3593 oap->op_type = OP_NOP;
3594 oap->regname = 0;
3595 oap->motion_force = NUL;
3596 oap->use_reg_one = FALSE;
3597}
3598
3599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003600clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601{
3602 clearop(oap);
3603 beep_flush();
3604}
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606/*
3607 * Remove the shift modifier from a special key.
3608 */
3609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003610unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611{
3612 switch (cap->cmdchar)
3613 {
3614 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3615 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3616 case K_S_UP: cap->cmdchar = K_UP; break;
3617 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3618 case K_S_HOME: cap->cmdchar = K_HOME; break;
3619 case K_S_END: cap->cmdchar = K_END; break;
3620 }
3621 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3622}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003624/*
3625 * If the mode is currently displayed clear the command line or update the
3626 * command displayed.
3627 */
3628 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003629may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003630{
3631 if (mode_displayed)
3632 clear_cmdline = TRUE; /* unshow visual mode later */
3633#ifdef FEAT_CMDL_INFO
3634 else
3635 clear_showcmd();
3636#endif
3637}
3638
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3640/*
3641 * Routines for displaying a partly typed command
3642 */
3643
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003644#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645static char_u showcmd_buf[SHOWCMD_BUFLEN];
3646static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3647static int showcmd_is_clear = TRUE;
3648static int showcmd_visual = FALSE;
3649
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003650static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003653clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654{
3655 if (!p_sc)
3656 return;
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 if (VIsual_active && !char_avail())
3659 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003660 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 long lines;
3662 colnr_T leftcol, rightcol;
3663 linenr_T top, bot;
3664
3665 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003666 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
3668 top = VIsual.lnum;
3669 bot = curwin->w_cursor.lnum;
3670 }
3671 else
3672 {
3673 top = curwin->w_cursor.lnum;
3674 bot = VIsual.lnum;
3675 }
3676# ifdef FEAT_FOLDING
3677 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003678 (void)hasFolding(top, &top, NULL);
3679 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680# endif
3681 lines = bot - top + 1;
3682
3683 if (VIsual_mode == Ctrl_V)
3684 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003685# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003686 char_u *saved_sbr = p_sbr;
3687
3688 /* Make 'sbr' empty for a moment to get the correct size. */
3689 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003692# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003693 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3696 (long)(rightcol - leftcol + 1));
3697 }
3698 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3699 sprintf((char *)showcmd_buf, "%ld", lines);
3700 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003701 {
3702 char_u *s, *e;
3703 int l;
3704 int bytes = 0;
3705 int chars = 0;
3706
3707 if (cursor_bot)
3708 {
3709 s = ml_get_pos(&VIsual);
3710 e = ml_get_cursor();
3711 }
3712 else
3713 {
3714 s = ml_get_cursor();
3715 e = ml_get_pos(&VIsual);
3716 }
3717 while ((*p_sel != 'e') ? s <= e : s < e)
3718 {
3719 l = (*mb_ptr2len)(s);
3720 if (l == 0)
3721 {
3722 ++bytes;
3723 ++chars;
3724 break; /* end of line */
3725 }
3726 bytes += l;
3727 ++chars;
3728 s += l;
3729 }
3730 if (bytes == chars)
3731 sprintf((char *)showcmd_buf, "%d", chars);
3732 else
3733 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3736 showcmd_visual = TRUE;
3737 }
3738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
3740 showcmd_buf[0] = NUL;
3741 showcmd_visual = FALSE;
3742
3743 /* Don't actually display something if there is nothing to clear. */
3744 if (showcmd_is_clear)
3745 return;
3746 }
3747
3748 display_showcmd();
3749}
3750
3751/*
3752 * Add 'c' to string of shown command chars.
3753 * Return TRUE if output has been written (and setcursor() has been called).
3754 */
3755 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003756add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
3758 char_u *p;
3759 int old_len;
3760 int extra_len;
3761 int overflow;
3762#if defined(FEAT_MOUSE)
3763 int i;
3764 static int ignore[] =
3765 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003766# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3768 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003769# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003770 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003771 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3773 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003774 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003776 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 0
3778 };
3779#endif
3780
Bram Moolenaar09df3122006-01-23 22:23:09 +00003781 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 return FALSE;
3783
3784 if (showcmd_visual)
3785 {
3786 showcmd_buf[0] = NUL;
3787 showcmd_visual = FALSE;
3788 }
3789
3790#if defined(FEAT_MOUSE)
3791 /* Ignore keys that are scrollbar updates and mouse clicks */
3792 if (IS_SPECIAL(c))
3793 for (i = 0; ignore[i] != 0; ++i)
3794 if (ignore[i] == c)
3795 return FALSE;
3796#endif
3797
3798 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003799 if (*p == ' ')
3800 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 old_len = (int)STRLEN(showcmd_buf);
3802 extra_len = (int)STRLEN(p);
3803 overflow = old_len + extra_len - SHOWCMD_COLS;
3804 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003805 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3806 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 STRCAT(showcmd_buf, p);
3808
3809 if (char_avail())
3810 return FALSE;
3811
3812 display_showcmd();
3813
3814 return TRUE;
3815}
3816
3817 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003818add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 if (!add_to_showcmd(c))
3821 setcursor();
3822}
3823
3824/*
3825 * Delete 'len' characters from the end of the shown command.
3826 */
3827 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003828del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
3830 int old_len;
3831
3832 if (!p_sc)
3833 return;
3834
3835 old_len = (int)STRLEN(showcmd_buf);
3836 if (len > old_len)
3837 len = old_len;
3838 showcmd_buf[old_len - len] = NUL;
3839
3840 if (!char_avail())
3841 display_showcmd();
3842}
3843
3844/*
3845 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3846 * something and there is a partial mapping.
3847 */
3848 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003849push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
3851 if (p_sc)
3852 STRCPY(old_showcmd_buf, showcmd_buf);
3853}
3854
3855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003856pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
3858 if (!p_sc)
3859 return;
3860
3861 STRCPY(showcmd_buf, old_showcmd_buf);
3862
3863 display_showcmd();
3864}
3865
3866 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003867display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
3869 int len;
3870
3871 cursor_off();
3872
3873 len = (int)STRLEN(showcmd_buf);
3874 if (len == 0)
3875 showcmd_is_clear = TRUE;
3876 else
3877 {
3878 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3879 showcmd_is_clear = FALSE;
3880 }
3881
3882 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003883 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3884 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 */
3886 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3887
3888 setcursor(); /* put cursor back where it belongs */
3889}
3890#endif
3891
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892/*
3893 * When "check" is FALSE, prepare for commands that scroll the window.
3894 * When "check" is TRUE, take care of scroll-binding after the window has
3895 * scrolled. Called from normal_cmd() and edit().
3896 */
3897 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003898do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899{
3900 static win_T *old_curwin = NULL;
3901 static linenr_T old_topline = 0;
3902#ifdef FEAT_DIFF
3903 static int old_topfill = 0;
3904#endif
3905 static buf_T *old_buf = NULL;
3906 static colnr_T old_leftcol = 0;
3907
3908 if (check && curwin->w_p_scb)
3909 {
3910 /* If a ":syncbind" command was just used, don't scroll, only reset
3911 * the values. */
3912 if (did_syncbind)
3913 did_syncbind = FALSE;
3914 else if (curwin == old_curwin)
3915 {
3916 /*
3917 * Synchronize other windows, as necessary according to
3918 * 'scrollbind'. Don't do this after an ":edit" command, except
3919 * when 'diff' is set.
3920 */
3921 if ((curwin->w_buffer == old_buf
3922#ifdef FEAT_DIFF
3923 || curwin->w_p_diff
3924#endif
3925 )
3926 && (curwin->w_topline != old_topline
3927#ifdef FEAT_DIFF
3928 || curwin->w_topfill != old_topfill
3929#endif
3930 || curwin->w_leftcol != old_leftcol))
3931 {
3932 check_scrollbind(curwin->w_topline - old_topline,
3933 (long)(curwin->w_leftcol - old_leftcol));
3934 }
3935 }
3936 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3937 {
3938 /*
3939 * When switching between windows, make sure that the relative
3940 * vertical offset is valid for the new window. The relative
3941 * offset is invalid whenever another 'scrollbind' window has
3942 * scrolled to a point that would force the current window to
3943 * scroll past the beginning or end of its buffer. When the
3944 * resync is performed, some of the other 'scrollbind' windows may
3945 * need to jump so that the current window's relative position is
3946 * visible on-screen.
3947 */
3948 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3949 }
3950 curwin->w_scbind_pos = curwin->w_topline;
3951 }
3952
3953 old_curwin = curwin;
3954 old_topline = curwin->w_topline;
3955#ifdef FEAT_DIFF
3956 old_topfill = curwin->w_topfill;
3957#endif
3958 old_buf = curwin->w_buffer;
3959 old_leftcol = curwin->w_leftcol;
3960}
3961
3962/*
3963 * Synchronize any windows that have "scrollbind" set, based on the
3964 * number of rows by which the current window has changed
3965 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3966 */
3967 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003968check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
3970 int want_ver;
3971 int want_hor;
3972 win_T *old_curwin = curwin;
3973 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 int old_VIsual_select = VIsual_select;
3975 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 colnr_T tgt_leftcol = curwin->w_leftcol;
3977 long topline;
3978 long y;
3979
3980 /*
3981 * check 'scrollopt' string for vertical and horizontal scroll options
3982 */
3983 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3984#ifdef FEAT_DIFF
3985 want_ver |= old_curwin->w_p_diff;
3986#endif
3987 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3988
3989 /*
3990 * loop through the scrollbound windows and scroll accordingly
3991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003993 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
3995 curbuf = curwin->w_buffer;
3996 /* skip original window and windows with 'noscrollbind' */
3997 if (curwin != old_curwin && curwin->w_p_scb)
3998 {
3999 /*
4000 * do the vertical scroll
4001 */
4002 if (want_ver)
4003 {
4004#ifdef FEAT_DIFF
4005 if (old_curwin->w_p_diff && curwin->w_p_diff)
4006 {
4007 diff_set_topline(old_curwin, curwin);
4008 }
4009 else
4010#endif
4011 {
4012 curwin->w_scbind_pos += topline_diff;
4013 topline = curwin->w_scbind_pos;
4014 if (topline > curbuf->b_ml.ml_line_count)
4015 topline = curbuf->b_ml.ml_line_count;
4016 if (topline < 1)
4017 topline = 1;
4018
4019 y = topline - curwin->w_topline;
4020 if (y > 0)
4021 scrollup(y, FALSE);
4022 else
4023 scrolldown(-y, FALSE);
4024 }
4025
4026 redraw_later(VALID);
4027 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030
4031 /*
4032 * do the horizontal scroll
4033 */
4034 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4035 {
4036 curwin->w_leftcol = tgt_leftcol;
4037 leftcol_changed();
4038 }
4039 }
4040 }
4041
4042 /*
4043 * reset current-window
4044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 VIsual_select = old_VIsual_select;
4046 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 curwin = old_curwin;
4048 curbuf = old_curbuf;
4049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051/*
4052 * Command character that's ignored.
4053 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004054 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004057nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004059 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060}
4061
4062/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004063 * Command character that doesn't do anything, but unlike nv_ignore() does
4064 * start edit(). Used for "startinsert" executed while starting up.
4065 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004066 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004067nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004068{
4069}
4070
4071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 * Command character doesn't exist.
4073 */
4074 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004075nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
4077 clearopbeep(cap->oap);
4078}
4079
4080/*
4081 * <Help> and <F1> commands.
4082 */
4083 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004084nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086 if (!checkclearopq(cap->oap))
4087 ex_help(NULL);
4088}
4089
4090/*
4091 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4092 */
4093 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004094nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095{
Bram Moolenaarf2732452018-06-03 14:47:35 +02004096#ifdef FEAT_JOB_CHANNEL
4097 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4098 clearopbeep(cap->oap);
4099 else
4100#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01004101 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004102 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004103 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004104 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4105 op_addsub(cap->oap, cap->count1, cap->arg);
4106 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004107 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004108 else if (VIsual_active)
4109 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004110 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004111 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112}
4113
4114/*
4115 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4116 */
4117 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004118nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004121 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004122 if (mod_mask & MOD_MASK_CTRL)
4123 {
4124 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4125 if (cap->arg == BACKWARD)
4126 goto_tabpage(-(int)cap->count1);
4127 else
4128 goto_tabpage((int)cap->count0);
4129 }
4130 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004131 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133}
4134
4135/*
4136 * Implementation of "gd" and "gD" command.
4137 */
4138 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004139nv_gd(
4140 oparg_T *oap,
4141 int nchar,
4142 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 char_u *ptr;
4146
Bram Moolenaard9d30582005-05-18 22:10:28 +00004147 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004148 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4149 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004151#ifdef FEAT_FOLDING
4152 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4153 foldOpenCursor();
4154#endif
4155}
4156
4157/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004158 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4159 * otherwise.
4160 */
4161 static int
4162is_ident(char_u *line, int offset)
4163{
4164 int i;
4165 int incomment = FALSE;
4166 int instring = 0;
4167 int prev = 0;
4168
4169 for (i = 0; i < offset && line[i] != NUL; i++)
4170 {
4171 if (instring != 0)
4172 {
4173 if (prev != '\\' && line[i] == instring)
4174 instring = 0;
4175 }
4176 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4177 {
4178 instring = line[i];
4179 }
4180 else
4181 {
4182 if (incomment)
4183 {
4184 if (prev == '*' && line[i] == '/')
4185 incomment = FALSE;
4186 }
4187 else if (prev == '/' && line[i] == '*')
4188 {
4189 incomment = TRUE;
4190 }
4191 else if (prev == '/' && line[i] == '/')
4192 {
4193 return FALSE;
4194 }
4195 }
4196
4197 prev = line[i];
4198 }
4199
4200 return incomment == FALSE && instring == 0;
4201}
4202
4203/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004204 * Search for variable declaration of "ptr[len]".
4205 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4206 * current file ("gD").
4207 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004208 * Return FAIL when not found.
4209 */
4210 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004211find_decl(
4212 char_u *ptr,
4213 int len,
4214 int locally,
4215 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004216 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004217{
4218 char_u *pat;
4219 pos_T old_pos;
4220 pos_T par_pos;
4221 pos_T found_pos;
4222 int t;
4223 int save_p_ws;
4224 int save_p_scs;
4225 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004226 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004227 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004228 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004229
4230 if ((pat = alloc(len + 7)) == NULL)
4231 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004232
4233 /* Put "\V" before the pattern to avoid that the special meaning of "."
4234 * and "~" causes trouble. */
4235 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4236 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 old_pos = curwin->w_cursor;
4238 save_p_ws = p_ws;
4239 save_p_scs = p_scs;
4240 p_ws = FALSE; /* don't wrap around end of file now */
4241 p_scs = FALSE; /* don't switch ignorecase off now */
4242
4243 /*
4244 * With "gD" go to line 1.
4245 * With "gd" Search back for the start of the current function, then go
4246 * back until a blank line. If this fails go to line 1.
4247 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004248 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 setpcmark(); /* Set in findpar() otherwise */
4251 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004252 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 else
4255 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004256 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4258 --curwin->w_cursor.lnum;
4259 }
4260 curwin->w_cursor.col = 0;
4261
4262 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004263 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004264 for (;;)
4265 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004266 valid = FALSE;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004267 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004268 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004269 if (curwin->w_cursor.lnum >= old_pos.lnum)
4270 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004271
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004272 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004273 {
4274 pos_T *pos;
4275
4276 /* Check that the block the match is in doesn't end before the
4277 * position where we started the search from. */
4278 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4279 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4280 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004281 {
4282 /* There can't be a useful match before the end of this block.
4283 * Skip to the end. */
4284 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004285 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004286 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004287 }
4288
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004289 if (t == FAIL)
4290 {
4291 /* If we previously found a valid position, use it. */
4292 if (found_pos.lnum != 0)
4293 {
4294 curwin->w_cursor = found_pos;
4295 t = OK;
4296 }
4297 break;
4298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004300 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301 {
4302 /* Ignore this line, continue at start of next line. */
4303 ++curwin->w_cursor.lnum;
4304 curwin->w_cursor.col = 0;
4305 continue;
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004308 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4309
4310 /* If the current position is not a valid identifier and a previous
4311 * match is present, favor that one instead. */
4312 if (!valid && found_pos.lnum != 0)
4313 {
4314 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004315 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004316 }
4317
4318 /* Global search: use first valid match found */
4319 if (valid && !locally)
4320 break;
4321 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004322 {
4323 /* If we previously found a valid position, use it. */
4324 if (found_pos.lnum != 0)
4325 curwin->w_cursor = found_pos;
4326 break;
4327 }
4328
Bram Moolenaar226630a2016-10-08 19:21:31 +02004329 /* For finding a local variable and the match is before the "{" or
4330 * inside a comment, continue searching. For K&R style function
4331 * declarations this skips the function header without types. */
4332 if (!valid)
4333 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004334 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004335 }
4336 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004337 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004338 /* Remove SEARCH_START from flags to avoid getting stuck at one
4339 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004340 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004342
4343 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004345 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 curwin->w_cursor = old_pos;
4347 }
4348 else
4349 {
4350 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 /* "n" searches forward now */
4352 reset_search_dir();
4353 }
4354
4355 vim_free(pat);
4356 p_ws = save_p_ws;
4357 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004358
4359 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360}
4361
4362/*
4363 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4364 * lines rather than lines in the file.
4365 * 'dist' must be positive.
4366 *
4367 * Return OK if able to move cursor, FAIL otherwise.
4368 */
4369 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004370nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
4372 int linelen = linetabsize(ml_get_curline());
4373 int retval = OK;
4374 int atend = FALSE;
4375 int n;
4376 int col_off1; /* margin offset for first screen line */
4377 int col_off2; /* margin offset for wrapped screen line */
4378 int width1; /* text width for first screen line */
4379 int width2; /* test width for wrapped screen line */
4380
4381 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004382 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
4384 col_off1 = curwin_col_off();
4385 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004386 width1 = curwin->w_width - col_off1;
4387 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004388 if (width2 == 0)
4389 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 /*
4394 * Instead of sticking at the last character of the buffer line we
4395 * try to stick in the last column of the screen.
4396 */
4397 if (curwin->w_curswant == MAXCOL)
4398 {
4399 atend = TRUE;
4400 validate_virtcol();
4401 if (width1 <= 0)
4402 curwin->w_curswant = 0;
4403 else
4404 {
4405 curwin->w_curswant = width1 - 1;
4406 if (curwin->w_virtcol > curwin->w_curswant)
4407 curwin->w_curswant += ((curwin->w_virtcol
4408 - curwin->w_curswant - 1) / width2 + 1) * width2;
4409 }
4410 }
4411 else
4412 {
4413 if (linelen > width1)
4414 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4415 else
4416 n = width1;
4417 if (curwin->w_curswant > (colnr_T)n + 1)
4418 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4419 * width2;
4420 }
4421
4422 while (dist--)
4423 {
4424 if (dir == BACKWARD)
4425 {
4426 if ((long)curwin->w_curswant >= width2)
4427 /* move back within line */
4428 curwin->w_curswant -= width2;
4429 else
4430 {
4431 /* to previous line */
4432 if (curwin->w_cursor.lnum == 1)
4433 {
4434 retval = FAIL;
4435 break;
4436 }
4437 --curwin->w_cursor.lnum;
4438#ifdef FEAT_FOLDING
4439 /* Move to the start of a closed fold. Don't do that when
4440 * 'foldopen' contains "all": it will open in a moment. */
4441 if (!(fdo_flags & FDO_ALL))
4442 (void)hasFolding(curwin->w_cursor.lnum,
4443 &curwin->w_cursor.lnum, NULL);
4444#endif
4445 linelen = linetabsize(ml_get_curline());
4446 if (linelen > width1)
4447 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4448 + 1) * width2;
4449 }
4450 }
4451 else /* dir == FORWARD */
4452 {
4453 if (linelen > width1)
4454 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4455 else
4456 n = width1;
4457 if (curwin->w_curswant + width2 < (colnr_T)n)
4458 /* move forward within line */
4459 curwin->w_curswant += width2;
4460 else
4461 {
4462 /* to next line */
4463#ifdef FEAT_FOLDING
4464 /* Move to the end of a closed fold. */
4465 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4466 &curwin->w_cursor.lnum);
4467#endif
4468 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4469 {
4470 retval = FAIL;
4471 break;
4472 }
4473 curwin->w_cursor.lnum++;
4474 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004475 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 }
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004481 if (virtual_active() && atend)
4482 coladvance(MAXCOL);
4483 else
4484 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4487 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004488 colnr_T virtcol;
4489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 /*
4491 * Check for landing on a character that got split at the end of the
4492 * last line. We want to advance a screenline, not end up in the same
4493 * screenline or move two screenlines.
4494 */
4495 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004496 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004497#if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004498 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4499 virtcol -= vim_strsize(p_sbr);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004500#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004501
4502 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 && (curwin->w_curswant < (colnr_T)width1
4504 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4505 : ((curwin->w_curswant - width1) % width2
4506 > (colnr_T)width2 / 2)))
4507 --curwin->w_cursor.col;
4508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 if (atend)
4511 curwin->w_curswant = MAXCOL; /* stick in the last column */
4512
4513 return retval;
4514}
4515
4516#ifdef FEAT_MOUSE
4517/*
4518 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4519 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004520 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4521 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
4523 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004524nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004526 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004528 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 int row, col;
4531
4532 row = mouse_row;
4533 col = mouse_col;
4534
4535 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004536 wp = mouse_find_win(&row, &col);
4537 if (wp == NULL)
4538 return;
4539 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 curbuf = curwin->w_buffer;
4541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004543 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004545# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004546 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004547 /* This window is a terminal window, send the mouse event there.
4548 * Set "typed" to FALSE to avoid an endless loop. */
4549 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004550 else
4551# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004552 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4553 {
4554 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4555 }
4556 else
4557 {
4558 cap->count1 = 3;
4559 cap->count0 = 3;
4560 nv_scroll_line(cap);
4561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004563# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 else
4565 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004566 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4567 if (!curwin->w_p_wrap)
4568 {
4569 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004570
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004571 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004572 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004573 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4574 if (val < 0)
4575 val = 0;
4576
4577 gui_do_horiz_scroll(val, TRUE);
4578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004580# endif
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +01004581# ifdef FEAT_SYN_HL
4582 if (curwin != old_curwin && curwin->w_p_cul)
4583 redraw_for_cursorline(curwin);
4584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 curwin->w_redr_status = TRUE;
4587
4588 curwin = old_curwin;
4589 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590}
4591
4592/*
4593 * Mouse clicks and drags.
4594 */
4595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004596nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597{
4598 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4599}
4600#endif
4601
4602/*
4603 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4604 * cap->arg must be TRUE for CTRL-E.
4605 */
4606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004607nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608{
4609 if (!checkclearop(cap->oap))
4610 scroll_redraw(cap->arg, cap->count1);
4611}
4612
4613/*
4614 * Scroll "count" lines up or down, and redraw.
4615 */
4616 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004617scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618{
4619 linenr_T prev_topline = curwin->w_topline;
4620#ifdef FEAT_DIFF
4621 int prev_topfill = curwin->w_topfill;
4622#endif
4623 linenr_T prev_lnum = curwin->w_cursor.lnum;
4624
4625 if (up)
4626 scrollup(count, TRUE);
4627 else
4628 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01004629 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
4631 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4632 * valid, otherwise the screen jumps back at the end of the file. */
4633 cursor_correct();
4634 check_cursor_moved(curwin);
4635 curwin->w_valid |= VALID_TOPLINE;
4636
4637 /* If moved back to where we were, at least move the cursor, otherwise
4638 * we get stuck at one position. Don't move the cursor up if the
4639 * first line of the buffer is already on the screen */
4640 while (curwin->w_topline == prev_topline
4641#ifdef FEAT_DIFF
4642 && curwin->w_topfill == prev_topfill
4643#endif
4644 )
4645 {
4646 if (up)
4647 {
4648 if (curwin->w_cursor.lnum > prev_lnum
4649 || cursor_down(1L, FALSE) == FAIL)
4650 break;
4651 }
4652 else
4653 {
4654 if (curwin->w_cursor.lnum < prev_lnum
4655 || prev_topline == 1L
4656 || cursor_up(1L, FALSE) == FAIL)
4657 break;
4658 }
4659 /* Mark w_topline as valid, otherwise the screen jumps back at the
4660 * end of the file. */
4661 check_cursor_moved(curwin);
4662 curwin->w_valid |= VALID_TOPLINE;
4663 }
4664 }
4665 if (curwin->w_cursor.lnum != prev_lnum)
4666 coladvance(curwin->w_curswant);
4667 redraw_later(VALID);
4668}
4669
4670/*
4671 * Commands that start with "z".
4672 */
4673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004674nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
4676 long n;
4677 colnr_T col;
4678 int nchar = cap->nchar;
4679#ifdef FEAT_FOLDING
4680 long old_fdl = curwin->w_p_fdl;
4681 int old_fen = curwin->w_p_fen;
4682#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004683#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004684 int undo = FALSE;
4685#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01004686 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688 if (VIM_ISDIGIT(nchar))
4689 {
4690 /*
4691 * "z123{nchar}": edit the count before obtaining {nchar}
4692 */
4693 if (checkclearop(cap->oap))
4694 return;
4695 n = nchar - '0';
4696 for (;;)
4697 {
4698#ifdef USE_ON_FLY_SCROLL
4699 dont_scroll = TRUE; /* disallow scrolling here */
4700#endif
4701 ++no_mapping;
4702 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004703 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 --no_mapping;
4706 --allow_keys;
4707#ifdef FEAT_CMDL_INFO
4708 (void)add_to_showcmd(nchar);
4709#endif
4710 if (nchar == K_DEL || nchar == K_KDEL)
4711 n /= 10;
4712 else if (VIM_ISDIGIT(nchar))
4713 n = n * 10 + (nchar - '0');
4714 else if (nchar == CAR)
4715 {
4716#ifdef FEAT_GUI
4717 need_mouse_correct = TRUE;
4718#endif
4719 win_setheight((int)n);
4720 break;
4721 }
4722 else if (nchar == 'l'
4723 || nchar == 'h'
4724 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004725 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 {
4727 cap->count1 = n ? n * cap->count1 : cap->count1;
4728 goto dozet;
4729 }
4730 else
4731 {
4732 clearopbeep(cap->oap);
4733 break;
4734 }
4735 }
4736 cap->oap->op_type = OP_NOP;
4737 return;
4738 }
4739
4740dozet:
4741 if (
4742#ifdef FEAT_FOLDING
4743 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4744 * and "zC" only in Visual mode. "zj" and "zk" are motion
4745 * commands. */
4746 cap->nchar != 'f' && cap->nchar != 'F'
4747 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4748 && cap->nchar != 'j' && cap->nchar != 'k'
4749 &&
4750#endif
4751 checkclearop(cap->oap))
4752 return;
4753
4754 /*
4755 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4756 * If line number given, set cursor.
4757 */
4758 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4759 && cap->count0
4760 && cap->count0 != curwin->w_cursor.lnum)
4761 {
4762 setpcmark();
4763 if (cap->count0 > curbuf->b_ml.ml_line_count)
4764 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4765 else
4766 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004767 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769
4770 switch (nchar)
4771 {
4772 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4773 case '+':
4774 if (cap->count0 == 0)
4775 {
4776 /* No count given: put cursor at the line below screen */
4777 validate_botline(); /* make sure w_botline is valid */
4778 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4779 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4780 else
4781 curwin->w_cursor.lnum = curwin->w_botline;
4782 }
4783 /* FALLTHROUGH */
4784 case NL:
4785 case CAR:
4786 case K_KENTER:
4787 beginline(BL_WHITE | BL_FIX);
4788 /* FALLTHROUGH */
4789
4790 case 't': scroll_cursor_top(0, TRUE);
4791 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004792 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 break;
4794
4795 /* "z." and "zz": put cursor in middle of screen */
4796 case '.': beginline(BL_WHITE | BL_FIX);
4797 /* FALLTHROUGH */
4798
4799 case 'z': scroll_cursor_halfway(TRUE);
4800 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004801 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 break;
4803
4804 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4805 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4806 * when <count> is at bottom of window, and puts that one at
4807 * bottom of window. */
4808 if (cap->count0 != 0)
4809 {
4810 scroll_cursor_bot(0, TRUE);
4811 curwin->w_cursor.lnum = curwin->w_topline;
4812 }
4813 else if (curwin->w_topline == 1)
4814 curwin->w_cursor.lnum = 1;
4815 else
4816 curwin->w_cursor.lnum = curwin->w_topline - 1;
4817 /* FALLTHROUGH */
4818 case '-':
4819 beginline(BL_WHITE | BL_FIX);
4820 /* FALLTHROUGH */
4821
4822 case 'b': scroll_cursor_bot(0, TRUE);
4823 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004824 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 break;
4826
4827 /* "zH" - scroll screen right half-page */
4828 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004829 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 /* FALLTHROUGH */
4831
4832 /* "zh" - scroll screen to the right */
4833 case 'h':
4834 case K_LEFT:
4835 if (!curwin->w_p_wrap)
4836 {
4837 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4838 curwin->w_leftcol = 0;
4839 else
4840 curwin->w_leftcol -= (colnr_T)cap->count1;
4841 leftcol_changed();
4842 }
4843 break;
4844
4845 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004846 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 /* FALLTHROUGH */
4848
4849 /* "zl" - scroll screen to the left */
4850 case 'l':
4851 case K_RIGHT:
4852 if (!curwin->w_p_wrap)
4853 {
4854 /* scroll the window left */
4855 curwin->w_leftcol += (colnr_T)cap->count1;
4856 leftcol_changed();
4857 }
4858 break;
4859
4860 /* "zs" - scroll screen, cursor at the start */
4861 case 's': if (!curwin->w_p_wrap)
4862 {
4863#ifdef FEAT_FOLDING
4864 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4865 col = 0; /* like the cursor is in col 0 */
4866 else
4867#endif
4868 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01004869 if ((long)col > siso)
4870 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 else
4872 col = 0;
4873 if (curwin->w_leftcol != col)
4874 {
4875 curwin->w_leftcol = col;
4876 redraw_later(NOT_VALID);
4877 }
4878 }
4879 break;
4880
4881 /* "ze" - scroll screen, cursor at the end */
4882 case 'e': if (!curwin->w_p_wrap)
4883 {
4884#ifdef FEAT_FOLDING
4885 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4886 col = 0; /* like the cursor is in col 0 */
4887 else
4888#endif
4889 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004890 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01004891 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 col = 0;
4893 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01004894 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (curwin->w_leftcol != col)
4896 {
4897 curwin->w_leftcol = col;
4898 redraw_later(NOT_VALID);
4899 }
4900 }
4901 break;
4902
4903#ifdef FEAT_FOLDING
4904 /* "zF": create fold command */
4905 /* "zf": create fold operator */
4906 case 'F':
4907 case 'f': if (foldManualAllowed(TRUE))
4908 {
4909 cap->nchar = 'f';
4910 nv_operator(cap);
4911 curwin->w_p_fen = TRUE;
4912
4913 /* "zF" is like "zfzf" */
4914 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4915 {
4916 nv_operator(cap);
4917 finish_op = TRUE;
4918 }
4919 }
4920 else
4921 clearopbeep(cap->oap);
4922 break;
4923
4924 /* "zd": delete fold at cursor */
4925 /* "zD": delete fold at cursor recursively */
4926 case 'd':
4927 case 'D': if (foldManualAllowed(FALSE))
4928 {
4929 if (VIsual_active)
4930 nv_operator(cap);
4931 else
4932 deleteFold(curwin->w_cursor.lnum,
4933 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4934 }
4935 break;
4936
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004937 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 case 'E': if (foldmethodIsManual(curwin))
4939 {
4940 clearFolding(curwin);
4941 changed_window_setting();
4942 }
4943 else if (foldmethodIsMarker(curwin))
4944 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4945 TRUE, FALSE);
4946 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004947 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 break;
4949
4950 /* "zn": fold none: reset 'foldenable' */
4951 case 'n': curwin->w_p_fen = FALSE;
4952 break;
4953
4954 /* "zN": fold Normal: set 'foldenable' */
4955 case 'N': curwin->w_p_fen = TRUE;
4956 break;
4957
4958 /* "zi": invert folding: toggle 'foldenable' */
4959 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4960 break;
4961
4962 /* "za": open closed fold or close open fold at cursor */
4963 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4964 openFold(curwin->w_cursor.lnum, cap->count1);
4965 else
4966 {
4967 closeFold(curwin->w_cursor.lnum, cap->count1);
4968 curwin->w_p_fen = TRUE;
4969 }
4970 break;
4971
4972 /* "zA": open fold at cursor recursively */
4973 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4974 openFoldRecurse(curwin->w_cursor.lnum);
4975 else
4976 {
4977 closeFoldRecurse(curwin->w_cursor.lnum);
4978 curwin->w_p_fen = TRUE;
4979 }
4980 break;
4981
4982 /* "zo": open fold at cursor or Visual area */
4983 case 'o': if (VIsual_active)
4984 nv_operator(cap);
4985 else
4986 openFold(curwin->w_cursor.lnum, cap->count1);
4987 break;
4988
4989 /* "zO": open fold recursively */
4990 case 'O': if (VIsual_active)
4991 nv_operator(cap);
4992 else
4993 openFoldRecurse(curwin->w_cursor.lnum);
4994 break;
4995
4996 /* "zc": close fold at cursor or Visual area */
4997 case 'c': if (VIsual_active)
4998 nv_operator(cap);
4999 else
5000 closeFold(curwin->w_cursor.lnum, cap->count1);
5001 curwin->w_p_fen = TRUE;
5002 break;
5003
5004 /* "zC": close fold recursively */
5005 case 'C': if (VIsual_active)
5006 nv_operator(cap);
5007 else
5008 closeFoldRecurse(curwin->w_cursor.lnum);
5009 curwin->w_p_fen = TRUE;
5010 break;
5011
5012 /* "zv": open folds at the cursor */
5013 case 'v': foldOpenCursor();
5014 break;
5015
5016 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5017 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005018 curwin->w_foldinvalid = TRUE; /* recompute folds */
5019 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 foldOpenCursor();
5021 break;
5022
5023 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5024 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005025 curwin->w_foldinvalid = TRUE; /* recompute folds */
5026 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 break;
5028
5029 /* "zm": fold more */
5030 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005031 {
5032 curwin->w_p_fdl -= cap->count1;
5033 if (curwin->w_p_fdl < 0)
5034 curwin->w_p_fdl = 0;
5035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 old_fdl = -1; /* force an update */
5037 curwin->w_p_fen = TRUE;
5038 break;
5039
5040 /* "zM": close all folds */
5041 case 'M': curwin->w_p_fdl = 0;
5042 old_fdl = -1; /* force an update */
5043 curwin->w_p_fen = TRUE;
5044 break;
5045
5046 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005047 case 'r': curwin->w_p_fdl += cap->count1;
5048 {
5049 int d = getDeepestNesting();
5050
5051 if (curwin->w_p_fdl >= d)
5052 curwin->w_p_fdl = d;
5053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 break;
5055
5056 /* "zR": open all folds */
5057 case 'R': curwin->w_p_fdl = getDeepestNesting();
5058 old_fdl = -1; /* force an update */
5059 break;
5060
5061 case 'j': /* "zj" move to next fold downwards */
5062 case 'k': /* "zk" move to next fold upwards */
5063 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5064 cap->count1) == FAIL)
5065 clearopbeep(cap->oap);
5066 break;
5067
5068#endif /* FEAT_FOLDING */
5069
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005070#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005071 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5072 ++no_mapping;
5073 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005074 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005075 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005076 --no_mapping;
5077 --allow_keys;
5078#ifdef FEAT_CMDL_INFO
5079 (void)add_to_showcmd(nchar);
5080#endif
5081 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5082 {
5083 clearopbeep(cap->oap);
5084 break;
5085 }
5086 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005087 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005088
Bram Moolenaarb765d632005-06-07 21:00:02 +00005089 case 'g': /* "zg": add good word to word list */
5090 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005091 case 'G': /* "zG": add good word to temp word list */
5092 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005093 {
5094 char_u *ptr = NULL;
5095 int len;
5096
5097 if (checkclearop(cap->oap))
5098 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005099 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5100 == FAIL)
5101 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005102 if (ptr == NULL)
5103 {
5104 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005105
Bram Moolenaar134bf072013-09-25 18:54:24 +02005106 /* Find bad word under the cursor. When 'spell' is
5107 * off this fails and find_ident_under_cursor() is
5108 * used below. */
5109 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005110 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005111 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005112 if (len != 0 && curwin->w_cursor.col <= pos.col)
5113 ptr = ml_get_pos(&curwin->w_cursor);
5114 curwin->w_cursor = pos;
5115 }
5116
Bram Moolenaarb765d632005-06-07 21:00:02 +00005117 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5118 FIND_IDENT)) == 0)
5119 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005120 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005121 (nchar == 'G' || nchar == 'W')
5122 ? 0 : (int)cap->count1,
5123 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005124 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005125 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126
Bram Moolenaar43abc522005-12-10 20:15:02 +00005127 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005128 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005129 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005130 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005131#endif
5132
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 default: clearopbeep(cap->oap);
5134 }
5135
5136#ifdef FEAT_FOLDING
5137 /* Redraw when 'foldenable' changed */
5138 if (old_fen != curwin->w_p_fen)
5139 {
5140# ifdef FEAT_DIFF
5141 win_T *wp;
5142
5143 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5144 {
5145 /* Adjust 'foldenable' in diff-synced windows. */
5146 FOR_ALL_WINDOWS(wp)
5147 {
5148 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5149 {
5150 wp->w_p_fen = curwin->w_p_fen;
5151 changed_window_setting_win(wp);
5152 }
5153 }
5154 }
5155# endif
5156 changed_window_setting();
5157 }
5158
5159 /* Redraw when 'foldlevel' changed. */
5160 if (old_fdl != curwin->w_p_fdl)
5161 newFoldLevel();
5162#endif
5163}
5164
5165#ifdef FEAT_GUI
5166/*
5167 * Vertical scrollbar movement.
5168 */
5169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 if (cap->oap->op_type != OP_NOP)
5173 clearopbeep(cap->oap);
5174
5175 /* Even if an operator was pending, we still want to scroll */
5176 gui_do_scroll();
5177}
5178
5179/*
5180 * Horizontal scrollbar movement.
5181 */
5182 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005183nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
5185 if (cap->oap->op_type != OP_NOP)
5186 clearopbeep(cap->oap);
5187
5188 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005189 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190}
5191#endif
5192
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005193#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005194/*
5195 * Click in GUI tab.
5196 */
5197 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005198nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005199{
5200 if (cap->oap->op_type != OP_NOP)
5201 clearopbeep(cap->oap);
5202
5203 /* Even if an operator was pending, we still want to jump tabs. */
5204 goto_tabpage(current_tab);
5205}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005206
5207/*
5208 * Selected item in tab line menu.
5209 */
5210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005211nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005212{
5213 if (cap->oap->op_type != OP_NOP)
5214 clearopbeep(cap->oap);
5215
5216 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005217 handle_tabmenu();
5218}
5219
5220/*
5221 * Handle selecting an item of the GUI tab line menu.
5222 * Used in Normal and Insert mode.
5223 */
5224 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005225handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005226{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005227 switch (current_tabmenu)
5228 {
5229 case TABLINE_MENU_CLOSE:
5230 if (current_tab == 0)
5231 do_cmdline_cmd((char_u *)"tabclose");
5232 else
5233 {
5234 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5235 current_tab);
5236 do_cmdline_cmd(IObuff);
5237 }
5238 break;
5239
5240 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005241 if (current_tab == 0)
5242 do_cmdline_cmd((char_u *)"$tabnew");
5243 else
5244 {
5245 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5246 current_tab - 1);
5247 do_cmdline_cmd(IObuff);
5248 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005249 break;
5250
5251 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005252 if (current_tab == 0)
5253 do_cmdline_cmd((char_u *)"browse $tabnew");
5254 else
5255 {
5256 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5257 current_tab - 1);
5258 do_cmdline_cmd(IObuff);
5259 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005260 break;
5261 }
5262}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005263#endif
5264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265/*
5266 * "Q" command.
5267 */
5268 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005269nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270{
5271 /*
5272 * Ignore 'Q' in Visual mode, just give a beep.
5273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005275 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005276 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 do_exmode(FALSE);
5278}
5279
5280/*
5281 * Handle a ":" command.
5282 */
5283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005284nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005287 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (VIsual_active)
5290 nv_operator(cap);
5291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 {
5293 if (cap->oap->op_type != OP_NOP)
5294 {
5295 /* Using ":" as a movement is characterwise exclusive. */
5296 cap->oap->motion_type = MCHAR;
5297 cap->oap->inclusive = FALSE;
5298 }
5299 else if (cap->count0)
5300 {
5301 /* translate "count:" into ":.,.+(count - 1)" */
5302 stuffcharReadbuff('.');
5303 if (cap->count0 > 1)
5304 {
5305 stuffReadbuff((char_u *)",.+");
5306 stuffnumReadbuff((long)cap->count0 - 1L);
5307 }
5308 }
5309
5310 /* When typing, don't type below an old message */
5311 if (KeyTyped)
5312 compute_cmdrow();
5313
5314 old_p_im = p_im;
5315
5316 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005317 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5319
5320 /* If 'insertmode' changed, enter or exit Insert mode */
5321 if (p_im != old_p_im)
5322 {
5323 if (p_im)
5324 restart_edit = 'i';
5325 else
5326 restart_edit = 0;
5327 }
5328
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005329 if (cmd_result == FAIL)
5330 /* The Ex command failed, do not execute the operator. */
5331 clearop(cap->oap);
5332 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5334 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005335 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5336 || did_emsg
5337 ))
5338 /* The start of the operator has become invalid by the Ex command.
5339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 clearopbeep(cap->oap);
5341 }
5342}
5343
5344/*
5345 * Handle CTRL-G command.
5346 */
5347 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005348nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (VIsual_active) /* toggle Selection/Visual mode */
5351 {
5352 VIsual_select = !VIsual_select;
5353 showmode();
5354 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005355 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 /* print full name if count given or :cd used */
5357 fileinfo((int)cap->count0, FALSE, TRUE);
5358}
5359
5360/*
5361 * Handle CTRL-H <Backspace> command.
5362 */
5363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005364nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (VIsual_active && VIsual_select)
5367 {
5368 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5369 v_visop(cap);
5370 }
5371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 nv_left(cap);
5373}
5374
5375/*
5376 * CTRL-L: clear screen and redraw.
5377 */
5378 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005379nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
5381 if (!checkclearop(cap->oap))
5382 {
5383#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5384 /*
5385 * Right now, the BeBox doesn't seem to have an easy way to detect
5386 * window resizing, so we cheat and make the user detect it
5387 * manually with CTRL-L instead
5388 */
5389 ui_get_shellsize();
5390#endif
5391#ifdef FEAT_SYN_HL
5392 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005393 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005394# ifdef FEAT_RELTIME
5395 {
5396 win_T *wp;
5397
5398 FOR_ALL_WINDOWS(wp)
5399 wp->w_s->b_syn_slow = FALSE;
5400 }
5401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#endif
5403 redraw_later(CLEAR);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005404#if defined(MSWIN) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01005405 resize_console_buf();
5406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408}
5409
5410/*
5411 * CTRL-O: In Select mode: switch to Visual mode for one command.
5412 * Otherwise: Go to older pcmark.
5413 */
5414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005415nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 if (VIsual_active && VIsual_select)
5418 {
5419 VIsual_select = FALSE;
5420 showmode();
5421 restart_VIsual_select = 2; /* restart Select mode later */
5422 }
5423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
5425 cap->count1 = -cap->count1;
5426 nv_pcmark(cap);
5427 }
5428}
5429
5430/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01005431 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
5432 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 */
5434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005435nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 if (!checkclearopq(cap->oap))
5438 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5439 GETF_SETMARK|GETF_ALT, FALSE);
5440}
5441
5442/*
5443 * "Z" commands.
5444 */
5445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005446nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 if (!checkclearopq(cap->oap))
5449 {
5450 switch (cap->nchar)
5451 {
5452 /* "ZZ": equivalent to ":x". */
5453 case 'Z': do_cmdline_cmd((char_u *)"x");
5454 break;
5455
5456 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5457 case 'Q': do_cmdline_cmd((char_u *)"q!");
5458 break;
5459
5460 default: clearopbeep(cap->oap);
5461 }
5462 }
5463}
5464
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465/*
5466 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5467 */
5468 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005469do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
5471 oparg_T oa;
5472 cmdarg_T ca;
5473
5474 clear_oparg(&oa);
5475 vim_memset(&ca, 0, sizeof(ca));
5476 ca.oap = &oa;
5477 ca.cmdchar = c1;
5478 ca.nchar = c2;
5479 nv_ident(&ca);
5480}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482/*
5483 * Handle the commands that use the word under the cursor.
5484 * [g] CTRL-] :ta to current identifier
5485 * [g] 'K' run program for current identifier
5486 * [g] '*' / to current identifier or string
5487 * [g] '#' ? to current identifier or string
5488 * g ']' :tselect for current identifier
5489 */
5490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005491nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 char_u *ptr = NULL;
5494 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005495 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005496 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 char_u *p;
5498 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005499 int kp_help; /* 'keywordprg' is ":he" */
5500 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 int n = 0; /* init for GCC */
5502 int cmdchar;
5503 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005504 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 char_u *aux_ptr;
5506 int isman;
5507 int isman_s;
5508
5509 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5510 {
5511 cmdchar = cap->nchar;
5512 g_cmd = TRUE;
5513 }
5514 else
5515 {
5516 cmdchar = cap->cmdchar;
5517 g_cmd = FALSE;
5518 }
5519
5520 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5521 cmdchar = '#';
5522
5523 /*
5524 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5525 */
5526 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5529 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (checkclearopq(cap->oap))
5531 return;
5532 }
5533
5534 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5535 (cmdchar == '*' || cmdchar == '#')
5536 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5537 {
5538 clearop(cap->oap);
5539 return;
5540 }
5541
5542 /* Allocate buffer to put the command in. Inserting backslashes can
5543 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5544 * and some numbers. */
5545 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5546 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5547 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005548 if (kp_help && *skipwhite(ptr) == NUL)
5549 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005550 emsg(_(e_noident)); /* found white space only */
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005551 return;
5552 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005553 kp_ex = (*kp == ':');
5554 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5555 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (buf == NULL)
5557 return;
5558 buf[0] = NUL;
5559
5560 switch (cmdchar)
5561 {
5562 case '*':
5563 case '#':
5564 /*
5565 * Put cursor at start of word, makes search skip the word
5566 * under the cursor.
5567 * Call setpcmark() first, so "*``" puts the cursor back where
5568 * it was.
5569 */
5570 setpcmark();
5571 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5572
5573 if (!g_cmd && vim_iswordp(ptr))
5574 STRCPY(buf, "\\<");
5575 no_smartcase = TRUE; /* don't use 'smartcase' now */
5576 break;
5577
5578 case 'K':
5579 if (kp_help)
5580 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005581 else if (kp_ex)
5582 {
5583 if (cap->count0 != 0)
5584 vim_snprintf((char *)buf, buflen, "%s %ld",
5585 kp, cap->count0);
5586 else
5587 STRCPY(buf, kp);
5588 STRCAT(buf, " ");
5589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 else
5591 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005592 /* An external command will probably use an argument starting
5593 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005594 while (*ptr == '-' && n > 0)
5595 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005596 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005597 --n;
5598 }
5599 if (n == 0)
5600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005601 emsg(_(e_noident)); /* found dashes only */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005602 vim_free(buf);
5603 return;
5604 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 /* When a count is given, turn it into a range. Is this
5607 * really what we want? */
5608 isman = (STRCMP(kp, "man") == 0);
5609 isman_s = (STRCMP(kp, "man -s") == 0);
5610 if (cap->count0 != 0 && !(isman || isman_s))
5611 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5612
5613 STRCAT(buf, "! ");
5614 if (cap->count0 == 0 && isman_s)
5615 STRCAT(buf, "man");
5616 else
5617 STRCAT(buf, kp);
5618 STRCAT(buf, " ");
5619 if (cap->count0 != 0 && (isman || isman_s))
5620 {
5621 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5622 STRCAT(buf, " ");
5623 }
5624 }
5625 break;
5626
5627 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005628 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629#ifdef FEAT_CSCOPE
5630 if (p_cst)
5631 STRCPY(buf, "cstag ");
5632 else
5633#endif
5634 STRCPY(buf, "ts ");
5635 break;
5636
5637 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005638 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 if (curbuf->b_help)
5640 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005642 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005643 if (g_cmd)
5644 STRCPY(buf, "tj ");
5645 else
5646 sprintf((char *)buf, "%ldta ", cap->count0);
5647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649
5650 /*
5651 * Now grab the chars in the identifier
5652 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005653 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005655 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005656 if (kp_ex)
5657 /* Escape the argument properly for an Ex command */
5658 p = vim_strsave_fnameescape(ptr, FALSE);
5659 else
5660 /* Escape the argument properly for a shell command */
5661 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005662 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005663 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005665 vim_free(buf);
5666 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005668 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5669 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005670 {
5671 vim_free(buf);
5672 vim_free(p);
5673 return;
5674 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005675 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005676 STRCAT(buf, p);
5677 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005679 else
5680 {
5681 if (cmdchar == '*')
5682 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5683 else if (cmdchar == '#')
5684 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005685 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005686 {
5687 if (curbuf->b_help)
5688 /* ":help" handles unescaped argument */
5689 aux_ptr = (char_u *)"";
5690 else
5691 aux_ptr = (char_u *)"\\|\"\n[";
5692 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005693 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005694 aux_ptr = (char_u *)"\\|\"\n*?[";
5695
5696 p = buf + STRLEN(buf);
5697 while (n-- > 0)
5698 {
5699 /* put a backslash before \ and some others */
5700 if (vim_strchr(aux_ptr, *ptr) != NULL)
5701 *p++ = '\\';
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005702 /* When current byte is a part of multibyte character, copy all
5703 * bytes of that character. */
5704 if (has_mbyte)
5705 {
5706 int i;
5707 int len = (*mb_ptr2len)(ptr) - 1;
5708
5709 for (i = 0; i < len && n >= 1; ++i, --n)
5710 *p++ = *ptr++;
5711 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005712 *p++ = *ptr++;
5713 }
5714 *p = NUL;
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 /*
5718 * Execute the command.
5719 */
5720 if (cmdchar == '*' || cmdchar == '#')
5721 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005722 if (!g_cmd && (has_mbyte
5723 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
5724 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 STRCAT(buf, "\\>");
5726#ifdef FEAT_CMDHIST
5727 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005728 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5730#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005731 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 else
5734 do_cmdline_cmd(buf);
5735
5736 vim_free(buf);
5737}
5738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739/*
5740 * Get visually selected text, within one line only.
5741 * Returns FAIL if more than one line selected.
5742 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005743 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005744get_visual_text(
5745 cmdarg_T *cap,
5746 char_u **pp, /* return: start of selected text */
5747 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
5749 if (VIsual_mode != 'V')
5750 unadjust_for_sel();
5751 if (VIsual.lnum != curwin->w_cursor.lnum)
5752 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005753 if (cap != NULL)
5754 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 return FAIL;
5756 }
5757 if (VIsual_mode == 'V')
5758 {
5759 *pp = ml_get_curline();
5760 *lenp = (int)STRLEN(*pp);
5761 }
5762 else
5763 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005764 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 {
5766 *pp = ml_get_pos(&curwin->w_cursor);
5767 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5768 }
5769 else
5770 {
5771 *pp = ml_get_pos(&VIsual);
5772 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 if (has_mbyte)
5775 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005776 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778 reset_VIsual_and_resel();
5779 return OK;
5780}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
5782/*
5783 * CTRL-T: backwards in tag stack
5784 */
5785 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005786nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 if (!checkclearopq(cap->oap))
5789 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5790}
5791
5792/*
5793 * Handle scrolling command 'H', 'L' and 'M'.
5794 */
5795 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005796nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798 int used = 0;
5799 long n;
5800#ifdef FEAT_FOLDING
5801 linenr_T lnum;
5802#endif
5803 int half;
5804
5805 cap->oap->motion_type = MLINE;
5806 setpcmark();
5807
5808 if (cap->cmdchar == 'L')
5809 {
5810 validate_botline(); /* make sure curwin->w_botline is valid */
5811 curwin->w_cursor.lnum = curwin->w_botline - 1;
5812 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5813 curwin->w_cursor.lnum = 1;
5814 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005815 {
5816#ifdef FEAT_FOLDING
5817 if (hasAnyFolding(curwin))
5818 {
5819 /* Count a fold for one screen line. */
5820 for (n = cap->count1 - 1; n > 0
5821 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5822 {
5823 (void)hasFolding(curwin->w_cursor.lnum,
5824 &curwin->w_cursor.lnum, NULL);
5825 --curwin->w_cursor.lnum;
5826 }
5827 }
5828 else
5829#endif
5830 curwin->w_cursor.lnum -= cap->count1 - 1;
5831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
5833 else
5834 {
5835 if (cap->cmdchar == 'M')
5836 {
5837#ifdef FEAT_DIFF
5838 /* Don't count filler lines above the window. */
5839 used -= diff_check_fill(curwin, curwin->w_topline)
5840 - curwin->w_topfill;
5841#endif
5842 validate_botline(); /* make sure w_empty_rows is valid */
5843 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5844 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5845 {
5846#ifdef FEAT_DIFF
5847 /* Count half he number of filler lines to be "below this
5848 * line" and half to be "above the next line". */
5849 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5850 + n) / 2 >= half)
5851 {
5852 --n;
5853 break;
5854 }
5855#endif
5856 used += plines(curwin->w_topline + n);
5857 if (used >= half)
5858 break;
5859#ifdef FEAT_FOLDING
5860 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5861 n = lnum - curwin->w_topline;
5862#endif
5863 }
5864 if (n > 0 && used > curwin->w_height)
5865 --n;
5866 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005867 else /* (cap->cmdchar == 'H') */
5868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005870#ifdef FEAT_FOLDING
5871 if (hasAnyFolding(curwin))
5872 {
5873 /* Count a fold for one screen line. */
5874 lnum = curwin->w_topline;
5875 while (n-- > 0 && lnum < curwin->w_botline - 1)
5876 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005877 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005878 ++lnum;
5879 }
5880 n = lnum - curwin->w_topline;
5881 }
5882#endif
5883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 curwin->w_cursor.lnum = curwin->w_topline + n;
5885 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5886 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5887 }
5888
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005889 /* Correct for 'so', except when an operator is pending. */
5890 if (cap->oap->op_type == OP_NOP)
5891 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 beginline(BL_SOL | BL_FIX);
5893}
5894
5895/*
5896 * Cursor right commands.
5897 */
5898 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005899nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005902 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005904 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5905 {
5906 /* <C-Right> and <S-Right> move a word or WORD right */
5907 if (mod_mask & MOD_MASK_CTRL)
5908 cap->arg = TRUE;
5909 nv_wordcmd(cap);
5910 return;
5911 }
5912
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 cap->oap->motion_type = MCHAR;
5914 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005915 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005918 * In virtual edit mode, there's no such thing as "past_line", as lines
5919 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 */
5921 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005922 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
5924 for (n = cap->count1; n > 0; --n)
5925 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005926 if ((!past_line && oneright() == FAIL)
5927 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005928 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 {
5930 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005931 * <Space> wraps to next line if 'whichwrap' has 's'.
5932 * 'l' wraps to next line if 'whichwrap' has 'l'.
5933 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 */
5935 if ( ((cap->cmdchar == ' '
5936 && vim_strchr(p_ww, 's') != NULL)
5937 || (cap->cmdchar == 'l'
5938 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005939 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 && vim_strchr(p_ww, '>') != NULL))
5941 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5942 {
5943 /* When deleting we also count the NL as a character.
5944 * Set cap->oap->inclusive when last char in the line is
5945 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005946 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005948 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 cap->oap->inclusive = TRUE;
5950 else
5951 {
5952 ++curwin->w_cursor.lnum;
5953 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 curwin->w_set_curswant = TRUE;
5956 cap->oap->inclusive = FALSE;
5957 }
5958 continue;
5959 }
5960 if (cap->oap->op_type == OP_NOP)
5961 {
5962 /* Only beep and flush if not moved at all */
5963 if (n == cap->count1)
5964 beep_flush();
5965 }
5966 else
5967 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005968 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 cap->oap->inclusive = TRUE;
5970 }
5971 break;
5972 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005973 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {
5975 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 if (virtual_active())
5977 oneright();
5978 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 if (has_mbyte)
5981 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005982 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 ++curwin->w_cursor.col;
5985 }
5986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 }
5988#ifdef FEAT_FOLDING
5989 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5990 && cap->oap->op_type == OP_NOP)
5991 foldOpenCursor();
5992#endif
5993}
5994
5995/*
5996 * Cursor left commands.
5997 *
5998 * Returns TRUE when operator end should not be adjusted.
5999 */
6000 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006001nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 long n;
6004
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006005 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6006 {
6007 /* <C-Left> and <S-Left> move a word or WORD left */
6008 if (mod_mask & MOD_MASK_CTRL)
6009 cap->arg = 1;
6010 nv_bck_word(cap);
6011 return;
6012 }
6013
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 cap->oap->motion_type = MCHAR;
6015 cap->oap->inclusive = FALSE;
6016 for (n = cap->count1; n > 0; --n)
6017 {
6018 if (oneleft() == FAIL)
6019 {
6020 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6021 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6022 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6023 */
6024 if ( (((cap->cmdchar == K_BS
6025 || cap->cmdchar == Ctrl_H)
6026 && vim_strchr(p_ww, 'b') != NULL)
6027 || (cap->cmdchar == 'h'
6028 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006029 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 && vim_strchr(p_ww, '<') != NULL))
6031 && curwin->w_cursor.lnum > 1)
6032 {
6033 --(curwin->w_cursor.lnum);
6034 coladvance((colnr_T)MAXCOL);
6035 curwin->w_set_curswant = TRUE;
6036
6037 /* When the NL before the first char has to be deleted we
6038 * put the cursor on the NUL after the previous line.
6039 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006040 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 if ( (cap->oap->op_type == OP_DELETE
6042 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006043 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006045 char_u *cp = ml_get_cursor();
6046
6047 if (*cp != NUL)
6048 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006049 if (has_mbyte)
6050 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6051 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006052 ++curwin->w_cursor.col;
6053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 cap->retval |= CA_NO_ADJ_OP_END;
6055 }
6056 continue;
6057 }
6058 /* Only beep and flush if not moved at all */
6059 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6060 beep_flush();
6061 break;
6062 }
6063 }
6064#ifdef FEAT_FOLDING
6065 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6066 && cap->oap->op_type == OP_NOP)
6067 foldOpenCursor();
6068#endif
6069}
6070
6071/*
6072 * Cursor up commands.
6073 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6074 */
6075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006076nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006078 if (mod_mask & MOD_MASK_SHIFT)
6079 {
6080 /* <S-Up> is page up */
6081 cap->arg = BACKWARD;
6082 nv_page(cap);
6083 }
6084 else
6085 {
6086 cap->oap->motion_type = MLINE;
6087 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6088 clearopbeep(cap->oap);
6089 else if (cap->arg)
6090 beginline(BL_WHITE | BL_FIX);
6091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092}
6093
6094/*
6095 * Cursor down commands.
6096 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6097 */
6098 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006099nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006101 if (mod_mask & MOD_MASK_SHIFT)
6102 {
6103 /* <S-Down> is page down */
6104 cap->arg = FORWARD;
6105 nv_page(cap);
6106 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006107#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006108 /* Quickfix window only: view the result under the cursor. */
6109 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6110 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006112 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {
6114#ifdef FEAT_CMDWIN
6115 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006116 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 cmdwin_result = CAR;
6118 else
6119#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006120#ifdef FEAT_JOB_CHANNEL
6121 /* In a prompt buffer a <CR> in the last line invokes the callback. */
6122 if (bt_prompt(curbuf) && cap->cmdchar == CAR
6123 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
6124 {
6125 invoke_prompt_callback();
6126 if (restart_edit == 0)
6127 restart_edit = 'a';
6128 }
6129 else
6130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 {
6132 cap->oap->motion_type = MLINE;
6133 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6134 clearopbeep(cap->oap);
6135 else if (cap->arg)
6136 beginline(BL_WHITE | BL_FIX);
6137 }
6138 }
6139}
6140
6141#ifdef FEAT_SEARCHPATH
6142/*
6143 * Grab the file name under the cursor and edit it.
6144 */
6145 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006146nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006149 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006151 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 {
6153 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006154 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 return;
6156 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006157 if (curbuf_locked())
6158 {
6159 clearop(cap->oap);
6160 return;
6161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006163 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
6165 if (ptr != NULL)
6166 {
6167 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006168 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006169 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006171 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006172 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006173 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006174 {
6175 curwin->w_cursor.lnum = lnum;
6176 check_cursor_lnum();
6177 beginline(BL_SOL | BL_FIX);
6178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 vim_free(ptr);
6180 }
6181 else
6182 clearop(cap->oap);
6183}
6184#endif
6185
6186/*
6187 * <End> command: to end of current line or last line.
6188 */
6189 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006190nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006192 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006194 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 nv_goto(cap);
6196 cap->count1 = 1; /* to end of current line */
6197 }
6198 nv_dollar(cap);
6199}
6200
6201/*
6202 * Handle the "$" command.
6203 */
6204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006205nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 cap->oap->motion_type = MCHAR;
6208 cap->oap->inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 /* In virtual mode when off the edge of a line and an operator
6210 * is pending (whew!) keep the cursor where it is.
6211 * Otherwise, send it to the end of the line. */
6212 if (!virtual_active() || gchar_cursor() != NUL
6213 || cap->oap->op_type == OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6215 if (cursor_down((long)(cap->count1 - 1),
6216 cap->oap->op_type == OP_NOP) == FAIL)
6217 clearopbeep(cap->oap);
6218#ifdef FEAT_FOLDING
6219 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6220 foldOpenCursor();
6221#endif
6222}
6223
6224/*
6225 * Implementation of '?' and '/' commands.
6226 * If cap->arg is TRUE don't set PC mark.
6227 */
6228 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006229nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
6231 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006232 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233
6234 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6235 {
6236 /* Translate "g??" to "g?g?" */
6237 cap->cmdchar = 'g';
6238 cap->nchar = '?';
6239 nv_operator(cap);
6240 return;
6241 }
6242
Bram Moolenaardda933d2016-09-03 21:04:58 +02006243 /* When using 'incsearch' the cursor may be moved to set a different search
6244 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6246
6247 if (cap->searchbuf == NULL)
6248 {
6249 clearop(oap);
6250 return;
6251 }
6252
Bram Moolenaar46539112015-02-17 15:43:57 +01006253 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006254 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006255 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256}
6257
6258/*
6259 * Handle "N" and "n" commands.
6260 * cap->arg is SEARCH_REV for "N", 0 for "n".
6261 */
6262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006263nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
Bram Moolenaar46539112015-02-17 15:43:57 +01006265 pos_T old = curwin->w_cursor;
6266 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6267
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006268 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006269 {
6270 /* Avoid getting stuck on the current cursor position, which can
6271 * happen when an offset is given and the cursor is on the last char
6272 * in the buffer: Repeat with count + 1. */
6273 cap->count1 += 1;
6274 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6275 cap->count1 -= 1;
6276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277}
6278
6279/*
6280 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6281 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006282 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006284 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006285normal_search(
6286 cmdarg_T *cap,
6287 int dir,
6288 char_u *pat,
6289 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 int i;
6292
6293 cap->oap->motion_type = MCHAR;
6294 cap->oap->inclusive = FALSE;
6295 cap->oap->use_reg_one = TRUE;
6296 curwin->w_set_curswant = TRUE;
6297
6298 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006299 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 if (i == 0)
6301 clearop(cap->oap);
6302 else
6303 {
6304 if (i == 2)
6305 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307#ifdef FEAT_FOLDING
6308 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6309 foldOpenCursor();
6310#endif
6311 }
6312
6313 /* "/$" will put the cursor after the end of the line, may need to
6314 * correct that here */
6315 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006316 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317}
6318
6319/*
6320 * Character search commands.
6321 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6322 * ',' and FALSE for ';'.
6323 * cap->nchar is NUL for ',' and ';' (repeat the search)
6324 */
6325 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006326nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 int t_cmd;
6329
6330 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6331 t_cmd = TRUE;
6332 else
6333 t_cmd = FALSE;
6334
6335 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6337 clearopbeep(cap->oap);
6338 else
6339 {
6340 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 /* Include a Tab for "tx" and for "dfx". */
6342 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6343 && (t_cmd || cap->oap->op_type != OP_NOP))
6344 {
6345 colnr_T scol, ecol;
6346
6347 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6348 curwin->w_cursor.coladd = ecol - scol;
6349 }
6350 else
6351 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353#ifdef FEAT_FOLDING
6354 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6355 foldOpenCursor();
6356#endif
6357 }
6358}
6359
6360/*
6361 * "[" and "]" commands.
6362 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6363 */
6364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006365nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006367 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 pos_T prev_pos;
6369 pos_T *pos = NULL; /* init for GCC */
6370 pos_T old_pos; /* cursor position before command */
6371 int flag;
6372 long n;
6373 int findc;
6374 int c;
6375
6376 cap->oap->motion_type = MCHAR;
6377 cap->oap->inclusive = FALSE;
6378 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01006379 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380
6381#ifdef FEAT_SEARCHPATH
6382 /*
6383 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6384 */
6385 if (cap->nchar == 'f')
6386 nv_gotofile(cap);
6387 else
6388#endif
6389
6390#ifdef FEAT_FIND_ID
6391 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006392 * Find the occurrence(s) of the identifier or define under cursor
6393 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 *
6395 * search list jump
6396 * fwd bwd fwd bwd fwd bwd
6397 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6398 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6399 */
6400 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006401# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006403# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 cap->nchar) != NULL)
6407 {
6408 char_u *ptr;
6409 int len;
6410
6411 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6412 clearop(cap->oap);
6413 else
6414 {
6415 find_pattern_in_path(ptr, 0, len, TRUE,
6416 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6417 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6418 cap->count1,
6419 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6420 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6421 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6422 (linenr_T)MAXLNUM);
6423 curwin->w_set_curswant = TRUE;
6424 }
6425 }
6426 else
6427#endif
6428
6429 /*
6430 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6431 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6432 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6433 * "[m" or "]m" search for prev/next start of (Java) method.
6434 * "[M" or "]M" search for prev/next end of (Java) method.
6435 */
6436 if ( (cap->cmdchar == '['
6437 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6438 || (cap->cmdchar == ']'
6439 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6440 {
6441 if (cap->nchar == '*')
6442 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 prev_pos.lnum = 0;
6444 if (cap->nchar == 'm' || cap->nchar == 'M')
6445 {
6446 if (cap->cmdchar == '[')
6447 findc = '{';
6448 else
6449 findc = '}';
6450 n = 9999;
6451 }
6452 else
6453 {
6454 findc = cap->nchar;
6455 n = cap->count1;
6456 }
6457 for ( ; n > 0; --n)
6458 {
6459 if ((pos = findmatchlimit(cap->oap, findc,
6460 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6461 {
6462 if (new_pos.lnum == 0) /* nothing found */
6463 {
6464 if (cap->nchar != 'm' && cap->nchar != 'M')
6465 clearopbeep(cap->oap);
6466 }
6467 else
6468 pos = &new_pos; /* use last one found */
6469 break;
6470 }
6471 prev_pos = new_pos;
6472 curwin->w_cursor = *pos;
6473 new_pos = *pos;
6474 }
6475 curwin->w_cursor = old_pos;
6476
6477 /*
6478 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6479 * brought us to the match for "[m" and "]M" when inside a method.
6480 * Try finding the '{' or '}' we want to be at.
6481 * Also repeat for the given count.
6482 */
6483 if (cap->nchar == 'm' || cap->nchar == 'M')
6484 {
6485 /* norm is TRUE for "]M" and "[m" */
6486 int norm = ((findc == '{') == (cap->nchar == 'm'));
6487
6488 n = cap->count1;
6489 /* found a match: we were inside a method */
6490 if (prev_pos.lnum != 0)
6491 {
6492 pos = &prev_pos;
6493 curwin->w_cursor = prev_pos;
6494 if (norm)
6495 --n;
6496 }
6497 else
6498 pos = NULL;
6499 while (n > 0)
6500 {
6501 for (;;)
6502 {
6503 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6504 {
6505 /* if not found anything, that's an error */
6506 if (pos == NULL)
6507 clearopbeep(cap->oap);
6508 n = 0;
6509 break;
6510 }
6511 c = gchar_cursor();
6512 if (c == '{' || c == '}')
6513 {
6514 /* Must have found end/start of class: use it.
6515 * Or found the place to be at. */
6516 if ((c == findc && norm) || (n == 1 && !norm))
6517 {
6518 new_pos = curwin->w_cursor;
6519 pos = &new_pos;
6520 n = 0;
6521 }
6522 /* if no match found at all, we started outside of the
6523 * class and we're inside now. Just go on. */
6524 else if (new_pos.lnum == 0)
6525 {
6526 new_pos = curwin->w_cursor;
6527 pos = &new_pos;
6528 }
6529 /* found start/end of other method: go to match */
6530 else if ((pos = findmatchlimit(cap->oap, findc,
6531 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6532 0)) == NULL)
6533 n = 0;
6534 else
6535 curwin->w_cursor = *pos;
6536 break;
6537 }
6538 }
6539 --n;
6540 }
6541 curwin->w_cursor = old_pos;
6542 if (pos == NULL && new_pos.lnum != 0)
6543 clearopbeep(cap->oap);
6544 }
6545 if (pos != NULL)
6546 {
6547 setpcmark();
6548 curwin->w_cursor = *pos;
6549 curwin->w_set_curswant = TRUE;
6550#ifdef FEAT_FOLDING
6551 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6552 && cap->oap->op_type == OP_NOP)
6553 foldOpenCursor();
6554#endif
6555 }
6556 }
6557
6558 /*
6559 * "[[", "[]", "]]" and "][": move to start or end of function
6560 */
6561 else if (cap->nchar == '[' || cap->nchar == ']')
6562 {
6563 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6564 flag = '{';
6565 else
6566 flag = '}'; /* "][" or "[]" */
6567
6568 curwin->w_set_curswant = TRUE;
6569 /*
6570 * Imitate strange Vi behaviour: When using "]]" with an operator
6571 * we also stop at '}'.
6572 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006573 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 (cap->oap->op_type != OP_NOP
6575 && cap->arg == FORWARD && flag == '{')))
6576 clearopbeep(cap->oap);
6577 else
6578 {
6579 if (cap->oap->op_type == OP_NOP)
6580 beginline(BL_WHITE | BL_FIX);
6581#ifdef FEAT_FOLDING
6582 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6583 foldOpenCursor();
6584#endif
6585 }
6586 }
6587
6588 /*
6589 * "[p", "[P", "]P" and "]p": put with indent adjustment
6590 */
6591 else if (cap->nchar == 'p' || cap->nchar == 'P')
6592 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006593 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006595 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6596 ? FORWARD : BACKWARD;
6597 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006598 int was_visual = VIsual_active;
6599 int line_count = curbuf->b_ml.ml_line_count;
6600 pos_T start, end;
6601
6602 if (VIsual_active)
6603 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006604 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006605 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006606 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006607 curwin->w_cursor = (dir == BACKWARD ? start : end);
6608 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006609# ifdef FEAT_CLIPBOARD
6610 adjust_clip_reg(&regname);
6611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006613
6614 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006615 if (was_visual)
6616 {
6617 VIsual = start;
6618 curwin->w_cursor = end;
6619 if (dir == BACKWARD)
6620 {
6621 /* adjust lines */
6622 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6623 curwin->w_cursor.lnum +=
6624 curbuf->b_ml.ml_line_count - line_count;
6625 }
6626
6627 VIsual_active = TRUE;
6628 if (VIsual_mode == 'V')
6629 {
6630 /* delete visually selected lines */
6631 cap->cmdchar = 'd';
6632 cap->nchar = NUL;
6633 cap->oap->regname = regname;
6634 nv_operator(cap);
6635 do_pending_operator(cap, 0, FALSE);
6636 }
6637 if (VIsual_active)
6638 {
6639 end_visual_mode();
6640 redraw_later(SOME_VALID);
6641 }
6642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
6644 }
6645
6646 /*
6647 * "['", "[`", "]'" and "]`": jump to next mark
6648 */
6649 else if (cap->nchar == '\'' || cap->nchar == '`')
6650 {
6651 pos = &curwin->w_cursor;
6652 for (n = cap->count1; n > 0; --n)
6653 {
6654 prev_pos = *pos;
6655 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6656 cap->nchar == '\'');
6657 if (pos == NULL)
6658 break;
6659 }
6660 if (pos == NULL)
6661 pos = &prev_pos;
6662 nv_cursormark(cap, cap->nchar == '\'', pos);
6663 }
6664
6665#ifdef FEAT_MOUSE
6666 /*
6667 * [ or ] followed by a middle mouse click: put selected text with
6668 * indent adjustment. Any other button just does as usual.
6669 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006670 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 {
6672 (void)do_mouse(cap->oap, cap->nchar,
6673 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6674 cap->count1, PUT_FIXINDENT);
6675 }
6676#endif /* FEAT_MOUSE */
6677
6678#ifdef FEAT_FOLDING
6679 /*
6680 * "[z" and "]z": move to start or end of open fold.
6681 */
6682 else if (cap->nchar == 'z')
6683 {
6684 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6685 cap->count1) == FAIL)
6686 clearopbeep(cap->oap);
6687 }
6688#endif
6689
6690#ifdef FEAT_DIFF
6691 /*
6692 * "[c" and "]c": move to next or previous diff-change.
6693 */
6694 else if (cap->nchar == 'c')
6695 {
6696 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6697 cap->count1) == FAIL)
6698 clearopbeep(cap->oap);
6699 }
6700#endif
6701
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006702#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006703 /*
6704 * "[s", "[S", "]s" and "]S": move to next spell error.
6705 */
6706 else if (cap->nchar == 's' || cap->nchar == 'S')
6707 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006708 setpcmark();
6709 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006710 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6711 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006712 {
6713 clearopbeep(cap->oap);
6714 break;
6715 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006716 else
6717 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006718# ifdef FEAT_FOLDING
6719 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6720 foldOpenCursor();
6721# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006722 }
6723#endif
6724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 /* Not a valid cap->nchar. */
6726 else
6727 clearopbeep(cap->oap);
6728}
6729
6730/*
6731 * Handle Normal mode "%" command.
6732 */
6733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006734nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006737#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 linenr_T lnum = curwin->w_cursor.lnum;
6739#endif
6740
6741 cap->oap->inclusive = TRUE;
6742 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6743 {
6744 if (cap->count0 > 100)
6745 clearopbeep(cap->oap);
6746 else
6747 {
6748 cap->oap->motion_type = MLINE;
6749 setpcmark();
6750 /* Round up, so CTRL-G will give same value. Watch out for a
6751 * large line count, the line number must not go negative! */
6752 if (curbuf->b_ml.ml_line_count > 1000000)
6753 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6754 / 100L * cap->count0;
6755 else
6756 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6757 cap->count0 + 99L) / 100L;
6758 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6759 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6760 beginline(BL_SOL | BL_FIX);
6761 }
6762 }
6763 else /* "%" : go to matching paren */
6764 {
6765 cap->oap->motion_type = MCHAR;
6766 cap->oap->use_reg_one = TRUE;
6767 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6768 clearopbeep(cap->oap);
6769 else
6770 {
6771 setpcmark();
6772 curwin->w_cursor = *pos;
6773 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 }
6777 }
6778#ifdef FEAT_FOLDING
6779 if (cap->oap->op_type == OP_NOP
6780 && lnum != curwin->w_cursor.lnum
6781 && (fdo_flags & FDO_PERCENT)
6782 && KeyTyped)
6783 foldOpenCursor();
6784#endif
6785}
6786
6787/*
6788 * Handle "(" and ")" commands.
6789 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6790 */
6791 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006792nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794 cap->oap->motion_type = MCHAR;
6795 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006796 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6797 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 curwin->w_set_curswant = TRUE;
6799
6800 if (findsent(cap->arg, cap->count1) == FAIL)
6801 clearopbeep(cap->oap);
6802 else
6803 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006804 /* Don't leave the cursor on the NUL past end of line. */
6805 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807#ifdef FEAT_FOLDING
6808 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6809 foldOpenCursor();
6810#endif
6811 }
6812}
6813
6814/*
6815 * "m" command: Mark a position.
6816 */
6817 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006818nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
6820 if (!checkclearop(cap->oap))
6821 {
6822 if (setmark(cap->nchar) == FAIL)
6823 clearopbeep(cap->oap);
6824 }
6825}
6826
6827/*
6828 * "{" and "}" commands.
6829 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6830 */
6831 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006832nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833{
6834 cap->oap->motion_type = MCHAR;
6835 cap->oap->inclusive = FALSE;
6836 cap->oap->use_reg_one = TRUE;
6837 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006838 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 clearopbeep(cap->oap);
6840 else
6841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843#ifdef FEAT_FOLDING
6844 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6845 foldOpenCursor();
6846#endif
6847 }
6848}
6849
6850/*
6851 * "u" command: Undo or make lower case.
6852 */
6853 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006854nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006856 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 {
6858 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6859 cap->cmdchar = 'g';
6860 cap->nchar = 'u';
6861 nv_operator(cap);
6862 }
6863 else
6864 nv_kundo(cap);
6865}
6866
6867/*
6868 * <Undo> command.
6869 */
6870 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006871nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
6873 if (!checkclearopq(cap->oap))
6874 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02006875#ifdef FEAT_JOB_CHANNEL
6876 if (bt_prompt(curbuf))
6877 {
6878 clearopbeep(cap->oap);
6879 return;
6880 }
6881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 u_undo((int)cap->count1);
6883 curwin->w_set_curswant = TRUE;
6884 }
6885}
6886
6887/*
6888 * Handle the "r" command.
6889 */
6890 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006891nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 char_u *ptr;
6894 int had_ctrl_v;
6895 long n;
6896
6897 if (checkclearop(cap->oap))
6898 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006899#ifdef FEAT_JOB_CHANNEL
6900 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6901 {
6902 clearopbeep(cap->oap);
6903 return;
6904 }
6905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
6907 /* get another character */
6908 if (cap->nchar == Ctrl_V)
6909 {
6910 had_ctrl_v = Ctrl_V;
6911 cap->nchar = get_literal();
6912 /* Don't redo a multibyte character with CTRL-V. */
6913 if (cap->nchar > DEL)
6914 had_ctrl_v = NUL;
6915 }
6916 else
6917 had_ctrl_v = NUL;
6918
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006919 /* Abort if the character is a special key. */
6920 if (IS_SPECIAL(cap->nchar))
6921 {
6922 clearopbeep(cap->oap);
6923 return;
6924 }
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 /* Visual mode "r" */
6927 if (VIsual_active)
6928 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006929 if (got_int)
6930 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006931 if (had_ctrl_v)
6932 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006933 /* Use a special (negative) number to make a difference between a
6934 * literal CR or NL and a line break. */
6935 if (cap->nchar == CAR)
6936 cap->nchar = REPLACE_CR_NCHAR;
6937 else if (cap->nchar == NL)
6938 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01006939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 nv_operator(cap);
6941 return;
6942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 /* Break tabs, etc. */
6945 if (virtual_active())
6946 {
6947 if (u_save_cursor() == FAIL)
6948 return;
6949 if (gchar_cursor() == NUL)
6950 {
6951 /* Add extra space and put the cursor on the first one. */
6952 coladvance_force((colnr_T)(getviscol() + cap->count1));
6953 curwin->w_cursor.col -= cap->count1;
6954 }
6955 else if (gchar_cursor() == TAB)
6956 coladvance_force(getviscol());
6957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006959 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006961 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006962 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {
6964 clearopbeep(cap->oap);
6965 return;
6966 }
6967
6968 /*
6969 * Replacing with a TAB is done by edit() when it is complicated because
6970 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6971 * Other characters are done below to avoid problems with things like
6972 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6973 */
6974 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6975 {
6976 stuffnumReadbuff(cap->count1);
6977 stuffcharReadbuff('R');
6978 stuffcharReadbuff('\t');
6979 stuffcharReadbuff(ESC);
6980 return;
6981 }
6982
6983 /* save line for undo */
6984 if (u_save_cursor() == FAIL)
6985 return;
6986
6987 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6988 {
6989 /*
6990 * Replace character(s) by a single newline.
6991 * Strange vi behaviour: Only one newline is inserted.
6992 * Delete the characters here.
6993 * Insert the newline with an insert command, takes care of
6994 * autoindent. The insert command depends on being on the last
6995 * character of a line or not.
6996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 (void)del_chars(cap->count1, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 stuffcharReadbuff('\r');
6999 stuffcharReadbuff(ESC);
7000
7001 /* Give 'r' to edit(), to get the redo command right. */
7002 invoke_edit(cap, TRUE, 'r', FALSE);
7003 }
7004 else
7005 {
7006 prep_redo(cap->oap->regname, cap->count1,
7007 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7008
7009 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (has_mbyte)
7011 {
7012 int old_State = State;
7013
7014 if (cap->ncharC1 != 0)
7015 AppendCharToRedobuff(cap->ncharC1);
7016 if (cap->ncharC2 != 0)
7017 AppendCharToRedobuff(cap->ncharC2);
7018
7019 /* This is slow, but it handles replacing a single-byte with a
7020 * multi-byte and the other way around. Also handles adding
7021 * composing characters for utf-8. */
7022 for (n = cap->count1; n > 0; --n)
7023 {
7024 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007025 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7026 {
7027 int c = ins_copychar(curwin->w_cursor.lnum
7028 + (cap->nchar == Ctrl_Y ? -1 : 1));
7029 if (c != NUL)
7030 ins_char(c);
7031 else
7032 /* will be decremented further down */
7033 ++curwin->w_cursor.col;
7034 }
7035 else
7036 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 State = old_State;
7038 if (cap->ncharC1 != 0)
7039 ins_char(cap->ncharC1);
7040 if (cap->ncharC2 != 0)
7041 ins_char(cap->ncharC2);
7042 }
7043 }
7044 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {
7046 /*
7047 * Replace the characters within one line.
7048 */
7049 for (n = cap->count1; n > 0; --n)
7050 {
7051 /*
7052 * Get ptr again, because u_save and/or showmatch() will have
7053 * released the line. At the same time we let know that the
7054 * line will be changed.
7055 */
7056 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007057 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7058 {
7059 int c = ins_copychar(curwin->w_cursor.lnum
7060 + (cap->nchar == Ctrl_Y ? -1 : 1));
7061 if (c != NUL)
7062 ptr[curwin->w_cursor.col] = c;
7063 }
7064 else
7065 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 if (p_sm && msg_silent == 0)
7067 showmatch(cap->nchar);
7068 ++curwin->w_cursor.col;
7069 }
7070#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007071 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007073 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
Bram Moolenaar009b2592004-10-24 19:18:58 +00007075 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007076 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007078 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 }
7080#endif
7081
7082 /* mark the buffer as changed and prepare for displaying */
7083 changed_bytes(curwin->w_cursor.lnum,
7084 (colnr_T)(curwin->w_cursor.col - cap->count1));
7085 }
7086 --curwin->w_cursor.col; /* cursor on the last replaced char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 /* if the character on the left of the current cursor is a multi-byte
7088 * character, move two characters left */
7089 if (has_mbyte)
7090 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 curbuf->b_op_end = curwin->w_cursor;
7092 curwin->w_set_curswant = TRUE;
7093 set_last_insert(cap->nchar);
7094 }
7095}
7096
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097/*
7098 * 'o': Exchange start and end of Visual area.
7099 * 'O': same, but in block mode exchange left and right corners.
7100 */
7101 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007102v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 pos_T old_cursor;
7105 colnr_T left, right;
7106
7107 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7108 {
7109 old_cursor = curwin->w_cursor;
7110 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7111 curwin->w_cursor.lnum = VIsual.lnum;
7112 coladvance(left);
7113 VIsual = curwin->w_cursor;
7114
7115 curwin->w_cursor.lnum = old_cursor.lnum;
7116 curwin->w_curswant = right;
7117 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7118 * right one column */
7119 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7120 ++curwin->w_curswant;
7121 coladvance(curwin->w_curswant);
7122 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007124 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 {
7126 curwin->w_cursor.lnum = VIsual.lnum;
7127 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7128 ++right;
7129 coladvance(right);
7130 VIsual = curwin->w_cursor;
7131
7132 curwin->w_cursor.lnum = old_cursor.lnum;
7133 coladvance(left);
7134 curwin->w_curswant = left;
7135 }
7136 }
7137 else
7138 {
7139 old_cursor = curwin->w_cursor;
7140 curwin->w_cursor = VIsual;
7141 VIsual = old_cursor;
7142 curwin->w_set_curswant = TRUE;
7143 }
7144}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146/*
7147 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7148 */
7149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007150nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 if (VIsual_active) /* "R" is replace lines */
7153 {
7154 cap->cmdchar = 'c';
7155 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007156 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 VIsual_mode = 'V';
7158 nv_operator(cap);
7159 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007160 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 {
7162 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007163 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 else
7165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 if (virtual_active())
7167 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7169 }
7170 }
7171}
7172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173/*
7174 * "gr".
7175 */
7176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007177nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 if (VIsual_active)
7180 {
7181 cap->cmdchar = 'r';
7182 cap->nchar = cap->extra_char;
7183 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7184 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007185 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 {
7187 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007188 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 else
7190 {
7191 if (cap->extra_char == Ctrl_V) /* get another character */
7192 cap->extra_char = get_literal();
7193 stuffcharReadbuff(cap->extra_char);
7194 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 if (virtual_active())
7196 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 invoke_edit(cap, TRUE, 'v', FALSE);
7198 }
7199 }
7200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
7202/*
7203 * Swap case for "~" command, when it does not work like an operator.
7204 */
7205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007206n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207{
7208 long n;
7209 pos_T startpos;
7210 int did_change = 0;
7211#ifdef FEAT_NETBEANS_INTG
7212 pos_T pos;
7213 char_u *ptr;
7214 int count;
7215#endif
7216
7217 if (checkclearopq(cap->oap))
7218 return;
7219
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007220 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 {
7222 clearopbeep(cap->oap);
7223 return;
7224 }
7225
7226 prep_redo_cmd(cap);
7227
7228 if (u_save_cursor() == FAIL)
7229 return;
7230
7231 startpos = curwin->w_cursor;
7232#ifdef FEAT_NETBEANS_INTG
7233 pos = startpos;
7234#endif
7235 for (n = cap->count1; n > 0; --n)
7236 {
7237 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7238 inc_cursor();
7239 if (gchar_cursor() == NUL)
7240 {
7241 if (vim_strchr(p_ww, '~') != NULL
7242 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7243 {
7244#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007245 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {
7247 if (did_change)
7248 {
7249 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007250 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007251 netbeans_removed(curbuf, pos.lnum, pos.col,
7252 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007254 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 }
7256 pos.col = 0;
7257 pos.lnum++;
7258 }
7259#endif
7260 ++curwin->w_cursor.lnum;
7261 curwin->w_cursor.col = 0;
7262 if (n > 1)
7263 {
7264 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7265 break;
7266 u_clearline();
7267 }
7268 }
7269 else
7270 break;
7271 }
7272 }
7273#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007274 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
7276 ptr = ml_get(pos.lnum);
7277 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007278 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7279 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281#endif
7282
7283
7284 check_cursor();
7285 curwin->w_set_curswant = TRUE;
7286 if (did_change)
7287 {
7288 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7289 0L);
7290 curbuf->b_op_start = startpos;
7291 curbuf->b_op_end = curwin->w_cursor;
7292 if (curbuf->b_op_end.col > 0)
7293 --curbuf->b_op_end.col;
7294 }
7295}
7296
7297/*
7298 * Move cursor to mark.
7299 */
7300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007301nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 if (check_mark(pos) == FAIL)
7304 clearop(cap->oap);
7305 else
7306 {
7307 if (cap->cmdchar == '\''
7308 || cap->cmdchar == '`'
7309 || cap->cmdchar == '['
7310 || cap->cmdchar == ']')
7311 setpcmark();
7312 curwin->w_cursor = *pos;
7313 if (flag)
7314 beginline(BL_WHITE | BL_FIX);
7315 else
7316 check_cursor();
7317 }
7318 cap->oap->motion_type = flag ? MLINE : MCHAR;
7319 if (cap->cmdchar == '`')
7320 cap->oap->use_reg_one = TRUE;
7321 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7322 curwin->w_set_curswant = TRUE;
7323}
7324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325/*
7326 * Handle commands that are operators in Visual mode.
7327 */
7328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007329v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
7331 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7332
7333 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007334 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (isupper(cap->cmdchar))
7336 {
7337 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007338 {
7339 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7343 curwin->w_curswant = MAXCOL;
7344 }
7345 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7346 nv_operator(cap);
7347}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
7349/*
7350 * "s" and "S" commands.
7351 */
7352 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007353nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007355#ifdef FEAT_TERMINAL
7356 /* When showing output of term_dumpdiff() swap the top and botom. */
7357 if (term_swap_diff() == OK)
7358 return;
7359#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02007360#ifdef FEAT_JOB_CHANNEL
7361 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7362 {
7363 clearopbeep(cap->oap);
7364 return;
7365 }
7366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7368 {
7369 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007370 {
7371 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 cap->cmdchar = 'c';
7375 nv_operator(cap);
7376 }
7377 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 nv_optrans(cap);
7379}
7380
7381/*
7382 * Abbreviated commands.
7383 */
7384 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007385nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386{
7387 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7388 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7389
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 /* in Visual mode these commands are operators */
7391 if (VIsual_active)
7392 v_visop(cap);
7393 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 nv_optrans(cap);
7395}
7396
7397/*
7398 * Translate a command into another command.
7399 */
7400 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007401nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7404 (char_u *)"d$", (char_u *)"c$",
7405 (char_u *)"cl", (char_u *)"cc",
7406 (char_u *)"yy", (char_u *)":s\r"};
7407 static char_u *str = (char_u *)"xXDCsSY&";
7408
7409 if (!checkclearopq(cap->oap))
7410 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007411 /* In Vi "2D" doesn't delete the next line. Can't translate it
7412 * either, because "2." should also not use the count. */
7413 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7414 {
7415 cap->oap->start = curwin->w_cursor;
7416 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007417#ifdef FEAT_EVAL
7418 set_op_var(OP_DELETE);
7419#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007420 cap->count1 = 1;
7421 nv_dollar(cap);
7422 finish_op = TRUE;
7423 ResetRedobuff();
7424 AppendCharToRedobuff('D');
7425 }
7426 else
7427 {
7428 if (cap->count0)
7429 stuffnumReadbuff(cap->count0);
7430 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 }
7433 cap->opcount = 0;
7434}
7435
7436/*
7437 * "'" and "`" commands. Also for "g'" and "g`".
7438 * cap->arg is TRUE for "'" and "g'".
7439 */
7440 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007441nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
7443 pos_T *pos;
7444 int c;
7445#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007446 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7448#endif
7449
7450 if (cap->cmdchar == 'g')
7451 c = cap->extra_char;
7452 else
7453 c = cap->nchar;
7454 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7455 if (pos == (pos_T *)-1) /* jumped to other file */
7456 {
7457 if (cap->arg)
7458 {
7459 check_cursor_lnum();
7460 beginline(BL_WHITE | BL_FIX);
7461 }
7462 else
7463 check_cursor();
7464 }
7465 else
7466 nv_cursormark(cap, cap->arg, pos);
7467
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 /* May need to clear the coladd that a mark includes. */
7469 if (!virtual_active())
7470 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007471 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472#ifdef FEAT_FOLDING
7473 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007474 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007475 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 && (fdo_flags & FDO_MARK)
7477 && old_KeyTyped)
7478 foldOpenCursor();
7479#endif
7480}
7481
7482/*
7483 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7484 */
7485 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007486nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
7488#ifdef FEAT_JUMPLIST
7489 pos_T *pos;
7490# ifdef FEAT_FOLDING
7491 linenr_T lnum = curwin->w_cursor.lnum;
7492 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7493# endif
7494
7495 if (!checkclearopq(cap->oap))
7496 {
7497 if (cap->cmdchar == 'g')
7498 pos = movechangelist((int)cap->count1);
7499 else
7500 pos = movemark((int)cap->count1);
7501 if (pos == (pos_T *)-1) /* jump to other file */
7502 {
7503 curwin->w_set_curswant = TRUE;
7504 check_cursor();
7505 }
7506 else if (pos != NULL) /* can jump */
7507 nv_cursormark(cap, FALSE, pos);
7508 else if (cap->cmdchar == 'g')
7509 {
7510 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007511 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007513 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007515 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 }
7517 else
7518 clearopbeep(cap->oap);
7519# ifdef FEAT_FOLDING
7520 if (cap->oap->op_type == OP_NOP
7521 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7522 && (fdo_flags & FDO_MARK)
7523 && old_KeyTyped)
7524 foldOpenCursor();
7525# endif
7526 }
7527#else
7528 clearopbeep(cap->oap);
7529#endif
7530}
7531
7532/*
7533 * Handle '"' command.
7534 */
7535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007536nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 if (checkclearop(cap->oap))
7539 return;
7540#ifdef FEAT_EVAL
7541 if (cap->nchar == '=')
7542 cap->nchar = get_expr_register();
7543#endif
7544 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7545 {
7546 cap->oap->regname = cap->nchar;
7547 cap->opcount = cap->count0; /* remember count before '"' */
7548#ifdef FEAT_EVAL
7549 set_reg_var(cap->oap->regname);
7550#endif
7551 }
7552 else
7553 clearopbeep(cap->oap);
7554}
7555
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556/*
7557 * Handle "v", "V" and "CTRL-V" commands.
7558 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7559 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007560 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 */
7562 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007563nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007565 if (cap->cmdchar == Ctrl_Q)
7566 cap->cmdchar = Ctrl_V;
7567
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7569 * characterwise, linewise, or blockwise. */
7570 if (cap->oap->op_type != OP_NOP)
7571 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007572 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 finish_op = FALSE; /* operator doesn't finish now but later */
7574 return;
7575 }
7576
7577 VIsual_select = cap->arg;
7578 if (VIsual_active) /* change Visual mode */
7579 {
7580 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7581 end_visual_mode();
7582 else /* toggle char/block mode */
7583 { /* or char/line mode */
7584 VIsual_mode = cap->cmdchar;
7585 showmode();
7586 }
7587 redraw_curbuf_later(INVERTED); /* update the inversion */
7588 }
7589 else /* start Visual mode */
7590 {
7591 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007592 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007594 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 VIsual = curwin->w_cursor;
7596
7597 VIsual_active = TRUE;
7598 VIsual_reselect = TRUE;
7599 if (!cap->arg)
7600 /* start Select mode when 'selectmode' contains "cmd" */
7601 may_start_select('c');
7602#ifdef FEAT_MOUSE
7603 setmouse();
7604#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007605 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 redraw_cmdline = TRUE; /* show visual mode later */
7607 /*
7608 * For V and ^V, we multiply the number of lines even if there
7609 * was only one -- webb
7610 */
7611 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7612 {
7613 curwin->w_cursor.lnum +=
7614 resel_VIsual_line_count * cap->count0 - 1;
7615 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7616 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7617 }
7618 VIsual_mode = resel_VIsual_mode;
7619 if (VIsual_mode == 'v')
7620 {
7621 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007622 {
7623 validate_virtcol();
7624 curwin->w_curswant = curwin->w_virtcol
7625 + resel_VIsual_vcol * cap->count0 - 1;
7626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007628 curwin->w_curswant = resel_VIsual_vcol;
7629 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007631 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 {
7633 curwin->w_curswant = MAXCOL;
7634 coladvance((colnr_T)MAXCOL);
7635 }
7636 else if (VIsual_mode == Ctrl_V)
7637 {
7638 validate_virtcol();
7639 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007640 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 coladvance(curwin->w_curswant);
7642 }
7643 else
7644 curwin->w_set_curswant = TRUE;
7645 redraw_curbuf_later(INVERTED); /* show the inversion */
7646 }
7647 else
7648 {
7649 if (!cap->arg)
7650 /* start Select mode when 'selectmode' contains "cmd" */
7651 may_start_select('c');
7652 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007653 if (VIsual_mode != 'V' && *p_sel == 'e')
7654 ++cap->count1; /* include one more char */
7655 if (cap->count0 > 0 && --cap->count1 > 0)
7656 {
7657 /* With a count select that many characters or lines. */
7658 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7659 nv_right(cap);
7660 else if (VIsual_mode == 'V')
7661 nv_down(cap);
7662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
7664 }
7665}
7666
7667/*
7668 * Start selection for Shift-movement keys.
7669 */
7670 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007671start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
7673 /* if 'selectmode' contains "key", start Select mode */
7674 may_start_select('k');
7675 n_start_visual_mode('v');
7676}
7677
7678/*
7679 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7680 */
7681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007682may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 VIsual_select = (stuff_empty() && typebuf_typed()
7685 && (vim_strchr(p_slm, c) != NULL));
7686}
7687
7688/*
7689 * Start Visual mode "c".
7690 * Should set VIsual_select before calling this.
7691 */
7692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007693n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007695#ifdef FEAT_CONCEAL
7696 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007697 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007698#endif
7699
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 VIsual_mode = c;
7701 VIsual_active = TRUE;
7702 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007703
7704 // Corner case: the 0 position in a tab may change when going into
7705 // virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007707 {
7708 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 VIsual = curwin->w_cursor;
7712
7713#ifdef FEAT_FOLDING
7714 foldAdjustVisual();
7715#endif
7716
7717#ifdef FEAT_MOUSE
7718 setmouse();
7719#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007720#ifdef FEAT_CONCEAL
7721 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007722 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007723#endif
7724
Bram Moolenaar09df3122006-01-23 22:23:09 +00007725 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 redraw_cmdline = TRUE; /* show visual mode later */
7727#ifdef FEAT_CLIPBOARD
7728 /* Make sure the clipboard gets updated. Needed because start and
7729 * end may still be the same, and the selection needs to be owned */
7730 clip_star.vmode = NUL;
7731#endif
7732
7733 /* Only need to redraw this line, unless still need to redraw an old
7734 * Visual area (when 'lazyredraw' is set). */
7735 if (curwin->w_redr_type < INVERTED)
7736 {
7737 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7738 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7739 }
7740}
7741
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742
7743/*
7744 * CTRL-W: Window commands
7745 */
7746 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007747nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007749 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007750 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007751 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007752 cap->cmdchar = ':';
7753 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007754 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007755 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007756 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758}
7759
7760/*
7761 * CTRL-Z: Suspend
7762 */
7763 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007764nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765{
7766 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 if (VIsual_active)
7768 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 do_cmdline_cmd((char_u *)"st");
7770}
7771
7772/*
7773 * Commands starting with "g".
7774 */
7775 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007776nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 int i;
7781 int flag = FALSE;
7782
7783 switch (cap->nchar)
7784 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007785 case Ctrl_A:
7786 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787#ifdef MEM_PROFILE
7788 /*
7789 * "g^A": dump log of used memory.
7790 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007791 if (!VIsual_active && cap->nchar == Ctrl_A)
7792 vim_mem_profile_dump();
7793 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007795 /*
7796 * "g^A/g^X": sequentially increment visually selected region
7797 */
7798 if (VIsual_active)
7799 {
7800 cap->arg = TRUE;
7801 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007802 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007803 nv_addsub(cap);
7804 }
7805 else
7806 clearopbeep(oap);
7807 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 /*
7810 * "gR": Enter virtual replace mode.
7811 */
7812 case 'R':
7813 cap->arg = TRUE;
7814 nv_Replace(cap);
7815 break;
7816
7817 case 'r':
7818 nv_vreplace(cap);
7819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820
7821 case '&':
7822 do_cmdline_cmd((char_u *)"%s//~/&");
7823 break;
7824
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 /*
7826 * "gv": Reselect the previous Visual area. If Visual already active,
7827 * exchange previous and current Visual area.
7828 */
7829 case 'v':
7830 if (checkclearop(oap))
7831 break;
7832
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007833 if ( curbuf->b_visual.vi_start.lnum == 0
7834 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7835 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 beep_flush();
7837 else
7838 {
7839 /* set w_cursor to the start of the Visual area, tpos to the end */
7840 if (VIsual_active)
7841 {
7842 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007843 VIsual_mode = curbuf->b_visual.vi_mode;
7844 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845# ifdef FEAT_EVAL
7846 curbuf->b_visual_mode_eval = i;
7847# endif
7848 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007849 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7850 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007852 tpos = curbuf->b_visual.vi_end;
7853 curbuf->b_visual.vi_end = curwin->w_cursor;
7854 curwin->w_cursor = curbuf->b_visual.vi_start;
7855 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
7857 else
7858 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007859 VIsual_mode = curbuf->b_visual.vi_mode;
7860 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7861 tpos = curbuf->b_visual.vi_end;
7862 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 }
7864
7865 VIsual_active = TRUE;
7866 VIsual_reselect = TRUE;
7867
7868 /* Set Visual to the start and w_cursor to the end of the Visual
7869 * area. Make sure they are on an existing character. */
7870 check_cursor();
7871 VIsual = curwin->w_cursor;
7872 curwin->w_cursor = tpos;
7873 check_cursor();
7874 update_topline();
7875 /*
7876 * When called from normal "g" command: start Select mode when
7877 * 'selectmode' contains "cmd". When called for K_SELECT, always
7878 * start Select mode.
7879 */
7880 if (cap->arg)
7881 VIsual_select = TRUE;
7882 else
7883 may_start_select('c');
7884#ifdef FEAT_MOUSE
7885 setmouse();
7886#endif
7887#ifdef FEAT_CLIPBOARD
7888 /* Make sure the clipboard gets updated. Needed because start and
7889 * end are still the same, and the selection needs to be owned */
7890 clip_star.vmode = NUL;
7891#endif
7892 redraw_curbuf_later(INVERTED);
7893 showmode();
7894 }
7895 break;
7896 /*
7897 * "gV": Don't reselect the previous Visual area after a Select mode
7898 * mapping of menu.
7899 */
7900 case 'V':
7901 VIsual_reselect = FALSE;
7902 break;
7903
7904 /*
7905 * "gh": start Select mode.
7906 * "gH": start Select line mode.
7907 * "g^H": start Select block mode.
7908 */
7909 case K_BS:
7910 cap->nchar = Ctrl_H;
7911 /* FALLTHROUGH */
7912 case 'h':
7913 case 'H':
7914 case Ctrl_H:
7915# ifdef EBCDIC
7916 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7917 if (cap->nchar == Ctrl_H)
7918 cap->cmdchar = Ctrl_V;
7919 else
7920# endif
7921 cap->cmdchar = cap->nchar + ('v' - 'h');
7922 cap->arg = TRUE;
7923 nv_visual(cap);
7924 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007925
7926 /* "gn", "gN" visually select next/previous search match
7927 * "gn" selects next match
7928 * "gN" selects previous match
7929 */
7930 case 'N':
7931 case 'n':
7932 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007933 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007934 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
7936 /*
7937 * "gj" and "gk" two new funny movement keys -- up and down
7938 * movement based on *screen* line rather than *file* line.
7939 */
7940 case 'j':
7941 case K_DOWN:
7942 /* with 'nowrap' it works just like the normal "j" command; also when
7943 * in a closed fold */
7944 if (!curwin->w_p_wrap
7945#ifdef FEAT_FOLDING
7946 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7947#endif
7948 )
7949 {
7950 oap->motion_type = MLINE;
7951 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7952 }
7953 else
7954 i = nv_screengo(oap, FORWARD, cap->count1);
7955 if (i == FAIL)
7956 clearopbeep(oap);
7957 break;
7958
7959 case 'k':
7960 case K_UP:
7961 /* with 'nowrap' it works just like the normal "k" command; also when
7962 * in a closed fold */
7963 if (!curwin->w_p_wrap
7964#ifdef FEAT_FOLDING
7965 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7966#endif
7967 )
7968 {
7969 oap->motion_type = MLINE;
7970 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7971 }
7972 else
7973 i = nv_screengo(oap, BACKWARD, cap->count1);
7974 if (i == FAIL)
7975 clearopbeep(oap);
7976 break;
7977
7978 /*
7979 * "gJ": join two lines without inserting a space.
7980 */
7981 case 'J':
7982 nv_join(cap);
7983 break;
7984
7985 /*
7986 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7987 * "gm": middle of "g0" and "g$".
7988 */
7989 case '^':
7990 flag = TRUE;
7991 /* FALLTHROUGH */
7992
7993 case '0':
7994 case 'm':
7995 case K_HOME:
7996 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 oap->motion_type = MCHAR;
7998 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007999 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008001 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 int width2 = width1 + curwin_col_off2();
8003
8004 validate_virtcol();
8005 i = 0;
8006 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8007 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8008 }
8009 else
8010 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008011 /* Go to the middle of the screen line. When 'number' or
8012 * 'relativenumber' is on and lines are wrapping the middle can be more
8013 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008015 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 + ((curwin->w_p_wrap && i > 0)
8017 ? curwin_col_off2() : 0)) / 2;
8018 coladvance((colnr_T)i);
8019 if (flag)
8020 {
8021 do
8022 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008023 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008024 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 }
8026 curwin->w_set_curswant = TRUE;
8027 break;
8028
8029 case '_':
8030 /* "g_": to the last non-blank character in the line or <count> lines
8031 * downward. */
8032 cap->oap->motion_type = MCHAR;
8033 cap->oap->inclusive = TRUE;
8034 curwin->w_curswant = MAXCOL;
8035 if (cursor_down((long)(cap->count1 - 1),
8036 cap->oap->op_type == OP_NOP) == FAIL)
8037 clearopbeep(cap->oap);
8038 else
8039 {
8040 char_u *ptr = ml_get_curline();
8041
8042 /* In Visual mode we may end up after the line. */
8043 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8044 --curwin->w_cursor.col;
8045
8046 /* Decrease the cursor column until it's on a non-blank. */
8047 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008048 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 --curwin->w_cursor.col;
8050 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008051 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 }
8053 break;
8054
8055 case '$':
8056 case K_END:
8057 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {
8059 int col_off = curwin_col_off();
8060
8061 oap->motion_type = MCHAR;
8062 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008063 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {
8065 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8066 if (cap->count1 == 1)
8067 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008068 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 int width2 = width1 + curwin_col_off2();
8070
8071 validate_virtcol();
8072 i = width1 - 1;
8073 if (curwin->w_virtcol >= (colnr_T)width1)
8074 i += ((curwin->w_virtcol - width1) / width2 + 1)
8075 * width2;
8076 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008077
8078 /* Make sure we stick in this column. */
8079 validate_virtcol();
8080 curwin->w_curswant = curwin->w_virtcol;
8081 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8083 {
8084 /*
8085 * Check for landing on a character that got split at
8086 * the end of the line. We do not want to advance to
8087 * the next screen line.
8088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 if (curwin->w_virtcol > (colnr_T)i)
8090 --curwin->w_cursor.col;
8091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 }
8093 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8094 clearopbeep(oap);
8095 }
8096 else
8097 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008098 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008100
8101 /* Make sure we stick in this column. */
8102 validate_virtcol();
8103 curwin->w_curswant = curwin->w_virtcol;
8104 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 }
8106 }
8107 break;
8108
8109 /*
8110 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8111 */
8112 case '*':
8113 case '#':
8114#if POUND != '#'
8115 case POUND: /* pound sign (sometimes equal to '#') */
8116#endif
8117 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8118 case ']': /* :tselect for current identifier */
8119 nv_ident(cap);
8120 break;
8121
8122 /*
8123 * ge and gE: go back to end of word
8124 */
8125 case 'e':
8126 case 'E':
8127 oap->motion_type = MCHAR;
8128 curwin->w_set_curswant = TRUE;
8129 oap->inclusive = TRUE;
8130 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8131 clearopbeep(oap);
8132 break;
8133
8134 /*
8135 * "g CTRL-G": display info about cursor position
8136 */
8137 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008138 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 break;
8140
8141 /*
8142 * "gi": start Insert at the last position.
8143 */
8144 case 'i':
8145 if (curbuf->b_last_insert.lnum != 0)
8146 {
8147 curwin->w_cursor = curbuf->b_last_insert;
8148 check_cursor_lnum();
8149 i = (int)STRLEN(ml_get_curline());
8150 if (curwin->w_cursor.col > (colnr_T)i)
8151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 if (virtual_active())
8153 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 curwin->w_cursor.col = i;
8155 }
8156 }
8157 cap->cmdchar = 'i';
8158 nv_edit(cap);
8159 break;
8160
8161 /*
8162 * "gI": Start insert in column 1.
8163 */
8164 case 'I':
8165 beginline(0);
8166 if (!checkclearopq(oap))
8167 invoke_edit(cap, FALSE, 'g', FALSE);
8168 break;
8169
8170#ifdef FEAT_SEARCHPATH
8171 /*
8172 * "gf": goto file, edit file under cursor
8173 * "]f" and "[f": can also be used.
8174 */
8175 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008176 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 nv_gotofile(cap);
8178 break;
8179#endif
8180
8181 /* "g'm" and "g`m": jump to mark without setting pcmark */
8182 case '\'':
8183 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008184 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 case '`':
8186 nv_gomark(cap);
8187 break;
8188
8189 /*
8190 * "gs": Goto sleep.
8191 */
8192 case 's':
8193 do_sleep(cap->count1 * 1000L);
8194 break;
8195
8196 /*
8197 * "ga": Display the ascii value of the character under the
8198 * cursor. It is displayed in decimal, hex, and octal. -- webb
8199 */
8200 case 'a':
8201 do_ascii(NULL);
8202 break;
8203
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 /*
8205 * "g8": Display the bytes used for the UTF-8 character under the
8206 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008207 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 */
8209 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008210 if (cap->count0 == 8)
8211 utf_find_illegal();
8212 else
8213 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215
Bram Moolenaar60402d62017-04-20 18:54:50 +02008216 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008217 case '<':
8218 show_sb_text();
8219 break;
8220
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 /*
8222 * "gg": Goto the first line in file. With a count it goes to
8223 * that line number like for "G". -- webb
8224 */
8225 case 'g':
8226 cap->arg = FALSE;
8227 nv_goto(cap);
8228 break;
8229
8230 /*
8231 * Two-character operators:
8232 * "gq" Format text
8233 * "gw" Format text and keep cursor position
8234 * "g~" Toggle the case of the text.
8235 * "gu" Change text to lower case.
8236 * "gU" Change text to upper case.
8237 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008238 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 */
8240 case 'q':
8241 case 'w':
8242 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008243 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 case '~':
8245 case 'u':
8246 case 'U':
8247 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008248 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 nv_operator(cap);
8250 break;
8251
8252 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008253 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 * current function
8255 * "gD": idem, but in the current file.
8256 */
8257 case 'd':
8258 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008259 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 break;
8261
8262#ifdef FEAT_MOUSE
8263 /*
8264 * g<*Mouse> : <C-*mouse>
8265 */
8266 case K_MIDDLEMOUSE:
8267 case K_MIDDLEDRAG:
8268 case K_MIDDLERELEASE:
8269 case K_LEFTMOUSE:
8270 case K_LEFTDRAG:
8271 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008272 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 case K_RIGHTMOUSE:
8274 case K_RIGHTDRAG:
8275 case K_RIGHTRELEASE:
8276 case K_X1MOUSE:
8277 case K_X1DRAG:
8278 case K_X1RELEASE:
8279 case K_X2MOUSE:
8280 case K_X2DRAG:
8281 case K_X2RELEASE:
8282 mod_mask = MOD_MASK_CTRL;
8283 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8284 break;
8285#endif
8286
8287 case K_IGNORE:
8288 break;
8289
8290 /*
8291 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8292 */
8293 case 'p':
8294 case 'P':
8295 nv_put(cap);
8296 break;
8297
8298#ifdef FEAT_BYTEOFF
8299 /* "go": goto byte count from start of buffer */
8300 case 'o':
8301 goto_byte(cap->count0);
8302 break;
8303#endif
8304
8305 /* "gQ": improved Ex mode */
8306 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008307 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {
8309 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008310 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 break;
8312 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008313
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (!checkclearopq(oap))
8315 do_exmode(TRUE);
8316 break;
8317
8318#ifdef FEAT_JUMPLIST
8319 case ',':
8320 nv_pcmark(cap);
8321 break;
8322
8323 case ';':
8324 cap->count1 = -cap->count1;
8325 nv_pcmark(cap);
8326 break;
8327#endif
8328
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008329 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008330 if (!checkclearop(oap))
8331 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008332 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008333 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008334 if (!checkclearop(oap))
8335 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008336 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008337
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008338 case '+':
8339 case '-': /* "g+" and "g-": undo or redo along the timeline */
8340 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008341 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008342 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008343 break;
8344
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 default:
8346 clearopbeep(oap);
8347 break;
8348 }
8349}
8350
8351/*
8352 * Handle "o" and "O" commands.
8353 */
8354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008355n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008357#ifdef FEAT_CONCEAL
8358 linenr_T oldline = curwin->w_cursor.lnum;
8359#endif
8360
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 if (!checkclearopq(cap->oap))
8362 {
8363#ifdef FEAT_FOLDING
8364 if (cap->cmdchar == 'O')
8365 /* Open above the first line of a folded sequence of lines */
8366 (void)hasFolding(curwin->w_cursor.lnum,
8367 &curwin->w_cursor.lnum, NULL);
8368 else
8369 /* Open below the last line of a folded sequence of lines */
8370 (void)hasFolding(curwin->w_cursor.lnum,
8371 NULL, &curwin->w_cursor.lnum);
8372#endif
8373 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8374 (cap->cmdchar == 'O' ? 1 : 0)),
8375 (linenr_T)(curwin->w_cursor.lnum +
8376 (cap->cmdchar == 'o' ? 1 : 0))
8377 ) == OK
8378 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8379#ifdef FEAT_COMMENTS
8380 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8381#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02008382 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008384#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008385 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01008386 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008387#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008388#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008389 if (curwin->w_p_cul)
8390 /* force redraw of cursorline */
8391 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008392#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008393 /* When '#' is in 'cpoptions' ignore the count. */
8394 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8395 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8397 }
8398 }
8399}
8400
8401/*
8402 * "." command: redo last change.
8403 */
8404 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008405nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
8407 if (!checkclearopq(cap->oap))
8408 {
8409 /*
8410 * If "restart_edit" is TRUE, the last but one command is repeated
8411 * instead of the last command (inserting text). This is used for
8412 * CTRL-O <.> in insert mode.
8413 */
8414 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8415 clearopbeep(cap->oap);
8416 }
8417}
8418
8419/*
8420 * CTRL-R: undo undo
8421 */
8422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008423nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
8425 if (!checkclearopq(cap->oap))
8426 {
8427 u_redo((int)cap->count1);
8428 curwin->w_set_curswant = TRUE;
8429 }
8430}
8431
8432/*
8433 * Handle "U" command.
8434 */
8435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008436nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437{
8438 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008439 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {
8441 /* translate "gUU" to "gUgU" */
8442 cap->cmdchar = 'g';
8443 cap->nchar = 'U';
8444 nv_operator(cap);
8445 }
8446 else if (!checkclearopq(cap->oap))
8447 {
8448 u_undoline();
8449 curwin->w_set_curswant = TRUE;
8450 }
8451}
8452
8453/*
8454 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8455 * single character.
8456 */
8457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008458nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008460 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02008461 {
8462#ifdef FEAT_JOB_CHANNEL
8463 if (bt_prompt(curbuf) && !prompt_curpos_editable())
8464 {
8465 clearopbeep(cap->oap);
8466 return;
8467 }
8468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 else
8472 nv_operator(cap);
8473}
8474
8475/*
8476 * Handle an operator command.
8477 * The actual work is done by do_pending_operator().
8478 */
8479 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008480nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
8482 int op_type;
8483
8484 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008485#ifdef FEAT_JOB_CHANNEL
8486 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
8487 {
8488 clearopbeep(cap->oap);
8489 return;
8490 }
8491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492
8493 if (op_type == cap->oap->op_type) /* double operator works on lines */
8494 nv_lineop(cap);
8495 else if (!checkclearop(cap->oap))
8496 {
8497 cap->oap->start = curwin->w_cursor;
8498 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008499#ifdef FEAT_EVAL
8500 set_op_var(op_type);
8501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 }
8503}
8504
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008505#ifdef FEAT_EVAL
8506/*
8507 * Set v:operator to the characters for "optype".
8508 */
8509 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008510set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008511{
8512 char_u opchars[3];
8513
8514 if (optype == OP_NOP)
8515 set_vim_var_string(VV_OP, NULL, 0);
8516 else
8517 {
8518 opchars[0] = get_op_char(optype);
8519 opchars[1] = get_extra_op_char(optype);
8520 opchars[2] = NUL;
8521 set_vim_var_string(VV_OP, opchars, -1);
8522 }
8523}
8524#endif
8525
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526/*
8527 * Handle linewise operator "dd", "yy", etc.
8528 *
8529 * "_" is is a strange motion command that helps make operators more logical.
8530 * It is actually implemented, but not documented in the real Vi. This motion
8531 * command actually refers to "the current line". Commands like "dd" and "yy"
8532 * are really an alternate form of "d_" and "y_". It does accept a count, so
8533 * "d3_" works to delete 3 lines.
8534 */
8535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008536nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538 cap->oap->motion_type = MLINE;
8539 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8540 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008541 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8542 && cap->oap->motion_force != 'v'
8543 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 || cap->oap->op_type == OP_LSHIFT
8545 || cap->oap->op_type == OP_RSHIFT)
8546 beginline(BL_SOL | BL_FIX);
8547 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8548 beginline(BL_WHITE | BL_FIX);
8549}
8550
8551/*
8552 * <Home> command.
8553 */
8554 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008555nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008557 /* CTRL-HOME is like "gg" */
8558 if (mod_mask & MOD_MASK_CTRL)
8559 nv_goto(cap);
8560 else
8561 {
8562 cap->count0 = 1;
8563 nv_pipe(cap);
8564 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008565 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8566 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567}
8568
8569/*
8570 * "|" command.
8571 */
8572 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008573nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 cap->oap->motion_type = MCHAR;
8576 cap->oap->inclusive = FALSE;
8577 beginline(0);
8578 if (cap->count0 > 0)
8579 {
8580 coladvance((colnr_T)(cap->count0 - 1));
8581 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8582 }
8583 else
8584 curwin->w_curswant = 0;
8585 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008586 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 curwin->w_set_curswant = FALSE;
8588}
8589
8590/*
8591 * Handle back-word command "b" and "B".
8592 * cap->arg is 1 for "B"
8593 */
8594 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008595nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 cap->oap->motion_type = MCHAR;
8598 cap->oap->inclusive = FALSE;
8599 curwin->w_set_curswant = TRUE;
8600 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8601 clearopbeep(cap->oap);
8602#ifdef FEAT_FOLDING
8603 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8604 foldOpenCursor();
8605#endif
8606}
8607
8608/*
8609 * Handle word motion commands "e", "E", "w" and "W".
8610 * cap->arg is TRUE for "E" and "W".
8611 */
8612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008613nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 int n;
8616 int word_end;
8617 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008618 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619
8620 /*
8621 * Set inclusive for the "E" and "e" command.
8622 */
8623 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8624 word_end = TRUE;
8625 else
8626 word_end = FALSE;
8627 cap->oap->inclusive = word_end;
8628
8629 /*
8630 * "cw" and "cW" are a special case.
8631 */
8632 if (!word_end && cap->oap->op_type == OP_CHANGE)
8633 {
8634 n = gchar_cursor();
8635 if (n != NUL) /* not an empty line */
8636 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008637 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 {
8639 /*
8640 * Reproduce a funny Vi behaviour: "cw" on a blank only
8641 * changes one character, not all blanks until the start of
8642 * the next word. Only do this when the 'w' flag is included
8643 * in 'cpoptions'.
8644 */
8645 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8646 {
8647 cap->oap->inclusive = TRUE;
8648 cap->oap->motion_type = MCHAR;
8649 return;
8650 }
8651 }
8652 else
8653 {
8654 /*
8655 * This is a little strange. To match what the real Vi does,
8656 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8657 * that we are not on a space or a TAB. This seems impolite
8658 * at first, but it's really more what we mean when we say
8659 * 'cw'.
8660 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008661 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 * will change only one character! This is done by setting
8663 * flag.
8664 */
8665 cap->oap->inclusive = TRUE;
8666 word_end = TRUE;
8667 flag = TRUE;
8668 }
8669 }
8670 }
8671
8672 cap->oap->motion_type = MCHAR;
8673 curwin->w_set_curswant = TRUE;
8674 if (word_end)
8675 n = end_word(cap->count1, cap->arg, flag, FALSE);
8676 else
8677 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8678
Bram Moolenaardfefb982008-04-01 10:06:39 +00008679 /* Don't leave the cursor on the NUL past the end of line. Unless we
8680 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008681 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008682 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683
8684 if (n == FAIL && cap->oap->op_type == OP_NOP)
8685 clearopbeep(cap->oap);
8686 else
8687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689#ifdef FEAT_FOLDING
8690 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8691 foldOpenCursor();
8692#endif
8693 }
8694}
8695
8696/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008697 * Used after a movement command: If the cursor ends up on the NUL after the
8698 * end of the line, may move it back to the last character and make the motion
8699 * inclusive.
8700 */
8701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008702adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008703{
8704 /* The cursor cannot remain on the NUL when:
8705 * - the column is > 0
8706 * - not in Visual mode or 'selection' is "o"
8707 * - 'virtualedit' is not "all" and not "onemore".
8708 */
8709 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008710 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008711 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008712 {
8713 --curwin->w_cursor.col;
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008714 /* prevent cursor from moving on the trail byte */
8715 if (has_mbyte)
8716 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008717 oap->inclusive = TRUE;
8718 }
8719}
8720
8721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 * "0" and "^" commands.
8723 * cap->arg is the argument for beginline().
8724 */
8725 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008726nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728 cap->oap->motion_type = MCHAR;
8729 cap->oap->inclusive = FALSE;
8730 beginline(cap->arg);
8731#ifdef FEAT_FOLDING
8732 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8733 foldOpenCursor();
8734#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008735 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8736 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737}
8738
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739/*
8740 * In exclusive Visual mode, may include the last character.
8741 */
8742 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008743adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744{
8745 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008746 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 if (has_mbyte)
8749 inc_cursor();
8750 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 ++curwin->w_cursor.col;
8752 cap->oap->inclusive = FALSE;
8753 }
8754}
8755
8756/*
8757 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8758 * Should check VIsual_mode before calling this.
8759 * Returns TRUE when backed up to the previous line.
8760 */
8761 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008762unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
8764 pos_T *pp;
8765
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008766 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008768 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 pp = &curwin->w_cursor;
8770 else
8771 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 if (pp->coladd > 0)
8773 --pp->coladd;
8774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (pp->col > 0)
8776 {
8777 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008778 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 }
8780 else if (pp->lnum > 1)
8781 {
8782 --pp->lnum;
8783 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8784 return TRUE;
8785 }
8786 }
8787 return FALSE;
8788}
8789
8790/*
8791 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8792 */
8793 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008794nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
8796 if (VIsual_active)
8797 VIsual_select = TRUE;
8798 else if (VIsual_reselect)
8799 {
8800 cap->nchar = 'v'; /* fake "gv" command */
8801 cap->arg = TRUE;
8802 nv_g_cmd(cap);
8803 }
8804}
8805
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
8807/*
8808 * "G", "gg", CTRL-END, CTRL-HOME.
8809 * cap->arg is TRUE for "G".
8810 */
8811 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008812nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
8814 linenr_T lnum;
8815
8816 if (cap->arg)
8817 lnum = curbuf->b_ml.ml_line_count;
8818 else
8819 lnum = 1L;
8820 cap->oap->motion_type = MLINE;
8821 setpcmark();
8822
8823 /* When a count is given, use it instead of the default lnum */
8824 if (cap->count0 != 0)
8825 lnum = cap->count0;
8826 if (lnum < 1L)
8827 lnum = 1L;
8828 else if (lnum > curbuf->b_ml.ml_line_count)
8829 lnum = curbuf->b_ml.ml_line_count;
8830 curwin->w_cursor.lnum = lnum;
8831 beginline(BL_SOL | BL_FIX);
8832#ifdef FEAT_FOLDING
8833 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8834 foldOpenCursor();
8835#endif
8836}
8837
8838/*
8839 * CTRL-\ in Normal mode.
8840 */
8841 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008842nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
8844 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8845 {
8846 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008847 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 clear_cmdline = TRUE; /* unshow mode later */
8849 restart_edit = 0;
8850#ifdef FEAT_CMDWIN
8851 if (cmdwin_type != 0)
8852 cmdwin_result = Ctrl_C;
8853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 if (VIsual_active)
8855 {
8856 end_visual_mode(); /* stop Visual */
8857 redraw_curbuf_later(INVERTED);
8858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8860 if (cap->nchar == Ctrl_G && p_im)
8861 restart_edit = 'a';
8862 }
8863 else
8864 clearopbeep(cap->oap);
8865}
8866
8867/*
8868 * ESC in Normal mode: beep, but don't flush buffers.
8869 * Don't even beep if we are canceling a command.
8870 */
8871 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008872nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
8874 int no_reason;
8875
8876 no_reason = (cap->oap->op_type == OP_NOP
8877 && cap->opcount == 0
8878 && cap->count0 == 0
8879 && cap->oap->regname == 0
8880 && !p_im);
8881
8882 if (cap->arg) /* TRUE for CTRL-C */
8883 {
8884 if (restart_edit == 0
8885#ifdef FEAT_CMDWIN
8886 && cmdwin_type == 0
8887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 && no_reason)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008890 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
8892 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8893 * set again below when halfway a mapping. */
8894 if (!p_im)
8895 restart_edit = 0;
8896#ifdef FEAT_CMDWIN
8897 if (cmdwin_type != 0)
8898 {
8899 cmdwin_result = K_IGNORE;
8900 got_int = FALSE; /* don't stop executing autocommands et al. */
8901 return;
8902 }
8903#endif
8904 }
8905
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 if (VIsual_active)
8907 {
8908 end_visual_mode(); /* stop Visual */
8909 check_cursor_col(); /* make sure cursor is not beyond EOL */
8910 curwin->w_set_curswant = TRUE;
8911 redraw_curbuf_later(INVERTED);
8912 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008913 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008914 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 clearop(cap->oap);
8916
8917 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8918 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01008919 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 restart_edit = 'a';
8921}
8922
8923/*
8924 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01008925 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 */
8927 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008928nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
8930 /* <Insert> is equal to "i" */
8931 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8932 cap->cmdchar = 'i';
8933
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 /* in Visual mode "A" and "I" are an operator */
8935 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02008936 {
8937#ifdef FEAT_TERMINAL
8938 if (term_in_normal_mode())
8939 {
8940 end_visual_mode();
8941 clearop(cap->oap);
8942 term_enter_job_mode();
8943 return;
8944 }
8945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02008947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948
8949 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008950 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8951 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 {
8953#ifdef FEAT_TEXTOBJ
8954 nv_object(cap);
8955#else
8956 clearopbeep(cap->oap);
8957#endif
8958 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02008959#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02008960 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02008961 {
8962 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02008963 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02008964 return;
8965 }
8966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 else if (!curbuf->b_p_ma && !p_im)
8968 {
8969 /* Only give this error when 'insertmode' is off. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008970 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01008972 if (cap->cmdchar == K_PS)
8973 /* drop the pasted text */
8974 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 }
Bram Moolenaara1891842017-02-04 21:34:31 +01008976 else if (cap->cmdchar == K_PS && VIsual_active)
8977 {
8978 pos_T old_pos = curwin->w_cursor;
8979 pos_T old_visual = VIsual;
8980
8981 /* In Visual mode the selected text is deleted. */
8982 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
8983 {
8984 shift_delete_registers();
8985 cap->oap->regname = '1';
8986 }
8987 else
8988 cap->oap->regname = '-';
8989 cap->cmdchar = 'd';
8990 cap->nchar = NUL;
8991 nv_operator(cap);
8992 do_pending_operator(cap, 0, FALSE);
8993 cap->cmdchar = K_PS;
8994
8995 /* When the last char in the line was deleted then append. Detect this
8996 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008997 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
8998 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01008999 inc_cursor();
9000
9001 /* Insert to replace the deleted text with the pasted text. */
9002 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 else if (!checkclearopq(cap->oap))
9005 {
9006 switch (cap->cmdchar)
9007 {
9008 case 'A': /* "A"ppend after the line */
9009 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 if (ve_flags == VE_ALL)
9011 {
9012 int save_State = State;
9013
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009014 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 * character past the end of the line */
9016 State = INSERT;
9017 coladvance((colnr_T)MAXCOL);
9018 State = save_State;
9019 }
9020 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9022 break;
9023
9024 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009025 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9026 beginline(BL_WHITE);
9027 else
9028 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 break;
9030
Bram Moolenaara1891842017-02-04 21:34:31 +01009031 case K_PS:
9032 /* Bracketed paste works like "a"ppend, unless the cursor is in
9033 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009034 if (curwin->w_cursor.col == 0)
9035 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009036 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009037
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 case 'a': /* "a"ppend is like "i"nsert on the next character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 /* increment coladd when in virtual space, increment the
9040 * column otherwise, also to append after an unprintable char */
9041 if (virtual_active()
9042 && (curwin->w_cursor.coladd > 0
9043 || *ml_get_cursor() == NUL
9044 || *ml_get_cursor() == TAB))
9045 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009046 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 inc_cursor();
9048 break;
9049 }
9050
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9052 {
9053 int save_State = State;
9054
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009055 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 * character past the end of the line */
9057 State = INSERT;
9058 coladvance(getviscol());
9059 State = save_State;
9060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061
9062 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9063 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009064 else if (cap->cmdchar == K_PS)
9065 /* drop the pasted text */
9066 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067}
9068
9069/*
9070 * Invoke edit() and take care of "restart_edit" and the return value.
9071 */
9072 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009073invoke_edit(
9074 cmdarg_T *cap,
9075 int repl, /* "r" or "gr" command */
9076 int cmd,
9077 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
9079 int restart_edit_save = 0;
9080
9081 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9082 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9083 * it. */
9084 if (repl || !stuff_empty())
9085 restart_edit_save = restart_edit;
9086 else
9087 restart_edit_save = 0;
9088
9089 /* Always reset "restart_edit", this is not a restarted edit. */
9090 restart_edit = 0;
9091
9092 if (edit(cmd, startln, cap->count1))
9093 cap->retval |= CA_COMMAND_BUSY;
9094
9095 if (restart_edit == 0)
9096 restart_edit = restart_edit_save;
9097}
9098
9099#ifdef FEAT_TEXTOBJ
9100/*
9101 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9102 */
9103 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009104nv_object(
9105 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 int flag;
9108 int include;
9109 char_u *mps_save;
9110
9111 if (cap->cmdchar == 'i')
9112 include = FALSE; /* "ix" = inner object: exclude white space */
9113 else
9114 include = TRUE; /* "ax" = an object: include white space */
9115
9116 /* Make sure (), [], {} and <> are in 'matchpairs' */
9117 mps_save = curbuf->b_p_mps;
9118 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9119
9120 switch (cap->nchar)
9121 {
9122 case 'w': /* "aw" = a word */
9123 flag = current_word(cap->oap, cap->count1, include, FALSE);
9124 break;
9125 case 'W': /* "aW" = a WORD */
9126 flag = current_word(cap->oap, cap->count1, include, TRUE);
9127 break;
9128 case 'b': /* "ab" = a braces block */
9129 case '(':
9130 case ')':
9131 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9132 break;
9133 case 'B': /* "aB" = a Brackets block */
9134 case '{':
9135 case '}':
9136 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9137 break;
9138 case '[': /* "a[" = a [] block */
9139 case ']':
9140 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9141 break;
9142 case '<': /* "a<" = a <> block */
9143 case '>':
9144 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9145 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009146 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009147 /* Do not adjust oap->end in do_pending_operator()
9148 * otherwise there are different results for 'dit'
9149 * (note leading whitespace in last line):
9150 * 1) <b> 2) <b>
9151 * foobar foobar
9152 * </b> </b>
9153 */
9154 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009155 flag = current_tagblock(cap->oap, cap->count1, include);
9156 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 case 'p': /* "ap" = a paragraph */
9158 flag = current_par(cap->oap, cap->count1, include, 'p');
9159 break;
9160 case 's': /* "as" = a sentence */
9161 flag = current_sent(cap->oap, cap->count1, include);
9162 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009163 case '"': /* "a"" = a double quoted string */
9164 case '\'': /* "a'" = a single quoted string */
9165 case '`': /* "a`" = a backtick quoted string */
9166 flag = current_quote(cap->oap, cap->count1, include,
9167 cap->nchar);
9168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169#if 0 /* TODO */
9170 case 'S': /* "aS" = a section */
9171 case 'f': /* "af" = a filename */
9172 case 'u': /* "au" = a URL */
9173#endif
9174 default:
9175 flag = FAIL;
9176 break;
9177 }
9178
9179 curbuf->b_p_mps = mps_save;
9180 if (flag == FAIL)
9181 clearopbeep(cap->oap);
9182 adjust_cursor_col();
9183 curwin->w_set_curswant = TRUE;
9184}
9185#endif
9186
9187/*
9188 * "q" command: Start/stop recording.
9189 * "q:", "q/", "q?": edit command-line in command-line window.
9190 */
9191 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009192nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193{
9194 if (cap->oap->op_type == OP_FORMAT)
9195 {
9196 /* "gqq" is the same as "gqgq": format line */
9197 cap->cmdchar = 'g';
9198 cap->nchar = 'q';
9199 nv_operator(cap);
9200 }
9201 else if (!checkclearop(cap->oap))
9202 {
9203#ifdef FEAT_CMDWIN
9204 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9205 {
9206 stuffcharReadbuff(cap->nchar);
9207 stuffcharReadbuff(K_CMDWIN);
9208 }
9209 else
9210#endif
9211 /* (stop) recording into a named register, unless executing a
9212 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009213 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 clearopbeep(cap->oap);
9215 }
9216}
9217
9218/*
9219 * Handle the "@r" command.
9220 */
9221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009222nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223{
9224 if (checkclearop(cap->oap))
9225 return;
9226#ifdef FEAT_EVAL
9227 if (cap->nchar == '=')
9228 {
9229 if (get_expr_register() == NUL)
9230 return;
9231 }
9232#endif
9233 while (cap->count1-- && !got_int)
9234 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009235 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 {
9237 clearopbeep(cap->oap);
9238 break;
9239 }
9240 line_breakcheck();
9241 }
9242}
9243
9244/*
9245 * Handle the CTRL-U and CTRL-D commands.
9246 */
9247 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009248nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249{
9250 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9251 || (cap->cmdchar == Ctrl_D
9252 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9253 clearopbeep(cap->oap);
9254 else if (!checkclearop(cap->oap))
9255 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9256}
9257
9258/*
9259 * Handle "J" or "gJ" command.
9260 */
9261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009262nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 if (VIsual_active) /* join the visual lines */
9265 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009266 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 {
9268 if (cap->count0 <= 1)
9269 cap->count0 = 2; /* default for join is two lines! */
9270 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9271 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009273 /* can't join when on the last line */
9274 if (cap->count0 <= 2)
9275 {
9276 clearopbeep(cap->oap);
9277 return;
9278 }
9279 cap->count0 = curbuf->b_ml.ml_line_count
9280 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009282
9283 prep_redo(cap->oap->regname, cap->count0,
9284 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9285 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 }
9287}
9288
9289/*
9290 * "P", "gP", "p" and "gp" commands.
9291 */
9292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009293nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 int regname = 0;
9296 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009297 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009298 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 int dir;
9300 int flags = 0;
9301
9302 if (cap->oap->op_type != OP_NOP)
9303 {
9304#ifdef FEAT_DIFF
9305 /* "dp" is ":diffput" */
9306 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9307 {
9308 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009309 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 }
9311 else
9312#endif
9313 clearopbeep(cap->oap);
9314 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02009315#ifdef FEAT_JOB_CHANNEL
9316 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
9317 {
9318 clearopbeep(cap->oap);
9319 }
9320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 else
9322 {
9323 dir = (cap->cmdchar == 'P'
9324 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9325 ? BACKWARD : FORWARD;
9326 prep_redo_cmd(cap);
9327 if (cap->cmdchar == 'g')
9328 flags |= PUT_CURSEND;
9329
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 if (VIsual_active)
9331 {
9332 /* Putting in Visual mode: The put text replaces the selected
9333 * text. First delete the selected text, then put the new text.
9334 * Need to save and restore the registers that the delete
9335 * overwrites if the old contents is being put.
9336 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009337 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009339#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009341#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009342 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009343 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009344#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347
9348 )
9349 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009350 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 * put, save it first. */
9352 reg1 = get_register(regname, TRUE);
9353 }
9354
9355 /* Now delete the selected text. */
9356 cap->cmdchar = 'd';
9357 cap->nchar = NUL;
9358 cap->oap->regname = NUL;
9359 nv_operator(cap);
9360 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009361 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362
9363 /* delete PUT_LINE_BACKWARD; */
9364 cap->oap->regname = regname;
9365
9366 if (reg1 != NULL)
9367 {
9368 /* Delete probably changed the register we want to put, save
9369 * it first. Then put back what was there before the delete. */
9370 reg2 = get_register(regname, FALSE);
9371 put_register(regname, reg1);
9372 }
9373
9374 /* When deleted a linewise Visual area, put the register as
9375 * lines to avoid it joined with the next line. When deletion was
9376 * characterwise, split a line when putting lines. */
9377 if (VIsual_mode == 'V')
9378 flags |= PUT_LINE;
9379 else if (VIsual_mode == 'v')
9380 flags |= PUT_LINE_SPLIT;
9381 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9382 flags |= PUT_LINE_FORWARD;
9383 dir = BACKWARD;
9384 if ((VIsual_mode != 'V'
9385 && curwin->w_cursor.col < curbuf->b_op_start.col)
9386 || (VIsual_mode == 'V'
9387 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9388 /* cursor is at the end of the line or end of file, put
9389 * forward. */
9390 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009391 /* May have been reset in do_put(). */
9392 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 do_put(cap->oap->regname, dir, cap->count1, flags);
9395
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 /* If a register was saved, put it back now. */
9397 if (reg2 != NULL)
9398 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009399
9400 /* What to reselect with "gv"? Selecting the just put text seems to
9401 * be the most useful, since the original text was removed. */
9402 if (was_visual)
9403 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009404 curbuf->b_visual.vi_start = curbuf->b_op_start;
9405 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009406 /* need to adjust cursor position */
9407 if (*p_sel == 'e')
9408 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009409 }
9410
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009411 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009412 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009413 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009414 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009415 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009416
9417 /* If the cursor was in that line, move it to the end of the last
9418 * line. */
9419 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9420 {
9421 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9422 coladvance((colnr_T)MAXCOL);
9423 }
9424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 auto_format(FALSE, TRUE);
9426 }
9427}
9428
9429/*
9430 * "o" and "O" commands.
9431 */
9432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009433nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434{
9435#ifdef FEAT_DIFF
9436 /* "do" is ":diffget" */
9437 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9438 {
9439 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009440 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 }
9442 else
9443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 if (VIsual_active) /* switch start and end of visual */
9445 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009446#ifdef FEAT_JOB_CHANNEL
9447 else if (bt_prompt(curbuf))
9448 {
9449 clearopbeep(cap->oap);
9450 }
9451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 n_opencmd(cap);
9454}
9455
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456#ifdef FEAT_NETBEANS_INTG
9457 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009458nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459{
9460 netbeans_keycommand(cap->nchar);
9461}
9462#endif
9463
9464#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009466nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
9468 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9469}
9470#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009471
Bram Moolenaar3918c952005-03-15 22:34:55 +00009472/*
9473 * Trigger CursorHold event.
9474 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9475 * input buffer. "did_cursorhold" is set to avoid retriggering.
9476 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009477 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009478nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009479{
9480 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9481 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009482 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009483}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009484
9485/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009486 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009487 */
9488 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009489get_op_vcol(
9490 oparg_T *oap,
9491 colnr_T redo_VIsual_vcol,
9492 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009493{
9494 colnr_T start, end;
9495
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009496 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009497 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009498 return;
9499
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009500 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009501
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009502 /* prevent from moving onto a trail byte */
9503 if (has_mbyte)
9504 mb_adjustpos(curwin->w_buffer, &oap->end);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009505
9506 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009507
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009508 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009509 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009510 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9511
9512 if (start < oap->start_vcol)
9513 oap->start_vcol = start;
9514 if (end > oap->end_vcol)
9515 {
9516 if (initial && *p_sel == 'e' && start >= 1
9517 && start - 1 >= oap->end_vcol)
9518 oap->end_vcol = start - 1;
9519 else
9520 oap->end_vcol = end;
9521 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009522 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009523
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009524 /* if '$' was used, get oap->end_vcol from longest line */
9525 if (curwin->w_curswant == MAXCOL)
9526 {
9527 curwin->w_cursor.col = MAXCOL;
9528 oap->end_vcol = 0;
9529 for (curwin->w_cursor.lnum = oap->start.lnum;
9530 curwin->w_cursor.lnum <= oap->end.lnum;
9531 ++curwin->w_cursor.lnum)
9532 {
9533 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9534 if (end > oap->end_vcol)
9535 oap->end_vcol = end;
9536 }
9537 }
9538 else if (redo_VIsual_busy)
9539 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9540 /*
9541 * Correct oap->end.col and oap->start.col to be the
9542 * upper-left and lower-right corner of the block area.
9543 *
9544 * (Actually, this does convert column positions into character
9545 * positions)
9546 */
9547 curwin->w_cursor.lnum = oap->end.lnum;
9548 coladvance(oap->end_vcol);
9549 oap->end = curwin->w_cursor;
9550
9551 curwin->w_cursor = oap->start;
9552 coladvance(oap->start_vcol);
9553 oap->start = curwin->w_cursor;
9554}