blob: 81bedfdba1f00bd72621380eb4dfbc8896c7a11a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017/*
18 * The Visual area is remembered for reselection.
19 */
20static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
21static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020022static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar7d311c52014-02-22 23:49:35 +010023static int VIsual_mode_orig = NUL; /* saved Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010027#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020031#ifdef __BORLANDC__
32 _RTLENTRYF
33#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034 nv_compare(const void *s1, const void *s2);
35static int find_command(int cmdchar);
36static void op_colon(oparg_T *oap);
37static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010038#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void find_start_of_word(pos_T *);
40static void find_end_of_word(pos_T *);
41static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void prep_redo_cmd(cmdarg_T *cap);
44static void prep_redo(int regname, long, int, int, int, int, int);
45static int checkclearop(oparg_T *oap);
46static int checkclearopq(oparg_T *oap);
47static void clearop(oparg_T *oap);
48static void clearopbeep(oparg_T *oap);
49static void unshift_special(cmdarg_T *cap);
50static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010052static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#endif
54
55/*
56 * nv_*(): functions called to handle Normal and Visual mode commands.
57 * n_*(): functions called to handle Normal mode commands.
58 * v_*(): functions called to handle Visual mode commands.
59 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_ignore(cmdarg_T *cap);
61static void nv_nop(cmdarg_T *cap);
62static void nv_error(cmdarg_T *cap);
63static void nv_help(cmdarg_T *cap);
64static void nv_addsub(cmdarg_T *cap);
65static void nv_page(cmdarg_T *cap);
66static void nv_gd(oparg_T *oap, int nchar, int thisblock);
67static int nv_screengo(oparg_T *oap, int dir, long dist);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_mousescroll(cmdarg_T *cap);
70static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010072static void nv_scroll_line(cmdarg_T *cap);
73static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_ver_scrollbar(cmdarg_T *cap);
76static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000078#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_tabline(cmdarg_T *cap);
80static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010082static void nv_exmode(cmdarg_T *cap);
83static void nv_colon(cmdarg_T *cap);
84static void nv_ctrlg(cmdarg_T *cap);
85static void nv_ctrlh(cmdarg_T *cap);
86static void nv_clear(cmdarg_T *cap);
87static void nv_ctrlo(cmdarg_T *cap);
88static void nv_hat(cmdarg_T *cap);
89static void nv_Zet(cmdarg_T *cap);
90static void nv_ident(cmdarg_T *cap);
91static void nv_tagpop(cmdarg_T *cap);
92static void nv_scroll(cmdarg_T *cap);
93static void nv_right(cmdarg_T *cap);
94static void nv_left(cmdarg_T *cap);
95static void nv_up(cmdarg_T *cap);
96static void nv_down(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_SEARCHPATH
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void nv_gotofile(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_end(cmdarg_T *cap);
101static void nv_dollar(cmdarg_T *cap);
102static void nv_search(cmdarg_T *cap);
103static void nv_next(cmdarg_T *cap);
104static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
105static void nv_csearch(cmdarg_T *cap);
106static void nv_brackets(cmdarg_T *cap);
107static void nv_percent(cmdarg_T *cap);
108static void nv_brace(cmdarg_T *cap);
109static void nv_mark(cmdarg_T *cap);
110static void nv_findpar(cmdarg_T *cap);
111static void nv_undo(cmdarg_T *cap);
112static void nv_kundo(cmdarg_T *cap);
113static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_VREPLACE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_vreplace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void v_swap_corners(int cmdchar);
118static void nv_replace(cmdarg_T *cap);
119static void n_swapchar(cmdarg_T *cap);
120static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
121static void v_visop(cmdarg_T *cap);
122static void nv_subst(cmdarg_T *cap);
123static void nv_abbrev(cmdarg_T *cap);
124static void nv_optrans(cmdarg_T *cap);
125static void nv_gomark(cmdarg_T *cap);
126static void nv_pcmark(cmdarg_T *cap);
127static void nv_regname(cmdarg_T *cap);
128static void nv_visual(cmdarg_T *cap);
129static void n_start_visual_mode(int c);
130static void nv_window(cmdarg_T *cap);
131static void nv_suspend(cmdarg_T *cap);
132static void nv_g_cmd(cmdarg_T *cap);
133static void n_opencmd(cmdarg_T *cap);
134static void nv_dot(cmdarg_T *cap);
135static void nv_redo(cmdarg_T *cap);
136static void nv_Undo(cmdarg_T *cap);
137static void nv_tilde(cmdarg_T *cap);
138static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000139#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000141#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100142static void nv_lineop(cmdarg_T *cap);
143static void nv_home(cmdarg_T *cap);
144static void nv_pipe(cmdarg_T *cap);
145static void nv_bck_word(cmdarg_T *cap);
146static void nv_wordcmd(cmdarg_T *cap);
147static void nv_beginline(cmdarg_T *cap);
148static void adjust_cursor(oparg_T *oap);
149static void adjust_for_sel(cmdarg_T *cap);
150static int unadjust_for_sel(void);
151static void nv_select(cmdarg_T *cap);
152static void nv_goto(cmdarg_T *cap);
153static void nv_normal(cmdarg_T *cap);
154static void nv_esc(cmdarg_T *oap);
155static void nv_edit(cmdarg_T *cap);
156static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100158static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100160static void nv_record(cmdarg_T *cap);
161static void nv_at(cmdarg_T *cap);
162static void nv_halfpage(cmdarg_T *cap);
163static void nv_join(cmdarg_T *cap);
164static void nv_put(cmdarg_T *cap);
165static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100167static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100170static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000172#ifdef FEAT_AUTOCMD
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100173static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar3918c952005-03-15 22:34:55 +0000174#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100175static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000177static char *e_noident = N_("E349: No identifier under cursor");
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Function to be called for a Normal or Visual mode command.
181 * The argument is a cmdarg_T.
182 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100183typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185/* Values for cmd_flags. */
186#define NV_NCH 0x01 /* may need to get a second char */
187#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
188#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
189#define NV_LANG 0x08 /* second char needs language adjustment */
190
191#define NV_SS 0x10 /* may start selection */
192#define NV_SSS 0x20 /* may start selection with shift modifier */
193#define NV_STS 0x40 /* may stop selection without shift modif. */
194#define NV_RL 0x80 /* 'rightleft' modifies command */
195#define NV_KEEPREG 0x100 /* don't clear regname */
196#define NV_NCW 0x200 /* not allowed in command-line window */
197
198/*
199 * Generally speaking, every Normal mode command should either clear any
200 * pending operator (with *clearop*()), or set the motion type variable
201 * oap->motion_type.
202 *
203 * When a cursor motion command is made, it is marked as being a character or
204 * line oriented motion. Then, if an operator is in effect, the operation
205 * becomes character or line oriented accordingly.
206 */
207
208/*
209 * This table contains one entry for every Normal or Visual mode command.
210 * The order doesn't matter, init_normal_cmds() will create a sorted index.
211 * It is faster when all keys from zero to '~' are present.
212 */
213static const struct nv_cmd
214{
215 int cmd_char; /* (first) command character */
216 nv_func_T cmd_func; /* function for this command */
217 short_u cmd_flags; /* NV_ flags */
218 short cmd_arg; /* value for ca.arg */
219} nv_cmds[] =
220{
221 {NUL, nv_error, 0, 0},
222 {Ctrl_A, nv_addsub, 0, 0},
223 {Ctrl_B, nv_page, NV_STS, BACKWARD},
224 {Ctrl_C, nv_esc, 0, TRUE},
225 {Ctrl_D, nv_halfpage, 0, 0},
226 {Ctrl_E, nv_scroll_line, 0, TRUE},
227 {Ctrl_F, nv_page, NV_STS, FORWARD},
228 {Ctrl_G, nv_ctrlg, 0, 0},
229 {Ctrl_H, nv_ctrlh, 0, 0},
230 {Ctrl_I, nv_pcmark, 0, 0},
231 {NL, nv_down, 0, FALSE},
232 {Ctrl_K, nv_error, 0, 0},
233 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000234 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {Ctrl_N, nv_down, NV_STS, FALSE},
236 {Ctrl_O, nv_ctrlo, 0, 0},
237 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000238 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {Ctrl_R, nv_redo, 0, 0},
240 {Ctrl_S, nv_ignore, 0, 0},
241 {Ctrl_T, nv_tagpop, NV_NCW, 0},
242 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {Ctrl_V, nv_visual, 0, FALSE},
244 {'V', nv_visual, 0, FALSE},
245 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {Ctrl_W, nv_window, 0, 0},
247 {Ctrl_X, nv_addsub, 0, 0},
248 {Ctrl_Y, nv_scroll_line, 0, FALSE},
249 {Ctrl_Z, nv_suspend, 0, 0},
250 {ESC, nv_esc, 0, FALSE},
251 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
252 {Ctrl_RSB, nv_ident, NV_NCW, 0},
253 {Ctrl_HAT, nv_hat, NV_NCW, 0},
254 {Ctrl__, nv_error, 0, 0},
255 {' ', nv_right, 0, 0},
256 {'!', nv_operator, 0, 0},
257 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
258 {'#', nv_ident, 0, 0},
259 {'$', nv_dollar, 0, 0},
260 {'%', nv_percent, 0, 0},
261 {'&', nv_optrans, 0, 0},
262 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
263 {'(', nv_brace, 0, BACKWARD},
264 {')', nv_brace, 0, FORWARD},
265 {'*', nv_ident, 0, 0},
266 {'+', nv_down, 0, TRUE},
267 {',', nv_csearch, 0, TRUE},
268 {'-', nv_up, 0, TRUE},
269 {'.', nv_dot, NV_KEEPREG, 0},
270 {'/', nv_search, 0, FALSE},
271 {'0', nv_beginline, 0, 0},
272 {'1', nv_ignore, 0, 0},
273 {'2', nv_ignore, 0, 0},
274 {'3', nv_ignore, 0, 0},
275 {'4', nv_ignore, 0, 0},
276 {'5', nv_ignore, 0, 0},
277 {'6', nv_ignore, 0, 0},
278 {'7', nv_ignore, 0, 0},
279 {'8', nv_ignore, 0, 0},
280 {'9', nv_ignore, 0, 0},
281 {':', nv_colon, 0, 0},
282 {';', nv_csearch, 0, FALSE},
283 {'<', nv_operator, NV_RL, 0},
284 {'=', nv_operator, 0, 0},
285 {'>', nv_operator, NV_RL, 0},
286 {'?', nv_search, 0, FALSE},
287 {'@', nv_at, NV_NCH_NOP, FALSE},
288 {'A', nv_edit, 0, 0},
289 {'B', nv_bck_word, 0, 1},
290 {'C', nv_abbrev, NV_KEEPREG, 0},
291 {'D', nv_abbrev, NV_KEEPREG, 0},
292 {'E', nv_wordcmd, 0, TRUE},
293 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
294 {'G', nv_goto, 0, TRUE},
295 {'H', nv_scroll, 0, 0},
296 {'I', nv_edit, 0, 0},
297 {'J', nv_join, 0, 0},
298 {'K', nv_ident, 0, 0},
299 {'L', nv_scroll, 0, 0},
300 {'M', nv_scroll, 0, 0},
301 {'N', nv_next, 0, SEARCH_REV},
302 {'O', nv_open, 0, 0},
303 {'P', nv_put, 0, 0},
304 {'Q', nv_exmode, NV_NCW, 0},
305 {'R', nv_Replace, 0, FALSE},
306 {'S', nv_subst, NV_KEEPREG, 0},
307 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
308 {'U', nv_Undo, 0, 0},
309 {'W', nv_wordcmd, 0, TRUE},
310 {'X', nv_abbrev, NV_KEEPREG, 0},
311 {'Y', nv_abbrev, NV_KEEPREG, 0},
312 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
313 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
314 {'\\', nv_error, 0, 0},
315 {']', nv_brackets, NV_NCH_ALW, FORWARD},
316 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
317 {'_', nv_lineop, 0, 0},
318 {'`', nv_gomark, NV_NCH_ALW, FALSE},
319 {'a', nv_edit, NV_NCH, 0},
320 {'b', nv_bck_word, 0, 0},
321 {'c', nv_operator, 0, 0},
322 {'d', nv_operator, 0, 0},
323 {'e', nv_wordcmd, 0, FALSE},
324 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
325 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
326 {'h', nv_left, NV_RL, 0},
327 {'i', nv_edit, NV_NCH, 0},
328 {'j', nv_down, 0, FALSE},
329 {'k', nv_up, 0, FALSE},
330 {'l', nv_right, NV_RL, 0},
331 {'m', nv_mark, NV_NCH_NOP, 0},
332 {'n', nv_next, 0, 0},
333 {'o', nv_open, 0, 0},
334 {'p', nv_put, 0, 0},
335 {'q', nv_record, NV_NCH, 0},
336 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
337 {'s', nv_subst, NV_KEEPREG, 0},
338 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
339 {'u', nv_undo, 0, 0},
340 {'w', nv_wordcmd, 0, FALSE},
341 {'x', nv_abbrev, NV_KEEPREG, 0},
342 {'y', nv_operator, 0, 0},
343 {'z', nv_zet, NV_NCH_ALW, 0},
344 {'{', nv_findpar, 0, BACKWARD},
345 {'|', nv_pipe, 0, 0},
346 {'}', nv_findpar, 0, FORWARD},
347 {'~', nv_tilde, 0, 0},
348
349 /* pound sign */
350 {POUND, nv_ident, 0, 0},
351#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200352 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
353 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
354 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
355 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {K_LEFTMOUSE, nv_mouse, 0, 0},
357 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
358 {K_LEFTDRAG, nv_mouse, 0, 0},
359 {K_LEFTRELEASE, nv_mouse, 0, 0},
360 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100361 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
363 {K_MIDDLEDRAG, nv_mouse, 0, 0},
364 {K_MIDDLERELEASE, nv_mouse, 0, 0},
365 {K_RIGHTMOUSE, nv_mouse, 0, 0},
366 {K_RIGHTDRAG, nv_mouse, 0, 0},
367 {K_RIGHTRELEASE, nv_mouse, 0, 0},
368 {K_X1MOUSE, nv_mouse, 0, 0},
369 {K_X1DRAG, nv_mouse, 0, 0},
370 {K_X1RELEASE, nv_mouse, 0, 0},
371 {K_X2MOUSE, nv_mouse, 0, 0},
372 {K_X2DRAG, nv_mouse, 0, 0},
373 {K_X2RELEASE, nv_mouse, 0, 0},
374#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000375 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000376 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {K_INS, nv_edit, 0, 0},
378 {K_KINS, nv_edit, 0, 0},
379 {K_BS, nv_ctrlh, 0, 0},
380 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
381 {K_S_UP, nv_page, NV_SS, BACKWARD},
382 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
383 {K_S_DOWN, nv_page, NV_SS, FORWARD},
384 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
385 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
386 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
387 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
388 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
389 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
390 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
391 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
392 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
393 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
394 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
395 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {K_S_END, nv_end, NV_SS, FALSE},
397 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
398 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
399 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {K_S_HOME, nv_home, NV_SS, 0},
401 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
402 {K_DEL, nv_abbrev, 0, 0},
403 {K_KDEL, nv_abbrev, 0, 0},
404 {K_UNDO, nv_kundo, 0, 0},
405 {K_HELP, nv_help, NV_NCW, 0},
406 {K_F1, nv_help, NV_NCW, 0},
407 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_GUI
410 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
411 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
412#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000413#ifdef FEAT_GUI_TABLINE
414 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000415 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#ifdef FEAT_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200418 {K_F8, farsi_f8, 0, 0},
419 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#ifdef FEAT_NETBEANS_INTG
422 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
423#endif
424#ifdef FEAT_DND
425 {K_DROP, nv_drop, NV_STS, 0},
426#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000427#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000428 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000429#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100430 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431};
432
433/* Number of commands in nv_cmds[]. */
434#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
435
436/* Sorted index of commands in nv_cmds[]. */
437static short nv_cmd_idx[NV_CMDS_SIZE];
438
439/* The highest index for which
440 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
441static int nv_max_linear;
442
443/*
444 * Compare functions for qsort() below, that checks the command character
445 * through the index in nv_cmd_idx[].
446 */
447 static int
448#ifdef __BORLANDC__
449_RTLENTRYF
450#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100451nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 int c1, c2;
454
455 /* The commands are sorted on absolute value. */
456 c1 = nv_cmds[*(const short *)s1].cmd_char;
457 c2 = nv_cmds[*(const short *)s2].cmd_char;
458 if (c1 < 0)
459 c1 = -c1;
460 if (c2 < 0)
461 c2 = -c2;
462 return c1 - c2;
463}
464
465/*
466 * Initialize the nv_cmd_idx[] table.
467 */
468 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100469init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int i;
472
473 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000474 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 nv_cmd_idx[i] = i;
476
477 /* Sort the commands by the command character. */
478 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
479
480 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000481 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
483 break;
484 nv_max_linear = i - 1;
485}
486
487/*
488 * Search for a command in the commands table.
489 * Returns -1 for invalid command.
490 */
491 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 int i;
495 int idx;
496 int top, bot;
497 int c;
498
499#ifdef FEAT_MBYTE
500 /* A multi-byte character is never a command. */
501 if (cmdchar >= 0x100)
502 return -1;
503#endif
504
505 /* We use the absolute value of the character. Special keys have a
506 * negative value, but are sorted on their absolute value. */
507 if (cmdchar < 0)
508 cmdchar = -cmdchar;
509
510 /* If the character is in the first part: The character is the index into
511 * nv_cmd_idx[]. */
512 if (cmdchar <= nv_max_linear)
513 return nv_cmd_idx[cmdchar];
514
515 /* Perform a binary search. */
516 bot = nv_max_linear + 1;
517 top = NV_CMDS_SIZE - 1;
518 idx = -1;
519 while (bot <= top)
520 {
521 i = (top + bot) / 2;
522 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
523 if (c < 0)
524 c = -c;
525 if (cmdchar == c)
526 {
527 idx = nv_cmd_idx[i];
528 break;
529 }
530 if (cmdchar > c)
531 bot = i + 1;
532 else
533 top = i - 1;
534 }
535 return idx;
536}
537
538/*
539 * Execute a command in Normal mode.
540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100542normal_cmd(
543 oparg_T *oap,
544 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 cmdarg_T ca; /* command arguments */
547 int c;
548 int ctrl_w = FALSE; /* got CTRL-W command */
549 int old_col = curwin->w_curswant;
550#ifdef FEAT_CMDL_INFO
551 int need_flushbuf; /* need to call out_flush() */
552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 pos_T old_pos; /* cursor position before command */
554 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 static int old_mapped_len = 0;
556 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000557#ifdef FEAT_EVAL
558 int set_prevcount = FALSE;
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
562 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000563
564 /* Use a count remembered from before entering an operator. After typing
565 * "3d" we return from normal_cmd() and come back here, the "3" is
566 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 ca.opcount = opcount;
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 /*
570 * If there is an operator pending, then the command we take this time
571 * will terminate it. Finish_op tells us to finish the operation before
572 * returning this time (unless the operation was cancelled).
573 */
574#ifdef CURSOR_SHAPE
575 c = finish_op;
576#endif
577 finish_op = (oap->op_type != OP_NOP);
578#ifdef CURSOR_SHAPE
579 if (finish_op != c)
580 {
581 ui_cursor_shape(); /* may show different cursor shape */
582# ifdef FEAT_MOUSESHAPE
583 update_mouseshape(-1);
584# endif
585 }
586#endif
587
Bram Moolenaara983fe92008-07-31 20:04:27 +0000588 /* When not finishing an operator and no register name typed, reset the
589 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000593#ifdef FEAT_EVAL
594 set_prevcount = TRUE;
595#endif
596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaara983fe92008-07-31 20:04:27 +0000598#ifdef FEAT_AUTOCMD
599 /* Restore counts from before receiving K_CURSORHOLD. This means after
600 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
601 * "3 * 2". */
602 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
603 {
604 ca.opcount = oap->prev_opcount;
605 ca.count0 = oap->prev_count0;
606 oap->prev_opcount = 0;
607 oap->prev_count0 = 0;
608 }
609#endif
610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613 State = NORMAL_BUSY;
614#ifdef USE_ON_FLY_SCROLL
615 dont_scroll = FALSE; /* allow scrolling here */
616#endif
617
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100618#ifdef FEAT_EVAL
619 /* Set v:count here, when called from main() and not a stuffed
620 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100621 * when there is no count. Do set it for redo. */
622 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100623 set_vcount_ca(&ca, &set_prevcount);
624#endif
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 /*
627 * Get the command character from the user.
628 */
629 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100630 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 /*
633 * If a mapping was started in Visual or Select mode, remember the length
634 * of the mapping. This is used below to not return to Insert mode for as
635 * long as the mapping is being executed.
636 */
637 if (restart_edit == 0)
638 old_mapped_len = 0;
639 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000640 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 old_mapped_len = typebuf_maplen();
642
643 if (c == NUL)
644 c = K_ZERO;
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /*
647 * In Select mode, typed text replaces the selection.
648 */
649 if (VIsual_active
650 && VIsual_select
651 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
652 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000653 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
654 * 'insertmode' is set) fake a "d"elete command, Insert mode will
655 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000656 * Insert the typed character in the typeahead buffer, so that it can
657 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000658 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000659 if (restart_edit != 0)
660 c = 'd';
661 else
662 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000663 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200664 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667#ifdef FEAT_CMDL_INFO
668 need_flushbuf = add_to_showcmd(c);
669#endif
670
671getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
674 /*
675 * Handle a count before a command and compute ca.count0.
676 * Note that '0' is a command and not the start of a count, but it's
677 * part of a count after other digits.
678 */
679 while ( (c >= '1' && c <= '9')
680 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
681 {
682 if (c == K_DEL || c == K_KDEL)
683 {
684 ca.count0 /= 10;
685#ifdef FEAT_CMDL_INFO
686 del_from_showcmd(4); /* delete the digit and ~@% */
687#endif
688 }
689 else
690 ca.count0 = ca.count0 * 10 + (c - '0');
691 if (ca.count0 < 0) /* got too large! */
692 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000693#ifdef FEAT_EVAL
694 /* Set v:count here, when called from main() and not a stuffed
695 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100696 * right after the count. Do set it for redo. */
697 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100698 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (ctrl_w)
701 {
702 ++no_mapping;
703 ++allow_keys; /* no mapping for nchar, but keys */
704 }
705 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000706 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 --no_zero_mapping;
709 if (ctrl_w)
710 {
711 --no_mapping;
712 --allow_keys;
713 }
714#ifdef FEAT_CMDL_INFO
715 need_flushbuf |= add_to_showcmd(c);
716#endif
717 }
718
719 /*
720 * If we got CTRL-W there may be a/another count
721 */
722 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
723 {
724 ctrl_w = TRUE;
725 ca.opcount = ca.count0; /* remember first count */
726 ca.count0 = 0;
727 ++no_mapping;
728 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000729 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 --no_mapping;
732 --allow_keys;
733#ifdef FEAT_CMDL_INFO
734 need_flushbuf |= add_to_showcmd(c);
735#endif
736 goto getcount; /* jump back */
737 }
738 }
739
Bram Moolenaara983fe92008-07-31 20:04:27 +0000740#ifdef FEAT_AUTOCMD
741 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000743 /* Save the count values so that ca.opcount and ca.count0 are exactly
744 * the same when coming back here after handling K_CURSORHOLD. */
745 oap->prev_opcount = ca.opcount;
746 oap->prev_count0 = ca.count0;
747 }
748 else
749#endif
750 if (ca.opcount != 0)
751 {
752 /*
753 * If we're in the middle of an operator (including after entering a
754 * yank buffer with '"') AND we had a count before the operator, then
755 * that count overrides the current value of ca.count0.
756 * What this means effectively, is that commands like "3dw" get turned
757 * into "d3w" which makes things fall into place pretty neatly.
758 * If you give a count before AND after the operator, they are
759 * multiplied.
760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (ca.count0)
762 ca.count0 *= ca.opcount;
763 else
764 ca.count0 = ca.opcount;
765 }
766
767 /*
768 * Always remember the count. It will be set to zero (on the next call,
769 * above) when there is no pending operator.
770 * When called from main(), save the count for use by the "count" built-in
771 * variable.
772 */
773 ca.opcount = ca.count0;
774 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
775
776#ifdef FEAT_EVAL
777 /*
778 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100779 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100781 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000782 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#endif
784
785 /*
786 * Find the command character in the table of commands.
787 * For CTRL-W we already got nchar when looking for a count.
788 */
789 if (ctrl_w)
790 {
791 ca.nchar = c;
792 ca.cmdchar = Ctrl_W;
793 }
794 else
795 ca.cmdchar = c;
796 idx = find_command(ca.cmdchar);
797 if (idx < 0)
798 {
799 /* Not a known command: beep. */
800 clearopbeep(oap);
801 goto normal_end;
802 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000803
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000804 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200806 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000808 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 goto normal_end;
810 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000811#ifdef FEAT_AUTOCMD
812 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
813 goto normal_end;
814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 /*
817 * In Visual/Select mode, a few keys are handled in a special way.
818 */
819 if (VIsual_active)
820 {
821 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
822 if (km_stopsel
823 && (nv_cmds[idx].cmd_flags & NV_STS)
824 && !(mod_mask & MOD_MASK_SHIFT))
825 {
826 end_visual_mode();
827 redraw_curbuf_later(INVERTED);
828 }
829
830 /* Keys that work different when 'keymodel' contains "startsel" */
831 if (km_startsel)
832 {
833 if (nv_cmds[idx].cmd_flags & NV_SS)
834 {
835 unshift_special(&ca);
836 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000837 if (idx < 0)
838 {
839 /* Just in case */
840 clearopbeep(oap);
841 goto normal_end;
842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
845 && (mod_mask & MOD_MASK_SHIFT))
846 {
847 mod_mask &= ~MOD_MASK_SHIFT;
848 }
849 }
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852#ifdef FEAT_RIGHTLEFT
853 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
854 && (nv_cmds[idx].cmd_flags & NV_RL))
855 {
856 /* Invert horizontal movements and operations. Only when typed by the
857 * user directly, not when the result of a mapping or "x" translated
858 * to "dl". */
859 switch (ca.cmdchar)
860 {
861 case 'l': ca.cmdchar = 'h'; break;
862 case K_RIGHT: ca.cmdchar = K_LEFT; break;
863 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
864 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
865 case 'h': ca.cmdchar = 'l'; break;
866 case K_LEFT: ca.cmdchar = K_RIGHT; break;
867 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
868 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
869 case '>': ca.cmdchar = '<'; break;
870 case '<': ca.cmdchar = '>'; break;
871 }
872 idx = find_command(ca.cmdchar);
873 }
874#endif
875
876 /*
877 * Get an additional character if we need one.
878 */
879 if ((nv_cmds[idx].cmd_flags & NV_NCH)
880 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
881 && oap->op_type == OP_NOP)
882 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
883 || (ca.cmdchar == 'q'
884 && oap->op_type == OP_NOP
885 && !Recording
886 && !Exec_reg)
887 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100888 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
890 int *cp;
891 int repl = FALSE; /* get character for replace mode */
892 int lit = FALSE; /* get extra character literally */
893 int langmap_active = FALSE; /* using :lmap mappings */
894 int lang; /* getting a text character */
895#ifdef USE_IM_CONTROL
896 int save_smd; /* saved value of p_smd */
897#endif
898
899 ++no_mapping;
900 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000901#ifdef FEAT_AUTOCMD
902 /* Don't generate a CursorHold event here, most commands can't handle
903 * it, e.g., nv_replace(), nv_csearch(). */
904 did_cursorhold = TRUE;
905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (ca.cmdchar == 'g')
907 {
908 /*
909 * For 'g' get the next character now, so that we can check for
910 * "gr", "g'" and "g`".
911 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000912 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#ifdef FEAT_CMDL_INFO
915 need_flushbuf |= add_to_showcmd(ca.nchar);
916#endif
917 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100918 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 cp = &ca.extra_char; /* need to get a third character */
921 if (ca.nchar != 'r')
922 lit = TRUE; /* get it literally */
923 else
924 repl = TRUE; /* get it in replace mode */
925 }
926 else
927 cp = NULL; /* no third character needed */
928 }
929 else
930 {
931 if (ca.cmdchar == 'r') /* get it in replace mode */
932 repl = TRUE;
933 cp = &ca.nchar;
934 }
935 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
936
937 /*
938 * Get a second or third character.
939 */
940 if (cp != NULL)
941 {
942#ifdef CURSOR_SHAPE
943 if (repl)
944 {
945 State = REPLACE; /* pretend Replace mode */
946 ui_cursor_shape(); /* show different cursor shape */
947 }
948#endif
949 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
950 {
951 /* Allow mappings defined with ":lmap". */
952 --no_mapping;
953 --allow_keys;
954 if (repl)
955 State = LREPLACE;
956 else
957 State = LANGMAP;
958 langmap_active = TRUE;
959 }
960#ifdef USE_IM_CONTROL
961 save_smd = p_smd;
962 p_smd = FALSE; /* Don't let the IM code show the mode here */
963 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
964 im_set_active(TRUE);
965#endif
966
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000967 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 if (langmap_active)
970 {
971 /* Undo the decrement done above */
972 ++no_mapping;
973 ++allow_keys;
974 State = NORMAL_BUSY;
975 }
976#ifdef USE_IM_CONTROL
977 if (lang)
978 {
979 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
980 im_save_status(&curbuf->b_p_iminsert);
981 im_set_active(FALSE);
982 }
983 p_smd = save_smd;
984#endif
985#ifdef CURSOR_SHAPE
986 State = NORMAL_BUSY;
987#endif
988#ifdef FEAT_CMDL_INFO
989 need_flushbuf |= add_to_showcmd(*cp);
990#endif
991
992 if (!lit)
993 {
994#ifdef FEAT_DIGRAPHS
995 /* Typing CTRL-K gets a digraph. */
996 if (*cp == Ctrl_K
997 && ((nv_cmds[idx].cmd_flags & NV_LANG)
998 || cp == &ca.extra_char)
999 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1000 {
1001 c = get_digraph(FALSE);
1002 if (c > 0)
1003 {
1004 *cp = c;
1005# ifdef FEAT_CMDL_INFO
1006 /* Guessing how to update showcmd here... */
1007 del_from_showcmd(3);
1008 need_flushbuf |= add_to_showcmd(*cp);
1009# endif
1010 }
1011 }
1012#endif
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 /* adjust chars > 127, except after "tTfFr" commands */
1015 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#ifdef FEAT_RIGHTLEFT
1017 /* adjust Hebrew mapped char */
1018 if (p_hkmap && lang && KeyTyped)
1019 *cp = hkmap(*cp);
1020# ifdef FEAT_FKMAP
1021 /* adjust Farsi mapped char */
1022 if (p_fkmap && lang && KeyTyped)
1023 *cp = fkmap(*cp);
1024# endif
1025#endif
1026 }
1027
1028 /*
1029 * When the next character is CTRL-\ a following CTRL-N means the
1030 * command is aborted and we go to Normal mode.
1031 */
1032 if (cp == &ca.extra_char
1033 && ca.nchar == Ctrl_BSL
1034 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1035 {
1036 ca.cmdchar = Ctrl_BSL;
1037 ca.nchar = ca.extra_char;
1038 idx = find_command(ca.cmdchar);
1039 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001040 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001041 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 else if (*cp == Ctrl_BSL)
1043 {
1044 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1045
1046 /* There is a busy wait here when typing "f<C-\>" and then
1047 * something different from CTRL-N. Can't be avoided. */
1048 while ((c = vpeekc()) <= 0 && towait > 0L)
1049 {
1050 do_sleep(towait > 50L ? 50L : towait);
1051 towait -= 50L;
1052 }
1053 if (c > 0)
1054 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001055 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (c != Ctrl_N && c != Ctrl_G)
1057 vungetc(c);
1058 else
1059 {
1060 ca.cmdchar = Ctrl_BSL;
1061 ca.nchar = c;
1062 idx = find_command(ca.cmdchar);
1063 }
1064 }
1065 }
1066
1067#ifdef FEAT_MBYTE
1068 /* When getting a text character and the next character is a
1069 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001070 * However, don't wait for it to arrive. Also, do enable mapping,
1071 * because if it's put back with vungetc() it's too late to apply
1072 * mapping. */
1073 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 while (enc_utf8 && lang && (c = vpeekc()) > 0
1075 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1076 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001077 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (!utf_iscomposing(c))
1079 {
1080 vungetc(c); /* it wasn't, put it back */
1081 break;
1082 }
1083 else if (ca.ncharC1 == 0)
1084 ca.ncharC1 = c;
1085 else
1086 ca.ncharC2 = c;
1087 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001088 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#endif
1090 }
1091 --no_mapping;
1092 --allow_keys;
1093 }
1094
1095#ifdef FEAT_CMDL_INFO
1096 /*
1097 * Flush the showcmd characters onto the screen so we can see them while
1098 * the command is being executed. Only do this when the shown command was
1099 * actually displayed, otherwise this will slow down a lot when executing
1100 * mappings.
1101 */
1102 if (need_flushbuf)
1103 out_flush();
1104#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001105#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001106 if (ca.cmdchar != K_IGNORE)
1107 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
1110 State = NORMAL;
1111
1112 if (ca.nchar == ESC)
1113 {
1114 clearop(oap);
1115 if (restart_edit == 0 && goto_im())
1116 restart_edit = 'a';
1117 goto normal_end;
1118 }
1119
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001120 if (ca.cmdchar != K_IGNORE)
1121 {
1122 msg_didout = FALSE; /* don't scroll screen up for normal command */
1123 msg_col = 0;
1124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 old_pos = curwin->w_cursor; /* remember where cursor was */
1127
1128 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1129 * mode. */
1130 if (!VIsual_active && km_startsel)
1131 {
1132 if (nv_cmds[idx].cmd_flags & NV_SS)
1133 {
1134 start_selection();
1135 unshift_special(&ca);
1136 idx = find_command(ca.cmdchar);
1137 }
1138 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1139 && (mod_mask & MOD_MASK_SHIFT))
1140 {
1141 start_selection();
1142 mod_mask &= ~MOD_MASK_SHIFT;
1143 }
1144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
1146 /*
1147 * Execute the command!
1148 * Call the command function found in the commands table.
1149 */
1150 ca.arg = nv_cmds[idx].cmd_arg;
1151 (nv_cmds[idx].cmd_func)(&ca);
1152
1153 /*
1154 * If we didn't start or finish an operator, reset oap->regname, unless we
1155 * need it later.
1156 */
1157 if (!finish_op
1158 && !oap->op_type
1159 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1160 {
1161 clearop(oap);
1162#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001163 {
1164 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001165
Bram Moolenaar536681b2011-05-10 16:12:45 +02001166 /* Adjust the register according to 'clipboard', so that when
1167 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001168# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001169 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001170# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001171 set_reg_var(regname);
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174 }
1175
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001176 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001177 * character or "z333<cr>". */
1178 if (old_mapped_len > 0)
1179 old_mapped_len = typebuf_maplen();
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 /*
1182 * If an operation is pending, handle it...
1183 */
1184 do_pending_operator(&ca, old_col, FALSE);
1185
1186 /*
1187 * Wait for a moment when a message is displayed that will be overwritten
1188 * by the mode message.
1189 * In Visual mode and with "^O" in Insert mode, a short message will be
1190 * overwritten by the mode message. Wait a bit, until a key is hit.
1191 * In Visual mode, it's more important to keep the Visual area updated
1192 * than keeping a message (e.g. from a /pat search).
1193 * Only do this if the command was typed, not from a mapping.
1194 * Don't wait when emsg_silent is non-zero.
1195 * Also wait a bit after an error message, e.g. for "^O:".
1196 * Don't redraw the screen, it would remove the message.
1197 */
1198 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001199 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 || (VIsual_active
1202 && old_pos.lnum == curwin->w_cursor.lnum
1203 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 )
1205 && (clear_cmdline
1206 || redraw_cmdline)
1207 && (msg_didout || (msg_didany && msg_scroll))
1208 && !msg_nowait
1209 && KeyTyped)
1210 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 && (msg_scroll
1213 || emsg_on_display)))
1214 && oap->regname == 0
1215 && !(ca.retval & CA_COMMAND_BUSY)
1216 && stuff_empty()
1217 && typebuf_typed()
1218 && emsg_silent == 0
1219 && !did_wait_return
1220 && oap->op_type == OP_NOP)
1221 {
1222 int save_State = State;
1223
1224 /* Draw the cursor with the right shape here */
1225 if (restart_edit != 0)
1226 State = INSERT;
1227
1228 /* If need to redraw, and there is a "keep_msg", redraw before the
1229 * delay */
1230 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1231 {
1232 char_u *kmsg;
1233
1234 kmsg = keep_msg;
1235 keep_msg = NULL;
1236 /* showmode() will clear keep_msg, but we want to use it anyway */
1237 update_screen(0);
1238 /* now reset it, otherwise it's put in the history again */
1239 keep_msg = kmsg;
1240 msg_attr(kmsg, keep_msg_attr);
1241 vim_free(kmsg);
1242 }
1243 setcursor();
1244 cursor_on();
1245 out_flush();
1246 if (msg_scroll || emsg_on_display)
1247 ui_delay(1000L, TRUE); /* wait at least one second */
1248 ui_delay(3000L, FALSE); /* wait up to three seconds */
1249 State = save_State;
1250
1251 msg_scroll = FALSE;
1252 emsg_on_display = FALSE;
1253 }
1254
1255 /*
1256 * Finish up after executing a Normal mode command.
1257 */
1258normal_end:
1259
1260 msg_nowait = FALSE;
1261
1262 /* Reset finish_op, in case it was set */
1263#ifdef CURSOR_SHAPE
1264 c = finish_op;
1265#endif
1266 finish_op = FALSE;
1267#ifdef CURSOR_SHAPE
1268 /* Redraw the cursor with another shape, if we were in Operator-pending
1269 * mode or did a replace command. */
1270 if (c || ca.cmdchar == 'r')
1271 {
1272 ui_cursor_shape(); /* may show different cursor shape */
1273# ifdef FEAT_MOUSESHAPE
1274 update_mouseshape(-1);
1275# endif
1276 }
1277#endif
1278
1279#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001280 if (oap->op_type == OP_NOP && oap->regname == 0
1281# ifdef FEAT_AUTOCMD
1282 && ca.cmdchar != K_CURSORHOLD
1283# endif
1284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 clear_showcmd();
1286#endif
1287
1288 checkpcmark(); /* check if we moved since setting pcmark */
1289 vim_free(ca.searchbuf);
1290
1291#ifdef FEAT_MBYTE
1292 if (has_mbyte)
1293 mb_adjust_cursor();
1294#endif
1295
1296#ifdef FEAT_SCROLLBIND
1297 if (curwin->w_p_scb && toplevel)
1298 {
1299 validate_cursor(); /* may need to update w_leftcol */
1300 do_check_scrollbind(TRUE);
1301 }
1302#endif
1303
Bram Moolenaar860cae12010-06-05 23:22:07 +02001304#ifdef FEAT_CURSORBIND
1305 if (curwin->w_p_crb && toplevel)
1306 {
1307 validate_cursor(); /* may need to update w_leftcol */
1308 do_check_cursorbind();
1309 }
1310#endif
1311
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001312#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001313 /* don't go to Insert mode if a terminal has a running job */
1314 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001315 restart_edit = 0;
1316#endif
1317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 /*
1319 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1320 * if still inside a mapping that started in Visual mode).
1321 * May switch from Visual to Select mode after CTRL-O command.
1322 */
1323 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1325 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 && !(ca.retval & CA_COMMAND_BUSY)
1327 && stuff_empty()
1328 && oap->regname == 0)
1329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (restart_VIsual_select == 1)
1331 {
1332 VIsual_select = TRUE;
1333 showmode();
1334 restart_VIsual_select = 0;
1335 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001336 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 (void)edit(restart_edit, FALSE, 1L);
1338 }
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (restart_VIsual_select == 2)
1341 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 /* Save count before an operator for next time. */
1344 opcount = ca.opcount;
1345}
1346
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001347#ifdef FEAT_EVAL
1348/*
1349 * Set v:count and v:count1 according to "cap".
1350 * Set v:prevcount only when "set_prevcount" is TRUE.
1351 */
1352 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001353set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001354{
1355 long count = cap->count0;
1356
1357 /* multiply with cap->opcount the same way as above */
1358 if (cap->opcount != 0)
1359 count = cap->opcount * (count == 0 ? 1 : count);
1360 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1361 *set_prevcount = FALSE; /* only set v:prevcount once */
1362}
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365/*
1366 * Handle an operator after visual mode or when the movement is finished
1367 */
1368 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001369do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 oparg_T *oap = cap->oap;
1372 pos_T old_cursor;
1373 int empty_region_error;
1374 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001375#ifdef FEAT_LINEBREAK
1376 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /* The visual area is remembered for redo */
1380 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1381 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001382 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001384 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001385#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388
1389#if defined(FEAT_CLIPBOARD)
1390 /*
1391 * Yank the visual area into the GUI selection register before we operate
1392 * on it and lose it forever.
1393 * Don't do it if a specific register was specified, so that ""x"*P works.
1394 * This could call do_pending_operator() recursively, but that's OK
1395 * because gui_yank will be TRUE for the nested call.
1396 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001397 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 && oap->op_type != OP_NOP
1399 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 && VIsual_active
1401 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 && oap->regname == 0)
1403 clip_auto_select();
1404#endif
1405 old_cursor = curwin->w_cursor;
1406
1407 /*
1408 * If an operation is pending, handle it...
1409 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001410 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001412#ifdef FEAT_LINEBREAK
1413 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001414 if (curwin->w_p_lbr)
1415 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001416 curwin->w_p_lbr = FALSE;
1417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 oap->is_VIsual = VIsual_active;
1419 if (oap->motion_force == 'V')
1420 oap->motion_type = MLINE;
1421 else if (oap->motion_force == 'v')
1422 {
1423 /* If the motion was linewise, "inclusive" will not have been set.
1424 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1425 if (oap->motion_type == MLINE)
1426 oap->inclusive = FALSE;
1427 /* If the motion already was characterwise, toggle "inclusive" */
1428 else if (oap->motion_type == MCHAR)
1429 oap->inclusive = !oap->inclusive;
1430 oap->motion_type = MCHAR;
1431 }
1432 else if (oap->motion_force == Ctrl_V)
1433 {
1434 /* Change line- or characterwise motion into Visual block mode. */
1435 VIsual_active = TRUE;
1436 VIsual = oap->start;
1437 VIsual_mode = Ctrl_V;
1438 VIsual_select = FALSE;
1439 VIsual_reselect = FALSE;
1440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar7afea822013-04-24 18:34:45 +02001442 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1443 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001445 && ((!VIsual_active || oap->motion_force)
1446 /* Also redo Operator-pending Visual mode mappings */
1447 || (VIsual_active && cap->cmdchar == ':'
1448 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001449 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450#ifdef FEAT_FOLDING
1451 && oap->op_type != OP_FOLD
1452 && oap->op_type != OP_FOLDOPEN
1453 && oap->op_type != OP_FOLDOPENREC
1454 && oap->op_type != OP_FOLDCLOSE
1455 && oap->op_type != OP_FOLDCLOSEREC
1456 && oap->op_type != OP_FOLDDEL
1457 && oap->op_type != OP_FOLDDELREC
1458#endif
1459 )
1460 {
1461 prep_redo(oap->regname, cap->count0,
1462 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1463 oap->motion_force, cap->cmdchar, cap->nchar);
1464 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1465 {
1466 /*
1467 * If 'cpoptions' does not contain 'r', insert the search
1468 * pattern to really repeat the same command.
1469 */
1470 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001471 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 AppendToRedobuff(NL_STR);
1473 }
1474 else if (cap->cmdchar == ':')
1475 {
1476 /* do_cmdline() has stored the first typed line in
1477 * "repeat_cmdline". When several lines are typed repeating
1478 * won't be possible. */
1479 if (repeat_cmdline == NULL)
1480 ResetRedobuff();
1481 else
1482 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001483 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 AppendToRedobuff(NL_STR);
1485 vim_free(repeat_cmdline);
1486 repeat_cmdline = NULL;
1487 }
1488 }
1489 }
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (redo_VIsual_busy)
1492 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001493 /* Redo of an operation on a Visual area. Use the same size from
1494 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 oap->start = curwin->w_cursor;
1496 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1497 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1498 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1499 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001500 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001502 if (VIsual_mode == 'v')
1503 {
1504 if (redo_VIsual_line_count <= 1)
1505 {
1506 validate_virtcol();
1507 curwin->w_curswant =
1508 curwin->w_virtcol + redo_VIsual_vcol - 1;
1509 }
1510 else
1511 curwin->w_curswant = redo_VIsual_vcol;
1512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001514 {
1515 curwin->w_curswant = MAXCOL;
1516 }
1517 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519 cap->count0 = redo_VIsual_count;
1520 if (redo_VIsual_count != 0)
1521 cap->count1 = redo_VIsual_count;
1522 else
1523 cap->count1 = 1;
1524 }
1525 else if (VIsual_active)
1526 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001527 if (!gui_yank)
1528 {
1529 /* Save the current VIsual area for '< and '> marks, and "gv" */
1530 curbuf->b_visual.vi_start = VIsual;
1531 curbuf->b_visual.vi_end = curwin->w_cursor;
1532 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001533 if (VIsual_mode_orig != NUL)
1534 {
1535 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1536 VIsual_mode_orig = NUL;
1537 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001538 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001540 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001545 * characterwise selection.
1546 * Special case: gH<Del> deletes the last line. */
1547 if (VIsual_select && VIsual_mode == 'V'
1548 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001550 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552 VIsual.col = 0;
1553 curwin->w_cursor.col =
1554 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1555 }
1556 else
1557 {
1558 curwin->w_cursor.col = 0;
1559 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1560 }
1561 VIsual_mode = 'v';
1562 }
1563 /* If 'selection' is "exclusive", backup one character for
1564 * charwise selections. */
1565 else if (VIsual_mode == 'v')
1566 {
1567# ifdef FEAT_VIRTUALEDIT
1568 include_line_break =
1569# endif
1570 unadjust_for_sel();
1571 }
1572
1573 oap->start = VIsual;
1574 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001577# ifdef FEAT_VIRTUALEDIT
1578 oap->start.coladd = 0;
1579# endif
1580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
1583 /*
1584 * Set oap->start to the first position of the operated text, oap->end
1585 * to the end of the operated text. w_cursor is equal to oap->start.
1586 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001587 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 {
1589#ifdef FEAT_FOLDING
1590 /* Include folded lines completely. */
1591 if (!VIsual_active)
1592 {
1593 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1594 oap->start.col = 0;
1595 if (hasFolding(curwin->w_cursor.lnum, NULL,
1596 &curwin->w_cursor.lnum))
1597 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1598 }
1599#endif
1600 oap->end = curwin->w_cursor;
1601 curwin->w_cursor = oap->start;
1602
1603 /* w_virtcol may have been updated; if the cursor goes back to its
1604 * previous position w_virtcol becomes invalid and isn't updated
1605 * automatically. */
1606 curwin->w_valid &= ~VALID_VIRTCOL;
1607 }
1608 else
1609 {
1610#ifdef FEAT_FOLDING
1611 /* Include folded lines completely. */
1612 if (!VIsual_active && oap->motion_type == MLINE)
1613 {
1614 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1615 NULL))
1616 curwin->w_cursor.col = 0;
1617 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1618 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1619 }
1620#endif
1621 oap->end = oap->start;
1622 oap->start = curwin->w_cursor;
1623 }
1624
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001625 /* Just in case lines were deleted that make the position invalid. */
1626 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1628
1629#ifdef FEAT_VIRTUALEDIT
1630 /* Set "virtual_op" before resetting VIsual_active. */
1631 virtual_op = virtual_active();
1632#endif
1633
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (VIsual_active || redo_VIsual_busy)
1635 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001636 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 if (!redo_VIsual_busy && !gui_yank)
1639 {
1640 /*
1641 * Prepare to reselect and redo Visual: this is based on the
1642 * size of the Visual text
1643 */
1644 resel_VIsual_mode = VIsual_mode;
1645 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001646 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001648 {
1649 if (VIsual_mode != Ctrl_V)
1650 getvvcol(curwin, &(oap->end),
1651 NULL, NULL, &oap->end_vcol);
1652 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1653 {
1654 if (VIsual_mode != Ctrl_V)
1655 getvvcol(curwin, &(oap->start),
1656 &oap->start_vcol, NULL, NULL);
1657 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1658 }
1659 else
1660 resel_VIsual_vcol = oap->end_vcol;
1661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 resel_VIsual_line_count = oap->line_count;
1663 }
1664
1665 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1666 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1667 && oap->op_type != OP_COLON
1668#ifdef FEAT_FOLDING
1669 && oap->op_type != OP_FOLD
1670 && oap->op_type != OP_FOLDOPEN
1671 && oap->op_type != OP_FOLDOPENREC
1672 && oap->op_type != OP_FOLDCLOSE
1673 && oap->op_type != OP_FOLDCLOSEREC
1674 && oap->op_type != OP_FOLDDEL
1675 && oap->op_type != OP_FOLDDELREC
1676#endif
1677 && oap->motion_force == NUL
1678 )
1679 {
1680 /* Prepare for redoing. Only use the nchar field for "r",
1681 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001682 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1683 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001684 prep_redo(oap->regname, cap->count0,
1685 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1686 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001687 else if (cap->cmdchar != ':')
Bram Moolenaar641e2862012-07-25 15:06:34 +02001688 prep_redo(oap->regname, 0L, NUL, 'v',
1689 get_op_char(oap->op_type),
1690 get_extra_op_char(oap->op_type),
1691 oap->op_type == OP_REPLACE
1692 ? cap->nchar : NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (!redo_VIsual_busy)
1694 {
1695 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001696 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 redo_VIsual_line_count = resel_VIsual_line_count;
1698 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001699 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 }
1701 }
1702
1703 /*
1704 * oap->inclusive defaults to TRUE.
1705 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1706 * FALSE. This makes "d}P" and "v}dP" work the same.
1707 */
1708 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1709 oap->inclusive = TRUE;
1710 if (VIsual_mode == 'V')
1711 oap->motion_type = MLINE;
1712 else
1713 {
1714 oap->motion_type = MCHAR;
1715 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001716#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 )
1720 {
1721 oap->inclusive = FALSE;
1722 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001723 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001724 if (*p_sel != 'o'
1725 && !op_on_lines(oap->op_type)
1726 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001728 ++oap->end.lnum;
1729 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001730#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001731 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001732#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001733 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 }
1735 }
1736 }
1737
1738 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 /*
1741 * Switch Visual off now, so screen updating does
1742 * not show inverted text when the screen is redrawn.
1743 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1744 * no screen redraw, so it is done here to remove the inverted
1745 * part.
1746 */
1747 if (!gui_yank)
1748 {
1749 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001750#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 setmouse();
1752 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001753#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001754 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if ((oap->op_type == OP_YANK
1756 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001757 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 || oap->op_type == OP_FILTER)
1759 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001760 {
1761#ifdef FEAT_LINEBREAK
1762 /* make sure redrawing is correct */
1763 curwin->w_p_lbr = lbr_saved;
1764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
1768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
1770#ifdef FEAT_MBYTE
1771 /* Include the trailing byte of a multi-byte char. */
1772 if (has_mbyte && oap->inclusive)
1773 {
1774 int l;
1775
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001776 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 if (l > 1)
1778 oap->end.col += l - 1;
1779 }
1780#endif
1781 curwin->w_set_curswant = TRUE;
1782
1783 /*
1784 * oap->empty is set when start and end are the same. The inclusive
1785 * flag affects this too, unless yanking and the end is on a NUL.
1786 */
1787 oap->empty = (oap->motion_type == MCHAR
1788 && (!oap->inclusive
1789 || (oap->op_type == OP_YANK
1790 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001791 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#ifdef FEAT_VIRTUALEDIT
1793 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1794#endif
1795 );
1796 /*
1797 * For delete, change and yank, it's an error to operate on an
1798 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1799 */
1800 empty_region_error = (oap->empty
1801 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 /* Force a redraw when operating on an empty Visual region, when
1804 * 'modifiable is off or creating a fold. */
1805 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001806#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001810 {
1811#ifdef FEAT_LINEBREAK
1812 curwin->w_p_lbr = lbr_saved;
1813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 /*
1818 * If the end of an operator is in column one while oap->motion_type
1819 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1820 * character in the previous line. If op_start is on or before the
1821 * first non-blank in the line, the operator becomes linewise
1822 * (strange, but that's the way vi does it).
1823 */
1824 if ( oap->motion_type == MCHAR
1825 && oap->inclusive == FALSE
1826 && !(cap->retval & CA_NO_ADJ_OP_END)
1827 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001829 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 && oap->line_count > 1)
1831 {
1832 oap->end_adjusted = TRUE; /* remember that we did this */
1833 --oap->line_count;
1834 --oap->end.lnum;
1835 if (inindent(0))
1836 oap->motion_type = MLINE;
1837 else
1838 {
1839 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1840 if (oap->end.col)
1841 {
1842 --oap->end.col;
1843 oap->inclusive = TRUE;
1844 }
1845 }
1846 }
1847 else
1848 oap->end_adjusted = FALSE;
1849
1850 switch (oap->op_type)
1851 {
1852 case OP_LSHIFT:
1853 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001854 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 auto_format(FALSE, TRUE);
1856 break;
1857
1858 case OP_JOIN_NS:
1859 case OP_JOIN:
1860 if (oap->line_count < 2)
1861 oap->line_count = 2;
1862 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1863 curbuf->b_ml.ml_line_count)
1864 beep_flush();
1865 else
1866 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001867 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001868 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 auto_format(FALSE, TRUE);
1870 }
1871 break;
1872
1873 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001876 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001877 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001878 CancelRedo();
1879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 else
1881 {
1882 (void)op_delete(oap);
1883 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1884 u_save_cursor(); /* cursor line wasn't saved yet */
1885 auto_format(FALSE, TRUE);
1886 }
1887 break;
1888
1889 case OP_YANK:
1890 if (empty_region_error)
1891 {
1892 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001893 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001894 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001895 CancelRedo();
1896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 }
1898 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001899 {
1900#ifdef FEAT_LINEBREAK
1901 curwin->w_p_lbr = lbr_saved;
1902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 check_cursor_col();
1906 break;
1907
1908 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001911 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001912 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001913 CancelRedo();
1914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 else
1916 {
1917 /* This is a new edit command, not a restart. Need to
1918 * remember it to make 'insertmode' work with mappings for
1919 * Visual mode. But do this only once and not when typed and
1920 * 'insertmode' isn't set. */
1921 if (p_im || !KeyTyped)
1922 restart_edit_save = restart_edit;
1923 else
1924 restart_edit_save = 0;
1925 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001926#ifdef FEAT_LINEBREAK
1927 /* Restore linebreak, so that when the user edits it looks as
1928 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001929 if (curwin->w_p_lbr != lbr_saved)
1930 {
1931 curwin->w_p_lbr = lbr_saved;
1932 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1933 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 /* Reset finish_op now, don't want it set inside edit(). */
1936 finish_op = FALSE;
1937 if (op_change(oap)) /* will call edit() */
1938 cap->retval |= CA_COMMAND_BUSY;
1939 if (restart_edit == 0)
1940 restart_edit = restart_edit_save;
1941 }
1942 break;
1943
1944 case OP_FILTER:
1945 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1946 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1947 else
1948 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001949 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950
1951 case OP_INDENT:
1952 case OP_COLON:
1953
1954#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1955 /*
1956 * If 'equalprg' is empty, do the indenting internally.
1957 */
1958 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1959 {
1960# ifdef FEAT_LISP
1961 if (curbuf->b_p_lisp)
1962 {
1963 op_reindent(oap, get_lisp_indent);
1964 break;
1965 }
1966# endif
1967# ifdef FEAT_CINDENT
1968 op_reindent(oap,
1969# ifdef FEAT_EVAL
1970 *curbuf->b_p_inde != NUL ? get_expr_indent :
1971# endif
1972 get_c_indent);
1973 break;
1974# endif
1975 }
1976#endif
1977
1978 op_colon(oap);
1979 break;
1980
1981 case OP_TILDE:
1982 case OP_UPPER:
1983 case OP_LOWER:
1984 case OP_ROT13:
1985 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001986 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001987 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001988 CancelRedo();
1989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 else
1991 op_tilde(oap);
1992 check_cursor_col();
1993 break;
1994
1995 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001996#if defined(FEAT_EVAL)
1997 if (*curbuf->b_p_fex != NUL)
1998 op_formatexpr(oap); /* use expression */
1999 else
2000#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002001 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 op_colon(oap); /* use external command */
2003 else
2004 op_format(oap, FALSE); /* use internal function */
2005 break;
2006
2007 case OP_FORMAT2:
2008 op_format(oap, TRUE); /* use internal function */
2009 break;
2010
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002011 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01002012#ifdef FEAT_LINEBREAK
2013 /* Restore linebreak, so that when the user edits it looks as
2014 * before. */
2015 curwin->w_p_lbr = lbr_saved;
2016#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002017 op_function(oap); /* call 'operatorfunc' */
2018 break;
2019
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 case OP_INSERT:
2021 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023#ifdef FEAT_VISUALEXTRA
2024 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002025 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002026 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002027 CancelRedo();
2028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 else
2030 {
2031 /* This is a new edit command, not a restart. Need to
2032 * remember it to make 'insertmode' work with mappings for
2033 * Visual mode. But do this only once. */
2034 restart_edit_save = restart_edit;
2035 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002036#ifdef FEAT_LINEBREAK
2037 /* Restore linebreak, so that when the user edits it looks as
2038 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002039 if (curwin->w_p_lbr != lbr_saved)
2040 {
2041 curwin->w_p_lbr = lbr_saved;
2042 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2043 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002046#ifdef FEAT_LINEBREAK
2047 /* Reset linebreak, so that formatting works correctly. */
2048 curwin->w_p_lbr = FALSE;
2049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 /* TODO: when inserting in several lines, should format all
2052 * the lines. */
2053 auto_format(FALSE, TRUE);
2054
2055 if (restart_edit == 0)
2056 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002057 else
2058 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 }
2060#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002061 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062#endif
2063 break;
2064
2065 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067#ifdef FEAT_VISUALEXTRA
2068 if (empty_region_error)
2069#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002070 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002071 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002072 CancelRedo();
2073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074#ifdef FEAT_VISUALEXTRA
2075 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002076 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002077# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002078 /* Restore linebreak, so that when the user edits it looks as
2079 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002080 if (curwin->w_p_lbr != lbr_saved)
2081 {
2082 curwin->w_p_lbr = lbr_saved;
2083 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2084 }
2085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#endif
2089 break;
2090
2091#ifdef FEAT_FOLDING
2092 case OP_FOLD:
2093 VIsual_reselect = FALSE; /* don't reselect now */
2094 foldCreate(oap->start.lnum, oap->end.lnum);
2095 break;
2096
2097 case OP_FOLDOPEN:
2098 case OP_FOLDOPENREC:
2099 case OP_FOLDCLOSE:
2100 case OP_FOLDCLOSEREC:
2101 VIsual_reselect = FALSE; /* don't reselect now */
2102 opFoldRange(oap->start.lnum, oap->end.lnum,
2103 oap->op_type == OP_FOLDOPEN
2104 || oap->op_type == OP_FOLDOPENREC,
2105 oap->op_type == OP_FOLDOPENREC
2106 || oap->op_type == OP_FOLDCLOSEREC,
2107 oap->is_VIsual);
2108 break;
2109
2110 case OP_FOLDDEL:
2111 case OP_FOLDDELREC:
2112 VIsual_reselect = FALSE; /* don't reselect now */
2113 deleteFold(oap->start.lnum, oap->end.lnum,
2114 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2115 break;
2116#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002117 case OP_NR_ADD:
2118 case OP_NR_SUB:
2119 if (empty_region_error)
2120 {
2121 vim_beep(BO_OPER);
2122 CancelRedo();
2123 }
2124 else
2125 {
2126 VIsual_active = TRUE;
2127#ifdef FEAT_LINEBREAK
2128 curwin->w_p_lbr = lbr_saved;
2129#endif
2130 op_addsub(oap, cap->count1, redo_VIsual_arg);
2131 VIsual_active = FALSE;
2132 }
2133 check_cursor_col();
2134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 default:
2136 clearopbeep(oap);
2137 }
2138#ifdef FEAT_VIRTUALEDIT
2139 virtual_op = MAYBE;
2140#endif
2141 if (!gui_yank)
2142 {
2143 /*
2144 * if 'sol' not set, go back to old column for some commands
2145 */
2146 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2147 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2148 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002149 {
2150#ifdef FEAT_LINEBREAK
2151 curwin->w_p_lbr = FALSE;
2152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 }
2156 else
2157 {
2158 curwin->w_cursor = old_cursor;
2159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 clearop(oap);
2162 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002163#ifdef FEAT_LINEBREAK
2164 curwin->w_p_lbr = lbr_saved;
2165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166}
2167
2168/*
2169 * Handle indent and format operators and visual mode ":".
2170 */
2171 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002172op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
2174 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (oap->is_VIsual)
2176 stuffReadbuff((char_u *)"'<,'>");
2177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
2179 /*
2180 * Make the range look nice, so it can be repeated.
2181 */
2182 if (oap->start.lnum == curwin->w_cursor.lnum)
2183 stuffcharReadbuff('.');
2184 else
2185 stuffnumReadbuff((long)oap->start.lnum);
2186 if (oap->end.lnum != oap->start.lnum)
2187 {
2188 stuffcharReadbuff(',');
2189 if (oap->end.lnum == curwin->w_cursor.lnum)
2190 stuffcharReadbuff('.');
2191 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2192 stuffcharReadbuff('$');
2193 else if (oap->start.lnum == curwin->w_cursor.lnum)
2194 {
2195 stuffReadbuff((char_u *)".+");
2196 stuffnumReadbuff((long)oap->line_count - 1);
2197 }
2198 else
2199 stuffnumReadbuff((long)oap->end.lnum);
2200 }
2201 }
2202 if (oap->op_type != OP_COLON)
2203 stuffReadbuff((char_u *)"!");
2204 if (oap->op_type == OP_INDENT)
2205 {
2206#ifndef FEAT_CINDENT
2207 if (*get_equalprg() == NUL)
2208 stuffReadbuff((char_u *)"indent");
2209 else
2210#endif
2211 stuffReadbuff(get_equalprg());
2212 stuffReadbuff((char_u *)"\n");
2213 }
2214 else if (oap->op_type == OP_FORMAT)
2215 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002216 if (*curbuf->b_p_fp != NUL)
2217 stuffReadbuff(curbuf->b_p_fp);
2218 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002220 else
2221 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002222 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224
2225 /*
2226 * do_cmdline() does the rest
2227 */
2228}
2229
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002230/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002231 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002232 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002233 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002234op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002235{
2236#ifdef FEAT_EVAL
2237 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002238# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002239 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002240# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002241
2242 if (*p_opfunc == NUL)
2243 EMSG(_("E774: 'operatorfunc' is empty"));
2244 else
2245 {
2246 /* Set '[ and '] marks to text to be operated on. */
2247 curbuf->b_op_start = oap->start;
2248 curbuf->b_op_end = oap->end;
2249 if (oap->motion_type != MLINE && !oap->inclusive)
2250 /* Exclude the end position. */
2251 decl(&curbuf->b_op_end);
2252
2253 if (oap->block_mode)
2254 argv[0] = (char_u *)"block";
2255 else if (oap->motion_type == MLINE)
2256 argv[0] = (char_u *)"line";
2257 else
2258 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002259
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002260# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002261 /* Reset virtual_op so that 'virtualedit' can be changed in the
2262 * function. */
2263 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002264# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002265
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002266 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002267
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002268# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002269 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002270# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002271 }
2272#else
2273 EMSG(_("E775: Eval feature not available"));
2274#endif
2275}
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#if defined(FEAT_MOUSE) || defined(PROTO)
2278/*
2279 * Do the appropriate action for the current mouse click in the current mode.
2280 * Not used for Command-line mode.
2281 *
2282 * Normal Mode:
2283 * event modi- position visual change action
2284 * fier cursor window
2285 * left press - yes end yes
2286 * left press C yes end yes "^]" (2)
2287 * left press S yes end yes "*" (2)
2288 * left drag - yes start if moved no
2289 * left relse - yes start if moved no
2290 * middle press - yes if not active no put register
2291 * middle press - yes if active no yank and put
2292 * right press - yes start or extend yes
2293 * right press S yes no change yes "#" (2)
2294 * right drag - yes extend no
2295 * right relse - yes extend no
2296 *
2297 * Insert or Replace Mode:
2298 * event modi- position visual change action
2299 * fier cursor window
2300 * left press - yes (cannot be active) yes
2301 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2302 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2303 * left drag - yes start or extend (1) no CTRL-O (1)
2304 * left relse - yes start or extend (1) no CTRL-O (1)
2305 * middle press - no (cannot be active) no put register
2306 * right press - yes start or extend yes CTRL-O
2307 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2308 *
2309 * (1) only if mouse pointer moved since press
2310 * (2) only if click is in same buffer
2311 *
2312 * Return TRUE if start_arrow() should be called for edit mode.
2313 */
2314 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002315do_mouse(
2316 oparg_T *oap, /* operator argument, can be NULL */
2317 int c, /* K_LEFTMOUSE, etc */
2318 int dir, /* Direction to 'put' if necessary */
2319 long count,
2320 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
2322 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2323 static int got_click = FALSE; /* got a click some time back */
2324
2325 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2326 int is_click; /* If FALSE it's a drag or release event */
2327 int is_drag; /* If TRUE it's a drag event */
2328 int jump_flags = 0; /* flags for jump_to_mouse() */
2329 pos_T start_visual;
2330 int moved; /* Has cursor moved? */
2331 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002332 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 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
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002400 if (c == K_MOUSEMOVE)
2401 {
2402 /* Mouse moved without a button pressed. */
2403#ifdef FEAT_BEVALTERM
2404 ui_may_remove_balloon();
2405 if (p_bevalterm && !VIsual_active)
2406 {
2407 profile_setlimit(p_bdlay, &bevalexpr_due);
2408 bevalexpr_due_set = TRUE;
2409 }
2410#endif
2411 return FALSE;
2412 }
2413
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#ifdef FEAT_MOUSESHAPE
2415 /* May have stopped dragging the status or separator line. The pointer is
2416 * most likely still on the status or separator line. */
2417 if (!is_drag && drag_status_line)
2418 {
2419 drag_status_line = FALSE;
2420 update_mouseshape(SHAPE_IDX_STATUS);
2421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (!is_drag && drag_sep_line)
2423 {
2424 drag_sep_line = FALSE;
2425 update_mouseshape(SHAPE_IDX_VSEP);
2426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427#endif
2428
2429 /*
2430 * Ignore drag and release events if we didn't get a click.
2431 */
2432 if (is_click)
2433 got_click = TRUE;
2434 else
2435 {
2436 if (!got_click) /* didn't get click, ignore */
2437 return FALSE;
2438 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002439 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002441 if (in_tab_line)
2442 {
2443 in_tab_line = FALSE;
2444 return FALSE;
2445 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 /*
2450 * CTRL right mouse button does CTRL-T
2451 */
2452 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2453 {
2454 if (State & INSERT)
2455 stuffcharReadbuff(Ctrl_O);
2456 if (count > 1)
2457 stuffnumReadbuff(count);
2458 stuffcharReadbuff(Ctrl_T);
2459 got_click = FALSE; /* ignore drag&release now */
2460 return FALSE;
2461 }
2462
2463 /*
2464 * CTRL only works with left mouse button
2465 */
2466 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2467 return FALSE;
2468
2469 /*
2470 * When a modifier is down, ignore drag and release events, as well as
2471 * multiple clicks and the middle mouse button.
2472 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2473 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002474 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2475 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 && (!is_click
2477 || (mod_mask & MOD_MASK_MULTI_CLICK)
2478 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002479 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 && mouse_model_popup()
2481 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002482 && !((mod_mask & MOD_MASK_ALT)
2483 && !mouse_model_popup()
2484 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 )
2486 return FALSE;
2487
2488 /*
2489 * If the button press was used as the movement command for an operator
2490 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2491 * drag/release events.
2492 */
2493 if (!is_click && which_button == MOUSE_MIDDLE)
2494 return FALSE;
2495
2496 if (oap != NULL)
2497 regname = oap->regname;
2498 else
2499 regname = 0;
2500
2501 /*
2502 * Middle mouse button does a 'put' of the selected text
2503 */
2504 if (which_button == MOUSE_MIDDLE)
2505 {
2506 if (State == NORMAL)
2507 {
2508 /*
2509 * If an operator was pending, we don't know what the user wanted
2510 * to do. Go back to normal mode: Clear the operator and beep().
2511 */
2512 if (oap != NULL && oap->op_type != OP_NOP)
2513 {
2514 clearopbeep(oap);
2515 return FALSE;
2516 }
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 /*
2519 * If visual was active, yank the highlighted text and put it
2520 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002521 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
2523 if (VIsual_active)
2524 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002525 if (VIsual_select)
2526 {
2527 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002528 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002529 }
2530 else
2531 {
2532 stuffcharReadbuff('y');
2533 stuffcharReadbuff(K_MIDDLEMOUSE);
2534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 do_always = TRUE; /* ignore 'mouse' setting next time */
2536 return FALSE;
2537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 /*
2539 * The rest is below jump_to_mouse()
2540 */
2541 }
2542
2543 else if ((State & INSERT) == 0)
2544 return FALSE;
2545
2546 /*
2547 * Middle click in insert mode doesn't move the mouse, just insert the
2548 * contents of a register. '.' register is special, can't insert that
2549 * with do_put().
2550 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2551 * happens for the GUI).
2552 */
2553 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2554 {
2555 if (regname == '.')
2556 insert_reg(regname, TRUE);
2557 else
2558 {
2559#ifdef FEAT_CLIPBOARD
2560 if (clip_star.available && regname == 0)
2561 regname = '*';
2562#endif
2563 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2564 insert_reg(regname, TRUE);
2565 else
2566 {
2567 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2568
2569 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2570 AppendCharToRedobuff(Ctrl_R);
2571 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2572 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2573 }
2574 }
2575 return FALSE;
2576 }
2577 }
2578
2579 /* When dragging or button-up stay in the same window. */
2580 if (!is_click)
2581 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2582
2583 start_visual.lnum = 0;
2584
Bram Moolenaarf740b292006-02-16 22:11:02 +00002585 /* Check for clicking in the tab page line. */
2586 if (mouse_row == 0 && firstwin->w_winrow > 0)
2587 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002588 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002589 {
2590 if (in_tab_line)
2591 {
2592 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002593 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2594 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002595 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002596 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002597 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002598
Bram Moolenaarf740b292006-02-16 22:11:02 +00002599 /* click in a tab selects that tab page */
2600 if (is_click
2601# ifdef FEAT_CMDWIN
2602 && cmdwin_type == 0
2603# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002604 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002605 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002606 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002607 c1 = TabPageIdxs[mouse_col];
2608 if (c1 >= 0)
2609 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002610 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2611 {
2612 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002613 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002614 tabpage_new();
2615 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2616 }
2617 else
2618 {
2619 /* Go to specified tab page, or next one if not clicking
2620 * on a label. */
2621 goto_tabpage(c1);
2622
2623 /* It's like clicking on the status line of a window. */
2624 if (curwin != old_curwin)
2625 end_visual_mode();
2626 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002627 }
2628 else if (c1 < 0)
2629 {
2630 tabpage_T *tp;
2631
2632 /* Close the current or specified tab page. */
2633 if (c1 == -999)
2634 tp = curtab;
2635 else
2636 tp = find_tabpage(-c1);
2637 if (tp == curtab)
2638 {
2639 if (first_tabpage->tp_next != NULL)
2640 tabpage_close(FALSE);
2641 }
2642 else if (tp != NULL)
2643 tabpage_close_other(tp, FALSE);
2644 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002645 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002646 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002647 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002648 else if (is_drag && in_tab_line)
2649 {
2650 c1 = TabPageIdxs[mouse_col];
2651 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2652 return FALSE;
2653 }
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 /*
2656 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2657 * right button up -> pop-up menu
2658 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002659 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 */
2661 if (mouse_model_popup())
2662 {
2663 if (which_button == MOUSE_RIGHT
2664 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2665 {
2666 /*
2667 * NOTE: Ignore right button down and drag mouse events.
2668 * Windows only shows the popup menu on the button up event.
2669 */
Bram Moolenaar968bbbe2006-08-16 19:41:08 +00002670#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2671 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (!is_click)
2673 return FALSE;
2674#endif
2675#if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2676 if (is_click || is_drag)
2677 return FALSE;
2678#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002679#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2681 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2682 if (gui.in_use)
2683 {
2684 jump_flags = 0;
2685 if (STRCMP(p_mousem, "popup_setpos") == 0)
2686 {
2687 /* First set the cursor position before showing the popup
2688 * menu. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 if (VIsual_active)
2690 {
2691 pos_T m_pos;
2692
2693 /*
2694 * set MOUSE_MAY_STOP_VIS if we are outside the
2695 * selection or the current window (might have false
2696 * negative here)
2697 */
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002698 if (mouse_row < curwin->w_winrow
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 || mouse_row
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02002700 > (curwin->w_winrow + curwin->w_height))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 jump_flags = MOUSE_MAY_STOP_VIS;
2702 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2703 jump_flags = MOUSE_MAY_STOP_VIS;
2704 else
2705 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002706 if ((LT_POS(curwin->w_cursor, VIsual)
2707 && (LT_POS(m_pos, curwin->w_cursor)
2708 || LT_POS(VIsual, m_pos)))
2709 || (LT_POS(VIsual, curwin->w_cursor)
2710 && (LT_POS(m_pos, VIsual)
2711 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
2713 jump_flags = MOUSE_MAY_STOP_VIS;
2714 }
2715 else if (VIsual_mode == Ctrl_V)
2716 {
2717 getvcols(curwin, &curwin->w_cursor, &VIsual,
2718 &leftcol, &rightcol);
2719 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2720 if (m_pos.col < leftcol || m_pos.col > rightcol)
2721 jump_flags = MOUSE_MAY_STOP_VIS;
2722 }
2723 }
2724 }
2725 else
2726 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
2728 if (jump_flags)
2729 {
2730 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002731 update_curbuf(VIsual_active ? INVERTED : VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 setcursor();
2733 out_flush(); /* Update before showing popup menu */
2734 }
2735# ifdef FEAT_MENU
2736 gui_show_popupmenu();
2737# endif
2738 return (jump_flags & CURSOR_MOVED) != 0;
2739 }
2740 else
2741 return FALSE;
2742#else
2743 return FALSE;
2744#endif
2745 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002746 if (which_button == MOUSE_LEFT
2747 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
2749 which_button = MOUSE_RIGHT;
2750 mod_mask &= ~MOD_MASK_SHIFT;
2751 }
2752 }
2753
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 if ((State & (NORMAL | INSERT))
2755 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2756 {
2757 if (which_button == MOUSE_LEFT)
2758 {
2759 if (is_click)
2760 {
2761 /* stop Visual mode for a left click in a window, but not when
2762 * on a status line */
2763 if (VIsual_active)
2764 jump_flags |= MOUSE_MAY_STOP_VIS;
2765 }
2766 else if (mouse_has(MOUSE_VISUAL))
2767 jump_flags |= MOUSE_MAY_VIS;
2768 }
2769 else if (which_button == MOUSE_RIGHT)
2770 {
2771 if (is_click && VIsual_active)
2772 {
2773 /*
2774 * Remember the start and end of visual before moving the
2775 * cursor.
2776 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002777 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
2779 start_visual = curwin->w_cursor;
2780 end_visual = VIsual;
2781 }
2782 else
2783 {
2784 start_visual = VIsual;
2785 end_visual = curwin->w_cursor;
2786 }
2787 }
2788 jump_flags |= MOUSE_FOCUS;
2789 if (mouse_has(MOUSE_VISUAL))
2790 jump_flags |= MOUSE_MAY_VIS;
2791 }
2792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 /*
2795 * If an operator is pending, ignore all drags and releases until the
2796 * next mouse click.
2797 */
2798 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2799 {
2800 got_click = FALSE;
2801 oap->motion_type = MCHAR;
2802 }
2803
2804 /* When releasing the button let jump_to_mouse() know. */
2805 if (!is_click && !is_drag)
2806 jump_flags |= MOUSE_RELEASED;
2807
2808 /*
2809 * JUMP!
2810 */
2811 jump_flags = jump_to_mouse(jump_flags,
2812 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002813
2814#ifdef FEAT_MENU
2815 /* A click in the window toolbar has no side effects. */
2816 if (jump_flags & MOUSE_WINBAR)
2817 return FALSE;
2818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 moved = (jump_flags & CURSOR_MOVED);
2820 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
2823#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002824 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2826 {
2827 int key = KEY2TERMCAP1(c);
2828
2829 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2830 || key == (int)KE_RIGHTRELEASE)
2831 netbeans_button_release(which_button);
2832 }
2833#endif
2834
2835 /* When jumping to another window, clear a pending operator. That's a bit
2836 * friendlier than beeping and not jumping to that window. */
2837 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2838 clearop(oap);
2839
2840#ifdef FEAT_FOLDING
2841 if (mod_mask == 0
2842 && !is_drag
2843 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2844 && which_button == MOUSE_LEFT)
2845 {
2846 /* open or close a fold at this line */
2847 if (jump_flags & MOUSE_FOLD_OPEN)
2848 openFold(curwin->w_cursor.lnum, 1L);
2849 else
2850 closeFold(curwin->w_cursor.lnum, 1L);
2851 /* don't move the cursor if still in the same window */
2852 if (curwin == old_curwin)
2853 curwin->w_cursor = save_cursor;
2854 }
2855#endif
2856
2857#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2858 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2859 {
2860 clip_modeless(which_button, is_click, is_drag);
2861 return FALSE;
2862 }
2863#endif
2864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002866 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (VIsual_active && is_drag && p_so)
2868 {
2869 /* In the very first line, allow scrolling one line */
2870 if (mouse_row == 0)
2871 mouse_dragging = 2;
2872 else
2873 mouse_dragging = 1;
2874 }
2875
2876 /* When dragging the mouse above the window, scroll down. */
2877 if (is_drag && mouse_row < 0 && !in_status_line)
2878 {
2879 scroll_redraw(FALSE, 1L);
2880 mouse_row = 0;
2881 }
2882
2883 if (start_visual.lnum) /* right click in visual mode */
2884 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002885 /* When ALT is pressed make Visual mode blockwise. */
2886 if (mod_mask & MOD_MASK_ALT)
2887 VIsual_mode = Ctrl_V;
2888
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 /*
2890 * In Visual-block mode, divide the area in four, pick up the corner
2891 * that is in the quarter that the cursor is in.
2892 */
2893 if (VIsual_mode == Ctrl_V)
2894 {
2895 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2896 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2897 end_visual.col = leftcol;
2898 else
2899 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002900 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 end_visual.lnum = start_visual.lnum;
2903
2904 /* move VIsual to the right column */
2905 start_visual = curwin->w_cursor; /* save the cursor pos */
2906 curwin->w_cursor = end_visual;
2907 coladvance(end_visual.col);
2908 VIsual = curwin->w_cursor;
2909 curwin->w_cursor = start_visual; /* restore the cursor */
2910 }
2911 else
2912 {
2913 /*
2914 * If the click is before the start of visual, change the start.
2915 * If the click is after the end of visual, change the end. If
2916 * the click is inside the visual, change the closest side.
2917 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002918 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002920 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 VIsual = start_visual;
2922 else
2923 {
2924 /* In the same line, compare column number */
2925 if (end_visual.lnum == start_visual.lnum)
2926 {
2927 if (curwin->w_cursor.col - start_visual.col >
2928 end_visual.col - curwin->w_cursor.col)
2929 VIsual = start_visual;
2930 else
2931 VIsual = end_visual;
2932 }
2933
2934 /* In different lines, compare line number */
2935 else
2936 {
2937 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2938 (end_visual.lnum - curwin->w_cursor.lnum);
2939
2940 if (diff > 0) /* closest to end */
2941 VIsual = start_visual;
2942 else if (diff < 0) /* closest to start */
2943 VIsual = end_visual;
2944 else /* in the middle line */
2945 {
2946 if (curwin->w_cursor.col <
2947 (start_visual.col + end_visual.col) / 2)
2948 VIsual = end_visual;
2949 else
2950 VIsual = start_visual;
2951 }
2952 }
2953 }
2954 }
2955 }
2956 /*
2957 * If Visual mode started in insert mode, execute "CTRL-O"
2958 */
2959 else if ((State & INSERT) && VIsual_active)
2960 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962 /*
2963 * Middle mouse click: Put text before cursor.
2964 */
2965 if (which_button == MOUSE_MIDDLE)
2966 {
2967#ifdef FEAT_CLIPBOARD
2968 if (clip_star.available && regname == 0)
2969 regname = '*';
2970#endif
2971 if (yank_register_mline(regname))
2972 {
2973 if (mouse_past_bottom)
2974 dir = FORWARD;
2975 }
2976 else if (mouse_past_eol)
2977 dir = FORWARD;
2978
2979 if (fixindent)
2980 {
2981 c1 = (dir == BACKWARD) ? '[' : ']';
2982 c2 = 'p';
2983 }
2984 else
2985 {
2986 c1 = (dir == FORWARD) ? 'p' : 'P';
2987 c2 = NUL;
2988 }
2989 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2990
2991 /*
2992 * Remember where the paste started, so in edit() Insstart can be set
2993 * to this position
2994 */
2995 if (restart_edit != 0)
2996 where_paste_started = curwin->w_cursor;
2997 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2998 }
2999
Bram Moolenaar4033c552017-09-16 20:54:51 +02003000#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 /*
3002 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3003 * error under the mouse pointer.
3004 */
3005 else if (((mod_mask & MOD_MASK_CTRL)
3006 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3007 && bt_quickfix(curbuf))
3008 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003009 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003010 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003011 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003012 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 got_click = FALSE; /* ignore drag&release now */
3014 }
3015#endif
3016
3017 /*
3018 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3019 * under the mouse pointer.
3020 */
3021 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3022 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3023 {
3024 if (State & INSERT)
3025 stuffcharReadbuff(Ctrl_O);
3026 stuffcharReadbuff(Ctrl_RSB);
3027 got_click = FALSE; /* ignore drag&release now */
3028 }
3029
3030 /*
3031 * Shift-Mouse click searches for the next occurrence of the word under
3032 * the mouse pointer
3033 */
3034 else if ((mod_mask & MOD_MASK_SHIFT))
3035 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003036 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 stuffcharReadbuff(Ctrl_O);
3038 if (which_button == MOUSE_LEFT)
3039 stuffcharReadbuff('*');
3040 else /* MOUSE_RIGHT */
3041 stuffcharReadbuff('#');
3042 }
3043
3044 /* Handle double clicks, unless on status line */
3045 else if (in_status_line)
3046 {
3047#ifdef FEAT_MOUSESHAPE
3048 if ((is_drag || is_click) && !drag_status_line)
3049 {
3050 drag_status_line = TRUE;
3051 update_mouseshape(-1);
3052 }
3053#endif
3054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 else if (in_sep_line)
3056 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003057#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 if ((is_drag || is_click) && !drag_sep_line)
3059 {
3060 drag_sep_line = TRUE;
3061 update_mouseshape(-1);
3062 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003063#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3066 && mouse_has(MOUSE_VISUAL))
3067 {
3068 if (is_click || !VIsual_active)
3069 {
3070 if (VIsual_active)
3071 orig_cursor = VIsual;
3072 else
3073 {
3074 check_visual_highlight();
3075 VIsual = curwin->w_cursor;
3076 orig_cursor = VIsual;
3077 VIsual_active = TRUE;
3078 VIsual_reselect = TRUE;
3079 /* start Select mode if 'selectmode' contains "mouse" */
3080 may_start_select('o');
3081 setmouse();
3082 }
3083 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003084 {
3085 /* Double click with ALT pressed makes it blockwise. */
3086 if (mod_mask & MOD_MASK_ALT)
3087 VIsual_mode = Ctrl_V;
3088 else
3089 VIsual_mode = 'v';
3090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3092 VIsual_mode = 'V';
3093 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3094 VIsual_mode = Ctrl_V;
3095#ifdef FEAT_CLIPBOARD
3096 /* Make sure the clipboard gets updated. Needed because start and
3097 * end may still be the same, and the selection needs to be owned */
3098 clip_star.vmode = NUL;
3099#endif
3100 }
3101 /*
3102 * A double click selects a word or a block.
3103 */
3104 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3105 {
3106 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003107 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109 if (is_click)
3110 {
3111 /* If the character under the cursor (skipping white space) is
3112 * not a word character, try finding a match and select a (),
3113 * {}, [], #if/#endif, etc. block. */
3114 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003115 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 inc(&end_visual);
3117 if (oap != NULL)
3118 oap->motion_type = MCHAR;
3119 if (oap != NULL
3120 && VIsual_mode == 'v'
3121 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003122 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 && (pos = findmatch(oap, NUL)) != NULL)
3124 {
3125 curwin->w_cursor = *pos;
3126 if (oap->motion_type == MLINE)
3127 VIsual_mode = 'V';
3128 else if (*p_sel == 'e')
3129 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003130 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 ++VIsual.col;
3132 else
3133 ++curwin->w_cursor.col;
3134 }
3135 }
3136 }
3137
3138 if (pos == NULL && (is_click || is_drag))
3139 {
3140 /* When not found a match or when dragging: extend to include
3141 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003142 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
3144 find_start_of_word(&curwin->w_cursor);
3145 find_end_of_word(&VIsual);
3146 }
3147 else
3148 {
3149 find_start_of_word(&VIsual);
3150 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3151#ifdef FEAT_MBYTE
3152 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003153 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154#else
3155 ++curwin->w_cursor.col;
3156#endif
3157 find_end_of_word(&curwin->w_cursor);
3158 }
3159 }
3160 curwin->w_set_curswant = TRUE;
3161 }
3162 if (is_click)
3163 redraw_curbuf_later(INVERTED); /* update the inversion */
3164 }
3165 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003166 {
3167 if (mod_mask & MOD_MASK_ALT)
3168 VIsual_mode = Ctrl_V;
3169 else
3170 VIsual_mode = 'v';
3171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172
3173 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003174 if ((!VIsual_active && old_active && mode_displayed)
3175 || (VIsual_active && p_smd && msg_silent == 0
3176 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 return moved;
3180}
3181
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182/*
3183 * Move "pos" back to the start of the word it's in.
3184 */
3185 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003186find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
3188 char_u *line;
3189 int cclass;
3190 int col;
3191
3192 line = ml_get(pos->lnum);
3193 cclass = get_mouse_class(line + pos->col);
3194
3195 while (pos->col > 0)
3196 {
3197 col = pos->col - 1;
3198#ifdef FEAT_MBYTE
3199 col -= (*mb_head_off)(line, line + col);
3200#endif
3201 if (get_mouse_class(line + col) != cclass)
3202 break;
3203 pos->col = col;
3204 }
3205}
3206
3207/*
3208 * Move "pos" forward to the end of the word it's in.
3209 * When 'selection' is "exclusive", the position is just after the word.
3210 */
3211 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003212find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
3214 char_u *line;
3215 int cclass;
3216 int col;
3217
3218 line = ml_get(pos->lnum);
3219 if (*p_sel == 'e' && pos->col > 0)
3220 {
3221 --pos->col;
3222#ifdef FEAT_MBYTE
3223 pos->col -= (*mb_head_off)(line, line + pos->col);
3224#endif
3225 }
3226 cclass = get_mouse_class(line + pos->col);
3227 while (line[pos->col] != NUL)
3228 {
3229#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003230 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231#else
3232 col = pos->col + 1;
3233#endif
3234 if (get_mouse_class(line + col) != cclass)
3235 {
3236 if (*p_sel == 'e')
3237 pos->col = col;
3238 break;
3239 }
3240 pos->col = col;
3241 }
3242}
3243
3244/*
3245 * Get class of a character for selection: same class means same word.
3246 * 0: blank
3247 * 1: punctuation groups
3248 * 2: normal word character
3249 * >2: multi-byte word character.
3250 */
3251 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003252get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 int c;
3255
3256#ifdef FEAT_MBYTE
3257 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3258 return mb_get_class(p);
3259#endif
3260
3261 c = *p;
3262 if (c == ' ' || c == '\t')
3263 return 0;
3264
3265 if (vim_iswordc(c))
3266 return 2;
3267
3268 /*
3269 * There are a few special cases where we want certain combinations of
3270 * characters to be considered as a single word. These are things like
3271 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003272 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 */
3274 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3275 return 1;
3276 return c;
3277}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278#endif /* FEAT_MOUSE */
3279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280/*
3281 * Check if highlighting for visual mode is possible, give a warning message
3282 * if not.
3283 */
3284 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003285check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 static int did_check = FALSE;
3288
3289 if (full_screen)
3290 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003291 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 MSG(_("Warning: terminal cannot highlight"));
3293 did_check = TRUE;
3294 }
3295}
3296
3297/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003298 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 * This function should ALWAYS be called to end Visual mode, except from
3300 * do_pending_operator().
3301 */
3302 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003303end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305#ifdef FEAT_CLIPBOARD
3306 /*
3307 * If we are using the clipboard, then remember what was selected in case
3308 * we need to paste it somewhere while we still own the selection.
3309 * Only do this when the clipboard is already owned. Don't want to grab
3310 * the selection when hitting ESC.
3311 */
3312 if (clip_star.available && clip_star.owned)
3313 clip_auto_select();
3314#endif
3315
3316 VIsual_active = FALSE;
3317#ifdef FEAT_MOUSE
3318 setmouse();
3319 mouse_dragging = 0;
3320#endif
3321
3322 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003323 curbuf->b_visual.vi_mode = VIsual_mode;
3324 curbuf->b_visual.vi_start = VIsual;
3325 curbuf->b_visual.vi_end = curwin->w_cursor;
3326 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#ifdef FEAT_EVAL
3328 curbuf->b_visual_mode_eval = VIsual_mode;
3329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#ifdef FEAT_VIRTUALEDIT
3331 if (!virtual_active())
3332 curwin->w_cursor.coladd = 0;
3333#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003334 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003336 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337}
3338
3339/*
3340 * Reset VIsual_active and VIsual_reselect.
3341 */
3342 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003343reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 if (VIsual_active)
3346 {
3347 end_visual_mode();
3348 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3349 }
3350 VIsual_reselect = FALSE;
3351}
3352
3353/*
3354 * Reset VIsual_active and VIsual_reselect if it's set.
3355 */
3356 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003357reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 if (VIsual_active)
3360 {
3361 end_visual_mode();
3362 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3363 VIsual_reselect = FALSE;
3364 }
3365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367/*
3368 * Check for a balloon-eval special item to include when searching for an
3369 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3370 * Returns TRUE if the character at "*ptr" should be included.
3371 * "dir" is FORWARD or BACKWARD, the direction of searching.
3372 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3373 * "bnp" points to a counter for square brackets.
3374 */
3375 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003376find_is_eval_item(
3377 char_u *ptr,
3378 int *colp,
3379 int *bnp,
3380 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 /* Accept everything inside []. */
3383 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3384 ++*bnp;
3385 if (*bnp > 0)
3386 {
3387 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3388 --*bnp;
3389 return TRUE;
3390 }
3391
3392 /* skip over "s.var" */
3393 if (*ptr == '.')
3394 return TRUE;
3395
3396 /* two-character item: s->var */
3397 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3398 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3399 {
3400 *colp += dir;
3401 return TRUE;
3402 }
3403 return FALSE;
3404}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
3406/*
3407 * Find the identifier under or to the right of the cursor.
3408 * "find_type" can have one of three values:
3409 * FIND_IDENT: find an identifier (keyword)
3410 * FIND_STRING: find any non-white string
3411 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003412 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 *
3414 * There are three steps:
3415 * 1. Search forward for the start of an identifier/string. Doesn't move if
3416 * already on one.
3417 * 2. Search backward for the start of this identifier/string.
3418 * This doesn't match the real Vi but I like it a little better and it
3419 * shouldn't bother anyone.
3420 * 3. Search forward to the end of this identifier/string.
3421 * When FIND_IDENT isn't defined, we backup until a blank.
3422 *
3423 * Returns the length of the string, or zero if no string is found.
3424 * If a string is found, a pointer to the string is put in "*string". This
3425 * string is not always NUL terminated.
3426 */
3427 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003428find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
3430 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3431 curwin->w_cursor.col, string, find_type);
3432}
3433
3434/*
3435 * Like find_ident_under_cursor(), but for any window and any position.
3436 * However: Uses 'iskeyword' from the current window!.
3437 */
3438 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003439find_ident_at_pos(
3440 win_T *wp,
3441 linenr_T lnum,
3442 colnr_T startcol,
3443 char_u **string,
3444 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 char_u *ptr;
3447 int col = 0; /* init to shut up GCC */
3448 int i;
3449#ifdef FEAT_MBYTE
3450 int this_class = 0;
3451 int prev_class;
3452 int prevcol;
3453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 /*
3457 * if i == 0: try to find an identifier
3458 * if i == 1: try to find any non-white string
3459 */
3460 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3461 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3462 {
3463 /*
3464 * 1. skip to start of identifier/string
3465 */
3466 col = startcol;
3467#ifdef FEAT_MBYTE
3468 if (has_mbyte)
3469 {
3470 while (ptr[col] != NUL)
3471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 /* Stop at a ']' to evaluate "a[x]". */
3473 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3474 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 this_class = mb_get_class(ptr + col);
3476 if (this_class != 0 && (i == 1 || this_class != 1))
3477 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003478 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 }
3481 else
3482#endif
3483 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003484 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 )
3487 ++col;
3488
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 /* When starting on a ']' count it, so that we include the '['. */
3490 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 /*
3493 * 2. Back up to start of identifier/string.
3494 */
3495#ifdef FEAT_MBYTE
3496 if (has_mbyte)
3497 {
3498 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3500 this_class = mb_get_class((char_u *)"a");
3501 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003503 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3506 prev_class = mb_get_class(ptr + prevcol);
3507 if (this_class != prev_class
3508 && (i == 0
3509 || prev_class == 0
3510 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 && (!(find_type & FIND_EVAL)
3512 || prevcol == 0
3513 || !find_is_eval_item(ptr + prevcol, &prevcol,
3514 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 )
3516 break;
3517 col = prevcol;
3518 }
3519
3520 /* If we don't want just any old string, or we've found an
3521 * identifier, stop searching. */
3522 if (this_class > 2)
3523 this_class = 2;
3524 if (!(find_type & FIND_STRING) || this_class == 2)
3525 break;
3526 }
3527 else
3528#endif
3529 {
3530 while (col > 0
3531 && ((i == 0
3532 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003533 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 && (!(find_type & FIND_IDENT)
3535 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 || ((find_type & FIND_EVAL)
3537 && col > 1
3538 && find_is_eval_item(ptr + col - 1, &col,
3539 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 ))
3541 --col;
3542
3543 /* If we don't want just any old string, or we've found an
3544 * identifier, stop searching. */
3545 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3546 break;
3547 }
3548 }
3549
3550 if (ptr[col] == NUL || (i == 0 && (
3551#ifdef FEAT_MBYTE
3552 has_mbyte ? this_class != 2 :
3553#endif
3554 !vim_iswordc(ptr[col]))))
3555 {
3556 /*
3557 * didn't find an identifier or string
3558 */
3559 if (find_type & FIND_STRING)
3560 EMSG(_("E348: No string under cursor"));
3561 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003562 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 return 0;
3564 }
3565 ptr += col;
3566 *string = ptr;
3567
3568 /*
3569 * 3. Find the end if the identifier/string.
3570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 bn = 0;
3572 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 col = 0;
3574#ifdef FEAT_MBYTE
3575 if (has_mbyte)
3576 {
3577 /* Search for point of changing multibyte character class. */
3578 this_class = mb_get_class(ptr);
3579 while (ptr[col] != NUL
3580 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3581 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 || ((find_type & FIND_EVAL)
3583 && col <= (int)startcol
3584 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003586 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 else
3589#endif
3590 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003591 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 || ((find_type & FIND_EVAL)
3593 && col <= (int)startcol
3594 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 )
3596 {
3597 ++col;
3598 }
3599
3600 return col;
3601}
3602
3603/*
3604 * Prepare for redo of a normal command.
3605 */
3606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003607prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 prep_redo(cap->oap->regname, cap->count0,
3610 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3611}
3612
3613/*
3614 * Prepare for redo of any command.
3615 * Note that only the last argument can be a multi-byte char.
3616 */
3617 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003618prep_redo(
3619 int regname,
3620 long num,
3621 int cmd1,
3622 int cmd2,
3623 int cmd3,
3624 int cmd4,
3625 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 ResetRedobuff();
3628 if (regname != 0) /* yank from specified buffer */
3629 {
3630 AppendCharToRedobuff('"');
3631 AppendCharToRedobuff(regname);
3632 }
3633 if (num)
3634 AppendNumberToRedobuff(num);
3635
3636 if (cmd1 != NUL)
3637 AppendCharToRedobuff(cmd1);
3638 if (cmd2 != NUL)
3639 AppendCharToRedobuff(cmd2);
3640 if (cmd3 != NUL)
3641 AppendCharToRedobuff(cmd3);
3642 if (cmd4 != NUL)
3643 AppendCharToRedobuff(cmd4);
3644 if (cmd5 != NUL)
3645 AppendCharToRedobuff(cmd5);
3646}
3647
3648/*
3649 * check for operator active and clear it
3650 *
3651 * return TRUE if operator was active
3652 */
3653 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003654checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 if (oap->op_type == OP_NOP)
3657 return FALSE;
3658 clearopbeep(oap);
3659 return TRUE;
3660}
3661
3662/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003663 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003665 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 */
3667 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003668checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003670 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 return FALSE;
3672 clearopbeep(oap);
3673 return TRUE;
3674}
3675
3676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003677clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 oap->op_type = OP_NOP;
3680 oap->regname = 0;
3681 oap->motion_force = NUL;
3682 oap->use_reg_one = FALSE;
3683}
3684
3685 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003686clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 clearop(oap);
3689 beep_flush();
3690}
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692/*
3693 * Remove the shift modifier from a special key.
3694 */
3695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003696unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 switch (cap->cmdchar)
3699 {
3700 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3701 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3702 case K_S_UP: cap->cmdchar = K_UP; break;
3703 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3704 case K_S_HOME: cap->cmdchar = K_HOME; break;
3705 case K_S_END: cap->cmdchar = K_END; break;
3706 }
3707 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003710/*
3711 * If the mode is currently displayed clear the command line or update the
3712 * command displayed.
3713 */
3714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003715may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003716{
3717 if (mode_displayed)
3718 clear_cmdline = TRUE; /* unshow visual mode later */
3719#ifdef FEAT_CMDL_INFO
3720 else
3721 clear_showcmd();
3722#endif
3723}
3724
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3726/*
3727 * Routines for displaying a partly typed command
3728 */
3729
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003730#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731static char_u showcmd_buf[SHOWCMD_BUFLEN];
3732static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3733static int showcmd_is_clear = TRUE;
3734static int showcmd_visual = FALSE;
3735
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003736static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003739clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
3741 if (!p_sc)
3742 return;
3743
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (VIsual_active && !char_avail())
3745 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003746 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 long lines;
3748 colnr_T leftcol, rightcol;
3749 linenr_T top, bot;
3750
3751 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003752 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
3754 top = VIsual.lnum;
3755 bot = curwin->w_cursor.lnum;
3756 }
3757 else
3758 {
3759 top = curwin->w_cursor.lnum;
3760 bot = VIsual.lnum;
3761 }
3762# ifdef FEAT_FOLDING
3763 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003764 (void)hasFolding(top, &top, NULL);
3765 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766# endif
3767 lines = bot - top + 1;
3768
3769 if (VIsual_mode == Ctrl_V)
3770 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003771# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003772 char_u *saved_sbr = p_sbr;
3773
3774 /* Make 'sbr' empty for a moment to get the correct size. */
3775 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003778# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003779 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003780# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3782 (long)(rightcol - leftcol + 1));
3783 }
3784 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3785 sprintf((char *)showcmd_buf, "%ld", lines);
3786 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003787 {
3788 char_u *s, *e;
3789 int l;
3790 int bytes = 0;
3791 int chars = 0;
3792
3793 if (cursor_bot)
3794 {
3795 s = ml_get_pos(&VIsual);
3796 e = ml_get_cursor();
3797 }
3798 else
3799 {
3800 s = ml_get_cursor();
3801 e = ml_get_pos(&VIsual);
3802 }
3803 while ((*p_sel != 'e') ? s <= e : s < e)
3804 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003805# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003806 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003807# else
3808 l = (*s == NUL) ? 0 : 1;
3809# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003810 if (l == 0)
3811 {
3812 ++bytes;
3813 ++chars;
3814 break; /* end of line */
3815 }
3816 bytes += l;
3817 ++chars;
3818 s += l;
3819 }
3820 if (bytes == chars)
3821 sprintf((char *)showcmd_buf, "%d", chars);
3822 else
3823 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3826 showcmd_visual = TRUE;
3827 }
3828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 {
3830 showcmd_buf[0] = NUL;
3831 showcmd_visual = FALSE;
3832
3833 /* Don't actually display something if there is nothing to clear. */
3834 if (showcmd_is_clear)
3835 return;
3836 }
3837
3838 display_showcmd();
3839}
3840
3841/*
3842 * Add 'c' to string of shown command chars.
3843 * Return TRUE if output has been written (and setcursor() has been called).
3844 */
3845 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003846add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847{
3848 char_u *p;
3849 int old_len;
3850 int extra_len;
3851 int overflow;
3852#if defined(FEAT_MOUSE)
3853 int i;
3854 static int ignore[] =
3855 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003856# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3858 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003859# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003860 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003861 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3863 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003864 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003866 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 0
3868 };
3869#endif
3870
Bram Moolenaar09df3122006-01-23 22:23:09 +00003871 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 return FALSE;
3873
3874 if (showcmd_visual)
3875 {
3876 showcmd_buf[0] = NUL;
3877 showcmd_visual = FALSE;
3878 }
3879
3880#if defined(FEAT_MOUSE)
3881 /* Ignore keys that are scrollbar updates and mouse clicks */
3882 if (IS_SPECIAL(c))
3883 for (i = 0; ignore[i] != 0; ++i)
3884 if (ignore[i] == c)
3885 return FALSE;
3886#endif
3887
3888 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003889 if (*p == ' ')
3890 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 old_len = (int)STRLEN(showcmd_buf);
3892 extra_len = (int)STRLEN(p);
3893 overflow = old_len + extra_len - SHOWCMD_COLS;
3894 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003895 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3896 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 STRCAT(showcmd_buf, p);
3898
3899 if (char_avail())
3900 return FALSE;
3901
3902 display_showcmd();
3903
3904 return TRUE;
3905}
3906
3907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003908add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
3910 if (!add_to_showcmd(c))
3911 setcursor();
3912}
3913
3914/*
3915 * Delete 'len' characters from the end of the shown command.
3916 */
3917 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003918del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
3920 int old_len;
3921
3922 if (!p_sc)
3923 return;
3924
3925 old_len = (int)STRLEN(showcmd_buf);
3926 if (len > old_len)
3927 len = old_len;
3928 showcmd_buf[old_len - len] = NUL;
3929
3930 if (!char_avail())
3931 display_showcmd();
3932}
3933
3934/*
3935 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3936 * something and there is a partial mapping.
3937 */
3938 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003939push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941 if (p_sc)
3942 STRCPY(old_showcmd_buf, showcmd_buf);
3943}
3944
3945 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003946pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
3948 if (!p_sc)
3949 return;
3950
3951 STRCPY(showcmd_buf, old_showcmd_buf);
3952
3953 display_showcmd();
3954}
3955
3956 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003957display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
3959 int len;
3960
3961 cursor_off();
3962
3963 len = (int)STRLEN(showcmd_buf);
3964 if (len == 0)
3965 showcmd_is_clear = TRUE;
3966 else
3967 {
3968 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3969 showcmd_is_clear = FALSE;
3970 }
3971
3972 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003973 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3974 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 */
3976 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3977
3978 setcursor(); /* put cursor back where it belongs */
3979}
3980#endif
3981
3982#ifdef FEAT_SCROLLBIND
3983/*
3984 * When "check" is FALSE, prepare for commands that scroll the window.
3985 * When "check" is TRUE, take care of scroll-binding after the window has
3986 * scrolled. Called from normal_cmd() and edit().
3987 */
3988 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003989do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
3991 static win_T *old_curwin = NULL;
3992 static linenr_T old_topline = 0;
3993#ifdef FEAT_DIFF
3994 static int old_topfill = 0;
3995#endif
3996 static buf_T *old_buf = NULL;
3997 static colnr_T old_leftcol = 0;
3998
3999 if (check && curwin->w_p_scb)
4000 {
4001 /* If a ":syncbind" command was just used, don't scroll, only reset
4002 * the values. */
4003 if (did_syncbind)
4004 did_syncbind = FALSE;
4005 else if (curwin == old_curwin)
4006 {
4007 /*
4008 * Synchronize other windows, as necessary according to
4009 * 'scrollbind'. Don't do this after an ":edit" command, except
4010 * when 'diff' is set.
4011 */
4012 if ((curwin->w_buffer == old_buf
4013#ifdef FEAT_DIFF
4014 || curwin->w_p_diff
4015#endif
4016 )
4017 && (curwin->w_topline != old_topline
4018#ifdef FEAT_DIFF
4019 || curwin->w_topfill != old_topfill
4020#endif
4021 || curwin->w_leftcol != old_leftcol))
4022 {
4023 check_scrollbind(curwin->w_topline - old_topline,
4024 (long)(curwin->w_leftcol - old_leftcol));
4025 }
4026 }
4027 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4028 {
4029 /*
4030 * When switching between windows, make sure that the relative
4031 * vertical offset is valid for the new window. The relative
4032 * offset is invalid whenever another 'scrollbind' window has
4033 * scrolled to a point that would force the current window to
4034 * scroll past the beginning or end of its buffer. When the
4035 * resync is performed, some of the other 'scrollbind' windows may
4036 * need to jump so that the current window's relative position is
4037 * visible on-screen.
4038 */
4039 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4040 }
4041 curwin->w_scbind_pos = curwin->w_topline;
4042 }
4043
4044 old_curwin = curwin;
4045 old_topline = curwin->w_topline;
4046#ifdef FEAT_DIFF
4047 old_topfill = curwin->w_topfill;
4048#endif
4049 old_buf = curwin->w_buffer;
4050 old_leftcol = curwin->w_leftcol;
4051}
4052
4053/*
4054 * Synchronize any windows that have "scrollbind" set, based on the
4055 * number of rows by which the current window has changed
4056 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4057 */
4058 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004059check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
4061 int want_ver;
4062 int want_hor;
4063 win_T *old_curwin = curwin;
4064 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 int old_VIsual_select = VIsual_select;
4066 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 colnr_T tgt_leftcol = curwin->w_leftcol;
4068 long topline;
4069 long y;
4070
4071 /*
4072 * check 'scrollopt' string for vertical and horizontal scroll options
4073 */
4074 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4075#ifdef FEAT_DIFF
4076 want_ver |= old_curwin->w_p_diff;
4077#endif
4078 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4079
4080 /*
4081 * loop through the scrollbound windows and scroll accordingly
4082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004084 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 {
4086 curbuf = curwin->w_buffer;
4087 /* skip original window and windows with 'noscrollbind' */
4088 if (curwin != old_curwin && curwin->w_p_scb)
4089 {
4090 /*
4091 * do the vertical scroll
4092 */
4093 if (want_ver)
4094 {
4095#ifdef FEAT_DIFF
4096 if (old_curwin->w_p_diff && curwin->w_p_diff)
4097 {
4098 diff_set_topline(old_curwin, curwin);
4099 }
4100 else
4101#endif
4102 {
4103 curwin->w_scbind_pos += topline_diff;
4104 topline = curwin->w_scbind_pos;
4105 if (topline > curbuf->b_ml.ml_line_count)
4106 topline = curbuf->b_ml.ml_line_count;
4107 if (topline < 1)
4108 topline = 1;
4109
4110 y = topline - curwin->w_topline;
4111 if (y > 0)
4112 scrollup(y, FALSE);
4113 else
4114 scrolldown(-y, FALSE);
4115 }
4116
4117 redraw_later(VALID);
4118 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
4121
4122 /*
4123 * do the horizontal scroll
4124 */
4125 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4126 {
4127 curwin->w_leftcol = tgt_leftcol;
4128 leftcol_changed();
4129 }
4130 }
4131 }
4132
4133 /*
4134 * reset current-window
4135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 VIsual_select = old_VIsual_select;
4137 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 curwin = old_curwin;
4139 curbuf = old_curbuf;
4140}
4141#endif /* #ifdef FEAT_SCROLLBIND */
4142
4143/*
4144 * Command character that's ignored.
4145 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004146 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004149nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004151 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152}
4153
4154/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004155 * Command character that doesn't do anything, but unlike nv_ignore() does
4156 * start edit(). Used for "startinsert" executed while starting up.
4157 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004158 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004159nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004160{
4161}
4162
4163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 * Command character doesn't exist.
4165 */
4166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004167nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
4169 clearopbeep(cap->oap);
4170}
4171
4172/*
4173 * <Help> and <F1> commands.
4174 */
4175 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004176nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
4178 if (!checkclearopq(cap->oap))
4179 ex_help(NULL);
4180}
4181
4182/*
4183 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4184 */
4185 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004186nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
Bram Moolenaard79e5502016-01-10 22:13:02 +01004188 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004189 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004190 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004191 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4192 op_addsub(cap->oap, cap->count1, cap->arg);
4193 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004194 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004195 else if (VIsual_active)
4196 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004197 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004198 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199}
4200
4201/*
4202 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4203 */
4204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004205nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004208 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004209 if (mod_mask & MOD_MASK_CTRL)
4210 {
4211 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4212 if (cap->arg == BACKWARD)
4213 goto_tabpage(-(int)cap->count1);
4214 else
4215 goto_tabpage((int)cap->count0);
4216 }
4217 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004218 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220}
4221
4222/*
4223 * Implementation of "gd" and "gD" command.
4224 */
4225 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004226nv_gd(
4227 oparg_T *oap,
4228 int nchar,
4229 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u *ptr;
4233
Bram Moolenaard9d30582005-05-18 22:10:28 +00004234 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004235 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4236 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004238#ifdef FEAT_FOLDING
4239 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4240 foldOpenCursor();
4241#endif
4242}
4243
4244/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004245 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4246 * otherwise.
4247 */
4248 static int
4249is_ident(char_u *line, int offset)
4250{
4251 int i;
4252 int incomment = FALSE;
4253 int instring = 0;
4254 int prev = 0;
4255
4256 for (i = 0; i < offset && line[i] != NUL; i++)
4257 {
4258 if (instring != 0)
4259 {
4260 if (prev != '\\' && line[i] == instring)
4261 instring = 0;
4262 }
4263 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4264 {
4265 instring = line[i];
4266 }
4267 else
4268 {
4269 if (incomment)
4270 {
4271 if (prev == '*' && line[i] == '/')
4272 incomment = FALSE;
4273 }
4274 else if (prev == '/' && line[i] == '*')
4275 {
4276 incomment = TRUE;
4277 }
4278 else if (prev == '/' && line[i] == '/')
4279 {
4280 return FALSE;
4281 }
4282 }
4283
4284 prev = line[i];
4285 }
4286
4287 return incomment == FALSE && instring == 0;
4288}
4289
4290/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004291 * Search for variable declaration of "ptr[len]".
4292 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4293 * current file ("gD").
4294 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004295 * Return FAIL when not found.
4296 */
4297 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004298find_decl(
4299 char_u *ptr,
4300 int len,
4301 int locally,
4302 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004303 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004304{
4305 char_u *pat;
4306 pos_T old_pos;
4307 pos_T par_pos;
4308 pos_T found_pos;
4309 int t;
4310 int save_p_ws;
4311 int save_p_scs;
4312 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004313 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004314 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004315 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004316
4317 if ((pat = alloc(len + 7)) == NULL)
4318 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004319
4320 /* Put "\V" before the pattern to avoid that the special meaning of "."
4321 * and "~" causes trouble. */
4322 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4323 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 old_pos = curwin->w_cursor;
4325 save_p_ws = p_ws;
4326 save_p_scs = p_scs;
4327 p_ws = FALSE; /* don't wrap around end of file now */
4328 p_scs = FALSE; /* don't switch ignorecase off now */
4329
4330 /*
4331 * With "gD" go to line 1.
4332 * With "gd" Search back for the start of the current function, then go
4333 * back until a blank line. If this fails go to line 1.
4334 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004335 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
4337 setpcmark(); /* Set in findpar() otherwise */
4338 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004339 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 else
4342 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004343 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4345 --curwin->w_cursor.lnum;
4346 }
4347 curwin->w_cursor.col = 0;
4348
4349 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004350 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004351 for (;;)
4352 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004353 valid = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004354 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004355 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004356 if (curwin->w_cursor.lnum >= old_pos.lnum)
4357 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004358
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004359 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004360 {
4361 pos_T *pos;
4362
4363 /* Check that the block the match is in doesn't end before the
4364 * position where we started the search from. */
4365 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4366 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4367 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004368 {
4369 /* There can't be a useful match before the end of this block.
4370 * Skip to the end. */
4371 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004372 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004373 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004374 }
4375
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004376 if (t == FAIL)
4377 {
4378 /* If we previously found a valid position, use it. */
4379 if (found_pos.lnum != 0)
4380 {
4381 curwin->w_cursor = found_pos;
4382 t = OK;
4383 }
4384 break;
4385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004387 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004388 {
4389 /* Ignore this line, continue at start of next line. */
4390 ++curwin->w_cursor.lnum;
4391 curwin->w_cursor.col = 0;
4392 continue;
4393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004395 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4396
4397 /* If the current position is not a valid identifier and a previous
4398 * match is present, favor that one instead. */
4399 if (!valid && found_pos.lnum != 0)
4400 {
4401 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004402 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004403 }
4404
4405 /* Global search: use first valid match found */
4406 if (valid && !locally)
4407 break;
4408 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004409 {
4410 /* If we previously found a valid position, use it. */
4411 if (found_pos.lnum != 0)
4412 curwin->w_cursor = found_pos;
4413 break;
4414 }
4415
Bram Moolenaar226630a2016-10-08 19:21:31 +02004416 /* For finding a local variable and the match is before the "{" or
4417 * inside a comment, continue searching. For K&R style function
4418 * declarations this skips the function header without types. */
4419 if (!valid)
4420 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004421 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004422 }
4423 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004424 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004425 /* Remove SEARCH_START from flags to avoid getting stuck at one
4426 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004427 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004429
4430 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004432 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 curwin->w_cursor = old_pos;
4434 }
4435 else
4436 {
4437 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 /* "n" searches forward now */
4439 reset_search_dir();
4440 }
4441
4442 vim_free(pat);
4443 p_ws = save_p_ws;
4444 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004445
4446 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447}
4448
4449/*
4450 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4451 * lines rather than lines in the file.
4452 * 'dist' must be positive.
4453 *
4454 * Return OK if able to move cursor, FAIL otherwise.
4455 */
4456 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004457nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
4459 int linelen = linetabsize(ml_get_curline());
4460 int retval = OK;
4461 int atend = FALSE;
4462 int n;
4463 int col_off1; /* margin offset for first screen line */
4464 int col_off2; /* margin offset for wrapped screen line */
4465 int width1; /* text width for first screen line */
4466 int width2; /* test width for wrapped screen line */
4467
4468 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004469 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 col_off1 = curwin_col_off();
4472 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004473 width1 = curwin->w_width - col_off1;
4474 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004475 if (width2 == 0)
4476 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 /*
4481 * Instead of sticking at the last character of the buffer line we
4482 * try to stick in the last column of the screen.
4483 */
4484 if (curwin->w_curswant == MAXCOL)
4485 {
4486 atend = TRUE;
4487 validate_virtcol();
4488 if (width1 <= 0)
4489 curwin->w_curswant = 0;
4490 else
4491 {
4492 curwin->w_curswant = width1 - 1;
4493 if (curwin->w_virtcol > curwin->w_curswant)
4494 curwin->w_curswant += ((curwin->w_virtcol
4495 - curwin->w_curswant - 1) / width2 + 1) * width2;
4496 }
4497 }
4498 else
4499 {
4500 if (linelen > width1)
4501 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4502 else
4503 n = width1;
4504 if (curwin->w_curswant > (colnr_T)n + 1)
4505 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4506 * width2;
4507 }
4508
4509 while (dist--)
4510 {
4511 if (dir == BACKWARD)
4512 {
4513 if ((long)curwin->w_curswant >= width2)
4514 /* move back within line */
4515 curwin->w_curswant -= width2;
4516 else
4517 {
4518 /* to previous line */
4519 if (curwin->w_cursor.lnum == 1)
4520 {
4521 retval = FAIL;
4522 break;
4523 }
4524 --curwin->w_cursor.lnum;
4525#ifdef FEAT_FOLDING
4526 /* Move to the start of a closed fold. Don't do that when
4527 * 'foldopen' contains "all": it will open in a moment. */
4528 if (!(fdo_flags & FDO_ALL))
4529 (void)hasFolding(curwin->w_cursor.lnum,
4530 &curwin->w_cursor.lnum, NULL);
4531#endif
4532 linelen = linetabsize(ml_get_curline());
4533 if (linelen > width1)
4534 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4535 + 1) * width2;
4536 }
4537 }
4538 else /* dir == FORWARD */
4539 {
4540 if (linelen > width1)
4541 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4542 else
4543 n = width1;
4544 if (curwin->w_curswant + width2 < (colnr_T)n)
4545 /* move forward within line */
4546 curwin->w_curswant += width2;
4547 else
4548 {
4549 /* to next line */
4550#ifdef FEAT_FOLDING
4551 /* Move to the end of a closed fold. */
4552 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4553 &curwin->w_cursor.lnum);
4554#endif
4555 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4556 {
4557 retval = FAIL;
4558 break;
4559 }
4560 curwin->w_cursor.lnum++;
4561 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004562 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
4564 }
4565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004568 if (virtual_active() && atend)
4569 coladvance(MAXCOL);
4570 else
4571 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4574 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4575 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004576 colnr_T virtcol;
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 /*
4579 * Check for landing on a character that got split at the end of the
4580 * last line. We want to advance a screenline, not end up in the same
4581 * screenline or move two screenlines.
4582 */
4583 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004584 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004585# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004586 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4587 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004588# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004589
4590 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 && (curwin->w_curswant < (colnr_T)width1
4592 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4593 : ((curwin->w_curswant - width1) % width2
4594 > (colnr_T)width2 / 2)))
4595 --curwin->w_cursor.col;
4596 }
4597#endif
4598
4599 if (atend)
4600 curwin->w_curswant = MAXCOL; /* stick in the last column */
4601
4602 return retval;
4603}
4604
4605#ifdef FEAT_MOUSE
4606/*
4607 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4608 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004609 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4610 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 */
4612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004613nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004615 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004617 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
4619 int row, col;
4620
4621 row = mouse_row;
4622 col = mouse_col;
4623
4624 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004625 wp = mouse_find_win(&row, &col);
4626 if (wp == NULL)
4627 return;
4628 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 curbuf = curwin->w_buffer;
4630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004632 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004634# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004635 if (term_use_loop())
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004636 send_keys_to_term(curbuf->b_term, cap->cmdchar, TRUE);
4637 else
4638# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004639 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4640 {
4641 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4642 }
4643 else
4644 {
4645 cap->count1 = 3;
4646 cap->count0 = 3;
4647 nv_scroll_line(cap);
4648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004650# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 else
4652 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004653 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4654 if (!curwin->w_p_wrap)
4655 {
4656 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004657
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004658 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004659 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004660 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4661 if (val < 0)
4662 val = 0;
4663
4664 gui_do_horiz_scroll(val, TRUE);
4665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004667# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 curwin->w_redr_status = TRUE;
4670
4671 curwin = old_curwin;
4672 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673}
4674
4675/*
4676 * Mouse clicks and drags.
4677 */
4678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004679nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680{
4681 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4682}
4683#endif
4684
4685/*
4686 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4687 * cap->arg must be TRUE for CTRL-E.
4688 */
4689 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004690nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692 if (!checkclearop(cap->oap))
4693 scroll_redraw(cap->arg, cap->count1);
4694}
4695
4696/*
4697 * Scroll "count" lines up or down, and redraw.
4698 */
4699 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004700scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 linenr_T prev_topline = curwin->w_topline;
4703#ifdef FEAT_DIFF
4704 int prev_topfill = curwin->w_topfill;
4705#endif
4706 linenr_T prev_lnum = curwin->w_cursor.lnum;
4707
4708 if (up)
4709 scrollup(count, TRUE);
4710 else
4711 scrolldown(count, TRUE);
4712 if (p_so)
4713 {
4714 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4715 * valid, otherwise the screen jumps back at the end of the file. */
4716 cursor_correct();
4717 check_cursor_moved(curwin);
4718 curwin->w_valid |= VALID_TOPLINE;
4719
4720 /* If moved back to where we were, at least move the cursor, otherwise
4721 * we get stuck at one position. Don't move the cursor up if the
4722 * first line of the buffer is already on the screen */
4723 while (curwin->w_topline == prev_topline
4724#ifdef FEAT_DIFF
4725 && curwin->w_topfill == prev_topfill
4726#endif
4727 )
4728 {
4729 if (up)
4730 {
4731 if (curwin->w_cursor.lnum > prev_lnum
4732 || cursor_down(1L, FALSE) == FAIL)
4733 break;
4734 }
4735 else
4736 {
4737 if (curwin->w_cursor.lnum < prev_lnum
4738 || prev_topline == 1L
4739 || cursor_up(1L, FALSE) == FAIL)
4740 break;
4741 }
4742 /* Mark w_topline as valid, otherwise the screen jumps back at the
4743 * end of the file. */
4744 check_cursor_moved(curwin);
4745 curwin->w_valid |= VALID_TOPLINE;
4746 }
4747 }
4748 if (curwin->w_cursor.lnum != prev_lnum)
4749 coladvance(curwin->w_curswant);
4750 redraw_later(VALID);
4751}
4752
4753/*
4754 * Commands that start with "z".
4755 */
4756 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004757nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759 long n;
4760 colnr_T col;
4761 int nchar = cap->nchar;
4762#ifdef FEAT_FOLDING
4763 long old_fdl = curwin->w_p_fdl;
4764 int old_fen = curwin->w_p_fen;
4765#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004766#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004767 int undo = FALSE;
4768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 if (VIM_ISDIGIT(nchar))
4771 {
4772 /*
4773 * "z123{nchar}": edit the count before obtaining {nchar}
4774 */
4775 if (checkclearop(cap->oap))
4776 return;
4777 n = nchar - '0';
4778 for (;;)
4779 {
4780#ifdef USE_ON_FLY_SCROLL
4781 dont_scroll = TRUE; /* disallow scrolling here */
4782#endif
4783 ++no_mapping;
4784 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004785 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 --no_mapping;
4788 --allow_keys;
4789#ifdef FEAT_CMDL_INFO
4790 (void)add_to_showcmd(nchar);
4791#endif
4792 if (nchar == K_DEL || nchar == K_KDEL)
4793 n /= 10;
4794 else if (VIM_ISDIGIT(nchar))
4795 n = n * 10 + (nchar - '0');
4796 else if (nchar == CAR)
4797 {
4798#ifdef FEAT_GUI
4799 need_mouse_correct = TRUE;
4800#endif
4801 win_setheight((int)n);
4802 break;
4803 }
4804 else if (nchar == 'l'
4805 || nchar == 'h'
4806 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004807 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
4809 cap->count1 = n ? n * cap->count1 : cap->count1;
4810 goto dozet;
4811 }
4812 else
4813 {
4814 clearopbeep(cap->oap);
4815 break;
4816 }
4817 }
4818 cap->oap->op_type = OP_NOP;
4819 return;
4820 }
4821
4822dozet:
4823 if (
4824#ifdef FEAT_FOLDING
4825 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4826 * and "zC" only in Visual mode. "zj" and "zk" are motion
4827 * commands. */
4828 cap->nchar != 'f' && cap->nchar != 'F'
4829 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4830 && cap->nchar != 'j' && cap->nchar != 'k'
4831 &&
4832#endif
4833 checkclearop(cap->oap))
4834 return;
4835
4836 /*
4837 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4838 * If line number given, set cursor.
4839 */
4840 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4841 && cap->count0
4842 && cap->count0 != curwin->w_cursor.lnum)
4843 {
4844 setpcmark();
4845 if (cap->count0 > curbuf->b_ml.ml_line_count)
4846 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4847 else
4848 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004849 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851
4852 switch (nchar)
4853 {
4854 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4855 case '+':
4856 if (cap->count0 == 0)
4857 {
4858 /* No count given: put cursor at the line below screen */
4859 validate_botline(); /* make sure w_botline is valid */
4860 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4861 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4862 else
4863 curwin->w_cursor.lnum = curwin->w_botline;
4864 }
4865 /* FALLTHROUGH */
4866 case NL:
4867 case CAR:
4868 case K_KENTER:
4869 beginline(BL_WHITE | BL_FIX);
4870 /* FALLTHROUGH */
4871
4872 case 't': scroll_cursor_top(0, TRUE);
4873 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004874 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 break;
4876
4877 /* "z." and "zz": put cursor in middle of screen */
4878 case '.': beginline(BL_WHITE | BL_FIX);
4879 /* FALLTHROUGH */
4880
4881 case 'z': scroll_cursor_halfway(TRUE);
4882 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004883 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 break;
4885
4886 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4887 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4888 * when <count> is at bottom of window, and puts that one at
4889 * bottom of window. */
4890 if (cap->count0 != 0)
4891 {
4892 scroll_cursor_bot(0, TRUE);
4893 curwin->w_cursor.lnum = curwin->w_topline;
4894 }
4895 else if (curwin->w_topline == 1)
4896 curwin->w_cursor.lnum = 1;
4897 else
4898 curwin->w_cursor.lnum = curwin->w_topline - 1;
4899 /* FALLTHROUGH */
4900 case '-':
4901 beginline(BL_WHITE | BL_FIX);
4902 /* FALLTHROUGH */
4903
4904 case 'b': scroll_cursor_bot(0, TRUE);
4905 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004906 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 break;
4908
4909 /* "zH" - scroll screen right half-page */
4910 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004911 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 /* FALLTHROUGH */
4913
4914 /* "zh" - scroll screen to the right */
4915 case 'h':
4916 case K_LEFT:
4917 if (!curwin->w_p_wrap)
4918 {
4919 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4920 curwin->w_leftcol = 0;
4921 else
4922 curwin->w_leftcol -= (colnr_T)cap->count1;
4923 leftcol_changed();
4924 }
4925 break;
4926
4927 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004928 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 /* FALLTHROUGH */
4930
4931 /* "zl" - scroll screen to the left */
4932 case 'l':
4933 case K_RIGHT:
4934 if (!curwin->w_p_wrap)
4935 {
4936 /* scroll the window left */
4937 curwin->w_leftcol += (colnr_T)cap->count1;
4938 leftcol_changed();
4939 }
4940 break;
4941
4942 /* "zs" - scroll screen, cursor at the start */
4943 case 's': if (!curwin->w_p_wrap)
4944 {
4945#ifdef FEAT_FOLDING
4946 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4947 col = 0; /* like the cursor is in col 0 */
4948 else
4949#endif
4950 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4951 if ((long)col > p_siso)
4952 col -= p_siso;
4953 else
4954 col = 0;
4955 if (curwin->w_leftcol != col)
4956 {
4957 curwin->w_leftcol = col;
4958 redraw_later(NOT_VALID);
4959 }
4960 }
4961 break;
4962
4963 /* "ze" - scroll screen, cursor at the end */
4964 case 'e': if (!curwin->w_p_wrap)
4965 {
4966#ifdef FEAT_FOLDING
4967 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4968 col = 0; /* like the cursor is in col 0 */
4969 else
4970#endif
4971 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004972 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if ((long)col + p_siso < n)
4974 col = 0;
4975 else
4976 col = col + p_siso - n + 1;
4977 if (curwin->w_leftcol != col)
4978 {
4979 curwin->w_leftcol = col;
4980 redraw_later(NOT_VALID);
4981 }
4982 }
4983 break;
4984
4985#ifdef FEAT_FOLDING
4986 /* "zF": create fold command */
4987 /* "zf": create fold operator */
4988 case 'F':
4989 case 'f': if (foldManualAllowed(TRUE))
4990 {
4991 cap->nchar = 'f';
4992 nv_operator(cap);
4993 curwin->w_p_fen = TRUE;
4994
4995 /* "zF" is like "zfzf" */
4996 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4997 {
4998 nv_operator(cap);
4999 finish_op = TRUE;
5000 }
5001 }
5002 else
5003 clearopbeep(cap->oap);
5004 break;
5005
5006 /* "zd": delete fold at cursor */
5007 /* "zD": delete fold at cursor recursively */
5008 case 'd':
5009 case 'D': if (foldManualAllowed(FALSE))
5010 {
5011 if (VIsual_active)
5012 nv_operator(cap);
5013 else
5014 deleteFold(curwin->w_cursor.lnum,
5015 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5016 }
5017 break;
5018
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005019 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 case 'E': if (foldmethodIsManual(curwin))
5021 {
5022 clearFolding(curwin);
5023 changed_window_setting();
5024 }
5025 else if (foldmethodIsMarker(curwin))
5026 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5027 TRUE, FALSE);
5028 else
5029 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5030 break;
5031
5032 /* "zn": fold none: reset 'foldenable' */
5033 case 'n': curwin->w_p_fen = FALSE;
5034 break;
5035
5036 /* "zN": fold Normal: set 'foldenable' */
5037 case 'N': curwin->w_p_fen = TRUE;
5038 break;
5039
5040 /* "zi": invert folding: toggle 'foldenable' */
5041 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5042 break;
5043
5044 /* "za": open closed fold or close open fold at cursor */
5045 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5046 openFold(curwin->w_cursor.lnum, cap->count1);
5047 else
5048 {
5049 closeFold(curwin->w_cursor.lnum, cap->count1);
5050 curwin->w_p_fen = TRUE;
5051 }
5052 break;
5053
5054 /* "zA": open fold at cursor recursively */
5055 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5056 openFoldRecurse(curwin->w_cursor.lnum);
5057 else
5058 {
5059 closeFoldRecurse(curwin->w_cursor.lnum);
5060 curwin->w_p_fen = TRUE;
5061 }
5062 break;
5063
5064 /* "zo": open fold at cursor or Visual area */
5065 case 'o': if (VIsual_active)
5066 nv_operator(cap);
5067 else
5068 openFold(curwin->w_cursor.lnum, cap->count1);
5069 break;
5070
5071 /* "zO": open fold recursively */
5072 case 'O': if (VIsual_active)
5073 nv_operator(cap);
5074 else
5075 openFoldRecurse(curwin->w_cursor.lnum);
5076 break;
5077
5078 /* "zc": close fold at cursor or Visual area */
5079 case 'c': if (VIsual_active)
5080 nv_operator(cap);
5081 else
5082 closeFold(curwin->w_cursor.lnum, cap->count1);
5083 curwin->w_p_fen = TRUE;
5084 break;
5085
5086 /* "zC": close fold recursively */
5087 case 'C': if (VIsual_active)
5088 nv_operator(cap);
5089 else
5090 closeFoldRecurse(curwin->w_cursor.lnum);
5091 curwin->w_p_fen = TRUE;
5092 break;
5093
5094 /* "zv": open folds at the cursor */
5095 case 'v': foldOpenCursor();
5096 break;
5097
5098 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5099 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005100 curwin->w_foldinvalid = TRUE; /* recompute folds */
5101 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 foldOpenCursor();
5103 break;
5104
5105 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5106 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005107 curwin->w_foldinvalid = TRUE; /* recompute folds */
5108 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110
5111 /* "zm": fold more */
5112 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005113 {
5114 curwin->w_p_fdl -= cap->count1;
5115 if (curwin->w_p_fdl < 0)
5116 curwin->w_p_fdl = 0;
5117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 old_fdl = -1; /* force an update */
5119 curwin->w_p_fen = TRUE;
5120 break;
5121
5122 /* "zM": close all folds */
5123 case 'M': curwin->w_p_fdl = 0;
5124 old_fdl = -1; /* force an update */
5125 curwin->w_p_fen = TRUE;
5126 break;
5127
5128 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005129 case 'r': curwin->w_p_fdl += cap->count1;
5130 {
5131 int d = getDeepestNesting();
5132
5133 if (curwin->w_p_fdl >= d)
5134 curwin->w_p_fdl = d;
5135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 break;
5137
5138 /* "zR": open all folds */
5139 case 'R': curwin->w_p_fdl = getDeepestNesting();
5140 old_fdl = -1; /* force an update */
5141 break;
5142
5143 case 'j': /* "zj" move to next fold downwards */
5144 case 'k': /* "zk" move to next fold upwards */
5145 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5146 cap->count1) == FAIL)
5147 clearopbeep(cap->oap);
5148 break;
5149
5150#endif /* FEAT_FOLDING */
5151
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005152#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005153 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5154 ++no_mapping;
5155 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005156 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005157 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005158 --no_mapping;
5159 --allow_keys;
5160#ifdef FEAT_CMDL_INFO
5161 (void)add_to_showcmd(nchar);
5162#endif
5163 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5164 {
5165 clearopbeep(cap->oap);
5166 break;
5167 }
5168 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005169 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005170
Bram Moolenaarb765d632005-06-07 21:00:02 +00005171 case 'g': /* "zg": add good word to word list */
5172 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005173 case 'G': /* "zG": add good word to temp word list */
5174 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005175 {
5176 char_u *ptr = NULL;
5177 int len;
5178
5179 if (checkclearop(cap->oap))
5180 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005181 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5182 == FAIL)
5183 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005184 if (ptr == NULL)
5185 {
5186 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005187
Bram Moolenaar134bf072013-09-25 18:54:24 +02005188 /* Find bad word under the cursor. When 'spell' is
5189 * off this fails and find_ident_under_cursor() is
5190 * used below. */
5191 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005192 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005193 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005194 if (len != 0 && curwin->w_cursor.col <= pos.col)
5195 ptr = ml_get_pos(&curwin->w_cursor);
5196 curwin->w_cursor = pos;
5197 }
5198
Bram Moolenaarb765d632005-06-07 21:00:02 +00005199 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5200 FIND_IDENT)) == 0)
5201 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005202 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005203 (nchar == 'G' || nchar == 'W')
5204 ? 0 : (int)cap->count1,
5205 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005206 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005207 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208
Bram Moolenaar43abc522005-12-10 20:15:02 +00005209 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005210 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005211 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005213#endif
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 default: clearopbeep(cap->oap);
5216 }
5217
5218#ifdef FEAT_FOLDING
5219 /* Redraw when 'foldenable' changed */
5220 if (old_fen != curwin->w_p_fen)
5221 {
5222# ifdef FEAT_DIFF
5223 win_T *wp;
5224
5225 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5226 {
5227 /* Adjust 'foldenable' in diff-synced windows. */
5228 FOR_ALL_WINDOWS(wp)
5229 {
5230 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5231 {
5232 wp->w_p_fen = curwin->w_p_fen;
5233 changed_window_setting_win(wp);
5234 }
5235 }
5236 }
5237# endif
5238 changed_window_setting();
5239 }
5240
5241 /* Redraw when 'foldlevel' changed. */
5242 if (old_fdl != curwin->w_p_fdl)
5243 newFoldLevel();
5244#endif
5245}
5246
5247#ifdef FEAT_GUI
5248/*
5249 * Vertical scrollbar movement.
5250 */
5251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005252nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253{
5254 if (cap->oap->op_type != OP_NOP)
5255 clearopbeep(cap->oap);
5256
5257 /* Even if an operator was pending, we still want to scroll */
5258 gui_do_scroll();
5259}
5260
5261/*
5262 * Horizontal scrollbar movement.
5263 */
5264 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005265nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
5267 if (cap->oap->op_type != OP_NOP)
5268 clearopbeep(cap->oap);
5269
5270 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005271 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272}
5273#endif
5274
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005275#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005276/*
5277 * Click in GUI tab.
5278 */
5279 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005280nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005281{
5282 if (cap->oap->op_type != OP_NOP)
5283 clearopbeep(cap->oap);
5284
5285 /* Even if an operator was pending, we still want to jump tabs. */
5286 goto_tabpage(current_tab);
5287}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005288
5289/*
5290 * Selected item in tab line menu.
5291 */
5292 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005293nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005294{
5295 if (cap->oap->op_type != OP_NOP)
5296 clearopbeep(cap->oap);
5297
5298 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005299 handle_tabmenu();
5300}
5301
5302/*
5303 * Handle selecting an item of the GUI tab line menu.
5304 * Used in Normal and Insert mode.
5305 */
5306 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005307handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005308{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005309 switch (current_tabmenu)
5310 {
5311 case TABLINE_MENU_CLOSE:
5312 if (current_tab == 0)
5313 do_cmdline_cmd((char_u *)"tabclose");
5314 else
5315 {
5316 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5317 current_tab);
5318 do_cmdline_cmd(IObuff);
5319 }
5320 break;
5321
5322 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005323 if (current_tab == 0)
5324 do_cmdline_cmd((char_u *)"$tabnew");
5325 else
5326 {
5327 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5328 current_tab - 1);
5329 do_cmdline_cmd(IObuff);
5330 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005331 break;
5332
5333 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005334 if (current_tab == 0)
5335 do_cmdline_cmd((char_u *)"browse $tabnew");
5336 else
5337 {
5338 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5339 current_tab - 1);
5340 do_cmdline_cmd(IObuff);
5341 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005342 break;
5343 }
5344}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005345#endif
5346
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347/*
5348 * "Q" command.
5349 */
5350 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005351nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
5353 /*
5354 * Ignore 'Q' in Visual mode, just give a beep.
5355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005357 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005358 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 do_exmode(FALSE);
5360}
5361
5362/*
5363 * Handle a ":" command.
5364 */
5365 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005366nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367{
5368 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005369 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 if (VIsual_active)
5372 nv_operator(cap);
5373 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
5375 if (cap->oap->op_type != OP_NOP)
5376 {
5377 /* Using ":" as a movement is characterwise exclusive. */
5378 cap->oap->motion_type = MCHAR;
5379 cap->oap->inclusive = FALSE;
5380 }
5381 else if (cap->count0)
5382 {
5383 /* translate "count:" into ":.,.+(count - 1)" */
5384 stuffcharReadbuff('.');
5385 if (cap->count0 > 1)
5386 {
5387 stuffReadbuff((char_u *)",.+");
5388 stuffnumReadbuff((long)cap->count0 - 1L);
5389 }
5390 }
5391
5392 /* When typing, don't type below an old message */
5393 if (KeyTyped)
5394 compute_cmdrow();
5395
5396 old_p_im = p_im;
5397
5398 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005399 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5401
5402 /* If 'insertmode' changed, enter or exit Insert mode */
5403 if (p_im != old_p_im)
5404 {
5405 if (p_im)
5406 restart_edit = 'i';
5407 else
5408 restart_edit = 0;
5409 }
5410
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005411 if (cmd_result == FAIL)
5412 /* The Ex command failed, do not execute the operator. */
5413 clearop(cap->oap);
5414 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5416 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005417 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5418 || did_emsg
5419 ))
5420 /* The start of the operator has become invalid by the Ex command.
5421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 clearopbeep(cap->oap);
5423 }
5424}
5425
5426/*
5427 * Handle CTRL-G command.
5428 */
5429 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005430nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 if (VIsual_active) /* toggle Selection/Visual mode */
5433 {
5434 VIsual_select = !VIsual_select;
5435 showmode();
5436 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005437 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 /* print full name if count given or :cd used */
5439 fileinfo((int)cap->count0, FALSE, TRUE);
5440}
5441
5442/*
5443 * Handle CTRL-H <Backspace> command.
5444 */
5445 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005446nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 if (VIsual_active && VIsual_select)
5449 {
5450 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5451 v_visop(cap);
5452 }
5453 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 nv_left(cap);
5455}
5456
5457/*
5458 * CTRL-L: clear screen and redraw.
5459 */
5460 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005461nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
5463 if (!checkclearop(cap->oap))
5464 {
5465#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5466 /*
5467 * Right now, the BeBox doesn't seem to have an easy way to detect
5468 * window resizing, so we cheat and make the user detect it
5469 * manually with CTRL-L instead
5470 */
5471 ui_get_shellsize();
5472#endif
5473#ifdef FEAT_SYN_HL
5474 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005475 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005476# ifdef FEAT_RELTIME
5477 {
5478 win_T *wp;
5479
5480 FOR_ALL_WINDOWS(wp)
5481 wp->w_s->b_syn_slow = FALSE;
5482 }
5483# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484#endif
5485 redraw_later(CLEAR);
5486 }
5487}
5488
5489/*
5490 * CTRL-O: In Select mode: switch to Visual mode for one command.
5491 * Otherwise: Go to older pcmark.
5492 */
5493 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005494nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 if (VIsual_active && VIsual_select)
5497 {
5498 VIsual_select = FALSE;
5499 showmode();
5500 restart_VIsual_select = 2; /* restart Select mode later */
5501 }
5502 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 cap->count1 = -cap->count1;
5505 nv_pcmark(cap);
5506 }
5507}
5508
5509/*
5510 * CTRL-^ command, short for ":e #"
5511 */
5512 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005513nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
5515 if (!checkclearopq(cap->oap))
5516 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5517 GETF_SETMARK|GETF_ALT, FALSE);
5518}
5519
5520/*
5521 * "Z" commands.
5522 */
5523 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005524nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 if (!checkclearopq(cap->oap))
5527 {
5528 switch (cap->nchar)
5529 {
5530 /* "ZZ": equivalent to ":x". */
5531 case 'Z': do_cmdline_cmd((char_u *)"x");
5532 break;
5533
5534 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5535 case 'Q': do_cmdline_cmd((char_u *)"q!");
5536 break;
5537
5538 default: clearopbeep(cap->oap);
5539 }
5540 }
5541}
5542
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543/*
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
5560/*
5561 * Handle the commands that use the word under the cursor.
5562 * [g] CTRL-] :ta to current identifier
5563 * [g] 'K' run program for current identifier
5564 * [g] '*' / to current identifier or string
5565 * [g] '#' ? to current identifier or string
5566 * g ']' :tselect for current identifier
5567 */
5568 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005569nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570{
5571 char_u *ptr = NULL;
5572 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005573 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005574 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 char_u *p;
5576 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005577 int kp_help; /* 'keywordprg' is ":he" */
5578 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 int n = 0; /* init for GCC */
5580 int cmdchar;
5581 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005582 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 char_u *aux_ptr;
5584 int isman;
5585 int isman_s;
5586
5587 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5588 {
5589 cmdchar = cap->nchar;
5590 g_cmd = TRUE;
5591 }
5592 else
5593 {
5594 cmdchar = cap->cmdchar;
5595 g_cmd = FALSE;
5596 }
5597
5598 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5599 cmdchar = '#';
5600
5601 /*
5602 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5603 */
5604 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5607 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (checkclearopq(cap->oap))
5609 return;
5610 }
5611
5612 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5613 (cmdchar == '*' || cmdchar == '#')
5614 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5615 {
5616 clearop(cap->oap);
5617 return;
5618 }
5619
5620 /* Allocate buffer to put the command in. Inserting backslashes can
5621 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5622 * and some numbers. */
5623 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5624 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5625 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005626 if (kp_help && *skipwhite(ptr) == NUL)
5627 {
5628 EMSG(_(e_noident)); /* found white space only */
5629 return;
5630 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005631 kp_ex = (*kp == ':');
5632 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5633 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 if (buf == NULL)
5635 return;
5636 buf[0] = NUL;
5637
5638 switch (cmdchar)
5639 {
5640 case '*':
5641 case '#':
5642 /*
5643 * Put cursor at start of word, makes search skip the word
5644 * under the cursor.
5645 * Call setpcmark() first, so "*``" puts the cursor back where
5646 * it was.
5647 */
5648 setpcmark();
5649 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5650
5651 if (!g_cmd && vim_iswordp(ptr))
5652 STRCPY(buf, "\\<");
5653 no_smartcase = TRUE; /* don't use 'smartcase' now */
5654 break;
5655
5656 case 'K':
5657 if (kp_help)
5658 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005659 else if (kp_ex)
5660 {
5661 if (cap->count0 != 0)
5662 vim_snprintf((char *)buf, buflen, "%s %ld",
5663 kp, cap->count0);
5664 else
5665 STRCPY(buf, kp);
5666 STRCAT(buf, " ");
5667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 else
5669 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005670 /* An external command will probably use an argument starting
5671 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005672 while (*ptr == '-' && n > 0)
5673 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005674 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005675 --n;
5676 }
5677 if (n == 0)
5678 {
5679 EMSG(_(e_noident)); /* found dashes only */
5680 vim_free(buf);
5681 return;
5682 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005683
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 /* When a count is given, turn it into a range. Is this
5685 * really what we want? */
5686 isman = (STRCMP(kp, "man") == 0);
5687 isman_s = (STRCMP(kp, "man -s") == 0);
5688 if (cap->count0 != 0 && !(isman || isman_s))
5689 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5690
5691 STRCAT(buf, "! ");
5692 if (cap->count0 == 0 && isman_s)
5693 STRCAT(buf, "man");
5694 else
5695 STRCAT(buf, kp);
5696 STRCAT(buf, " ");
5697 if (cap->count0 != 0 && (isman || isman_s))
5698 {
5699 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5700 STRCAT(buf, " ");
5701 }
5702 }
5703 break;
5704
5705 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005706 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707#ifdef FEAT_CSCOPE
5708 if (p_cst)
5709 STRCPY(buf, "cstag ");
5710 else
5711#endif
5712 STRCPY(buf, "ts ");
5713 break;
5714
5715 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005716 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (curbuf->b_help)
5718 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005720 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005721 if (g_cmd)
5722 STRCPY(buf, "tj ");
5723 else
5724 sprintf((char *)buf, "%ldta ", cap->count0);
5725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727
5728 /*
5729 * Now grab the chars in the identifier
5730 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005731 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005733 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005734 if (kp_ex)
5735 /* Escape the argument properly for an Ex command */
5736 p = vim_strsave_fnameescape(ptr, FALSE);
5737 else
5738 /* Escape the argument properly for a shell command */
5739 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005740 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005741 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005743 vim_free(buf);
5744 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005746 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5747 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005748 {
5749 vim_free(buf);
5750 vim_free(p);
5751 return;
5752 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005753 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005754 STRCAT(buf, p);
5755 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005757 else
5758 {
5759 if (cmdchar == '*')
5760 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5761 else if (cmdchar == '#')
5762 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005763 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005764 {
5765 if (curbuf->b_help)
5766 /* ":help" handles unescaped argument */
5767 aux_ptr = (char_u *)"";
5768 else
5769 aux_ptr = (char_u *)"\\|\"\n[";
5770 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005771 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005772 aux_ptr = (char_u *)"\\|\"\n*?[";
5773
5774 p = buf + STRLEN(buf);
5775 while (n-- > 0)
5776 {
5777 /* put a backslash before \ and some others */
5778 if (vim_strchr(aux_ptr, *ptr) != NULL)
5779 *p++ = '\\';
5780#ifdef FEAT_MBYTE
5781 /* When current byte is a part of multibyte character, copy all
5782 * bytes of that character. */
5783 if (has_mbyte)
5784 {
5785 int i;
5786 int len = (*mb_ptr2len)(ptr) - 1;
5787
5788 for (i = 0; i < len && n >= 1; ++i, --n)
5789 *p++ = *ptr++;
5790 }
5791#endif
5792 *p++ = *ptr++;
5793 }
5794 *p = NUL;
5795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 /*
5798 * Execute the command.
5799 */
5800 if (cmdchar == '*' || cmdchar == '#')
5801 {
5802 if (!g_cmd && (
5803#ifdef FEAT_MBYTE
5804 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5805#endif
5806 vim_iswordc(ptr[-1])))
5807 STRCAT(buf, "\\>");
5808#ifdef FEAT_CMDHIST
5809 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005810 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5812#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005813 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
5815 else
5816 do_cmdline_cmd(buf);
5817
5818 vim_free(buf);
5819}
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821/*
5822 * Get visually selected text, within one line only.
5823 * Returns FAIL if more than one line selected.
5824 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005825 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005826get_visual_text(
5827 cmdarg_T *cap,
5828 char_u **pp, /* return: start of selected text */
5829 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
5831 if (VIsual_mode != 'V')
5832 unadjust_for_sel();
5833 if (VIsual.lnum != curwin->w_cursor.lnum)
5834 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005835 if (cap != NULL)
5836 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 return FAIL;
5838 }
5839 if (VIsual_mode == 'V')
5840 {
5841 *pp = ml_get_curline();
5842 *lenp = (int)STRLEN(*pp);
5843 }
5844 else
5845 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005846 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
5848 *pp = ml_get_pos(&curwin->w_cursor);
5849 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5850 }
5851 else
5852 {
5853 *pp = ml_get_pos(&VIsual);
5854 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5855 }
5856#ifdef FEAT_MBYTE
5857 if (has_mbyte)
5858 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005859 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860#endif
5861 }
5862 reset_VIsual_and_resel();
5863 return OK;
5864}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
5866/*
5867 * CTRL-T: backwards in tag stack
5868 */
5869 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005870nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
5872 if (!checkclearopq(cap->oap))
5873 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5874}
5875
5876/*
5877 * Handle scrolling command 'H', 'L' and 'M'.
5878 */
5879 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005880nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 int used = 0;
5883 long n;
5884#ifdef FEAT_FOLDING
5885 linenr_T lnum;
5886#endif
5887 int half;
5888
5889 cap->oap->motion_type = MLINE;
5890 setpcmark();
5891
5892 if (cap->cmdchar == 'L')
5893 {
5894 validate_botline(); /* make sure curwin->w_botline is valid */
5895 curwin->w_cursor.lnum = curwin->w_botline - 1;
5896 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5897 curwin->w_cursor.lnum = 1;
5898 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005899 {
5900#ifdef FEAT_FOLDING
5901 if (hasAnyFolding(curwin))
5902 {
5903 /* Count a fold for one screen line. */
5904 for (n = cap->count1 - 1; n > 0
5905 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5906 {
5907 (void)hasFolding(curwin->w_cursor.lnum,
5908 &curwin->w_cursor.lnum, NULL);
5909 --curwin->w_cursor.lnum;
5910 }
5911 }
5912 else
5913#endif
5914 curwin->w_cursor.lnum -= cap->count1 - 1;
5915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917 else
5918 {
5919 if (cap->cmdchar == 'M')
5920 {
5921#ifdef FEAT_DIFF
5922 /* Don't count filler lines above the window. */
5923 used -= diff_check_fill(curwin, curwin->w_topline)
5924 - curwin->w_topfill;
5925#endif
5926 validate_botline(); /* make sure w_empty_rows is valid */
5927 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5928 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5929 {
5930#ifdef FEAT_DIFF
5931 /* Count half he number of filler lines to be "below this
5932 * line" and half to be "above the next line". */
5933 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5934 + n) / 2 >= half)
5935 {
5936 --n;
5937 break;
5938 }
5939#endif
5940 used += plines(curwin->w_topline + n);
5941 if (used >= half)
5942 break;
5943#ifdef FEAT_FOLDING
5944 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5945 n = lnum - curwin->w_topline;
5946#endif
5947 }
5948 if (n > 0 && used > curwin->w_height)
5949 --n;
5950 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005951 else /* (cap->cmdchar == 'H') */
5952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005954#ifdef FEAT_FOLDING
5955 if (hasAnyFolding(curwin))
5956 {
5957 /* Count a fold for one screen line. */
5958 lnum = curwin->w_topline;
5959 while (n-- > 0 && lnum < curwin->w_botline - 1)
5960 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005961 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005962 ++lnum;
5963 }
5964 n = lnum - curwin->w_topline;
5965 }
5966#endif
5967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 curwin->w_cursor.lnum = curwin->w_topline + n;
5969 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5970 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5971 }
5972
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005973 /* Correct for 'so', except when an operator is pending. */
5974 if (cap->oap->op_type == OP_NOP)
5975 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 beginline(BL_SOL | BL_FIX);
5977}
5978
5979/*
5980 * Cursor right commands.
5981 */
5982 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005983nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005986 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005988 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5989 {
5990 /* <C-Right> and <S-Right> move a word or WORD right */
5991 if (mod_mask & MOD_MASK_CTRL)
5992 cap->arg = TRUE;
5993 nv_wordcmd(cap);
5994 return;
5995 }
5996
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 cap->oap->motion_type = MCHAR;
5998 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005999 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006001#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006003 * In virtual edit mode, there's no such thing as "past_line", as lines
6004 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 */
6006 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006007 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008#endif
6009
6010 for (n = cap->count1; n > 0; --n)
6011 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006012 if ((!past_line && oneright() == FAIL)
6013 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006014 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 {
6016 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006017 * <Space> wraps to next line if 'whichwrap' has 's'.
6018 * 'l' wraps to next line if 'whichwrap' has 'l'.
6019 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 */
6021 if ( ((cap->cmdchar == ' '
6022 && vim_strchr(p_ww, 's') != NULL)
6023 || (cap->cmdchar == 'l'
6024 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006025 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 && vim_strchr(p_ww, '>') != NULL))
6027 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6028 {
6029 /* When deleting we also count the NL as a character.
6030 * Set cap->oap->inclusive when last char in the line is
6031 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006032 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006034 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 cap->oap->inclusive = TRUE;
6036 else
6037 {
6038 ++curwin->w_cursor.lnum;
6039 curwin->w_cursor.col = 0;
6040#ifdef FEAT_VIRTUALEDIT
6041 curwin->w_cursor.coladd = 0;
6042#endif
6043 curwin->w_set_curswant = TRUE;
6044 cap->oap->inclusive = FALSE;
6045 }
6046 continue;
6047 }
6048 if (cap->oap->op_type == OP_NOP)
6049 {
6050 /* Only beep and flush if not moved at all */
6051 if (n == cap->count1)
6052 beep_flush();
6053 }
6054 else
6055 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006056 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 cap->oap->inclusive = TRUE;
6058 }
6059 break;
6060 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006061 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 {
6063 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006064#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 if (virtual_active())
6066 oneright();
6067 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006070#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 if (has_mbyte)
6072 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006073 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 ++curwin->w_cursor.col;
6077 }
6078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 }
6080#ifdef FEAT_FOLDING
6081 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6082 && cap->oap->op_type == OP_NOP)
6083 foldOpenCursor();
6084#endif
6085}
6086
6087/*
6088 * Cursor left commands.
6089 *
6090 * Returns TRUE when operator end should not be adjusted.
6091 */
6092 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006093nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094{
6095 long n;
6096
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006097 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6098 {
6099 /* <C-Left> and <S-Left> move a word or WORD left */
6100 if (mod_mask & MOD_MASK_CTRL)
6101 cap->arg = 1;
6102 nv_bck_word(cap);
6103 return;
6104 }
6105
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 cap->oap->motion_type = MCHAR;
6107 cap->oap->inclusive = FALSE;
6108 for (n = cap->count1; n > 0; --n)
6109 {
6110 if (oneleft() == FAIL)
6111 {
6112 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6113 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6114 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6115 */
6116 if ( (((cap->cmdchar == K_BS
6117 || cap->cmdchar == Ctrl_H)
6118 && vim_strchr(p_ww, 'b') != NULL)
6119 || (cap->cmdchar == 'h'
6120 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006121 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 && vim_strchr(p_ww, '<') != NULL))
6123 && curwin->w_cursor.lnum > 1)
6124 {
6125 --(curwin->w_cursor.lnum);
6126 coladvance((colnr_T)MAXCOL);
6127 curwin->w_set_curswant = TRUE;
6128
6129 /* When the NL before the first char has to be deleted we
6130 * put the cursor on the NUL after the previous line.
6131 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006132 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if ( (cap->oap->op_type == OP_DELETE
6134 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006135 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006137 char_u *cp = ml_get_cursor();
6138
6139 if (*cp != NUL)
6140 {
6141#ifdef FEAT_MBYTE
6142 if (has_mbyte)
6143 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6144 else
6145#endif
6146 ++curwin->w_cursor.col;
6147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 cap->retval |= CA_NO_ADJ_OP_END;
6149 }
6150 continue;
6151 }
6152 /* Only beep and flush if not moved at all */
6153 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6154 beep_flush();
6155 break;
6156 }
6157 }
6158#ifdef FEAT_FOLDING
6159 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6160 && cap->oap->op_type == OP_NOP)
6161 foldOpenCursor();
6162#endif
6163}
6164
6165/*
6166 * Cursor up commands.
6167 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6168 */
6169 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006170nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006172 if (mod_mask & MOD_MASK_SHIFT)
6173 {
6174 /* <S-Up> is page up */
6175 cap->arg = BACKWARD;
6176 nv_page(cap);
6177 }
6178 else
6179 {
6180 cap->oap->motion_type = MLINE;
6181 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6182 clearopbeep(cap->oap);
6183 else if (cap->arg)
6184 beginline(BL_WHITE | BL_FIX);
6185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186}
6187
6188/*
6189 * Cursor down commands.
6190 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6191 */
6192 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006193nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006195 if (mod_mask & MOD_MASK_SHIFT)
6196 {
6197 /* <S-Down> is page down */
6198 cap->arg = FORWARD;
6199 nv_page(cap);
6200 }
6201 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006202#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006204 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar74a47162017-02-26 19:09:05 +01006205 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006206 if (curwin->w_llist_ref == NULL)
6207 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6208 else
6209 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar74a47162017-02-26 19:09:05 +01006210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 else
6212#endif
6213 {
6214#ifdef FEAT_CMDWIN
6215 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006216 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 cmdwin_result = CAR;
6218 else
6219#endif
6220 {
6221 cap->oap->motion_type = MLINE;
6222 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6223 clearopbeep(cap->oap);
6224 else if (cap->arg)
6225 beginline(BL_WHITE | BL_FIX);
6226 }
6227 }
6228}
6229
6230#ifdef FEAT_SEARCHPATH
6231/*
6232 * Grab the file name under the cursor and edit it.
6233 */
6234 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006235nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006238 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006240 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
6242 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006243 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 return;
6245 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006246#ifdef FEAT_AUTOCMD
6247 if (curbuf_locked())
6248 {
6249 clearop(cap->oap);
6250 return;
6251 }
6252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006254 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
6256 if (ptr != NULL)
6257 {
6258 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006259 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006260 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006262 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006263 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006264 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006265 {
6266 curwin->w_cursor.lnum = lnum;
6267 check_cursor_lnum();
6268 beginline(BL_SOL | BL_FIX);
6269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 vim_free(ptr);
6271 }
6272 else
6273 clearop(cap->oap);
6274}
6275#endif
6276
6277/*
6278 * <End> command: to end of current line or last line.
6279 */
6280 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006281nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006283 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006285 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 nv_goto(cap);
6287 cap->count1 = 1; /* to end of current line */
6288 }
6289 nv_dollar(cap);
6290}
6291
6292/*
6293 * Handle the "$" command.
6294 */
6295 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006296nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
6298 cap->oap->motion_type = MCHAR;
6299 cap->oap->inclusive = TRUE;
6300#ifdef FEAT_VIRTUALEDIT
6301 /* In virtual mode when off the edge of a line and an operator
6302 * is pending (whew!) keep the cursor where it is.
6303 * Otherwise, send it to the end of the line. */
6304 if (!virtual_active() || gchar_cursor() != NUL
6305 || cap->oap->op_type == OP_NOP)
6306#endif
6307 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6308 if (cursor_down((long)(cap->count1 - 1),
6309 cap->oap->op_type == OP_NOP) == FAIL)
6310 clearopbeep(cap->oap);
6311#ifdef FEAT_FOLDING
6312 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6313 foldOpenCursor();
6314#endif
6315}
6316
6317/*
6318 * Implementation of '?' and '/' commands.
6319 * If cap->arg is TRUE don't set PC mark.
6320 */
6321 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006322nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006325 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
6327 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6328 {
6329 /* Translate "g??" to "g?g?" */
6330 cap->cmdchar = 'g';
6331 cap->nchar = '?';
6332 nv_operator(cap);
6333 return;
6334 }
6335
Bram Moolenaardda933d2016-09-03 21:04:58 +02006336 /* When using 'incsearch' the cursor may be moved to set a different search
6337 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6339
6340 if (cap->searchbuf == NULL)
6341 {
6342 clearop(oap);
6343 return;
6344 }
6345
Bram Moolenaar46539112015-02-17 15:43:57 +01006346 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006347 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006348 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349}
6350
6351/*
6352 * Handle "N" and "n" commands.
6353 * cap->arg is SEARCH_REV for "N", 0 for "n".
6354 */
6355 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006356nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
Bram Moolenaar46539112015-02-17 15:43:57 +01006358 pos_T old = curwin->w_cursor;
6359 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6360
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006361 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006362 {
6363 /* Avoid getting stuck on the current cursor position, which can
6364 * happen when an offset is given and the cursor is on the last char
6365 * in the buffer: Repeat with count + 1. */
6366 cap->count1 += 1;
6367 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6368 cap->count1 -= 1;
6369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370}
6371
6372/*
6373 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6374 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006375 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006377 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006378normal_search(
6379 cmdarg_T *cap,
6380 int dir,
6381 char_u *pat,
6382 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 int i;
6385
6386 cap->oap->motion_type = MCHAR;
6387 cap->oap->inclusive = FALSE;
6388 cap->oap->use_reg_one = TRUE;
6389 curwin->w_set_curswant = TRUE;
6390
6391 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006392 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 if (i == 0)
6394 clearop(cap->oap);
6395 else
6396 {
6397 if (i == 2)
6398 cap->oap->motion_type = MLINE;
6399#ifdef FEAT_VIRTUALEDIT
6400 curwin->w_cursor.coladd = 0;
6401#endif
6402#ifdef FEAT_FOLDING
6403 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6404 foldOpenCursor();
6405#endif
6406 }
6407
6408 /* "/$" will put the cursor after the end of the line, may need to
6409 * correct that here */
6410 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006411 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412}
6413
6414/*
6415 * Character search commands.
6416 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6417 * ',' and FALSE for ';'.
6418 * cap->nchar is NUL for ',' and ';' (repeat the search)
6419 */
6420 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006421nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422{
6423 int t_cmd;
6424
6425 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6426 t_cmd = TRUE;
6427 else
6428 t_cmd = FALSE;
6429
6430 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6432 clearopbeep(cap->oap);
6433 else
6434 {
6435 curwin->w_set_curswant = TRUE;
6436#ifdef FEAT_VIRTUALEDIT
6437 /* Include a Tab for "tx" and for "dfx". */
6438 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6439 && (t_cmd || cap->oap->op_type != OP_NOP))
6440 {
6441 colnr_T scol, ecol;
6442
6443 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6444 curwin->w_cursor.coladd = ecol - scol;
6445 }
6446 else
6447 curwin->w_cursor.coladd = 0;
6448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450#ifdef FEAT_FOLDING
6451 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6452 foldOpenCursor();
6453#endif
6454 }
6455}
6456
6457/*
6458 * "[" and "]" commands.
6459 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6460 */
6461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006462nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006464 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 pos_T prev_pos;
6466 pos_T *pos = NULL; /* init for GCC */
6467 pos_T old_pos; /* cursor position before command */
6468 int flag;
6469 long n;
6470 int findc;
6471 int c;
6472
6473 cap->oap->motion_type = MCHAR;
6474 cap->oap->inclusive = FALSE;
6475 old_pos = curwin->w_cursor;
6476#ifdef FEAT_VIRTUALEDIT
6477 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6478#endif
6479
6480#ifdef FEAT_SEARCHPATH
6481 /*
6482 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6483 */
6484 if (cap->nchar == 'f')
6485 nv_gotofile(cap);
6486 else
6487#endif
6488
6489#ifdef FEAT_FIND_ID
6490 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006491 * Find the occurrence(s) of the identifier or define under cursor
6492 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 *
6494 * search list jump
6495 * fwd bwd fwd bwd fwd bwd
6496 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6497 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6498 */
6499 if (vim_strchr((char_u *)
6500#ifdef EBCDIC
6501 "iI\005dD\067",
6502#else
6503 "iI\011dD\004",
6504#endif
6505 cap->nchar) != NULL)
6506 {
6507 char_u *ptr;
6508 int len;
6509
6510 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6511 clearop(cap->oap);
6512 else
6513 {
6514 find_pattern_in_path(ptr, 0, len, TRUE,
6515 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6516 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6517 cap->count1,
6518 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6519 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6520 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6521 (linenr_T)MAXLNUM);
6522 curwin->w_set_curswant = TRUE;
6523 }
6524 }
6525 else
6526#endif
6527
6528 /*
6529 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6530 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6531 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6532 * "[m" or "]m" search for prev/next start of (Java) method.
6533 * "[M" or "]M" search for prev/next end of (Java) method.
6534 */
6535 if ( (cap->cmdchar == '['
6536 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6537 || (cap->cmdchar == ']'
6538 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6539 {
6540 if (cap->nchar == '*')
6541 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 prev_pos.lnum = 0;
6543 if (cap->nchar == 'm' || cap->nchar == 'M')
6544 {
6545 if (cap->cmdchar == '[')
6546 findc = '{';
6547 else
6548 findc = '}';
6549 n = 9999;
6550 }
6551 else
6552 {
6553 findc = cap->nchar;
6554 n = cap->count1;
6555 }
6556 for ( ; n > 0; --n)
6557 {
6558 if ((pos = findmatchlimit(cap->oap, findc,
6559 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6560 {
6561 if (new_pos.lnum == 0) /* nothing found */
6562 {
6563 if (cap->nchar != 'm' && cap->nchar != 'M')
6564 clearopbeep(cap->oap);
6565 }
6566 else
6567 pos = &new_pos; /* use last one found */
6568 break;
6569 }
6570 prev_pos = new_pos;
6571 curwin->w_cursor = *pos;
6572 new_pos = *pos;
6573 }
6574 curwin->w_cursor = old_pos;
6575
6576 /*
6577 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6578 * brought us to the match for "[m" and "]M" when inside a method.
6579 * Try finding the '{' or '}' we want to be at.
6580 * Also repeat for the given count.
6581 */
6582 if (cap->nchar == 'm' || cap->nchar == 'M')
6583 {
6584 /* norm is TRUE for "]M" and "[m" */
6585 int norm = ((findc == '{') == (cap->nchar == 'm'));
6586
6587 n = cap->count1;
6588 /* found a match: we were inside a method */
6589 if (prev_pos.lnum != 0)
6590 {
6591 pos = &prev_pos;
6592 curwin->w_cursor = prev_pos;
6593 if (norm)
6594 --n;
6595 }
6596 else
6597 pos = NULL;
6598 while (n > 0)
6599 {
6600 for (;;)
6601 {
6602 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6603 {
6604 /* if not found anything, that's an error */
6605 if (pos == NULL)
6606 clearopbeep(cap->oap);
6607 n = 0;
6608 break;
6609 }
6610 c = gchar_cursor();
6611 if (c == '{' || c == '}')
6612 {
6613 /* Must have found end/start of class: use it.
6614 * Or found the place to be at. */
6615 if ((c == findc && norm) || (n == 1 && !norm))
6616 {
6617 new_pos = curwin->w_cursor;
6618 pos = &new_pos;
6619 n = 0;
6620 }
6621 /* if no match found at all, we started outside of the
6622 * class and we're inside now. Just go on. */
6623 else if (new_pos.lnum == 0)
6624 {
6625 new_pos = curwin->w_cursor;
6626 pos = &new_pos;
6627 }
6628 /* found start/end of other method: go to match */
6629 else if ((pos = findmatchlimit(cap->oap, findc,
6630 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6631 0)) == NULL)
6632 n = 0;
6633 else
6634 curwin->w_cursor = *pos;
6635 break;
6636 }
6637 }
6638 --n;
6639 }
6640 curwin->w_cursor = old_pos;
6641 if (pos == NULL && new_pos.lnum != 0)
6642 clearopbeep(cap->oap);
6643 }
6644 if (pos != NULL)
6645 {
6646 setpcmark();
6647 curwin->w_cursor = *pos;
6648 curwin->w_set_curswant = TRUE;
6649#ifdef FEAT_FOLDING
6650 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6651 && cap->oap->op_type == OP_NOP)
6652 foldOpenCursor();
6653#endif
6654 }
6655 }
6656
6657 /*
6658 * "[[", "[]", "]]" and "][": move to start or end of function
6659 */
6660 else if (cap->nchar == '[' || cap->nchar == ']')
6661 {
6662 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6663 flag = '{';
6664 else
6665 flag = '}'; /* "][" or "[]" */
6666
6667 curwin->w_set_curswant = TRUE;
6668 /*
6669 * Imitate strange Vi behaviour: When using "]]" with an operator
6670 * we also stop at '}'.
6671 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006672 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 (cap->oap->op_type != OP_NOP
6674 && cap->arg == FORWARD && flag == '{')))
6675 clearopbeep(cap->oap);
6676 else
6677 {
6678 if (cap->oap->op_type == OP_NOP)
6679 beginline(BL_WHITE | BL_FIX);
6680#ifdef FEAT_FOLDING
6681 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6682 foldOpenCursor();
6683#endif
6684 }
6685 }
6686
6687 /*
6688 * "[p", "[P", "]P" and "]p": put with indent adjustment
6689 */
6690 else if (cap->nchar == 'p' || cap->nchar == 'P')
6691 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006692 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006694 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6695 ? FORWARD : BACKWARD;
6696 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006697 int was_visual = VIsual_active;
6698 int line_count = curbuf->b_ml.ml_line_count;
6699 pos_T start, end;
6700
6701 if (VIsual_active)
6702 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006703 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006704 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006705 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006706 curwin->w_cursor = (dir == BACKWARD ? start : end);
6707 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006708# ifdef FEAT_CLIPBOARD
6709 adjust_clip_reg(&regname);
6710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006712
6713 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006714 if (was_visual)
6715 {
6716 VIsual = start;
6717 curwin->w_cursor = end;
6718 if (dir == BACKWARD)
6719 {
6720 /* adjust lines */
6721 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6722 curwin->w_cursor.lnum +=
6723 curbuf->b_ml.ml_line_count - line_count;
6724 }
6725
6726 VIsual_active = TRUE;
6727 if (VIsual_mode == 'V')
6728 {
6729 /* delete visually selected lines */
6730 cap->cmdchar = 'd';
6731 cap->nchar = NUL;
6732 cap->oap->regname = regname;
6733 nv_operator(cap);
6734 do_pending_operator(cap, 0, FALSE);
6735 }
6736 if (VIsual_active)
6737 {
6738 end_visual_mode();
6739 redraw_later(SOME_VALID);
6740 }
6741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743 }
6744
6745 /*
6746 * "['", "[`", "]'" and "]`": jump to next mark
6747 */
6748 else if (cap->nchar == '\'' || cap->nchar == '`')
6749 {
6750 pos = &curwin->w_cursor;
6751 for (n = cap->count1; n > 0; --n)
6752 {
6753 prev_pos = *pos;
6754 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6755 cap->nchar == '\'');
6756 if (pos == NULL)
6757 break;
6758 }
6759 if (pos == NULL)
6760 pos = &prev_pos;
6761 nv_cursormark(cap, cap->nchar == '\'', pos);
6762 }
6763
6764#ifdef FEAT_MOUSE
6765 /*
6766 * [ or ] followed by a middle mouse click: put selected text with
6767 * indent adjustment. Any other button just does as usual.
6768 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006769 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 {
6771 (void)do_mouse(cap->oap, cap->nchar,
6772 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6773 cap->count1, PUT_FIXINDENT);
6774 }
6775#endif /* FEAT_MOUSE */
6776
6777#ifdef FEAT_FOLDING
6778 /*
6779 * "[z" and "]z": move to start or end of open fold.
6780 */
6781 else if (cap->nchar == 'z')
6782 {
6783 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6784 cap->count1) == FAIL)
6785 clearopbeep(cap->oap);
6786 }
6787#endif
6788
6789#ifdef FEAT_DIFF
6790 /*
6791 * "[c" and "]c": move to next or previous diff-change.
6792 */
6793 else if (cap->nchar == 'c')
6794 {
6795 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6796 cap->count1) == FAIL)
6797 clearopbeep(cap->oap);
6798 }
6799#endif
6800
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006801#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006802 /*
6803 * "[s", "[S", "]s" and "]S": move to next spell error.
6804 */
6805 else if (cap->nchar == 's' || cap->nchar == 'S')
6806 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006807 setpcmark();
6808 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006809 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6810 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006811 {
6812 clearopbeep(cap->oap);
6813 break;
6814 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006815# ifdef FEAT_FOLDING
6816 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6817 foldOpenCursor();
6818# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006819 }
6820#endif
6821
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 /* Not a valid cap->nchar. */
6823 else
6824 clearopbeep(cap->oap);
6825}
6826
6827/*
6828 * Handle Normal mode "%" command.
6829 */
6830 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006831nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832{
6833 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006834#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 linenr_T lnum = curwin->w_cursor.lnum;
6836#endif
6837
6838 cap->oap->inclusive = TRUE;
6839 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6840 {
6841 if (cap->count0 > 100)
6842 clearopbeep(cap->oap);
6843 else
6844 {
6845 cap->oap->motion_type = MLINE;
6846 setpcmark();
6847 /* Round up, so CTRL-G will give same value. Watch out for a
6848 * large line count, the line number must not go negative! */
6849 if (curbuf->b_ml.ml_line_count > 1000000)
6850 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6851 / 100L * cap->count0;
6852 else
6853 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6854 cap->count0 + 99L) / 100L;
6855 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6856 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6857 beginline(BL_SOL | BL_FIX);
6858 }
6859 }
6860 else /* "%" : go to matching paren */
6861 {
6862 cap->oap->motion_type = MCHAR;
6863 cap->oap->use_reg_one = TRUE;
6864 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6865 clearopbeep(cap->oap);
6866 else
6867 {
6868 setpcmark();
6869 curwin->w_cursor = *pos;
6870 curwin->w_set_curswant = TRUE;
6871#ifdef FEAT_VIRTUALEDIT
6872 curwin->w_cursor.coladd = 0;
6873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 }
6876 }
6877#ifdef FEAT_FOLDING
6878 if (cap->oap->op_type == OP_NOP
6879 && lnum != curwin->w_cursor.lnum
6880 && (fdo_flags & FDO_PERCENT)
6881 && KeyTyped)
6882 foldOpenCursor();
6883#endif
6884}
6885
6886/*
6887 * Handle "(" and ")" commands.
6888 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6889 */
6890 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006891nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892{
6893 cap->oap->motion_type = MCHAR;
6894 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006895 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6896 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 curwin->w_set_curswant = TRUE;
6898
6899 if (findsent(cap->arg, cap->count1) == FAIL)
6900 clearopbeep(cap->oap);
6901 else
6902 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006903 /* Don't leave the cursor on the NUL past end of line. */
6904 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905#ifdef FEAT_VIRTUALEDIT
6906 curwin->w_cursor.coladd = 0;
6907#endif
6908#ifdef FEAT_FOLDING
6909 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6910 foldOpenCursor();
6911#endif
6912 }
6913}
6914
6915/*
6916 * "m" command: Mark a position.
6917 */
6918 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006919nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 if (!checkclearop(cap->oap))
6922 {
6923 if (setmark(cap->nchar) == FAIL)
6924 clearopbeep(cap->oap);
6925 }
6926}
6927
6928/*
6929 * "{" and "}" commands.
6930 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6931 */
6932 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006933nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
6935 cap->oap->motion_type = MCHAR;
6936 cap->oap->inclusive = FALSE;
6937 cap->oap->use_reg_one = TRUE;
6938 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006939 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 clearopbeep(cap->oap);
6941 else
6942 {
6943#ifdef FEAT_VIRTUALEDIT
6944 curwin->w_cursor.coladd = 0;
6945#endif
6946#ifdef FEAT_FOLDING
6947 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6948 foldOpenCursor();
6949#endif
6950 }
6951}
6952
6953/*
6954 * "u" command: Undo or make lower case.
6955 */
6956 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006957nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006959 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 {
6961 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6962 cap->cmdchar = 'g';
6963 cap->nchar = 'u';
6964 nv_operator(cap);
6965 }
6966 else
6967 nv_kundo(cap);
6968}
6969
6970/*
6971 * <Undo> command.
6972 */
6973 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006974nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 if (!checkclearopq(cap->oap))
6977 {
6978 u_undo((int)cap->count1);
6979 curwin->w_set_curswant = TRUE;
6980 }
6981}
6982
6983/*
6984 * Handle the "r" command.
6985 */
6986 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006987nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 char_u *ptr;
6990 int had_ctrl_v;
6991 long n;
6992
6993 if (checkclearop(cap->oap))
6994 return;
6995
6996 /* get another character */
6997 if (cap->nchar == Ctrl_V)
6998 {
6999 had_ctrl_v = Ctrl_V;
7000 cap->nchar = get_literal();
7001 /* Don't redo a multibyte character with CTRL-V. */
7002 if (cap->nchar > DEL)
7003 had_ctrl_v = NUL;
7004 }
7005 else
7006 had_ctrl_v = NUL;
7007
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007008 /* Abort if the character is a special key. */
7009 if (IS_SPECIAL(cap->nchar))
7010 {
7011 clearopbeep(cap->oap);
7012 return;
7013 }
7014
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 /* Visual mode "r" */
7016 if (VIsual_active)
7017 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007018 if (got_int)
7019 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007020 if (had_ctrl_v)
7021 {
7022 if (cap->nchar == '\r')
7023 cap->nchar = -1;
7024 else if (cap->nchar == '\n')
7025 cap->nchar = -2;
7026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 nv_operator(cap);
7028 return;
7029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
7031#ifdef FEAT_VIRTUALEDIT
7032 /* Break tabs, etc. */
7033 if (virtual_active())
7034 {
7035 if (u_save_cursor() == FAIL)
7036 return;
7037 if (gchar_cursor() == NUL)
7038 {
7039 /* Add extra space and put the cursor on the first one. */
7040 coladvance_force((colnr_T)(getviscol() + cap->count1));
7041 curwin->w_cursor.col -= cap->count1;
7042 }
7043 else if (gchar_cursor() == TAB)
7044 coladvance_force(getviscol());
7045 }
7046#endif
7047
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007048 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007050 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051#ifdef FEAT_MBYTE
7052 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7053#endif
7054 )
7055 {
7056 clearopbeep(cap->oap);
7057 return;
7058 }
7059
7060 /*
7061 * Replacing with a TAB is done by edit() when it is complicated because
7062 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7063 * Other characters are done below to avoid problems with things like
7064 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7065 */
7066 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7067 {
7068 stuffnumReadbuff(cap->count1);
7069 stuffcharReadbuff('R');
7070 stuffcharReadbuff('\t');
7071 stuffcharReadbuff(ESC);
7072 return;
7073 }
7074
7075 /* save line for undo */
7076 if (u_save_cursor() == FAIL)
7077 return;
7078
7079 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7080 {
7081 /*
7082 * Replace character(s) by a single newline.
7083 * Strange vi behaviour: Only one newline is inserted.
7084 * Delete the characters here.
7085 * Insert the newline with an insert command, takes care of
7086 * autoindent. The insert command depends on being on the last
7087 * character of a line or not.
7088 */
7089#ifdef FEAT_MBYTE
7090 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7091#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007092 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093#endif
7094 stuffcharReadbuff('\r');
7095 stuffcharReadbuff(ESC);
7096
7097 /* Give 'r' to edit(), to get the redo command right. */
7098 invoke_edit(cap, TRUE, 'r', FALSE);
7099 }
7100 else
7101 {
7102 prep_redo(cap->oap->regname, cap->count1,
7103 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7104
7105 curbuf->b_op_start = curwin->w_cursor;
7106#ifdef FEAT_MBYTE
7107 if (has_mbyte)
7108 {
7109 int old_State = State;
7110
7111 if (cap->ncharC1 != 0)
7112 AppendCharToRedobuff(cap->ncharC1);
7113 if (cap->ncharC2 != 0)
7114 AppendCharToRedobuff(cap->ncharC2);
7115
7116 /* This is slow, but it handles replacing a single-byte with a
7117 * multi-byte and the other way around. Also handles adding
7118 * composing characters for utf-8. */
7119 for (n = cap->count1; n > 0; --n)
7120 {
7121 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007122 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7123 {
7124 int c = ins_copychar(curwin->w_cursor.lnum
7125 + (cap->nchar == Ctrl_Y ? -1 : 1));
7126 if (c != NUL)
7127 ins_char(c);
7128 else
7129 /* will be decremented further down */
7130 ++curwin->w_cursor.col;
7131 }
7132 else
7133 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 State = old_State;
7135 if (cap->ncharC1 != 0)
7136 ins_char(cap->ncharC1);
7137 if (cap->ncharC2 != 0)
7138 ins_char(cap->ncharC2);
7139 }
7140 }
7141 else
7142#endif
7143 {
7144 /*
7145 * Replace the characters within one line.
7146 */
7147 for (n = cap->count1; n > 0; --n)
7148 {
7149 /*
7150 * Get ptr again, because u_save and/or showmatch() will have
7151 * released the line. At the same time we let know that the
7152 * line will be changed.
7153 */
7154 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007155 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7156 {
7157 int c = ins_copychar(curwin->w_cursor.lnum
7158 + (cap->nchar == Ctrl_Y ? -1 : 1));
7159 if (c != NUL)
7160 ptr[curwin->w_cursor.col] = c;
7161 }
7162 else
7163 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 if (p_sm && msg_silent == 0)
7165 showmatch(cap->nchar);
7166 ++curwin->w_cursor.col;
7167 }
7168#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007169 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007171 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaar009b2592004-10-24 19:18:58 +00007173 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007174 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007176 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 }
7178#endif
7179
7180 /* mark the buffer as changed and prepare for displaying */
7181 changed_bytes(curwin->w_cursor.lnum,
7182 (colnr_T)(curwin->w_cursor.col - cap->count1));
7183 }
7184 --curwin->w_cursor.col; /* cursor on the last replaced char */
7185#ifdef FEAT_MBYTE
7186 /* if the character on the left of the current cursor is a multi-byte
7187 * character, move two characters left */
7188 if (has_mbyte)
7189 mb_adjust_cursor();
7190#endif
7191 curbuf->b_op_end = curwin->w_cursor;
7192 curwin->w_set_curswant = TRUE;
7193 set_last_insert(cap->nchar);
7194 }
7195}
7196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197/*
7198 * 'o': Exchange start and end of Visual area.
7199 * 'O': same, but in block mode exchange left and right corners.
7200 */
7201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007202v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
7204 pos_T old_cursor;
7205 colnr_T left, right;
7206
7207 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7208 {
7209 old_cursor = curwin->w_cursor;
7210 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7211 curwin->w_cursor.lnum = VIsual.lnum;
7212 coladvance(left);
7213 VIsual = curwin->w_cursor;
7214
7215 curwin->w_cursor.lnum = old_cursor.lnum;
7216 curwin->w_curswant = right;
7217 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7218 * right one column */
7219 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7220 ++curwin->w_curswant;
7221 coladvance(curwin->w_curswant);
7222 if (curwin->w_cursor.col == old_cursor.col
7223#ifdef FEAT_VIRTUALEDIT
7224 && (!virtual_active()
7225 || curwin->w_cursor.coladd == old_cursor.coladd)
7226#endif
7227 )
7228 {
7229 curwin->w_cursor.lnum = VIsual.lnum;
7230 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7231 ++right;
7232 coladvance(right);
7233 VIsual = curwin->w_cursor;
7234
7235 curwin->w_cursor.lnum = old_cursor.lnum;
7236 coladvance(left);
7237 curwin->w_curswant = left;
7238 }
7239 }
7240 else
7241 {
7242 old_cursor = curwin->w_cursor;
7243 curwin->w_cursor = VIsual;
7244 VIsual = old_cursor;
7245 curwin->w_set_curswant = TRUE;
7246 }
7247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
7249/*
7250 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7251 */
7252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007253nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 if (VIsual_active) /* "R" is replace lines */
7256 {
7257 cap->cmdchar = 'c';
7258 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007259 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 VIsual_mode = 'V';
7261 nv_operator(cap);
7262 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007263 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 {
7265 if (!curbuf->b_p_ma)
7266 EMSG(_(e_modifiable));
7267 else
7268 {
7269#ifdef FEAT_VIRTUALEDIT
7270 if (virtual_active())
7271 coladvance(getviscol());
7272#endif
7273 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7274 }
7275 }
7276}
7277
7278#ifdef FEAT_VREPLACE
7279/*
7280 * "gr".
7281 */
7282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007283nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 if (VIsual_active)
7286 {
7287 cap->cmdchar = 'r';
7288 cap->nchar = cap->extra_char;
7289 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7290 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007291 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
7293 if (!curbuf->b_p_ma)
7294 EMSG(_(e_modifiable));
7295 else
7296 {
7297 if (cap->extra_char == Ctrl_V) /* get another character */
7298 cap->extra_char = get_literal();
7299 stuffcharReadbuff(cap->extra_char);
7300 stuffcharReadbuff(ESC);
7301# ifdef FEAT_VIRTUALEDIT
7302 if (virtual_active())
7303 coladvance(getviscol());
7304# endif
7305 invoke_edit(cap, TRUE, 'v', FALSE);
7306 }
7307 }
7308}
7309#endif
7310
7311/*
7312 * Swap case for "~" command, when it does not work like an operator.
7313 */
7314 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007315n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
7317 long n;
7318 pos_T startpos;
7319 int did_change = 0;
7320#ifdef FEAT_NETBEANS_INTG
7321 pos_T pos;
7322 char_u *ptr;
7323 int count;
7324#endif
7325
7326 if (checkclearopq(cap->oap))
7327 return;
7328
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007329 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 {
7331 clearopbeep(cap->oap);
7332 return;
7333 }
7334
7335 prep_redo_cmd(cap);
7336
7337 if (u_save_cursor() == FAIL)
7338 return;
7339
7340 startpos = curwin->w_cursor;
7341#ifdef FEAT_NETBEANS_INTG
7342 pos = startpos;
7343#endif
7344 for (n = cap->count1; n > 0; --n)
7345 {
7346 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7347 inc_cursor();
7348 if (gchar_cursor() == NUL)
7349 {
7350 if (vim_strchr(p_ww, '~') != NULL
7351 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7352 {
7353#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007354 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 {
7356 if (did_change)
7357 {
7358 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007359 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007360 netbeans_removed(curbuf, pos.lnum, pos.col,
7361 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007363 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 }
7365 pos.col = 0;
7366 pos.lnum++;
7367 }
7368#endif
7369 ++curwin->w_cursor.lnum;
7370 curwin->w_cursor.col = 0;
7371 if (n > 1)
7372 {
7373 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7374 break;
7375 u_clearline();
7376 }
7377 }
7378 else
7379 break;
7380 }
7381 }
7382#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007383 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
7385 ptr = ml_get(pos.lnum);
7386 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007387 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7388 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 }
7390#endif
7391
7392
7393 check_cursor();
7394 curwin->w_set_curswant = TRUE;
7395 if (did_change)
7396 {
7397 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7398 0L);
7399 curbuf->b_op_start = startpos;
7400 curbuf->b_op_end = curwin->w_cursor;
7401 if (curbuf->b_op_end.col > 0)
7402 --curbuf->b_op_end.col;
7403 }
7404}
7405
7406/*
7407 * Move cursor to mark.
7408 */
7409 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007410nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411{
7412 if (check_mark(pos) == FAIL)
7413 clearop(cap->oap);
7414 else
7415 {
7416 if (cap->cmdchar == '\''
7417 || cap->cmdchar == '`'
7418 || cap->cmdchar == '['
7419 || cap->cmdchar == ']')
7420 setpcmark();
7421 curwin->w_cursor = *pos;
7422 if (flag)
7423 beginline(BL_WHITE | BL_FIX);
7424 else
7425 check_cursor();
7426 }
7427 cap->oap->motion_type = flag ? MLINE : MCHAR;
7428 if (cap->cmdchar == '`')
7429 cap->oap->use_reg_one = TRUE;
7430 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7431 curwin->w_set_curswant = TRUE;
7432}
7433
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434/*
7435 * Handle commands that are operators in Visual mode.
7436 */
7437 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007438v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7441
7442 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007443 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 if (isupper(cap->cmdchar))
7445 {
7446 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007447 {
7448 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7452 curwin->w_curswant = MAXCOL;
7453 }
7454 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7455 nv_operator(cap);
7456}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458/*
7459 * "s" and "S" commands.
7460 */
7461 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007462nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7465 {
7466 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007467 {
7468 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 cap->cmdchar = 'c';
7472 nv_operator(cap);
7473 }
7474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 nv_optrans(cap);
7476}
7477
7478/*
7479 * Abbreviated commands.
7480 */
7481 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007482nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483{
7484 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7485 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 /* in Visual mode these commands are operators */
7488 if (VIsual_active)
7489 v_visop(cap);
7490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 nv_optrans(cap);
7492}
7493
7494/*
7495 * Translate a command into another command.
7496 */
7497 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007498nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499{
7500 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7501 (char_u *)"d$", (char_u *)"c$",
7502 (char_u *)"cl", (char_u *)"cc",
7503 (char_u *)"yy", (char_u *)":s\r"};
7504 static char_u *str = (char_u *)"xXDCsSY&";
7505
7506 if (!checkclearopq(cap->oap))
7507 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007508 /* In Vi "2D" doesn't delete the next line. Can't translate it
7509 * either, because "2." should also not use the count. */
7510 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7511 {
7512 cap->oap->start = curwin->w_cursor;
7513 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007514#ifdef FEAT_EVAL
7515 set_op_var(OP_DELETE);
7516#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007517 cap->count1 = 1;
7518 nv_dollar(cap);
7519 finish_op = TRUE;
7520 ResetRedobuff();
7521 AppendCharToRedobuff('D');
7522 }
7523 else
7524 {
7525 if (cap->count0)
7526 stuffnumReadbuff(cap->count0);
7527 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
7530 cap->opcount = 0;
7531}
7532
7533/*
7534 * "'" and "`" commands. Also for "g'" and "g`".
7535 * cap->arg is TRUE for "'" and "g'".
7536 */
7537 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007538nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539{
7540 pos_T *pos;
7541 int c;
7542#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007543 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7545#endif
7546
7547 if (cap->cmdchar == 'g')
7548 c = cap->extra_char;
7549 else
7550 c = cap->nchar;
7551 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7552 if (pos == (pos_T *)-1) /* jumped to other file */
7553 {
7554 if (cap->arg)
7555 {
7556 check_cursor_lnum();
7557 beginline(BL_WHITE | BL_FIX);
7558 }
7559 else
7560 check_cursor();
7561 }
7562 else
7563 nv_cursormark(cap, cap->arg, pos);
7564
7565#ifdef FEAT_VIRTUALEDIT
7566 /* May need to clear the coladd that a mark includes. */
7567 if (!virtual_active())
7568 curwin->w_cursor.coladd = 0;
7569#endif
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007570 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571#ifdef FEAT_FOLDING
7572 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007573 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007574 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 && (fdo_flags & FDO_MARK)
7576 && old_KeyTyped)
7577 foldOpenCursor();
7578#endif
7579}
7580
7581/*
7582 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7583 */
7584 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007585nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586{
7587#ifdef FEAT_JUMPLIST
7588 pos_T *pos;
7589# ifdef FEAT_FOLDING
7590 linenr_T lnum = curwin->w_cursor.lnum;
7591 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7592# endif
7593
7594 if (!checkclearopq(cap->oap))
7595 {
7596 if (cap->cmdchar == 'g')
7597 pos = movechangelist((int)cap->count1);
7598 else
7599 pos = movemark((int)cap->count1);
7600 if (pos == (pos_T *)-1) /* jump to other file */
7601 {
7602 curwin->w_set_curswant = TRUE;
7603 check_cursor();
7604 }
7605 else if (pos != NULL) /* can jump */
7606 nv_cursormark(cap, FALSE, pos);
7607 else if (cap->cmdchar == 'g')
7608 {
7609 if (curbuf->b_changelistlen == 0)
7610 EMSG(_("E664: changelist is empty"));
7611 else if (cap->count1 < 0)
7612 EMSG(_("E662: At start of changelist"));
7613 else
7614 EMSG(_("E663: At end of changelist"));
7615 }
7616 else
7617 clearopbeep(cap->oap);
7618# ifdef FEAT_FOLDING
7619 if (cap->oap->op_type == OP_NOP
7620 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7621 && (fdo_flags & FDO_MARK)
7622 && old_KeyTyped)
7623 foldOpenCursor();
7624# endif
7625 }
7626#else
7627 clearopbeep(cap->oap);
7628#endif
7629}
7630
7631/*
7632 * Handle '"' command.
7633 */
7634 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007635nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636{
7637 if (checkclearop(cap->oap))
7638 return;
7639#ifdef FEAT_EVAL
7640 if (cap->nchar == '=')
7641 cap->nchar = get_expr_register();
7642#endif
7643 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7644 {
7645 cap->oap->regname = cap->nchar;
7646 cap->opcount = cap->count0; /* remember count before '"' */
7647#ifdef FEAT_EVAL
7648 set_reg_var(cap->oap->regname);
7649#endif
7650 }
7651 else
7652 clearopbeep(cap->oap);
7653}
7654
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655/*
7656 * Handle "v", "V" and "CTRL-V" commands.
7657 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7658 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007659 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 */
7661 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007662nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007664 if (cap->cmdchar == Ctrl_Q)
7665 cap->cmdchar = Ctrl_V;
7666
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7668 * characterwise, linewise, or blockwise. */
7669 if (cap->oap->op_type != OP_NOP)
7670 {
7671 cap->oap->motion_force = cap->cmdchar;
7672 finish_op = FALSE; /* operator doesn't finish now but later */
7673 return;
7674 }
7675
7676 VIsual_select = cap->arg;
7677 if (VIsual_active) /* change Visual mode */
7678 {
7679 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7680 end_visual_mode();
7681 else /* toggle char/block mode */
7682 { /* or char/line mode */
7683 VIsual_mode = cap->cmdchar;
7684 showmode();
7685 }
7686 redraw_curbuf_later(INVERTED); /* update the inversion */
7687 }
7688 else /* start Visual mode */
7689 {
7690 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007691 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007693 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 VIsual = curwin->w_cursor;
7695
7696 VIsual_active = TRUE;
7697 VIsual_reselect = TRUE;
7698 if (!cap->arg)
7699 /* start Select mode when 'selectmode' contains "cmd" */
7700 may_start_select('c');
7701#ifdef FEAT_MOUSE
7702 setmouse();
7703#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007704 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 redraw_cmdline = TRUE; /* show visual mode later */
7706 /*
7707 * For V and ^V, we multiply the number of lines even if there
7708 * was only one -- webb
7709 */
7710 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7711 {
7712 curwin->w_cursor.lnum +=
7713 resel_VIsual_line_count * cap->count0 - 1;
7714 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7715 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7716 }
7717 VIsual_mode = resel_VIsual_mode;
7718 if (VIsual_mode == 'v')
7719 {
7720 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007721 {
7722 validate_virtcol();
7723 curwin->w_curswant = curwin->w_virtcol
7724 + resel_VIsual_vcol * cap->count0 - 1;
7725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007727 curwin->w_curswant = resel_VIsual_vcol;
7728 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007730 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 {
7732 curwin->w_curswant = MAXCOL;
7733 coladvance((colnr_T)MAXCOL);
7734 }
7735 else if (VIsual_mode == Ctrl_V)
7736 {
7737 validate_virtcol();
7738 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007739 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 coladvance(curwin->w_curswant);
7741 }
7742 else
7743 curwin->w_set_curswant = TRUE;
7744 redraw_curbuf_later(INVERTED); /* show the inversion */
7745 }
7746 else
7747 {
7748 if (!cap->arg)
7749 /* start Select mode when 'selectmode' contains "cmd" */
7750 may_start_select('c');
7751 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007752 if (VIsual_mode != 'V' && *p_sel == 'e')
7753 ++cap->count1; /* include one more char */
7754 if (cap->count0 > 0 && --cap->count1 > 0)
7755 {
7756 /* With a count select that many characters or lines. */
7757 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7758 nv_right(cap);
7759 else if (VIsual_mode == 'V')
7760 nv_down(cap);
7761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
7763 }
7764}
7765
7766/*
7767 * Start selection for Shift-movement keys.
7768 */
7769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007770start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
7772 /* if 'selectmode' contains "key", start Select mode */
7773 may_start_select('k');
7774 n_start_visual_mode('v');
7775}
7776
7777/*
7778 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7779 */
7780 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007781may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
7783 VIsual_select = (stuff_empty() && typebuf_typed()
7784 && (vim_strchr(p_slm, c) != NULL));
7785}
7786
7787/*
7788 * Start Visual mode "c".
7789 * Should set VIsual_select before calling this.
7790 */
7791 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007792n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007794#ifdef FEAT_CONCEAL
7795 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007796 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007797#endif
7798
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 VIsual_mode = c;
7800 VIsual_active = TRUE;
7801 VIsual_reselect = TRUE;
7802#ifdef FEAT_VIRTUALEDIT
7803 /* Corner case: the 0 position in a tab may change when going into
7804 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7805 */
7806 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007807 {
7808 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811#endif
7812 VIsual = curwin->w_cursor;
7813
7814#ifdef FEAT_FOLDING
7815 foldAdjustVisual();
7816#endif
7817
7818#ifdef FEAT_MOUSE
7819 setmouse();
7820#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007821#ifdef FEAT_CONCEAL
7822 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007823 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007824#endif
7825
Bram Moolenaar09df3122006-01-23 22:23:09 +00007826 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 redraw_cmdline = TRUE; /* show visual mode later */
7828#ifdef FEAT_CLIPBOARD
7829 /* Make sure the clipboard gets updated. Needed because start and
7830 * end may still be the same, and the selection needs to be owned */
7831 clip_star.vmode = NUL;
7832#endif
7833
7834 /* Only need to redraw this line, unless still need to redraw an old
7835 * Visual area (when 'lazyredraw' is set). */
7836 if (curwin->w_redr_type < INVERTED)
7837 {
7838 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7839 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7840 }
7841}
7842
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843
7844/*
7845 * CTRL-W: Window commands
7846 */
7847 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007848nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855}
7856
7857/*
7858 * CTRL-Z: Suspend
7859 */
7860 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007861nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 if (VIsual_active)
7865 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 do_cmdline_cmd((char_u *)"st");
7867}
7868
7869/*
7870 * Commands starting with "g".
7871 */
7872 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007873nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874{
7875 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 int i;
7878 int flag = FALSE;
7879
7880 switch (cap->nchar)
7881 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007882 case Ctrl_A:
7883 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884#ifdef MEM_PROFILE
7885 /*
7886 * "g^A": dump log of used memory.
7887 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007888 if (!VIsual_active && cap->nchar == Ctrl_A)
7889 vim_mem_profile_dump();
7890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007892 /*
7893 * "g^A/g^X": sequentially increment visually selected region
7894 */
7895 if (VIsual_active)
7896 {
7897 cap->arg = TRUE;
7898 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007899 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007900 nv_addsub(cap);
7901 }
7902 else
7903 clearopbeep(oap);
7904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905
7906#ifdef FEAT_VREPLACE
7907 /*
7908 * "gR": Enter virtual replace mode.
7909 */
7910 case 'R':
7911 cap->arg = TRUE;
7912 nv_Replace(cap);
7913 break;
7914
7915 case 'r':
7916 nv_vreplace(cap);
7917 break;
7918#endif
7919
7920 case '&':
7921 do_cmdline_cmd((char_u *)"%s//~/&");
7922 break;
7923
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 /*
7925 * "gv": Reselect the previous Visual area. If Visual already active,
7926 * exchange previous and current Visual area.
7927 */
7928 case 'v':
7929 if (checkclearop(oap))
7930 break;
7931
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007932 if ( curbuf->b_visual.vi_start.lnum == 0
7933 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7934 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 beep_flush();
7936 else
7937 {
7938 /* set w_cursor to the start of the Visual area, tpos to the end */
7939 if (VIsual_active)
7940 {
7941 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007942 VIsual_mode = curbuf->b_visual.vi_mode;
7943 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944# ifdef FEAT_EVAL
7945 curbuf->b_visual_mode_eval = i;
7946# endif
7947 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007948 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7949 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007951 tpos = curbuf->b_visual.vi_end;
7952 curbuf->b_visual.vi_end = curwin->w_cursor;
7953 curwin->w_cursor = curbuf->b_visual.vi_start;
7954 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 }
7956 else
7957 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007958 VIsual_mode = curbuf->b_visual.vi_mode;
7959 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7960 tpos = curbuf->b_visual.vi_end;
7961 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 }
7963
7964 VIsual_active = TRUE;
7965 VIsual_reselect = TRUE;
7966
7967 /* Set Visual to the start and w_cursor to the end of the Visual
7968 * area. Make sure they are on an existing character. */
7969 check_cursor();
7970 VIsual = curwin->w_cursor;
7971 curwin->w_cursor = tpos;
7972 check_cursor();
7973 update_topline();
7974 /*
7975 * When called from normal "g" command: start Select mode when
7976 * 'selectmode' contains "cmd". When called for K_SELECT, always
7977 * start Select mode.
7978 */
7979 if (cap->arg)
7980 VIsual_select = TRUE;
7981 else
7982 may_start_select('c');
7983#ifdef FEAT_MOUSE
7984 setmouse();
7985#endif
7986#ifdef FEAT_CLIPBOARD
7987 /* Make sure the clipboard gets updated. Needed because start and
7988 * end are still the same, and the selection needs to be owned */
7989 clip_star.vmode = NUL;
7990#endif
7991 redraw_curbuf_later(INVERTED);
7992 showmode();
7993 }
7994 break;
7995 /*
7996 * "gV": Don't reselect the previous Visual area after a Select mode
7997 * mapping of menu.
7998 */
7999 case 'V':
8000 VIsual_reselect = FALSE;
8001 break;
8002
8003 /*
8004 * "gh": start Select mode.
8005 * "gH": start Select line mode.
8006 * "g^H": start Select block mode.
8007 */
8008 case K_BS:
8009 cap->nchar = Ctrl_H;
8010 /* FALLTHROUGH */
8011 case 'h':
8012 case 'H':
8013 case Ctrl_H:
8014# ifdef EBCDIC
8015 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8016 if (cap->nchar == Ctrl_H)
8017 cap->cmdchar = Ctrl_V;
8018 else
8019# endif
8020 cap->cmdchar = cap->nchar + ('v' - 'h');
8021 cap->arg = TRUE;
8022 nv_visual(cap);
8023 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008024
8025 /* "gn", "gN" visually select next/previous search match
8026 * "gn" selects next match
8027 * "gN" selects previous match
8028 */
8029 case 'N':
8030 case 'n':
8031 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008032 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008033 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034
8035 /*
8036 * "gj" and "gk" two new funny movement keys -- up and down
8037 * movement based on *screen* line rather than *file* line.
8038 */
8039 case 'j':
8040 case K_DOWN:
8041 /* with 'nowrap' it works just like the normal "j" command; also when
8042 * in a closed fold */
8043 if (!curwin->w_p_wrap
8044#ifdef FEAT_FOLDING
8045 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8046#endif
8047 )
8048 {
8049 oap->motion_type = MLINE;
8050 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8051 }
8052 else
8053 i = nv_screengo(oap, FORWARD, cap->count1);
8054 if (i == FAIL)
8055 clearopbeep(oap);
8056 break;
8057
8058 case 'k':
8059 case K_UP:
8060 /* with 'nowrap' it works just like the normal "k" command; also when
8061 * in a closed fold */
8062 if (!curwin->w_p_wrap
8063#ifdef FEAT_FOLDING
8064 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8065#endif
8066 )
8067 {
8068 oap->motion_type = MLINE;
8069 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8070 }
8071 else
8072 i = nv_screengo(oap, BACKWARD, cap->count1);
8073 if (i == FAIL)
8074 clearopbeep(oap);
8075 break;
8076
8077 /*
8078 * "gJ": join two lines without inserting a space.
8079 */
8080 case 'J':
8081 nv_join(cap);
8082 break;
8083
8084 /*
8085 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8086 * "gm": middle of "g0" and "g$".
8087 */
8088 case '^':
8089 flag = TRUE;
8090 /* FALLTHROUGH */
8091
8092 case '0':
8093 case 'm':
8094 case K_HOME:
8095 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 oap->motion_type = MCHAR;
8097 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008098 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008100 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 int width2 = width1 + curwin_col_off2();
8102
8103 validate_virtcol();
8104 i = 0;
8105 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8106 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8107 }
8108 else
8109 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008110 /* Go to the middle of the screen line. When 'number' or
8111 * 'relativenumber' is on and lines are wrapping the middle can be more
8112 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008114 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 + ((curwin->w_p_wrap && i > 0)
8116 ? curwin_col_off2() : 0)) / 2;
8117 coladvance((colnr_T)i);
8118 if (flag)
8119 {
8120 do
8121 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008122 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 }
8124 curwin->w_set_curswant = TRUE;
8125 break;
8126
8127 case '_':
8128 /* "g_": to the last non-blank character in the line or <count> lines
8129 * downward. */
8130 cap->oap->motion_type = MCHAR;
8131 cap->oap->inclusive = TRUE;
8132 curwin->w_curswant = MAXCOL;
8133 if (cursor_down((long)(cap->count1 - 1),
8134 cap->oap->op_type == OP_NOP) == FAIL)
8135 clearopbeep(cap->oap);
8136 else
8137 {
8138 char_u *ptr = ml_get_curline();
8139
8140 /* In Visual mode we may end up after the line. */
8141 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8142 --curwin->w_cursor.col;
8143
8144 /* Decrease the cursor column until it's on a non-blank. */
8145 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008146 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 --curwin->w_cursor.col;
8148 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008149 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 }
8151 break;
8152
8153 case '$':
8154 case K_END:
8155 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {
8157 int col_off = curwin_col_off();
8158
8159 oap->motion_type = MCHAR;
8160 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008161 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {
8163 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8164 if (cap->count1 == 1)
8165 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008166 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 int width2 = width1 + curwin_col_off2();
8168
8169 validate_virtcol();
8170 i = width1 - 1;
8171 if (curwin->w_virtcol >= (colnr_T)width1)
8172 i += ((curwin->w_virtcol - width1) / width2 + 1)
8173 * width2;
8174 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008175
8176 /* Make sure we stick in this column. */
8177 validate_virtcol();
8178 curwin->w_curswant = curwin->w_virtcol;
8179 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8181 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8182 {
8183 /*
8184 * Check for landing on a character that got split at
8185 * the end of the line. We do not want to advance to
8186 * the next screen line.
8187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 if (curwin->w_virtcol > (colnr_T)i)
8189 --curwin->w_cursor.col;
8190 }
8191#endif
8192 }
8193 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8194 clearopbeep(oap);
8195 }
8196 else
8197 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008198 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008200
8201 /* Make sure we stick in this column. */
8202 validate_virtcol();
8203 curwin->w_curswant = curwin->w_virtcol;
8204 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 }
8206 }
8207 break;
8208
8209 /*
8210 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8211 */
8212 case '*':
8213 case '#':
8214#if POUND != '#'
8215 case POUND: /* pound sign (sometimes equal to '#') */
8216#endif
8217 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8218 case ']': /* :tselect for current identifier */
8219 nv_ident(cap);
8220 break;
8221
8222 /*
8223 * ge and gE: go back to end of word
8224 */
8225 case 'e':
8226 case 'E':
8227 oap->motion_type = MCHAR;
8228 curwin->w_set_curswant = TRUE;
8229 oap->inclusive = TRUE;
8230 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8231 clearopbeep(oap);
8232 break;
8233
8234 /*
8235 * "g CTRL-G": display info about cursor position
8236 */
8237 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008238 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 break;
8240
8241 /*
8242 * "gi": start Insert at the last position.
8243 */
8244 case 'i':
8245 if (curbuf->b_last_insert.lnum != 0)
8246 {
8247 curwin->w_cursor = curbuf->b_last_insert;
8248 check_cursor_lnum();
8249 i = (int)STRLEN(ml_get_curline());
8250 if (curwin->w_cursor.col > (colnr_T)i)
8251 {
8252#ifdef FEAT_VIRTUALEDIT
8253 if (virtual_active())
8254 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8255#endif
8256 curwin->w_cursor.col = i;
8257 }
8258 }
8259 cap->cmdchar = 'i';
8260 nv_edit(cap);
8261 break;
8262
8263 /*
8264 * "gI": Start insert in column 1.
8265 */
8266 case 'I':
8267 beginline(0);
8268 if (!checkclearopq(oap))
8269 invoke_edit(cap, FALSE, 'g', FALSE);
8270 break;
8271
8272#ifdef FEAT_SEARCHPATH
8273 /*
8274 * "gf": goto file, edit file under cursor
8275 * "]f" and "[f": can also be used.
8276 */
8277 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008278 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 nv_gotofile(cap);
8280 break;
8281#endif
8282
8283 /* "g'm" and "g`m": jump to mark without setting pcmark */
8284 case '\'':
8285 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008286 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 case '`':
8288 nv_gomark(cap);
8289 break;
8290
8291 /*
8292 * "gs": Goto sleep.
8293 */
8294 case 's':
8295 do_sleep(cap->count1 * 1000L);
8296 break;
8297
8298 /*
8299 * "ga": Display the ascii value of the character under the
8300 * cursor. It is displayed in decimal, hex, and octal. -- webb
8301 */
8302 case 'a':
8303 do_ascii(NULL);
8304 break;
8305
8306#ifdef FEAT_MBYTE
8307 /*
8308 * "g8": Display the bytes used for the UTF-8 character under the
8309 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008310 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 */
8312 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008313 if (cap->count0 == 8)
8314 utf_find_illegal();
8315 else
8316 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 break;
8318#endif
8319
Bram Moolenaar60402d62017-04-20 18:54:50 +02008320 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008321 case '<':
8322 show_sb_text();
8323 break;
8324
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 /*
8326 * "gg": Goto the first line in file. With a count it goes to
8327 * that line number like for "G". -- webb
8328 */
8329 case 'g':
8330 cap->arg = FALSE;
8331 nv_goto(cap);
8332 break;
8333
8334 /*
8335 * Two-character operators:
8336 * "gq" Format text
8337 * "gw" Format text and keep cursor position
8338 * "g~" Toggle the case of the text.
8339 * "gu" Change text to lower case.
8340 * "gU" Change text to upper case.
8341 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008342 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 */
8344 case 'q':
8345 case 'w':
8346 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008347 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 case '~':
8349 case 'u':
8350 case 'U':
8351 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008352 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 nv_operator(cap);
8354 break;
8355
8356 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008357 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 * current function
8359 * "gD": idem, but in the current file.
8360 */
8361 case 'd':
8362 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008363 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 break;
8365
8366#ifdef FEAT_MOUSE
8367 /*
8368 * g<*Mouse> : <C-*mouse>
8369 */
8370 case K_MIDDLEMOUSE:
8371 case K_MIDDLEDRAG:
8372 case K_MIDDLERELEASE:
8373 case K_LEFTMOUSE:
8374 case K_LEFTDRAG:
8375 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008376 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 case K_RIGHTMOUSE:
8378 case K_RIGHTDRAG:
8379 case K_RIGHTRELEASE:
8380 case K_X1MOUSE:
8381 case K_X1DRAG:
8382 case K_X1RELEASE:
8383 case K_X2MOUSE:
8384 case K_X2DRAG:
8385 case K_X2RELEASE:
8386 mod_mask = MOD_MASK_CTRL;
8387 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8388 break;
8389#endif
8390
8391 case K_IGNORE:
8392 break;
8393
8394 /*
8395 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8396 */
8397 case 'p':
8398 case 'P':
8399 nv_put(cap);
8400 break;
8401
8402#ifdef FEAT_BYTEOFF
8403 /* "go": goto byte count from start of buffer */
8404 case 'o':
8405 goto_byte(cap->count0);
8406 break;
8407#endif
8408
8409 /* "gQ": improved Ex mode */
8410 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008411 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {
8413 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008414 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 break;
8416 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (!checkclearopq(oap))
8419 do_exmode(TRUE);
8420 break;
8421
8422#ifdef FEAT_JUMPLIST
8423 case ',':
8424 nv_pcmark(cap);
8425 break;
8426
8427 case ';':
8428 cap->count1 = -cap->count1;
8429 nv_pcmark(cap);
8430 break;
8431#endif
8432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008433 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008434 if (!checkclearop(oap))
8435 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008436 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008437 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008438 if (!checkclearop(oap))
8439 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008440 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008441
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008442 case '+':
8443 case '-': /* "g+" and "g-": undo or redo along the timeline */
8444 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008445 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008446 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008447 break;
8448
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 default:
8450 clearopbeep(oap);
8451 break;
8452 }
8453}
8454
8455/*
8456 * Handle "o" and "O" commands.
8457 */
8458 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008459n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008461#ifdef FEAT_CONCEAL
8462 linenr_T oldline = curwin->w_cursor.lnum;
8463#endif
8464
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 if (!checkclearopq(cap->oap))
8466 {
8467#ifdef FEAT_FOLDING
8468 if (cap->cmdchar == 'O')
8469 /* Open above the first line of a folded sequence of lines */
8470 (void)hasFolding(curwin->w_cursor.lnum,
8471 &curwin->w_cursor.lnum, NULL);
8472 else
8473 /* Open below the last line of a folded sequence of lines */
8474 (void)hasFolding(curwin->w_cursor.lnum,
8475 NULL, &curwin->w_cursor.lnum);
8476#endif
8477 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8478 (cap->cmdchar == 'O' ? 1 : 0)),
8479 (linenr_T)(curwin->w_cursor.lnum +
8480 (cap->cmdchar == 'o' ? 1 : 0))
8481 ) == OK
8482 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8483#ifdef FEAT_COMMENTS
8484 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8485#endif
8486 0, 0))
8487 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008488#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008489 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008490 update_single_line(curwin, oldline);
8491#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008492 /* When '#' is in 'cpoptions' ignore the count. */
8493 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8494 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008495#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008496 if (curwin->w_p_cul)
8497 /* force redraw of cursorline */
8498 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8501 }
8502 }
8503}
8504
8505/*
8506 * "." command: redo last change.
8507 */
8508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008509nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510{
8511 if (!checkclearopq(cap->oap))
8512 {
8513 /*
8514 * If "restart_edit" is TRUE, the last but one command is repeated
8515 * instead of the last command (inserting text). This is used for
8516 * CTRL-O <.> in insert mode.
8517 */
8518 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8519 clearopbeep(cap->oap);
8520 }
8521}
8522
8523/*
8524 * CTRL-R: undo undo
8525 */
8526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008527nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 if (!checkclearopq(cap->oap))
8530 {
8531 u_redo((int)cap->count1);
8532 curwin->w_set_curswant = TRUE;
8533 }
8534}
8535
8536/*
8537 * Handle "U" command.
8538 */
8539 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008540nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541{
8542 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008543 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {
8545 /* translate "gUU" to "gUgU" */
8546 cap->cmdchar = 'g';
8547 cap->nchar = 'U';
8548 nv_operator(cap);
8549 }
8550 else if (!checkclearopq(cap->oap))
8551 {
8552 u_undoline();
8553 curwin->w_set_curswant = TRUE;
8554 }
8555}
8556
8557/*
8558 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8559 * single character.
8560 */
8561 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008562nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008564 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 n_swapchar(cap);
8566 else
8567 nv_operator(cap);
8568}
8569
8570/*
8571 * Handle an operator command.
8572 * The actual work is done by do_pending_operator().
8573 */
8574 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008575nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577 int op_type;
8578
8579 op_type = get_op_type(cap->cmdchar, cap->nchar);
8580
8581 if (op_type == cap->oap->op_type) /* double operator works on lines */
8582 nv_lineop(cap);
8583 else if (!checkclearop(cap->oap))
8584 {
8585 cap->oap->start = curwin->w_cursor;
8586 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008587#ifdef FEAT_EVAL
8588 set_op_var(op_type);
8589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 }
8591}
8592
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008593#ifdef FEAT_EVAL
8594/*
8595 * Set v:operator to the characters for "optype".
8596 */
8597 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008598set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008599{
8600 char_u opchars[3];
8601
8602 if (optype == OP_NOP)
8603 set_vim_var_string(VV_OP, NULL, 0);
8604 else
8605 {
8606 opchars[0] = get_op_char(optype);
8607 opchars[1] = get_extra_op_char(optype);
8608 opchars[2] = NUL;
8609 set_vim_var_string(VV_OP, opchars, -1);
8610 }
8611}
8612#endif
8613
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614/*
8615 * Handle linewise operator "dd", "yy", etc.
8616 *
8617 * "_" is is a strange motion command that helps make operators more logical.
8618 * It is actually implemented, but not documented in the real Vi. This motion
8619 * command actually refers to "the current line". Commands like "dd" and "yy"
8620 * are really an alternate form of "d_" and "y_". It does accept a count, so
8621 * "d3_" works to delete 3 lines.
8622 */
8623 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008624nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
8626 cap->oap->motion_type = MLINE;
8627 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8628 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008629 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8630 && cap->oap->motion_force != 'v'
8631 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 || cap->oap->op_type == OP_LSHIFT
8633 || cap->oap->op_type == OP_RSHIFT)
8634 beginline(BL_SOL | BL_FIX);
8635 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8636 beginline(BL_WHITE | BL_FIX);
8637}
8638
8639/*
8640 * <Home> command.
8641 */
8642 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008643nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008645 /* CTRL-HOME is like "gg" */
8646 if (mod_mask & MOD_MASK_CTRL)
8647 nv_goto(cap);
8648 else
8649 {
8650 cap->count0 = 1;
8651 nv_pipe(cap);
8652 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008653 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8654 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655}
8656
8657/*
8658 * "|" command.
8659 */
8660 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008661nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662{
8663 cap->oap->motion_type = MCHAR;
8664 cap->oap->inclusive = FALSE;
8665 beginline(0);
8666 if (cap->count0 > 0)
8667 {
8668 coladvance((colnr_T)(cap->count0 - 1));
8669 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8670 }
8671 else
8672 curwin->w_curswant = 0;
8673 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008674 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 curwin->w_set_curswant = FALSE;
8676}
8677
8678/*
8679 * Handle back-word command "b" and "B".
8680 * cap->arg is 1 for "B"
8681 */
8682 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008683nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
8685 cap->oap->motion_type = MCHAR;
8686 cap->oap->inclusive = FALSE;
8687 curwin->w_set_curswant = TRUE;
8688 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8689 clearopbeep(cap->oap);
8690#ifdef FEAT_FOLDING
8691 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8692 foldOpenCursor();
8693#endif
8694}
8695
8696/*
8697 * Handle word motion commands "e", "E", "w" and "W".
8698 * cap->arg is TRUE for "E" and "W".
8699 */
8700 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008701nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 int n;
8704 int word_end;
8705 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008706 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707
8708 /*
8709 * Set inclusive for the "E" and "e" command.
8710 */
8711 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8712 word_end = TRUE;
8713 else
8714 word_end = FALSE;
8715 cap->oap->inclusive = word_end;
8716
8717 /*
8718 * "cw" and "cW" are a special case.
8719 */
8720 if (!word_end && cap->oap->op_type == OP_CHANGE)
8721 {
8722 n = gchar_cursor();
8723 if (n != NUL) /* not an empty line */
8724 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008725 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 {
8727 /*
8728 * Reproduce a funny Vi behaviour: "cw" on a blank only
8729 * changes one character, not all blanks until the start of
8730 * the next word. Only do this when the 'w' flag is included
8731 * in 'cpoptions'.
8732 */
8733 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8734 {
8735 cap->oap->inclusive = TRUE;
8736 cap->oap->motion_type = MCHAR;
8737 return;
8738 }
8739 }
8740 else
8741 {
8742 /*
8743 * This is a little strange. To match what the real Vi does,
8744 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8745 * that we are not on a space or a TAB. This seems impolite
8746 * at first, but it's really more what we mean when we say
8747 * 'cw'.
8748 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008749 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 * will change only one character! This is done by setting
8751 * flag.
8752 */
8753 cap->oap->inclusive = TRUE;
8754 word_end = TRUE;
8755 flag = TRUE;
8756 }
8757 }
8758 }
8759
8760 cap->oap->motion_type = MCHAR;
8761 curwin->w_set_curswant = TRUE;
8762 if (word_end)
8763 n = end_word(cap->count1, cap->arg, flag, FALSE);
8764 else
8765 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8766
Bram Moolenaardfefb982008-04-01 10:06:39 +00008767 /* Don't leave the cursor on the NUL past the end of line. Unless we
8768 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008769 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008770 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
8772 if (n == FAIL && cap->oap->op_type == OP_NOP)
8773 clearopbeep(cap->oap);
8774 else
8775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777#ifdef FEAT_FOLDING
8778 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8779 foldOpenCursor();
8780#endif
8781 }
8782}
8783
8784/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008785 * Used after a movement command: If the cursor ends up on the NUL after the
8786 * end of the line, may move it back to the last character and make the motion
8787 * inclusive.
8788 */
8789 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008790adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008791{
8792 /* The cursor cannot remain on the NUL when:
8793 * - the column is > 0
8794 * - not in Visual mode or 'selection' is "o"
8795 * - 'virtualedit' is not "all" and not "onemore".
8796 */
8797 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008798 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008799#ifdef FEAT_VIRTUALEDIT
8800 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8801#endif
8802 )
8803 {
8804 --curwin->w_cursor.col;
8805#ifdef FEAT_MBYTE
8806 /* prevent cursor from moving on the trail byte */
8807 if (has_mbyte)
8808 mb_adjust_cursor();
8809#endif
8810 oap->inclusive = TRUE;
8811 }
8812}
8813
8814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 * "0" and "^" commands.
8816 * cap->arg is the argument for beginline().
8817 */
8818 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008819nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 cap->oap->motion_type = MCHAR;
8822 cap->oap->inclusive = FALSE;
8823 beginline(cap->arg);
8824#ifdef FEAT_FOLDING
8825 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8826 foldOpenCursor();
8827#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008828 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8829 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830}
8831
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832/*
8833 * In exclusive Visual mode, may include the last character.
8834 */
8835 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008836adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837{
8838 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008839 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008841#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 if (has_mbyte)
8843 inc_cursor();
8844 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 ++curwin->w_cursor.col;
8847 cap->oap->inclusive = FALSE;
8848 }
8849}
8850
8851/*
8852 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8853 * Should check VIsual_mode before calling this.
8854 * Returns TRUE when backed up to the previous line.
8855 */
8856 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008857unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858{
8859 pos_T *pp;
8860
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008861 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008863 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 pp = &curwin->w_cursor;
8865 else
8866 pp = &VIsual;
8867#ifdef FEAT_VIRTUALEDIT
8868 if (pp->coladd > 0)
8869 --pp->coladd;
8870 else
8871#endif
8872 if (pp->col > 0)
8873 {
8874 --pp->col;
8875#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008876 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877#endif
8878 }
8879 else if (pp->lnum > 1)
8880 {
8881 --pp->lnum;
8882 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8883 return TRUE;
8884 }
8885 }
8886 return FALSE;
8887}
8888
8889/*
8890 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8891 */
8892 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008893nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 if (VIsual_active)
8896 VIsual_select = TRUE;
8897 else if (VIsual_reselect)
8898 {
8899 cap->nchar = 'v'; /* fake "gv" command */
8900 cap->arg = TRUE;
8901 nv_g_cmd(cap);
8902 }
8903}
8904
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905
8906/*
8907 * "G", "gg", CTRL-END, CTRL-HOME.
8908 * cap->arg is TRUE for "G".
8909 */
8910 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008911nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912{
8913 linenr_T lnum;
8914
8915 if (cap->arg)
8916 lnum = curbuf->b_ml.ml_line_count;
8917 else
8918 lnum = 1L;
8919 cap->oap->motion_type = MLINE;
8920 setpcmark();
8921
8922 /* When a count is given, use it instead of the default lnum */
8923 if (cap->count0 != 0)
8924 lnum = cap->count0;
8925 if (lnum < 1L)
8926 lnum = 1L;
8927 else if (lnum > curbuf->b_ml.ml_line_count)
8928 lnum = curbuf->b_ml.ml_line_count;
8929 curwin->w_cursor.lnum = lnum;
8930 beginline(BL_SOL | BL_FIX);
8931#ifdef FEAT_FOLDING
8932 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8933 foldOpenCursor();
8934#endif
8935}
8936
8937/*
8938 * CTRL-\ in Normal mode.
8939 */
8940 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008941nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942{
8943 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8944 {
8945 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008946 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 clear_cmdline = TRUE; /* unshow mode later */
8948 restart_edit = 0;
8949#ifdef FEAT_CMDWIN
8950 if (cmdwin_type != 0)
8951 cmdwin_result = Ctrl_C;
8952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 if (VIsual_active)
8954 {
8955 end_visual_mode(); /* stop Visual */
8956 redraw_curbuf_later(INVERTED);
8957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8959 if (cap->nchar == Ctrl_G && p_im)
8960 restart_edit = 'a';
8961 }
8962 else
8963 clearopbeep(cap->oap);
8964}
8965
8966/*
8967 * ESC in Normal mode: beep, but don't flush buffers.
8968 * Don't even beep if we are canceling a command.
8969 */
8970 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008971nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
8973 int no_reason;
8974
8975 no_reason = (cap->oap->op_type == OP_NOP
8976 && cap->opcount == 0
8977 && cap->count0 == 0
8978 && cap->oap->regname == 0
8979 && !p_im);
8980
8981 if (cap->arg) /* TRUE for CTRL-C */
8982 {
8983 if (restart_edit == 0
8984#ifdef FEAT_CMDWIN
8985 && cmdwin_type == 0
8986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02008989 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990
8991 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8992 * set again below when halfway a mapping. */
8993 if (!p_im)
8994 restart_edit = 0;
8995#ifdef FEAT_CMDWIN
8996 if (cmdwin_type != 0)
8997 {
8998 cmdwin_result = K_IGNORE;
8999 got_int = FALSE; /* don't stop executing autocommands et al. */
9000 return;
9001 }
9002#endif
9003 }
9004
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 if (VIsual_active)
9006 {
9007 end_visual_mode(); /* stop Visual */
9008 check_cursor_col(); /* make sure cursor is not beyond EOL */
9009 curwin->w_set_curswant = TRUE;
9010 redraw_curbuf_later(INVERTED);
9011 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009012 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009013 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 clearop(cap->oap);
9015
9016 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9017 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009018 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 restart_edit = 'a';
9020}
9021
9022/*
9023 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009024 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 */
9026 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009027nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028{
9029 /* <Insert> is equal to "i" */
9030 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9031 cap->cmdchar = 'i';
9032
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 /* in Visual mode "A" and "I" are an operator */
9034 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02009035 {
9036#ifdef FEAT_TERMINAL
9037 if (term_in_normal_mode())
9038 {
9039 end_visual_mode();
9040 clearop(cap->oap);
9041 term_enter_job_mode();
9042 return;
9043 }
9044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02009046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047
9048 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009049 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9050 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 {
9052#ifdef FEAT_TEXTOBJ
9053 nv_object(cap);
9054#else
9055 clearopbeep(cap->oap);
9056#endif
9057 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009058#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009059 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009060 {
9061 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009062 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009063 return;
9064 }
9065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 else if (!curbuf->b_p_ma && !p_im)
9067 {
9068 /* Only give this error when 'insertmode' is off. */
9069 EMSG(_(e_modifiable));
9070 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009071 if (cap->cmdchar == K_PS)
9072 /* drop the pasted text */
9073 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009075 else if (cap->cmdchar == K_PS && VIsual_active)
9076 {
9077 pos_T old_pos = curwin->w_cursor;
9078 pos_T old_visual = VIsual;
9079
9080 /* In Visual mode the selected text is deleted. */
9081 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9082 {
9083 shift_delete_registers();
9084 cap->oap->regname = '1';
9085 }
9086 else
9087 cap->oap->regname = '-';
9088 cap->cmdchar = 'd';
9089 cap->nchar = NUL;
9090 nv_operator(cap);
9091 do_pending_operator(cap, 0, FALSE);
9092 cap->cmdchar = K_PS;
9093
9094 /* When the last char in the line was deleted then append. Detect this
9095 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009096 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9097 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009098 inc_cursor();
9099
9100 /* Insert to replace the deleted text with the pasted text. */
9101 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 else if (!checkclearopq(cap->oap))
9104 {
9105 switch (cap->cmdchar)
9106 {
9107 case 'A': /* "A"ppend after the line */
9108 curwin->w_set_curswant = TRUE;
9109#ifdef FEAT_VIRTUALEDIT
9110 if (ve_flags == VE_ALL)
9111 {
9112 int save_State = State;
9113
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009114 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 * character past the end of the line */
9116 State = INSERT;
9117 coladvance((colnr_T)MAXCOL);
9118 State = save_State;
9119 }
9120 else
9121#endif
9122 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9123 break;
9124
9125 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009126 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9127 beginline(BL_WHITE);
9128 else
9129 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 break;
9131
Bram Moolenaara1891842017-02-04 21:34:31 +01009132 case K_PS:
9133 /* Bracketed paste works like "a"ppend, unless the cursor is in
9134 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009135 if (curwin->w_cursor.col == 0)
9136 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009137 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009138
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9140#ifdef FEAT_VIRTUALEDIT
9141 /* increment coladd when in virtual space, increment the
9142 * column otherwise, also to append after an unprintable char */
9143 if (virtual_active()
9144 && (curwin->w_cursor.coladd > 0
9145 || *ml_get_cursor() == NUL
9146 || *ml_get_cursor() == TAB))
9147 curwin->w_cursor.coladd++;
9148 else
9149#endif
9150 if (*ml_get_cursor() != NUL)
9151 inc_cursor();
9152 break;
9153 }
9154
9155#ifdef FEAT_VIRTUALEDIT
9156 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9157 {
9158 int save_State = State;
9159
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009160 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 * character past the end of the line */
9162 State = INSERT;
9163 coladvance(getviscol());
9164 State = save_State;
9165 }
9166#endif
9167
9168 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9169 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009170 else if (cap->cmdchar == K_PS)
9171 /* drop the pasted text */
9172 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173}
9174
9175/*
9176 * Invoke edit() and take care of "restart_edit" and the return value.
9177 */
9178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009179invoke_edit(
9180 cmdarg_T *cap,
9181 int repl, /* "r" or "gr" command */
9182 int cmd,
9183 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184{
9185 int restart_edit_save = 0;
9186
9187 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9188 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9189 * it. */
9190 if (repl || !stuff_empty())
9191 restart_edit_save = restart_edit;
9192 else
9193 restart_edit_save = 0;
9194
9195 /* Always reset "restart_edit", this is not a restarted edit. */
9196 restart_edit = 0;
9197
9198 if (edit(cmd, startln, cap->count1))
9199 cap->retval |= CA_COMMAND_BUSY;
9200
9201 if (restart_edit == 0)
9202 restart_edit = restart_edit_save;
9203}
9204
9205#ifdef FEAT_TEXTOBJ
9206/*
9207 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9208 */
9209 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009210nv_object(
9211 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
9213 int flag;
9214 int include;
9215 char_u *mps_save;
9216
9217 if (cap->cmdchar == 'i')
9218 include = FALSE; /* "ix" = inner object: exclude white space */
9219 else
9220 include = TRUE; /* "ax" = an object: include white space */
9221
9222 /* Make sure (), [], {} and <> are in 'matchpairs' */
9223 mps_save = curbuf->b_p_mps;
9224 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9225
9226 switch (cap->nchar)
9227 {
9228 case 'w': /* "aw" = a word */
9229 flag = current_word(cap->oap, cap->count1, include, FALSE);
9230 break;
9231 case 'W': /* "aW" = a WORD */
9232 flag = current_word(cap->oap, cap->count1, include, TRUE);
9233 break;
9234 case 'b': /* "ab" = a braces block */
9235 case '(':
9236 case ')':
9237 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9238 break;
9239 case 'B': /* "aB" = a Brackets block */
9240 case '{':
9241 case '}':
9242 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9243 break;
9244 case '[': /* "a[" = a [] block */
9245 case ']':
9246 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9247 break;
9248 case '<': /* "a<" = a <> block */
9249 case '>':
9250 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9251 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009252 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009253 /* Do not adjust oap->end in do_pending_operator()
9254 * otherwise there are different results for 'dit'
9255 * (note leading whitespace in last line):
9256 * 1) <b> 2) <b>
9257 * foobar foobar
9258 * </b> </b>
9259 */
9260 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009261 flag = current_tagblock(cap->oap, cap->count1, include);
9262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 case 'p': /* "ap" = a paragraph */
9264 flag = current_par(cap->oap, cap->count1, include, 'p');
9265 break;
9266 case 's': /* "as" = a sentence */
9267 flag = current_sent(cap->oap, cap->count1, include);
9268 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009269 case '"': /* "a"" = a double quoted string */
9270 case '\'': /* "a'" = a single quoted string */
9271 case '`': /* "a`" = a backtick quoted string */
9272 flag = current_quote(cap->oap, cap->count1, include,
9273 cap->nchar);
9274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275#if 0 /* TODO */
9276 case 'S': /* "aS" = a section */
9277 case 'f': /* "af" = a filename */
9278 case 'u': /* "au" = a URL */
9279#endif
9280 default:
9281 flag = FAIL;
9282 break;
9283 }
9284
9285 curbuf->b_p_mps = mps_save;
9286 if (flag == FAIL)
9287 clearopbeep(cap->oap);
9288 adjust_cursor_col();
9289 curwin->w_set_curswant = TRUE;
9290}
9291#endif
9292
9293/*
9294 * "q" command: Start/stop recording.
9295 * "q:", "q/", "q?": edit command-line in command-line window.
9296 */
9297 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009298nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 if (cap->oap->op_type == OP_FORMAT)
9301 {
9302 /* "gqq" is the same as "gqgq": format line */
9303 cap->cmdchar = 'g';
9304 cap->nchar = 'q';
9305 nv_operator(cap);
9306 }
9307 else if (!checkclearop(cap->oap))
9308 {
9309#ifdef FEAT_CMDWIN
9310 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9311 {
9312 stuffcharReadbuff(cap->nchar);
9313 stuffcharReadbuff(K_CMDWIN);
9314 }
9315 else
9316#endif
9317 /* (stop) recording into a named register, unless executing a
9318 * register */
9319 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9320 clearopbeep(cap->oap);
9321 }
9322}
9323
9324/*
9325 * Handle the "@r" command.
9326 */
9327 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009328nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329{
9330 if (checkclearop(cap->oap))
9331 return;
9332#ifdef FEAT_EVAL
9333 if (cap->nchar == '=')
9334 {
9335 if (get_expr_register() == NUL)
9336 return;
9337 }
9338#endif
9339 while (cap->count1-- && !got_int)
9340 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009341 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 {
9343 clearopbeep(cap->oap);
9344 break;
9345 }
9346 line_breakcheck();
9347 }
9348}
9349
9350/*
9351 * Handle the CTRL-U and CTRL-D commands.
9352 */
9353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009354nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9357 || (cap->cmdchar == Ctrl_D
9358 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9359 clearopbeep(cap->oap);
9360 else if (!checkclearop(cap->oap))
9361 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9362}
9363
9364/*
9365 * Handle "J" or "gJ" command.
9366 */
9367 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009368nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 if (VIsual_active) /* join the visual lines */
9371 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009372 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 {
9374 if (cap->count0 <= 1)
9375 cap->count0 = 2; /* default for join is two lines! */
9376 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9377 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009379 /* can't join when on the last line */
9380 if (cap->count0 <= 2)
9381 {
9382 clearopbeep(cap->oap);
9383 return;
9384 }
9385 cap->count0 = curbuf->b_ml.ml_line_count
9386 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009388
9389 prep_redo(cap->oap->regname, cap->count0,
9390 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9391 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 }
9393}
9394
9395/*
9396 * "P", "gP", "p" and "gp" commands.
9397 */
9398 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009399nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 int regname = 0;
9402 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009403 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009404 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 int dir;
9406 int flags = 0;
9407
9408 if (cap->oap->op_type != OP_NOP)
9409 {
9410#ifdef FEAT_DIFF
9411 /* "dp" is ":diffput" */
9412 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9413 {
9414 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009415 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 }
9417 else
9418#endif
9419 clearopbeep(cap->oap);
9420 }
9421 else
9422 {
9423 dir = (cap->cmdchar == 'P'
9424 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9425 ? BACKWARD : FORWARD;
9426 prep_redo_cmd(cap);
9427 if (cap->cmdchar == 'g')
9428 flags |= PUT_CURSEND;
9429
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 if (VIsual_active)
9431 {
9432 /* Putting in Visual mode: The put text replaces the selected
9433 * text. First delete the selected text, then put the new text.
9434 * Need to save and restore the registers that the delete
9435 * overwrites if the old contents is being put.
9436 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009437 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009439#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009441#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009442 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009443 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009444#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447
9448 )
9449 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009450 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 * put, save it first. */
9452 reg1 = get_register(regname, TRUE);
9453 }
9454
9455 /* Now delete the selected text. */
9456 cap->cmdchar = 'd';
9457 cap->nchar = NUL;
9458 cap->oap->regname = NUL;
9459 nv_operator(cap);
9460 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009461 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462
9463 /* delete PUT_LINE_BACKWARD; */
9464 cap->oap->regname = regname;
9465
9466 if (reg1 != NULL)
9467 {
9468 /* Delete probably changed the register we want to put, save
9469 * it first. Then put back what was there before the delete. */
9470 reg2 = get_register(regname, FALSE);
9471 put_register(regname, reg1);
9472 }
9473
9474 /* When deleted a linewise Visual area, put the register as
9475 * lines to avoid it joined with the next line. When deletion was
9476 * characterwise, split a line when putting lines. */
9477 if (VIsual_mode == 'V')
9478 flags |= PUT_LINE;
9479 else if (VIsual_mode == 'v')
9480 flags |= PUT_LINE_SPLIT;
9481 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9482 flags |= PUT_LINE_FORWARD;
9483 dir = BACKWARD;
9484 if ((VIsual_mode != 'V'
9485 && curwin->w_cursor.col < curbuf->b_op_start.col)
9486 || (VIsual_mode == 'V'
9487 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9488 /* cursor is at the end of the line or end of file, put
9489 * forward. */
9490 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009491 /* May have been reset in do_put(). */
9492 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 do_put(cap->oap->regname, dir, cap->count1, flags);
9495
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 /* If a register was saved, put it back now. */
9497 if (reg2 != NULL)
9498 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009499
9500 /* What to reselect with "gv"? Selecting the just put text seems to
9501 * be the most useful, since the original text was removed. */
9502 if (was_visual)
9503 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009504 curbuf->b_visual.vi_start = curbuf->b_op_start;
9505 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009506 /* need to adjust cursor position */
9507 if (*p_sel == 'e')
9508 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009509 }
9510
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009511 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009512 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009513 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009514 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009515 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009516
9517 /* If the cursor was in that line, move it to the end of the last
9518 * line. */
9519 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9520 {
9521 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9522 coladvance((colnr_T)MAXCOL);
9523 }
9524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 auto_format(FALSE, TRUE);
9526 }
9527}
9528
9529/*
9530 * "o" and "O" commands.
9531 */
9532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009533nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535#ifdef FEAT_DIFF
9536 /* "do" is ":diffget" */
9537 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9538 {
9539 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009540 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 }
9542 else
9543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 if (VIsual_active) /* switch start and end of visual */
9545 v_swap_corners(cap->cmdchar);
9546 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 n_opencmd(cap);
9548}
9549
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550#ifdef FEAT_NETBEANS_INTG
9551 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009552nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554 netbeans_keycommand(cap->nchar);
9555}
9556#endif
9557
9558#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009560nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561{
9562 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9563}
9564#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009565
9566#ifdef FEAT_AUTOCMD
9567/*
9568 * Trigger CursorHold event.
9569 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9570 * input buffer. "did_cursorhold" is set to avoid retriggering.
9571 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009572 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009573nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009574{
9575 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9576 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009577 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009578}
9579#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009580
9581/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009582 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009583 */
9584 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009585get_op_vcol(
9586 oparg_T *oap,
9587 colnr_T redo_VIsual_vcol,
9588 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009589{
9590 colnr_T start, end;
9591
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009592 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009593 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009594 return;
9595
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009596 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009597
9598#ifdef FEAT_MBYTE
9599 /* prevent from moving onto a trail byte */
9600 if (has_mbyte)
9601 mb_adjustpos(curwin->w_buffer, &oap->end);
9602#endif
9603
9604 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009605
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009606 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009607 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009608 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9609
9610 if (start < oap->start_vcol)
9611 oap->start_vcol = start;
9612 if (end > oap->end_vcol)
9613 {
9614 if (initial && *p_sel == 'e' && start >= 1
9615 && start - 1 >= oap->end_vcol)
9616 oap->end_vcol = start - 1;
9617 else
9618 oap->end_vcol = end;
9619 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009620 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009621
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009622 /* if '$' was used, get oap->end_vcol from longest line */
9623 if (curwin->w_curswant == MAXCOL)
9624 {
9625 curwin->w_cursor.col = MAXCOL;
9626 oap->end_vcol = 0;
9627 for (curwin->w_cursor.lnum = oap->start.lnum;
9628 curwin->w_cursor.lnum <= oap->end.lnum;
9629 ++curwin->w_cursor.lnum)
9630 {
9631 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9632 if (end > oap->end_vcol)
9633 oap->end_vcol = end;
9634 }
9635 }
9636 else if (redo_VIsual_busy)
9637 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9638 /*
9639 * Correct oap->end.col and oap->start.col to be the
9640 * upper-left and lower-right corner of the block area.
9641 *
9642 * (Actually, this does convert column positions into character
9643 * positions)
9644 */
9645 curwin->w_cursor.lnum = oap->end.lnum;
9646 coladvance(oap->end_vcol);
9647 oap->end = curwin->w_cursor;
9648
9649 curwin->w_cursor = oap->start;
9650 coladvance(oap->start_vcol);
9651 oap->start = curwin->w_cursor;
9652}