blob: a01a434867965181b25f4229dae5a7c6ea2b1a88 [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);
35static int find_command(int cmdchar);
36static void op_colon(oparg_T *oap);
37static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010038#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void find_start_of_word(pos_T *);
40static void find_end_of_word(pos_T *);
41static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void prep_redo_cmd(cmdarg_T *cap);
44static void prep_redo(int regname, long, int, int, int, int, int);
45static int checkclearop(oparg_T *oap);
46static int checkclearopq(oparg_T *oap);
47static void clearop(oparg_T *oap);
48static void clearopbeep(oparg_T *oap);
49static void unshift_special(cmdarg_T *cap);
50static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010052static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#endif
54
55/*
56 * nv_*(): functions called to handle Normal and Visual mode commands.
57 * n_*(): functions called to handle Normal mode commands.
58 * v_*(): functions called to handle Visual mode commands.
59 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_ignore(cmdarg_T *cap);
61static void nv_nop(cmdarg_T *cap);
62static void nv_error(cmdarg_T *cap);
63static void nv_help(cmdarg_T *cap);
64static void nv_addsub(cmdarg_T *cap);
65static void nv_page(cmdarg_T *cap);
66static void nv_gd(oparg_T *oap, int nchar, int thisblock);
67static int nv_screengo(oparg_T *oap, int dir, long dist);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_mousescroll(cmdarg_T *cap);
70static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010072static void nv_scroll_line(cmdarg_T *cap);
73static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_ver_scrollbar(cmdarg_T *cap);
76static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000078#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_tabline(cmdarg_T *cap);
80static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010082static void nv_exmode(cmdarg_T *cap);
83static void nv_colon(cmdarg_T *cap);
84static void nv_ctrlg(cmdarg_T *cap);
85static void nv_ctrlh(cmdarg_T *cap);
86static void nv_clear(cmdarg_T *cap);
87static void nv_ctrlo(cmdarg_T *cap);
88static void nv_hat(cmdarg_T *cap);
89static void nv_Zet(cmdarg_T *cap);
90static void nv_ident(cmdarg_T *cap);
91static void nv_tagpop(cmdarg_T *cap);
92static void nv_scroll(cmdarg_T *cap);
93static void nv_right(cmdarg_T *cap);
94static void nv_left(cmdarg_T *cap);
95static void nv_up(cmdarg_T *cap);
96static void nv_down(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_SEARCHPATH
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void nv_gotofile(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_end(cmdarg_T *cap);
101static void nv_dollar(cmdarg_T *cap);
102static void nv_search(cmdarg_T *cap);
103static void nv_next(cmdarg_T *cap);
104static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
105static void nv_csearch(cmdarg_T *cap);
106static void nv_brackets(cmdarg_T *cap);
107static void nv_percent(cmdarg_T *cap);
108static void nv_brace(cmdarg_T *cap);
109static void nv_mark(cmdarg_T *cap);
110static void nv_findpar(cmdarg_T *cap);
111static void nv_undo(cmdarg_T *cap);
112static void nv_kundo(cmdarg_T *cap);
113static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_VREPLACE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_vreplace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void v_swap_corners(int cmdchar);
118static void nv_replace(cmdarg_T *cap);
119static void n_swapchar(cmdarg_T *cap);
120static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
121static void v_visop(cmdarg_T *cap);
122static void nv_subst(cmdarg_T *cap);
123static void nv_abbrev(cmdarg_T *cap);
124static void nv_optrans(cmdarg_T *cap);
125static void nv_gomark(cmdarg_T *cap);
126static void nv_pcmark(cmdarg_T *cap);
127static void nv_regname(cmdarg_T *cap);
128static void nv_visual(cmdarg_T *cap);
129static void n_start_visual_mode(int c);
130static void nv_window(cmdarg_T *cap);
131static void nv_suspend(cmdarg_T *cap);
132static void nv_g_cmd(cmdarg_T *cap);
133static void n_opencmd(cmdarg_T *cap);
134static void nv_dot(cmdarg_T *cap);
135static void nv_redo(cmdarg_T *cap);
136static void nv_Undo(cmdarg_T *cap);
137static void nv_tilde(cmdarg_T *cap);
138static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000139#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000141#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100142static void nv_lineop(cmdarg_T *cap);
143static void nv_home(cmdarg_T *cap);
144static void nv_pipe(cmdarg_T *cap);
145static void nv_bck_word(cmdarg_T *cap);
146static void nv_wordcmd(cmdarg_T *cap);
147static void nv_beginline(cmdarg_T *cap);
148static void adjust_cursor(oparg_T *oap);
149static void adjust_for_sel(cmdarg_T *cap);
150static int unadjust_for_sel(void);
151static void nv_select(cmdarg_T *cap);
152static void nv_goto(cmdarg_T *cap);
153static void nv_normal(cmdarg_T *cap);
154static void nv_esc(cmdarg_T *oap);
155static void nv_edit(cmdarg_T *cap);
156static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100158static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100160static void nv_record(cmdarg_T *cap);
161static void nv_at(cmdarg_T *cap);
162static void nv_halfpage(cmdarg_T *cap);
163static void nv_join(cmdarg_T *cap);
164static void nv_put(cmdarg_T *cap);
165static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100167static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100170static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100172static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100173static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000175static char *e_noident = N_("E349: No identifier under cursor");
176
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177/*
178 * Function to be called for a Normal or Visual mode command.
179 * The argument is a cmdarg_T.
180 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100181typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
183/* Values for cmd_flags. */
184#define NV_NCH 0x01 /* may need to get a second char */
185#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
186#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
187#define NV_LANG 0x08 /* second char needs language adjustment */
188
189#define NV_SS 0x10 /* may start selection */
190#define NV_SSS 0x20 /* may start selection with shift modifier */
191#define NV_STS 0x40 /* may stop selection without shift modif. */
192#define NV_RL 0x80 /* 'rightleft' modifies command */
193#define NV_KEEPREG 0x100 /* don't clear regname */
194#define NV_NCW 0x200 /* not allowed in command-line window */
195
196/*
197 * Generally speaking, every Normal mode command should either clear any
198 * pending operator (with *clearop*()), or set the motion type variable
199 * oap->motion_type.
200 *
201 * When a cursor motion command is made, it is marked as being a character or
202 * line oriented motion. Then, if an operator is in effect, the operation
203 * becomes character or line oriented accordingly.
204 */
205
206/*
207 * This table contains one entry for every Normal or Visual mode command.
208 * The order doesn't matter, init_normal_cmds() will create a sorted index.
209 * It is faster when all keys from zero to '~' are present.
210 */
211static const struct nv_cmd
212{
213 int cmd_char; /* (first) command character */
214 nv_func_T cmd_func; /* function for this command */
215 short_u cmd_flags; /* NV_ flags */
216 short cmd_arg; /* value for ca.arg */
217} nv_cmds[] =
218{
219 {NUL, nv_error, 0, 0},
220 {Ctrl_A, nv_addsub, 0, 0},
221 {Ctrl_B, nv_page, NV_STS, BACKWARD},
222 {Ctrl_C, nv_esc, 0, TRUE},
223 {Ctrl_D, nv_halfpage, 0, 0},
224 {Ctrl_E, nv_scroll_line, 0, TRUE},
225 {Ctrl_F, nv_page, NV_STS, FORWARD},
226 {Ctrl_G, nv_ctrlg, 0, 0},
227 {Ctrl_H, nv_ctrlh, 0, 0},
228 {Ctrl_I, nv_pcmark, 0, 0},
229 {NL, nv_down, 0, FALSE},
230 {Ctrl_K, nv_error, 0, 0},
231 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000232 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {Ctrl_N, nv_down, NV_STS, FALSE},
234 {Ctrl_O, nv_ctrlo, 0, 0},
235 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000236 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {Ctrl_R, nv_redo, 0, 0},
238 {Ctrl_S, nv_ignore, 0, 0},
239 {Ctrl_T, nv_tagpop, NV_NCW, 0},
240 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {Ctrl_V, nv_visual, 0, FALSE},
242 {'V', nv_visual, 0, FALSE},
243 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {Ctrl_W, nv_window, 0, 0},
245 {Ctrl_X, nv_addsub, 0, 0},
246 {Ctrl_Y, nv_scroll_line, 0, FALSE},
247 {Ctrl_Z, nv_suspend, 0, 0},
248 {ESC, nv_esc, 0, FALSE},
249 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
250 {Ctrl_RSB, nv_ident, NV_NCW, 0},
251 {Ctrl_HAT, nv_hat, NV_NCW, 0},
252 {Ctrl__, nv_error, 0, 0},
253 {' ', nv_right, 0, 0},
254 {'!', nv_operator, 0, 0},
255 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
256 {'#', nv_ident, 0, 0},
257 {'$', nv_dollar, 0, 0},
258 {'%', nv_percent, 0, 0},
259 {'&', nv_optrans, 0, 0},
260 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
261 {'(', nv_brace, 0, BACKWARD},
262 {')', nv_brace, 0, FORWARD},
263 {'*', nv_ident, 0, 0},
264 {'+', nv_down, 0, TRUE},
265 {',', nv_csearch, 0, TRUE},
266 {'-', nv_up, 0, TRUE},
267 {'.', nv_dot, NV_KEEPREG, 0},
268 {'/', nv_search, 0, FALSE},
269 {'0', nv_beginline, 0, 0},
270 {'1', nv_ignore, 0, 0},
271 {'2', nv_ignore, 0, 0},
272 {'3', nv_ignore, 0, 0},
273 {'4', nv_ignore, 0, 0},
274 {'5', nv_ignore, 0, 0},
275 {'6', nv_ignore, 0, 0},
276 {'7', nv_ignore, 0, 0},
277 {'8', nv_ignore, 0, 0},
278 {'9', nv_ignore, 0, 0},
279 {':', nv_colon, 0, 0},
280 {';', nv_csearch, 0, FALSE},
281 {'<', nv_operator, NV_RL, 0},
282 {'=', nv_operator, 0, 0},
283 {'>', nv_operator, NV_RL, 0},
284 {'?', nv_search, 0, FALSE},
285 {'@', nv_at, NV_NCH_NOP, FALSE},
286 {'A', nv_edit, 0, 0},
287 {'B', nv_bck_word, 0, 1},
288 {'C', nv_abbrev, NV_KEEPREG, 0},
289 {'D', nv_abbrev, NV_KEEPREG, 0},
290 {'E', nv_wordcmd, 0, TRUE},
291 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
292 {'G', nv_goto, 0, TRUE},
293 {'H', nv_scroll, 0, 0},
294 {'I', nv_edit, 0, 0},
295 {'J', nv_join, 0, 0},
296 {'K', nv_ident, 0, 0},
297 {'L', nv_scroll, 0, 0},
298 {'M', nv_scroll, 0, 0},
299 {'N', nv_next, 0, SEARCH_REV},
300 {'O', nv_open, 0, 0},
301 {'P', nv_put, 0, 0},
302 {'Q', nv_exmode, NV_NCW, 0},
303 {'R', nv_Replace, 0, FALSE},
304 {'S', nv_subst, NV_KEEPREG, 0},
305 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
306 {'U', nv_Undo, 0, 0},
307 {'W', nv_wordcmd, 0, TRUE},
308 {'X', nv_abbrev, NV_KEEPREG, 0},
309 {'Y', nv_abbrev, NV_KEEPREG, 0},
310 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
311 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
312 {'\\', nv_error, 0, 0},
313 {']', nv_brackets, NV_NCH_ALW, FORWARD},
314 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
315 {'_', nv_lineop, 0, 0},
316 {'`', nv_gomark, NV_NCH_ALW, FALSE},
317 {'a', nv_edit, NV_NCH, 0},
318 {'b', nv_bck_word, 0, 0},
319 {'c', nv_operator, 0, 0},
320 {'d', nv_operator, 0, 0},
321 {'e', nv_wordcmd, 0, FALSE},
322 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
323 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
324 {'h', nv_left, NV_RL, 0},
325 {'i', nv_edit, NV_NCH, 0},
326 {'j', nv_down, 0, FALSE},
327 {'k', nv_up, 0, FALSE},
328 {'l', nv_right, NV_RL, 0},
329 {'m', nv_mark, NV_NCH_NOP, 0},
330 {'n', nv_next, 0, 0},
331 {'o', nv_open, 0, 0},
332 {'p', nv_put, 0, 0},
333 {'q', nv_record, NV_NCH, 0},
334 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
335 {'s', nv_subst, NV_KEEPREG, 0},
336 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
337 {'u', nv_undo, 0, 0},
338 {'w', nv_wordcmd, 0, FALSE},
339 {'x', nv_abbrev, NV_KEEPREG, 0},
340 {'y', nv_operator, 0, 0},
341 {'z', nv_zet, NV_NCH_ALW, 0},
342 {'{', nv_findpar, 0, BACKWARD},
343 {'|', nv_pipe, 0, 0},
344 {'}', nv_findpar, 0, FORWARD},
345 {'~', nv_tilde, 0, 0},
346
347 /* pound sign */
348 {POUND, nv_ident, 0, 0},
349#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200350 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
351 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
352 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
353 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {K_LEFTMOUSE, nv_mouse, 0, 0},
355 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
356 {K_LEFTDRAG, nv_mouse, 0, 0},
357 {K_LEFTRELEASE, nv_mouse, 0, 0},
358 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100359 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
361 {K_MIDDLEDRAG, nv_mouse, 0, 0},
362 {K_MIDDLERELEASE, nv_mouse, 0, 0},
363 {K_RIGHTMOUSE, nv_mouse, 0, 0},
364 {K_RIGHTDRAG, nv_mouse, 0, 0},
365 {K_RIGHTRELEASE, nv_mouse, 0, 0},
366 {K_X1MOUSE, nv_mouse, 0, 0},
367 {K_X1DRAG, nv_mouse, 0, 0},
368 {K_X1RELEASE, nv_mouse, 0, 0},
369 {K_X2MOUSE, nv_mouse, 0, 0},
370 {K_X2DRAG, nv_mouse, 0, 0},
371 {K_X2RELEASE, nv_mouse, 0, 0},
372#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000373 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000374 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {K_INS, nv_edit, 0, 0},
376 {K_KINS, nv_edit, 0, 0},
377 {K_BS, nv_ctrlh, 0, 0},
378 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
379 {K_S_UP, nv_page, NV_SS, BACKWARD},
380 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
381 {K_S_DOWN, nv_page, NV_SS, FORWARD},
382 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
383 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
384 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
385 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
386 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
387 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
388 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
389 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
390 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
391 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
392 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
393 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 {K_S_END, nv_end, NV_SS, FALSE},
395 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
396 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
397 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {K_S_HOME, nv_home, NV_SS, 0},
399 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
400 {K_DEL, nv_abbrev, 0, 0},
401 {K_KDEL, nv_abbrev, 0, 0},
402 {K_UNDO, nv_kundo, 0, 0},
403 {K_HELP, nv_help, NV_NCW, 0},
404 {K_F1, nv_help, NV_NCW, 0},
405 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407#ifdef FEAT_GUI
408 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
409 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
410#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000411#ifdef FEAT_GUI_TABLINE
412 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000413 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415#ifdef FEAT_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200416 {K_F8, farsi_f8, 0, 0},
417 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#ifdef FEAT_NETBEANS_INTG
420 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
421#endif
422#ifdef FEAT_DND
423 {K_DROP, nv_drop, NV_STS, 0},
424#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000425 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100426 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427};
428
429/* Number of commands in nv_cmds[]. */
430#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
431
432/* Sorted index of commands in nv_cmds[]. */
433static short nv_cmd_idx[NV_CMDS_SIZE];
434
435/* The highest index for which
436 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
437static int nv_max_linear;
438
439/*
440 * Compare functions for qsort() below, that checks the command character
441 * through the index in nv_cmd_idx[].
442 */
443 static int
444#ifdef __BORLANDC__
445_RTLENTRYF
446#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100447nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449 int c1, c2;
450
451 /* The commands are sorted on absolute value. */
452 c1 = nv_cmds[*(const short *)s1].cmd_char;
453 c2 = nv_cmds[*(const short *)s2].cmd_char;
454 if (c1 < 0)
455 c1 = -c1;
456 if (c2 < 0)
457 c2 = -c2;
458 return c1 - c2;
459}
460
461/*
462 * Initialize the nv_cmd_idx[] table.
463 */
464 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100465init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
467 int i;
468
469 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000470 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 nv_cmd_idx[i] = i;
472
473 /* Sort the commands by the command character. */
474 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
475
476 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000477 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
479 break;
480 nv_max_linear = i - 1;
481}
482
483/*
484 * Search for a command in the commands table.
485 * Returns -1 for invalid command.
486 */
487 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
490 int i;
491 int idx;
492 int top, bot;
493 int c;
494
495#ifdef FEAT_MBYTE
496 /* A multi-byte character is never a command. */
497 if (cmdchar >= 0x100)
498 return -1;
499#endif
500
501 /* We use the absolute value of the character. Special keys have a
502 * negative value, but are sorted on their absolute value. */
503 if (cmdchar < 0)
504 cmdchar = -cmdchar;
505
506 /* If the character is in the first part: The character is the index into
507 * nv_cmd_idx[]. */
508 if (cmdchar <= nv_max_linear)
509 return nv_cmd_idx[cmdchar];
510
511 /* Perform a binary search. */
512 bot = nv_max_linear + 1;
513 top = NV_CMDS_SIZE - 1;
514 idx = -1;
515 while (bot <= top)
516 {
517 i = (top + bot) / 2;
518 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
519 if (c < 0)
520 c = -c;
521 if (cmdchar == c)
522 {
523 idx = nv_cmd_idx[i];
524 break;
525 }
526 if (cmdchar > c)
527 bot = i + 1;
528 else
529 top = i - 1;
530 }
531 return idx;
532}
533
534/*
535 * Execute a command in Normal mode.
536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100538normal_cmd(
539 oparg_T *oap,
540 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 cmdarg_T ca; /* command arguments */
543 int c;
544 int ctrl_w = FALSE; /* got CTRL-W command */
545 int old_col = curwin->w_curswant;
546#ifdef FEAT_CMDL_INFO
547 int need_flushbuf; /* need to call out_flush() */
548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 pos_T old_pos; /* cursor position before command */
550 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 static int old_mapped_len = 0;
552 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000553#ifdef FEAT_EVAL
554 int set_prevcount = FALSE;
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
558 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000559
560 /* Use a count remembered from before entering an operator. After typing
561 * "3d" we return from normal_cmd() and come back here, the "3" is
562 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 ca.opcount = opcount;
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 /*
566 * If there is an operator pending, then the command we take this time
567 * will terminate it. Finish_op tells us to finish the operation before
568 * returning this time (unless the operation was cancelled).
569 */
570#ifdef CURSOR_SHAPE
571 c = finish_op;
572#endif
573 finish_op = (oap->op_type != OP_NOP);
574#ifdef CURSOR_SHAPE
575 if (finish_op != c)
576 {
577 ui_cursor_shape(); /* may show different cursor shape */
578# ifdef FEAT_MOUSESHAPE
579 update_mouseshape(-1);
580# endif
581 }
582#endif
583
Bram Moolenaara983fe92008-07-31 20:04:27 +0000584 /* When not finishing an operator and no register name typed, reset the
585 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000589#ifdef FEAT_EVAL
590 set_prevcount = TRUE;
591#endif
592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
Bram Moolenaara983fe92008-07-31 20:04:27 +0000594 /* Restore counts from before receiving K_CURSORHOLD. This means after
595 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
596 * "3 * 2". */
597 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
598 {
599 ca.opcount = oap->prev_opcount;
600 ca.count0 = oap->prev_count0;
601 oap->prev_opcount = 0;
602 oap->prev_count0 = 0;
603 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
607 State = NORMAL_BUSY;
608#ifdef USE_ON_FLY_SCROLL
609 dont_scroll = FALSE; /* allow scrolling here */
610#endif
611
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100612#ifdef FEAT_EVAL
613 /* Set v:count here, when called from main() and not a stuffed
614 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100615 * when there is no count. Do set it for redo. */
616 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100617 set_vcount_ca(&ca, &set_prevcount);
618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 /*
621 * Get the command character from the user.
622 */
623 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100624 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626 /*
627 * If a mapping was started in Visual or Select mode, remember the length
628 * of the mapping. This is used below to not return to Insert mode for as
629 * long as the mapping is being executed.
630 */
631 if (restart_edit == 0)
632 old_mapped_len = 0;
633 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000634 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 old_mapped_len = typebuf_maplen();
636
637 if (c == NUL)
638 c = K_ZERO;
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 /*
641 * In Select mode, typed text replaces the selection.
642 */
643 if (VIsual_active
644 && VIsual_select
645 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
646 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000647 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
648 * 'insertmode' is set) fake a "d"elete command, Insert mode will
649 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000650 * Insert the typed character in the typeahead buffer, so that it can
651 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000652 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000653 if (restart_edit != 0)
654 c = 'd';
655 else
656 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000657 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200658 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661#ifdef FEAT_CMDL_INFO
662 need_flushbuf = add_to_showcmd(c);
663#endif
664
665getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {
668 /*
669 * Handle a count before a command and compute ca.count0.
670 * Note that '0' is a command and not the start of a count, but it's
671 * part of a count after other digits.
672 */
673 while ( (c >= '1' && c <= '9')
674 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
675 {
676 if (c == K_DEL || c == K_KDEL)
677 {
678 ca.count0 /= 10;
679#ifdef FEAT_CMDL_INFO
680 del_from_showcmd(4); /* delete the digit and ~@% */
681#endif
682 }
683 else
684 ca.count0 = ca.count0 * 10 + (c - '0');
685 if (ca.count0 < 0) /* got too large! */
686 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000687#ifdef FEAT_EVAL
688 /* Set v:count here, when called from main() and not a stuffed
689 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100690 * right after the count. Do set it for redo. */
691 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100692 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (ctrl_w)
695 {
696 ++no_mapping;
697 ++allow_keys; /* no mapping for nchar, but keys */
698 }
699 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000700 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 --no_zero_mapping;
703 if (ctrl_w)
704 {
705 --no_mapping;
706 --allow_keys;
707 }
708#ifdef FEAT_CMDL_INFO
709 need_flushbuf |= add_to_showcmd(c);
710#endif
711 }
712
713 /*
714 * If we got CTRL-W there may be a/another count
715 */
716 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
717 {
718 ctrl_w = TRUE;
719 ca.opcount = ca.count0; /* remember first count */
720 ca.count0 = 0;
721 ++no_mapping;
722 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000723 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 --no_mapping;
726 --allow_keys;
727#ifdef FEAT_CMDL_INFO
728 need_flushbuf |= add_to_showcmd(c);
729#endif
730 goto getcount; /* jump back */
731 }
732 }
733
Bram Moolenaara983fe92008-07-31 20:04:27 +0000734 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000736 /* Save the count values so that ca.opcount and ca.count0 are exactly
737 * the same when coming back here after handling K_CURSORHOLD. */
738 oap->prev_opcount = ca.opcount;
739 oap->prev_count0 = ca.count0;
740 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100741 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000742 {
743 /*
744 * If we're in the middle of an operator (including after entering a
745 * yank buffer with '"') AND we had a count before the operator, then
746 * that count overrides the current value of ca.count0.
747 * What this means effectively, is that commands like "3dw" get turned
748 * into "d3w" which makes things fall into place pretty neatly.
749 * If you give a count before AND after the operator, they are
750 * multiplied.
751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 if (ca.count0)
753 ca.count0 *= ca.opcount;
754 else
755 ca.count0 = ca.opcount;
756 }
757
758 /*
759 * Always remember the count. It will be set to zero (on the next call,
760 * above) when there is no pending operator.
761 * When called from main(), save the count for use by the "count" built-in
762 * variable.
763 */
764 ca.opcount = ca.count0;
765 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
766
767#ifdef FEAT_EVAL
768 /*
769 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100770 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100772 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000773 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
775
776 /*
777 * Find the command character in the table of commands.
778 * For CTRL-W we already got nchar when looking for a count.
779 */
780 if (ctrl_w)
781 {
782 ca.nchar = c;
783 ca.cmdchar = Ctrl_W;
784 }
785 else
786 ca.cmdchar = c;
787 idx = find_command(ca.cmdchar);
788 if (idx < 0)
789 {
790 /* Not a known command: beep. */
791 clearopbeep(oap);
792 goto normal_end;
793 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000794
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000795 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200797 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000799 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 goto normal_end;
801 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000802 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
803 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 /*
806 * In Visual/Select mode, a few keys are handled in a special way.
807 */
808 if (VIsual_active)
809 {
810 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
811 if (km_stopsel
812 && (nv_cmds[idx].cmd_flags & NV_STS)
813 && !(mod_mask & MOD_MASK_SHIFT))
814 {
815 end_visual_mode();
816 redraw_curbuf_later(INVERTED);
817 }
818
819 /* Keys that work different when 'keymodel' contains "startsel" */
820 if (km_startsel)
821 {
822 if (nv_cmds[idx].cmd_flags & NV_SS)
823 {
824 unshift_special(&ca);
825 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000826 if (idx < 0)
827 {
828 /* Just in case */
829 clearopbeep(oap);
830 goto normal_end;
831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
834 && (mod_mask & MOD_MASK_SHIFT))
835 {
836 mod_mask &= ~MOD_MASK_SHIFT;
837 }
838 }
839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
841#ifdef FEAT_RIGHTLEFT
842 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
843 && (nv_cmds[idx].cmd_flags & NV_RL))
844 {
845 /* Invert horizontal movements and operations. Only when typed by the
846 * user directly, not when the result of a mapping or "x" translated
847 * to "dl". */
848 switch (ca.cmdchar)
849 {
850 case 'l': ca.cmdchar = 'h'; break;
851 case K_RIGHT: ca.cmdchar = K_LEFT; break;
852 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
853 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
854 case 'h': ca.cmdchar = 'l'; break;
855 case K_LEFT: ca.cmdchar = K_RIGHT; break;
856 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
857 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
858 case '>': ca.cmdchar = '<'; break;
859 case '<': ca.cmdchar = '>'; break;
860 }
861 idx = find_command(ca.cmdchar);
862 }
863#endif
864
865 /*
866 * Get an additional character if we need one.
867 */
868 if ((nv_cmds[idx].cmd_flags & NV_NCH)
869 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
870 && oap->op_type == OP_NOP)
871 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
872 || (ca.cmdchar == 'q'
873 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200874 && reg_recording == 0
875 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100877 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 {
879 int *cp;
880 int repl = FALSE; /* get character for replace mode */
881 int lit = FALSE; /* get extra character literally */
882 int langmap_active = FALSE; /* using :lmap mappings */
883 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100884#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 int save_smd; /* saved value of p_smd */
886#endif
887
888 ++no_mapping;
889 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000890 /* Don't generate a CursorHold event here, most commands can't handle
891 * it, e.g., nv_replace(), nv_csearch(). */
892 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (ca.cmdchar == 'g')
894 {
895 /*
896 * For 'g' get the next character now, so that we can check for
897 * "gr", "g'" and "g`".
898 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000899 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#ifdef FEAT_CMDL_INFO
902 need_flushbuf |= add_to_showcmd(ca.nchar);
903#endif
904 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100905 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
907 cp = &ca.extra_char; /* need to get a third character */
908 if (ca.nchar != 'r')
909 lit = TRUE; /* get it literally */
910 else
911 repl = TRUE; /* get it in replace mode */
912 }
913 else
914 cp = NULL; /* no third character needed */
915 }
916 else
917 {
918 if (ca.cmdchar == 'r') /* get it in replace mode */
919 repl = TRUE;
920 cp = &ca.nchar;
921 }
922 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
923
924 /*
925 * Get a second or third character.
926 */
927 if (cp != NULL)
928 {
929#ifdef CURSOR_SHAPE
930 if (repl)
931 {
932 State = REPLACE; /* pretend Replace mode */
933 ui_cursor_shape(); /* show different cursor shape */
934 }
935#endif
936 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
937 {
938 /* Allow mappings defined with ":lmap". */
939 --no_mapping;
940 --allow_keys;
941 if (repl)
942 State = LREPLACE;
943 else
944 State = LANGMAP;
945 langmap_active = TRUE;
946 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100947#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 save_smd = p_smd;
949 p_smd = FALSE; /* Don't let the IM code show the mode here */
950 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
951 im_set_active(TRUE);
952#endif
953
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000954 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 if (langmap_active)
957 {
958 /* Undo the decrement done above */
959 ++no_mapping;
960 ++allow_keys;
961 State = NORMAL_BUSY;
962 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100963#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (lang)
965 {
966 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
967 im_save_status(&curbuf->b_p_iminsert);
968 im_set_active(FALSE);
969 }
970 p_smd = save_smd;
971#endif
972#ifdef CURSOR_SHAPE
973 State = NORMAL_BUSY;
974#endif
975#ifdef FEAT_CMDL_INFO
976 need_flushbuf |= add_to_showcmd(*cp);
977#endif
978
979 if (!lit)
980 {
981#ifdef FEAT_DIGRAPHS
982 /* Typing CTRL-K gets a digraph. */
983 if (*cp == Ctrl_K
984 && ((nv_cmds[idx].cmd_flags & NV_LANG)
985 || cp == &ca.extra_char)
986 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
987 {
988 c = get_digraph(FALSE);
989 if (c > 0)
990 {
991 *cp = c;
992# ifdef FEAT_CMDL_INFO
993 /* Guessing how to update showcmd here... */
994 del_from_showcmd(3);
995 need_flushbuf |= add_to_showcmd(*cp);
996# endif
997 }
998 }
999#endif
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /* adjust chars > 127, except after "tTfFr" commands */
1002 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#ifdef FEAT_RIGHTLEFT
1004 /* adjust Hebrew mapped char */
1005 if (p_hkmap && lang && KeyTyped)
1006 *cp = hkmap(*cp);
1007# ifdef FEAT_FKMAP
1008 /* adjust Farsi mapped char */
1009 if (p_fkmap && lang && KeyTyped)
1010 *cp = fkmap(*cp);
1011# endif
1012#endif
1013 }
1014
1015 /*
1016 * When the next character is CTRL-\ a following CTRL-N means the
1017 * command is aborted and we go to Normal mode.
1018 */
1019 if (cp == &ca.extra_char
1020 && ca.nchar == Ctrl_BSL
1021 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1022 {
1023 ca.cmdchar = Ctrl_BSL;
1024 ca.nchar = ca.extra_char;
1025 idx = find_command(ca.cmdchar);
1026 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001027 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001028 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 else if (*cp == Ctrl_BSL)
1030 {
1031 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1032
1033 /* There is a busy wait here when typing "f<C-\>" and then
1034 * something different from CTRL-N. Can't be avoided. */
1035 while ((c = vpeekc()) <= 0 && towait > 0L)
1036 {
1037 do_sleep(towait > 50L ? 50L : towait);
1038 towait -= 50L;
1039 }
1040 if (c > 0)
1041 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001042 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (c != Ctrl_N && c != Ctrl_G)
1044 vungetc(c);
1045 else
1046 {
1047 ca.cmdchar = Ctrl_BSL;
1048 ca.nchar = c;
1049 idx = find_command(ca.cmdchar);
1050 }
1051 }
1052 }
1053
1054#ifdef FEAT_MBYTE
1055 /* When getting a text character and the next character is a
1056 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001057 * However, don't wait for it to arrive. Also, do enable mapping,
1058 * because if it's put back with vungetc() it's too late to apply
1059 * mapping. */
1060 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 while (enc_utf8 && lang && (c = vpeekc()) > 0
1062 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1063 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001064 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (!utf_iscomposing(c))
1066 {
1067 vungetc(c); /* it wasn't, put it back */
1068 break;
1069 }
1070 else if (ca.ncharC1 == 0)
1071 ca.ncharC1 = c;
1072 else
1073 ca.ncharC2 = c;
1074 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001075 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#endif
1077 }
1078 --no_mapping;
1079 --allow_keys;
1080 }
1081
1082#ifdef FEAT_CMDL_INFO
1083 /*
1084 * Flush the showcmd characters onto the screen so we can see them while
1085 * the command is being executed. Only do this when the shown command was
1086 * actually displayed, otherwise this will slow down a lot when executing
1087 * mappings.
1088 */
1089 if (need_flushbuf)
1090 out_flush();
1091#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001092 if (ca.cmdchar != K_IGNORE)
1093 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
1095 State = NORMAL;
1096
1097 if (ca.nchar == ESC)
1098 {
1099 clearop(oap);
1100 if (restart_edit == 0 && goto_im())
1101 restart_edit = 'a';
1102 goto normal_end;
1103 }
1104
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001105 if (ca.cmdchar != K_IGNORE)
1106 {
1107 msg_didout = FALSE; /* don't scroll screen up for normal command */
1108 msg_col = 0;
1109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 old_pos = curwin->w_cursor; /* remember where cursor was */
1112
1113 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1114 * mode. */
1115 if (!VIsual_active && km_startsel)
1116 {
1117 if (nv_cmds[idx].cmd_flags & NV_SS)
1118 {
1119 start_selection();
1120 unshift_special(&ca);
1121 idx = find_command(ca.cmdchar);
1122 }
1123 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1124 && (mod_mask & MOD_MASK_SHIFT))
1125 {
1126 start_selection();
1127 mod_mask &= ~MOD_MASK_SHIFT;
1128 }
1129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
1131 /*
1132 * Execute the command!
1133 * Call the command function found in the commands table.
1134 */
1135 ca.arg = nv_cmds[idx].cmd_arg;
1136 (nv_cmds[idx].cmd_func)(&ca);
1137
1138 /*
1139 * If we didn't start or finish an operator, reset oap->regname, unless we
1140 * need it later.
1141 */
1142 if (!finish_op
1143 && !oap->op_type
1144 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1145 {
1146 clearop(oap);
1147#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001148 {
1149 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001150
Bram Moolenaar536681b2011-05-10 16:12:45 +02001151 /* Adjust the register according to 'clipboard', so that when
1152 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001153# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001154 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001155# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001156 set_reg_var(regname);
1157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#endif
1159 }
1160
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001161 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001162 * character or "z333<cr>". */
1163 if (old_mapped_len > 0)
1164 old_mapped_len = typebuf_maplen();
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 /*
1167 * If an operation is pending, handle it...
1168 */
1169 do_pending_operator(&ca, old_col, FALSE);
1170
1171 /*
1172 * Wait for a moment when a message is displayed that will be overwritten
1173 * by the mode message.
1174 * In Visual mode and with "^O" in Insert mode, a short message will be
1175 * overwritten by the mode message. Wait a bit, until a key is hit.
1176 * In Visual mode, it's more important to keep the Visual area updated
1177 * than keeping a message (e.g. from a /pat search).
1178 * Only do this if the command was typed, not from a mapping.
1179 * Don't wait when emsg_silent is non-zero.
1180 * Also wait a bit after an error message, e.g. for "^O:".
1181 * Don't redraw the screen, it would remove the message.
1182 */
1183 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001184 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 || (VIsual_active
1187 && old_pos.lnum == curwin->w_cursor.lnum
1188 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 )
1190 && (clear_cmdline
1191 || redraw_cmdline)
1192 && (msg_didout || (msg_didany && msg_scroll))
1193 && !msg_nowait
1194 && KeyTyped)
1195 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 && (msg_scroll
1198 || emsg_on_display)))
1199 && oap->regname == 0
1200 && !(ca.retval & CA_COMMAND_BUSY)
1201 && stuff_empty()
1202 && typebuf_typed()
1203 && emsg_silent == 0
1204 && !did_wait_return
1205 && oap->op_type == OP_NOP)
1206 {
1207 int save_State = State;
1208
1209 /* Draw the cursor with the right shape here */
1210 if (restart_edit != 0)
1211 State = INSERT;
1212
1213 /* If need to redraw, and there is a "keep_msg", redraw before the
1214 * delay */
1215 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1216 {
1217 char_u *kmsg;
1218
1219 kmsg = keep_msg;
1220 keep_msg = NULL;
1221 /* showmode() will clear keep_msg, but we want to use it anyway */
1222 update_screen(0);
1223 /* now reset it, otherwise it's put in the history again */
1224 keep_msg = kmsg;
1225 msg_attr(kmsg, keep_msg_attr);
1226 vim_free(kmsg);
1227 }
1228 setcursor();
1229 cursor_on();
1230 out_flush();
1231 if (msg_scroll || emsg_on_display)
1232 ui_delay(1000L, TRUE); /* wait at least one second */
1233 ui_delay(3000L, FALSE); /* wait up to three seconds */
1234 State = save_State;
1235
1236 msg_scroll = FALSE;
1237 emsg_on_display = FALSE;
1238 }
1239
1240 /*
1241 * Finish up after executing a Normal mode command.
1242 */
1243normal_end:
1244
1245 msg_nowait = FALSE;
1246
1247 /* Reset finish_op, in case it was set */
1248#ifdef CURSOR_SHAPE
1249 c = finish_op;
1250#endif
1251 finish_op = FALSE;
1252#ifdef CURSOR_SHAPE
1253 /* Redraw the cursor with another shape, if we were in Operator-pending
1254 * mode or did a replace command. */
1255 if (c || ca.cmdchar == 'r')
1256 {
1257 ui_cursor_shape(); /* may show different cursor shape */
1258# ifdef FEAT_MOUSESHAPE
1259 update_mouseshape(-1);
1260# endif
1261 }
1262#endif
1263
1264#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001265 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001266 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 clear_showcmd();
1268#endif
1269
1270 checkpcmark(); /* check if we moved since setting pcmark */
1271 vim_free(ca.searchbuf);
1272
1273#ifdef FEAT_MBYTE
1274 if (has_mbyte)
1275 mb_adjust_cursor();
1276#endif
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (curwin->w_p_scb && toplevel)
1279 {
1280 validate_cursor(); /* may need to update w_leftcol */
1281 do_check_scrollbind(TRUE);
1282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283
Bram Moolenaar860cae12010-06-05 23:22:07 +02001284 if (curwin->w_p_crb && toplevel)
1285 {
1286 validate_cursor(); /* may need to update w_leftcol */
1287 do_check_cursorbind();
1288 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001289
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001290#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001291 /* don't go to Insert mode if a terminal has a running job */
1292 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001293 restart_edit = 0;
1294#endif
1295
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 /*
1297 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1298 * if still inside a mapping that started in Visual mode).
1299 * May switch from Visual to Select mode after CTRL-O command.
1300 */
1301 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1303 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 && !(ca.retval & CA_COMMAND_BUSY)
1305 && stuff_empty()
1306 && oap->regname == 0)
1307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (restart_VIsual_select == 1)
1309 {
1310 VIsual_select = TRUE;
1311 showmode();
1312 restart_VIsual_select = 0;
1313 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001314 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 (void)edit(restart_edit, FALSE, 1L);
1316 }
1317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (restart_VIsual_select == 2)
1319 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
1321 /* Save count before an operator for next time. */
1322 opcount = ca.opcount;
1323}
1324
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001325#ifdef FEAT_EVAL
1326/*
1327 * Set v:count and v:count1 according to "cap".
1328 * Set v:prevcount only when "set_prevcount" is TRUE.
1329 */
1330 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001331set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001332{
1333 long count = cap->count0;
1334
1335 /* multiply with cap->opcount the same way as above */
1336 if (cap->opcount != 0)
1337 count = cap->opcount * (count == 0 ? 1 : count);
1338 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1339 *set_prevcount = FALSE; /* only set v:prevcount once */
1340}
1341#endif
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343/*
1344 * Handle an operator after visual mode or when the movement is finished
1345 */
1346 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001347do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 oparg_T *oap = cap->oap;
1350 pos_T old_cursor;
1351 int empty_region_error;
1352 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001353#ifdef FEAT_LINEBREAK
1354 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 /* The visual area is remembered for redo */
1358 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1359 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001360 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001362 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001363#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365#endif
1366
1367#if defined(FEAT_CLIPBOARD)
1368 /*
1369 * Yank the visual area into the GUI selection register before we operate
1370 * on it and lose it forever.
1371 * Don't do it if a specific register was specified, so that ""x"*P works.
1372 * This could call do_pending_operator() recursively, but that's OK
1373 * because gui_yank will be TRUE for the nested call.
1374 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001375 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 && oap->op_type != OP_NOP
1377 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 && VIsual_active
1379 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 && oap->regname == 0)
1381 clip_auto_select();
1382#endif
1383 old_cursor = curwin->w_cursor;
1384
1385 /*
1386 * If an operation is pending, handle it...
1387 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001388 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001390#ifdef FEAT_LINEBREAK
1391 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001392 if (curwin->w_p_lbr)
1393 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001394 curwin->w_p_lbr = FALSE;
1395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 oap->is_VIsual = VIsual_active;
1397 if (oap->motion_force == 'V')
1398 oap->motion_type = MLINE;
1399 else if (oap->motion_force == 'v')
1400 {
1401 /* If the motion was linewise, "inclusive" will not have been set.
1402 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1403 if (oap->motion_type == MLINE)
1404 oap->inclusive = FALSE;
1405 /* If the motion already was characterwise, toggle "inclusive" */
1406 else if (oap->motion_type == MCHAR)
1407 oap->inclusive = !oap->inclusive;
1408 oap->motion_type = MCHAR;
1409 }
1410 else if (oap->motion_force == Ctrl_V)
1411 {
1412 /* Change line- or characterwise motion into Visual block mode. */
1413 VIsual_active = TRUE;
1414 VIsual = oap->start;
1415 VIsual_mode = Ctrl_V;
1416 VIsual_select = FALSE;
1417 VIsual_reselect = FALSE;
1418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
Bram Moolenaar7afea822013-04-24 18:34:45 +02001420 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1421 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001423 && ((!VIsual_active || oap->motion_force)
1424 /* Also redo Operator-pending Visual mode mappings */
1425 || (VIsual_active && cap->cmdchar == ':'
1426 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001427 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428#ifdef FEAT_FOLDING
1429 && oap->op_type != OP_FOLD
1430 && oap->op_type != OP_FOLDOPEN
1431 && oap->op_type != OP_FOLDOPENREC
1432 && oap->op_type != OP_FOLDCLOSE
1433 && oap->op_type != OP_FOLDCLOSEREC
1434 && oap->op_type != OP_FOLDDEL
1435 && oap->op_type != OP_FOLDDELREC
1436#endif
1437 )
1438 {
1439 prep_redo(oap->regname, cap->count0,
1440 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1441 oap->motion_force, cap->cmdchar, cap->nchar);
1442 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1443 {
1444 /*
1445 * If 'cpoptions' does not contain 'r', insert the search
1446 * pattern to really repeat the same command.
1447 */
1448 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001449 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 AppendToRedobuff(NL_STR);
1451 }
1452 else if (cap->cmdchar == ':')
1453 {
1454 /* do_cmdline() has stored the first typed line in
1455 * "repeat_cmdline". When several lines are typed repeating
1456 * won't be possible. */
1457 if (repeat_cmdline == NULL)
1458 ResetRedobuff();
1459 else
1460 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001461 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001463 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465 }
1466 }
1467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (redo_VIsual_busy)
1469 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001470 /* Redo of an operation on a Visual area. Use the same size from
1471 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 oap->start = curwin->w_cursor;
1473 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1474 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1475 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1476 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001477 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001479 if (VIsual_mode == 'v')
1480 {
1481 if (redo_VIsual_line_count <= 1)
1482 {
1483 validate_virtcol();
1484 curwin->w_curswant =
1485 curwin->w_virtcol + redo_VIsual_vcol - 1;
1486 }
1487 else
1488 curwin->w_curswant = redo_VIsual_vcol;
1489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001491 {
1492 curwin->w_curswant = MAXCOL;
1493 }
1494 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496 cap->count0 = redo_VIsual_count;
1497 if (redo_VIsual_count != 0)
1498 cap->count1 = redo_VIsual_count;
1499 else
1500 cap->count1 = 1;
1501 }
1502 else if (VIsual_active)
1503 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001504 if (!gui_yank)
1505 {
1506 /* Save the current VIsual area for '< and '> marks, and "gv" */
1507 curbuf->b_visual.vi_start = VIsual;
1508 curbuf->b_visual.vi_end = curwin->w_cursor;
1509 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001510 if (VIsual_mode_orig != NUL)
1511 {
1512 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1513 VIsual_mode_orig = NUL;
1514 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001515 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001517 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
1521 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001522 * characterwise selection.
1523 * Special case: gH<Del> deletes the last line. */
1524 if (VIsual_select && VIsual_mode == 'V'
1525 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001527 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 VIsual.col = 0;
1530 curwin->w_cursor.col =
1531 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1532 }
1533 else
1534 {
1535 curwin->w_cursor.col = 0;
1536 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1537 }
1538 VIsual_mode = 'v';
1539 }
1540 /* If 'selection' is "exclusive", backup one character for
1541 * charwise selections. */
1542 else if (VIsual_mode == 'v')
1543 {
1544# ifdef FEAT_VIRTUALEDIT
1545 include_line_break =
1546# endif
1547 unadjust_for_sel();
1548 }
1549
1550 oap->start = VIsual;
1551 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001554# ifdef FEAT_VIRTUALEDIT
1555 oap->start.coladd = 0;
1556# endif
1557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
1560 /*
1561 * Set oap->start to the first position of the operated text, oap->end
1562 * to the end of the operated text. w_cursor is equal to oap->start.
1563 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001564 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
1566#ifdef FEAT_FOLDING
1567 /* Include folded lines completely. */
1568 if (!VIsual_active)
1569 {
1570 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1571 oap->start.col = 0;
1572 if (hasFolding(curwin->w_cursor.lnum, NULL,
1573 &curwin->w_cursor.lnum))
1574 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1575 }
1576#endif
1577 oap->end = curwin->w_cursor;
1578 curwin->w_cursor = oap->start;
1579
1580 /* w_virtcol may have been updated; if the cursor goes back to its
1581 * previous position w_virtcol becomes invalid and isn't updated
1582 * automatically. */
1583 curwin->w_valid &= ~VALID_VIRTCOL;
1584 }
1585 else
1586 {
1587#ifdef FEAT_FOLDING
1588 /* Include folded lines completely. */
1589 if (!VIsual_active && oap->motion_type == MLINE)
1590 {
1591 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1592 NULL))
1593 curwin->w_cursor.col = 0;
1594 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1595 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1596 }
1597#endif
1598 oap->end = oap->start;
1599 oap->start = curwin->w_cursor;
1600 }
1601
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001602 /* Just in case lines were deleted that make the position invalid. */
1603 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1605
1606#ifdef FEAT_VIRTUALEDIT
1607 /* Set "virtual_op" before resetting VIsual_active. */
1608 virtual_op = virtual_active();
1609#endif
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 if (VIsual_active || redo_VIsual_busy)
1612 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001613 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 if (!redo_VIsual_busy && !gui_yank)
1616 {
1617 /*
1618 * Prepare to reselect and redo Visual: this is based on the
1619 * size of the Visual text
1620 */
1621 resel_VIsual_mode = VIsual_mode;
1622 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001623 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001625 {
1626 if (VIsual_mode != Ctrl_V)
1627 getvvcol(curwin, &(oap->end),
1628 NULL, NULL, &oap->end_vcol);
1629 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1630 {
1631 if (VIsual_mode != Ctrl_V)
1632 getvvcol(curwin, &(oap->start),
1633 &oap->start_vcol, NULL, NULL);
1634 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1635 }
1636 else
1637 resel_VIsual_vcol = oap->end_vcol;
1638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 resel_VIsual_line_count = oap->line_count;
1640 }
1641
1642 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1643 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1644 && oap->op_type != OP_COLON
1645#ifdef FEAT_FOLDING
1646 && oap->op_type != OP_FOLD
1647 && oap->op_type != OP_FOLDOPEN
1648 && oap->op_type != OP_FOLDOPENREC
1649 && oap->op_type != OP_FOLDCLOSE
1650 && oap->op_type != OP_FOLDCLOSEREC
1651 && oap->op_type != OP_FOLDDEL
1652 && oap->op_type != OP_FOLDDELREC
1653#endif
1654 && oap->motion_force == NUL
1655 )
1656 {
1657 /* Prepare for redoing. Only use the nchar field for "r",
1658 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001659 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1660 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001661 prep_redo(oap->regname, cap->count0,
1662 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1663 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001664 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001665 {
1666 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1667
1668 /* reverse what nv_replace() did */
1669 if (nchar == REPLACE_CR_NCHAR)
1670 nchar = CAR;
1671 else if (nchar == REPLACE_NL_NCHAR)
1672 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001673 prep_redo(oap->regname, 0L, NUL, 'v',
1674 get_op_char(oap->op_type),
1675 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001676 nchar);
1677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (!redo_VIsual_busy)
1679 {
1680 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001681 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 redo_VIsual_line_count = resel_VIsual_line_count;
1683 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001684 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
1686 }
1687
1688 /*
1689 * oap->inclusive defaults to TRUE.
1690 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1691 * FALSE. This makes "d}P" and "v}dP" work the same.
1692 */
1693 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1694 oap->inclusive = TRUE;
1695 if (VIsual_mode == 'V')
1696 oap->motion_type = MLINE;
1697 else
1698 {
1699 oap->motion_type = MCHAR;
1700 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001701#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 )
1705 {
1706 oap->inclusive = FALSE;
1707 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001708 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001709 if (*p_sel != 'o'
1710 && !op_on_lines(oap->op_type)
1711 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001713 ++oap->end.lnum;
1714 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001715#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001716 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001717#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001718 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 }
1721 }
1722
1723 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 /*
1726 * Switch Visual off now, so screen updating does
1727 * not show inverted text when the screen is redrawn.
1728 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1729 * no screen redraw, so it is done here to remove the inverted
1730 * part.
1731 */
1732 if (!gui_yank)
1733 {
1734 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001735#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 setmouse();
1737 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001738#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001739 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 if ((oap->op_type == OP_YANK
1741 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001742 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 || oap->op_type == OP_FILTER)
1744 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001745 {
1746#ifdef FEAT_LINEBREAK
1747 /* make sure redrawing is correct */
1748 curwin->w_p_lbr = lbr_saved;
1749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 }
1753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755#ifdef FEAT_MBYTE
1756 /* Include the trailing byte of a multi-byte char. */
1757 if (has_mbyte && oap->inclusive)
1758 {
1759 int l;
1760
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001761 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (l > 1)
1763 oap->end.col += l - 1;
1764 }
1765#endif
1766 curwin->w_set_curswant = TRUE;
1767
1768 /*
1769 * oap->empty is set when start and end are the same. The inclusive
1770 * flag affects this too, unless yanking and the end is on a NUL.
1771 */
1772 oap->empty = (oap->motion_type == MCHAR
1773 && (!oap->inclusive
1774 || (oap->op_type == OP_YANK
1775 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001776 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#ifdef FEAT_VIRTUALEDIT
1778 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1779#endif
1780 );
1781 /*
1782 * For delete, change and yank, it's an error to operate on an
1783 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1784 */
1785 empty_region_error = (oap->empty
1786 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 /* Force a redraw when operating on an empty Visual region, when
1789 * 'modifiable is off or creating a fold. */
1790 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001791#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001795 {
1796#ifdef FEAT_LINEBREAK
1797 curwin->w_p_lbr = lbr_saved;
1798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
1802 /*
1803 * If the end of an operator is in column one while oap->motion_type
1804 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1805 * character in the previous line. If op_start is on or before the
1806 * first non-blank in the line, the operator becomes linewise
1807 * (strange, but that's the way vi does it).
1808 */
1809 if ( oap->motion_type == MCHAR
1810 && oap->inclusive == FALSE
1811 && !(cap->retval & CA_NO_ADJ_OP_END)
1812 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001814 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 && oap->line_count > 1)
1816 {
1817 oap->end_adjusted = TRUE; /* remember that we did this */
1818 --oap->line_count;
1819 --oap->end.lnum;
1820 if (inindent(0))
1821 oap->motion_type = MLINE;
1822 else
1823 {
1824 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1825 if (oap->end.col)
1826 {
1827 --oap->end.col;
1828 oap->inclusive = TRUE;
1829 }
1830 }
1831 }
1832 else
1833 oap->end_adjusted = FALSE;
1834
1835 switch (oap->op_type)
1836 {
1837 case OP_LSHIFT:
1838 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001839 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 auto_format(FALSE, TRUE);
1841 break;
1842
1843 case OP_JOIN_NS:
1844 case OP_JOIN:
1845 if (oap->line_count < 2)
1846 oap->line_count = 2;
1847 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1848 curbuf->b_ml.ml_line_count)
1849 beep_flush();
1850 else
1851 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001852 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001853 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 auto_format(FALSE, TRUE);
1855 }
1856 break;
1857
1858 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001861 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001862 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001863 CancelRedo();
1864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 else
1866 {
1867 (void)op_delete(oap);
1868 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1869 u_save_cursor(); /* cursor line wasn't saved yet */
1870 auto_format(FALSE, TRUE);
1871 }
1872 break;
1873
1874 case OP_YANK:
1875 if (empty_region_error)
1876 {
1877 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001878 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001879 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001880 CancelRedo();
1881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 }
1883 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001884 {
1885#ifdef FEAT_LINEBREAK
1886 curwin->w_p_lbr = lbr_saved;
1887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 check_cursor_col();
1891 break;
1892
1893 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001896 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001897 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001898 CancelRedo();
1899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 else
1901 {
1902 /* This is a new edit command, not a restart. Need to
1903 * remember it to make 'insertmode' work with mappings for
1904 * Visual mode. But do this only once and not when typed and
1905 * 'insertmode' isn't set. */
1906 if (p_im || !KeyTyped)
1907 restart_edit_save = restart_edit;
1908 else
1909 restart_edit_save = 0;
1910 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001911#ifdef FEAT_LINEBREAK
1912 /* Restore linebreak, so that when the user edits it looks as
1913 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001914 if (curwin->w_p_lbr != lbr_saved)
1915 {
1916 curwin->w_p_lbr = lbr_saved;
1917 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1918 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 /* Reset finish_op now, don't want it set inside edit(). */
1921 finish_op = FALSE;
1922 if (op_change(oap)) /* will call edit() */
1923 cap->retval |= CA_COMMAND_BUSY;
1924 if (restart_edit == 0)
1925 restart_edit = restart_edit_save;
1926 }
1927 break;
1928
1929 case OP_FILTER:
1930 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1931 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1932 else
1933 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001934 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
1936 case OP_INDENT:
1937 case OP_COLON:
1938
1939#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1940 /*
1941 * If 'equalprg' is empty, do the indenting internally.
1942 */
1943 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1944 {
1945# ifdef FEAT_LISP
1946 if (curbuf->b_p_lisp)
1947 {
1948 op_reindent(oap, get_lisp_indent);
1949 break;
1950 }
1951# endif
1952# ifdef FEAT_CINDENT
1953 op_reindent(oap,
1954# ifdef FEAT_EVAL
1955 *curbuf->b_p_inde != NUL ? get_expr_indent :
1956# endif
1957 get_c_indent);
1958 break;
1959# endif
1960 }
1961#endif
1962
1963 op_colon(oap);
1964 break;
1965
1966 case OP_TILDE:
1967 case OP_UPPER:
1968 case OP_LOWER:
1969 case OP_ROT13:
1970 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001971 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001972 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001973 CancelRedo();
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 else
1976 op_tilde(oap);
1977 check_cursor_col();
1978 break;
1979
1980 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001981#if defined(FEAT_EVAL)
1982 if (*curbuf->b_p_fex != NUL)
1983 op_formatexpr(oap); /* use expression */
1984 else
1985#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001986 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 op_colon(oap); /* use external command */
1988 else
1989 op_format(oap, FALSE); /* use internal function */
1990 break;
1991
1992 case OP_FORMAT2:
1993 op_format(oap, TRUE); /* use internal function */
1994 break;
1995
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001996 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001997#ifdef FEAT_LINEBREAK
1998 /* Restore linebreak, so that when the user edits it looks as
1999 * before. */
2000 curwin->w_p_lbr = lbr_saved;
2001#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002002 op_function(oap); /* call 'operatorfunc' */
2003 break;
2004
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 case OP_INSERT:
2006 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008#ifdef FEAT_VISUALEXTRA
2009 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002010 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002011 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002012 CancelRedo();
2013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 else
2015 {
2016 /* This is a new edit command, not a restart. Need to
2017 * remember it to make 'insertmode' work with mappings for
2018 * Visual mode. But do this only once. */
2019 restart_edit_save = restart_edit;
2020 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002021#ifdef FEAT_LINEBREAK
2022 /* Restore linebreak, so that when the user edits it looks as
2023 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002024 if (curwin->w_p_lbr != lbr_saved)
2025 {
2026 curwin->w_p_lbr = lbr_saved;
2027 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2028 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002031#ifdef FEAT_LINEBREAK
2032 /* Reset linebreak, so that formatting works correctly. */
2033 curwin->w_p_lbr = FALSE;
2034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 /* TODO: when inserting in several lines, should format all
2037 * the lines. */
2038 auto_format(FALSE, TRUE);
2039
2040 if (restart_edit == 0)
2041 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002042 else
2043 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002046 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047#endif
2048 break;
2049
2050 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#ifdef FEAT_VISUALEXTRA
2053 if (empty_region_error)
2054#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002055 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002056 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002057 CancelRedo();
2058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059#ifdef FEAT_VISUALEXTRA
2060 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002061 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002062# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002063 /* Restore linebreak, so that when the user edits it looks as
2064 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002065 if (curwin->w_p_lbr != lbr_saved)
2066 {
2067 curwin->w_p_lbr = lbr_saved;
2068 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2069 }
2070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073#endif
2074 break;
2075
2076#ifdef FEAT_FOLDING
2077 case OP_FOLD:
2078 VIsual_reselect = FALSE; /* don't reselect now */
2079 foldCreate(oap->start.lnum, oap->end.lnum);
2080 break;
2081
2082 case OP_FOLDOPEN:
2083 case OP_FOLDOPENREC:
2084 case OP_FOLDCLOSE:
2085 case OP_FOLDCLOSEREC:
2086 VIsual_reselect = FALSE; /* don't reselect now */
2087 opFoldRange(oap->start.lnum, oap->end.lnum,
2088 oap->op_type == OP_FOLDOPEN
2089 || oap->op_type == OP_FOLDOPENREC,
2090 oap->op_type == OP_FOLDOPENREC
2091 || oap->op_type == OP_FOLDCLOSEREC,
2092 oap->is_VIsual);
2093 break;
2094
2095 case OP_FOLDDEL:
2096 case OP_FOLDDELREC:
2097 VIsual_reselect = FALSE; /* don't reselect now */
2098 deleteFold(oap->start.lnum, oap->end.lnum,
2099 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2100 break;
2101#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002102 case OP_NR_ADD:
2103 case OP_NR_SUB:
2104 if (empty_region_error)
2105 {
2106 vim_beep(BO_OPER);
2107 CancelRedo();
2108 }
2109 else
2110 {
2111 VIsual_active = TRUE;
2112#ifdef FEAT_LINEBREAK
2113 curwin->w_p_lbr = lbr_saved;
2114#endif
2115 op_addsub(oap, cap->count1, redo_VIsual_arg);
2116 VIsual_active = FALSE;
2117 }
2118 check_cursor_col();
2119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 default:
2121 clearopbeep(oap);
2122 }
2123#ifdef FEAT_VIRTUALEDIT
2124 virtual_op = MAYBE;
2125#endif
2126 if (!gui_yank)
2127 {
2128 /*
2129 * if 'sol' not set, go back to old column for some commands
2130 */
2131 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2132 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2133 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002134 {
2135#ifdef FEAT_LINEBREAK
2136 curwin->w_p_lbr = FALSE;
2137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141 else
2142 {
2143 curwin->w_cursor = old_cursor;
2144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 clearop(oap);
2147 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002148#ifdef FEAT_LINEBREAK
2149 curwin->w_p_lbr = lbr_saved;
2150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151}
2152
2153/*
2154 * Handle indent and format operators and visual mode ":".
2155 */
2156 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002157op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158{
2159 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (oap->is_VIsual)
2161 stuffReadbuff((char_u *)"'<,'>");
2162 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164 /*
2165 * Make the range look nice, so it can be repeated.
2166 */
2167 if (oap->start.lnum == curwin->w_cursor.lnum)
2168 stuffcharReadbuff('.');
2169 else
2170 stuffnumReadbuff((long)oap->start.lnum);
2171 if (oap->end.lnum != oap->start.lnum)
2172 {
2173 stuffcharReadbuff(',');
2174 if (oap->end.lnum == curwin->w_cursor.lnum)
2175 stuffcharReadbuff('.');
2176 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2177 stuffcharReadbuff('$');
2178 else if (oap->start.lnum == curwin->w_cursor.lnum)
2179 {
2180 stuffReadbuff((char_u *)".+");
2181 stuffnumReadbuff((long)oap->line_count - 1);
2182 }
2183 else
2184 stuffnumReadbuff((long)oap->end.lnum);
2185 }
2186 }
2187 if (oap->op_type != OP_COLON)
2188 stuffReadbuff((char_u *)"!");
2189 if (oap->op_type == OP_INDENT)
2190 {
2191#ifndef FEAT_CINDENT
2192 if (*get_equalprg() == NUL)
2193 stuffReadbuff((char_u *)"indent");
2194 else
2195#endif
2196 stuffReadbuff(get_equalprg());
2197 stuffReadbuff((char_u *)"\n");
2198 }
2199 else if (oap->op_type == OP_FORMAT)
2200 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002201 if (*curbuf->b_p_fp != NUL)
2202 stuffReadbuff(curbuf->b_p_fp);
2203 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002205 else
2206 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002207 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209
2210 /*
2211 * do_cmdline() does the rest
2212 */
2213}
2214
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002215/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002216 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002217 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002219op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002220{
2221#ifdef FEAT_EVAL
2222 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002223# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002224 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002225# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002226
2227 if (*p_opfunc == NUL)
2228 EMSG(_("E774: 'operatorfunc' is empty"));
2229 else
2230 {
2231 /* Set '[ and '] marks to text to be operated on. */
2232 curbuf->b_op_start = oap->start;
2233 curbuf->b_op_end = oap->end;
2234 if (oap->motion_type != MLINE && !oap->inclusive)
2235 /* Exclude the end position. */
2236 decl(&curbuf->b_op_end);
2237
2238 if (oap->block_mode)
2239 argv[0] = (char_u *)"block";
2240 else if (oap->motion_type == MLINE)
2241 argv[0] = (char_u *)"line";
2242 else
2243 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002244
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002245# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002246 /* Reset virtual_op so that 'virtualedit' can be changed in the
2247 * function. */
2248 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002249# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002250
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002251 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002252
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002253# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002254 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002255# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002256 }
2257#else
2258 EMSG(_("E775: Eval feature not available"));
2259#endif
2260}
2261
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#if defined(FEAT_MOUSE) || defined(PROTO)
2263/*
2264 * Do the appropriate action for the current mouse click in the current mode.
2265 * Not used for Command-line mode.
2266 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002267 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 * event modi- position visual change action
2269 * fier cursor window
2270 * left press - yes end yes
2271 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002272 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 * left drag - yes start if moved no
2274 * left relse - yes start if moved no
2275 * middle press - yes if not active no put register
2276 * middle press - yes if active no yank and put
2277 * right press - yes start or extend yes
2278 * right press S yes no change yes "#" (2)
2279 * right drag - yes extend no
2280 * right relse - yes extend no
2281 *
2282 * Insert or Replace Mode:
2283 * event modi- position visual change action
2284 * fier cursor window
2285 * left press - yes (cannot be active) yes
2286 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2287 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2288 * left drag - yes start or extend (1) no CTRL-O (1)
2289 * left relse - yes start or extend (1) no CTRL-O (1)
2290 * middle press - no (cannot be active) no put register
2291 * right press - yes start or extend yes CTRL-O
2292 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2293 *
2294 * (1) only if mouse pointer moved since press
2295 * (2) only if click is in same buffer
2296 *
2297 * Return TRUE if start_arrow() should be called for edit mode.
2298 */
2299 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002300do_mouse(
2301 oparg_T *oap, /* operator argument, can be NULL */
2302 int c, /* K_LEFTMOUSE, etc */
2303 int dir, /* Direction to 'put' if necessary */
2304 long count,
2305 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2308 static int got_click = FALSE; /* got a click some time back */
2309
2310 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2311 int is_click; /* If FALSE it's a drag or release event */
2312 int is_drag; /* If TRUE it's a drag event */
2313 int jump_flags = 0; /* flags for jump_to_mouse() */
2314 pos_T start_visual;
2315 int moved; /* Has cursor moved? */
2316 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002317 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 int c1, c2;
2320#if defined(FEAT_FOLDING)
2321 pos_T save_cursor;
2322#endif
2323 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 static pos_T orig_cursor;
2325 colnr_T leftcol, rightcol;
2326 pos_T end_visual;
2327 int diff;
2328 int old_active = VIsual_active;
2329 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 int regname;
2331
2332#if defined(FEAT_FOLDING)
2333 save_cursor = curwin->w_cursor;
2334#endif
2335
2336 /*
2337 * When GUI is active, always recognize mouse events, otherwise:
2338 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2339 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2340 * - For command line and insert mode 'mouse' is checked before calling
2341 * do_mouse().
2342 */
2343 if (do_always)
2344 do_always = FALSE;
2345 else
2346#ifdef FEAT_GUI
2347 if (!gui.in_use)
2348#endif
2349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 if (VIsual_active)
2351 {
2352 if (!mouse_has(MOUSE_VISUAL))
2353 return FALSE;
2354 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002355 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 return FALSE;
2357 }
2358
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002359 for (;;)
2360 {
2361 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2362 if (is_drag)
2363 {
2364 /* If the next character is the same mouse event then use that
2365 * one. Speeds up dragging the status line. */
2366 if (vpeekc() != NUL)
2367 {
2368 int nc;
2369 int save_mouse_row = mouse_row;
2370 int save_mouse_col = mouse_col;
2371
2372 /* Need to get the character, peeking doesn't get the actual
2373 * one. */
2374 nc = safe_vgetc();
2375 if (c == nc)
2376 continue;
2377 vungetc(nc);
2378 mouse_row = save_mouse_row;
2379 mouse_col = save_mouse_col;
2380 }
2381 }
2382 break;
2383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002385 if (c == K_MOUSEMOVE)
2386 {
2387 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002388#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002389 ui_may_remove_balloon();
2390 if (p_bevalterm && !VIsual_active)
2391 {
2392 profile_setlimit(p_bdlay, &bevalexpr_due);
2393 bevalexpr_due_set = TRUE;
2394 }
2395#endif
2396 return FALSE;
2397 }
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399#ifdef FEAT_MOUSESHAPE
2400 /* May have stopped dragging the status or separator line. The pointer is
2401 * most likely still on the status or separator line. */
2402 if (!is_drag && drag_status_line)
2403 {
2404 drag_status_line = FALSE;
2405 update_mouseshape(SHAPE_IDX_STATUS);
2406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (!is_drag && drag_sep_line)
2408 {
2409 drag_sep_line = FALSE;
2410 update_mouseshape(SHAPE_IDX_VSEP);
2411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#endif
2413
2414 /*
2415 * Ignore drag and release events if we didn't get a click.
2416 */
2417 if (is_click)
2418 got_click = TRUE;
2419 else
2420 {
2421 if (!got_click) /* didn't get click, ignore */
2422 return FALSE;
2423 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002426 if (in_tab_line)
2427 {
2428 in_tab_line = FALSE;
2429 return FALSE;
2430 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 /*
2435 * CTRL right mouse button does CTRL-T
2436 */
2437 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2438 {
2439 if (State & INSERT)
2440 stuffcharReadbuff(Ctrl_O);
2441 if (count > 1)
2442 stuffnumReadbuff(count);
2443 stuffcharReadbuff(Ctrl_T);
2444 got_click = FALSE; /* ignore drag&release now */
2445 return FALSE;
2446 }
2447
2448 /*
2449 * CTRL only works with left mouse button
2450 */
2451 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2452 return FALSE;
2453
2454 /*
2455 * When a modifier is down, ignore drag and release events, as well as
2456 * multiple clicks and the middle mouse button.
2457 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2458 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002459 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2460 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 && (!is_click
2462 || (mod_mask & MOD_MASK_MULTI_CLICK)
2463 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002464 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 && mouse_model_popup()
2466 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002467 && !((mod_mask & MOD_MASK_ALT)
2468 && !mouse_model_popup()
2469 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 )
2471 return FALSE;
2472
2473 /*
2474 * If the button press was used as the movement command for an operator
2475 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2476 * drag/release events.
2477 */
2478 if (!is_click && which_button == MOUSE_MIDDLE)
2479 return FALSE;
2480
2481 if (oap != NULL)
2482 regname = oap->regname;
2483 else
2484 regname = 0;
2485
2486 /*
2487 * Middle mouse button does a 'put' of the selected text
2488 */
2489 if (which_button == MOUSE_MIDDLE)
2490 {
2491 if (State == NORMAL)
2492 {
2493 /*
2494 * If an operator was pending, we don't know what the user wanted
2495 * to do. Go back to normal mode: Clear the operator and beep().
2496 */
2497 if (oap != NULL && oap->op_type != OP_NOP)
2498 {
2499 clearopbeep(oap);
2500 return FALSE;
2501 }
2502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 /*
2504 * If visual was active, yank the highlighted text and put it
2505 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002506 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 */
2508 if (VIsual_active)
2509 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002510 if (VIsual_select)
2511 {
2512 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002513 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002514 }
2515 else
2516 {
2517 stuffcharReadbuff('y');
2518 stuffcharReadbuff(K_MIDDLEMOUSE);
2519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 do_always = TRUE; /* ignore 'mouse' setting next time */
2521 return FALSE;
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 /*
2524 * The rest is below jump_to_mouse()
2525 */
2526 }
2527
2528 else if ((State & INSERT) == 0)
2529 return FALSE;
2530
2531 /*
2532 * Middle click in insert mode doesn't move the mouse, just insert the
2533 * contents of a register. '.' register is special, can't insert that
2534 * with do_put().
2535 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2536 * happens for the GUI).
2537 */
2538 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2539 {
2540 if (regname == '.')
2541 insert_reg(regname, TRUE);
2542 else
2543 {
2544#ifdef FEAT_CLIPBOARD
2545 if (clip_star.available && regname == 0)
2546 regname = '*';
2547#endif
2548 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2549 insert_reg(regname, TRUE);
2550 else
2551 {
2552 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2553
2554 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2555 AppendCharToRedobuff(Ctrl_R);
2556 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2557 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2558 }
2559 }
2560 return FALSE;
2561 }
2562 }
2563
2564 /* When dragging or button-up stay in the same window. */
2565 if (!is_click)
2566 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2567
2568 start_visual.lnum = 0;
2569
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 /* Check for clicking in the tab page line. */
2571 if (mouse_row == 0 && firstwin->w_winrow > 0)
2572 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002573 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002574 {
2575 if (in_tab_line)
2576 {
2577 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002578 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2579 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002580 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002581 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002582 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002583
Bram Moolenaarf740b292006-02-16 22:11:02 +00002584 /* click in a tab selects that tab page */
2585 if (is_click
2586# ifdef FEAT_CMDWIN
2587 && cmdwin_type == 0
2588# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002589 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002590 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002591 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002592 c1 = TabPageIdxs[mouse_col];
2593 if (c1 >= 0)
2594 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002595 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2596 {
2597 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002598 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002599 tabpage_new();
2600 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2601 }
2602 else
2603 {
2604 /* Go to specified tab page, or next one if not clicking
2605 * on a label. */
2606 goto_tabpage(c1);
2607
2608 /* It's like clicking on the status line of a window. */
2609 if (curwin != old_curwin)
2610 end_visual_mode();
2611 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002612 }
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002613 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002614 {
2615 tabpage_T *tp;
2616
2617 /* Close the current or specified tab page. */
2618 if (c1 == -999)
2619 tp = curtab;
2620 else
2621 tp = find_tabpage(-c1);
2622 if (tp == curtab)
2623 {
2624 if (first_tabpage->tp_next != NULL)
2625 tabpage_close(FALSE);
2626 }
2627 else if (tp != NULL)
2628 tabpage_close_other(tp, FALSE);
2629 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002630 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002631 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002632 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002633 else if (is_drag && in_tab_line)
2634 {
2635 c1 = TabPageIdxs[mouse_col];
2636 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2637 return FALSE;
2638 }
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 /*
2641 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2642 * right button up -> pop-up menu
2643 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002644 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 */
2646 if (mouse_model_popup())
2647 {
2648 if (which_button == MOUSE_RIGHT
2649 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2650 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002651#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002653 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2654 || defined(FEAT_TERM_POPUP_MENU)
2655# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if (gui.in_use)
2657 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002658# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2659 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2660 if (!is_click)
2661 /* Ignore right button release events, only shows the popup
2662 * menu on the button down event. */
2663 return FALSE;
2664# endif
2665# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2666 if (is_click || is_drag)
2667 /* Ignore right button down and drag mouse events. Windows
2668 * only shows the popup menu on the button up event. */
2669 return FALSE;
2670# endif
2671 }
2672# endif
2673# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2674 else
2675# endif
2676# if defined(FEAT_TERM_POPUP_MENU)
2677 if (!is_click)
2678 /* Ignore right button release events, only shows the popup
2679 * menu on the button down event. */
2680 return FALSE;
2681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002683 jump_flags = 0;
2684 if (STRCMP(p_mousem, "popup_setpos") == 0)
2685 {
2686 /* First set the cursor position before showing the popup
2687 * menu. */
2688 if (VIsual_active)
2689 {
2690 pos_T m_pos;
2691
2692 /*
2693 * set MOUSE_MAY_STOP_VIS if we are outside the
2694 * selection or the current window (might have false
2695 * negative here)
2696 */
2697 if (mouse_row < curwin->w_winrow
2698 || mouse_row
2699 > (curwin->w_winrow + curwin->w_height))
2700 jump_flags = MOUSE_MAY_STOP_VIS;
2701 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2702 jump_flags = MOUSE_MAY_STOP_VIS;
2703 else
2704 {
2705 if ((LT_POS(curwin->w_cursor, VIsual)
2706 && (LT_POS(m_pos, curwin->w_cursor)
2707 || LT_POS(VIsual, m_pos)))
2708 || (LT_POS(VIsual, curwin->w_cursor)
2709 && (LT_POS(m_pos, VIsual)
2710 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002712 jump_flags = MOUSE_MAY_STOP_VIS;
2713 }
2714 else if (VIsual_mode == Ctrl_V)
2715 {
2716 getvcols(curwin, &curwin->w_cursor, &VIsual,
2717 &leftcol, &rightcol);
2718 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2719 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002724 else
2725 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002727 if (jump_flags)
2728 {
2729 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2730 update_curbuf(VIsual_active ? INVERTED : VALID);
2731 setcursor();
2732 out_flush(); /* Update before showing popup menu */
2733 }
2734# ifdef FEAT_MENU
2735 show_popupmenu();
2736 got_click = FALSE; /* ignore release events */
2737# endif
2738 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739#else
2740 return FALSE;
2741#endif
2742 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002743 if (which_button == MOUSE_LEFT
2744 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 {
2746 which_button = MOUSE_RIGHT;
2747 mod_mask &= ~MOD_MASK_SHIFT;
2748 }
2749 }
2750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if ((State & (NORMAL | INSERT))
2752 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2753 {
2754 if (which_button == MOUSE_LEFT)
2755 {
2756 if (is_click)
2757 {
2758 /* stop Visual mode for a left click in a window, but not when
2759 * on a status line */
2760 if (VIsual_active)
2761 jump_flags |= MOUSE_MAY_STOP_VIS;
2762 }
2763 else if (mouse_has(MOUSE_VISUAL))
2764 jump_flags |= MOUSE_MAY_VIS;
2765 }
2766 else if (which_button == MOUSE_RIGHT)
2767 {
2768 if (is_click && VIsual_active)
2769 {
2770 /*
2771 * Remember the start and end of visual before moving the
2772 * cursor.
2773 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002774 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
2776 start_visual = curwin->w_cursor;
2777 end_visual = VIsual;
2778 }
2779 else
2780 {
2781 start_visual = VIsual;
2782 end_visual = curwin->w_cursor;
2783 }
2784 }
2785 jump_flags |= MOUSE_FOCUS;
2786 if (mouse_has(MOUSE_VISUAL))
2787 jump_flags |= MOUSE_MAY_VIS;
2788 }
2789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 /*
2792 * If an operator is pending, ignore all drags and releases until the
2793 * next mouse click.
2794 */
2795 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2796 {
2797 got_click = FALSE;
2798 oap->motion_type = MCHAR;
2799 }
2800
2801 /* When releasing the button let jump_to_mouse() know. */
2802 if (!is_click && !is_drag)
2803 jump_flags |= MOUSE_RELEASED;
2804
2805 /*
2806 * JUMP!
2807 */
2808 jump_flags = jump_to_mouse(jump_flags,
2809 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002810
2811#ifdef FEAT_MENU
2812 /* A click in the window toolbar has no side effects. */
2813 if (jump_flags & MOUSE_WINBAR)
2814 return FALSE;
2815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 moved = (jump_flags & CURSOR_MOVED);
2817 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819
2820#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002821 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2823 {
2824 int key = KEY2TERMCAP1(c);
2825
2826 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2827 || key == (int)KE_RIGHTRELEASE)
2828 netbeans_button_release(which_button);
2829 }
2830#endif
2831
2832 /* When jumping to another window, clear a pending operator. That's a bit
2833 * friendlier than beeping and not jumping to that window. */
2834 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2835 clearop(oap);
2836
2837#ifdef FEAT_FOLDING
2838 if (mod_mask == 0
2839 && !is_drag
2840 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2841 && which_button == MOUSE_LEFT)
2842 {
2843 /* open or close a fold at this line */
2844 if (jump_flags & MOUSE_FOLD_OPEN)
2845 openFold(curwin->w_cursor.lnum, 1L);
2846 else
2847 closeFold(curwin->w_cursor.lnum, 1L);
2848 /* don't move the cursor if still in the same window */
2849 if (curwin == old_curwin)
2850 curwin->w_cursor = save_cursor;
2851 }
2852#endif
2853
2854#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2855 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2856 {
2857 clip_modeless(which_button, is_click, is_drag);
2858 return FALSE;
2859 }
2860#endif
2861
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002863 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 if (VIsual_active && is_drag && p_so)
2865 {
2866 /* In the very first line, allow scrolling one line */
2867 if (mouse_row == 0)
2868 mouse_dragging = 2;
2869 else
2870 mouse_dragging = 1;
2871 }
2872
2873 /* When dragging the mouse above the window, scroll down. */
2874 if (is_drag && mouse_row < 0 && !in_status_line)
2875 {
2876 scroll_redraw(FALSE, 1L);
2877 mouse_row = 0;
2878 }
2879
2880 if (start_visual.lnum) /* right click in visual mode */
2881 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002882 /* When ALT is pressed make Visual mode blockwise. */
2883 if (mod_mask & MOD_MASK_ALT)
2884 VIsual_mode = Ctrl_V;
2885
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 /*
2887 * In Visual-block mode, divide the area in four, pick up the corner
2888 * that is in the quarter that the cursor is in.
2889 */
2890 if (VIsual_mode == Ctrl_V)
2891 {
2892 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2893 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2894 end_visual.col = leftcol;
2895 else
2896 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002897 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 end_visual.lnum = start_visual.lnum;
2900
2901 /* move VIsual to the right column */
2902 start_visual = curwin->w_cursor; /* save the cursor pos */
2903 curwin->w_cursor = end_visual;
2904 coladvance(end_visual.col);
2905 VIsual = curwin->w_cursor;
2906 curwin->w_cursor = start_visual; /* restore the cursor */
2907 }
2908 else
2909 {
2910 /*
2911 * If the click is before the start of visual, change the start.
2912 * If the click is after the end of visual, change the end. If
2913 * the click is inside the visual, change the closest side.
2914 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002915 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002917 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 VIsual = start_visual;
2919 else
2920 {
2921 /* In the same line, compare column number */
2922 if (end_visual.lnum == start_visual.lnum)
2923 {
2924 if (curwin->w_cursor.col - start_visual.col >
2925 end_visual.col - curwin->w_cursor.col)
2926 VIsual = start_visual;
2927 else
2928 VIsual = end_visual;
2929 }
2930
2931 /* In different lines, compare line number */
2932 else
2933 {
2934 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2935 (end_visual.lnum - curwin->w_cursor.lnum);
2936
2937 if (diff > 0) /* closest to end */
2938 VIsual = start_visual;
2939 else if (diff < 0) /* closest to start */
2940 VIsual = end_visual;
2941 else /* in the middle line */
2942 {
2943 if (curwin->w_cursor.col <
2944 (start_visual.col + end_visual.col) / 2)
2945 VIsual = end_visual;
2946 else
2947 VIsual = start_visual;
2948 }
2949 }
2950 }
2951 }
2952 }
2953 /*
2954 * If Visual mode started in insert mode, execute "CTRL-O"
2955 */
2956 else if ((State & INSERT) && VIsual_active)
2957 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958
2959 /*
2960 * Middle mouse click: Put text before cursor.
2961 */
2962 if (which_button == MOUSE_MIDDLE)
2963 {
2964#ifdef FEAT_CLIPBOARD
2965 if (clip_star.available && regname == 0)
2966 regname = '*';
2967#endif
2968 if (yank_register_mline(regname))
2969 {
2970 if (mouse_past_bottom)
2971 dir = FORWARD;
2972 }
2973 else if (mouse_past_eol)
2974 dir = FORWARD;
2975
2976 if (fixindent)
2977 {
2978 c1 = (dir == BACKWARD) ? '[' : ']';
2979 c2 = 'p';
2980 }
2981 else
2982 {
2983 c1 = (dir == FORWARD) ? 'p' : 'P';
2984 c2 = NUL;
2985 }
2986 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2987
2988 /*
2989 * Remember where the paste started, so in edit() Insstart can be set
2990 * to this position
2991 */
2992 if (restart_edit != 0)
2993 where_paste_started = curwin->w_cursor;
2994 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2995 }
2996
Bram Moolenaar4033c552017-09-16 20:54:51 +02002997#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 /*
2999 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3000 * error under the mouse pointer.
3001 */
3002 else if (((mod_mask & MOD_MASK_CTRL)
3003 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3004 && bt_quickfix(curbuf))
3005 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003006 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003007 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003008 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003009 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 got_click = FALSE; /* ignore drag&release now */
3011 }
3012#endif
3013
3014 /*
3015 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3016 * under the mouse pointer.
3017 */
3018 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3019 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3020 {
3021 if (State & INSERT)
3022 stuffcharReadbuff(Ctrl_O);
3023 stuffcharReadbuff(Ctrl_RSB);
3024 got_click = FALSE; /* ignore drag&release now */
3025 }
3026
3027 /*
3028 * Shift-Mouse click searches for the next occurrence of the word under
3029 * the mouse pointer
3030 */
3031 else if ((mod_mask & MOD_MASK_SHIFT))
3032 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003033 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 stuffcharReadbuff(Ctrl_O);
3035 if (which_button == MOUSE_LEFT)
3036 stuffcharReadbuff('*');
3037 else /* MOUSE_RIGHT */
3038 stuffcharReadbuff('#');
3039 }
3040
3041 /* Handle double clicks, unless on status line */
3042 else if (in_status_line)
3043 {
3044#ifdef FEAT_MOUSESHAPE
3045 if ((is_drag || is_click) && !drag_status_line)
3046 {
3047 drag_status_line = TRUE;
3048 update_mouseshape(-1);
3049 }
3050#endif
3051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 else if (in_sep_line)
3053 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003054#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 if ((is_drag || is_click) && !drag_sep_line)
3056 {
3057 drag_sep_line = TRUE;
3058 update_mouseshape(-1);
3059 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003060#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3063 && mouse_has(MOUSE_VISUAL))
3064 {
3065 if (is_click || !VIsual_active)
3066 {
3067 if (VIsual_active)
3068 orig_cursor = VIsual;
3069 else
3070 {
3071 check_visual_highlight();
3072 VIsual = curwin->w_cursor;
3073 orig_cursor = VIsual;
3074 VIsual_active = TRUE;
3075 VIsual_reselect = TRUE;
3076 /* start Select mode if 'selectmode' contains "mouse" */
3077 may_start_select('o');
3078 setmouse();
3079 }
3080 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003081 {
3082 /* Double click with ALT pressed makes it blockwise. */
3083 if (mod_mask & MOD_MASK_ALT)
3084 VIsual_mode = Ctrl_V;
3085 else
3086 VIsual_mode = 'v';
3087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3089 VIsual_mode = 'V';
3090 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3091 VIsual_mode = Ctrl_V;
3092#ifdef FEAT_CLIPBOARD
3093 /* Make sure the clipboard gets updated. Needed because start and
3094 * end may still be the same, and the selection needs to be owned */
3095 clip_star.vmode = NUL;
3096#endif
3097 }
3098 /*
3099 * A double click selects a word or a block.
3100 */
3101 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3102 {
3103 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003104 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105
3106 if (is_click)
3107 {
3108 /* If the character under the cursor (skipping white space) is
3109 * not a word character, try finding a match and select a (),
3110 * {}, [], #if/#endif, etc. block. */
3111 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003112 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 inc(&end_visual);
3114 if (oap != NULL)
3115 oap->motion_type = MCHAR;
3116 if (oap != NULL
3117 && VIsual_mode == 'v'
3118 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003119 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 && (pos = findmatch(oap, NUL)) != NULL)
3121 {
3122 curwin->w_cursor = *pos;
3123 if (oap->motion_type == MLINE)
3124 VIsual_mode = 'V';
3125 else if (*p_sel == 'e')
3126 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003127 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 ++VIsual.col;
3129 else
3130 ++curwin->w_cursor.col;
3131 }
3132 }
3133 }
3134
3135 if (pos == NULL && (is_click || is_drag))
3136 {
3137 /* When not found a match or when dragging: extend to include
3138 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003139 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 {
3141 find_start_of_word(&curwin->w_cursor);
3142 find_end_of_word(&VIsual);
3143 }
3144 else
3145 {
3146 find_start_of_word(&VIsual);
3147 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3148#ifdef FEAT_MBYTE
3149 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003150 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151#else
3152 ++curwin->w_cursor.col;
3153#endif
3154 find_end_of_word(&curwin->w_cursor);
3155 }
3156 }
3157 curwin->w_set_curswant = TRUE;
3158 }
3159 if (is_click)
3160 redraw_curbuf_later(INVERTED); /* update the inversion */
3161 }
3162 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003163 {
3164 if (mod_mask & MOD_MASK_ALT)
3165 VIsual_mode = Ctrl_V;
3166 else
3167 VIsual_mode = 'v';
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003171 if ((!VIsual_active && old_active && mode_displayed)
3172 || (VIsual_active && p_smd && msg_silent == 0
3173 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 return moved;
3177}
3178
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179/*
3180 * Move "pos" back to the start of the word it's in.
3181 */
3182 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003183find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 char_u *line;
3186 int cclass;
3187 int col;
3188
3189 line = ml_get(pos->lnum);
3190 cclass = get_mouse_class(line + pos->col);
3191
3192 while (pos->col > 0)
3193 {
3194 col = pos->col - 1;
3195#ifdef FEAT_MBYTE
3196 col -= (*mb_head_off)(line, line + col);
3197#endif
3198 if (get_mouse_class(line + col) != cclass)
3199 break;
3200 pos->col = col;
3201 }
3202}
3203
3204/*
3205 * Move "pos" forward to the end of the word it's in.
3206 * When 'selection' is "exclusive", the position is just after the word.
3207 */
3208 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003209find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 char_u *line;
3212 int cclass;
3213 int col;
3214
3215 line = ml_get(pos->lnum);
3216 if (*p_sel == 'e' && pos->col > 0)
3217 {
3218 --pos->col;
3219#ifdef FEAT_MBYTE
3220 pos->col -= (*mb_head_off)(line, line + pos->col);
3221#endif
3222 }
3223 cclass = get_mouse_class(line + pos->col);
3224 while (line[pos->col] != NUL)
3225 {
3226#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003227 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228#else
3229 col = pos->col + 1;
3230#endif
3231 if (get_mouse_class(line + col) != cclass)
3232 {
3233 if (*p_sel == 'e')
3234 pos->col = col;
3235 break;
3236 }
3237 pos->col = col;
3238 }
3239}
3240
3241/*
3242 * Get class of a character for selection: same class means same word.
3243 * 0: blank
3244 * 1: punctuation groups
3245 * 2: normal word character
3246 * >2: multi-byte word character.
3247 */
3248 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003249get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 int c;
3252
3253#ifdef FEAT_MBYTE
3254 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3255 return mb_get_class(p);
3256#endif
3257
3258 c = *p;
3259 if (c == ' ' || c == '\t')
3260 return 0;
3261
3262 if (vim_iswordc(c))
3263 return 2;
3264
3265 /*
3266 * There are a few special cases where we want certain combinations of
3267 * characters to be considered as a single word. These are things like
3268 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003269 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 */
3271 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3272 return 1;
3273 return c;
3274}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#endif /* FEAT_MOUSE */
3276
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277/*
3278 * Check if highlighting for visual mode is possible, give a warning message
3279 * if not.
3280 */
3281 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003282check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 static int did_check = FALSE;
3285
3286 if (full_screen)
3287 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003288 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 MSG(_("Warning: terminal cannot highlight"));
3290 did_check = TRUE;
3291 }
3292}
3293
3294/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003295 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 * This function should ALWAYS be called to end Visual mode, except from
3297 * do_pending_operator().
3298 */
3299 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003300end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302#ifdef FEAT_CLIPBOARD
3303 /*
3304 * If we are using the clipboard, then remember what was selected in case
3305 * we need to paste it somewhere while we still own the selection.
3306 * Only do this when the clipboard is already owned. Don't want to grab
3307 * the selection when hitting ESC.
3308 */
3309 if (clip_star.available && clip_star.owned)
3310 clip_auto_select();
3311#endif
3312
3313 VIsual_active = FALSE;
3314#ifdef FEAT_MOUSE
3315 setmouse();
3316 mouse_dragging = 0;
3317#endif
3318
3319 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003320 curbuf->b_visual.vi_mode = VIsual_mode;
3321 curbuf->b_visual.vi_start = VIsual;
3322 curbuf->b_visual.vi_end = curwin->w_cursor;
3323 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#ifdef FEAT_EVAL
3325 curbuf->b_visual_mode_eval = VIsual_mode;
3326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#ifdef FEAT_VIRTUALEDIT
3328 if (!virtual_active())
3329 curwin->w_cursor.coladd = 0;
3330#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003331 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003333 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334}
3335
3336/*
3337 * Reset VIsual_active and VIsual_reselect.
3338 */
3339 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003340reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 if (VIsual_active)
3343 {
3344 end_visual_mode();
3345 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3346 }
3347 VIsual_reselect = FALSE;
3348}
3349
3350/*
3351 * Reset VIsual_active and VIsual_reselect if it's set.
3352 */
3353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003354reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 if (VIsual_active)
3357 {
3358 end_visual_mode();
3359 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3360 VIsual_reselect = FALSE;
3361 }
3362}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364/*
3365 * Check for a balloon-eval special item to include when searching for an
3366 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3367 * Returns TRUE if the character at "*ptr" should be included.
3368 * "dir" is FORWARD or BACKWARD, the direction of searching.
3369 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3370 * "bnp" points to a counter for square brackets.
3371 */
3372 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003373find_is_eval_item(
3374 char_u *ptr,
3375 int *colp,
3376 int *bnp,
3377 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 /* Accept everything inside []. */
3380 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3381 ++*bnp;
3382 if (*bnp > 0)
3383 {
3384 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3385 --*bnp;
3386 return TRUE;
3387 }
3388
3389 /* skip over "s.var" */
3390 if (*ptr == '.')
3391 return TRUE;
3392
3393 /* two-character item: s->var */
3394 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3395 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3396 {
3397 *colp += dir;
3398 return TRUE;
3399 }
3400 return FALSE;
3401}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
3403/*
3404 * Find the identifier under or to the right of the cursor.
3405 * "find_type" can have one of three values:
3406 * FIND_IDENT: find an identifier (keyword)
3407 * FIND_STRING: find any non-white string
3408 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003409 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 *
3411 * There are three steps:
3412 * 1. Search forward for the start of an identifier/string. Doesn't move if
3413 * already on one.
3414 * 2. Search backward for the start of this identifier/string.
3415 * This doesn't match the real Vi but I like it a little better and it
3416 * shouldn't bother anyone.
3417 * 3. Search forward to the end of this identifier/string.
3418 * When FIND_IDENT isn't defined, we backup until a blank.
3419 *
3420 * Returns the length of the string, or zero if no string is found.
3421 * If a string is found, a pointer to the string is put in "*string". This
3422 * string is not always NUL terminated.
3423 */
3424 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003425find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3428 curwin->w_cursor.col, string, find_type);
3429}
3430
3431/*
3432 * Like find_ident_under_cursor(), but for any window and any position.
3433 * However: Uses 'iskeyword' from the current window!.
3434 */
3435 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003436find_ident_at_pos(
3437 win_T *wp,
3438 linenr_T lnum,
3439 colnr_T startcol,
3440 char_u **string,
3441 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
3443 char_u *ptr;
3444 int col = 0; /* init to shut up GCC */
3445 int i;
3446#ifdef FEAT_MBYTE
3447 int this_class = 0;
3448 int prev_class;
3449 int prevcol;
3450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452
3453 /*
3454 * if i == 0: try to find an identifier
3455 * if i == 1: try to find any non-white string
3456 */
3457 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3458 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3459 {
3460 /*
3461 * 1. skip to start of identifier/string
3462 */
3463 col = startcol;
3464#ifdef FEAT_MBYTE
3465 if (has_mbyte)
3466 {
3467 while (ptr[col] != NUL)
3468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 /* Stop at a ']' to evaluate "a[x]". */
3470 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 this_class = mb_get_class(ptr + col);
3473 if (this_class != 0 && (i == 1 || this_class != 1))
3474 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003475 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 }
3477 }
3478 else
3479#endif
3480 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003481 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 )
3484 ++col;
3485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 /* When starting on a ']' count it, so that we include the '['. */
3487 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 /*
3490 * 2. Back up to start of identifier/string.
3491 */
3492#ifdef FEAT_MBYTE
3493 if (has_mbyte)
3494 {
3495 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3497 this_class = mb_get_class((char_u *)"a");
3498 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003500 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3503 prev_class = mb_get_class(ptr + prevcol);
3504 if (this_class != prev_class
3505 && (i == 0
3506 || prev_class == 0
3507 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 && (!(find_type & FIND_EVAL)
3509 || prevcol == 0
3510 || !find_is_eval_item(ptr + prevcol, &prevcol,
3511 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 )
3513 break;
3514 col = prevcol;
3515 }
3516
3517 /* If we don't want just any old string, or we've found an
3518 * identifier, stop searching. */
3519 if (this_class > 2)
3520 this_class = 2;
3521 if (!(find_type & FIND_STRING) || this_class == 2)
3522 break;
3523 }
3524 else
3525#endif
3526 {
3527 while (col > 0
3528 && ((i == 0
3529 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003530 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 && (!(find_type & FIND_IDENT)
3532 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 || ((find_type & FIND_EVAL)
3534 && col > 1
3535 && find_is_eval_item(ptr + col - 1, &col,
3536 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 ))
3538 --col;
3539
3540 /* If we don't want just any old string, or we've found an
3541 * identifier, stop searching. */
3542 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3543 break;
3544 }
3545 }
3546
3547 if (ptr[col] == NUL || (i == 0 && (
3548#ifdef FEAT_MBYTE
3549 has_mbyte ? this_class != 2 :
3550#endif
3551 !vim_iswordc(ptr[col]))))
3552 {
3553 /*
3554 * didn't find an identifier or string
3555 */
3556 if (find_type & FIND_STRING)
3557 EMSG(_("E348: No string under cursor"));
3558 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003559 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 return 0;
3561 }
3562 ptr += col;
3563 *string = ptr;
3564
3565 /*
3566 * 3. Find the end if the identifier/string.
3567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 bn = 0;
3569 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 col = 0;
3571#ifdef FEAT_MBYTE
3572 if (has_mbyte)
3573 {
3574 /* Search for point of changing multibyte character class. */
3575 this_class = mb_get_class(ptr);
3576 while (ptr[col] != NUL
3577 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3578 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 || ((find_type & FIND_EVAL)
3580 && col <= (int)startcol
3581 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003583 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585 else
3586#endif
3587 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003588 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 || ((find_type & FIND_EVAL)
3590 && col <= (int)startcol
3591 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 )
3593 {
3594 ++col;
3595 }
3596
3597 return col;
3598}
3599
3600/*
3601 * Prepare for redo of a normal command.
3602 */
3603 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003604prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
3606 prep_redo(cap->oap->regname, cap->count0,
3607 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3608}
3609
3610/*
3611 * Prepare for redo of any command.
3612 * Note that only the last argument can be a multi-byte char.
3613 */
3614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003615prep_redo(
3616 int regname,
3617 long num,
3618 int cmd1,
3619 int cmd2,
3620 int cmd3,
3621 int cmd4,
3622 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623{
3624 ResetRedobuff();
3625 if (regname != 0) /* yank from specified buffer */
3626 {
3627 AppendCharToRedobuff('"');
3628 AppendCharToRedobuff(regname);
3629 }
3630 if (num)
3631 AppendNumberToRedobuff(num);
3632
3633 if (cmd1 != NUL)
3634 AppendCharToRedobuff(cmd1);
3635 if (cmd2 != NUL)
3636 AppendCharToRedobuff(cmd2);
3637 if (cmd3 != NUL)
3638 AppendCharToRedobuff(cmd3);
3639 if (cmd4 != NUL)
3640 AppendCharToRedobuff(cmd4);
3641 if (cmd5 != NUL)
3642 AppendCharToRedobuff(cmd5);
3643}
3644
3645/*
3646 * check for operator active and clear it
3647 *
3648 * return TRUE if operator was active
3649 */
3650 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003651checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
3653 if (oap->op_type == OP_NOP)
3654 return FALSE;
3655 clearopbeep(oap);
3656 return TRUE;
3657}
3658
3659/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003660 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003662 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 */
3664 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003665checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003667 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 return FALSE;
3669 clearopbeep(oap);
3670 return TRUE;
3671}
3672
3673 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003674clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 oap->op_type = OP_NOP;
3677 oap->regname = 0;
3678 oap->motion_force = NUL;
3679 oap->use_reg_one = FALSE;
3680}
3681
3682 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003683clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684{
3685 clearop(oap);
3686 beep_flush();
3687}
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689/*
3690 * Remove the shift modifier from a special key.
3691 */
3692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003693unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
3695 switch (cap->cmdchar)
3696 {
3697 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3698 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3699 case K_S_UP: cap->cmdchar = K_UP; break;
3700 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3701 case K_S_HOME: cap->cmdchar = K_HOME; break;
3702 case K_S_END: cap->cmdchar = K_END; break;
3703 }
3704 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3705}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003707/*
3708 * If the mode is currently displayed clear the command line or update the
3709 * command displayed.
3710 */
3711 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003712may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003713{
3714 if (mode_displayed)
3715 clear_cmdline = TRUE; /* unshow visual mode later */
3716#ifdef FEAT_CMDL_INFO
3717 else
3718 clear_showcmd();
3719#endif
3720}
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3723/*
3724 * Routines for displaying a partly typed command
3725 */
3726
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003727#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728static char_u showcmd_buf[SHOWCMD_BUFLEN];
3729static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3730static int showcmd_is_clear = TRUE;
3731static int showcmd_visual = FALSE;
3732
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003733static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003736clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
3738 if (!p_sc)
3739 return;
3740
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (VIsual_active && !char_avail())
3742 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003743 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 long lines;
3745 colnr_T leftcol, rightcol;
3746 linenr_T top, bot;
3747
3748 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003749 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
3751 top = VIsual.lnum;
3752 bot = curwin->w_cursor.lnum;
3753 }
3754 else
3755 {
3756 top = curwin->w_cursor.lnum;
3757 bot = VIsual.lnum;
3758 }
3759# ifdef FEAT_FOLDING
3760 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003761 (void)hasFolding(top, &top, NULL);
3762 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763# endif
3764 lines = bot - top + 1;
3765
3766 if (VIsual_mode == Ctrl_V)
3767 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003768# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003769 char_u *saved_sbr = p_sbr;
3770
3771 /* Make 'sbr' empty for a moment to get the correct size. */
3772 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003775# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003776 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3779 (long)(rightcol - leftcol + 1));
3780 }
3781 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3782 sprintf((char *)showcmd_buf, "%ld", lines);
3783 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003784 {
3785 char_u *s, *e;
3786 int l;
3787 int bytes = 0;
3788 int chars = 0;
3789
3790 if (cursor_bot)
3791 {
3792 s = ml_get_pos(&VIsual);
3793 e = ml_get_cursor();
3794 }
3795 else
3796 {
3797 s = ml_get_cursor();
3798 e = ml_get_pos(&VIsual);
3799 }
3800 while ((*p_sel != 'e') ? s <= e : s < e)
3801 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003802# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003803 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003804# else
3805 l = (*s == NUL) ? 0 : 1;
3806# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003807 if (l == 0)
3808 {
3809 ++bytes;
3810 ++chars;
3811 break; /* end of line */
3812 }
3813 bytes += l;
3814 ++chars;
3815 s += l;
3816 }
3817 if (bytes == chars)
3818 sprintf((char *)showcmd_buf, "%d", chars);
3819 else
3820 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3823 showcmd_visual = TRUE;
3824 }
3825 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 {
3827 showcmd_buf[0] = NUL;
3828 showcmd_visual = FALSE;
3829
3830 /* Don't actually display something if there is nothing to clear. */
3831 if (showcmd_is_clear)
3832 return;
3833 }
3834
3835 display_showcmd();
3836}
3837
3838/*
3839 * Add 'c' to string of shown command chars.
3840 * Return TRUE if output has been written (and setcursor() has been called).
3841 */
3842 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003843add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
3845 char_u *p;
3846 int old_len;
3847 int extra_len;
3848 int overflow;
3849#if defined(FEAT_MOUSE)
3850 int i;
3851 static int ignore[] =
3852 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003853# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3855 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003856# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003857 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003858 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3860 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003861 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003863 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 0
3865 };
3866#endif
3867
Bram Moolenaar09df3122006-01-23 22:23:09 +00003868 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 return FALSE;
3870
3871 if (showcmd_visual)
3872 {
3873 showcmd_buf[0] = NUL;
3874 showcmd_visual = FALSE;
3875 }
3876
3877#if defined(FEAT_MOUSE)
3878 /* Ignore keys that are scrollbar updates and mouse clicks */
3879 if (IS_SPECIAL(c))
3880 for (i = 0; ignore[i] != 0; ++i)
3881 if (ignore[i] == c)
3882 return FALSE;
3883#endif
3884
3885 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003886 if (*p == ' ')
3887 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 old_len = (int)STRLEN(showcmd_buf);
3889 extra_len = (int)STRLEN(p);
3890 overflow = old_len + extra_len - SHOWCMD_COLS;
3891 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003892 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3893 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 STRCAT(showcmd_buf, p);
3895
3896 if (char_avail())
3897 return FALSE;
3898
3899 display_showcmd();
3900
3901 return TRUE;
3902}
3903
3904 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003905add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
3907 if (!add_to_showcmd(c))
3908 setcursor();
3909}
3910
3911/*
3912 * Delete 'len' characters from the end of the shown command.
3913 */
3914 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003915del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916{
3917 int old_len;
3918
3919 if (!p_sc)
3920 return;
3921
3922 old_len = (int)STRLEN(showcmd_buf);
3923 if (len > old_len)
3924 len = old_len;
3925 showcmd_buf[old_len - len] = NUL;
3926
3927 if (!char_avail())
3928 display_showcmd();
3929}
3930
3931/*
3932 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3933 * something and there is a partial mapping.
3934 */
3935 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003936push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 if (p_sc)
3939 STRCPY(old_showcmd_buf, showcmd_buf);
3940}
3941
3942 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003943pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944{
3945 if (!p_sc)
3946 return;
3947
3948 STRCPY(showcmd_buf, old_showcmd_buf);
3949
3950 display_showcmd();
3951}
3952
3953 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003954display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955{
3956 int len;
3957
3958 cursor_off();
3959
3960 len = (int)STRLEN(showcmd_buf);
3961 if (len == 0)
3962 showcmd_is_clear = TRUE;
3963 else
3964 {
3965 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3966 showcmd_is_clear = FALSE;
3967 }
3968
3969 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003970 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3971 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 */
3973 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3974
3975 setcursor(); /* put cursor back where it belongs */
3976}
3977#endif
3978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979/*
3980 * When "check" is FALSE, prepare for commands that scroll the window.
3981 * When "check" is TRUE, take care of scroll-binding after the window has
3982 * scrolled. Called from normal_cmd() and edit().
3983 */
3984 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003985do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
3987 static win_T *old_curwin = NULL;
3988 static linenr_T old_topline = 0;
3989#ifdef FEAT_DIFF
3990 static int old_topfill = 0;
3991#endif
3992 static buf_T *old_buf = NULL;
3993 static colnr_T old_leftcol = 0;
3994
3995 if (check && curwin->w_p_scb)
3996 {
3997 /* If a ":syncbind" command was just used, don't scroll, only reset
3998 * the values. */
3999 if (did_syncbind)
4000 did_syncbind = FALSE;
4001 else if (curwin == old_curwin)
4002 {
4003 /*
4004 * Synchronize other windows, as necessary according to
4005 * 'scrollbind'. Don't do this after an ":edit" command, except
4006 * when 'diff' is set.
4007 */
4008 if ((curwin->w_buffer == old_buf
4009#ifdef FEAT_DIFF
4010 || curwin->w_p_diff
4011#endif
4012 )
4013 && (curwin->w_topline != old_topline
4014#ifdef FEAT_DIFF
4015 || curwin->w_topfill != old_topfill
4016#endif
4017 || curwin->w_leftcol != old_leftcol))
4018 {
4019 check_scrollbind(curwin->w_topline - old_topline,
4020 (long)(curwin->w_leftcol - old_leftcol));
4021 }
4022 }
4023 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4024 {
4025 /*
4026 * When switching between windows, make sure that the relative
4027 * vertical offset is valid for the new window. The relative
4028 * offset is invalid whenever another 'scrollbind' window has
4029 * scrolled to a point that would force the current window to
4030 * scroll past the beginning or end of its buffer. When the
4031 * resync is performed, some of the other 'scrollbind' windows may
4032 * need to jump so that the current window's relative position is
4033 * visible on-screen.
4034 */
4035 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4036 }
4037 curwin->w_scbind_pos = curwin->w_topline;
4038 }
4039
4040 old_curwin = curwin;
4041 old_topline = curwin->w_topline;
4042#ifdef FEAT_DIFF
4043 old_topfill = curwin->w_topfill;
4044#endif
4045 old_buf = curwin->w_buffer;
4046 old_leftcol = curwin->w_leftcol;
4047}
4048
4049/*
4050 * Synchronize any windows that have "scrollbind" set, based on the
4051 * number of rows by which the current window has changed
4052 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4053 */
4054 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004055check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056{
4057 int want_ver;
4058 int want_hor;
4059 win_T *old_curwin = curwin;
4060 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 int old_VIsual_select = VIsual_select;
4062 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 colnr_T tgt_leftcol = curwin->w_leftcol;
4064 long topline;
4065 long y;
4066
4067 /*
4068 * check 'scrollopt' string for vertical and horizontal scroll options
4069 */
4070 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4071#ifdef FEAT_DIFF
4072 want_ver |= old_curwin->w_p_diff;
4073#endif
4074 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4075
4076 /*
4077 * loop through the scrollbound windows and scroll accordingly
4078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004080 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
4082 curbuf = curwin->w_buffer;
4083 /* skip original window and windows with 'noscrollbind' */
4084 if (curwin != old_curwin && curwin->w_p_scb)
4085 {
4086 /*
4087 * do the vertical scroll
4088 */
4089 if (want_ver)
4090 {
4091#ifdef FEAT_DIFF
4092 if (old_curwin->w_p_diff && curwin->w_p_diff)
4093 {
4094 diff_set_topline(old_curwin, curwin);
4095 }
4096 else
4097#endif
4098 {
4099 curwin->w_scbind_pos += topline_diff;
4100 topline = curwin->w_scbind_pos;
4101 if (topline > curbuf->b_ml.ml_line_count)
4102 topline = curbuf->b_ml.ml_line_count;
4103 if (topline < 1)
4104 topline = 1;
4105
4106 y = topline - curwin->w_topline;
4107 if (y > 0)
4108 scrollup(y, FALSE);
4109 else
4110 scrolldown(-y, FALSE);
4111 }
4112
4113 redraw_later(VALID);
4114 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 }
4117
4118 /*
4119 * do the horizontal scroll
4120 */
4121 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4122 {
4123 curwin->w_leftcol = tgt_leftcol;
4124 leftcol_changed();
4125 }
4126 }
4127 }
4128
4129 /*
4130 * reset current-window
4131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 VIsual_select = old_VIsual_select;
4133 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 curwin = old_curwin;
4135 curbuf = old_curbuf;
4136}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138/*
4139 * Command character that's ignored.
4140 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004141 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004144nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004146 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147}
4148
4149/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004150 * Command character that doesn't do anything, but unlike nv_ignore() does
4151 * start edit(). Used for "startinsert" executed while starting up.
4152 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004153 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004154nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004155{
4156}
4157
4158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 * Command character doesn't exist.
4160 */
4161 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004162nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
4164 clearopbeep(cap->oap);
4165}
4166
4167/*
4168 * <Help> and <F1> commands.
4169 */
4170 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004171nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
4173 if (!checkclearopq(cap->oap))
4174 ex_help(NULL);
4175}
4176
4177/*
4178 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4179 */
4180 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004181nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
Bram Moolenaarf2732452018-06-03 14:47:35 +02004183#ifdef FEAT_JOB_CHANNEL
4184 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4185 clearopbeep(cap->oap);
4186 else
4187#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01004188 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004189 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004190 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004191 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4192 op_addsub(cap->oap, cap->count1, cap->arg);
4193 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004194 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004195 else if (VIsual_active)
4196 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004197 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004198 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199}
4200
4201/*
4202 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4203 */
4204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004205nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004208 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004209 if (mod_mask & MOD_MASK_CTRL)
4210 {
4211 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4212 if (cap->arg == BACKWARD)
4213 goto_tabpage(-(int)cap->count1);
4214 else
4215 goto_tabpage((int)cap->count0);
4216 }
4217 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004218 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220}
4221
4222/*
4223 * Implementation of "gd" and "gD" command.
4224 */
4225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004226nv_gd(
4227 oparg_T *oap,
4228 int nchar,
4229 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u *ptr;
4233
Bram Moolenaard9d30582005-05-18 22:10:28 +00004234 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004235 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4236 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004238#ifdef FEAT_FOLDING
4239 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4240 foldOpenCursor();
4241#endif
4242}
4243
4244/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004245 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4246 * otherwise.
4247 */
4248 static int
4249is_ident(char_u *line, int offset)
4250{
4251 int i;
4252 int incomment = FALSE;
4253 int instring = 0;
4254 int prev = 0;
4255
4256 for (i = 0; i < offset && line[i] != NUL; i++)
4257 {
4258 if (instring != 0)
4259 {
4260 if (prev != '\\' && line[i] == instring)
4261 instring = 0;
4262 }
4263 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4264 {
4265 instring = line[i];
4266 }
4267 else
4268 {
4269 if (incomment)
4270 {
4271 if (prev == '*' && line[i] == '/')
4272 incomment = FALSE;
4273 }
4274 else if (prev == '/' && line[i] == '*')
4275 {
4276 incomment = TRUE;
4277 }
4278 else if (prev == '/' && line[i] == '/')
4279 {
4280 return FALSE;
4281 }
4282 }
4283
4284 prev = line[i];
4285 }
4286
4287 return incomment == FALSE && instring == 0;
4288}
4289
4290/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004291 * Search for variable declaration of "ptr[len]".
4292 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4293 * current file ("gD").
4294 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004295 * Return FAIL when not found.
4296 */
4297 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004298find_decl(
4299 char_u *ptr,
4300 int len,
4301 int locally,
4302 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004303 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004304{
4305 char_u *pat;
4306 pos_T old_pos;
4307 pos_T par_pos;
4308 pos_T found_pos;
4309 int t;
4310 int save_p_ws;
4311 int save_p_scs;
4312 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004313 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004314 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004315 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004316
4317 if ((pat = alloc(len + 7)) == NULL)
4318 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004319
4320 /* Put "\V" before the pattern to avoid that the special meaning of "."
4321 * and "~" causes trouble. */
4322 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4323 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 old_pos = curwin->w_cursor;
4325 save_p_ws = p_ws;
4326 save_p_scs = p_scs;
4327 p_ws = FALSE; /* don't wrap around end of file now */
4328 p_scs = FALSE; /* don't switch ignorecase off now */
4329
4330 /*
4331 * With "gD" go to line 1.
4332 * With "gd" Search back for the start of the current function, then go
4333 * back until a blank line. If this fails go to line 1.
4334 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004335 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
4337 setpcmark(); /* Set in findpar() otherwise */
4338 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004339 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 else
4342 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004343 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4345 --curwin->w_cursor.lnum;
4346 }
4347 curwin->w_cursor.col = 0;
4348
4349 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004350 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004351 for (;;)
4352 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004353 valid = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004354 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004355 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004356 if (curwin->w_cursor.lnum >= old_pos.lnum)
4357 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004358
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004359 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004360 {
4361 pos_T *pos;
4362
4363 /* Check that the block the match is in doesn't end before the
4364 * position where we started the search from. */
4365 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4366 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4367 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004368 {
4369 /* There can't be a useful match before the end of this block.
4370 * Skip to the end. */
4371 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004372 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004373 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004374 }
4375
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004376 if (t == FAIL)
4377 {
4378 /* If we previously found a valid position, use it. */
4379 if (found_pos.lnum != 0)
4380 {
4381 curwin->w_cursor = found_pos;
4382 t = OK;
4383 }
4384 break;
4385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004387 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004388 {
4389 /* Ignore this line, continue at start of next line. */
4390 ++curwin->w_cursor.lnum;
4391 curwin->w_cursor.col = 0;
4392 continue;
4393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004395 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4396
4397 /* If the current position is not a valid identifier and a previous
4398 * match is present, favor that one instead. */
4399 if (!valid && found_pos.lnum != 0)
4400 {
4401 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004402 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004403 }
4404
4405 /* Global search: use first valid match found */
4406 if (valid && !locally)
4407 break;
4408 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004409 {
4410 /* If we previously found a valid position, use it. */
4411 if (found_pos.lnum != 0)
4412 curwin->w_cursor = found_pos;
4413 break;
4414 }
4415
Bram Moolenaar226630a2016-10-08 19:21:31 +02004416 /* For finding a local variable and the match is before the "{" or
4417 * inside a comment, continue searching. For K&R style function
4418 * declarations this skips the function header without types. */
4419 if (!valid)
4420 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004421 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004422 }
4423 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004424 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004425 /* Remove SEARCH_START from flags to avoid getting stuck at one
4426 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004427 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004429
4430 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004432 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 curwin->w_cursor = old_pos;
4434 }
4435 else
4436 {
4437 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 /* "n" searches forward now */
4439 reset_search_dir();
4440 }
4441
4442 vim_free(pat);
4443 p_ws = save_p_ws;
4444 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004445
4446 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447}
4448
4449/*
4450 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4451 * lines rather than lines in the file.
4452 * 'dist' must be positive.
4453 *
4454 * Return OK if able to move cursor, FAIL otherwise.
4455 */
4456 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004457nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
4459 int linelen = linetabsize(ml_get_curline());
4460 int retval = OK;
4461 int atend = FALSE;
4462 int n;
4463 int col_off1; /* margin offset for first screen line */
4464 int col_off2; /* margin offset for wrapped screen line */
4465 int width1; /* text width for first screen line */
4466 int width2; /* test width for wrapped screen line */
4467
4468 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004469 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 col_off1 = curwin_col_off();
4472 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004473 width1 = curwin->w_width - col_off1;
4474 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004475 if (width2 == 0)
4476 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 /*
4481 * Instead of sticking at the last character of the buffer line we
4482 * try to stick in the last column of the screen.
4483 */
4484 if (curwin->w_curswant == MAXCOL)
4485 {
4486 atend = TRUE;
4487 validate_virtcol();
4488 if (width1 <= 0)
4489 curwin->w_curswant = 0;
4490 else
4491 {
4492 curwin->w_curswant = width1 - 1;
4493 if (curwin->w_virtcol > curwin->w_curswant)
4494 curwin->w_curswant += ((curwin->w_virtcol
4495 - curwin->w_curswant - 1) / width2 + 1) * width2;
4496 }
4497 }
4498 else
4499 {
4500 if (linelen > width1)
4501 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4502 else
4503 n = width1;
4504 if (curwin->w_curswant > (colnr_T)n + 1)
4505 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4506 * width2;
4507 }
4508
4509 while (dist--)
4510 {
4511 if (dir == BACKWARD)
4512 {
4513 if ((long)curwin->w_curswant >= width2)
4514 /* move back within line */
4515 curwin->w_curswant -= width2;
4516 else
4517 {
4518 /* to previous line */
4519 if (curwin->w_cursor.lnum == 1)
4520 {
4521 retval = FAIL;
4522 break;
4523 }
4524 --curwin->w_cursor.lnum;
4525#ifdef FEAT_FOLDING
4526 /* Move to the start of a closed fold. Don't do that when
4527 * 'foldopen' contains "all": it will open in a moment. */
4528 if (!(fdo_flags & FDO_ALL))
4529 (void)hasFolding(curwin->w_cursor.lnum,
4530 &curwin->w_cursor.lnum, NULL);
4531#endif
4532 linelen = linetabsize(ml_get_curline());
4533 if (linelen > width1)
4534 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4535 + 1) * width2;
4536 }
4537 }
4538 else /* dir == FORWARD */
4539 {
4540 if (linelen > width1)
4541 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4542 else
4543 n = width1;
4544 if (curwin->w_curswant + width2 < (colnr_T)n)
4545 /* move forward within line */
4546 curwin->w_curswant += width2;
4547 else
4548 {
4549 /* to next line */
4550#ifdef FEAT_FOLDING
4551 /* Move to the end of a closed fold. */
4552 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4553 &curwin->w_cursor.lnum);
4554#endif
4555 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4556 {
4557 retval = FAIL;
4558 break;
4559 }
4560 curwin->w_cursor.lnum++;
4561 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004562 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
4564 }
4565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004568 if (virtual_active() && atend)
4569 coladvance(MAXCOL);
4570 else
4571 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4574 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4575 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004576 colnr_T virtcol;
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 /*
4579 * Check for landing on a character that got split at the end of the
4580 * last line. We want to advance a screenline, not end up in the same
4581 * screenline or move two screenlines.
4582 */
4583 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004584 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004585# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004586 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4587 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004588# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004589
4590 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 && (curwin->w_curswant < (colnr_T)width1
4592 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4593 : ((curwin->w_curswant - width1) % width2
4594 > (colnr_T)width2 / 2)))
4595 --curwin->w_cursor.col;
4596 }
4597#endif
4598
4599 if (atend)
4600 curwin->w_curswant = MAXCOL; /* stick in the last column */
4601
4602 return retval;
4603}
4604
4605#ifdef FEAT_MOUSE
4606/*
4607 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4608 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004609 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4610 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 */
4612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004613nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004615 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004617 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
4619 int row, col;
4620
4621 row = mouse_row;
4622 col = mouse_col;
4623
4624 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004625 wp = mouse_find_win(&row, &col);
4626 if (wp == NULL)
4627 return;
4628 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 curbuf = curwin->w_buffer;
4630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004632 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004634# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004635 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004636 /* This window is a terminal window, send the mouse event there.
4637 * Set "typed" to FALSE to avoid an endless loop. */
4638 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004639 else
4640# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004641 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4642 {
4643 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4644 }
4645 else
4646 {
4647 cap->count1 = 3;
4648 cap->count0 = 3;
4649 nv_scroll_line(cap);
4650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004652# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 else
4654 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004655 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4656 if (!curwin->w_p_wrap)
4657 {
4658 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004659
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004660 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004661 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004662 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4663 if (val < 0)
4664 val = 0;
4665
4666 gui_do_horiz_scroll(val, TRUE);
4667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004669# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 curwin->w_redr_status = TRUE;
4672
4673 curwin = old_curwin;
4674 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675}
4676
4677/*
4678 * Mouse clicks and drags.
4679 */
4680 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004681nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682{
4683 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4684}
4685#endif
4686
4687/*
4688 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4689 * cap->arg must be TRUE for CTRL-E.
4690 */
4691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004692nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 if (!checkclearop(cap->oap))
4695 scroll_redraw(cap->arg, cap->count1);
4696}
4697
4698/*
4699 * Scroll "count" lines up or down, and redraw.
4700 */
4701 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004702scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
4704 linenr_T prev_topline = curwin->w_topline;
4705#ifdef FEAT_DIFF
4706 int prev_topfill = curwin->w_topfill;
4707#endif
4708 linenr_T prev_lnum = curwin->w_cursor.lnum;
4709
4710 if (up)
4711 scrollup(count, TRUE);
4712 else
4713 scrolldown(count, TRUE);
4714 if (p_so)
4715 {
4716 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4717 * valid, otherwise the screen jumps back at the end of the file. */
4718 cursor_correct();
4719 check_cursor_moved(curwin);
4720 curwin->w_valid |= VALID_TOPLINE;
4721
4722 /* If moved back to where we were, at least move the cursor, otherwise
4723 * we get stuck at one position. Don't move the cursor up if the
4724 * first line of the buffer is already on the screen */
4725 while (curwin->w_topline == prev_topline
4726#ifdef FEAT_DIFF
4727 && curwin->w_topfill == prev_topfill
4728#endif
4729 )
4730 {
4731 if (up)
4732 {
4733 if (curwin->w_cursor.lnum > prev_lnum
4734 || cursor_down(1L, FALSE) == FAIL)
4735 break;
4736 }
4737 else
4738 {
4739 if (curwin->w_cursor.lnum < prev_lnum
4740 || prev_topline == 1L
4741 || cursor_up(1L, FALSE) == FAIL)
4742 break;
4743 }
4744 /* Mark w_topline as valid, otherwise the screen jumps back at the
4745 * end of the file. */
4746 check_cursor_moved(curwin);
4747 curwin->w_valid |= VALID_TOPLINE;
4748 }
4749 }
4750 if (curwin->w_cursor.lnum != prev_lnum)
4751 coladvance(curwin->w_curswant);
4752 redraw_later(VALID);
4753}
4754
4755/*
4756 * Commands that start with "z".
4757 */
4758 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004759nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
4761 long n;
4762 colnr_T col;
4763 int nchar = cap->nchar;
4764#ifdef FEAT_FOLDING
4765 long old_fdl = curwin->w_p_fdl;
4766 int old_fen = curwin->w_p_fen;
4767#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004768#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004769 int undo = FALSE;
4770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 if (VIM_ISDIGIT(nchar))
4773 {
4774 /*
4775 * "z123{nchar}": edit the count before obtaining {nchar}
4776 */
4777 if (checkclearop(cap->oap))
4778 return;
4779 n = nchar - '0';
4780 for (;;)
4781 {
4782#ifdef USE_ON_FLY_SCROLL
4783 dont_scroll = TRUE; /* disallow scrolling here */
4784#endif
4785 ++no_mapping;
4786 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004787 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 --no_mapping;
4790 --allow_keys;
4791#ifdef FEAT_CMDL_INFO
4792 (void)add_to_showcmd(nchar);
4793#endif
4794 if (nchar == K_DEL || nchar == K_KDEL)
4795 n /= 10;
4796 else if (VIM_ISDIGIT(nchar))
4797 n = n * 10 + (nchar - '0');
4798 else if (nchar == CAR)
4799 {
4800#ifdef FEAT_GUI
4801 need_mouse_correct = TRUE;
4802#endif
4803 win_setheight((int)n);
4804 break;
4805 }
4806 else if (nchar == 'l'
4807 || nchar == 'h'
4808 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004809 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
4811 cap->count1 = n ? n * cap->count1 : cap->count1;
4812 goto dozet;
4813 }
4814 else
4815 {
4816 clearopbeep(cap->oap);
4817 break;
4818 }
4819 }
4820 cap->oap->op_type = OP_NOP;
4821 return;
4822 }
4823
4824dozet:
4825 if (
4826#ifdef FEAT_FOLDING
4827 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4828 * and "zC" only in Visual mode. "zj" and "zk" are motion
4829 * commands. */
4830 cap->nchar != 'f' && cap->nchar != 'F'
4831 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4832 && cap->nchar != 'j' && cap->nchar != 'k'
4833 &&
4834#endif
4835 checkclearop(cap->oap))
4836 return;
4837
4838 /*
4839 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4840 * If line number given, set cursor.
4841 */
4842 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4843 && cap->count0
4844 && cap->count0 != curwin->w_cursor.lnum)
4845 {
4846 setpcmark();
4847 if (cap->count0 > curbuf->b_ml.ml_line_count)
4848 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4849 else
4850 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004851 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853
4854 switch (nchar)
4855 {
4856 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4857 case '+':
4858 if (cap->count0 == 0)
4859 {
4860 /* No count given: put cursor at the line below screen */
4861 validate_botline(); /* make sure w_botline is valid */
4862 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4863 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4864 else
4865 curwin->w_cursor.lnum = curwin->w_botline;
4866 }
4867 /* FALLTHROUGH */
4868 case NL:
4869 case CAR:
4870 case K_KENTER:
4871 beginline(BL_WHITE | BL_FIX);
4872 /* FALLTHROUGH */
4873
4874 case 't': scroll_cursor_top(0, TRUE);
4875 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004876 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 break;
4878
4879 /* "z." and "zz": put cursor in middle of screen */
4880 case '.': beginline(BL_WHITE | BL_FIX);
4881 /* FALLTHROUGH */
4882
4883 case 'z': scroll_cursor_halfway(TRUE);
4884 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004885 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 break;
4887
4888 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4889 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4890 * when <count> is at bottom of window, and puts that one at
4891 * bottom of window. */
4892 if (cap->count0 != 0)
4893 {
4894 scroll_cursor_bot(0, TRUE);
4895 curwin->w_cursor.lnum = curwin->w_topline;
4896 }
4897 else if (curwin->w_topline == 1)
4898 curwin->w_cursor.lnum = 1;
4899 else
4900 curwin->w_cursor.lnum = curwin->w_topline - 1;
4901 /* FALLTHROUGH */
4902 case '-':
4903 beginline(BL_WHITE | BL_FIX);
4904 /* FALLTHROUGH */
4905
4906 case 'b': scroll_cursor_bot(0, TRUE);
4907 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004908 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 break;
4910
4911 /* "zH" - scroll screen right half-page */
4912 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004913 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 /* FALLTHROUGH */
4915
4916 /* "zh" - scroll screen to the right */
4917 case 'h':
4918 case K_LEFT:
4919 if (!curwin->w_p_wrap)
4920 {
4921 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4922 curwin->w_leftcol = 0;
4923 else
4924 curwin->w_leftcol -= (colnr_T)cap->count1;
4925 leftcol_changed();
4926 }
4927 break;
4928
4929 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004930 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 /* FALLTHROUGH */
4932
4933 /* "zl" - scroll screen to the left */
4934 case 'l':
4935 case K_RIGHT:
4936 if (!curwin->w_p_wrap)
4937 {
4938 /* scroll the window left */
4939 curwin->w_leftcol += (colnr_T)cap->count1;
4940 leftcol_changed();
4941 }
4942 break;
4943
4944 /* "zs" - scroll screen, cursor at the start */
4945 case 's': if (!curwin->w_p_wrap)
4946 {
4947#ifdef FEAT_FOLDING
4948 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4949 col = 0; /* like the cursor is in col 0 */
4950 else
4951#endif
4952 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4953 if ((long)col > p_siso)
4954 col -= p_siso;
4955 else
4956 col = 0;
4957 if (curwin->w_leftcol != col)
4958 {
4959 curwin->w_leftcol = col;
4960 redraw_later(NOT_VALID);
4961 }
4962 }
4963 break;
4964
4965 /* "ze" - scroll screen, cursor at the end */
4966 case 'e': if (!curwin->w_p_wrap)
4967 {
4968#ifdef FEAT_FOLDING
4969 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4970 col = 0; /* like the cursor is in col 0 */
4971 else
4972#endif
4973 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004974 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if ((long)col + p_siso < n)
4976 col = 0;
4977 else
4978 col = col + p_siso - n + 1;
4979 if (curwin->w_leftcol != col)
4980 {
4981 curwin->w_leftcol = col;
4982 redraw_later(NOT_VALID);
4983 }
4984 }
4985 break;
4986
4987#ifdef FEAT_FOLDING
4988 /* "zF": create fold command */
4989 /* "zf": create fold operator */
4990 case 'F':
4991 case 'f': if (foldManualAllowed(TRUE))
4992 {
4993 cap->nchar = 'f';
4994 nv_operator(cap);
4995 curwin->w_p_fen = TRUE;
4996
4997 /* "zF" is like "zfzf" */
4998 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4999 {
5000 nv_operator(cap);
5001 finish_op = TRUE;
5002 }
5003 }
5004 else
5005 clearopbeep(cap->oap);
5006 break;
5007
5008 /* "zd": delete fold at cursor */
5009 /* "zD": delete fold at cursor recursively */
5010 case 'd':
5011 case 'D': if (foldManualAllowed(FALSE))
5012 {
5013 if (VIsual_active)
5014 nv_operator(cap);
5015 else
5016 deleteFold(curwin->w_cursor.lnum,
5017 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5018 }
5019 break;
5020
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005021 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 case 'E': if (foldmethodIsManual(curwin))
5023 {
5024 clearFolding(curwin);
5025 changed_window_setting();
5026 }
5027 else if (foldmethodIsMarker(curwin))
5028 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5029 TRUE, FALSE);
5030 else
5031 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5032 break;
5033
5034 /* "zn": fold none: reset 'foldenable' */
5035 case 'n': curwin->w_p_fen = FALSE;
5036 break;
5037
5038 /* "zN": fold Normal: set 'foldenable' */
5039 case 'N': curwin->w_p_fen = TRUE;
5040 break;
5041
5042 /* "zi": invert folding: toggle 'foldenable' */
5043 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5044 break;
5045
5046 /* "za": open closed fold or close open fold at cursor */
5047 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5048 openFold(curwin->w_cursor.lnum, cap->count1);
5049 else
5050 {
5051 closeFold(curwin->w_cursor.lnum, cap->count1);
5052 curwin->w_p_fen = TRUE;
5053 }
5054 break;
5055
5056 /* "zA": open fold at cursor recursively */
5057 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5058 openFoldRecurse(curwin->w_cursor.lnum);
5059 else
5060 {
5061 closeFoldRecurse(curwin->w_cursor.lnum);
5062 curwin->w_p_fen = TRUE;
5063 }
5064 break;
5065
5066 /* "zo": open fold at cursor or Visual area */
5067 case 'o': if (VIsual_active)
5068 nv_operator(cap);
5069 else
5070 openFold(curwin->w_cursor.lnum, cap->count1);
5071 break;
5072
5073 /* "zO": open fold recursively */
5074 case 'O': if (VIsual_active)
5075 nv_operator(cap);
5076 else
5077 openFoldRecurse(curwin->w_cursor.lnum);
5078 break;
5079
5080 /* "zc": close fold at cursor or Visual area */
5081 case 'c': if (VIsual_active)
5082 nv_operator(cap);
5083 else
5084 closeFold(curwin->w_cursor.lnum, cap->count1);
5085 curwin->w_p_fen = TRUE;
5086 break;
5087
5088 /* "zC": close fold recursively */
5089 case 'C': if (VIsual_active)
5090 nv_operator(cap);
5091 else
5092 closeFoldRecurse(curwin->w_cursor.lnum);
5093 curwin->w_p_fen = TRUE;
5094 break;
5095
5096 /* "zv": open folds at the cursor */
5097 case 'v': foldOpenCursor();
5098 break;
5099
5100 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5101 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005102 curwin->w_foldinvalid = TRUE; /* recompute folds */
5103 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 foldOpenCursor();
5105 break;
5106
5107 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5108 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005109 curwin->w_foldinvalid = TRUE; /* recompute folds */
5110 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 break;
5112
5113 /* "zm": fold more */
5114 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005115 {
5116 curwin->w_p_fdl -= cap->count1;
5117 if (curwin->w_p_fdl < 0)
5118 curwin->w_p_fdl = 0;
5119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 old_fdl = -1; /* force an update */
5121 curwin->w_p_fen = TRUE;
5122 break;
5123
5124 /* "zM": close all folds */
5125 case 'M': curwin->w_p_fdl = 0;
5126 old_fdl = -1; /* force an update */
5127 curwin->w_p_fen = TRUE;
5128 break;
5129
5130 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005131 case 'r': curwin->w_p_fdl += cap->count1;
5132 {
5133 int d = getDeepestNesting();
5134
5135 if (curwin->w_p_fdl >= d)
5136 curwin->w_p_fdl = d;
5137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 break;
5139
5140 /* "zR": open all folds */
5141 case 'R': curwin->w_p_fdl = getDeepestNesting();
5142 old_fdl = -1; /* force an update */
5143 break;
5144
5145 case 'j': /* "zj" move to next fold downwards */
5146 case 'k': /* "zk" move to next fold upwards */
5147 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5148 cap->count1) == FAIL)
5149 clearopbeep(cap->oap);
5150 break;
5151
5152#endif /* FEAT_FOLDING */
5153
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005154#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005155 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5156 ++no_mapping;
5157 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005158 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005159 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005160 --no_mapping;
5161 --allow_keys;
5162#ifdef FEAT_CMDL_INFO
5163 (void)add_to_showcmd(nchar);
5164#endif
5165 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5166 {
5167 clearopbeep(cap->oap);
5168 break;
5169 }
5170 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005171 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005172
Bram Moolenaarb765d632005-06-07 21:00:02 +00005173 case 'g': /* "zg": add good word to word list */
5174 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005175 case 'G': /* "zG": add good word to temp word list */
5176 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005177 {
5178 char_u *ptr = NULL;
5179 int len;
5180
5181 if (checkclearop(cap->oap))
5182 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005183 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5184 == FAIL)
5185 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005186 if (ptr == NULL)
5187 {
5188 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005189
Bram Moolenaar134bf072013-09-25 18:54:24 +02005190 /* Find bad word under the cursor. When 'spell' is
5191 * off this fails and find_ident_under_cursor() is
5192 * used below. */
5193 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005194 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005195 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005196 if (len != 0 && curwin->w_cursor.col <= pos.col)
5197 ptr = ml_get_pos(&curwin->w_cursor);
5198 curwin->w_cursor = pos;
5199 }
5200
Bram Moolenaarb765d632005-06-07 21:00:02 +00005201 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5202 FIND_IDENT)) == 0)
5203 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005204 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005205 (nchar == 'G' || nchar == 'W')
5206 ? 0 : (int)cap->count1,
5207 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005208 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005209 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210
Bram Moolenaar43abc522005-12-10 20:15:02 +00005211 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005212 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005213 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005214 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005215#endif
5216
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 default: clearopbeep(cap->oap);
5218 }
5219
5220#ifdef FEAT_FOLDING
5221 /* Redraw when 'foldenable' changed */
5222 if (old_fen != curwin->w_p_fen)
5223 {
5224# ifdef FEAT_DIFF
5225 win_T *wp;
5226
5227 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5228 {
5229 /* Adjust 'foldenable' in diff-synced windows. */
5230 FOR_ALL_WINDOWS(wp)
5231 {
5232 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5233 {
5234 wp->w_p_fen = curwin->w_p_fen;
5235 changed_window_setting_win(wp);
5236 }
5237 }
5238 }
5239# endif
5240 changed_window_setting();
5241 }
5242
5243 /* Redraw when 'foldlevel' changed. */
5244 if (old_fdl != curwin->w_p_fdl)
5245 newFoldLevel();
5246#endif
5247}
5248
5249#ifdef FEAT_GUI
5250/*
5251 * Vertical scrollbar movement.
5252 */
5253 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005254nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
5256 if (cap->oap->op_type != OP_NOP)
5257 clearopbeep(cap->oap);
5258
5259 /* Even if an operator was pending, we still want to scroll */
5260 gui_do_scroll();
5261}
5262
5263/*
5264 * Horizontal scrollbar movement.
5265 */
5266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005267nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 if (cap->oap->op_type != OP_NOP)
5270 clearopbeep(cap->oap);
5271
5272 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005273 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274}
5275#endif
5276
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005277#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005278/*
5279 * Click in GUI tab.
5280 */
5281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005282nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005283{
5284 if (cap->oap->op_type != OP_NOP)
5285 clearopbeep(cap->oap);
5286
5287 /* Even if an operator was pending, we still want to jump tabs. */
5288 goto_tabpage(current_tab);
5289}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005290
5291/*
5292 * Selected item in tab line menu.
5293 */
5294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005295nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005296{
5297 if (cap->oap->op_type != OP_NOP)
5298 clearopbeep(cap->oap);
5299
5300 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005301 handle_tabmenu();
5302}
5303
5304/*
5305 * Handle selecting an item of the GUI tab line menu.
5306 * Used in Normal and Insert mode.
5307 */
5308 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005309handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005310{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005311 switch (current_tabmenu)
5312 {
5313 case TABLINE_MENU_CLOSE:
5314 if (current_tab == 0)
5315 do_cmdline_cmd((char_u *)"tabclose");
5316 else
5317 {
5318 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5319 current_tab);
5320 do_cmdline_cmd(IObuff);
5321 }
5322 break;
5323
5324 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005325 if (current_tab == 0)
5326 do_cmdline_cmd((char_u *)"$tabnew");
5327 else
5328 {
5329 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5330 current_tab - 1);
5331 do_cmdline_cmd(IObuff);
5332 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005333 break;
5334
5335 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005336 if (current_tab == 0)
5337 do_cmdline_cmd((char_u *)"browse $tabnew");
5338 else
5339 {
5340 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5341 current_tab - 1);
5342 do_cmdline_cmd(IObuff);
5343 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005344 break;
5345 }
5346}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005347#endif
5348
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349/*
5350 * "Q" command.
5351 */
5352 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005353nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 /*
5356 * Ignore 'Q' in Visual mode, just give a beep.
5357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005359 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005360 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 do_exmode(FALSE);
5362}
5363
5364/*
5365 * Handle a ":" command.
5366 */
5367 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005368nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005371 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 if (VIsual_active)
5374 nv_operator(cap);
5375 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
5377 if (cap->oap->op_type != OP_NOP)
5378 {
5379 /* Using ":" as a movement is characterwise exclusive. */
5380 cap->oap->motion_type = MCHAR;
5381 cap->oap->inclusive = FALSE;
5382 }
5383 else if (cap->count0)
5384 {
5385 /* translate "count:" into ":.,.+(count - 1)" */
5386 stuffcharReadbuff('.');
5387 if (cap->count0 > 1)
5388 {
5389 stuffReadbuff((char_u *)",.+");
5390 stuffnumReadbuff((long)cap->count0 - 1L);
5391 }
5392 }
5393
5394 /* When typing, don't type below an old message */
5395 if (KeyTyped)
5396 compute_cmdrow();
5397
5398 old_p_im = p_im;
5399
5400 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005401 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5403
5404 /* If 'insertmode' changed, enter or exit Insert mode */
5405 if (p_im != old_p_im)
5406 {
5407 if (p_im)
5408 restart_edit = 'i';
5409 else
5410 restart_edit = 0;
5411 }
5412
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005413 if (cmd_result == FAIL)
5414 /* The Ex command failed, do not execute the operator. */
5415 clearop(cap->oap);
5416 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5418 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005419 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5420 || did_emsg
5421 ))
5422 /* The start of the operator has become invalid by the Ex command.
5423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 clearopbeep(cap->oap);
5425 }
5426}
5427
5428/*
5429 * Handle CTRL-G command.
5430 */
5431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005432nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 if (VIsual_active) /* toggle Selection/Visual mode */
5435 {
5436 VIsual_select = !VIsual_select;
5437 showmode();
5438 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005439 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 /* print full name if count given or :cd used */
5441 fileinfo((int)cap->count0, FALSE, TRUE);
5442}
5443
5444/*
5445 * Handle CTRL-H <Backspace> command.
5446 */
5447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005448nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 if (VIsual_active && VIsual_select)
5451 {
5452 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5453 v_visop(cap);
5454 }
5455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 nv_left(cap);
5457}
5458
5459/*
5460 * CTRL-L: clear screen and redraw.
5461 */
5462 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005463nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 if (!checkclearop(cap->oap))
5466 {
5467#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5468 /*
5469 * Right now, the BeBox doesn't seem to have an easy way to detect
5470 * window resizing, so we cheat and make the user detect it
5471 * manually with CTRL-L instead
5472 */
5473 ui_get_shellsize();
5474#endif
5475#ifdef FEAT_SYN_HL
5476 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005477 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005478# ifdef FEAT_RELTIME
5479 {
5480 win_T *wp;
5481
5482 FOR_ALL_WINDOWS(wp)
5483 wp->w_s->b_syn_slow = FALSE;
5484 }
5485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486#endif
5487 redraw_later(CLEAR);
5488 }
5489}
5490
5491/*
5492 * CTRL-O: In Select mode: switch to Visual mode for one command.
5493 * Otherwise: Go to older pcmark.
5494 */
5495 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005496nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 if (VIsual_active && VIsual_select)
5499 {
5500 VIsual_select = FALSE;
5501 showmode();
5502 restart_VIsual_select = 2; /* restart Select mode later */
5503 }
5504 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 cap->count1 = -cap->count1;
5507 nv_pcmark(cap);
5508 }
5509}
5510
5511/*
5512 * CTRL-^ command, short for ":e #"
5513 */
5514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005515nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 if (!checkclearopq(cap->oap))
5518 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5519 GETF_SETMARK|GETF_ALT, FALSE);
5520}
5521
5522/*
5523 * "Z" commands.
5524 */
5525 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005526nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 if (!checkclearopq(cap->oap))
5529 {
5530 switch (cap->nchar)
5531 {
5532 /* "ZZ": equivalent to ":x". */
5533 case 'Z': do_cmdline_cmd((char_u *)"x");
5534 break;
5535
5536 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5537 case 'Q': do_cmdline_cmd((char_u *)"q!");
5538 break;
5539
5540 default: clearopbeep(cap->oap);
5541 }
5542 }
5543}
5544
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545/*
5546 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5547 */
5548 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005549do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551 oparg_T oa;
5552 cmdarg_T ca;
5553
5554 clear_oparg(&oa);
5555 vim_memset(&ca, 0, sizeof(ca));
5556 ca.oap = &oa;
5557 ca.cmdchar = c1;
5558 ca.nchar = c2;
5559 nv_ident(&ca);
5560}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
5562/*
5563 * Handle the commands that use the word under the cursor.
5564 * [g] CTRL-] :ta to current identifier
5565 * [g] 'K' run program for current identifier
5566 * [g] '*' / to current identifier or string
5567 * [g] '#' ? to current identifier or string
5568 * g ']' :tselect for current identifier
5569 */
5570 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005571nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572{
5573 char_u *ptr = NULL;
5574 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005575 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005576 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 char_u *p;
5578 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005579 int kp_help; /* 'keywordprg' is ":he" */
5580 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 int n = 0; /* init for GCC */
5582 int cmdchar;
5583 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005584 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 char_u *aux_ptr;
5586 int isman;
5587 int isman_s;
5588
5589 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5590 {
5591 cmdchar = cap->nchar;
5592 g_cmd = TRUE;
5593 }
5594 else
5595 {
5596 cmdchar = cap->cmdchar;
5597 g_cmd = FALSE;
5598 }
5599
5600 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5601 cmdchar = '#';
5602
5603 /*
5604 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5605 */
5606 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5609 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (checkclearopq(cap->oap))
5611 return;
5612 }
5613
5614 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5615 (cmdchar == '*' || cmdchar == '#')
5616 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5617 {
5618 clearop(cap->oap);
5619 return;
5620 }
5621
5622 /* Allocate buffer to put the command in. Inserting backslashes can
5623 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5624 * and some numbers. */
5625 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5626 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5627 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005628 if (kp_help && *skipwhite(ptr) == NUL)
5629 {
5630 EMSG(_(e_noident)); /* found white space only */
5631 return;
5632 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005633 kp_ex = (*kp == ':');
5634 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5635 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 if (buf == NULL)
5637 return;
5638 buf[0] = NUL;
5639
5640 switch (cmdchar)
5641 {
5642 case '*':
5643 case '#':
5644 /*
5645 * Put cursor at start of word, makes search skip the word
5646 * under the cursor.
5647 * Call setpcmark() first, so "*``" puts the cursor back where
5648 * it was.
5649 */
5650 setpcmark();
5651 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5652
5653 if (!g_cmd && vim_iswordp(ptr))
5654 STRCPY(buf, "\\<");
5655 no_smartcase = TRUE; /* don't use 'smartcase' now */
5656 break;
5657
5658 case 'K':
5659 if (kp_help)
5660 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005661 else if (kp_ex)
5662 {
5663 if (cap->count0 != 0)
5664 vim_snprintf((char *)buf, buflen, "%s %ld",
5665 kp, cap->count0);
5666 else
5667 STRCPY(buf, kp);
5668 STRCAT(buf, " ");
5669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 else
5671 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005672 /* An external command will probably use an argument starting
5673 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005674 while (*ptr == '-' && n > 0)
5675 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005676 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005677 --n;
5678 }
5679 if (n == 0)
5680 {
5681 EMSG(_(e_noident)); /* found dashes only */
5682 vim_free(buf);
5683 return;
5684 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 /* When a count is given, turn it into a range. Is this
5687 * really what we want? */
5688 isman = (STRCMP(kp, "man") == 0);
5689 isman_s = (STRCMP(kp, "man -s") == 0);
5690 if (cap->count0 != 0 && !(isman || isman_s))
5691 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5692
5693 STRCAT(buf, "! ");
5694 if (cap->count0 == 0 && isman_s)
5695 STRCAT(buf, "man");
5696 else
5697 STRCAT(buf, kp);
5698 STRCAT(buf, " ");
5699 if (cap->count0 != 0 && (isman || isman_s))
5700 {
5701 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5702 STRCAT(buf, " ");
5703 }
5704 }
5705 break;
5706
5707 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005708 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709#ifdef FEAT_CSCOPE
5710 if (p_cst)
5711 STRCPY(buf, "cstag ");
5712 else
5713#endif
5714 STRCPY(buf, "ts ");
5715 break;
5716
5717 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005718 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (curbuf->b_help)
5720 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005722 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005723 if (g_cmd)
5724 STRCPY(buf, "tj ");
5725 else
5726 sprintf((char *)buf, "%ldta ", cap->count0);
5727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729
5730 /*
5731 * Now grab the chars in the identifier
5732 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005733 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005735 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005736 if (kp_ex)
5737 /* Escape the argument properly for an Ex command */
5738 p = vim_strsave_fnameescape(ptr, FALSE);
5739 else
5740 /* Escape the argument properly for a shell command */
5741 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005742 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005743 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005745 vim_free(buf);
5746 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005748 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5749 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005750 {
5751 vim_free(buf);
5752 vim_free(p);
5753 return;
5754 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005755 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005756 STRCAT(buf, p);
5757 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005759 else
5760 {
5761 if (cmdchar == '*')
5762 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5763 else if (cmdchar == '#')
5764 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005765 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005766 {
5767 if (curbuf->b_help)
5768 /* ":help" handles unescaped argument */
5769 aux_ptr = (char_u *)"";
5770 else
5771 aux_ptr = (char_u *)"\\|\"\n[";
5772 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005773 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005774 aux_ptr = (char_u *)"\\|\"\n*?[";
5775
5776 p = buf + STRLEN(buf);
5777 while (n-- > 0)
5778 {
5779 /* put a backslash before \ and some others */
5780 if (vim_strchr(aux_ptr, *ptr) != NULL)
5781 *p++ = '\\';
5782#ifdef FEAT_MBYTE
5783 /* When current byte is a part of multibyte character, copy all
5784 * bytes of that character. */
5785 if (has_mbyte)
5786 {
5787 int i;
5788 int len = (*mb_ptr2len)(ptr) - 1;
5789
5790 for (i = 0; i < len && n >= 1; ++i, --n)
5791 *p++ = *ptr++;
5792 }
5793#endif
5794 *p++ = *ptr++;
5795 }
5796 *p = NUL;
5797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
5799 /*
5800 * Execute the command.
5801 */
5802 if (cmdchar == '*' || cmdchar == '#')
5803 {
5804 if (!g_cmd && (
5805#ifdef FEAT_MBYTE
5806 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5807#endif
5808 vim_iswordc(ptr[-1])))
5809 STRCAT(buf, "\\>");
5810#ifdef FEAT_CMDHIST
5811 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005812 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5814#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005815 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
5817 else
5818 do_cmdline_cmd(buf);
5819
5820 vim_free(buf);
5821}
5822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823/*
5824 * Get visually selected text, within one line only.
5825 * Returns FAIL if more than one line selected.
5826 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005827 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005828get_visual_text(
5829 cmdarg_T *cap,
5830 char_u **pp, /* return: start of selected text */
5831 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
5833 if (VIsual_mode != 'V')
5834 unadjust_for_sel();
5835 if (VIsual.lnum != curwin->w_cursor.lnum)
5836 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005837 if (cap != NULL)
5838 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 return FAIL;
5840 }
5841 if (VIsual_mode == 'V')
5842 {
5843 *pp = ml_get_curline();
5844 *lenp = (int)STRLEN(*pp);
5845 }
5846 else
5847 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005848 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {
5850 *pp = ml_get_pos(&curwin->w_cursor);
5851 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5852 }
5853 else
5854 {
5855 *pp = ml_get_pos(&VIsual);
5856 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5857 }
5858#ifdef FEAT_MBYTE
5859 if (has_mbyte)
5860 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005861 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862#endif
5863 }
5864 reset_VIsual_and_resel();
5865 return OK;
5866}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868/*
5869 * CTRL-T: backwards in tag stack
5870 */
5871 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005872nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873{
5874 if (!checkclearopq(cap->oap))
5875 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5876}
5877
5878/*
5879 * Handle scrolling command 'H', 'L' and 'M'.
5880 */
5881 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005882nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 int used = 0;
5885 long n;
5886#ifdef FEAT_FOLDING
5887 linenr_T lnum;
5888#endif
5889 int half;
5890
5891 cap->oap->motion_type = MLINE;
5892 setpcmark();
5893
5894 if (cap->cmdchar == 'L')
5895 {
5896 validate_botline(); /* make sure curwin->w_botline is valid */
5897 curwin->w_cursor.lnum = curwin->w_botline - 1;
5898 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5899 curwin->w_cursor.lnum = 1;
5900 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005901 {
5902#ifdef FEAT_FOLDING
5903 if (hasAnyFolding(curwin))
5904 {
5905 /* Count a fold for one screen line. */
5906 for (n = cap->count1 - 1; n > 0
5907 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5908 {
5909 (void)hasFolding(curwin->w_cursor.lnum,
5910 &curwin->w_cursor.lnum, NULL);
5911 --curwin->w_cursor.lnum;
5912 }
5913 }
5914 else
5915#endif
5916 curwin->w_cursor.lnum -= cap->count1 - 1;
5917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919 else
5920 {
5921 if (cap->cmdchar == 'M')
5922 {
5923#ifdef FEAT_DIFF
5924 /* Don't count filler lines above the window. */
5925 used -= diff_check_fill(curwin, curwin->w_topline)
5926 - curwin->w_topfill;
5927#endif
5928 validate_botline(); /* make sure w_empty_rows is valid */
5929 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5930 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5931 {
5932#ifdef FEAT_DIFF
5933 /* Count half he number of filler lines to be "below this
5934 * line" and half to be "above the next line". */
5935 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5936 + n) / 2 >= half)
5937 {
5938 --n;
5939 break;
5940 }
5941#endif
5942 used += plines(curwin->w_topline + n);
5943 if (used >= half)
5944 break;
5945#ifdef FEAT_FOLDING
5946 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5947 n = lnum - curwin->w_topline;
5948#endif
5949 }
5950 if (n > 0 && used > curwin->w_height)
5951 --n;
5952 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005953 else /* (cap->cmdchar == 'H') */
5954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005956#ifdef FEAT_FOLDING
5957 if (hasAnyFolding(curwin))
5958 {
5959 /* Count a fold for one screen line. */
5960 lnum = curwin->w_topline;
5961 while (n-- > 0 && lnum < curwin->w_botline - 1)
5962 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005963 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005964 ++lnum;
5965 }
5966 n = lnum - curwin->w_topline;
5967 }
5968#endif
5969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 curwin->w_cursor.lnum = curwin->w_topline + n;
5971 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5972 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5973 }
5974
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005975 /* Correct for 'so', except when an operator is pending. */
5976 if (cap->oap->op_type == OP_NOP)
5977 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 beginline(BL_SOL | BL_FIX);
5979}
5980
5981/*
5982 * Cursor right commands.
5983 */
5984 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005985nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
5987 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005988 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005990 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5991 {
5992 /* <C-Right> and <S-Right> move a word or WORD right */
5993 if (mod_mask & MOD_MASK_CTRL)
5994 cap->arg = TRUE;
5995 nv_wordcmd(cap);
5996 return;
5997 }
5998
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 cap->oap->motion_type = MCHAR;
6000 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006001 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006003#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006005 * In virtual edit mode, there's no such thing as "past_line", as lines
6006 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 */
6008 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006009 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010#endif
6011
6012 for (n = cap->count1; n > 0; --n)
6013 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006014 if ((!past_line && oneright() == FAIL)
6015 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006016 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 {
6018 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006019 * <Space> wraps to next line if 'whichwrap' has 's'.
6020 * 'l' wraps to next line if 'whichwrap' has 'l'.
6021 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 */
6023 if ( ((cap->cmdchar == ' '
6024 && vim_strchr(p_ww, 's') != NULL)
6025 || (cap->cmdchar == 'l'
6026 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006027 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 && vim_strchr(p_ww, '>') != NULL))
6029 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6030 {
6031 /* When deleting we also count the NL as a character.
6032 * Set cap->oap->inclusive when last char in the line is
6033 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006034 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006036 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 cap->oap->inclusive = TRUE;
6038 else
6039 {
6040 ++curwin->w_cursor.lnum;
6041 curwin->w_cursor.col = 0;
6042#ifdef FEAT_VIRTUALEDIT
6043 curwin->w_cursor.coladd = 0;
6044#endif
6045 curwin->w_set_curswant = TRUE;
6046 cap->oap->inclusive = FALSE;
6047 }
6048 continue;
6049 }
6050 if (cap->oap->op_type == OP_NOP)
6051 {
6052 /* Only beep and flush if not moved at all */
6053 if (n == cap->count1)
6054 beep_flush();
6055 }
6056 else
6057 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006058 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 cap->oap->inclusive = TRUE;
6060 }
6061 break;
6062 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006063 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 {
6065 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006066#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (virtual_active())
6068 oneright();
6069 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006072#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 if (has_mbyte)
6074 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006075 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 ++curwin->w_cursor.col;
6079 }
6080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 }
6082#ifdef FEAT_FOLDING
6083 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6084 && cap->oap->op_type == OP_NOP)
6085 foldOpenCursor();
6086#endif
6087}
6088
6089/*
6090 * Cursor left commands.
6091 *
6092 * Returns TRUE when operator end should not be adjusted.
6093 */
6094 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006095nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 long n;
6098
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006099 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6100 {
6101 /* <C-Left> and <S-Left> move a word or WORD left */
6102 if (mod_mask & MOD_MASK_CTRL)
6103 cap->arg = 1;
6104 nv_bck_word(cap);
6105 return;
6106 }
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 cap->oap->motion_type = MCHAR;
6109 cap->oap->inclusive = FALSE;
6110 for (n = cap->count1; n > 0; --n)
6111 {
6112 if (oneleft() == FAIL)
6113 {
6114 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6115 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6116 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6117 */
6118 if ( (((cap->cmdchar == K_BS
6119 || cap->cmdchar == Ctrl_H)
6120 && vim_strchr(p_ww, 'b') != NULL)
6121 || (cap->cmdchar == 'h'
6122 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006123 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 && vim_strchr(p_ww, '<') != NULL))
6125 && curwin->w_cursor.lnum > 1)
6126 {
6127 --(curwin->w_cursor.lnum);
6128 coladvance((colnr_T)MAXCOL);
6129 curwin->w_set_curswant = TRUE;
6130
6131 /* When the NL before the first char has to be deleted we
6132 * put the cursor on the NUL after the previous line.
6133 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006134 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if ( (cap->oap->op_type == OP_DELETE
6136 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006137 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006139 char_u *cp = ml_get_cursor();
6140
6141 if (*cp != NUL)
6142 {
6143#ifdef FEAT_MBYTE
6144 if (has_mbyte)
6145 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6146 else
6147#endif
6148 ++curwin->w_cursor.col;
6149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 cap->retval |= CA_NO_ADJ_OP_END;
6151 }
6152 continue;
6153 }
6154 /* Only beep and flush if not moved at all */
6155 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6156 beep_flush();
6157 break;
6158 }
6159 }
6160#ifdef FEAT_FOLDING
6161 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6162 && cap->oap->op_type == OP_NOP)
6163 foldOpenCursor();
6164#endif
6165}
6166
6167/*
6168 * Cursor up commands.
6169 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6170 */
6171 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006172nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006174 if (mod_mask & MOD_MASK_SHIFT)
6175 {
6176 /* <S-Up> is page up */
6177 cap->arg = BACKWARD;
6178 nv_page(cap);
6179 }
6180 else
6181 {
6182 cap->oap->motion_type = MLINE;
6183 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6184 clearopbeep(cap->oap);
6185 else if (cap->arg)
6186 beginline(BL_WHITE | BL_FIX);
6187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188}
6189
6190/*
6191 * Cursor down commands.
6192 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6193 */
6194 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006195nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006197 if (mod_mask & MOD_MASK_SHIFT)
6198 {
6199 /* <S-Down> is page down */
6200 cap->arg = FORWARD;
6201 nv_page(cap);
6202 }
6203 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006204#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006206 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar74a47162017-02-26 19:09:05 +01006207 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006208 if (curwin->w_llist_ref == NULL)
6209 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6210 else
6211 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar74a47162017-02-26 19:09:05 +01006212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 else
6214#endif
6215 {
6216#ifdef FEAT_CMDWIN
6217 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006218 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 cmdwin_result = CAR;
6220 else
6221#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006222#ifdef FEAT_JOB_CHANNEL
6223 /* In a prompt buffer a <CR> in the last line invokes the callback. */
6224 if (bt_prompt(curbuf) && cap->cmdchar == CAR
6225 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
6226 {
6227 invoke_prompt_callback();
6228 if (restart_edit == 0)
6229 restart_edit = 'a';
6230 }
6231 else
6232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234 cap->oap->motion_type = MLINE;
6235 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6236 clearopbeep(cap->oap);
6237 else if (cap->arg)
6238 beginline(BL_WHITE | BL_FIX);
6239 }
6240 }
6241}
6242
6243#ifdef FEAT_SEARCHPATH
6244/*
6245 * Grab the file name under the cursor and edit it.
6246 */
6247 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006248nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006251 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006253 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 {
6255 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006256 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 return;
6258 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006259 if (curbuf_locked())
6260 {
6261 clearop(cap->oap);
6262 return;
6263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006265 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267 if (ptr != NULL)
6268 {
6269 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006270 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006271 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006273 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006274 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006275 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006276 {
6277 curwin->w_cursor.lnum = lnum;
6278 check_cursor_lnum();
6279 beginline(BL_SOL | BL_FIX);
6280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 vim_free(ptr);
6282 }
6283 else
6284 clearop(cap->oap);
6285}
6286#endif
6287
6288/*
6289 * <End> command: to end of current line or last line.
6290 */
6291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006292nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006294 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006296 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 nv_goto(cap);
6298 cap->count1 = 1; /* to end of current line */
6299 }
6300 nv_dollar(cap);
6301}
6302
6303/*
6304 * Handle the "$" command.
6305 */
6306 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006307nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
6309 cap->oap->motion_type = MCHAR;
6310 cap->oap->inclusive = TRUE;
6311#ifdef FEAT_VIRTUALEDIT
6312 /* In virtual mode when off the edge of a line and an operator
6313 * is pending (whew!) keep the cursor where it is.
6314 * Otherwise, send it to the end of the line. */
6315 if (!virtual_active() || gchar_cursor() != NUL
6316 || cap->oap->op_type == OP_NOP)
6317#endif
6318 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6319 if (cursor_down((long)(cap->count1 - 1),
6320 cap->oap->op_type == OP_NOP) == FAIL)
6321 clearopbeep(cap->oap);
6322#ifdef FEAT_FOLDING
6323 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6324 foldOpenCursor();
6325#endif
6326}
6327
6328/*
6329 * Implementation of '?' and '/' commands.
6330 * If cap->arg is TRUE don't set PC mark.
6331 */
6332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006333nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
6335 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006336 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
6338 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6339 {
6340 /* Translate "g??" to "g?g?" */
6341 cap->cmdchar = 'g';
6342 cap->nchar = '?';
6343 nv_operator(cap);
6344 return;
6345 }
6346
Bram Moolenaardda933d2016-09-03 21:04:58 +02006347 /* When using 'incsearch' the cursor may be moved to set a different search
6348 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6350
6351 if (cap->searchbuf == NULL)
6352 {
6353 clearop(oap);
6354 return;
6355 }
6356
Bram Moolenaar46539112015-02-17 15:43:57 +01006357 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006358 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006359 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360}
6361
6362/*
6363 * Handle "N" and "n" commands.
6364 * cap->arg is SEARCH_REV for "N", 0 for "n".
6365 */
6366 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006367nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
Bram Moolenaar46539112015-02-17 15:43:57 +01006369 pos_T old = curwin->w_cursor;
6370 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6371
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006372 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006373 {
6374 /* Avoid getting stuck on the current cursor position, which can
6375 * happen when an offset is given and the cursor is on the last char
6376 * in the buffer: Repeat with count + 1. */
6377 cap->count1 += 1;
6378 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6379 cap->count1 -= 1;
6380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381}
6382
6383/*
6384 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6385 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006386 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006388 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006389normal_search(
6390 cmdarg_T *cap,
6391 int dir,
6392 char_u *pat,
6393 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 int i;
6396
6397 cap->oap->motion_type = MCHAR;
6398 cap->oap->inclusive = FALSE;
6399 cap->oap->use_reg_one = TRUE;
6400 curwin->w_set_curswant = TRUE;
6401
6402 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006403 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (i == 0)
6405 clearop(cap->oap);
6406 else
6407 {
6408 if (i == 2)
6409 cap->oap->motion_type = MLINE;
6410#ifdef FEAT_VIRTUALEDIT
6411 curwin->w_cursor.coladd = 0;
6412#endif
6413#ifdef FEAT_FOLDING
6414 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6415 foldOpenCursor();
6416#endif
6417 }
6418
6419 /* "/$" will put the cursor after the end of the line, may need to
6420 * correct that here */
6421 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006422 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423}
6424
6425/*
6426 * Character search commands.
6427 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6428 * ',' and FALSE for ';'.
6429 * cap->nchar is NUL for ',' and ';' (repeat the search)
6430 */
6431 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006432nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 int t_cmd;
6435
6436 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6437 t_cmd = TRUE;
6438 else
6439 t_cmd = FALSE;
6440
6441 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6443 clearopbeep(cap->oap);
6444 else
6445 {
6446 curwin->w_set_curswant = TRUE;
6447#ifdef FEAT_VIRTUALEDIT
6448 /* Include a Tab for "tx" and for "dfx". */
6449 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6450 && (t_cmd || cap->oap->op_type != OP_NOP))
6451 {
6452 colnr_T scol, ecol;
6453
6454 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6455 curwin->w_cursor.coladd = ecol - scol;
6456 }
6457 else
6458 curwin->w_cursor.coladd = 0;
6459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461#ifdef FEAT_FOLDING
6462 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6463 foldOpenCursor();
6464#endif
6465 }
6466}
6467
6468/*
6469 * "[" and "]" commands.
6470 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6471 */
6472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006473nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006475 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 pos_T prev_pos;
6477 pos_T *pos = NULL; /* init for GCC */
6478 pos_T old_pos; /* cursor position before command */
6479 int flag;
6480 long n;
6481 int findc;
6482 int c;
6483
6484 cap->oap->motion_type = MCHAR;
6485 cap->oap->inclusive = FALSE;
6486 old_pos = curwin->w_cursor;
6487#ifdef FEAT_VIRTUALEDIT
6488 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6489#endif
6490
6491#ifdef FEAT_SEARCHPATH
6492 /*
6493 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6494 */
6495 if (cap->nchar == 'f')
6496 nv_gotofile(cap);
6497 else
6498#endif
6499
6500#ifdef FEAT_FIND_ID
6501 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006502 * Find the occurrence(s) of the identifier or define under cursor
6503 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 *
6505 * search list jump
6506 * fwd bwd fwd bwd fwd bwd
6507 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6508 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6509 */
6510 if (vim_strchr((char_u *)
6511#ifdef EBCDIC
6512 "iI\005dD\067",
6513#else
6514 "iI\011dD\004",
6515#endif
6516 cap->nchar) != NULL)
6517 {
6518 char_u *ptr;
6519 int len;
6520
6521 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6522 clearop(cap->oap);
6523 else
6524 {
6525 find_pattern_in_path(ptr, 0, len, TRUE,
6526 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6527 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6528 cap->count1,
6529 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6530 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6531 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6532 (linenr_T)MAXLNUM);
6533 curwin->w_set_curswant = TRUE;
6534 }
6535 }
6536 else
6537#endif
6538
6539 /*
6540 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6541 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6542 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6543 * "[m" or "]m" search for prev/next start of (Java) method.
6544 * "[M" or "]M" search for prev/next end of (Java) method.
6545 */
6546 if ( (cap->cmdchar == '['
6547 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6548 || (cap->cmdchar == ']'
6549 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6550 {
6551 if (cap->nchar == '*')
6552 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 prev_pos.lnum = 0;
6554 if (cap->nchar == 'm' || cap->nchar == 'M')
6555 {
6556 if (cap->cmdchar == '[')
6557 findc = '{';
6558 else
6559 findc = '}';
6560 n = 9999;
6561 }
6562 else
6563 {
6564 findc = cap->nchar;
6565 n = cap->count1;
6566 }
6567 for ( ; n > 0; --n)
6568 {
6569 if ((pos = findmatchlimit(cap->oap, findc,
6570 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6571 {
6572 if (new_pos.lnum == 0) /* nothing found */
6573 {
6574 if (cap->nchar != 'm' && cap->nchar != 'M')
6575 clearopbeep(cap->oap);
6576 }
6577 else
6578 pos = &new_pos; /* use last one found */
6579 break;
6580 }
6581 prev_pos = new_pos;
6582 curwin->w_cursor = *pos;
6583 new_pos = *pos;
6584 }
6585 curwin->w_cursor = old_pos;
6586
6587 /*
6588 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6589 * brought us to the match for "[m" and "]M" when inside a method.
6590 * Try finding the '{' or '}' we want to be at.
6591 * Also repeat for the given count.
6592 */
6593 if (cap->nchar == 'm' || cap->nchar == 'M')
6594 {
6595 /* norm is TRUE for "]M" and "[m" */
6596 int norm = ((findc == '{') == (cap->nchar == 'm'));
6597
6598 n = cap->count1;
6599 /* found a match: we were inside a method */
6600 if (prev_pos.lnum != 0)
6601 {
6602 pos = &prev_pos;
6603 curwin->w_cursor = prev_pos;
6604 if (norm)
6605 --n;
6606 }
6607 else
6608 pos = NULL;
6609 while (n > 0)
6610 {
6611 for (;;)
6612 {
6613 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6614 {
6615 /* if not found anything, that's an error */
6616 if (pos == NULL)
6617 clearopbeep(cap->oap);
6618 n = 0;
6619 break;
6620 }
6621 c = gchar_cursor();
6622 if (c == '{' || c == '}')
6623 {
6624 /* Must have found end/start of class: use it.
6625 * Or found the place to be at. */
6626 if ((c == findc && norm) || (n == 1 && !norm))
6627 {
6628 new_pos = curwin->w_cursor;
6629 pos = &new_pos;
6630 n = 0;
6631 }
6632 /* if no match found at all, we started outside of the
6633 * class and we're inside now. Just go on. */
6634 else if (new_pos.lnum == 0)
6635 {
6636 new_pos = curwin->w_cursor;
6637 pos = &new_pos;
6638 }
6639 /* found start/end of other method: go to match */
6640 else if ((pos = findmatchlimit(cap->oap, findc,
6641 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6642 0)) == NULL)
6643 n = 0;
6644 else
6645 curwin->w_cursor = *pos;
6646 break;
6647 }
6648 }
6649 --n;
6650 }
6651 curwin->w_cursor = old_pos;
6652 if (pos == NULL && new_pos.lnum != 0)
6653 clearopbeep(cap->oap);
6654 }
6655 if (pos != NULL)
6656 {
6657 setpcmark();
6658 curwin->w_cursor = *pos;
6659 curwin->w_set_curswant = TRUE;
6660#ifdef FEAT_FOLDING
6661 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6662 && cap->oap->op_type == OP_NOP)
6663 foldOpenCursor();
6664#endif
6665 }
6666 }
6667
6668 /*
6669 * "[[", "[]", "]]" and "][": move to start or end of function
6670 */
6671 else if (cap->nchar == '[' || cap->nchar == ']')
6672 {
6673 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6674 flag = '{';
6675 else
6676 flag = '}'; /* "][" or "[]" */
6677
6678 curwin->w_set_curswant = TRUE;
6679 /*
6680 * Imitate strange Vi behaviour: When using "]]" with an operator
6681 * we also stop at '}'.
6682 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006683 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 (cap->oap->op_type != OP_NOP
6685 && cap->arg == FORWARD && flag == '{')))
6686 clearopbeep(cap->oap);
6687 else
6688 {
6689 if (cap->oap->op_type == OP_NOP)
6690 beginline(BL_WHITE | BL_FIX);
6691#ifdef FEAT_FOLDING
6692 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6693 foldOpenCursor();
6694#endif
6695 }
6696 }
6697
6698 /*
6699 * "[p", "[P", "]P" and "]p": put with indent adjustment
6700 */
6701 else if (cap->nchar == 'p' || cap->nchar == 'P')
6702 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006703 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006705 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6706 ? FORWARD : BACKWARD;
6707 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006708 int was_visual = VIsual_active;
6709 int line_count = curbuf->b_ml.ml_line_count;
6710 pos_T start, end;
6711
6712 if (VIsual_active)
6713 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006714 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006715 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006716 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006717 curwin->w_cursor = (dir == BACKWARD ? start : end);
6718 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006719# ifdef FEAT_CLIPBOARD
6720 adjust_clip_reg(&regname);
6721# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006723
6724 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006725 if (was_visual)
6726 {
6727 VIsual = start;
6728 curwin->w_cursor = end;
6729 if (dir == BACKWARD)
6730 {
6731 /* adjust lines */
6732 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6733 curwin->w_cursor.lnum +=
6734 curbuf->b_ml.ml_line_count - line_count;
6735 }
6736
6737 VIsual_active = TRUE;
6738 if (VIsual_mode == 'V')
6739 {
6740 /* delete visually selected lines */
6741 cap->cmdchar = 'd';
6742 cap->nchar = NUL;
6743 cap->oap->regname = regname;
6744 nv_operator(cap);
6745 do_pending_operator(cap, 0, FALSE);
6746 }
6747 if (VIsual_active)
6748 {
6749 end_visual_mode();
6750 redraw_later(SOME_VALID);
6751 }
6752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 }
6754 }
6755
6756 /*
6757 * "['", "[`", "]'" and "]`": jump to next mark
6758 */
6759 else if (cap->nchar == '\'' || cap->nchar == '`')
6760 {
6761 pos = &curwin->w_cursor;
6762 for (n = cap->count1; n > 0; --n)
6763 {
6764 prev_pos = *pos;
6765 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6766 cap->nchar == '\'');
6767 if (pos == NULL)
6768 break;
6769 }
6770 if (pos == NULL)
6771 pos = &prev_pos;
6772 nv_cursormark(cap, cap->nchar == '\'', pos);
6773 }
6774
6775#ifdef FEAT_MOUSE
6776 /*
6777 * [ or ] followed by a middle mouse click: put selected text with
6778 * indent adjustment. Any other button just does as usual.
6779 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006780 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 {
6782 (void)do_mouse(cap->oap, cap->nchar,
6783 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6784 cap->count1, PUT_FIXINDENT);
6785 }
6786#endif /* FEAT_MOUSE */
6787
6788#ifdef FEAT_FOLDING
6789 /*
6790 * "[z" and "]z": move to start or end of open fold.
6791 */
6792 else if (cap->nchar == 'z')
6793 {
6794 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6795 cap->count1) == FAIL)
6796 clearopbeep(cap->oap);
6797 }
6798#endif
6799
6800#ifdef FEAT_DIFF
6801 /*
6802 * "[c" and "]c": move to next or previous diff-change.
6803 */
6804 else if (cap->nchar == 'c')
6805 {
6806 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6807 cap->count1) == FAIL)
6808 clearopbeep(cap->oap);
6809 }
6810#endif
6811
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006812#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006813 /*
6814 * "[s", "[S", "]s" and "]S": move to next spell error.
6815 */
6816 else if (cap->nchar == 's' || cap->nchar == 'S')
6817 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006818 setpcmark();
6819 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006820 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6821 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006822 {
6823 clearopbeep(cap->oap);
6824 break;
6825 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006826 else
6827 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006828# ifdef FEAT_FOLDING
6829 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6830 foldOpenCursor();
6831# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006832 }
6833#endif
6834
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 /* Not a valid cap->nchar. */
6836 else
6837 clearopbeep(cap->oap);
6838}
6839
6840/*
6841 * Handle Normal mode "%" command.
6842 */
6843 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006844nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006847#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 linenr_T lnum = curwin->w_cursor.lnum;
6849#endif
6850
6851 cap->oap->inclusive = TRUE;
6852 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6853 {
6854 if (cap->count0 > 100)
6855 clearopbeep(cap->oap);
6856 else
6857 {
6858 cap->oap->motion_type = MLINE;
6859 setpcmark();
6860 /* Round up, so CTRL-G will give same value. Watch out for a
6861 * large line count, the line number must not go negative! */
6862 if (curbuf->b_ml.ml_line_count > 1000000)
6863 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6864 / 100L * cap->count0;
6865 else
6866 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6867 cap->count0 + 99L) / 100L;
6868 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6869 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6870 beginline(BL_SOL | BL_FIX);
6871 }
6872 }
6873 else /* "%" : go to matching paren */
6874 {
6875 cap->oap->motion_type = MCHAR;
6876 cap->oap->use_reg_one = TRUE;
6877 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6878 clearopbeep(cap->oap);
6879 else
6880 {
6881 setpcmark();
6882 curwin->w_cursor = *pos;
6883 curwin->w_set_curswant = TRUE;
6884#ifdef FEAT_VIRTUALEDIT
6885 curwin->w_cursor.coladd = 0;
6886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
6889 }
6890#ifdef FEAT_FOLDING
6891 if (cap->oap->op_type == OP_NOP
6892 && lnum != curwin->w_cursor.lnum
6893 && (fdo_flags & FDO_PERCENT)
6894 && KeyTyped)
6895 foldOpenCursor();
6896#endif
6897}
6898
6899/*
6900 * Handle "(" and ")" commands.
6901 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6902 */
6903 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006904nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905{
6906 cap->oap->motion_type = MCHAR;
6907 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006908 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6909 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 curwin->w_set_curswant = TRUE;
6911
6912 if (findsent(cap->arg, cap->count1) == FAIL)
6913 clearopbeep(cap->oap);
6914 else
6915 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006916 /* Don't leave the cursor on the NUL past end of line. */
6917 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918#ifdef FEAT_VIRTUALEDIT
6919 curwin->w_cursor.coladd = 0;
6920#endif
6921#ifdef FEAT_FOLDING
6922 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6923 foldOpenCursor();
6924#endif
6925 }
6926}
6927
6928/*
6929 * "m" command: Mark a position.
6930 */
6931 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006932nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
6934 if (!checkclearop(cap->oap))
6935 {
6936 if (setmark(cap->nchar) == FAIL)
6937 clearopbeep(cap->oap);
6938 }
6939}
6940
6941/*
6942 * "{" and "}" commands.
6943 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6944 */
6945 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006946nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947{
6948 cap->oap->motion_type = MCHAR;
6949 cap->oap->inclusive = FALSE;
6950 cap->oap->use_reg_one = TRUE;
6951 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006952 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 clearopbeep(cap->oap);
6954 else
6955 {
6956#ifdef FEAT_VIRTUALEDIT
6957 curwin->w_cursor.coladd = 0;
6958#endif
6959#ifdef FEAT_FOLDING
6960 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6961 foldOpenCursor();
6962#endif
6963 }
6964}
6965
6966/*
6967 * "u" command: Undo or make lower case.
6968 */
6969 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006970nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006972 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 {
6974 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6975 cap->cmdchar = 'g';
6976 cap->nchar = 'u';
6977 nv_operator(cap);
6978 }
6979 else
6980 nv_kundo(cap);
6981}
6982
6983/*
6984 * <Undo> command.
6985 */
6986 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006987nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 if (!checkclearopq(cap->oap))
6990 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02006991#ifdef FEAT_JOB_CHANNEL
6992 if (bt_prompt(curbuf))
6993 {
6994 clearopbeep(cap->oap);
6995 return;
6996 }
6997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 u_undo((int)cap->count1);
6999 curwin->w_set_curswant = TRUE;
7000 }
7001}
7002
7003/*
7004 * Handle the "r" command.
7005 */
7006 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007007nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008{
7009 char_u *ptr;
7010 int had_ctrl_v;
7011 long n;
7012
7013 if (checkclearop(cap->oap))
7014 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007015#ifdef FEAT_JOB_CHANNEL
7016 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7017 {
7018 clearopbeep(cap->oap);
7019 return;
7020 }
7021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
7023 /* get another character */
7024 if (cap->nchar == Ctrl_V)
7025 {
7026 had_ctrl_v = Ctrl_V;
7027 cap->nchar = get_literal();
7028 /* Don't redo a multibyte character with CTRL-V. */
7029 if (cap->nchar > DEL)
7030 had_ctrl_v = NUL;
7031 }
7032 else
7033 had_ctrl_v = NUL;
7034
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007035 /* Abort if the character is a special key. */
7036 if (IS_SPECIAL(cap->nchar))
7037 {
7038 clearopbeep(cap->oap);
7039 return;
7040 }
7041
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 /* Visual mode "r" */
7043 if (VIsual_active)
7044 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007045 if (got_int)
7046 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007047 if (had_ctrl_v)
7048 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01007049 /* Use a special (negative) number to make a difference between a
7050 * literal CR or NL and a line break. */
7051 if (cap->nchar == CAR)
7052 cap->nchar = REPLACE_CR_NCHAR;
7053 else if (cap->nchar == NL)
7054 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01007055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 nv_operator(cap);
7057 return;
7058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
7060#ifdef FEAT_VIRTUALEDIT
7061 /* Break tabs, etc. */
7062 if (virtual_active())
7063 {
7064 if (u_save_cursor() == FAIL)
7065 return;
7066 if (gchar_cursor() == NUL)
7067 {
7068 /* Add extra space and put the cursor on the first one. */
7069 coladvance_force((colnr_T)(getviscol() + cap->count1));
7070 curwin->w_cursor.col -= cap->count1;
7071 }
7072 else if (gchar_cursor() == TAB)
7073 coladvance_force(getviscol());
7074 }
7075#endif
7076
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007077 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007079 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080#ifdef FEAT_MBYTE
7081 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7082#endif
7083 )
7084 {
7085 clearopbeep(cap->oap);
7086 return;
7087 }
7088
7089 /*
7090 * Replacing with a TAB is done by edit() when it is complicated because
7091 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7092 * Other characters are done below to avoid problems with things like
7093 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7094 */
7095 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7096 {
7097 stuffnumReadbuff(cap->count1);
7098 stuffcharReadbuff('R');
7099 stuffcharReadbuff('\t');
7100 stuffcharReadbuff(ESC);
7101 return;
7102 }
7103
7104 /* save line for undo */
7105 if (u_save_cursor() == FAIL)
7106 return;
7107
7108 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7109 {
7110 /*
7111 * Replace character(s) by a single newline.
7112 * Strange vi behaviour: Only one newline is inserted.
7113 * Delete the characters here.
7114 * Insert the newline with an insert command, takes care of
7115 * autoindent. The insert command depends on being on the last
7116 * character of a line or not.
7117 */
7118#ifdef FEAT_MBYTE
7119 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7120#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007121 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122#endif
7123 stuffcharReadbuff('\r');
7124 stuffcharReadbuff(ESC);
7125
7126 /* Give 'r' to edit(), to get the redo command right. */
7127 invoke_edit(cap, TRUE, 'r', FALSE);
7128 }
7129 else
7130 {
7131 prep_redo(cap->oap->regname, cap->count1,
7132 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7133
7134 curbuf->b_op_start = curwin->w_cursor;
7135#ifdef FEAT_MBYTE
7136 if (has_mbyte)
7137 {
7138 int old_State = State;
7139
7140 if (cap->ncharC1 != 0)
7141 AppendCharToRedobuff(cap->ncharC1);
7142 if (cap->ncharC2 != 0)
7143 AppendCharToRedobuff(cap->ncharC2);
7144
7145 /* This is slow, but it handles replacing a single-byte with a
7146 * multi-byte and the other way around. Also handles adding
7147 * composing characters for utf-8. */
7148 for (n = cap->count1; n > 0; --n)
7149 {
7150 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007151 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7152 {
7153 int c = ins_copychar(curwin->w_cursor.lnum
7154 + (cap->nchar == Ctrl_Y ? -1 : 1));
7155 if (c != NUL)
7156 ins_char(c);
7157 else
7158 /* will be decremented further down */
7159 ++curwin->w_cursor.col;
7160 }
7161 else
7162 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 State = old_State;
7164 if (cap->ncharC1 != 0)
7165 ins_char(cap->ncharC1);
7166 if (cap->ncharC2 != 0)
7167 ins_char(cap->ncharC2);
7168 }
7169 }
7170 else
7171#endif
7172 {
7173 /*
7174 * Replace the characters within one line.
7175 */
7176 for (n = cap->count1; n > 0; --n)
7177 {
7178 /*
7179 * Get ptr again, because u_save and/or showmatch() will have
7180 * released the line. At the same time we let know that the
7181 * line will be changed.
7182 */
7183 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007184 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7185 {
7186 int c = ins_copychar(curwin->w_cursor.lnum
7187 + (cap->nchar == Ctrl_Y ? -1 : 1));
7188 if (c != NUL)
7189 ptr[curwin->w_cursor.col] = c;
7190 }
7191 else
7192 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 if (p_sm && msg_silent == 0)
7194 showmatch(cap->nchar);
7195 ++curwin->w_cursor.col;
7196 }
7197#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007198 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007200 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
Bram Moolenaar009b2592004-10-24 19:18:58 +00007202 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007203 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007205 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
7207#endif
7208
7209 /* mark the buffer as changed and prepare for displaying */
7210 changed_bytes(curwin->w_cursor.lnum,
7211 (colnr_T)(curwin->w_cursor.col - cap->count1));
7212 }
7213 --curwin->w_cursor.col; /* cursor on the last replaced char */
7214#ifdef FEAT_MBYTE
7215 /* if the character on the left of the current cursor is a multi-byte
7216 * character, move two characters left */
7217 if (has_mbyte)
7218 mb_adjust_cursor();
7219#endif
7220 curbuf->b_op_end = curwin->w_cursor;
7221 curwin->w_set_curswant = TRUE;
7222 set_last_insert(cap->nchar);
7223 }
7224}
7225
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226/*
7227 * 'o': Exchange start and end of Visual area.
7228 * 'O': same, but in block mode exchange left and right corners.
7229 */
7230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007231v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 pos_T old_cursor;
7234 colnr_T left, right;
7235
7236 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7237 {
7238 old_cursor = curwin->w_cursor;
7239 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7240 curwin->w_cursor.lnum = VIsual.lnum;
7241 coladvance(left);
7242 VIsual = curwin->w_cursor;
7243
7244 curwin->w_cursor.lnum = old_cursor.lnum;
7245 curwin->w_curswant = right;
7246 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7247 * right one column */
7248 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7249 ++curwin->w_curswant;
7250 coladvance(curwin->w_curswant);
7251 if (curwin->w_cursor.col == old_cursor.col
7252#ifdef FEAT_VIRTUALEDIT
7253 && (!virtual_active()
7254 || curwin->w_cursor.coladd == old_cursor.coladd)
7255#endif
7256 )
7257 {
7258 curwin->w_cursor.lnum = VIsual.lnum;
7259 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7260 ++right;
7261 coladvance(right);
7262 VIsual = curwin->w_cursor;
7263
7264 curwin->w_cursor.lnum = old_cursor.lnum;
7265 coladvance(left);
7266 curwin->w_curswant = left;
7267 }
7268 }
7269 else
7270 {
7271 old_cursor = curwin->w_cursor;
7272 curwin->w_cursor = VIsual;
7273 VIsual = old_cursor;
7274 curwin->w_set_curswant = TRUE;
7275 }
7276}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
7278/*
7279 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7280 */
7281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007282nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 if (VIsual_active) /* "R" is replace lines */
7285 {
7286 cap->cmdchar = 'c';
7287 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007288 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 VIsual_mode = 'V';
7290 nv_operator(cap);
7291 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007292 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
7294 if (!curbuf->b_p_ma)
7295 EMSG(_(e_modifiable));
7296 else
7297 {
7298#ifdef FEAT_VIRTUALEDIT
7299 if (virtual_active())
7300 coladvance(getviscol());
7301#endif
7302 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7303 }
7304 }
7305}
7306
7307#ifdef FEAT_VREPLACE
7308/*
7309 * "gr".
7310 */
7311 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007312nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 if (VIsual_active)
7315 {
7316 cap->cmdchar = 'r';
7317 cap->nchar = cap->extra_char;
7318 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7319 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007320 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 {
7322 if (!curbuf->b_p_ma)
7323 EMSG(_(e_modifiable));
7324 else
7325 {
7326 if (cap->extra_char == Ctrl_V) /* get another character */
7327 cap->extra_char = get_literal();
7328 stuffcharReadbuff(cap->extra_char);
7329 stuffcharReadbuff(ESC);
7330# ifdef FEAT_VIRTUALEDIT
7331 if (virtual_active())
7332 coladvance(getviscol());
7333# endif
7334 invoke_edit(cap, TRUE, 'v', FALSE);
7335 }
7336 }
7337}
7338#endif
7339
7340/*
7341 * Swap case for "~" command, when it does not work like an operator.
7342 */
7343 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007344n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345{
7346 long n;
7347 pos_T startpos;
7348 int did_change = 0;
7349#ifdef FEAT_NETBEANS_INTG
7350 pos_T pos;
7351 char_u *ptr;
7352 int count;
7353#endif
7354
7355 if (checkclearopq(cap->oap))
7356 return;
7357
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007358 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
7360 clearopbeep(cap->oap);
7361 return;
7362 }
7363
7364 prep_redo_cmd(cap);
7365
7366 if (u_save_cursor() == FAIL)
7367 return;
7368
7369 startpos = curwin->w_cursor;
7370#ifdef FEAT_NETBEANS_INTG
7371 pos = startpos;
7372#endif
7373 for (n = cap->count1; n > 0; --n)
7374 {
7375 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7376 inc_cursor();
7377 if (gchar_cursor() == NUL)
7378 {
7379 if (vim_strchr(p_ww, '~') != NULL
7380 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7381 {
7382#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007383 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
7385 if (did_change)
7386 {
7387 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007388 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007389 netbeans_removed(curbuf, pos.lnum, pos.col,
7390 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007392 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 }
7394 pos.col = 0;
7395 pos.lnum++;
7396 }
7397#endif
7398 ++curwin->w_cursor.lnum;
7399 curwin->w_cursor.col = 0;
7400 if (n > 1)
7401 {
7402 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7403 break;
7404 u_clearline();
7405 }
7406 }
7407 else
7408 break;
7409 }
7410 }
7411#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007412 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
7414 ptr = ml_get(pos.lnum);
7415 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007416 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7417 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 }
7419#endif
7420
7421
7422 check_cursor();
7423 curwin->w_set_curswant = TRUE;
7424 if (did_change)
7425 {
7426 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7427 0L);
7428 curbuf->b_op_start = startpos;
7429 curbuf->b_op_end = curwin->w_cursor;
7430 if (curbuf->b_op_end.col > 0)
7431 --curbuf->b_op_end.col;
7432 }
7433}
7434
7435/*
7436 * Move cursor to mark.
7437 */
7438 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007439nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
7441 if (check_mark(pos) == FAIL)
7442 clearop(cap->oap);
7443 else
7444 {
7445 if (cap->cmdchar == '\''
7446 || cap->cmdchar == '`'
7447 || cap->cmdchar == '['
7448 || cap->cmdchar == ']')
7449 setpcmark();
7450 curwin->w_cursor = *pos;
7451 if (flag)
7452 beginline(BL_WHITE | BL_FIX);
7453 else
7454 check_cursor();
7455 }
7456 cap->oap->motion_type = flag ? MLINE : MCHAR;
7457 if (cap->cmdchar == '`')
7458 cap->oap->use_reg_one = TRUE;
7459 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7460 curwin->w_set_curswant = TRUE;
7461}
7462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463/*
7464 * Handle commands that are operators in Visual mode.
7465 */
7466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007467v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
7469 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7470
7471 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007472 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 if (isupper(cap->cmdchar))
7474 {
7475 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007476 {
7477 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7481 curwin->w_curswant = MAXCOL;
7482 }
7483 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7484 nv_operator(cap);
7485}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486
7487/*
7488 * "s" and "S" commands.
7489 */
7490 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007491nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007493#ifdef FEAT_TERMINAL
7494 /* When showing output of term_dumpdiff() swap the top and botom. */
7495 if (term_swap_diff() == OK)
7496 return;
7497#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02007498#ifdef FEAT_JOB_CHANNEL
7499 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7500 {
7501 clearopbeep(cap->oap);
7502 return;
7503 }
7504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7506 {
7507 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007508 {
7509 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 cap->cmdchar = 'c';
7513 nv_operator(cap);
7514 }
7515 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 nv_optrans(cap);
7517}
7518
7519/*
7520 * Abbreviated commands.
7521 */
7522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007523nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524{
7525 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7526 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7527
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 /* in Visual mode these commands are operators */
7529 if (VIsual_active)
7530 v_visop(cap);
7531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 nv_optrans(cap);
7533}
7534
7535/*
7536 * Translate a command into another command.
7537 */
7538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007539nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
7541 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7542 (char_u *)"d$", (char_u *)"c$",
7543 (char_u *)"cl", (char_u *)"cc",
7544 (char_u *)"yy", (char_u *)":s\r"};
7545 static char_u *str = (char_u *)"xXDCsSY&";
7546
7547 if (!checkclearopq(cap->oap))
7548 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007549 /* In Vi "2D" doesn't delete the next line. Can't translate it
7550 * either, because "2." should also not use the count. */
7551 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7552 {
7553 cap->oap->start = curwin->w_cursor;
7554 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007555#ifdef FEAT_EVAL
7556 set_op_var(OP_DELETE);
7557#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007558 cap->count1 = 1;
7559 nv_dollar(cap);
7560 finish_op = TRUE;
7561 ResetRedobuff();
7562 AppendCharToRedobuff('D');
7563 }
7564 else
7565 {
7566 if (cap->count0)
7567 stuffnumReadbuff(cap->count0);
7568 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 }
7571 cap->opcount = 0;
7572}
7573
7574/*
7575 * "'" and "`" commands. Also for "g'" and "g`".
7576 * cap->arg is TRUE for "'" and "g'".
7577 */
7578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007579nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
7581 pos_T *pos;
7582 int c;
7583#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007584 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7586#endif
7587
7588 if (cap->cmdchar == 'g')
7589 c = cap->extra_char;
7590 else
7591 c = cap->nchar;
7592 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7593 if (pos == (pos_T *)-1) /* jumped to other file */
7594 {
7595 if (cap->arg)
7596 {
7597 check_cursor_lnum();
7598 beginline(BL_WHITE | BL_FIX);
7599 }
7600 else
7601 check_cursor();
7602 }
7603 else
7604 nv_cursormark(cap, cap->arg, pos);
7605
7606#ifdef FEAT_VIRTUALEDIT
7607 /* May need to clear the coladd that a mark includes. */
7608 if (!virtual_active())
7609 curwin->w_cursor.coladd = 0;
7610#endif
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007611 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612#ifdef FEAT_FOLDING
7613 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007614 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007615 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 && (fdo_flags & FDO_MARK)
7617 && old_KeyTyped)
7618 foldOpenCursor();
7619#endif
7620}
7621
7622/*
7623 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7624 */
7625 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007626nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627{
7628#ifdef FEAT_JUMPLIST
7629 pos_T *pos;
7630# ifdef FEAT_FOLDING
7631 linenr_T lnum = curwin->w_cursor.lnum;
7632 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7633# endif
7634
7635 if (!checkclearopq(cap->oap))
7636 {
7637 if (cap->cmdchar == 'g')
7638 pos = movechangelist((int)cap->count1);
7639 else
7640 pos = movemark((int)cap->count1);
7641 if (pos == (pos_T *)-1) /* jump to other file */
7642 {
7643 curwin->w_set_curswant = TRUE;
7644 check_cursor();
7645 }
7646 else if (pos != NULL) /* can jump */
7647 nv_cursormark(cap, FALSE, pos);
7648 else if (cap->cmdchar == 'g')
7649 {
7650 if (curbuf->b_changelistlen == 0)
7651 EMSG(_("E664: changelist is empty"));
7652 else if (cap->count1 < 0)
7653 EMSG(_("E662: At start of changelist"));
7654 else
7655 EMSG(_("E663: At end of changelist"));
7656 }
7657 else
7658 clearopbeep(cap->oap);
7659# ifdef FEAT_FOLDING
7660 if (cap->oap->op_type == OP_NOP
7661 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7662 && (fdo_flags & FDO_MARK)
7663 && old_KeyTyped)
7664 foldOpenCursor();
7665# endif
7666 }
7667#else
7668 clearopbeep(cap->oap);
7669#endif
7670}
7671
7672/*
7673 * Handle '"' command.
7674 */
7675 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007676nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
7678 if (checkclearop(cap->oap))
7679 return;
7680#ifdef FEAT_EVAL
7681 if (cap->nchar == '=')
7682 cap->nchar = get_expr_register();
7683#endif
7684 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7685 {
7686 cap->oap->regname = cap->nchar;
7687 cap->opcount = cap->count0; /* remember count before '"' */
7688#ifdef FEAT_EVAL
7689 set_reg_var(cap->oap->regname);
7690#endif
7691 }
7692 else
7693 clearopbeep(cap->oap);
7694}
7695
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696/*
7697 * Handle "v", "V" and "CTRL-V" commands.
7698 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7699 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007700 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 */
7702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007703nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007705 if (cap->cmdchar == Ctrl_Q)
7706 cap->cmdchar = Ctrl_V;
7707
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7709 * characterwise, linewise, or blockwise. */
7710 if (cap->oap->op_type != OP_NOP)
7711 {
7712 cap->oap->motion_force = cap->cmdchar;
7713 finish_op = FALSE; /* operator doesn't finish now but later */
7714 return;
7715 }
7716
7717 VIsual_select = cap->arg;
7718 if (VIsual_active) /* change Visual mode */
7719 {
7720 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7721 end_visual_mode();
7722 else /* toggle char/block mode */
7723 { /* or char/line mode */
7724 VIsual_mode = cap->cmdchar;
7725 showmode();
7726 }
7727 redraw_curbuf_later(INVERTED); /* update the inversion */
7728 }
7729 else /* start Visual mode */
7730 {
7731 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007732 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007734 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 VIsual = curwin->w_cursor;
7736
7737 VIsual_active = TRUE;
7738 VIsual_reselect = TRUE;
7739 if (!cap->arg)
7740 /* start Select mode when 'selectmode' contains "cmd" */
7741 may_start_select('c');
7742#ifdef FEAT_MOUSE
7743 setmouse();
7744#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007745 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 redraw_cmdline = TRUE; /* show visual mode later */
7747 /*
7748 * For V and ^V, we multiply the number of lines even if there
7749 * was only one -- webb
7750 */
7751 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7752 {
7753 curwin->w_cursor.lnum +=
7754 resel_VIsual_line_count * cap->count0 - 1;
7755 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7756 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7757 }
7758 VIsual_mode = resel_VIsual_mode;
7759 if (VIsual_mode == 'v')
7760 {
7761 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007762 {
7763 validate_virtcol();
7764 curwin->w_curswant = curwin->w_virtcol
7765 + resel_VIsual_vcol * cap->count0 - 1;
7766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007768 curwin->w_curswant = resel_VIsual_vcol;
7769 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007771 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {
7773 curwin->w_curswant = MAXCOL;
7774 coladvance((colnr_T)MAXCOL);
7775 }
7776 else if (VIsual_mode == Ctrl_V)
7777 {
7778 validate_virtcol();
7779 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007780 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 coladvance(curwin->w_curswant);
7782 }
7783 else
7784 curwin->w_set_curswant = TRUE;
7785 redraw_curbuf_later(INVERTED); /* show the inversion */
7786 }
7787 else
7788 {
7789 if (!cap->arg)
7790 /* start Select mode when 'selectmode' contains "cmd" */
7791 may_start_select('c');
7792 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007793 if (VIsual_mode != 'V' && *p_sel == 'e')
7794 ++cap->count1; /* include one more char */
7795 if (cap->count0 > 0 && --cap->count1 > 0)
7796 {
7797 /* With a count select that many characters or lines. */
7798 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7799 nv_right(cap);
7800 else if (VIsual_mode == 'V')
7801 nv_down(cap);
7802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 }
7804 }
7805}
7806
7807/*
7808 * Start selection for Shift-movement keys.
7809 */
7810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007811start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 /* if 'selectmode' contains "key", start Select mode */
7814 may_start_select('k');
7815 n_start_visual_mode('v');
7816}
7817
7818/*
7819 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7820 */
7821 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007822may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 VIsual_select = (stuff_empty() && typebuf_typed()
7825 && (vim_strchr(p_slm, c) != NULL));
7826}
7827
7828/*
7829 * Start Visual mode "c".
7830 * Should set VIsual_select before calling this.
7831 */
7832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007833n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007835#ifdef FEAT_CONCEAL
7836 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007837 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007838#endif
7839
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 VIsual_mode = c;
7841 VIsual_active = TRUE;
7842 VIsual_reselect = TRUE;
7843#ifdef FEAT_VIRTUALEDIT
7844 /* Corner case: the 0 position in a tab may change when going into
7845 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7846 */
7847 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007848 {
7849 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852#endif
7853 VIsual = curwin->w_cursor;
7854
7855#ifdef FEAT_FOLDING
7856 foldAdjustVisual();
7857#endif
7858
7859#ifdef FEAT_MOUSE
7860 setmouse();
7861#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007862#ifdef FEAT_CONCEAL
7863 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007864 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007865#endif
7866
Bram Moolenaar09df3122006-01-23 22:23:09 +00007867 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 redraw_cmdline = TRUE; /* show visual mode later */
7869#ifdef FEAT_CLIPBOARD
7870 /* Make sure the clipboard gets updated. Needed because start and
7871 * end may still be the same, and the selection needs to be owned */
7872 clip_star.vmode = NUL;
7873#endif
7874
7875 /* Only need to redraw this line, unless still need to redraw an old
7876 * Visual area (when 'lazyredraw' is set). */
7877 if (curwin->w_redr_type < INVERTED)
7878 {
7879 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7880 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7881 }
7882}
7883
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884
7885/*
7886 * CTRL-W: Window commands
7887 */
7888 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007889nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007891 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007892 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007893 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007894 cap->cmdchar = ':';
7895 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007896 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007897 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007898 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900}
7901
7902/*
7903 * CTRL-Z: Suspend
7904 */
7905 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007906nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 if (VIsual_active)
7910 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 do_cmdline_cmd((char_u *)"st");
7912}
7913
7914/*
7915 * Commands starting with "g".
7916 */
7917 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007918nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919{
7920 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 int i;
7923 int flag = FALSE;
7924
7925 switch (cap->nchar)
7926 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007927 case Ctrl_A:
7928 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929#ifdef MEM_PROFILE
7930 /*
7931 * "g^A": dump log of used memory.
7932 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007933 if (!VIsual_active && cap->nchar == Ctrl_A)
7934 vim_mem_profile_dump();
7935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007937 /*
7938 * "g^A/g^X": sequentially increment visually selected region
7939 */
7940 if (VIsual_active)
7941 {
7942 cap->arg = TRUE;
7943 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007944 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007945 nv_addsub(cap);
7946 }
7947 else
7948 clearopbeep(oap);
7949 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
7951#ifdef FEAT_VREPLACE
7952 /*
7953 * "gR": Enter virtual replace mode.
7954 */
7955 case 'R':
7956 cap->arg = TRUE;
7957 nv_Replace(cap);
7958 break;
7959
7960 case 'r':
7961 nv_vreplace(cap);
7962 break;
7963#endif
7964
7965 case '&':
7966 do_cmdline_cmd((char_u *)"%s//~/&");
7967 break;
7968
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 /*
7970 * "gv": Reselect the previous Visual area. If Visual already active,
7971 * exchange previous and current Visual area.
7972 */
7973 case 'v':
7974 if (checkclearop(oap))
7975 break;
7976
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007977 if ( curbuf->b_visual.vi_start.lnum == 0
7978 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7979 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 beep_flush();
7981 else
7982 {
7983 /* set w_cursor to the start of the Visual area, tpos to the end */
7984 if (VIsual_active)
7985 {
7986 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007987 VIsual_mode = curbuf->b_visual.vi_mode;
7988 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989# ifdef FEAT_EVAL
7990 curbuf->b_visual_mode_eval = i;
7991# endif
7992 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007993 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7994 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007996 tpos = curbuf->b_visual.vi_end;
7997 curbuf->b_visual.vi_end = curwin->w_cursor;
7998 curwin->w_cursor = curbuf->b_visual.vi_start;
7999 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 }
8001 else
8002 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00008003 VIsual_mode = curbuf->b_visual.vi_mode;
8004 curwin->w_curswant = curbuf->b_visual.vi_curswant;
8005 tpos = curbuf->b_visual.vi_end;
8006 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 }
8008
8009 VIsual_active = TRUE;
8010 VIsual_reselect = TRUE;
8011
8012 /* Set Visual to the start and w_cursor to the end of the Visual
8013 * area. Make sure they are on an existing character. */
8014 check_cursor();
8015 VIsual = curwin->w_cursor;
8016 curwin->w_cursor = tpos;
8017 check_cursor();
8018 update_topline();
8019 /*
8020 * When called from normal "g" command: start Select mode when
8021 * 'selectmode' contains "cmd". When called for K_SELECT, always
8022 * start Select mode.
8023 */
8024 if (cap->arg)
8025 VIsual_select = TRUE;
8026 else
8027 may_start_select('c');
8028#ifdef FEAT_MOUSE
8029 setmouse();
8030#endif
8031#ifdef FEAT_CLIPBOARD
8032 /* Make sure the clipboard gets updated. Needed because start and
8033 * end are still the same, and the selection needs to be owned */
8034 clip_star.vmode = NUL;
8035#endif
8036 redraw_curbuf_later(INVERTED);
8037 showmode();
8038 }
8039 break;
8040 /*
8041 * "gV": Don't reselect the previous Visual area after a Select mode
8042 * mapping of menu.
8043 */
8044 case 'V':
8045 VIsual_reselect = FALSE;
8046 break;
8047
8048 /*
8049 * "gh": start Select mode.
8050 * "gH": start Select line mode.
8051 * "g^H": start Select block mode.
8052 */
8053 case K_BS:
8054 cap->nchar = Ctrl_H;
8055 /* FALLTHROUGH */
8056 case 'h':
8057 case 'H':
8058 case Ctrl_H:
8059# ifdef EBCDIC
8060 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8061 if (cap->nchar == Ctrl_H)
8062 cap->cmdchar = Ctrl_V;
8063 else
8064# endif
8065 cap->cmdchar = cap->nchar + ('v' - 'h');
8066 cap->arg = TRUE;
8067 nv_visual(cap);
8068 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008069
8070 /* "gn", "gN" visually select next/previous search match
8071 * "gn" selects next match
8072 * "gN" selects previous match
8073 */
8074 case 'N':
8075 case 'n':
8076 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008077 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080 /*
8081 * "gj" and "gk" two new funny movement keys -- up and down
8082 * movement based on *screen* line rather than *file* line.
8083 */
8084 case 'j':
8085 case K_DOWN:
8086 /* with 'nowrap' it works just like the normal "j" command; also when
8087 * in a closed fold */
8088 if (!curwin->w_p_wrap
8089#ifdef FEAT_FOLDING
8090 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8091#endif
8092 )
8093 {
8094 oap->motion_type = MLINE;
8095 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8096 }
8097 else
8098 i = nv_screengo(oap, FORWARD, cap->count1);
8099 if (i == FAIL)
8100 clearopbeep(oap);
8101 break;
8102
8103 case 'k':
8104 case K_UP:
8105 /* with 'nowrap' it works just like the normal "k" command; also when
8106 * in a closed fold */
8107 if (!curwin->w_p_wrap
8108#ifdef FEAT_FOLDING
8109 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8110#endif
8111 )
8112 {
8113 oap->motion_type = MLINE;
8114 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8115 }
8116 else
8117 i = nv_screengo(oap, BACKWARD, cap->count1);
8118 if (i == FAIL)
8119 clearopbeep(oap);
8120 break;
8121
8122 /*
8123 * "gJ": join two lines without inserting a space.
8124 */
8125 case 'J':
8126 nv_join(cap);
8127 break;
8128
8129 /*
8130 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8131 * "gm": middle of "g0" and "g$".
8132 */
8133 case '^':
8134 flag = TRUE;
8135 /* FALLTHROUGH */
8136
8137 case '0':
8138 case 'm':
8139 case K_HOME:
8140 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 oap->motion_type = MCHAR;
8142 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008143 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008145 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 int width2 = width1 + curwin_col_off2();
8147
8148 validate_virtcol();
8149 i = 0;
8150 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8151 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8152 }
8153 else
8154 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008155 /* Go to the middle of the screen line. When 'number' or
8156 * 'relativenumber' is on and lines are wrapping the middle can be more
8157 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008159 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 + ((curwin->w_p_wrap && i > 0)
8161 ? curwin_col_off2() : 0)) / 2;
8162 coladvance((colnr_T)i);
8163 if (flag)
8164 {
8165 do
8166 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008167 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 }
8169 curwin->w_set_curswant = TRUE;
8170 break;
8171
8172 case '_':
8173 /* "g_": to the last non-blank character in the line or <count> lines
8174 * downward. */
8175 cap->oap->motion_type = MCHAR;
8176 cap->oap->inclusive = TRUE;
8177 curwin->w_curswant = MAXCOL;
8178 if (cursor_down((long)(cap->count1 - 1),
8179 cap->oap->op_type == OP_NOP) == FAIL)
8180 clearopbeep(cap->oap);
8181 else
8182 {
8183 char_u *ptr = ml_get_curline();
8184
8185 /* In Visual mode we may end up after the line. */
8186 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8187 --curwin->w_cursor.col;
8188
8189 /* Decrease the cursor column until it's on a non-blank. */
8190 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008191 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 --curwin->w_cursor.col;
8193 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008194 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 }
8196 break;
8197
8198 case '$':
8199 case K_END:
8200 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 {
8202 int col_off = curwin_col_off();
8203
8204 oap->motion_type = MCHAR;
8205 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008206 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {
8208 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8209 if (cap->count1 == 1)
8210 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008211 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 int width2 = width1 + curwin_col_off2();
8213
8214 validate_virtcol();
8215 i = width1 - 1;
8216 if (curwin->w_virtcol >= (colnr_T)width1)
8217 i += ((curwin->w_virtcol - width1) / width2 + 1)
8218 * width2;
8219 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008220
8221 /* Make sure we stick in this column. */
8222 validate_virtcol();
8223 curwin->w_curswant = curwin->w_virtcol;
8224 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8226 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8227 {
8228 /*
8229 * Check for landing on a character that got split at
8230 * the end of the line. We do not want to advance to
8231 * the next screen line.
8232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 if (curwin->w_virtcol > (colnr_T)i)
8234 --curwin->w_cursor.col;
8235 }
8236#endif
8237 }
8238 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8239 clearopbeep(oap);
8240 }
8241 else
8242 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008243 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008245
8246 /* Make sure we stick in this column. */
8247 validate_virtcol();
8248 curwin->w_curswant = curwin->w_virtcol;
8249 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 }
8251 }
8252 break;
8253
8254 /*
8255 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8256 */
8257 case '*':
8258 case '#':
8259#if POUND != '#'
8260 case POUND: /* pound sign (sometimes equal to '#') */
8261#endif
8262 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8263 case ']': /* :tselect for current identifier */
8264 nv_ident(cap);
8265 break;
8266
8267 /*
8268 * ge and gE: go back to end of word
8269 */
8270 case 'e':
8271 case 'E':
8272 oap->motion_type = MCHAR;
8273 curwin->w_set_curswant = TRUE;
8274 oap->inclusive = TRUE;
8275 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8276 clearopbeep(oap);
8277 break;
8278
8279 /*
8280 * "g CTRL-G": display info about cursor position
8281 */
8282 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008283 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 break;
8285
8286 /*
8287 * "gi": start Insert at the last position.
8288 */
8289 case 'i':
8290 if (curbuf->b_last_insert.lnum != 0)
8291 {
8292 curwin->w_cursor = curbuf->b_last_insert;
8293 check_cursor_lnum();
8294 i = (int)STRLEN(ml_get_curline());
8295 if (curwin->w_cursor.col > (colnr_T)i)
8296 {
8297#ifdef FEAT_VIRTUALEDIT
8298 if (virtual_active())
8299 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8300#endif
8301 curwin->w_cursor.col = i;
8302 }
8303 }
8304 cap->cmdchar = 'i';
8305 nv_edit(cap);
8306 break;
8307
8308 /*
8309 * "gI": Start insert in column 1.
8310 */
8311 case 'I':
8312 beginline(0);
8313 if (!checkclearopq(oap))
8314 invoke_edit(cap, FALSE, 'g', FALSE);
8315 break;
8316
8317#ifdef FEAT_SEARCHPATH
8318 /*
8319 * "gf": goto file, edit file under cursor
8320 * "]f" and "[f": can also be used.
8321 */
8322 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008323 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 nv_gotofile(cap);
8325 break;
8326#endif
8327
8328 /* "g'm" and "g`m": jump to mark without setting pcmark */
8329 case '\'':
8330 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008331 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 case '`':
8333 nv_gomark(cap);
8334 break;
8335
8336 /*
8337 * "gs": Goto sleep.
8338 */
8339 case 's':
8340 do_sleep(cap->count1 * 1000L);
8341 break;
8342
8343 /*
8344 * "ga": Display the ascii value of the character under the
8345 * cursor. It is displayed in decimal, hex, and octal. -- webb
8346 */
8347 case 'a':
8348 do_ascii(NULL);
8349 break;
8350
8351#ifdef FEAT_MBYTE
8352 /*
8353 * "g8": Display the bytes used for the UTF-8 character under the
8354 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008355 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 */
8357 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008358 if (cap->count0 == 8)
8359 utf_find_illegal();
8360 else
8361 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 break;
8363#endif
8364
Bram Moolenaar60402d62017-04-20 18:54:50 +02008365 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008366 case '<':
8367 show_sb_text();
8368 break;
8369
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 /*
8371 * "gg": Goto the first line in file. With a count it goes to
8372 * that line number like for "G". -- webb
8373 */
8374 case 'g':
8375 cap->arg = FALSE;
8376 nv_goto(cap);
8377 break;
8378
8379 /*
8380 * Two-character operators:
8381 * "gq" Format text
8382 * "gw" Format text and keep cursor position
8383 * "g~" Toggle the case of the text.
8384 * "gu" Change text to lower case.
8385 * "gU" Change text to upper case.
8386 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008387 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 */
8389 case 'q':
8390 case 'w':
8391 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008392 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 case '~':
8394 case 'u':
8395 case 'U':
8396 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008397 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 nv_operator(cap);
8399 break;
8400
8401 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008402 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 * current function
8404 * "gD": idem, but in the current file.
8405 */
8406 case 'd':
8407 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008408 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 break;
8410
8411#ifdef FEAT_MOUSE
8412 /*
8413 * g<*Mouse> : <C-*mouse>
8414 */
8415 case K_MIDDLEMOUSE:
8416 case K_MIDDLEDRAG:
8417 case K_MIDDLERELEASE:
8418 case K_LEFTMOUSE:
8419 case K_LEFTDRAG:
8420 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008421 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 case K_RIGHTMOUSE:
8423 case K_RIGHTDRAG:
8424 case K_RIGHTRELEASE:
8425 case K_X1MOUSE:
8426 case K_X1DRAG:
8427 case K_X1RELEASE:
8428 case K_X2MOUSE:
8429 case K_X2DRAG:
8430 case K_X2RELEASE:
8431 mod_mask = MOD_MASK_CTRL;
8432 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8433 break;
8434#endif
8435
8436 case K_IGNORE:
8437 break;
8438
8439 /*
8440 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8441 */
8442 case 'p':
8443 case 'P':
8444 nv_put(cap);
8445 break;
8446
8447#ifdef FEAT_BYTEOFF
8448 /* "go": goto byte count from start of buffer */
8449 case 'o':
8450 goto_byte(cap->count0);
8451 break;
8452#endif
8453
8454 /* "gQ": improved Ex mode */
8455 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008456 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
8458 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008459 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 break;
8461 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 if (!checkclearopq(oap))
8464 do_exmode(TRUE);
8465 break;
8466
8467#ifdef FEAT_JUMPLIST
8468 case ',':
8469 nv_pcmark(cap);
8470 break;
8471
8472 case ';':
8473 cap->count1 = -cap->count1;
8474 nv_pcmark(cap);
8475 break;
8476#endif
8477
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008478 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008479 if (!checkclearop(oap))
8480 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008481 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008482 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008483 if (!checkclearop(oap))
8484 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008485 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008486
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008487 case '+':
8488 case '-': /* "g+" and "g-": undo or redo along the timeline */
8489 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008490 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008491 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008492 break;
8493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 default:
8495 clearopbeep(oap);
8496 break;
8497 }
8498}
8499
8500/*
8501 * Handle "o" and "O" commands.
8502 */
8503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008504n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008506#ifdef FEAT_CONCEAL
8507 linenr_T oldline = curwin->w_cursor.lnum;
8508#endif
8509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 if (!checkclearopq(cap->oap))
8511 {
8512#ifdef FEAT_FOLDING
8513 if (cap->cmdchar == 'O')
8514 /* Open above the first line of a folded sequence of lines */
8515 (void)hasFolding(curwin->w_cursor.lnum,
8516 &curwin->w_cursor.lnum, NULL);
8517 else
8518 /* Open below the last line of a folded sequence of lines */
8519 (void)hasFolding(curwin->w_cursor.lnum,
8520 NULL, &curwin->w_cursor.lnum);
8521#endif
8522 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8523 (cap->cmdchar == 'O' ? 1 : 0)),
8524 (linenr_T)(curwin->w_cursor.lnum +
8525 (cap->cmdchar == 'o' ? 1 : 0))
8526 ) == OK
8527 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8528#ifdef FEAT_COMMENTS
8529 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8530#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02008531 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008533#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008534 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008535 update_single_line(curwin, oldline);
8536#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008537 /* When '#' is in 'cpoptions' ignore the count. */
8538 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8539 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008540#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008541 if (curwin->w_p_cul)
8542 /* force redraw of cursorline */
8543 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8546 }
8547 }
8548}
8549
8550/*
8551 * "." command: redo last change.
8552 */
8553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008554nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 if (!checkclearopq(cap->oap))
8557 {
8558 /*
8559 * If "restart_edit" is TRUE, the last but one command is repeated
8560 * instead of the last command (inserting text). This is used for
8561 * CTRL-O <.> in insert mode.
8562 */
8563 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8564 clearopbeep(cap->oap);
8565 }
8566}
8567
8568/*
8569 * CTRL-R: undo undo
8570 */
8571 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008572nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 if (!checkclearopq(cap->oap))
8575 {
8576 u_redo((int)cap->count1);
8577 curwin->w_set_curswant = TRUE;
8578 }
8579}
8580
8581/*
8582 * Handle "U" command.
8583 */
8584 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008585nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
8587 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008588 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 {
8590 /* translate "gUU" to "gUgU" */
8591 cap->cmdchar = 'g';
8592 cap->nchar = 'U';
8593 nv_operator(cap);
8594 }
8595 else if (!checkclearopq(cap->oap))
8596 {
8597 u_undoline();
8598 curwin->w_set_curswant = TRUE;
8599 }
8600}
8601
8602/*
8603 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8604 * single character.
8605 */
8606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008607nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008609 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02008610 {
8611#ifdef FEAT_JOB_CHANNEL
8612 if (bt_prompt(curbuf) && !prompt_curpos_editable())
8613 {
8614 clearopbeep(cap->oap);
8615 return;
8616 }
8617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 else
8621 nv_operator(cap);
8622}
8623
8624/*
8625 * Handle an operator command.
8626 * The actual work is done by do_pending_operator().
8627 */
8628 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008629nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 int op_type;
8632
8633 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008634#ifdef FEAT_JOB_CHANNEL
8635 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
8636 {
8637 clearopbeep(cap->oap);
8638 return;
8639 }
8640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
8642 if (op_type == cap->oap->op_type) /* double operator works on lines */
8643 nv_lineop(cap);
8644 else if (!checkclearop(cap->oap))
8645 {
8646 cap->oap->start = curwin->w_cursor;
8647 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008648#ifdef FEAT_EVAL
8649 set_op_var(op_type);
8650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 }
8652}
8653
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008654#ifdef FEAT_EVAL
8655/*
8656 * Set v:operator to the characters for "optype".
8657 */
8658 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008659set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008660{
8661 char_u opchars[3];
8662
8663 if (optype == OP_NOP)
8664 set_vim_var_string(VV_OP, NULL, 0);
8665 else
8666 {
8667 opchars[0] = get_op_char(optype);
8668 opchars[1] = get_extra_op_char(optype);
8669 opchars[2] = NUL;
8670 set_vim_var_string(VV_OP, opchars, -1);
8671 }
8672}
8673#endif
8674
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675/*
8676 * Handle linewise operator "dd", "yy", etc.
8677 *
8678 * "_" is is a strange motion command that helps make operators more logical.
8679 * It is actually implemented, but not documented in the real Vi. This motion
8680 * command actually refers to "the current line". Commands like "dd" and "yy"
8681 * are really an alternate form of "d_" and "y_". It does accept a count, so
8682 * "d3_" works to delete 3 lines.
8683 */
8684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008685nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686{
8687 cap->oap->motion_type = MLINE;
8688 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8689 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008690 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8691 && cap->oap->motion_force != 'v'
8692 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 || cap->oap->op_type == OP_LSHIFT
8694 || cap->oap->op_type == OP_RSHIFT)
8695 beginline(BL_SOL | BL_FIX);
8696 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8697 beginline(BL_WHITE | BL_FIX);
8698}
8699
8700/*
8701 * <Home> command.
8702 */
8703 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008704nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008706 /* CTRL-HOME is like "gg" */
8707 if (mod_mask & MOD_MASK_CTRL)
8708 nv_goto(cap);
8709 else
8710 {
8711 cap->count0 = 1;
8712 nv_pipe(cap);
8713 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008714 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8715 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716}
8717
8718/*
8719 * "|" command.
8720 */
8721 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008722nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 cap->oap->motion_type = MCHAR;
8725 cap->oap->inclusive = FALSE;
8726 beginline(0);
8727 if (cap->count0 > 0)
8728 {
8729 coladvance((colnr_T)(cap->count0 - 1));
8730 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8731 }
8732 else
8733 curwin->w_curswant = 0;
8734 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008735 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 curwin->w_set_curswant = FALSE;
8737}
8738
8739/*
8740 * Handle back-word command "b" and "B".
8741 * cap->arg is 1 for "B"
8742 */
8743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008744nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
8746 cap->oap->motion_type = MCHAR;
8747 cap->oap->inclusive = FALSE;
8748 curwin->w_set_curswant = TRUE;
8749 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8750 clearopbeep(cap->oap);
8751#ifdef FEAT_FOLDING
8752 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8753 foldOpenCursor();
8754#endif
8755}
8756
8757/*
8758 * Handle word motion commands "e", "E", "w" and "W".
8759 * cap->arg is TRUE for "E" and "W".
8760 */
8761 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008762nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763{
8764 int n;
8765 int word_end;
8766 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008767 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
8769 /*
8770 * Set inclusive for the "E" and "e" command.
8771 */
8772 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8773 word_end = TRUE;
8774 else
8775 word_end = FALSE;
8776 cap->oap->inclusive = word_end;
8777
8778 /*
8779 * "cw" and "cW" are a special case.
8780 */
8781 if (!word_end && cap->oap->op_type == OP_CHANGE)
8782 {
8783 n = gchar_cursor();
8784 if (n != NUL) /* not an empty line */
8785 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008786 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {
8788 /*
8789 * Reproduce a funny Vi behaviour: "cw" on a blank only
8790 * changes one character, not all blanks until the start of
8791 * the next word. Only do this when the 'w' flag is included
8792 * in 'cpoptions'.
8793 */
8794 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8795 {
8796 cap->oap->inclusive = TRUE;
8797 cap->oap->motion_type = MCHAR;
8798 return;
8799 }
8800 }
8801 else
8802 {
8803 /*
8804 * This is a little strange. To match what the real Vi does,
8805 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8806 * that we are not on a space or a TAB. This seems impolite
8807 * at first, but it's really more what we mean when we say
8808 * 'cw'.
8809 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008810 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 * will change only one character! This is done by setting
8812 * flag.
8813 */
8814 cap->oap->inclusive = TRUE;
8815 word_end = TRUE;
8816 flag = TRUE;
8817 }
8818 }
8819 }
8820
8821 cap->oap->motion_type = MCHAR;
8822 curwin->w_set_curswant = TRUE;
8823 if (word_end)
8824 n = end_word(cap->count1, cap->arg, flag, FALSE);
8825 else
8826 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8827
Bram Moolenaardfefb982008-04-01 10:06:39 +00008828 /* Don't leave the cursor on the NUL past the end of line. Unless we
8829 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008830 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008831 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833 if (n == FAIL && cap->oap->op_type == OP_NOP)
8834 clearopbeep(cap->oap);
8835 else
8836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838#ifdef FEAT_FOLDING
8839 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8840 foldOpenCursor();
8841#endif
8842 }
8843}
8844
8845/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008846 * Used after a movement command: If the cursor ends up on the NUL after the
8847 * end of the line, may move it back to the last character and make the motion
8848 * inclusive.
8849 */
8850 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008851adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008852{
8853 /* The cursor cannot remain on the NUL when:
8854 * - the column is > 0
8855 * - not in Visual mode or 'selection' is "o"
8856 * - 'virtualedit' is not "all" and not "onemore".
8857 */
8858 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008859 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008860#ifdef FEAT_VIRTUALEDIT
8861 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8862#endif
8863 )
8864 {
8865 --curwin->w_cursor.col;
8866#ifdef FEAT_MBYTE
8867 /* prevent cursor from moving on the trail byte */
8868 if (has_mbyte)
8869 mb_adjust_cursor();
8870#endif
8871 oap->inclusive = TRUE;
8872 }
8873}
8874
8875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 * "0" and "^" commands.
8877 * cap->arg is the argument for beginline().
8878 */
8879 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008880nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882 cap->oap->motion_type = MCHAR;
8883 cap->oap->inclusive = FALSE;
8884 beginline(cap->arg);
8885#ifdef FEAT_FOLDING
8886 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8887 foldOpenCursor();
8888#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008889 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8890 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891}
8892
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893/*
8894 * In exclusive Visual mode, may include the last character.
8895 */
8896 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008897adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898{
8899 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008900 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008902#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 if (has_mbyte)
8904 inc_cursor();
8905 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 ++curwin->w_cursor.col;
8908 cap->oap->inclusive = FALSE;
8909 }
8910}
8911
8912/*
8913 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8914 * Should check VIsual_mode before calling this.
8915 * Returns TRUE when backed up to the previous line.
8916 */
8917 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008918unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
8920 pos_T *pp;
8921
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008922 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008924 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 pp = &curwin->w_cursor;
8926 else
8927 pp = &VIsual;
8928#ifdef FEAT_VIRTUALEDIT
8929 if (pp->coladd > 0)
8930 --pp->coladd;
8931 else
8932#endif
8933 if (pp->col > 0)
8934 {
8935 --pp->col;
8936#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008937 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938#endif
8939 }
8940 else if (pp->lnum > 1)
8941 {
8942 --pp->lnum;
8943 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8944 return TRUE;
8945 }
8946 }
8947 return FALSE;
8948}
8949
8950/*
8951 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8952 */
8953 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008954nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956 if (VIsual_active)
8957 VIsual_select = TRUE;
8958 else if (VIsual_reselect)
8959 {
8960 cap->nchar = 'v'; /* fake "gv" command */
8961 cap->arg = TRUE;
8962 nv_g_cmd(cap);
8963 }
8964}
8965
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966
8967/*
8968 * "G", "gg", CTRL-END, CTRL-HOME.
8969 * cap->arg is TRUE for "G".
8970 */
8971 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008972nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
8974 linenr_T lnum;
8975
8976 if (cap->arg)
8977 lnum = curbuf->b_ml.ml_line_count;
8978 else
8979 lnum = 1L;
8980 cap->oap->motion_type = MLINE;
8981 setpcmark();
8982
8983 /* When a count is given, use it instead of the default lnum */
8984 if (cap->count0 != 0)
8985 lnum = cap->count0;
8986 if (lnum < 1L)
8987 lnum = 1L;
8988 else if (lnum > curbuf->b_ml.ml_line_count)
8989 lnum = curbuf->b_ml.ml_line_count;
8990 curwin->w_cursor.lnum = lnum;
8991 beginline(BL_SOL | BL_FIX);
8992#ifdef FEAT_FOLDING
8993 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8994 foldOpenCursor();
8995#endif
8996}
8997
8998/*
8999 * CTRL-\ in Normal mode.
9000 */
9001 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009002nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
9005 {
9006 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00009007 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 clear_cmdline = TRUE; /* unshow mode later */
9009 restart_edit = 0;
9010#ifdef FEAT_CMDWIN
9011 if (cmdwin_type != 0)
9012 cmdwin_result = Ctrl_C;
9013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 if (VIsual_active)
9015 {
9016 end_visual_mode(); /* stop Visual */
9017 redraw_curbuf_later(INVERTED);
9018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
9020 if (cap->nchar == Ctrl_G && p_im)
9021 restart_edit = 'a';
9022 }
9023 else
9024 clearopbeep(cap->oap);
9025}
9026
9027/*
9028 * ESC in Normal mode: beep, but don't flush buffers.
9029 * Don't even beep if we are canceling a command.
9030 */
9031 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009032nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
9034 int no_reason;
9035
9036 no_reason = (cap->oap->op_type == OP_NOP
9037 && cap->opcount == 0
9038 && cap->count0 == 0
9039 && cap->oap->regname == 0
9040 && !p_im);
9041
9042 if (cap->arg) /* TRUE for CTRL-C */
9043 {
9044 if (restart_edit == 0
9045#ifdef FEAT_CMDWIN
9046 && cmdwin_type == 0
9047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02009050 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
9052 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9053 * set again below when halfway a mapping. */
9054 if (!p_im)
9055 restart_edit = 0;
9056#ifdef FEAT_CMDWIN
9057 if (cmdwin_type != 0)
9058 {
9059 cmdwin_result = K_IGNORE;
9060 got_int = FALSE; /* don't stop executing autocommands et al. */
9061 return;
9062 }
9063#endif
9064 }
9065
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 if (VIsual_active)
9067 {
9068 end_visual_mode(); /* stop Visual */
9069 check_cursor_col(); /* make sure cursor is not beyond EOL */
9070 curwin->w_set_curswant = TRUE;
9071 redraw_curbuf_later(INVERTED);
9072 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009073 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009074 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 clearop(cap->oap);
9076
9077 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9078 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009079 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 restart_edit = 'a';
9081}
9082
9083/*
9084 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009085 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 */
9087 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009088nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
9090 /* <Insert> is equal to "i" */
9091 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9092 cap->cmdchar = 'i';
9093
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 /* in Visual mode "A" and "I" are an operator */
9095 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02009096 {
9097#ifdef FEAT_TERMINAL
9098 if (term_in_normal_mode())
9099 {
9100 end_visual_mode();
9101 clearop(cap->oap);
9102 term_enter_job_mode();
9103 return;
9104 }
9105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02009107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108
9109 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009110 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9111 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 {
9113#ifdef FEAT_TEXTOBJ
9114 nv_object(cap);
9115#else
9116 clearopbeep(cap->oap);
9117#endif
9118 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009119#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009120 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009121 {
9122 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009123 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009124 return;
9125 }
9126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 else if (!curbuf->b_p_ma && !p_im)
9128 {
9129 /* Only give this error when 'insertmode' is off. */
9130 EMSG(_(e_modifiable));
9131 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009132 if (cap->cmdchar == K_PS)
9133 /* drop the pasted text */
9134 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009136 else if (cap->cmdchar == K_PS && VIsual_active)
9137 {
9138 pos_T old_pos = curwin->w_cursor;
9139 pos_T old_visual = VIsual;
9140
9141 /* In Visual mode the selected text is deleted. */
9142 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9143 {
9144 shift_delete_registers();
9145 cap->oap->regname = '1';
9146 }
9147 else
9148 cap->oap->regname = '-';
9149 cap->cmdchar = 'd';
9150 cap->nchar = NUL;
9151 nv_operator(cap);
9152 do_pending_operator(cap, 0, FALSE);
9153 cap->cmdchar = K_PS;
9154
9155 /* When the last char in the line was deleted then append. Detect this
9156 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009157 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9158 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009159 inc_cursor();
9160
9161 /* Insert to replace the deleted text with the pasted text. */
9162 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 else if (!checkclearopq(cap->oap))
9165 {
9166 switch (cap->cmdchar)
9167 {
9168 case 'A': /* "A"ppend after the line */
9169 curwin->w_set_curswant = TRUE;
9170#ifdef FEAT_VIRTUALEDIT
9171 if (ve_flags == VE_ALL)
9172 {
9173 int save_State = State;
9174
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009175 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 * character past the end of the line */
9177 State = INSERT;
9178 coladvance((colnr_T)MAXCOL);
9179 State = save_State;
9180 }
9181 else
9182#endif
9183 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9184 break;
9185
9186 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009187 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9188 beginline(BL_WHITE);
9189 else
9190 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 break;
9192
Bram Moolenaara1891842017-02-04 21:34:31 +01009193 case K_PS:
9194 /* Bracketed paste works like "a"ppend, unless the cursor is in
9195 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009196 if (curwin->w_cursor.col == 0)
9197 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009198 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009199
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9201#ifdef FEAT_VIRTUALEDIT
9202 /* increment coladd when in virtual space, increment the
9203 * column otherwise, also to append after an unprintable char */
9204 if (virtual_active()
9205 && (curwin->w_cursor.coladd > 0
9206 || *ml_get_cursor() == NUL
9207 || *ml_get_cursor() == TAB))
9208 curwin->w_cursor.coladd++;
9209 else
9210#endif
9211 if (*ml_get_cursor() != NUL)
9212 inc_cursor();
9213 break;
9214 }
9215
9216#ifdef FEAT_VIRTUALEDIT
9217 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9218 {
9219 int save_State = State;
9220
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009221 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 * character past the end of the line */
9223 State = INSERT;
9224 coladvance(getviscol());
9225 State = save_State;
9226 }
9227#endif
9228
9229 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9230 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009231 else if (cap->cmdchar == K_PS)
9232 /* drop the pasted text */
9233 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234}
9235
9236/*
9237 * Invoke edit() and take care of "restart_edit" and the return value.
9238 */
9239 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009240invoke_edit(
9241 cmdarg_T *cap,
9242 int repl, /* "r" or "gr" command */
9243 int cmd,
9244 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245{
9246 int restart_edit_save = 0;
9247
9248 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9249 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9250 * it. */
9251 if (repl || !stuff_empty())
9252 restart_edit_save = restart_edit;
9253 else
9254 restart_edit_save = 0;
9255
9256 /* Always reset "restart_edit", this is not a restarted edit. */
9257 restart_edit = 0;
9258
9259 if (edit(cmd, startln, cap->count1))
9260 cap->retval |= CA_COMMAND_BUSY;
9261
9262 if (restart_edit == 0)
9263 restart_edit = restart_edit_save;
9264}
9265
9266#ifdef FEAT_TEXTOBJ
9267/*
9268 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9269 */
9270 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009271nv_object(
9272 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273{
9274 int flag;
9275 int include;
9276 char_u *mps_save;
9277
9278 if (cap->cmdchar == 'i')
9279 include = FALSE; /* "ix" = inner object: exclude white space */
9280 else
9281 include = TRUE; /* "ax" = an object: include white space */
9282
9283 /* Make sure (), [], {} and <> are in 'matchpairs' */
9284 mps_save = curbuf->b_p_mps;
9285 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9286
9287 switch (cap->nchar)
9288 {
9289 case 'w': /* "aw" = a word */
9290 flag = current_word(cap->oap, cap->count1, include, FALSE);
9291 break;
9292 case 'W': /* "aW" = a WORD */
9293 flag = current_word(cap->oap, cap->count1, include, TRUE);
9294 break;
9295 case 'b': /* "ab" = a braces block */
9296 case '(':
9297 case ')':
9298 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9299 break;
9300 case 'B': /* "aB" = a Brackets block */
9301 case '{':
9302 case '}':
9303 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9304 break;
9305 case '[': /* "a[" = a [] block */
9306 case ']':
9307 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9308 break;
9309 case '<': /* "a<" = a <> block */
9310 case '>':
9311 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9312 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009313 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009314 /* Do not adjust oap->end in do_pending_operator()
9315 * otherwise there are different results for 'dit'
9316 * (note leading whitespace in last line):
9317 * 1) <b> 2) <b>
9318 * foobar foobar
9319 * </b> </b>
9320 */
9321 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009322 flag = current_tagblock(cap->oap, cap->count1, include);
9323 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 case 'p': /* "ap" = a paragraph */
9325 flag = current_par(cap->oap, cap->count1, include, 'p');
9326 break;
9327 case 's': /* "as" = a sentence */
9328 flag = current_sent(cap->oap, cap->count1, include);
9329 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009330 case '"': /* "a"" = a double quoted string */
9331 case '\'': /* "a'" = a single quoted string */
9332 case '`': /* "a`" = a backtick quoted string */
9333 flag = current_quote(cap->oap, cap->count1, include,
9334 cap->nchar);
9335 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336#if 0 /* TODO */
9337 case 'S': /* "aS" = a section */
9338 case 'f': /* "af" = a filename */
9339 case 'u': /* "au" = a URL */
9340#endif
9341 default:
9342 flag = FAIL;
9343 break;
9344 }
9345
9346 curbuf->b_p_mps = mps_save;
9347 if (flag == FAIL)
9348 clearopbeep(cap->oap);
9349 adjust_cursor_col();
9350 curwin->w_set_curswant = TRUE;
9351}
9352#endif
9353
9354/*
9355 * "q" command: Start/stop recording.
9356 * "q:", "q/", "q?": edit command-line in command-line window.
9357 */
9358 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009359nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360{
9361 if (cap->oap->op_type == OP_FORMAT)
9362 {
9363 /* "gqq" is the same as "gqgq": format line */
9364 cap->cmdchar = 'g';
9365 cap->nchar = 'q';
9366 nv_operator(cap);
9367 }
9368 else if (!checkclearop(cap->oap))
9369 {
9370#ifdef FEAT_CMDWIN
9371 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9372 {
9373 stuffcharReadbuff(cap->nchar);
9374 stuffcharReadbuff(K_CMDWIN);
9375 }
9376 else
9377#endif
9378 /* (stop) recording into a named register, unless executing a
9379 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009380 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 clearopbeep(cap->oap);
9382 }
9383}
9384
9385/*
9386 * Handle the "@r" command.
9387 */
9388 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009389nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
9391 if (checkclearop(cap->oap))
9392 return;
9393#ifdef FEAT_EVAL
9394 if (cap->nchar == '=')
9395 {
9396 if (get_expr_register() == NUL)
9397 return;
9398 }
9399#endif
9400 while (cap->count1-- && !got_int)
9401 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009402 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 {
9404 clearopbeep(cap->oap);
9405 break;
9406 }
9407 line_breakcheck();
9408 }
9409}
9410
9411/*
9412 * Handle the CTRL-U and CTRL-D commands.
9413 */
9414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009415nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416{
9417 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9418 || (cap->cmdchar == Ctrl_D
9419 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9420 clearopbeep(cap->oap);
9421 else if (!checkclearop(cap->oap))
9422 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9423}
9424
9425/*
9426 * Handle "J" or "gJ" command.
9427 */
9428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009429nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 if (VIsual_active) /* join the visual lines */
9432 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009433 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 {
9435 if (cap->count0 <= 1)
9436 cap->count0 = 2; /* default for join is two lines! */
9437 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9438 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009440 /* can't join when on the last line */
9441 if (cap->count0 <= 2)
9442 {
9443 clearopbeep(cap->oap);
9444 return;
9445 }
9446 cap->count0 = curbuf->b_ml.ml_line_count
9447 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009449
9450 prep_redo(cap->oap->regname, cap->count0,
9451 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9452 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 }
9454}
9455
9456/*
9457 * "P", "gP", "p" and "gp" commands.
9458 */
9459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009460nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 int regname = 0;
9463 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009464 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009465 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 int dir;
9467 int flags = 0;
9468
9469 if (cap->oap->op_type != OP_NOP)
9470 {
9471#ifdef FEAT_DIFF
9472 /* "dp" is ":diffput" */
9473 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9474 {
9475 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009476 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 }
9478 else
9479#endif
9480 clearopbeep(cap->oap);
9481 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02009482#ifdef FEAT_JOB_CHANNEL
9483 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
9484 {
9485 clearopbeep(cap->oap);
9486 }
9487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 else
9489 {
9490 dir = (cap->cmdchar == 'P'
9491 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9492 ? BACKWARD : FORWARD;
9493 prep_redo_cmd(cap);
9494 if (cap->cmdchar == 'g')
9495 flags |= PUT_CURSEND;
9496
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 if (VIsual_active)
9498 {
9499 /* Putting in Visual mode: The put text replaces the selected
9500 * text. First delete the selected text, then put the new text.
9501 * Need to save and restore the registers that the delete
9502 * overwrites if the old contents is being put.
9503 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009504 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009506#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009508#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009509 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009510 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009511#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514
9515 )
9516 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009517 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 * put, save it first. */
9519 reg1 = get_register(regname, TRUE);
9520 }
9521
9522 /* Now delete the selected text. */
9523 cap->cmdchar = 'd';
9524 cap->nchar = NUL;
9525 cap->oap->regname = NUL;
9526 nv_operator(cap);
9527 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009528 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529
9530 /* delete PUT_LINE_BACKWARD; */
9531 cap->oap->regname = regname;
9532
9533 if (reg1 != NULL)
9534 {
9535 /* Delete probably changed the register we want to put, save
9536 * it first. Then put back what was there before the delete. */
9537 reg2 = get_register(regname, FALSE);
9538 put_register(regname, reg1);
9539 }
9540
9541 /* When deleted a linewise Visual area, put the register as
9542 * lines to avoid it joined with the next line. When deletion was
9543 * characterwise, split a line when putting lines. */
9544 if (VIsual_mode == 'V')
9545 flags |= PUT_LINE;
9546 else if (VIsual_mode == 'v')
9547 flags |= PUT_LINE_SPLIT;
9548 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9549 flags |= PUT_LINE_FORWARD;
9550 dir = BACKWARD;
9551 if ((VIsual_mode != 'V'
9552 && curwin->w_cursor.col < curbuf->b_op_start.col)
9553 || (VIsual_mode == 'V'
9554 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9555 /* cursor is at the end of the line or end of file, put
9556 * forward. */
9557 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009558 /* May have been reset in do_put(). */
9559 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 do_put(cap->oap->regname, dir, cap->count1, flags);
9562
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 /* If a register was saved, put it back now. */
9564 if (reg2 != NULL)
9565 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009566
9567 /* What to reselect with "gv"? Selecting the just put text seems to
9568 * be the most useful, since the original text was removed. */
9569 if (was_visual)
9570 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009571 curbuf->b_visual.vi_start = curbuf->b_op_start;
9572 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009573 /* need to adjust cursor position */
9574 if (*p_sel == 'e')
9575 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009576 }
9577
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009578 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009579 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009580 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009581 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009582 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009583
9584 /* If the cursor was in that line, move it to the end of the last
9585 * line. */
9586 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9587 {
9588 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9589 coladvance((colnr_T)MAXCOL);
9590 }
9591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592 auto_format(FALSE, TRUE);
9593 }
9594}
9595
9596/*
9597 * "o" and "O" commands.
9598 */
9599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009600nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602#ifdef FEAT_DIFF
9603 /* "do" is ":diffget" */
9604 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9605 {
9606 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009607 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 }
9609 else
9610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 if (VIsual_active) /* switch start and end of visual */
9612 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009613#ifdef FEAT_JOB_CHANNEL
9614 else if (bt_prompt(curbuf))
9615 {
9616 clearopbeep(cap->oap);
9617 }
9618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 n_opencmd(cap);
9621}
9622
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623#ifdef FEAT_NETBEANS_INTG
9624 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009625nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626{
9627 netbeans_keycommand(cap->nchar);
9628}
9629#endif
9630
9631#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009633nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9636}
9637#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009638
Bram Moolenaar3918c952005-03-15 22:34:55 +00009639/*
9640 * Trigger CursorHold event.
9641 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9642 * input buffer. "did_cursorhold" is set to avoid retriggering.
9643 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009645nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009646{
9647 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9648 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009649 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009650}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009651
9652/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009653 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009654 */
9655 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009656get_op_vcol(
9657 oparg_T *oap,
9658 colnr_T redo_VIsual_vcol,
9659 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009660{
9661 colnr_T start, end;
9662
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009663 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009664 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009665 return;
9666
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009667 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009668
9669#ifdef FEAT_MBYTE
9670 /* prevent from moving onto a trail byte */
9671 if (has_mbyte)
9672 mb_adjustpos(curwin->w_buffer, &oap->end);
9673#endif
9674
9675 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009676
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009677 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009678 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009679 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9680
9681 if (start < oap->start_vcol)
9682 oap->start_vcol = start;
9683 if (end > oap->end_vcol)
9684 {
9685 if (initial && *p_sel == 'e' && start >= 1
9686 && start - 1 >= oap->end_vcol)
9687 oap->end_vcol = start - 1;
9688 else
9689 oap->end_vcol = end;
9690 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009691 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009692
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009693 /* if '$' was used, get oap->end_vcol from longest line */
9694 if (curwin->w_curswant == MAXCOL)
9695 {
9696 curwin->w_cursor.col = MAXCOL;
9697 oap->end_vcol = 0;
9698 for (curwin->w_cursor.lnum = oap->start.lnum;
9699 curwin->w_cursor.lnum <= oap->end.lnum;
9700 ++curwin->w_cursor.lnum)
9701 {
9702 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9703 if (end > oap->end_vcol)
9704 oap->end_vcol = end;
9705 }
9706 }
9707 else if (redo_VIsual_busy)
9708 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9709 /*
9710 * Correct oap->end.col and oap->start.col to be the
9711 * upper-left and lower-right corner of the block area.
9712 *
9713 * (Actually, this does convert column positions into character
9714 * positions)
9715 */
9716 curwin->w_cursor.lnum = oap->end.lnum;
9717 coladvance(oap->end_vcol);
9718 oap->end = curwin->w_cursor;
9719
9720 curwin->w_cursor = oap->start;
9721 coladvance(oap->start_vcol);
9722 oap->start = curwin->w_cursor;
9723}