blob: 79e0b68aa8f8f407a2ea37dce4115de720337238 [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 Moolenaar3918c952005-03-15 22:34:55 +0000172#ifdef FEAT_AUTOCMD
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100173static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar3918c952005-03-15 22:34:55 +0000174#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100175static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000177static char *e_noident = N_("E349: No identifier under cursor");
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Function to be called for a Normal or Visual mode command.
181 * The argument is a cmdarg_T.
182 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100183typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185/* Values for cmd_flags. */
186#define NV_NCH 0x01 /* may need to get a second char */
187#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
188#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
189#define NV_LANG 0x08 /* second char needs language adjustment */
190
191#define NV_SS 0x10 /* may start selection */
192#define NV_SSS 0x20 /* may start selection with shift modifier */
193#define NV_STS 0x40 /* may stop selection without shift modif. */
194#define NV_RL 0x80 /* 'rightleft' modifies command */
195#define NV_KEEPREG 0x100 /* don't clear regname */
196#define NV_NCW 0x200 /* not allowed in command-line window */
197
198/*
199 * Generally speaking, every Normal mode command should either clear any
200 * pending operator (with *clearop*()), or set the motion type variable
201 * oap->motion_type.
202 *
203 * When a cursor motion command is made, it is marked as being a character or
204 * line oriented motion. Then, if an operator is in effect, the operation
205 * becomes character or line oriented accordingly.
206 */
207
208/*
209 * This table contains one entry for every Normal or Visual mode command.
210 * The order doesn't matter, init_normal_cmds() will create a sorted index.
211 * It is faster when all keys from zero to '~' are present.
212 */
213static const struct nv_cmd
214{
215 int cmd_char; /* (first) command character */
216 nv_func_T cmd_func; /* function for this command */
217 short_u cmd_flags; /* NV_ flags */
218 short cmd_arg; /* value for ca.arg */
219} nv_cmds[] =
220{
221 {NUL, nv_error, 0, 0},
222 {Ctrl_A, nv_addsub, 0, 0},
223 {Ctrl_B, nv_page, NV_STS, BACKWARD},
224 {Ctrl_C, nv_esc, 0, TRUE},
225 {Ctrl_D, nv_halfpage, 0, 0},
226 {Ctrl_E, nv_scroll_line, 0, TRUE},
227 {Ctrl_F, nv_page, NV_STS, FORWARD},
228 {Ctrl_G, nv_ctrlg, 0, 0},
229 {Ctrl_H, nv_ctrlh, 0, 0},
230 {Ctrl_I, nv_pcmark, 0, 0},
231 {NL, nv_down, 0, FALSE},
232 {Ctrl_K, nv_error, 0, 0},
233 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000234 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {Ctrl_N, nv_down, NV_STS, FALSE},
236 {Ctrl_O, nv_ctrlo, 0, 0},
237 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000238 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {Ctrl_R, nv_redo, 0, 0},
240 {Ctrl_S, nv_ignore, 0, 0},
241 {Ctrl_T, nv_tagpop, NV_NCW, 0},
242 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {Ctrl_V, nv_visual, 0, FALSE},
244 {'V', nv_visual, 0, FALSE},
245 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {Ctrl_W, nv_window, 0, 0},
247 {Ctrl_X, nv_addsub, 0, 0},
248 {Ctrl_Y, nv_scroll_line, 0, FALSE},
249 {Ctrl_Z, nv_suspend, 0, 0},
250 {ESC, nv_esc, 0, FALSE},
251 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
252 {Ctrl_RSB, nv_ident, NV_NCW, 0},
253 {Ctrl_HAT, nv_hat, NV_NCW, 0},
254 {Ctrl__, nv_error, 0, 0},
255 {' ', nv_right, 0, 0},
256 {'!', nv_operator, 0, 0},
257 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
258 {'#', nv_ident, 0, 0},
259 {'$', nv_dollar, 0, 0},
260 {'%', nv_percent, 0, 0},
261 {'&', nv_optrans, 0, 0},
262 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
263 {'(', nv_brace, 0, BACKWARD},
264 {')', nv_brace, 0, FORWARD},
265 {'*', nv_ident, 0, 0},
266 {'+', nv_down, 0, TRUE},
267 {',', nv_csearch, 0, TRUE},
268 {'-', nv_up, 0, TRUE},
269 {'.', nv_dot, NV_KEEPREG, 0},
270 {'/', nv_search, 0, FALSE},
271 {'0', nv_beginline, 0, 0},
272 {'1', nv_ignore, 0, 0},
273 {'2', nv_ignore, 0, 0},
274 {'3', nv_ignore, 0, 0},
275 {'4', nv_ignore, 0, 0},
276 {'5', nv_ignore, 0, 0},
277 {'6', nv_ignore, 0, 0},
278 {'7', nv_ignore, 0, 0},
279 {'8', nv_ignore, 0, 0},
280 {'9', nv_ignore, 0, 0},
281 {':', nv_colon, 0, 0},
282 {';', nv_csearch, 0, FALSE},
283 {'<', nv_operator, NV_RL, 0},
284 {'=', nv_operator, 0, 0},
285 {'>', nv_operator, NV_RL, 0},
286 {'?', nv_search, 0, FALSE},
287 {'@', nv_at, NV_NCH_NOP, FALSE},
288 {'A', nv_edit, 0, 0},
289 {'B', nv_bck_word, 0, 1},
290 {'C', nv_abbrev, NV_KEEPREG, 0},
291 {'D', nv_abbrev, NV_KEEPREG, 0},
292 {'E', nv_wordcmd, 0, TRUE},
293 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
294 {'G', nv_goto, 0, TRUE},
295 {'H', nv_scroll, 0, 0},
296 {'I', nv_edit, 0, 0},
297 {'J', nv_join, 0, 0},
298 {'K', nv_ident, 0, 0},
299 {'L', nv_scroll, 0, 0},
300 {'M', nv_scroll, 0, 0},
301 {'N', nv_next, 0, SEARCH_REV},
302 {'O', nv_open, 0, 0},
303 {'P', nv_put, 0, 0},
304 {'Q', nv_exmode, NV_NCW, 0},
305 {'R', nv_Replace, 0, FALSE},
306 {'S', nv_subst, NV_KEEPREG, 0},
307 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
308 {'U', nv_Undo, 0, 0},
309 {'W', nv_wordcmd, 0, TRUE},
310 {'X', nv_abbrev, NV_KEEPREG, 0},
311 {'Y', nv_abbrev, NV_KEEPREG, 0},
312 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
313 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
314 {'\\', nv_error, 0, 0},
315 {']', nv_brackets, NV_NCH_ALW, FORWARD},
316 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
317 {'_', nv_lineop, 0, 0},
318 {'`', nv_gomark, NV_NCH_ALW, FALSE},
319 {'a', nv_edit, NV_NCH, 0},
320 {'b', nv_bck_word, 0, 0},
321 {'c', nv_operator, 0, 0},
322 {'d', nv_operator, 0, 0},
323 {'e', nv_wordcmd, 0, FALSE},
324 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
325 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
326 {'h', nv_left, NV_RL, 0},
327 {'i', nv_edit, NV_NCH, 0},
328 {'j', nv_down, 0, FALSE},
329 {'k', nv_up, 0, FALSE},
330 {'l', nv_right, NV_RL, 0},
331 {'m', nv_mark, NV_NCH_NOP, 0},
332 {'n', nv_next, 0, 0},
333 {'o', nv_open, 0, 0},
334 {'p', nv_put, 0, 0},
335 {'q', nv_record, NV_NCH, 0},
336 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
337 {'s', nv_subst, NV_KEEPREG, 0},
338 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
339 {'u', nv_undo, 0, 0},
340 {'w', nv_wordcmd, 0, FALSE},
341 {'x', nv_abbrev, NV_KEEPREG, 0},
342 {'y', nv_operator, 0, 0},
343 {'z', nv_zet, NV_NCH_ALW, 0},
344 {'{', nv_findpar, 0, BACKWARD},
345 {'|', nv_pipe, 0, 0},
346 {'}', nv_findpar, 0, FORWARD},
347 {'~', nv_tilde, 0, 0},
348
349 /* pound sign */
350 {POUND, nv_ident, 0, 0},
351#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200352 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
353 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
354 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
355 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {K_LEFTMOUSE, nv_mouse, 0, 0},
357 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
358 {K_LEFTDRAG, nv_mouse, 0, 0},
359 {K_LEFTRELEASE, nv_mouse, 0, 0},
360 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100361 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
363 {K_MIDDLEDRAG, nv_mouse, 0, 0},
364 {K_MIDDLERELEASE, nv_mouse, 0, 0},
365 {K_RIGHTMOUSE, nv_mouse, 0, 0},
366 {K_RIGHTDRAG, nv_mouse, 0, 0},
367 {K_RIGHTRELEASE, nv_mouse, 0, 0},
368 {K_X1MOUSE, nv_mouse, 0, 0},
369 {K_X1DRAG, nv_mouse, 0, 0},
370 {K_X1RELEASE, nv_mouse, 0, 0},
371 {K_X2MOUSE, nv_mouse, 0, 0},
372 {K_X2DRAG, nv_mouse, 0, 0},
373 {K_X2RELEASE, nv_mouse, 0, 0},
374#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000375 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000376 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {K_INS, nv_edit, 0, 0},
378 {K_KINS, nv_edit, 0, 0},
379 {K_BS, nv_ctrlh, 0, 0},
380 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
381 {K_S_UP, nv_page, NV_SS, BACKWARD},
382 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
383 {K_S_DOWN, nv_page, NV_SS, FORWARD},
384 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
385 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
386 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
387 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
388 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
389 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
390 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
391 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
392 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
393 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
394 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
395 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {K_S_END, nv_end, NV_SS, FALSE},
397 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
398 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
399 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {K_S_HOME, nv_home, NV_SS, 0},
401 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
402 {K_DEL, nv_abbrev, 0, 0},
403 {K_KDEL, nv_abbrev, 0, 0},
404 {K_UNDO, nv_kundo, 0, 0},
405 {K_HELP, nv_help, NV_NCW, 0},
406 {K_F1, nv_help, NV_NCW, 0},
407 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_GUI
410 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
411 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
412#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000413#ifdef FEAT_GUI_TABLINE
414 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000415 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#ifdef FEAT_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200418 {K_F8, farsi_f8, 0, 0},
419 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#ifdef FEAT_NETBEANS_INTG
422 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
423#endif
424#ifdef FEAT_DND
425 {K_DROP, nv_drop, NV_STS, 0},
426#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000427#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000428 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000429#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100430 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431};
432
433/* Number of commands in nv_cmds[]. */
434#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
435
436/* Sorted index of commands in nv_cmds[]. */
437static short nv_cmd_idx[NV_CMDS_SIZE];
438
439/* The highest index for which
440 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
441static int nv_max_linear;
442
443/*
444 * Compare functions for qsort() below, that checks the command character
445 * through the index in nv_cmd_idx[].
446 */
447 static int
448#ifdef __BORLANDC__
449_RTLENTRYF
450#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100451nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 int c1, c2;
454
455 /* The commands are sorted on absolute value. */
456 c1 = nv_cmds[*(const short *)s1].cmd_char;
457 c2 = nv_cmds[*(const short *)s2].cmd_char;
458 if (c1 < 0)
459 c1 = -c1;
460 if (c2 < 0)
461 c2 = -c2;
462 return c1 - c2;
463}
464
465/*
466 * Initialize the nv_cmd_idx[] table.
467 */
468 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100469init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int i;
472
473 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000474 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 nv_cmd_idx[i] = i;
476
477 /* Sort the commands by the command character. */
478 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
479
480 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000481 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
483 break;
484 nv_max_linear = i - 1;
485}
486
487/*
488 * Search for a command in the commands table.
489 * Returns -1 for invalid command.
490 */
491 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 int i;
495 int idx;
496 int top, bot;
497 int c;
498
499#ifdef FEAT_MBYTE
500 /* A multi-byte character is never a command. */
501 if (cmdchar >= 0x100)
502 return -1;
503#endif
504
505 /* We use the absolute value of the character. Special keys have a
506 * negative value, but are sorted on their absolute value. */
507 if (cmdchar < 0)
508 cmdchar = -cmdchar;
509
510 /* If the character is in the first part: The character is the index into
511 * nv_cmd_idx[]. */
512 if (cmdchar <= nv_max_linear)
513 return nv_cmd_idx[cmdchar];
514
515 /* Perform a binary search. */
516 bot = nv_max_linear + 1;
517 top = NV_CMDS_SIZE - 1;
518 idx = -1;
519 while (bot <= top)
520 {
521 i = (top + bot) / 2;
522 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
523 if (c < 0)
524 c = -c;
525 if (cmdchar == c)
526 {
527 idx = nv_cmd_idx[i];
528 break;
529 }
530 if (cmdchar > c)
531 bot = i + 1;
532 else
533 top = i - 1;
534 }
535 return idx;
536}
537
538/*
539 * Execute a command in Normal mode.
540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100542normal_cmd(
543 oparg_T *oap,
544 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 cmdarg_T ca; /* command arguments */
547 int c;
548 int ctrl_w = FALSE; /* got CTRL-W command */
549 int old_col = curwin->w_curswant;
550#ifdef FEAT_CMDL_INFO
551 int need_flushbuf; /* need to call out_flush() */
552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 pos_T old_pos; /* cursor position before command */
554 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 static int old_mapped_len = 0;
556 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000557#ifdef FEAT_EVAL
558 int set_prevcount = FALSE;
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
562 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000563
564 /* Use a count remembered from before entering an operator. After typing
565 * "3d" we return from normal_cmd() and come back here, the "3" is
566 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 ca.opcount = opcount;
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 /*
570 * If there is an operator pending, then the command we take this time
571 * will terminate it. Finish_op tells us to finish the operation before
572 * returning this time (unless the operation was cancelled).
573 */
574#ifdef CURSOR_SHAPE
575 c = finish_op;
576#endif
577 finish_op = (oap->op_type != OP_NOP);
578#ifdef CURSOR_SHAPE
579 if (finish_op != c)
580 {
581 ui_cursor_shape(); /* may show different cursor shape */
582# ifdef FEAT_MOUSESHAPE
583 update_mouseshape(-1);
584# endif
585 }
586#endif
587
Bram Moolenaara983fe92008-07-31 20:04:27 +0000588 /* When not finishing an operator and no register name typed, reset the
589 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000593#ifdef FEAT_EVAL
594 set_prevcount = TRUE;
595#endif
596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaara983fe92008-07-31 20:04:27 +0000598#ifdef FEAT_AUTOCMD
599 /* Restore counts from before receiving K_CURSORHOLD. This means after
600 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
601 * "3 * 2". */
602 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
603 {
604 ca.opcount = oap->prev_opcount;
605 ca.count0 = oap->prev_count0;
606 oap->prev_opcount = 0;
607 oap->prev_count0 = 0;
608 }
609#endif
610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613 State = NORMAL_BUSY;
614#ifdef USE_ON_FLY_SCROLL
615 dont_scroll = FALSE; /* allow scrolling here */
616#endif
617
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100618#ifdef FEAT_EVAL
619 /* Set v:count here, when called from main() and not a stuffed
620 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100621 * when there is no count. Do set it for redo. */
622 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100623 set_vcount_ca(&ca, &set_prevcount);
624#endif
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 /*
627 * Get the command character from the user.
628 */
629 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100630 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 /*
633 * If a mapping was started in Visual or Select mode, remember the length
634 * of the mapping. This is used below to not return to Insert mode for as
635 * long as the mapping is being executed.
636 */
637 if (restart_edit == 0)
638 old_mapped_len = 0;
639 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000640 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 old_mapped_len = typebuf_maplen();
642
643 if (c == NUL)
644 c = K_ZERO;
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /*
647 * In Select mode, typed text replaces the selection.
648 */
649 if (VIsual_active
650 && VIsual_select
651 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
652 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000653 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
654 * 'insertmode' is set) fake a "d"elete command, Insert mode will
655 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000656 * Insert the typed character in the typeahead buffer, so that it can
657 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000658 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000659 if (restart_edit != 0)
660 c = 'd';
661 else
662 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000663 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200664 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667#ifdef FEAT_CMDL_INFO
668 need_flushbuf = add_to_showcmd(c);
669#endif
670
671getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
674 /*
675 * Handle a count before a command and compute ca.count0.
676 * Note that '0' is a command and not the start of a count, but it's
677 * part of a count after other digits.
678 */
679 while ( (c >= '1' && c <= '9')
680 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
681 {
682 if (c == K_DEL || c == K_KDEL)
683 {
684 ca.count0 /= 10;
685#ifdef FEAT_CMDL_INFO
686 del_from_showcmd(4); /* delete the digit and ~@% */
687#endif
688 }
689 else
690 ca.count0 = ca.count0 * 10 + (c - '0');
691 if (ca.count0 < 0) /* got too large! */
692 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000693#ifdef FEAT_EVAL
694 /* Set v:count here, when called from main() and not a stuffed
695 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100696 * right after the count. Do set it for redo. */
697 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100698 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (ctrl_w)
701 {
702 ++no_mapping;
703 ++allow_keys; /* no mapping for nchar, but keys */
704 }
705 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000706 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 --no_zero_mapping;
709 if (ctrl_w)
710 {
711 --no_mapping;
712 --allow_keys;
713 }
714#ifdef FEAT_CMDL_INFO
715 need_flushbuf |= add_to_showcmd(c);
716#endif
717 }
718
719 /*
720 * If we got CTRL-W there may be a/another count
721 */
722 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
723 {
724 ctrl_w = TRUE;
725 ca.opcount = ca.count0; /* remember first count */
726 ca.count0 = 0;
727 ++no_mapping;
728 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000729 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 --no_mapping;
732 --allow_keys;
733#ifdef FEAT_CMDL_INFO
734 need_flushbuf |= add_to_showcmd(c);
735#endif
736 goto getcount; /* jump back */
737 }
738 }
739
Bram Moolenaara983fe92008-07-31 20:04:27 +0000740#ifdef FEAT_AUTOCMD
741 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000743 /* Save the count values so that ca.opcount and ca.count0 are exactly
744 * the same when coming back here after handling K_CURSORHOLD. */
745 oap->prev_opcount = ca.opcount;
746 oap->prev_count0 = ca.count0;
747 }
748 else
749#endif
750 if (ca.opcount != 0)
751 {
752 /*
753 * If we're in the middle of an operator (including after entering a
754 * yank buffer with '"') AND we had a count before the operator, then
755 * that count overrides the current value of ca.count0.
756 * What this means effectively, is that commands like "3dw" get turned
757 * into "d3w" which makes things fall into place pretty neatly.
758 * If you give a count before AND after the operator, they are
759 * multiplied.
760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (ca.count0)
762 ca.count0 *= ca.opcount;
763 else
764 ca.count0 = ca.opcount;
765 }
766
767 /*
768 * Always remember the count. It will be set to zero (on the next call,
769 * above) when there is no pending operator.
770 * When called from main(), save the count for use by the "count" built-in
771 * variable.
772 */
773 ca.opcount = ca.count0;
774 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
775
776#ifdef FEAT_EVAL
777 /*
778 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100779 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100781 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000782 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#endif
784
785 /*
786 * Find the command character in the table of commands.
787 * For CTRL-W we already got nchar when looking for a count.
788 */
789 if (ctrl_w)
790 {
791 ca.nchar = c;
792 ca.cmdchar = Ctrl_W;
793 }
794 else
795 ca.cmdchar = c;
796 idx = find_command(ca.cmdchar);
797 if (idx < 0)
798 {
799 /* Not a known command: beep. */
800 clearopbeep(oap);
801 goto normal_end;
802 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000803
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000804 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200806 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000808 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 goto normal_end;
810 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000811#ifdef FEAT_AUTOCMD
812 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
813 goto normal_end;
814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 /*
817 * In Visual/Select mode, a few keys are handled in a special way.
818 */
819 if (VIsual_active)
820 {
821 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
822 if (km_stopsel
823 && (nv_cmds[idx].cmd_flags & NV_STS)
824 && !(mod_mask & MOD_MASK_SHIFT))
825 {
826 end_visual_mode();
827 redraw_curbuf_later(INVERTED);
828 }
829
830 /* Keys that work different when 'keymodel' contains "startsel" */
831 if (km_startsel)
832 {
833 if (nv_cmds[idx].cmd_flags & NV_SS)
834 {
835 unshift_special(&ca);
836 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000837 if (idx < 0)
838 {
839 /* Just in case */
840 clearopbeep(oap);
841 goto normal_end;
842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
845 && (mod_mask & MOD_MASK_SHIFT))
846 {
847 mod_mask &= ~MOD_MASK_SHIFT;
848 }
849 }
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852#ifdef FEAT_RIGHTLEFT
853 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
854 && (nv_cmds[idx].cmd_flags & NV_RL))
855 {
856 /* Invert horizontal movements and operations. Only when typed by the
857 * user directly, not when the result of a mapping or "x" translated
858 * to "dl". */
859 switch (ca.cmdchar)
860 {
861 case 'l': ca.cmdchar = 'h'; break;
862 case K_RIGHT: ca.cmdchar = K_LEFT; break;
863 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
864 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
865 case 'h': ca.cmdchar = 'l'; break;
866 case K_LEFT: ca.cmdchar = K_RIGHT; break;
867 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
868 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
869 case '>': ca.cmdchar = '<'; break;
870 case '<': ca.cmdchar = '>'; break;
871 }
872 idx = find_command(ca.cmdchar);
873 }
874#endif
875
876 /*
877 * Get an additional character if we need one.
878 */
879 if ((nv_cmds[idx].cmd_flags & NV_NCH)
880 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
881 && oap->op_type == OP_NOP)
882 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
883 || (ca.cmdchar == 'q'
884 && oap->op_type == OP_NOP
885 && !Recording
886 && !Exec_reg)
887 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100888 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
890 int *cp;
891 int repl = FALSE; /* get character for replace mode */
892 int lit = FALSE; /* get extra character literally */
893 int langmap_active = FALSE; /* using :lmap mappings */
894 int lang; /* getting a text character */
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100895#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 int save_smd; /* saved value of p_smd */
897#endif
898
899 ++no_mapping;
900 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000901#ifdef FEAT_AUTOCMD
902 /* Don't generate a CursorHold event here, most commands can't handle
903 * it, e.g., nv_replace(), nv_csearch(). */
904 did_cursorhold = TRUE;
905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (ca.cmdchar == 'g')
907 {
908 /*
909 * For 'g' get the next character now, so that we can check for
910 * "gr", "g'" and "g`".
911 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000912 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#ifdef FEAT_CMDL_INFO
915 need_flushbuf |= add_to_showcmd(ca.nchar);
916#endif
917 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100918 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 cp = &ca.extra_char; /* need to get a third character */
921 if (ca.nchar != 'r')
922 lit = TRUE; /* get it literally */
923 else
924 repl = TRUE; /* get it in replace mode */
925 }
926 else
927 cp = NULL; /* no third character needed */
928 }
929 else
930 {
931 if (ca.cmdchar == 'r') /* get it in replace mode */
932 repl = TRUE;
933 cp = &ca.nchar;
934 }
935 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
936
937 /*
938 * Get a second or third character.
939 */
940 if (cp != NULL)
941 {
942#ifdef CURSOR_SHAPE
943 if (repl)
944 {
945 State = REPLACE; /* pretend Replace mode */
946 ui_cursor_shape(); /* show different cursor shape */
947 }
948#endif
949 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
950 {
951 /* Allow mappings defined with ":lmap". */
952 --no_mapping;
953 --allow_keys;
954 if (repl)
955 State = LREPLACE;
956 else
957 State = LANGMAP;
958 langmap_active = TRUE;
959 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100960#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 save_smd = p_smd;
962 p_smd = FALSE; /* Don't let the IM code show the mode here */
963 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
964 im_set_active(TRUE);
965#endif
966
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000967 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 if (langmap_active)
970 {
971 /* Undo the decrement done above */
972 ++no_mapping;
973 ++allow_keys;
974 State = NORMAL_BUSY;
975 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100976#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (lang)
978 {
979 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
980 im_save_status(&curbuf->b_p_iminsert);
981 im_set_active(FALSE);
982 }
983 p_smd = save_smd;
984#endif
985#ifdef CURSOR_SHAPE
986 State = NORMAL_BUSY;
987#endif
988#ifdef FEAT_CMDL_INFO
989 need_flushbuf |= add_to_showcmd(*cp);
990#endif
991
992 if (!lit)
993 {
994#ifdef FEAT_DIGRAPHS
995 /* Typing CTRL-K gets a digraph. */
996 if (*cp == Ctrl_K
997 && ((nv_cmds[idx].cmd_flags & NV_LANG)
998 || cp == &ca.extra_char)
999 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1000 {
1001 c = get_digraph(FALSE);
1002 if (c > 0)
1003 {
1004 *cp = c;
1005# ifdef FEAT_CMDL_INFO
1006 /* Guessing how to update showcmd here... */
1007 del_from_showcmd(3);
1008 need_flushbuf |= add_to_showcmd(*cp);
1009# endif
1010 }
1011 }
1012#endif
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 /* adjust chars > 127, except after "tTfFr" commands */
1015 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#ifdef FEAT_RIGHTLEFT
1017 /* adjust Hebrew mapped char */
1018 if (p_hkmap && lang && KeyTyped)
1019 *cp = hkmap(*cp);
1020# ifdef FEAT_FKMAP
1021 /* adjust Farsi mapped char */
1022 if (p_fkmap && lang && KeyTyped)
1023 *cp = fkmap(*cp);
1024# endif
1025#endif
1026 }
1027
1028 /*
1029 * When the next character is CTRL-\ a following CTRL-N means the
1030 * command is aborted and we go to Normal mode.
1031 */
1032 if (cp == &ca.extra_char
1033 && ca.nchar == Ctrl_BSL
1034 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1035 {
1036 ca.cmdchar = Ctrl_BSL;
1037 ca.nchar = ca.extra_char;
1038 idx = find_command(ca.cmdchar);
1039 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001040 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001041 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 else if (*cp == Ctrl_BSL)
1043 {
1044 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1045
1046 /* There is a busy wait here when typing "f<C-\>" and then
1047 * something different from CTRL-N. Can't be avoided. */
1048 while ((c = vpeekc()) <= 0 && towait > 0L)
1049 {
1050 do_sleep(towait > 50L ? 50L : towait);
1051 towait -= 50L;
1052 }
1053 if (c > 0)
1054 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001055 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (c != Ctrl_N && c != Ctrl_G)
1057 vungetc(c);
1058 else
1059 {
1060 ca.cmdchar = Ctrl_BSL;
1061 ca.nchar = c;
1062 idx = find_command(ca.cmdchar);
1063 }
1064 }
1065 }
1066
1067#ifdef FEAT_MBYTE
1068 /* When getting a text character and the next character is a
1069 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001070 * However, don't wait for it to arrive. Also, do enable mapping,
1071 * because if it's put back with vungetc() it's too late to apply
1072 * mapping. */
1073 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 while (enc_utf8 && lang && (c = vpeekc()) > 0
1075 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1076 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001077 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (!utf_iscomposing(c))
1079 {
1080 vungetc(c); /* it wasn't, put it back */
1081 break;
1082 }
1083 else if (ca.ncharC1 == 0)
1084 ca.ncharC1 = c;
1085 else
1086 ca.ncharC2 = c;
1087 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001088 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#endif
1090 }
1091 --no_mapping;
1092 --allow_keys;
1093 }
1094
1095#ifdef FEAT_CMDL_INFO
1096 /*
1097 * Flush the showcmd characters onto the screen so we can see them while
1098 * the command is being executed. Only do this when the shown command was
1099 * actually displayed, otherwise this will slow down a lot when executing
1100 * mappings.
1101 */
1102 if (need_flushbuf)
1103 out_flush();
1104#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001105#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001106 if (ca.cmdchar != K_IGNORE)
1107 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
1110 State = NORMAL;
1111
1112 if (ca.nchar == ESC)
1113 {
1114 clearop(oap);
1115 if (restart_edit == 0 && goto_im())
1116 restart_edit = 'a';
1117 goto normal_end;
1118 }
1119
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001120 if (ca.cmdchar != K_IGNORE)
1121 {
1122 msg_didout = FALSE; /* don't scroll screen up for normal command */
1123 msg_col = 0;
1124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 old_pos = curwin->w_cursor; /* remember where cursor was */
1127
1128 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1129 * mode. */
1130 if (!VIsual_active && km_startsel)
1131 {
1132 if (nv_cmds[idx].cmd_flags & NV_SS)
1133 {
1134 start_selection();
1135 unshift_special(&ca);
1136 idx = find_command(ca.cmdchar);
1137 }
1138 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1139 && (mod_mask & MOD_MASK_SHIFT))
1140 {
1141 start_selection();
1142 mod_mask &= ~MOD_MASK_SHIFT;
1143 }
1144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
1146 /*
1147 * Execute the command!
1148 * Call the command function found in the commands table.
1149 */
1150 ca.arg = nv_cmds[idx].cmd_arg;
1151 (nv_cmds[idx].cmd_func)(&ca);
1152
1153 /*
1154 * If we didn't start or finish an operator, reset oap->regname, unless we
1155 * need it later.
1156 */
1157 if (!finish_op
1158 && !oap->op_type
1159 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1160 {
1161 clearop(oap);
1162#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001163 {
1164 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001165
Bram Moolenaar536681b2011-05-10 16:12:45 +02001166 /* Adjust the register according to 'clipboard', so that when
1167 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001168# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001169 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001170# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001171 set_reg_var(regname);
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174 }
1175
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001176 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001177 * character or "z333<cr>". */
1178 if (old_mapped_len > 0)
1179 old_mapped_len = typebuf_maplen();
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 /*
1182 * If an operation is pending, handle it...
1183 */
1184 do_pending_operator(&ca, old_col, FALSE);
1185
1186 /*
1187 * Wait for a moment when a message is displayed that will be overwritten
1188 * by the mode message.
1189 * In Visual mode and with "^O" in Insert mode, a short message will be
1190 * overwritten by the mode message. Wait a bit, until a key is hit.
1191 * In Visual mode, it's more important to keep the Visual area updated
1192 * than keeping a message (e.g. from a /pat search).
1193 * Only do this if the command was typed, not from a mapping.
1194 * Don't wait when emsg_silent is non-zero.
1195 * Also wait a bit after an error message, e.g. for "^O:".
1196 * Don't redraw the screen, it would remove the message.
1197 */
1198 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001199 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 || (VIsual_active
1202 && old_pos.lnum == curwin->w_cursor.lnum
1203 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 )
1205 && (clear_cmdline
1206 || redraw_cmdline)
1207 && (msg_didout || (msg_didany && msg_scroll))
1208 && !msg_nowait
1209 && KeyTyped)
1210 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 && (msg_scroll
1213 || emsg_on_display)))
1214 && oap->regname == 0
1215 && !(ca.retval & CA_COMMAND_BUSY)
1216 && stuff_empty()
1217 && typebuf_typed()
1218 && emsg_silent == 0
1219 && !did_wait_return
1220 && oap->op_type == OP_NOP)
1221 {
1222 int save_State = State;
1223
1224 /* Draw the cursor with the right shape here */
1225 if (restart_edit != 0)
1226 State = INSERT;
1227
1228 /* If need to redraw, and there is a "keep_msg", redraw before the
1229 * delay */
1230 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1231 {
1232 char_u *kmsg;
1233
1234 kmsg = keep_msg;
1235 keep_msg = NULL;
1236 /* showmode() will clear keep_msg, but we want to use it anyway */
1237 update_screen(0);
1238 /* now reset it, otherwise it's put in the history again */
1239 keep_msg = kmsg;
1240 msg_attr(kmsg, keep_msg_attr);
1241 vim_free(kmsg);
1242 }
1243 setcursor();
1244 cursor_on();
1245 out_flush();
1246 if (msg_scroll || emsg_on_display)
1247 ui_delay(1000L, TRUE); /* wait at least one second */
1248 ui_delay(3000L, FALSE); /* wait up to three seconds */
1249 State = save_State;
1250
1251 msg_scroll = FALSE;
1252 emsg_on_display = FALSE;
1253 }
1254
1255 /*
1256 * Finish up after executing a Normal mode command.
1257 */
1258normal_end:
1259
1260 msg_nowait = FALSE;
1261
1262 /* Reset finish_op, in case it was set */
1263#ifdef CURSOR_SHAPE
1264 c = finish_op;
1265#endif
1266 finish_op = FALSE;
1267#ifdef CURSOR_SHAPE
1268 /* Redraw the cursor with another shape, if we were in Operator-pending
1269 * mode or did a replace command. */
1270 if (c || ca.cmdchar == 'r')
1271 {
1272 ui_cursor_shape(); /* may show different cursor shape */
1273# ifdef FEAT_MOUSESHAPE
1274 update_mouseshape(-1);
1275# endif
1276 }
1277#endif
1278
1279#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001280 if (oap->op_type == OP_NOP && oap->regname == 0
1281# ifdef FEAT_AUTOCMD
1282 && ca.cmdchar != K_CURSORHOLD
1283# endif
1284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 clear_showcmd();
1286#endif
1287
1288 checkpcmark(); /* check if we moved since setting pcmark */
1289 vim_free(ca.searchbuf);
1290
1291#ifdef FEAT_MBYTE
1292 if (has_mbyte)
1293 mb_adjust_cursor();
1294#endif
1295
1296#ifdef FEAT_SCROLLBIND
1297 if (curwin->w_p_scb && toplevel)
1298 {
1299 validate_cursor(); /* may need to update w_leftcol */
1300 do_check_scrollbind(TRUE);
1301 }
1302#endif
1303
Bram Moolenaar860cae12010-06-05 23:22:07 +02001304#ifdef FEAT_CURSORBIND
1305 if (curwin->w_p_crb && toplevel)
1306 {
1307 validate_cursor(); /* may need to update w_leftcol */
1308 do_check_cursorbind();
1309 }
1310#endif
1311
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001312#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001313 /* don't go to Insert mode if a terminal has a running job */
1314 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001315 restart_edit = 0;
1316#endif
1317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 /*
1319 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1320 * if still inside a mapping that started in Visual mode).
1321 * May switch from Visual to Select mode after CTRL-O command.
1322 */
1323 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1325 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 && !(ca.retval & CA_COMMAND_BUSY)
1327 && stuff_empty()
1328 && oap->regname == 0)
1329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (restart_VIsual_select == 1)
1331 {
1332 VIsual_select = TRUE;
1333 showmode();
1334 restart_VIsual_select = 0;
1335 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001336 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 (void)edit(restart_edit, FALSE, 1L);
1338 }
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (restart_VIsual_select == 2)
1341 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 /* Save count before an operator for next time. */
1344 opcount = ca.opcount;
1345}
1346
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001347#ifdef FEAT_EVAL
1348/*
1349 * Set v:count and v:count1 according to "cap".
1350 * Set v:prevcount only when "set_prevcount" is TRUE.
1351 */
1352 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001353set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001354{
1355 long count = cap->count0;
1356
1357 /* multiply with cap->opcount the same way as above */
1358 if (cap->opcount != 0)
1359 count = cap->opcount * (count == 0 ? 1 : count);
1360 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1361 *set_prevcount = FALSE; /* only set v:prevcount once */
1362}
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365/*
1366 * Handle an operator after visual mode or when the movement is finished
1367 */
1368 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001369do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 oparg_T *oap = cap->oap;
1372 pos_T old_cursor;
1373 int empty_region_error;
1374 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001375#ifdef FEAT_LINEBREAK
1376 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /* The visual area is remembered for redo */
1380 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1381 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001382 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001384 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001385#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388
1389#if defined(FEAT_CLIPBOARD)
1390 /*
1391 * Yank the visual area into the GUI selection register before we operate
1392 * on it and lose it forever.
1393 * Don't do it if a specific register was specified, so that ""x"*P works.
1394 * This could call do_pending_operator() recursively, but that's OK
1395 * because gui_yank will be TRUE for the nested call.
1396 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001397 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 && oap->op_type != OP_NOP
1399 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 && VIsual_active
1401 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 && oap->regname == 0)
1403 clip_auto_select();
1404#endif
1405 old_cursor = curwin->w_cursor;
1406
1407 /*
1408 * If an operation is pending, handle it...
1409 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001410 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001412#ifdef FEAT_LINEBREAK
1413 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001414 if (curwin->w_p_lbr)
1415 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001416 curwin->w_p_lbr = FALSE;
1417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 oap->is_VIsual = VIsual_active;
1419 if (oap->motion_force == 'V')
1420 oap->motion_type = MLINE;
1421 else if (oap->motion_force == 'v')
1422 {
1423 /* If the motion was linewise, "inclusive" will not have been set.
1424 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1425 if (oap->motion_type == MLINE)
1426 oap->inclusive = FALSE;
1427 /* If the motion already was characterwise, toggle "inclusive" */
1428 else if (oap->motion_type == MCHAR)
1429 oap->inclusive = !oap->inclusive;
1430 oap->motion_type = MCHAR;
1431 }
1432 else if (oap->motion_force == Ctrl_V)
1433 {
1434 /* Change line- or characterwise motion into Visual block mode. */
1435 VIsual_active = TRUE;
1436 VIsual = oap->start;
1437 VIsual_mode = Ctrl_V;
1438 VIsual_select = FALSE;
1439 VIsual_reselect = FALSE;
1440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar7afea822013-04-24 18:34:45 +02001442 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1443 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001445 && ((!VIsual_active || oap->motion_force)
1446 /* Also redo Operator-pending Visual mode mappings */
1447 || (VIsual_active && cap->cmdchar == ':'
1448 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001449 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450#ifdef FEAT_FOLDING
1451 && oap->op_type != OP_FOLD
1452 && oap->op_type != OP_FOLDOPEN
1453 && oap->op_type != OP_FOLDOPENREC
1454 && oap->op_type != OP_FOLDCLOSE
1455 && oap->op_type != OP_FOLDCLOSEREC
1456 && oap->op_type != OP_FOLDDEL
1457 && oap->op_type != OP_FOLDDELREC
1458#endif
1459 )
1460 {
1461 prep_redo(oap->regname, cap->count0,
1462 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1463 oap->motion_force, cap->cmdchar, cap->nchar);
1464 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1465 {
1466 /*
1467 * If 'cpoptions' does not contain 'r', insert the search
1468 * pattern to really repeat the same command.
1469 */
1470 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001471 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 AppendToRedobuff(NL_STR);
1473 }
1474 else if (cap->cmdchar == ':')
1475 {
1476 /* do_cmdline() has stored the first typed line in
1477 * "repeat_cmdline". When several lines are typed repeating
1478 * won't be possible. */
1479 if (repeat_cmdline == NULL)
1480 ResetRedobuff();
1481 else
1482 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001483 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 AppendToRedobuff(NL_STR);
1485 vim_free(repeat_cmdline);
1486 repeat_cmdline = NULL;
1487 }
1488 }
1489 }
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (redo_VIsual_busy)
1492 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001493 /* Redo of an operation on a Visual area. Use the same size from
1494 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 oap->start = curwin->w_cursor;
1496 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1497 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1498 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1499 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001500 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001502 if (VIsual_mode == 'v')
1503 {
1504 if (redo_VIsual_line_count <= 1)
1505 {
1506 validate_virtcol();
1507 curwin->w_curswant =
1508 curwin->w_virtcol + redo_VIsual_vcol - 1;
1509 }
1510 else
1511 curwin->w_curswant = redo_VIsual_vcol;
1512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001514 {
1515 curwin->w_curswant = MAXCOL;
1516 }
1517 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519 cap->count0 = redo_VIsual_count;
1520 if (redo_VIsual_count != 0)
1521 cap->count1 = redo_VIsual_count;
1522 else
1523 cap->count1 = 1;
1524 }
1525 else if (VIsual_active)
1526 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001527 if (!gui_yank)
1528 {
1529 /* Save the current VIsual area for '< and '> marks, and "gv" */
1530 curbuf->b_visual.vi_start = VIsual;
1531 curbuf->b_visual.vi_end = curwin->w_cursor;
1532 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001533 if (VIsual_mode_orig != NUL)
1534 {
1535 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1536 VIsual_mode_orig = NUL;
1537 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001538 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001540 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001545 * characterwise selection.
1546 * Special case: gH<Del> deletes the last line. */
1547 if (VIsual_select && VIsual_mode == 'V'
1548 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001550 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552 VIsual.col = 0;
1553 curwin->w_cursor.col =
1554 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1555 }
1556 else
1557 {
1558 curwin->w_cursor.col = 0;
1559 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1560 }
1561 VIsual_mode = 'v';
1562 }
1563 /* If 'selection' is "exclusive", backup one character for
1564 * charwise selections. */
1565 else if (VIsual_mode == 'v')
1566 {
1567# ifdef FEAT_VIRTUALEDIT
1568 include_line_break =
1569# endif
1570 unadjust_for_sel();
1571 }
1572
1573 oap->start = VIsual;
1574 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001577# ifdef FEAT_VIRTUALEDIT
1578 oap->start.coladd = 0;
1579# endif
1580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 /*
1584 * Set oap->start to the first position of the operated text, oap->end
1585 * to the end of the operated text. w_cursor is equal to oap->start.
1586 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001587 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589#ifdef FEAT_FOLDING
1590 /* Include folded lines completely. */
1591 if (!VIsual_active)
1592 {
1593 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1594 oap->start.col = 0;
1595 if (hasFolding(curwin->w_cursor.lnum, NULL,
1596 &curwin->w_cursor.lnum))
1597 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1598 }
1599#endif
1600 oap->end = curwin->w_cursor;
1601 curwin->w_cursor = oap->start;
1602
1603 /* w_virtcol may have been updated; if the cursor goes back to its
1604 * previous position w_virtcol becomes invalid and isn't updated
1605 * automatically. */
1606 curwin->w_valid &= ~VALID_VIRTCOL;
1607 }
1608 else
1609 {
1610#ifdef FEAT_FOLDING
1611 /* Include folded lines completely. */
1612 if (!VIsual_active && oap->motion_type == MLINE)
1613 {
1614 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1615 NULL))
1616 curwin->w_cursor.col = 0;
1617 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1618 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1619 }
1620#endif
1621 oap->end = oap->start;
1622 oap->start = curwin->w_cursor;
1623 }
1624
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001625 /* Just in case lines were deleted that make the position invalid. */
1626 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1628
1629#ifdef FEAT_VIRTUALEDIT
1630 /* Set "virtual_op" before resetting VIsual_active. */
1631 virtual_op = virtual_active();
1632#endif
1633
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (VIsual_active || redo_VIsual_busy)
1635 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001636 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 if (!redo_VIsual_busy && !gui_yank)
1639 {
1640 /*
1641 * Prepare to reselect and redo Visual: this is based on the
1642 * size of the Visual text
1643 */
1644 resel_VIsual_mode = VIsual_mode;
1645 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001646 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001648 {
1649 if (VIsual_mode != Ctrl_V)
1650 getvvcol(curwin, &(oap->end),
1651 NULL, NULL, &oap->end_vcol);
1652 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1653 {
1654 if (VIsual_mode != Ctrl_V)
1655 getvvcol(curwin, &(oap->start),
1656 &oap->start_vcol, NULL, NULL);
1657 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1658 }
1659 else
1660 resel_VIsual_vcol = oap->end_vcol;
1661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 resel_VIsual_line_count = oap->line_count;
1663 }
1664
1665 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1666 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1667 && oap->op_type != OP_COLON
1668#ifdef FEAT_FOLDING
1669 && oap->op_type != OP_FOLD
1670 && oap->op_type != OP_FOLDOPEN
1671 && oap->op_type != OP_FOLDOPENREC
1672 && oap->op_type != OP_FOLDCLOSE
1673 && oap->op_type != OP_FOLDCLOSEREC
1674 && oap->op_type != OP_FOLDDEL
1675 && oap->op_type != OP_FOLDDELREC
1676#endif
1677 && oap->motion_force == NUL
1678 )
1679 {
1680 /* Prepare for redoing. Only use the nchar field for "r",
1681 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001682 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1683 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001684 prep_redo(oap->regname, cap->count0,
1685 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1686 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001687 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001688 {
1689 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1690
1691 /* reverse what nv_replace() did */
1692 if (nchar == REPLACE_CR_NCHAR)
1693 nchar = CAR;
1694 else if (nchar == REPLACE_NL_NCHAR)
1695 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001696 prep_redo(oap->regname, 0L, NUL, 'v',
1697 get_op_char(oap->op_type),
1698 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001699 nchar);
1700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (!redo_VIsual_busy)
1702 {
1703 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001704 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 redo_VIsual_line_count = resel_VIsual_line_count;
1706 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001707 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 }
1709 }
1710
1711 /*
1712 * oap->inclusive defaults to TRUE.
1713 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1714 * FALSE. This makes "d}P" and "v}dP" work the same.
1715 */
1716 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1717 oap->inclusive = TRUE;
1718 if (VIsual_mode == 'V')
1719 oap->motion_type = MLINE;
1720 else
1721 {
1722 oap->motion_type = MCHAR;
1723 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001724#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 )
1728 {
1729 oap->inclusive = FALSE;
1730 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001731 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001732 if (*p_sel != 'o'
1733 && !op_on_lines(oap->op_type)
1734 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001736 ++oap->end.lnum;
1737 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001738#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001739 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001740#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001741 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 }
1743 }
1744 }
1745
1746 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001747
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 /*
1749 * Switch Visual off now, so screen updating does
1750 * not show inverted text when the screen is redrawn.
1751 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1752 * no screen redraw, so it is done here to remove the inverted
1753 * part.
1754 */
1755 if (!gui_yank)
1756 {
1757 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001758#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 setmouse();
1760 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001761#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001762 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if ((oap->op_type == OP_YANK
1764 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001765 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 || oap->op_type == OP_FILTER)
1767 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001768 {
1769#ifdef FEAT_LINEBREAK
1770 /* make sure redrawing is correct */
1771 curwin->w_p_lbr = lbr_saved;
1772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 }
1776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778#ifdef FEAT_MBYTE
1779 /* Include the trailing byte of a multi-byte char. */
1780 if (has_mbyte && oap->inclusive)
1781 {
1782 int l;
1783
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001784 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (l > 1)
1786 oap->end.col += l - 1;
1787 }
1788#endif
1789 curwin->w_set_curswant = TRUE;
1790
1791 /*
1792 * oap->empty is set when start and end are the same. The inclusive
1793 * flag affects this too, unless yanking and the end is on a NUL.
1794 */
1795 oap->empty = (oap->motion_type == MCHAR
1796 && (!oap->inclusive
1797 || (oap->op_type == OP_YANK
1798 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001799 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#ifdef FEAT_VIRTUALEDIT
1801 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1802#endif
1803 );
1804 /*
1805 * For delete, change and yank, it's an error to operate on an
1806 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1807 */
1808 empty_region_error = (oap->empty
1809 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 /* Force a redraw when operating on an empty Visual region, when
1812 * 'modifiable is off or creating a fold. */
1813 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001814#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001818 {
1819#ifdef FEAT_LINEBREAK
1820 curwin->w_p_lbr = lbr_saved;
1821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 /*
1826 * If the end of an operator is in column one while oap->motion_type
1827 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1828 * character in the previous line. If op_start is on or before the
1829 * first non-blank in the line, the operator becomes linewise
1830 * (strange, but that's the way vi does it).
1831 */
1832 if ( oap->motion_type == MCHAR
1833 && oap->inclusive == FALSE
1834 && !(cap->retval & CA_NO_ADJ_OP_END)
1835 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001837 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 && oap->line_count > 1)
1839 {
1840 oap->end_adjusted = TRUE; /* remember that we did this */
1841 --oap->line_count;
1842 --oap->end.lnum;
1843 if (inindent(0))
1844 oap->motion_type = MLINE;
1845 else
1846 {
1847 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1848 if (oap->end.col)
1849 {
1850 --oap->end.col;
1851 oap->inclusive = TRUE;
1852 }
1853 }
1854 }
1855 else
1856 oap->end_adjusted = FALSE;
1857
1858 switch (oap->op_type)
1859 {
1860 case OP_LSHIFT:
1861 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001862 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 auto_format(FALSE, TRUE);
1864 break;
1865
1866 case OP_JOIN_NS:
1867 case OP_JOIN:
1868 if (oap->line_count < 2)
1869 oap->line_count = 2;
1870 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1871 curbuf->b_ml.ml_line_count)
1872 beep_flush();
1873 else
1874 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001875 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001876 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 auto_format(FALSE, TRUE);
1878 }
1879 break;
1880
1881 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001884 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001885 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001886 CancelRedo();
1887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 else
1889 {
1890 (void)op_delete(oap);
1891 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1892 u_save_cursor(); /* cursor line wasn't saved yet */
1893 auto_format(FALSE, TRUE);
1894 }
1895 break;
1896
1897 case OP_YANK:
1898 if (empty_region_error)
1899 {
1900 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001901 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001902 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001903 CancelRedo();
1904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001907 {
1908#ifdef FEAT_LINEBREAK
1909 curwin->w_p_lbr = lbr_saved;
1910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 check_cursor_col();
1914 break;
1915
1916 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001919 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001920 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001921 CancelRedo();
1922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 {
1925 /* This is a new edit command, not a restart. Need to
1926 * remember it to make 'insertmode' work with mappings for
1927 * Visual mode. But do this only once and not when typed and
1928 * 'insertmode' isn't set. */
1929 if (p_im || !KeyTyped)
1930 restart_edit_save = restart_edit;
1931 else
1932 restart_edit_save = 0;
1933 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001934#ifdef FEAT_LINEBREAK
1935 /* Restore linebreak, so that when the user edits it looks as
1936 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001937 if (curwin->w_p_lbr != lbr_saved)
1938 {
1939 curwin->w_p_lbr = lbr_saved;
1940 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1941 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 /* Reset finish_op now, don't want it set inside edit(). */
1944 finish_op = FALSE;
1945 if (op_change(oap)) /* will call edit() */
1946 cap->retval |= CA_COMMAND_BUSY;
1947 if (restart_edit == 0)
1948 restart_edit = restart_edit_save;
1949 }
1950 break;
1951
1952 case OP_FILTER:
1953 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1954 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1955 else
1956 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001957 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958
1959 case OP_INDENT:
1960 case OP_COLON:
1961
1962#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1963 /*
1964 * If 'equalprg' is empty, do the indenting internally.
1965 */
1966 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1967 {
1968# ifdef FEAT_LISP
1969 if (curbuf->b_p_lisp)
1970 {
1971 op_reindent(oap, get_lisp_indent);
1972 break;
1973 }
1974# endif
1975# ifdef FEAT_CINDENT
1976 op_reindent(oap,
1977# ifdef FEAT_EVAL
1978 *curbuf->b_p_inde != NUL ? get_expr_indent :
1979# endif
1980 get_c_indent);
1981 break;
1982# endif
1983 }
1984#endif
1985
1986 op_colon(oap);
1987 break;
1988
1989 case OP_TILDE:
1990 case OP_UPPER:
1991 case OP_LOWER:
1992 case OP_ROT13:
1993 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001994 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001995 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001996 CancelRedo();
1997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 else
1999 op_tilde(oap);
2000 check_cursor_col();
2001 break;
2002
2003 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002004#if defined(FEAT_EVAL)
2005 if (*curbuf->b_p_fex != NUL)
2006 op_formatexpr(oap); /* use expression */
2007 else
2008#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002009 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 op_colon(oap); /* use external command */
2011 else
2012 op_format(oap, FALSE); /* use internal function */
2013 break;
2014
2015 case OP_FORMAT2:
2016 op_format(oap, TRUE); /* use internal function */
2017 break;
2018
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002019 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01002020#ifdef FEAT_LINEBREAK
2021 /* Restore linebreak, so that when the user edits it looks as
2022 * before. */
2023 curwin->w_p_lbr = lbr_saved;
2024#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002025 op_function(oap); /* call 'operatorfunc' */
2026 break;
2027
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 case OP_INSERT:
2029 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031#ifdef FEAT_VISUALEXTRA
2032 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002033 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002034 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002035 CancelRedo();
2036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 else
2038 {
2039 /* This is a new edit command, not a restart. Need to
2040 * remember it to make 'insertmode' work with mappings for
2041 * Visual mode. But do this only once. */
2042 restart_edit_save = restart_edit;
2043 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002044#ifdef FEAT_LINEBREAK
2045 /* Restore linebreak, so that when the user edits it looks as
2046 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002047 if (curwin->w_p_lbr != lbr_saved)
2048 {
2049 curwin->w_p_lbr = lbr_saved;
2050 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2051 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002054#ifdef FEAT_LINEBREAK
2055 /* Reset linebreak, so that formatting works correctly. */
2056 curwin->w_p_lbr = FALSE;
2057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 /* TODO: when inserting in several lines, should format all
2060 * the lines. */
2061 auto_format(FALSE, TRUE);
2062
2063 if (restart_edit == 0)
2064 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002065 else
2066 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002069 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070#endif
2071 break;
2072
2073 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075#ifdef FEAT_VISUALEXTRA
2076 if (empty_region_error)
2077#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002078 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002079 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002080 CancelRedo();
2081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082#ifdef FEAT_VISUALEXTRA
2083 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002084 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002085# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002086 /* Restore linebreak, so that when the user edits it looks as
2087 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002088 if (curwin->w_p_lbr != lbr_saved)
2089 {
2090 curwin->w_p_lbr = lbr_saved;
2091 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2092 }
2093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096#endif
2097 break;
2098
2099#ifdef FEAT_FOLDING
2100 case OP_FOLD:
2101 VIsual_reselect = FALSE; /* don't reselect now */
2102 foldCreate(oap->start.lnum, oap->end.lnum);
2103 break;
2104
2105 case OP_FOLDOPEN:
2106 case OP_FOLDOPENREC:
2107 case OP_FOLDCLOSE:
2108 case OP_FOLDCLOSEREC:
2109 VIsual_reselect = FALSE; /* don't reselect now */
2110 opFoldRange(oap->start.lnum, oap->end.lnum,
2111 oap->op_type == OP_FOLDOPEN
2112 || oap->op_type == OP_FOLDOPENREC,
2113 oap->op_type == OP_FOLDOPENREC
2114 || oap->op_type == OP_FOLDCLOSEREC,
2115 oap->is_VIsual);
2116 break;
2117
2118 case OP_FOLDDEL:
2119 case OP_FOLDDELREC:
2120 VIsual_reselect = FALSE; /* don't reselect now */
2121 deleteFold(oap->start.lnum, oap->end.lnum,
2122 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2123 break;
2124#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002125 case OP_NR_ADD:
2126 case OP_NR_SUB:
2127 if (empty_region_error)
2128 {
2129 vim_beep(BO_OPER);
2130 CancelRedo();
2131 }
2132 else
2133 {
2134 VIsual_active = TRUE;
2135#ifdef FEAT_LINEBREAK
2136 curwin->w_p_lbr = lbr_saved;
2137#endif
2138 op_addsub(oap, cap->count1, redo_VIsual_arg);
2139 VIsual_active = FALSE;
2140 }
2141 check_cursor_col();
2142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 default:
2144 clearopbeep(oap);
2145 }
2146#ifdef FEAT_VIRTUALEDIT
2147 virtual_op = MAYBE;
2148#endif
2149 if (!gui_yank)
2150 {
2151 /*
2152 * if 'sol' not set, go back to old column for some commands
2153 */
2154 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2155 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2156 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002157 {
2158#ifdef FEAT_LINEBREAK
2159 curwin->w_p_lbr = FALSE;
2160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 else
2165 {
2166 curwin->w_cursor = old_cursor;
2167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 clearop(oap);
2170 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002171#ifdef FEAT_LINEBREAK
2172 curwin->w_p_lbr = lbr_saved;
2173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174}
2175
2176/*
2177 * Handle indent and format operators and visual mode ":".
2178 */
2179 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002180op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181{
2182 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (oap->is_VIsual)
2184 stuffReadbuff((char_u *)"'<,'>");
2185 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187 /*
2188 * Make the range look nice, so it can be repeated.
2189 */
2190 if (oap->start.lnum == curwin->w_cursor.lnum)
2191 stuffcharReadbuff('.');
2192 else
2193 stuffnumReadbuff((long)oap->start.lnum);
2194 if (oap->end.lnum != oap->start.lnum)
2195 {
2196 stuffcharReadbuff(',');
2197 if (oap->end.lnum == curwin->w_cursor.lnum)
2198 stuffcharReadbuff('.');
2199 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2200 stuffcharReadbuff('$');
2201 else if (oap->start.lnum == curwin->w_cursor.lnum)
2202 {
2203 stuffReadbuff((char_u *)".+");
2204 stuffnumReadbuff((long)oap->line_count - 1);
2205 }
2206 else
2207 stuffnumReadbuff((long)oap->end.lnum);
2208 }
2209 }
2210 if (oap->op_type != OP_COLON)
2211 stuffReadbuff((char_u *)"!");
2212 if (oap->op_type == OP_INDENT)
2213 {
2214#ifndef FEAT_CINDENT
2215 if (*get_equalprg() == NUL)
2216 stuffReadbuff((char_u *)"indent");
2217 else
2218#endif
2219 stuffReadbuff(get_equalprg());
2220 stuffReadbuff((char_u *)"\n");
2221 }
2222 else if (oap->op_type == OP_FORMAT)
2223 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002224 if (*curbuf->b_p_fp != NUL)
2225 stuffReadbuff(curbuf->b_p_fp);
2226 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002228 else
2229 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002230 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232
2233 /*
2234 * do_cmdline() does the rest
2235 */
2236}
2237
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002238/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002239 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002240 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002241 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002242op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002243{
2244#ifdef FEAT_EVAL
2245 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002246# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002247 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002248# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002249
2250 if (*p_opfunc == NUL)
2251 EMSG(_("E774: 'operatorfunc' is empty"));
2252 else
2253 {
2254 /* Set '[ and '] marks to text to be operated on. */
2255 curbuf->b_op_start = oap->start;
2256 curbuf->b_op_end = oap->end;
2257 if (oap->motion_type != MLINE && !oap->inclusive)
2258 /* Exclude the end position. */
2259 decl(&curbuf->b_op_end);
2260
2261 if (oap->block_mode)
2262 argv[0] = (char_u *)"block";
2263 else if (oap->motion_type == MLINE)
2264 argv[0] = (char_u *)"line";
2265 else
2266 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002267
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002268# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002269 /* Reset virtual_op so that 'virtualedit' can be changed in the
2270 * function. */
2271 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002272# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002273
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002274 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002275
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002276# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002277 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002278# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002279 }
2280#else
2281 EMSG(_("E775: Eval feature not available"));
2282#endif
2283}
2284
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#if defined(FEAT_MOUSE) || defined(PROTO)
2286/*
2287 * Do the appropriate action for the current mouse click in the current mode.
2288 * Not used for Command-line mode.
2289 *
2290 * Normal Mode:
2291 * event modi- position visual change action
2292 * fier cursor window
2293 * left press - yes end yes
2294 * left press C yes end yes "^]" (2)
2295 * left press S yes end yes "*" (2)
2296 * left drag - yes start if moved no
2297 * left relse - yes start if moved no
2298 * middle press - yes if not active no put register
2299 * middle press - yes if active no yank and put
2300 * right press - yes start or extend yes
2301 * right press S yes no change yes "#" (2)
2302 * right drag - yes extend no
2303 * right relse - yes extend no
2304 *
2305 * Insert or Replace Mode:
2306 * event modi- position visual change action
2307 * fier cursor window
2308 * left press - yes (cannot be active) yes
2309 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2310 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2311 * left drag - yes start or extend (1) no CTRL-O (1)
2312 * left relse - yes start or extend (1) no CTRL-O (1)
2313 * middle press - no (cannot be active) no put register
2314 * right press - yes start or extend yes CTRL-O
2315 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2316 *
2317 * (1) only if mouse pointer moved since press
2318 * (2) only if click is in same buffer
2319 *
2320 * Return TRUE if start_arrow() should be called for edit mode.
2321 */
2322 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002323do_mouse(
2324 oparg_T *oap, /* operator argument, can be NULL */
2325 int c, /* K_LEFTMOUSE, etc */
2326 int dir, /* Direction to 'put' if necessary */
2327 long count,
2328 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2331 static int got_click = FALSE; /* got a click some time back */
2332
2333 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2334 int is_click; /* If FALSE it's a drag or release event */
2335 int is_drag; /* If TRUE it's a drag event */
2336 int jump_flags = 0; /* flags for jump_to_mouse() */
2337 pos_T start_visual;
2338 int moved; /* Has cursor moved? */
2339 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002340 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 int c1, c2;
2343#if defined(FEAT_FOLDING)
2344 pos_T save_cursor;
2345#endif
2346 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 static pos_T orig_cursor;
2348 colnr_T leftcol, rightcol;
2349 pos_T end_visual;
2350 int diff;
2351 int old_active = VIsual_active;
2352 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 int regname;
2354
2355#if defined(FEAT_FOLDING)
2356 save_cursor = curwin->w_cursor;
2357#endif
2358
2359 /*
2360 * When GUI is active, always recognize mouse events, otherwise:
2361 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2362 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2363 * - For command line and insert mode 'mouse' is checked before calling
2364 * do_mouse().
2365 */
2366 if (do_always)
2367 do_always = FALSE;
2368 else
2369#ifdef FEAT_GUI
2370 if (!gui.in_use)
2371#endif
2372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (VIsual_active)
2374 {
2375 if (!mouse_has(MOUSE_VISUAL))
2376 return FALSE;
2377 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002378 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 return FALSE;
2380 }
2381
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002382 for (;;)
2383 {
2384 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2385 if (is_drag)
2386 {
2387 /* If the next character is the same mouse event then use that
2388 * one. Speeds up dragging the status line. */
2389 if (vpeekc() != NUL)
2390 {
2391 int nc;
2392 int save_mouse_row = mouse_row;
2393 int save_mouse_col = mouse_col;
2394
2395 /* Need to get the character, peeking doesn't get the actual
2396 * one. */
2397 nc = safe_vgetc();
2398 if (c == nc)
2399 continue;
2400 vungetc(nc);
2401 mouse_row = save_mouse_row;
2402 mouse_col = save_mouse_col;
2403 }
2404 }
2405 break;
2406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002408 if (c == K_MOUSEMOVE)
2409 {
2410 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002411#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002412 ui_may_remove_balloon();
2413 if (p_bevalterm && !VIsual_active)
2414 {
2415 profile_setlimit(p_bdlay, &bevalexpr_due);
2416 bevalexpr_due_set = TRUE;
2417 }
2418#endif
2419 return FALSE;
2420 }
2421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#ifdef FEAT_MOUSESHAPE
2423 /* May have stopped dragging the status or separator line. The pointer is
2424 * most likely still on the status or separator line. */
2425 if (!is_drag && drag_status_line)
2426 {
2427 drag_status_line = FALSE;
2428 update_mouseshape(SHAPE_IDX_STATUS);
2429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 if (!is_drag && drag_sep_line)
2431 {
2432 drag_sep_line = FALSE;
2433 update_mouseshape(SHAPE_IDX_VSEP);
2434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435#endif
2436
2437 /*
2438 * Ignore drag and release events if we didn't get a click.
2439 */
2440 if (is_click)
2441 got_click = TRUE;
2442 else
2443 {
2444 if (!got_click) /* didn't get click, ignore */
2445 return FALSE;
2446 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002447 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002449 if (in_tab_line)
2450 {
2451 in_tab_line = FALSE;
2452 return FALSE;
2453 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 }
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 /*
2458 * CTRL right mouse button does CTRL-T
2459 */
2460 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2461 {
2462 if (State & INSERT)
2463 stuffcharReadbuff(Ctrl_O);
2464 if (count > 1)
2465 stuffnumReadbuff(count);
2466 stuffcharReadbuff(Ctrl_T);
2467 got_click = FALSE; /* ignore drag&release now */
2468 return FALSE;
2469 }
2470
2471 /*
2472 * CTRL only works with left mouse button
2473 */
2474 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2475 return FALSE;
2476
2477 /*
2478 * When a modifier is down, ignore drag and release events, as well as
2479 * multiple clicks and the middle mouse button.
2480 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2481 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002482 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2483 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 && (!is_click
2485 || (mod_mask & MOD_MASK_MULTI_CLICK)
2486 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002487 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 && mouse_model_popup()
2489 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002490 && !((mod_mask & MOD_MASK_ALT)
2491 && !mouse_model_popup()
2492 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 )
2494 return FALSE;
2495
2496 /*
2497 * If the button press was used as the movement command for an operator
2498 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2499 * drag/release events.
2500 */
2501 if (!is_click && which_button == MOUSE_MIDDLE)
2502 return FALSE;
2503
2504 if (oap != NULL)
2505 regname = oap->regname;
2506 else
2507 regname = 0;
2508
2509 /*
2510 * Middle mouse button does a 'put' of the selected text
2511 */
2512 if (which_button == MOUSE_MIDDLE)
2513 {
2514 if (State == NORMAL)
2515 {
2516 /*
2517 * If an operator was pending, we don't know what the user wanted
2518 * to do. Go back to normal mode: Clear the operator and beep().
2519 */
2520 if (oap != NULL && oap->op_type != OP_NOP)
2521 {
2522 clearopbeep(oap);
2523 return FALSE;
2524 }
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 /*
2527 * If visual was active, yank the highlighted text and put it
2528 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002529 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 */
2531 if (VIsual_active)
2532 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002533 if (VIsual_select)
2534 {
2535 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002536 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002537 }
2538 else
2539 {
2540 stuffcharReadbuff('y');
2541 stuffcharReadbuff(K_MIDDLEMOUSE);
2542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 do_always = TRUE; /* ignore 'mouse' setting next time */
2544 return FALSE;
2545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /*
2547 * The rest is below jump_to_mouse()
2548 */
2549 }
2550
2551 else if ((State & INSERT) == 0)
2552 return FALSE;
2553
2554 /*
2555 * Middle click in insert mode doesn't move the mouse, just insert the
2556 * contents of a register. '.' register is special, can't insert that
2557 * with do_put().
2558 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2559 * happens for the GUI).
2560 */
2561 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2562 {
2563 if (regname == '.')
2564 insert_reg(regname, TRUE);
2565 else
2566 {
2567#ifdef FEAT_CLIPBOARD
2568 if (clip_star.available && regname == 0)
2569 regname = '*';
2570#endif
2571 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2572 insert_reg(regname, TRUE);
2573 else
2574 {
2575 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2576
2577 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2578 AppendCharToRedobuff(Ctrl_R);
2579 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2580 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2581 }
2582 }
2583 return FALSE;
2584 }
2585 }
2586
2587 /* When dragging or button-up stay in the same window. */
2588 if (!is_click)
2589 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2590
2591 start_visual.lnum = 0;
2592
Bram Moolenaarf740b292006-02-16 22:11:02 +00002593 /* Check for clicking in the tab page line. */
2594 if (mouse_row == 0 && firstwin->w_winrow > 0)
2595 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002596 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002597 {
2598 if (in_tab_line)
2599 {
2600 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002601 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2602 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002603 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002604 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002605 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002606
Bram Moolenaarf740b292006-02-16 22:11:02 +00002607 /* click in a tab selects that tab page */
2608 if (is_click
2609# ifdef FEAT_CMDWIN
2610 && cmdwin_type == 0
2611# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002612 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002613 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002614 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002615 c1 = TabPageIdxs[mouse_col];
2616 if (c1 >= 0)
2617 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002618 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2619 {
2620 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002621 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002622 tabpage_new();
2623 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2624 }
2625 else
2626 {
2627 /* Go to specified tab page, or next one if not clicking
2628 * on a label. */
2629 goto_tabpage(c1);
2630
2631 /* It's like clicking on the status line of a window. */
2632 if (curwin != old_curwin)
2633 end_visual_mode();
2634 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002635 }
2636 else if (c1 < 0)
2637 {
2638 tabpage_T *tp;
2639
2640 /* Close the current or specified tab page. */
2641 if (c1 == -999)
2642 tp = curtab;
2643 else
2644 tp = find_tabpage(-c1);
2645 if (tp == curtab)
2646 {
2647 if (first_tabpage->tp_next != NULL)
2648 tabpage_close(FALSE);
2649 }
2650 else if (tp != NULL)
2651 tabpage_close_other(tp, FALSE);
2652 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002653 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002654 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002655 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002656 else if (is_drag && in_tab_line)
2657 {
2658 c1 = TabPageIdxs[mouse_col];
2659 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2660 return FALSE;
2661 }
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 /*
2664 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2665 * right button up -> pop-up menu
2666 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002667 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 */
2669 if (mouse_model_popup())
2670 {
2671 if (which_button == MOUSE_RIGHT
2672 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2673 {
2674 /*
2675 * NOTE: Ignore right button down and drag mouse events.
2676 * Windows only shows the popup menu on the button up event.
2677 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002678#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2679 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (!is_click)
2681 return FALSE;
2682#endif
2683#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2684 if (is_click || is_drag)
2685 return FALSE;
2686#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002687#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2689 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2690 if (gui.in_use)
2691 {
2692 jump_flags = 0;
2693 if (STRCMP(p_mousem, "popup_setpos") == 0)
2694 {
2695 /* First set the cursor position before showing the popup
2696 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if (VIsual_active)
2698 {
2699 pos_T m_pos;
2700
2701 /*
2702 * set MOUSE_MAY_STOP_VIS if we are outside the
2703 * selection or the current window (might have false
2704 * negative here)
2705 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002706 if (mouse_row < curwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 || mouse_row
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002708 > (curwin->w_winrow + curwin->w_height))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 jump_flags = MOUSE_MAY_STOP_VIS;
2710 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2711 jump_flags = MOUSE_MAY_STOP_VIS;
2712 else
2713 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002714 if ((LT_POS(curwin->w_cursor, VIsual)
2715 && (LT_POS(m_pos, curwin->w_cursor)
2716 || LT_POS(VIsual, m_pos)))
2717 || (LT_POS(VIsual, curwin->w_cursor)
2718 && (LT_POS(m_pos, VIsual)
2719 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
2721 jump_flags = MOUSE_MAY_STOP_VIS;
2722 }
2723 else if (VIsual_mode == Ctrl_V)
2724 {
2725 getvcols(curwin, &curwin->w_cursor, &VIsual,
2726 &leftcol, &rightcol);
2727 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2728 if (m_pos.col < leftcol || m_pos.col > rightcol)
2729 jump_flags = MOUSE_MAY_STOP_VIS;
2730 }
2731 }
2732 }
2733 else
2734 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
2736 if (jump_flags)
2737 {
2738 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002739 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 setcursor();
2741 out_flush(); /* Update before showing popup menu */
2742 }
2743# ifdef FEAT_MENU
2744 gui_show_popupmenu();
2745# endif
2746 return (jump_flags & CURSOR_MOVED) != 0;
2747 }
2748 else
2749 return FALSE;
2750#else
2751 return FALSE;
2752#endif
2753 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002754 if (which_button == MOUSE_LEFT
2755 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
2757 which_button = MOUSE_RIGHT;
2758 mod_mask &= ~MOD_MASK_SHIFT;
2759 }
2760 }
2761
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if ((State & (NORMAL | INSERT))
2763 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2764 {
2765 if (which_button == MOUSE_LEFT)
2766 {
2767 if (is_click)
2768 {
2769 /* stop Visual mode for a left click in a window, but not when
2770 * on a status line */
2771 if (VIsual_active)
2772 jump_flags |= MOUSE_MAY_STOP_VIS;
2773 }
2774 else if (mouse_has(MOUSE_VISUAL))
2775 jump_flags |= MOUSE_MAY_VIS;
2776 }
2777 else if (which_button == MOUSE_RIGHT)
2778 {
2779 if (is_click && VIsual_active)
2780 {
2781 /*
2782 * Remember the start and end of visual before moving the
2783 * cursor.
2784 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002785 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
2787 start_visual = curwin->w_cursor;
2788 end_visual = VIsual;
2789 }
2790 else
2791 {
2792 start_visual = VIsual;
2793 end_visual = curwin->w_cursor;
2794 }
2795 }
2796 jump_flags |= MOUSE_FOCUS;
2797 if (mouse_has(MOUSE_VISUAL))
2798 jump_flags |= MOUSE_MAY_VIS;
2799 }
2800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
2802 /*
2803 * If an operator is pending, ignore all drags and releases until the
2804 * next mouse click.
2805 */
2806 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2807 {
2808 got_click = FALSE;
2809 oap->motion_type = MCHAR;
2810 }
2811
2812 /* When releasing the button let jump_to_mouse() know. */
2813 if (!is_click && !is_drag)
2814 jump_flags |= MOUSE_RELEASED;
2815
2816 /*
2817 * JUMP!
2818 */
2819 jump_flags = jump_to_mouse(jump_flags,
2820 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002821
2822#ifdef FEAT_MENU
2823 /* A click in the window toolbar has no side effects. */
2824 if (jump_flags & MOUSE_WINBAR)
2825 return FALSE;
2826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 moved = (jump_flags & CURSOR_MOVED);
2828 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
2831#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002832 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2834 {
2835 int key = KEY2TERMCAP1(c);
2836
2837 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2838 || key == (int)KE_RIGHTRELEASE)
2839 netbeans_button_release(which_button);
2840 }
2841#endif
2842
2843 /* When jumping to another window, clear a pending operator. That's a bit
2844 * friendlier than beeping and not jumping to that window. */
2845 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2846 clearop(oap);
2847
2848#ifdef FEAT_FOLDING
2849 if (mod_mask == 0
2850 && !is_drag
2851 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2852 && which_button == MOUSE_LEFT)
2853 {
2854 /* open or close a fold at this line */
2855 if (jump_flags & MOUSE_FOLD_OPEN)
2856 openFold(curwin->w_cursor.lnum, 1L);
2857 else
2858 closeFold(curwin->w_cursor.lnum, 1L);
2859 /* don't move the cursor if still in the same window */
2860 if (curwin == old_curwin)
2861 curwin->w_cursor = save_cursor;
2862 }
2863#endif
2864
2865#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2866 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2867 {
2868 clip_modeless(which_button, is_click, is_drag);
2869 return FALSE;
2870 }
2871#endif
2872
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002874 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 if (VIsual_active && is_drag && p_so)
2876 {
2877 /* In the very first line, allow scrolling one line */
2878 if (mouse_row == 0)
2879 mouse_dragging = 2;
2880 else
2881 mouse_dragging = 1;
2882 }
2883
2884 /* When dragging the mouse above the window, scroll down. */
2885 if (is_drag && mouse_row < 0 && !in_status_line)
2886 {
2887 scroll_redraw(FALSE, 1L);
2888 mouse_row = 0;
2889 }
2890
2891 if (start_visual.lnum) /* right click in visual mode */
2892 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002893 /* When ALT is pressed make Visual mode blockwise. */
2894 if (mod_mask & MOD_MASK_ALT)
2895 VIsual_mode = Ctrl_V;
2896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 /*
2898 * In Visual-block mode, divide the area in four, pick up the corner
2899 * that is in the quarter that the cursor is in.
2900 */
2901 if (VIsual_mode == Ctrl_V)
2902 {
2903 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2904 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2905 end_visual.col = leftcol;
2906 else
2907 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002908 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 end_visual.lnum = start_visual.lnum;
2911
2912 /* move VIsual to the right column */
2913 start_visual = curwin->w_cursor; /* save the cursor pos */
2914 curwin->w_cursor = end_visual;
2915 coladvance(end_visual.col);
2916 VIsual = curwin->w_cursor;
2917 curwin->w_cursor = start_visual; /* restore the cursor */
2918 }
2919 else
2920 {
2921 /*
2922 * If the click is before the start of visual, change the start.
2923 * If the click is after the end of visual, change the end. If
2924 * the click is inside the visual, change the closest side.
2925 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002926 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002928 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 VIsual = start_visual;
2930 else
2931 {
2932 /* In the same line, compare column number */
2933 if (end_visual.lnum == start_visual.lnum)
2934 {
2935 if (curwin->w_cursor.col - start_visual.col >
2936 end_visual.col - curwin->w_cursor.col)
2937 VIsual = start_visual;
2938 else
2939 VIsual = end_visual;
2940 }
2941
2942 /* In different lines, compare line number */
2943 else
2944 {
2945 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2946 (end_visual.lnum - curwin->w_cursor.lnum);
2947
2948 if (diff > 0) /* closest to end */
2949 VIsual = start_visual;
2950 else if (diff < 0) /* closest to start */
2951 VIsual = end_visual;
2952 else /* in the middle line */
2953 {
2954 if (curwin->w_cursor.col <
2955 (start_visual.col + end_visual.col) / 2)
2956 VIsual = end_visual;
2957 else
2958 VIsual = start_visual;
2959 }
2960 }
2961 }
2962 }
2963 }
2964 /*
2965 * If Visual mode started in insert mode, execute "CTRL-O"
2966 */
2967 else if ((State & INSERT) && VIsual_active)
2968 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
2970 /*
2971 * Middle mouse click: Put text before cursor.
2972 */
2973 if (which_button == MOUSE_MIDDLE)
2974 {
2975#ifdef FEAT_CLIPBOARD
2976 if (clip_star.available && regname == 0)
2977 regname = '*';
2978#endif
2979 if (yank_register_mline(regname))
2980 {
2981 if (mouse_past_bottom)
2982 dir = FORWARD;
2983 }
2984 else if (mouse_past_eol)
2985 dir = FORWARD;
2986
2987 if (fixindent)
2988 {
2989 c1 = (dir == BACKWARD) ? '[' : ']';
2990 c2 = 'p';
2991 }
2992 else
2993 {
2994 c1 = (dir == FORWARD) ? 'p' : 'P';
2995 c2 = NUL;
2996 }
2997 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2998
2999 /*
3000 * Remember where the paste started, so in edit() Insstart can be set
3001 * to this position
3002 */
3003 if (restart_edit != 0)
3004 where_paste_started = curwin->w_cursor;
3005 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3006 }
3007
Bram Moolenaar4033c552017-09-16 20:54:51 +02003008#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 /*
3010 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3011 * error under the mouse pointer.
3012 */
3013 else if (((mod_mask & MOD_MASK_CTRL)
3014 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3015 && bt_quickfix(curbuf))
3016 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003017 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003018 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003019 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003020 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 got_click = FALSE; /* ignore drag&release now */
3022 }
3023#endif
3024
3025 /*
3026 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3027 * under the mouse pointer.
3028 */
3029 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3030 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3031 {
3032 if (State & INSERT)
3033 stuffcharReadbuff(Ctrl_O);
3034 stuffcharReadbuff(Ctrl_RSB);
3035 got_click = FALSE; /* ignore drag&release now */
3036 }
3037
3038 /*
3039 * Shift-Mouse click searches for the next occurrence of the word under
3040 * the mouse pointer
3041 */
3042 else if ((mod_mask & MOD_MASK_SHIFT))
3043 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003044 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 stuffcharReadbuff(Ctrl_O);
3046 if (which_button == MOUSE_LEFT)
3047 stuffcharReadbuff('*');
3048 else /* MOUSE_RIGHT */
3049 stuffcharReadbuff('#');
3050 }
3051
3052 /* Handle double clicks, unless on status line */
3053 else if (in_status_line)
3054 {
3055#ifdef FEAT_MOUSESHAPE
3056 if ((is_drag || is_click) && !drag_status_line)
3057 {
3058 drag_status_line = TRUE;
3059 update_mouseshape(-1);
3060 }
3061#endif
3062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 else if (in_sep_line)
3064 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003065#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 if ((is_drag || is_click) && !drag_sep_line)
3067 {
3068 drag_sep_line = TRUE;
3069 update_mouseshape(-1);
3070 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003071#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3074 && mouse_has(MOUSE_VISUAL))
3075 {
3076 if (is_click || !VIsual_active)
3077 {
3078 if (VIsual_active)
3079 orig_cursor = VIsual;
3080 else
3081 {
3082 check_visual_highlight();
3083 VIsual = curwin->w_cursor;
3084 orig_cursor = VIsual;
3085 VIsual_active = TRUE;
3086 VIsual_reselect = TRUE;
3087 /* start Select mode if 'selectmode' contains "mouse" */
3088 may_start_select('o');
3089 setmouse();
3090 }
3091 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003092 {
3093 /* Double click with ALT pressed makes it blockwise. */
3094 if (mod_mask & MOD_MASK_ALT)
3095 VIsual_mode = Ctrl_V;
3096 else
3097 VIsual_mode = 'v';
3098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3100 VIsual_mode = 'V';
3101 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3102 VIsual_mode = Ctrl_V;
3103#ifdef FEAT_CLIPBOARD
3104 /* Make sure the clipboard gets updated. Needed because start and
3105 * end may still be the same, and the selection needs to be owned */
3106 clip_star.vmode = NUL;
3107#endif
3108 }
3109 /*
3110 * A double click selects a word or a block.
3111 */
3112 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3113 {
3114 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003115 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 if (is_click)
3118 {
3119 /* If the character under the cursor (skipping white space) is
3120 * not a word character, try finding a match and select a (),
3121 * {}, [], #if/#endif, etc. block. */
3122 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003123 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 inc(&end_visual);
3125 if (oap != NULL)
3126 oap->motion_type = MCHAR;
3127 if (oap != NULL
3128 && VIsual_mode == 'v'
3129 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003130 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 && (pos = findmatch(oap, NUL)) != NULL)
3132 {
3133 curwin->w_cursor = *pos;
3134 if (oap->motion_type == MLINE)
3135 VIsual_mode = 'V';
3136 else if (*p_sel == 'e')
3137 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003138 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 ++VIsual.col;
3140 else
3141 ++curwin->w_cursor.col;
3142 }
3143 }
3144 }
3145
3146 if (pos == NULL && (is_click || is_drag))
3147 {
3148 /* When not found a match or when dragging: extend to include
3149 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003150 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 {
3152 find_start_of_word(&curwin->w_cursor);
3153 find_end_of_word(&VIsual);
3154 }
3155 else
3156 {
3157 find_start_of_word(&VIsual);
3158 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3159#ifdef FEAT_MBYTE
3160 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003161 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162#else
3163 ++curwin->w_cursor.col;
3164#endif
3165 find_end_of_word(&curwin->w_cursor);
3166 }
3167 }
3168 curwin->w_set_curswant = TRUE;
3169 }
3170 if (is_click)
3171 redraw_curbuf_later(INVERTED); /* update the inversion */
3172 }
3173 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003174 {
3175 if (mod_mask & MOD_MASK_ALT)
3176 VIsual_mode = Ctrl_V;
3177 else
3178 VIsual_mode = 'v';
3179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003182 if ((!VIsual_active && old_active && mode_displayed)
3183 || (VIsual_active && p_smd && msg_silent == 0
3184 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 return moved;
3188}
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190/*
3191 * Move "pos" back to the start of the word it's in.
3192 */
3193 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003194find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
3196 char_u *line;
3197 int cclass;
3198 int col;
3199
3200 line = ml_get(pos->lnum);
3201 cclass = get_mouse_class(line + pos->col);
3202
3203 while (pos->col > 0)
3204 {
3205 col = pos->col - 1;
3206#ifdef FEAT_MBYTE
3207 col -= (*mb_head_off)(line, line + col);
3208#endif
3209 if (get_mouse_class(line + col) != cclass)
3210 break;
3211 pos->col = col;
3212 }
3213}
3214
3215/*
3216 * Move "pos" forward to the end of the word it's in.
3217 * When 'selection' is "exclusive", the position is just after the word.
3218 */
3219 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003220find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 char_u *line;
3223 int cclass;
3224 int col;
3225
3226 line = ml_get(pos->lnum);
3227 if (*p_sel == 'e' && pos->col > 0)
3228 {
3229 --pos->col;
3230#ifdef FEAT_MBYTE
3231 pos->col -= (*mb_head_off)(line, line + pos->col);
3232#endif
3233 }
3234 cclass = get_mouse_class(line + pos->col);
3235 while (line[pos->col] != NUL)
3236 {
3237#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003238 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239#else
3240 col = pos->col + 1;
3241#endif
3242 if (get_mouse_class(line + col) != cclass)
3243 {
3244 if (*p_sel == 'e')
3245 pos->col = col;
3246 break;
3247 }
3248 pos->col = col;
3249 }
3250}
3251
3252/*
3253 * Get class of a character for selection: same class means same word.
3254 * 0: blank
3255 * 1: punctuation groups
3256 * 2: normal word character
3257 * >2: multi-byte word character.
3258 */
3259 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003260get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262 int c;
3263
3264#ifdef FEAT_MBYTE
3265 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3266 return mb_get_class(p);
3267#endif
3268
3269 c = *p;
3270 if (c == ' ' || c == '\t')
3271 return 0;
3272
3273 if (vim_iswordc(c))
3274 return 2;
3275
3276 /*
3277 * There are a few special cases where we want certain combinations of
3278 * characters to be considered as a single word. These are things like
3279 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003280 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 */
3282 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3283 return 1;
3284 return c;
3285}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286#endif /* FEAT_MOUSE */
3287
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288/*
3289 * Check if highlighting for visual mode is possible, give a warning message
3290 * if not.
3291 */
3292 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003293check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 static int did_check = FALSE;
3296
3297 if (full_screen)
3298 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003299 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 MSG(_("Warning: terminal cannot highlight"));
3301 did_check = TRUE;
3302 }
3303}
3304
3305/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003306 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 * This function should ALWAYS be called to end Visual mode, except from
3308 * do_pending_operator().
3309 */
3310 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003311end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313#ifdef FEAT_CLIPBOARD
3314 /*
3315 * If we are using the clipboard, then remember what was selected in case
3316 * we need to paste it somewhere while we still own the selection.
3317 * Only do this when the clipboard is already owned. Don't want to grab
3318 * the selection when hitting ESC.
3319 */
3320 if (clip_star.available && clip_star.owned)
3321 clip_auto_select();
3322#endif
3323
3324 VIsual_active = FALSE;
3325#ifdef FEAT_MOUSE
3326 setmouse();
3327 mouse_dragging = 0;
3328#endif
3329
3330 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003331 curbuf->b_visual.vi_mode = VIsual_mode;
3332 curbuf->b_visual.vi_start = VIsual;
3333 curbuf->b_visual.vi_end = curwin->w_cursor;
3334 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#ifdef FEAT_EVAL
3336 curbuf->b_visual_mode_eval = VIsual_mode;
3337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#ifdef FEAT_VIRTUALEDIT
3339 if (!virtual_active())
3340 curwin->w_cursor.coladd = 0;
3341#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003342 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003344 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345}
3346
3347/*
3348 * Reset VIsual_active and VIsual_reselect.
3349 */
3350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003351reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
3353 if (VIsual_active)
3354 {
3355 end_visual_mode();
3356 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3357 }
3358 VIsual_reselect = FALSE;
3359}
3360
3361/*
3362 * Reset VIsual_active and VIsual_reselect if it's set.
3363 */
3364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003365reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
3367 if (VIsual_active)
3368 {
3369 end_visual_mode();
3370 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3371 VIsual_reselect = FALSE;
3372 }
3373}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375/*
3376 * Check for a balloon-eval special item to include when searching for an
3377 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3378 * Returns TRUE if the character at "*ptr" should be included.
3379 * "dir" is FORWARD or BACKWARD, the direction of searching.
3380 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3381 * "bnp" points to a counter for square brackets.
3382 */
3383 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003384find_is_eval_item(
3385 char_u *ptr,
3386 int *colp,
3387 int *bnp,
3388 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
3390 /* Accept everything inside []. */
3391 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3392 ++*bnp;
3393 if (*bnp > 0)
3394 {
3395 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3396 --*bnp;
3397 return TRUE;
3398 }
3399
3400 /* skip over "s.var" */
3401 if (*ptr == '.')
3402 return TRUE;
3403
3404 /* two-character item: s->var */
3405 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3406 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3407 {
3408 *colp += dir;
3409 return TRUE;
3410 }
3411 return FALSE;
3412}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
3414/*
3415 * Find the identifier under or to the right of the cursor.
3416 * "find_type" can have one of three values:
3417 * FIND_IDENT: find an identifier (keyword)
3418 * FIND_STRING: find any non-white string
3419 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 *
3422 * There are three steps:
3423 * 1. Search forward for the start of an identifier/string. Doesn't move if
3424 * already on one.
3425 * 2. Search backward for the start of this identifier/string.
3426 * This doesn't match the real Vi but I like it a little better and it
3427 * shouldn't bother anyone.
3428 * 3. Search forward to the end of this identifier/string.
3429 * When FIND_IDENT isn't defined, we backup until a blank.
3430 *
3431 * Returns the length of the string, or zero if no string is found.
3432 * If a string is found, a pointer to the string is put in "*string". This
3433 * string is not always NUL terminated.
3434 */
3435 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003436find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3439 curwin->w_cursor.col, string, find_type);
3440}
3441
3442/*
3443 * Like find_ident_under_cursor(), but for any window and any position.
3444 * However: Uses 'iskeyword' from the current window!.
3445 */
3446 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003447find_ident_at_pos(
3448 win_T *wp,
3449 linenr_T lnum,
3450 colnr_T startcol,
3451 char_u **string,
3452 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 char_u *ptr;
3455 int col = 0; /* init to shut up GCC */
3456 int i;
3457#ifdef FEAT_MBYTE
3458 int this_class = 0;
3459 int prev_class;
3460 int prevcol;
3461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 /*
3465 * if i == 0: try to find an identifier
3466 * if i == 1: try to find any non-white string
3467 */
3468 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3469 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3470 {
3471 /*
3472 * 1. skip to start of identifier/string
3473 */
3474 col = startcol;
3475#ifdef FEAT_MBYTE
3476 if (has_mbyte)
3477 {
3478 while (ptr[col] != NUL)
3479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 /* Stop at a ']' to evaluate "a[x]". */
3481 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3482 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 this_class = mb_get_class(ptr + col);
3484 if (this_class != 0 && (i == 1 || this_class != 1))
3485 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003486 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488 }
3489 else
3490#endif
3491 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003492 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 )
3495 ++col;
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 /* When starting on a ']' count it, so that we include the '['. */
3498 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
3500 /*
3501 * 2. Back up to start of identifier/string.
3502 */
3503#ifdef FEAT_MBYTE
3504 if (has_mbyte)
3505 {
3506 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3508 this_class = mb_get_class((char_u *)"a");
3509 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003511 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3514 prev_class = mb_get_class(ptr + prevcol);
3515 if (this_class != prev_class
3516 && (i == 0
3517 || prev_class == 0
3518 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 && (!(find_type & FIND_EVAL)
3520 || prevcol == 0
3521 || !find_is_eval_item(ptr + prevcol, &prevcol,
3522 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 )
3524 break;
3525 col = prevcol;
3526 }
3527
3528 /* If we don't want just any old string, or we've found an
3529 * identifier, stop searching. */
3530 if (this_class > 2)
3531 this_class = 2;
3532 if (!(find_type & FIND_STRING) || this_class == 2)
3533 break;
3534 }
3535 else
3536#endif
3537 {
3538 while (col > 0
3539 && ((i == 0
3540 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003541 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 && (!(find_type & FIND_IDENT)
3543 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 || ((find_type & FIND_EVAL)
3545 && col > 1
3546 && find_is_eval_item(ptr + col - 1, &col,
3547 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 ))
3549 --col;
3550
3551 /* If we don't want just any old string, or we've found an
3552 * identifier, stop searching. */
3553 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3554 break;
3555 }
3556 }
3557
3558 if (ptr[col] == NUL || (i == 0 && (
3559#ifdef FEAT_MBYTE
3560 has_mbyte ? this_class != 2 :
3561#endif
3562 !vim_iswordc(ptr[col]))))
3563 {
3564 /*
3565 * didn't find an identifier or string
3566 */
3567 if (find_type & FIND_STRING)
3568 EMSG(_("E348: No string under cursor"));
3569 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003570 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 return 0;
3572 }
3573 ptr += col;
3574 *string = ptr;
3575
3576 /*
3577 * 3. Find the end if the identifier/string.
3578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 bn = 0;
3580 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 col = 0;
3582#ifdef FEAT_MBYTE
3583 if (has_mbyte)
3584 {
3585 /* Search for point of changing multibyte character class. */
3586 this_class = mb_get_class(ptr);
3587 while (ptr[col] != NUL
3588 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3589 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 || ((find_type & FIND_EVAL)
3591 && col <= (int)startcol
3592 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003594 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
3596 else
3597#endif
3598 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003599 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 || ((find_type & FIND_EVAL)
3601 && col <= (int)startcol
3602 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 )
3604 {
3605 ++col;
3606 }
3607
3608 return col;
3609}
3610
3611/*
3612 * Prepare for redo of a normal command.
3613 */
3614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003615prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
3617 prep_redo(cap->oap->regname, cap->count0,
3618 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3619}
3620
3621/*
3622 * Prepare for redo of any command.
3623 * Note that only the last argument can be a multi-byte char.
3624 */
3625 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003626prep_redo(
3627 int regname,
3628 long num,
3629 int cmd1,
3630 int cmd2,
3631 int cmd3,
3632 int cmd4,
3633 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
3635 ResetRedobuff();
3636 if (regname != 0) /* yank from specified buffer */
3637 {
3638 AppendCharToRedobuff('"');
3639 AppendCharToRedobuff(regname);
3640 }
3641 if (num)
3642 AppendNumberToRedobuff(num);
3643
3644 if (cmd1 != NUL)
3645 AppendCharToRedobuff(cmd1);
3646 if (cmd2 != NUL)
3647 AppendCharToRedobuff(cmd2);
3648 if (cmd3 != NUL)
3649 AppendCharToRedobuff(cmd3);
3650 if (cmd4 != NUL)
3651 AppendCharToRedobuff(cmd4);
3652 if (cmd5 != NUL)
3653 AppendCharToRedobuff(cmd5);
3654}
3655
3656/*
3657 * check for operator active and clear it
3658 *
3659 * return TRUE if operator was active
3660 */
3661 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003662checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
3664 if (oap->op_type == OP_NOP)
3665 return FALSE;
3666 clearopbeep(oap);
3667 return TRUE;
3668}
3669
3670/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003671 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003673 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 */
3675 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003676checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003678 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return FALSE;
3680 clearopbeep(oap);
3681 return TRUE;
3682}
3683
3684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003685clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 oap->op_type = OP_NOP;
3688 oap->regname = 0;
3689 oap->motion_force = NUL;
3690 oap->use_reg_one = FALSE;
3691}
3692
3693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003694clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695{
3696 clearop(oap);
3697 beep_flush();
3698}
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
3701 * Remove the shift modifier from a special key.
3702 */
3703 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003704unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 switch (cap->cmdchar)
3707 {
3708 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3709 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3710 case K_S_UP: cap->cmdchar = K_UP; break;
3711 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3712 case K_S_HOME: cap->cmdchar = K_HOME; break;
3713 case K_S_END: cap->cmdchar = K_END; break;
3714 }
3715 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003718/*
3719 * If the mode is currently displayed clear the command line or update the
3720 * command displayed.
3721 */
3722 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003723may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003724{
3725 if (mode_displayed)
3726 clear_cmdline = TRUE; /* unshow visual mode later */
3727#ifdef FEAT_CMDL_INFO
3728 else
3729 clear_showcmd();
3730#endif
3731}
3732
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3734/*
3735 * Routines for displaying a partly typed command
3736 */
3737
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003738#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739static char_u showcmd_buf[SHOWCMD_BUFLEN];
3740static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3741static int showcmd_is_clear = TRUE;
3742static int showcmd_visual = FALSE;
3743
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003744static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
3746 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003747clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
3749 if (!p_sc)
3750 return;
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 if (VIsual_active && !char_avail())
3753 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003754 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 long lines;
3756 colnr_T leftcol, rightcol;
3757 linenr_T top, bot;
3758
3759 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003760 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
3762 top = VIsual.lnum;
3763 bot = curwin->w_cursor.lnum;
3764 }
3765 else
3766 {
3767 top = curwin->w_cursor.lnum;
3768 bot = VIsual.lnum;
3769 }
3770# ifdef FEAT_FOLDING
3771 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003772 (void)hasFolding(top, &top, NULL);
3773 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774# endif
3775 lines = bot - top + 1;
3776
3777 if (VIsual_mode == Ctrl_V)
3778 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003779# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003780 char_u *saved_sbr = p_sbr;
3781
3782 /* Make 'sbr' empty for a moment to get the correct size. */
3783 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003786# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003787 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3790 (long)(rightcol - leftcol + 1));
3791 }
3792 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3793 sprintf((char *)showcmd_buf, "%ld", lines);
3794 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003795 {
3796 char_u *s, *e;
3797 int l;
3798 int bytes = 0;
3799 int chars = 0;
3800
3801 if (cursor_bot)
3802 {
3803 s = ml_get_pos(&VIsual);
3804 e = ml_get_cursor();
3805 }
3806 else
3807 {
3808 s = ml_get_cursor();
3809 e = ml_get_pos(&VIsual);
3810 }
3811 while ((*p_sel != 'e') ? s <= e : s < e)
3812 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003813# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003814 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003815# else
3816 l = (*s == NUL) ? 0 : 1;
3817# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003818 if (l == 0)
3819 {
3820 ++bytes;
3821 ++chars;
3822 break; /* end of line */
3823 }
3824 bytes += l;
3825 ++chars;
3826 s += l;
3827 }
3828 if (bytes == chars)
3829 sprintf((char *)showcmd_buf, "%d", chars);
3830 else
3831 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3834 showcmd_visual = TRUE;
3835 }
3836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
3838 showcmd_buf[0] = NUL;
3839 showcmd_visual = FALSE;
3840
3841 /* Don't actually display something if there is nothing to clear. */
3842 if (showcmd_is_clear)
3843 return;
3844 }
3845
3846 display_showcmd();
3847}
3848
3849/*
3850 * Add 'c' to string of shown command chars.
3851 * Return TRUE if output has been written (and setcursor() has been called).
3852 */
3853 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003854add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
3856 char_u *p;
3857 int old_len;
3858 int extra_len;
3859 int overflow;
3860#if defined(FEAT_MOUSE)
3861 int i;
3862 static int ignore[] =
3863 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003864# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3866 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003867# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003868 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003869 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3871 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003872 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003874 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 0
3876 };
3877#endif
3878
Bram Moolenaar09df3122006-01-23 22:23:09 +00003879 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return FALSE;
3881
3882 if (showcmd_visual)
3883 {
3884 showcmd_buf[0] = NUL;
3885 showcmd_visual = FALSE;
3886 }
3887
3888#if defined(FEAT_MOUSE)
3889 /* Ignore keys that are scrollbar updates and mouse clicks */
3890 if (IS_SPECIAL(c))
3891 for (i = 0; ignore[i] != 0; ++i)
3892 if (ignore[i] == c)
3893 return FALSE;
3894#endif
3895
3896 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003897 if (*p == ' ')
3898 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 old_len = (int)STRLEN(showcmd_buf);
3900 extra_len = (int)STRLEN(p);
3901 overflow = old_len + extra_len - SHOWCMD_COLS;
3902 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003903 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3904 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 STRCAT(showcmd_buf, p);
3906
3907 if (char_avail())
3908 return FALSE;
3909
3910 display_showcmd();
3911
3912 return TRUE;
3913}
3914
3915 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003916add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917{
3918 if (!add_to_showcmd(c))
3919 setcursor();
3920}
3921
3922/*
3923 * Delete 'len' characters from the end of the shown command.
3924 */
3925 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003926del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
3928 int old_len;
3929
3930 if (!p_sc)
3931 return;
3932
3933 old_len = (int)STRLEN(showcmd_buf);
3934 if (len > old_len)
3935 len = old_len;
3936 showcmd_buf[old_len - len] = NUL;
3937
3938 if (!char_avail())
3939 display_showcmd();
3940}
3941
3942/*
3943 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3944 * something and there is a partial mapping.
3945 */
3946 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003947push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
3949 if (p_sc)
3950 STRCPY(old_showcmd_buf, showcmd_buf);
3951}
3952
3953 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003954pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955{
3956 if (!p_sc)
3957 return;
3958
3959 STRCPY(showcmd_buf, old_showcmd_buf);
3960
3961 display_showcmd();
3962}
3963
3964 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003965display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966{
3967 int len;
3968
3969 cursor_off();
3970
3971 len = (int)STRLEN(showcmd_buf);
3972 if (len == 0)
3973 showcmd_is_clear = TRUE;
3974 else
3975 {
3976 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3977 showcmd_is_clear = FALSE;
3978 }
3979
3980 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003981 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3982 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 */
3984 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3985
3986 setcursor(); /* put cursor back where it belongs */
3987}
3988#endif
3989
3990#ifdef FEAT_SCROLLBIND
3991/*
3992 * When "check" is FALSE, prepare for commands that scroll the window.
3993 * When "check" is TRUE, take care of scroll-binding after the window has
3994 * scrolled. Called from normal_cmd() and edit().
3995 */
3996 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003997do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998{
3999 static win_T *old_curwin = NULL;
4000 static linenr_T old_topline = 0;
4001#ifdef FEAT_DIFF
4002 static int old_topfill = 0;
4003#endif
4004 static buf_T *old_buf = NULL;
4005 static colnr_T old_leftcol = 0;
4006
4007 if (check && curwin->w_p_scb)
4008 {
4009 /* If a ":syncbind" command was just used, don't scroll, only reset
4010 * the values. */
4011 if (did_syncbind)
4012 did_syncbind = FALSE;
4013 else if (curwin == old_curwin)
4014 {
4015 /*
4016 * Synchronize other windows, as necessary according to
4017 * 'scrollbind'. Don't do this after an ":edit" command, except
4018 * when 'diff' is set.
4019 */
4020 if ((curwin->w_buffer == old_buf
4021#ifdef FEAT_DIFF
4022 || curwin->w_p_diff
4023#endif
4024 )
4025 && (curwin->w_topline != old_topline
4026#ifdef FEAT_DIFF
4027 || curwin->w_topfill != old_topfill
4028#endif
4029 || curwin->w_leftcol != old_leftcol))
4030 {
4031 check_scrollbind(curwin->w_topline - old_topline,
4032 (long)(curwin->w_leftcol - old_leftcol));
4033 }
4034 }
4035 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4036 {
4037 /*
4038 * When switching between windows, make sure that the relative
4039 * vertical offset is valid for the new window. The relative
4040 * offset is invalid whenever another 'scrollbind' window has
4041 * scrolled to a point that would force the current window to
4042 * scroll past the beginning or end of its buffer. When the
4043 * resync is performed, some of the other 'scrollbind' windows may
4044 * need to jump so that the current window's relative position is
4045 * visible on-screen.
4046 */
4047 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4048 }
4049 curwin->w_scbind_pos = curwin->w_topline;
4050 }
4051
4052 old_curwin = curwin;
4053 old_topline = curwin->w_topline;
4054#ifdef FEAT_DIFF
4055 old_topfill = curwin->w_topfill;
4056#endif
4057 old_buf = curwin->w_buffer;
4058 old_leftcol = curwin->w_leftcol;
4059}
4060
4061/*
4062 * Synchronize any windows that have "scrollbind" set, based on the
4063 * number of rows by which the current window has changed
4064 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4065 */
4066 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004067check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
4069 int want_ver;
4070 int want_hor;
4071 win_T *old_curwin = curwin;
4072 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 int old_VIsual_select = VIsual_select;
4074 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 colnr_T tgt_leftcol = curwin->w_leftcol;
4076 long topline;
4077 long y;
4078
4079 /*
4080 * check 'scrollopt' string for vertical and horizontal scroll options
4081 */
4082 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4083#ifdef FEAT_DIFF
4084 want_ver |= old_curwin->w_p_diff;
4085#endif
4086 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4087
4088 /*
4089 * loop through the scrollbound windows and scroll accordingly
4090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004092 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
4094 curbuf = curwin->w_buffer;
4095 /* skip original window and windows with 'noscrollbind' */
4096 if (curwin != old_curwin && curwin->w_p_scb)
4097 {
4098 /*
4099 * do the vertical scroll
4100 */
4101 if (want_ver)
4102 {
4103#ifdef FEAT_DIFF
4104 if (old_curwin->w_p_diff && curwin->w_p_diff)
4105 {
4106 diff_set_topline(old_curwin, curwin);
4107 }
4108 else
4109#endif
4110 {
4111 curwin->w_scbind_pos += topline_diff;
4112 topline = curwin->w_scbind_pos;
4113 if (topline > curbuf->b_ml.ml_line_count)
4114 topline = curbuf->b_ml.ml_line_count;
4115 if (topline < 1)
4116 topline = 1;
4117
4118 y = topline - curwin->w_topline;
4119 if (y > 0)
4120 scrollup(y, FALSE);
4121 else
4122 scrolldown(-y, FALSE);
4123 }
4124
4125 redraw_later(VALID);
4126 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129
4130 /*
4131 * do the horizontal scroll
4132 */
4133 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4134 {
4135 curwin->w_leftcol = tgt_leftcol;
4136 leftcol_changed();
4137 }
4138 }
4139 }
4140
4141 /*
4142 * reset current-window
4143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 VIsual_select = old_VIsual_select;
4145 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 curwin = old_curwin;
4147 curbuf = old_curbuf;
4148}
4149#endif /* #ifdef FEAT_SCROLLBIND */
4150
4151/*
4152 * Command character that's ignored.
4153 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004154 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004157nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004159 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160}
4161
4162/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004163 * Command character that doesn't do anything, but unlike nv_ignore() does
4164 * start edit(). Used for "startinsert" executed while starting up.
4165 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004167nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004168{
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * Command character doesn't exist.
4173 */
4174 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004175nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
4177 clearopbeep(cap->oap);
4178}
4179
4180/*
4181 * <Help> and <F1> commands.
4182 */
4183 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004184nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185{
4186 if (!checkclearopq(cap->oap))
4187 ex_help(NULL);
4188}
4189
4190/*
4191 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4192 */
4193 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004194nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
Bram Moolenaard79e5502016-01-10 22:13:02 +01004196 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004197 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004198 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004199 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4200 op_addsub(cap->oap, cap->count1, cap->arg);
4201 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004202 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004203 else if (VIsual_active)
4204 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004205 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004206 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207}
4208
4209/*
4210 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4211 */
4212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004213nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214{
4215 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004216 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004217 if (mod_mask & MOD_MASK_CTRL)
4218 {
4219 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4220 if (cap->arg == BACKWARD)
4221 goto_tabpage(-(int)cap->count1);
4222 else
4223 goto_tabpage((int)cap->count0);
4224 }
4225 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004226 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228}
4229
4230/*
4231 * Implementation of "gd" and "gD" command.
4232 */
4233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004234nv_gd(
4235 oparg_T *oap,
4236 int nchar,
4237 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
4239 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 char_u *ptr;
4241
Bram Moolenaard9d30582005-05-18 22:10:28 +00004242 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004243 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4244 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004246#ifdef FEAT_FOLDING
4247 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4248 foldOpenCursor();
4249#endif
4250}
4251
4252/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004253 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4254 * otherwise.
4255 */
4256 static int
4257is_ident(char_u *line, int offset)
4258{
4259 int i;
4260 int incomment = FALSE;
4261 int instring = 0;
4262 int prev = 0;
4263
4264 for (i = 0; i < offset && line[i] != NUL; i++)
4265 {
4266 if (instring != 0)
4267 {
4268 if (prev != '\\' && line[i] == instring)
4269 instring = 0;
4270 }
4271 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4272 {
4273 instring = line[i];
4274 }
4275 else
4276 {
4277 if (incomment)
4278 {
4279 if (prev == '*' && line[i] == '/')
4280 incomment = FALSE;
4281 }
4282 else if (prev == '/' && line[i] == '*')
4283 {
4284 incomment = TRUE;
4285 }
4286 else if (prev == '/' && line[i] == '/')
4287 {
4288 return FALSE;
4289 }
4290 }
4291
4292 prev = line[i];
4293 }
4294
4295 return incomment == FALSE && instring == 0;
4296}
4297
4298/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004299 * Search for variable declaration of "ptr[len]".
4300 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4301 * current file ("gD").
4302 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004303 * Return FAIL when not found.
4304 */
4305 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004306find_decl(
4307 char_u *ptr,
4308 int len,
4309 int locally,
4310 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004311 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312{
4313 char_u *pat;
4314 pos_T old_pos;
4315 pos_T par_pos;
4316 pos_T found_pos;
4317 int t;
4318 int save_p_ws;
4319 int save_p_scs;
4320 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004321 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004322 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004323 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004324
4325 if ((pat = alloc(len + 7)) == NULL)
4326 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004327
4328 /* Put "\V" before the pattern to avoid that the special meaning of "."
4329 * and "~" causes trouble. */
4330 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4331 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 old_pos = curwin->w_cursor;
4333 save_p_ws = p_ws;
4334 save_p_scs = p_scs;
4335 p_ws = FALSE; /* don't wrap around end of file now */
4336 p_scs = FALSE; /* don't switch ignorecase off now */
4337
4338 /*
4339 * With "gD" go to line 1.
4340 * With "gd" Search back for the start of the current function, then go
4341 * back until a blank line. If this fails go to line 1.
4342 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004343 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
4345 setpcmark(); /* Set in findpar() otherwise */
4346 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004347 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 else
4350 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004351 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4353 --curwin->w_cursor.lnum;
4354 }
4355 curwin->w_cursor.col = 0;
4356
4357 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004358 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004359 for (;;)
4360 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004361 valid = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004362 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004363 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004364 if (curwin->w_cursor.lnum >= old_pos.lnum)
4365 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004366
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004367 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004368 {
4369 pos_T *pos;
4370
4371 /* Check that the block the match is in doesn't end before the
4372 * position where we started the search from. */
4373 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4374 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4375 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004376 {
4377 /* There can't be a useful match before the end of this block.
4378 * Skip to the end. */
4379 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004380 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004381 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004382 }
4383
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004384 if (t == FAIL)
4385 {
4386 /* If we previously found a valid position, use it. */
4387 if (found_pos.lnum != 0)
4388 {
4389 curwin->w_cursor = found_pos;
4390 t = OK;
4391 }
4392 break;
4393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004395 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004396 {
4397 /* Ignore this line, continue at start of next line. */
4398 ++curwin->w_cursor.lnum;
4399 curwin->w_cursor.col = 0;
4400 continue;
4401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004403 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4404
4405 /* If the current position is not a valid identifier and a previous
4406 * match is present, favor that one instead. */
4407 if (!valid && found_pos.lnum != 0)
4408 {
4409 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004410 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004411 }
4412
4413 /* Global search: use first valid match found */
4414 if (valid && !locally)
4415 break;
4416 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004417 {
4418 /* If we previously found a valid position, use it. */
4419 if (found_pos.lnum != 0)
4420 curwin->w_cursor = found_pos;
4421 break;
4422 }
4423
Bram Moolenaar226630a2016-10-08 19:21:31 +02004424 /* For finding a local variable and the match is before the "{" or
4425 * inside a comment, continue searching. For K&R style function
4426 * declarations this skips the function header without types. */
4427 if (!valid)
4428 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004429 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004430 }
4431 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004432 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004433 /* Remove SEARCH_START from flags to avoid getting stuck at one
4434 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004435 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004437
4438 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004440 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 curwin->w_cursor = old_pos;
4442 }
4443 else
4444 {
4445 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 /* "n" searches forward now */
4447 reset_search_dir();
4448 }
4449
4450 vim_free(pat);
4451 p_ws = save_p_ws;
4452 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004453
4454 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455}
4456
4457/*
4458 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4459 * lines rather than lines in the file.
4460 * 'dist' must be positive.
4461 *
4462 * Return OK if able to move cursor, FAIL otherwise.
4463 */
4464 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004465nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466{
4467 int linelen = linetabsize(ml_get_curline());
4468 int retval = OK;
4469 int atend = FALSE;
4470 int n;
4471 int col_off1; /* margin offset for first screen line */
4472 int col_off2; /* margin offset for wrapped screen line */
4473 int width1; /* text width for first screen line */
4474 int width2; /* test width for wrapped screen line */
4475
4476 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004477 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 col_off1 = curwin_col_off();
4480 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004481 width1 = curwin->w_width - col_off1;
4482 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004483 if (width2 == 0)
4484 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 /*
4489 * Instead of sticking at the last character of the buffer line we
4490 * try to stick in the last column of the screen.
4491 */
4492 if (curwin->w_curswant == MAXCOL)
4493 {
4494 atend = TRUE;
4495 validate_virtcol();
4496 if (width1 <= 0)
4497 curwin->w_curswant = 0;
4498 else
4499 {
4500 curwin->w_curswant = width1 - 1;
4501 if (curwin->w_virtcol > curwin->w_curswant)
4502 curwin->w_curswant += ((curwin->w_virtcol
4503 - curwin->w_curswant - 1) / width2 + 1) * width2;
4504 }
4505 }
4506 else
4507 {
4508 if (linelen > width1)
4509 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4510 else
4511 n = width1;
4512 if (curwin->w_curswant > (colnr_T)n + 1)
4513 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4514 * width2;
4515 }
4516
4517 while (dist--)
4518 {
4519 if (dir == BACKWARD)
4520 {
4521 if ((long)curwin->w_curswant >= width2)
4522 /* move back within line */
4523 curwin->w_curswant -= width2;
4524 else
4525 {
4526 /* to previous line */
4527 if (curwin->w_cursor.lnum == 1)
4528 {
4529 retval = FAIL;
4530 break;
4531 }
4532 --curwin->w_cursor.lnum;
4533#ifdef FEAT_FOLDING
4534 /* Move to the start of a closed fold. Don't do that when
4535 * 'foldopen' contains "all": it will open in a moment. */
4536 if (!(fdo_flags & FDO_ALL))
4537 (void)hasFolding(curwin->w_cursor.lnum,
4538 &curwin->w_cursor.lnum, NULL);
4539#endif
4540 linelen = linetabsize(ml_get_curline());
4541 if (linelen > width1)
4542 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4543 + 1) * width2;
4544 }
4545 }
4546 else /* dir == FORWARD */
4547 {
4548 if (linelen > width1)
4549 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4550 else
4551 n = width1;
4552 if (curwin->w_curswant + width2 < (colnr_T)n)
4553 /* move forward within line */
4554 curwin->w_curswant += width2;
4555 else
4556 {
4557 /* to next line */
4558#ifdef FEAT_FOLDING
4559 /* Move to the end of a closed fold. */
4560 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4561 &curwin->w_cursor.lnum);
4562#endif
4563 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4564 {
4565 retval = FAIL;
4566 break;
4567 }
4568 curwin->w_cursor.lnum++;
4569 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004570 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
4572 }
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004576 if (virtual_active() && atend)
4577 coladvance(MAXCOL);
4578 else
4579 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
4581#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4582 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4583 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004584 colnr_T virtcol;
4585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 /*
4587 * Check for landing on a character that got split at the end of the
4588 * last line. We want to advance a screenline, not end up in the same
4589 * screenline or move two screenlines.
4590 */
4591 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004592 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004593# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004594 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4595 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004596# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004597
4598 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 && (curwin->w_curswant < (colnr_T)width1
4600 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4601 : ((curwin->w_curswant - width1) % width2
4602 > (colnr_T)width2 / 2)))
4603 --curwin->w_cursor.col;
4604 }
4605#endif
4606
4607 if (atend)
4608 curwin->w_curswant = MAXCOL; /* stick in the last column */
4609
4610 return retval;
4611}
4612
4613#ifdef FEAT_MOUSE
4614/*
4615 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4616 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004617 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4618 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 */
4620 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004621nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004623 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004625 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
4627 int row, col;
4628
4629 row = mouse_row;
4630 col = mouse_col;
4631
4632 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004633 wp = mouse_find_win(&row, &col);
4634 if (wp == NULL)
4635 return;
4636 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 curbuf = curwin->w_buffer;
4638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004640 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004642# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004643 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004644 /* This window is a terminal window, send the mouse event there.
4645 * Set "typed" to FALSE to avoid an endless loop. */
4646 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004647 else
4648# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004649 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4650 {
4651 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4652 }
4653 else
4654 {
4655 cap->count1 = 3;
4656 cap->count0 = 3;
4657 nv_scroll_line(cap);
4658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004660# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 else
4662 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004663 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4664 if (!curwin->w_p_wrap)
4665 {
4666 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004667
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004668 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004669 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004670 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4671 if (val < 0)
4672 val = 0;
4673
4674 gui_do_horiz_scroll(val, TRUE);
4675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 curwin->w_redr_status = TRUE;
4680
4681 curwin = old_curwin;
4682 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683}
4684
4685/*
4686 * Mouse clicks and drags.
4687 */
4688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004689nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4692}
4693#endif
4694
4695/*
4696 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4697 * cap->arg must be TRUE for CTRL-E.
4698 */
4699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004700nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 if (!checkclearop(cap->oap))
4703 scroll_redraw(cap->arg, cap->count1);
4704}
4705
4706/*
4707 * Scroll "count" lines up or down, and redraw.
4708 */
4709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004710scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 linenr_T prev_topline = curwin->w_topline;
4713#ifdef FEAT_DIFF
4714 int prev_topfill = curwin->w_topfill;
4715#endif
4716 linenr_T prev_lnum = curwin->w_cursor.lnum;
4717
4718 if (up)
4719 scrollup(count, TRUE);
4720 else
4721 scrolldown(count, TRUE);
4722 if (p_so)
4723 {
4724 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4725 * valid, otherwise the screen jumps back at the end of the file. */
4726 cursor_correct();
4727 check_cursor_moved(curwin);
4728 curwin->w_valid |= VALID_TOPLINE;
4729
4730 /* If moved back to where we were, at least move the cursor, otherwise
4731 * we get stuck at one position. Don't move the cursor up if the
4732 * first line of the buffer is already on the screen */
4733 while (curwin->w_topline == prev_topline
4734#ifdef FEAT_DIFF
4735 && curwin->w_topfill == prev_topfill
4736#endif
4737 )
4738 {
4739 if (up)
4740 {
4741 if (curwin->w_cursor.lnum > prev_lnum
4742 || cursor_down(1L, FALSE) == FAIL)
4743 break;
4744 }
4745 else
4746 {
4747 if (curwin->w_cursor.lnum < prev_lnum
4748 || prev_topline == 1L
4749 || cursor_up(1L, FALSE) == FAIL)
4750 break;
4751 }
4752 /* Mark w_topline as valid, otherwise the screen jumps back at the
4753 * end of the file. */
4754 check_cursor_moved(curwin);
4755 curwin->w_valid |= VALID_TOPLINE;
4756 }
4757 }
4758 if (curwin->w_cursor.lnum != prev_lnum)
4759 coladvance(curwin->w_curswant);
4760 redraw_later(VALID);
4761}
4762
4763/*
4764 * Commands that start with "z".
4765 */
4766 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004767nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
4769 long n;
4770 colnr_T col;
4771 int nchar = cap->nchar;
4772#ifdef FEAT_FOLDING
4773 long old_fdl = curwin->w_p_fdl;
4774 int old_fen = curwin->w_p_fen;
4775#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004776#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004777 int undo = FALSE;
4778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 if (VIM_ISDIGIT(nchar))
4781 {
4782 /*
4783 * "z123{nchar}": edit the count before obtaining {nchar}
4784 */
4785 if (checkclearop(cap->oap))
4786 return;
4787 n = nchar - '0';
4788 for (;;)
4789 {
4790#ifdef USE_ON_FLY_SCROLL
4791 dont_scroll = TRUE; /* disallow scrolling here */
4792#endif
4793 ++no_mapping;
4794 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004795 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 --no_mapping;
4798 --allow_keys;
4799#ifdef FEAT_CMDL_INFO
4800 (void)add_to_showcmd(nchar);
4801#endif
4802 if (nchar == K_DEL || nchar == K_KDEL)
4803 n /= 10;
4804 else if (VIM_ISDIGIT(nchar))
4805 n = n * 10 + (nchar - '0');
4806 else if (nchar == CAR)
4807 {
4808#ifdef FEAT_GUI
4809 need_mouse_correct = TRUE;
4810#endif
4811 win_setheight((int)n);
4812 break;
4813 }
4814 else if (nchar == 'l'
4815 || nchar == 'h'
4816 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004817 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
4819 cap->count1 = n ? n * cap->count1 : cap->count1;
4820 goto dozet;
4821 }
4822 else
4823 {
4824 clearopbeep(cap->oap);
4825 break;
4826 }
4827 }
4828 cap->oap->op_type = OP_NOP;
4829 return;
4830 }
4831
4832dozet:
4833 if (
4834#ifdef FEAT_FOLDING
4835 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4836 * and "zC" only in Visual mode. "zj" and "zk" are motion
4837 * commands. */
4838 cap->nchar != 'f' && cap->nchar != 'F'
4839 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4840 && cap->nchar != 'j' && cap->nchar != 'k'
4841 &&
4842#endif
4843 checkclearop(cap->oap))
4844 return;
4845
4846 /*
4847 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4848 * If line number given, set cursor.
4849 */
4850 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4851 && cap->count0
4852 && cap->count0 != curwin->w_cursor.lnum)
4853 {
4854 setpcmark();
4855 if (cap->count0 > curbuf->b_ml.ml_line_count)
4856 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4857 else
4858 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004859 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861
4862 switch (nchar)
4863 {
4864 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4865 case '+':
4866 if (cap->count0 == 0)
4867 {
4868 /* No count given: put cursor at the line below screen */
4869 validate_botline(); /* make sure w_botline is valid */
4870 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4871 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4872 else
4873 curwin->w_cursor.lnum = curwin->w_botline;
4874 }
4875 /* FALLTHROUGH */
4876 case NL:
4877 case CAR:
4878 case K_KENTER:
4879 beginline(BL_WHITE | BL_FIX);
4880 /* FALLTHROUGH */
4881
4882 case 't': scroll_cursor_top(0, TRUE);
4883 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004884 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 break;
4886
4887 /* "z." and "zz": put cursor in middle of screen */
4888 case '.': beginline(BL_WHITE | BL_FIX);
4889 /* FALLTHROUGH */
4890
4891 case 'z': scroll_cursor_halfway(TRUE);
4892 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004893 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 break;
4895
4896 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4897 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4898 * when <count> is at bottom of window, and puts that one at
4899 * bottom of window. */
4900 if (cap->count0 != 0)
4901 {
4902 scroll_cursor_bot(0, TRUE);
4903 curwin->w_cursor.lnum = curwin->w_topline;
4904 }
4905 else if (curwin->w_topline == 1)
4906 curwin->w_cursor.lnum = 1;
4907 else
4908 curwin->w_cursor.lnum = curwin->w_topline - 1;
4909 /* FALLTHROUGH */
4910 case '-':
4911 beginline(BL_WHITE | BL_FIX);
4912 /* FALLTHROUGH */
4913
4914 case 'b': scroll_cursor_bot(0, TRUE);
4915 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004916 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 break;
4918
4919 /* "zH" - scroll screen right half-page */
4920 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004921 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 /* FALLTHROUGH */
4923
4924 /* "zh" - scroll screen to the right */
4925 case 'h':
4926 case K_LEFT:
4927 if (!curwin->w_p_wrap)
4928 {
4929 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4930 curwin->w_leftcol = 0;
4931 else
4932 curwin->w_leftcol -= (colnr_T)cap->count1;
4933 leftcol_changed();
4934 }
4935 break;
4936
4937 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004938 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 /* FALLTHROUGH */
4940
4941 /* "zl" - scroll screen to the left */
4942 case 'l':
4943 case K_RIGHT:
4944 if (!curwin->w_p_wrap)
4945 {
4946 /* scroll the window left */
4947 curwin->w_leftcol += (colnr_T)cap->count1;
4948 leftcol_changed();
4949 }
4950 break;
4951
4952 /* "zs" - scroll screen, cursor at the start */
4953 case 's': if (!curwin->w_p_wrap)
4954 {
4955#ifdef FEAT_FOLDING
4956 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4957 col = 0; /* like the cursor is in col 0 */
4958 else
4959#endif
4960 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4961 if ((long)col > p_siso)
4962 col -= p_siso;
4963 else
4964 col = 0;
4965 if (curwin->w_leftcol != col)
4966 {
4967 curwin->w_leftcol = col;
4968 redraw_later(NOT_VALID);
4969 }
4970 }
4971 break;
4972
4973 /* "ze" - scroll screen, cursor at the end */
4974 case 'e': if (!curwin->w_p_wrap)
4975 {
4976#ifdef FEAT_FOLDING
4977 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4978 col = 0; /* like the cursor is in col 0 */
4979 else
4980#endif
4981 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004982 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if ((long)col + p_siso < n)
4984 col = 0;
4985 else
4986 col = col + p_siso - n + 1;
4987 if (curwin->w_leftcol != col)
4988 {
4989 curwin->w_leftcol = col;
4990 redraw_later(NOT_VALID);
4991 }
4992 }
4993 break;
4994
4995#ifdef FEAT_FOLDING
4996 /* "zF": create fold command */
4997 /* "zf": create fold operator */
4998 case 'F':
4999 case 'f': if (foldManualAllowed(TRUE))
5000 {
5001 cap->nchar = 'f';
5002 nv_operator(cap);
5003 curwin->w_p_fen = TRUE;
5004
5005 /* "zF" is like "zfzf" */
5006 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5007 {
5008 nv_operator(cap);
5009 finish_op = TRUE;
5010 }
5011 }
5012 else
5013 clearopbeep(cap->oap);
5014 break;
5015
5016 /* "zd": delete fold at cursor */
5017 /* "zD": delete fold at cursor recursively */
5018 case 'd':
5019 case 'D': if (foldManualAllowed(FALSE))
5020 {
5021 if (VIsual_active)
5022 nv_operator(cap);
5023 else
5024 deleteFold(curwin->w_cursor.lnum,
5025 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5026 }
5027 break;
5028
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005029 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 case 'E': if (foldmethodIsManual(curwin))
5031 {
5032 clearFolding(curwin);
5033 changed_window_setting();
5034 }
5035 else if (foldmethodIsMarker(curwin))
5036 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5037 TRUE, FALSE);
5038 else
5039 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5040 break;
5041
5042 /* "zn": fold none: reset 'foldenable' */
5043 case 'n': curwin->w_p_fen = FALSE;
5044 break;
5045
5046 /* "zN": fold Normal: set 'foldenable' */
5047 case 'N': curwin->w_p_fen = TRUE;
5048 break;
5049
5050 /* "zi": invert folding: toggle 'foldenable' */
5051 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5052 break;
5053
5054 /* "za": open closed fold or close open fold at cursor */
5055 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5056 openFold(curwin->w_cursor.lnum, cap->count1);
5057 else
5058 {
5059 closeFold(curwin->w_cursor.lnum, cap->count1);
5060 curwin->w_p_fen = TRUE;
5061 }
5062 break;
5063
5064 /* "zA": open fold at cursor recursively */
5065 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5066 openFoldRecurse(curwin->w_cursor.lnum);
5067 else
5068 {
5069 closeFoldRecurse(curwin->w_cursor.lnum);
5070 curwin->w_p_fen = TRUE;
5071 }
5072 break;
5073
5074 /* "zo": open fold at cursor or Visual area */
5075 case 'o': if (VIsual_active)
5076 nv_operator(cap);
5077 else
5078 openFold(curwin->w_cursor.lnum, cap->count1);
5079 break;
5080
5081 /* "zO": open fold recursively */
5082 case 'O': if (VIsual_active)
5083 nv_operator(cap);
5084 else
5085 openFoldRecurse(curwin->w_cursor.lnum);
5086 break;
5087
5088 /* "zc": close fold at cursor or Visual area */
5089 case 'c': if (VIsual_active)
5090 nv_operator(cap);
5091 else
5092 closeFold(curwin->w_cursor.lnum, cap->count1);
5093 curwin->w_p_fen = TRUE;
5094 break;
5095
5096 /* "zC": close fold recursively */
5097 case 'C': if (VIsual_active)
5098 nv_operator(cap);
5099 else
5100 closeFoldRecurse(curwin->w_cursor.lnum);
5101 curwin->w_p_fen = TRUE;
5102 break;
5103
5104 /* "zv": open folds at the cursor */
5105 case 'v': foldOpenCursor();
5106 break;
5107
5108 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5109 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005110 curwin->w_foldinvalid = TRUE; /* recompute folds */
5111 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 foldOpenCursor();
5113 break;
5114
5115 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5116 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005117 curwin->w_foldinvalid = TRUE; /* recompute folds */
5118 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 break;
5120
5121 /* "zm": fold more */
5122 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005123 {
5124 curwin->w_p_fdl -= cap->count1;
5125 if (curwin->w_p_fdl < 0)
5126 curwin->w_p_fdl = 0;
5127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 old_fdl = -1; /* force an update */
5129 curwin->w_p_fen = TRUE;
5130 break;
5131
5132 /* "zM": close all folds */
5133 case 'M': curwin->w_p_fdl = 0;
5134 old_fdl = -1; /* force an update */
5135 curwin->w_p_fen = TRUE;
5136 break;
5137
5138 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005139 case 'r': curwin->w_p_fdl += cap->count1;
5140 {
5141 int d = getDeepestNesting();
5142
5143 if (curwin->w_p_fdl >= d)
5144 curwin->w_p_fdl = d;
5145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 break;
5147
5148 /* "zR": open all folds */
5149 case 'R': curwin->w_p_fdl = getDeepestNesting();
5150 old_fdl = -1; /* force an update */
5151 break;
5152
5153 case 'j': /* "zj" move to next fold downwards */
5154 case 'k': /* "zk" move to next fold upwards */
5155 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5156 cap->count1) == FAIL)
5157 clearopbeep(cap->oap);
5158 break;
5159
5160#endif /* FEAT_FOLDING */
5161
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005162#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005163 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5164 ++no_mapping;
5165 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005166 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005167 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005168 --no_mapping;
5169 --allow_keys;
5170#ifdef FEAT_CMDL_INFO
5171 (void)add_to_showcmd(nchar);
5172#endif
5173 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5174 {
5175 clearopbeep(cap->oap);
5176 break;
5177 }
5178 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005179 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005180
Bram Moolenaarb765d632005-06-07 21:00:02 +00005181 case 'g': /* "zg": add good word to word list */
5182 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005183 case 'G': /* "zG": add good word to temp word list */
5184 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005185 {
5186 char_u *ptr = NULL;
5187 int len;
5188
5189 if (checkclearop(cap->oap))
5190 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005191 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5192 == FAIL)
5193 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005194 if (ptr == NULL)
5195 {
5196 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005197
Bram Moolenaar134bf072013-09-25 18:54:24 +02005198 /* Find bad word under the cursor. When 'spell' is
5199 * off this fails and find_ident_under_cursor() is
5200 * used below. */
5201 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005202 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005203 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005204 if (len != 0 && curwin->w_cursor.col <= pos.col)
5205 ptr = ml_get_pos(&curwin->w_cursor);
5206 curwin->w_cursor = pos;
5207 }
5208
Bram Moolenaarb765d632005-06-07 21:00:02 +00005209 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5210 FIND_IDENT)) == 0)
5211 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005212 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005213 (nchar == 'G' || nchar == 'W')
5214 ? 0 : (int)cap->count1,
5215 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005216 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005217 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218
Bram Moolenaar43abc522005-12-10 20:15:02 +00005219 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005220 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005221 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005223#endif
5224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 default: clearopbeep(cap->oap);
5226 }
5227
5228#ifdef FEAT_FOLDING
5229 /* Redraw when 'foldenable' changed */
5230 if (old_fen != curwin->w_p_fen)
5231 {
5232# ifdef FEAT_DIFF
5233 win_T *wp;
5234
5235 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5236 {
5237 /* Adjust 'foldenable' in diff-synced windows. */
5238 FOR_ALL_WINDOWS(wp)
5239 {
5240 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5241 {
5242 wp->w_p_fen = curwin->w_p_fen;
5243 changed_window_setting_win(wp);
5244 }
5245 }
5246 }
5247# endif
5248 changed_window_setting();
5249 }
5250
5251 /* Redraw when 'foldlevel' changed. */
5252 if (old_fdl != curwin->w_p_fdl)
5253 newFoldLevel();
5254#endif
5255}
5256
5257#ifdef FEAT_GUI
5258/*
5259 * Vertical scrollbar movement.
5260 */
5261 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005262nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 if (cap->oap->op_type != OP_NOP)
5265 clearopbeep(cap->oap);
5266
5267 /* Even if an operator was pending, we still want to scroll */
5268 gui_do_scroll();
5269}
5270
5271/*
5272 * Horizontal scrollbar movement.
5273 */
5274 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005275nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276{
5277 if (cap->oap->op_type != OP_NOP)
5278 clearopbeep(cap->oap);
5279
5280 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005281 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282}
5283#endif
5284
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005285#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005286/*
5287 * Click in GUI tab.
5288 */
5289 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005290nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005291{
5292 if (cap->oap->op_type != OP_NOP)
5293 clearopbeep(cap->oap);
5294
5295 /* Even if an operator was pending, we still want to jump tabs. */
5296 goto_tabpage(current_tab);
5297}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005298
5299/*
5300 * Selected item in tab line menu.
5301 */
5302 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005303nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005304{
5305 if (cap->oap->op_type != OP_NOP)
5306 clearopbeep(cap->oap);
5307
5308 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005309 handle_tabmenu();
5310}
5311
5312/*
5313 * Handle selecting an item of the GUI tab line menu.
5314 * Used in Normal and Insert mode.
5315 */
5316 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005317handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005318{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005319 switch (current_tabmenu)
5320 {
5321 case TABLINE_MENU_CLOSE:
5322 if (current_tab == 0)
5323 do_cmdline_cmd((char_u *)"tabclose");
5324 else
5325 {
5326 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5327 current_tab);
5328 do_cmdline_cmd(IObuff);
5329 }
5330 break;
5331
5332 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005333 if (current_tab == 0)
5334 do_cmdline_cmd((char_u *)"$tabnew");
5335 else
5336 {
5337 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5338 current_tab - 1);
5339 do_cmdline_cmd(IObuff);
5340 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005341 break;
5342
5343 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005344 if (current_tab == 0)
5345 do_cmdline_cmd((char_u *)"browse $tabnew");
5346 else
5347 {
5348 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5349 current_tab - 1);
5350 do_cmdline_cmd(IObuff);
5351 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005352 break;
5353 }
5354}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005355#endif
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357/*
5358 * "Q" command.
5359 */
5360 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005361nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
5363 /*
5364 * Ignore 'Q' in Visual mode, just give a beep.
5365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005367 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005368 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 do_exmode(FALSE);
5370}
5371
5372/*
5373 * Handle a ":" command.
5374 */
5375 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005376nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005379 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 if (VIsual_active)
5382 nv_operator(cap);
5383 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
5385 if (cap->oap->op_type != OP_NOP)
5386 {
5387 /* Using ":" as a movement is characterwise exclusive. */
5388 cap->oap->motion_type = MCHAR;
5389 cap->oap->inclusive = FALSE;
5390 }
5391 else if (cap->count0)
5392 {
5393 /* translate "count:" into ":.,.+(count - 1)" */
5394 stuffcharReadbuff('.');
5395 if (cap->count0 > 1)
5396 {
5397 stuffReadbuff((char_u *)",.+");
5398 stuffnumReadbuff((long)cap->count0 - 1L);
5399 }
5400 }
5401
5402 /* When typing, don't type below an old message */
5403 if (KeyTyped)
5404 compute_cmdrow();
5405
5406 old_p_im = p_im;
5407
5408 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005409 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5411
5412 /* If 'insertmode' changed, enter or exit Insert mode */
5413 if (p_im != old_p_im)
5414 {
5415 if (p_im)
5416 restart_edit = 'i';
5417 else
5418 restart_edit = 0;
5419 }
5420
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005421 if (cmd_result == FAIL)
5422 /* The Ex command failed, do not execute the operator. */
5423 clearop(cap->oap);
5424 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5426 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005427 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5428 || did_emsg
5429 ))
5430 /* The start of the operator has become invalid by the Ex command.
5431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 clearopbeep(cap->oap);
5433 }
5434}
5435
5436/*
5437 * Handle CTRL-G command.
5438 */
5439 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005440nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 if (VIsual_active) /* toggle Selection/Visual mode */
5443 {
5444 VIsual_select = !VIsual_select;
5445 showmode();
5446 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005447 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 /* print full name if count given or :cd used */
5449 fileinfo((int)cap->count0, FALSE, TRUE);
5450}
5451
5452/*
5453 * Handle CTRL-H <Backspace> command.
5454 */
5455 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005456nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 if (VIsual_active && VIsual_select)
5459 {
5460 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5461 v_visop(cap);
5462 }
5463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 nv_left(cap);
5465}
5466
5467/*
5468 * CTRL-L: clear screen and redraw.
5469 */
5470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005471nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
5473 if (!checkclearop(cap->oap))
5474 {
5475#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5476 /*
5477 * Right now, the BeBox doesn't seem to have an easy way to detect
5478 * window resizing, so we cheat and make the user detect it
5479 * manually with CTRL-L instead
5480 */
5481 ui_get_shellsize();
5482#endif
5483#ifdef FEAT_SYN_HL
5484 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005485 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005486# ifdef FEAT_RELTIME
5487 {
5488 win_T *wp;
5489
5490 FOR_ALL_WINDOWS(wp)
5491 wp->w_s->b_syn_slow = FALSE;
5492 }
5493# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494#endif
5495 redraw_later(CLEAR);
5496 }
5497}
5498
5499/*
5500 * CTRL-O: In Select mode: switch to Visual mode for one command.
5501 * Otherwise: Go to older pcmark.
5502 */
5503 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005504nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 if (VIsual_active && VIsual_select)
5507 {
5508 VIsual_select = FALSE;
5509 showmode();
5510 restart_VIsual_select = 2; /* restart Select mode later */
5511 }
5512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
5514 cap->count1 = -cap->count1;
5515 nv_pcmark(cap);
5516 }
5517}
5518
5519/*
5520 * CTRL-^ command, short for ":e #"
5521 */
5522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005523nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 if (!checkclearopq(cap->oap))
5526 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5527 GETF_SETMARK|GETF_ALT, FALSE);
5528}
5529
5530/*
5531 * "Z" commands.
5532 */
5533 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005534nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536 if (!checkclearopq(cap->oap))
5537 {
5538 switch (cap->nchar)
5539 {
5540 /* "ZZ": equivalent to ":x". */
5541 case 'Z': do_cmdline_cmd((char_u *)"x");
5542 break;
5543
5544 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5545 case 'Q': do_cmdline_cmd((char_u *)"q!");
5546 break;
5547
5548 default: clearopbeep(cap->oap);
5549 }
5550 }
5551}
5552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553/*
5554 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5555 */
5556 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005557do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 oparg_T oa;
5560 cmdarg_T ca;
5561
5562 clear_oparg(&oa);
5563 vim_memset(&ca, 0, sizeof(ca));
5564 ca.oap = &oa;
5565 ca.cmdchar = c1;
5566 ca.nchar = c2;
5567 nv_ident(&ca);
5568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
5570/*
5571 * Handle the commands that use the word under the cursor.
5572 * [g] CTRL-] :ta to current identifier
5573 * [g] 'K' run program for current identifier
5574 * [g] '*' / to current identifier or string
5575 * [g] '#' ? to current identifier or string
5576 * g ']' :tselect for current identifier
5577 */
5578 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005579nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 char_u *ptr = NULL;
5582 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005583 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005584 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 char_u *p;
5586 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005587 int kp_help; /* 'keywordprg' is ":he" */
5588 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 int n = 0; /* init for GCC */
5590 int cmdchar;
5591 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005592 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 char_u *aux_ptr;
5594 int isman;
5595 int isman_s;
5596
5597 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5598 {
5599 cmdchar = cap->nchar;
5600 g_cmd = TRUE;
5601 }
5602 else
5603 {
5604 cmdchar = cap->cmdchar;
5605 g_cmd = FALSE;
5606 }
5607
5608 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5609 cmdchar = '#';
5610
5611 /*
5612 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5613 */
5614 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5617 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 if (checkclearopq(cap->oap))
5619 return;
5620 }
5621
5622 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5623 (cmdchar == '*' || cmdchar == '#')
5624 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5625 {
5626 clearop(cap->oap);
5627 return;
5628 }
5629
5630 /* Allocate buffer to put the command in. Inserting backslashes can
5631 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5632 * and some numbers. */
5633 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5634 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5635 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005636 if (kp_help && *skipwhite(ptr) == NUL)
5637 {
5638 EMSG(_(e_noident)); /* found white space only */
5639 return;
5640 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005641 kp_ex = (*kp == ':');
5642 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5643 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 if (buf == NULL)
5645 return;
5646 buf[0] = NUL;
5647
5648 switch (cmdchar)
5649 {
5650 case '*':
5651 case '#':
5652 /*
5653 * Put cursor at start of word, makes search skip the word
5654 * under the cursor.
5655 * Call setpcmark() first, so "*``" puts the cursor back where
5656 * it was.
5657 */
5658 setpcmark();
5659 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5660
5661 if (!g_cmd && vim_iswordp(ptr))
5662 STRCPY(buf, "\\<");
5663 no_smartcase = TRUE; /* don't use 'smartcase' now */
5664 break;
5665
5666 case 'K':
5667 if (kp_help)
5668 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005669 else if (kp_ex)
5670 {
5671 if (cap->count0 != 0)
5672 vim_snprintf((char *)buf, buflen, "%s %ld",
5673 kp, cap->count0);
5674 else
5675 STRCPY(buf, kp);
5676 STRCAT(buf, " ");
5677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 else
5679 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005680 /* An external command will probably use an argument starting
5681 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005682 while (*ptr == '-' && n > 0)
5683 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005684 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005685 --n;
5686 }
5687 if (n == 0)
5688 {
5689 EMSG(_(e_noident)); /* found dashes only */
5690 vim_free(buf);
5691 return;
5692 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 /* When a count is given, turn it into a range. Is this
5695 * really what we want? */
5696 isman = (STRCMP(kp, "man") == 0);
5697 isman_s = (STRCMP(kp, "man -s") == 0);
5698 if (cap->count0 != 0 && !(isman || isman_s))
5699 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5700
5701 STRCAT(buf, "! ");
5702 if (cap->count0 == 0 && isman_s)
5703 STRCAT(buf, "man");
5704 else
5705 STRCAT(buf, kp);
5706 STRCAT(buf, " ");
5707 if (cap->count0 != 0 && (isman || isman_s))
5708 {
5709 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5710 STRCAT(buf, " ");
5711 }
5712 }
5713 break;
5714
5715 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005716 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717#ifdef FEAT_CSCOPE
5718 if (p_cst)
5719 STRCPY(buf, "cstag ");
5720 else
5721#endif
5722 STRCPY(buf, "ts ");
5723 break;
5724
5725 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005726 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 if (curbuf->b_help)
5728 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005730 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005731 if (g_cmd)
5732 STRCPY(buf, "tj ");
5733 else
5734 sprintf((char *)buf, "%ldta ", cap->count0);
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737
5738 /*
5739 * Now grab the chars in the identifier
5740 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005741 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005743 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005744 if (kp_ex)
5745 /* Escape the argument properly for an Ex command */
5746 p = vim_strsave_fnameescape(ptr, FALSE);
5747 else
5748 /* Escape the argument properly for a shell command */
5749 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005750 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005751 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005753 vim_free(buf);
5754 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005756 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5757 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005758 {
5759 vim_free(buf);
5760 vim_free(p);
5761 return;
5762 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005763 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005764 STRCAT(buf, p);
5765 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005767 else
5768 {
5769 if (cmdchar == '*')
5770 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5771 else if (cmdchar == '#')
5772 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005773 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005774 {
5775 if (curbuf->b_help)
5776 /* ":help" handles unescaped argument */
5777 aux_ptr = (char_u *)"";
5778 else
5779 aux_ptr = (char_u *)"\\|\"\n[";
5780 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005781 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005782 aux_ptr = (char_u *)"\\|\"\n*?[";
5783
5784 p = buf + STRLEN(buf);
5785 while (n-- > 0)
5786 {
5787 /* put a backslash before \ and some others */
5788 if (vim_strchr(aux_ptr, *ptr) != NULL)
5789 *p++ = '\\';
5790#ifdef FEAT_MBYTE
5791 /* When current byte is a part of multibyte character, copy all
5792 * bytes of that character. */
5793 if (has_mbyte)
5794 {
5795 int i;
5796 int len = (*mb_ptr2len)(ptr) - 1;
5797
5798 for (i = 0; i < len && n >= 1; ++i, --n)
5799 *p++ = *ptr++;
5800 }
5801#endif
5802 *p++ = *ptr++;
5803 }
5804 *p = NUL;
5805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
5807 /*
5808 * Execute the command.
5809 */
5810 if (cmdchar == '*' || cmdchar == '#')
5811 {
5812 if (!g_cmd && (
5813#ifdef FEAT_MBYTE
5814 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5815#endif
5816 vim_iswordc(ptr[-1])))
5817 STRCAT(buf, "\\>");
5818#ifdef FEAT_CMDHIST
5819 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005820 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5822#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005823 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
5825 else
5826 do_cmdline_cmd(buf);
5827
5828 vim_free(buf);
5829}
5830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831/*
5832 * Get visually selected text, within one line only.
5833 * Returns FAIL if more than one line selected.
5834 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005835 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005836get_visual_text(
5837 cmdarg_T *cap,
5838 char_u **pp, /* return: start of selected text */
5839 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 if (VIsual_mode != 'V')
5842 unadjust_for_sel();
5843 if (VIsual.lnum != curwin->w_cursor.lnum)
5844 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005845 if (cap != NULL)
5846 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 return FAIL;
5848 }
5849 if (VIsual_mode == 'V')
5850 {
5851 *pp = ml_get_curline();
5852 *lenp = (int)STRLEN(*pp);
5853 }
5854 else
5855 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005856 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {
5858 *pp = ml_get_pos(&curwin->w_cursor);
5859 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5860 }
5861 else
5862 {
5863 *pp = ml_get_pos(&VIsual);
5864 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5865 }
5866#ifdef FEAT_MBYTE
5867 if (has_mbyte)
5868 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005869 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870#endif
5871 }
5872 reset_VIsual_and_resel();
5873 return OK;
5874}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876/*
5877 * CTRL-T: backwards in tag stack
5878 */
5879 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005880nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 if (!checkclearopq(cap->oap))
5883 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5884}
5885
5886/*
5887 * Handle scrolling command 'H', 'L' and 'M'.
5888 */
5889 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005890nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 int used = 0;
5893 long n;
5894#ifdef FEAT_FOLDING
5895 linenr_T lnum;
5896#endif
5897 int half;
5898
5899 cap->oap->motion_type = MLINE;
5900 setpcmark();
5901
5902 if (cap->cmdchar == 'L')
5903 {
5904 validate_botline(); /* make sure curwin->w_botline is valid */
5905 curwin->w_cursor.lnum = curwin->w_botline - 1;
5906 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5907 curwin->w_cursor.lnum = 1;
5908 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005909 {
5910#ifdef FEAT_FOLDING
5911 if (hasAnyFolding(curwin))
5912 {
5913 /* Count a fold for one screen line. */
5914 for (n = cap->count1 - 1; n > 0
5915 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5916 {
5917 (void)hasFolding(curwin->w_cursor.lnum,
5918 &curwin->w_cursor.lnum, NULL);
5919 --curwin->w_cursor.lnum;
5920 }
5921 }
5922 else
5923#endif
5924 curwin->w_cursor.lnum -= cap->count1 - 1;
5925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 }
5927 else
5928 {
5929 if (cap->cmdchar == 'M')
5930 {
5931#ifdef FEAT_DIFF
5932 /* Don't count filler lines above the window. */
5933 used -= diff_check_fill(curwin, curwin->w_topline)
5934 - curwin->w_topfill;
5935#endif
5936 validate_botline(); /* make sure w_empty_rows is valid */
5937 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5938 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5939 {
5940#ifdef FEAT_DIFF
5941 /* Count half he number of filler lines to be "below this
5942 * line" and half to be "above the next line". */
5943 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5944 + n) / 2 >= half)
5945 {
5946 --n;
5947 break;
5948 }
5949#endif
5950 used += plines(curwin->w_topline + n);
5951 if (used >= half)
5952 break;
5953#ifdef FEAT_FOLDING
5954 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5955 n = lnum - curwin->w_topline;
5956#endif
5957 }
5958 if (n > 0 && used > curwin->w_height)
5959 --n;
5960 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005961 else /* (cap->cmdchar == 'H') */
5962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005964#ifdef FEAT_FOLDING
5965 if (hasAnyFolding(curwin))
5966 {
5967 /* Count a fold for one screen line. */
5968 lnum = curwin->w_topline;
5969 while (n-- > 0 && lnum < curwin->w_botline - 1)
5970 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005971 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005972 ++lnum;
5973 }
5974 n = lnum - curwin->w_topline;
5975 }
5976#endif
5977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 curwin->w_cursor.lnum = curwin->w_topline + n;
5979 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5980 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5981 }
5982
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005983 /* Correct for 'so', except when an operator is pending. */
5984 if (cap->oap->op_type == OP_NOP)
5985 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 beginline(BL_SOL | BL_FIX);
5987}
5988
5989/*
5990 * Cursor right commands.
5991 */
5992 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005993nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005996 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005998 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5999 {
6000 /* <C-Right> and <S-Right> move a word or WORD right */
6001 if (mod_mask & MOD_MASK_CTRL)
6002 cap->arg = TRUE;
6003 nv_wordcmd(cap);
6004 return;
6005 }
6006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 cap->oap->motion_type = MCHAR;
6008 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006009 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006011#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006013 * In virtual edit mode, there's no such thing as "past_line", as lines
6014 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 */
6016 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006017 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018#endif
6019
6020 for (n = cap->count1; n > 0; --n)
6021 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006022 if ((!past_line && oneright() == FAIL)
6023 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006024 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 {
6026 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006027 * <Space> wraps to next line if 'whichwrap' has 's'.
6028 * 'l' wraps to next line if 'whichwrap' has 'l'.
6029 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 */
6031 if ( ((cap->cmdchar == ' '
6032 && vim_strchr(p_ww, 's') != NULL)
6033 || (cap->cmdchar == 'l'
6034 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006035 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 && vim_strchr(p_ww, '>') != NULL))
6037 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6038 {
6039 /* When deleting we also count the NL as a character.
6040 * Set cap->oap->inclusive when last char in the line is
6041 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006042 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006044 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 cap->oap->inclusive = TRUE;
6046 else
6047 {
6048 ++curwin->w_cursor.lnum;
6049 curwin->w_cursor.col = 0;
6050#ifdef FEAT_VIRTUALEDIT
6051 curwin->w_cursor.coladd = 0;
6052#endif
6053 curwin->w_set_curswant = TRUE;
6054 cap->oap->inclusive = FALSE;
6055 }
6056 continue;
6057 }
6058 if (cap->oap->op_type == OP_NOP)
6059 {
6060 /* Only beep and flush if not moved at all */
6061 if (n == cap->count1)
6062 beep_flush();
6063 }
6064 else
6065 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006066 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 cap->oap->inclusive = TRUE;
6068 }
6069 break;
6070 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006071 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
6073 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006074#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 if (virtual_active())
6076 oneright();
6077 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006080#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (has_mbyte)
6082 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006083 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 ++curwin->w_cursor.col;
6087 }
6088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 }
6090#ifdef FEAT_FOLDING
6091 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6092 && cap->oap->op_type == OP_NOP)
6093 foldOpenCursor();
6094#endif
6095}
6096
6097/*
6098 * Cursor left commands.
6099 *
6100 * Returns TRUE when operator end should not be adjusted.
6101 */
6102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006103nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 long n;
6106
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006107 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6108 {
6109 /* <C-Left> and <S-Left> move a word or WORD left */
6110 if (mod_mask & MOD_MASK_CTRL)
6111 cap->arg = 1;
6112 nv_bck_word(cap);
6113 return;
6114 }
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 cap->oap->motion_type = MCHAR;
6117 cap->oap->inclusive = FALSE;
6118 for (n = cap->count1; n > 0; --n)
6119 {
6120 if (oneleft() == FAIL)
6121 {
6122 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6123 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6124 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6125 */
6126 if ( (((cap->cmdchar == K_BS
6127 || cap->cmdchar == Ctrl_H)
6128 && vim_strchr(p_ww, 'b') != NULL)
6129 || (cap->cmdchar == 'h'
6130 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006131 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 && vim_strchr(p_ww, '<') != NULL))
6133 && curwin->w_cursor.lnum > 1)
6134 {
6135 --(curwin->w_cursor.lnum);
6136 coladvance((colnr_T)MAXCOL);
6137 curwin->w_set_curswant = TRUE;
6138
6139 /* When the NL before the first char has to be deleted we
6140 * put the cursor on the NUL after the previous line.
6141 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006142 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if ( (cap->oap->op_type == OP_DELETE
6144 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006145 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006147 char_u *cp = ml_get_cursor();
6148
6149 if (*cp != NUL)
6150 {
6151#ifdef FEAT_MBYTE
6152 if (has_mbyte)
6153 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6154 else
6155#endif
6156 ++curwin->w_cursor.col;
6157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 cap->retval |= CA_NO_ADJ_OP_END;
6159 }
6160 continue;
6161 }
6162 /* Only beep and flush if not moved at all */
6163 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6164 beep_flush();
6165 break;
6166 }
6167 }
6168#ifdef FEAT_FOLDING
6169 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6170 && cap->oap->op_type == OP_NOP)
6171 foldOpenCursor();
6172#endif
6173}
6174
6175/*
6176 * Cursor up commands.
6177 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6178 */
6179 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006180nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006182 if (mod_mask & MOD_MASK_SHIFT)
6183 {
6184 /* <S-Up> is page up */
6185 cap->arg = BACKWARD;
6186 nv_page(cap);
6187 }
6188 else
6189 {
6190 cap->oap->motion_type = MLINE;
6191 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6192 clearopbeep(cap->oap);
6193 else if (cap->arg)
6194 beginline(BL_WHITE | BL_FIX);
6195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196}
6197
6198/*
6199 * Cursor down commands.
6200 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6201 */
6202 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006203nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006205 if (mod_mask & MOD_MASK_SHIFT)
6206 {
6207 /* <S-Down> is page down */
6208 cap->arg = FORWARD;
6209 nv_page(cap);
6210 }
6211 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006212#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006214 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar74a47162017-02-26 19:09:05 +01006215 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006216 if (curwin->w_llist_ref == NULL)
6217 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6218 else
6219 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar74a47162017-02-26 19:09:05 +01006220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 else
6222#endif
6223 {
6224#ifdef FEAT_CMDWIN
6225 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006226 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 cmdwin_result = CAR;
6228 else
6229#endif
6230 {
6231 cap->oap->motion_type = MLINE;
6232 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6233 clearopbeep(cap->oap);
6234 else if (cap->arg)
6235 beginline(BL_WHITE | BL_FIX);
6236 }
6237 }
6238}
6239
6240#ifdef FEAT_SEARCHPATH
6241/*
6242 * Grab the file name under the cursor and edit it.
6243 */
6244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006245nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
6247 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006248 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006250 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {
6252 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006253 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 return;
6255 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006256#ifdef FEAT_AUTOCMD
6257 if (curbuf_locked())
6258 {
6259 clearop(cap->oap);
6260 return;
6261 }
6262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006264 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
6266 if (ptr != NULL)
6267 {
6268 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006269 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006270 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006272 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006273 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006274 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006275 {
6276 curwin->w_cursor.lnum = lnum;
6277 check_cursor_lnum();
6278 beginline(BL_SOL | BL_FIX);
6279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 vim_free(ptr);
6281 }
6282 else
6283 clearop(cap->oap);
6284}
6285#endif
6286
6287/*
6288 * <End> command: to end of current line or last line.
6289 */
6290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006291nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006293 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006295 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 nv_goto(cap);
6297 cap->count1 = 1; /* to end of current line */
6298 }
6299 nv_dollar(cap);
6300}
6301
6302/*
6303 * Handle the "$" command.
6304 */
6305 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006306nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 cap->oap->motion_type = MCHAR;
6309 cap->oap->inclusive = TRUE;
6310#ifdef FEAT_VIRTUALEDIT
6311 /* In virtual mode when off the edge of a line and an operator
6312 * is pending (whew!) keep the cursor where it is.
6313 * Otherwise, send it to the end of the line. */
6314 if (!virtual_active() || gchar_cursor() != NUL
6315 || cap->oap->op_type == OP_NOP)
6316#endif
6317 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6318 if (cursor_down((long)(cap->count1 - 1),
6319 cap->oap->op_type == OP_NOP) == FAIL)
6320 clearopbeep(cap->oap);
6321#ifdef FEAT_FOLDING
6322 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6323 foldOpenCursor();
6324#endif
6325}
6326
6327/*
6328 * Implementation of '?' and '/' commands.
6329 * If cap->arg is TRUE don't set PC mark.
6330 */
6331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006332nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
6334 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006335 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6338 {
6339 /* Translate "g??" to "g?g?" */
6340 cap->cmdchar = 'g';
6341 cap->nchar = '?';
6342 nv_operator(cap);
6343 return;
6344 }
6345
Bram Moolenaardda933d2016-09-03 21:04:58 +02006346 /* When using 'incsearch' the cursor may be moved to set a different search
6347 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6349
6350 if (cap->searchbuf == NULL)
6351 {
6352 clearop(oap);
6353 return;
6354 }
6355
Bram Moolenaar46539112015-02-17 15:43:57 +01006356 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006357 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006358 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359}
6360
6361/*
6362 * Handle "N" and "n" commands.
6363 * cap->arg is SEARCH_REV for "N", 0 for "n".
6364 */
6365 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006366nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
Bram Moolenaar46539112015-02-17 15:43:57 +01006368 pos_T old = curwin->w_cursor;
6369 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6370
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006371 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006372 {
6373 /* Avoid getting stuck on the current cursor position, which can
6374 * happen when an offset is given and the cursor is on the last char
6375 * in the buffer: Repeat with count + 1. */
6376 cap->count1 += 1;
6377 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6378 cap->count1 -= 1;
6379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380}
6381
6382/*
6383 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6384 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006385 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006387 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006388normal_search(
6389 cmdarg_T *cap,
6390 int dir,
6391 char_u *pat,
6392 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 int i;
6395
6396 cap->oap->motion_type = MCHAR;
6397 cap->oap->inclusive = FALSE;
6398 cap->oap->use_reg_one = TRUE;
6399 curwin->w_set_curswant = TRUE;
6400
6401 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006402 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (i == 0)
6404 clearop(cap->oap);
6405 else
6406 {
6407 if (i == 2)
6408 cap->oap->motion_type = MLINE;
6409#ifdef FEAT_VIRTUALEDIT
6410 curwin->w_cursor.coladd = 0;
6411#endif
6412#ifdef FEAT_FOLDING
6413 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6414 foldOpenCursor();
6415#endif
6416 }
6417
6418 /* "/$" will put the cursor after the end of the line, may need to
6419 * correct that here */
6420 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006421 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422}
6423
6424/*
6425 * Character search commands.
6426 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6427 * ',' and FALSE for ';'.
6428 * cap->nchar is NUL for ',' and ';' (repeat the search)
6429 */
6430 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006431nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 int t_cmd;
6434
6435 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6436 t_cmd = TRUE;
6437 else
6438 t_cmd = FALSE;
6439
6440 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6442 clearopbeep(cap->oap);
6443 else
6444 {
6445 curwin->w_set_curswant = TRUE;
6446#ifdef FEAT_VIRTUALEDIT
6447 /* Include a Tab for "tx" and for "dfx". */
6448 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6449 && (t_cmd || cap->oap->op_type != OP_NOP))
6450 {
6451 colnr_T scol, ecol;
6452
6453 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6454 curwin->w_cursor.coladd = ecol - scol;
6455 }
6456 else
6457 curwin->w_cursor.coladd = 0;
6458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460#ifdef FEAT_FOLDING
6461 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6462 foldOpenCursor();
6463#endif
6464 }
6465}
6466
6467/*
6468 * "[" and "]" commands.
6469 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6470 */
6471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006472nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006474 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 pos_T prev_pos;
6476 pos_T *pos = NULL; /* init for GCC */
6477 pos_T old_pos; /* cursor position before command */
6478 int flag;
6479 long n;
6480 int findc;
6481 int c;
6482
6483 cap->oap->motion_type = MCHAR;
6484 cap->oap->inclusive = FALSE;
6485 old_pos = curwin->w_cursor;
6486#ifdef FEAT_VIRTUALEDIT
6487 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6488#endif
6489
6490#ifdef FEAT_SEARCHPATH
6491 /*
6492 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6493 */
6494 if (cap->nchar == 'f')
6495 nv_gotofile(cap);
6496 else
6497#endif
6498
6499#ifdef FEAT_FIND_ID
6500 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006501 * Find the occurrence(s) of the identifier or define under cursor
6502 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 *
6504 * search list jump
6505 * fwd bwd fwd bwd fwd bwd
6506 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6507 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6508 */
6509 if (vim_strchr((char_u *)
6510#ifdef EBCDIC
6511 "iI\005dD\067",
6512#else
6513 "iI\011dD\004",
6514#endif
6515 cap->nchar) != NULL)
6516 {
6517 char_u *ptr;
6518 int len;
6519
6520 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6521 clearop(cap->oap);
6522 else
6523 {
6524 find_pattern_in_path(ptr, 0, len, TRUE,
6525 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6526 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6527 cap->count1,
6528 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6529 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6530 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6531 (linenr_T)MAXLNUM);
6532 curwin->w_set_curswant = TRUE;
6533 }
6534 }
6535 else
6536#endif
6537
6538 /*
6539 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6540 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6541 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6542 * "[m" or "]m" search for prev/next start of (Java) method.
6543 * "[M" or "]M" search for prev/next end of (Java) method.
6544 */
6545 if ( (cap->cmdchar == '['
6546 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6547 || (cap->cmdchar == ']'
6548 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6549 {
6550 if (cap->nchar == '*')
6551 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 prev_pos.lnum = 0;
6553 if (cap->nchar == 'm' || cap->nchar == 'M')
6554 {
6555 if (cap->cmdchar == '[')
6556 findc = '{';
6557 else
6558 findc = '}';
6559 n = 9999;
6560 }
6561 else
6562 {
6563 findc = cap->nchar;
6564 n = cap->count1;
6565 }
6566 for ( ; n > 0; --n)
6567 {
6568 if ((pos = findmatchlimit(cap->oap, findc,
6569 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6570 {
6571 if (new_pos.lnum == 0) /* nothing found */
6572 {
6573 if (cap->nchar != 'm' && cap->nchar != 'M')
6574 clearopbeep(cap->oap);
6575 }
6576 else
6577 pos = &new_pos; /* use last one found */
6578 break;
6579 }
6580 prev_pos = new_pos;
6581 curwin->w_cursor = *pos;
6582 new_pos = *pos;
6583 }
6584 curwin->w_cursor = old_pos;
6585
6586 /*
6587 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6588 * brought us to the match for "[m" and "]M" when inside a method.
6589 * Try finding the '{' or '}' we want to be at.
6590 * Also repeat for the given count.
6591 */
6592 if (cap->nchar == 'm' || cap->nchar == 'M')
6593 {
6594 /* norm is TRUE for "]M" and "[m" */
6595 int norm = ((findc == '{') == (cap->nchar == 'm'));
6596
6597 n = cap->count1;
6598 /* found a match: we were inside a method */
6599 if (prev_pos.lnum != 0)
6600 {
6601 pos = &prev_pos;
6602 curwin->w_cursor = prev_pos;
6603 if (norm)
6604 --n;
6605 }
6606 else
6607 pos = NULL;
6608 while (n > 0)
6609 {
6610 for (;;)
6611 {
6612 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6613 {
6614 /* if not found anything, that's an error */
6615 if (pos == NULL)
6616 clearopbeep(cap->oap);
6617 n = 0;
6618 break;
6619 }
6620 c = gchar_cursor();
6621 if (c == '{' || c == '}')
6622 {
6623 /* Must have found end/start of class: use it.
6624 * Or found the place to be at. */
6625 if ((c == findc && norm) || (n == 1 && !norm))
6626 {
6627 new_pos = curwin->w_cursor;
6628 pos = &new_pos;
6629 n = 0;
6630 }
6631 /* if no match found at all, we started outside of the
6632 * class and we're inside now. Just go on. */
6633 else if (new_pos.lnum == 0)
6634 {
6635 new_pos = curwin->w_cursor;
6636 pos = &new_pos;
6637 }
6638 /* found start/end of other method: go to match */
6639 else if ((pos = findmatchlimit(cap->oap, findc,
6640 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6641 0)) == NULL)
6642 n = 0;
6643 else
6644 curwin->w_cursor = *pos;
6645 break;
6646 }
6647 }
6648 --n;
6649 }
6650 curwin->w_cursor = old_pos;
6651 if (pos == NULL && new_pos.lnum != 0)
6652 clearopbeep(cap->oap);
6653 }
6654 if (pos != NULL)
6655 {
6656 setpcmark();
6657 curwin->w_cursor = *pos;
6658 curwin->w_set_curswant = TRUE;
6659#ifdef FEAT_FOLDING
6660 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6661 && cap->oap->op_type == OP_NOP)
6662 foldOpenCursor();
6663#endif
6664 }
6665 }
6666
6667 /*
6668 * "[[", "[]", "]]" and "][": move to start or end of function
6669 */
6670 else if (cap->nchar == '[' || cap->nchar == ']')
6671 {
6672 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6673 flag = '{';
6674 else
6675 flag = '}'; /* "][" or "[]" */
6676
6677 curwin->w_set_curswant = TRUE;
6678 /*
6679 * Imitate strange Vi behaviour: When using "]]" with an operator
6680 * we also stop at '}'.
6681 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006682 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 (cap->oap->op_type != OP_NOP
6684 && cap->arg == FORWARD && flag == '{')))
6685 clearopbeep(cap->oap);
6686 else
6687 {
6688 if (cap->oap->op_type == OP_NOP)
6689 beginline(BL_WHITE | BL_FIX);
6690#ifdef FEAT_FOLDING
6691 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6692 foldOpenCursor();
6693#endif
6694 }
6695 }
6696
6697 /*
6698 * "[p", "[P", "]P" and "]p": put with indent adjustment
6699 */
6700 else if (cap->nchar == 'p' || cap->nchar == 'P')
6701 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006702 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006704 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6705 ? FORWARD : BACKWARD;
6706 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006707 int was_visual = VIsual_active;
6708 int line_count = curbuf->b_ml.ml_line_count;
6709 pos_T start, end;
6710
6711 if (VIsual_active)
6712 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006713 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006714 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006715 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006716 curwin->w_cursor = (dir == BACKWARD ? start : end);
6717 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006718# ifdef FEAT_CLIPBOARD
6719 adjust_clip_reg(&regname);
6720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006722
6723 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006724 if (was_visual)
6725 {
6726 VIsual = start;
6727 curwin->w_cursor = end;
6728 if (dir == BACKWARD)
6729 {
6730 /* adjust lines */
6731 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6732 curwin->w_cursor.lnum +=
6733 curbuf->b_ml.ml_line_count - line_count;
6734 }
6735
6736 VIsual_active = TRUE;
6737 if (VIsual_mode == 'V')
6738 {
6739 /* delete visually selected lines */
6740 cap->cmdchar = 'd';
6741 cap->nchar = NUL;
6742 cap->oap->regname = regname;
6743 nv_operator(cap);
6744 do_pending_operator(cap, 0, FALSE);
6745 }
6746 if (VIsual_active)
6747 {
6748 end_visual_mode();
6749 redraw_later(SOME_VALID);
6750 }
6751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
6753 }
6754
6755 /*
6756 * "['", "[`", "]'" and "]`": jump to next mark
6757 */
6758 else if (cap->nchar == '\'' || cap->nchar == '`')
6759 {
6760 pos = &curwin->w_cursor;
6761 for (n = cap->count1; n > 0; --n)
6762 {
6763 prev_pos = *pos;
6764 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6765 cap->nchar == '\'');
6766 if (pos == NULL)
6767 break;
6768 }
6769 if (pos == NULL)
6770 pos = &prev_pos;
6771 nv_cursormark(cap, cap->nchar == '\'', pos);
6772 }
6773
6774#ifdef FEAT_MOUSE
6775 /*
6776 * [ or ] followed by a middle mouse click: put selected text with
6777 * indent adjustment. Any other button just does as usual.
6778 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006779 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {
6781 (void)do_mouse(cap->oap, cap->nchar,
6782 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6783 cap->count1, PUT_FIXINDENT);
6784 }
6785#endif /* FEAT_MOUSE */
6786
6787#ifdef FEAT_FOLDING
6788 /*
6789 * "[z" and "]z": move to start or end of open fold.
6790 */
6791 else if (cap->nchar == 'z')
6792 {
6793 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6794 cap->count1) == FAIL)
6795 clearopbeep(cap->oap);
6796 }
6797#endif
6798
6799#ifdef FEAT_DIFF
6800 /*
6801 * "[c" and "]c": move to next or previous diff-change.
6802 */
6803 else if (cap->nchar == 'c')
6804 {
6805 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6806 cap->count1) == FAIL)
6807 clearopbeep(cap->oap);
6808 }
6809#endif
6810
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006811#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006812 /*
6813 * "[s", "[S", "]s" and "]S": move to next spell error.
6814 */
6815 else if (cap->nchar == 's' || cap->nchar == 'S')
6816 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006817 setpcmark();
6818 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006819 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6820 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006821 {
6822 clearopbeep(cap->oap);
6823 break;
6824 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006825 else
6826 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006827# ifdef FEAT_FOLDING
6828 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6829 foldOpenCursor();
6830# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006831 }
6832#endif
6833
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 /* Not a valid cap->nchar. */
6835 else
6836 clearopbeep(cap->oap);
6837}
6838
6839/*
6840 * Handle Normal mode "%" command.
6841 */
6842 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006843nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006846#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 linenr_T lnum = curwin->w_cursor.lnum;
6848#endif
6849
6850 cap->oap->inclusive = TRUE;
6851 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6852 {
6853 if (cap->count0 > 100)
6854 clearopbeep(cap->oap);
6855 else
6856 {
6857 cap->oap->motion_type = MLINE;
6858 setpcmark();
6859 /* Round up, so CTRL-G will give same value. Watch out for a
6860 * large line count, the line number must not go negative! */
6861 if (curbuf->b_ml.ml_line_count > 1000000)
6862 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6863 / 100L * cap->count0;
6864 else
6865 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6866 cap->count0 + 99L) / 100L;
6867 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6868 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6869 beginline(BL_SOL | BL_FIX);
6870 }
6871 }
6872 else /* "%" : go to matching paren */
6873 {
6874 cap->oap->motion_type = MCHAR;
6875 cap->oap->use_reg_one = TRUE;
6876 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6877 clearopbeep(cap->oap);
6878 else
6879 {
6880 setpcmark();
6881 curwin->w_cursor = *pos;
6882 curwin->w_set_curswant = TRUE;
6883#ifdef FEAT_VIRTUALEDIT
6884 curwin->w_cursor.coladd = 0;
6885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888 }
6889#ifdef FEAT_FOLDING
6890 if (cap->oap->op_type == OP_NOP
6891 && lnum != curwin->w_cursor.lnum
6892 && (fdo_flags & FDO_PERCENT)
6893 && KeyTyped)
6894 foldOpenCursor();
6895#endif
6896}
6897
6898/*
6899 * Handle "(" and ")" commands.
6900 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6901 */
6902 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006903nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
6905 cap->oap->motion_type = MCHAR;
6906 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006907 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6908 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 curwin->w_set_curswant = TRUE;
6910
6911 if (findsent(cap->arg, cap->count1) == FAIL)
6912 clearopbeep(cap->oap);
6913 else
6914 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006915 /* Don't leave the cursor on the NUL past end of line. */
6916 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917#ifdef FEAT_VIRTUALEDIT
6918 curwin->w_cursor.coladd = 0;
6919#endif
6920#ifdef FEAT_FOLDING
6921 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6922 foldOpenCursor();
6923#endif
6924 }
6925}
6926
6927/*
6928 * "m" command: Mark a position.
6929 */
6930 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006931nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932{
6933 if (!checkclearop(cap->oap))
6934 {
6935 if (setmark(cap->nchar) == FAIL)
6936 clearopbeep(cap->oap);
6937 }
6938}
6939
6940/*
6941 * "{" and "}" commands.
6942 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6943 */
6944 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006945nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946{
6947 cap->oap->motion_type = MCHAR;
6948 cap->oap->inclusive = FALSE;
6949 cap->oap->use_reg_one = TRUE;
6950 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006951 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 clearopbeep(cap->oap);
6953 else
6954 {
6955#ifdef FEAT_VIRTUALEDIT
6956 curwin->w_cursor.coladd = 0;
6957#endif
6958#ifdef FEAT_FOLDING
6959 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6960 foldOpenCursor();
6961#endif
6962 }
6963}
6964
6965/*
6966 * "u" command: Undo or make lower case.
6967 */
6968 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006969nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006971 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 {
6973 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6974 cap->cmdchar = 'g';
6975 cap->nchar = 'u';
6976 nv_operator(cap);
6977 }
6978 else
6979 nv_kundo(cap);
6980}
6981
6982/*
6983 * <Undo> command.
6984 */
6985 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006986nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 if (!checkclearopq(cap->oap))
6989 {
6990 u_undo((int)cap->count1);
6991 curwin->w_set_curswant = TRUE;
6992 }
6993}
6994
6995/*
6996 * Handle the "r" command.
6997 */
6998 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006999nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
7001 char_u *ptr;
7002 int had_ctrl_v;
7003 long n;
7004
7005 if (checkclearop(cap->oap))
7006 return;
7007
7008 /* get another character */
7009 if (cap->nchar == Ctrl_V)
7010 {
7011 had_ctrl_v = Ctrl_V;
7012 cap->nchar = get_literal();
7013 /* Don't redo a multibyte character with CTRL-V. */
7014 if (cap->nchar > DEL)
7015 had_ctrl_v = NUL;
7016 }
7017 else
7018 had_ctrl_v = NUL;
7019
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007020 /* Abort if the character is a special key. */
7021 if (IS_SPECIAL(cap->nchar))
7022 {
7023 clearopbeep(cap->oap);
7024 return;
7025 }
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 /* Visual mode "r" */
7028 if (VIsual_active)
7029 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007030 if (got_int)
7031 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007032 if (had_ctrl_v)
7033 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01007034 /* Use a special (negative) number to make a difference between a
7035 * literal CR or NL and a line break. */
7036 if (cap->nchar == CAR)
7037 cap->nchar = REPLACE_CR_NCHAR;
7038 else if (cap->nchar == NL)
7039 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01007040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 nv_operator(cap);
7042 return;
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044
7045#ifdef FEAT_VIRTUALEDIT
7046 /* Break tabs, etc. */
7047 if (virtual_active())
7048 {
7049 if (u_save_cursor() == FAIL)
7050 return;
7051 if (gchar_cursor() == NUL)
7052 {
7053 /* Add extra space and put the cursor on the first one. */
7054 coladvance_force((colnr_T)(getviscol() + cap->count1));
7055 curwin->w_cursor.col -= cap->count1;
7056 }
7057 else if (gchar_cursor() == TAB)
7058 coladvance_force(getviscol());
7059 }
7060#endif
7061
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007062 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007064 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065#ifdef FEAT_MBYTE
7066 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7067#endif
7068 )
7069 {
7070 clearopbeep(cap->oap);
7071 return;
7072 }
7073
7074 /*
7075 * Replacing with a TAB is done by edit() when it is complicated because
7076 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7077 * Other characters are done below to avoid problems with things like
7078 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7079 */
7080 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7081 {
7082 stuffnumReadbuff(cap->count1);
7083 stuffcharReadbuff('R');
7084 stuffcharReadbuff('\t');
7085 stuffcharReadbuff(ESC);
7086 return;
7087 }
7088
7089 /* save line for undo */
7090 if (u_save_cursor() == FAIL)
7091 return;
7092
7093 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7094 {
7095 /*
7096 * Replace character(s) by a single newline.
7097 * Strange vi behaviour: Only one newline is inserted.
7098 * Delete the characters here.
7099 * Insert the newline with an insert command, takes care of
7100 * autoindent. The insert command depends on being on the last
7101 * character of a line or not.
7102 */
7103#ifdef FEAT_MBYTE
7104 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7105#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007106 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107#endif
7108 stuffcharReadbuff('\r');
7109 stuffcharReadbuff(ESC);
7110
7111 /* Give 'r' to edit(), to get the redo command right. */
7112 invoke_edit(cap, TRUE, 'r', FALSE);
7113 }
7114 else
7115 {
7116 prep_redo(cap->oap->regname, cap->count1,
7117 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7118
7119 curbuf->b_op_start = curwin->w_cursor;
7120#ifdef FEAT_MBYTE
7121 if (has_mbyte)
7122 {
7123 int old_State = State;
7124
7125 if (cap->ncharC1 != 0)
7126 AppendCharToRedobuff(cap->ncharC1);
7127 if (cap->ncharC2 != 0)
7128 AppendCharToRedobuff(cap->ncharC2);
7129
7130 /* This is slow, but it handles replacing a single-byte with a
7131 * multi-byte and the other way around. Also handles adding
7132 * composing characters for utf-8. */
7133 for (n = cap->count1; n > 0; --n)
7134 {
7135 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007136 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7137 {
7138 int c = ins_copychar(curwin->w_cursor.lnum
7139 + (cap->nchar == Ctrl_Y ? -1 : 1));
7140 if (c != NUL)
7141 ins_char(c);
7142 else
7143 /* will be decremented further down */
7144 ++curwin->w_cursor.col;
7145 }
7146 else
7147 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 State = old_State;
7149 if (cap->ncharC1 != 0)
7150 ins_char(cap->ncharC1);
7151 if (cap->ncharC2 != 0)
7152 ins_char(cap->ncharC2);
7153 }
7154 }
7155 else
7156#endif
7157 {
7158 /*
7159 * Replace the characters within one line.
7160 */
7161 for (n = cap->count1; n > 0; --n)
7162 {
7163 /*
7164 * Get ptr again, because u_save and/or showmatch() will have
7165 * released the line. At the same time we let know that the
7166 * line will be changed.
7167 */
7168 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007169 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7170 {
7171 int c = ins_copychar(curwin->w_cursor.lnum
7172 + (cap->nchar == Ctrl_Y ? -1 : 1));
7173 if (c != NUL)
7174 ptr[curwin->w_cursor.col] = c;
7175 }
7176 else
7177 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 if (p_sm && msg_silent == 0)
7179 showmatch(cap->nchar);
7180 ++curwin->w_cursor.col;
7181 }
7182#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007183 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007185 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
Bram Moolenaar009b2592004-10-24 19:18:58 +00007187 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007188 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007190 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
7192#endif
7193
7194 /* mark the buffer as changed and prepare for displaying */
7195 changed_bytes(curwin->w_cursor.lnum,
7196 (colnr_T)(curwin->w_cursor.col - cap->count1));
7197 }
7198 --curwin->w_cursor.col; /* cursor on the last replaced char */
7199#ifdef FEAT_MBYTE
7200 /* if the character on the left of the current cursor is a multi-byte
7201 * character, move two characters left */
7202 if (has_mbyte)
7203 mb_adjust_cursor();
7204#endif
7205 curbuf->b_op_end = curwin->w_cursor;
7206 curwin->w_set_curswant = TRUE;
7207 set_last_insert(cap->nchar);
7208 }
7209}
7210
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211/*
7212 * 'o': Exchange start and end of Visual area.
7213 * 'O': same, but in block mode exchange left and right corners.
7214 */
7215 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007216v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217{
7218 pos_T old_cursor;
7219 colnr_T left, right;
7220
7221 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7222 {
7223 old_cursor = curwin->w_cursor;
7224 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7225 curwin->w_cursor.lnum = VIsual.lnum;
7226 coladvance(left);
7227 VIsual = curwin->w_cursor;
7228
7229 curwin->w_cursor.lnum = old_cursor.lnum;
7230 curwin->w_curswant = right;
7231 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7232 * right one column */
7233 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7234 ++curwin->w_curswant;
7235 coladvance(curwin->w_curswant);
7236 if (curwin->w_cursor.col == old_cursor.col
7237#ifdef FEAT_VIRTUALEDIT
7238 && (!virtual_active()
7239 || curwin->w_cursor.coladd == old_cursor.coladd)
7240#endif
7241 )
7242 {
7243 curwin->w_cursor.lnum = VIsual.lnum;
7244 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7245 ++right;
7246 coladvance(right);
7247 VIsual = curwin->w_cursor;
7248
7249 curwin->w_cursor.lnum = old_cursor.lnum;
7250 coladvance(left);
7251 curwin->w_curswant = left;
7252 }
7253 }
7254 else
7255 {
7256 old_cursor = curwin->w_cursor;
7257 curwin->w_cursor = VIsual;
7258 VIsual = old_cursor;
7259 curwin->w_set_curswant = TRUE;
7260 }
7261}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262
7263/*
7264 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7265 */
7266 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007267nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 if (VIsual_active) /* "R" is replace lines */
7270 {
7271 cap->cmdchar = 'c';
7272 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007273 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 VIsual_mode = 'V';
7275 nv_operator(cap);
7276 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007277 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 {
7279 if (!curbuf->b_p_ma)
7280 EMSG(_(e_modifiable));
7281 else
7282 {
7283#ifdef FEAT_VIRTUALEDIT
7284 if (virtual_active())
7285 coladvance(getviscol());
7286#endif
7287 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7288 }
7289 }
7290}
7291
7292#ifdef FEAT_VREPLACE
7293/*
7294 * "gr".
7295 */
7296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007297nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (VIsual_active)
7300 {
7301 cap->cmdchar = 'r';
7302 cap->nchar = cap->extra_char;
7303 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7304 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007305 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
7307 if (!curbuf->b_p_ma)
7308 EMSG(_(e_modifiable));
7309 else
7310 {
7311 if (cap->extra_char == Ctrl_V) /* get another character */
7312 cap->extra_char = get_literal();
7313 stuffcharReadbuff(cap->extra_char);
7314 stuffcharReadbuff(ESC);
7315# ifdef FEAT_VIRTUALEDIT
7316 if (virtual_active())
7317 coladvance(getviscol());
7318# endif
7319 invoke_edit(cap, TRUE, 'v', FALSE);
7320 }
7321 }
7322}
7323#endif
7324
7325/*
7326 * Swap case for "~" command, when it does not work like an operator.
7327 */
7328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007329n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
7331 long n;
7332 pos_T startpos;
7333 int did_change = 0;
7334#ifdef FEAT_NETBEANS_INTG
7335 pos_T pos;
7336 char_u *ptr;
7337 int count;
7338#endif
7339
7340 if (checkclearopq(cap->oap))
7341 return;
7342
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007343 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 {
7345 clearopbeep(cap->oap);
7346 return;
7347 }
7348
7349 prep_redo_cmd(cap);
7350
7351 if (u_save_cursor() == FAIL)
7352 return;
7353
7354 startpos = curwin->w_cursor;
7355#ifdef FEAT_NETBEANS_INTG
7356 pos = startpos;
7357#endif
7358 for (n = cap->count1; n > 0; --n)
7359 {
7360 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7361 inc_cursor();
7362 if (gchar_cursor() == NUL)
7363 {
7364 if (vim_strchr(p_ww, '~') != NULL
7365 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7366 {
7367#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007368 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
7370 if (did_change)
7371 {
7372 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007373 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007374 netbeans_removed(curbuf, pos.lnum, pos.col,
7375 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007377 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 }
7379 pos.col = 0;
7380 pos.lnum++;
7381 }
7382#endif
7383 ++curwin->w_cursor.lnum;
7384 curwin->w_cursor.col = 0;
7385 if (n > 1)
7386 {
7387 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7388 break;
7389 u_clearline();
7390 }
7391 }
7392 else
7393 break;
7394 }
7395 }
7396#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007397 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
7399 ptr = ml_get(pos.lnum);
7400 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007401 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7402 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 }
7404#endif
7405
7406
7407 check_cursor();
7408 curwin->w_set_curswant = TRUE;
7409 if (did_change)
7410 {
7411 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7412 0L);
7413 curbuf->b_op_start = startpos;
7414 curbuf->b_op_end = curwin->w_cursor;
7415 if (curbuf->b_op_end.col > 0)
7416 --curbuf->b_op_end.col;
7417 }
7418}
7419
7420/*
7421 * Move cursor to mark.
7422 */
7423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007424nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425{
7426 if (check_mark(pos) == FAIL)
7427 clearop(cap->oap);
7428 else
7429 {
7430 if (cap->cmdchar == '\''
7431 || cap->cmdchar == '`'
7432 || cap->cmdchar == '['
7433 || cap->cmdchar == ']')
7434 setpcmark();
7435 curwin->w_cursor = *pos;
7436 if (flag)
7437 beginline(BL_WHITE | BL_FIX);
7438 else
7439 check_cursor();
7440 }
7441 cap->oap->motion_type = flag ? MLINE : MCHAR;
7442 if (cap->cmdchar == '`')
7443 cap->oap->use_reg_one = TRUE;
7444 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7445 curwin->w_set_curswant = TRUE;
7446}
7447
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448/*
7449 * Handle commands that are operators in Visual mode.
7450 */
7451 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007452v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
7454 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7455
7456 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007457 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 if (isupper(cap->cmdchar))
7459 {
7460 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007461 {
7462 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7466 curwin->w_curswant = MAXCOL;
7467 }
7468 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7469 nv_operator(cap);
7470}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
7472/*
7473 * "s" and "S" commands.
7474 */
7475 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007476nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7479 {
7480 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007481 {
7482 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 cap->cmdchar = 'c';
7486 nv_operator(cap);
7487 }
7488 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 nv_optrans(cap);
7490}
7491
7492/*
7493 * Abbreviated commands.
7494 */
7495 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007496nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7499 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7500
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 /* in Visual mode these commands are operators */
7502 if (VIsual_active)
7503 v_visop(cap);
7504 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 nv_optrans(cap);
7506}
7507
7508/*
7509 * Translate a command into another command.
7510 */
7511 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007512nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513{
7514 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7515 (char_u *)"d$", (char_u *)"c$",
7516 (char_u *)"cl", (char_u *)"cc",
7517 (char_u *)"yy", (char_u *)":s\r"};
7518 static char_u *str = (char_u *)"xXDCsSY&";
7519
7520 if (!checkclearopq(cap->oap))
7521 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007522 /* In Vi "2D" doesn't delete the next line. Can't translate it
7523 * either, because "2." should also not use the count. */
7524 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7525 {
7526 cap->oap->start = curwin->w_cursor;
7527 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007528#ifdef FEAT_EVAL
7529 set_op_var(OP_DELETE);
7530#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007531 cap->count1 = 1;
7532 nv_dollar(cap);
7533 finish_op = TRUE;
7534 ResetRedobuff();
7535 AppendCharToRedobuff('D');
7536 }
7537 else
7538 {
7539 if (cap->count0)
7540 stuffnumReadbuff(cap->count0);
7541 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 }
7544 cap->opcount = 0;
7545}
7546
7547/*
7548 * "'" and "`" commands. Also for "g'" and "g`".
7549 * cap->arg is TRUE for "'" and "g'".
7550 */
7551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007552nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553{
7554 pos_T *pos;
7555 int c;
7556#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007557 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7559#endif
7560
7561 if (cap->cmdchar == 'g')
7562 c = cap->extra_char;
7563 else
7564 c = cap->nchar;
7565 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7566 if (pos == (pos_T *)-1) /* jumped to other file */
7567 {
7568 if (cap->arg)
7569 {
7570 check_cursor_lnum();
7571 beginline(BL_WHITE | BL_FIX);
7572 }
7573 else
7574 check_cursor();
7575 }
7576 else
7577 nv_cursormark(cap, cap->arg, pos);
7578
7579#ifdef FEAT_VIRTUALEDIT
7580 /* May need to clear the coladd that a mark includes. */
7581 if (!virtual_active())
7582 curwin->w_cursor.coladd = 0;
7583#endif
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007584 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585#ifdef FEAT_FOLDING
7586 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007587 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007588 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 && (fdo_flags & FDO_MARK)
7590 && old_KeyTyped)
7591 foldOpenCursor();
7592#endif
7593}
7594
7595/*
7596 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7597 */
7598 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007599nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
7601#ifdef FEAT_JUMPLIST
7602 pos_T *pos;
7603# ifdef FEAT_FOLDING
7604 linenr_T lnum = curwin->w_cursor.lnum;
7605 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7606# endif
7607
7608 if (!checkclearopq(cap->oap))
7609 {
7610 if (cap->cmdchar == 'g')
7611 pos = movechangelist((int)cap->count1);
7612 else
7613 pos = movemark((int)cap->count1);
7614 if (pos == (pos_T *)-1) /* jump to other file */
7615 {
7616 curwin->w_set_curswant = TRUE;
7617 check_cursor();
7618 }
7619 else if (pos != NULL) /* can jump */
7620 nv_cursormark(cap, FALSE, pos);
7621 else if (cap->cmdchar == 'g')
7622 {
7623 if (curbuf->b_changelistlen == 0)
7624 EMSG(_("E664: changelist is empty"));
7625 else if (cap->count1 < 0)
7626 EMSG(_("E662: At start of changelist"));
7627 else
7628 EMSG(_("E663: At end of changelist"));
7629 }
7630 else
7631 clearopbeep(cap->oap);
7632# ifdef FEAT_FOLDING
7633 if (cap->oap->op_type == OP_NOP
7634 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7635 && (fdo_flags & FDO_MARK)
7636 && old_KeyTyped)
7637 foldOpenCursor();
7638# endif
7639 }
7640#else
7641 clearopbeep(cap->oap);
7642#endif
7643}
7644
7645/*
7646 * Handle '"' command.
7647 */
7648 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007649nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
7651 if (checkclearop(cap->oap))
7652 return;
7653#ifdef FEAT_EVAL
7654 if (cap->nchar == '=')
7655 cap->nchar = get_expr_register();
7656#endif
7657 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7658 {
7659 cap->oap->regname = cap->nchar;
7660 cap->opcount = cap->count0; /* remember count before '"' */
7661#ifdef FEAT_EVAL
7662 set_reg_var(cap->oap->regname);
7663#endif
7664 }
7665 else
7666 clearopbeep(cap->oap);
7667}
7668
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669/*
7670 * Handle "v", "V" and "CTRL-V" commands.
7671 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7672 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007673 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 */
7675 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007676nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007678 if (cap->cmdchar == Ctrl_Q)
7679 cap->cmdchar = Ctrl_V;
7680
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7682 * characterwise, linewise, or blockwise. */
7683 if (cap->oap->op_type != OP_NOP)
7684 {
7685 cap->oap->motion_force = cap->cmdchar;
7686 finish_op = FALSE; /* operator doesn't finish now but later */
7687 return;
7688 }
7689
7690 VIsual_select = cap->arg;
7691 if (VIsual_active) /* change Visual mode */
7692 {
7693 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7694 end_visual_mode();
7695 else /* toggle char/block mode */
7696 { /* or char/line mode */
7697 VIsual_mode = cap->cmdchar;
7698 showmode();
7699 }
7700 redraw_curbuf_later(INVERTED); /* update the inversion */
7701 }
7702 else /* start Visual mode */
7703 {
7704 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007705 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007707 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 VIsual = curwin->w_cursor;
7709
7710 VIsual_active = TRUE;
7711 VIsual_reselect = TRUE;
7712 if (!cap->arg)
7713 /* start Select mode when 'selectmode' contains "cmd" */
7714 may_start_select('c');
7715#ifdef FEAT_MOUSE
7716 setmouse();
7717#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007718 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 redraw_cmdline = TRUE; /* show visual mode later */
7720 /*
7721 * For V and ^V, we multiply the number of lines even if there
7722 * was only one -- webb
7723 */
7724 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7725 {
7726 curwin->w_cursor.lnum +=
7727 resel_VIsual_line_count * cap->count0 - 1;
7728 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7729 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7730 }
7731 VIsual_mode = resel_VIsual_mode;
7732 if (VIsual_mode == 'v')
7733 {
7734 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007735 {
7736 validate_virtcol();
7737 curwin->w_curswant = curwin->w_virtcol
7738 + resel_VIsual_vcol * cap->count0 - 1;
7739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007741 curwin->w_curswant = resel_VIsual_vcol;
7742 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007744 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {
7746 curwin->w_curswant = MAXCOL;
7747 coladvance((colnr_T)MAXCOL);
7748 }
7749 else if (VIsual_mode == Ctrl_V)
7750 {
7751 validate_virtcol();
7752 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007753 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 coladvance(curwin->w_curswant);
7755 }
7756 else
7757 curwin->w_set_curswant = TRUE;
7758 redraw_curbuf_later(INVERTED); /* show the inversion */
7759 }
7760 else
7761 {
7762 if (!cap->arg)
7763 /* start Select mode when 'selectmode' contains "cmd" */
7764 may_start_select('c');
7765 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007766 if (VIsual_mode != 'V' && *p_sel == 'e')
7767 ++cap->count1; /* include one more char */
7768 if (cap->count0 > 0 && --cap->count1 > 0)
7769 {
7770 /* With a count select that many characters or lines. */
7771 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7772 nv_right(cap);
7773 else if (VIsual_mode == 'V')
7774 nv_down(cap);
7775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777 }
7778}
7779
7780/*
7781 * Start selection for Shift-movement keys.
7782 */
7783 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007784start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 /* if 'selectmode' contains "key", start Select mode */
7787 may_start_select('k');
7788 n_start_visual_mode('v');
7789}
7790
7791/*
7792 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7793 */
7794 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007795may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 VIsual_select = (stuff_empty() && typebuf_typed()
7798 && (vim_strchr(p_slm, c) != NULL));
7799}
7800
7801/*
7802 * Start Visual mode "c".
7803 * Should set VIsual_select before calling this.
7804 */
7805 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007806n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007808#ifdef FEAT_CONCEAL
7809 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007810 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007811#endif
7812
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 VIsual_mode = c;
7814 VIsual_active = TRUE;
7815 VIsual_reselect = TRUE;
7816#ifdef FEAT_VIRTUALEDIT
7817 /* Corner case: the 0 position in a tab may change when going into
7818 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7819 */
7820 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007821 {
7822 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825#endif
7826 VIsual = curwin->w_cursor;
7827
7828#ifdef FEAT_FOLDING
7829 foldAdjustVisual();
7830#endif
7831
7832#ifdef FEAT_MOUSE
7833 setmouse();
7834#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007835#ifdef FEAT_CONCEAL
7836 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007837 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007838#endif
7839
Bram Moolenaar09df3122006-01-23 22:23:09 +00007840 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 redraw_cmdline = TRUE; /* show visual mode later */
7842#ifdef FEAT_CLIPBOARD
7843 /* Make sure the clipboard gets updated. Needed because start and
7844 * end may still be the same, and the selection needs to be owned */
7845 clip_star.vmode = NUL;
7846#endif
7847
7848 /* Only need to redraw this line, unless still need to redraw an old
7849 * Visual area (when 'lazyredraw' is set). */
7850 if (curwin->w_redr_type < INVERTED)
7851 {
7852 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7853 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7854 }
7855}
7856
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857
7858/*
7859 * CTRL-W: Window commands
7860 */
7861 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007862nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007864 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007865 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007866 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007867 cap->cmdchar = ':';
7868 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007869 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007870 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007871 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873}
7874
7875/*
7876 * CTRL-Z: Suspend
7877 */
7878 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007879nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (VIsual_active)
7883 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 do_cmdline_cmd((char_u *)"st");
7885}
7886
7887/*
7888 * Commands starting with "g".
7889 */
7890 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007891nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
7893 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 int i;
7896 int flag = FALSE;
7897
7898 switch (cap->nchar)
7899 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007900 case Ctrl_A:
7901 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902#ifdef MEM_PROFILE
7903 /*
7904 * "g^A": dump log of used memory.
7905 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007906 if (!VIsual_active && cap->nchar == Ctrl_A)
7907 vim_mem_profile_dump();
7908 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007910 /*
7911 * "g^A/g^X": sequentially increment visually selected region
7912 */
7913 if (VIsual_active)
7914 {
7915 cap->arg = TRUE;
7916 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007917 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007918 nv_addsub(cap);
7919 }
7920 else
7921 clearopbeep(oap);
7922 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
7924#ifdef FEAT_VREPLACE
7925 /*
7926 * "gR": Enter virtual replace mode.
7927 */
7928 case 'R':
7929 cap->arg = TRUE;
7930 nv_Replace(cap);
7931 break;
7932
7933 case 'r':
7934 nv_vreplace(cap);
7935 break;
7936#endif
7937
7938 case '&':
7939 do_cmdline_cmd((char_u *)"%s//~/&");
7940 break;
7941
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 /*
7943 * "gv": Reselect the previous Visual area. If Visual already active,
7944 * exchange previous and current Visual area.
7945 */
7946 case 'v':
7947 if (checkclearop(oap))
7948 break;
7949
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007950 if ( curbuf->b_visual.vi_start.lnum == 0
7951 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7952 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 beep_flush();
7954 else
7955 {
7956 /* set w_cursor to the start of the Visual area, tpos to the end */
7957 if (VIsual_active)
7958 {
7959 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007960 VIsual_mode = curbuf->b_visual.vi_mode;
7961 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962# ifdef FEAT_EVAL
7963 curbuf->b_visual_mode_eval = i;
7964# endif
7965 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007966 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7967 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007969 tpos = curbuf->b_visual.vi_end;
7970 curbuf->b_visual.vi_end = curwin->w_cursor;
7971 curwin->w_cursor = curbuf->b_visual.vi_start;
7972 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 }
7974 else
7975 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007976 VIsual_mode = curbuf->b_visual.vi_mode;
7977 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7978 tpos = curbuf->b_visual.vi_end;
7979 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 }
7981
7982 VIsual_active = TRUE;
7983 VIsual_reselect = TRUE;
7984
7985 /* Set Visual to the start and w_cursor to the end of the Visual
7986 * area. Make sure they are on an existing character. */
7987 check_cursor();
7988 VIsual = curwin->w_cursor;
7989 curwin->w_cursor = tpos;
7990 check_cursor();
7991 update_topline();
7992 /*
7993 * When called from normal "g" command: start Select mode when
7994 * 'selectmode' contains "cmd". When called for K_SELECT, always
7995 * start Select mode.
7996 */
7997 if (cap->arg)
7998 VIsual_select = TRUE;
7999 else
8000 may_start_select('c');
8001#ifdef FEAT_MOUSE
8002 setmouse();
8003#endif
8004#ifdef FEAT_CLIPBOARD
8005 /* Make sure the clipboard gets updated. Needed because start and
8006 * end are still the same, and the selection needs to be owned */
8007 clip_star.vmode = NUL;
8008#endif
8009 redraw_curbuf_later(INVERTED);
8010 showmode();
8011 }
8012 break;
8013 /*
8014 * "gV": Don't reselect the previous Visual area after a Select mode
8015 * mapping of menu.
8016 */
8017 case 'V':
8018 VIsual_reselect = FALSE;
8019 break;
8020
8021 /*
8022 * "gh": start Select mode.
8023 * "gH": start Select line mode.
8024 * "g^H": start Select block mode.
8025 */
8026 case K_BS:
8027 cap->nchar = Ctrl_H;
8028 /* FALLTHROUGH */
8029 case 'h':
8030 case 'H':
8031 case Ctrl_H:
8032# ifdef EBCDIC
8033 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8034 if (cap->nchar == Ctrl_H)
8035 cap->cmdchar = Ctrl_V;
8036 else
8037# endif
8038 cap->cmdchar = cap->nchar + ('v' - 'h');
8039 cap->arg = TRUE;
8040 nv_visual(cap);
8041 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008042
8043 /* "gn", "gN" visually select next/previous search match
8044 * "gn" selects next match
8045 * "gN" selects previous match
8046 */
8047 case 'N':
8048 case 'n':
8049 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008050 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008051 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052
8053 /*
8054 * "gj" and "gk" two new funny movement keys -- up and down
8055 * movement based on *screen* line rather than *file* line.
8056 */
8057 case 'j':
8058 case K_DOWN:
8059 /* with 'nowrap' it works just like the normal "j" command; also when
8060 * in a closed fold */
8061 if (!curwin->w_p_wrap
8062#ifdef FEAT_FOLDING
8063 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8064#endif
8065 )
8066 {
8067 oap->motion_type = MLINE;
8068 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8069 }
8070 else
8071 i = nv_screengo(oap, FORWARD, cap->count1);
8072 if (i == FAIL)
8073 clearopbeep(oap);
8074 break;
8075
8076 case 'k':
8077 case K_UP:
8078 /* with 'nowrap' it works just like the normal "k" command; also when
8079 * in a closed fold */
8080 if (!curwin->w_p_wrap
8081#ifdef FEAT_FOLDING
8082 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8083#endif
8084 )
8085 {
8086 oap->motion_type = MLINE;
8087 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8088 }
8089 else
8090 i = nv_screengo(oap, BACKWARD, cap->count1);
8091 if (i == FAIL)
8092 clearopbeep(oap);
8093 break;
8094
8095 /*
8096 * "gJ": join two lines without inserting a space.
8097 */
8098 case 'J':
8099 nv_join(cap);
8100 break;
8101
8102 /*
8103 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8104 * "gm": middle of "g0" and "g$".
8105 */
8106 case '^':
8107 flag = TRUE;
8108 /* FALLTHROUGH */
8109
8110 case '0':
8111 case 'm':
8112 case K_HOME:
8113 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 oap->motion_type = MCHAR;
8115 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008116 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008118 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 int width2 = width1 + curwin_col_off2();
8120
8121 validate_virtcol();
8122 i = 0;
8123 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8124 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8125 }
8126 else
8127 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008128 /* Go to the middle of the screen line. When 'number' or
8129 * 'relativenumber' is on and lines are wrapping the middle can be more
8130 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008132 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 + ((curwin->w_p_wrap && i > 0)
8134 ? curwin_col_off2() : 0)) / 2;
8135 coladvance((colnr_T)i);
8136 if (flag)
8137 {
8138 do
8139 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008140 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
8142 curwin->w_set_curswant = TRUE;
8143 break;
8144
8145 case '_':
8146 /* "g_": to the last non-blank character in the line or <count> lines
8147 * downward. */
8148 cap->oap->motion_type = MCHAR;
8149 cap->oap->inclusive = TRUE;
8150 curwin->w_curswant = MAXCOL;
8151 if (cursor_down((long)(cap->count1 - 1),
8152 cap->oap->op_type == OP_NOP) == FAIL)
8153 clearopbeep(cap->oap);
8154 else
8155 {
8156 char_u *ptr = ml_get_curline();
8157
8158 /* In Visual mode we may end up after the line. */
8159 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8160 --curwin->w_cursor.col;
8161
8162 /* Decrease the cursor column until it's on a non-blank. */
8163 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008164 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 --curwin->w_cursor.col;
8166 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008167 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 }
8169 break;
8170
8171 case '$':
8172 case K_END:
8173 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
8175 int col_off = curwin_col_off();
8176
8177 oap->motion_type = MCHAR;
8178 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008179 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {
8181 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8182 if (cap->count1 == 1)
8183 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008184 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 int width2 = width1 + curwin_col_off2();
8186
8187 validate_virtcol();
8188 i = width1 - 1;
8189 if (curwin->w_virtcol >= (colnr_T)width1)
8190 i += ((curwin->w_virtcol - width1) / width2 + 1)
8191 * width2;
8192 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008193
8194 /* Make sure we stick in this column. */
8195 validate_virtcol();
8196 curwin->w_curswant = curwin->w_virtcol;
8197 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8199 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8200 {
8201 /*
8202 * Check for landing on a character that got split at
8203 * the end of the line. We do not want to advance to
8204 * the next screen line.
8205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 if (curwin->w_virtcol > (colnr_T)i)
8207 --curwin->w_cursor.col;
8208 }
8209#endif
8210 }
8211 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8212 clearopbeep(oap);
8213 }
8214 else
8215 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008216 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008218
8219 /* Make sure we stick in this column. */
8220 validate_virtcol();
8221 curwin->w_curswant = curwin->w_virtcol;
8222 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 }
8224 }
8225 break;
8226
8227 /*
8228 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8229 */
8230 case '*':
8231 case '#':
8232#if POUND != '#'
8233 case POUND: /* pound sign (sometimes equal to '#') */
8234#endif
8235 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8236 case ']': /* :tselect for current identifier */
8237 nv_ident(cap);
8238 break;
8239
8240 /*
8241 * ge and gE: go back to end of word
8242 */
8243 case 'e':
8244 case 'E':
8245 oap->motion_type = MCHAR;
8246 curwin->w_set_curswant = TRUE;
8247 oap->inclusive = TRUE;
8248 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8249 clearopbeep(oap);
8250 break;
8251
8252 /*
8253 * "g CTRL-G": display info about cursor position
8254 */
8255 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008256 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 break;
8258
8259 /*
8260 * "gi": start Insert at the last position.
8261 */
8262 case 'i':
8263 if (curbuf->b_last_insert.lnum != 0)
8264 {
8265 curwin->w_cursor = curbuf->b_last_insert;
8266 check_cursor_lnum();
8267 i = (int)STRLEN(ml_get_curline());
8268 if (curwin->w_cursor.col > (colnr_T)i)
8269 {
8270#ifdef FEAT_VIRTUALEDIT
8271 if (virtual_active())
8272 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8273#endif
8274 curwin->w_cursor.col = i;
8275 }
8276 }
8277 cap->cmdchar = 'i';
8278 nv_edit(cap);
8279 break;
8280
8281 /*
8282 * "gI": Start insert in column 1.
8283 */
8284 case 'I':
8285 beginline(0);
8286 if (!checkclearopq(oap))
8287 invoke_edit(cap, FALSE, 'g', FALSE);
8288 break;
8289
8290#ifdef FEAT_SEARCHPATH
8291 /*
8292 * "gf": goto file, edit file under cursor
8293 * "]f" and "[f": can also be used.
8294 */
8295 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008296 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 nv_gotofile(cap);
8298 break;
8299#endif
8300
8301 /* "g'm" and "g`m": jump to mark without setting pcmark */
8302 case '\'':
8303 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008304 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 case '`':
8306 nv_gomark(cap);
8307 break;
8308
8309 /*
8310 * "gs": Goto sleep.
8311 */
8312 case 's':
8313 do_sleep(cap->count1 * 1000L);
8314 break;
8315
8316 /*
8317 * "ga": Display the ascii value of the character under the
8318 * cursor. It is displayed in decimal, hex, and octal. -- webb
8319 */
8320 case 'a':
8321 do_ascii(NULL);
8322 break;
8323
8324#ifdef FEAT_MBYTE
8325 /*
8326 * "g8": Display the bytes used for the UTF-8 character under the
8327 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008328 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 */
8330 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008331 if (cap->count0 == 8)
8332 utf_find_illegal();
8333 else
8334 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 break;
8336#endif
8337
Bram Moolenaar60402d62017-04-20 18:54:50 +02008338 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008339 case '<':
8340 show_sb_text();
8341 break;
8342
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 /*
8344 * "gg": Goto the first line in file. With a count it goes to
8345 * that line number like for "G". -- webb
8346 */
8347 case 'g':
8348 cap->arg = FALSE;
8349 nv_goto(cap);
8350 break;
8351
8352 /*
8353 * Two-character operators:
8354 * "gq" Format text
8355 * "gw" Format text and keep cursor position
8356 * "g~" Toggle the case of the text.
8357 * "gu" Change text to lower case.
8358 * "gU" Change text to upper case.
8359 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008360 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 */
8362 case 'q':
8363 case 'w':
8364 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008365 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 case '~':
8367 case 'u':
8368 case 'U':
8369 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008370 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 nv_operator(cap);
8372 break;
8373
8374 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008375 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 * current function
8377 * "gD": idem, but in the current file.
8378 */
8379 case 'd':
8380 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008381 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 break;
8383
8384#ifdef FEAT_MOUSE
8385 /*
8386 * g<*Mouse> : <C-*mouse>
8387 */
8388 case K_MIDDLEMOUSE:
8389 case K_MIDDLEDRAG:
8390 case K_MIDDLERELEASE:
8391 case K_LEFTMOUSE:
8392 case K_LEFTDRAG:
8393 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008394 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 case K_RIGHTMOUSE:
8396 case K_RIGHTDRAG:
8397 case K_RIGHTRELEASE:
8398 case K_X1MOUSE:
8399 case K_X1DRAG:
8400 case K_X1RELEASE:
8401 case K_X2MOUSE:
8402 case K_X2DRAG:
8403 case K_X2RELEASE:
8404 mod_mask = MOD_MASK_CTRL;
8405 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8406 break;
8407#endif
8408
8409 case K_IGNORE:
8410 break;
8411
8412 /*
8413 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8414 */
8415 case 'p':
8416 case 'P':
8417 nv_put(cap);
8418 break;
8419
8420#ifdef FEAT_BYTEOFF
8421 /* "go": goto byte count from start of buffer */
8422 case 'o':
8423 goto_byte(cap->count0);
8424 break;
8425#endif
8426
8427 /* "gQ": improved Ex mode */
8428 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008429 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {
8431 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008432 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 break;
8434 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 if (!checkclearopq(oap))
8437 do_exmode(TRUE);
8438 break;
8439
8440#ifdef FEAT_JUMPLIST
8441 case ',':
8442 nv_pcmark(cap);
8443 break;
8444
8445 case ';':
8446 cap->count1 = -cap->count1;
8447 nv_pcmark(cap);
8448 break;
8449#endif
8450
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008451 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008452 if (!checkclearop(oap))
8453 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008454 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008455 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008456 if (!checkclearop(oap))
8457 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008458 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008459
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008460 case '+':
8461 case '-': /* "g+" and "g-": undo or redo along the timeline */
8462 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008463 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008464 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008465 break;
8466
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 default:
8468 clearopbeep(oap);
8469 break;
8470 }
8471}
8472
8473/*
8474 * Handle "o" and "O" commands.
8475 */
8476 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008477n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008479#ifdef FEAT_CONCEAL
8480 linenr_T oldline = curwin->w_cursor.lnum;
8481#endif
8482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 if (!checkclearopq(cap->oap))
8484 {
8485#ifdef FEAT_FOLDING
8486 if (cap->cmdchar == 'O')
8487 /* Open above the first line of a folded sequence of lines */
8488 (void)hasFolding(curwin->w_cursor.lnum,
8489 &curwin->w_cursor.lnum, NULL);
8490 else
8491 /* Open below the last line of a folded sequence of lines */
8492 (void)hasFolding(curwin->w_cursor.lnum,
8493 NULL, &curwin->w_cursor.lnum);
8494#endif
8495 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8496 (cap->cmdchar == 'O' ? 1 : 0)),
8497 (linenr_T)(curwin->w_cursor.lnum +
8498 (cap->cmdchar == 'o' ? 1 : 0))
8499 ) == OK
8500 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8501#ifdef FEAT_COMMENTS
8502 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8503#endif
8504 0, 0))
8505 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008506#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008507 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008508 update_single_line(curwin, oldline);
8509#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008510 /* When '#' is in 'cpoptions' ignore the count. */
8511 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8512 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008513#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008514 if (curwin->w_p_cul)
8515 /* force redraw of cursorline */
8516 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8519 }
8520 }
8521}
8522
8523/*
8524 * "." command: redo last change.
8525 */
8526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008527nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 if (!checkclearopq(cap->oap))
8530 {
8531 /*
8532 * If "restart_edit" is TRUE, the last but one command is repeated
8533 * instead of the last command (inserting text). This is used for
8534 * CTRL-O <.> in insert mode.
8535 */
8536 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8537 clearopbeep(cap->oap);
8538 }
8539}
8540
8541/*
8542 * CTRL-R: undo undo
8543 */
8544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008545nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 if (!checkclearopq(cap->oap))
8548 {
8549 u_redo((int)cap->count1);
8550 curwin->w_set_curswant = TRUE;
8551 }
8552}
8553
8554/*
8555 * Handle "U" command.
8556 */
8557 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008558nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559{
8560 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008561 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {
8563 /* translate "gUU" to "gUgU" */
8564 cap->cmdchar = 'g';
8565 cap->nchar = 'U';
8566 nv_operator(cap);
8567 }
8568 else if (!checkclearopq(cap->oap))
8569 {
8570 u_undoline();
8571 curwin->w_set_curswant = TRUE;
8572 }
8573}
8574
8575/*
8576 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8577 * single character.
8578 */
8579 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008580nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008582 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 n_swapchar(cap);
8584 else
8585 nv_operator(cap);
8586}
8587
8588/*
8589 * Handle an operator command.
8590 * The actual work is done by do_pending_operator().
8591 */
8592 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008593nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
8595 int op_type;
8596
8597 op_type = get_op_type(cap->cmdchar, cap->nchar);
8598
8599 if (op_type == cap->oap->op_type) /* double operator works on lines */
8600 nv_lineop(cap);
8601 else if (!checkclearop(cap->oap))
8602 {
8603 cap->oap->start = curwin->w_cursor;
8604 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008605#ifdef FEAT_EVAL
8606 set_op_var(op_type);
8607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 }
8609}
8610
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008611#ifdef FEAT_EVAL
8612/*
8613 * Set v:operator to the characters for "optype".
8614 */
8615 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008616set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008617{
8618 char_u opchars[3];
8619
8620 if (optype == OP_NOP)
8621 set_vim_var_string(VV_OP, NULL, 0);
8622 else
8623 {
8624 opchars[0] = get_op_char(optype);
8625 opchars[1] = get_extra_op_char(optype);
8626 opchars[2] = NUL;
8627 set_vim_var_string(VV_OP, opchars, -1);
8628 }
8629}
8630#endif
8631
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632/*
8633 * Handle linewise operator "dd", "yy", etc.
8634 *
8635 * "_" is is a strange motion command that helps make operators more logical.
8636 * It is actually implemented, but not documented in the real Vi. This motion
8637 * command actually refers to "the current line". Commands like "dd" and "yy"
8638 * are really an alternate form of "d_" and "y_". It does accept a count, so
8639 * "d3_" works to delete 3 lines.
8640 */
8641 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008642nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
8644 cap->oap->motion_type = MLINE;
8645 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8646 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008647 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8648 && cap->oap->motion_force != 'v'
8649 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 || cap->oap->op_type == OP_LSHIFT
8651 || cap->oap->op_type == OP_RSHIFT)
8652 beginline(BL_SOL | BL_FIX);
8653 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8654 beginline(BL_WHITE | BL_FIX);
8655}
8656
8657/*
8658 * <Home> command.
8659 */
8660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008661nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008663 /* CTRL-HOME is like "gg" */
8664 if (mod_mask & MOD_MASK_CTRL)
8665 nv_goto(cap);
8666 else
8667 {
8668 cap->count0 = 1;
8669 nv_pipe(cap);
8670 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008671 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8672 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673}
8674
8675/*
8676 * "|" command.
8677 */
8678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008679nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680{
8681 cap->oap->motion_type = MCHAR;
8682 cap->oap->inclusive = FALSE;
8683 beginline(0);
8684 if (cap->count0 > 0)
8685 {
8686 coladvance((colnr_T)(cap->count0 - 1));
8687 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8688 }
8689 else
8690 curwin->w_curswant = 0;
8691 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008692 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 curwin->w_set_curswant = FALSE;
8694}
8695
8696/*
8697 * Handle back-word command "b" and "B".
8698 * cap->arg is 1 for "B"
8699 */
8700 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008701nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 cap->oap->motion_type = MCHAR;
8704 cap->oap->inclusive = FALSE;
8705 curwin->w_set_curswant = TRUE;
8706 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8707 clearopbeep(cap->oap);
8708#ifdef FEAT_FOLDING
8709 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8710 foldOpenCursor();
8711#endif
8712}
8713
8714/*
8715 * Handle word motion commands "e", "E", "w" and "W".
8716 * cap->arg is TRUE for "E" and "W".
8717 */
8718 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008719nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
8721 int n;
8722 int word_end;
8723 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008724 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725
8726 /*
8727 * Set inclusive for the "E" and "e" command.
8728 */
8729 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8730 word_end = TRUE;
8731 else
8732 word_end = FALSE;
8733 cap->oap->inclusive = word_end;
8734
8735 /*
8736 * "cw" and "cW" are a special case.
8737 */
8738 if (!word_end && cap->oap->op_type == OP_CHANGE)
8739 {
8740 n = gchar_cursor();
8741 if (n != NUL) /* not an empty line */
8742 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008743 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {
8745 /*
8746 * Reproduce a funny Vi behaviour: "cw" on a blank only
8747 * changes one character, not all blanks until the start of
8748 * the next word. Only do this when the 'w' flag is included
8749 * in 'cpoptions'.
8750 */
8751 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8752 {
8753 cap->oap->inclusive = TRUE;
8754 cap->oap->motion_type = MCHAR;
8755 return;
8756 }
8757 }
8758 else
8759 {
8760 /*
8761 * This is a little strange. To match what the real Vi does,
8762 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8763 * that we are not on a space or a TAB. This seems impolite
8764 * at first, but it's really more what we mean when we say
8765 * 'cw'.
8766 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008767 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 * will change only one character! This is done by setting
8769 * flag.
8770 */
8771 cap->oap->inclusive = TRUE;
8772 word_end = TRUE;
8773 flag = TRUE;
8774 }
8775 }
8776 }
8777
8778 cap->oap->motion_type = MCHAR;
8779 curwin->w_set_curswant = TRUE;
8780 if (word_end)
8781 n = end_word(cap->count1, cap->arg, flag, FALSE);
8782 else
8783 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8784
Bram Moolenaardfefb982008-04-01 10:06:39 +00008785 /* Don't leave the cursor on the NUL past the end of line. Unless we
8786 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008787 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008788 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789
8790 if (n == FAIL && cap->oap->op_type == OP_NOP)
8791 clearopbeep(cap->oap);
8792 else
8793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795#ifdef FEAT_FOLDING
8796 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8797 foldOpenCursor();
8798#endif
8799 }
8800}
8801
8802/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008803 * Used after a movement command: If the cursor ends up on the NUL after the
8804 * end of the line, may move it back to the last character and make the motion
8805 * inclusive.
8806 */
8807 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008808adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008809{
8810 /* The cursor cannot remain on the NUL when:
8811 * - the column is > 0
8812 * - not in Visual mode or 'selection' is "o"
8813 * - 'virtualedit' is not "all" and not "onemore".
8814 */
8815 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008816 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008817#ifdef FEAT_VIRTUALEDIT
8818 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8819#endif
8820 )
8821 {
8822 --curwin->w_cursor.col;
8823#ifdef FEAT_MBYTE
8824 /* prevent cursor from moving on the trail byte */
8825 if (has_mbyte)
8826 mb_adjust_cursor();
8827#endif
8828 oap->inclusive = TRUE;
8829 }
8830}
8831
8832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 * "0" and "^" commands.
8834 * cap->arg is the argument for beginline().
8835 */
8836 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008837nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 cap->oap->motion_type = MCHAR;
8840 cap->oap->inclusive = FALSE;
8841 beginline(cap->arg);
8842#ifdef FEAT_FOLDING
8843 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8844 foldOpenCursor();
8845#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008846 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8847 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848}
8849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850/*
8851 * In exclusive Visual mode, may include the last character.
8852 */
8853 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008854adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
8856 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008857 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008859#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 if (has_mbyte)
8861 inc_cursor();
8862 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 ++curwin->w_cursor.col;
8865 cap->oap->inclusive = FALSE;
8866 }
8867}
8868
8869/*
8870 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8871 * Should check VIsual_mode before calling this.
8872 * Returns TRUE when backed up to the previous line.
8873 */
8874 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008875unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 pos_T *pp;
8878
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008879 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008881 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 pp = &curwin->w_cursor;
8883 else
8884 pp = &VIsual;
8885#ifdef FEAT_VIRTUALEDIT
8886 if (pp->coladd > 0)
8887 --pp->coladd;
8888 else
8889#endif
8890 if (pp->col > 0)
8891 {
8892 --pp->col;
8893#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008894 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895#endif
8896 }
8897 else if (pp->lnum > 1)
8898 {
8899 --pp->lnum;
8900 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8901 return TRUE;
8902 }
8903 }
8904 return FALSE;
8905}
8906
8907/*
8908 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8909 */
8910 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008911nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912{
8913 if (VIsual_active)
8914 VIsual_select = TRUE;
8915 else if (VIsual_reselect)
8916 {
8917 cap->nchar = 'v'; /* fake "gv" command */
8918 cap->arg = TRUE;
8919 nv_g_cmd(cap);
8920 }
8921}
8922
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
8924/*
8925 * "G", "gg", CTRL-END, CTRL-HOME.
8926 * cap->arg is TRUE for "G".
8927 */
8928 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008929nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930{
8931 linenr_T lnum;
8932
8933 if (cap->arg)
8934 lnum = curbuf->b_ml.ml_line_count;
8935 else
8936 lnum = 1L;
8937 cap->oap->motion_type = MLINE;
8938 setpcmark();
8939
8940 /* When a count is given, use it instead of the default lnum */
8941 if (cap->count0 != 0)
8942 lnum = cap->count0;
8943 if (lnum < 1L)
8944 lnum = 1L;
8945 else if (lnum > curbuf->b_ml.ml_line_count)
8946 lnum = curbuf->b_ml.ml_line_count;
8947 curwin->w_cursor.lnum = lnum;
8948 beginline(BL_SOL | BL_FIX);
8949#ifdef FEAT_FOLDING
8950 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8951 foldOpenCursor();
8952#endif
8953}
8954
8955/*
8956 * CTRL-\ in Normal mode.
8957 */
8958 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008959nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
8961 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8962 {
8963 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008964 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 clear_cmdline = TRUE; /* unshow mode later */
8966 restart_edit = 0;
8967#ifdef FEAT_CMDWIN
8968 if (cmdwin_type != 0)
8969 cmdwin_result = Ctrl_C;
8970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 if (VIsual_active)
8972 {
8973 end_visual_mode(); /* stop Visual */
8974 redraw_curbuf_later(INVERTED);
8975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8977 if (cap->nchar == Ctrl_G && p_im)
8978 restart_edit = 'a';
8979 }
8980 else
8981 clearopbeep(cap->oap);
8982}
8983
8984/*
8985 * ESC in Normal mode: beep, but don't flush buffers.
8986 * Don't even beep if we are canceling a command.
8987 */
8988 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008989nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
8991 int no_reason;
8992
8993 no_reason = (cap->oap->op_type == OP_NOP
8994 && cap->opcount == 0
8995 && cap->count0 == 0
8996 && cap->oap->regname == 0
8997 && !p_im);
8998
8999 if (cap->arg) /* TRUE for CTRL-C */
9000 {
9001 if (restart_edit == 0
9002#ifdef FEAT_CMDWIN
9003 && cmdwin_type == 0
9004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02009007 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008
9009 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9010 * set again below when halfway a mapping. */
9011 if (!p_im)
9012 restart_edit = 0;
9013#ifdef FEAT_CMDWIN
9014 if (cmdwin_type != 0)
9015 {
9016 cmdwin_result = K_IGNORE;
9017 got_int = FALSE; /* don't stop executing autocommands et al. */
9018 return;
9019 }
9020#endif
9021 }
9022
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 if (VIsual_active)
9024 {
9025 end_visual_mode(); /* stop Visual */
9026 check_cursor_col(); /* make sure cursor is not beyond EOL */
9027 curwin->w_set_curswant = TRUE;
9028 redraw_curbuf_later(INVERTED);
9029 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009030 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009031 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 clearop(cap->oap);
9033
9034 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9035 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009036 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 restart_edit = 'a';
9038}
9039
9040/*
9041 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009042 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 */
9044 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009045nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046{
9047 /* <Insert> is equal to "i" */
9048 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9049 cap->cmdchar = 'i';
9050
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 /* in Visual mode "A" and "I" are an operator */
9052 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02009053 {
9054#ifdef FEAT_TERMINAL
9055 if (term_in_normal_mode())
9056 {
9057 end_visual_mode();
9058 clearop(cap->oap);
9059 term_enter_job_mode();
9060 return;
9061 }
9062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02009064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
9066 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009067 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9068 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 {
9070#ifdef FEAT_TEXTOBJ
9071 nv_object(cap);
9072#else
9073 clearopbeep(cap->oap);
9074#endif
9075 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009076#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009077 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009078 {
9079 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009080 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009081 return;
9082 }
9083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 else if (!curbuf->b_p_ma && !p_im)
9085 {
9086 /* Only give this error when 'insertmode' is off. */
9087 EMSG(_(e_modifiable));
9088 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009089 if (cap->cmdchar == K_PS)
9090 /* drop the pasted text */
9091 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009093 else if (cap->cmdchar == K_PS && VIsual_active)
9094 {
9095 pos_T old_pos = curwin->w_cursor;
9096 pos_T old_visual = VIsual;
9097
9098 /* In Visual mode the selected text is deleted. */
9099 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9100 {
9101 shift_delete_registers();
9102 cap->oap->regname = '1';
9103 }
9104 else
9105 cap->oap->regname = '-';
9106 cap->cmdchar = 'd';
9107 cap->nchar = NUL;
9108 nv_operator(cap);
9109 do_pending_operator(cap, 0, FALSE);
9110 cap->cmdchar = K_PS;
9111
9112 /* When the last char in the line was deleted then append. Detect this
9113 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009114 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9115 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009116 inc_cursor();
9117
9118 /* Insert to replace the deleted text with the pasted text. */
9119 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 else if (!checkclearopq(cap->oap))
9122 {
9123 switch (cap->cmdchar)
9124 {
9125 case 'A': /* "A"ppend after the line */
9126 curwin->w_set_curswant = TRUE;
9127#ifdef FEAT_VIRTUALEDIT
9128 if (ve_flags == VE_ALL)
9129 {
9130 int save_State = State;
9131
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009132 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 * character past the end of the line */
9134 State = INSERT;
9135 coladvance((colnr_T)MAXCOL);
9136 State = save_State;
9137 }
9138 else
9139#endif
9140 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9141 break;
9142
9143 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009144 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9145 beginline(BL_WHITE);
9146 else
9147 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 break;
9149
Bram Moolenaara1891842017-02-04 21:34:31 +01009150 case K_PS:
9151 /* Bracketed paste works like "a"ppend, unless the cursor is in
9152 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009153 if (curwin->w_cursor.col == 0)
9154 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009155 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009156
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9158#ifdef FEAT_VIRTUALEDIT
9159 /* increment coladd when in virtual space, increment the
9160 * column otherwise, also to append after an unprintable char */
9161 if (virtual_active()
9162 && (curwin->w_cursor.coladd > 0
9163 || *ml_get_cursor() == NUL
9164 || *ml_get_cursor() == TAB))
9165 curwin->w_cursor.coladd++;
9166 else
9167#endif
9168 if (*ml_get_cursor() != NUL)
9169 inc_cursor();
9170 break;
9171 }
9172
9173#ifdef FEAT_VIRTUALEDIT
9174 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9175 {
9176 int save_State = State;
9177
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009178 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 * character past the end of the line */
9180 State = INSERT;
9181 coladvance(getviscol());
9182 State = save_State;
9183 }
9184#endif
9185
9186 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9187 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009188 else if (cap->cmdchar == K_PS)
9189 /* drop the pasted text */
9190 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191}
9192
9193/*
9194 * Invoke edit() and take care of "restart_edit" and the return value.
9195 */
9196 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009197invoke_edit(
9198 cmdarg_T *cap,
9199 int repl, /* "r" or "gr" command */
9200 int cmd,
9201 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 int restart_edit_save = 0;
9204
9205 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9206 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9207 * it. */
9208 if (repl || !stuff_empty())
9209 restart_edit_save = restart_edit;
9210 else
9211 restart_edit_save = 0;
9212
9213 /* Always reset "restart_edit", this is not a restarted edit. */
9214 restart_edit = 0;
9215
9216 if (edit(cmd, startln, cap->count1))
9217 cap->retval |= CA_COMMAND_BUSY;
9218
9219 if (restart_edit == 0)
9220 restart_edit = restart_edit_save;
9221}
9222
9223#ifdef FEAT_TEXTOBJ
9224/*
9225 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9226 */
9227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009228nv_object(
9229 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230{
9231 int flag;
9232 int include;
9233 char_u *mps_save;
9234
9235 if (cap->cmdchar == 'i')
9236 include = FALSE; /* "ix" = inner object: exclude white space */
9237 else
9238 include = TRUE; /* "ax" = an object: include white space */
9239
9240 /* Make sure (), [], {} and <> are in 'matchpairs' */
9241 mps_save = curbuf->b_p_mps;
9242 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9243
9244 switch (cap->nchar)
9245 {
9246 case 'w': /* "aw" = a word */
9247 flag = current_word(cap->oap, cap->count1, include, FALSE);
9248 break;
9249 case 'W': /* "aW" = a WORD */
9250 flag = current_word(cap->oap, cap->count1, include, TRUE);
9251 break;
9252 case 'b': /* "ab" = a braces block */
9253 case '(':
9254 case ')':
9255 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9256 break;
9257 case 'B': /* "aB" = a Brackets block */
9258 case '{':
9259 case '}':
9260 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9261 break;
9262 case '[': /* "a[" = a [] block */
9263 case ']':
9264 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9265 break;
9266 case '<': /* "a<" = a <> block */
9267 case '>':
9268 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9269 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009270 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009271 /* Do not adjust oap->end in do_pending_operator()
9272 * otherwise there are different results for 'dit'
9273 * (note leading whitespace in last line):
9274 * 1) <b> 2) <b>
9275 * foobar foobar
9276 * </b> </b>
9277 */
9278 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009279 flag = current_tagblock(cap->oap, cap->count1, include);
9280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 case 'p': /* "ap" = a paragraph */
9282 flag = current_par(cap->oap, cap->count1, include, 'p');
9283 break;
9284 case 's': /* "as" = a sentence */
9285 flag = current_sent(cap->oap, cap->count1, include);
9286 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009287 case '"': /* "a"" = a double quoted string */
9288 case '\'': /* "a'" = a single quoted string */
9289 case '`': /* "a`" = a backtick quoted string */
9290 flag = current_quote(cap->oap, cap->count1, include,
9291 cap->nchar);
9292 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293#if 0 /* TODO */
9294 case 'S': /* "aS" = a section */
9295 case 'f': /* "af" = a filename */
9296 case 'u': /* "au" = a URL */
9297#endif
9298 default:
9299 flag = FAIL;
9300 break;
9301 }
9302
9303 curbuf->b_p_mps = mps_save;
9304 if (flag == FAIL)
9305 clearopbeep(cap->oap);
9306 adjust_cursor_col();
9307 curwin->w_set_curswant = TRUE;
9308}
9309#endif
9310
9311/*
9312 * "q" command: Start/stop recording.
9313 * "q:", "q/", "q?": edit command-line in command-line window.
9314 */
9315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009316nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317{
9318 if (cap->oap->op_type == OP_FORMAT)
9319 {
9320 /* "gqq" is the same as "gqgq": format line */
9321 cap->cmdchar = 'g';
9322 cap->nchar = 'q';
9323 nv_operator(cap);
9324 }
9325 else if (!checkclearop(cap->oap))
9326 {
9327#ifdef FEAT_CMDWIN
9328 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9329 {
9330 stuffcharReadbuff(cap->nchar);
9331 stuffcharReadbuff(K_CMDWIN);
9332 }
9333 else
9334#endif
9335 /* (stop) recording into a named register, unless executing a
9336 * register */
9337 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9338 clearopbeep(cap->oap);
9339 }
9340}
9341
9342/*
9343 * Handle the "@r" command.
9344 */
9345 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009346nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347{
9348 if (checkclearop(cap->oap))
9349 return;
9350#ifdef FEAT_EVAL
9351 if (cap->nchar == '=')
9352 {
9353 if (get_expr_register() == NUL)
9354 return;
9355 }
9356#endif
9357 while (cap->count1-- && !got_int)
9358 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009359 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 {
9361 clearopbeep(cap->oap);
9362 break;
9363 }
9364 line_breakcheck();
9365 }
9366}
9367
9368/*
9369 * Handle the CTRL-U and CTRL-D commands.
9370 */
9371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009372nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373{
9374 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9375 || (cap->cmdchar == Ctrl_D
9376 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9377 clearopbeep(cap->oap);
9378 else if (!checkclearop(cap->oap))
9379 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9380}
9381
9382/*
9383 * Handle "J" or "gJ" command.
9384 */
9385 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009386nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 if (VIsual_active) /* join the visual lines */
9389 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009390 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 {
9392 if (cap->count0 <= 1)
9393 cap->count0 = 2; /* default for join is two lines! */
9394 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9395 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009397 /* can't join when on the last line */
9398 if (cap->count0 <= 2)
9399 {
9400 clearopbeep(cap->oap);
9401 return;
9402 }
9403 cap->count0 = curbuf->b_ml.ml_line_count
9404 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009406
9407 prep_redo(cap->oap->regname, cap->count0,
9408 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9409 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 }
9411}
9412
9413/*
9414 * "P", "gP", "p" and "gp" commands.
9415 */
9416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009417nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 int regname = 0;
9420 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009421 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009422 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 int dir;
9424 int flags = 0;
9425
9426 if (cap->oap->op_type != OP_NOP)
9427 {
9428#ifdef FEAT_DIFF
9429 /* "dp" is ":diffput" */
9430 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9431 {
9432 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009433 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434 }
9435 else
9436#endif
9437 clearopbeep(cap->oap);
9438 }
9439 else
9440 {
9441 dir = (cap->cmdchar == 'P'
9442 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9443 ? BACKWARD : FORWARD;
9444 prep_redo_cmd(cap);
9445 if (cap->cmdchar == 'g')
9446 flags |= PUT_CURSEND;
9447
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 if (VIsual_active)
9449 {
9450 /* Putting in Visual mode: The put text replaces the selected
9451 * text. First delete the selected text, then put the new text.
9452 * Need to save and restore the registers that the delete
9453 * overwrites if the old contents is being put.
9454 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009455 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009457#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009459#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009460 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009461 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009462#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465
9466 )
9467 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009468 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 * put, save it first. */
9470 reg1 = get_register(regname, TRUE);
9471 }
9472
9473 /* Now delete the selected text. */
9474 cap->cmdchar = 'd';
9475 cap->nchar = NUL;
9476 cap->oap->regname = NUL;
9477 nv_operator(cap);
9478 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009479 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480
9481 /* delete PUT_LINE_BACKWARD; */
9482 cap->oap->regname = regname;
9483
9484 if (reg1 != NULL)
9485 {
9486 /* Delete probably changed the register we want to put, save
9487 * it first. Then put back what was there before the delete. */
9488 reg2 = get_register(regname, FALSE);
9489 put_register(regname, reg1);
9490 }
9491
9492 /* When deleted a linewise Visual area, put the register as
9493 * lines to avoid it joined with the next line. When deletion was
9494 * characterwise, split a line when putting lines. */
9495 if (VIsual_mode == 'V')
9496 flags |= PUT_LINE;
9497 else if (VIsual_mode == 'v')
9498 flags |= PUT_LINE_SPLIT;
9499 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9500 flags |= PUT_LINE_FORWARD;
9501 dir = BACKWARD;
9502 if ((VIsual_mode != 'V'
9503 && curwin->w_cursor.col < curbuf->b_op_start.col)
9504 || (VIsual_mode == 'V'
9505 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9506 /* cursor is at the end of the line or end of file, put
9507 * forward. */
9508 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009509 /* May have been reset in do_put(). */
9510 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 do_put(cap->oap->regname, dir, cap->count1, flags);
9513
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 /* If a register was saved, put it back now. */
9515 if (reg2 != NULL)
9516 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009517
9518 /* What to reselect with "gv"? Selecting the just put text seems to
9519 * be the most useful, since the original text was removed. */
9520 if (was_visual)
9521 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009522 curbuf->b_visual.vi_start = curbuf->b_op_start;
9523 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009524 /* need to adjust cursor position */
9525 if (*p_sel == 'e')
9526 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009527 }
9528
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009529 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009530 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009531 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009532 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009533 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009534
9535 /* If the cursor was in that line, move it to the end of the last
9536 * line. */
9537 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9538 {
9539 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9540 coladvance((colnr_T)MAXCOL);
9541 }
9542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 auto_format(FALSE, TRUE);
9544 }
9545}
9546
9547/*
9548 * "o" and "O" commands.
9549 */
9550 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009551nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553#ifdef FEAT_DIFF
9554 /* "do" is ":diffget" */
9555 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9556 {
9557 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009558 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 }
9560 else
9561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 if (VIsual_active) /* switch start and end of visual */
9563 v_swap_corners(cap->cmdchar);
9564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 n_opencmd(cap);
9566}
9567
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568#ifdef FEAT_NETBEANS_INTG
9569 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009570nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
9572 netbeans_keycommand(cap->nchar);
9573}
9574#endif
9575
9576#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009578nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9581}
9582#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009583
9584#ifdef FEAT_AUTOCMD
9585/*
9586 * Trigger CursorHold event.
9587 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9588 * input buffer. "did_cursorhold" is set to avoid retriggering.
9589 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009590 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009591nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009592{
9593 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9594 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009595 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009596}
9597#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009598
9599/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009600 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009601 */
9602 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009603get_op_vcol(
9604 oparg_T *oap,
9605 colnr_T redo_VIsual_vcol,
9606 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009607{
9608 colnr_T start, end;
9609
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009610 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009611 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009612 return;
9613
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009614 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009615
9616#ifdef FEAT_MBYTE
9617 /* prevent from moving onto a trail byte */
9618 if (has_mbyte)
9619 mb_adjustpos(curwin->w_buffer, &oap->end);
9620#endif
9621
9622 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009623
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009624 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009625 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009626 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9627
9628 if (start < oap->start_vcol)
9629 oap->start_vcol = start;
9630 if (end > oap->end_vcol)
9631 {
9632 if (initial && *p_sel == 'e' && start >= 1
9633 && start - 1 >= oap->end_vcol)
9634 oap->end_vcol = start - 1;
9635 else
9636 oap->end_vcol = end;
9637 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009638 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009639
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009640 /* if '$' was used, get oap->end_vcol from longest line */
9641 if (curwin->w_curswant == MAXCOL)
9642 {
9643 curwin->w_cursor.col = MAXCOL;
9644 oap->end_vcol = 0;
9645 for (curwin->w_cursor.lnum = oap->start.lnum;
9646 curwin->w_cursor.lnum <= oap->end.lnum;
9647 ++curwin->w_cursor.lnum)
9648 {
9649 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9650 if (end > oap->end_vcol)
9651 oap->end_vcol = end;
9652 }
9653 }
9654 else if (redo_VIsual_busy)
9655 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9656 /*
9657 * Correct oap->end.col and oap->start.col to be the
9658 * upper-left and lower-right corner of the block area.
9659 *
9660 * (Actually, this does convert column positions into character
9661 * positions)
9662 */
9663 curwin->w_cursor.lnum = oap->end.lnum;
9664 coladvance(oap->end_vcol);
9665 oap->end = curwin->w_cursor;
9666
9667 curwin->w_cursor = oap->start;
9668 coladvance(oap->start_vcol);
9669 oap->start = curwin->w_cursor;
9670}