blob: cea8c9f167562e780ff7630bf223a9d08d67e435 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017/*
18 * The Visual area is remembered for reselection.
19 */
20static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
21static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020022static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar7d311c52014-02-22 23:49:35 +010023static int VIsual_mode_orig = NUL; /* saved Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010027#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020031#ifdef __BORLANDC__
32 _RTLENTRYF
33#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034 nv_compare(const void *s1, const void *s2);
35static int find_command(int cmdchar);
36static void op_colon(oparg_T *oap);
37static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010038#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void find_start_of_word(pos_T *);
40static void find_end_of_word(pos_T *);
41static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void prep_redo_cmd(cmdarg_T *cap);
44static void prep_redo(int regname, long, int, int, int, int, int);
45static int checkclearop(oparg_T *oap);
46static int checkclearopq(oparg_T *oap);
47static void clearop(oparg_T *oap);
48static void clearopbeep(oparg_T *oap);
49static void unshift_special(cmdarg_T *cap);
50static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010052static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#endif
54
55/*
56 * nv_*(): functions called to handle Normal and Visual mode commands.
57 * n_*(): functions called to handle Normal mode commands.
58 * v_*(): functions called to handle Visual mode commands.
59 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_ignore(cmdarg_T *cap);
61static void nv_nop(cmdarg_T *cap);
62static void nv_error(cmdarg_T *cap);
63static void nv_help(cmdarg_T *cap);
64static void nv_addsub(cmdarg_T *cap);
65static void nv_page(cmdarg_T *cap);
66static void nv_gd(oparg_T *oap, int nchar, int thisblock);
67static int nv_screengo(oparg_T *oap, int dir, long dist);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_mousescroll(cmdarg_T *cap);
70static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010072static void nv_scroll_line(cmdarg_T *cap);
73static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_ver_scrollbar(cmdarg_T *cap);
76static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000078#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_tabline(cmdarg_T *cap);
80static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010082static void nv_exmode(cmdarg_T *cap);
83static void nv_colon(cmdarg_T *cap);
84static void nv_ctrlg(cmdarg_T *cap);
85static void nv_ctrlh(cmdarg_T *cap);
86static void nv_clear(cmdarg_T *cap);
87static void nv_ctrlo(cmdarg_T *cap);
88static void nv_hat(cmdarg_T *cap);
89static void nv_Zet(cmdarg_T *cap);
90static void nv_ident(cmdarg_T *cap);
91static void nv_tagpop(cmdarg_T *cap);
92static void nv_scroll(cmdarg_T *cap);
93static void nv_right(cmdarg_T *cap);
94static void nv_left(cmdarg_T *cap);
95static void nv_up(cmdarg_T *cap);
96static void nv_down(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_SEARCHPATH
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void nv_gotofile(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_end(cmdarg_T *cap);
101static void nv_dollar(cmdarg_T *cap);
102static void nv_search(cmdarg_T *cap);
103static void nv_next(cmdarg_T *cap);
104static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
105static void nv_csearch(cmdarg_T *cap);
106static void nv_brackets(cmdarg_T *cap);
107static void nv_percent(cmdarg_T *cap);
108static void nv_brace(cmdarg_T *cap);
109static void nv_mark(cmdarg_T *cap);
110static void nv_findpar(cmdarg_T *cap);
111static void nv_undo(cmdarg_T *cap);
112static void nv_kundo(cmdarg_T *cap);
113static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_VREPLACE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_vreplace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void v_swap_corners(int cmdchar);
118static void nv_replace(cmdarg_T *cap);
119static void n_swapchar(cmdarg_T *cap);
120static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
121static void v_visop(cmdarg_T *cap);
122static void nv_subst(cmdarg_T *cap);
123static void nv_abbrev(cmdarg_T *cap);
124static void nv_optrans(cmdarg_T *cap);
125static void nv_gomark(cmdarg_T *cap);
126static void nv_pcmark(cmdarg_T *cap);
127static void nv_regname(cmdarg_T *cap);
128static void nv_visual(cmdarg_T *cap);
129static void n_start_visual_mode(int c);
130static void nv_window(cmdarg_T *cap);
131static void nv_suspend(cmdarg_T *cap);
132static void nv_g_cmd(cmdarg_T *cap);
133static void n_opencmd(cmdarg_T *cap);
134static void nv_dot(cmdarg_T *cap);
135static void nv_redo(cmdarg_T *cap);
136static void nv_Undo(cmdarg_T *cap);
137static void nv_tilde(cmdarg_T *cap);
138static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000139#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000141#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100142static void nv_lineop(cmdarg_T *cap);
143static void nv_home(cmdarg_T *cap);
144static void nv_pipe(cmdarg_T *cap);
145static void nv_bck_word(cmdarg_T *cap);
146static void nv_wordcmd(cmdarg_T *cap);
147static void nv_beginline(cmdarg_T *cap);
148static void adjust_cursor(oparg_T *oap);
149static void adjust_for_sel(cmdarg_T *cap);
150static int unadjust_for_sel(void);
151static void nv_select(cmdarg_T *cap);
152static void nv_goto(cmdarg_T *cap);
153static void nv_normal(cmdarg_T *cap);
154static void nv_esc(cmdarg_T *oap);
155static void nv_edit(cmdarg_T *cap);
156static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100158static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100160static void nv_record(cmdarg_T *cap);
161static void nv_at(cmdarg_T *cap);
162static void nv_halfpage(cmdarg_T *cap);
163static void nv_join(cmdarg_T *cap);
164static void nv_put(cmdarg_T *cap);
165static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100167static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100170static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000172#ifdef FEAT_AUTOCMD
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100173static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar3918c952005-03-15 22:34:55 +0000174#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100175static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000177static char *e_noident = N_("E349: No identifier under cursor");
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179/*
180 * Function to be called for a Normal or Visual mode command.
181 * The argument is a cmdarg_T.
182 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100183typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
185/* Values for cmd_flags. */
186#define NV_NCH 0x01 /* may need to get a second char */
187#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
188#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
189#define NV_LANG 0x08 /* second char needs language adjustment */
190
191#define NV_SS 0x10 /* may start selection */
192#define NV_SSS 0x20 /* may start selection with shift modifier */
193#define NV_STS 0x40 /* may stop selection without shift modif. */
194#define NV_RL 0x80 /* 'rightleft' modifies command */
195#define NV_KEEPREG 0x100 /* don't clear regname */
196#define NV_NCW 0x200 /* not allowed in command-line window */
197
198/*
199 * Generally speaking, every Normal mode command should either clear any
200 * pending operator (with *clearop*()), or set the motion type variable
201 * oap->motion_type.
202 *
203 * When a cursor motion command is made, it is marked as being a character or
204 * line oriented motion. Then, if an operator is in effect, the operation
205 * becomes character or line oriented accordingly.
206 */
207
208/*
209 * This table contains one entry for every Normal or Visual mode command.
210 * The order doesn't matter, init_normal_cmds() will create a sorted index.
211 * It is faster when all keys from zero to '~' are present.
212 */
213static const struct nv_cmd
214{
215 int cmd_char; /* (first) command character */
216 nv_func_T cmd_func; /* function for this command */
217 short_u cmd_flags; /* NV_ flags */
218 short cmd_arg; /* value for ca.arg */
219} nv_cmds[] =
220{
221 {NUL, nv_error, 0, 0},
222 {Ctrl_A, nv_addsub, 0, 0},
223 {Ctrl_B, nv_page, NV_STS, BACKWARD},
224 {Ctrl_C, nv_esc, 0, TRUE},
225 {Ctrl_D, nv_halfpage, 0, 0},
226 {Ctrl_E, nv_scroll_line, 0, TRUE},
227 {Ctrl_F, nv_page, NV_STS, FORWARD},
228 {Ctrl_G, nv_ctrlg, 0, 0},
229 {Ctrl_H, nv_ctrlh, 0, 0},
230 {Ctrl_I, nv_pcmark, 0, 0},
231 {NL, nv_down, 0, FALSE},
232 {Ctrl_K, nv_error, 0, 0},
233 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000234 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {Ctrl_N, nv_down, NV_STS, FALSE},
236 {Ctrl_O, nv_ctrlo, 0, 0},
237 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000238 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 {Ctrl_R, nv_redo, 0, 0},
240 {Ctrl_S, nv_ignore, 0, 0},
241 {Ctrl_T, nv_tagpop, NV_NCW, 0},
242 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 {Ctrl_V, nv_visual, 0, FALSE},
244 {'V', nv_visual, 0, FALSE},
245 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 {Ctrl_W, nv_window, 0, 0},
247 {Ctrl_X, nv_addsub, 0, 0},
248 {Ctrl_Y, nv_scroll_line, 0, FALSE},
249 {Ctrl_Z, nv_suspend, 0, 0},
250 {ESC, nv_esc, 0, FALSE},
251 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
252 {Ctrl_RSB, nv_ident, NV_NCW, 0},
253 {Ctrl_HAT, nv_hat, NV_NCW, 0},
254 {Ctrl__, nv_error, 0, 0},
255 {' ', nv_right, 0, 0},
256 {'!', nv_operator, 0, 0},
257 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
258 {'#', nv_ident, 0, 0},
259 {'$', nv_dollar, 0, 0},
260 {'%', nv_percent, 0, 0},
261 {'&', nv_optrans, 0, 0},
262 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
263 {'(', nv_brace, 0, BACKWARD},
264 {')', nv_brace, 0, FORWARD},
265 {'*', nv_ident, 0, 0},
266 {'+', nv_down, 0, TRUE},
267 {',', nv_csearch, 0, TRUE},
268 {'-', nv_up, 0, TRUE},
269 {'.', nv_dot, NV_KEEPREG, 0},
270 {'/', nv_search, 0, FALSE},
271 {'0', nv_beginline, 0, 0},
272 {'1', nv_ignore, 0, 0},
273 {'2', nv_ignore, 0, 0},
274 {'3', nv_ignore, 0, 0},
275 {'4', nv_ignore, 0, 0},
276 {'5', nv_ignore, 0, 0},
277 {'6', nv_ignore, 0, 0},
278 {'7', nv_ignore, 0, 0},
279 {'8', nv_ignore, 0, 0},
280 {'9', nv_ignore, 0, 0},
281 {':', nv_colon, 0, 0},
282 {';', nv_csearch, 0, FALSE},
283 {'<', nv_operator, NV_RL, 0},
284 {'=', nv_operator, 0, 0},
285 {'>', nv_operator, NV_RL, 0},
286 {'?', nv_search, 0, FALSE},
287 {'@', nv_at, NV_NCH_NOP, FALSE},
288 {'A', nv_edit, 0, 0},
289 {'B', nv_bck_word, 0, 1},
290 {'C', nv_abbrev, NV_KEEPREG, 0},
291 {'D', nv_abbrev, NV_KEEPREG, 0},
292 {'E', nv_wordcmd, 0, TRUE},
293 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
294 {'G', nv_goto, 0, TRUE},
295 {'H', nv_scroll, 0, 0},
296 {'I', nv_edit, 0, 0},
297 {'J', nv_join, 0, 0},
298 {'K', nv_ident, 0, 0},
299 {'L', nv_scroll, 0, 0},
300 {'M', nv_scroll, 0, 0},
301 {'N', nv_next, 0, SEARCH_REV},
302 {'O', nv_open, 0, 0},
303 {'P', nv_put, 0, 0},
304 {'Q', nv_exmode, NV_NCW, 0},
305 {'R', nv_Replace, 0, FALSE},
306 {'S', nv_subst, NV_KEEPREG, 0},
307 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
308 {'U', nv_Undo, 0, 0},
309 {'W', nv_wordcmd, 0, TRUE},
310 {'X', nv_abbrev, NV_KEEPREG, 0},
311 {'Y', nv_abbrev, NV_KEEPREG, 0},
312 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
313 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
314 {'\\', nv_error, 0, 0},
315 {']', nv_brackets, NV_NCH_ALW, FORWARD},
316 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
317 {'_', nv_lineop, 0, 0},
318 {'`', nv_gomark, NV_NCH_ALW, FALSE},
319 {'a', nv_edit, NV_NCH, 0},
320 {'b', nv_bck_word, 0, 0},
321 {'c', nv_operator, 0, 0},
322 {'d', nv_operator, 0, 0},
323 {'e', nv_wordcmd, 0, FALSE},
324 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
325 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
326 {'h', nv_left, NV_RL, 0},
327 {'i', nv_edit, NV_NCH, 0},
328 {'j', nv_down, 0, FALSE},
329 {'k', nv_up, 0, FALSE},
330 {'l', nv_right, NV_RL, 0},
331 {'m', nv_mark, NV_NCH_NOP, 0},
332 {'n', nv_next, 0, 0},
333 {'o', nv_open, 0, 0},
334 {'p', nv_put, 0, 0},
335 {'q', nv_record, NV_NCH, 0},
336 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
337 {'s', nv_subst, NV_KEEPREG, 0},
338 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
339 {'u', nv_undo, 0, 0},
340 {'w', nv_wordcmd, 0, FALSE},
341 {'x', nv_abbrev, NV_KEEPREG, 0},
342 {'y', nv_operator, 0, 0},
343 {'z', nv_zet, NV_NCH_ALW, 0},
344 {'{', nv_findpar, 0, BACKWARD},
345 {'|', nv_pipe, 0, 0},
346 {'}', nv_findpar, 0, FORWARD},
347 {'~', nv_tilde, 0, 0},
348
349 /* pound sign */
350 {POUND, nv_ident, 0, 0},
351#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200352 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
353 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
354 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
355 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {K_LEFTMOUSE, nv_mouse, 0, 0},
357 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
358 {K_LEFTDRAG, nv_mouse, 0, 0},
359 {K_LEFTRELEASE, nv_mouse, 0, 0},
360 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100361 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
363 {K_MIDDLEDRAG, nv_mouse, 0, 0},
364 {K_MIDDLERELEASE, nv_mouse, 0, 0},
365 {K_RIGHTMOUSE, nv_mouse, 0, 0},
366 {K_RIGHTDRAG, nv_mouse, 0, 0},
367 {K_RIGHTRELEASE, nv_mouse, 0, 0},
368 {K_X1MOUSE, nv_mouse, 0, 0},
369 {K_X1DRAG, nv_mouse, 0, 0},
370 {K_X1RELEASE, nv_mouse, 0, 0},
371 {K_X2MOUSE, nv_mouse, 0, 0},
372 {K_X2DRAG, nv_mouse, 0, 0},
373 {K_X2RELEASE, nv_mouse, 0, 0},
374#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000375 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000376 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 {K_INS, nv_edit, 0, 0},
378 {K_KINS, nv_edit, 0, 0},
379 {K_BS, nv_ctrlh, 0, 0},
380 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
381 {K_S_UP, nv_page, NV_SS, BACKWARD},
382 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
383 {K_S_DOWN, nv_page, NV_SS, FORWARD},
384 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
385 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
386 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
387 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
388 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
389 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
390 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
391 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
392 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
393 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
394 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
395 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {K_S_END, nv_end, NV_SS, FALSE},
397 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
398 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
399 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {K_S_HOME, nv_home, NV_SS, 0},
401 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
402 {K_DEL, nv_abbrev, 0, 0},
403 {K_KDEL, nv_abbrev, 0, 0},
404 {K_UNDO, nv_kundo, 0, 0},
405 {K_HELP, nv_help, NV_NCW, 0},
406 {K_F1, nv_help, NV_NCW, 0},
407 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_GUI
410 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
411 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
412#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000413#ifdef FEAT_GUI_TABLINE
414 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000415 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#ifdef FEAT_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200418 {K_F8, farsi_f8, 0, 0},
419 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#ifdef FEAT_NETBEANS_INTG
422 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
423#endif
424#ifdef FEAT_DND
425 {K_DROP, nv_drop, NV_STS, 0},
426#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +0000427#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000428 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaar3918c952005-03-15 22:34:55 +0000429#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +0100430 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431};
432
433/* Number of commands in nv_cmds[]. */
434#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
435
436/* Sorted index of commands in nv_cmds[]. */
437static short nv_cmd_idx[NV_CMDS_SIZE];
438
439/* The highest index for which
440 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
441static int nv_max_linear;
442
443/*
444 * Compare functions for qsort() below, that checks the command character
445 * through the index in nv_cmd_idx[].
446 */
447 static int
448#ifdef __BORLANDC__
449_RTLENTRYF
450#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100451nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452{
453 int c1, c2;
454
455 /* The commands are sorted on absolute value. */
456 c1 = nv_cmds[*(const short *)s1].cmd_char;
457 c2 = nv_cmds[*(const short *)s2].cmd_char;
458 if (c1 < 0)
459 c1 = -c1;
460 if (c2 < 0)
461 c2 = -c2;
462 return c1 - c2;
463}
464
465/*
466 * Initialize the nv_cmd_idx[] table.
467 */
468 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100469init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int i;
472
473 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000474 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 nv_cmd_idx[i] = i;
476
477 /* Sort the commands by the command character. */
478 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
479
480 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000481 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
483 break;
484 nv_max_linear = i - 1;
485}
486
487/*
488 * Search for a command in the commands table.
489 * Returns -1 for invalid command.
490 */
491 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 int i;
495 int idx;
496 int top, bot;
497 int c;
498
499#ifdef FEAT_MBYTE
500 /* A multi-byte character is never a command. */
501 if (cmdchar >= 0x100)
502 return -1;
503#endif
504
505 /* We use the absolute value of the character. Special keys have a
506 * negative value, but are sorted on their absolute value. */
507 if (cmdchar < 0)
508 cmdchar = -cmdchar;
509
510 /* If the character is in the first part: The character is the index into
511 * nv_cmd_idx[]. */
512 if (cmdchar <= nv_max_linear)
513 return nv_cmd_idx[cmdchar];
514
515 /* Perform a binary search. */
516 bot = nv_max_linear + 1;
517 top = NV_CMDS_SIZE - 1;
518 idx = -1;
519 while (bot <= top)
520 {
521 i = (top + bot) / 2;
522 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
523 if (c < 0)
524 c = -c;
525 if (cmdchar == c)
526 {
527 idx = nv_cmd_idx[i];
528 break;
529 }
530 if (cmdchar > c)
531 bot = i + 1;
532 else
533 top = i - 1;
534 }
535 return idx;
536}
537
538/*
539 * Execute a command in Normal mode.
540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100542normal_cmd(
543 oparg_T *oap,
544 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 cmdarg_T ca; /* command arguments */
547 int c;
548 int ctrl_w = FALSE; /* got CTRL-W command */
549 int old_col = curwin->w_curswant;
550#ifdef FEAT_CMDL_INFO
551 int need_flushbuf; /* need to call out_flush() */
552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 pos_T old_pos; /* cursor position before command */
554 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 static int old_mapped_len = 0;
556 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000557#ifdef FEAT_EVAL
558 int set_prevcount = FALSE;
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
562 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000563
564 /* Use a count remembered from before entering an operator. After typing
565 * "3d" we return from normal_cmd() and come back here, the "3" is
566 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 ca.opcount = opcount;
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 /*
570 * If there is an operator pending, then the command we take this time
571 * will terminate it. Finish_op tells us to finish the operation before
572 * returning this time (unless the operation was cancelled).
573 */
574#ifdef CURSOR_SHAPE
575 c = finish_op;
576#endif
577 finish_op = (oap->op_type != OP_NOP);
578#ifdef CURSOR_SHAPE
579 if (finish_op != c)
580 {
581 ui_cursor_shape(); /* may show different cursor shape */
582# ifdef FEAT_MOUSESHAPE
583 update_mouseshape(-1);
584# endif
585 }
586#endif
587
Bram Moolenaara983fe92008-07-31 20:04:27 +0000588 /* When not finishing an operator and no register name typed, reset the
589 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000593#ifdef FEAT_EVAL
594 set_prevcount = TRUE;
595#endif
596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaara983fe92008-07-31 20:04:27 +0000598#ifdef FEAT_AUTOCMD
599 /* Restore counts from before receiving K_CURSORHOLD. This means after
600 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
601 * "3 * 2". */
602 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
603 {
604 ca.opcount = oap->prev_opcount;
605 ca.count0 = oap->prev_count0;
606 oap->prev_opcount = 0;
607 oap->prev_count0 = 0;
608 }
609#endif
610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613 State = NORMAL_BUSY;
614#ifdef USE_ON_FLY_SCROLL
615 dont_scroll = FALSE; /* allow scrolling here */
616#endif
617
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100618#ifdef FEAT_EVAL
619 /* Set v:count here, when called from main() and not a stuffed
620 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100621 * when there is no count. Do set it for redo. */
622 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100623 set_vcount_ca(&ca, &set_prevcount);
624#endif
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 /*
627 * Get the command character from the user.
628 */
629 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100630 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632 /*
633 * If a mapping was started in Visual or Select mode, remember the length
634 * of the mapping. This is used below to not return to Insert mode for as
635 * long as the mapping is being executed.
636 */
637 if (restart_edit == 0)
638 old_mapped_len = 0;
639 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000640 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 old_mapped_len = typebuf_maplen();
642
643 if (c == NUL)
644 c = K_ZERO;
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /*
647 * In Select mode, typed text replaces the selection.
648 */
649 if (VIsual_active
650 && VIsual_select
651 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
652 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000653 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
654 * 'insertmode' is set) fake a "d"elete command, Insert mode will
655 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000656 * Insert the typed character in the typeahead buffer, so that it can
657 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000658 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000659 if (restart_edit != 0)
660 c = 'd';
661 else
662 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000663 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200664 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667#ifdef FEAT_CMDL_INFO
668 need_flushbuf = add_to_showcmd(c);
669#endif
670
671getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
674 /*
675 * Handle a count before a command and compute ca.count0.
676 * Note that '0' is a command and not the start of a count, but it's
677 * part of a count after other digits.
678 */
679 while ( (c >= '1' && c <= '9')
680 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
681 {
682 if (c == K_DEL || c == K_KDEL)
683 {
684 ca.count0 /= 10;
685#ifdef FEAT_CMDL_INFO
686 del_from_showcmd(4); /* delete the digit and ~@% */
687#endif
688 }
689 else
690 ca.count0 = ca.count0 * 10 + (c - '0');
691 if (ca.count0 < 0) /* got too large! */
692 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000693#ifdef FEAT_EVAL
694 /* Set v:count here, when called from main() and not a stuffed
695 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100696 * right after the count. Do set it for redo. */
697 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100698 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (ctrl_w)
701 {
702 ++no_mapping;
703 ++allow_keys; /* no mapping for nchar, but keys */
704 }
705 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000706 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 --no_zero_mapping;
709 if (ctrl_w)
710 {
711 --no_mapping;
712 --allow_keys;
713 }
714#ifdef FEAT_CMDL_INFO
715 need_flushbuf |= add_to_showcmd(c);
716#endif
717 }
718
719 /*
720 * If we got CTRL-W there may be a/another count
721 */
722 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
723 {
724 ctrl_w = TRUE;
725 ca.opcount = ca.count0; /* remember first count */
726 ca.count0 = 0;
727 ++no_mapping;
728 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000729 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 --no_mapping;
732 --allow_keys;
733#ifdef FEAT_CMDL_INFO
734 need_flushbuf |= add_to_showcmd(c);
735#endif
736 goto getcount; /* jump back */
737 }
738 }
739
Bram Moolenaara983fe92008-07-31 20:04:27 +0000740#ifdef FEAT_AUTOCMD
741 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000743 /* Save the count values so that ca.opcount and ca.count0 are exactly
744 * the same when coming back here after handling K_CURSORHOLD. */
745 oap->prev_opcount = ca.opcount;
746 oap->prev_count0 = ca.count0;
747 }
748 else
749#endif
750 if (ca.opcount != 0)
751 {
752 /*
753 * If we're in the middle of an operator (including after entering a
754 * yank buffer with '"') AND we had a count before the operator, then
755 * that count overrides the current value of ca.count0.
756 * What this means effectively, is that commands like "3dw" get turned
757 * into "d3w" which makes things fall into place pretty neatly.
758 * If you give a count before AND after the operator, they are
759 * multiplied.
760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (ca.count0)
762 ca.count0 *= ca.opcount;
763 else
764 ca.count0 = ca.opcount;
765 }
766
767 /*
768 * Always remember the count. It will be set to zero (on the next call,
769 * above) when there is no pending operator.
770 * When called from main(), save the count for use by the "count" built-in
771 * variable.
772 */
773 ca.opcount = ca.count0;
774 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
775
776#ifdef FEAT_EVAL
777 /*
778 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100779 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100781 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000782 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783#endif
784
785 /*
786 * Find the command character in the table of commands.
787 * For CTRL-W we already got nchar when looking for a count.
788 */
789 if (ctrl_w)
790 {
791 ca.nchar = c;
792 ca.cmdchar = Ctrl_W;
793 }
794 else
795 ca.cmdchar = c;
796 idx = find_command(ca.cmdchar);
797 if (idx < 0)
798 {
799 /* Not a known command: beep. */
800 clearopbeep(oap);
801 goto normal_end;
802 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000803
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000804 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200806 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000808 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 goto normal_end;
810 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000811#ifdef FEAT_AUTOCMD
812 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
813 goto normal_end;
814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 /*
817 * In Visual/Select mode, a few keys are handled in a special way.
818 */
819 if (VIsual_active)
820 {
821 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
822 if (km_stopsel
823 && (nv_cmds[idx].cmd_flags & NV_STS)
824 && !(mod_mask & MOD_MASK_SHIFT))
825 {
826 end_visual_mode();
827 redraw_curbuf_later(INVERTED);
828 }
829
830 /* Keys that work different when 'keymodel' contains "startsel" */
831 if (km_startsel)
832 {
833 if (nv_cmds[idx].cmd_flags & NV_SS)
834 {
835 unshift_special(&ca);
836 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000837 if (idx < 0)
838 {
839 /* Just in case */
840 clearopbeep(oap);
841 goto normal_end;
842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
845 && (mod_mask & MOD_MASK_SHIFT))
846 {
847 mod_mask &= ~MOD_MASK_SHIFT;
848 }
849 }
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852#ifdef FEAT_RIGHTLEFT
853 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
854 && (nv_cmds[idx].cmd_flags & NV_RL))
855 {
856 /* Invert horizontal movements and operations. Only when typed by the
857 * user directly, not when the result of a mapping or "x" translated
858 * to "dl". */
859 switch (ca.cmdchar)
860 {
861 case 'l': ca.cmdchar = 'h'; break;
862 case K_RIGHT: ca.cmdchar = K_LEFT; break;
863 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
864 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
865 case 'h': ca.cmdchar = 'l'; break;
866 case K_LEFT: ca.cmdchar = K_RIGHT; break;
867 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
868 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
869 case '>': ca.cmdchar = '<'; break;
870 case '<': ca.cmdchar = '>'; break;
871 }
872 idx = find_command(ca.cmdchar);
873 }
874#endif
875
876 /*
877 * Get an additional character if we need one.
878 */
879 if ((nv_cmds[idx].cmd_flags & NV_NCH)
880 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
881 && oap->op_type == OP_NOP)
882 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
883 || (ca.cmdchar == 'q'
884 && oap->op_type == OP_NOP
885 && !Recording
886 && !Exec_reg)
887 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100888 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
890 int *cp;
891 int repl = FALSE; /* get character for replace mode */
892 int lit = FALSE; /* get extra character literally */
893 int langmap_active = FALSE; /* using :lmap mappings */
894 int lang; /* getting a text character */
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100895#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 int save_smd; /* saved value of p_smd */
897#endif
898
899 ++no_mapping;
900 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000901#ifdef FEAT_AUTOCMD
902 /* Don't generate a CursorHold event here, most commands can't handle
903 * it, e.g., nv_replace(), nv_csearch(). */
904 did_cursorhold = TRUE;
905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (ca.cmdchar == 'g')
907 {
908 /*
909 * For 'g' get the next character now, so that we can check for
910 * "gr", "g'" and "g`".
911 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000912 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#ifdef FEAT_CMDL_INFO
915 need_flushbuf |= add_to_showcmd(ca.nchar);
916#endif
917 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100918 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 cp = &ca.extra_char; /* need to get a third character */
921 if (ca.nchar != 'r')
922 lit = TRUE; /* get it literally */
923 else
924 repl = TRUE; /* get it in replace mode */
925 }
926 else
927 cp = NULL; /* no third character needed */
928 }
929 else
930 {
931 if (ca.cmdchar == 'r') /* get it in replace mode */
932 repl = TRUE;
933 cp = &ca.nchar;
934 }
935 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
936
937 /*
938 * Get a second or third character.
939 */
940 if (cp != NULL)
941 {
942#ifdef CURSOR_SHAPE
943 if (repl)
944 {
945 State = REPLACE; /* pretend Replace mode */
946 ui_cursor_shape(); /* show different cursor shape */
947 }
948#endif
949 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
950 {
951 /* Allow mappings defined with ":lmap". */
952 --no_mapping;
953 --allow_keys;
954 if (repl)
955 State = LREPLACE;
956 else
957 State = LANGMAP;
958 langmap_active = TRUE;
959 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100960#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 save_smd = p_smd;
962 p_smd = FALSE; /* Don't let the IM code show the mode here */
963 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
964 im_set_active(TRUE);
965#endif
966
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000967 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 if (langmap_active)
970 {
971 /* Undo the decrement done above */
972 ++no_mapping;
973 ++allow_keys;
974 State = NORMAL_BUSY;
975 }
Bram Moolenaar819edbe2017-11-25 17:14:33 +0100976#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (lang)
978 {
979 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
980 im_save_status(&curbuf->b_p_iminsert);
981 im_set_active(FALSE);
982 }
983 p_smd = save_smd;
984#endif
985#ifdef CURSOR_SHAPE
986 State = NORMAL_BUSY;
987#endif
988#ifdef FEAT_CMDL_INFO
989 need_flushbuf |= add_to_showcmd(*cp);
990#endif
991
992 if (!lit)
993 {
994#ifdef FEAT_DIGRAPHS
995 /* Typing CTRL-K gets a digraph. */
996 if (*cp == Ctrl_K
997 && ((nv_cmds[idx].cmd_flags & NV_LANG)
998 || cp == &ca.extra_char)
999 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
1000 {
1001 c = get_digraph(FALSE);
1002 if (c > 0)
1003 {
1004 *cp = c;
1005# ifdef FEAT_CMDL_INFO
1006 /* Guessing how to update showcmd here... */
1007 del_from_showcmd(3);
1008 need_flushbuf |= add_to_showcmd(*cp);
1009# endif
1010 }
1011 }
1012#endif
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 /* adjust chars > 127, except after "tTfFr" commands */
1015 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#ifdef FEAT_RIGHTLEFT
1017 /* adjust Hebrew mapped char */
1018 if (p_hkmap && lang && KeyTyped)
1019 *cp = hkmap(*cp);
1020# ifdef FEAT_FKMAP
1021 /* adjust Farsi mapped char */
1022 if (p_fkmap && lang && KeyTyped)
1023 *cp = fkmap(*cp);
1024# endif
1025#endif
1026 }
1027
1028 /*
1029 * When the next character is CTRL-\ a following CTRL-N means the
1030 * command is aborted and we go to Normal mode.
1031 */
1032 if (cp == &ca.extra_char
1033 && ca.nchar == Ctrl_BSL
1034 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1035 {
1036 ca.cmdchar = Ctrl_BSL;
1037 ca.nchar = ca.extra_char;
1038 idx = find_command(ca.cmdchar);
1039 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001040 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001041 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 else if (*cp == Ctrl_BSL)
1043 {
1044 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1045
1046 /* There is a busy wait here when typing "f<C-\>" and then
1047 * something different from CTRL-N. Can't be avoided. */
1048 while ((c = vpeekc()) <= 0 && towait > 0L)
1049 {
1050 do_sleep(towait > 50L ? 50L : towait);
1051 towait -= 50L;
1052 }
1053 if (c > 0)
1054 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001055 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (c != Ctrl_N && c != Ctrl_G)
1057 vungetc(c);
1058 else
1059 {
1060 ca.cmdchar = Ctrl_BSL;
1061 ca.nchar = c;
1062 idx = find_command(ca.cmdchar);
1063 }
1064 }
1065 }
1066
1067#ifdef FEAT_MBYTE
1068 /* When getting a text character and the next character is a
1069 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001070 * However, don't wait for it to arrive. Also, do enable mapping,
1071 * because if it's put back with vungetc() it's too late to apply
1072 * mapping. */
1073 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 while (enc_utf8 && lang && (c = vpeekc()) > 0
1075 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1076 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001077 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (!utf_iscomposing(c))
1079 {
1080 vungetc(c); /* it wasn't, put it back */
1081 break;
1082 }
1083 else if (ca.ncharC1 == 0)
1084 ca.ncharC1 = c;
1085 else
1086 ca.ncharC2 = c;
1087 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001088 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#endif
1090 }
1091 --no_mapping;
1092 --allow_keys;
1093 }
1094
1095#ifdef FEAT_CMDL_INFO
1096 /*
1097 * Flush the showcmd characters onto the screen so we can see them while
1098 * the command is being executed. Only do this when the shown command was
1099 * actually displayed, otherwise this will slow down a lot when executing
1100 * mappings.
1101 */
1102 if (need_flushbuf)
1103 out_flush();
1104#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00001105#ifdef FEAT_AUTOCMD
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001106 if (ca.cmdchar != K_IGNORE)
1107 did_cursorhold = FALSE;
Bram Moolenaar3918c952005-03-15 22:34:55 +00001108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
1110 State = NORMAL;
1111
1112 if (ca.nchar == ESC)
1113 {
1114 clearop(oap);
1115 if (restart_edit == 0 && goto_im())
1116 restart_edit = 'a';
1117 goto normal_end;
1118 }
1119
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001120 if (ca.cmdchar != K_IGNORE)
1121 {
1122 msg_didout = FALSE; /* don't scroll screen up for normal command */
1123 msg_col = 0;
1124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 old_pos = curwin->w_cursor; /* remember where cursor was */
1127
1128 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1129 * mode. */
1130 if (!VIsual_active && km_startsel)
1131 {
1132 if (nv_cmds[idx].cmd_flags & NV_SS)
1133 {
1134 start_selection();
1135 unshift_special(&ca);
1136 idx = find_command(ca.cmdchar);
1137 }
1138 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1139 && (mod_mask & MOD_MASK_SHIFT))
1140 {
1141 start_selection();
1142 mod_mask &= ~MOD_MASK_SHIFT;
1143 }
1144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
1146 /*
1147 * Execute the command!
1148 * Call the command function found in the commands table.
1149 */
1150 ca.arg = nv_cmds[idx].cmd_arg;
1151 (nv_cmds[idx].cmd_func)(&ca);
1152
1153 /*
1154 * If we didn't start or finish an operator, reset oap->regname, unless we
1155 * need it later.
1156 */
1157 if (!finish_op
1158 && !oap->op_type
1159 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1160 {
1161 clearop(oap);
1162#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001163 {
1164 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001165
Bram Moolenaar536681b2011-05-10 16:12:45 +02001166 /* Adjust the register according to 'clipboard', so that when
1167 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001168# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001169 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001170# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001171 set_reg_var(regname);
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173#endif
1174 }
1175
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001176 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001177 * character or "z333<cr>". */
1178 if (old_mapped_len > 0)
1179 old_mapped_len = typebuf_maplen();
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 /*
1182 * If an operation is pending, handle it...
1183 */
1184 do_pending_operator(&ca, old_col, FALSE);
1185
1186 /*
1187 * Wait for a moment when a message is displayed that will be overwritten
1188 * by the mode message.
1189 * In Visual mode and with "^O" in Insert mode, a short message will be
1190 * overwritten by the mode message. Wait a bit, until a key is hit.
1191 * In Visual mode, it's more important to keep the Visual area updated
1192 * than keeping a message (e.g. from a /pat search).
1193 * Only do this if the command was typed, not from a mapping.
1194 * Don't wait when emsg_silent is non-zero.
1195 * Also wait a bit after an error message, e.g. for "^O:".
1196 * Don't redraw the screen, it would remove the message.
1197 */
1198 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001199 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 || (VIsual_active
1202 && old_pos.lnum == curwin->w_cursor.lnum
1203 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 )
1205 && (clear_cmdline
1206 || redraw_cmdline)
1207 && (msg_didout || (msg_didany && msg_scroll))
1208 && !msg_nowait
1209 && KeyTyped)
1210 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 && (msg_scroll
1213 || emsg_on_display)))
1214 && oap->regname == 0
1215 && !(ca.retval & CA_COMMAND_BUSY)
1216 && stuff_empty()
1217 && typebuf_typed()
1218 && emsg_silent == 0
1219 && !did_wait_return
1220 && oap->op_type == OP_NOP)
1221 {
1222 int save_State = State;
1223
1224 /* Draw the cursor with the right shape here */
1225 if (restart_edit != 0)
1226 State = INSERT;
1227
1228 /* If need to redraw, and there is a "keep_msg", redraw before the
1229 * delay */
1230 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1231 {
1232 char_u *kmsg;
1233
1234 kmsg = keep_msg;
1235 keep_msg = NULL;
1236 /* showmode() will clear keep_msg, but we want to use it anyway */
1237 update_screen(0);
1238 /* now reset it, otherwise it's put in the history again */
1239 keep_msg = kmsg;
1240 msg_attr(kmsg, keep_msg_attr);
1241 vim_free(kmsg);
1242 }
1243 setcursor();
1244 cursor_on();
1245 out_flush();
1246 if (msg_scroll || emsg_on_display)
1247 ui_delay(1000L, TRUE); /* wait at least one second */
1248 ui_delay(3000L, FALSE); /* wait up to three seconds */
1249 State = save_State;
1250
1251 msg_scroll = FALSE;
1252 emsg_on_display = FALSE;
1253 }
1254
1255 /*
1256 * Finish up after executing a Normal mode command.
1257 */
1258normal_end:
1259
1260 msg_nowait = FALSE;
1261
1262 /* Reset finish_op, in case it was set */
1263#ifdef CURSOR_SHAPE
1264 c = finish_op;
1265#endif
1266 finish_op = FALSE;
1267#ifdef CURSOR_SHAPE
1268 /* Redraw the cursor with another shape, if we were in Operator-pending
1269 * mode or did a replace command. */
1270 if (c || ca.cmdchar == 'r')
1271 {
1272 ui_cursor_shape(); /* may show different cursor shape */
1273# ifdef FEAT_MOUSESHAPE
1274 update_mouseshape(-1);
1275# endif
1276 }
1277#endif
1278
1279#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001280 if (oap->op_type == OP_NOP && oap->regname == 0
1281# ifdef FEAT_AUTOCMD
1282 && ca.cmdchar != K_CURSORHOLD
1283# endif
1284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 clear_showcmd();
1286#endif
1287
1288 checkpcmark(); /* check if we moved since setting pcmark */
1289 vim_free(ca.searchbuf);
1290
1291#ifdef FEAT_MBYTE
1292 if (has_mbyte)
1293 mb_adjust_cursor();
1294#endif
1295
1296#ifdef FEAT_SCROLLBIND
1297 if (curwin->w_p_scb && toplevel)
1298 {
1299 validate_cursor(); /* may need to update w_leftcol */
1300 do_check_scrollbind(TRUE);
1301 }
1302#endif
1303
Bram Moolenaar860cae12010-06-05 23:22:07 +02001304#ifdef FEAT_CURSORBIND
1305 if (curwin->w_p_crb && toplevel)
1306 {
1307 validate_cursor(); /* may need to update w_leftcol */
1308 do_check_cursorbind();
1309 }
1310#endif
1311
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001312#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001313 /* don't go to Insert mode if a terminal has a running job */
1314 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001315 restart_edit = 0;
1316#endif
1317
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 /*
1319 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1320 * if still inside a mapping that started in Visual mode).
1321 * May switch from Visual to Select mode after CTRL-O command.
1322 */
1323 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1325 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 && !(ca.retval & CA_COMMAND_BUSY)
1327 && stuff_empty()
1328 && oap->regname == 0)
1329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (restart_VIsual_select == 1)
1331 {
1332 VIsual_select = TRUE;
1333 showmode();
1334 restart_VIsual_select = 0;
1335 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001336 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 (void)edit(restart_edit, FALSE, 1L);
1338 }
1339
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (restart_VIsual_select == 2)
1341 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 /* Save count before an operator for next time. */
1344 opcount = ca.opcount;
1345}
1346
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001347#ifdef FEAT_EVAL
1348/*
1349 * Set v:count and v:count1 according to "cap".
1350 * Set v:prevcount only when "set_prevcount" is TRUE.
1351 */
1352 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001353set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001354{
1355 long count = cap->count0;
1356
1357 /* multiply with cap->opcount the same way as above */
1358 if (cap->opcount != 0)
1359 count = cap->opcount * (count == 0 ? 1 : count);
1360 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1361 *set_prevcount = FALSE; /* only set v:prevcount once */
1362}
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365/*
1366 * Handle an operator after visual mode or when the movement is finished
1367 */
1368 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001369do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
1371 oparg_T *oap = cap->oap;
1372 pos_T old_cursor;
1373 int empty_region_error;
1374 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001375#ifdef FEAT_LINEBREAK
1376 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /* The visual area is remembered for redo */
1380 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1381 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001382 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001384 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001385#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388
1389#if defined(FEAT_CLIPBOARD)
1390 /*
1391 * Yank the visual area into the GUI selection register before we operate
1392 * on it and lose it forever.
1393 * Don't do it if a specific register was specified, so that ""x"*P works.
1394 * This could call do_pending_operator() recursively, but that's OK
1395 * because gui_yank will be TRUE for the nested call.
1396 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001397 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 && oap->op_type != OP_NOP
1399 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 && VIsual_active
1401 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 && oap->regname == 0)
1403 clip_auto_select();
1404#endif
1405 old_cursor = curwin->w_cursor;
1406
1407 /*
1408 * If an operation is pending, handle it...
1409 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001410 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001412#ifdef FEAT_LINEBREAK
1413 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001414 if (curwin->w_p_lbr)
1415 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001416 curwin->w_p_lbr = FALSE;
1417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 oap->is_VIsual = VIsual_active;
1419 if (oap->motion_force == 'V')
1420 oap->motion_type = MLINE;
1421 else if (oap->motion_force == 'v')
1422 {
1423 /* If the motion was linewise, "inclusive" will not have been set.
1424 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1425 if (oap->motion_type == MLINE)
1426 oap->inclusive = FALSE;
1427 /* If the motion already was characterwise, toggle "inclusive" */
1428 else if (oap->motion_type == MCHAR)
1429 oap->inclusive = !oap->inclusive;
1430 oap->motion_type = MCHAR;
1431 }
1432 else if (oap->motion_force == Ctrl_V)
1433 {
1434 /* Change line- or characterwise motion into Visual block mode. */
1435 VIsual_active = TRUE;
1436 VIsual = oap->start;
1437 VIsual_mode = Ctrl_V;
1438 VIsual_select = FALSE;
1439 VIsual_reselect = FALSE;
1440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar7afea822013-04-24 18:34:45 +02001442 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1443 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001445 && ((!VIsual_active || oap->motion_force)
1446 /* Also redo Operator-pending Visual mode mappings */
1447 || (VIsual_active && cap->cmdchar == ':'
1448 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001449 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450#ifdef FEAT_FOLDING
1451 && oap->op_type != OP_FOLD
1452 && oap->op_type != OP_FOLDOPEN
1453 && oap->op_type != OP_FOLDOPENREC
1454 && oap->op_type != OP_FOLDCLOSE
1455 && oap->op_type != OP_FOLDCLOSEREC
1456 && oap->op_type != OP_FOLDDEL
1457 && oap->op_type != OP_FOLDDELREC
1458#endif
1459 )
1460 {
1461 prep_redo(oap->regname, cap->count0,
1462 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1463 oap->motion_force, cap->cmdchar, cap->nchar);
1464 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1465 {
1466 /*
1467 * If 'cpoptions' does not contain 'r', insert the search
1468 * pattern to really repeat the same command.
1469 */
1470 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001471 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 AppendToRedobuff(NL_STR);
1473 }
1474 else if (cap->cmdchar == ':')
1475 {
1476 /* do_cmdline() has stored the first typed line in
1477 * "repeat_cmdline". When several lines are typed repeating
1478 * won't be possible. */
1479 if (repeat_cmdline == NULL)
1480 ResetRedobuff();
1481 else
1482 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001483 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001485 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 }
1487 }
1488 }
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (redo_VIsual_busy)
1491 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001492 /* Redo of an operation on a Visual area. Use the same size from
1493 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 oap->start = curwin->w_cursor;
1495 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1496 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1497 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1498 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001499 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001501 if (VIsual_mode == 'v')
1502 {
1503 if (redo_VIsual_line_count <= 1)
1504 {
1505 validate_virtcol();
1506 curwin->w_curswant =
1507 curwin->w_virtcol + redo_VIsual_vcol - 1;
1508 }
1509 else
1510 curwin->w_curswant = redo_VIsual_vcol;
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001513 {
1514 curwin->w_curswant = MAXCOL;
1515 }
1516 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518 cap->count0 = redo_VIsual_count;
1519 if (redo_VIsual_count != 0)
1520 cap->count1 = redo_VIsual_count;
1521 else
1522 cap->count1 = 1;
1523 }
1524 else if (VIsual_active)
1525 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001526 if (!gui_yank)
1527 {
1528 /* Save the current VIsual area for '< and '> marks, and "gv" */
1529 curbuf->b_visual.vi_start = VIsual;
1530 curbuf->b_visual.vi_end = curwin->w_cursor;
1531 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001532 if (VIsual_mode_orig != NUL)
1533 {
1534 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1535 VIsual_mode_orig = NUL;
1536 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001537 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001539 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
1543 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001544 * characterwise selection.
1545 * Special case: gH<Del> deletes the last line. */
1546 if (VIsual_select && VIsual_mode == 'V'
1547 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001549 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 VIsual.col = 0;
1552 curwin->w_cursor.col =
1553 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1554 }
1555 else
1556 {
1557 curwin->w_cursor.col = 0;
1558 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1559 }
1560 VIsual_mode = 'v';
1561 }
1562 /* If 'selection' is "exclusive", backup one character for
1563 * charwise selections. */
1564 else if (VIsual_mode == 'v')
1565 {
1566# ifdef FEAT_VIRTUALEDIT
1567 include_line_break =
1568# endif
1569 unadjust_for_sel();
1570 }
1571
1572 oap->start = VIsual;
1573 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001576# ifdef FEAT_VIRTUALEDIT
1577 oap->start.coladd = 0;
1578# endif
1579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 /*
1583 * Set oap->start to the first position of the operated text, oap->end
1584 * to the end of the operated text. w_cursor is equal to oap->start.
1585 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001586 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588#ifdef FEAT_FOLDING
1589 /* Include folded lines completely. */
1590 if (!VIsual_active)
1591 {
1592 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1593 oap->start.col = 0;
1594 if (hasFolding(curwin->w_cursor.lnum, NULL,
1595 &curwin->w_cursor.lnum))
1596 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1597 }
1598#endif
1599 oap->end = curwin->w_cursor;
1600 curwin->w_cursor = oap->start;
1601
1602 /* w_virtcol may have been updated; if the cursor goes back to its
1603 * previous position w_virtcol becomes invalid and isn't updated
1604 * automatically. */
1605 curwin->w_valid &= ~VALID_VIRTCOL;
1606 }
1607 else
1608 {
1609#ifdef FEAT_FOLDING
1610 /* Include folded lines completely. */
1611 if (!VIsual_active && oap->motion_type == MLINE)
1612 {
1613 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1614 NULL))
1615 curwin->w_cursor.col = 0;
1616 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1617 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1618 }
1619#endif
1620 oap->end = oap->start;
1621 oap->start = curwin->w_cursor;
1622 }
1623
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001624 /* Just in case lines were deleted that make the position invalid. */
1625 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1627
1628#ifdef FEAT_VIRTUALEDIT
1629 /* Set "virtual_op" before resetting VIsual_active. */
1630 virtual_op = virtual_active();
1631#endif
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (VIsual_active || redo_VIsual_busy)
1634 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001635 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 if (!redo_VIsual_busy && !gui_yank)
1638 {
1639 /*
1640 * Prepare to reselect and redo Visual: this is based on the
1641 * size of the Visual text
1642 */
1643 resel_VIsual_mode = VIsual_mode;
1644 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001645 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001647 {
1648 if (VIsual_mode != Ctrl_V)
1649 getvvcol(curwin, &(oap->end),
1650 NULL, NULL, &oap->end_vcol);
1651 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1652 {
1653 if (VIsual_mode != Ctrl_V)
1654 getvvcol(curwin, &(oap->start),
1655 &oap->start_vcol, NULL, NULL);
1656 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1657 }
1658 else
1659 resel_VIsual_vcol = oap->end_vcol;
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 resel_VIsual_line_count = oap->line_count;
1662 }
1663
1664 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1665 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1666 && oap->op_type != OP_COLON
1667#ifdef FEAT_FOLDING
1668 && oap->op_type != OP_FOLD
1669 && oap->op_type != OP_FOLDOPEN
1670 && oap->op_type != OP_FOLDOPENREC
1671 && oap->op_type != OP_FOLDCLOSE
1672 && oap->op_type != OP_FOLDCLOSEREC
1673 && oap->op_type != OP_FOLDDEL
1674 && oap->op_type != OP_FOLDDELREC
1675#endif
1676 && oap->motion_force == NUL
1677 )
1678 {
1679 /* Prepare for redoing. Only use the nchar field for "r",
1680 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001681 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1682 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001683 prep_redo(oap->regname, cap->count0,
1684 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1685 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001686 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001687 {
1688 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1689
1690 /* reverse what nv_replace() did */
1691 if (nchar == REPLACE_CR_NCHAR)
1692 nchar = CAR;
1693 else if (nchar == REPLACE_NL_NCHAR)
1694 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001695 prep_redo(oap->regname, 0L, NUL, 'v',
1696 get_op_char(oap->op_type),
1697 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001698 nchar);
1699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if (!redo_VIsual_busy)
1701 {
1702 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001703 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 redo_VIsual_line_count = resel_VIsual_line_count;
1705 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001706 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
1708 }
1709
1710 /*
1711 * oap->inclusive defaults to TRUE.
1712 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1713 * FALSE. This makes "d}P" and "v}dP" work the same.
1714 */
1715 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1716 oap->inclusive = TRUE;
1717 if (VIsual_mode == 'V')
1718 oap->motion_type = MLINE;
1719 else
1720 {
1721 oap->motion_type = MCHAR;
1722 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001723#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 )
1727 {
1728 oap->inclusive = FALSE;
1729 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001730 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001731 if (*p_sel != 'o'
1732 && !op_on_lines(oap->op_type)
1733 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001735 ++oap->end.lnum;
1736 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001737#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001738 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001739#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001740 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
1742 }
1743 }
1744
1745 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 /*
1748 * Switch Visual off now, so screen updating does
1749 * not show inverted text when the screen is redrawn.
1750 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1751 * no screen redraw, so it is done here to remove the inverted
1752 * part.
1753 */
1754 if (!gui_yank)
1755 {
1756 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001757#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 setmouse();
1759 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001760#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001761 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if ((oap->op_type == OP_YANK
1763 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001764 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 || oap->op_type == OP_FILTER)
1766 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001767 {
1768#ifdef FEAT_LINEBREAK
1769 /* make sure redrawing is correct */
1770 curwin->w_p_lbr = lbr_saved;
1771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777#ifdef FEAT_MBYTE
1778 /* Include the trailing byte of a multi-byte char. */
1779 if (has_mbyte && oap->inclusive)
1780 {
1781 int l;
1782
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001783 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (l > 1)
1785 oap->end.col += l - 1;
1786 }
1787#endif
1788 curwin->w_set_curswant = TRUE;
1789
1790 /*
1791 * oap->empty is set when start and end are the same. The inclusive
1792 * flag affects this too, unless yanking and the end is on a NUL.
1793 */
1794 oap->empty = (oap->motion_type == MCHAR
1795 && (!oap->inclusive
1796 || (oap->op_type == OP_YANK
1797 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001798 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799#ifdef FEAT_VIRTUALEDIT
1800 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1801#endif
1802 );
1803 /*
1804 * For delete, change and yank, it's an error to operate on an
1805 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1806 */
1807 empty_region_error = (oap->empty
1808 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 /* Force a redraw when operating on an empty Visual region, when
1811 * 'modifiable is off or creating a fold. */
1812 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001813#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001817 {
1818#ifdef FEAT_LINEBREAK
1819 curwin->w_p_lbr = lbr_saved;
1820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824 /*
1825 * If the end of an operator is in column one while oap->motion_type
1826 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1827 * character in the previous line. If op_start is on or before the
1828 * first non-blank in the line, the operator becomes linewise
1829 * (strange, but that's the way vi does it).
1830 */
1831 if ( oap->motion_type == MCHAR
1832 && oap->inclusive == FALSE
1833 && !(cap->retval & CA_NO_ADJ_OP_END)
1834 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001836 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 && oap->line_count > 1)
1838 {
1839 oap->end_adjusted = TRUE; /* remember that we did this */
1840 --oap->line_count;
1841 --oap->end.lnum;
1842 if (inindent(0))
1843 oap->motion_type = MLINE;
1844 else
1845 {
1846 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1847 if (oap->end.col)
1848 {
1849 --oap->end.col;
1850 oap->inclusive = TRUE;
1851 }
1852 }
1853 }
1854 else
1855 oap->end_adjusted = FALSE;
1856
1857 switch (oap->op_type)
1858 {
1859 case OP_LSHIFT:
1860 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001861 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 auto_format(FALSE, TRUE);
1863 break;
1864
1865 case OP_JOIN_NS:
1866 case OP_JOIN:
1867 if (oap->line_count < 2)
1868 oap->line_count = 2;
1869 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1870 curbuf->b_ml.ml_line_count)
1871 beep_flush();
1872 else
1873 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001874 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001875 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 auto_format(FALSE, TRUE);
1877 }
1878 break;
1879
1880 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001883 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001884 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001885 CancelRedo();
1886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 else
1888 {
1889 (void)op_delete(oap);
1890 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1891 u_save_cursor(); /* cursor line wasn't saved yet */
1892 auto_format(FALSE, TRUE);
1893 }
1894 break;
1895
1896 case OP_YANK:
1897 if (empty_region_error)
1898 {
1899 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001900 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001901 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001902 CancelRedo();
1903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001906 {
1907#ifdef FEAT_LINEBREAK
1908 curwin->w_p_lbr = lbr_saved;
1909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 check_cursor_col();
1913 break;
1914
1915 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001918 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001919 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001920 CancelRedo();
1921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 else
1923 {
1924 /* This is a new edit command, not a restart. Need to
1925 * remember it to make 'insertmode' work with mappings for
1926 * Visual mode. But do this only once and not when typed and
1927 * 'insertmode' isn't set. */
1928 if (p_im || !KeyTyped)
1929 restart_edit_save = restart_edit;
1930 else
1931 restart_edit_save = 0;
1932 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001933#ifdef FEAT_LINEBREAK
1934 /* Restore linebreak, so that when the user edits it looks as
1935 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001936 if (curwin->w_p_lbr != lbr_saved)
1937 {
1938 curwin->w_p_lbr = lbr_saved;
1939 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1940 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 /* Reset finish_op now, don't want it set inside edit(). */
1943 finish_op = FALSE;
1944 if (op_change(oap)) /* will call edit() */
1945 cap->retval |= CA_COMMAND_BUSY;
1946 if (restart_edit == 0)
1947 restart_edit = restart_edit_save;
1948 }
1949 break;
1950
1951 case OP_FILTER:
1952 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1953 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1954 else
1955 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001956 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 case OP_INDENT:
1959 case OP_COLON:
1960
1961#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1962 /*
1963 * If 'equalprg' is empty, do the indenting internally.
1964 */
1965 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1966 {
1967# ifdef FEAT_LISP
1968 if (curbuf->b_p_lisp)
1969 {
1970 op_reindent(oap, get_lisp_indent);
1971 break;
1972 }
1973# endif
1974# ifdef FEAT_CINDENT
1975 op_reindent(oap,
1976# ifdef FEAT_EVAL
1977 *curbuf->b_p_inde != NUL ? get_expr_indent :
1978# endif
1979 get_c_indent);
1980 break;
1981# endif
1982 }
1983#endif
1984
1985 op_colon(oap);
1986 break;
1987
1988 case OP_TILDE:
1989 case OP_UPPER:
1990 case OP_LOWER:
1991 case OP_ROT13:
1992 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001993 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001994 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001995 CancelRedo();
1996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 else
1998 op_tilde(oap);
1999 check_cursor_col();
2000 break;
2001
2002 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002003#if defined(FEAT_EVAL)
2004 if (*curbuf->b_p_fex != NUL)
2005 op_formatexpr(oap); /* use expression */
2006 else
2007#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002008 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 op_colon(oap); /* use external command */
2010 else
2011 op_format(oap, FALSE); /* use internal function */
2012 break;
2013
2014 case OP_FORMAT2:
2015 op_format(oap, TRUE); /* use internal function */
2016 break;
2017
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002018 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01002019#ifdef FEAT_LINEBREAK
2020 /* Restore linebreak, so that when the user edits it looks as
2021 * before. */
2022 curwin->w_p_lbr = lbr_saved;
2023#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002024 op_function(oap); /* call 'operatorfunc' */
2025 break;
2026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 case OP_INSERT:
2028 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030#ifdef FEAT_VISUALEXTRA
2031 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002032 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002033 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002034 CancelRedo();
2035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 else
2037 {
2038 /* This is a new edit command, not a restart. Need to
2039 * remember it to make 'insertmode' work with mappings for
2040 * Visual mode. But do this only once. */
2041 restart_edit_save = restart_edit;
2042 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002043#ifdef FEAT_LINEBREAK
2044 /* Restore linebreak, so that when the user edits it looks as
2045 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002046 if (curwin->w_p_lbr != lbr_saved)
2047 {
2048 curwin->w_p_lbr = lbr_saved;
2049 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2050 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002053#ifdef FEAT_LINEBREAK
2054 /* Reset linebreak, so that formatting works correctly. */
2055 curwin->w_p_lbr = FALSE;
2056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
2058 /* TODO: when inserting in several lines, should format all
2059 * the lines. */
2060 auto_format(FALSE, TRUE);
2061
2062 if (restart_edit == 0)
2063 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002064 else
2065 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002068 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069#endif
2070 break;
2071
2072 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074#ifdef FEAT_VISUALEXTRA
2075 if (empty_region_error)
2076#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002077 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002078 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002079 CancelRedo();
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081#ifdef FEAT_VISUALEXTRA
2082 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002083 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002084# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002085 /* Restore linebreak, so that when the user edits it looks as
2086 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002087 if (curwin->w_p_lbr != lbr_saved)
2088 {
2089 curwin->w_p_lbr = lbr_saved;
2090 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2091 }
2092# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#endif
2096 break;
2097
2098#ifdef FEAT_FOLDING
2099 case OP_FOLD:
2100 VIsual_reselect = FALSE; /* don't reselect now */
2101 foldCreate(oap->start.lnum, oap->end.lnum);
2102 break;
2103
2104 case OP_FOLDOPEN:
2105 case OP_FOLDOPENREC:
2106 case OP_FOLDCLOSE:
2107 case OP_FOLDCLOSEREC:
2108 VIsual_reselect = FALSE; /* don't reselect now */
2109 opFoldRange(oap->start.lnum, oap->end.lnum,
2110 oap->op_type == OP_FOLDOPEN
2111 || oap->op_type == OP_FOLDOPENREC,
2112 oap->op_type == OP_FOLDOPENREC
2113 || oap->op_type == OP_FOLDCLOSEREC,
2114 oap->is_VIsual);
2115 break;
2116
2117 case OP_FOLDDEL:
2118 case OP_FOLDDELREC:
2119 VIsual_reselect = FALSE; /* don't reselect now */
2120 deleteFold(oap->start.lnum, oap->end.lnum,
2121 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2122 break;
2123#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002124 case OP_NR_ADD:
2125 case OP_NR_SUB:
2126 if (empty_region_error)
2127 {
2128 vim_beep(BO_OPER);
2129 CancelRedo();
2130 }
2131 else
2132 {
2133 VIsual_active = TRUE;
2134#ifdef FEAT_LINEBREAK
2135 curwin->w_p_lbr = lbr_saved;
2136#endif
2137 op_addsub(oap, cap->count1, redo_VIsual_arg);
2138 VIsual_active = FALSE;
2139 }
2140 check_cursor_col();
2141 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 default:
2143 clearopbeep(oap);
2144 }
2145#ifdef FEAT_VIRTUALEDIT
2146 virtual_op = MAYBE;
2147#endif
2148 if (!gui_yank)
2149 {
2150 /*
2151 * if 'sol' not set, go back to old column for some commands
2152 */
2153 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2154 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2155 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002156 {
2157#ifdef FEAT_LINEBREAK
2158 curwin->w_p_lbr = FALSE;
2159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
2163 else
2164 {
2165 curwin->w_cursor = old_cursor;
2166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 clearop(oap);
2169 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002170#ifdef FEAT_LINEBREAK
2171 curwin->w_p_lbr = lbr_saved;
2172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173}
2174
2175/*
2176 * Handle indent and format operators and visual mode ":".
2177 */
2178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002179op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
2181 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (oap->is_VIsual)
2183 stuffReadbuff((char_u *)"'<,'>");
2184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
2186 /*
2187 * Make the range look nice, so it can be repeated.
2188 */
2189 if (oap->start.lnum == curwin->w_cursor.lnum)
2190 stuffcharReadbuff('.');
2191 else
2192 stuffnumReadbuff((long)oap->start.lnum);
2193 if (oap->end.lnum != oap->start.lnum)
2194 {
2195 stuffcharReadbuff(',');
2196 if (oap->end.lnum == curwin->w_cursor.lnum)
2197 stuffcharReadbuff('.');
2198 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2199 stuffcharReadbuff('$');
2200 else if (oap->start.lnum == curwin->w_cursor.lnum)
2201 {
2202 stuffReadbuff((char_u *)".+");
2203 stuffnumReadbuff((long)oap->line_count - 1);
2204 }
2205 else
2206 stuffnumReadbuff((long)oap->end.lnum);
2207 }
2208 }
2209 if (oap->op_type != OP_COLON)
2210 stuffReadbuff((char_u *)"!");
2211 if (oap->op_type == OP_INDENT)
2212 {
2213#ifndef FEAT_CINDENT
2214 if (*get_equalprg() == NUL)
2215 stuffReadbuff((char_u *)"indent");
2216 else
2217#endif
2218 stuffReadbuff(get_equalprg());
2219 stuffReadbuff((char_u *)"\n");
2220 }
2221 else if (oap->op_type == OP_FORMAT)
2222 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002223 if (*curbuf->b_p_fp != NUL)
2224 stuffReadbuff(curbuf->b_p_fp);
2225 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002227 else
2228 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002229 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231
2232 /*
2233 * do_cmdline() does the rest
2234 */
2235}
2236
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002237/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002238 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002239 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002240 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002241op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002242{
2243#ifdef FEAT_EVAL
2244 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002245# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002246 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002247# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002248
2249 if (*p_opfunc == NUL)
2250 EMSG(_("E774: 'operatorfunc' is empty"));
2251 else
2252 {
2253 /* Set '[ and '] marks to text to be operated on. */
2254 curbuf->b_op_start = oap->start;
2255 curbuf->b_op_end = oap->end;
2256 if (oap->motion_type != MLINE && !oap->inclusive)
2257 /* Exclude the end position. */
2258 decl(&curbuf->b_op_end);
2259
2260 if (oap->block_mode)
2261 argv[0] = (char_u *)"block";
2262 else if (oap->motion_type == MLINE)
2263 argv[0] = (char_u *)"line";
2264 else
2265 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002266
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002267# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002268 /* Reset virtual_op so that 'virtualedit' can be changed in the
2269 * function. */
2270 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002271# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002272
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002273 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002274
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002275# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002276 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002277# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002278 }
2279#else
2280 EMSG(_("E775: Eval feature not available"));
2281#endif
2282}
2283
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284#if defined(FEAT_MOUSE) || defined(PROTO)
2285/*
2286 * Do the appropriate action for the current mouse click in the current mode.
2287 * Not used for Command-line mode.
2288 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002289 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 * event modi- position visual change action
2291 * fier cursor window
2292 * left press - yes end yes
2293 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002294 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 * left drag - yes start if moved no
2296 * left relse - yes start if moved no
2297 * middle press - yes if not active no put register
2298 * middle press - yes if active no yank and put
2299 * right press - yes start or extend yes
2300 * right press S yes no change yes "#" (2)
2301 * right drag - yes extend no
2302 * right relse - yes extend no
2303 *
2304 * Insert or Replace Mode:
2305 * event modi- position visual change action
2306 * fier cursor window
2307 * left press - yes (cannot be active) yes
2308 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2309 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2310 * left drag - yes start or extend (1) no CTRL-O (1)
2311 * left relse - yes start or extend (1) no CTRL-O (1)
2312 * middle press - no (cannot be active) no put register
2313 * right press - yes start or extend yes CTRL-O
2314 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2315 *
2316 * (1) only if mouse pointer moved since press
2317 * (2) only if click is in same buffer
2318 *
2319 * Return TRUE if start_arrow() should be called for edit mode.
2320 */
2321 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002322do_mouse(
2323 oparg_T *oap, /* operator argument, can be NULL */
2324 int c, /* K_LEFTMOUSE, etc */
2325 int dir, /* Direction to 'put' if necessary */
2326 long count,
2327 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2330 static int got_click = FALSE; /* got a click some time back */
2331
2332 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2333 int is_click; /* If FALSE it's a drag or release event */
2334 int is_drag; /* If TRUE it's a drag event */
2335 int jump_flags = 0; /* flags for jump_to_mouse() */
2336 pos_T start_visual;
2337 int moved; /* Has cursor moved? */
2338 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002339 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 int c1, c2;
2342#if defined(FEAT_FOLDING)
2343 pos_T save_cursor;
2344#endif
2345 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 static pos_T orig_cursor;
2347 colnr_T leftcol, rightcol;
2348 pos_T end_visual;
2349 int diff;
2350 int old_active = VIsual_active;
2351 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 int regname;
2353
2354#if defined(FEAT_FOLDING)
2355 save_cursor = curwin->w_cursor;
2356#endif
2357
2358 /*
2359 * When GUI is active, always recognize mouse events, otherwise:
2360 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2361 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2362 * - For command line and insert mode 'mouse' is checked before calling
2363 * do_mouse().
2364 */
2365 if (do_always)
2366 do_always = FALSE;
2367 else
2368#ifdef FEAT_GUI
2369 if (!gui.in_use)
2370#endif
2371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 if (VIsual_active)
2373 {
2374 if (!mouse_has(MOUSE_VISUAL))
2375 return FALSE;
2376 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002377 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 return FALSE;
2379 }
2380
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002381 for (;;)
2382 {
2383 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2384 if (is_drag)
2385 {
2386 /* If the next character is the same mouse event then use that
2387 * one. Speeds up dragging the status line. */
2388 if (vpeekc() != NUL)
2389 {
2390 int nc;
2391 int save_mouse_row = mouse_row;
2392 int save_mouse_col = mouse_col;
2393
2394 /* Need to get the character, peeking doesn't get the actual
2395 * one. */
2396 nc = safe_vgetc();
2397 if (c == nc)
2398 continue;
2399 vungetc(nc);
2400 mouse_row = save_mouse_row;
2401 mouse_col = save_mouse_col;
2402 }
2403 }
2404 break;
2405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002407 if (c == K_MOUSEMOVE)
2408 {
2409 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002410#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002411 ui_may_remove_balloon();
2412 if (p_bevalterm && !VIsual_active)
2413 {
2414 profile_setlimit(p_bdlay, &bevalexpr_due);
2415 bevalexpr_due_set = TRUE;
2416 }
2417#endif
2418 return FALSE;
2419 }
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421#ifdef FEAT_MOUSESHAPE
2422 /* May have stopped dragging the status or separator line. The pointer is
2423 * most likely still on the status or separator line. */
2424 if (!is_drag && drag_status_line)
2425 {
2426 drag_status_line = FALSE;
2427 update_mouseshape(SHAPE_IDX_STATUS);
2428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (!is_drag && drag_sep_line)
2430 {
2431 drag_sep_line = FALSE;
2432 update_mouseshape(SHAPE_IDX_VSEP);
2433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#endif
2435
2436 /*
2437 * Ignore drag and release events if we didn't get a click.
2438 */
2439 if (is_click)
2440 got_click = TRUE;
2441 else
2442 {
2443 if (!got_click) /* didn't get click, ignore */
2444 return FALSE;
2445 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002446 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002448 if (in_tab_line)
2449 {
2450 in_tab_line = FALSE;
2451 return FALSE;
2452 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
2455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 /*
2457 * CTRL right mouse button does CTRL-T
2458 */
2459 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2460 {
2461 if (State & INSERT)
2462 stuffcharReadbuff(Ctrl_O);
2463 if (count > 1)
2464 stuffnumReadbuff(count);
2465 stuffcharReadbuff(Ctrl_T);
2466 got_click = FALSE; /* ignore drag&release now */
2467 return FALSE;
2468 }
2469
2470 /*
2471 * CTRL only works with left mouse button
2472 */
2473 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2474 return FALSE;
2475
2476 /*
2477 * When a modifier is down, ignore drag and release events, as well as
2478 * multiple clicks and the middle mouse button.
2479 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2480 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002481 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2482 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 && (!is_click
2484 || (mod_mask & MOD_MASK_MULTI_CLICK)
2485 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002486 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 && mouse_model_popup()
2488 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002489 && !((mod_mask & MOD_MASK_ALT)
2490 && !mouse_model_popup()
2491 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 )
2493 return FALSE;
2494
2495 /*
2496 * If the button press was used as the movement command for an operator
2497 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2498 * drag/release events.
2499 */
2500 if (!is_click && which_button == MOUSE_MIDDLE)
2501 return FALSE;
2502
2503 if (oap != NULL)
2504 regname = oap->regname;
2505 else
2506 regname = 0;
2507
2508 /*
2509 * Middle mouse button does a 'put' of the selected text
2510 */
2511 if (which_button == MOUSE_MIDDLE)
2512 {
2513 if (State == NORMAL)
2514 {
2515 /*
2516 * If an operator was pending, we don't know what the user wanted
2517 * to do. Go back to normal mode: Clear the operator and beep().
2518 */
2519 if (oap != NULL && oap->op_type != OP_NOP)
2520 {
2521 clearopbeep(oap);
2522 return FALSE;
2523 }
2524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /*
2526 * If visual was active, yank the highlighted text and put it
2527 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002528 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 */
2530 if (VIsual_active)
2531 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002532 if (VIsual_select)
2533 {
2534 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002535 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002536 }
2537 else
2538 {
2539 stuffcharReadbuff('y');
2540 stuffcharReadbuff(K_MIDDLEMOUSE);
2541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 do_always = TRUE; /* ignore 'mouse' setting next time */
2543 return FALSE;
2544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 /*
2546 * The rest is below jump_to_mouse()
2547 */
2548 }
2549
2550 else if ((State & INSERT) == 0)
2551 return FALSE;
2552
2553 /*
2554 * Middle click in insert mode doesn't move the mouse, just insert the
2555 * contents of a register. '.' register is special, can't insert that
2556 * with do_put().
2557 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2558 * happens for the GUI).
2559 */
2560 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2561 {
2562 if (regname == '.')
2563 insert_reg(regname, TRUE);
2564 else
2565 {
2566#ifdef FEAT_CLIPBOARD
2567 if (clip_star.available && regname == 0)
2568 regname = '*';
2569#endif
2570 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2571 insert_reg(regname, TRUE);
2572 else
2573 {
2574 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2575
2576 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2577 AppendCharToRedobuff(Ctrl_R);
2578 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2579 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2580 }
2581 }
2582 return FALSE;
2583 }
2584 }
2585
2586 /* When dragging or button-up stay in the same window. */
2587 if (!is_click)
2588 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2589
2590 start_visual.lnum = 0;
2591
Bram Moolenaarf740b292006-02-16 22:11:02 +00002592 /* Check for clicking in the tab page line. */
2593 if (mouse_row == 0 && firstwin->w_winrow > 0)
2594 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002595 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002596 {
2597 if (in_tab_line)
2598 {
2599 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002600 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2601 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002602 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002603 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002604 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002605
Bram Moolenaarf740b292006-02-16 22:11:02 +00002606 /* click in a tab selects that tab page */
2607 if (is_click
2608# ifdef FEAT_CMDWIN
2609 && cmdwin_type == 0
2610# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002611 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002612 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002613 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002614 c1 = TabPageIdxs[mouse_col];
2615 if (c1 >= 0)
2616 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002617 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2618 {
2619 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002620 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002621 tabpage_new();
2622 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2623 }
2624 else
2625 {
2626 /* Go to specified tab page, or next one if not clicking
2627 * on a label. */
2628 goto_tabpage(c1);
2629
2630 /* It's like clicking on the status line of a window. */
2631 if (curwin != old_curwin)
2632 end_visual_mode();
2633 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002634 }
2635 else if (c1 < 0)
2636 {
2637 tabpage_T *tp;
2638
2639 /* Close the current or specified tab page. */
2640 if (c1 == -999)
2641 tp = curtab;
2642 else
2643 tp = find_tabpage(-c1);
2644 if (tp == curtab)
2645 {
2646 if (first_tabpage->tp_next != NULL)
2647 tabpage_close(FALSE);
2648 }
2649 else if (tp != NULL)
2650 tabpage_close_other(tp, FALSE);
2651 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002652 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002653 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002654 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002655 else if (is_drag && in_tab_line)
2656 {
2657 c1 = TabPageIdxs[mouse_col];
2658 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2659 return FALSE;
2660 }
2661
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 /*
2663 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2664 * right button up -> pop-up menu
2665 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002666 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 */
2668 if (mouse_model_popup())
2669 {
2670 if (which_button == MOUSE_RIGHT
2671 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2672 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002673#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002675 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2676 || defined(FEAT_TERM_POPUP_MENU)
2677# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (gui.in_use)
2679 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002680# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2681 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2682 if (!is_click)
2683 /* Ignore right button release events, only shows the popup
2684 * menu on the button down event. */
2685 return FALSE;
2686# endif
2687# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2688 if (is_click || is_drag)
2689 /* Ignore right button down and drag mouse events. Windows
2690 * only shows the popup menu on the button up event. */
2691 return FALSE;
2692# endif
2693 }
2694# endif
2695# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2696 else
2697# endif
2698# if defined(FEAT_TERM_POPUP_MENU)
2699 if (!is_click)
2700 /* Ignore right button release events, only shows the popup
2701 * menu on the button down event. */
2702 return FALSE;
2703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002705 jump_flags = 0;
2706 if (STRCMP(p_mousem, "popup_setpos") == 0)
2707 {
2708 /* First set the cursor position before showing the popup
2709 * menu. */
2710 if (VIsual_active)
2711 {
2712 pos_T m_pos;
2713
2714 /*
2715 * set MOUSE_MAY_STOP_VIS if we are outside the
2716 * selection or the current window (might have false
2717 * negative here)
2718 */
2719 if (mouse_row < curwin->w_winrow
2720 || mouse_row
2721 > (curwin->w_winrow + curwin->w_height))
2722 jump_flags = MOUSE_MAY_STOP_VIS;
2723 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2724 jump_flags = MOUSE_MAY_STOP_VIS;
2725 else
2726 {
2727 if ((LT_POS(curwin->w_cursor, VIsual)
2728 && (LT_POS(m_pos, curwin->w_cursor)
2729 || LT_POS(VIsual, m_pos)))
2730 || (LT_POS(VIsual, curwin->w_cursor)
2731 && (LT_POS(m_pos, VIsual)
2732 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002734 jump_flags = MOUSE_MAY_STOP_VIS;
2735 }
2736 else if (VIsual_mode == Ctrl_V)
2737 {
2738 getvcols(curwin, &curwin->w_cursor, &VIsual,
2739 &leftcol, &rightcol);
2740 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2741 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002746 else
2747 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002749 if (jump_flags)
2750 {
2751 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2752 update_curbuf(VIsual_active ? INVERTED : VALID);
2753 setcursor();
2754 out_flush(); /* Update before showing popup menu */
2755 }
2756# ifdef FEAT_MENU
2757 show_popupmenu();
2758 got_click = FALSE; /* ignore release events */
2759# endif
2760 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761#else
2762 return FALSE;
2763#endif
2764 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002765 if (which_button == MOUSE_LEFT
2766 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
2768 which_button = MOUSE_RIGHT;
2769 mod_mask &= ~MOD_MASK_SHIFT;
2770 }
2771 }
2772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 if ((State & (NORMAL | INSERT))
2774 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2775 {
2776 if (which_button == MOUSE_LEFT)
2777 {
2778 if (is_click)
2779 {
2780 /* stop Visual mode for a left click in a window, but not when
2781 * on a status line */
2782 if (VIsual_active)
2783 jump_flags |= MOUSE_MAY_STOP_VIS;
2784 }
2785 else if (mouse_has(MOUSE_VISUAL))
2786 jump_flags |= MOUSE_MAY_VIS;
2787 }
2788 else if (which_button == MOUSE_RIGHT)
2789 {
2790 if (is_click && VIsual_active)
2791 {
2792 /*
2793 * Remember the start and end of visual before moving the
2794 * cursor.
2795 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002796 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
2798 start_visual = curwin->w_cursor;
2799 end_visual = VIsual;
2800 }
2801 else
2802 {
2803 start_visual = VIsual;
2804 end_visual = curwin->w_cursor;
2805 }
2806 }
2807 jump_flags |= MOUSE_FOCUS;
2808 if (mouse_has(MOUSE_VISUAL))
2809 jump_flags |= MOUSE_MAY_VIS;
2810 }
2811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 /*
2814 * If an operator is pending, ignore all drags and releases until the
2815 * next mouse click.
2816 */
2817 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2818 {
2819 got_click = FALSE;
2820 oap->motion_type = MCHAR;
2821 }
2822
2823 /* When releasing the button let jump_to_mouse() know. */
2824 if (!is_click && !is_drag)
2825 jump_flags |= MOUSE_RELEASED;
2826
2827 /*
2828 * JUMP!
2829 */
2830 jump_flags = jump_to_mouse(jump_flags,
2831 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002832
2833#ifdef FEAT_MENU
2834 /* A click in the window toolbar has no side effects. */
2835 if (jump_flags & MOUSE_WINBAR)
2836 return FALSE;
2837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 moved = (jump_flags & CURSOR_MOVED);
2839 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
2842#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002843 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2845 {
2846 int key = KEY2TERMCAP1(c);
2847
2848 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2849 || key == (int)KE_RIGHTRELEASE)
2850 netbeans_button_release(which_button);
2851 }
2852#endif
2853
2854 /* When jumping to another window, clear a pending operator. That's a bit
2855 * friendlier than beeping and not jumping to that window. */
2856 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2857 clearop(oap);
2858
2859#ifdef FEAT_FOLDING
2860 if (mod_mask == 0
2861 && !is_drag
2862 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2863 && which_button == MOUSE_LEFT)
2864 {
2865 /* open or close a fold at this line */
2866 if (jump_flags & MOUSE_FOLD_OPEN)
2867 openFold(curwin->w_cursor.lnum, 1L);
2868 else
2869 closeFold(curwin->w_cursor.lnum, 1L);
2870 /* don't move the cursor if still in the same window */
2871 if (curwin == old_curwin)
2872 curwin->w_cursor = save_cursor;
2873 }
2874#endif
2875
2876#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2877 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2878 {
2879 clip_modeless(which_button, is_click, is_drag);
2880 return FALSE;
2881 }
2882#endif
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002885 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 if (VIsual_active && is_drag && p_so)
2887 {
2888 /* In the very first line, allow scrolling one line */
2889 if (mouse_row == 0)
2890 mouse_dragging = 2;
2891 else
2892 mouse_dragging = 1;
2893 }
2894
2895 /* When dragging the mouse above the window, scroll down. */
2896 if (is_drag && mouse_row < 0 && !in_status_line)
2897 {
2898 scroll_redraw(FALSE, 1L);
2899 mouse_row = 0;
2900 }
2901
2902 if (start_visual.lnum) /* right click in visual mode */
2903 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002904 /* When ALT is pressed make Visual mode blockwise. */
2905 if (mod_mask & MOD_MASK_ALT)
2906 VIsual_mode = Ctrl_V;
2907
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 /*
2909 * In Visual-block mode, divide the area in four, pick up the corner
2910 * that is in the quarter that the cursor is in.
2911 */
2912 if (VIsual_mode == Ctrl_V)
2913 {
2914 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2915 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2916 end_visual.col = leftcol;
2917 else
2918 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002919 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 end_visual.lnum = start_visual.lnum;
2922
2923 /* move VIsual to the right column */
2924 start_visual = curwin->w_cursor; /* save the cursor pos */
2925 curwin->w_cursor = end_visual;
2926 coladvance(end_visual.col);
2927 VIsual = curwin->w_cursor;
2928 curwin->w_cursor = start_visual; /* restore the cursor */
2929 }
2930 else
2931 {
2932 /*
2933 * If the click is before the start of visual, change the start.
2934 * If the click is after the end of visual, change the end. If
2935 * the click is inside the visual, change the closest side.
2936 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002937 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002939 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 VIsual = start_visual;
2941 else
2942 {
2943 /* In the same line, compare column number */
2944 if (end_visual.lnum == start_visual.lnum)
2945 {
2946 if (curwin->w_cursor.col - start_visual.col >
2947 end_visual.col - curwin->w_cursor.col)
2948 VIsual = start_visual;
2949 else
2950 VIsual = end_visual;
2951 }
2952
2953 /* In different lines, compare line number */
2954 else
2955 {
2956 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2957 (end_visual.lnum - curwin->w_cursor.lnum);
2958
2959 if (diff > 0) /* closest to end */
2960 VIsual = start_visual;
2961 else if (diff < 0) /* closest to start */
2962 VIsual = end_visual;
2963 else /* in the middle line */
2964 {
2965 if (curwin->w_cursor.col <
2966 (start_visual.col + end_visual.col) / 2)
2967 VIsual = end_visual;
2968 else
2969 VIsual = start_visual;
2970 }
2971 }
2972 }
2973 }
2974 }
2975 /*
2976 * If Visual mode started in insert mode, execute "CTRL-O"
2977 */
2978 else if ((State & INSERT) && VIsual_active)
2979 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
2981 /*
2982 * Middle mouse click: Put text before cursor.
2983 */
2984 if (which_button == MOUSE_MIDDLE)
2985 {
2986#ifdef FEAT_CLIPBOARD
2987 if (clip_star.available && regname == 0)
2988 regname = '*';
2989#endif
2990 if (yank_register_mline(regname))
2991 {
2992 if (mouse_past_bottom)
2993 dir = FORWARD;
2994 }
2995 else if (mouse_past_eol)
2996 dir = FORWARD;
2997
2998 if (fixindent)
2999 {
3000 c1 = (dir == BACKWARD) ? '[' : ']';
3001 c2 = 'p';
3002 }
3003 else
3004 {
3005 c1 = (dir == FORWARD) ? 'p' : 'P';
3006 c2 = NUL;
3007 }
3008 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
3009
3010 /*
3011 * Remember where the paste started, so in edit() Insstart can be set
3012 * to this position
3013 */
3014 if (restart_edit != 0)
3015 where_paste_started = curwin->w_cursor;
3016 do_put(regname, dir, count, fixindent | PUT_CURSEND);
3017 }
3018
Bram Moolenaar4033c552017-09-16 20:54:51 +02003019#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 /*
3021 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3022 * error under the mouse pointer.
3023 */
3024 else if (((mod_mask & MOD_MASK_CTRL)
3025 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3026 && bt_quickfix(curbuf))
3027 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003028 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003029 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003030 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003031 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 got_click = FALSE; /* ignore drag&release now */
3033 }
3034#endif
3035
3036 /*
3037 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3038 * under the mouse pointer.
3039 */
3040 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3041 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3042 {
3043 if (State & INSERT)
3044 stuffcharReadbuff(Ctrl_O);
3045 stuffcharReadbuff(Ctrl_RSB);
3046 got_click = FALSE; /* ignore drag&release now */
3047 }
3048
3049 /*
3050 * Shift-Mouse click searches for the next occurrence of the word under
3051 * the mouse pointer
3052 */
3053 else if ((mod_mask & MOD_MASK_SHIFT))
3054 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003055 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 stuffcharReadbuff(Ctrl_O);
3057 if (which_button == MOUSE_LEFT)
3058 stuffcharReadbuff('*');
3059 else /* MOUSE_RIGHT */
3060 stuffcharReadbuff('#');
3061 }
3062
3063 /* Handle double clicks, unless on status line */
3064 else if (in_status_line)
3065 {
3066#ifdef FEAT_MOUSESHAPE
3067 if ((is_drag || is_click) && !drag_status_line)
3068 {
3069 drag_status_line = TRUE;
3070 update_mouseshape(-1);
3071 }
3072#endif
3073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 else if (in_sep_line)
3075 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003076#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 if ((is_drag || is_click) && !drag_sep_line)
3078 {
3079 drag_sep_line = TRUE;
3080 update_mouseshape(-1);
3081 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003082#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3085 && mouse_has(MOUSE_VISUAL))
3086 {
3087 if (is_click || !VIsual_active)
3088 {
3089 if (VIsual_active)
3090 orig_cursor = VIsual;
3091 else
3092 {
3093 check_visual_highlight();
3094 VIsual = curwin->w_cursor;
3095 orig_cursor = VIsual;
3096 VIsual_active = TRUE;
3097 VIsual_reselect = TRUE;
3098 /* start Select mode if 'selectmode' contains "mouse" */
3099 may_start_select('o');
3100 setmouse();
3101 }
3102 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003103 {
3104 /* Double click with ALT pressed makes it blockwise. */
3105 if (mod_mask & MOD_MASK_ALT)
3106 VIsual_mode = Ctrl_V;
3107 else
3108 VIsual_mode = 'v';
3109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3111 VIsual_mode = 'V';
3112 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3113 VIsual_mode = Ctrl_V;
3114#ifdef FEAT_CLIPBOARD
3115 /* Make sure the clipboard gets updated. Needed because start and
3116 * end may still be the same, and the selection needs to be owned */
3117 clip_star.vmode = NUL;
3118#endif
3119 }
3120 /*
3121 * A double click selects a word or a block.
3122 */
3123 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3124 {
3125 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003126 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128 if (is_click)
3129 {
3130 /* If the character under the cursor (skipping white space) is
3131 * not a word character, try finding a match and select a (),
3132 * {}, [], #if/#endif, etc. block. */
3133 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003134 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 inc(&end_visual);
3136 if (oap != NULL)
3137 oap->motion_type = MCHAR;
3138 if (oap != NULL
3139 && VIsual_mode == 'v'
3140 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003141 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 && (pos = findmatch(oap, NUL)) != NULL)
3143 {
3144 curwin->w_cursor = *pos;
3145 if (oap->motion_type == MLINE)
3146 VIsual_mode = 'V';
3147 else if (*p_sel == 'e')
3148 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003149 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 ++VIsual.col;
3151 else
3152 ++curwin->w_cursor.col;
3153 }
3154 }
3155 }
3156
3157 if (pos == NULL && (is_click || is_drag))
3158 {
3159 /* When not found a match or when dragging: extend to include
3160 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003161 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
3163 find_start_of_word(&curwin->w_cursor);
3164 find_end_of_word(&VIsual);
3165 }
3166 else
3167 {
3168 find_start_of_word(&VIsual);
3169 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3170#ifdef FEAT_MBYTE
3171 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003172 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173#else
3174 ++curwin->w_cursor.col;
3175#endif
3176 find_end_of_word(&curwin->w_cursor);
3177 }
3178 }
3179 curwin->w_set_curswant = TRUE;
3180 }
3181 if (is_click)
3182 redraw_curbuf_later(INVERTED); /* update the inversion */
3183 }
3184 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003185 {
3186 if (mod_mask & MOD_MASK_ALT)
3187 VIsual_mode = Ctrl_V;
3188 else
3189 VIsual_mode = 'v';
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003193 if ((!VIsual_active && old_active && mode_displayed)
3194 || (VIsual_active && p_smd && msg_silent == 0
3195 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198 return moved;
3199}
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201/*
3202 * Move "pos" back to the start of the word it's in.
3203 */
3204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003205find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
3207 char_u *line;
3208 int cclass;
3209 int col;
3210
3211 line = ml_get(pos->lnum);
3212 cclass = get_mouse_class(line + pos->col);
3213
3214 while (pos->col > 0)
3215 {
3216 col = pos->col - 1;
3217#ifdef FEAT_MBYTE
3218 col -= (*mb_head_off)(line, line + col);
3219#endif
3220 if (get_mouse_class(line + col) != cclass)
3221 break;
3222 pos->col = col;
3223 }
3224}
3225
3226/*
3227 * Move "pos" forward to the end of the word it's in.
3228 * When 'selection' is "exclusive", the position is just after the word.
3229 */
3230 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003231find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
3233 char_u *line;
3234 int cclass;
3235 int col;
3236
3237 line = ml_get(pos->lnum);
3238 if (*p_sel == 'e' && pos->col > 0)
3239 {
3240 --pos->col;
3241#ifdef FEAT_MBYTE
3242 pos->col -= (*mb_head_off)(line, line + pos->col);
3243#endif
3244 }
3245 cclass = get_mouse_class(line + pos->col);
3246 while (line[pos->col] != NUL)
3247 {
3248#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003249 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250#else
3251 col = pos->col + 1;
3252#endif
3253 if (get_mouse_class(line + col) != cclass)
3254 {
3255 if (*p_sel == 'e')
3256 pos->col = col;
3257 break;
3258 }
3259 pos->col = col;
3260 }
3261}
3262
3263/*
3264 * Get class of a character for selection: same class means same word.
3265 * 0: blank
3266 * 1: punctuation groups
3267 * 2: normal word character
3268 * >2: multi-byte word character.
3269 */
3270 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003271get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 int c;
3274
3275#ifdef FEAT_MBYTE
3276 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3277 return mb_get_class(p);
3278#endif
3279
3280 c = *p;
3281 if (c == ' ' || c == '\t')
3282 return 0;
3283
3284 if (vim_iswordc(c))
3285 return 2;
3286
3287 /*
3288 * There are a few special cases where we want certain combinations of
3289 * characters to be considered as a single word. These are things like
3290 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003291 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 */
3293 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3294 return 1;
3295 return c;
3296}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#endif /* FEAT_MOUSE */
3298
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299/*
3300 * Check if highlighting for visual mode is possible, give a warning message
3301 * if not.
3302 */
3303 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003304check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 static int did_check = FALSE;
3307
3308 if (full_screen)
3309 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003310 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 MSG(_("Warning: terminal cannot highlight"));
3312 did_check = TRUE;
3313 }
3314}
3315
3316/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003317 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 * This function should ALWAYS be called to end Visual mode, except from
3319 * do_pending_operator().
3320 */
3321 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003322end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324#ifdef FEAT_CLIPBOARD
3325 /*
3326 * If we are using the clipboard, then remember what was selected in case
3327 * we need to paste it somewhere while we still own the selection.
3328 * Only do this when the clipboard is already owned. Don't want to grab
3329 * the selection when hitting ESC.
3330 */
3331 if (clip_star.available && clip_star.owned)
3332 clip_auto_select();
3333#endif
3334
3335 VIsual_active = FALSE;
3336#ifdef FEAT_MOUSE
3337 setmouse();
3338 mouse_dragging = 0;
3339#endif
3340
3341 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003342 curbuf->b_visual.vi_mode = VIsual_mode;
3343 curbuf->b_visual.vi_start = VIsual;
3344 curbuf->b_visual.vi_end = curwin->w_cursor;
3345 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#ifdef FEAT_EVAL
3347 curbuf->b_visual_mode_eval = VIsual_mode;
3348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#ifdef FEAT_VIRTUALEDIT
3350 if (!virtual_active())
3351 curwin->w_cursor.coladd = 0;
3352#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003353 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003355 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356}
3357
3358/*
3359 * Reset VIsual_active and VIsual_reselect.
3360 */
3361 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003362reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 if (VIsual_active)
3365 {
3366 end_visual_mode();
3367 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3368 }
3369 VIsual_reselect = FALSE;
3370}
3371
3372/*
3373 * Reset VIsual_active and VIsual_reselect if it's set.
3374 */
3375 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003376reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 if (VIsual_active)
3379 {
3380 end_visual_mode();
3381 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3382 VIsual_reselect = FALSE;
3383 }
3384}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386/*
3387 * Check for a balloon-eval special item to include when searching for an
3388 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3389 * Returns TRUE if the character at "*ptr" should be included.
3390 * "dir" is FORWARD or BACKWARD, the direction of searching.
3391 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3392 * "bnp" points to a counter for square brackets.
3393 */
3394 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003395find_is_eval_item(
3396 char_u *ptr,
3397 int *colp,
3398 int *bnp,
3399 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 /* Accept everything inside []. */
3402 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3403 ++*bnp;
3404 if (*bnp > 0)
3405 {
3406 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3407 --*bnp;
3408 return TRUE;
3409 }
3410
3411 /* skip over "s.var" */
3412 if (*ptr == '.')
3413 return TRUE;
3414
3415 /* two-character item: s->var */
3416 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3417 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3418 {
3419 *colp += dir;
3420 return TRUE;
3421 }
3422 return FALSE;
3423}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425/*
3426 * Find the identifier under or to the right of the cursor.
3427 * "find_type" can have one of three values:
3428 * FIND_IDENT: find an identifier (keyword)
3429 * FIND_STRING: find any non-white string
3430 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003431 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 *
3433 * There are three steps:
3434 * 1. Search forward for the start of an identifier/string. Doesn't move if
3435 * already on one.
3436 * 2. Search backward for the start of this identifier/string.
3437 * This doesn't match the real Vi but I like it a little better and it
3438 * shouldn't bother anyone.
3439 * 3. Search forward to the end of this identifier/string.
3440 * When FIND_IDENT isn't defined, we backup until a blank.
3441 *
3442 * Returns the length of the string, or zero if no string is found.
3443 * If a string is found, a pointer to the string is put in "*string". This
3444 * string is not always NUL terminated.
3445 */
3446 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003447find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3450 curwin->w_cursor.col, string, find_type);
3451}
3452
3453/*
3454 * Like find_ident_under_cursor(), but for any window and any position.
3455 * However: Uses 'iskeyword' from the current window!.
3456 */
3457 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003458find_ident_at_pos(
3459 win_T *wp,
3460 linenr_T lnum,
3461 colnr_T startcol,
3462 char_u **string,
3463 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
3465 char_u *ptr;
3466 int col = 0; /* init to shut up GCC */
3467 int i;
3468#ifdef FEAT_MBYTE
3469 int this_class = 0;
3470 int prev_class;
3471 int prevcol;
3472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 /*
3476 * if i == 0: try to find an identifier
3477 * if i == 1: try to find any non-white string
3478 */
3479 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3480 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3481 {
3482 /*
3483 * 1. skip to start of identifier/string
3484 */
3485 col = startcol;
3486#ifdef FEAT_MBYTE
3487 if (has_mbyte)
3488 {
3489 while (ptr[col] != NUL)
3490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 /* Stop at a ']' to evaluate "a[x]". */
3492 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 this_class = mb_get_class(ptr + col);
3495 if (this_class != 0 && (i == 1 || this_class != 1))
3496 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003497 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499 }
3500 else
3501#endif
3502 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003503 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 )
3506 ++col;
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 /* When starting on a ']' count it, so that we include the '['. */
3509 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 /*
3512 * 2. Back up to start of identifier/string.
3513 */
3514#ifdef FEAT_MBYTE
3515 if (has_mbyte)
3516 {
3517 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3519 this_class = mb_get_class((char_u *)"a");
3520 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003522 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3525 prev_class = mb_get_class(ptr + prevcol);
3526 if (this_class != prev_class
3527 && (i == 0
3528 || prev_class == 0
3529 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 && (!(find_type & FIND_EVAL)
3531 || prevcol == 0
3532 || !find_is_eval_item(ptr + prevcol, &prevcol,
3533 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 )
3535 break;
3536 col = prevcol;
3537 }
3538
3539 /* If we don't want just any old string, or we've found an
3540 * identifier, stop searching. */
3541 if (this_class > 2)
3542 this_class = 2;
3543 if (!(find_type & FIND_STRING) || this_class == 2)
3544 break;
3545 }
3546 else
3547#endif
3548 {
3549 while (col > 0
3550 && ((i == 0
3551 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003552 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 && (!(find_type & FIND_IDENT)
3554 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 || ((find_type & FIND_EVAL)
3556 && col > 1
3557 && find_is_eval_item(ptr + col - 1, &col,
3558 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 ))
3560 --col;
3561
3562 /* If we don't want just any old string, or we've found an
3563 * identifier, stop searching. */
3564 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3565 break;
3566 }
3567 }
3568
3569 if (ptr[col] == NUL || (i == 0 && (
3570#ifdef FEAT_MBYTE
3571 has_mbyte ? this_class != 2 :
3572#endif
3573 !vim_iswordc(ptr[col]))))
3574 {
3575 /*
3576 * didn't find an identifier or string
3577 */
3578 if (find_type & FIND_STRING)
3579 EMSG(_("E348: No string under cursor"));
3580 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003581 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 return 0;
3583 }
3584 ptr += col;
3585 *string = ptr;
3586
3587 /*
3588 * 3. Find the end if the identifier/string.
3589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 bn = 0;
3591 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 col = 0;
3593#ifdef FEAT_MBYTE
3594 if (has_mbyte)
3595 {
3596 /* Search for point of changing multibyte character class. */
3597 this_class = mb_get_class(ptr);
3598 while (ptr[col] != NUL
3599 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3600 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 || ((find_type & FIND_EVAL)
3602 && col <= (int)startcol
3603 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003605 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607 else
3608#endif
3609 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003610 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 || ((find_type & FIND_EVAL)
3612 && col <= (int)startcol
3613 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 )
3615 {
3616 ++col;
3617 }
3618
3619 return col;
3620}
3621
3622/*
3623 * Prepare for redo of a normal command.
3624 */
3625 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003626prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
3628 prep_redo(cap->oap->regname, cap->count0,
3629 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3630}
3631
3632/*
3633 * Prepare for redo of any command.
3634 * Note that only the last argument can be a multi-byte char.
3635 */
3636 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003637prep_redo(
3638 int regname,
3639 long num,
3640 int cmd1,
3641 int cmd2,
3642 int cmd3,
3643 int cmd4,
3644 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645{
3646 ResetRedobuff();
3647 if (regname != 0) /* yank from specified buffer */
3648 {
3649 AppendCharToRedobuff('"');
3650 AppendCharToRedobuff(regname);
3651 }
3652 if (num)
3653 AppendNumberToRedobuff(num);
3654
3655 if (cmd1 != NUL)
3656 AppendCharToRedobuff(cmd1);
3657 if (cmd2 != NUL)
3658 AppendCharToRedobuff(cmd2);
3659 if (cmd3 != NUL)
3660 AppendCharToRedobuff(cmd3);
3661 if (cmd4 != NUL)
3662 AppendCharToRedobuff(cmd4);
3663 if (cmd5 != NUL)
3664 AppendCharToRedobuff(cmd5);
3665}
3666
3667/*
3668 * check for operator active and clear it
3669 *
3670 * return TRUE if operator was active
3671 */
3672 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003673checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 if (oap->op_type == OP_NOP)
3676 return FALSE;
3677 clearopbeep(oap);
3678 return TRUE;
3679}
3680
3681/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003682 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003684 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 */
3686 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003687checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003689 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 return FALSE;
3691 clearopbeep(oap);
3692 return TRUE;
3693}
3694
3695 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003696clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 oap->op_type = OP_NOP;
3699 oap->regname = 0;
3700 oap->motion_force = NUL;
3701 oap->use_reg_one = FALSE;
3702}
3703
3704 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003705clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 clearop(oap);
3708 beep_flush();
3709}
3710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711/*
3712 * Remove the shift modifier from a special key.
3713 */
3714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003715unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716{
3717 switch (cap->cmdchar)
3718 {
3719 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3720 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3721 case K_S_UP: cap->cmdchar = K_UP; break;
3722 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3723 case K_S_HOME: cap->cmdchar = K_HOME; break;
3724 case K_S_END: cap->cmdchar = K_END; break;
3725 }
3726 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003729/*
3730 * If the mode is currently displayed clear the command line or update the
3731 * command displayed.
3732 */
3733 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003734may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003735{
3736 if (mode_displayed)
3737 clear_cmdline = TRUE; /* unshow visual mode later */
3738#ifdef FEAT_CMDL_INFO
3739 else
3740 clear_showcmd();
3741#endif
3742}
3743
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3745/*
3746 * Routines for displaying a partly typed command
3747 */
3748
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003749#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750static char_u showcmd_buf[SHOWCMD_BUFLEN];
3751static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3752static int showcmd_is_clear = TRUE;
3753static int showcmd_visual = FALSE;
3754
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003755static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
3757 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003758clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759{
3760 if (!p_sc)
3761 return;
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (VIsual_active && !char_avail())
3764 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003765 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 long lines;
3767 colnr_T leftcol, rightcol;
3768 linenr_T top, bot;
3769
3770 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003771 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
3773 top = VIsual.lnum;
3774 bot = curwin->w_cursor.lnum;
3775 }
3776 else
3777 {
3778 top = curwin->w_cursor.lnum;
3779 bot = VIsual.lnum;
3780 }
3781# ifdef FEAT_FOLDING
3782 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003783 (void)hasFolding(top, &top, NULL);
3784 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785# endif
3786 lines = bot - top + 1;
3787
3788 if (VIsual_mode == Ctrl_V)
3789 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003790# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003791 char_u *saved_sbr = p_sbr;
3792
3793 /* Make 'sbr' empty for a moment to get the correct size. */
3794 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003797# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003798 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3801 (long)(rightcol - leftcol + 1));
3802 }
3803 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3804 sprintf((char *)showcmd_buf, "%ld", lines);
3805 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003806 {
3807 char_u *s, *e;
3808 int l;
3809 int bytes = 0;
3810 int chars = 0;
3811
3812 if (cursor_bot)
3813 {
3814 s = ml_get_pos(&VIsual);
3815 e = ml_get_cursor();
3816 }
3817 else
3818 {
3819 s = ml_get_cursor();
3820 e = ml_get_pos(&VIsual);
3821 }
3822 while ((*p_sel != 'e') ? s <= e : s < e)
3823 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003824# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003825 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003826# else
3827 l = (*s == NUL) ? 0 : 1;
3828# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003829 if (l == 0)
3830 {
3831 ++bytes;
3832 ++chars;
3833 break; /* end of line */
3834 }
3835 bytes += l;
3836 ++chars;
3837 s += l;
3838 }
3839 if (bytes == chars)
3840 sprintf((char *)showcmd_buf, "%d", chars);
3841 else
3842 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3845 showcmd_visual = TRUE;
3846 }
3847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
3849 showcmd_buf[0] = NUL;
3850 showcmd_visual = FALSE;
3851
3852 /* Don't actually display something if there is nothing to clear. */
3853 if (showcmd_is_clear)
3854 return;
3855 }
3856
3857 display_showcmd();
3858}
3859
3860/*
3861 * Add 'c' to string of shown command chars.
3862 * Return TRUE if output has been written (and setcursor() has been called).
3863 */
3864 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003865add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
3867 char_u *p;
3868 int old_len;
3869 int extra_len;
3870 int overflow;
3871#if defined(FEAT_MOUSE)
3872 int i;
3873 static int ignore[] =
3874 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003875# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3877 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003878# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003879 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003880 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3882 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003883 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003885 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 0
3887 };
3888#endif
3889
Bram Moolenaar09df3122006-01-23 22:23:09 +00003890 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 return FALSE;
3892
3893 if (showcmd_visual)
3894 {
3895 showcmd_buf[0] = NUL;
3896 showcmd_visual = FALSE;
3897 }
3898
3899#if defined(FEAT_MOUSE)
3900 /* Ignore keys that are scrollbar updates and mouse clicks */
3901 if (IS_SPECIAL(c))
3902 for (i = 0; ignore[i] != 0; ++i)
3903 if (ignore[i] == c)
3904 return FALSE;
3905#endif
3906
3907 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003908 if (*p == ' ')
3909 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 old_len = (int)STRLEN(showcmd_buf);
3911 extra_len = (int)STRLEN(p);
3912 overflow = old_len + extra_len - SHOWCMD_COLS;
3913 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003914 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3915 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 STRCAT(showcmd_buf, p);
3917
3918 if (char_avail())
3919 return FALSE;
3920
3921 display_showcmd();
3922
3923 return TRUE;
3924}
3925
3926 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003927add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928{
3929 if (!add_to_showcmd(c))
3930 setcursor();
3931}
3932
3933/*
3934 * Delete 'len' characters from the end of the shown command.
3935 */
3936 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003937del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939 int old_len;
3940
3941 if (!p_sc)
3942 return;
3943
3944 old_len = (int)STRLEN(showcmd_buf);
3945 if (len > old_len)
3946 len = old_len;
3947 showcmd_buf[old_len - len] = NUL;
3948
3949 if (!char_avail())
3950 display_showcmd();
3951}
3952
3953/*
3954 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3955 * something and there is a partial mapping.
3956 */
3957 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003958push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
3960 if (p_sc)
3961 STRCPY(old_showcmd_buf, showcmd_buf);
3962}
3963
3964 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003965pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966{
3967 if (!p_sc)
3968 return;
3969
3970 STRCPY(showcmd_buf, old_showcmd_buf);
3971
3972 display_showcmd();
3973}
3974
3975 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003976display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 int len;
3979
3980 cursor_off();
3981
3982 len = (int)STRLEN(showcmd_buf);
3983 if (len == 0)
3984 showcmd_is_clear = TRUE;
3985 else
3986 {
3987 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3988 showcmd_is_clear = FALSE;
3989 }
3990
3991 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003992 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3993 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 */
3995 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3996
3997 setcursor(); /* put cursor back where it belongs */
3998}
3999#endif
4000
4001#ifdef FEAT_SCROLLBIND
4002/*
4003 * When "check" is FALSE, prepare for commands that scroll the window.
4004 * When "check" is TRUE, take care of scroll-binding after the window has
4005 * scrolled. Called from normal_cmd() and edit().
4006 */
4007 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004008do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
4010 static win_T *old_curwin = NULL;
4011 static linenr_T old_topline = 0;
4012#ifdef FEAT_DIFF
4013 static int old_topfill = 0;
4014#endif
4015 static buf_T *old_buf = NULL;
4016 static colnr_T old_leftcol = 0;
4017
4018 if (check && curwin->w_p_scb)
4019 {
4020 /* If a ":syncbind" command was just used, don't scroll, only reset
4021 * the values. */
4022 if (did_syncbind)
4023 did_syncbind = FALSE;
4024 else if (curwin == old_curwin)
4025 {
4026 /*
4027 * Synchronize other windows, as necessary according to
4028 * 'scrollbind'. Don't do this after an ":edit" command, except
4029 * when 'diff' is set.
4030 */
4031 if ((curwin->w_buffer == old_buf
4032#ifdef FEAT_DIFF
4033 || curwin->w_p_diff
4034#endif
4035 )
4036 && (curwin->w_topline != old_topline
4037#ifdef FEAT_DIFF
4038 || curwin->w_topfill != old_topfill
4039#endif
4040 || curwin->w_leftcol != old_leftcol))
4041 {
4042 check_scrollbind(curwin->w_topline - old_topline,
4043 (long)(curwin->w_leftcol - old_leftcol));
4044 }
4045 }
4046 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4047 {
4048 /*
4049 * When switching between windows, make sure that the relative
4050 * vertical offset is valid for the new window. The relative
4051 * offset is invalid whenever another 'scrollbind' window has
4052 * scrolled to a point that would force the current window to
4053 * scroll past the beginning or end of its buffer. When the
4054 * resync is performed, some of the other 'scrollbind' windows may
4055 * need to jump so that the current window's relative position is
4056 * visible on-screen.
4057 */
4058 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4059 }
4060 curwin->w_scbind_pos = curwin->w_topline;
4061 }
4062
4063 old_curwin = curwin;
4064 old_topline = curwin->w_topline;
4065#ifdef FEAT_DIFF
4066 old_topfill = curwin->w_topfill;
4067#endif
4068 old_buf = curwin->w_buffer;
4069 old_leftcol = curwin->w_leftcol;
4070}
4071
4072/*
4073 * Synchronize any windows that have "scrollbind" set, based on the
4074 * number of rows by which the current window has changed
4075 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4076 */
4077 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004078check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079{
4080 int want_ver;
4081 int want_hor;
4082 win_T *old_curwin = curwin;
4083 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 int old_VIsual_select = VIsual_select;
4085 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 colnr_T tgt_leftcol = curwin->w_leftcol;
4087 long topline;
4088 long y;
4089
4090 /*
4091 * check 'scrollopt' string for vertical and horizontal scroll options
4092 */
4093 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4094#ifdef FEAT_DIFF
4095 want_ver |= old_curwin->w_p_diff;
4096#endif
4097 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4098
4099 /*
4100 * loop through the scrollbound windows and scroll accordingly
4101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004103 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
4105 curbuf = curwin->w_buffer;
4106 /* skip original window and windows with 'noscrollbind' */
4107 if (curwin != old_curwin && curwin->w_p_scb)
4108 {
4109 /*
4110 * do the vertical scroll
4111 */
4112 if (want_ver)
4113 {
4114#ifdef FEAT_DIFF
4115 if (old_curwin->w_p_diff && curwin->w_p_diff)
4116 {
4117 diff_set_topline(old_curwin, curwin);
4118 }
4119 else
4120#endif
4121 {
4122 curwin->w_scbind_pos += topline_diff;
4123 topline = curwin->w_scbind_pos;
4124 if (topline > curbuf->b_ml.ml_line_count)
4125 topline = curbuf->b_ml.ml_line_count;
4126 if (topline < 1)
4127 topline = 1;
4128
4129 y = topline - curwin->w_topline;
4130 if (y > 0)
4131 scrollup(y, FALSE);
4132 else
4133 scrolldown(-y, FALSE);
4134 }
4135
4136 redraw_later(VALID);
4137 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140
4141 /*
4142 * do the horizontal scroll
4143 */
4144 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4145 {
4146 curwin->w_leftcol = tgt_leftcol;
4147 leftcol_changed();
4148 }
4149 }
4150 }
4151
4152 /*
4153 * reset current-window
4154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 VIsual_select = old_VIsual_select;
4156 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 curwin = old_curwin;
4158 curbuf = old_curbuf;
4159}
4160#endif /* #ifdef FEAT_SCROLLBIND */
4161
4162/*
4163 * Command character that's ignored.
4164 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004165 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004168nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004170 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171}
4172
4173/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004174 * Command character that doesn't do anything, but unlike nv_ignore() does
4175 * start edit(). Used for "startinsert" executed while starting up.
4176 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004178nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004179{
4180}
4181
4182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 * Command character doesn't exist.
4184 */
4185 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004186nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
4188 clearopbeep(cap->oap);
4189}
4190
4191/*
4192 * <Help> and <F1> commands.
4193 */
4194 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004195nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
4197 if (!checkclearopq(cap->oap))
4198 ex_help(NULL);
4199}
4200
4201/*
4202 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4203 */
4204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004205nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
Bram Moolenaard79e5502016-01-10 22:13:02 +01004207 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004208 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004209 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004210 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4211 op_addsub(cap->oap, cap->count1, cap->arg);
4212 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004213 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004214 else if (VIsual_active)
4215 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004216 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004217 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218}
4219
4220/*
4221 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4222 */
4223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004224nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004227 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004228 if (mod_mask & MOD_MASK_CTRL)
4229 {
4230 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4231 if (cap->arg == BACKWARD)
4232 goto_tabpage(-(int)cap->count1);
4233 else
4234 goto_tabpage((int)cap->count0);
4235 }
4236 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004237 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239}
4240
4241/*
4242 * Implementation of "gd" and "gD" command.
4243 */
4244 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004245nv_gd(
4246 oparg_T *oap,
4247 int nchar,
4248 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
4250 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 char_u *ptr;
4252
Bram Moolenaard9d30582005-05-18 22:10:28 +00004253 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004254 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4255 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004257#ifdef FEAT_FOLDING
4258 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4259 foldOpenCursor();
4260#endif
4261}
4262
4263/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004264 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4265 * otherwise.
4266 */
4267 static int
4268is_ident(char_u *line, int offset)
4269{
4270 int i;
4271 int incomment = FALSE;
4272 int instring = 0;
4273 int prev = 0;
4274
4275 for (i = 0; i < offset && line[i] != NUL; i++)
4276 {
4277 if (instring != 0)
4278 {
4279 if (prev != '\\' && line[i] == instring)
4280 instring = 0;
4281 }
4282 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4283 {
4284 instring = line[i];
4285 }
4286 else
4287 {
4288 if (incomment)
4289 {
4290 if (prev == '*' && line[i] == '/')
4291 incomment = FALSE;
4292 }
4293 else if (prev == '/' && line[i] == '*')
4294 {
4295 incomment = TRUE;
4296 }
4297 else if (prev == '/' && line[i] == '/')
4298 {
4299 return FALSE;
4300 }
4301 }
4302
4303 prev = line[i];
4304 }
4305
4306 return incomment == FALSE && instring == 0;
4307}
4308
4309/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004310 * Search for variable declaration of "ptr[len]".
4311 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4312 * current file ("gD").
4313 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004314 * Return FAIL when not found.
4315 */
4316 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004317find_decl(
4318 char_u *ptr,
4319 int len,
4320 int locally,
4321 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004322 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004323{
4324 char_u *pat;
4325 pos_T old_pos;
4326 pos_T par_pos;
4327 pos_T found_pos;
4328 int t;
4329 int save_p_ws;
4330 int save_p_scs;
4331 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004332 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004333 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004334 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004335
4336 if ((pat = alloc(len + 7)) == NULL)
4337 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004338
4339 /* Put "\V" before the pattern to avoid that the special meaning of "."
4340 * and "~" causes trouble. */
4341 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4342 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 old_pos = curwin->w_cursor;
4344 save_p_ws = p_ws;
4345 save_p_scs = p_scs;
4346 p_ws = FALSE; /* don't wrap around end of file now */
4347 p_scs = FALSE; /* don't switch ignorecase off now */
4348
4349 /*
4350 * With "gD" go to line 1.
4351 * With "gd" Search back for the start of the current function, then go
4352 * back until a blank line. If this fails go to line 1.
4353 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004354 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {
4356 setpcmark(); /* Set in findpar() otherwise */
4357 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004358 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 else
4361 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004362 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4364 --curwin->w_cursor.lnum;
4365 }
4366 curwin->w_cursor.col = 0;
4367
4368 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004369 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004370 for (;;)
4371 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004372 valid = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004373 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004374 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004375 if (curwin->w_cursor.lnum >= old_pos.lnum)
4376 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004377
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004378 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004379 {
4380 pos_T *pos;
4381
4382 /* Check that the block the match is in doesn't end before the
4383 * position where we started the search from. */
4384 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4385 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4386 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004387 {
4388 /* There can't be a useful match before the end of this block.
4389 * Skip to the end. */
4390 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004391 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004392 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004393 }
4394
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004395 if (t == FAIL)
4396 {
4397 /* If we previously found a valid position, use it. */
4398 if (found_pos.lnum != 0)
4399 {
4400 curwin->w_cursor = found_pos;
4401 t = OK;
4402 }
4403 break;
4404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004406 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004407 {
4408 /* Ignore this line, continue at start of next line. */
4409 ++curwin->w_cursor.lnum;
4410 curwin->w_cursor.col = 0;
4411 continue;
4412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004414 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4415
4416 /* If the current position is not a valid identifier and a previous
4417 * match is present, favor that one instead. */
4418 if (!valid && found_pos.lnum != 0)
4419 {
4420 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004421 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004422 }
4423
4424 /* Global search: use first valid match found */
4425 if (valid && !locally)
4426 break;
4427 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004428 {
4429 /* If we previously found a valid position, use it. */
4430 if (found_pos.lnum != 0)
4431 curwin->w_cursor = found_pos;
4432 break;
4433 }
4434
Bram Moolenaar226630a2016-10-08 19:21:31 +02004435 /* For finding a local variable and the match is before the "{" or
4436 * inside a comment, continue searching. For K&R style function
4437 * declarations this skips the function header without types. */
4438 if (!valid)
4439 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004440 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004441 }
4442 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004443 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004444 /* Remove SEARCH_START from flags to avoid getting stuck at one
4445 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004446 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004448
4449 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004451 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 curwin->w_cursor = old_pos;
4453 }
4454 else
4455 {
4456 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 /* "n" searches forward now */
4458 reset_search_dir();
4459 }
4460
4461 vim_free(pat);
4462 p_ws = save_p_ws;
4463 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004464
4465 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466}
4467
4468/*
4469 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4470 * lines rather than lines in the file.
4471 * 'dist' must be positive.
4472 *
4473 * Return OK if able to move cursor, FAIL otherwise.
4474 */
4475 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004476nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477{
4478 int linelen = linetabsize(ml_get_curline());
4479 int retval = OK;
4480 int atend = FALSE;
4481 int n;
4482 int col_off1; /* margin offset for first screen line */
4483 int col_off2; /* margin offset for wrapped screen line */
4484 int width1; /* text width for first screen line */
4485 int width2; /* test width for wrapped screen line */
4486
4487 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004488 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 col_off1 = curwin_col_off();
4491 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004492 width1 = curwin->w_width - col_off1;
4493 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004494 if (width2 == 0)
4495 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 /*
4500 * Instead of sticking at the last character of the buffer line we
4501 * try to stick in the last column of the screen.
4502 */
4503 if (curwin->w_curswant == MAXCOL)
4504 {
4505 atend = TRUE;
4506 validate_virtcol();
4507 if (width1 <= 0)
4508 curwin->w_curswant = 0;
4509 else
4510 {
4511 curwin->w_curswant = width1 - 1;
4512 if (curwin->w_virtcol > curwin->w_curswant)
4513 curwin->w_curswant += ((curwin->w_virtcol
4514 - curwin->w_curswant - 1) / width2 + 1) * width2;
4515 }
4516 }
4517 else
4518 {
4519 if (linelen > width1)
4520 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4521 else
4522 n = width1;
4523 if (curwin->w_curswant > (colnr_T)n + 1)
4524 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4525 * width2;
4526 }
4527
4528 while (dist--)
4529 {
4530 if (dir == BACKWARD)
4531 {
4532 if ((long)curwin->w_curswant >= width2)
4533 /* move back within line */
4534 curwin->w_curswant -= width2;
4535 else
4536 {
4537 /* to previous line */
4538 if (curwin->w_cursor.lnum == 1)
4539 {
4540 retval = FAIL;
4541 break;
4542 }
4543 --curwin->w_cursor.lnum;
4544#ifdef FEAT_FOLDING
4545 /* Move to the start of a closed fold. Don't do that when
4546 * 'foldopen' contains "all": it will open in a moment. */
4547 if (!(fdo_flags & FDO_ALL))
4548 (void)hasFolding(curwin->w_cursor.lnum,
4549 &curwin->w_cursor.lnum, NULL);
4550#endif
4551 linelen = linetabsize(ml_get_curline());
4552 if (linelen > width1)
4553 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4554 + 1) * width2;
4555 }
4556 }
4557 else /* dir == FORWARD */
4558 {
4559 if (linelen > width1)
4560 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4561 else
4562 n = width1;
4563 if (curwin->w_curswant + width2 < (colnr_T)n)
4564 /* move forward within line */
4565 curwin->w_curswant += width2;
4566 else
4567 {
4568 /* to next line */
4569#ifdef FEAT_FOLDING
4570 /* Move to the end of a closed fold. */
4571 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4572 &curwin->w_cursor.lnum);
4573#endif
4574 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4575 {
4576 retval = FAIL;
4577 break;
4578 }
4579 curwin->w_cursor.lnum++;
4580 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004581 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 }
4584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004587 if (virtual_active() && atend)
4588 coladvance(MAXCOL);
4589 else
4590 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
4592#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4593 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4594 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004595 colnr_T virtcol;
4596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 /*
4598 * Check for landing on a character that got split at the end of the
4599 * last line. We want to advance a screenline, not end up in the same
4600 * screenline or move two screenlines.
4601 */
4602 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004603 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004604# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004605 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4606 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004607# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004608
4609 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 && (curwin->w_curswant < (colnr_T)width1
4611 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4612 : ((curwin->w_curswant - width1) % width2
4613 > (colnr_T)width2 / 2)))
4614 --curwin->w_cursor.col;
4615 }
4616#endif
4617
4618 if (atend)
4619 curwin->w_curswant = MAXCOL; /* stick in the last column */
4620
4621 return retval;
4622}
4623
4624#ifdef FEAT_MOUSE
4625/*
4626 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4627 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004628 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4629 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 */
4631 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004632nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004634 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004636 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
4638 int row, col;
4639
4640 row = mouse_row;
4641 col = mouse_col;
4642
4643 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004644 wp = mouse_find_win(&row, &col);
4645 if (wp == NULL)
4646 return;
4647 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 curbuf = curwin->w_buffer;
4649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004651 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004653# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004654 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004655 /* This window is a terminal window, send the mouse event there.
4656 * Set "typed" to FALSE to avoid an endless loop. */
4657 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004658 else
4659# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004660 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4661 {
4662 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4663 }
4664 else
4665 {
4666 cap->count1 = 3;
4667 cap->count0 = 3;
4668 nv_scroll_line(cap);
4669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004671# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 else
4673 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004674 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4675 if (!curwin->w_p_wrap)
4676 {
4677 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004678
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004679 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004680 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004681 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4682 if (val < 0)
4683 val = 0;
4684
4685 gui_do_horiz_scroll(val, TRUE);
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 curwin->w_redr_status = TRUE;
4691
4692 curwin = old_curwin;
4693 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694}
4695
4696/*
4697 * Mouse clicks and drags.
4698 */
4699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004700nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4703}
4704#endif
4705
4706/*
4707 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4708 * cap->arg must be TRUE for CTRL-E.
4709 */
4710 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004711nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
4713 if (!checkclearop(cap->oap))
4714 scroll_redraw(cap->arg, cap->count1);
4715}
4716
4717/*
4718 * Scroll "count" lines up or down, and redraw.
4719 */
4720 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004721scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 linenr_T prev_topline = curwin->w_topline;
4724#ifdef FEAT_DIFF
4725 int prev_topfill = curwin->w_topfill;
4726#endif
4727 linenr_T prev_lnum = curwin->w_cursor.lnum;
4728
4729 if (up)
4730 scrollup(count, TRUE);
4731 else
4732 scrolldown(count, TRUE);
4733 if (p_so)
4734 {
4735 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4736 * valid, otherwise the screen jumps back at the end of the file. */
4737 cursor_correct();
4738 check_cursor_moved(curwin);
4739 curwin->w_valid |= VALID_TOPLINE;
4740
4741 /* If moved back to where we were, at least move the cursor, otherwise
4742 * we get stuck at one position. Don't move the cursor up if the
4743 * first line of the buffer is already on the screen */
4744 while (curwin->w_topline == prev_topline
4745#ifdef FEAT_DIFF
4746 && curwin->w_topfill == prev_topfill
4747#endif
4748 )
4749 {
4750 if (up)
4751 {
4752 if (curwin->w_cursor.lnum > prev_lnum
4753 || cursor_down(1L, FALSE) == FAIL)
4754 break;
4755 }
4756 else
4757 {
4758 if (curwin->w_cursor.lnum < prev_lnum
4759 || prev_topline == 1L
4760 || cursor_up(1L, FALSE) == FAIL)
4761 break;
4762 }
4763 /* Mark w_topline as valid, otherwise the screen jumps back at the
4764 * end of the file. */
4765 check_cursor_moved(curwin);
4766 curwin->w_valid |= VALID_TOPLINE;
4767 }
4768 }
4769 if (curwin->w_cursor.lnum != prev_lnum)
4770 coladvance(curwin->w_curswant);
4771 redraw_later(VALID);
4772}
4773
4774/*
4775 * Commands that start with "z".
4776 */
4777 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004778nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
4780 long n;
4781 colnr_T col;
4782 int nchar = cap->nchar;
4783#ifdef FEAT_FOLDING
4784 long old_fdl = curwin->w_p_fdl;
4785 int old_fen = curwin->w_p_fen;
4786#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004787#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004788 int undo = FALSE;
4789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
4791 if (VIM_ISDIGIT(nchar))
4792 {
4793 /*
4794 * "z123{nchar}": edit the count before obtaining {nchar}
4795 */
4796 if (checkclearop(cap->oap))
4797 return;
4798 n = nchar - '0';
4799 for (;;)
4800 {
4801#ifdef USE_ON_FLY_SCROLL
4802 dont_scroll = TRUE; /* disallow scrolling here */
4803#endif
4804 ++no_mapping;
4805 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004806 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 --no_mapping;
4809 --allow_keys;
4810#ifdef FEAT_CMDL_INFO
4811 (void)add_to_showcmd(nchar);
4812#endif
4813 if (nchar == K_DEL || nchar == K_KDEL)
4814 n /= 10;
4815 else if (VIM_ISDIGIT(nchar))
4816 n = n * 10 + (nchar - '0');
4817 else if (nchar == CAR)
4818 {
4819#ifdef FEAT_GUI
4820 need_mouse_correct = TRUE;
4821#endif
4822 win_setheight((int)n);
4823 break;
4824 }
4825 else if (nchar == 'l'
4826 || nchar == 'h'
4827 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004828 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
4830 cap->count1 = n ? n * cap->count1 : cap->count1;
4831 goto dozet;
4832 }
4833 else
4834 {
4835 clearopbeep(cap->oap);
4836 break;
4837 }
4838 }
4839 cap->oap->op_type = OP_NOP;
4840 return;
4841 }
4842
4843dozet:
4844 if (
4845#ifdef FEAT_FOLDING
4846 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4847 * and "zC" only in Visual mode. "zj" and "zk" are motion
4848 * commands. */
4849 cap->nchar != 'f' && cap->nchar != 'F'
4850 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4851 && cap->nchar != 'j' && cap->nchar != 'k'
4852 &&
4853#endif
4854 checkclearop(cap->oap))
4855 return;
4856
4857 /*
4858 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4859 * If line number given, set cursor.
4860 */
4861 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4862 && cap->count0
4863 && cap->count0 != curwin->w_cursor.lnum)
4864 {
4865 setpcmark();
4866 if (cap->count0 > curbuf->b_ml.ml_line_count)
4867 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4868 else
4869 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004870 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872
4873 switch (nchar)
4874 {
4875 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4876 case '+':
4877 if (cap->count0 == 0)
4878 {
4879 /* No count given: put cursor at the line below screen */
4880 validate_botline(); /* make sure w_botline is valid */
4881 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4882 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4883 else
4884 curwin->w_cursor.lnum = curwin->w_botline;
4885 }
4886 /* FALLTHROUGH */
4887 case NL:
4888 case CAR:
4889 case K_KENTER:
4890 beginline(BL_WHITE | BL_FIX);
4891 /* FALLTHROUGH */
4892
4893 case 't': scroll_cursor_top(0, TRUE);
4894 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004895 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 break;
4897
4898 /* "z." and "zz": put cursor in middle of screen */
4899 case '.': beginline(BL_WHITE | BL_FIX);
4900 /* FALLTHROUGH */
4901
4902 case 'z': scroll_cursor_halfway(TRUE);
4903 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004904 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 break;
4906
4907 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4908 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4909 * when <count> is at bottom of window, and puts that one at
4910 * bottom of window. */
4911 if (cap->count0 != 0)
4912 {
4913 scroll_cursor_bot(0, TRUE);
4914 curwin->w_cursor.lnum = curwin->w_topline;
4915 }
4916 else if (curwin->w_topline == 1)
4917 curwin->w_cursor.lnum = 1;
4918 else
4919 curwin->w_cursor.lnum = curwin->w_topline - 1;
4920 /* FALLTHROUGH */
4921 case '-':
4922 beginline(BL_WHITE | BL_FIX);
4923 /* FALLTHROUGH */
4924
4925 case 'b': scroll_cursor_bot(0, TRUE);
4926 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004927 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 break;
4929
4930 /* "zH" - scroll screen right half-page */
4931 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004932 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 /* FALLTHROUGH */
4934
4935 /* "zh" - scroll screen to the right */
4936 case 'h':
4937 case K_LEFT:
4938 if (!curwin->w_p_wrap)
4939 {
4940 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4941 curwin->w_leftcol = 0;
4942 else
4943 curwin->w_leftcol -= (colnr_T)cap->count1;
4944 leftcol_changed();
4945 }
4946 break;
4947
4948 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004949 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 /* FALLTHROUGH */
4951
4952 /* "zl" - scroll screen to the left */
4953 case 'l':
4954 case K_RIGHT:
4955 if (!curwin->w_p_wrap)
4956 {
4957 /* scroll the window left */
4958 curwin->w_leftcol += (colnr_T)cap->count1;
4959 leftcol_changed();
4960 }
4961 break;
4962
4963 /* "zs" - scroll screen, cursor at the start */
4964 case 's': 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, &col, NULL, NULL);
4972 if ((long)col > p_siso)
4973 col -= p_siso;
4974 else
4975 col = 0;
4976 if (curwin->w_leftcol != col)
4977 {
4978 curwin->w_leftcol = col;
4979 redraw_later(NOT_VALID);
4980 }
4981 }
4982 break;
4983
4984 /* "ze" - scroll screen, cursor at the end */
4985 case 'e': if (!curwin->w_p_wrap)
4986 {
4987#ifdef FEAT_FOLDING
4988 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4989 col = 0; /* like the cursor is in col 0 */
4990 else
4991#endif
4992 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004993 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 if ((long)col + p_siso < n)
4995 col = 0;
4996 else
4997 col = col + p_siso - n + 1;
4998 if (curwin->w_leftcol != col)
4999 {
5000 curwin->w_leftcol = col;
5001 redraw_later(NOT_VALID);
5002 }
5003 }
5004 break;
5005
5006#ifdef FEAT_FOLDING
5007 /* "zF": create fold command */
5008 /* "zf": create fold operator */
5009 case 'F':
5010 case 'f': if (foldManualAllowed(TRUE))
5011 {
5012 cap->nchar = 'f';
5013 nv_operator(cap);
5014 curwin->w_p_fen = TRUE;
5015
5016 /* "zF" is like "zfzf" */
5017 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5018 {
5019 nv_operator(cap);
5020 finish_op = TRUE;
5021 }
5022 }
5023 else
5024 clearopbeep(cap->oap);
5025 break;
5026
5027 /* "zd": delete fold at cursor */
5028 /* "zD": delete fold at cursor recursively */
5029 case 'd':
5030 case 'D': if (foldManualAllowed(FALSE))
5031 {
5032 if (VIsual_active)
5033 nv_operator(cap);
5034 else
5035 deleteFold(curwin->w_cursor.lnum,
5036 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5037 }
5038 break;
5039
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005040 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 case 'E': if (foldmethodIsManual(curwin))
5042 {
5043 clearFolding(curwin);
5044 changed_window_setting();
5045 }
5046 else if (foldmethodIsMarker(curwin))
5047 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5048 TRUE, FALSE);
5049 else
5050 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5051 break;
5052
5053 /* "zn": fold none: reset 'foldenable' */
5054 case 'n': curwin->w_p_fen = FALSE;
5055 break;
5056
5057 /* "zN": fold Normal: set 'foldenable' */
5058 case 'N': curwin->w_p_fen = TRUE;
5059 break;
5060
5061 /* "zi": invert folding: toggle 'foldenable' */
5062 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5063 break;
5064
5065 /* "za": open closed fold or close open fold at cursor */
5066 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5067 openFold(curwin->w_cursor.lnum, cap->count1);
5068 else
5069 {
5070 closeFold(curwin->w_cursor.lnum, cap->count1);
5071 curwin->w_p_fen = TRUE;
5072 }
5073 break;
5074
5075 /* "zA": open fold at cursor recursively */
5076 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5077 openFoldRecurse(curwin->w_cursor.lnum);
5078 else
5079 {
5080 closeFoldRecurse(curwin->w_cursor.lnum);
5081 curwin->w_p_fen = TRUE;
5082 }
5083 break;
5084
5085 /* "zo": open fold at cursor or Visual area */
5086 case 'o': if (VIsual_active)
5087 nv_operator(cap);
5088 else
5089 openFold(curwin->w_cursor.lnum, cap->count1);
5090 break;
5091
5092 /* "zO": open fold recursively */
5093 case 'O': if (VIsual_active)
5094 nv_operator(cap);
5095 else
5096 openFoldRecurse(curwin->w_cursor.lnum);
5097 break;
5098
5099 /* "zc": close fold at cursor or Visual area */
5100 case 'c': if (VIsual_active)
5101 nv_operator(cap);
5102 else
5103 closeFold(curwin->w_cursor.lnum, cap->count1);
5104 curwin->w_p_fen = TRUE;
5105 break;
5106
5107 /* "zC": close fold recursively */
5108 case 'C': if (VIsual_active)
5109 nv_operator(cap);
5110 else
5111 closeFoldRecurse(curwin->w_cursor.lnum);
5112 curwin->w_p_fen = TRUE;
5113 break;
5114
5115 /* "zv": open folds at the cursor */
5116 case 'v': foldOpenCursor();
5117 break;
5118
5119 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5120 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005121 curwin->w_foldinvalid = TRUE; /* recompute folds */
5122 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 foldOpenCursor();
5124 break;
5125
5126 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5127 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005128 curwin->w_foldinvalid = TRUE; /* recompute folds */
5129 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 break;
5131
5132 /* "zm": fold more */
5133 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005134 {
5135 curwin->w_p_fdl -= cap->count1;
5136 if (curwin->w_p_fdl < 0)
5137 curwin->w_p_fdl = 0;
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 old_fdl = -1; /* force an update */
5140 curwin->w_p_fen = TRUE;
5141 break;
5142
5143 /* "zM": close all folds */
5144 case 'M': curwin->w_p_fdl = 0;
5145 old_fdl = -1; /* force an update */
5146 curwin->w_p_fen = TRUE;
5147 break;
5148
5149 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005150 case 'r': curwin->w_p_fdl += cap->count1;
5151 {
5152 int d = getDeepestNesting();
5153
5154 if (curwin->w_p_fdl >= d)
5155 curwin->w_p_fdl = d;
5156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 break;
5158
5159 /* "zR": open all folds */
5160 case 'R': curwin->w_p_fdl = getDeepestNesting();
5161 old_fdl = -1; /* force an update */
5162 break;
5163
5164 case 'j': /* "zj" move to next fold downwards */
5165 case 'k': /* "zk" move to next fold upwards */
5166 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5167 cap->count1) == FAIL)
5168 clearopbeep(cap->oap);
5169 break;
5170
5171#endif /* FEAT_FOLDING */
5172
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005173#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005174 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5175 ++no_mapping;
5176 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005177 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005178 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005179 --no_mapping;
5180 --allow_keys;
5181#ifdef FEAT_CMDL_INFO
5182 (void)add_to_showcmd(nchar);
5183#endif
5184 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5185 {
5186 clearopbeep(cap->oap);
5187 break;
5188 }
5189 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005190 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005191
Bram Moolenaarb765d632005-06-07 21:00:02 +00005192 case 'g': /* "zg": add good word to word list */
5193 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005194 case 'G': /* "zG": add good word to temp word list */
5195 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005196 {
5197 char_u *ptr = NULL;
5198 int len;
5199
5200 if (checkclearop(cap->oap))
5201 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005202 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5203 == FAIL)
5204 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005205 if (ptr == NULL)
5206 {
5207 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005208
Bram Moolenaar134bf072013-09-25 18:54:24 +02005209 /* Find bad word under the cursor. When 'spell' is
5210 * off this fails and find_ident_under_cursor() is
5211 * used below. */
5212 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005213 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005214 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005215 if (len != 0 && curwin->w_cursor.col <= pos.col)
5216 ptr = ml_get_pos(&curwin->w_cursor);
5217 curwin->w_cursor = pos;
5218 }
5219
Bram Moolenaarb765d632005-06-07 21:00:02 +00005220 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5221 FIND_IDENT)) == 0)
5222 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005223 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005224 (nchar == 'G' || nchar == 'W')
5225 ? 0 : (int)cap->count1,
5226 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005227 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005228 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229
Bram Moolenaar43abc522005-12-10 20:15:02 +00005230 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005231 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005232 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005234#endif
5235
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 default: clearopbeep(cap->oap);
5237 }
5238
5239#ifdef FEAT_FOLDING
5240 /* Redraw when 'foldenable' changed */
5241 if (old_fen != curwin->w_p_fen)
5242 {
5243# ifdef FEAT_DIFF
5244 win_T *wp;
5245
5246 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5247 {
5248 /* Adjust 'foldenable' in diff-synced windows. */
5249 FOR_ALL_WINDOWS(wp)
5250 {
5251 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5252 {
5253 wp->w_p_fen = curwin->w_p_fen;
5254 changed_window_setting_win(wp);
5255 }
5256 }
5257 }
5258# endif
5259 changed_window_setting();
5260 }
5261
5262 /* Redraw when 'foldlevel' changed. */
5263 if (old_fdl != curwin->w_p_fdl)
5264 newFoldLevel();
5265#endif
5266}
5267
5268#ifdef FEAT_GUI
5269/*
5270 * Vertical scrollbar movement.
5271 */
5272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005273nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 if (cap->oap->op_type != OP_NOP)
5276 clearopbeep(cap->oap);
5277
5278 /* Even if an operator was pending, we still want to scroll */
5279 gui_do_scroll();
5280}
5281
5282/*
5283 * Horizontal scrollbar movement.
5284 */
5285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 if (cap->oap->op_type != OP_NOP)
5289 clearopbeep(cap->oap);
5290
5291 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005292 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293}
5294#endif
5295
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005296#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005297/*
5298 * Click in GUI tab.
5299 */
5300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005301nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005302{
5303 if (cap->oap->op_type != OP_NOP)
5304 clearopbeep(cap->oap);
5305
5306 /* Even if an operator was pending, we still want to jump tabs. */
5307 goto_tabpage(current_tab);
5308}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005309
5310/*
5311 * Selected item in tab line menu.
5312 */
5313 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005314nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005315{
5316 if (cap->oap->op_type != OP_NOP)
5317 clearopbeep(cap->oap);
5318
5319 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005320 handle_tabmenu();
5321}
5322
5323/*
5324 * Handle selecting an item of the GUI tab line menu.
5325 * Used in Normal and Insert mode.
5326 */
5327 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005328handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005329{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005330 switch (current_tabmenu)
5331 {
5332 case TABLINE_MENU_CLOSE:
5333 if (current_tab == 0)
5334 do_cmdline_cmd((char_u *)"tabclose");
5335 else
5336 {
5337 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5338 current_tab);
5339 do_cmdline_cmd(IObuff);
5340 }
5341 break;
5342
5343 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005344 if (current_tab == 0)
5345 do_cmdline_cmd((char_u *)"$tabnew");
5346 else
5347 {
5348 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5349 current_tab - 1);
5350 do_cmdline_cmd(IObuff);
5351 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005352 break;
5353
5354 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005355 if (current_tab == 0)
5356 do_cmdline_cmd((char_u *)"browse $tabnew");
5357 else
5358 {
5359 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5360 current_tab - 1);
5361 do_cmdline_cmd(IObuff);
5362 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005363 break;
5364 }
5365}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005366#endif
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368/*
5369 * "Q" command.
5370 */
5371 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005372nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
5374 /*
5375 * Ignore 'Q' in Visual mode, just give a beep.
5376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005378 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005379 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 do_exmode(FALSE);
5381}
5382
5383/*
5384 * Handle a ":" command.
5385 */
5386 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005387nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005390 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 if (VIsual_active)
5393 nv_operator(cap);
5394 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 if (cap->oap->op_type != OP_NOP)
5397 {
5398 /* Using ":" as a movement is characterwise exclusive. */
5399 cap->oap->motion_type = MCHAR;
5400 cap->oap->inclusive = FALSE;
5401 }
5402 else if (cap->count0)
5403 {
5404 /* translate "count:" into ":.,.+(count - 1)" */
5405 stuffcharReadbuff('.');
5406 if (cap->count0 > 1)
5407 {
5408 stuffReadbuff((char_u *)",.+");
5409 stuffnumReadbuff((long)cap->count0 - 1L);
5410 }
5411 }
5412
5413 /* When typing, don't type below an old message */
5414 if (KeyTyped)
5415 compute_cmdrow();
5416
5417 old_p_im = p_im;
5418
5419 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005420 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5422
5423 /* If 'insertmode' changed, enter or exit Insert mode */
5424 if (p_im != old_p_im)
5425 {
5426 if (p_im)
5427 restart_edit = 'i';
5428 else
5429 restart_edit = 0;
5430 }
5431
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005432 if (cmd_result == FAIL)
5433 /* The Ex command failed, do not execute the operator. */
5434 clearop(cap->oap);
5435 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5437 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005438 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5439 || did_emsg
5440 ))
5441 /* The start of the operator has become invalid by the Ex command.
5442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 clearopbeep(cap->oap);
5444 }
5445}
5446
5447/*
5448 * Handle CTRL-G command.
5449 */
5450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005451nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 if (VIsual_active) /* toggle Selection/Visual mode */
5454 {
5455 VIsual_select = !VIsual_select;
5456 showmode();
5457 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005458 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 /* print full name if count given or :cd used */
5460 fileinfo((int)cap->count0, FALSE, TRUE);
5461}
5462
5463/*
5464 * Handle CTRL-H <Backspace> command.
5465 */
5466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005467nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 if (VIsual_active && VIsual_select)
5470 {
5471 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5472 v_visop(cap);
5473 }
5474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 nv_left(cap);
5476}
5477
5478/*
5479 * CTRL-L: clear screen and redraw.
5480 */
5481 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005482nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
5484 if (!checkclearop(cap->oap))
5485 {
5486#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5487 /*
5488 * Right now, the BeBox doesn't seem to have an easy way to detect
5489 * window resizing, so we cheat and make the user detect it
5490 * manually with CTRL-L instead
5491 */
5492 ui_get_shellsize();
5493#endif
5494#ifdef FEAT_SYN_HL
5495 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005496 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005497# ifdef FEAT_RELTIME
5498 {
5499 win_T *wp;
5500
5501 FOR_ALL_WINDOWS(wp)
5502 wp->w_s->b_syn_slow = FALSE;
5503 }
5504# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505#endif
5506 redraw_later(CLEAR);
5507 }
5508}
5509
5510/*
5511 * CTRL-O: In Select mode: switch to Visual mode for one command.
5512 * Otherwise: Go to older pcmark.
5513 */
5514 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005515nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (VIsual_active && VIsual_select)
5518 {
5519 VIsual_select = FALSE;
5520 showmode();
5521 restart_VIsual_select = 2; /* restart Select mode later */
5522 }
5523 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
5525 cap->count1 = -cap->count1;
5526 nv_pcmark(cap);
5527 }
5528}
5529
5530/*
5531 * CTRL-^ command, short for ":e #"
5532 */
5533 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005534nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536 if (!checkclearopq(cap->oap))
5537 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5538 GETF_SETMARK|GETF_ALT, FALSE);
5539}
5540
5541/*
5542 * "Z" commands.
5543 */
5544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005545nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 if (!checkclearopq(cap->oap))
5548 {
5549 switch (cap->nchar)
5550 {
5551 /* "ZZ": equivalent to ":x". */
5552 case 'Z': do_cmdline_cmd((char_u *)"x");
5553 break;
5554
5555 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5556 case 'Q': do_cmdline_cmd((char_u *)"q!");
5557 break;
5558
5559 default: clearopbeep(cap->oap);
5560 }
5561 }
5562}
5563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564/*
5565 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5566 */
5567 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005568do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 oparg_T oa;
5571 cmdarg_T ca;
5572
5573 clear_oparg(&oa);
5574 vim_memset(&ca, 0, sizeof(ca));
5575 ca.oap = &oa;
5576 ca.cmdchar = c1;
5577 ca.nchar = c2;
5578 nv_ident(&ca);
5579}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
5581/*
5582 * Handle the commands that use the word under the cursor.
5583 * [g] CTRL-] :ta to current identifier
5584 * [g] 'K' run program for current identifier
5585 * [g] '*' / to current identifier or string
5586 * [g] '#' ? to current identifier or string
5587 * g ']' :tselect for current identifier
5588 */
5589 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005590nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 char_u *ptr = NULL;
5593 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005594 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005595 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 char_u *p;
5597 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005598 int kp_help; /* 'keywordprg' is ":he" */
5599 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 int n = 0; /* init for GCC */
5601 int cmdchar;
5602 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005603 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 char_u *aux_ptr;
5605 int isman;
5606 int isman_s;
5607
5608 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5609 {
5610 cmdchar = cap->nchar;
5611 g_cmd = TRUE;
5612 }
5613 else
5614 {
5615 cmdchar = cap->cmdchar;
5616 g_cmd = FALSE;
5617 }
5618
5619 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5620 cmdchar = '#';
5621
5622 /*
5623 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5624 */
5625 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5628 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 if (checkclearopq(cap->oap))
5630 return;
5631 }
5632
5633 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5634 (cmdchar == '*' || cmdchar == '#')
5635 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5636 {
5637 clearop(cap->oap);
5638 return;
5639 }
5640
5641 /* Allocate buffer to put the command in. Inserting backslashes can
5642 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5643 * and some numbers. */
5644 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5645 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5646 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005647 if (kp_help && *skipwhite(ptr) == NUL)
5648 {
5649 EMSG(_(e_noident)); /* found white space only */
5650 return;
5651 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005652 kp_ex = (*kp == ':');
5653 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5654 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 if (buf == NULL)
5656 return;
5657 buf[0] = NUL;
5658
5659 switch (cmdchar)
5660 {
5661 case '*':
5662 case '#':
5663 /*
5664 * Put cursor at start of word, makes search skip the word
5665 * under the cursor.
5666 * Call setpcmark() first, so "*``" puts the cursor back where
5667 * it was.
5668 */
5669 setpcmark();
5670 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5671
5672 if (!g_cmd && vim_iswordp(ptr))
5673 STRCPY(buf, "\\<");
5674 no_smartcase = TRUE; /* don't use 'smartcase' now */
5675 break;
5676
5677 case 'K':
5678 if (kp_help)
5679 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005680 else if (kp_ex)
5681 {
5682 if (cap->count0 != 0)
5683 vim_snprintf((char *)buf, buflen, "%s %ld",
5684 kp, cap->count0);
5685 else
5686 STRCPY(buf, kp);
5687 STRCAT(buf, " ");
5688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 else
5690 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005691 /* An external command will probably use an argument starting
5692 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005693 while (*ptr == '-' && n > 0)
5694 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005695 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005696 --n;
5697 }
5698 if (n == 0)
5699 {
5700 EMSG(_(e_noident)); /* found dashes only */
5701 vim_free(buf);
5702 return;
5703 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005704
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 /* When a count is given, turn it into a range. Is this
5706 * really what we want? */
5707 isman = (STRCMP(kp, "man") == 0);
5708 isman_s = (STRCMP(kp, "man -s") == 0);
5709 if (cap->count0 != 0 && !(isman || isman_s))
5710 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5711
5712 STRCAT(buf, "! ");
5713 if (cap->count0 == 0 && isman_s)
5714 STRCAT(buf, "man");
5715 else
5716 STRCAT(buf, kp);
5717 STRCAT(buf, " ");
5718 if (cap->count0 != 0 && (isman || isman_s))
5719 {
5720 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5721 STRCAT(buf, " ");
5722 }
5723 }
5724 break;
5725
5726 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005727 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728#ifdef FEAT_CSCOPE
5729 if (p_cst)
5730 STRCPY(buf, "cstag ");
5731 else
5732#endif
5733 STRCPY(buf, "ts ");
5734 break;
5735
5736 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005737 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (curbuf->b_help)
5739 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005741 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005742 if (g_cmd)
5743 STRCPY(buf, "tj ");
5744 else
5745 sprintf((char *)buf, "%ldta ", cap->count0);
5746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748
5749 /*
5750 * Now grab the chars in the identifier
5751 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005752 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005754 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005755 if (kp_ex)
5756 /* Escape the argument properly for an Ex command */
5757 p = vim_strsave_fnameescape(ptr, FALSE);
5758 else
5759 /* Escape the argument properly for a shell command */
5760 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005761 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005762 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005764 vim_free(buf);
5765 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005767 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5768 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005769 {
5770 vim_free(buf);
5771 vim_free(p);
5772 return;
5773 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005774 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005775 STRCAT(buf, p);
5776 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005778 else
5779 {
5780 if (cmdchar == '*')
5781 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5782 else if (cmdchar == '#')
5783 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005784 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005785 {
5786 if (curbuf->b_help)
5787 /* ":help" handles unescaped argument */
5788 aux_ptr = (char_u *)"";
5789 else
5790 aux_ptr = (char_u *)"\\|\"\n[";
5791 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005792 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005793 aux_ptr = (char_u *)"\\|\"\n*?[";
5794
5795 p = buf + STRLEN(buf);
5796 while (n-- > 0)
5797 {
5798 /* put a backslash before \ and some others */
5799 if (vim_strchr(aux_ptr, *ptr) != NULL)
5800 *p++ = '\\';
5801#ifdef FEAT_MBYTE
5802 /* When current byte is a part of multibyte character, copy all
5803 * bytes of that character. */
5804 if (has_mbyte)
5805 {
5806 int i;
5807 int len = (*mb_ptr2len)(ptr) - 1;
5808
5809 for (i = 0; i < len && n >= 1; ++i, --n)
5810 *p++ = *ptr++;
5811 }
5812#endif
5813 *p++ = *ptr++;
5814 }
5815 *p = NUL;
5816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
5818 /*
5819 * Execute the command.
5820 */
5821 if (cmdchar == '*' || cmdchar == '#')
5822 {
5823 if (!g_cmd && (
5824#ifdef FEAT_MBYTE
5825 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5826#endif
5827 vim_iswordc(ptr[-1])))
5828 STRCAT(buf, "\\>");
5829#ifdef FEAT_CMDHIST
5830 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005831 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5833#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005834 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
5836 else
5837 do_cmdline_cmd(buf);
5838
5839 vim_free(buf);
5840}
5841
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842/*
5843 * Get visually selected text, within one line only.
5844 * Returns FAIL if more than one line selected.
5845 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005846 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005847get_visual_text(
5848 cmdarg_T *cap,
5849 char_u **pp, /* return: start of selected text */
5850 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 if (VIsual_mode != 'V')
5853 unadjust_for_sel();
5854 if (VIsual.lnum != curwin->w_cursor.lnum)
5855 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005856 if (cap != NULL)
5857 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 return FAIL;
5859 }
5860 if (VIsual_mode == 'V')
5861 {
5862 *pp = ml_get_curline();
5863 *lenp = (int)STRLEN(*pp);
5864 }
5865 else
5866 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005867 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {
5869 *pp = ml_get_pos(&curwin->w_cursor);
5870 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5871 }
5872 else
5873 {
5874 *pp = ml_get_pos(&VIsual);
5875 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5876 }
5877#ifdef FEAT_MBYTE
5878 if (has_mbyte)
5879 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005880 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881#endif
5882 }
5883 reset_VIsual_and_resel();
5884 return OK;
5885}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
5887/*
5888 * CTRL-T: backwards in tag stack
5889 */
5890 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005891nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892{
5893 if (!checkclearopq(cap->oap))
5894 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5895}
5896
5897/*
5898 * Handle scrolling command 'H', 'L' and 'M'.
5899 */
5900 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005901nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 int used = 0;
5904 long n;
5905#ifdef FEAT_FOLDING
5906 linenr_T lnum;
5907#endif
5908 int half;
5909
5910 cap->oap->motion_type = MLINE;
5911 setpcmark();
5912
5913 if (cap->cmdchar == 'L')
5914 {
5915 validate_botline(); /* make sure curwin->w_botline is valid */
5916 curwin->w_cursor.lnum = curwin->w_botline - 1;
5917 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5918 curwin->w_cursor.lnum = 1;
5919 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005920 {
5921#ifdef FEAT_FOLDING
5922 if (hasAnyFolding(curwin))
5923 {
5924 /* Count a fold for one screen line. */
5925 for (n = cap->count1 - 1; n > 0
5926 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5927 {
5928 (void)hasFolding(curwin->w_cursor.lnum,
5929 &curwin->w_cursor.lnum, NULL);
5930 --curwin->w_cursor.lnum;
5931 }
5932 }
5933 else
5934#endif
5935 curwin->w_cursor.lnum -= cap->count1 - 1;
5936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938 else
5939 {
5940 if (cap->cmdchar == 'M')
5941 {
5942#ifdef FEAT_DIFF
5943 /* Don't count filler lines above the window. */
5944 used -= diff_check_fill(curwin, curwin->w_topline)
5945 - curwin->w_topfill;
5946#endif
5947 validate_botline(); /* make sure w_empty_rows is valid */
5948 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5949 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5950 {
5951#ifdef FEAT_DIFF
5952 /* Count half he number of filler lines to be "below this
5953 * line" and half to be "above the next line". */
5954 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5955 + n) / 2 >= half)
5956 {
5957 --n;
5958 break;
5959 }
5960#endif
5961 used += plines(curwin->w_topline + n);
5962 if (used >= half)
5963 break;
5964#ifdef FEAT_FOLDING
5965 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5966 n = lnum - curwin->w_topline;
5967#endif
5968 }
5969 if (n > 0 && used > curwin->w_height)
5970 --n;
5971 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005972 else /* (cap->cmdchar == 'H') */
5973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005975#ifdef FEAT_FOLDING
5976 if (hasAnyFolding(curwin))
5977 {
5978 /* Count a fold for one screen line. */
5979 lnum = curwin->w_topline;
5980 while (n-- > 0 && lnum < curwin->w_botline - 1)
5981 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005982 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005983 ++lnum;
5984 }
5985 n = lnum - curwin->w_topline;
5986 }
5987#endif
5988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 curwin->w_cursor.lnum = curwin->w_topline + n;
5990 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5991 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5992 }
5993
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005994 /* Correct for 'so', except when an operator is pending. */
5995 if (cap->oap->op_type == OP_NOP)
5996 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 beginline(BL_SOL | BL_FIX);
5998}
5999
6000/*
6001 * Cursor right commands.
6002 */
6003 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006004nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006007 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006009 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6010 {
6011 /* <C-Right> and <S-Right> move a word or WORD right */
6012 if (mod_mask & MOD_MASK_CTRL)
6013 cap->arg = TRUE;
6014 nv_wordcmd(cap);
6015 return;
6016 }
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 cap->oap->motion_type = MCHAR;
6019 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006020 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006022#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006024 * In virtual edit mode, there's no such thing as "past_line", as lines
6025 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 */
6027 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006028 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029#endif
6030
6031 for (n = cap->count1; n > 0; --n)
6032 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006033 if ((!past_line && oneright() == FAIL)
6034 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006035 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 {
6037 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006038 * <Space> wraps to next line if 'whichwrap' has 's'.
6039 * 'l' wraps to next line if 'whichwrap' has 'l'.
6040 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 */
6042 if ( ((cap->cmdchar == ' '
6043 && vim_strchr(p_ww, 's') != NULL)
6044 || (cap->cmdchar == 'l'
6045 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006046 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 && vim_strchr(p_ww, '>') != NULL))
6048 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6049 {
6050 /* When deleting we also count the NL as a character.
6051 * Set cap->oap->inclusive when last char in the line is
6052 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006053 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006055 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 cap->oap->inclusive = TRUE;
6057 else
6058 {
6059 ++curwin->w_cursor.lnum;
6060 curwin->w_cursor.col = 0;
6061#ifdef FEAT_VIRTUALEDIT
6062 curwin->w_cursor.coladd = 0;
6063#endif
6064 curwin->w_set_curswant = TRUE;
6065 cap->oap->inclusive = FALSE;
6066 }
6067 continue;
6068 }
6069 if (cap->oap->op_type == OP_NOP)
6070 {
6071 /* Only beep and flush if not moved at all */
6072 if (n == cap->count1)
6073 beep_flush();
6074 }
6075 else
6076 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006077 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 cap->oap->inclusive = TRUE;
6079 }
6080 break;
6081 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006082 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
6084 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006085#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 if (virtual_active())
6087 oneright();
6088 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006091#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (has_mbyte)
6093 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006094 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 ++curwin->w_cursor.col;
6098 }
6099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 }
6101#ifdef FEAT_FOLDING
6102 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6103 && cap->oap->op_type == OP_NOP)
6104 foldOpenCursor();
6105#endif
6106}
6107
6108/*
6109 * Cursor left commands.
6110 *
6111 * Returns TRUE when operator end should not be adjusted.
6112 */
6113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006114nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
6116 long n;
6117
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006118 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6119 {
6120 /* <C-Left> and <S-Left> move a word or WORD left */
6121 if (mod_mask & MOD_MASK_CTRL)
6122 cap->arg = 1;
6123 nv_bck_word(cap);
6124 return;
6125 }
6126
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 cap->oap->motion_type = MCHAR;
6128 cap->oap->inclusive = FALSE;
6129 for (n = cap->count1; n > 0; --n)
6130 {
6131 if (oneleft() == FAIL)
6132 {
6133 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6134 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6135 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6136 */
6137 if ( (((cap->cmdchar == K_BS
6138 || cap->cmdchar == Ctrl_H)
6139 && vim_strchr(p_ww, 'b') != NULL)
6140 || (cap->cmdchar == 'h'
6141 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006142 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 && vim_strchr(p_ww, '<') != NULL))
6144 && curwin->w_cursor.lnum > 1)
6145 {
6146 --(curwin->w_cursor.lnum);
6147 coladvance((colnr_T)MAXCOL);
6148 curwin->w_set_curswant = TRUE;
6149
6150 /* When the NL before the first char has to be deleted we
6151 * put the cursor on the NUL after the previous line.
6152 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006153 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if ( (cap->oap->op_type == OP_DELETE
6155 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006156 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006158 char_u *cp = ml_get_cursor();
6159
6160 if (*cp != NUL)
6161 {
6162#ifdef FEAT_MBYTE
6163 if (has_mbyte)
6164 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6165 else
6166#endif
6167 ++curwin->w_cursor.col;
6168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 cap->retval |= CA_NO_ADJ_OP_END;
6170 }
6171 continue;
6172 }
6173 /* Only beep and flush if not moved at all */
6174 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6175 beep_flush();
6176 break;
6177 }
6178 }
6179#ifdef FEAT_FOLDING
6180 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6181 && cap->oap->op_type == OP_NOP)
6182 foldOpenCursor();
6183#endif
6184}
6185
6186/*
6187 * Cursor up commands.
6188 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6189 */
6190 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006191nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006193 if (mod_mask & MOD_MASK_SHIFT)
6194 {
6195 /* <S-Up> is page up */
6196 cap->arg = BACKWARD;
6197 nv_page(cap);
6198 }
6199 else
6200 {
6201 cap->oap->motion_type = MLINE;
6202 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6203 clearopbeep(cap->oap);
6204 else if (cap->arg)
6205 beginline(BL_WHITE | BL_FIX);
6206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207}
6208
6209/*
6210 * Cursor down commands.
6211 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6212 */
6213 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006214nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006216 if (mod_mask & MOD_MASK_SHIFT)
6217 {
6218 /* <S-Down> is page down */
6219 cap->arg = FORWARD;
6220 nv_page(cap);
6221 }
6222 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006223#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006225 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar74a47162017-02-26 19:09:05 +01006226 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006227 if (curwin->w_llist_ref == NULL)
6228 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6229 else
6230 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar74a47162017-02-26 19:09:05 +01006231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 else
6233#endif
6234 {
6235#ifdef FEAT_CMDWIN
6236 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006237 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 cmdwin_result = CAR;
6239 else
6240#endif
6241 {
6242 cap->oap->motion_type = MLINE;
6243 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6244 clearopbeep(cap->oap);
6245 else if (cap->arg)
6246 beginline(BL_WHITE | BL_FIX);
6247 }
6248 }
6249}
6250
6251#ifdef FEAT_SEARCHPATH
6252/*
6253 * Grab the file name under the cursor and edit it.
6254 */
6255 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006256nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
6258 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006259 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006261 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006264 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 return;
6266 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006267#ifdef FEAT_AUTOCMD
6268 if (curbuf_locked())
6269 {
6270 clearop(cap->oap);
6271 return;
6272 }
6273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006275 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277 if (ptr != NULL)
6278 {
6279 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006280 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006281 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006283 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006284 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006285 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006286 {
6287 curwin->w_cursor.lnum = lnum;
6288 check_cursor_lnum();
6289 beginline(BL_SOL | BL_FIX);
6290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 vim_free(ptr);
6292 }
6293 else
6294 clearop(cap->oap);
6295}
6296#endif
6297
6298/*
6299 * <End> command: to end of current line or last line.
6300 */
6301 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006302nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006304 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006306 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 nv_goto(cap);
6308 cap->count1 = 1; /* to end of current line */
6309 }
6310 nv_dollar(cap);
6311}
6312
6313/*
6314 * Handle the "$" command.
6315 */
6316 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006317nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
6319 cap->oap->motion_type = MCHAR;
6320 cap->oap->inclusive = TRUE;
6321#ifdef FEAT_VIRTUALEDIT
6322 /* In virtual mode when off the edge of a line and an operator
6323 * is pending (whew!) keep the cursor where it is.
6324 * Otherwise, send it to the end of the line. */
6325 if (!virtual_active() || gchar_cursor() != NUL
6326 || cap->oap->op_type == OP_NOP)
6327#endif
6328 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6329 if (cursor_down((long)(cap->count1 - 1),
6330 cap->oap->op_type == OP_NOP) == FAIL)
6331 clearopbeep(cap->oap);
6332#ifdef FEAT_FOLDING
6333 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6334 foldOpenCursor();
6335#endif
6336}
6337
6338/*
6339 * Implementation of '?' and '/' commands.
6340 * If cap->arg is TRUE don't set PC mark.
6341 */
6342 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006343nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006346 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
6348 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6349 {
6350 /* Translate "g??" to "g?g?" */
6351 cap->cmdchar = 'g';
6352 cap->nchar = '?';
6353 nv_operator(cap);
6354 return;
6355 }
6356
Bram Moolenaardda933d2016-09-03 21:04:58 +02006357 /* When using 'incsearch' the cursor may be moved to set a different search
6358 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6360
6361 if (cap->searchbuf == NULL)
6362 {
6363 clearop(oap);
6364 return;
6365 }
6366
Bram Moolenaar46539112015-02-17 15:43:57 +01006367 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006368 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006369 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370}
6371
6372/*
6373 * Handle "N" and "n" commands.
6374 * cap->arg is SEARCH_REV for "N", 0 for "n".
6375 */
6376 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006377nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
Bram Moolenaar46539112015-02-17 15:43:57 +01006379 pos_T old = curwin->w_cursor;
6380 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6381
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006382 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006383 {
6384 /* Avoid getting stuck on the current cursor position, which can
6385 * happen when an offset is given and the cursor is on the last char
6386 * in the buffer: Repeat with count + 1. */
6387 cap->count1 += 1;
6388 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6389 cap->count1 -= 1;
6390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391}
6392
6393/*
6394 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6395 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006396 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006398 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006399normal_search(
6400 cmdarg_T *cap,
6401 int dir,
6402 char_u *pat,
6403 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 int i;
6406
6407 cap->oap->motion_type = MCHAR;
6408 cap->oap->inclusive = FALSE;
6409 cap->oap->use_reg_one = TRUE;
6410 curwin->w_set_curswant = TRUE;
6411
6412 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006413 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (i == 0)
6415 clearop(cap->oap);
6416 else
6417 {
6418 if (i == 2)
6419 cap->oap->motion_type = MLINE;
6420#ifdef FEAT_VIRTUALEDIT
6421 curwin->w_cursor.coladd = 0;
6422#endif
6423#ifdef FEAT_FOLDING
6424 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6425 foldOpenCursor();
6426#endif
6427 }
6428
6429 /* "/$" will put the cursor after the end of the line, may need to
6430 * correct that here */
6431 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006432 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433}
6434
6435/*
6436 * Character search commands.
6437 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6438 * ',' and FALSE for ';'.
6439 * cap->nchar is NUL for ',' and ';' (repeat the search)
6440 */
6441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006442nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
6444 int t_cmd;
6445
6446 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6447 t_cmd = TRUE;
6448 else
6449 t_cmd = FALSE;
6450
6451 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6453 clearopbeep(cap->oap);
6454 else
6455 {
6456 curwin->w_set_curswant = TRUE;
6457#ifdef FEAT_VIRTUALEDIT
6458 /* Include a Tab for "tx" and for "dfx". */
6459 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6460 && (t_cmd || cap->oap->op_type != OP_NOP))
6461 {
6462 colnr_T scol, ecol;
6463
6464 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6465 curwin->w_cursor.coladd = ecol - scol;
6466 }
6467 else
6468 curwin->w_cursor.coladd = 0;
6469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471#ifdef FEAT_FOLDING
6472 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6473 foldOpenCursor();
6474#endif
6475 }
6476}
6477
6478/*
6479 * "[" and "]" commands.
6480 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6481 */
6482 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006483nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006485 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 pos_T prev_pos;
6487 pos_T *pos = NULL; /* init for GCC */
6488 pos_T old_pos; /* cursor position before command */
6489 int flag;
6490 long n;
6491 int findc;
6492 int c;
6493
6494 cap->oap->motion_type = MCHAR;
6495 cap->oap->inclusive = FALSE;
6496 old_pos = curwin->w_cursor;
6497#ifdef FEAT_VIRTUALEDIT
6498 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6499#endif
6500
6501#ifdef FEAT_SEARCHPATH
6502 /*
6503 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6504 */
6505 if (cap->nchar == 'f')
6506 nv_gotofile(cap);
6507 else
6508#endif
6509
6510#ifdef FEAT_FIND_ID
6511 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006512 * Find the occurrence(s) of the identifier or define under cursor
6513 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 *
6515 * search list jump
6516 * fwd bwd fwd bwd fwd bwd
6517 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6518 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6519 */
6520 if (vim_strchr((char_u *)
6521#ifdef EBCDIC
6522 "iI\005dD\067",
6523#else
6524 "iI\011dD\004",
6525#endif
6526 cap->nchar) != NULL)
6527 {
6528 char_u *ptr;
6529 int len;
6530
6531 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6532 clearop(cap->oap);
6533 else
6534 {
6535 find_pattern_in_path(ptr, 0, len, TRUE,
6536 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6537 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6538 cap->count1,
6539 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6540 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6541 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6542 (linenr_T)MAXLNUM);
6543 curwin->w_set_curswant = TRUE;
6544 }
6545 }
6546 else
6547#endif
6548
6549 /*
6550 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6551 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6552 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6553 * "[m" or "]m" search for prev/next start of (Java) method.
6554 * "[M" or "]M" search for prev/next end of (Java) method.
6555 */
6556 if ( (cap->cmdchar == '['
6557 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6558 || (cap->cmdchar == ']'
6559 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6560 {
6561 if (cap->nchar == '*')
6562 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 prev_pos.lnum = 0;
6564 if (cap->nchar == 'm' || cap->nchar == 'M')
6565 {
6566 if (cap->cmdchar == '[')
6567 findc = '{';
6568 else
6569 findc = '}';
6570 n = 9999;
6571 }
6572 else
6573 {
6574 findc = cap->nchar;
6575 n = cap->count1;
6576 }
6577 for ( ; n > 0; --n)
6578 {
6579 if ((pos = findmatchlimit(cap->oap, findc,
6580 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6581 {
6582 if (new_pos.lnum == 0) /* nothing found */
6583 {
6584 if (cap->nchar != 'm' && cap->nchar != 'M')
6585 clearopbeep(cap->oap);
6586 }
6587 else
6588 pos = &new_pos; /* use last one found */
6589 break;
6590 }
6591 prev_pos = new_pos;
6592 curwin->w_cursor = *pos;
6593 new_pos = *pos;
6594 }
6595 curwin->w_cursor = old_pos;
6596
6597 /*
6598 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6599 * brought us to the match for "[m" and "]M" when inside a method.
6600 * Try finding the '{' or '}' we want to be at.
6601 * Also repeat for the given count.
6602 */
6603 if (cap->nchar == 'm' || cap->nchar == 'M')
6604 {
6605 /* norm is TRUE for "]M" and "[m" */
6606 int norm = ((findc == '{') == (cap->nchar == 'm'));
6607
6608 n = cap->count1;
6609 /* found a match: we were inside a method */
6610 if (prev_pos.lnum != 0)
6611 {
6612 pos = &prev_pos;
6613 curwin->w_cursor = prev_pos;
6614 if (norm)
6615 --n;
6616 }
6617 else
6618 pos = NULL;
6619 while (n > 0)
6620 {
6621 for (;;)
6622 {
6623 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6624 {
6625 /* if not found anything, that's an error */
6626 if (pos == NULL)
6627 clearopbeep(cap->oap);
6628 n = 0;
6629 break;
6630 }
6631 c = gchar_cursor();
6632 if (c == '{' || c == '}')
6633 {
6634 /* Must have found end/start of class: use it.
6635 * Or found the place to be at. */
6636 if ((c == findc && norm) || (n == 1 && !norm))
6637 {
6638 new_pos = curwin->w_cursor;
6639 pos = &new_pos;
6640 n = 0;
6641 }
6642 /* if no match found at all, we started outside of the
6643 * class and we're inside now. Just go on. */
6644 else if (new_pos.lnum == 0)
6645 {
6646 new_pos = curwin->w_cursor;
6647 pos = &new_pos;
6648 }
6649 /* found start/end of other method: go to match */
6650 else if ((pos = findmatchlimit(cap->oap, findc,
6651 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6652 0)) == NULL)
6653 n = 0;
6654 else
6655 curwin->w_cursor = *pos;
6656 break;
6657 }
6658 }
6659 --n;
6660 }
6661 curwin->w_cursor = old_pos;
6662 if (pos == NULL && new_pos.lnum != 0)
6663 clearopbeep(cap->oap);
6664 }
6665 if (pos != NULL)
6666 {
6667 setpcmark();
6668 curwin->w_cursor = *pos;
6669 curwin->w_set_curswant = TRUE;
6670#ifdef FEAT_FOLDING
6671 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6672 && cap->oap->op_type == OP_NOP)
6673 foldOpenCursor();
6674#endif
6675 }
6676 }
6677
6678 /*
6679 * "[[", "[]", "]]" and "][": move to start or end of function
6680 */
6681 else if (cap->nchar == '[' || cap->nchar == ']')
6682 {
6683 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6684 flag = '{';
6685 else
6686 flag = '}'; /* "][" or "[]" */
6687
6688 curwin->w_set_curswant = TRUE;
6689 /*
6690 * Imitate strange Vi behaviour: When using "]]" with an operator
6691 * we also stop at '}'.
6692 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006693 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 (cap->oap->op_type != OP_NOP
6695 && cap->arg == FORWARD && flag == '{')))
6696 clearopbeep(cap->oap);
6697 else
6698 {
6699 if (cap->oap->op_type == OP_NOP)
6700 beginline(BL_WHITE | BL_FIX);
6701#ifdef FEAT_FOLDING
6702 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6703 foldOpenCursor();
6704#endif
6705 }
6706 }
6707
6708 /*
6709 * "[p", "[P", "]P" and "]p": put with indent adjustment
6710 */
6711 else if (cap->nchar == 'p' || cap->nchar == 'P')
6712 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006713 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006715 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6716 ? FORWARD : BACKWARD;
6717 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006718 int was_visual = VIsual_active;
6719 int line_count = curbuf->b_ml.ml_line_count;
6720 pos_T start, end;
6721
6722 if (VIsual_active)
6723 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006724 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006725 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006726 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006727 curwin->w_cursor = (dir == BACKWARD ? start : end);
6728 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006729# ifdef FEAT_CLIPBOARD
6730 adjust_clip_reg(&regname);
6731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006733
6734 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006735 if (was_visual)
6736 {
6737 VIsual = start;
6738 curwin->w_cursor = end;
6739 if (dir == BACKWARD)
6740 {
6741 /* adjust lines */
6742 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6743 curwin->w_cursor.lnum +=
6744 curbuf->b_ml.ml_line_count - line_count;
6745 }
6746
6747 VIsual_active = TRUE;
6748 if (VIsual_mode == 'V')
6749 {
6750 /* delete visually selected lines */
6751 cap->cmdchar = 'd';
6752 cap->nchar = NUL;
6753 cap->oap->regname = regname;
6754 nv_operator(cap);
6755 do_pending_operator(cap, 0, FALSE);
6756 }
6757 if (VIsual_active)
6758 {
6759 end_visual_mode();
6760 redraw_later(SOME_VALID);
6761 }
6762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 }
6764 }
6765
6766 /*
6767 * "['", "[`", "]'" and "]`": jump to next mark
6768 */
6769 else if (cap->nchar == '\'' || cap->nchar == '`')
6770 {
6771 pos = &curwin->w_cursor;
6772 for (n = cap->count1; n > 0; --n)
6773 {
6774 prev_pos = *pos;
6775 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6776 cap->nchar == '\'');
6777 if (pos == NULL)
6778 break;
6779 }
6780 if (pos == NULL)
6781 pos = &prev_pos;
6782 nv_cursormark(cap, cap->nchar == '\'', pos);
6783 }
6784
6785#ifdef FEAT_MOUSE
6786 /*
6787 * [ or ] followed by a middle mouse click: put selected text with
6788 * indent adjustment. Any other button just does as usual.
6789 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006790 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 (void)do_mouse(cap->oap, cap->nchar,
6793 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6794 cap->count1, PUT_FIXINDENT);
6795 }
6796#endif /* FEAT_MOUSE */
6797
6798#ifdef FEAT_FOLDING
6799 /*
6800 * "[z" and "]z": move to start or end of open fold.
6801 */
6802 else if (cap->nchar == 'z')
6803 {
6804 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6805 cap->count1) == FAIL)
6806 clearopbeep(cap->oap);
6807 }
6808#endif
6809
6810#ifdef FEAT_DIFF
6811 /*
6812 * "[c" and "]c": move to next or previous diff-change.
6813 */
6814 else if (cap->nchar == 'c')
6815 {
6816 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6817 cap->count1) == FAIL)
6818 clearopbeep(cap->oap);
6819 }
6820#endif
6821
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006822#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006823 /*
6824 * "[s", "[S", "]s" and "]S": move to next spell error.
6825 */
6826 else if (cap->nchar == 's' || cap->nchar == 'S')
6827 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006828 setpcmark();
6829 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006830 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6831 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006832 {
6833 clearopbeep(cap->oap);
6834 break;
6835 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006836 else
6837 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006838# ifdef FEAT_FOLDING
6839 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6840 foldOpenCursor();
6841# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006842 }
6843#endif
6844
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 /* Not a valid cap->nchar. */
6846 else
6847 clearopbeep(cap->oap);
6848}
6849
6850/*
6851 * Handle Normal mode "%" command.
6852 */
6853 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006854nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855{
6856 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006857#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 linenr_T lnum = curwin->w_cursor.lnum;
6859#endif
6860
6861 cap->oap->inclusive = TRUE;
6862 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6863 {
6864 if (cap->count0 > 100)
6865 clearopbeep(cap->oap);
6866 else
6867 {
6868 cap->oap->motion_type = MLINE;
6869 setpcmark();
6870 /* Round up, so CTRL-G will give same value. Watch out for a
6871 * large line count, the line number must not go negative! */
6872 if (curbuf->b_ml.ml_line_count > 1000000)
6873 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6874 / 100L * cap->count0;
6875 else
6876 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6877 cap->count0 + 99L) / 100L;
6878 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6879 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6880 beginline(BL_SOL | BL_FIX);
6881 }
6882 }
6883 else /* "%" : go to matching paren */
6884 {
6885 cap->oap->motion_type = MCHAR;
6886 cap->oap->use_reg_one = TRUE;
6887 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6888 clearopbeep(cap->oap);
6889 else
6890 {
6891 setpcmark();
6892 curwin->w_cursor = *pos;
6893 curwin->w_set_curswant = TRUE;
6894#ifdef FEAT_VIRTUALEDIT
6895 curwin->w_cursor.coladd = 0;
6896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 }
6899 }
6900#ifdef FEAT_FOLDING
6901 if (cap->oap->op_type == OP_NOP
6902 && lnum != curwin->w_cursor.lnum
6903 && (fdo_flags & FDO_PERCENT)
6904 && KeyTyped)
6905 foldOpenCursor();
6906#endif
6907}
6908
6909/*
6910 * Handle "(" and ")" commands.
6911 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6912 */
6913 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006914nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
6916 cap->oap->motion_type = MCHAR;
6917 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006918 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6919 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 curwin->w_set_curswant = TRUE;
6921
6922 if (findsent(cap->arg, cap->count1) == FAIL)
6923 clearopbeep(cap->oap);
6924 else
6925 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006926 /* Don't leave the cursor on the NUL past end of line. */
6927 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928#ifdef FEAT_VIRTUALEDIT
6929 curwin->w_cursor.coladd = 0;
6930#endif
6931#ifdef FEAT_FOLDING
6932 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6933 foldOpenCursor();
6934#endif
6935 }
6936}
6937
6938/*
6939 * "m" command: Mark a position.
6940 */
6941 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006942nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
6944 if (!checkclearop(cap->oap))
6945 {
6946 if (setmark(cap->nchar) == FAIL)
6947 clearopbeep(cap->oap);
6948 }
6949}
6950
6951/*
6952 * "{" and "}" commands.
6953 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6954 */
6955 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006956nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957{
6958 cap->oap->motion_type = MCHAR;
6959 cap->oap->inclusive = FALSE;
6960 cap->oap->use_reg_one = TRUE;
6961 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006962 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 clearopbeep(cap->oap);
6964 else
6965 {
6966#ifdef FEAT_VIRTUALEDIT
6967 curwin->w_cursor.coladd = 0;
6968#endif
6969#ifdef FEAT_FOLDING
6970 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6971 foldOpenCursor();
6972#endif
6973 }
6974}
6975
6976/*
6977 * "u" command: Undo or make lower case.
6978 */
6979 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006980nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006982 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
6984 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6985 cap->cmdchar = 'g';
6986 cap->nchar = 'u';
6987 nv_operator(cap);
6988 }
6989 else
6990 nv_kundo(cap);
6991}
6992
6993/*
6994 * <Undo> command.
6995 */
6996 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006997nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 if (!checkclearopq(cap->oap))
7000 {
7001 u_undo((int)cap->count1);
7002 curwin->w_set_curswant = TRUE;
7003 }
7004}
7005
7006/*
7007 * Handle the "r" command.
7008 */
7009 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007010nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
7012 char_u *ptr;
7013 int had_ctrl_v;
7014 long n;
7015
7016 if (checkclearop(cap->oap))
7017 return;
7018
7019 /* get another character */
7020 if (cap->nchar == Ctrl_V)
7021 {
7022 had_ctrl_v = Ctrl_V;
7023 cap->nchar = get_literal();
7024 /* Don't redo a multibyte character with CTRL-V. */
7025 if (cap->nchar > DEL)
7026 had_ctrl_v = NUL;
7027 }
7028 else
7029 had_ctrl_v = NUL;
7030
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007031 /* Abort if the character is a special key. */
7032 if (IS_SPECIAL(cap->nchar))
7033 {
7034 clearopbeep(cap->oap);
7035 return;
7036 }
7037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 /* Visual mode "r" */
7039 if (VIsual_active)
7040 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007041 if (got_int)
7042 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007043 if (had_ctrl_v)
7044 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01007045 /* Use a special (negative) number to make a difference between a
7046 * literal CR or NL and a line break. */
7047 if (cap->nchar == CAR)
7048 cap->nchar = REPLACE_CR_NCHAR;
7049 else if (cap->nchar == NL)
7050 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01007051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 nv_operator(cap);
7053 return;
7054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
7056#ifdef FEAT_VIRTUALEDIT
7057 /* Break tabs, etc. */
7058 if (virtual_active())
7059 {
7060 if (u_save_cursor() == FAIL)
7061 return;
7062 if (gchar_cursor() == NUL)
7063 {
7064 /* Add extra space and put the cursor on the first one. */
7065 coladvance_force((colnr_T)(getviscol() + cap->count1));
7066 curwin->w_cursor.col -= cap->count1;
7067 }
7068 else if (gchar_cursor() == TAB)
7069 coladvance_force(getviscol());
7070 }
7071#endif
7072
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007073 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007075 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076#ifdef FEAT_MBYTE
7077 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7078#endif
7079 )
7080 {
7081 clearopbeep(cap->oap);
7082 return;
7083 }
7084
7085 /*
7086 * Replacing with a TAB is done by edit() when it is complicated because
7087 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7088 * Other characters are done below to avoid problems with things like
7089 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7090 */
7091 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7092 {
7093 stuffnumReadbuff(cap->count1);
7094 stuffcharReadbuff('R');
7095 stuffcharReadbuff('\t');
7096 stuffcharReadbuff(ESC);
7097 return;
7098 }
7099
7100 /* save line for undo */
7101 if (u_save_cursor() == FAIL)
7102 return;
7103
7104 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7105 {
7106 /*
7107 * Replace character(s) by a single newline.
7108 * Strange vi behaviour: Only one newline is inserted.
7109 * Delete the characters here.
7110 * Insert the newline with an insert command, takes care of
7111 * autoindent. The insert command depends on being on the last
7112 * character of a line or not.
7113 */
7114#ifdef FEAT_MBYTE
7115 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7116#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007117 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#endif
7119 stuffcharReadbuff('\r');
7120 stuffcharReadbuff(ESC);
7121
7122 /* Give 'r' to edit(), to get the redo command right. */
7123 invoke_edit(cap, TRUE, 'r', FALSE);
7124 }
7125 else
7126 {
7127 prep_redo(cap->oap->regname, cap->count1,
7128 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7129
7130 curbuf->b_op_start = curwin->w_cursor;
7131#ifdef FEAT_MBYTE
7132 if (has_mbyte)
7133 {
7134 int old_State = State;
7135
7136 if (cap->ncharC1 != 0)
7137 AppendCharToRedobuff(cap->ncharC1);
7138 if (cap->ncharC2 != 0)
7139 AppendCharToRedobuff(cap->ncharC2);
7140
7141 /* This is slow, but it handles replacing a single-byte with a
7142 * multi-byte and the other way around. Also handles adding
7143 * composing characters for utf-8. */
7144 for (n = cap->count1; n > 0; --n)
7145 {
7146 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007147 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7148 {
7149 int c = ins_copychar(curwin->w_cursor.lnum
7150 + (cap->nchar == Ctrl_Y ? -1 : 1));
7151 if (c != NUL)
7152 ins_char(c);
7153 else
7154 /* will be decremented further down */
7155 ++curwin->w_cursor.col;
7156 }
7157 else
7158 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 State = old_State;
7160 if (cap->ncharC1 != 0)
7161 ins_char(cap->ncharC1);
7162 if (cap->ncharC2 != 0)
7163 ins_char(cap->ncharC2);
7164 }
7165 }
7166 else
7167#endif
7168 {
7169 /*
7170 * Replace the characters within one line.
7171 */
7172 for (n = cap->count1; n > 0; --n)
7173 {
7174 /*
7175 * Get ptr again, because u_save and/or showmatch() will have
7176 * released the line. At the same time we let know that the
7177 * line will be changed.
7178 */
7179 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007180 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7181 {
7182 int c = ins_copychar(curwin->w_cursor.lnum
7183 + (cap->nchar == Ctrl_Y ? -1 : 1));
7184 if (c != NUL)
7185 ptr[curwin->w_cursor.col] = c;
7186 }
7187 else
7188 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (p_sm && msg_silent == 0)
7190 showmatch(cap->nchar);
7191 ++curwin->w_cursor.col;
7192 }
7193#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007194 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007196 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
Bram Moolenaar009b2592004-10-24 19:18:58 +00007198 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007199 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007201 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 }
7203#endif
7204
7205 /* mark the buffer as changed and prepare for displaying */
7206 changed_bytes(curwin->w_cursor.lnum,
7207 (colnr_T)(curwin->w_cursor.col - cap->count1));
7208 }
7209 --curwin->w_cursor.col; /* cursor on the last replaced char */
7210#ifdef FEAT_MBYTE
7211 /* if the character on the left of the current cursor is a multi-byte
7212 * character, move two characters left */
7213 if (has_mbyte)
7214 mb_adjust_cursor();
7215#endif
7216 curbuf->b_op_end = curwin->w_cursor;
7217 curwin->w_set_curswant = TRUE;
7218 set_last_insert(cap->nchar);
7219 }
7220}
7221
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222/*
7223 * 'o': Exchange start and end of Visual area.
7224 * 'O': same, but in block mode exchange left and right corners.
7225 */
7226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007227v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
7229 pos_T old_cursor;
7230 colnr_T left, right;
7231
7232 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7233 {
7234 old_cursor = curwin->w_cursor;
7235 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7236 curwin->w_cursor.lnum = VIsual.lnum;
7237 coladvance(left);
7238 VIsual = curwin->w_cursor;
7239
7240 curwin->w_cursor.lnum = old_cursor.lnum;
7241 curwin->w_curswant = right;
7242 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7243 * right one column */
7244 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7245 ++curwin->w_curswant;
7246 coladvance(curwin->w_curswant);
7247 if (curwin->w_cursor.col == old_cursor.col
7248#ifdef FEAT_VIRTUALEDIT
7249 && (!virtual_active()
7250 || curwin->w_cursor.coladd == old_cursor.coladd)
7251#endif
7252 )
7253 {
7254 curwin->w_cursor.lnum = VIsual.lnum;
7255 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7256 ++right;
7257 coladvance(right);
7258 VIsual = curwin->w_cursor;
7259
7260 curwin->w_cursor.lnum = old_cursor.lnum;
7261 coladvance(left);
7262 curwin->w_curswant = left;
7263 }
7264 }
7265 else
7266 {
7267 old_cursor = curwin->w_cursor;
7268 curwin->w_cursor = VIsual;
7269 VIsual = old_cursor;
7270 curwin->w_set_curswant = TRUE;
7271 }
7272}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
7274/*
7275 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7276 */
7277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007278nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 if (VIsual_active) /* "R" is replace lines */
7281 {
7282 cap->cmdchar = 'c';
7283 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007284 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 VIsual_mode = 'V';
7286 nv_operator(cap);
7287 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007288 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
7290 if (!curbuf->b_p_ma)
7291 EMSG(_(e_modifiable));
7292 else
7293 {
7294#ifdef FEAT_VIRTUALEDIT
7295 if (virtual_active())
7296 coladvance(getviscol());
7297#endif
7298 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7299 }
7300 }
7301}
7302
7303#ifdef FEAT_VREPLACE
7304/*
7305 * "gr".
7306 */
7307 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007308nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 if (VIsual_active)
7311 {
7312 cap->cmdchar = 'r';
7313 cap->nchar = cap->extra_char;
7314 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7315 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007316 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 {
7318 if (!curbuf->b_p_ma)
7319 EMSG(_(e_modifiable));
7320 else
7321 {
7322 if (cap->extra_char == Ctrl_V) /* get another character */
7323 cap->extra_char = get_literal();
7324 stuffcharReadbuff(cap->extra_char);
7325 stuffcharReadbuff(ESC);
7326# ifdef FEAT_VIRTUALEDIT
7327 if (virtual_active())
7328 coladvance(getviscol());
7329# endif
7330 invoke_edit(cap, TRUE, 'v', FALSE);
7331 }
7332 }
7333}
7334#endif
7335
7336/*
7337 * Swap case for "~" command, when it does not work like an operator.
7338 */
7339 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007340n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
7342 long n;
7343 pos_T startpos;
7344 int did_change = 0;
7345#ifdef FEAT_NETBEANS_INTG
7346 pos_T pos;
7347 char_u *ptr;
7348 int count;
7349#endif
7350
7351 if (checkclearopq(cap->oap))
7352 return;
7353
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007354 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 {
7356 clearopbeep(cap->oap);
7357 return;
7358 }
7359
7360 prep_redo_cmd(cap);
7361
7362 if (u_save_cursor() == FAIL)
7363 return;
7364
7365 startpos = curwin->w_cursor;
7366#ifdef FEAT_NETBEANS_INTG
7367 pos = startpos;
7368#endif
7369 for (n = cap->count1; n > 0; --n)
7370 {
7371 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7372 inc_cursor();
7373 if (gchar_cursor() == NUL)
7374 {
7375 if (vim_strchr(p_ww, '~') != NULL
7376 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7377 {
7378#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007379 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 {
7381 if (did_change)
7382 {
7383 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007384 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007385 netbeans_removed(curbuf, pos.lnum, pos.col,
7386 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007388 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 }
7390 pos.col = 0;
7391 pos.lnum++;
7392 }
7393#endif
7394 ++curwin->w_cursor.lnum;
7395 curwin->w_cursor.col = 0;
7396 if (n > 1)
7397 {
7398 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7399 break;
7400 u_clearline();
7401 }
7402 }
7403 else
7404 break;
7405 }
7406 }
7407#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007408 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 {
7410 ptr = ml_get(pos.lnum);
7411 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007412 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7413 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 }
7415#endif
7416
7417
7418 check_cursor();
7419 curwin->w_set_curswant = TRUE;
7420 if (did_change)
7421 {
7422 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7423 0L);
7424 curbuf->b_op_start = startpos;
7425 curbuf->b_op_end = curwin->w_cursor;
7426 if (curbuf->b_op_end.col > 0)
7427 --curbuf->b_op_end.col;
7428 }
7429}
7430
7431/*
7432 * Move cursor to mark.
7433 */
7434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007435nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436{
7437 if (check_mark(pos) == FAIL)
7438 clearop(cap->oap);
7439 else
7440 {
7441 if (cap->cmdchar == '\''
7442 || cap->cmdchar == '`'
7443 || cap->cmdchar == '['
7444 || cap->cmdchar == ']')
7445 setpcmark();
7446 curwin->w_cursor = *pos;
7447 if (flag)
7448 beginline(BL_WHITE | BL_FIX);
7449 else
7450 check_cursor();
7451 }
7452 cap->oap->motion_type = flag ? MLINE : MCHAR;
7453 if (cap->cmdchar == '`')
7454 cap->oap->use_reg_one = TRUE;
7455 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7456 curwin->w_set_curswant = TRUE;
7457}
7458
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459/*
7460 * Handle commands that are operators in Visual mode.
7461 */
7462 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007463v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
7465 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7466
7467 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007468 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 if (isupper(cap->cmdchar))
7470 {
7471 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007472 {
7473 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7477 curwin->w_curswant = MAXCOL;
7478 }
7479 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7480 nv_operator(cap);
7481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482
7483/*
7484 * "s" and "S" commands.
7485 */
7486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007487nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007489#ifdef FEAT_TERMINAL
7490 /* When showing output of term_dumpdiff() swap the top and botom. */
7491 if (term_swap_diff() == OK)
7492 return;
7493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7495 {
7496 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007497 {
7498 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 cap->cmdchar = 'c';
7502 nv_operator(cap);
7503 }
7504 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 nv_optrans(cap);
7506}
7507
7508/*
7509 * Abbreviated commands.
7510 */
7511 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007512nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513{
7514 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7515 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7516
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 /* in Visual mode these commands are operators */
7518 if (VIsual_active)
7519 v_visop(cap);
7520 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 nv_optrans(cap);
7522}
7523
7524/*
7525 * Translate a command into another command.
7526 */
7527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007528nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7531 (char_u *)"d$", (char_u *)"c$",
7532 (char_u *)"cl", (char_u *)"cc",
7533 (char_u *)"yy", (char_u *)":s\r"};
7534 static char_u *str = (char_u *)"xXDCsSY&";
7535
7536 if (!checkclearopq(cap->oap))
7537 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007538 /* In Vi "2D" doesn't delete the next line. Can't translate it
7539 * either, because "2." should also not use the count. */
7540 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7541 {
7542 cap->oap->start = curwin->w_cursor;
7543 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007544#ifdef FEAT_EVAL
7545 set_op_var(OP_DELETE);
7546#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007547 cap->count1 = 1;
7548 nv_dollar(cap);
7549 finish_op = TRUE;
7550 ResetRedobuff();
7551 AppendCharToRedobuff('D');
7552 }
7553 else
7554 {
7555 if (cap->count0)
7556 stuffnumReadbuff(cap->count0);
7557 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 }
7560 cap->opcount = 0;
7561}
7562
7563/*
7564 * "'" and "`" commands. Also for "g'" and "g`".
7565 * cap->arg is TRUE for "'" and "g'".
7566 */
7567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007568nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570 pos_T *pos;
7571 int c;
7572#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007573 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7575#endif
7576
7577 if (cap->cmdchar == 'g')
7578 c = cap->extra_char;
7579 else
7580 c = cap->nchar;
7581 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7582 if (pos == (pos_T *)-1) /* jumped to other file */
7583 {
7584 if (cap->arg)
7585 {
7586 check_cursor_lnum();
7587 beginline(BL_WHITE | BL_FIX);
7588 }
7589 else
7590 check_cursor();
7591 }
7592 else
7593 nv_cursormark(cap, cap->arg, pos);
7594
7595#ifdef FEAT_VIRTUALEDIT
7596 /* May need to clear the coladd that a mark includes. */
7597 if (!virtual_active())
7598 curwin->w_cursor.coladd = 0;
7599#endif
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007600 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601#ifdef FEAT_FOLDING
7602 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007603 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007604 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 && (fdo_flags & FDO_MARK)
7606 && old_KeyTyped)
7607 foldOpenCursor();
7608#endif
7609}
7610
7611/*
7612 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7613 */
7614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007615nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
7617#ifdef FEAT_JUMPLIST
7618 pos_T *pos;
7619# ifdef FEAT_FOLDING
7620 linenr_T lnum = curwin->w_cursor.lnum;
7621 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7622# endif
7623
7624 if (!checkclearopq(cap->oap))
7625 {
7626 if (cap->cmdchar == 'g')
7627 pos = movechangelist((int)cap->count1);
7628 else
7629 pos = movemark((int)cap->count1);
7630 if (pos == (pos_T *)-1) /* jump to other file */
7631 {
7632 curwin->w_set_curswant = TRUE;
7633 check_cursor();
7634 }
7635 else if (pos != NULL) /* can jump */
7636 nv_cursormark(cap, FALSE, pos);
7637 else if (cap->cmdchar == 'g')
7638 {
7639 if (curbuf->b_changelistlen == 0)
7640 EMSG(_("E664: changelist is empty"));
7641 else if (cap->count1 < 0)
7642 EMSG(_("E662: At start of changelist"));
7643 else
7644 EMSG(_("E663: At end of changelist"));
7645 }
7646 else
7647 clearopbeep(cap->oap);
7648# ifdef FEAT_FOLDING
7649 if (cap->oap->op_type == OP_NOP
7650 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7651 && (fdo_flags & FDO_MARK)
7652 && old_KeyTyped)
7653 foldOpenCursor();
7654# endif
7655 }
7656#else
7657 clearopbeep(cap->oap);
7658#endif
7659}
7660
7661/*
7662 * Handle '"' command.
7663 */
7664 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007665nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
7667 if (checkclearop(cap->oap))
7668 return;
7669#ifdef FEAT_EVAL
7670 if (cap->nchar == '=')
7671 cap->nchar = get_expr_register();
7672#endif
7673 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7674 {
7675 cap->oap->regname = cap->nchar;
7676 cap->opcount = cap->count0; /* remember count before '"' */
7677#ifdef FEAT_EVAL
7678 set_reg_var(cap->oap->regname);
7679#endif
7680 }
7681 else
7682 clearopbeep(cap->oap);
7683}
7684
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685/*
7686 * Handle "v", "V" and "CTRL-V" commands.
7687 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7688 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007689 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 */
7691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007692nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007694 if (cap->cmdchar == Ctrl_Q)
7695 cap->cmdchar = Ctrl_V;
7696
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7698 * characterwise, linewise, or blockwise. */
7699 if (cap->oap->op_type != OP_NOP)
7700 {
7701 cap->oap->motion_force = cap->cmdchar;
7702 finish_op = FALSE; /* operator doesn't finish now but later */
7703 return;
7704 }
7705
7706 VIsual_select = cap->arg;
7707 if (VIsual_active) /* change Visual mode */
7708 {
7709 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7710 end_visual_mode();
7711 else /* toggle char/block mode */
7712 { /* or char/line mode */
7713 VIsual_mode = cap->cmdchar;
7714 showmode();
7715 }
7716 redraw_curbuf_later(INVERTED); /* update the inversion */
7717 }
7718 else /* start Visual mode */
7719 {
7720 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007721 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007723 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 VIsual = curwin->w_cursor;
7725
7726 VIsual_active = TRUE;
7727 VIsual_reselect = TRUE;
7728 if (!cap->arg)
7729 /* start Select mode when 'selectmode' contains "cmd" */
7730 may_start_select('c');
7731#ifdef FEAT_MOUSE
7732 setmouse();
7733#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007734 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 redraw_cmdline = TRUE; /* show visual mode later */
7736 /*
7737 * For V and ^V, we multiply the number of lines even if there
7738 * was only one -- webb
7739 */
7740 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7741 {
7742 curwin->w_cursor.lnum +=
7743 resel_VIsual_line_count * cap->count0 - 1;
7744 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7745 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7746 }
7747 VIsual_mode = resel_VIsual_mode;
7748 if (VIsual_mode == 'v')
7749 {
7750 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007751 {
7752 validate_virtcol();
7753 curwin->w_curswant = curwin->w_virtcol
7754 + resel_VIsual_vcol * cap->count0 - 1;
7755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007757 curwin->w_curswant = resel_VIsual_vcol;
7758 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007760 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {
7762 curwin->w_curswant = MAXCOL;
7763 coladvance((colnr_T)MAXCOL);
7764 }
7765 else if (VIsual_mode == Ctrl_V)
7766 {
7767 validate_virtcol();
7768 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007769 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 coladvance(curwin->w_curswant);
7771 }
7772 else
7773 curwin->w_set_curswant = TRUE;
7774 redraw_curbuf_later(INVERTED); /* show the inversion */
7775 }
7776 else
7777 {
7778 if (!cap->arg)
7779 /* start Select mode when 'selectmode' contains "cmd" */
7780 may_start_select('c');
7781 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007782 if (VIsual_mode != 'V' && *p_sel == 'e')
7783 ++cap->count1; /* include one more char */
7784 if (cap->count0 > 0 && --cap->count1 > 0)
7785 {
7786 /* With a count select that many characters or lines. */
7787 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7788 nv_right(cap);
7789 else if (VIsual_mode == 'V')
7790 nv_down(cap);
7791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793 }
7794}
7795
7796/*
7797 * Start selection for Shift-movement keys.
7798 */
7799 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007800start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802 /* if 'selectmode' contains "key", start Select mode */
7803 may_start_select('k');
7804 n_start_visual_mode('v');
7805}
7806
7807/*
7808 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7809 */
7810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007811may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 VIsual_select = (stuff_empty() && typebuf_typed()
7814 && (vim_strchr(p_slm, c) != NULL));
7815}
7816
7817/*
7818 * Start Visual mode "c".
7819 * Should set VIsual_select before calling this.
7820 */
7821 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007822n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007824#ifdef FEAT_CONCEAL
7825 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007826 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007827#endif
7828
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 VIsual_mode = c;
7830 VIsual_active = TRUE;
7831 VIsual_reselect = TRUE;
7832#ifdef FEAT_VIRTUALEDIT
7833 /* Corner case: the 0 position in a tab may change when going into
7834 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7835 */
7836 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007837 {
7838 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841#endif
7842 VIsual = curwin->w_cursor;
7843
7844#ifdef FEAT_FOLDING
7845 foldAdjustVisual();
7846#endif
7847
7848#ifdef FEAT_MOUSE
7849 setmouse();
7850#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007851#ifdef FEAT_CONCEAL
7852 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007853 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007854#endif
7855
Bram Moolenaar09df3122006-01-23 22:23:09 +00007856 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 redraw_cmdline = TRUE; /* show visual mode later */
7858#ifdef FEAT_CLIPBOARD
7859 /* Make sure the clipboard gets updated. Needed because start and
7860 * end may still be the same, and the selection needs to be owned */
7861 clip_star.vmode = NUL;
7862#endif
7863
7864 /* Only need to redraw this line, unless still need to redraw an old
7865 * Visual area (when 'lazyredraw' is set). */
7866 if (curwin->w_redr_type < INVERTED)
7867 {
7868 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7869 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7870 }
7871}
7872
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873
7874/*
7875 * CTRL-W: Window commands
7876 */
7877 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007878nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007880 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007881 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007882 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007883 cap->cmdchar = ':';
7884 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007885 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007886 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007887 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891/*
7892 * CTRL-Z: Suspend
7893 */
7894 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007895nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (VIsual_active)
7899 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 do_cmdline_cmd((char_u *)"st");
7901}
7902
7903/*
7904 * Commands starting with "g".
7905 */
7906 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007907nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
7909 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 int i;
7912 int flag = FALSE;
7913
7914 switch (cap->nchar)
7915 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007916 case Ctrl_A:
7917 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918#ifdef MEM_PROFILE
7919 /*
7920 * "g^A": dump log of used memory.
7921 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007922 if (!VIsual_active && cap->nchar == Ctrl_A)
7923 vim_mem_profile_dump();
7924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007926 /*
7927 * "g^A/g^X": sequentially increment visually selected region
7928 */
7929 if (VIsual_active)
7930 {
7931 cap->arg = TRUE;
7932 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007933 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007934 nv_addsub(cap);
7935 }
7936 else
7937 clearopbeep(oap);
7938 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939
7940#ifdef FEAT_VREPLACE
7941 /*
7942 * "gR": Enter virtual replace mode.
7943 */
7944 case 'R':
7945 cap->arg = TRUE;
7946 nv_Replace(cap);
7947 break;
7948
7949 case 'r':
7950 nv_vreplace(cap);
7951 break;
7952#endif
7953
7954 case '&':
7955 do_cmdline_cmd((char_u *)"%s//~/&");
7956 break;
7957
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 /*
7959 * "gv": Reselect the previous Visual area. If Visual already active,
7960 * exchange previous and current Visual area.
7961 */
7962 case 'v':
7963 if (checkclearop(oap))
7964 break;
7965
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007966 if ( curbuf->b_visual.vi_start.lnum == 0
7967 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7968 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 beep_flush();
7970 else
7971 {
7972 /* set w_cursor to the start of the Visual area, tpos to the end */
7973 if (VIsual_active)
7974 {
7975 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007976 VIsual_mode = curbuf->b_visual.vi_mode;
7977 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978# ifdef FEAT_EVAL
7979 curbuf->b_visual_mode_eval = i;
7980# endif
7981 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007982 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7983 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007985 tpos = curbuf->b_visual.vi_end;
7986 curbuf->b_visual.vi_end = curwin->w_cursor;
7987 curwin->w_cursor = curbuf->b_visual.vi_start;
7988 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 }
7990 else
7991 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007992 VIsual_mode = curbuf->b_visual.vi_mode;
7993 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7994 tpos = curbuf->b_visual.vi_end;
7995 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 }
7997
7998 VIsual_active = TRUE;
7999 VIsual_reselect = TRUE;
8000
8001 /* Set Visual to the start and w_cursor to the end of the Visual
8002 * area. Make sure they are on an existing character. */
8003 check_cursor();
8004 VIsual = curwin->w_cursor;
8005 curwin->w_cursor = tpos;
8006 check_cursor();
8007 update_topline();
8008 /*
8009 * When called from normal "g" command: start Select mode when
8010 * 'selectmode' contains "cmd". When called for K_SELECT, always
8011 * start Select mode.
8012 */
8013 if (cap->arg)
8014 VIsual_select = TRUE;
8015 else
8016 may_start_select('c');
8017#ifdef FEAT_MOUSE
8018 setmouse();
8019#endif
8020#ifdef FEAT_CLIPBOARD
8021 /* Make sure the clipboard gets updated. Needed because start and
8022 * end are still the same, and the selection needs to be owned */
8023 clip_star.vmode = NUL;
8024#endif
8025 redraw_curbuf_later(INVERTED);
8026 showmode();
8027 }
8028 break;
8029 /*
8030 * "gV": Don't reselect the previous Visual area after a Select mode
8031 * mapping of menu.
8032 */
8033 case 'V':
8034 VIsual_reselect = FALSE;
8035 break;
8036
8037 /*
8038 * "gh": start Select mode.
8039 * "gH": start Select line mode.
8040 * "g^H": start Select block mode.
8041 */
8042 case K_BS:
8043 cap->nchar = Ctrl_H;
8044 /* FALLTHROUGH */
8045 case 'h':
8046 case 'H':
8047 case Ctrl_H:
8048# ifdef EBCDIC
8049 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8050 if (cap->nchar == Ctrl_H)
8051 cap->cmdchar = Ctrl_V;
8052 else
8053# endif
8054 cap->cmdchar = cap->nchar + ('v' - 'h');
8055 cap->arg = TRUE;
8056 nv_visual(cap);
8057 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008058
8059 /* "gn", "gN" visually select next/previous search match
8060 * "gn" selects next match
8061 * "gN" selects previous match
8062 */
8063 case 'N':
8064 case 'n':
8065 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008066 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008067 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068
8069 /*
8070 * "gj" and "gk" two new funny movement keys -- up and down
8071 * movement based on *screen* line rather than *file* line.
8072 */
8073 case 'j':
8074 case K_DOWN:
8075 /* with 'nowrap' it works just like the normal "j" command; also when
8076 * in a closed fold */
8077 if (!curwin->w_p_wrap
8078#ifdef FEAT_FOLDING
8079 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8080#endif
8081 )
8082 {
8083 oap->motion_type = MLINE;
8084 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8085 }
8086 else
8087 i = nv_screengo(oap, FORWARD, cap->count1);
8088 if (i == FAIL)
8089 clearopbeep(oap);
8090 break;
8091
8092 case 'k':
8093 case K_UP:
8094 /* with 'nowrap' it works just like the normal "k" command; also when
8095 * in a closed fold */
8096 if (!curwin->w_p_wrap
8097#ifdef FEAT_FOLDING
8098 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8099#endif
8100 )
8101 {
8102 oap->motion_type = MLINE;
8103 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8104 }
8105 else
8106 i = nv_screengo(oap, BACKWARD, cap->count1);
8107 if (i == FAIL)
8108 clearopbeep(oap);
8109 break;
8110
8111 /*
8112 * "gJ": join two lines without inserting a space.
8113 */
8114 case 'J':
8115 nv_join(cap);
8116 break;
8117
8118 /*
8119 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8120 * "gm": middle of "g0" and "g$".
8121 */
8122 case '^':
8123 flag = TRUE;
8124 /* FALLTHROUGH */
8125
8126 case '0':
8127 case 'm':
8128 case K_HOME:
8129 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 oap->motion_type = MCHAR;
8131 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008132 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008134 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 int width2 = width1 + curwin_col_off2();
8136
8137 validate_virtcol();
8138 i = 0;
8139 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8140 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8141 }
8142 else
8143 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008144 /* Go to the middle of the screen line. When 'number' or
8145 * 'relativenumber' is on and lines are wrapping the middle can be more
8146 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008148 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 + ((curwin->w_p_wrap && i > 0)
8150 ? curwin_col_off2() : 0)) / 2;
8151 coladvance((colnr_T)i);
8152 if (flag)
8153 {
8154 do
8155 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008156 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 }
8158 curwin->w_set_curswant = TRUE;
8159 break;
8160
8161 case '_':
8162 /* "g_": to the last non-blank character in the line or <count> lines
8163 * downward. */
8164 cap->oap->motion_type = MCHAR;
8165 cap->oap->inclusive = TRUE;
8166 curwin->w_curswant = MAXCOL;
8167 if (cursor_down((long)(cap->count1 - 1),
8168 cap->oap->op_type == OP_NOP) == FAIL)
8169 clearopbeep(cap->oap);
8170 else
8171 {
8172 char_u *ptr = ml_get_curline();
8173
8174 /* In Visual mode we may end up after the line. */
8175 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8176 --curwin->w_cursor.col;
8177
8178 /* Decrease the cursor column until it's on a non-blank. */
8179 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008180 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 --curwin->w_cursor.col;
8182 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008183 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185 break;
8186
8187 case '$':
8188 case K_END:
8189 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {
8191 int col_off = curwin_col_off();
8192
8193 oap->motion_type = MCHAR;
8194 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008195 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {
8197 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8198 if (cap->count1 == 1)
8199 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008200 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 int width2 = width1 + curwin_col_off2();
8202
8203 validate_virtcol();
8204 i = width1 - 1;
8205 if (curwin->w_virtcol >= (colnr_T)width1)
8206 i += ((curwin->w_virtcol - width1) / width2 + 1)
8207 * width2;
8208 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008209
8210 /* Make sure we stick in this column. */
8211 validate_virtcol();
8212 curwin->w_curswant = curwin->w_virtcol;
8213 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8215 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8216 {
8217 /*
8218 * Check for landing on a character that got split at
8219 * the end of the line. We do not want to advance to
8220 * the next screen line.
8221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 if (curwin->w_virtcol > (colnr_T)i)
8223 --curwin->w_cursor.col;
8224 }
8225#endif
8226 }
8227 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8228 clearopbeep(oap);
8229 }
8230 else
8231 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008232 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008234
8235 /* Make sure we stick in this column. */
8236 validate_virtcol();
8237 curwin->w_curswant = curwin->w_virtcol;
8238 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 }
8240 }
8241 break;
8242
8243 /*
8244 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8245 */
8246 case '*':
8247 case '#':
8248#if POUND != '#'
8249 case POUND: /* pound sign (sometimes equal to '#') */
8250#endif
8251 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8252 case ']': /* :tselect for current identifier */
8253 nv_ident(cap);
8254 break;
8255
8256 /*
8257 * ge and gE: go back to end of word
8258 */
8259 case 'e':
8260 case 'E':
8261 oap->motion_type = MCHAR;
8262 curwin->w_set_curswant = TRUE;
8263 oap->inclusive = TRUE;
8264 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8265 clearopbeep(oap);
8266 break;
8267
8268 /*
8269 * "g CTRL-G": display info about cursor position
8270 */
8271 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008272 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 break;
8274
8275 /*
8276 * "gi": start Insert at the last position.
8277 */
8278 case 'i':
8279 if (curbuf->b_last_insert.lnum != 0)
8280 {
8281 curwin->w_cursor = curbuf->b_last_insert;
8282 check_cursor_lnum();
8283 i = (int)STRLEN(ml_get_curline());
8284 if (curwin->w_cursor.col > (colnr_T)i)
8285 {
8286#ifdef FEAT_VIRTUALEDIT
8287 if (virtual_active())
8288 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8289#endif
8290 curwin->w_cursor.col = i;
8291 }
8292 }
8293 cap->cmdchar = 'i';
8294 nv_edit(cap);
8295 break;
8296
8297 /*
8298 * "gI": Start insert in column 1.
8299 */
8300 case 'I':
8301 beginline(0);
8302 if (!checkclearopq(oap))
8303 invoke_edit(cap, FALSE, 'g', FALSE);
8304 break;
8305
8306#ifdef FEAT_SEARCHPATH
8307 /*
8308 * "gf": goto file, edit file under cursor
8309 * "]f" and "[f": can also be used.
8310 */
8311 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008312 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 nv_gotofile(cap);
8314 break;
8315#endif
8316
8317 /* "g'm" and "g`m": jump to mark without setting pcmark */
8318 case '\'':
8319 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008320 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 case '`':
8322 nv_gomark(cap);
8323 break;
8324
8325 /*
8326 * "gs": Goto sleep.
8327 */
8328 case 's':
8329 do_sleep(cap->count1 * 1000L);
8330 break;
8331
8332 /*
8333 * "ga": Display the ascii value of the character under the
8334 * cursor. It is displayed in decimal, hex, and octal. -- webb
8335 */
8336 case 'a':
8337 do_ascii(NULL);
8338 break;
8339
8340#ifdef FEAT_MBYTE
8341 /*
8342 * "g8": Display the bytes used for the UTF-8 character under the
8343 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008344 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 */
8346 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008347 if (cap->count0 == 8)
8348 utf_find_illegal();
8349 else
8350 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 break;
8352#endif
8353
Bram Moolenaar60402d62017-04-20 18:54:50 +02008354 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008355 case '<':
8356 show_sb_text();
8357 break;
8358
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 /*
8360 * "gg": Goto the first line in file. With a count it goes to
8361 * that line number like for "G". -- webb
8362 */
8363 case 'g':
8364 cap->arg = FALSE;
8365 nv_goto(cap);
8366 break;
8367
8368 /*
8369 * Two-character operators:
8370 * "gq" Format text
8371 * "gw" Format text and keep cursor position
8372 * "g~" Toggle the case of the text.
8373 * "gu" Change text to lower case.
8374 * "gU" Change text to upper case.
8375 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008376 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 */
8378 case 'q':
8379 case 'w':
8380 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008381 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 case '~':
8383 case 'u':
8384 case 'U':
8385 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008386 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 nv_operator(cap);
8388 break;
8389
8390 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008391 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 * current function
8393 * "gD": idem, but in the current file.
8394 */
8395 case 'd':
8396 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008397 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 break;
8399
8400#ifdef FEAT_MOUSE
8401 /*
8402 * g<*Mouse> : <C-*mouse>
8403 */
8404 case K_MIDDLEMOUSE:
8405 case K_MIDDLEDRAG:
8406 case K_MIDDLERELEASE:
8407 case K_LEFTMOUSE:
8408 case K_LEFTDRAG:
8409 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008410 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 case K_RIGHTMOUSE:
8412 case K_RIGHTDRAG:
8413 case K_RIGHTRELEASE:
8414 case K_X1MOUSE:
8415 case K_X1DRAG:
8416 case K_X1RELEASE:
8417 case K_X2MOUSE:
8418 case K_X2DRAG:
8419 case K_X2RELEASE:
8420 mod_mask = MOD_MASK_CTRL;
8421 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8422 break;
8423#endif
8424
8425 case K_IGNORE:
8426 break;
8427
8428 /*
8429 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8430 */
8431 case 'p':
8432 case 'P':
8433 nv_put(cap);
8434 break;
8435
8436#ifdef FEAT_BYTEOFF
8437 /* "go": goto byte count from start of buffer */
8438 case 'o':
8439 goto_byte(cap->count0);
8440 break;
8441#endif
8442
8443 /* "gQ": improved Ex mode */
8444 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008445 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {
8447 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008448 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 break;
8450 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008451
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 if (!checkclearopq(oap))
8453 do_exmode(TRUE);
8454 break;
8455
8456#ifdef FEAT_JUMPLIST
8457 case ',':
8458 nv_pcmark(cap);
8459 break;
8460
8461 case ';':
8462 cap->count1 = -cap->count1;
8463 nv_pcmark(cap);
8464 break;
8465#endif
8466
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008467 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008468 if (!checkclearop(oap))
8469 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008470 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008471 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008472 if (!checkclearop(oap))
8473 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008474 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008475
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008476 case '+':
8477 case '-': /* "g+" and "g-": undo or redo along the timeline */
8478 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008479 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008480 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008481 break;
8482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 default:
8484 clearopbeep(oap);
8485 break;
8486 }
8487}
8488
8489/*
8490 * Handle "o" and "O" commands.
8491 */
8492 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008493n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008495#ifdef FEAT_CONCEAL
8496 linenr_T oldline = curwin->w_cursor.lnum;
8497#endif
8498
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 if (!checkclearopq(cap->oap))
8500 {
8501#ifdef FEAT_FOLDING
8502 if (cap->cmdchar == 'O')
8503 /* Open above the first line of a folded sequence of lines */
8504 (void)hasFolding(curwin->w_cursor.lnum,
8505 &curwin->w_cursor.lnum, NULL);
8506 else
8507 /* Open below the last line of a folded sequence of lines */
8508 (void)hasFolding(curwin->w_cursor.lnum,
8509 NULL, &curwin->w_cursor.lnum);
8510#endif
8511 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8512 (cap->cmdchar == 'O' ? 1 : 0)),
8513 (linenr_T)(curwin->w_cursor.lnum +
8514 (cap->cmdchar == 'o' ? 1 : 0))
8515 ) == OK
8516 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8517#ifdef FEAT_COMMENTS
8518 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8519#endif
8520 0, 0))
8521 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008522#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008523 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008524 update_single_line(curwin, oldline);
8525#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008526 /* When '#' is in 'cpoptions' ignore the count. */
8527 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8528 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008529#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008530 if (curwin->w_p_cul)
8531 /* force redraw of cursorline */
8532 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8535 }
8536 }
8537}
8538
8539/*
8540 * "." command: redo last change.
8541 */
8542 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008543nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544{
8545 if (!checkclearopq(cap->oap))
8546 {
8547 /*
8548 * If "restart_edit" is TRUE, the last but one command is repeated
8549 * instead of the last command (inserting text). This is used for
8550 * CTRL-O <.> in insert mode.
8551 */
8552 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8553 clearopbeep(cap->oap);
8554 }
8555}
8556
8557/*
8558 * CTRL-R: undo undo
8559 */
8560 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008561nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562{
8563 if (!checkclearopq(cap->oap))
8564 {
8565 u_redo((int)cap->count1);
8566 curwin->w_set_curswant = TRUE;
8567 }
8568}
8569
8570/*
8571 * Handle "U" command.
8572 */
8573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008574nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008577 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {
8579 /* translate "gUU" to "gUgU" */
8580 cap->cmdchar = 'g';
8581 cap->nchar = 'U';
8582 nv_operator(cap);
8583 }
8584 else if (!checkclearopq(cap->oap))
8585 {
8586 u_undoline();
8587 curwin->w_set_curswant = TRUE;
8588 }
8589}
8590
8591/*
8592 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8593 * single character.
8594 */
8595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008596nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008598 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 n_swapchar(cap);
8600 else
8601 nv_operator(cap);
8602}
8603
8604/*
8605 * Handle an operator command.
8606 * The actual work is done by do_pending_operator().
8607 */
8608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008609nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
8611 int op_type;
8612
8613 op_type = get_op_type(cap->cmdchar, cap->nchar);
8614
8615 if (op_type == cap->oap->op_type) /* double operator works on lines */
8616 nv_lineop(cap);
8617 else if (!checkclearop(cap->oap))
8618 {
8619 cap->oap->start = curwin->w_cursor;
8620 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008621#ifdef FEAT_EVAL
8622 set_op_var(op_type);
8623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 }
8625}
8626
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008627#ifdef FEAT_EVAL
8628/*
8629 * Set v:operator to the characters for "optype".
8630 */
8631 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008632set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008633{
8634 char_u opchars[3];
8635
8636 if (optype == OP_NOP)
8637 set_vim_var_string(VV_OP, NULL, 0);
8638 else
8639 {
8640 opchars[0] = get_op_char(optype);
8641 opchars[1] = get_extra_op_char(optype);
8642 opchars[2] = NUL;
8643 set_vim_var_string(VV_OP, opchars, -1);
8644 }
8645}
8646#endif
8647
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648/*
8649 * Handle linewise operator "dd", "yy", etc.
8650 *
8651 * "_" is is a strange motion command that helps make operators more logical.
8652 * It is actually implemented, but not documented in the real Vi. This motion
8653 * command actually refers to "the current line". Commands like "dd" and "yy"
8654 * are really an alternate form of "d_" and "y_". It does accept a count, so
8655 * "d3_" works to delete 3 lines.
8656 */
8657 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008658nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 cap->oap->motion_type = MLINE;
8661 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8662 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008663 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8664 && cap->oap->motion_force != 'v'
8665 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 || cap->oap->op_type == OP_LSHIFT
8667 || cap->oap->op_type == OP_RSHIFT)
8668 beginline(BL_SOL | BL_FIX);
8669 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8670 beginline(BL_WHITE | BL_FIX);
8671}
8672
8673/*
8674 * <Home> command.
8675 */
8676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008677nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008679 /* CTRL-HOME is like "gg" */
8680 if (mod_mask & MOD_MASK_CTRL)
8681 nv_goto(cap);
8682 else
8683 {
8684 cap->count0 = 1;
8685 nv_pipe(cap);
8686 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008687 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8688 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689}
8690
8691/*
8692 * "|" command.
8693 */
8694 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008695nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 cap->oap->motion_type = MCHAR;
8698 cap->oap->inclusive = FALSE;
8699 beginline(0);
8700 if (cap->count0 > 0)
8701 {
8702 coladvance((colnr_T)(cap->count0 - 1));
8703 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8704 }
8705 else
8706 curwin->w_curswant = 0;
8707 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008708 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 curwin->w_set_curswant = FALSE;
8710}
8711
8712/*
8713 * Handle back-word command "b" and "B".
8714 * cap->arg is 1 for "B"
8715 */
8716 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008717nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718{
8719 cap->oap->motion_type = MCHAR;
8720 cap->oap->inclusive = FALSE;
8721 curwin->w_set_curswant = TRUE;
8722 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8723 clearopbeep(cap->oap);
8724#ifdef FEAT_FOLDING
8725 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8726 foldOpenCursor();
8727#endif
8728}
8729
8730/*
8731 * Handle word motion commands "e", "E", "w" and "W".
8732 * cap->arg is TRUE for "E" and "W".
8733 */
8734 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008735nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736{
8737 int n;
8738 int word_end;
8739 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008740 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
8742 /*
8743 * Set inclusive for the "E" and "e" command.
8744 */
8745 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8746 word_end = TRUE;
8747 else
8748 word_end = FALSE;
8749 cap->oap->inclusive = word_end;
8750
8751 /*
8752 * "cw" and "cW" are a special case.
8753 */
8754 if (!word_end && cap->oap->op_type == OP_CHANGE)
8755 {
8756 n = gchar_cursor();
8757 if (n != NUL) /* not an empty line */
8758 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008759 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {
8761 /*
8762 * Reproduce a funny Vi behaviour: "cw" on a blank only
8763 * changes one character, not all blanks until the start of
8764 * the next word. Only do this when the 'w' flag is included
8765 * in 'cpoptions'.
8766 */
8767 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8768 {
8769 cap->oap->inclusive = TRUE;
8770 cap->oap->motion_type = MCHAR;
8771 return;
8772 }
8773 }
8774 else
8775 {
8776 /*
8777 * This is a little strange. To match what the real Vi does,
8778 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8779 * that we are not on a space or a TAB. This seems impolite
8780 * at first, but it's really more what we mean when we say
8781 * 'cw'.
8782 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008783 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 * will change only one character! This is done by setting
8785 * flag.
8786 */
8787 cap->oap->inclusive = TRUE;
8788 word_end = TRUE;
8789 flag = TRUE;
8790 }
8791 }
8792 }
8793
8794 cap->oap->motion_type = MCHAR;
8795 curwin->w_set_curswant = TRUE;
8796 if (word_end)
8797 n = end_word(cap->count1, cap->arg, flag, FALSE);
8798 else
8799 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8800
Bram Moolenaardfefb982008-04-01 10:06:39 +00008801 /* Don't leave the cursor on the NUL past the end of line. Unless we
8802 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008803 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008804 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
8806 if (n == FAIL && cap->oap->op_type == OP_NOP)
8807 clearopbeep(cap->oap);
8808 else
8809 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811#ifdef FEAT_FOLDING
8812 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8813 foldOpenCursor();
8814#endif
8815 }
8816}
8817
8818/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008819 * Used after a movement command: If the cursor ends up on the NUL after the
8820 * end of the line, may move it back to the last character and make the motion
8821 * inclusive.
8822 */
8823 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008824adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008825{
8826 /* The cursor cannot remain on the NUL when:
8827 * - the column is > 0
8828 * - not in Visual mode or 'selection' is "o"
8829 * - 'virtualedit' is not "all" and not "onemore".
8830 */
8831 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008832 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008833#ifdef FEAT_VIRTUALEDIT
8834 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8835#endif
8836 )
8837 {
8838 --curwin->w_cursor.col;
8839#ifdef FEAT_MBYTE
8840 /* prevent cursor from moving on the trail byte */
8841 if (has_mbyte)
8842 mb_adjust_cursor();
8843#endif
8844 oap->inclusive = TRUE;
8845 }
8846}
8847
8848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 * "0" and "^" commands.
8850 * cap->arg is the argument for beginline().
8851 */
8852 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008853nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
8855 cap->oap->motion_type = MCHAR;
8856 cap->oap->inclusive = FALSE;
8857 beginline(cap->arg);
8858#ifdef FEAT_FOLDING
8859 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8860 foldOpenCursor();
8861#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008862 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8863 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864}
8865
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866/*
8867 * In exclusive Visual mode, may include the last character.
8868 */
8869 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008870adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008873 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008875#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 if (has_mbyte)
8877 inc_cursor();
8878 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 ++curwin->w_cursor.col;
8881 cap->oap->inclusive = FALSE;
8882 }
8883}
8884
8885/*
8886 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8887 * Should check VIsual_mode before calling this.
8888 * Returns TRUE when backed up to the previous line.
8889 */
8890 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008891unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892{
8893 pos_T *pp;
8894
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008895 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008897 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 pp = &curwin->w_cursor;
8899 else
8900 pp = &VIsual;
8901#ifdef FEAT_VIRTUALEDIT
8902 if (pp->coladd > 0)
8903 --pp->coladd;
8904 else
8905#endif
8906 if (pp->col > 0)
8907 {
8908 --pp->col;
8909#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008910 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911#endif
8912 }
8913 else if (pp->lnum > 1)
8914 {
8915 --pp->lnum;
8916 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8917 return TRUE;
8918 }
8919 }
8920 return FALSE;
8921}
8922
8923/*
8924 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8925 */
8926 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008927nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
8929 if (VIsual_active)
8930 VIsual_select = TRUE;
8931 else if (VIsual_reselect)
8932 {
8933 cap->nchar = 'v'; /* fake "gv" command */
8934 cap->arg = TRUE;
8935 nv_g_cmd(cap);
8936 }
8937}
8938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
8940/*
8941 * "G", "gg", CTRL-END, CTRL-HOME.
8942 * cap->arg is TRUE for "G".
8943 */
8944 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008945nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
8947 linenr_T lnum;
8948
8949 if (cap->arg)
8950 lnum = curbuf->b_ml.ml_line_count;
8951 else
8952 lnum = 1L;
8953 cap->oap->motion_type = MLINE;
8954 setpcmark();
8955
8956 /* When a count is given, use it instead of the default lnum */
8957 if (cap->count0 != 0)
8958 lnum = cap->count0;
8959 if (lnum < 1L)
8960 lnum = 1L;
8961 else if (lnum > curbuf->b_ml.ml_line_count)
8962 lnum = curbuf->b_ml.ml_line_count;
8963 curwin->w_cursor.lnum = lnum;
8964 beginline(BL_SOL | BL_FIX);
8965#ifdef FEAT_FOLDING
8966 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8967 foldOpenCursor();
8968#endif
8969}
8970
8971/*
8972 * CTRL-\ in Normal mode.
8973 */
8974 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008975nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
8977 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8978 {
8979 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008980 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 clear_cmdline = TRUE; /* unshow mode later */
8982 restart_edit = 0;
8983#ifdef FEAT_CMDWIN
8984 if (cmdwin_type != 0)
8985 cmdwin_result = Ctrl_C;
8986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 if (VIsual_active)
8988 {
8989 end_visual_mode(); /* stop Visual */
8990 redraw_curbuf_later(INVERTED);
8991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8993 if (cap->nchar == Ctrl_G && p_im)
8994 restart_edit = 'a';
8995 }
8996 else
8997 clearopbeep(cap->oap);
8998}
8999
9000/*
9001 * ESC in Normal mode: beep, but don't flush buffers.
9002 * Don't even beep if we are canceling a command.
9003 */
9004 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009005nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006{
9007 int no_reason;
9008
9009 no_reason = (cap->oap->op_type == OP_NOP
9010 && cap->opcount == 0
9011 && cap->count0 == 0
9012 && cap->oap->regname == 0
9013 && !p_im);
9014
9015 if (cap->arg) /* TRUE for CTRL-C */
9016 {
9017 if (restart_edit == 0
9018#ifdef FEAT_CMDWIN
9019 && cmdwin_type == 0
9020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02009023 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024
9025 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9026 * set again below when halfway a mapping. */
9027 if (!p_im)
9028 restart_edit = 0;
9029#ifdef FEAT_CMDWIN
9030 if (cmdwin_type != 0)
9031 {
9032 cmdwin_result = K_IGNORE;
9033 got_int = FALSE; /* don't stop executing autocommands et al. */
9034 return;
9035 }
9036#endif
9037 }
9038
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 if (VIsual_active)
9040 {
9041 end_visual_mode(); /* stop Visual */
9042 check_cursor_col(); /* make sure cursor is not beyond EOL */
9043 curwin->w_set_curswant = TRUE;
9044 redraw_curbuf_later(INVERTED);
9045 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009046 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009047 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 clearop(cap->oap);
9049
9050 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9051 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009052 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 restart_edit = 'a';
9054}
9055
9056/*
9057 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009058 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 */
9060 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009061nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063 /* <Insert> is equal to "i" */
9064 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9065 cap->cmdchar = 'i';
9066
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 /* in Visual mode "A" and "I" are an operator */
9068 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02009069 {
9070#ifdef FEAT_TERMINAL
9071 if (term_in_normal_mode())
9072 {
9073 end_visual_mode();
9074 clearop(cap->oap);
9075 term_enter_job_mode();
9076 return;
9077 }
9078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02009080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081
9082 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009083 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9084 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 {
9086#ifdef FEAT_TEXTOBJ
9087 nv_object(cap);
9088#else
9089 clearopbeep(cap->oap);
9090#endif
9091 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009092#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009093 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009094 {
9095 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009096 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009097 return;
9098 }
9099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 else if (!curbuf->b_p_ma && !p_im)
9101 {
9102 /* Only give this error when 'insertmode' is off. */
9103 EMSG(_(e_modifiable));
9104 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009105 if (cap->cmdchar == K_PS)
9106 /* drop the pasted text */
9107 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009109 else if (cap->cmdchar == K_PS && VIsual_active)
9110 {
9111 pos_T old_pos = curwin->w_cursor;
9112 pos_T old_visual = VIsual;
9113
9114 /* In Visual mode the selected text is deleted. */
9115 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9116 {
9117 shift_delete_registers();
9118 cap->oap->regname = '1';
9119 }
9120 else
9121 cap->oap->regname = '-';
9122 cap->cmdchar = 'd';
9123 cap->nchar = NUL;
9124 nv_operator(cap);
9125 do_pending_operator(cap, 0, FALSE);
9126 cap->cmdchar = K_PS;
9127
9128 /* When the last char in the line was deleted then append. Detect this
9129 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009130 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9131 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009132 inc_cursor();
9133
9134 /* Insert to replace the deleted text with the pasted text. */
9135 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 else if (!checkclearopq(cap->oap))
9138 {
9139 switch (cap->cmdchar)
9140 {
9141 case 'A': /* "A"ppend after the line */
9142 curwin->w_set_curswant = TRUE;
9143#ifdef FEAT_VIRTUALEDIT
9144 if (ve_flags == VE_ALL)
9145 {
9146 int save_State = State;
9147
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009148 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 * character past the end of the line */
9150 State = INSERT;
9151 coladvance((colnr_T)MAXCOL);
9152 State = save_State;
9153 }
9154 else
9155#endif
9156 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9157 break;
9158
9159 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009160 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9161 beginline(BL_WHITE);
9162 else
9163 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 break;
9165
Bram Moolenaara1891842017-02-04 21:34:31 +01009166 case K_PS:
9167 /* Bracketed paste works like "a"ppend, unless the cursor is in
9168 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009169 if (curwin->w_cursor.col == 0)
9170 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009171 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009172
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9174#ifdef FEAT_VIRTUALEDIT
9175 /* increment coladd when in virtual space, increment the
9176 * column otherwise, also to append after an unprintable char */
9177 if (virtual_active()
9178 && (curwin->w_cursor.coladd > 0
9179 || *ml_get_cursor() == NUL
9180 || *ml_get_cursor() == TAB))
9181 curwin->w_cursor.coladd++;
9182 else
9183#endif
9184 if (*ml_get_cursor() != NUL)
9185 inc_cursor();
9186 break;
9187 }
9188
9189#ifdef FEAT_VIRTUALEDIT
9190 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9191 {
9192 int save_State = State;
9193
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009194 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 * character past the end of the line */
9196 State = INSERT;
9197 coladvance(getviscol());
9198 State = save_State;
9199 }
9200#endif
9201
9202 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9203 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009204 else if (cap->cmdchar == K_PS)
9205 /* drop the pasted text */
9206 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207}
9208
9209/*
9210 * Invoke edit() and take care of "restart_edit" and the return value.
9211 */
9212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009213invoke_edit(
9214 cmdarg_T *cap,
9215 int repl, /* "r" or "gr" command */
9216 int cmd,
9217 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218{
9219 int restart_edit_save = 0;
9220
9221 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9222 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9223 * it. */
9224 if (repl || !stuff_empty())
9225 restart_edit_save = restart_edit;
9226 else
9227 restart_edit_save = 0;
9228
9229 /* Always reset "restart_edit", this is not a restarted edit. */
9230 restart_edit = 0;
9231
9232 if (edit(cmd, startln, cap->count1))
9233 cap->retval |= CA_COMMAND_BUSY;
9234
9235 if (restart_edit == 0)
9236 restart_edit = restart_edit_save;
9237}
9238
9239#ifdef FEAT_TEXTOBJ
9240/*
9241 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9242 */
9243 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009244nv_object(
9245 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246{
9247 int flag;
9248 int include;
9249 char_u *mps_save;
9250
9251 if (cap->cmdchar == 'i')
9252 include = FALSE; /* "ix" = inner object: exclude white space */
9253 else
9254 include = TRUE; /* "ax" = an object: include white space */
9255
9256 /* Make sure (), [], {} and <> are in 'matchpairs' */
9257 mps_save = curbuf->b_p_mps;
9258 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9259
9260 switch (cap->nchar)
9261 {
9262 case 'w': /* "aw" = a word */
9263 flag = current_word(cap->oap, cap->count1, include, FALSE);
9264 break;
9265 case 'W': /* "aW" = a WORD */
9266 flag = current_word(cap->oap, cap->count1, include, TRUE);
9267 break;
9268 case 'b': /* "ab" = a braces block */
9269 case '(':
9270 case ')':
9271 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9272 break;
9273 case 'B': /* "aB" = a Brackets block */
9274 case '{':
9275 case '}':
9276 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9277 break;
9278 case '[': /* "a[" = a [] block */
9279 case ']':
9280 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9281 break;
9282 case '<': /* "a<" = a <> block */
9283 case '>':
9284 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9285 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009286 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009287 /* Do not adjust oap->end in do_pending_operator()
9288 * otherwise there are different results for 'dit'
9289 * (note leading whitespace in last line):
9290 * 1) <b> 2) <b>
9291 * foobar foobar
9292 * </b> </b>
9293 */
9294 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009295 flag = current_tagblock(cap->oap, cap->count1, include);
9296 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 case 'p': /* "ap" = a paragraph */
9298 flag = current_par(cap->oap, cap->count1, include, 'p');
9299 break;
9300 case 's': /* "as" = a sentence */
9301 flag = current_sent(cap->oap, cap->count1, include);
9302 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009303 case '"': /* "a"" = a double quoted string */
9304 case '\'': /* "a'" = a single quoted string */
9305 case '`': /* "a`" = a backtick quoted string */
9306 flag = current_quote(cap->oap, cap->count1, include,
9307 cap->nchar);
9308 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309#if 0 /* TODO */
9310 case 'S': /* "aS" = a section */
9311 case 'f': /* "af" = a filename */
9312 case 'u': /* "au" = a URL */
9313#endif
9314 default:
9315 flag = FAIL;
9316 break;
9317 }
9318
9319 curbuf->b_p_mps = mps_save;
9320 if (flag == FAIL)
9321 clearopbeep(cap->oap);
9322 adjust_cursor_col();
9323 curwin->w_set_curswant = TRUE;
9324}
9325#endif
9326
9327/*
9328 * "q" command: Start/stop recording.
9329 * "q:", "q/", "q?": edit command-line in command-line window.
9330 */
9331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009332nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 if (cap->oap->op_type == OP_FORMAT)
9335 {
9336 /* "gqq" is the same as "gqgq": format line */
9337 cap->cmdchar = 'g';
9338 cap->nchar = 'q';
9339 nv_operator(cap);
9340 }
9341 else if (!checkclearop(cap->oap))
9342 {
9343#ifdef FEAT_CMDWIN
9344 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9345 {
9346 stuffcharReadbuff(cap->nchar);
9347 stuffcharReadbuff(K_CMDWIN);
9348 }
9349 else
9350#endif
9351 /* (stop) recording into a named register, unless executing a
9352 * register */
9353 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9354 clearopbeep(cap->oap);
9355 }
9356}
9357
9358/*
9359 * Handle the "@r" command.
9360 */
9361 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009362nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363{
9364 if (checkclearop(cap->oap))
9365 return;
9366#ifdef FEAT_EVAL
9367 if (cap->nchar == '=')
9368 {
9369 if (get_expr_register() == NUL)
9370 return;
9371 }
9372#endif
9373 while (cap->count1-- && !got_int)
9374 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009375 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
9377 clearopbeep(cap->oap);
9378 break;
9379 }
9380 line_breakcheck();
9381 }
9382}
9383
9384/*
9385 * Handle the CTRL-U and CTRL-D commands.
9386 */
9387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009388nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389{
9390 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9391 || (cap->cmdchar == Ctrl_D
9392 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9393 clearopbeep(cap->oap);
9394 else if (!checkclearop(cap->oap))
9395 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9396}
9397
9398/*
9399 * Handle "J" or "gJ" command.
9400 */
9401 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009402nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 if (VIsual_active) /* join the visual lines */
9405 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009406 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 {
9408 if (cap->count0 <= 1)
9409 cap->count0 = 2; /* default for join is two lines! */
9410 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9411 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009413 /* can't join when on the last line */
9414 if (cap->count0 <= 2)
9415 {
9416 clearopbeep(cap->oap);
9417 return;
9418 }
9419 cap->count0 = curbuf->b_ml.ml_line_count
9420 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009422
9423 prep_redo(cap->oap->regname, cap->count0,
9424 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9425 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
9427}
9428
9429/*
9430 * "P", "gP", "p" and "gp" commands.
9431 */
9432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009433nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 int regname = 0;
9436 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009437 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009438 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 int dir;
9440 int flags = 0;
9441
9442 if (cap->oap->op_type != OP_NOP)
9443 {
9444#ifdef FEAT_DIFF
9445 /* "dp" is ":diffput" */
9446 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9447 {
9448 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009449 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 }
9451 else
9452#endif
9453 clearopbeep(cap->oap);
9454 }
9455 else
9456 {
9457 dir = (cap->cmdchar == 'P'
9458 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9459 ? BACKWARD : FORWARD;
9460 prep_redo_cmd(cap);
9461 if (cap->cmdchar == 'g')
9462 flags |= PUT_CURSEND;
9463
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 if (VIsual_active)
9465 {
9466 /* Putting in Visual mode: The put text replaces the selected
9467 * text. First delete the selected text, then put the new text.
9468 * Need to save and restore the registers that the delete
9469 * overwrites if the old contents is being put.
9470 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009471 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009473#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009475#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009476 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009477 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009478#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
9482 )
9483 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009484 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 * put, save it first. */
9486 reg1 = get_register(regname, TRUE);
9487 }
9488
9489 /* Now delete the selected text. */
9490 cap->cmdchar = 'd';
9491 cap->nchar = NUL;
9492 cap->oap->regname = NUL;
9493 nv_operator(cap);
9494 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009495 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496
9497 /* delete PUT_LINE_BACKWARD; */
9498 cap->oap->regname = regname;
9499
9500 if (reg1 != NULL)
9501 {
9502 /* Delete probably changed the register we want to put, save
9503 * it first. Then put back what was there before the delete. */
9504 reg2 = get_register(regname, FALSE);
9505 put_register(regname, reg1);
9506 }
9507
9508 /* When deleted a linewise Visual area, put the register as
9509 * lines to avoid it joined with the next line. When deletion was
9510 * characterwise, split a line when putting lines. */
9511 if (VIsual_mode == 'V')
9512 flags |= PUT_LINE;
9513 else if (VIsual_mode == 'v')
9514 flags |= PUT_LINE_SPLIT;
9515 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9516 flags |= PUT_LINE_FORWARD;
9517 dir = BACKWARD;
9518 if ((VIsual_mode != 'V'
9519 && curwin->w_cursor.col < curbuf->b_op_start.col)
9520 || (VIsual_mode == 'V'
9521 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9522 /* cursor is at the end of the line or end of file, put
9523 * forward. */
9524 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009525 /* May have been reset in do_put(). */
9526 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 do_put(cap->oap->regname, dir, cap->count1, flags);
9529
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 /* If a register was saved, put it back now. */
9531 if (reg2 != NULL)
9532 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009533
9534 /* What to reselect with "gv"? Selecting the just put text seems to
9535 * be the most useful, since the original text was removed. */
9536 if (was_visual)
9537 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009538 curbuf->b_visual.vi_start = curbuf->b_op_start;
9539 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009540 /* need to adjust cursor position */
9541 if (*p_sel == 'e')
9542 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009543 }
9544
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009545 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009546 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009547 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009548 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009549 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009550
9551 /* If the cursor was in that line, move it to the end of the last
9552 * line. */
9553 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9554 {
9555 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9556 coladvance((colnr_T)MAXCOL);
9557 }
9558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 auto_format(FALSE, TRUE);
9560 }
9561}
9562
9563/*
9564 * "o" and "O" commands.
9565 */
9566 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009567nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568{
9569#ifdef FEAT_DIFF
9570 /* "do" is ":diffget" */
9571 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9572 {
9573 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009574 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 }
9576 else
9577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 if (VIsual_active) /* switch start and end of visual */
9579 v_swap_corners(cap->cmdchar);
9580 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 n_opencmd(cap);
9582}
9583
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584#ifdef FEAT_NETBEANS_INTG
9585 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009586nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 netbeans_keycommand(cap->nchar);
9589}
9590#endif
9591
9592#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009594nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595{
9596 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9597}
9598#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009599
9600#ifdef FEAT_AUTOCMD
9601/*
9602 * Trigger CursorHold event.
9603 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9604 * input buffer. "did_cursorhold" is set to avoid retriggering.
9605 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009606 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009607nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009608{
9609 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9610 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009611 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009612}
9613#endif
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009614
9615/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009616 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009617 */
9618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009619get_op_vcol(
9620 oparg_T *oap,
9621 colnr_T redo_VIsual_vcol,
9622 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009623{
9624 colnr_T start, end;
9625
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009626 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009627 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009628 return;
9629
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009630 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009631
9632#ifdef FEAT_MBYTE
9633 /* prevent from moving onto a trail byte */
9634 if (has_mbyte)
9635 mb_adjustpos(curwin->w_buffer, &oap->end);
9636#endif
9637
9638 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009639
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009640 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009641 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009642 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9643
9644 if (start < oap->start_vcol)
9645 oap->start_vcol = start;
9646 if (end > oap->end_vcol)
9647 {
9648 if (initial && *p_sel == 'e' && start >= 1
9649 && start - 1 >= oap->end_vcol)
9650 oap->end_vcol = start - 1;
9651 else
9652 oap->end_vcol = end;
9653 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009654 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009655
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009656 /* if '$' was used, get oap->end_vcol from longest line */
9657 if (curwin->w_curswant == MAXCOL)
9658 {
9659 curwin->w_cursor.col = MAXCOL;
9660 oap->end_vcol = 0;
9661 for (curwin->w_cursor.lnum = oap->start.lnum;
9662 curwin->w_cursor.lnum <= oap->end.lnum;
9663 ++curwin->w_cursor.lnum)
9664 {
9665 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9666 if (end > oap->end_vcol)
9667 oap->end_vcol = end;
9668 }
9669 }
9670 else if (redo_VIsual_busy)
9671 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9672 /*
9673 * Correct oap->end.col and oap->start.col to be the
9674 * upper-left and lower-right corner of the block area.
9675 *
9676 * (Actually, this does convert column positions into character
9677 * positions)
9678 */
9679 curwin->w_cursor.lnum = oap->end.lnum;
9680 coladvance(oap->end_vcol);
9681 oap->end = curwin->w_cursor;
9682
9683 curwin->w_cursor = oap->start;
9684 coladvance(oap->start_vcol);
9685 oap->start = curwin->w_cursor;
9686}