blob: a1b2612f7701f40488288733a599114154929b77 [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},
361 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
362 {K_MIDDLEDRAG, nv_mouse, 0, 0},
363 {K_MIDDLERELEASE, nv_mouse, 0, 0},
364 {K_RIGHTMOUSE, nv_mouse, 0, 0},
365 {K_RIGHTDRAG, nv_mouse, 0, 0},
366 {K_RIGHTRELEASE, nv_mouse, 0, 0},
367 {K_X1MOUSE, nv_mouse, 0, 0},
368 {K_X1DRAG, nv_mouse, 0, 0},
369 {K_X1RELEASE, nv_mouse, 0, 0},
370 {K_X2MOUSE, nv_mouse, 0, 0},
371 {K_X2DRAG, nv_mouse, 0, 0},
372 {K_X2RELEASE, nv_mouse, 0, 0},
373#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000374 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000375 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {K_INS, nv_edit, 0, 0},
377 {K_KINS, nv_edit, 0, 0},
378 {K_BS, nv_ctrlh, 0, 0},
379 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
380 {K_S_UP, nv_page, NV_SS, BACKWARD},
381 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
382 {K_S_DOWN, nv_page, NV_SS, FORWARD},
383 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
384 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
385 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
386 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
387 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
388 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
389 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
390 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
391 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
392 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
393 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
394 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {K_S_END, nv_end, NV_SS, FALSE},
396 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
397 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
398 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {K_S_HOME, nv_home, NV_SS, 0},
400 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
401 {K_DEL, nv_abbrev, 0, 0},
402 {K_KDEL, nv_abbrev, 0, 0},
403 {K_UNDO, nv_kundo, 0, 0},
404 {K_HELP, nv_help, NV_NCW, 0},
405 {K_F1, nv_help, NV_NCW, 0},
406 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408#ifdef FEAT_GUI
409 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
410 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
411#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000412#ifdef FEAT_GUI_TABLINE
413 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000414 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#ifdef FEAT_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200417 {K_F8, farsi_f8, 0, 0},
418 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#ifdef FEAT_NETBEANS_INTG
421 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
422#endif
423#ifdef FEAT_DND
424 {K_DROP, nv_drop, NV_STS, 0},
425#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000426#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000427 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000428#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100429 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430};
431
432/* Number of commands in nv_cmds[]. */
433#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
434
435/* Sorted index of commands in nv_cmds[]. */
436static short nv_cmd_idx[NV_CMDS_SIZE];
437
438/* The highest index for which
439 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
440static int nv_max_linear;
441
442/*
443 * Compare functions for qsort() below, that checks the command character
444 * through the index in nv_cmd_idx[].
445 */
446 static int
447#ifdef __BORLANDC__
448_RTLENTRYF
449#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100450nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
452 int c1, c2;
453
454 /* The commands are sorted on absolute value. */
455 c1 = nv_cmds[*(const short *)s1].cmd_char;
456 c2 = nv_cmds[*(const short *)s2].cmd_char;
457 if (c1 < 0)
458 c1 = -c1;
459 if (c2 < 0)
460 c2 = -c2;
461 return c1 - c2;
462}
463
464/*
465 * Initialize the nv_cmd_idx[] table.
466 */
467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100468init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469{
470 int i;
471
472 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000473 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 nv_cmd_idx[i] = i;
475
476 /* Sort the commands by the command character. */
477 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
478
479 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000480 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
482 break;
483 nv_max_linear = i - 1;
484}
485
486/*
487 * Search for a command in the commands table.
488 * Returns -1 for invalid command.
489 */
490 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100491find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 int i;
494 int idx;
495 int top, bot;
496 int c;
497
498#ifdef FEAT_MBYTE
499 /* A multi-byte character is never a command. */
500 if (cmdchar >= 0x100)
501 return -1;
502#endif
503
504 /* We use the absolute value of the character. Special keys have a
505 * negative value, but are sorted on their absolute value. */
506 if (cmdchar < 0)
507 cmdchar = -cmdchar;
508
509 /* If the character is in the first part: The character is the index into
510 * nv_cmd_idx[]. */
511 if (cmdchar <= nv_max_linear)
512 return nv_cmd_idx[cmdchar];
513
514 /* Perform a binary search. */
515 bot = nv_max_linear + 1;
516 top = NV_CMDS_SIZE - 1;
517 idx = -1;
518 while (bot <= top)
519 {
520 i = (top + bot) / 2;
521 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
522 if (c < 0)
523 c = -c;
524 if (cmdchar == c)
525 {
526 idx = nv_cmd_idx[i];
527 break;
528 }
529 if (cmdchar > c)
530 bot = i + 1;
531 else
532 top = i - 1;
533 }
534 return idx;
535}
536
537/*
538 * Execute a command in Normal mode.
539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100541normal_cmd(
542 oparg_T *oap,
543 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 cmdarg_T ca; /* command arguments */
546 int c;
547 int ctrl_w = FALSE; /* got CTRL-W command */
548 int old_col = curwin->w_curswant;
549#ifdef FEAT_CMDL_INFO
550 int need_flushbuf; /* need to call out_flush() */
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 pos_T old_pos; /* cursor position before command */
553 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 static int old_mapped_len = 0;
555 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000556#ifdef FEAT_EVAL
557 int set_prevcount = FALSE;
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559
560 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
561 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000562
563 /* Use a count remembered from before entering an operator. After typing
564 * "3d" we return from normal_cmd() and come back here, the "3" is
565 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 ca.opcount = opcount;
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 /*
569 * If there is an operator pending, then the command we take this time
570 * will terminate it. Finish_op tells us to finish the operation before
571 * returning this time (unless the operation was cancelled).
572 */
573#ifdef CURSOR_SHAPE
574 c = finish_op;
575#endif
576 finish_op = (oap->op_type != OP_NOP);
577#ifdef CURSOR_SHAPE
578 if (finish_op != c)
579 {
580 ui_cursor_shape(); /* may show different cursor shape */
581# ifdef FEAT_MOUSESHAPE
582 update_mouseshape(-1);
583# endif
584 }
585#endif
586
Bram Moolenaara983fe92008-07-31 20:04:27 +0000587 /* When not finishing an operator and no register name typed, reset the
588 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000592#ifdef FEAT_EVAL
593 set_prevcount = TRUE;
594#endif
595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
Bram Moolenaara983fe92008-07-31 20:04:27 +0000597#ifdef FEAT_AUTOCMD
598 /* Restore counts from before receiving K_CURSORHOLD. This means after
599 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
600 * "3 * 2". */
601 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
602 {
603 ca.opcount = oap->prev_opcount;
604 ca.count0 = oap->prev_count0;
605 oap->prev_opcount = 0;
606 oap->prev_count0 = 0;
607 }
608#endif
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611
612 State = NORMAL_BUSY;
613#ifdef USE_ON_FLY_SCROLL
614 dont_scroll = FALSE; /* allow scrolling here */
615#endif
616
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100617#ifdef FEAT_EVAL
618 /* Set v:count here, when called from main() and not a stuffed
619 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100620 * when there is no count. Do set it for redo. */
621 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100622 set_vcount_ca(&ca, &set_prevcount);
623#endif
624
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 /*
626 * Get the command character from the user.
627 */
628 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100629 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631 /*
632 * If a mapping was started in Visual or Select mode, remember the length
633 * of the mapping. This is used below to not return to Insert mode for as
634 * long as the mapping is being executed.
635 */
636 if (restart_edit == 0)
637 old_mapped_len = 0;
638 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000639 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 old_mapped_len = typebuf_maplen();
641
642 if (c == NUL)
643 c = K_ZERO;
644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 /*
646 * In Select mode, typed text replaces the selection.
647 */
648 if (VIsual_active
649 && VIsual_select
650 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
651 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000652 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
653 * 'insertmode' is set) fake a "d"elete command, Insert mode will
654 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000655 * Insert the typed character in the typeahead buffer, so that it can
656 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000657 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000658 if (restart_edit != 0)
659 c = 'd';
660 else
661 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000662 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200663 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
666#ifdef FEAT_CMDL_INFO
667 need_flushbuf = add_to_showcmd(c);
668#endif
669
670getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
673 /*
674 * Handle a count before a command and compute ca.count0.
675 * Note that '0' is a command and not the start of a count, but it's
676 * part of a count after other digits.
677 */
678 while ( (c >= '1' && c <= '9')
679 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
680 {
681 if (c == K_DEL || c == K_KDEL)
682 {
683 ca.count0 /= 10;
684#ifdef FEAT_CMDL_INFO
685 del_from_showcmd(4); /* delete the digit and ~@% */
686#endif
687 }
688 else
689 ca.count0 = ca.count0 * 10 + (c - '0');
690 if (ca.count0 < 0) /* got too large! */
691 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000692#ifdef FEAT_EVAL
693 /* Set v:count here, when called from main() and not a stuffed
694 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100695 * right after the count. Do set it for redo. */
696 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100697 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (ctrl_w)
700 {
701 ++no_mapping;
702 ++allow_keys; /* no mapping for nchar, but keys */
703 }
704 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000705 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 --no_zero_mapping;
708 if (ctrl_w)
709 {
710 --no_mapping;
711 --allow_keys;
712 }
713#ifdef FEAT_CMDL_INFO
714 need_flushbuf |= add_to_showcmd(c);
715#endif
716 }
717
718 /*
719 * If we got CTRL-W there may be a/another count
720 */
721 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
722 {
723 ctrl_w = TRUE;
724 ca.opcount = ca.count0; /* remember first count */
725 ca.count0 = 0;
726 ++no_mapping;
727 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000728 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 --no_mapping;
731 --allow_keys;
732#ifdef FEAT_CMDL_INFO
733 need_flushbuf |= add_to_showcmd(c);
734#endif
735 goto getcount; /* jump back */
736 }
737 }
738
Bram Moolenaara983fe92008-07-31 20:04:27 +0000739#ifdef FEAT_AUTOCMD
740 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000742 /* Save the count values so that ca.opcount and ca.count0 are exactly
743 * the same when coming back here after handling K_CURSORHOLD. */
744 oap->prev_opcount = ca.opcount;
745 oap->prev_count0 = ca.count0;
746 }
747 else
748#endif
749 if (ca.opcount != 0)
750 {
751 /*
752 * If we're in the middle of an operator (including after entering a
753 * yank buffer with '"') AND we had a count before the operator, then
754 * that count overrides the current value of ca.count0.
755 * What this means effectively, is that commands like "3dw" get turned
756 * into "d3w" which makes things fall into place pretty neatly.
757 * If you give a count before AND after the operator, they are
758 * multiplied.
759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 if (ca.count0)
761 ca.count0 *= ca.opcount;
762 else
763 ca.count0 = ca.opcount;
764 }
765
766 /*
767 * Always remember the count. It will be set to zero (on the next call,
768 * above) when there is no pending operator.
769 * When called from main(), save the count for use by the "count" built-in
770 * variable.
771 */
772 ca.opcount = ca.count0;
773 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
774
775#ifdef FEAT_EVAL
776 /*
777 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100778 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100780 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000781 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#endif
783
784 /*
785 * Find the command character in the table of commands.
786 * For CTRL-W we already got nchar when looking for a count.
787 */
788 if (ctrl_w)
789 {
790 ca.nchar = c;
791 ca.cmdchar = Ctrl_W;
792 }
793 else
794 ca.cmdchar = c;
795 idx = find_command(ca.cmdchar);
796 if (idx < 0)
797 {
798 /* Not a known command: beep. */
799 clearopbeep(oap);
800 goto normal_end;
801 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000802
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000803 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200805 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000807 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 goto normal_end;
809 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000810#ifdef FEAT_AUTOCMD
811 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
812 goto normal_end;
813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 /*
816 * In Visual/Select mode, a few keys are handled in a special way.
817 */
818 if (VIsual_active)
819 {
820 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
821 if (km_stopsel
822 && (nv_cmds[idx].cmd_flags & NV_STS)
823 && !(mod_mask & MOD_MASK_SHIFT))
824 {
825 end_visual_mode();
826 redraw_curbuf_later(INVERTED);
827 }
828
829 /* Keys that work different when 'keymodel' contains "startsel" */
830 if (km_startsel)
831 {
832 if (nv_cmds[idx].cmd_flags & NV_SS)
833 {
834 unshift_special(&ca);
835 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000836 if (idx < 0)
837 {
838 /* Just in case */
839 clearopbeep(oap);
840 goto normal_end;
841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
844 && (mod_mask & MOD_MASK_SHIFT))
845 {
846 mod_mask &= ~MOD_MASK_SHIFT;
847 }
848 }
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
851#ifdef FEAT_RIGHTLEFT
852 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
853 && (nv_cmds[idx].cmd_flags & NV_RL))
854 {
855 /* Invert horizontal movements and operations. Only when typed by the
856 * user directly, not when the result of a mapping or "x" translated
857 * to "dl". */
858 switch (ca.cmdchar)
859 {
860 case 'l': ca.cmdchar = 'h'; break;
861 case K_RIGHT: ca.cmdchar = K_LEFT; break;
862 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
863 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
864 case 'h': ca.cmdchar = 'l'; break;
865 case K_LEFT: ca.cmdchar = K_RIGHT; break;
866 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
867 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
868 case '>': ca.cmdchar = '<'; break;
869 case '<': ca.cmdchar = '>'; break;
870 }
871 idx = find_command(ca.cmdchar);
872 }
873#endif
874
875 /*
876 * Get an additional character if we need one.
877 */
878 if ((nv_cmds[idx].cmd_flags & NV_NCH)
879 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
880 && oap->op_type == OP_NOP)
881 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
882 || (ca.cmdchar == 'q'
883 && oap->op_type == OP_NOP
884 && !Recording
885 && !Exec_reg)
886 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100887 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {
889 int *cp;
890 int repl = FALSE; /* get character for replace mode */
891 int lit = FALSE; /* get extra character literally */
892 int langmap_active = FALSE; /* using :lmap mappings */
893 int lang; /* getting a text character */
894#ifdef USE_IM_CONTROL
895 int save_smd; /* saved value of p_smd */
896#endif
897
898 ++no_mapping;
899 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000900#ifdef FEAT_AUTOCMD
901 /* Don't generate a CursorHold event here, most commands can't handle
902 * it, e.g., nv_replace(), nv_csearch(). */
903 did_cursorhold = TRUE;
904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if (ca.cmdchar == 'g')
906 {
907 /*
908 * For 'g' get the next character now, so that we can check for
909 * "gr", "g'" and "g`".
910 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000911 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#ifdef FEAT_CMDL_INFO
914 need_flushbuf |= add_to_showcmd(ca.nchar);
915#endif
916 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100917 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 {
919 cp = &ca.extra_char; /* need to get a third character */
920 if (ca.nchar != 'r')
921 lit = TRUE; /* get it literally */
922 else
923 repl = TRUE; /* get it in replace mode */
924 }
925 else
926 cp = NULL; /* no third character needed */
927 }
928 else
929 {
930 if (ca.cmdchar == 'r') /* get it in replace mode */
931 repl = TRUE;
932 cp = &ca.nchar;
933 }
934 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
935
936 /*
937 * Get a second or third character.
938 */
939 if (cp != NULL)
940 {
941#ifdef CURSOR_SHAPE
942 if (repl)
943 {
944 State = REPLACE; /* pretend Replace mode */
945 ui_cursor_shape(); /* show different cursor shape */
946 }
947#endif
948 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
949 {
950 /* Allow mappings defined with ":lmap". */
951 --no_mapping;
952 --allow_keys;
953 if (repl)
954 State = LREPLACE;
955 else
956 State = LANGMAP;
957 langmap_active = TRUE;
958 }
959#ifdef USE_IM_CONTROL
960 save_smd = p_smd;
961 p_smd = FALSE; /* Don't let the IM code show the mode here */
962 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
963 im_set_active(TRUE);
964#endif
965
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000966 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
968 if (langmap_active)
969 {
970 /* Undo the decrement done above */
971 ++no_mapping;
972 ++allow_keys;
973 State = NORMAL_BUSY;
974 }
975#ifdef USE_IM_CONTROL
976 if (lang)
977 {
978 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
979 im_save_status(&curbuf->b_p_iminsert);
980 im_set_active(FALSE);
981 }
982 p_smd = save_smd;
983#endif
984#ifdef CURSOR_SHAPE
985 State = NORMAL_BUSY;
986#endif
987#ifdef FEAT_CMDL_INFO
988 need_flushbuf |= add_to_showcmd(*cp);
989#endif
990
991 if (!lit)
992 {
993#ifdef FEAT_DIGRAPHS
994 /* Typing CTRL-K gets a digraph. */
995 if (*cp == Ctrl_K
996 && ((nv_cmds[idx].cmd_flags & NV_LANG)
997 || cp == &ca.extra_char)
998 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
999 {
1000 c = get_digraph(FALSE);
1001 if (c > 0)
1002 {
1003 *cp = c;
1004# ifdef FEAT_CMDL_INFO
1005 /* Guessing how to update showcmd here... */
1006 del_from_showcmd(3);
1007 need_flushbuf |= add_to_showcmd(*cp);
1008# endif
1009 }
1010 }
1011#endif
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 /* adjust chars > 127, except after "tTfFr" commands */
1014 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#ifdef FEAT_RIGHTLEFT
1016 /* adjust Hebrew mapped char */
1017 if (p_hkmap && lang && KeyTyped)
1018 *cp = hkmap(*cp);
1019# ifdef FEAT_FKMAP
1020 /* adjust Farsi mapped char */
1021 if (p_fkmap && lang && KeyTyped)
1022 *cp = fkmap(*cp);
1023# endif
1024#endif
1025 }
1026
1027 /*
1028 * When the next character is CTRL-\ a following CTRL-N means the
1029 * command is aborted and we go to Normal mode.
1030 */
1031 if (cp == &ca.extra_char
1032 && ca.nchar == Ctrl_BSL
1033 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1034 {
1035 ca.cmdchar = Ctrl_BSL;
1036 ca.nchar = ca.extra_char;
1037 idx = find_command(ca.cmdchar);
1038 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001039 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001040 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 else if (*cp == Ctrl_BSL)
1042 {
1043 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1044
1045 /* There is a busy wait here when typing "f<C-\>" and then
1046 * something different from CTRL-N. Can't be avoided. */
1047 while ((c = vpeekc()) <= 0 && towait > 0L)
1048 {
1049 do_sleep(towait > 50L ? 50L : towait);
1050 towait -= 50L;
1051 }
1052 if (c > 0)
1053 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001054 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 if (c != Ctrl_N && c != Ctrl_G)
1056 vungetc(c);
1057 else
1058 {
1059 ca.cmdchar = Ctrl_BSL;
1060 ca.nchar = c;
1061 idx = find_command(ca.cmdchar);
1062 }
1063 }
1064 }
1065
1066#ifdef FEAT_MBYTE
1067 /* When getting a text character and the next character is a
1068 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001069 * However, don't wait for it to arrive. Also, do enable mapping,
1070 * because if it's put back with vungetc() it's too late to apply
1071 * mapping. */
1072 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 while (enc_utf8 && lang && (c = vpeekc()) > 0
1074 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1075 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001076 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (!utf_iscomposing(c))
1078 {
1079 vungetc(c); /* it wasn't, put it back */
1080 break;
1081 }
1082 else if (ca.ncharC1 == 0)
1083 ca.ncharC1 = c;
1084 else
1085 ca.ncharC2 = c;
1086 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001087 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#endif
1089 }
1090 --no_mapping;
1091 --allow_keys;
1092 }
1093
1094#ifdef FEAT_CMDL_INFO
1095 /*
1096 * Flush the showcmd characters onto the screen so we can see them while
1097 * the command is being executed. Only do this when the shown command was
1098 * actually displayed, otherwise this will slow down a lot when executing
1099 * mappings.
1100 */
1101 if (need_flushbuf)
1102 out_flush();
1103#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001104#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001105 if (ca.cmdchar != K_IGNORE)
1106 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 State = NORMAL;
1110
1111 if (ca.nchar == ESC)
1112 {
1113 clearop(oap);
1114 if (restart_edit == 0 && goto_im())
1115 restart_edit = 'a';
1116 goto normal_end;
1117 }
1118
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001119 if (ca.cmdchar != K_IGNORE)
1120 {
1121 msg_didout = FALSE; /* don't scroll screen up for normal command */
1122 msg_col = 0;
1123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 old_pos = curwin->w_cursor; /* remember where cursor was */
1126
1127 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1128 * mode. */
1129 if (!VIsual_active && km_startsel)
1130 {
1131 if (nv_cmds[idx].cmd_flags & NV_SS)
1132 {
1133 start_selection();
1134 unshift_special(&ca);
1135 idx = find_command(ca.cmdchar);
1136 }
1137 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1138 && (mod_mask & MOD_MASK_SHIFT))
1139 {
1140 start_selection();
1141 mod_mask &= ~MOD_MASK_SHIFT;
1142 }
1143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 /*
1146 * Execute the command!
1147 * Call the command function found in the commands table.
1148 */
1149 ca.arg = nv_cmds[idx].cmd_arg;
1150 (nv_cmds[idx].cmd_func)(&ca);
1151
1152 /*
1153 * If we didn't start or finish an operator, reset oap->regname, unless we
1154 * need it later.
1155 */
1156 if (!finish_op
1157 && !oap->op_type
1158 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1159 {
1160 clearop(oap);
1161#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001162 {
1163 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001164
Bram Moolenaar536681b2011-05-10 16:12:45 +02001165 /* Adjust the register according to 'clipboard', so that when
1166 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001167# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001168 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001169# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001170 set_reg_var(regname);
1171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172#endif
1173 }
1174
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001175 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001176 * character or "z333<cr>". */
1177 if (old_mapped_len > 0)
1178 old_mapped_len = typebuf_maplen();
1179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 /*
1181 * If an operation is pending, handle it...
1182 */
1183 do_pending_operator(&ca, old_col, FALSE);
1184
1185 /*
1186 * Wait for a moment when a message is displayed that will be overwritten
1187 * by the mode message.
1188 * In Visual mode and with "^O" in Insert mode, a short message will be
1189 * overwritten by the mode message. Wait a bit, until a key is hit.
1190 * In Visual mode, it's more important to keep the Visual area updated
1191 * than keeping a message (e.g. from a /pat search).
1192 * Only do this if the command was typed, not from a mapping.
1193 * Don't wait when emsg_silent is non-zero.
1194 * Also wait a bit after an error message, e.g. for "^O:".
1195 * Don't redraw the screen, it would remove the message.
1196 */
1197 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001198 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 || (VIsual_active
1201 && old_pos.lnum == curwin->w_cursor.lnum
1202 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 )
1204 && (clear_cmdline
1205 || redraw_cmdline)
1206 && (msg_didout || (msg_didany && msg_scroll))
1207 && !msg_nowait
1208 && KeyTyped)
1209 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && (msg_scroll
1212 || emsg_on_display)))
1213 && oap->regname == 0
1214 && !(ca.retval & CA_COMMAND_BUSY)
1215 && stuff_empty()
1216 && typebuf_typed()
1217 && emsg_silent == 0
1218 && !did_wait_return
1219 && oap->op_type == OP_NOP)
1220 {
1221 int save_State = State;
1222
1223 /* Draw the cursor with the right shape here */
1224 if (restart_edit != 0)
1225 State = INSERT;
1226
1227 /* If need to redraw, and there is a "keep_msg", redraw before the
1228 * delay */
1229 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1230 {
1231 char_u *kmsg;
1232
1233 kmsg = keep_msg;
1234 keep_msg = NULL;
1235 /* showmode() will clear keep_msg, but we want to use it anyway */
1236 update_screen(0);
1237 /* now reset it, otherwise it's put in the history again */
1238 keep_msg = kmsg;
1239 msg_attr(kmsg, keep_msg_attr);
1240 vim_free(kmsg);
1241 }
1242 setcursor();
1243 cursor_on();
1244 out_flush();
1245 if (msg_scroll || emsg_on_display)
1246 ui_delay(1000L, TRUE); /* wait at least one second */
1247 ui_delay(3000L, FALSE); /* wait up to three seconds */
1248 State = save_State;
1249
1250 msg_scroll = FALSE;
1251 emsg_on_display = FALSE;
1252 }
1253
1254 /*
1255 * Finish up after executing a Normal mode command.
1256 */
1257normal_end:
1258
1259 msg_nowait = FALSE;
1260
1261 /* Reset finish_op, in case it was set */
1262#ifdef CURSOR_SHAPE
1263 c = finish_op;
1264#endif
1265 finish_op = FALSE;
1266#ifdef CURSOR_SHAPE
1267 /* Redraw the cursor with another shape, if we were in Operator-pending
1268 * mode or did a replace command. */
1269 if (c || ca.cmdchar == 'r')
1270 {
1271 ui_cursor_shape(); /* may show different cursor shape */
1272# ifdef FEAT_MOUSESHAPE
1273 update_mouseshape(-1);
1274# endif
1275 }
1276#endif
1277
1278#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001279 if (oap->op_type == OP_NOP && oap->regname == 0
1280# ifdef FEAT_AUTOCMD
1281 && ca.cmdchar != K_CURSORHOLD
1282# endif
1283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 clear_showcmd();
1285#endif
1286
1287 checkpcmark(); /* check if we moved since setting pcmark */
1288 vim_free(ca.searchbuf);
1289
1290#ifdef FEAT_MBYTE
1291 if (has_mbyte)
1292 mb_adjust_cursor();
1293#endif
1294
1295#ifdef FEAT_SCROLLBIND
1296 if (curwin->w_p_scb && toplevel)
1297 {
1298 validate_cursor(); /* may need to update w_leftcol */
1299 do_check_scrollbind(TRUE);
1300 }
1301#endif
1302
Bram Moolenaar860cae12010-06-05 23:22:07 +02001303#ifdef FEAT_CURSORBIND
1304 if (curwin->w_p_crb && toplevel)
1305 {
1306 validate_cursor(); /* may need to update w_leftcol */
1307 do_check_cursorbind();
1308 }
1309#endif
1310
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001311#ifdef FEAT_TERMINAL
1312 /* don't go to Insert mode from Terminal-Job mode */
1313 if (term_use_loop())
1314 restart_edit = 0;
1315#endif
1316
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 /*
1318 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1319 * if still inside a mapping that started in Visual mode).
1320 * May switch from Visual to Select mode after CTRL-O command.
1321 */
1322 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1324 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && !(ca.retval & CA_COMMAND_BUSY)
1326 && stuff_empty()
1327 && oap->regname == 0)
1328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (restart_VIsual_select == 1)
1330 {
1331 VIsual_select = TRUE;
1332 showmode();
1333 restart_VIsual_select = 0;
1334 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001335 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 (void)edit(restart_edit, FALSE, 1L);
1337 }
1338
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (restart_VIsual_select == 2)
1340 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342 /* Save count before an operator for next time. */
1343 opcount = ca.opcount;
1344}
1345
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001346#ifdef FEAT_EVAL
1347/*
1348 * Set v:count and v:count1 according to "cap".
1349 * Set v:prevcount only when "set_prevcount" is TRUE.
1350 */
1351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001352set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001353{
1354 long count = cap->count0;
1355
1356 /* multiply with cap->opcount the same way as above */
1357 if (cap->opcount != 0)
1358 count = cap->opcount * (count == 0 ? 1 : count);
1359 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1360 *set_prevcount = FALSE; /* only set v:prevcount once */
1361}
1362#endif
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364/*
1365 * Handle an operator after visual mode or when the movement is finished
1366 */
1367 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001368do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370 oparg_T *oap = cap->oap;
1371 pos_T old_cursor;
1372 int empty_region_error;
1373 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001374#ifdef FEAT_LINEBREAK
1375 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /* The visual area is remembered for redo */
1379 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1380 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001381 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001383 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001384#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#endif
1387
1388#if defined(FEAT_CLIPBOARD)
1389 /*
1390 * Yank the visual area into the GUI selection register before we operate
1391 * on it and lose it forever.
1392 * Don't do it if a specific register was specified, so that ""x"*P works.
1393 * This could call do_pending_operator() recursively, but that's OK
1394 * because gui_yank will be TRUE for the nested call.
1395 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001396 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 && oap->op_type != OP_NOP
1398 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 && VIsual_active
1400 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 && oap->regname == 0)
1402 clip_auto_select();
1403#endif
1404 old_cursor = curwin->w_cursor;
1405
1406 /*
1407 * If an operation is pending, handle it...
1408 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001409 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001411#ifdef FEAT_LINEBREAK
1412 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001413 if (curwin->w_p_lbr)
1414 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001415 curwin->w_p_lbr = FALSE;
1416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 oap->is_VIsual = VIsual_active;
1418 if (oap->motion_force == 'V')
1419 oap->motion_type = MLINE;
1420 else if (oap->motion_force == 'v')
1421 {
1422 /* If the motion was linewise, "inclusive" will not have been set.
1423 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1424 if (oap->motion_type == MLINE)
1425 oap->inclusive = FALSE;
1426 /* If the motion already was characterwise, toggle "inclusive" */
1427 else if (oap->motion_type == MCHAR)
1428 oap->inclusive = !oap->inclusive;
1429 oap->motion_type = MCHAR;
1430 }
1431 else if (oap->motion_force == Ctrl_V)
1432 {
1433 /* Change line- or characterwise motion into Visual block mode. */
1434 VIsual_active = TRUE;
1435 VIsual = oap->start;
1436 VIsual_mode = Ctrl_V;
1437 VIsual_select = FALSE;
1438 VIsual_reselect = FALSE;
1439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaar7afea822013-04-24 18:34:45 +02001441 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1442 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001444 && ((!VIsual_active || oap->motion_force)
1445 /* Also redo Operator-pending Visual mode mappings */
1446 || (VIsual_active && cap->cmdchar == ':'
1447 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001448 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449#ifdef FEAT_FOLDING
1450 && oap->op_type != OP_FOLD
1451 && oap->op_type != OP_FOLDOPEN
1452 && oap->op_type != OP_FOLDOPENREC
1453 && oap->op_type != OP_FOLDCLOSE
1454 && oap->op_type != OP_FOLDCLOSEREC
1455 && oap->op_type != OP_FOLDDEL
1456 && oap->op_type != OP_FOLDDELREC
1457#endif
1458 )
1459 {
1460 prep_redo(oap->regname, cap->count0,
1461 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1462 oap->motion_force, cap->cmdchar, cap->nchar);
1463 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1464 {
1465 /*
1466 * If 'cpoptions' does not contain 'r', insert the search
1467 * pattern to really repeat the same command.
1468 */
1469 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001470 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 AppendToRedobuff(NL_STR);
1472 }
1473 else if (cap->cmdchar == ':')
1474 {
1475 /* do_cmdline() has stored the first typed line in
1476 * "repeat_cmdline". When several lines are typed repeating
1477 * won't be possible. */
1478 if (repeat_cmdline == NULL)
1479 ResetRedobuff();
1480 else
1481 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001482 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 AppendToRedobuff(NL_STR);
1484 vim_free(repeat_cmdline);
1485 repeat_cmdline = NULL;
1486 }
1487 }
1488 }
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (redo_VIsual_busy)
1491 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001492 /* Redo of an operation on a Visual area. Use the same size from
1493 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 oap->start = curwin->w_cursor;
1495 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1496 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1497 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1498 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001499 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001501 if (VIsual_mode == 'v')
1502 {
1503 if (redo_VIsual_line_count <= 1)
1504 {
1505 validate_virtcol();
1506 curwin->w_curswant =
1507 curwin->w_virtcol + redo_VIsual_vcol - 1;
1508 }
1509 else
1510 curwin->w_curswant = redo_VIsual_vcol;
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001513 {
1514 curwin->w_curswant = MAXCOL;
1515 }
1516 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518 cap->count0 = redo_VIsual_count;
1519 if (redo_VIsual_count != 0)
1520 cap->count1 = redo_VIsual_count;
1521 else
1522 cap->count1 = 1;
1523 }
1524 else if (VIsual_active)
1525 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001526 if (!gui_yank)
1527 {
1528 /* Save the current VIsual area for '< and '> marks, and "gv" */
1529 curbuf->b_visual.vi_start = VIsual;
1530 curbuf->b_visual.vi_end = curwin->w_cursor;
1531 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001532 if (VIsual_mode_orig != NUL)
1533 {
1534 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1535 VIsual_mode_orig = NUL;
1536 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001537 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001539 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
1543 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001544 * characterwise selection.
1545 * Special case: gH<Del> deletes the last line. */
1546 if (VIsual_select && VIsual_mode == 'V'
1547 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001549 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 VIsual.col = 0;
1552 curwin->w_cursor.col =
1553 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1554 }
1555 else
1556 {
1557 curwin->w_cursor.col = 0;
1558 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1559 }
1560 VIsual_mode = 'v';
1561 }
1562 /* If 'selection' is "exclusive", backup one character for
1563 * charwise selections. */
1564 else if (VIsual_mode == 'v')
1565 {
1566# ifdef FEAT_VIRTUALEDIT
1567 include_line_break =
1568# endif
1569 unadjust_for_sel();
1570 }
1571
1572 oap->start = VIsual;
1573 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001576# ifdef FEAT_VIRTUALEDIT
1577 oap->start.coladd = 0;
1578# endif
1579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 /*
1583 * Set oap->start to the first position of the operated text, oap->end
1584 * to the end of the operated text. w_cursor is equal to oap->start.
1585 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001586 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588#ifdef FEAT_FOLDING
1589 /* Include folded lines completely. */
1590 if (!VIsual_active)
1591 {
1592 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1593 oap->start.col = 0;
1594 if (hasFolding(curwin->w_cursor.lnum, NULL,
1595 &curwin->w_cursor.lnum))
1596 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1597 }
1598#endif
1599 oap->end = curwin->w_cursor;
1600 curwin->w_cursor = oap->start;
1601
1602 /* w_virtcol may have been updated; if the cursor goes back to its
1603 * previous position w_virtcol becomes invalid and isn't updated
1604 * automatically. */
1605 curwin->w_valid &= ~VALID_VIRTCOL;
1606 }
1607 else
1608 {
1609#ifdef FEAT_FOLDING
1610 /* Include folded lines completely. */
1611 if (!VIsual_active && oap->motion_type == MLINE)
1612 {
1613 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1614 NULL))
1615 curwin->w_cursor.col = 0;
1616 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1617 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1618 }
1619#endif
1620 oap->end = oap->start;
1621 oap->start = curwin->w_cursor;
1622 }
1623
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001624 /* Just in case lines were deleted that make the position invalid. */
1625 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1627
1628#ifdef FEAT_VIRTUALEDIT
1629 /* Set "virtual_op" before resetting VIsual_active. */
1630 virtual_op = virtual_active();
1631#endif
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (VIsual_active || redo_VIsual_busy)
1634 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001635 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 if (!redo_VIsual_busy && !gui_yank)
1638 {
1639 /*
1640 * Prepare to reselect and redo Visual: this is based on the
1641 * size of the Visual text
1642 */
1643 resel_VIsual_mode = VIsual_mode;
1644 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001645 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001647 {
1648 if (VIsual_mode != Ctrl_V)
1649 getvvcol(curwin, &(oap->end),
1650 NULL, NULL, &oap->end_vcol);
1651 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1652 {
1653 if (VIsual_mode != Ctrl_V)
1654 getvvcol(curwin, &(oap->start),
1655 &oap->start_vcol, NULL, NULL);
1656 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1657 }
1658 else
1659 resel_VIsual_vcol = oap->end_vcol;
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 resel_VIsual_line_count = oap->line_count;
1662 }
1663
1664 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1665 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1666 && oap->op_type != OP_COLON
1667#ifdef FEAT_FOLDING
1668 && oap->op_type != OP_FOLD
1669 && oap->op_type != OP_FOLDOPEN
1670 && oap->op_type != OP_FOLDOPENREC
1671 && oap->op_type != OP_FOLDCLOSE
1672 && oap->op_type != OP_FOLDCLOSEREC
1673 && oap->op_type != OP_FOLDDEL
1674 && oap->op_type != OP_FOLDDELREC
1675#endif
1676 && oap->motion_force == NUL
1677 )
1678 {
1679 /* Prepare for redoing. Only use the nchar field for "r",
1680 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001681 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1682 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001683 prep_redo(oap->regname, cap->count0,
1684 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1685 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001686 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001687 prep_redo(oap->regname, 0L, NUL, 'v',
1688 get_op_char(oap->op_type),
1689 get_extra_op_char(oap->op_type),
1690 oap->op_type == OP_REPLACE
1691 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (!redo_VIsual_busy)
1693 {
1694 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001695 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 redo_VIsual_line_count = resel_VIsual_line_count;
1697 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001698 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
1700 }
1701
1702 /*
1703 * oap->inclusive defaults to TRUE.
1704 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1705 * FALSE. This makes "d}P" and "v}dP" work the same.
1706 */
1707 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1708 oap->inclusive = TRUE;
1709 if (VIsual_mode == 'V')
1710 oap->motion_type = MLINE;
1711 else
1712 {
1713 oap->motion_type = MCHAR;
1714 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001715#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 )
1719 {
1720 oap->inclusive = FALSE;
1721 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001722 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001723 if (*p_sel != 'o'
1724 && !op_on_lines(oap->op_type)
1725 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001727 ++oap->end.lnum;
1728 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001729#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001730 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001731#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001732 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
1734 }
1735 }
1736
1737 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 /*
1740 * Switch Visual off now, so screen updating does
1741 * not show inverted text when the screen is redrawn.
1742 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1743 * no screen redraw, so it is done here to remove the inverted
1744 * part.
1745 */
1746 if (!gui_yank)
1747 {
1748 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001749#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 setmouse();
1751 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001752#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001753 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if ((oap->op_type == OP_YANK
1755 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001756 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 || oap->op_type == OP_FILTER)
1758 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001759 {
1760#ifdef FEAT_LINEBREAK
1761 /* make sure redrawing is correct */
1762 curwin->w_p_lbr = lbr_saved;
1763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769#ifdef FEAT_MBYTE
1770 /* Include the trailing byte of a multi-byte char. */
1771 if (has_mbyte && oap->inclusive)
1772 {
1773 int l;
1774
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001775 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (l > 1)
1777 oap->end.col += l - 1;
1778 }
1779#endif
1780 curwin->w_set_curswant = TRUE;
1781
1782 /*
1783 * oap->empty is set when start and end are the same. The inclusive
1784 * flag affects this too, unless yanking and the end is on a NUL.
1785 */
1786 oap->empty = (oap->motion_type == MCHAR
1787 && (!oap->inclusive
1788 || (oap->op_type == OP_YANK
1789 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001790 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#ifdef FEAT_VIRTUALEDIT
1792 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1793#endif
1794 );
1795 /*
1796 * For delete, change and yank, it's an error to operate on an
1797 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1798 */
1799 empty_region_error = (oap->empty
1800 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 /* Force a redraw when operating on an empty Visual region, when
1803 * 'modifiable is off or creating a fold. */
1804 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001805#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001809 {
1810#ifdef FEAT_LINEBREAK
1811 curwin->w_p_lbr = lbr_saved;
1812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
1816 /*
1817 * If the end of an operator is in column one while oap->motion_type
1818 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1819 * character in the previous line. If op_start is on or before the
1820 * first non-blank in the line, the operator becomes linewise
1821 * (strange, but that's the way vi does it).
1822 */
1823 if ( oap->motion_type == MCHAR
1824 && oap->inclusive == FALSE
1825 && !(cap->retval & CA_NO_ADJ_OP_END)
1826 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001828 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 && oap->line_count > 1)
1830 {
1831 oap->end_adjusted = TRUE; /* remember that we did this */
1832 --oap->line_count;
1833 --oap->end.lnum;
1834 if (inindent(0))
1835 oap->motion_type = MLINE;
1836 else
1837 {
1838 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1839 if (oap->end.col)
1840 {
1841 --oap->end.col;
1842 oap->inclusive = TRUE;
1843 }
1844 }
1845 }
1846 else
1847 oap->end_adjusted = FALSE;
1848
1849 switch (oap->op_type)
1850 {
1851 case OP_LSHIFT:
1852 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001853 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 auto_format(FALSE, TRUE);
1855 break;
1856
1857 case OP_JOIN_NS:
1858 case OP_JOIN:
1859 if (oap->line_count < 2)
1860 oap->line_count = 2;
1861 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1862 curbuf->b_ml.ml_line_count)
1863 beep_flush();
1864 else
1865 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001866 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001867 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 auto_format(FALSE, TRUE);
1869 }
1870 break;
1871
1872 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001875 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001876 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001877 CancelRedo();
1878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 else
1880 {
1881 (void)op_delete(oap);
1882 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1883 u_save_cursor(); /* cursor line wasn't saved yet */
1884 auto_format(FALSE, TRUE);
1885 }
1886 break;
1887
1888 case OP_YANK:
1889 if (empty_region_error)
1890 {
1891 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001892 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001893 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001894 CancelRedo();
1895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001898 {
1899#ifdef FEAT_LINEBREAK
1900 curwin->w_p_lbr = lbr_saved;
1901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 check_cursor_col();
1905 break;
1906
1907 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001910 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001911 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001912 CancelRedo();
1913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 else
1915 {
1916 /* This is a new edit command, not a restart. Need to
1917 * remember it to make 'insertmode' work with mappings for
1918 * Visual mode. But do this only once and not when typed and
1919 * 'insertmode' isn't set. */
1920 if (p_im || !KeyTyped)
1921 restart_edit_save = restart_edit;
1922 else
1923 restart_edit_save = 0;
1924 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001925#ifdef FEAT_LINEBREAK
1926 /* Restore linebreak, so that when the user edits it looks as
1927 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001928 if (curwin->w_p_lbr != lbr_saved)
1929 {
1930 curwin->w_p_lbr = lbr_saved;
1931 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1932 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 /* Reset finish_op now, don't want it set inside edit(). */
1935 finish_op = FALSE;
1936 if (op_change(oap)) /* will call edit() */
1937 cap->retval |= CA_COMMAND_BUSY;
1938 if (restart_edit == 0)
1939 restart_edit = restart_edit_save;
1940 }
1941 break;
1942
1943 case OP_FILTER:
1944 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1945 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1946 else
1947 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
1948
1949 case OP_INDENT:
1950 case OP_COLON:
1951
1952#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1953 /*
1954 * If 'equalprg' is empty, do the indenting internally.
1955 */
1956 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1957 {
1958# ifdef FEAT_LISP
1959 if (curbuf->b_p_lisp)
1960 {
1961 op_reindent(oap, get_lisp_indent);
1962 break;
1963 }
1964# endif
1965# ifdef FEAT_CINDENT
1966 op_reindent(oap,
1967# ifdef FEAT_EVAL
1968 *curbuf->b_p_inde != NUL ? get_expr_indent :
1969# endif
1970 get_c_indent);
1971 break;
1972# endif
1973 }
1974#endif
1975
1976 op_colon(oap);
1977 break;
1978
1979 case OP_TILDE:
1980 case OP_UPPER:
1981 case OP_LOWER:
1982 case OP_ROT13:
1983 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001984 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001985 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001986 CancelRedo();
1987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 else
1989 op_tilde(oap);
1990 check_cursor_col();
1991 break;
1992
1993 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001994#if defined(FEAT_EVAL)
1995 if (*curbuf->b_p_fex != NUL)
1996 op_formatexpr(oap); /* use expression */
1997 else
1998#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001999 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 op_colon(oap); /* use external command */
2001 else
2002 op_format(oap, FALSE); /* use internal function */
2003 break;
2004
2005 case OP_FORMAT2:
2006 op_format(oap, TRUE); /* use internal function */
2007 break;
2008
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002009 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01002010#ifdef FEAT_LINEBREAK
2011 /* Restore linebreak, so that when the user edits it looks as
2012 * before. */
2013 curwin->w_p_lbr = lbr_saved;
2014#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002015 op_function(oap); /* call 'operatorfunc' */
2016 break;
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 case OP_INSERT:
2019 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021#ifdef FEAT_VISUALEXTRA
2022 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002023 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002024 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002025 CancelRedo();
2026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 else
2028 {
2029 /* This is a new edit command, not a restart. Need to
2030 * remember it to make 'insertmode' work with mappings for
2031 * Visual mode. But do this only once. */
2032 restart_edit_save = restart_edit;
2033 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002034#ifdef FEAT_LINEBREAK
2035 /* Restore linebreak, so that when the user edits it looks as
2036 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002037 if (curwin->w_p_lbr != lbr_saved)
2038 {
2039 curwin->w_p_lbr = lbr_saved;
2040 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2041 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002044#ifdef FEAT_LINEBREAK
2045 /* Reset linebreak, so that formatting works correctly. */
2046 curwin->w_p_lbr = FALSE;
2047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 /* TODO: when inserting in several lines, should format all
2050 * the lines. */
2051 auto_format(FALSE, TRUE);
2052
2053 if (restart_edit == 0)
2054 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002055 else
2056 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 }
2058#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002059 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060#endif
2061 break;
2062
2063 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065#ifdef FEAT_VISUALEXTRA
2066 if (empty_region_error)
2067#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002068 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002069 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002070 CancelRedo();
2071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072#ifdef FEAT_VISUALEXTRA
2073 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002074 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002075# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002076 /* Restore linebreak, so that when the user edits it looks as
2077 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002078 if (curwin->w_p_lbr != lbr_saved)
2079 {
2080 curwin->w_p_lbr = lbr_saved;
2081 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2082 }
2083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086#endif
2087 break;
2088
2089#ifdef FEAT_FOLDING
2090 case OP_FOLD:
2091 VIsual_reselect = FALSE; /* don't reselect now */
2092 foldCreate(oap->start.lnum, oap->end.lnum);
2093 break;
2094
2095 case OP_FOLDOPEN:
2096 case OP_FOLDOPENREC:
2097 case OP_FOLDCLOSE:
2098 case OP_FOLDCLOSEREC:
2099 VIsual_reselect = FALSE; /* don't reselect now */
2100 opFoldRange(oap->start.lnum, oap->end.lnum,
2101 oap->op_type == OP_FOLDOPEN
2102 || oap->op_type == OP_FOLDOPENREC,
2103 oap->op_type == OP_FOLDOPENREC
2104 || oap->op_type == OP_FOLDCLOSEREC,
2105 oap->is_VIsual);
2106 break;
2107
2108 case OP_FOLDDEL:
2109 case OP_FOLDDELREC:
2110 VIsual_reselect = FALSE; /* don't reselect now */
2111 deleteFold(oap->start.lnum, oap->end.lnum,
2112 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2113 break;
2114#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002115 case OP_NR_ADD:
2116 case OP_NR_SUB:
2117 if (empty_region_error)
2118 {
2119 vim_beep(BO_OPER);
2120 CancelRedo();
2121 }
2122 else
2123 {
2124 VIsual_active = TRUE;
2125#ifdef FEAT_LINEBREAK
2126 curwin->w_p_lbr = lbr_saved;
2127#endif
2128 op_addsub(oap, cap->count1, redo_VIsual_arg);
2129 VIsual_active = FALSE;
2130 }
2131 check_cursor_col();
2132 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 default:
2134 clearopbeep(oap);
2135 }
2136#ifdef FEAT_VIRTUALEDIT
2137 virtual_op = MAYBE;
2138#endif
2139 if (!gui_yank)
2140 {
2141 /*
2142 * if 'sol' not set, go back to old column for some commands
2143 */
2144 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2145 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2146 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002147 {
2148#ifdef FEAT_LINEBREAK
2149 curwin->w_p_lbr = FALSE;
2150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 else
2155 {
2156 curwin->w_cursor = old_cursor;
2157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 clearop(oap);
2160 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002161#ifdef FEAT_LINEBREAK
2162 curwin->w_p_lbr = lbr_saved;
2163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164}
2165
2166/*
2167 * Handle indent and format operators and visual mode ":".
2168 */
2169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002170op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171{
2172 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (oap->is_VIsual)
2174 stuffReadbuff((char_u *)"'<,'>");
2175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 /*
2178 * Make the range look nice, so it can be repeated.
2179 */
2180 if (oap->start.lnum == curwin->w_cursor.lnum)
2181 stuffcharReadbuff('.');
2182 else
2183 stuffnumReadbuff((long)oap->start.lnum);
2184 if (oap->end.lnum != oap->start.lnum)
2185 {
2186 stuffcharReadbuff(',');
2187 if (oap->end.lnum == curwin->w_cursor.lnum)
2188 stuffcharReadbuff('.');
2189 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2190 stuffcharReadbuff('$');
2191 else if (oap->start.lnum == curwin->w_cursor.lnum)
2192 {
2193 stuffReadbuff((char_u *)".+");
2194 stuffnumReadbuff((long)oap->line_count - 1);
2195 }
2196 else
2197 stuffnumReadbuff((long)oap->end.lnum);
2198 }
2199 }
2200 if (oap->op_type != OP_COLON)
2201 stuffReadbuff((char_u *)"!");
2202 if (oap->op_type == OP_INDENT)
2203 {
2204#ifndef FEAT_CINDENT
2205 if (*get_equalprg() == NUL)
2206 stuffReadbuff((char_u *)"indent");
2207 else
2208#endif
2209 stuffReadbuff(get_equalprg());
2210 stuffReadbuff((char_u *)"\n");
2211 }
2212 else if (oap->op_type == OP_FORMAT)
2213 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002214 if (*curbuf->b_p_fp != NUL)
2215 stuffReadbuff(curbuf->b_p_fp);
2216 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002218 else
2219 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002220 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
2222
2223 /*
2224 * do_cmdline() does the rest
2225 */
2226}
2227
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002228/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002229 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002230 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002231 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002232op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002233{
2234#ifdef FEAT_EVAL
2235 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002236# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002237 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002238# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002239
2240 if (*p_opfunc == NUL)
2241 EMSG(_("E774: 'operatorfunc' is empty"));
2242 else
2243 {
2244 /* Set '[ and '] marks to text to be operated on. */
2245 curbuf->b_op_start = oap->start;
2246 curbuf->b_op_end = oap->end;
2247 if (oap->motion_type != MLINE && !oap->inclusive)
2248 /* Exclude the end position. */
2249 decl(&curbuf->b_op_end);
2250
2251 if (oap->block_mode)
2252 argv[0] = (char_u *)"block";
2253 else if (oap->motion_type == MLINE)
2254 argv[0] = (char_u *)"line";
2255 else
2256 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002257
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002258# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002259 /* Reset virtual_op so that 'virtualedit' can be changed in the
2260 * function. */
2261 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002262# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002263
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002264 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002265
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002266# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002267 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002268# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002269 }
2270#else
2271 EMSG(_("E775: Eval feature not available"));
2272#endif
2273}
2274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275#if defined(FEAT_MOUSE) || defined(PROTO)
2276/*
2277 * Do the appropriate action for the current mouse click in the current mode.
2278 * Not used for Command-line mode.
2279 *
2280 * Normal Mode:
2281 * event modi- position visual change action
2282 * fier cursor window
2283 * left press - yes end yes
2284 * left press C yes end yes "^]" (2)
2285 * left press S yes end yes "*" (2)
2286 * left drag - yes start if moved no
2287 * left relse - yes start if moved no
2288 * middle press - yes if not active no put register
2289 * middle press - yes if active no yank and put
2290 * right press - yes start or extend yes
2291 * right press S yes no change yes "#" (2)
2292 * right drag - yes extend no
2293 * right relse - yes extend no
2294 *
2295 * Insert or Replace Mode:
2296 * event modi- position visual change action
2297 * fier cursor window
2298 * left press - yes (cannot be active) yes
2299 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2300 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2301 * left drag - yes start or extend (1) no CTRL-O (1)
2302 * left relse - yes start or extend (1) no CTRL-O (1)
2303 * middle press - no (cannot be active) no put register
2304 * right press - yes start or extend yes CTRL-O
2305 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2306 *
2307 * (1) only if mouse pointer moved since press
2308 * (2) only if click is in same buffer
2309 *
2310 * Return TRUE if start_arrow() should be called for edit mode.
2311 */
2312 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002313do_mouse(
2314 oparg_T *oap, /* operator argument, can be NULL */
2315 int c, /* K_LEFTMOUSE, etc */
2316 int dir, /* Direction to 'put' if necessary */
2317 long count,
2318 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2321 static int got_click = FALSE; /* got a click some time back */
2322
2323 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2324 int is_click; /* If FALSE it's a drag or release event */
2325 int is_drag; /* If TRUE it's a drag event */
2326 int jump_flags = 0; /* flags for jump_to_mouse() */
2327 pos_T start_visual;
2328 int moved; /* Has cursor moved? */
2329 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002330#ifdef FEAT_WINDOWS
2331 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 int in_sep_line; /* mouse in vertical separator line */
2333#endif
2334 int c1, c2;
2335#if defined(FEAT_FOLDING)
2336 pos_T save_cursor;
2337#endif
2338 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 static pos_T orig_cursor;
2340 colnr_T leftcol, rightcol;
2341 pos_T end_visual;
2342 int diff;
2343 int old_active = VIsual_active;
2344 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 int regname;
2346
2347#if defined(FEAT_FOLDING)
2348 save_cursor = curwin->w_cursor;
2349#endif
2350
2351 /*
2352 * When GUI is active, always recognize mouse events, otherwise:
2353 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2354 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2355 * - For command line and insert mode 'mouse' is checked before calling
2356 * do_mouse().
2357 */
2358 if (do_always)
2359 do_always = FALSE;
2360 else
2361#ifdef FEAT_GUI
2362 if (!gui.in_use)
2363#endif
2364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (VIsual_active)
2366 {
2367 if (!mouse_has(MOUSE_VISUAL))
2368 return FALSE;
2369 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002370 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 return FALSE;
2372 }
2373
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002374 for (;;)
2375 {
2376 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2377 if (is_drag)
2378 {
2379 /* If the next character is the same mouse event then use that
2380 * one. Speeds up dragging the status line. */
2381 if (vpeekc() != NUL)
2382 {
2383 int nc;
2384 int save_mouse_row = mouse_row;
2385 int save_mouse_col = mouse_col;
2386
2387 /* Need to get the character, peeking doesn't get the actual
2388 * one. */
2389 nc = safe_vgetc();
2390 if (c == nc)
2391 continue;
2392 vungetc(nc);
2393 mouse_row = save_mouse_row;
2394 mouse_col = save_mouse_col;
2395 }
2396 }
2397 break;
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400#ifdef FEAT_MOUSESHAPE
2401 /* May have stopped dragging the status or separator line. The pointer is
2402 * most likely still on the status or separator line. */
2403 if (!is_drag && drag_status_line)
2404 {
2405 drag_status_line = FALSE;
2406 update_mouseshape(SHAPE_IDX_STATUS);
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 if (!is_drag && drag_sep_line)
2409 {
2410 drag_sep_line = FALSE;
2411 update_mouseshape(SHAPE_IDX_VSEP);
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413#endif
2414
2415 /*
2416 * Ignore drag and release events if we didn't get a click.
2417 */
2418 if (is_click)
2419 got_click = TRUE;
2420 else
2421 {
2422 if (!got_click) /* didn't get click, ignore */
2423 return FALSE;
2424 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002427#ifdef FEAT_WINDOWS
2428 if (in_tab_line)
2429 {
2430 in_tab_line = FALSE;
2431 return FALSE;
2432 }
2433#endif
2434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 /*
2438 * CTRL right mouse button does CTRL-T
2439 */
2440 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2441 {
2442 if (State & INSERT)
2443 stuffcharReadbuff(Ctrl_O);
2444 if (count > 1)
2445 stuffnumReadbuff(count);
2446 stuffcharReadbuff(Ctrl_T);
2447 got_click = FALSE; /* ignore drag&release now */
2448 return FALSE;
2449 }
2450
2451 /*
2452 * CTRL only works with left mouse button
2453 */
2454 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2455 return FALSE;
2456
2457 /*
2458 * When a modifier is down, ignore drag and release events, as well as
2459 * multiple clicks and the middle mouse button.
2460 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2461 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002462 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2463 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 && (!is_click
2465 || (mod_mask & MOD_MASK_MULTI_CLICK)
2466 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002467 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 && mouse_model_popup()
2469 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002470 && !((mod_mask & MOD_MASK_ALT)
2471 && !mouse_model_popup()
2472 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 )
2474 return FALSE;
2475
2476 /*
2477 * If the button press was used as the movement command for an operator
2478 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2479 * drag/release events.
2480 */
2481 if (!is_click && which_button == MOUSE_MIDDLE)
2482 return FALSE;
2483
2484 if (oap != NULL)
2485 regname = oap->regname;
2486 else
2487 regname = 0;
2488
2489 /*
2490 * Middle mouse button does a 'put' of the selected text
2491 */
2492 if (which_button == MOUSE_MIDDLE)
2493 {
2494 if (State == NORMAL)
2495 {
2496 /*
2497 * If an operator was pending, we don't know what the user wanted
2498 * to do. Go back to normal mode: Clear the operator and beep().
2499 */
2500 if (oap != NULL && oap->op_type != OP_NOP)
2501 {
2502 clearopbeep(oap);
2503 return FALSE;
2504 }
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 /*
2507 * If visual was active, yank the highlighted text and put it
2508 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002509 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 */
2511 if (VIsual_active)
2512 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002513 if (VIsual_select)
2514 {
2515 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002516 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002517 }
2518 else
2519 {
2520 stuffcharReadbuff('y');
2521 stuffcharReadbuff(K_MIDDLEMOUSE);
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 do_always = TRUE; /* ignore 'mouse' setting next time */
2524 return FALSE;
2525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 /*
2527 * The rest is below jump_to_mouse()
2528 */
2529 }
2530
2531 else if ((State & INSERT) == 0)
2532 return FALSE;
2533
2534 /*
2535 * Middle click in insert mode doesn't move the mouse, just insert the
2536 * contents of a register. '.' register is special, can't insert that
2537 * with do_put().
2538 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2539 * happens for the GUI).
2540 */
2541 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2542 {
2543 if (regname == '.')
2544 insert_reg(regname, TRUE);
2545 else
2546 {
2547#ifdef FEAT_CLIPBOARD
2548 if (clip_star.available && regname == 0)
2549 regname = '*';
2550#endif
2551 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2552 insert_reg(regname, TRUE);
2553 else
2554 {
2555 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2556
2557 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2558 AppendCharToRedobuff(Ctrl_R);
2559 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2560 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2561 }
2562 }
2563 return FALSE;
2564 }
2565 }
2566
2567 /* When dragging or button-up stay in the same window. */
2568 if (!is_click)
2569 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2570
2571 start_visual.lnum = 0;
2572
Bram Moolenaarf740b292006-02-16 22:11:02 +00002573#ifdef FEAT_WINDOWS
2574 /* Check for clicking in the tab page line. */
2575 if (mouse_row == 0 && firstwin->w_winrow > 0)
2576 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002577 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002578 {
2579 if (in_tab_line)
2580 {
2581 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002582 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2583 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002584 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002585 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002586 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002587
Bram Moolenaarf740b292006-02-16 22:11:02 +00002588 /* click in a tab selects that tab page */
2589 if (is_click
2590# ifdef FEAT_CMDWIN
2591 && cmdwin_type == 0
2592# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002593 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002594 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002595 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002596 c1 = TabPageIdxs[mouse_col];
2597 if (c1 >= 0)
2598 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002599 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2600 {
2601 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002602 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002603 tabpage_new();
2604 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2605 }
2606 else
2607 {
2608 /* Go to specified tab page, or next one if not clicking
2609 * on a label. */
2610 goto_tabpage(c1);
2611
2612 /* It's like clicking on the status line of a window. */
2613 if (curwin != old_curwin)
2614 end_visual_mode();
2615 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002616 }
2617 else if (c1 < 0)
2618 {
2619 tabpage_T *tp;
2620
2621 /* Close the current or specified tab page. */
2622 if (c1 == -999)
2623 tp = curtab;
2624 else
2625 tp = find_tabpage(-c1);
2626 if (tp == curtab)
2627 {
2628 if (first_tabpage->tp_next != NULL)
2629 tabpage_close(FALSE);
2630 }
2631 else if (tp != NULL)
2632 tabpage_close_other(tp, FALSE);
2633 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002634 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002635 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002636 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002637 else if (is_drag && in_tab_line)
2638 {
2639 c1 = TabPageIdxs[mouse_col];
2640 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2641 return FALSE;
2642 }
2643
Bram Moolenaarf740b292006-02-16 22:11:02 +00002644#endif
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 /*
2647 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2648 * right button up -> pop-up menu
2649 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002650 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 */
2652 if (mouse_model_popup())
2653 {
2654 if (which_button == MOUSE_RIGHT
2655 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2656 {
2657 /*
2658 * NOTE: Ignore right button down and drag mouse events.
2659 * Windows only shows the popup menu on the button up event.
2660 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002661#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2662 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (!is_click)
2664 return FALSE;
2665#endif
2666#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2667 if (is_click || is_drag)
2668 return FALSE;
2669#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002670#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2672 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2673 if (gui.in_use)
2674 {
2675 jump_flags = 0;
2676 if (STRCMP(p_mousem, "popup_setpos") == 0)
2677 {
2678 /* First set the cursor position before showing the popup
2679 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (VIsual_active)
2681 {
2682 pos_T m_pos;
2683
2684 /*
2685 * set MOUSE_MAY_STOP_VIS if we are outside the
2686 * selection or the current window (might have false
2687 * negative here)
2688 */
2689 if (mouse_row < W_WINROW(curwin)
2690 || mouse_row
2691 > (W_WINROW(curwin) + curwin->w_height))
2692 jump_flags = MOUSE_MAY_STOP_VIS;
2693 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2694 jump_flags = MOUSE_MAY_STOP_VIS;
2695 else
2696 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002697 if ((LT_POS(curwin->w_cursor, VIsual)
2698 && (LT_POS(m_pos, curwin->w_cursor)
2699 || LT_POS(VIsual, m_pos)))
2700 || (LT_POS(VIsual, curwin->w_cursor)
2701 && (LT_POS(m_pos, VIsual)
2702 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {
2704 jump_flags = MOUSE_MAY_STOP_VIS;
2705 }
2706 else if (VIsual_mode == Ctrl_V)
2707 {
2708 getvcols(curwin, &curwin->w_cursor, &VIsual,
2709 &leftcol, &rightcol);
2710 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2711 if (m_pos.col < leftcol || m_pos.col > rightcol)
2712 jump_flags = MOUSE_MAY_STOP_VIS;
2713 }
2714 }
2715 }
2716 else
2717 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 }
2719 if (jump_flags)
2720 {
2721 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002722 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 setcursor();
2724 out_flush(); /* Update before showing popup menu */
2725 }
2726# ifdef FEAT_MENU
2727 gui_show_popupmenu();
2728# endif
2729 return (jump_flags & CURSOR_MOVED) != 0;
2730 }
2731 else
2732 return FALSE;
2733#else
2734 return FALSE;
2735#endif
2736 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002737 if (which_button == MOUSE_LEFT
2738 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
2740 which_button = MOUSE_RIGHT;
2741 mod_mask &= ~MOD_MASK_SHIFT;
2742 }
2743 }
2744
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if ((State & (NORMAL | INSERT))
2746 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2747 {
2748 if (which_button == MOUSE_LEFT)
2749 {
2750 if (is_click)
2751 {
2752 /* stop Visual mode for a left click in a window, but not when
2753 * on a status line */
2754 if (VIsual_active)
2755 jump_flags |= MOUSE_MAY_STOP_VIS;
2756 }
2757 else if (mouse_has(MOUSE_VISUAL))
2758 jump_flags |= MOUSE_MAY_VIS;
2759 }
2760 else if (which_button == MOUSE_RIGHT)
2761 {
2762 if (is_click && VIsual_active)
2763 {
2764 /*
2765 * Remember the start and end of visual before moving the
2766 * cursor.
2767 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002768 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
2770 start_visual = curwin->w_cursor;
2771 end_visual = VIsual;
2772 }
2773 else
2774 {
2775 start_visual = VIsual;
2776 end_visual = curwin->w_cursor;
2777 }
2778 }
2779 jump_flags |= MOUSE_FOCUS;
2780 if (mouse_has(MOUSE_VISUAL))
2781 jump_flags |= MOUSE_MAY_VIS;
2782 }
2783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 /*
2786 * If an operator is pending, ignore all drags and releases until the
2787 * next mouse click.
2788 */
2789 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2790 {
2791 got_click = FALSE;
2792 oap->motion_type = MCHAR;
2793 }
2794
2795 /* When releasing the button let jump_to_mouse() know. */
2796 if (!is_click && !is_drag)
2797 jump_flags |= MOUSE_RELEASED;
2798
2799 /*
2800 * JUMP!
2801 */
2802 jump_flags = jump_to_mouse(jump_flags,
2803 oap == NULL ? NULL : &(oap->inclusive), which_button);
2804 moved = (jump_flags & CURSOR_MOVED);
2805 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar829c8e32016-03-19 23:07:23 +01002806#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar829c8e32016-03-19 23:07:23 +01002808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809
2810#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002811 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2813 {
2814 int key = KEY2TERMCAP1(c);
2815
2816 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2817 || key == (int)KE_RIGHTRELEASE)
2818 netbeans_button_release(which_button);
2819 }
2820#endif
2821
2822 /* When jumping to another window, clear a pending operator. That's a bit
2823 * friendlier than beeping and not jumping to that window. */
2824 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2825 clearop(oap);
2826
2827#ifdef FEAT_FOLDING
2828 if (mod_mask == 0
2829 && !is_drag
2830 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2831 && which_button == MOUSE_LEFT)
2832 {
2833 /* open or close a fold at this line */
2834 if (jump_flags & MOUSE_FOLD_OPEN)
2835 openFold(curwin->w_cursor.lnum, 1L);
2836 else
2837 closeFold(curwin->w_cursor.lnum, 1L);
2838 /* don't move the cursor if still in the same window */
2839 if (curwin == old_curwin)
2840 curwin->w_cursor = save_cursor;
2841 }
2842#endif
2843
2844#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2845 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2846 {
2847 clip_modeless(which_button, is_click, is_drag);
2848 return FALSE;
2849 }
2850#endif
2851
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002853 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 if (VIsual_active && is_drag && p_so)
2855 {
2856 /* In the very first line, allow scrolling one line */
2857 if (mouse_row == 0)
2858 mouse_dragging = 2;
2859 else
2860 mouse_dragging = 1;
2861 }
2862
2863 /* When dragging the mouse above the window, scroll down. */
2864 if (is_drag && mouse_row < 0 && !in_status_line)
2865 {
2866 scroll_redraw(FALSE, 1L);
2867 mouse_row = 0;
2868 }
2869
2870 if (start_visual.lnum) /* right click in visual mode */
2871 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002872 /* When ALT is pressed make Visual mode blockwise. */
2873 if (mod_mask & MOD_MASK_ALT)
2874 VIsual_mode = Ctrl_V;
2875
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 /*
2877 * In Visual-block mode, divide the area in four, pick up the corner
2878 * that is in the quarter that the cursor is in.
2879 */
2880 if (VIsual_mode == Ctrl_V)
2881 {
2882 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2883 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2884 end_visual.col = leftcol;
2885 else
2886 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002887 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 end_visual.lnum = start_visual.lnum;
2890
2891 /* move VIsual to the right column */
2892 start_visual = curwin->w_cursor; /* save the cursor pos */
2893 curwin->w_cursor = end_visual;
2894 coladvance(end_visual.col);
2895 VIsual = curwin->w_cursor;
2896 curwin->w_cursor = start_visual; /* restore the cursor */
2897 }
2898 else
2899 {
2900 /*
2901 * If the click is before the start of visual, change the start.
2902 * If the click is after the end of visual, change the end. If
2903 * the click is inside the visual, change the closest side.
2904 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002905 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002907 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 VIsual = start_visual;
2909 else
2910 {
2911 /* In the same line, compare column number */
2912 if (end_visual.lnum == start_visual.lnum)
2913 {
2914 if (curwin->w_cursor.col - start_visual.col >
2915 end_visual.col - curwin->w_cursor.col)
2916 VIsual = start_visual;
2917 else
2918 VIsual = end_visual;
2919 }
2920
2921 /* In different lines, compare line number */
2922 else
2923 {
2924 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2925 (end_visual.lnum - curwin->w_cursor.lnum);
2926
2927 if (diff > 0) /* closest to end */
2928 VIsual = start_visual;
2929 else if (diff < 0) /* closest to start */
2930 VIsual = end_visual;
2931 else /* in the middle line */
2932 {
2933 if (curwin->w_cursor.col <
2934 (start_visual.col + end_visual.col) / 2)
2935 VIsual = end_visual;
2936 else
2937 VIsual = start_visual;
2938 }
2939 }
2940 }
2941 }
2942 }
2943 /*
2944 * If Visual mode started in insert mode, execute "CTRL-O"
2945 */
2946 else if ((State & INSERT) && VIsual_active)
2947 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 /*
2950 * Middle mouse click: Put text before cursor.
2951 */
2952 if (which_button == MOUSE_MIDDLE)
2953 {
2954#ifdef FEAT_CLIPBOARD
2955 if (clip_star.available && regname == 0)
2956 regname = '*';
2957#endif
2958 if (yank_register_mline(regname))
2959 {
2960 if (mouse_past_bottom)
2961 dir = FORWARD;
2962 }
2963 else if (mouse_past_eol)
2964 dir = FORWARD;
2965
2966 if (fixindent)
2967 {
2968 c1 = (dir == BACKWARD) ? '[' : ']';
2969 c2 = 'p';
2970 }
2971 else
2972 {
2973 c1 = (dir == FORWARD) ? 'p' : 'P';
2974 c2 = NUL;
2975 }
2976 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2977
2978 /*
2979 * Remember where the paste started, so in edit() Insstart can be set
2980 * to this position
2981 */
2982 if (restart_edit != 0)
2983 where_paste_started = curwin->w_cursor;
2984 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2985 }
2986
2987#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2988 /*
2989 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2990 * error under the mouse pointer.
2991 */
2992 else if (((mod_mask & MOD_MASK_CTRL)
2993 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2994 && bt_quickfix(curbuf))
2995 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002996 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002997 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002998 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002999 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 got_click = FALSE; /* ignore drag&release now */
3001 }
3002#endif
3003
3004 /*
3005 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3006 * under the mouse pointer.
3007 */
3008 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3009 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3010 {
3011 if (State & INSERT)
3012 stuffcharReadbuff(Ctrl_O);
3013 stuffcharReadbuff(Ctrl_RSB);
3014 got_click = FALSE; /* ignore drag&release now */
3015 }
3016
3017 /*
3018 * Shift-Mouse click searches for the next occurrence of the word under
3019 * the mouse pointer
3020 */
3021 else if ((mod_mask & MOD_MASK_SHIFT))
3022 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003023 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 stuffcharReadbuff(Ctrl_O);
3025 if (which_button == MOUSE_LEFT)
3026 stuffcharReadbuff('*');
3027 else /* MOUSE_RIGHT */
3028 stuffcharReadbuff('#');
3029 }
3030
3031 /* Handle double clicks, unless on status line */
3032 else if (in_status_line)
3033 {
3034#ifdef FEAT_MOUSESHAPE
3035 if ((is_drag || is_click) && !drag_status_line)
3036 {
3037 drag_status_line = TRUE;
3038 update_mouseshape(-1);
3039 }
3040#endif
3041 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003042#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else if (in_sep_line)
3044 {
3045# ifdef FEAT_MOUSESHAPE
3046 if ((is_drag || is_click) && !drag_sep_line)
3047 {
3048 drag_sep_line = TRUE;
3049 update_mouseshape(-1);
3050 }
3051# endif
3052 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3055 && mouse_has(MOUSE_VISUAL))
3056 {
3057 if (is_click || !VIsual_active)
3058 {
3059 if (VIsual_active)
3060 orig_cursor = VIsual;
3061 else
3062 {
3063 check_visual_highlight();
3064 VIsual = curwin->w_cursor;
3065 orig_cursor = VIsual;
3066 VIsual_active = TRUE;
3067 VIsual_reselect = TRUE;
3068 /* start Select mode if 'selectmode' contains "mouse" */
3069 may_start_select('o');
3070 setmouse();
3071 }
3072 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003073 {
3074 /* Double click with ALT pressed makes it blockwise. */
3075 if (mod_mask & MOD_MASK_ALT)
3076 VIsual_mode = Ctrl_V;
3077 else
3078 VIsual_mode = 'v';
3079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3081 VIsual_mode = 'V';
3082 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3083 VIsual_mode = Ctrl_V;
3084#ifdef FEAT_CLIPBOARD
3085 /* Make sure the clipboard gets updated. Needed because start and
3086 * end may still be the same, and the selection needs to be owned */
3087 clip_star.vmode = NUL;
3088#endif
3089 }
3090 /*
3091 * A double click selects a word or a block.
3092 */
3093 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3094 {
3095 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003096 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097
3098 if (is_click)
3099 {
3100 /* If the character under the cursor (skipping white space) is
3101 * not a word character, try finding a match and select a (),
3102 * {}, [], #if/#endif, etc. block. */
3103 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003104 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 inc(&end_visual);
3106 if (oap != NULL)
3107 oap->motion_type = MCHAR;
3108 if (oap != NULL
3109 && VIsual_mode == 'v'
3110 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003111 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 && (pos = findmatch(oap, NUL)) != NULL)
3113 {
3114 curwin->w_cursor = *pos;
3115 if (oap->motion_type == MLINE)
3116 VIsual_mode = 'V';
3117 else if (*p_sel == 'e')
3118 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003119 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 ++VIsual.col;
3121 else
3122 ++curwin->w_cursor.col;
3123 }
3124 }
3125 }
3126
3127 if (pos == NULL && (is_click || is_drag))
3128 {
3129 /* When not found a match or when dragging: extend to include
3130 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003131 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 find_start_of_word(&curwin->w_cursor);
3134 find_end_of_word(&VIsual);
3135 }
3136 else
3137 {
3138 find_start_of_word(&VIsual);
3139 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3140#ifdef FEAT_MBYTE
3141 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003142 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143#else
3144 ++curwin->w_cursor.col;
3145#endif
3146 find_end_of_word(&curwin->w_cursor);
3147 }
3148 }
3149 curwin->w_set_curswant = TRUE;
3150 }
3151 if (is_click)
3152 redraw_curbuf_later(INVERTED); /* update the inversion */
3153 }
3154 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003155 {
3156 if (mod_mask & MOD_MASK_ALT)
3157 VIsual_mode = Ctrl_V;
3158 else
3159 VIsual_mode = 'v';
3160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
3162 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003163 if ((!VIsual_active && old_active && mode_displayed)
3164 || (VIsual_active && p_smd && msg_silent == 0
3165 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
3168 return moved;
3169}
3170
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171/*
3172 * Move "pos" back to the start of the word it's in.
3173 */
3174 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003175find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 char_u *line;
3178 int cclass;
3179 int col;
3180
3181 line = ml_get(pos->lnum);
3182 cclass = get_mouse_class(line + pos->col);
3183
3184 while (pos->col > 0)
3185 {
3186 col = pos->col - 1;
3187#ifdef FEAT_MBYTE
3188 col -= (*mb_head_off)(line, line + col);
3189#endif
3190 if (get_mouse_class(line + col) != cclass)
3191 break;
3192 pos->col = col;
3193 }
3194}
3195
3196/*
3197 * Move "pos" forward to the end of the word it's in.
3198 * When 'selection' is "exclusive", the position is just after the word.
3199 */
3200 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003201find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
3203 char_u *line;
3204 int cclass;
3205 int col;
3206
3207 line = ml_get(pos->lnum);
3208 if (*p_sel == 'e' && pos->col > 0)
3209 {
3210 --pos->col;
3211#ifdef FEAT_MBYTE
3212 pos->col -= (*mb_head_off)(line, line + pos->col);
3213#endif
3214 }
3215 cclass = get_mouse_class(line + pos->col);
3216 while (line[pos->col] != NUL)
3217 {
3218#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003219 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220#else
3221 col = pos->col + 1;
3222#endif
3223 if (get_mouse_class(line + col) != cclass)
3224 {
3225 if (*p_sel == 'e')
3226 pos->col = col;
3227 break;
3228 }
3229 pos->col = col;
3230 }
3231}
3232
3233/*
3234 * Get class of a character for selection: same class means same word.
3235 * 0: blank
3236 * 1: punctuation groups
3237 * 2: normal word character
3238 * >2: multi-byte word character.
3239 */
3240 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003241get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242{
3243 int c;
3244
3245#ifdef FEAT_MBYTE
3246 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3247 return mb_get_class(p);
3248#endif
3249
3250 c = *p;
3251 if (c == ' ' || c == '\t')
3252 return 0;
3253
3254 if (vim_iswordc(c))
3255 return 2;
3256
3257 /*
3258 * There are a few special cases where we want certain combinations of
3259 * characters to be considered as a single word. These are things like
3260 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003261 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 */
3263 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3264 return 1;
3265 return c;
3266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#endif /* FEAT_MOUSE */
3268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269/*
3270 * Check if highlighting for visual mode is possible, give a warning message
3271 * if not.
3272 */
3273 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003274check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 static int did_check = FALSE;
3277
3278 if (full_screen)
3279 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003280 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 MSG(_("Warning: terminal cannot highlight"));
3282 did_check = TRUE;
3283 }
3284}
3285
3286/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003287 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 * This function should ALWAYS be called to end Visual mode, except from
3289 * do_pending_operator().
3290 */
3291 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003292end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294#ifdef FEAT_CLIPBOARD
3295 /*
3296 * If we are using the clipboard, then remember what was selected in case
3297 * we need to paste it somewhere while we still own the selection.
3298 * Only do this when the clipboard is already owned. Don't want to grab
3299 * the selection when hitting ESC.
3300 */
3301 if (clip_star.available && clip_star.owned)
3302 clip_auto_select();
3303#endif
3304
3305 VIsual_active = FALSE;
3306#ifdef FEAT_MOUSE
3307 setmouse();
3308 mouse_dragging = 0;
3309#endif
3310
3311 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003312 curbuf->b_visual.vi_mode = VIsual_mode;
3313 curbuf->b_visual.vi_start = VIsual;
3314 curbuf->b_visual.vi_end = curwin->w_cursor;
3315 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316#ifdef FEAT_EVAL
3317 curbuf->b_visual_mode_eval = VIsual_mode;
3318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319#ifdef FEAT_VIRTUALEDIT
3320 if (!virtual_active())
3321 curwin->w_cursor.coladd = 0;
3322#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003323 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003325 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326}
3327
3328/*
3329 * Reset VIsual_active and VIsual_reselect.
3330 */
3331 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003332reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 if (VIsual_active)
3335 {
3336 end_visual_mode();
3337 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3338 }
3339 VIsual_reselect = FALSE;
3340}
3341
3342/*
3343 * Reset VIsual_active and VIsual_reselect if it's set.
3344 */
3345 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003346reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 if (VIsual_active)
3349 {
3350 end_visual_mode();
3351 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3352 VIsual_reselect = FALSE;
3353 }
3354}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356/*
3357 * Check for a balloon-eval special item to include when searching for an
3358 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3359 * Returns TRUE if the character at "*ptr" should be included.
3360 * "dir" is FORWARD or BACKWARD, the direction of searching.
3361 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3362 * "bnp" points to a counter for square brackets.
3363 */
3364 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003365find_is_eval_item(
3366 char_u *ptr,
3367 int *colp,
3368 int *bnp,
3369 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
3371 /* Accept everything inside []. */
3372 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3373 ++*bnp;
3374 if (*bnp > 0)
3375 {
3376 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3377 --*bnp;
3378 return TRUE;
3379 }
3380
3381 /* skip over "s.var" */
3382 if (*ptr == '.')
3383 return TRUE;
3384
3385 /* two-character item: s->var */
3386 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3387 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3388 {
3389 *colp += dir;
3390 return TRUE;
3391 }
3392 return FALSE;
3393}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395/*
3396 * Find the identifier under or to the right of the cursor.
3397 * "find_type" can have one of three values:
3398 * FIND_IDENT: find an identifier (keyword)
3399 * FIND_STRING: find any non-white string
3400 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003401 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 *
3403 * There are three steps:
3404 * 1. Search forward for the start of an identifier/string. Doesn't move if
3405 * already on one.
3406 * 2. Search backward for the start of this identifier/string.
3407 * This doesn't match the real Vi but I like it a little better and it
3408 * shouldn't bother anyone.
3409 * 3. Search forward to the end of this identifier/string.
3410 * When FIND_IDENT isn't defined, we backup until a blank.
3411 *
3412 * Returns the length of the string, or zero if no string is found.
3413 * If a string is found, a pointer to the string is put in "*string". This
3414 * string is not always NUL terminated.
3415 */
3416 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003417find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3420 curwin->w_cursor.col, string, find_type);
3421}
3422
3423/*
3424 * Like find_ident_under_cursor(), but for any window and any position.
3425 * However: Uses 'iskeyword' from the current window!.
3426 */
3427 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003428find_ident_at_pos(
3429 win_T *wp,
3430 linenr_T lnum,
3431 colnr_T startcol,
3432 char_u **string,
3433 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
3435 char_u *ptr;
3436 int col = 0; /* init to shut up GCC */
3437 int i;
3438#ifdef FEAT_MBYTE
3439 int this_class = 0;
3440 int prev_class;
3441 int prevcol;
3442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445 /*
3446 * if i == 0: try to find an identifier
3447 * if i == 1: try to find any non-white string
3448 */
3449 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3450 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3451 {
3452 /*
3453 * 1. skip to start of identifier/string
3454 */
3455 col = startcol;
3456#ifdef FEAT_MBYTE
3457 if (has_mbyte)
3458 {
3459 while (ptr[col] != NUL)
3460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 /* Stop at a ']' to evaluate "a[x]". */
3462 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 this_class = mb_get_class(ptr + col);
3465 if (this_class != 0 && (i == 1 || this_class != 1))
3466 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003467 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469 }
3470 else
3471#endif
3472 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003473 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 )
3476 ++col;
3477
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 /* When starting on a ']' count it, so that we include the '['. */
3479 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
3481 /*
3482 * 2. Back up to start of identifier/string.
3483 */
3484#ifdef FEAT_MBYTE
3485 if (has_mbyte)
3486 {
3487 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3489 this_class = mb_get_class((char_u *)"a");
3490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003492 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
3494 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3495 prev_class = mb_get_class(ptr + prevcol);
3496 if (this_class != prev_class
3497 && (i == 0
3498 || prev_class == 0
3499 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 && (!(find_type & FIND_EVAL)
3501 || prevcol == 0
3502 || !find_is_eval_item(ptr + prevcol, &prevcol,
3503 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 )
3505 break;
3506 col = prevcol;
3507 }
3508
3509 /* If we don't want just any old string, or we've found an
3510 * identifier, stop searching. */
3511 if (this_class > 2)
3512 this_class = 2;
3513 if (!(find_type & FIND_STRING) || this_class == 2)
3514 break;
3515 }
3516 else
3517#endif
3518 {
3519 while (col > 0
3520 && ((i == 0
3521 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003522 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 && (!(find_type & FIND_IDENT)
3524 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 || ((find_type & FIND_EVAL)
3526 && col > 1
3527 && find_is_eval_item(ptr + col - 1, &col,
3528 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 ))
3530 --col;
3531
3532 /* If we don't want just any old string, or we've found an
3533 * identifier, stop searching. */
3534 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3535 break;
3536 }
3537 }
3538
3539 if (ptr[col] == NUL || (i == 0 && (
3540#ifdef FEAT_MBYTE
3541 has_mbyte ? this_class != 2 :
3542#endif
3543 !vim_iswordc(ptr[col]))))
3544 {
3545 /*
3546 * didn't find an identifier or string
3547 */
3548 if (find_type & FIND_STRING)
3549 EMSG(_("E348: No string under cursor"));
3550 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003551 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 return 0;
3553 }
3554 ptr += col;
3555 *string = ptr;
3556
3557 /*
3558 * 3. Find the end if the identifier/string.
3559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 bn = 0;
3561 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 col = 0;
3563#ifdef FEAT_MBYTE
3564 if (has_mbyte)
3565 {
3566 /* Search for point of changing multibyte character class. */
3567 this_class = mb_get_class(ptr);
3568 while (ptr[col] != NUL
3569 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3570 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 || ((find_type & FIND_EVAL)
3572 && col <= (int)startcol
3573 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003575 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577 else
3578#endif
3579 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003580 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 || ((find_type & FIND_EVAL)
3582 && col <= (int)startcol
3583 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 )
3585 {
3586 ++col;
3587 }
3588
3589 return col;
3590}
3591
3592/*
3593 * Prepare for redo of a normal command.
3594 */
3595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003596prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597{
3598 prep_redo(cap->oap->regname, cap->count0,
3599 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3600}
3601
3602/*
3603 * Prepare for redo of any command.
3604 * Note that only the last argument can be a multi-byte char.
3605 */
3606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003607prep_redo(
3608 int regname,
3609 long num,
3610 int cmd1,
3611 int cmd2,
3612 int cmd3,
3613 int cmd4,
3614 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
3616 ResetRedobuff();
3617 if (regname != 0) /* yank from specified buffer */
3618 {
3619 AppendCharToRedobuff('"');
3620 AppendCharToRedobuff(regname);
3621 }
3622 if (num)
3623 AppendNumberToRedobuff(num);
3624
3625 if (cmd1 != NUL)
3626 AppendCharToRedobuff(cmd1);
3627 if (cmd2 != NUL)
3628 AppendCharToRedobuff(cmd2);
3629 if (cmd3 != NUL)
3630 AppendCharToRedobuff(cmd3);
3631 if (cmd4 != NUL)
3632 AppendCharToRedobuff(cmd4);
3633 if (cmd5 != NUL)
3634 AppendCharToRedobuff(cmd5);
3635}
3636
3637/*
3638 * check for operator active and clear it
3639 *
3640 * return TRUE if operator was active
3641 */
3642 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003643checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 if (oap->op_type == OP_NOP)
3646 return FALSE;
3647 clearopbeep(oap);
3648 return TRUE;
3649}
3650
3651/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003652 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003654 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 */
3656 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003657checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003659 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return FALSE;
3661 clearopbeep(oap);
3662 return TRUE;
3663}
3664
3665 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003666clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668 oap->op_type = OP_NOP;
3669 oap->regname = 0;
3670 oap->motion_force = NUL;
3671 oap->use_reg_one = FALSE;
3672}
3673
3674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003675clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 clearop(oap);
3678 beep_flush();
3679}
3680
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681/*
3682 * Remove the shift modifier from a special key.
3683 */
3684 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003685unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 switch (cap->cmdchar)
3688 {
3689 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3690 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3691 case K_S_UP: cap->cmdchar = K_UP; break;
3692 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3693 case K_S_HOME: cap->cmdchar = K_HOME; break;
3694 case K_S_END: cap->cmdchar = K_END; break;
3695 }
3696 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3697}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003699/*
3700 * If the mode is currently displayed clear the command line or update the
3701 * command displayed.
3702 */
3703 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003704may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003705{
3706 if (mode_displayed)
3707 clear_cmdline = TRUE; /* unshow visual mode later */
3708#ifdef FEAT_CMDL_INFO
3709 else
3710 clear_showcmd();
3711#endif
3712}
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3715/*
3716 * Routines for displaying a partly typed command
3717 */
3718
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003719#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720static char_u showcmd_buf[SHOWCMD_BUFLEN];
3721static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3722static int showcmd_is_clear = TRUE;
3723static int showcmd_visual = FALSE;
3724
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003725static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003728clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
3730 if (!p_sc)
3731 return;
3732
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (VIsual_active && !char_avail())
3734 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003735 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 long lines;
3737 colnr_T leftcol, rightcol;
3738 linenr_T top, bot;
3739
3740 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003741 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
3743 top = VIsual.lnum;
3744 bot = curwin->w_cursor.lnum;
3745 }
3746 else
3747 {
3748 top = curwin->w_cursor.lnum;
3749 bot = VIsual.lnum;
3750 }
3751# ifdef FEAT_FOLDING
3752 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003753 (void)hasFolding(top, &top, NULL);
3754 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755# endif
3756 lines = bot - top + 1;
3757
3758 if (VIsual_mode == Ctrl_V)
3759 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003760# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003761 char_u *saved_sbr = p_sbr;
3762
3763 /* Make 'sbr' empty for a moment to get the correct size. */
3764 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003767# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003768 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3771 (long)(rightcol - leftcol + 1));
3772 }
3773 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3774 sprintf((char *)showcmd_buf, "%ld", lines);
3775 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003776 {
3777 char_u *s, *e;
3778 int l;
3779 int bytes = 0;
3780 int chars = 0;
3781
3782 if (cursor_bot)
3783 {
3784 s = ml_get_pos(&VIsual);
3785 e = ml_get_cursor();
3786 }
3787 else
3788 {
3789 s = ml_get_cursor();
3790 e = ml_get_pos(&VIsual);
3791 }
3792 while ((*p_sel != 'e') ? s <= e : s < e)
3793 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003794# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003795 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003796# else
3797 l = (*s == NUL) ? 0 : 1;
3798# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003799 if (l == 0)
3800 {
3801 ++bytes;
3802 ++chars;
3803 break; /* end of line */
3804 }
3805 bytes += l;
3806 ++chars;
3807 s += l;
3808 }
3809 if (bytes == chars)
3810 sprintf((char *)showcmd_buf, "%d", chars);
3811 else
3812 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3815 showcmd_visual = TRUE;
3816 }
3817 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
3819 showcmd_buf[0] = NUL;
3820 showcmd_visual = FALSE;
3821
3822 /* Don't actually display something if there is nothing to clear. */
3823 if (showcmd_is_clear)
3824 return;
3825 }
3826
3827 display_showcmd();
3828}
3829
3830/*
3831 * Add 'c' to string of shown command chars.
3832 * Return TRUE if output has been written (and setcursor() has been called).
3833 */
3834 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003835add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836{
3837 char_u *p;
3838 int old_len;
3839 int extra_len;
3840 int overflow;
3841#if defined(FEAT_MOUSE)
3842 int i;
3843 static int ignore[] =
3844 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003845# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3847 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003848# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003849 K_IGNORE, K_PS,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE,
3851 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3852 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003853 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003855 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 0
3857 };
3858#endif
3859
Bram Moolenaar09df3122006-01-23 22:23:09 +00003860 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return FALSE;
3862
3863 if (showcmd_visual)
3864 {
3865 showcmd_buf[0] = NUL;
3866 showcmd_visual = FALSE;
3867 }
3868
3869#if defined(FEAT_MOUSE)
3870 /* Ignore keys that are scrollbar updates and mouse clicks */
3871 if (IS_SPECIAL(c))
3872 for (i = 0; ignore[i] != 0; ++i)
3873 if (ignore[i] == c)
3874 return FALSE;
3875#endif
3876
3877 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003878 if (*p == ' ')
3879 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 old_len = (int)STRLEN(showcmd_buf);
3881 extra_len = (int)STRLEN(p);
3882 overflow = old_len + extra_len - SHOWCMD_COLS;
3883 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003884 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3885 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 STRCAT(showcmd_buf, p);
3887
3888 if (char_avail())
3889 return FALSE;
3890
3891 display_showcmd();
3892
3893 return TRUE;
3894}
3895
3896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003897add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898{
3899 if (!add_to_showcmd(c))
3900 setcursor();
3901}
3902
3903/*
3904 * Delete 'len' characters from the end of the shown command.
3905 */
3906 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003907del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
3909 int old_len;
3910
3911 if (!p_sc)
3912 return;
3913
3914 old_len = (int)STRLEN(showcmd_buf);
3915 if (len > old_len)
3916 len = old_len;
3917 showcmd_buf[old_len - len] = NUL;
3918
3919 if (!char_avail())
3920 display_showcmd();
3921}
3922
3923/*
3924 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3925 * something and there is a partial mapping.
3926 */
3927 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003928push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930 if (p_sc)
3931 STRCPY(old_showcmd_buf, showcmd_buf);
3932}
3933
3934 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003935pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937 if (!p_sc)
3938 return;
3939
3940 STRCPY(showcmd_buf, old_showcmd_buf);
3941
3942 display_showcmd();
3943}
3944
3945 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003946display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
3948 int len;
3949
3950 cursor_off();
3951
3952 len = (int)STRLEN(showcmd_buf);
3953 if (len == 0)
3954 showcmd_is_clear = TRUE;
3955 else
3956 {
3957 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3958 showcmd_is_clear = FALSE;
3959 }
3960
3961 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003962 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3963 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 */
3965 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3966
3967 setcursor(); /* put cursor back where it belongs */
3968}
3969#endif
3970
3971#ifdef FEAT_SCROLLBIND
3972/*
3973 * When "check" is FALSE, prepare for commands that scroll the window.
3974 * When "check" is TRUE, take care of scroll-binding after the window has
3975 * scrolled. Called from normal_cmd() and edit().
3976 */
3977 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003978do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979{
3980 static win_T *old_curwin = NULL;
3981 static linenr_T old_topline = 0;
3982#ifdef FEAT_DIFF
3983 static int old_topfill = 0;
3984#endif
3985 static buf_T *old_buf = NULL;
3986 static colnr_T old_leftcol = 0;
3987
3988 if (check && curwin->w_p_scb)
3989 {
3990 /* If a ":syncbind" command was just used, don't scroll, only reset
3991 * the values. */
3992 if (did_syncbind)
3993 did_syncbind = FALSE;
3994 else if (curwin == old_curwin)
3995 {
3996 /*
3997 * Synchronize other windows, as necessary according to
3998 * 'scrollbind'. Don't do this after an ":edit" command, except
3999 * when 'diff' is set.
4000 */
4001 if ((curwin->w_buffer == old_buf
4002#ifdef FEAT_DIFF
4003 || curwin->w_p_diff
4004#endif
4005 )
4006 && (curwin->w_topline != old_topline
4007#ifdef FEAT_DIFF
4008 || curwin->w_topfill != old_topfill
4009#endif
4010 || curwin->w_leftcol != old_leftcol))
4011 {
4012 check_scrollbind(curwin->w_topline - old_topline,
4013 (long)(curwin->w_leftcol - old_leftcol));
4014 }
4015 }
4016 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4017 {
4018 /*
4019 * When switching between windows, make sure that the relative
4020 * vertical offset is valid for the new window. The relative
4021 * offset is invalid whenever another 'scrollbind' window has
4022 * scrolled to a point that would force the current window to
4023 * scroll past the beginning or end of its buffer. When the
4024 * resync is performed, some of the other 'scrollbind' windows may
4025 * need to jump so that the current window's relative position is
4026 * visible on-screen.
4027 */
4028 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4029 }
4030 curwin->w_scbind_pos = curwin->w_topline;
4031 }
4032
4033 old_curwin = curwin;
4034 old_topline = curwin->w_topline;
4035#ifdef FEAT_DIFF
4036 old_topfill = curwin->w_topfill;
4037#endif
4038 old_buf = curwin->w_buffer;
4039 old_leftcol = curwin->w_leftcol;
4040}
4041
4042/*
4043 * Synchronize any windows that have "scrollbind" set, based on the
4044 * number of rows by which the current window has changed
4045 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4046 */
4047 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004048check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
4050 int want_ver;
4051 int want_hor;
4052 win_T *old_curwin = curwin;
4053 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 int old_VIsual_select = VIsual_select;
4055 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 colnr_T tgt_leftcol = curwin->w_leftcol;
4057 long topline;
4058 long y;
4059
4060 /*
4061 * check 'scrollopt' string for vertical and horizontal scroll options
4062 */
4063 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4064#ifdef FEAT_DIFF
4065 want_ver |= old_curwin->w_p_diff;
4066#endif
4067 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4068
4069 /*
4070 * loop through the scrollbound windows and scroll accordingly
4071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004073 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
4075 curbuf = curwin->w_buffer;
4076 /* skip original window and windows with 'noscrollbind' */
4077 if (curwin != old_curwin && curwin->w_p_scb)
4078 {
4079 /*
4080 * do the vertical scroll
4081 */
4082 if (want_ver)
4083 {
4084#ifdef FEAT_DIFF
4085 if (old_curwin->w_p_diff && curwin->w_p_diff)
4086 {
4087 diff_set_topline(old_curwin, curwin);
4088 }
4089 else
4090#endif
4091 {
4092 curwin->w_scbind_pos += topline_diff;
4093 topline = curwin->w_scbind_pos;
4094 if (topline > curbuf->b_ml.ml_line_count)
4095 topline = curbuf->b_ml.ml_line_count;
4096 if (topline < 1)
4097 topline = 1;
4098
4099 y = topline - curwin->w_topline;
4100 if (y > 0)
4101 scrollup(y, FALSE);
4102 else
4103 scrolldown(-y, FALSE);
4104 }
4105
4106 redraw_later(VALID);
4107 cursor_correct();
4108#ifdef FEAT_WINDOWS
4109 curwin->w_redr_status = TRUE;
4110#endif
4111 }
4112
4113 /*
4114 * do the horizontal scroll
4115 */
4116 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4117 {
4118 curwin->w_leftcol = tgt_leftcol;
4119 leftcol_changed();
4120 }
4121 }
4122 }
4123
4124 /*
4125 * reset current-window
4126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 VIsual_select = old_VIsual_select;
4128 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 curwin = old_curwin;
4130 curbuf = old_curbuf;
4131}
4132#endif /* #ifdef FEAT_SCROLLBIND */
4133
4134/*
4135 * Command character that's ignored.
4136 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004137 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004140nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004142 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143}
4144
4145/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004146 * Command character that doesn't do anything, but unlike nv_ignore() does
4147 * start edit(). Used for "startinsert" executed while starting up.
4148 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004150nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004151{
4152}
4153
4154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * Command character doesn't exist.
4156 */
4157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004158nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
4160 clearopbeep(cap->oap);
4161}
4162
4163/*
4164 * <Help> and <F1> commands.
4165 */
4166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004167nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
4169 if (!checkclearopq(cap->oap))
4170 ex_help(NULL);
4171}
4172
4173/*
4174 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4175 */
4176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004177nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaard79e5502016-01-10 22:13:02 +01004179 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004180 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004181 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004182 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4183 op_addsub(cap->oap, cap->count1, cap->arg);
4184 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004185 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004186 else if (VIsual_active)
4187 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004188 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004189 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190}
4191
4192/*
4193 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4194 */
4195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004196nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
4198 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004199 {
4200#ifdef FEAT_WINDOWS
4201 if (mod_mask & MOD_MASK_CTRL)
4202 {
4203 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4204 if (cap->arg == BACKWARD)
4205 goto_tabpage(-(int)cap->count1);
4206 else
4207 goto_tabpage((int)cap->count0);
4208 }
4209 else
4210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213}
4214
4215/*
4216 * Implementation of "gd" and "gD" command.
4217 */
4218 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004219nv_gd(
4220 oparg_T *oap,
4221 int nchar,
4222 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
4224 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u *ptr;
4226
Bram Moolenaard9d30582005-05-18 22:10:28 +00004227 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004228 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4229 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004231#ifdef FEAT_FOLDING
4232 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4233 foldOpenCursor();
4234#endif
4235}
4236
4237/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004238 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4239 * otherwise.
4240 */
4241 static int
4242is_ident(char_u *line, int offset)
4243{
4244 int i;
4245 int incomment = FALSE;
4246 int instring = 0;
4247 int prev = 0;
4248
4249 for (i = 0; i < offset && line[i] != NUL; i++)
4250 {
4251 if (instring != 0)
4252 {
4253 if (prev != '\\' && line[i] == instring)
4254 instring = 0;
4255 }
4256 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4257 {
4258 instring = line[i];
4259 }
4260 else
4261 {
4262 if (incomment)
4263 {
4264 if (prev == '*' && line[i] == '/')
4265 incomment = FALSE;
4266 }
4267 else if (prev == '/' && line[i] == '*')
4268 {
4269 incomment = TRUE;
4270 }
4271 else if (prev == '/' && line[i] == '/')
4272 {
4273 return FALSE;
4274 }
4275 }
4276
4277 prev = line[i];
4278 }
4279
4280 return incomment == FALSE && instring == 0;
4281}
4282
4283/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004284 * Search for variable declaration of "ptr[len]".
4285 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4286 * current file ("gD").
4287 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004288 * Return FAIL when not found.
4289 */
4290 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004291find_decl(
4292 char_u *ptr,
4293 int len,
4294 int locally,
4295 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004296 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004297{
4298 char_u *pat;
4299 pos_T old_pos;
4300 pos_T par_pos;
4301 pos_T found_pos;
4302 int t;
4303 int save_p_ws;
4304 int save_p_scs;
4305 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004306 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004307 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004308 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004309
4310 if ((pat = alloc(len + 7)) == NULL)
4311 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004312
4313 /* Put "\V" before the pattern to avoid that the special meaning of "."
4314 * and "~" causes trouble. */
4315 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4316 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 old_pos = curwin->w_cursor;
4318 save_p_ws = p_ws;
4319 save_p_scs = p_scs;
4320 p_ws = FALSE; /* don't wrap around end of file now */
4321 p_scs = FALSE; /* don't switch ignorecase off now */
4322
4323 /*
4324 * With "gD" go to line 1.
4325 * With "gd" Search back for the start of the current function, then go
4326 * back until a blank line. If this fails go to line 1.
4327 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004328 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 setpcmark(); /* Set in findpar() otherwise */
4331 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004332 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 else
4335 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004336 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4338 --curwin->w_cursor.lnum;
4339 }
4340 curwin->w_cursor.col = 0;
4341
4342 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004343 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004344 for (;;)
4345 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004346 valid = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004347 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004348 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004349 if (curwin->w_cursor.lnum >= old_pos.lnum)
4350 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004351
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004352 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004353 {
4354 pos_T *pos;
4355
4356 /* Check that the block the match is in doesn't end before the
4357 * position where we started the search from. */
4358 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4359 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4360 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004361 {
4362 /* There can't be a useful match before the end of this block.
4363 * Skip to the end. */
4364 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004365 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004366 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004367 }
4368
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004369 if (t == FAIL)
4370 {
4371 /* If we previously found a valid position, use it. */
4372 if (found_pos.lnum != 0)
4373 {
4374 curwin->w_cursor = found_pos;
4375 t = OK;
4376 }
4377 break;
4378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004380 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004381 {
4382 /* Ignore this line, continue at start of next line. */
4383 ++curwin->w_cursor.lnum;
4384 curwin->w_cursor.col = 0;
4385 continue;
4386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004388 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4389
4390 /* If the current position is not a valid identifier and a previous
4391 * match is present, favor that one instead. */
4392 if (!valid && found_pos.lnum != 0)
4393 {
4394 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004395 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004396 }
4397
4398 /* Global search: use first valid match found */
4399 if (valid && !locally)
4400 break;
4401 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004402 {
4403 /* If we previously found a valid position, use it. */
4404 if (found_pos.lnum != 0)
4405 curwin->w_cursor = found_pos;
4406 break;
4407 }
4408
Bram Moolenaar226630a2016-10-08 19:21:31 +02004409 /* For finding a local variable and the match is before the "{" or
4410 * inside a comment, continue searching. For K&R style function
4411 * declarations this skips the function header without types. */
4412 if (!valid)
4413 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004414 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004415 }
4416 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004417 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004418 /* Remove SEARCH_START from flags to avoid getting stuck at one
4419 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004420 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004422
4423 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004425 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 curwin->w_cursor = old_pos;
4427 }
4428 else
4429 {
4430 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 /* "n" searches forward now */
4432 reset_search_dir();
4433 }
4434
4435 vim_free(pat);
4436 p_ws = save_p_ws;
4437 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004438
4439 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440}
4441
4442/*
4443 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4444 * lines rather than lines in the file.
4445 * 'dist' must be positive.
4446 *
4447 * Return OK if able to move cursor, FAIL otherwise.
4448 */
4449 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004450nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 int linelen = linetabsize(ml_get_curline());
4453 int retval = OK;
4454 int atend = FALSE;
4455 int n;
4456 int col_off1; /* margin offset for first screen line */
4457 int col_off2; /* margin offset for wrapped screen line */
4458 int width1; /* text width for first screen line */
4459 int width2; /* test width for wrapped screen line */
4460
4461 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004462 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 col_off1 = curwin_col_off();
4465 col_off2 = col_off1 - curwin_col_off2();
4466 width1 = W_WIDTH(curwin) - col_off1;
4467 width2 = W_WIDTH(curwin) - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004468 if (width2 == 0)
4469 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004471#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 /*
4476 * Instead of sticking at the last character of the buffer line we
4477 * try to stick in the last column of the screen.
4478 */
4479 if (curwin->w_curswant == MAXCOL)
4480 {
4481 atend = TRUE;
4482 validate_virtcol();
4483 if (width1 <= 0)
4484 curwin->w_curswant = 0;
4485 else
4486 {
4487 curwin->w_curswant = width1 - 1;
4488 if (curwin->w_virtcol > curwin->w_curswant)
4489 curwin->w_curswant += ((curwin->w_virtcol
4490 - curwin->w_curswant - 1) / width2 + 1) * width2;
4491 }
4492 }
4493 else
4494 {
4495 if (linelen > width1)
4496 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4497 else
4498 n = width1;
4499 if (curwin->w_curswant > (colnr_T)n + 1)
4500 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4501 * width2;
4502 }
4503
4504 while (dist--)
4505 {
4506 if (dir == BACKWARD)
4507 {
4508 if ((long)curwin->w_curswant >= width2)
4509 /* move back within line */
4510 curwin->w_curswant -= width2;
4511 else
4512 {
4513 /* to previous line */
4514 if (curwin->w_cursor.lnum == 1)
4515 {
4516 retval = FAIL;
4517 break;
4518 }
4519 --curwin->w_cursor.lnum;
4520#ifdef FEAT_FOLDING
4521 /* Move to the start of a closed fold. Don't do that when
4522 * 'foldopen' contains "all": it will open in a moment. */
4523 if (!(fdo_flags & FDO_ALL))
4524 (void)hasFolding(curwin->w_cursor.lnum,
4525 &curwin->w_cursor.lnum, NULL);
4526#endif
4527 linelen = linetabsize(ml_get_curline());
4528 if (linelen > width1)
4529 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4530 + 1) * width2;
4531 }
4532 }
4533 else /* dir == FORWARD */
4534 {
4535 if (linelen > width1)
4536 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4537 else
4538 n = width1;
4539 if (curwin->w_curswant + width2 < (colnr_T)n)
4540 /* move forward within line */
4541 curwin->w_curswant += width2;
4542 else
4543 {
4544 /* to next line */
4545#ifdef FEAT_FOLDING
4546 /* Move to the end of a closed fold. */
4547 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4548 &curwin->w_cursor.lnum);
4549#endif
4550 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4551 {
4552 retval = FAIL;
4553 break;
4554 }
4555 curwin->w_cursor.lnum++;
4556 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004557 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559 }
4560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004563 if (virtual_active() && atend)
4564 coladvance(MAXCOL);
4565 else
4566 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4569 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4570 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004571 colnr_T virtcol;
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 /*
4574 * Check for landing on a character that got split at the end of the
4575 * last line. We want to advance a screenline, not end up in the same
4576 * screenline or move two screenlines.
4577 */
4578 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004579 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004580# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004581 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4582 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004583# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004584
4585 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 && (curwin->w_curswant < (colnr_T)width1
4587 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4588 : ((curwin->w_curswant - width1) % width2
4589 > (colnr_T)width2 / 2)))
4590 --curwin->w_cursor.col;
4591 }
4592#endif
4593
4594 if (atend)
4595 curwin->w_curswant = MAXCOL; /* stick in the last column */
4596
4597 return retval;
4598}
4599
4600#ifdef FEAT_MOUSE
4601/*
4602 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4603 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004604 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4605 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 */
4607 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004608nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004610# ifdef FEAT_WINDOWS
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004611 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004613 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
4615 int row, col;
4616
4617 row = mouse_row;
4618 col = mouse_col;
4619
4620 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004621 wp = mouse_find_win(&row, &col);
4622 if (wp == NULL)
4623 return;
4624 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 curbuf = curwin->w_buffer;
4626 }
4627# endif
4628
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004629 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004631# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004632 if (term_use_loop())
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004633 send_keys_to_term(curbuf->b_term, cap->cmdchar, TRUE);
4634 else
4635# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004636 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4637 {
4638 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4639 }
4640 else
4641 {
4642 cap->count1 = 3;
4643 cap->count0 = 3;
4644 nv_scroll_line(cap);
4645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004647# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 else
4649 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004650 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4651 if (!curwin->w_p_wrap)
4652 {
4653 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004654
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004655 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4656 step = W_WIDTH(curwin);
4657 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4658 if (val < 0)
4659 val = 0;
4660
4661 gui_do_horiz_scroll(val, TRUE);
4662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004666# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 curwin->w_redr_status = TRUE;
4668
4669 curwin = old_curwin;
4670 curbuf = curwin->w_buffer;
4671# endif
4672}
4673
4674/*
4675 * Mouse clicks and drags.
4676 */
4677 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004678nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4681}
4682#endif
4683
4684/*
4685 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4686 * cap->arg must be TRUE for CTRL-E.
4687 */
4688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004689nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 if (!checkclearop(cap->oap))
4692 scroll_redraw(cap->arg, cap->count1);
4693}
4694
4695/*
4696 * Scroll "count" lines up or down, and redraw.
4697 */
4698 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004699scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
4701 linenr_T prev_topline = curwin->w_topline;
4702#ifdef FEAT_DIFF
4703 int prev_topfill = curwin->w_topfill;
4704#endif
4705 linenr_T prev_lnum = curwin->w_cursor.lnum;
4706
4707 if (up)
4708 scrollup(count, TRUE);
4709 else
4710 scrolldown(count, TRUE);
4711 if (p_so)
4712 {
4713 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4714 * valid, otherwise the screen jumps back at the end of the file. */
4715 cursor_correct();
4716 check_cursor_moved(curwin);
4717 curwin->w_valid |= VALID_TOPLINE;
4718
4719 /* If moved back to where we were, at least move the cursor, otherwise
4720 * we get stuck at one position. Don't move the cursor up if the
4721 * first line of the buffer is already on the screen */
4722 while (curwin->w_topline == prev_topline
4723#ifdef FEAT_DIFF
4724 && curwin->w_topfill == prev_topfill
4725#endif
4726 )
4727 {
4728 if (up)
4729 {
4730 if (curwin->w_cursor.lnum > prev_lnum
4731 || cursor_down(1L, FALSE) == FAIL)
4732 break;
4733 }
4734 else
4735 {
4736 if (curwin->w_cursor.lnum < prev_lnum
4737 || prev_topline == 1L
4738 || cursor_up(1L, FALSE) == FAIL)
4739 break;
4740 }
4741 /* Mark w_topline as valid, otherwise the screen jumps back at the
4742 * end of the file. */
4743 check_cursor_moved(curwin);
4744 curwin->w_valid |= VALID_TOPLINE;
4745 }
4746 }
4747 if (curwin->w_cursor.lnum != prev_lnum)
4748 coladvance(curwin->w_curswant);
4749 redraw_later(VALID);
4750}
4751
4752/*
4753 * Commands that start with "z".
4754 */
4755 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004756nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758 long n;
4759 colnr_T col;
4760 int nchar = cap->nchar;
4761#ifdef FEAT_FOLDING
4762 long old_fdl = curwin->w_p_fdl;
4763 int old_fen = curwin->w_p_fen;
4764#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004765#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004766 int undo = FALSE;
4767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
4769 if (VIM_ISDIGIT(nchar))
4770 {
4771 /*
4772 * "z123{nchar}": edit the count before obtaining {nchar}
4773 */
4774 if (checkclearop(cap->oap))
4775 return;
4776 n = nchar - '0';
4777 for (;;)
4778 {
4779#ifdef USE_ON_FLY_SCROLL
4780 dont_scroll = TRUE; /* disallow scrolling here */
4781#endif
4782 ++no_mapping;
4783 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004784 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 --no_mapping;
4787 --allow_keys;
4788#ifdef FEAT_CMDL_INFO
4789 (void)add_to_showcmd(nchar);
4790#endif
4791 if (nchar == K_DEL || nchar == K_KDEL)
4792 n /= 10;
4793 else if (VIM_ISDIGIT(nchar))
4794 n = n * 10 + (nchar - '0');
4795 else if (nchar == CAR)
4796 {
4797#ifdef FEAT_GUI
4798 need_mouse_correct = TRUE;
4799#endif
4800 win_setheight((int)n);
4801 break;
4802 }
4803 else if (nchar == 'l'
4804 || nchar == 'h'
4805 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004806 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
4808 cap->count1 = n ? n * cap->count1 : cap->count1;
4809 goto dozet;
4810 }
4811 else
4812 {
4813 clearopbeep(cap->oap);
4814 break;
4815 }
4816 }
4817 cap->oap->op_type = OP_NOP;
4818 return;
4819 }
4820
4821dozet:
4822 if (
4823#ifdef FEAT_FOLDING
4824 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4825 * and "zC" only in Visual mode. "zj" and "zk" are motion
4826 * commands. */
4827 cap->nchar != 'f' && cap->nchar != 'F'
4828 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4829 && cap->nchar != 'j' && cap->nchar != 'k'
4830 &&
4831#endif
4832 checkclearop(cap->oap))
4833 return;
4834
4835 /*
4836 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4837 * If line number given, set cursor.
4838 */
4839 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4840 && cap->count0
4841 && cap->count0 != curwin->w_cursor.lnum)
4842 {
4843 setpcmark();
4844 if (cap->count0 > curbuf->b_ml.ml_line_count)
4845 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4846 else
4847 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004848 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850
4851 switch (nchar)
4852 {
4853 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4854 case '+':
4855 if (cap->count0 == 0)
4856 {
4857 /* No count given: put cursor at the line below screen */
4858 validate_botline(); /* make sure w_botline is valid */
4859 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4860 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4861 else
4862 curwin->w_cursor.lnum = curwin->w_botline;
4863 }
4864 /* FALLTHROUGH */
4865 case NL:
4866 case CAR:
4867 case K_KENTER:
4868 beginline(BL_WHITE | BL_FIX);
4869 /* FALLTHROUGH */
4870
4871 case 't': scroll_cursor_top(0, TRUE);
4872 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004873 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 break;
4875
4876 /* "z." and "zz": put cursor in middle of screen */
4877 case '.': beginline(BL_WHITE | BL_FIX);
4878 /* FALLTHROUGH */
4879
4880 case 'z': scroll_cursor_halfway(TRUE);
4881 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004882 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 break;
4884
4885 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4886 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4887 * when <count> is at bottom of window, and puts that one at
4888 * bottom of window. */
4889 if (cap->count0 != 0)
4890 {
4891 scroll_cursor_bot(0, TRUE);
4892 curwin->w_cursor.lnum = curwin->w_topline;
4893 }
4894 else if (curwin->w_topline == 1)
4895 curwin->w_cursor.lnum = 1;
4896 else
4897 curwin->w_cursor.lnum = curwin->w_topline - 1;
4898 /* FALLTHROUGH */
4899 case '-':
4900 beginline(BL_WHITE | BL_FIX);
4901 /* FALLTHROUGH */
4902
4903 case 'b': scroll_cursor_bot(0, TRUE);
4904 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004905 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 break;
4907
4908 /* "zH" - scroll screen right half-page */
4909 case 'H':
4910 cap->count1 *= W_WIDTH(curwin) / 2;
4911 /* FALLTHROUGH */
4912
4913 /* "zh" - scroll screen to the right */
4914 case 'h':
4915 case K_LEFT:
4916 if (!curwin->w_p_wrap)
4917 {
4918 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4919 curwin->w_leftcol = 0;
4920 else
4921 curwin->w_leftcol -= (colnr_T)cap->count1;
4922 leftcol_changed();
4923 }
4924 break;
4925
4926 /* "zL" - scroll screen left half-page */
4927 case 'L': cap->count1 *= W_WIDTH(curwin) / 2;
4928 /* FALLTHROUGH */
4929
4930 /* "zl" - scroll screen to the left */
4931 case 'l':
4932 case K_RIGHT:
4933 if (!curwin->w_p_wrap)
4934 {
4935 /* scroll the window left */
4936 curwin->w_leftcol += (colnr_T)cap->count1;
4937 leftcol_changed();
4938 }
4939 break;
4940
4941 /* "zs" - scroll screen, cursor at the start */
4942 case 's': if (!curwin->w_p_wrap)
4943 {
4944#ifdef FEAT_FOLDING
4945 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4946 col = 0; /* like the cursor is in col 0 */
4947 else
4948#endif
4949 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4950 if ((long)col > p_siso)
4951 col -= p_siso;
4952 else
4953 col = 0;
4954 if (curwin->w_leftcol != col)
4955 {
4956 curwin->w_leftcol = col;
4957 redraw_later(NOT_VALID);
4958 }
4959 }
4960 break;
4961
4962 /* "ze" - scroll screen, cursor at the end */
4963 case 'e': if (!curwin->w_p_wrap)
4964 {
4965#ifdef FEAT_FOLDING
4966 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4967 col = 0; /* like the cursor is in col 0 */
4968 else
4969#endif
4970 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
4971 n = W_WIDTH(curwin) - curwin_col_off();
4972 if ((long)col + p_siso < n)
4973 col = 0;
4974 else
4975 col = col + p_siso - n + 1;
4976 if (curwin->w_leftcol != col)
4977 {
4978 curwin->w_leftcol = col;
4979 redraw_later(NOT_VALID);
4980 }
4981 }
4982 break;
4983
4984#ifdef FEAT_FOLDING
4985 /* "zF": create fold command */
4986 /* "zf": create fold operator */
4987 case 'F':
4988 case 'f': if (foldManualAllowed(TRUE))
4989 {
4990 cap->nchar = 'f';
4991 nv_operator(cap);
4992 curwin->w_p_fen = TRUE;
4993
4994 /* "zF" is like "zfzf" */
4995 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4996 {
4997 nv_operator(cap);
4998 finish_op = TRUE;
4999 }
5000 }
5001 else
5002 clearopbeep(cap->oap);
5003 break;
5004
5005 /* "zd": delete fold at cursor */
5006 /* "zD": delete fold at cursor recursively */
5007 case 'd':
5008 case 'D': if (foldManualAllowed(FALSE))
5009 {
5010 if (VIsual_active)
5011 nv_operator(cap);
5012 else
5013 deleteFold(curwin->w_cursor.lnum,
5014 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5015 }
5016 break;
5017
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005018 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 case 'E': if (foldmethodIsManual(curwin))
5020 {
5021 clearFolding(curwin);
5022 changed_window_setting();
5023 }
5024 else if (foldmethodIsMarker(curwin))
5025 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5026 TRUE, FALSE);
5027 else
5028 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5029 break;
5030
5031 /* "zn": fold none: reset 'foldenable' */
5032 case 'n': curwin->w_p_fen = FALSE;
5033 break;
5034
5035 /* "zN": fold Normal: set 'foldenable' */
5036 case 'N': curwin->w_p_fen = TRUE;
5037 break;
5038
5039 /* "zi": invert folding: toggle 'foldenable' */
5040 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5041 break;
5042
5043 /* "za": open closed fold or close open fold at cursor */
5044 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5045 openFold(curwin->w_cursor.lnum, cap->count1);
5046 else
5047 {
5048 closeFold(curwin->w_cursor.lnum, cap->count1);
5049 curwin->w_p_fen = TRUE;
5050 }
5051 break;
5052
5053 /* "zA": open fold at cursor recursively */
5054 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5055 openFoldRecurse(curwin->w_cursor.lnum);
5056 else
5057 {
5058 closeFoldRecurse(curwin->w_cursor.lnum);
5059 curwin->w_p_fen = TRUE;
5060 }
5061 break;
5062
5063 /* "zo": open fold at cursor or Visual area */
5064 case 'o': if (VIsual_active)
5065 nv_operator(cap);
5066 else
5067 openFold(curwin->w_cursor.lnum, cap->count1);
5068 break;
5069
5070 /* "zO": open fold recursively */
5071 case 'O': if (VIsual_active)
5072 nv_operator(cap);
5073 else
5074 openFoldRecurse(curwin->w_cursor.lnum);
5075 break;
5076
5077 /* "zc": close fold at cursor or Visual area */
5078 case 'c': if (VIsual_active)
5079 nv_operator(cap);
5080 else
5081 closeFold(curwin->w_cursor.lnum, cap->count1);
5082 curwin->w_p_fen = TRUE;
5083 break;
5084
5085 /* "zC": close fold recursively */
5086 case 'C': if (VIsual_active)
5087 nv_operator(cap);
5088 else
5089 closeFoldRecurse(curwin->w_cursor.lnum);
5090 curwin->w_p_fen = TRUE;
5091 break;
5092
5093 /* "zv": open folds at the cursor */
5094 case 'v': foldOpenCursor();
5095 break;
5096
5097 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5098 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005099 curwin->w_foldinvalid = TRUE; /* recompute folds */
5100 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 foldOpenCursor();
5102 break;
5103
5104 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5105 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005106 curwin->w_foldinvalid = TRUE; /* recompute folds */
5107 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 break;
5109
5110 /* "zm": fold more */
5111 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005112 {
5113 curwin->w_p_fdl -= cap->count1;
5114 if (curwin->w_p_fdl < 0)
5115 curwin->w_p_fdl = 0;
5116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 old_fdl = -1; /* force an update */
5118 curwin->w_p_fen = TRUE;
5119 break;
5120
5121 /* "zM": close all folds */
5122 case 'M': curwin->w_p_fdl = 0;
5123 old_fdl = -1; /* force an update */
5124 curwin->w_p_fen = TRUE;
5125 break;
5126
5127 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005128 case 'r': curwin->w_p_fdl += cap->count1;
5129 {
5130 int d = getDeepestNesting();
5131
5132 if (curwin->w_p_fdl >= d)
5133 curwin->w_p_fdl = d;
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136
5137 /* "zR": open all folds */
5138 case 'R': curwin->w_p_fdl = getDeepestNesting();
5139 old_fdl = -1; /* force an update */
5140 break;
5141
5142 case 'j': /* "zj" move to next fold downwards */
5143 case 'k': /* "zk" move to next fold upwards */
5144 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5145 cap->count1) == FAIL)
5146 clearopbeep(cap->oap);
5147 break;
5148
5149#endif /* FEAT_FOLDING */
5150
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005151#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005152 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5153 ++no_mapping;
5154 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005155 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005156 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005157 --no_mapping;
5158 --allow_keys;
5159#ifdef FEAT_CMDL_INFO
5160 (void)add_to_showcmd(nchar);
5161#endif
5162 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5163 {
5164 clearopbeep(cap->oap);
5165 break;
5166 }
5167 undo = TRUE;
5168 /*FALLTHROUGH*/
5169
Bram Moolenaarb765d632005-06-07 21:00:02 +00005170 case 'g': /* "zg": add good word to word list */
5171 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005172 case 'G': /* "zG": add good word to temp word list */
5173 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005174 {
5175 char_u *ptr = NULL;
5176 int len;
5177
5178 if (checkclearop(cap->oap))
5179 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005180 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5181 == FAIL)
5182 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005183 if (ptr == NULL)
5184 {
5185 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005186
Bram Moolenaar134bf072013-09-25 18:54:24 +02005187 /* Find bad word under the cursor. When 'spell' is
5188 * off this fails and find_ident_under_cursor() is
5189 * used below. */
5190 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005191 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005192 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005193 if (len != 0 && curwin->w_cursor.col <= pos.col)
5194 ptr = ml_get_pos(&curwin->w_cursor);
5195 curwin->w_cursor = pos;
5196 }
5197
Bram Moolenaarb765d632005-06-07 21:00:02 +00005198 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5199 FIND_IDENT)) == 0)
5200 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005201 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005202 (nchar == 'G' || nchar == 'W')
5203 ? 0 : (int)cap->count1,
5204 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005205 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005206 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207
Bram Moolenaar43abc522005-12-10 20:15:02 +00005208 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005209 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005210 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005212#endif
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 default: clearopbeep(cap->oap);
5215 }
5216
5217#ifdef FEAT_FOLDING
5218 /* Redraw when 'foldenable' changed */
5219 if (old_fen != curwin->w_p_fen)
5220 {
5221# ifdef FEAT_DIFF
5222 win_T *wp;
5223
5224 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5225 {
5226 /* Adjust 'foldenable' in diff-synced windows. */
5227 FOR_ALL_WINDOWS(wp)
5228 {
5229 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5230 {
5231 wp->w_p_fen = curwin->w_p_fen;
5232 changed_window_setting_win(wp);
5233 }
5234 }
5235 }
5236# endif
5237 changed_window_setting();
5238 }
5239
5240 /* Redraw when 'foldlevel' changed. */
5241 if (old_fdl != curwin->w_p_fdl)
5242 newFoldLevel();
5243#endif
5244}
5245
5246#ifdef FEAT_GUI
5247/*
5248 * Vertical scrollbar movement.
5249 */
5250 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005251nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 if (cap->oap->op_type != OP_NOP)
5254 clearopbeep(cap->oap);
5255
5256 /* Even if an operator was pending, we still want to scroll */
5257 gui_do_scroll();
5258}
5259
5260/*
5261 * Horizontal scrollbar movement.
5262 */
5263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005264nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
5266 if (cap->oap->op_type != OP_NOP)
5267 clearopbeep(cap->oap);
5268
5269 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005270 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271}
5272#endif
5273
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005274#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005275/*
5276 * Click in GUI tab.
5277 */
5278 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005279nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005280{
5281 if (cap->oap->op_type != OP_NOP)
5282 clearopbeep(cap->oap);
5283
5284 /* Even if an operator was pending, we still want to jump tabs. */
5285 goto_tabpage(current_tab);
5286}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005287
5288/*
5289 * Selected item in tab line menu.
5290 */
5291 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005293{
5294 if (cap->oap->op_type != OP_NOP)
5295 clearopbeep(cap->oap);
5296
5297 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005298 handle_tabmenu();
5299}
5300
5301/*
5302 * Handle selecting an item of the GUI tab line menu.
5303 * Used in Normal and Insert mode.
5304 */
5305 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005306handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005307{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005308 switch (current_tabmenu)
5309 {
5310 case TABLINE_MENU_CLOSE:
5311 if (current_tab == 0)
5312 do_cmdline_cmd((char_u *)"tabclose");
5313 else
5314 {
5315 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5316 current_tab);
5317 do_cmdline_cmd(IObuff);
5318 }
5319 break;
5320
5321 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005322 if (current_tab == 0)
5323 do_cmdline_cmd((char_u *)"$tabnew");
5324 else
5325 {
5326 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5327 current_tab - 1);
5328 do_cmdline_cmd(IObuff);
5329 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005330 break;
5331
5332 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005333 if (current_tab == 0)
5334 do_cmdline_cmd((char_u *)"browse $tabnew");
5335 else
5336 {
5337 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5338 current_tab - 1);
5339 do_cmdline_cmd(IObuff);
5340 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005341 break;
5342 }
5343}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005344#endif
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346/*
5347 * "Q" command.
5348 */
5349 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005350nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351{
5352 /*
5353 * Ignore 'Q' in Visual mode, just give a beep.
5354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005356 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005357 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 do_exmode(FALSE);
5359}
5360
5361/*
5362 * Handle a ":" command.
5363 */
5364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005365nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005368 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (VIsual_active)
5371 nv_operator(cap);
5372 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 if (cap->oap->op_type != OP_NOP)
5375 {
5376 /* Using ":" as a movement is characterwise exclusive. */
5377 cap->oap->motion_type = MCHAR;
5378 cap->oap->inclusive = FALSE;
5379 }
5380 else if (cap->count0)
5381 {
5382 /* translate "count:" into ":.,.+(count - 1)" */
5383 stuffcharReadbuff('.');
5384 if (cap->count0 > 1)
5385 {
5386 stuffReadbuff((char_u *)",.+");
5387 stuffnumReadbuff((long)cap->count0 - 1L);
5388 }
5389 }
5390
5391 /* When typing, don't type below an old message */
5392 if (KeyTyped)
5393 compute_cmdrow();
5394
5395 old_p_im = p_im;
5396
5397 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005398 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5400
5401 /* If 'insertmode' changed, enter or exit Insert mode */
5402 if (p_im != old_p_im)
5403 {
5404 if (p_im)
5405 restart_edit = 'i';
5406 else
5407 restart_edit = 0;
5408 }
5409
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005410 if (cmd_result == FAIL)
5411 /* The Ex command failed, do not execute the operator. */
5412 clearop(cap->oap);
5413 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5415 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005416 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5417 || did_emsg
5418 ))
5419 /* The start of the operator has become invalid by the Ex command.
5420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 clearopbeep(cap->oap);
5422 }
5423}
5424
5425/*
5426 * Handle CTRL-G command.
5427 */
5428 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005429nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 if (VIsual_active) /* toggle Selection/Visual mode */
5432 {
5433 VIsual_select = !VIsual_select;
5434 showmode();
5435 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005436 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 /* print full name if count given or :cd used */
5438 fileinfo((int)cap->count0, FALSE, TRUE);
5439}
5440
5441/*
5442 * Handle CTRL-H <Backspace> command.
5443 */
5444 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005445nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 if (VIsual_active && VIsual_select)
5448 {
5449 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5450 v_visop(cap);
5451 }
5452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 nv_left(cap);
5454}
5455
5456/*
5457 * CTRL-L: clear screen and redraw.
5458 */
5459 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005460nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
5462 if (!checkclearop(cap->oap))
5463 {
5464#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5465 /*
5466 * Right now, the BeBox doesn't seem to have an easy way to detect
5467 * window resizing, so we cheat and make the user detect it
5468 * manually with CTRL-L instead
5469 */
5470 ui_get_shellsize();
5471#endif
5472#ifdef FEAT_SYN_HL
5473 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005474 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005475# ifdef FEAT_RELTIME
5476 {
5477 win_T *wp;
5478
5479 FOR_ALL_WINDOWS(wp)
5480 wp->w_s->b_syn_slow = FALSE;
5481 }
5482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483#endif
5484 redraw_later(CLEAR);
5485 }
5486}
5487
5488/*
5489 * CTRL-O: In Select mode: switch to Visual mode for one command.
5490 * Otherwise: Go to older pcmark.
5491 */
5492 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005493nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 if (VIsual_active && VIsual_select)
5496 {
5497 VIsual_select = FALSE;
5498 showmode();
5499 restart_VIsual_select = 2; /* restart Select mode later */
5500 }
5501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 cap->count1 = -cap->count1;
5504 nv_pcmark(cap);
5505 }
5506}
5507
5508/*
5509 * CTRL-^ command, short for ":e #"
5510 */
5511 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005512nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 if (!checkclearopq(cap->oap))
5515 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5516 GETF_SETMARK|GETF_ALT, FALSE);
5517}
5518
5519/*
5520 * "Z" commands.
5521 */
5522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005523nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 if (!checkclearopq(cap->oap))
5526 {
5527 switch (cap->nchar)
5528 {
5529 /* "ZZ": equivalent to ":x". */
5530 case 'Z': do_cmdline_cmd((char_u *)"x");
5531 break;
5532
5533 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5534 case 'Q': do_cmdline_cmd((char_u *)"q!");
5535 break;
5536
5537 default: clearopbeep(cap->oap);
5538 }
5539 }
5540}
5541
5542#if defined(FEAT_WINDOWS) || defined(PROTO)
5543/*
5544 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5545 */
5546 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005547do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
5549 oparg_T oa;
5550 cmdarg_T ca;
5551
5552 clear_oparg(&oa);
5553 vim_memset(&ca, 0, sizeof(ca));
5554 ca.oap = &oa;
5555 ca.cmdchar = c1;
5556 ca.nchar = c2;
5557 nv_ident(&ca);
5558}
5559#endif
5560
5561/*
5562 * Handle the commands that use the word under the cursor.
5563 * [g] CTRL-] :ta to current identifier
5564 * [g] 'K' run program for current identifier
5565 * [g] '*' / to current identifier or string
5566 * [g] '#' ? to current identifier or string
5567 * g ']' :tselect for current identifier
5568 */
5569 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005570nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571{
5572 char_u *ptr = NULL;
5573 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005574 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005575 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 char_u *p;
5577 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005578 int kp_help; /* 'keywordprg' is ":he" */
5579 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 int n = 0; /* init for GCC */
5581 int cmdchar;
5582 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005583 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 char_u *aux_ptr;
5585 int isman;
5586 int isman_s;
5587
5588 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5589 {
5590 cmdchar = cap->nchar;
5591 g_cmd = TRUE;
5592 }
5593 else
5594 {
5595 cmdchar = cap->cmdchar;
5596 g_cmd = FALSE;
5597 }
5598
5599 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5600 cmdchar = '#';
5601
5602 /*
5603 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5604 */
5605 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5608 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if (checkclearopq(cap->oap))
5610 return;
5611 }
5612
5613 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5614 (cmdchar == '*' || cmdchar == '#')
5615 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5616 {
5617 clearop(cap->oap);
5618 return;
5619 }
5620
5621 /* Allocate buffer to put the command in. Inserting backslashes can
5622 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5623 * and some numbers. */
5624 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5625 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5626 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005627 if (kp_help && *skipwhite(ptr) == NUL)
5628 {
5629 EMSG(_(e_noident)); /* found white space only */
5630 return;
5631 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005632 kp_ex = (*kp == ':');
5633 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5634 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 if (buf == NULL)
5636 return;
5637 buf[0] = NUL;
5638
5639 switch (cmdchar)
5640 {
5641 case '*':
5642 case '#':
5643 /*
5644 * Put cursor at start of word, makes search skip the word
5645 * under the cursor.
5646 * Call setpcmark() first, so "*``" puts the cursor back where
5647 * it was.
5648 */
5649 setpcmark();
5650 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5651
5652 if (!g_cmd && vim_iswordp(ptr))
5653 STRCPY(buf, "\\<");
5654 no_smartcase = TRUE; /* don't use 'smartcase' now */
5655 break;
5656
5657 case 'K':
5658 if (kp_help)
5659 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005660 else if (kp_ex)
5661 {
5662 if (cap->count0 != 0)
5663 vim_snprintf((char *)buf, buflen, "%s %ld",
5664 kp, cap->count0);
5665 else
5666 STRCPY(buf, kp);
5667 STRCAT(buf, " ");
5668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 else
5670 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005671 /* An external command will probably use an argument starting
5672 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005673 while (*ptr == '-' && n > 0)
5674 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005675 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005676 --n;
5677 }
5678 if (n == 0)
5679 {
5680 EMSG(_(e_noident)); /* found dashes only */
5681 vim_free(buf);
5682 return;
5683 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005684
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 /* When a count is given, turn it into a range. Is this
5686 * really what we want? */
5687 isman = (STRCMP(kp, "man") == 0);
5688 isman_s = (STRCMP(kp, "man -s") == 0);
5689 if (cap->count0 != 0 && !(isman || isman_s))
5690 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5691
5692 STRCAT(buf, "! ");
5693 if (cap->count0 == 0 && isman_s)
5694 STRCAT(buf, "man");
5695 else
5696 STRCAT(buf, kp);
5697 STRCAT(buf, " ");
5698 if (cap->count0 != 0 && (isman || isman_s))
5699 {
5700 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5701 STRCAT(buf, " ");
5702 }
5703 }
5704 break;
5705
5706 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005707 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708#ifdef FEAT_CSCOPE
5709 if (p_cst)
5710 STRCPY(buf, "cstag ");
5711 else
5712#endif
5713 STRCPY(buf, "ts ");
5714 break;
5715
5716 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005717 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 if (curbuf->b_help)
5719 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005721 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005722 if (g_cmd)
5723 STRCPY(buf, "tj ");
5724 else
5725 sprintf((char *)buf, "%ldta ", cap->count0);
5726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
5728
5729 /*
5730 * Now grab the chars in the identifier
5731 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005732 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005734 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005735 if (kp_ex)
5736 /* Escape the argument properly for an Ex command */
5737 p = vim_strsave_fnameescape(ptr, FALSE);
5738 else
5739 /* Escape the argument properly for a shell command */
5740 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005741 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005742 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005744 vim_free(buf);
5745 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005747 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5748 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005749 {
5750 vim_free(buf);
5751 vim_free(p);
5752 return;
5753 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005754 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005755 STRCAT(buf, p);
5756 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005758 else
5759 {
5760 if (cmdchar == '*')
5761 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5762 else if (cmdchar == '#')
5763 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005764 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005765 {
5766 if (curbuf->b_help)
5767 /* ":help" handles unescaped argument */
5768 aux_ptr = (char_u *)"";
5769 else
5770 aux_ptr = (char_u *)"\\|\"\n[";
5771 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005772 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005773 aux_ptr = (char_u *)"\\|\"\n*?[";
5774
5775 p = buf + STRLEN(buf);
5776 while (n-- > 0)
5777 {
5778 /* put a backslash before \ and some others */
5779 if (vim_strchr(aux_ptr, *ptr) != NULL)
5780 *p++ = '\\';
5781#ifdef FEAT_MBYTE
5782 /* When current byte is a part of multibyte character, copy all
5783 * bytes of that character. */
5784 if (has_mbyte)
5785 {
5786 int i;
5787 int len = (*mb_ptr2len)(ptr) - 1;
5788
5789 for (i = 0; i < len && n >= 1; ++i, --n)
5790 *p++ = *ptr++;
5791 }
5792#endif
5793 *p++ = *ptr++;
5794 }
5795 *p = NUL;
5796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
5798 /*
5799 * Execute the command.
5800 */
5801 if (cmdchar == '*' || cmdchar == '#')
5802 {
5803 if (!g_cmd && (
5804#ifdef FEAT_MBYTE
5805 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5806#endif
5807 vim_iswordc(ptr[-1])))
5808 STRCAT(buf, "\\>");
5809#ifdef FEAT_CMDHIST
5810 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005811 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5813#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005814 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
5816 else
5817 do_cmdline_cmd(buf);
5818
5819 vim_free(buf);
5820}
5821
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822/*
5823 * Get visually selected text, within one line only.
5824 * Returns FAIL if more than one line selected.
5825 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005826 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005827get_visual_text(
5828 cmdarg_T *cap,
5829 char_u **pp, /* return: start of selected text */
5830 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 if (VIsual_mode != 'V')
5833 unadjust_for_sel();
5834 if (VIsual.lnum != curwin->w_cursor.lnum)
5835 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005836 if (cap != NULL)
5837 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 return FAIL;
5839 }
5840 if (VIsual_mode == 'V')
5841 {
5842 *pp = ml_get_curline();
5843 *lenp = (int)STRLEN(*pp);
5844 }
5845 else
5846 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005847 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
5849 *pp = ml_get_pos(&curwin->w_cursor);
5850 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5851 }
5852 else
5853 {
5854 *pp = ml_get_pos(&VIsual);
5855 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5856 }
5857#ifdef FEAT_MBYTE
5858 if (has_mbyte)
5859 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005860 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861#endif
5862 }
5863 reset_VIsual_and_resel();
5864 return OK;
5865}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867/*
5868 * CTRL-T: backwards in tag stack
5869 */
5870 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005871nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 if (!checkclearopq(cap->oap))
5874 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5875}
5876
5877/*
5878 * Handle scrolling command 'H', 'L' and 'M'.
5879 */
5880 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005881nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
5883 int used = 0;
5884 long n;
5885#ifdef FEAT_FOLDING
5886 linenr_T lnum;
5887#endif
5888 int half;
5889
5890 cap->oap->motion_type = MLINE;
5891 setpcmark();
5892
5893 if (cap->cmdchar == 'L')
5894 {
5895 validate_botline(); /* make sure curwin->w_botline is valid */
5896 curwin->w_cursor.lnum = curwin->w_botline - 1;
5897 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5898 curwin->w_cursor.lnum = 1;
5899 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005900 {
5901#ifdef FEAT_FOLDING
5902 if (hasAnyFolding(curwin))
5903 {
5904 /* Count a fold for one screen line. */
5905 for (n = cap->count1 - 1; n > 0
5906 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5907 {
5908 (void)hasFolding(curwin->w_cursor.lnum,
5909 &curwin->w_cursor.lnum, NULL);
5910 --curwin->w_cursor.lnum;
5911 }
5912 }
5913 else
5914#endif
5915 curwin->w_cursor.lnum -= cap->count1 - 1;
5916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 }
5918 else
5919 {
5920 if (cap->cmdchar == 'M')
5921 {
5922#ifdef FEAT_DIFF
5923 /* Don't count filler lines above the window. */
5924 used -= diff_check_fill(curwin, curwin->w_topline)
5925 - curwin->w_topfill;
5926#endif
5927 validate_botline(); /* make sure w_empty_rows is valid */
5928 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5929 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5930 {
5931#ifdef FEAT_DIFF
5932 /* Count half he number of filler lines to be "below this
5933 * line" and half to be "above the next line". */
5934 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5935 + n) / 2 >= half)
5936 {
5937 --n;
5938 break;
5939 }
5940#endif
5941 used += plines(curwin->w_topline + n);
5942 if (used >= half)
5943 break;
5944#ifdef FEAT_FOLDING
5945 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5946 n = lnum - curwin->w_topline;
5947#endif
5948 }
5949 if (n > 0 && used > curwin->w_height)
5950 --n;
5951 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005952 else /* (cap->cmdchar == 'H') */
5953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005955#ifdef FEAT_FOLDING
5956 if (hasAnyFolding(curwin))
5957 {
5958 /* Count a fold for one screen line. */
5959 lnum = curwin->w_topline;
5960 while (n-- > 0 && lnum < curwin->w_botline - 1)
5961 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005962 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005963 ++lnum;
5964 }
5965 n = lnum - curwin->w_topline;
5966 }
5967#endif
5968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 curwin->w_cursor.lnum = curwin->w_topline + n;
5970 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5971 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5972 }
5973
5974 cursor_correct(); /* correct for 'so' */
5975 beginline(BL_SOL | BL_FIX);
5976}
5977
5978/*
5979 * Cursor right commands.
5980 */
5981 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005982nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005985 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005987 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5988 {
5989 /* <C-Right> and <S-Right> move a word or WORD right */
5990 if (mod_mask & MOD_MASK_CTRL)
5991 cap->arg = TRUE;
5992 nv_wordcmd(cap);
5993 return;
5994 }
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 cap->oap->motion_type = MCHAR;
5997 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005998 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006000#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006002 * In virtual edit mode, there's no such thing as "past_line", as lines
6003 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 */
6005 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006006 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007#endif
6008
6009 for (n = cap->count1; n > 0; --n)
6010 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006011 if ((!past_line && oneright() == FAIL)
6012 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006013 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 {
6015 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006016 * <Space> wraps to next line if 'whichwrap' has 's'.
6017 * 'l' wraps to next line if 'whichwrap' has 'l'.
6018 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 */
6020 if ( ((cap->cmdchar == ' '
6021 && vim_strchr(p_ww, 's') != NULL)
6022 || (cap->cmdchar == 'l'
6023 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006024 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 && vim_strchr(p_ww, '>') != NULL))
6026 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6027 {
6028 /* When deleting we also count the NL as a character.
6029 * Set cap->oap->inclusive when last char in the line is
6030 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006031 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006033 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 cap->oap->inclusive = TRUE;
6035 else
6036 {
6037 ++curwin->w_cursor.lnum;
6038 curwin->w_cursor.col = 0;
6039#ifdef FEAT_VIRTUALEDIT
6040 curwin->w_cursor.coladd = 0;
6041#endif
6042 curwin->w_set_curswant = TRUE;
6043 cap->oap->inclusive = FALSE;
6044 }
6045 continue;
6046 }
6047 if (cap->oap->op_type == OP_NOP)
6048 {
6049 /* Only beep and flush if not moved at all */
6050 if (n == cap->count1)
6051 beep_flush();
6052 }
6053 else
6054 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006055 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 cap->oap->inclusive = TRUE;
6057 }
6058 break;
6059 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006060 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 {
6062 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006063#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 if (virtual_active())
6065 oneright();
6066 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006069#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (has_mbyte)
6071 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006072 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 ++curwin->w_cursor.col;
6076 }
6077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 }
6079#ifdef FEAT_FOLDING
6080 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6081 && cap->oap->op_type == OP_NOP)
6082 foldOpenCursor();
6083#endif
6084}
6085
6086/*
6087 * Cursor left commands.
6088 *
6089 * Returns TRUE when operator end should not be adjusted.
6090 */
6091 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006092nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
6094 long n;
6095
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006096 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6097 {
6098 /* <C-Left> and <S-Left> move a word or WORD left */
6099 if (mod_mask & MOD_MASK_CTRL)
6100 cap->arg = 1;
6101 nv_bck_word(cap);
6102 return;
6103 }
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 cap->oap->motion_type = MCHAR;
6106 cap->oap->inclusive = FALSE;
6107 for (n = cap->count1; n > 0; --n)
6108 {
6109 if (oneleft() == FAIL)
6110 {
6111 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6112 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6113 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6114 */
6115 if ( (((cap->cmdchar == K_BS
6116 || cap->cmdchar == Ctrl_H)
6117 && vim_strchr(p_ww, 'b') != NULL)
6118 || (cap->cmdchar == 'h'
6119 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006120 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 && vim_strchr(p_ww, '<') != NULL))
6122 && curwin->w_cursor.lnum > 1)
6123 {
6124 --(curwin->w_cursor.lnum);
6125 coladvance((colnr_T)MAXCOL);
6126 curwin->w_set_curswant = TRUE;
6127
6128 /* When the NL before the first char has to be deleted we
6129 * put the cursor on the NUL after the previous line.
6130 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006131 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if ( (cap->oap->op_type == OP_DELETE
6133 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006134 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006136 char_u *cp = ml_get_cursor();
6137
6138 if (*cp != NUL)
6139 {
6140#ifdef FEAT_MBYTE
6141 if (has_mbyte)
6142 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6143 else
6144#endif
6145 ++curwin->w_cursor.col;
6146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 cap->retval |= CA_NO_ADJ_OP_END;
6148 }
6149 continue;
6150 }
6151 /* Only beep and flush if not moved at all */
6152 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6153 beep_flush();
6154 break;
6155 }
6156 }
6157#ifdef FEAT_FOLDING
6158 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6159 && cap->oap->op_type == OP_NOP)
6160 foldOpenCursor();
6161#endif
6162}
6163
6164/*
6165 * Cursor up commands.
6166 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6167 */
6168 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006169nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006171 if (mod_mask & MOD_MASK_SHIFT)
6172 {
6173 /* <S-Up> is page up */
6174 cap->arg = BACKWARD;
6175 nv_page(cap);
6176 }
6177 else
6178 {
6179 cap->oap->motion_type = MLINE;
6180 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6181 clearopbeep(cap->oap);
6182 else if (cap->arg)
6183 beginline(BL_WHITE | BL_FIX);
6184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185}
6186
6187/*
6188 * Cursor down commands.
6189 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6190 */
6191 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006192nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006194 if (mod_mask & MOD_MASK_SHIFT)
6195 {
6196 /* <S-Down> is page down */
6197 cap->arg = FORWARD;
6198 nv_page(cap);
6199 }
6200 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6202 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006203 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar74a47162017-02-26 19:09:05 +01006204 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006205 if (curwin->w_llist_ref == NULL)
6206 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6207 else
6208 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar74a47162017-02-26 19:09:05 +01006209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 else
6211#endif
6212 {
6213#ifdef FEAT_CMDWIN
6214 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006215 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 cmdwin_result = CAR;
6217 else
6218#endif
6219 {
6220 cap->oap->motion_type = MLINE;
6221 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6222 clearopbeep(cap->oap);
6223 else if (cap->arg)
6224 beginline(BL_WHITE | BL_FIX);
6225 }
6226 }
6227}
6228
6229#ifdef FEAT_SEARCHPATH
6230/*
6231 * Grab the file name under the cursor and edit it.
6232 */
6233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006234nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006237 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006239 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 {
6241 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006242 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 return;
6244 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006245#ifdef FEAT_AUTOCMD
6246 if (curbuf_locked())
6247 {
6248 clearop(cap->oap);
6249 return;
6250 }
6251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006253 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
6255 if (ptr != NULL)
6256 {
6257 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006258 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006259 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006261 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006262 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006263 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006264 {
6265 curwin->w_cursor.lnum = lnum;
6266 check_cursor_lnum();
6267 beginline(BL_SOL | BL_FIX);
6268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 vim_free(ptr);
6270 }
6271 else
6272 clearop(cap->oap);
6273}
6274#endif
6275
6276/*
6277 * <End> command: to end of current line or last line.
6278 */
6279 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006280nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006282 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006284 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 nv_goto(cap);
6286 cap->count1 = 1; /* to end of current line */
6287 }
6288 nv_dollar(cap);
6289}
6290
6291/*
6292 * Handle the "$" command.
6293 */
6294 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006295nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296{
6297 cap->oap->motion_type = MCHAR;
6298 cap->oap->inclusive = TRUE;
6299#ifdef FEAT_VIRTUALEDIT
6300 /* In virtual mode when off the edge of a line and an operator
6301 * is pending (whew!) keep the cursor where it is.
6302 * Otherwise, send it to the end of the line. */
6303 if (!virtual_active() || gchar_cursor() != NUL
6304 || cap->oap->op_type == OP_NOP)
6305#endif
6306 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6307 if (cursor_down((long)(cap->count1 - 1),
6308 cap->oap->op_type == OP_NOP) == FAIL)
6309 clearopbeep(cap->oap);
6310#ifdef FEAT_FOLDING
6311 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6312 foldOpenCursor();
6313#endif
6314}
6315
6316/*
6317 * Implementation of '?' and '/' commands.
6318 * If cap->arg is TRUE don't set PC mark.
6319 */
6320 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006321nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006324 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
6326 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6327 {
6328 /* Translate "g??" to "g?g?" */
6329 cap->cmdchar = 'g';
6330 cap->nchar = '?';
6331 nv_operator(cap);
6332 return;
6333 }
6334
Bram Moolenaardda933d2016-09-03 21:04:58 +02006335 /* When using 'incsearch' the cursor may be moved to set a different search
6336 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6338
6339 if (cap->searchbuf == NULL)
6340 {
6341 clearop(oap);
6342 return;
6343 }
6344
Bram Moolenaar46539112015-02-17 15:43:57 +01006345 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006346 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006347 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
6350/*
6351 * Handle "N" and "n" commands.
6352 * cap->arg is SEARCH_REV for "N", 0 for "n".
6353 */
6354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006355nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
Bram Moolenaar46539112015-02-17 15:43:57 +01006357 pos_T old = curwin->w_cursor;
6358 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6359
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006360 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006361 {
6362 /* Avoid getting stuck on the current cursor position, which can
6363 * happen when an offset is given and the cursor is on the last char
6364 * in the buffer: Repeat with count + 1. */
6365 cap->count1 += 1;
6366 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6367 cap->count1 -= 1;
6368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369}
6370
6371/*
6372 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6373 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006374 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006376 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006377normal_search(
6378 cmdarg_T *cap,
6379 int dir,
6380 char_u *pat,
6381 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 int i;
6384
6385 cap->oap->motion_type = MCHAR;
6386 cap->oap->inclusive = FALSE;
6387 cap->oap->use_reg_one = TRUE;
6388 curwin->w_set_curswant = TRUE;
6389
6390 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006391 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 if (i == 0)
6393 clearop(cap->oap);
6394 else
6395 {
6396 if (i == 2)
6397 cap->oap->motion_type = MLINE;
6398#ifdef FEAT_VIRTUALEDIT
6399 curwin->w_cursor.coladd = 0;
6400#endif
6401#ifdef FEAT_FOLDING
6402 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6403 foldOpenCursor();
6404#endif
6405 }
6406
6407 /* "/$" will put the cursor after the end of the line, may need to
6408 * correct that here */
6409 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006410 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411}
6412
6413/*
6414 * Character search commands.
6415 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6416 * ',' and FALSE for ';'.
6417 * cap->nchar is NUL for ',' and ';' (repeat the search)
6418 */
6419 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006420nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421{
6422 int t_cmd;
6423
6424 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6425 t_cmd = TRUE;
6426 else
6427 t_cmd = FALSE;
6428
6429 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6431 clearopbeep(cap->oap);
6432 else
6433 {
6434 curwin->w_set_curswant = TRUE;
6435#ifdef FEAT_VIRTUALEDIT
6436 /* Include a Tab for "tx" and for "dfx". */
6437 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6438 && (t_cmd || cap->oap->op_type != OP_NOP))
6439 {
6440 colnr_T scol, ecol;
6441
6442 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6443 curwin->w_cursor.coladd = ecol - scol;
6444 }
6445 else
6446 curwin->w_cursor.coladd = 0;
6447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449#ifdef FEAT_FOLDING
6450 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6451 foldOpenCursor();
6452#endif
6453 }
6454}
6455
6456/*
6457 * "[" and "]" commands.
6458 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6459 */
6460 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006461nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006463 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 pos_T prev_pos;
6465 pos_T *pos = NULL; /* init for GCC */
6466 pos_T old_pos; /* cursor position before command */
6467 int flag;
6468 long n;
6469 int findc;
6470 int c;
6471
6472 cap->oap->motion_type = MCHAR;
6473 cap->oap->inclusive = FALSE;
6474 old_pos = curwin->w_cursor;
6475#ifdef FEAT_VIRTUALEDIT
6476 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6477#endif
6478
6479#ifdef FEAT_SEARCHPATH
6480 /*
6481 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6482 */
6483 if (cap->nchar == 'f')
6484 nv_gotofile(cap);
6485 else
6486#endif
6487
6488#ifdef FEAT_FIND_ID
6489 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006490 * Find the occurrence(s) of the identifier or define under cursor
6491 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 *
6493 * search list jump
6494 * fwd bwd fwd bwd fwd bwd
6495 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6496 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6497 */
6498 if (vim_strchr((char_u *)
6499#ifdef EBCDIC
6500 "iI\005dD\067",
6501#else
6502 "iI\011dD\004",
6503#endif
6504 cap->nchar) != NULL)
6505 {
6506 char_u *ptr;
6507 int len;
6508
6509 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6510 clearop(cap->oap);
6511 else
6512 {
6513 find_pattern_in_path(ptr, 0, len, TRUE,
6514 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6515 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6516 cap->count1,
6517 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6518 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6519 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6520 (linenr_T)MAXLNUM);
6521 curwin->w_set_curswant = TRUE;
6522 }
6523 }
6524 else
6525#endif
6526
6527 /*
6528 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6529 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6530 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6531 * "[m" or "]m" search for prev/next start of (Java) method.
6532 * "[M" or "]M" search for prev/next end of (Java) method.
6533 */
6534 if ( (cap->cmdchar == '['
6535 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6536 || (cap->cmdchar == ']'
6537 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6538 {
6539 if (cap->nchar == '*')
6540 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 prev_pos.lnum = 0;
6542 if (cap->nchar == 'm' || cap->nchar == 'M')
6543 {
6544 if (cap->cmdchar == '[')
6545 findc = '{';
6546 else
6547 findc = '}';
6548 n = 9999;
6549 }
6550 else
6551 {
6552 findc = cap->nchar;
6553 n = cap->count1;
6554 }
6555 for ( ; n > 0; --n)
6556 {
6557 if ((pos = findmatchlimit(cap->oap, findc,
6558 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6559 {
6560 if (new_pos.lnum == 0) /* nothing found */
6561 {
6562 if (cap->nchar != 'm' && cap->nchar != 'M')
6563 clearopbeep(cap->oap);
6564 }
6565 else
6566 pos = &new_pos; /* use last one found */
6567 break;
6568 }
6569 prev_pos = new_pos;
6570 curwin->w_cursor = *pos;
6571 new_pos = *pos;
6572 }
6573 curwin->w_cursor = old_pos;
6574
6575 /*
6576 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6577 * brought us to the match for "[m" and "]M" when inside a method.
6578 * Try finding the '{' or '}' we want to be at.
6579 * Also repeat for the given count.
6580 */
6581 if (cap->nchar == 'm' || cap->nchar == 'M')
6582 {
6583 /* norm is TRUE for "]M" and "[m" */
6584 int norm = ((findc == '{') == (cap->nchar == 'm'));
6585
6586 n = cap->count1;
6587 /* found a match: we were inside a method */
6588 if (prev_pos.lnum != 0)
6589 {
6590 pos = &prev_pos;
6591 curwin->w_cursor = prev_pos;
6592 if (norm)
6593 --n;
6594 }
6595 else
6596 pos = NULL;
6597 while (n > 0)
6598 {
6599 for (;;)
6600 {
6601 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6602 {
6603 /* if not found anything, that's an error */
6604 if (pos == NULL)
6605 clearopbeep(cap->oap);
6606 n = 0;
6607 break;
6608 }
6609 c = gchar_cursor();
6610 if (c == '{' || c == '}')
6611 {
6612 /* Must have found end/start of class: use it.
6613 * Or found the place to be at. */
6614 if ((c == findc && norm) || (n == 1 && !norm))
6615 {
6616 new_pos = curwin->w_cursor;
6617 pos = &new_pos;
6618 n = 0;
6619 }
6620 /* if no match found at all, we started outside of the
6621 * class and we're inside now. Just go on. */
6622 else if (new_pos.lnum == 0)
6623 {
6624 new_pos = curwin->w_cursor;
6625 pos = &new_pos;
6626 }
6627 /* found start/end of other method: go to match */
6628 else if ((pos = findmatchlimit(cap->oap, findc,
6629 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6630 0)) == NULL)
6631 n = 0;
6632 else
6633 curwin->w_cursor = *pos;
6634 break;
6635 }
6636 }
6637 --n;
6638 }
6639 curwin->w_cursor = old_pos;
6640 if (pos == NULL && new_pos.lnum != 0)
6641 clearopbeep(cap->oap);
6642 }
6643 if (pos != NULL)
6644 {
6645 setpcmark();
6646 curwin->w_cursor = *pos;
6647 curwin->w_set_curswant = TRUE;
6648#ifdef FEAT_FOLDING
6649 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6650 && cap->oap->op_type == OP_NOP)
6651 foldOpenCursor();
6652#endif
6653 }
6654 }
6655
6656 /*
6657 * "[[", "[]", "]]" and "][": move to start or end of function
6658 */
6659 else if (cap->nchar == '[' || cap->nchar == ']')
6660 {
6661 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6662 flag = '{';
6663 else
6664 flag = '}'; /* "][" or "[]" */
6665
6666 curwin->w_set_curswant = TRUE;
6667 /*
6668 * Imitate strange Vi behaviour: When using "]]" with an operator
6669 * we also stop at '}'.
6670 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006671 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 (cap->oap->op_type != OP_NOP
6673 && cap->arg == FORWARD && flag == '{')))
6674 clearopbeep(cap->oap);
6675 else
6676 {
6677 if (cap->oap->op_type == OP_NOP)
6678 beginline(BL_WHITE | BL_FIX);
6679#ifdef FEAT_FOLDING
6680 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6681 foldOpenCursor();
6682#endif
6683 }
6684 }
6685
6686 /*
6687 * "[p", "[P", "]P" and "]p": put with indent adjustment
6688 */
6689 else if (cap->nchar == 'p' || cap->nchar == 'P')
6690 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006691 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006693 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6694 ? FORWARD : BACKWARD;
6695 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006696 int was_visual = VIsual_active;
6697 int line_count = curbuf->b_ml.ml_line_count;
6698 pos_T start, end;
6699
6700 if (VIsual_active)
6701 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006702 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006703 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006704 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006705 curwin->w_cursor = (dir == BACKWARD ? start : end);
6706 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006707# ifdef FEAT_CLIPBOARD
6708 adjust_clip_reg(&regname);
6709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006711
6712 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006713 if (was_visual)
6714 {
6715 VIsual = start;
6716 curwin->w_cursor = end;
6717 if (dir == BACKWARD)
6718 {
6719 /* adjust lines */
6720 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6721 curwin->w_cursor.lnum +=
6722 curbuf->b_ml.ml_line_count - line_count;
6723 }
6724
6725 VIsual_active = TRUE;
6726 if (VIsual_mode == 'V')
6727 {
6728 /* delete visually selected lines */
6729 cap->cmdchar = 'd';
6730 cap->nchar = NUL;
6731 cap->oap->regname = regname;
6732 nv_operator(cap);
6733 do_pending_operator(cap, 0, FALSE);
6734 }
6735 if (VIsual_active)
6736 {
6737 end_visual_mode();
6738 redraw_later(SOME_VALID);
6739 }
6740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 }
6742 }
6743
6744 /*
6745 * "['", "[`", "]'" and "]`": jump to next mark
6746 */
6747 else if (cap->nchar == '\'' || cap->nchar == '`')
6748 {
6749 pos = &curwin->w_cursor;
6750 for (n = cap->count1; n > 0; --n)
6751 {
6752 prev_pos = *pos;
6753 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6754 cap->nchar == '\'');
6755 if (pos == NULL)
6756 break;
6757 }
6758 if (pos == NULL)
6759 pos = &prev_pos;
6760 nv_cursormark(cap, cap->nchar == '\'', pos);
6761 }
6762
6763#ifdef FEAT_MOUSE
6764 /*
6765 * [ or ] followed by a middle mouse click: put selected text with
6766 * indent adjustment. Any other button just does as usual.
6767 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006768 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {
6770 (void)do_mouse(cap->oap, cap->nchar,
6771 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6772 cap->count1, PUT_FIXINDENT);
6773 }
6774#endif /* FEAT_MOUSE */
6775
6776#ifdef FEAT_FOLDING
6777 /*
6778 * "[z" and "]z": move to start or end of open fold.
6779 */
6780 else if (cap->nchar == 'z')
6781 {
6782 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6783 cap->count1) == FAIL)
6784 clearopbeep(cap->oap);
6785 }
6786#endif
6787
6788#ifdef FEAT_DIFF
6789 /*
6790 * "[c" and "]c": move to next or previous diff-change.
6791 */
6792 else if (cap->nchar == 'c')
6793 {
6794 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6795 cap->count1) == FAIL)
6796 clearopbeep(cap->oap);
6797 }
6798#endif
6799
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006800#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006801 /*
6802 * "[s", "[S", "]s" and "]S": move to next spell error.
6803 */
6804 else if (cap->nchar == 's' || cap->nchar == 'S')
6805 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006806 setpcmark();
6807 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006808 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6809 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006810 {
6811 clearopbeep(cap->oap);
6812 break;
6813 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006814# ifdef FEAT_FOLDING
6815 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6816 foldOpenCursor();
6817# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006818 }
6819#endif
6820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 /* Not a valid cap->nchar. */
6822 else
6823 clearopbeep(cap->oap);
6824}
6825
6826/*
6827 * Handle Normal mode "%" command.
6828 */
6829 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006830nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
6832 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006833#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 linenr_T lnum = curwin->w_cursor.lnum;
6835#endif
6836
6837 cap->oap->inclusive = TRUE;
6838 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6839 {
6840 if (cap->count0 > 100)
6841 clearopbeep(cap->oap);
6842 else
6843 {
6844 cap->oap->motion_type = MLINE;
6845 setpcmark();
6846 /* Round up, so CTRL-G will give same value. Watch out for a
6847 * large line count, the line number must not go negative! */
6848 if (curbuf->b_ml.ml_line_count > 1000000)
6849 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6850 / 100L * cap->count0;
6851 else
6852 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6853 cap->count0 + 99L) / 100L;
6854 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6855 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6856 beginline(BL_SOL | BL_FIX);
6857 }
6858 }
6859 else /* "%" : go to matching paren */
6860 {
6861 cap->oap->motion_type = MCHAR;
6862 cap->oap->use_reg_one = TRUE;
6863 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6864 clearopbeep(cap->oap);
6865 else
6866 {
6867 setpcmark();
6868 curwin->w_cursor = *pos;
6869 curwin->w_set_curswant = TRUE;
6870#ifdef FEAT_VIRTUALEDIT
6871 curwin->w_cursor.coladd = 0;
6872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875 }
6876#ifdef FEAT_FOLDING
6877 if (cap->oap->op_type == OP_NOP
6878 && lnum != curwin->w_cursor.lnum
6879 && (fdo_flags & FDO_PERCENT)
6880 && KeyTyped)
6881 foldOpenCursor();
6882#endif
6883}
6884
6885/*
6886 * Handle "(" and ")" commands.
6887 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6888 */
6889 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006890nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 cap->oap->motion_type = MCHAR;
6893 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006894 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6895 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 curwin->w_set_curswant = TRUE;
6897
6898 if (findsent(cap->arg, cap->count1) == FAIL)
6899 clearopbeep(cap->oap);
6900 else
6901 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006902 /* Don't leave the cursor on the NUL past end of line. */
6903 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904#ifdef FEAT_VIRTUALEDIT
6905 curwin->w_cursor.coladd = 0;
6906#endif
6907#ifdef FEAT_FOLDING
6908 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6909 foldOpenCursor();
6910#endif
6911 }
6912}
6913
6914/*
6915 * "m" command: Mark a position.
6916 */
6917 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006918nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
6920 if (!checkclearop(cap->oap))
6921 {
6922 if (setmark(cap->nchar) == FAIL)
6923 clearopbeep(cap->oap);
6924 }
6925}
6926
6927/*
6928 * "{" and "}" commands.
6929 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6930 */
6931 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006932nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933{
6934 cap->oap->motion_type = MCHAR;
6935 cap->oap->inclusive = FALSE;
6936 cap->oap->use_reg_one = TRUE;
6937 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006938 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 clearopbeep(cap->oap);
6940 else
6941 {
6942#ifdef FEAT_VIRTUALEDIT
6943 curwin->w_cursor.coladd = 0;
6944#endif
6945#ifdef FEAT_FOLDING
6946 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6947 foldOpenCursor();
6948#endif
6949 }
6950}
6951
6952/*
6953 * "u" command: Undo or make lower case.
6954 */
6955 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006956nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006958 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 {
6960 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6961 cap->cmdchar = 'g';
6962 cap->nchar = 'u';
6963 nv_operator(cap);
6964 }
6965 else
6966 nv_kundo(cap);
6967}
6968
6969/*
6970 * <Undo> command.
6971 */
6972 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006973nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 if (!checkclearopq(cap->oap))
6976 {
6977 u_undo((int)cap->count1);
6978 curwin->w_set_curswant = TRUE;
6979 }
6980}
6981
6982/*
6983 * Handle the "r" command.
6984 */
6985 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006986nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 char_u *ptr;
6989 int had_ctrl_v;
6990 long n;
6991
6992 if (checkclearop(cap->oap))
6993 return;
6994
6995 /* get another character */
6996 if (cap->nchar == Ctrl_V)
6997 {
6998 had_ctrl_v = Ctrl_V;
6999 cap->nchar = get_literal();
7000 /* Don't redo a multibyte character with CTRL-V. */
7001 if (cap->nchar > DEL)
7002 had_ctrl_v = NUL;
7003 }
7004 else
7005 had_ctrl_v = NUL;
7006
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007007 /* Abort if the character is a special key. */
7008 if (IS_SPECIAL(cap->nchar))
7009 {
7010 clearopbeep(cap->oap);
7011 return;
7012 }
7013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 /* Visual mode "r" */
7015 if (VIsual_active)
7016 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007017 if (got_int)
7018 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007019 if (had_ctrl_v)
7020 {
7021 if (cap->nchar == '\r')
7022 cap->nchar = -1;
7023 else if (cap->nchar == '\n')
7024 cap->nchar = -2;
7025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 nv_operator(cap);
7027 return;
7028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
7030#ifdef FEAT_VIRTUALEDIT
7031 /* Break tabs, etc. */
7032 if (virtual_active())
7033 {
7034 if (u_save_cursor() == FAIL)
7035 return;
7036 if (gchar_cursor() == NUL)
7037 {
7038 /* Add extra space and put the cursor on the first one. */
7039 coladvance_force((colnr_T)(getviscol() + cap->count1));
7040 curwin->w_cursor.col -= cap->count1;
7041 }
7042 else if (gchar_cursor() == TAB)
7043 coladvance_force(getviscol());
7044 }
7045#endif
7046
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007047 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007049 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050#ifdef FEAT_MBYTE
7051 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7052#endif
7053 )
7054 {
7055 clearopbeep(cap->oap);
7056 return;
7057 }
7058
7059 /*
7060 * Replacing with a TAB is done by edit() when it is complicated because
7061 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7062 * Other characters are done below to avoid problems with things like
7063 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7064 */
7065 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7066 {
7067 stuffnumReadbuff(cap->count1);
7068 stuffcharReadbuff('R');
7069 stuffcharReadbuff('\t');
7070 stuffcharReadbuff(ESC);
7071 return;
7072 }
7073
7074 /* save line for undo */
7075 if (u_save_cursor() == FAIL)
7076 return;
7077
7078 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7079 {
7080 /*
7081 * Replace character(s) by a single newline.
7082 * Strange vi behaviour: Only one newline is inserted.
7083 * Delete the characters here.
7084 * Insert the newline with an insert command, takes care of
7085 * autoindent. The insert command depends on being on the last
7086 * character of a line or not.
7087 */
7088#ifdef FEAT_MBYTE
7089 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7090#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007091 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092#endif
7093 stuffcharReadbuff('\r');
7094 stuffcharReadbuff(ESC);
7095
7096 /* Give 'r' to edit(), to get the redo command right. */
7097 invoke_edit(cap, TRUE, 'r', FALSE);
7098 }
7099 else
7100 {
7101 prep_redo(cap->oap->regname, cap->count1,
7102 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7103
7104 curbuf->b_op_start = curwin->w_cursor;
7105#ifdef FEAT_MBYTE
7106 if (has_mbyte)
7107 {
7108 int old_State = State;
7109
7110 if (cap->ncharC1 != 0)
7111 AppendCharToRedobuff(cap->ncharC1);
7112 if (cap->ncharC2 != 0)
7113 AppendCharToRedobuff(cap->ncharC2);
7114
7115 /* This is slow, but it handles replacing a single-byte with a
7116 * multi-byte and the other way around. Also handles adding
7117 * composing characters for utf-8. */
7118 for (n = cap->count1; n > 0; --n)
7119 {
7120 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007121 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7122 {
7123 int c = ins_copychar(curwin->w_cursor.lnum
7124 + (cap->nchar == Ctrl_Y ? -1 : 1));
7125 if (c != NUL)
7126 ins_char(c);
7127 else
7128 /* will be decremented further down */
7129 ++curwin->w_cursor.col;
7130 }
7131 else
7132 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 State = old_State;
7134 if (cap->ncharC1 != 0)
7135 ins_char(cap->ncharC1);
7136 if (cap->ncharC2 != 0)
7137 ins_char(cap->ncharC2);
7138 }
7139 }
7140 else
7141#endif
7142 {
7143 /*
7144 * Replace the characters within one line.
7145 */
7146 for (n = cap->count1; n > 0; --n)
7147 {
7148 /*
7149 * Get ptr again, because u_save and/or showmatch() will have
7150 * released the line. At the same time we let know that the
7151 * line will be changed.
7152 */
7153 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007154 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7155 {
7156 int c = ins_copychar(curwin->w_cursor.lnum
7157 + (cap->nchar == Ctrl_Y ? -1 : 1));
7158 if (c != NUL)
7159 ptr[curwin->w_cursor.col] = c;
7160 }
7161 else
7162 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 if (p_sm && msg_silent == 0)
7164 showmatch(cap->nchar);
7165 ++curwin->w_cursor.col;
7166 }
7167#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007168 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007170 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
Bram Moolenaar009b2592004-10-24 19:18:58 +00007172 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007173 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007175 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
7177#endif
7178
7179 /* mark the buffer as changed and prepare for displaying */
7180 changed_bytes(curwin->w_cursor.lnum,
7181 (colnr_T)(curwin->w_cursor.col - cap->count1));
7182 }
7183 --curwin->w_cursor.col; /* cursor on the last replaced char */
7184#ifdef FEAT_MBYTE
7185 /* if the character on the left of the current cursor is a multi-byte
7186 * character, move two characters left */
7187 if (has_mbyte)
7188 mb_adjust_cursor();
7189#endif
7190 curbuf->b_op_end = curwin->w_cursor;
7191 curwin->w_set_curswant = TRUE;
7192 set_last_insert(cap->nchar);
7193 }
7194}
7195
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196/*
7197 * 'o': Exchange start and end of Visual area.
7198 * 'O': same, but in block mode exchange left and right corners.
7199 */
7200 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007201v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203 pos_T old_cursor;
7204 colnr_T left, right;
7205
7206 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7207 {
7208 old_cursor = curwin->w_cursor;
7209 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7210 curwin->w_cursor.lnum = VIsual.lnum;
7211 coladvance(left);
7212 VIsual = curwin->w_cursor;
7213
7214 curwin->w_cursor.lnum = old_cursor.lnum;
7215 curwin->w_curswant = right;
7216 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7217 * right one column */
7218 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7219 ++curwin->w_curswant;
7220 coladvance(curwin->w_curswant);
7221 if (curwin->w_cursor.col == old_cursor.col
7222#ifdef FEAT_VIRTUALEDIT
7223 && (!virtual_active()
7224 || curwin->w_cursor.coladd == old_cursor.coladd)
7225#endif
7226 )
7227 {
7228 curwin->w_cursor.lnum = VIsual.lnum;
7229 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7230 ++right;
7231 coladvance(right);
7232 VIsual = curwin->w_cursor;
7233
7234 curwin->w_cursor.lnum = old_cursor.lnum;
7235 coladvance(left);
7236 curwin->w_curswant = left;
7237 }
7238 }
7239 else
7240 {
7241 old_cursor = curwin->w_cursor;
7242 curwin->w_cursor = VIsual;
7243 VIsual = old_cursor;
7244 curwin->w_set_curswant = TRUE;
7245 }
7246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
7248/*
7249 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7250 */
7251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007252nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 if (VIsual_active) /* "R" is replace lines */
7255 {
7256 cap->cmdchar = 'c';
7257 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007258 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 VIsual_mode = 'V';
7260 nv_operator(cap);
7261 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007262 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 {
7264 if (!curbuf->b_p_ma)
7265 EMSG(_(e_modifiable));
7266 else
7267 {
7268#ifdef FEAT_VIRTUALEDIT
7269 if (virtual_active())
7270 coladvance(getviscol());
7271#endif
7272 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7273 }
7274 }
7275}
7276
7277#ifdef FEAT_VREPLACE
7278/*
7279 * "gr".
7280 */
7281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007282nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 if (VIsual_active)
7285 {
7286 cap->cmdchar = 'r';
7287 cap->nchar = cap->extra_char;
7288 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7289 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007290 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 {
7292 if (!curbuf->b_p_ma)
7293 EMSG(_(e_modifiable));
7294 else
7295 {
7296 if (cap->extra_char == Ctrl_V) /* get another character */
7297 cap->extra_char = get_literal();
7298 stuffcharReadbuff(cap->extra_char);
7299 stuffcharReadbuff(ESC);
7300# ifdef FEAT_VIRTUALEDIT
7301 if (virtual_active())
7302 coladvance(getviscol());
7303# endif
7304 invoke_edit(cap, TRUE, 'v', FALSE);
7305 }
7306 }
7307}
7308#endif
7309
7310/*
7311 * Swap case for "~" command, when it does not work like an operator.
7312 */
7313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007314n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 long n;
7317 pos_T startpos;
7318 int did_change = 0;
7319#ifdef FEAT_NETBEANS_INTG
7320 pos_T pos;
7321 char_u *ptr;
7322 int count;
7323#endif
7324
7325 if (checkclearopq(cap->oap))
7326 return;
7327
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007328 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 {
7330 clearopbeep(cap->oap);
7331 return;
7332 }
7333
7334 prep_redo_cmd(cap);
7335
7336 if (u_save_cursor() == FAIL)
7337 return;
7338
7339 startpos = curwin->w_cursor;
7340#ifdef FEAT_NETBEANS_INTG
7341 pos = startpos;
7342#endif
7343 for (n = cap->count1; n > 0; --n)
7344 {
7345 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7346 inc_cursor();
7347 if (gchar_cursor() == NUL)
7348 {
7349 if (vim_strchr(p_ww, '~') != NULL
7350 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7351 {
7352#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007353 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 {
7355 if (did_change)
7356 {
7357 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007358 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007359 netbeans_removed(curbuf, pos.lnum, pos.col,
7360 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007362 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 }
7364 pos.col = 0;
7365 pos.lnum++;
7366 }
7367#endif
7368 ++curwin->w_cursor.lnum;
7369 curwin->w_cursor.col = 0;
7370 if (n > 1)
7371 {
7372 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7373 break;
7374 u_clearline();
7375 }
7376 }
7377 else
7378 break;
7379 }
7380 }
7381#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007382 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 {
7384 ptr = ml_get(pos.lnum);
7385 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007386 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7387 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 }
7389#endif
7390
7391
7392 check_cursor();
7393 curwin->w_set_curswant = TRUE;
7394 if (did_change)
7395 {
7396 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7397 0L);
7398 curbuf->b_op_start = startpos;
7399 curbuf->b_op_end = curwin->w_cursor;
7400 if (curbuf->b_op_end.col > 0)
7401 --curbuf->b_op_end.col;
7402 }
7403}
7404
7405/*
7406 * Move cursor to mark.
7407 */
7408 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007409nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
7411 if (check_mark(pos) == FAIL)
7412 clearop(cap->oap);
7413 else
7414 {
7415 if (cap->cmdchar == '\''
7416 || cap->cmdchar == '`'
7417 || cap->cmdchar == '['
7418 || cap->cmdchar == ']')
7419 setpcmark();
7420 curwin->w_cursor = *pos;
7421 if (flag)
7422 beginline(BL_WHITE | BL_FIX);
7423 else
7424 check_cursor();
7425 }
7426 cap->oap->motion_type = flag ? MLINE : MCHAR;
7427 if (cap->cmdchar == '`')
7428 cap->oap->use_reg_one = TRUE;
7429 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7430 curwin->w_set_curswant = TRUE;
7431}
7432
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433/*
7434 * Handle commands that are operators in Visual mode.
7435 */
7436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007437v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
7439 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7440
7441 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007442 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (isupper(cap->cmdchar))
7444 {
7445 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007446 {
7447 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7451 curwin->w_curswant = MAXCOL;
7452 }
7453 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7454 nv_operator(cap);
7455}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456
7457/*
7458 * "s" and "S" commands.
7459 */
7460 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007461nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7464 {
7465 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007466 {
7467 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 cap->cmdchar = 'c';
7471 nv_operator(cap);
7472 }
7473 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 nv_optrans(cap);
7475}
7476
7477/*
7478 * Abbreviated commands.
7479 */
7480 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007481nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
7483 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7484 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7485
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 /* in Visual mode these commands are operators */
7487 if (VIsual_active)
7488 v_visop(cap);
7489 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 nv_optrans(cap);
7491}
7492
7493/*
7494 * Translate a command into another command.
7495 */
7496 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007497nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498{
7499 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7500 (char_u *)"d$", (char_u *)"c$",
7501 (char_u *)"cl", (char_u *)"cc",
7502 (char_u *)"yy", (char_u *)":s\r"};
7503 static char_u *str = (char_u *)"xXDCsSY&";
7504
7505 if (!checkclearopq(cap->oap))
7506 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007507 /* In Vi "2D" doesn't delete the next line. Can't translate it
7508 * either, because "2." should also not use the count. */
7509 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7510 {
7511 cap->oap->start = curwin->w_cursor;
7512 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007513#ifdef FEAT_EVAL
7514 set_op_var(OP_DELETE);
7515#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007516 cap->count1 = 1;
7517 nv_dollar(cap);
7518 finish_op = TRUE;
7519 ResetRedobuff();
7520 AppendCharToRedobuff('D');
7521 }
7522 else
7523 {
7524 if (cap->count0)
7525 stuffnumReadbuff(cap->count0);
7526 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529 cap->opcount = 0;
7530}
7531
7532/*
7533 * "'" and "`" commands. Also for "g'" and "g`".
7534 * cap->arg is TRUE for "'" and "g'".
7535 */
7536 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007537nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
7539 pos_T *pos;
7540 int c;
7541#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007542 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7544#endif
7545
7546 if (cap->cmdchar == 'g')
7547 c = cap->extra_char;
7548 else
7549 c = cap->nchar;
7550 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7551 if (pos == (pos_T *)-1) /* jumped to other file */
7552 {
7553 if (cap->arg)
7554 {
7555 check_cursor_lnum();
7556 beginline(BL_WHITE | BL_FIX);
7557 }
7558 else
7559 check_cursor();
7560 }
7561 else
7562 nv_cursormark(cap, cap->arg, pos);
7563
7564#ifdef FEAT_VIRTUALEDIT
7565 /* May need to clear the coladd that a mark includes. */
7566 if (!virtual_active())
7567 curwin->w_cursor.coladd = 0;
7568#endif
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007569 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570#ifdef FEAT_FOLDING
7571 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007572 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007573 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 && (fdo_flags & FDO_MARK)
7575 && old_KeyTyped)
7576 foldOpenCursor();
7577#endif
7578}
7579
7580/*
7581 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7582 */
7583 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007584nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
7586#ifdef FEAT_JUMPLIST
7587 pos_T *pos;
7588# ifdef FEAT_FOLDING
7589 linenr_T lnum = curwin->w_cursor.lnum;
7590 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7591# endif
7592
7593 if (!checkclearopq(cap->oap))
7594 {
7595 if (cap->cmdchar == 'g')
7596 pos = movechangelist((int)cap->count1);
7597 else
7598 pos = movemark((int)cap->count1);
7599 if (pos == (pos_T *)-1) /* jump to other file */
7600 {
7601 curwin->w_set_curswant = TRUE;
7602 check_cursor();
7603 }
7604 else if (pos != NULL) /* can jump */
7605 nv_cursormark(cap, FALSE, pos);
7606 else if (cap->cmdchar == 'g')
7607 {
7608 if (curbuf->b_changelistlen == 0)
7609 EMSG(_("E664: changelist is empty"));
7610 else if (cap->count1 < 0)
7611 EMSG(_("E662: At start of changelist"));
7612 else
7613 EMSG(_("E663: At end of changelist"));
7614 }
7615 else
7616 clearopbeep(cap->oap);
7617# ifdef FEAT_FOLDING
7618 if (cap->oap->op_type == OP_NOP
7619 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7620 && (fdo_flags & FDO_MARK)
7621 && old_KeyTyped)
7622 foldOpenCursor();
7623# endif
7624 }
7625#else
7626 clearopbeep(cap->oap);
7627#endif
7628}
7629
7630/*
7631 * Handle '"' command.
7632 */
7633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007634nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635{
7636 if (checkclearop(cap->oap))
7637 return;
7638#ifdef FEAT_EVAL
7639 if (cap->nchar == '=')
7640 cap->nchar = get_expr_register();
7641#endif
7642 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7643 {
7644 cap->oap->regname = cap->nchar;
7645 cap->opcount = cap->count0; /* remember count before '"' */
7646#ifdef FEAT_EVAL
7647 set_reg_var(cap->oap->regname);
7648#endif
7649 }
7650 else
7651 clearopbeep(cap->oap);
7652}
7653
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654/*
7655 * Handle "v", "V" and "CTRL-V" commands.
7656 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7657 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007658 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 */
7660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007661nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007663 if (cap->cmdchar == Ctrl_Q)
7664 cap->cmdchar = Ctrl_V;
7665
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7667 * characterwise, linewise, or blockwise. */
7668 if (cap->oap->op_type != OP_NOP)
7669 {
7670 cap->oap->motion_force = cap->cmdchar;
7671 finish_op = FALSE; /* operator doesn't finish now but later */
7672 return;
7673 }
7674
7675 VIsual_select = cap->arg;
7676 if (VIsual_active) /* change Visual mode */
7677 {
7678 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7679 end_visual_mode();
7680 else /* toggle char/block mode */
7681 { /* or char/line mode */
7682 VIsual_mode = cap->cmdchar;
7683 showmode();
7684 }
7685 redraw_curbuf_later(INVERTED); /* update the inversion */
7686 }
7687 else /* start Visual mode */
7688 {
7689 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007690 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007692 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 VIsual = curwin->w_cursor;
7694
7695 VIsual_active = TRUE;
7696 VIsual_reselect = TRUE;
7697 if (!cap->arg)
7698 /* start Select mode when 'selectmode' contains "cmd" */
7699 may_start_select('c');
7700#ifdef FEAT_MOUSE
7701 setmouse();
7702#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007703 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 redraw_cmdline = TRUE; /* show visual mode later */
7705 /*
7706 * For V and ^V, we multiply the number of lines even if there
7707 * was only one -- webb
7708 */
7709 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7710 {
7711 curwin->w_cursor.lnum +=
7712 resel_VIsual_line_count * cap->count0 - 1;
7713 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7714 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7715 }
7716 VIsual_mode = resel_VIsual_mode;
7717 if (VIsual_mode == 'v')
7718 {
7719 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007720 {
7721 validate_virtcol();
7722 curwin->w_curswant = curwin->w_virtcol
7723 + resel_VIsual_vcol * cap->count0 - 1;
7724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007726 curwin->w_curswant = resel_VIsual_vcol;
7727 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007729 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {
7731 curwin->w_curswant = MAXCOL;
7732 coladvance((colnr_T)MAXCOL);
7733 }
7734 else if (VIsual_mode == Ctrl_V)
7735 {
7736 validate_virtcol();
7737 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007738 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 coladvance(curwin->w_curswant);
7740 }
7741 else
7742 curwin->w_set_curswant = TRUE;
7743 redraw_curbuf_later(INVERTED); /* show the inversion */
7744 }
7745 else
7746 {
7747 if (!cap->arg)
7748 /* start Select mode when 'selectmode' contains "cmd" */
7749 may_start_select('c');
7750 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007751 if (VIsual_mode != 'V' && *p_sel == 'e')
7752 ++cap->count1; /* include one more char */
7753 if (cap->count0 > 0 && --cap->count1 > 0)
7754 {
7755 /* With a count select that many characters or lines. */
7756 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7757 nv_right(cap);
7758 else if (VIsual_mode == 'V')
7759 nv_down(cap);
7760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 }
7762 }
7763}
7764
7765/*
7766 * Start selection for Shift-movement keys.
7767 */
7768 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007769start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770{
7771 /* if 'selectmode' contains "key", start Select mode */
7772 may_start_select('k');
7773 n_start_visual_mode('v');
7774}
7775
7776/*
7777 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7778 */
7779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007780may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782 VIsual_select = (stuff_empty() && typebuf_typed()
7783 && (vim_strchr(p_slm, c) != NULL));
7784}
7785
7786/*
7787 * Start Visual mode "c".
7788 * Should set VIsual_select before calling this.
7789 */
7790 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007791n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007793#ifdef FEAT_CONCEAL
7794 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007795 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007796#endif
7797
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 VIsual_mode = c;
7799 VIsual_active = TRUE;
7800 VIsual_reselect = TRUE;
7801#ifdef FEAT_VIRTUALEDIT
7802 /* Corner case: the 0 position in a tab may change when going into
7803 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7804 */
7805 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007806 {
7807 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810#endif
7811 VIsual = curwin->w_cursor;
7812
7813#ifdef FEAT_FOLDING
7814 foldAdjustVisual();
7815#endif
7816
7817#ifdef FEAT_MOUSE
7818 setmouse();
7819#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007820#ifdef FEAT_CONCEAL
7821 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007822 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007823#endif
7824
Bram Moolenaar09df3122006-01-23 22:23:09 +00007825 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 redraw_cmdline = TRUE; /* show visual mode later */
7827#ifdef FEAT_CLIPBOARD
7828 /* Make sure the clipboard gets updated. Needed because start and
7829 * end may still be the same, and the selection needs to be owned */
7830 clip_star.vmode = NUL;
7831#endif
7832
7833 /* Only need to redraw this line, unless still need to redraw an old
7834 * Visual area (when 'lazyredraw' is set). */
7835 if (curwin->w_redr_type < INVERTED)
7836 {
7837 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7838 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7839 }
7840}
7841
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842
7843/*
7844 * CTRL-W: Window commands
7845 */
7846 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007847nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849#ifdef FEAT_WINDOWS
Bram Moolenaar938783d2017-07-16 20:13:26 +02007850 if (cap->nchar == ':')
7851 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
7852 nv_colon(cap);
7853 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
7855#else
7856 (void)checkclearop(cap->oap);
7857#endif
7858}
7859
7860/*
7861 * CTRL-Z: Suspend
7862 */
7863 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007864nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 if (VIsual_active)
7868 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 do_cmdline_cmd((char_u *)"st");
7870}
7871
7872/*
7873 * Commands starting with "g".
7874 */
7875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007876nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877{
7878 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 int i;
7881 int flag = FALSE;
7882
7883 switch (cap->nchar)
7884 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007885 case Ctrl_A:
7886 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887#ifdef MEM_PROFILE
7888 /*
7889 * "g^A": dump log of used memory.
7890 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007891 if (!VIsual_active && cap->nchar == Ctrl_A)
7892 vim_mem_profile_dump();
7893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007895 /*
7896 * "g^A/g^X": sequentially increment visually selected region
7897 */
7898 if (VIsual_active)
7899 {
7900 cap->arg = TRUE;
7901 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007902 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007903 nv_addsub(cap);
7904 }
7905 else
7906 clearopbeep(oap);
7907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908
7909#ifdef FEAT_VREPLACE
7910 /*
7911 * "gR": Enter virtual replace mode.
7912 */
7913 case 'R':
7914 cap->arg = TRUE;
7915 nv_Replace(cap);
7916 break;
7917
7918 case 'r':
7919 nv_vreplace(cap);
7920 break;
7921#endif
7922
7923 case '&':
7924 do_cmdline_cmd((char_u *)"%s//~/&");
7925 break;
7926
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 /*
7928 * "gv": Reselect the previous Visual area. If Visual already active,
7929 * exchange previous and current Visual area.
7930 */
7931 case 'v':
7932 if (checkclearop(oap))
7933 break;
7934
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007935 if ( curbuf->b_visual.vi_start.lnum == 0
7936 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7937 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 beep_flush();
7939 else
7940 {
7941 /* set w_cursor to the start of the Visual area, tpos to the end */
7942 if (VIsual_active)
7943 {
7944 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007945 VIsual_mode = curbuf->b_visual.vi_mode;
7946 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947# ifdef FEAT_EVAL
7948 curbuf->b_visual_mode_eval = i;
7949# endif
7950 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007951 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7952 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007954 tpos = curbuf->b_visual.vi_end;
7955 curbuf->b_visual.vi_end = curwin->w_cursor;
7956 curwin->w_cursor = curbuf->b_visual.vi_start;
7957 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
7959 else
7960 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007961 VIsual_mode = curbuf->b_visual.vi_mode;
7962 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7963 tpos = curbuf->b_visual.vi_end;
7964 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 }
7966
7967 VIsual_active = TRUE;
7968 VIsual_reselect = TRUE;
7969
7970 /* Set Visual to the start and w_cursor to the end of the Visual
7971 * area. Make sure they are on an existing character. */
7972 check_cursor();
7973 VIsual = curwin->w_cursor;
7974 curwin->w_cursor = tpos;
7975 check_cursor();
7976 update_topline();
7977 /*
7978 * When called from normal "g" command: start Select mode when
7979 * 'selectmode' contains "cmd". When called for K_SELECT, always
7980 * start Select mode.
7981 */
7982 if (cap->arg)
7983 VIsual_select = TRUE;
7984 else
7985 may_start_select('c');
7986#ifdef FEAT_MOUSE
7987 setmouse();
7988#endif
7989#ifdef FEAT_CLIPBOARD
7990 /* Make sure the clipboard gets updated. Needed because start and
7991 * end are still the same, and the selection needs to be owned */
7992 clip_star.vmode = NUL;
7993#endif
7994 redraw_curbuf_later(INVERTED);
7995 showmode();
7996 }
7997 break;
7998 /*
7999 * "gV": Don't reselect the previous Visual area after a Select mode
8000 * mapping of menu.
8001 */
8002 case 'V':
8003 VIsual_reselect = FALSE;
8004 break;
8005
8006 /*
8007 * "gh": start Select mode.
8008 * "gH": start Select line mode.
8009 * "g^H": start Select block mode.
8010 */
8011 case K_BS:
8012 cap->nchar = Ctrl_H;
8013 /* FALLTHROUGH */
8014 case 'h':
8015 case 'H':
8016 case Ctrl_H:
8017# ifdef EBCDIC
8018 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8019 if (cap->nchar == Ctrl_H)
8020 cap->cmdchar = Ctrl_V;
8021 else
8022# endif
8023 cap->cmdchar = cap->nchar + ('v' - 'h');
8024 cap->arg = TRUE;
8025 nv_visual(cap);
8026 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008027
8028 /* "gn", "gN" visually select next/previous search match
8029 * "gn" selects next match
8030 * "gN" selects previous match
8031 */
8032 case 'N':
8033 case 'n':
8034 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008035 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008036 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037
8038 /*
8039 * "gj" and "gk" two new funny movement keys -- up and down
8040 * movement based on *screen* line rather than *file* line.
8041 */
8042 case 'j':
8043 case K_DOWN:
8044 /* with 'nowrap' it works just like the normal "j" command; also when
8045 * in a closed fold */
8046 if (!curwin->w_p_wrap
8047#ifdef FEAT_FOLDING
8048 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8049#endif
8050 )
8051 {
8052 oap->motion_type = MLINE;
8053 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8054 }
8055 else
8056 i = nv_screengo(oap, FORWARD, cap->count1);
8057 if (i == FAIL)
8058 clearopbeep(oap);
8059 break;
8060
8061 case 'k':
8062 case K_UP:
8063 /* with 'nowrap' it works just like the normal "k" command; also when
8064 * in a closed fold */
8065 if (!curwin->w_p_wrap
8066#ifdef FEAT_FOLDING
8067 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8068#endif
8069 )
8070 {
8071 oap->motion_type = MLINE;
8072 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8073 }
8074 else
8075 i = nv_screengo(oap, BACKWARD, cap->count1);
8076 if (i == FAIL)
8077 clearopbeep(oap);
8078 break;
8079
8080 /*
8081 * "gJ": join two lines without inserting a space.
8082 */
8083 case 'J':
8084 nv_join(cap);
8085 break;
8086
8087 /*
8088 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8089 * "gm": middle of "g0" and "g$".
8090 */
8091 case '^':
8092 flag = TRUE;
8093 /* FALLTHROUGH */
8094
8095 case '0':
8096 case 'm':
8097 case K_HOME:
8098 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 oap->motion_type = MCHAR;
8100 oap->inclusive = FALSE;
8101 if (curwin->w_p_wrap
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008102#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 && curwin->w_width != 0
8104#endif
8105 )
8106 {
8107 int width1 = W_WIDTH(curwin) - curwin_col_off();
8108 int width2 = width1 + curwin_col_off2();
8109
8110 validate_virtcol();
8111 i = 0;
8112 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8113 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8114 }
8115 else
8116 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008117 /* Go to the middle of the screen line. When 'number' or
8118 * 'relativenumber' is on and lines are wrapping the middle can be more
8119 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 if (cap->nchar == 'm')
8121 i += (W_WIDTH(curwin) - curwin_col_off()
8122 + ((curwin->w_p_wrap && i > 0)
8123 ? curwin_col_off2() : 0)) / 2;
8124 coladvance((colnr_T)i);
8125 if (flag)
8126 {
8127 do
8128 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008129 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 }
8131 curwin->w_set_curswant = TRUE;
8132 break;
8133
8134 case '_':
8135 /* "g_": to the last non-blank character in the line or <count> lines
8136 * downward. */
8137 cap->oap->motion_type = MCHAR;
8138 cap->oap->inclusive = TRUE;
8139 curwin->w_curswant = MAXCOL;
8140 if (cursor_down((long)(cap->count1 - 1),
8141 cap->oap->op_type == OP_NOP) == FAIL)
8142 clearopbeep(cap->oap);
8143 else
8144 {
8145 char_u *ptr = ml_get_curline();
8146
8147 /* In Visual mode we may end up after the line. */
8148 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8149 --curwin->w_cursor.col;
8150
8151 /* Decrease the cursor column until it's on a non-blank. */
8152 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008153 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 --curwin->w_cursor.col;
8155 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008156 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 }
8158 break;
8159
8160 case '$':
8161 case K_END:
8162 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {
8164 int col_off = curwin_col_off();
8165
8166 oap->motion_type = MCHAR;
8167 oap->inclusive = TRUE;
8168 if (curwin->w_p_wrap
Bram Moolenaar44a2f922016-03-19 22:11:51 +01008169#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 && curwin->w_width != 0
8171#endif
8172 )
8173 {
8174 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8175 if (cap->count1 == 1)
8176 {
8177 int width1 = W_WIDTH(curwin) - col_off;
8178 int width2 = width1 + curwin_col_off2();
8179
8180 validate_virtcol();
8181 i = width1 - 1;
8182 if (curwin->w_virtcol >= (colnr_T)width1)
8183 i += ((curwin->w_virtcol - width1) / width2 + 1)
8184 * width2;
8185 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008186
8187 /* Make sure we stick in this column. */
8188 validate_virtcol();
8189 curwin->w_curswant = curwin->w_virtcol;
8190 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8192 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8193 {
8194 /*
8195 * Check for landing on a character that got split at
8196 * the end of the line. We do not want to advance to
8197 * the next screen line.
8198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 if (curwin->w_virtcol > (colnr_T)i)
8200 --curwin->w_cursor.col;
8201 }
8202#endif
8203 }
8204 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8205 clearopbeep(oap);
8206 }
8207 else
8208 {
8209 i = curwin->w_leftcol + W_WIDTH(curwin) - col_off - 1;
8210 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008211
8212 /* Make sure we stick in this column. */
8213 validate_virtcol();
8214 curwin->w_curswant = curwin->w_virtcol;
8215 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 }
8217 }
8218 break;
8219
8220 /*
8221 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8222 */
8223 case '*':
8224 case '#':
8225#if POUND != '#'
8226 case POUND: /* pound sign (sometimes equal to '#') */
8227#endif
8228 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8229 case ']': /* :tselect for current identifier */
8230 nv_ident(cap);
8231 break;
8232
8233 /*
8234 * ge and gE: go back to end of word
8235 */
8236 case 'e':
8237 case 'E':
8238 oap->motion_type = MCHAR;
8239 curwin->w_set_curswant = TRUE;
8240 oap->inclusive = TRUE;
8241 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8242 clearopbeep(oap);
8243 break;
8244
8245 /*
8246 * "g CTRL-G": display info about cursor position
8247 */
8248 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008249 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 break;
8251
8252 /*
8253 * "gi": start Insert at the last position.
8254 */
8255 case 'i':
8256 if (curbuf->b_last_insert.lnum != 0)
8257 {
8258 curwin->w_cursor = curbuf->b_last_insert;
8259 check_cursor_lnum();
8260 i = (int)STRLEN(ml_get_curline());
8261 if (curwin->w_cursor.col > (colnr_T)i)
8262 {
8263#ifdef FEAT_VIRTUALEDIT
8264 if (virtual_active())
8265 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8266#endif
8267 curwin->w_cursor.col = i;
8268 }
8269 }
8270 cap->cmdchar = 'i';
8271 nv_edit(cap);
8272 break;
8273
8274 /*
8275 * "gI": Start insert in column 1.
8276 */
8277 case 'I':
8278 beginline(0);
8279 if (!checkclearopq(oap))
8280 invoke_edit(cap, FALSE, 'g', FALSE);
8281 break;
8282
8283#ifdef FEAT_SEARCHPATH
8284 /*
8285 * "gf": goto file, edit file under cursor
8286 * "]f" and "[f": can also be used.
8287 */
8288 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008289 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 nv_gotofile(cap);
8291 break;
8292#endif
8293
8294 /* "g'm" and "g`m": jump to mark without setting pcmark */
8295 case '\'':
8296 cap->arg = TRUE;
8297 /*FALLTHROUGH*/
8298 case '`':
8299 nv_gomark(cap);
8300 break;
8301
8302 /*
8303 * "gs": Goto sleep.
8304 */
8305 case 's':
8306 do_sleep(cap->count1 * 1000L);
8307 break;
8308
8309 /*
8310 * "ga": Display the ascii value of the character under the
8311 * cursor. It is displayed in decimal, hex, and octal. -- webb
8312 */
8313 case 'a':
8314 do_ascii(NULL);
8315 break;
8316
8317#ifdef FEAT_MBYTE
8318 /*
8319 * "g8": Display the bytes used for the UTF-8 character under the
8320 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008321 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 */
8323 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008324 if (cap->count0 == 8)
8325 utf_find_illegal();
8326 else
8327 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 break;
8329#endif
8330
Bram Moolenaar60402d62017-04-20 18:54:50 +02008331 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008332 case '<':
8333 show_sb_text();
8334 break;
8335
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 /*
8337 * "gg": Goto the first line in file. With a count it goes to
8338 * that line number like for "G". -- webb
8339 */
8340 case 'g':
8341 cap->arg = FALSE;
8342 nv_goto(cap);
8343 break;
8344
8345 /*
8346 * Two-character operators:
8347 * "gq" Format text
8348 * "gw" Format text and keep cursor position
8349 * "g~" Toggle the case of the text.
8350 * "gu" Change text to lower case.
8351 * "gU" Change text to upper case.
8352 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008353 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 */
8355 case 'q':
8356 case 'w':
8357 oap->cursor_start = curwin->w_cursor;
8358 /*FALLTHROUGH*/
8359 case '~':
8360 case 'u':
8361 case 'U':
8362 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008363 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 nv_operator(cap);
8365 break;
8366
8367 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008368 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 * current function
8370 * "gD": idem, but in the current file.
8371 */
8372 case 'd':
8373 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008374 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 break;
8376
8377#ifdef FEAT_MOUSE
8378 /*
8379 * g<*Mouse> : <C-*mouse>
8380 */
8381 case K_MIDDLEMOUSE:
8382 case K_MIDDLEDRAG:
8383 case K_MIDDLERELEASE:
8384 case K_LEFTMOUSE:
8385 case K_LEFTDRAG:
8386 case K_LEFTRELEASE:
8387 case K_RIGHTMOUSE:
8388 case K_RIGHTDRAG:
8389 case K_RIGHTRELEASE:
8390 case K_X1MOUSE:
8391 case K_X1DRAG:
8392 case K_X1RELEASE:
8393 case K_X2MOUSE:
8394 case K_X2DRAG:
8395 case K_X2RELEASE:
8396 mod_mask = MOD_MASK_CTRL;
8397 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8398 break;
8399#endif
8400
8401 case K_IGNORE:
8402 break;
8403
8404 /*
8405 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8406 */
8407 case 'p':
8408 case 'P':
8409 nv_put(cap);
8410 break;
8411
8412#ifdef FEAT_BYTEOFF
8413 /* "go": goto byte count from start of buffer */
8414 case 'o':
8415 goto_byte(cap->count0);
8416 break;
8417#endif
8418
8419 /* "gQ": improved Ex mode */
8420 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008421 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {
8423 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008424 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 break;
8426 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008427
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 if (!checkclearopq(oap))
8429 do_exmode(TRUE);
8430 break;
8431
8432#ifdef FEAT_JUMPLIST
8433 case ',':
8434 nv_pcmark(cap);
8435 break;
8436
8437 case ';':
8438 cap->count1 = -cap->count1;
8439 nv_pcmark(cap);
8440 break;
8441#endif
8442
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008443#ifdef FEAT_WINDOWS
8444 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008445 if (!checkclearop(oap))
8446 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008447 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008448 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008449 if (!checkclearop(oap))
8450 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008451 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008452#endif
8453
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008454 case '+':
8455 case '-': /* "g+" and "g-": undo or redo along the timeline */
8456 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008457 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008458 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008459 break;
8460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 default:
8462 clearopbeep(oap);
8463 break;
8464 }
8465}
8466
8467/*
8468 * Handle "o" and "O" commands.
8469 */
8470 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008471n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008473#ifdef FEAT_CONCEAL
8474 linenr_T oldline = curwin->w_cursor.lnum;
8475#endif
8476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 if (!checkclearopq(cap->oap))
8478 {
8479#ifdef FEAT_FOLDING
8480 if (cap->cmdchar == 'O')
8481 /* Open above the first line of a folded sequence of lines */
8482 (void)hasFolding(curwin->w_cursor.lnum,
8483 &curwin->w_cursor.lnum, NULL);
8484 else
8485 /* Open below the last line of a folded sequence of lines */
8486 (void)hasFolding(curwin->w_cursor.lnum,
8487 NULL, &curwin->w_cursor.lnum);
8488#endif
8489 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8490 (cap->cmdchar == 'O' ? 1 : 0)),
8491 (linenr_T)(curwin->w_cursor.lnum +
8492 (cap->cmdchar == 'o' ? 1 : 0))
8493 ) == OK
8494 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8495#ifdef FEAT_COMMENTS
8496 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8497#endif
8498 0, 0))
8499 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008500#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008501 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008502 update_single_line(curwin, oldline);
8503#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008504 /* When '#' is in 'cpoptions' ignore the count. */
8505 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8506 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008507#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008508 if (curwin->w_p_cul)
8509 /* force redraw of cursorline */
8510 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8513 }
8514 }
8515}
8516
8517/*
8518 * "." command: redo last change.
8519 */
8520 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008521nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 if (!checkclearopq(cap->oap))
8524 {
8525 /*
8526 * If "restart_edit" is TRUE, the last but one command is repeated
8527 * instead of the last command (inserting text). This is used for
8528 * CTRL-O <.> in insert mode.
8529 */
8530 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8531 clearopbeep(cap->oap);
8532 }
8533}
8534
8535/*
8536 * CTRL-R: undo undo
8537 */
8538 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008539nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
8541 if (!checkclearopq(cap->oap))
8542 {
8543 u_redo((int)cap->count1);
8544 curwin->w_set_curswant = TRUE;
8545 }
8546}
8547
8548/*
8549 * Handle "U" command.
8550 */
8551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008552nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553{
8554 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008555 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 {
8557 /* translate "gUU" to "gUgU" */
8558 cap->cmdchar = 'g';
8559 cap->nchar = 'U';
8560 nv_operator(cap);
8561 }
8562 else if (!checkclearopq(cap->oap))
8563 {
8564 u_undoline();
8565 curwin->w_set_curswant = TRUE;
8566 }
8567}
8568
8569/*
8570 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8571 * single character.
8572 */
8573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008574nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008576 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 n_swapchar(cap);
8578 else
8579 nv_operator(cap);
8580}
8581
8582/*
8583 * Handle an operator command.
8584 * The actual work is done by do_pending_operator().
8585 */
8586 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008587nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588{
8589 int op_type;
8590
8591 op_type = get_op_type(cap->cmdchar, cap->nchar);
8592
8593 if (op_type == cap->oap->op_type) /* double operator works on lines */
8594 nv_lineop(cap);
8595 else if (!checkclearop(cap->oap))
8596 {
8597 cap->oap->start = curwin->w_cursor;
8598 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008599#ifdef FEAT_EVAL
8600 set_op_var(op_type);
8601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 }
8603}
8604
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008605#ifdef FEAT_EVAL
8606/*
8607 * Set v:operator to the characters for "optype".
8608 */
8609 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008610set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008611{
8612 char_u opchars[3];
8613
8614 if (optype == OP_NOP)
8615 set_vim_var_string(VV_OP, NULL, 0);
8616 else
8617 {
8618 opchars[0] = get_op_char(optype);
8619 opchars[1] = get_extra_op_char(optype);
8620 opchars[2] = NUL;
8621 set_vim_var_string(VV_OP, opchars, -1);
8622 }
8623}
8624#endif
8625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626/*
8627 * Handle linewise operator "dd", "yy", etc.
8628 *
8629 * "_" is is a strange motion command that helps make operators more logical.
8630 * It is actually implemented, but not documented in the real Vi. This motion
8631 * command actually refers to "the current line". Commands like "dd" and "yy"
8632 * are really an alternate form of "d_" and "y_". It does accept a count, so
8633 * "d3_" works to delete 3 lines.
8634 */
8635 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008636nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
8638 cap->oap->motion_type = MLINE;
8639 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8640 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008641 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8642 && cap->oap->motion_force != 'v'
8643 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 || cap->oap->op_type == OP_LSHIFT
8645 || cap->oap->op_type == OP_RSHIFT)
8646 beginline(BL_SOL | BL_FIX);
8647 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8648 beginline(BL_WHITE | BL_FIX);
8649}
8650
8651/*
8652 * <Home> command.
8653 */
8654 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008655nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008657 /* CTRL-HOME is like "gg" */
8658 if (mod_mask & MOD_MASK_CTRL)
8659 nv_goto(cap);
8660 else
8661 {
8662 cap->count0 = 1;
8663 nv_pipe(cap);
8664 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008665 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8666 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667}
8668
8669/*
8670 * "|" command.
8671 */
8672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008673nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
8675 cap->oap->motion_type = MCHAR;
8676 cap->oap->inclusive = FALSE;
8677 beginline(0);
8678 if (cap->count0 > 0)
8679 {
8680 coladvance((colnr_T)(cap->count0 - 1));
8681 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8682 }
8683 else
8684 curwin->w_curswant = 0;
8685 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008686 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 curwin->w_set_curswant = FALSE;
8688}
8689
8690/*
8691 * Handle back-word command "b" and "B".
8692 * cap->arg is 1 for "B"
8693 */
8694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008695nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 cap->oap->motion_type = MCHAR;
8698 cap->oap->inclusive = FALSE;
8699 curwin->w_set_curswant = TRUE;
8700 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8701 clearopbeep(cap->oap);
8702#ifdef FEAT_FOLDING
8703 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8704 foldOpenCursor();
8705#endif
8706}
8707
8708/*
8709 * Handle word motion commands "e", "E", "w" and "W".
8710 * cap->arg is TRUE for "E" and "W".
8711 */
8712 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008713nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
8715 int n;
8716 int word_end;
8717 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008718 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
8720 /*
8721 * Set inclusive for the "E" and "e" command.
8722 */
8723 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8724 word_end = TRUE;
8725 else
8726 word_end = FALSE;
8727 cap->oap->inclusive = word_end;
8728
8729 /*
8730 * "cw" and "cW" are a special case.
8731 */
8732 if (!word_end && cap->oap->op_type == OP_CHANGE)
8733 {
8734 n = gchar_cursor();
8735 if (n != NUL) /* not an empty line */
8736 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008737 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {
8739 /*
8740 * Reproduce a funny Vi behaviour: "cw" on a blank only
8741 * changes one character, not all blanks until the start of
8742 * the next word. Only do this when the 'w' flag is included
8743 * in 'cpoptions'.
8744 */
8745 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8746 {
8747 cap->oap->inclusive = TRUE;
8748 cap->oap->motion_type = MCHAR;
8749 return;
8750 }
8751 }
8752 else
8753 {
8754 /*
8755 * This is a little strange. To match what the real Vi does,
8756 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8757 * that we are not on a space or a TAB. This seems impolite
8758 * at first, but it's really more what we mean when we say
8759 * 'cw'.
8760 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008761 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 * will change only one character! This is done by setting
8763 * flag.
8764 */
8765 cap->oap->inclusive = TRUE;
8766 word_end = TRUE;
8767 flag = TRUE;
8768 }
8769 }
8770 }
8771
8772 cap->oap->motion_type = MCHAR;
8773 curwin->w_set_curswant = TRUE;
8774 if (word_end)
8775 n = end_word(cap->count1, cap->arg, flag, FALSE);
8776 else
8777 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8778
Bram Moolenaardfefb982008-04-01 10:06:39 +00008779 /* Don't leave the cursor on the NUL past the end of line. Unless we
8780 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008781 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008782 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 if (n == FAIL && cap->oap->op_type == OP_NOP)
8785 clearopbeep(cap->oap);
8786 else
8787 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789#ifdef FEAT_FOLDING
8790 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8791 foldOpenCursor();
8792#endif
8793 }
8794}
8795
8796/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008797 * Used after a movement command: If the cursor ends up on the NUL after the
8798 * end of the line, may move it back to the last character and make the motion
8799 * inclusive.
8800 */
8801 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008802adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008803{
8804 /* The cursor cannot remain on the NUL when:
8805 * - the column is > 0
8806 * - not in Visual mode or 'selection' is "o"
8807 * - 'virtualedit' is not "all" and not "onemore".
8808 */
8809 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008810 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008811#ifdef FEAT_VIRTUALEDIT
8812 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8813#endif
8814 )
8815 {
8816 --curwin->w_cursor.col;
8817#ifdef FEAT_MBYTE
8818 /* prevent cursor from moving on the trail byte */
8819 if (has_mbyte)
8820 mb_adjust_cursor();
8821#endif
8822 oap->inclusive = TRUE;
8823 }
8824}
8825
8826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 * "0" and "^" commands.
8828 * cap->arg is the argument for beginline().
8829 */
8830 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008831nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
8833 cap->oap->motion_type = MCHAR;
8834 cap->oap->inclusive = FALSE;
8835 beginline(cap->arg);
8836#ifdef FEAT_FOLDING
8837 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8838 foldOpenCursor();
8839#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008840 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8841 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842}
8843
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844/*
8845 * In exclusive Visual mode, may include the last character.
8846 */
8847 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008848adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
8850 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008851 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008853#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 if (has_mbyte)
8855 inc_cursor();
8856 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 ++curwin->w_cursor.col;
8859 cap->oap->inclusive = FALSE;
8860 }
8861}
8862
8863/*
8864 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8865 * Should check VIsual_mode before calling this.
8866 * Returns TRUE when backed up to the previous line.
8867 */
8868 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008869unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
8871 pos_T *pp;
8872
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008873 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008875 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 pp = &curwin->w_cursor;
8877 else
8878 pp = &VIsual;
8879#ifdef FEAT_VIRTUALEDIT
8880 if (pp->coladd > 0)
8881 --pp->coladd;
8882 else
8883#endif
8884 if (pp->col > 0)
8885 {
8886 --pp->col;
8887#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008888 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889#endif
8890 }
8891 else if (pp->lnum > 1)
8892 {
8893 --pp->lnum;
8894 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8895 return TRUE;
8896 }
8897 }
8898 return FALSE;
8899}
8900
8901/*
8902 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8903 */
8904 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008905nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907 if (VIsual_active)
8908 VIsual_select = TRUE;
8909 else if (VIsual_reselect)
8910 {
8911 cap->nchar = 'v'; /* fake "gv" command */
8912 cap->arg = TRUE;
8913 nv_g_cmd(cap);
8914 }
8915}
8916
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917
8918/*
8919 * "G", "gg", CTRL-END, CTRL-HOME.
8920 * cap->arg is TRUE for "G".
8921 */
8922 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008923nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 linenr_T lnum;
8926
8927 if (cap->arg)
8928 lnum = curbuf->b_ml.ml_line_count;
8929 else
8930 lnum = 1L;
8931 cap->oap->motion_type = MLINE;
8932 setpcmark();
8933
8934 /* When a count is given, use it instead of the default lnum */
8935 if (cap->count0 != 0)
8936 lnum = cap->count0;
8937 if (lnum < 1L)
8938 lnum = 1L;
8939 else if (lnum > curbuf->b_ml.ml_line_count)
8940 lnum = curbuf->b_ml.ml_line_count;
8941 curwin->w_cursor.lnum = lnum;
8942 beginline(BL_SOL | BL_FIX);
8943#ifdef FEAT_FOLDING
8944 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8945 foldOpenCursor();
8946#endif
8947}
8948
8949/*
8950 * CTRL-\ in Normal mode.
8951 */
8952 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008953nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954{
8955 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8956 {
8957 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008958 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 clear_cmdline = TRUE; /* unshow mode later */
8960 restart_edit = 0;
8961#ifdef FEAT_CMDWIN
8962 if (cmdwin_type != 0)
8963 cmdwin_result = Ctrl_C;
8964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 if (VIsual_active)
8966 {
8967 end_visual_mode(); /* stop Visual */
8968 redraw_curbuf_later(INVERTED);
8969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8971 if (cap->nchar == Ctrl_G && p_im)
8972 restart_edit = 'a';
8973 }
8974 else
8975 clearopbeep(cap->oap);
8976}
8977
8978/*
8979 * ESC in Normal mode: beep, but don't flush buffers.
8980 * Don't even beep if we are canceling a command.
8981 */
8982 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008983nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984{
8985 int no_reason;
8986
8987 no_reason = (cap->oap->op_type == OP_NOP
8988 && cap->opcount == 0
8989 && cap->count0 == 0
8990 && cap->oap->regname == 0
8991 && !p_im);
8992
8993 if (cap->arg) /* TRUE for CTRL-C */
8994 {
8995 if (restart_edit == 0
8996#ifdef FEAT_CMDWIN
8997 && cmdwin_type == 0
8998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02009001 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002
9003 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9004 * set again below when halfway a mapping. */
9005 if (!p_im)
9006 restart_edit = 0;
9007#ifdef FEAT_CMDWIN
9008 if (cmdwin_type != 0)
9009 {
9010 cmdwin_result = K_IGNORE;
9011 got_int = FALSE; /* don't stop executing autocommands et al. */
9012 return;
9013 }
9014#endif
9015 }
9016
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 if (VIsual_active)
9018 {
9019 end_visual_mode(); /* stop Visual */
9020 check_cursor_col(); /* make sure cursor is not beyond EOL */
9021 curwin->w_set_curswant = TRUE;
9022 redraw_curbuf_later(INVERTED);
9023 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009024 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009025 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 clearop(cap->oap);
9027
9028 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9029 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009030 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 restart_edit = 'a';
9032}
9033
9034/*
9035 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009036 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 */
9038 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009039nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040{
9041 /* <Insert> is equal to "i" */
9042 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9043 cap->cmdchar = 'i';
9044
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 /* in Visual mode "A" and "I" are an operator */
9046 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
9047 v_visop(cap);
9048
9049 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009050 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9051 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 {
9053#ifdef FEAT_TEXTOBJ
9054 nv_object(cap);
9055#else
9056 clearopbeep(cap->oap);
9057#endif
9058 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009059#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009060 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009061 {
9062 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009063 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009064 return;
9065 }
9066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 else if (!curbuf->b_p_ma && !p_im)
9068 {
9069 /* Only give this error when 'insertmode' is off. */
9070 EMSG(_(e_modifiable));
9071 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009072 if (cap->cmdchar == K_PS)
9073 /* drop the pasted text */
9074 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009076 else if (cap->cmdchar == K_PS && VIsual_active)
9077 {
9078 pos_T old_pos = curwin->w_cursor;
9079 pos_T old_visual = VIsual;
9080
9081 /* In Visual mode the selected text is deleted. */
9082 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9083 {
9084 shift_delete_registers();
9085 cap->oap->regname = '1';
9086 }
9087 else
9088 cap->oap->regname = '-';
9089 cap->cmdchar = 'd';
9090 cap->nchar = NUL;
9091 nv_operator(cap);
9092 do_pending_operator(cap, 0, FALSE);
9093 cap->cmdchar = K_PS;
9094
9095 /* When the last char in the line was deleted then append. Detect this
9096 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009097 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9098 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009099 inc_cursor();
9100
9101 /* Insert to replace the deleted text with the pasted text. */
9102 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 else if (!checkclearopq(cap->oap))
9105 {
9106 switch (cap->cmdchar)
9107 {
9108 case 'A': /* "A"ppend after the line */
9109 curwin->w_set_curswant = TRUE;
9110#ifdef FEAT_VIRTUALEDIT
9111 if (ve_flags == VE_ALL)
9112 {
9113 int save_State = State;
9114
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009115 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 * character past the end of the line */
9117 State = INSERT;
9118 coladvance((colnr_T)MAXCOL);
9119 State = save_State;
9120 }
9121 else
9122#endif
9123 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9124 break;
9125
9126 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009127 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9128 beginline(BL_WHITE);
9129 else
9130 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 break;
9132
Bram Moolenaara1891842017-02-04 21:34:31 +01009133 case K_PS:
9134 /* Bracketed paste works like "a"ppend, unless the cursor is in
9135 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009136 if (curwin->w_cursor.col == 0)
9137 break;
9138 /*FALLTHROUGH*/
9139
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9141#ifdef FEAT_VIRTUALEDIT
9142 /* increment coladd when in virtual space, increment the
9143 * column otherwise, also to append after an unprintable char */
9144 if (virtual_active()
9145 && (curwin->w_cursor.coladd > 0
9146 || *ml_get_cursor() == NUL
9147 || *ml_get_cursor() == TAB))
9148 curwin->w_cursor.coladd++;
9149 else
9150#endif
9151 if (*ml_get_cursor() != NUL)
9152 inc_cursor();
9153 break;
9154 }
9155
9156#ifdef FEAT_VIRTUALEDIT
9157 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9158 {
9159 int save_State = State;
9160
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009161 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 * character past the end of the line */
9163 State = INSERT;
9164 coladvance(getviscol());
9165 State = save_State;
9166 }
9167#endif
9168
9169 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9170 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009171 else if (cap->cmdchar == K_PS)
9172 /* drop the pasted text */
9173 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174}
9175
9176/*
9177 * Invoke edit() and take care of "restart_edit" and the return value.
9178 */
9179 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009180invoke_edit(
9181 cmdarg_T *cap,
9182 int repl, /* "r" or "gr" command */
9183 int cmd,
9184 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
9186 int restart_edit_save = 0;
9187
9188 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9189 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9190 * it. */
9191 if (repl || !stuff_empty())
9192 restart_edit_save = restart_edit;
9193 else
9194 restart_edit_save = 0;
9195
9196 /* Always reset "restart_edit", this is not a restarted edit. */
9197 restart_edit = 0;
9198
9199 if (edit(cmd, startln, cap->count1))
9200 cap->retval |= CA_COMMAND_BUSY;
9201
9202 if (restart_edit == 0)
9203 restart_edit = restart_edit_save;
9204}
9205
9206#ifdef FEAT_TEXTOBJ
9207/*
9208 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9209 */
9210 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009211nv_object(
9212 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213{
9214 int flag;
9215 int include;
9216 char_u *mps_save;
9217
9218 if (cap->cmdchar == 'i')
9219 include = FALSE; /* "ix" = inner object: exclude white space */
9220 else
9221 include = TRUE; /* "ax" = an object: include white space */
9222
9223 /* Make sure (), [], {} and <> are in 'matchpairs' */
9224 mps_save = curbuf->b_p_mps;
9225 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9226
9227 switch (cap->nchar)
9228 {
9229 case 'w': /* "aw" = a word */
9230 flag = current_word(cap->oap, cap->count1, include, FALSE);
9231 break;
9232 case 'W': /* "aW" = a WORD */
9233 flag = current_word(cap->oap, cap->count1, include, TRUE);
9234 break;
9235 case 'b': /* "ab" = a braces block */
9236 case '(':
9237 case ')':
9238 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9239 break;
9240 case 'B': /* "aB" = a Brackets block */
9241 case '{':
9242 case '}':
9243 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9244 break;
9245 case '[': /* "a[" = a [] block */
9246 case ']':
9247 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9248 break;
9249 case '<': /* "a<" = a <> block */
9250 case '>':
9251 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9252 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009253 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009254 /* Do not adjust oap->end in do_pending_operator()
9255 * otherwise there are different results for 'dit'
9256 * (note leading whitespace in last line):
9257 * 1) <b> 2) <b>
9258 * foobar foobar
9259 * </b> </b>
9260 */
9261 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009262 flag = current_tagblock(cap->oap, cap->count1, include);
9263 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 case 'p': /* "ap" = a paragraph */
9265 flag = current_par(cap->oap, cap->count1, include, 'p');
9266 break;
9267 case 's': /* "as" = a sentence */
9268 flag = current_sent(cap->oap, cap->count1, include);
9269 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009270 case '"': /* "a"" = a double quoted string */
9271 case '\'': /* "a'" = a single quoted string */
9272 case '`': /* "a`" = a backtick quoted string */
9273 flag = current_quote(cap->oap, cap->count1, include,
9274 cap->nchar);
9275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276#if 0 /* TODO */
9277 case 'S': /* "aS" = a section */
9278 case 'f': /* "af" = a filename */
9279 case 'u': /* "au" = a URL */
9280#endif
9281 default:
9282 flag = FAIL;
9283 break;
9284 }
9285
9286 curbuf->b_p_mps = mps_save;
9287 if (flag == FAIL)
9288 clearopbeep(cap->oap);
9289 adjust_cursor_col();
9290 curwin->w_set_curswant = TRUE;
9291}
9292#endif
9293
9294/*
9295 * "q" command: Start/stop recording.
9296 * "q:", "q/", "q?": edit command-line in command-line window.
9297 */
9298 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009299nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300{
9301 if (cap->oap->op_type == OP_FORMAT)
9302 {
9303 /* "gqq" is the same as "gqgq": format line */
9304 cap->cmdchar = 'g';
9305 cap->nchar = 'q';
9306 nv_operator(cap);
9307 }
9308 else if (!checkclearop(cap->oap))
9309 {
9310#ifdef FEAT_CMDWIN
9311 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9312 {
9313 stuffcharReadbuff(cap->nchar);
9314 stuffcharReadbuff(K_CMDWIN);
9315 }
9316 else
9317#endif
9318 /* (stop) recording into a named register, unless executing a
9319 * register */
9320 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9321 clearopbeep(cap->oap);
9322 }
9323}
9324
9325/*
9326 * Handle the "@r" command.
9327 */
9328 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009329nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
9331 if (checkclearop(cap->oap))
9332 return;
9333#ifdef FEAT_EVAL
9334 if (cap->nchar == '=')
9335 {
9336 if (get_expr_register() == NUL)
9337 return;
9338 }
9339#endif
9340 while (cap->count1-- && !got_int)
9341 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009342 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 {
9344 clearopbeep(cap->oap);
9345 break;
9346 }
9347 line_breakcheck();
9348 }
9349}
9350
9351/*
9352 * Handle the CTRL-U and CTRL-D commands.
9353 */
9354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009355nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356{
9357 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9358 || (cap->cmdchar == Ctrl_D
9359 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9360 clearopbeep(cap->oap);
9361 else if (!checkclearop(cap->oap))
9362 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9363}
9364
9365/*
9366 * Handle "J" or "gJ" command.
9367 */
9368 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009369nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 if (VIsual_active) /* join the visual lines */
9372 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009373 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 {
9375 if (cap->count0 <= 1)
9376 cap->count0 = 2; /* default for join is two lines! */
9377 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9378 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009380 /* can't join when on the last line */
9381 if (cap->count0 <= 2)
9382 {
9383 clearopbeep(cap->oap);
9384 return;
9385 }
9386 cap->count0 = curbuf->b_ml.ml_line_count
9387 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009389
9390 prep_redo(cap->oap->regname, cap->count0,
9391 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9392 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 }
9394}
9395
9396/*
9397 * "P", "gP", "p" and "gp" commands.
9398 */
9399 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009400nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 int regname = 0;
9403 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009404 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009405 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 int dir;
9407 int flags = 0;
9408
9409 if (cap->oap->op_type != OP_NOP)
9410 {
9411#ifdef FEAT_DIFF
9412 /* "dp" is ":diffput" */
9413 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9414 {
9415 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009416 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 }
9418 else
9419#endif
9420 clearopbeep(cap->oap);
9421 }
9422 else
9423 {
9424 dir = (cap->cmdchar == 'P'
9425 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9426 ? BACKWARD : FORWARD;
9427 prep_redo_cmd(cap);
9428 if (cap->cmdchar == 'g')
9429 flags |= PUT_CURSEND;
9430
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 if (VIsual_active)
9432 {
9433 /* Putting in Visual mode: The put text replaces the selected
9434 * text. First delete the selected text, then put the new text.
9435 * Need to save and restore the registers that the delete
9436 * overwrites if the old contents is being put.
9437 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009438 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009440#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009442#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009443 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009444 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009445#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448
9449 )
9450 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009451 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 * put, save it first. */
9453 reg1 = get_register(regname, TRUE);
9454 }
9455
9456 /* Now delete the selected text. */
9457 cap->cmdchar = 'd';
9458 cap->nchar = NUL;
9459 cap->oap->regname = NUL;
9460 nv_operator(cap);
9461 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009462 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463
9464 /* delete PUT_LINE_BACKWARD; */
9465 cap->oap->regname = regname;
9466
9467 if (reg1 != NULL)
9468 {
9469 /* Delete probably changed the register we want to put, save
9470 * it first. Then put back what was there before the delete. */
9471 reg2 = get_register(regname, FALSE);
9472 put_register(regname, reg1);
9473 }
9474
9475 /* When deleted a linewise Visual area, put the register as
9476 * lines to avoid it joined with the next line. When deletion was
9477 * characterwise, split a line when putting lines. */
9478 if (VIsual_mode == 'V')
9479 flags |= PUT_LINE;
9480 else if (VIsual_mode == 'v')
9481 flags |= PUT_LINE_SPLIT;
9482 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9483 flags |= PUT_LINE_FORWARD;
9484 dir = BACKWARD;
9485 if ((VIsual_mode != 'V'
9486 && curwin->w_cursor.col < curbuf->b_op_start.col)
9487 || (VIsual_mode == 'V'
9488 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9489 /* cursor is at the end of the line or end of file, put
9490 * forward. */
9491 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009492 /* May have been reset in do_put(). */
9493 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 do_put(cap->oap->regname, dir, cap->count1, flags);
9496
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 /* If a register was saved, put it back now. */
9498 if (reg2 != NULL)
9499 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009500
9501 /* What to reselect with "gv"? Selecting the just put text seems to
9502 * be the most useful, since the original text was removed. */
9503 if (was_visual)
9504 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009505 curbuf->b_visual.vi_start = curbuf->b_op_start;
9506 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009507 /* need to adjust cursor position */
9508 if (*p_sel == 'e')
9509 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009510 }
9511
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009512 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009513 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009514 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009515 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009516 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009517
9518 /* If the cursor was in that line, move it to the end of the last
9519 * line. */
9520 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9521 {
9522 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9523 coladvance((colnr_T)MAXCOL);
9524 }
9525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 auto_format(FALSE, TRUE);
9527 }
9528}
9529
9530/*
9531 * "o" and "O" commands.
9532 */
9533 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009534nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535{
9536#ifdef FEAT_DIFF
9537 /* "do" is ":diffget" */
9538 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9539 {
9540 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009541 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 }
9543 else
9544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 if (VIsual_active) /* switch start and end of visual */
9546 v_swap_corners(cap->cmdchar);
9547 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 n_opencmd(cap);
9549}
9550
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551#ifdef FEAT_NETBEANS_INTG
9552 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009553nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555 netbeans_keycommand(cap->nchar);
9556}
9557#endif
9558
9559#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009561nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562{
9563 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9564}
9565#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009566
9567#ifdef FEAT_AUTOCMD
9568/*
9569 * Trigger CursorHold event.
9570 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9571 * input buffer. "did_cursorhold" is set to avoid retriggering.
9572 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009574nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009575{
9576 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9577 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009578 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009579}
9580#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009581
9582/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009583 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009584 */
9585 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009586get_op_vcol(
9587 oparg_T *oap,
9588 colnr_T redo_VIsual_vcol,
9589 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009590{
9591 colnr_T start, end;
9592
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009593 if (VIsual_mode != Ctrl_V
9594 || (!initial && oap->end.col < W_WIDTH(curwin)))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009595 return;
9596
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009597 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009598
9599#ifdef FEAT_MBYTE
9600 /* prevent from moving onto a trail byte */
9601 if (has_mbyte)
9602 mb_adjustpos(curwin->w_buffer, &oap->end);
9603#endif
9604
9605 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009606
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009607 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009608 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009609 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9610
9611 if (start < oap->start_vcol)
9612 oap->start_vcol = start;
9613 if (end > oap->end_vcol)
9614 {
9615 if (initial && *p_sel == 'e' && start >= 1
9616 && start - 1 >= oap->end_vcol)
9617 oap->end_vcol = start - 1;
9618 else
9619 oap->end_vcol = end;
9620 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009621 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009622
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009623 /* if '$' was used, get oap->end_vcol from longest line */
9624 if (curwin->w_curswant == MAXCOL)
9625 {
9626 curwin->w_cursor.col = MAXCOL;
9627 oap->end_vcol = 0;
9628 for (curwin->w_cursor.lnum = oap->start.lnum;
9629 curwin->w_cursor.lnum <= oap->end.lnum;
9630 ++curwin->w_cursor.lnum)
9631 {
9632 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9633 if (end > oap->end_vcol)
9634 oap->end_vcol = end;
9635 }
9636 }
9637 else if (redo_VIsual_busy)
9638 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9639 /*
9640 * Correct oap->end.col and oap->start.col to be the
9641 * upper-left and lower-right corner of the block area.
9642 *
9643 * (Actually, this does convert column positions into character
9644 * positions)
9645 */
9646 curwin->w_cursor.lnum = oap->end.lnum;
9647 coladvance(oap->end_vcol);
9648 oap->end = curwin->w_cursor;
9649
9650 curwin->w_cursor = oap->start;
9651 coladvance(oap->start_vcol);
9652 oap->start = curwin->w_cursor;
9653}