blob: 5aa7fe0494cd86b06110e26e9a7b6140b3744e3f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
12 * the operators.
13 */
14
15#include "vim.h"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017/*
18 * The Visual area is remembered for reselection.
19 */
20static int resel_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
21static linenr_T resel_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +020022static colnr_T resel_VIsual_vcol; /* nr of cols or end col */
Bram Moolenaar7d311c52014-02-22 23:49:35 +010023static int VIsual_mode_orig = NUL; /* saved Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25static int restart_VIsual_select = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010027#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010028static void set_vcount_ca(cmdarg_T *cap, int *set_prevcount);
Bram Moolenaarf82a2d22010-12-17 18:53:01 +010029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static int
Bram Moolenaar0c50a6b2012-05-25 11:04:38 +020031#ifdef __BORLANDC__
32 _RTLENTRYF
33#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034 nv_compare(const void *s1, const void *s2);
35static int find_command(int cmdchar);
36static void op_colon(oparg_T *oap);
37static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010038#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void find_start_of_word(pos_T *);
40static void find_end_of_word(pos_T *);
41static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void prep_redo_cmd(cmdarg_T *cap);
44static void prep_redo(int regname, long, int, int, int, int, int);
45static int checkclearop(oparg_T *oap);
46static int checkclearopq(oparg_T *oap);
47static void clearop(oparg_T *oap);
48static void clearopbeep(oparg_T *oap);
49static void unshift_special(cmdarg_T *cap);
50static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010052static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#endif
54
55/*
56 * nv_*(): functions called to handle Normal and Visual mode commands.
57 * n_*(): functions called to handle Normal mode commands.
58 * v_*(): functions called to handle Visual mode commands.
59 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void nv_ignore(cmdarg_T *cap);
61static void nv_nop(cmdarg_T *cap);
62static void nv_error(cmdarg_T *cap);
63static void nv_help(cmdarg_T *cap);
64static void nv_addsub(cmdarg_T *cap);
65static void nv_page(cmdarg_T *cap);
66static void nv_gd(oparg_T *oap, int nchar, int thisblock);
67static int nv_screengo(oparg_T *oap, int dir, long dist);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_mousescroll(cmdarg_T *cap);
70static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010072static void nv_scroll_line(cmdarg_T *cap);
73static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010075static void nv_ver_scrollbar(cmdarg_T *cap);
76static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000078#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010079static void nv_tabline(cmdarg_T *cap);
80static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000081#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010082static void nv_exmode(cmdarg_T *cap);
83static void nv_colon(cmdarg_T *cap);
84static void nv_ctrlg(cmdarg_T *cap);
85static void nv_ctrlh(cmdarg_T *cap);
86static void nv_clear(cmdarg_T *cap);
87static void nv_ctrlo(cmdarg_T *cap);
88static void nv_hat(cmdarg_T *cap);
89static void nv_Zet(cmdarg_T *cap);
90static void nv_ident(cmdarg_T *cap);
91static void nv_tagpop(cmdarg_T *cap);
92static void nv_scroll(cmdarg_T *cap);
93static void nv_right(cmdarg_T *cap);
94static void nv_left(cmdarg_T *cap);
95static void nv_up(cmdarg_T *cap);
96static void nv_down(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_SEARCHPATH
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void nv_gotofile(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void nv_end(cmdarg_T *cap);
101static void nv_dollar(cmdarg_T *cap);
102static void nv_search(cmdarg_T *cap);
103static void nv_next(cmdarg_T *cap);
104static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
105static void nv_csearch(cmdarg_T *cap);
106static void nv_brackets(cmdarg_T *cap);
107static void nv_percent(cmdarg_T *cap);
108static void nv_brace(cmdarg_T *cap);
109static void nv_mark(cmdarg_T *cap);
110static void nv_findpar(cmdarg_T *cap);
111static void nv_undo(cmdarg_T *cap);
112static void nv_kundo(cmdarg_T *cap);
113static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_VREPLACE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_vreplace(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void v_swap_corners(int cmdchar);
118static void nv_replace(cmdarg_T *cap);
119static void n_swapchar(cmdarg_T *cap);
120static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
121static void v_visop(cmdarg_T *cap);
122static void nv_subst(cmdarg_T *cap);
123static void nv_abbrev(cmdarg_T *cap);
124static void nv_optrans(cmdarg_T *cap);
125static void nv_gomark(cmdarg_T *cap);
126static void nv_pcmark(cmdarg_T *cap);
127static void nv_regname(cmdarg_T *cap);
128static void nv_visual(cmdarg_T *cap);
129static void n_start_visual_mode(int c);
130static void nv_window(cmdarg_T *cap);
131static void nv_suspend(cmdarg_T *cap);
132static void nv_g_cmd(cmdarg_T *cap);
133static void n_opencmd(cmdarg_T *cap);
134static void nv_dot(cmdarg_T *cap);
135static void nv_redo(cmdarg_T *cap);
136static void nv_Undo(cmdarg_T *cap);
137static void nv_tilde(cmdarg_T *cap);
138static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000139#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100140static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000141#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100142static void nv_lineop(cmdarg_T *cap);
143static void nv_home(cmdarg_T *cap);
144static void nv_pipe(cmdarg_T *cap);
145static void nv_bck_word(cmdarg_T *cap);
146static void nv_wordcmd(cmdarg_T *cap);
147static void nv_beginline(cmdarg_T *cap);
148static void adjust_cursor(oparg_T *oap);
149static void adjust_for_sel(cmdarg_T *cap);
150static int unadjust_for_sel(void);
151static void nv_select(cmdarg_T *cap);
152static void nv_goto(cmdarg_T *cap);
153static void nv_normal(cmdarg_T *cap);
154static void nv_esc(cmdarg_T *oap);
155static void nv_edit(cmdarg_T *cap);
156static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100158static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100160static void nv_record(cmdarg_T *cap);
161static void nv_at(cmdarg_T *cap);
162static void nv_halfpage(cmdarg_T *cap);
163static void nv_join(cmdarg_T *cap);
164static void nv_put(cmdarg_T *cap);
165static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100167static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168#endif
169#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100170static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100172static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100173static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000175static char *e_noident = N_("E349: No identifier under cursor");
176
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177/*
178 * Function to be called for a Normal or Visual mode command.
179 * The argument is a cmdarg_T.
180 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100181typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
183/* Values for cmd_flags. */
184#define NV_NCH 0x01 /* may need to get a second char */
185#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
186#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
187#define NV_LANG 0x08 /* second char needs language adjustment */
188
189#define NV_SS 0x10 /* may start selection */
190#define NV_SSS 0x20 /* may start selection with shift modifier */
191#define NV_STS 0x40 /* may stop selection without shift modif. */
192#define NV_RL 0x80 /* 'rightleft' modifies command */
193#define NV_KEEPREG 0x100 /* don't clear regname */
194#define NV_NCW 0x200 /* not allowed in command-line window */
195
196/*
197 * Generally speaking, every Normal mode command should either clear any
198 * pending operator (with *clearop*()), or set the motion type variable
199 * oap->motion_type.
200 *
201 * When a cursor motion command is made, it is marked as being a character or
202 * line oriented motion. Then, if an operator is in effect, the operation
203 * becomes character or line oriented accordingly.
204 */
205
206/*
207 * This table contains one entry for every Normal or Visual mode command.
208 * The order doesn't matter, init_normal_cmds() will create a sorted index.
209 * It is faster when all keys from zero to '~' are present.
210 */
211static const struct nv_cmd
212{
213 int cmd_char; /* (first) command character */
214 nv_func_T cmd_func; /* function for this command */
215 short_u cmd_flags; /* NV_ flags */
216 short cmd_arg; /* value for ca.arg */
217} nv_cmds[] =
218{
219 {NUL, nv_error, 0, 0},
220 {Ctrl_A, nv_addsub, 0, 0},
221 {Ctrl_B, nv_page, NV_STS, BACKWARD},
222 {Ctrl_C, nv_esc, 0, TRUE},
223 {Ctrl_D, nv_halfpage, 0, 0},
224 {Ctrl_E, nv_scroll_line, 0, TRUE},
225 {Ctrl_F, nv_page, NV_STS, FORWARD},
226 {Ctrl_G, nv_ctrlg, 0, 0},
227 {Ctrl_H, nv_ctrlh, 0, 0},
228 {Ctrl_I, nv_pcmark, 0, 0},
229 {NL, nv_down, 0, FALSE},
230 {Ctrl_K, nv_error, 0, 0},
231 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000232 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {Ctrl_N, nv_down, NV_STS, FALSE},
234 {Ctrl_O, nv_ctrlo, 0, 0},
235 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000236 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {Ctrl_R, nv_redo, 0, 0},
238 {Ctrl_S, nv_ignore, 0, 0},
239 {Ctrl_T, nv_tagpop, NV_NCW, 0},
240 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 {Ctrl_V, nv_visual, 0, FALSE},
242 {'V', nv_visual, 0, FALSE},
243 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 {Ctrl_W, nv_window, 0, 0},
245 {Ctrl_X, nv_addsub, 0, 0},
246 {Ctrl_Y, nv_scroll_line, 0, FALSE},
247 {Ctrl_Z, nv_suspend, 0, 0},
248 {ESC, nv_esc, 0, FALSE},
249 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
250 {Ctrl_RSB, nv_ident, NV_NCW, 0},
251 {Ctrl_HAT, nv_hat, NV_NCW, 0},
252 {Ctrl__, nv_error, 0, 0},
253 {' ', nv_right, 0, 0},
254 {'!', nv_operator, 0, 0},
255 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
256 {'#', nv_ident, 0, 0},
257 {'$', nv_dollar, 0, 0},
258 {'%', nv_percent, 0, 0},
259 {'&', nv_optrans, 0, 0},
260 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
261 {'(', nv_brace, 0, BACKWARD},
262 {')', nv_brace, 0, FORWARD},
263 {'*', nv_ident, 0, 0},
264 {'+', nv_down, 0, TRUE},
265 {',', nv_csearch, 0, TRUE},
266 {'-', nv_up, 0, TRUE},
267 {'.', nv_dot, NV_KEEPREG, 0},
268 {'/', nv_search, 0, FALSE},
269 {'0', nv_beginline, 0, 0},
270 {'1', nv_ignore, 0, 0},
271 {'2', nv_ignore, 0, 0},
272 {'3', nv_ignore, 0, 0},
273 {'4', nv_ignore, 0, 0},
274 {'5', nv_ignore, 0, 0},
275 {'6', nv_ignore, 0, 0},
276 {'7', nv_ignore, 0, 0},
277 {'8', nv_ignore, 0, 0},
278 {'9', nv_ignore, 0, 0},
279 {':', nv_colon, 0, 0},
280 {';', nv_csearch, 0, FALSE},
281 {'<', nv_operator, NV_RL, 0},
282 {'=', nv_operator, 0, 0},
283 {'>', nv_operator, NV_RL, 0},
284 {'?', nv_search, 0, FALSE},
285 {'@', nv_at, NV_NCH_NOP, FALSE},
286 {'A', nv_edit, 0, 0},
287 {'B', nv_bck_word, 0, 1},
288 {'C', nv_abbrev, NV_KEEPREG, 0},
289 {'D', nv_abbrev, NV_KEEPREG, 0},
290 {'E', nv_wordcmd, 0, TRUE},
291 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
292 {'G', nv_goto, 0, TRUE},
293 {'H', nv_scroll, 0, 0},
294 {'I', nv_edit, 0, 0},
295 {'J', nv_join, 0, 0},
296 {'K', nv_ident, 0, 0},
297 {'L', nv_scroll, 0, 0},
298 {'M', nv_scroll, 0, 0},
299 {'N', nv_next, 0, SEARCH_REV},
300 {'O', nv_open, 0, 0},
301 {'P', nv_put, 0, 0},
302 {'Q', nv_exmode, NV_NCW, 0},
303 {'R', nv_Replace, 0, FALSE},
304 {'S', nv_subst, NV_KEEPREG, 0},
305 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
306 {'U', nv_Undo, 0, 0},
307 {'W', nv_wordcmd, 0, TRUE},
308 {'X', nv_abbrev, NV_KEEPREG, 0},
309 {'Y', nv_abbrev, NV_KEEPREG, 0},
310 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
311 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
312 {'\\', nv_error, 0, 0},
313 {']', nv_brackets, NV_NCH_ALW, FORWARD},
314 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
315 {'_', nv_lineop, 0, 0},
316 {'`', nv_gomark, NV_NCH_ALW, FALSE},
317 {'a', nv_edit, NV_NCH, 0},
318 {'b', nv_bck_word, 0, 0},
319 {'c', nv_operator, 0, 0},
320 {'d', nv_operator, 0, 0},
321 {'e', nv_wordcmd, 0, FALSE},
322 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
323 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
324 {'h', nv_left, NV_RL, 0},
325 {'i', nv_edit, NV_NCH, 0},
326 {'j', nv_down, 0, FALSE},
327 {'k', nv_up, 0, FALSE},
328 {'l', nv_right, NV_RL, 0},
329 {'m', nv_mark, NV_NCH_NOP, 0},
330 {'n', nv_next, 0, 0},
331 {'o', nv_open, 0, 0},
332 {'p', nv_put, 0, 0},
333 {'q', nv_record, NV_NCH, 0},
334 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
335 {'s', nv_subst, NV_KEEPREG, 0},
336 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
337 {'u', nv_undo, 0, 0},
338 {'w', nv_wordcmd, 0, FALSE},
339 {'x', nv_abbrev, NV_KEEPREG, 0},
340 {'y', nv_operator, 0, 0},
341 {'z', nv_zet, NV_NCH_ALW, 0},
342 {'{', nv_findpar, 0, BACKWARD},
343 {'|', nv_pipe, 0, 0},
344 {'}', nv_findpar, 0, FORWARD},
345 {'~', nv_tilde, 0, 0},
346
347 /* pound sign */
348 {POUND, nv_ident, 0, 0},
349#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200350 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
351 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
352 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
353 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 {K_LEFTMOUSE, nv_mouse, 0, 0},
355 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
356 {K_LEFTDRAG, nv_mouse, 0, 0},
357 {K_LEFTRELEASE, nv_mouse, 0, 0},
358 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100359 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
361 {K_MIDDLEDRAG, nv_mouse, 0, 0},
362 {K_MIDDLERELEASE, nv_mouse, 0, 0},
363 {K_RIGHTMOUSE, nv_mouse, 0, 0},
364 {K_RIGHTDRAG, nv_mouse, 0, 0},
365 {K_RIGHTRELEASE, nv_mouse, 0, 0},
366 {K_X1MOUSE, nv_mouse, 0, 0},
367 {K_X1DRAG, nv_mouse, 0, 0},
368 {K_X1RELEASE, nv_mouse, 0, 0},
369 {K_X2MOUSE, nv_mouse, 0, 0},
370 {K_X2DRAG, nv_mouse, 0, 0},
371 {K_X2RELEASE, nv_mouse, 0, 0},
372#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000373 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000374 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {K_INS, nv_edit, 0, 0},
376 {K_KINS, nv_edit, 0, 0},
377 {K_BS, nv_ctrlh, 0, 0},
378 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
379 {K_S_UP, nv_page, NV_SS, BACKWARD},
380 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
381 {K_S_DOWN, nv_page, NV_SS, FORWARD},
382 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
383 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
384 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
385 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
386 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
387 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
388 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
389 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
390 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
391 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
392 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
393 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 {K_S_END, nv_end, NV_SS, FALSE},
395 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
396 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
397 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {K_S_HOME, nv_home, NV_SS, 0},
399 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
400 {K_DEL, nv_abbrev, 0, 0},
401 {K_KDEL, nv_abbrev, 0, 0},
402 {K_UNDO, nv_kundo, 0, 0},
403 {K_HELP, nv_help, NV_NCW, 0},
404 {K_F1, nv_help, NV_NCW, 0},
405 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407#ifdef FEAT_GUI
408 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
409 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
410#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000411#ifdef FEAT_GUI_TABLINE
412 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000413 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415#ifdef FEAT_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200416 {K_F8, farsi_f8, 0, 0},
417 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#ifdef FEAT_NETBEANS_INTG
420 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
421#endif
422#ifdef FEAT_DND
423 {K_DROP, nv_drop, NV_STS, 0},
424#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000425 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100426 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427};
428
429/* Number of commands in nv_cmds[]. */
430#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
431
432/* Sorted index of commands in nv_cmds[]. */
433static short nv_cmd_idx[NV_CMDS_SIZE];
434
435/* The highest index for which
436 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
437static int nv_max_linear;
438
439/*
440 * Compare functions for qsort() below, that checks the command character
441 * through the index in nv_cmd_idx[].
442 */
443 static int
444#ifdef __BORLANDC__
445_RTLENTRYF
446#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100447nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449 int c1, c2;
450
451 /* The commands are sorted on absolute value. */
452 c1 = nv_cmds[*(const short *)s1].cmd_char;
453 c2 = nv_cmds[*(const short *)s2].cmd_char;
454 if (c1 < 0)
455 c1 = -c1;
456 if (c2 < 0)
457 c2 = -c2;
458 return c1 - c2;
459}
460
461/*
462 * Initialize the nv_cmd_idx[] table.
463 */
464 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100465init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466{
467 int i;
468
469 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000470 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 nv_cmd_idx[i] = i;
472
473 /* Sort the commands by the command character. */
474 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
475
476 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000477 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
479 break;
480 nv_max_linear = i - 1;
481}
482
483/*
484 * Search for a command in the commands table.
485 * Returns -1 for invalid command.
486 */
487 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489{
490 int i;
491 int idx;
492 int top, bot;
493 int c;
494
495#ifdef FEAT_MBYTE
496 /* A multi-byte character is never a command. */
497 if (cmdchar >= 0x100)
498 return -1;
499#endif
500
501 /* We use the absolute value of the character. Special keys have a
502 * negative value, but are sorted on their absolute value. */
503 if (cmdchar < 0)
504 cmdchar = -cmdchar;
505
506 /* If the character is in the first part: The character is the index into
507 * nv_cmd_idx[]. */
508 if (cmdchar <= nv_max_linear)
509 return nv_cmd_idx[cmdchar];
510
511 /* Perform a binary search. */
512 bot = nv_max_linear + 1;
513 top = NV_CMDS_SIZE - 1;
514 idx = -1;
515 while (bot <= top)
516 {
517 i = (top + bot) / 2;
518 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
519 if (c < 0)
520 c = -c;
521 if (cmdchar == c)
522 {
523 idx = nv_cmd_idx[i];
524 break;
525 }
526 if (cmdchar > c)
527 bot = i + 1;
528 else
529 top = i - 1;
530 }
531 return idx;
532}
533
534/*
535 * Execute a command in Normal mode.
536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100538normal_cmd(
539 oparg_T *oap,
540 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 cmdarg_T ca; /* command arguments */
543 int c;
544 int ctrl_w = FALSE; /* got CTRL-W command */
545 int old_col = curwin->w_curswant;
546#ifdef FEAT_CMDL_INFO
547 int need_flushbuf; /* need to call out_flush() */
548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 pos_T old_pos; /* cursor position before command */
550 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 static int old_mapped_len = 0;
552 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000553#ifdef FEAT_EVAL
554 int set_prevcount = FALSE;
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
558 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000559
560 /* Use a count remembered from before entering an operator. After typing
561 * "3d" we return from normal_cmd() and come back here, the "3" is
562 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 ca.opcount = opcount;
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 /*
566 * If there is an operator pending, then the command we take this time
567 * will terminate it. Finish_op tells us to finish the operation before
568 * returning this time (unless the operation was cancelled).
569 */
570#ifdef CURSOR_SHAPE
571 c = finish_op;
572#endif
573 finish_op = (oap->op_type != OP_NOP);
574#ifdef CURSOR_SHAPE
575 if (finish_op != c)
576 {
577 ui_cursor_shape(); /* may show different cursor shape */
578# ifdef FEAT_MOUSESHAPE
579 update_mouseshape(-1);
580# endif
581 }
582#endif
583
Bram Moolenaara983fe92008-07-31 20:04:27 +0000584 /* When not finishing an operator and no register name typed, reset the
585 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000589#ifdef FEAT_EVAL
590 set_prevcount = TRUE;
591#endif
592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
Bram Moolenaara983fe92008-07-31 20:04:27 +0000594 /* Restore counts from before receiving K_CURSORHOLD. This means after
595 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
596 * "3 * 2". */
597 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
598 {
599 ca.opcount = oap->prev_opcount;
600 ca.count0 = oap->prev_count0;
601 oap->prev_opcount = 0;
602 oap->prev_count0 = 0;
603 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
607 State = NORMAL_BUSY;
608#ifdef USE_ON_FLY_SCROLL
609 dont_scroll = FALSE; /* allow scrolling here */
610#endif
611
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100612#ifdef FEAT_EVAL
613 /* Set v:count here, when called from main() and not a stuffed
614 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100615 * when there is no count. Do set it for redo. */
616 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100617 set_vcount_ca(&ca, &set_prevcount);
618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 /*
621 * Get the command character from the user.
622 */
623 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100624 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
626 /*
627 * If a mapping was started in Visual or Select mode, remember the length
628 * of the mapping. This is used below to not return to Insert mode for as
629 * long as the mapping is being executed.
630 */
631 if (restart_edit == 0)
632 old_mapped_len = 0;
633 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000634 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 old_mapped_len = typebuf_maplen();
636
637 if (c == NUL)
638 c = K_ZERO;
639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 /*
641 * In Select mode, typed text replaces the selection.
642 */
643 if (VIsual_active
644 && VIsual_select
645 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
646 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000647 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
648 * 'insertmode' is set) fake a "d"elete command, Insert mode will
649 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000650 * Insert the typed character in the typeahead buffer, so that it can
651 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000652 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000653 if (restart_edit != 0)
654 c = 'd';
655 else
656 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000657 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200658 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661#ifdef FEAT_CMDL_INFO
662 need_flushbuf = add_to_showcmd(c);
663#endif
664
665getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {
668 /*
669 * Handle a count before a command and compute ca.count0.
670 * Note that '0' is a command and not the start of a count, but it's
671 * part of a count after other digits.
672 */
673 while ( (c >= '1' && c <= '9')
674 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
675 {
676 if (c == K_DEL || c == K_KDEL)
677 {
678 ca.count0 /= 10;
679#ifdef FEAT_CMDL_INFO
680 del_from_showcmd(4); /* delete the digit and ~@% */
681#endif
682 }
683 else
684 ca.count0 = ca.count0 * 10 + (c - '0');
685 if (ca.count0 < 0) /* got too large! */
686 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000687#ifdef FEAT_EVAL
688 /* Set v:count here, when called from main() and not a stuffed
689 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100690 * right after the count. Do set it for redo. */
691 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100692 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (ctrl_w)
695 {
696 ++no_mapping;
697 ++allow_keys; /* no mapping for nchar, but keys */
698 }
699 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000700 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 --no_zero_mapping;
703 if (ctrl_w)
704 {
705 --no_mapping;
706 --allow_keys;
707 }
708#ifdef FEAT_CMDL_INFO
709 need_flushbuf |= add_to_showcmd(c);
710#endif
711 }
712
713 /*
714 * If we got CTRL-W there may be a/another count
715 */
716 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
717 {
718 ctrl_w = TRUE;
719 ca.opcount = ca.count0; /* remember first count */
720 ca.count0 = 0;
721 ++no_mapping;
722 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000723 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 --no_mapping;
726 --allow_keys;
727#ifdef FEAT_CMDL_INFO
728 need_flushbuf |= add_to_showcmd(c);
729#endif
730 goto getcount; /* jump back */
731 }
732 }
733
Bram Moolenaara983fe92008-07-31 20:04:27 +0000734 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000736 /* Save the count values so that ca.opcount and ca.count0 are exactly
737 * the same when coming back here after handling K_CURSORHOLD. */
738 oap->prev_opcount = ca.opcount;
739 oap->prev_count0 = ca.count0;
740 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100741 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000742 {
743 /*
744 * If we're in the middle of an operator (including after entering a
745 * yank buffer with '"') AND we had a count before the operator, then
746 * that count overrides the current value of ca.count0.
747 * What this means effectively, is that commands like "3dw" get turned
748 * into "d3w" which makes things fall into place pretty neatly.
749 * If you give a count before AND after the operator, they are
750 * multiplied.
751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 if (ca.count0)
753 ca.count0 *= ca.opcount;
754 else
755 ca.count0 = ca.opcount;
756 }
757
758 /*
759 * Always remember the count. It will be set to zero (on the next call,
760 * above) when there is no pending operator.
761 * When called from main(), save the count for use by the "count" built-in
762 * variable.
763 */
764 ca.opcount = ca.count0;
765 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
766
767#ifdef FEAT_EVAL
768 /*
769 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100770 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100772 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000773 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
775
776 /*
777 * Find the command character in the table of commands.
778 * For CTRL-W we already got nchar when looking for a count.
779 */
780 if (ctrl_w)
781 {
782 ca.nchar = c;
783 ca.cmdchar = Ctrl_W;
784 }
785 else
786 ca.cmdchar = c;
787 idx = find_command(ca.cmdchar);
788 if (idx < 0)
789 {
790 /* Not a known command: beep. */
791 clearopbeep(oap);
792 goto normal_end;
793 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000794
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000795 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200797 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000799 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 goto normal_end;
801 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000802 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
803 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 /*
806 * In Visual/Select mode, a few keys are handled in a special way.
807 */
808 if (VIsual_active)
809 {
810 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
811 if (km_stopsel
812 && (nv_cmds[idx].cmd_flags & NV_STS)
813 && !(mod_mask & MOD_MASK_SHIFT))
814 {
815 end_visual_mode();
816 redraw_curbuf_later(INVERTED);
817 }
818
819 /* Keys that work different when 'keymodel' contains "startsel" */
820 if (km_startsel)
821 {
822 if (nv_cmds[idx].cmd_flags & NV_SS)
823 {
824 unshift_special(&ca);
825 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000826 if (idx < 0)
827 {
828 /* Just in case */
829 clearopbeep(oap);
830 goto normal_end;
831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
834 && (mod_mask & MOD_MASK_SHIFT))
835 {
836 mod_mask &= ~MOD_MASK_SHIFT;
837 }
838 }
839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
841#ifdef FEAT_RIGHTLEFT
842 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
843 && (nv_cmds[idx].cmd_flags & NV_RL))
844 {
845 /* Invert horizontal movements and operations. Only when typed by the
846 * user directly, not when the result of a mapping or "x" translated
847 * to "dl". */
848 switch (ca.cmdchar)
849 {
850 case 'l': ca.cmdchar = 'h'; break;
851 case K_RIGHT: ca.cmdchar = K_LEFT; break;
852 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
853 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
854 case 'h': ca.cmdchar = 'l'; break;
855 case K_LEFT: ca.cmdchar = K_RIGHT; break;
856 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
857 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
858 case '>': ca.cmdchar = '<'; break;
859 case '<': ca.cmdchar = '>'; break;
860 }
861 idx = find_command(ca.cmdchar);
862 }
863#endif
864
865 /*
866 * Get an additional character if we need one.
867 */
868 if ((nv_cmds[idx].cmd_flags & NV_NCH)
869 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
870 && oap->op_type == OP_NOP)
871 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
872 || (ca.cmdchar == 'q'
873 && oap->op_type == OP_NOP
874 && !Recording
875 && !Exec_reg)
876 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100877 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 {
879 int *cp;
880 int repl = FALSE; /* get character for replace mode */
881 int lit = FALSE; /* get extra character literally */
882 int langmap_active = FALSE; /* using :lmap mappings */
883 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100884#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 int save_smd; /* saved value of p_smd */
886#endif
887
888 ++no_mapping;
889 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000890 /* Don't generate a CursorHold event here, most commands can't handle
891 * it, e.g., nv_replace(), nv_csearch(). */
892 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (ca.cmdchar == 'g')
894 {
895 /*
896 * For 'g' get the next character now, so that we can check for
897 * "gr", "g'" and "g`".
898 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000899 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901#ifdef FEAT_CMDL_INFO
902 need_flushbuf |= add_to_showcmd(ca.nchar);
903#endif
904 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100905 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
907 cp = &ca.extra_char; /* need to get a third character */
908 if (ca.nchar != 'r')
909 lit = TRUE; /* get it literally */
910 else
911 repl = TRUE; /* get it in replace mode */
912 }
913 else
914 cp = NULL; /* no third character needed */
915 }
916 else
917 {
918 if (ca.cmdchar == 'r') /* get it in replace mode */
919 repl = TRUE;
920 cp = &ca.nchar;
921 }
922 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
923
924 /*
925 * Get a second or third character.
926 */
927 if (cp != NULL)
928 {
929#ifdef CURSOR_SHAPE
930 if (repl)
931 {
932 State = REPLACE; /* pretend Replace mode */
933 ui_cursor_shape(); /* show different cursor shape */
934 }
935#endif
936 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
937 {
938 /* Allow mappings defined with ":lmap". */
939 --no_mapping;
940 --allow_keys;
941 if (repl)
942 State = LREPLACE;
943 else
944 State = LANGMAP;
945 langmap_active = TRUE;
946 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100947#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 save_smd = p_smd;
949 p_smd = FALSE; /* Don't let the IM code show the mode here */
950 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
951 im_set_active(TRUE);
952#endif
953
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000954 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955
956 if (langmap_active)
957 {
958 /* Undo the decrement done above */
959 ++no_mapping;
960 ++allow_keys;
961 State = NORMAL_BUSY;
962 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100963#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (lang)
965 {
966 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
967 im_save_status(&curbuf->b_p_iminsert);
968 im_set_active(FALSE);
969 }
970 p_smd = save_smd;
971#endif
972#ifdef CURSOR_SHAPE
973 State = NORMAL_BUSY;
974#endif
975#ifdef FEAT_CMDL_INFO
976 need_flushbuf |= add_to_showcmd(*cp);
977#endif
978
979 if (!lit)
980 {
981#ifdef FEAT_DIGRAPHS
982 /* Typing CTRL-K gets a digraph. */
983 if (*cp == Ctrl_K
984 && ((nv_cmds[idx].cmd_flags & NV_LANG)
985 || cp == &ca.extra_char)
986 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
987 {
988 c = get_digraph(FALSE);
989 if (c > 0)
990 {
991 *cp = c;
992# ifdef FEAT_CMDL_INFO
993 /* Guessing how to update showcmd here... */
994 del_from_showcmd(3);
995 need_flushbuf |= add_to_showcmd(*cp);
996# endif
997 }
998 }
999#endif
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /* adjust chars > 127, except after "tTfFr" commands */
1002 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#ifdef FEAT_RIGHTLEFT
1004 /* adjust Hebrew mapped char */
1005 if (p_hkmap && lang && KeyTyped)
1006 *cp = hkmap(*cp);
1007# ifdef FEAT_FKMAP
1008 /* adjust Farsi mapped char */
1009 if (p_fkmap && lang && KeyTyped)
1010 *cp = fkmap(*cp);
1011# endif
1012#endif
1013 }
1014
1015 /*
1016 * When the next character is CTRL-\ a following CTRL-N means the
1017 * command is aborted and we go to Normal mode.
1018 */
1019 if (cp == &ca.extra_char
1020 && ca.nchar == Ctrl_BSL
1021 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1022 {
1023 ca.cmdchar = Ctrl_BSL;
1024 ca.nchar = ca.extra_char;
1025 idx = find_command(ca.cmdchar);
1026 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001027 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001028 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 else if (*cp == Ctrl_BSL)
1030 {
1031 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1032
1033 /* There is a busy wait here when typing "f<C-\>" and then
1034 * something different from CTRL-N. Can't be avoided. */
1035 while ((c = vpeekc()) <= 0 && towait > 0L)
1036 {
1037 do_sleep(towait > 50L ? 50L : towait);
1038 towait -= 50L;
1039 }
1040 if (c > 0)
1041 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001042 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (c != Ctrl_N && c != Ctrl_G)
1044 vungetc(c);
1045 else
1046 {
1047 ca.cmdchar = Ctrl_BSL;
1048 ca.nchar = c;
1049 idx = find_command(ca.cmdchar);
1050 }
1051 }
1052 }
1053
1054#ifdef FEAT_MBYTE
1055 /* When getting a text character and the next character is a
1056 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001057 * However, don't wait for it to arrive. Also, do enable mapping,
1058 * because if it's put back with vungetc() it's too late to apply
1059 * mapping. */
1060 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 while (enc_utf8 && lang && (c = vpeekc()) > 0
1062 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1063 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001064 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (!utf_iscomposing(c))
1066 {
1067 vungetc(c); /* it wasn't, put it back */
1068 break;
1069 }
1070 else if (ca.ncharC1 == 0)
1071 ca.ncharC1 = c;
1072 else
1073 ca.ncharC2 = c;
1074 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001075 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#endif
1077 }
1078 --no_mapping;
1079 --allow_keys;
1080 }
1081
1082#ifdef FEAT_CMDL_INFO
1083 /*
1084 * Flush the showcmd characters onto the screen so we can see them while
1085 * the command is being executed. Only do this when the shown command was
1086 * actually displayed, otherwise this will slow down a lot when executing
1087 * mappings.
1088 */
1089 if (need_flushbuf)
1090 out_flush();
1091#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001092 if (ca.cmdchar != K_IGNORE)
1093 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
1095 State = NORMAL;
1096
1097 if (ca.nchar == ESC)
1098 {
1099 clearop(oap);
1100 if (restart_edit == 0 && goto_im())
1101 restart_edit = 'a';
1102 goto normal_end;
1103 }
1104
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001105 if (ca.cmdchar != K_IGNORE)
1106 {
1107 msg_didout = FALSE; /* don't scroll screen up for normal command */
1108 msg_col = 0;
1109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 old_pos = curwin->w_cursor; /* remember where cursor was */
1112
1113 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1114 * mode. */
1115 if (!VIsual_active && km_startsel)
1116 {
1117 if (nv_cmds[idx].cmd_flags & NV_SS)
1118 {
1119 start_selection();
1120 unshift_special(&ca);
1121 idx = find_command(ca.cmdchar);
1122 }
1123 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1124 && (mod_mask & MOD_MASK_SHIFT))
1125 {
1126 start_selection();
1127 mod_mask &= ~MOD_MASK_SHIFT;
1128 }
1129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
1131 /*
1132 * Execute the command!
1133 * Call the command function found in the commands table.
1134 */
1135 ca.arg = nv_cmds[idx].cmd_arg;
1136 (nv_cmds[idx].cmd_func)(&ca);
1137
1138 /*
1139 * If we didn't start or finish an operator, reset oap->regname, unless we
1140 * need it later.
1141 */
1142 if (!finish_op
1143 && !oap->op_type
1144 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1145 {
1146 clearop(oap);
1147#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001148 {
1149 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001150
Bram Moolenaar536681b2011-05-10 16:12:45 +02001151 /* Adjust the register according to 'clipboard', so that when
1152 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001153# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001154 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001155# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001156 set_reg_var(regname);
1157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158#endif
1159 }
1160
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001161 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001162 * character or "z333<cr>". */
1163 if (old_mapped_len > 0)
1164 old_mapped_len = typebuf_maplen();
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 /*
1167 * If an operation is pending, handle it...
1168 */
1169 do_pending_operator(&ca, old_col, FALSE);
1170
1171 /*
1172 * Wait for a moment when a message is displayed that will be overwritten
1173 * by the mode message.
1174 * In Visual mode and with "^O" in Insert mode, a short message will be
1175 * overwritten by the mode message. Wait a bit, until a key is hit.
1176 * In Visual mode, it's more important to keep the Visual area updated
1177 * than keeping a message (e.g. from a /pat search).
1178 * Only do this if the command was typed, not from a mapping.
1179 * Don't wait when emsg_silent is non-zero.
1180 * Also wait a bit after an error message, e.g. for "^O:".
1181 * Don't redraw the screen, it would remove the message.
1182 */
1183 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001184 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 || (VIsual_active
1187 && old_pos.lnum == curwin->w_cursor.lnum
1188 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 )
1190 && (clear_cmdline
1191 || redraw_cmdline)
1192 && (msg_didout || (msg_didany && msg_scroll))
1193 && !msg_nowait
1194 && KeyTyped)
1195 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 && (msg_scroll
1198 || emsg_on_display)))
1199 && oap->regname == 0
1200 && !(ca.retval & CA_COMMAND_BUSY)
1201 && stuff_empty()
1202 && typebuf_typed()
1203 && emsg_silent == 0
1204 && !did_wait_return
1205 && oap->op_type == OP_NOP)
1206 {
1207 int save_State = State;
1208
1209 /* Draw the cursor with the right shape here */
1210 if (restart_edit != 0)
1211 State = INSERT;
1212
1213 /* If need to redraw, and there is a "keep_msg", redraw before the
1214 * delay */
1215 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1216 {
1217 char_u *kmsg;
1218
1219 kmsg = keep_msg;
1220 keep_msg = NULL;
1221 /* showmode() will clear keep_msg, but we want to use it anyway */
1222 update_screen(0);
1223 /* now reset it, otherwise it's put in the history again */
1224 keep_msg = kmsg;
1225 msg_attr(kmsg, keep_msg_attr);
1226 vim_free(kmsg);
1227 }
1228 setcursor();
1229 cursor_on();
1230 out_flush();
1231 if (msg_scroll || emsg_on_display)
1232 ui_delay(1000L, TRUE); /* wait at least one second */
1233 ui_delay(3000L, FALSE); /* wait up to three seconds */
1234 State = save_State;
1235
1236 msg_scroll = FALSE;
1237 emsg_on_display = FALSE;
1238 }
1239
1240 /*
1241 * Finish up after executing a Normal mode command.
1242 */
1243normal_end:
1244
1245 msg_nowait = FALSE;
1246
1247 /* Reset finish_op, in case it was set */
1248#ifdef CURSOR_SHAPE
1249 c = finish_op;
1250#endif
1251 finish_op = FALSE;
1252#ifdef CURSOR_SHAPE
1253 /* Redraw the cursor with another shape, if we were in Operator-pending
1254 * mode or did a replace command. */
1255 if (c || ca.cmdchar == 'r')
1256 {
1257 ui_cursor_shape(); /* may show different cursor shape */
1258# ifdef FEAT_MOUSESHAPE
1259 update_mouseshape(-1);
1260# endif
1261 }
1262#endif
1263
1264#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001265 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001266 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 clear_showcmd();
1268#endif
1269
1270 checkpcmark(); /* check if we moved since setting pcmark */
1271 vim_free(ca.searchbuf);
1272
1273#ifdef FEAT_MBYTE
1274 if (has_mbyte)
1275 mb_adjust_cursor();
1276#endif
1277
1278#ifdef FEAT_SCROLLBIND
1279 if (curwin->w_p_scb && toplevel)
1280 {
1281 validate_cursor(); /* may need to update w_leftcol */
1282 do_check_scrollbind(TRUE);
1283 }
1284#endif
1285
Bram Moolenaar860cae12010-06-05 23:22:07 +02001286#ifdef FEAT_CURSORBIND
1287 if (curwin->w_p_crb && toplevel)
1288 {
1289 validate_cursor(); /* may need to update w_leftcol */
1290 do_check_cursorbind();
1291 }
1292#endif
1293
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001294#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001295 /* don't go to Insert mode if a terminal has a running job */
1296 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001297 restart_edit = 0;
1298#endif
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 /*
1301 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1302 * if still inside a mapping that started in Visual mode).
1303 * May switch from Visual to Select mode after CTRL-O command.
1304 */
1305 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1307 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 && !(ca.retval & CA_COMMAND_BUSY)
1309 && stuff_empty()
1310 && oap->regname == 0)
1311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 if (restart_VIsual_select == 1)
1313 {
1314 VIsual_select = TRUE;
1315 showmode();
1316 restart_VIsual_select = 0;
1317 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001318 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 (void)edit(restart_edit, FALSE, 1L);
1320 }
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (restart_VIsual_select == 2)
1323 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
1325 /* Save count before an operator for next time. */
1326 opcount = ca.opcount;
1327}
1328
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001329#ifdef FEAT_EVAL
1330/*
1331 * Set v:count and v:count1 according to "cap".
1332 * Set v:prevcount only when "set_prevcount" is TRUE.
1333 */
1334 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001335set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001336{
1337 long count = cap->count0;
1338
1339 /* multiply with cap->opcount the same way as above */
1340 if (cap->opcount != 0)
1341 count = cap->opcount * (count == 0 ? 1 : count);
1342 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1343 *set_prevcount = FALSE; /* only set v:prevcount once */
1344}
1345#endif
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347/*
1348 * Handle an operator after visual mode or when the movement is finished
1349 */
1350 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001351do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 oparg_T *oap = cap->oap;
1354 pos_T old_cursor;
1355 int empty_region_error;
1356 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001357#ifdef FEAT_LINEBREAK
1358 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 /* The visual area is remembered for redo */
1362 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1363 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001364 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001366 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001367#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369#endif
1370
1371#if defined(FEAT_CLIPBOARD)
1372 /*
1373 * Yank the visual area into the GUI selection register before we operate
1374 * on it and lose it forever.
1375 * Don't do it if a specific register was specified, so that ""x"*P works.
1376 * This could call do_pending_operator() recursively, but that's OK
1377 * because gui_yank will be TRUE for the nested call.
1378 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001379 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 && oap->op_type != OP_NOP
1381 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 && VIsual_active
1383 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 && oap->regname == 0)
1385 clip_auto_select();
1386#endif
1387 old_cursor = curwin->w_cursor;
1388
1389 /*
1390 * If an operation is pending, handle it...
1391 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001392 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 {
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001394#ifdef FEAT_LINEBREAK
1395 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001396 if (curwin->w_p_lbr)
1397 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001398 curwin->w_p_lbr = FALSE;
1399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 oap->is_VIsual = VIsual_active;
1401 if (oap->motion_force == 'V')
1402 oap->motion_type = MLINE;
1403 else if (oap->motion_force == 'v')
1404 {
1405 /* If the motion was linewise, "inclusive" will not have been set.
1406 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1407 if (oap->motion_type == MLINE)
1408 oap->inclusive = FALSE;
1409 /* If the motion already was characterwise, toggle "inclusive" */
1410 else if (oap->motion_type == MCHAR)
1411 oap->inclusive = !oap->inclusive;
1412 oap->motion_type = MCHAR;
1413 }
1414 else if (oap->motion_force == Ctrl_V)
1415 {
1416 /* Change line- or characterwise motion into Visual block mode. */
1417 VIsual_active = TRUE;
1418 VIsual = oap->start;
1419 VIsual_mode = Ctrl_V;
1420 VIsual_select = FALSE;
1421 VIsual_reselect = FALSE;
1422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
Bram Moolenaar7afea822013-04-24 18:34:45 +02001424 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1425 /* Never redo "zf" (define fold). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001427 && ((!VIsual_active || oap->motion_force)
1428 /* Also redo Operator-pending Visual mode mappings */
1429 || (VIsual_active && cap->cmdchar == ':'
1430 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001431 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432#ifdef FEAT_FOLDING
1433 && oap->op_type != OP_FOLD
1434 && oap->op_type != OP_FOLDOPEN
1435 && oap->op_type != OP_FOLDOPENREC
1436 && oap->op_type != OP_FOLDCLOSE
1437 && oap->op_type != OP_FOLDCLOSEREC
1438 && oap->op_type != OP_FOLDDEL
1439 && oap->op_type != OP_FOLDDELREC
1440#endif
1441 )
1442 {
1443 prep_redo(oap->regname, cap->count0,
1444 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1445 oap->motion_force, cap->cmdchar, cap->nchar);
1446 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1447 {
1448 /*
1449 * If 'cpoptions' does not contain 'r', insert the search
1450 * pattern to really repeat the same command.
1451 */
1452 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001453 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 AppendToRedobuff(NL_STR);
1455 }
1456 else if (cap->cmdchar == ':')
1457 {
1458 /* do_cmdline() has stored the first typed line in
1459 * "repeat_cmdline". When several lines are typed repeating
1460 * won't be possible. */
1461 if (repeat_cmdline == NULL)
1462 ResetRedobuff();
1463 else
1464 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001465 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001467 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
1469 }
1470 }
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 if (redo_VIsual_busy)
1473 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001474 /* Redo of an operation on a Visual area. Use the same size from
1475 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 oap->start = curwin->w_cursor;
1477 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1478 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1479 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1480 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001481 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001483 if (VIsual_mode == 'v')
1484 {
1485 if (redo_VIsual_line_count <= 1)
1486 {
1487 validate_virtcol();
1488 curwin->w_curswant =
1489 curwin->w_virtcol + redo_VIsual_vcol - 1;
1490 }
1491 else
1492 curwin->w_curswant = redo_VIsual_vcol;
1493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001495 {
1496 curwin->w_curswant = MAXCOL;
1497 }
1498 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 cap->count0 = redo_VIsual_count;
1501 if (redo_VIsual_count != 0)
1502 cap->count1 = redo_VIsual_count;
1503 else
1504 cap->count1 = 1;
1505 }
1506 else if (VIsual_active)
1507 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001508 if (!gui_yank)
1509 {
1510 /* Save the current VIsual area for '< and '> marks, and "gv" */
1511 curbuf->b_visual.vi_start = VIsual;
1512 curbuf->b_visual.vi_end = curwin->w_cursor;
1513 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001514 if (VIsual_mode_orig != NUL)
1515 {
1516 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1517 VIsual_mode_orig = NUL;
1518 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001519 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001521 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
1525 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001526 * characterwise selection.
1527 * Special case: gH<Del> deletes the last line. */
1528 if (VIsual_select && VIsual_mode == 'V'
1529 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001531 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 VIsual.col = 0;
1534 curwin->w_cursor.col =
1535 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1536 }
1537 else
1538 {
1539 curwin->w_cursor.col = 0;
1540 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1541 }
1542 VIsual_mode = 'v';
1543 }
1544 /* If 'selection' is "exclusive", backup one character for
1545 * charwise selections. */
1546 else if (VIsual_mode == 'v')
1547 {
1548# ifdef FEAT_VIRTUALEDIT
1549 include_line_break =
1550# endif
1551 unadjust_for_sel();
1552 }
1553
1554 oap->start = VIsual;
1555 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001556 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001558# ifdef FEAT_VIRTUALEDIT
1559 oap->start.coladd = 0;
1560# endif
1561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
1564 /*
1565 * Set oap->start to the first position of the operated text, oap->end
1566 * to the end of the operated text. w_cursor is equal to oap->start.
1567 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001568 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570#ifdef FEAT_FOLDING
1571 /* Include folded lines completely. */
1572 if (!VIsual_active)
1573 {
1574 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1575 oap->start.col = 0;
1576 if (hasFolding(curwin->w_cursor.lnum, NULL,
1577 &curwin->w_cursor.lnum))
1578 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1579 }
1580#endif
1581 oap->end = curwin->w_cursor;
1582 curwin->w_cursor = oap->start;
1583
1584 /* w_virtcol may have been updated; if the cursor goes back to its
1585 * previous position w_virtcol becomes invalid and isn't updated
1586 * automatically. */
1587 curwin->w_valid &= ~VALID_VIRTCOL;
1588 }
1589 else
1590 {
1591#ifdef FEAT_FOLDING
1592 /* Include folded lines completely. */
1593 if (!VIsual_active && oap->motion_type == MLINE)
1594 {
1595 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1596 NULL))
1597 curwin->w_cursor.col = 0;
1598 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1599 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1600 }
1601#endif
1602 oap->end = oap->start;
1603 oap->start = curwin->w_cursor;
1604 }
1605
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001606 /* Just in case lines were deleted that make the position invalid. */
1607 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1609
1610#ifdef FEAT_VIRTUALEDIT
1611 /* Set "virtual_op" before resetting VIsual_active. */
1612 virtual_op = virtual_active();
1613#endif
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (VIsual_active || redo_VIsual_busy)
1616 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001617 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
1619 if (!redo_VIsual_busy && !gui_yank)
1620 {
1621 /*
1622 * Prepare to reselect and redo Visual: this is based on the
1623 * size of the Visual text
1624 */
1625 resel_VIsual_mode = VIsual_mode;
1626 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001627 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001629 {
1630 if (VIsual_mode != Ctrl_V)
1631 getvvcol(curwin, &(oap->end),
1632 NULL, NULL, &oap->end_vcol);
1633 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1634 {
1635 if (VIsual_mode != Ctrl_V)
1636 getvvcol(curwin, &(oap->start),
1637 &oap->start_vcol, NULL, NULL);
1638 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1639 }
1640 else
1641 resel_VIsual_vcol = oap->end_vcol;
1642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 resel_VIsual_line_count = oap->line_count;
1644 }
1645
1646 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1647 if ((vim_strchr(p_cpo, CPO_YANK) != NULL || oap->op_type != OP_YANK)
1648 && oap->op_type != OP_COLON
1649#ifdef FEAT_FOLDING
1650 && oap->op_type != OP_FOLD
1651 && oap->op_type != OP_FOLDOPEN
1652 && oap->op_type != OP_FOLDOPENREC
1653 && oap->op_type != OP_FOLDCLOSE
1654 && oap->op_type != OP_FOLDCLOSEREC
1655 && oap->op_type != OP_FOLDDEL
1656 && oap->op_type != OP_FOLDDELREC
1657#endif
1658 && oap->motion_force == NUL
1659 )
1660 {
1661 /* Prepare for redoing. Only use the nchar field for "r",
1662 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001663 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1664 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001665 prep_redo(oap->regname, cap->count0,
1666 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1667 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001668 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001669 {
1670 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1671
1672 /* reverse what nv_replace() did */
1673 if (nchar == REPLACE_CR_NCHAR)
1674 nchar = CAR;
1675 else if (nchar == REPLACE_NL_NCHAR)
1676 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001677 prep_redo(oap->regname, 0L, NUL, 'v',
1678 get_op_char(oap->op_type),
1679 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001680 nchar);
1681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 if (!redo_VIsual_busy)
1683 {
1684 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001685 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 redo_VIsual_line_count = resel_VIsual_line_count;
1687 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001688 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 }
1690 }
1691
1692 /*
1693 * oap->inclusive defaults to TRUE.
1694 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1695 * FALSE. This makes "d}P" and "v}dP" work the same.
1696 */
1697 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1698 oap->inclusive = TRUE;
1699 if (VIsual_mode == 'V')
1700 oap->motion_type = MLINE;
1701 else
1702 {
1703 oap->motion_type = MCHAR;
1704 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001705#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 )
1709 {
1710 oap->inclusive = FALSE;
1711 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001712 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001713 if (*p_sel != 'o'
1714 && !op_on_lines(oap->op_type)
1715 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001717 ++oap->end.lnum;
1718 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001719#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001720 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001721#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001722 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
1724 }
1725 }
1726
1727 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /*
1730 * Switch Visual off now, so screen updating does
1731 * not show inverted text when the screen is redrawn.
1732 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1733 * no screen redraw, so it is done here to remove the inverted
1734 * part.
1735 */
1736 if (!gui_yank)
1737 {
1738 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001739#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 setmouse();
1741 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001742#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001743 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if ((oap->op_type == OP_YANK
1745 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001746 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 || oap->op_type == OP_FILTER)
1748 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001749 {
1750#ifdef FEAT_LINEBREAK
1751 /* make sure redrawing is correct */
1752 curwin->w_p_lbr = lbr_saved;
1753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 }
1757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
1759#ifdef FEAT_MBYTE
1760 /* Include the trailing byte of a multi-byte char. */
1761 if (has_mbyte && oap->inclusive)
1762 {
1763 int l;
1764
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001765 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 if (l > 1)
1767 oap->end.col += l - 1;
1768 }
1769#endif
1770 curwin->w_set_curswant = TRUE;
1771
1772 /*
1773 * oap->empty is set when start and end are the same. The inclusive
1774 * flag affects this too, unless yanking and the end is on a NUL.
1775 */
1776 oap->empty = (oap->motion_type == MCHAR
1777 && (!oap->inclusive
1778 || (oap->op_type == OP_YANK
1779 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001780 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#ifdef FEAT_VIRTUALEDIT
1782 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1783#endif
1784 );
1785 /*
1786 * For delete, change and yank, it's an error to operate on an
1787 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1788 */
1789 empty_region_error = (oap->empty
1790 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 /* Force a redraw when operating on an empty Visual region, when
1793 * 'modifiable is off or creating a fold. */
1794 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001795#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001799 {
1800#ifdef FEAT_LINEBREAK
1801 curwin->w_p_lbr = lbr_saved;
1802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
1806 /*
1807 * If the end of an operator is in column one while oap->motion_type
1808 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1809 * character in the previous line. If op_start is on or before the
1810 * first non-blank in the line, the operator becomes linewise
1811 * (strange, but that's the way vi does it).
1812 */
1813 if ( oap->motion_type == MCHAR
1814 && oap->inclusive == FALSE
1815 && !(cap->retval & CA_NO_ADJ_OP_END)
1816 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001818 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 && oap->line_count > 1)
1820 {
1821 oap->end_adjusted = TRUE; /* remember that we did this */
1822 --oap->line_count;
1823 --oap->end.lnum;
1824 if (inindent(0))
1825 oap->motion_type = MLINE;
1826 else
1827 {
1828 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1829 if (oap->end.col)
1830 {
1831 --oap->end.col;
1832 oap->inclusive = TRUE;
1833 }
1834 }
1835 }
1836 else
1837 oap->end_adjusted = FALSE;
1838
1839 switch (oap->op_type)
1840 {
1841 case OP_LSHIFT:
1842 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001843 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 auto_format(FALSE, TRUE);
1845 break;
1846
1847 case OP_JOIN_NS:
1848 case OP_JOIN:
1849 if (oap->line_count < 2)
1850 oap->line_count = 2;
1851 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1852 curbuf->b_ml.ml_line_count)
1853 beep_flush();
1854 else
1855 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001856 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001857 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 auto_format(FALSE, TRUE);
1859 }
1860 break;
1861
1862 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001865 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001866 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001867 CancelRedo();
1868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 else
1870 {
1871 (void)op_delete(oap);
1872 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1873 u_save_cursor(); /* cursor line wasn't saved yet */
1874 auto_format(FALSE, TRUE);
1875 }
1876 break;
1877
1878 case OP_YANK:
1879 if (empty_region_error)
1880 {
1881 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001882 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001883 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001884 CancelRedo();
1885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 }
1887 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001888 {
1889#ifdef FEAT_LINEBREAK
1890 curwin->w_p_lbr = lbr_saved;
1891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 check_cursor_col();
1895 break;
1896
1897 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if (empty_region_error)
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 else
1905 {
1906 /* This is a new edit command, not a restart. Need to
1907 * remember it to make 'insertmode' work with mappings for
1908 * Visual mode. But do this only once and not when typed and
1909 * 'insertmode' isn't set. */
1910 if (p_im || !KeyTyped)
1911 restart_edit_save = restart_edit;
1912 else
1913 restart_edit_save = 0;
1914 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001915#ifdef FEAT_LINEBREAK
1916 /* Restore linebreak, so that when the user edits it looks as
1917 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001918 if (curwin->w_p_lbr != lbr_saved)
1919 {
1920 curwin->w_p_lbr = lbr_saved;
1921 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1922 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 /* Reset finish_op now, don't want it set inside edit(). */
1925 finish_op = FALSE;
1926 if (op_change(oap)) /* will call edit() */
1927 cap->retval |= CA_COMMAND_BUSY;
1928 if (restart_edit == 0)
1929 restart_edit = restart_edit_save;
1930 }
1931 break;
1932
1933 case OP_FILTER:
1934 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1935 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1936 else
1937 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001938 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
1940 case OP_INDENT:
1941 case OP_COLON:
1942
1943#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1944 /*
1945 * If 'equalprg' is empty, do the indenting internally.
1946 */
1947 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1948 {
1949# ifdef FEAT_LISP
1950 if (curbuf->b_p_lisp)
1951 {
1952 op_reindent(oap, get_lisp_indent);
1953 break;
1954 }
1955# endif
1956# ifdef FEAT_CINDENT
1957 op_reindent(oap,
1958# ifdef FEAT_EVAL
1959 *curbuf->b_p_inde != NUL ? get_expr_indent :
1960# endif
1961 get_c_indent);
1962 break;
1963# endif
1964 }
1965#endif
1966
1967 op_colon(oap);
1968 break;
1969
1970 case OP_TILDE:
1971 case OP_UPPER:
1972 case OP_LOWER:
1973 case OP_ROT13:
1974 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001975 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001976 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001977 CancelRedo();
1978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 else
1980 op_tilde(oap);
1981 check_cursor_col();
1982 break;
1983
1984 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001985#if defined(FEAT_EVAL)
1986 if (*curbuf->b_p_fex != NUL)
1987 op_formatexpr(oap); /* use expression */
1988 else
1989#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001990 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 op_colon(oap); /* use external command */
1992 else
1993 op_format(oap, FALSE); /* use internal function */
1994 break;
1995
1996 case OP_FORMAT2:
1997 op_format(oap, TRUE); /* use internal function */
1998 break;
1999
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002000 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01002001#ifdef FEAT_LINEBREAK
2002 /* Restore linebreak, so that when the user edits it looks as
2003 * before. */
2004 curwin->w_p_lbr = lbr_saved;
2005#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002006 op_function(oap); /* call 'operatorfunc' */
2007 break;
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 case OP_INSERT:
2010 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012#ifdef FEAT_VISUALEXTRA
2013 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002014 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002015 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002016 CancelRedo();
2017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 else
2019 {
2020 /* This is a new edit command, not a restart. Need to
2021 * remember it to make 'insertmode' work with mappings for
2022 * Visual mode. But do this only once. */
2023 restart_edit_save = restart_edit;
2024 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002025#ifdef FEAT_LINEBREAK
2026 /* Restore linebreak, so that when the user edits it looks as
2027 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002028 if (curwin->w_p_lbr != lbr_saved)
2029 {
2030 curwin->w_p_lbr = lbr_saved;
2031 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2032 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002035#ifdef FEAT_LINEBREAK
2036 /* Reset linebreak, so that formatting works correctly. */
2037 curwin->w_p_lbr = FALSE;
2038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
2040 /* TODO: when inserting in several lines, should format all
2041 * the lines. */
2042 auto_format(FALSE, TRUE);
2043
2044 if (restart_edit == 0)
2045 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002046 else
2047 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002050 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051#endif
2052 break;
2053
2054 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056#ifdef FEAT_VISUALEXTRA
2057 if (empty_region_error)
2058#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002059 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002060 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002061 CancelRedo();
2062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063#ifdef FEAT_VISUALEXTRA
2064 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002065 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002066# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002067 /* Restore linebreak, so that when the user edits it looks as
2068 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002069 if (curwin->w_p_lbr != lbr_saved)
2070 {
2071 curwin->w_p_lbr = lbr_saved;
2072 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2073 }
2074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#endif
2078 break;
2079
2080#ifdef FEAT_FOLDING
2081 case OP_FOLD:
2082 VIsual_reselect = FALSE; /* don't reselect now */
2083 foldCreate(oap->start.lnum, oap->end.lnum);
2084 break;
2085
2086 case OP_FOLDOPEN:
2087 case OP_FOLDOPENREC:
2088 case OP_FOLDCLOSE:
2089 case OP_FOLDCLOSEREC:
2090 VIsual_reselect = FALSE; /* don't reselect now */
2091 opFoldRange(oap->start.lnum, oap->end.lnum,
2092 oap->op_type == OP_FOLDOPEN
2093 || oap->op_type == OP_FOLDOPENREC,
2094 oap->op_type == OP_FOLDOPENREC
2095 || oap->op_type == OP_FOLDCLOSEREC,
2096 oap->is_VIsual);
2097 break;
2098
2099 case OP_FOLDDEL:
2100 case OP_FOLDDELREC:
2101 VIsual_reselect = FALSE; /* don't reselect now */
2102 deleteFold(oap->start.lnum, oap->end.lnum,
2103 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2104 break;
2105#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002106 case OP_NR_ADD:
2107 case OP_NR_SUB:
2108 if (empty_region_error)
2109 {
2110 vim_beep(BO_OPER);
2111 CancelRedo();
2112 }
2113 else
2114 {
2115 VIsual_active = TRUE;
2116#ifdef FEAT_LINEBREAK
2117 curwin->w_p_lbr = lbr_saved;
2118#endif
2119 op_addsub(oap, cap->count1, redo_VIsual_arg);
2120 VIsual_active = FALSE;
2121 }
2122 check_cursor_col();
2123 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 default:
2125 clearopbeep(oap);
2126 }
2127#ifdef FEAT_VIRTUALEDIT
2128 virtual_op = MAYBE;
2129#endif
2130 if (!gui_yank)
2131 {
2132 /*
2133 * if 'sol' not set, go back to old column for some commands
2134 */
2135 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2136 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2137 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002138 {
2139#ifdef FEAT_LINEBREAK
2140 curwin->w_p_lbr = FALSE;
2141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145 else
2146 {
2147 curwin->w_cursor = old_cursor;
2148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 clearop(oap);
2151 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002152#ifdef FEAT_LINEBREAK
2153 curwin->w_p_lbr = lbr_saved;
2154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155}
2156
2157/*
2158 * Handle indent and format operators and visual mode ":".
2159 */
2160 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002161op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162{
2163 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (oap->is_VIsual)
2165 stuffReadbuff((char_u *)"'<,'>");
2166 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
2168 /*
2169 * Make the range look nice, so it can be repeated.
2170 */
2171 if (oap->start.lnum == curwin->w_cursor.lnum)
2172 stuffcharReadbuff('.');
2173 else
2174 stuffnumReadbuff((long)oap->start.lnum);
2175 if (oap->end.lnum != oap->start.lnum)
2176 {
2177 stuffcharReadbuff(',');
2178 if (oap->end.lnum == curwin->w_cursor.lnum)
2179 stuffcharReadbuff('.');
2180 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2181 stuffcharReadbuff('$');
2182 else if (oap->start.lnum == curwin->w_cursor.lnum)
2183 {
2184 stuffReadbuff((char_u *)".+");
2185 stuffnumReadbuff((long)oap->line_count - 1);
2186 }
2187 else
2188 stuffnumReadbuff((long)oap->end.lnum);
2189 }
2190 }
2191 if (oap->op_type != OP_COLON)
2192 stuffReadbuff((char_u *)"!");
2193 if (oap->op_type == OP_INDENT)
2194 {
2195#ifndef FEAT_CINDENT
2196 if (*get_equalprg() == NUL)
2197 stuffReadbuff((char_u *)"indent");
2198 else
2199#endif
2200 stuffReadbuff(get_equalprg());
2201 stuffReadbuff((char_u *)"\n");
2202 }
2203 else if (oap->op_type == OP_FORMAT)
2204 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002205 if (*curbuf->b_p_fp != NUL)
2206 stuffReadbuff(curbuf->b_p_fp);
2207 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002209 else
2210 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002211 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 }
2213
2214 /*
2215 * do_cmdline() does the rest
2216 */
2217}
2218
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002219/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002220 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002221 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002223op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002224{
2225#ifdef FEAT_EVAL
2226 char_u *(argv[1]);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002227# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002228 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002229# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002230
2231 if (*p_opfunc == NUL)
2232 EMSG(_("E774: 'operatorfunc' is empty"));
2233 else
2234 {
2235 /* Set '[ and '] marks to text to be operated on. */
2236 curbuf->b_op_start = oap->start;
2237 curbuf->b_op_end = oap->end;
2238 if (oap->motion_type != MLINE && !oap->inclusive)
2239 /* Exclude the end position. */
2240 decl(&curbuf->b_op_end);
2241
2242 if (oap->block_mode)
2243 argv[0] = (char_u *)"block";
2244 else if (oap->motion_type == MLINE)
2245 argv[0] = (char_u *)"line";
2246 else
2247 argv[0] = (char_u *)"char";
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002248
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002249# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002250 /* Reset virtual_op so that 'virtualedit' can be changed in the
2251 * function. */
2252 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002253# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002254
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002255 (void)call_func_retnr(p_opfunc, 1, argv, FALSE);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002256
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002257# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002258 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002259# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002260 }
2261#else
2262 EMSG(_("E775: Eval feature not available"));
2263#endif
2264}
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266#if defined(FEAT_MOUSE) || defined(PROTO)
2267/*
2268 * Do the appropriate action for the current mouse click in the current mode.
2269 * Not used for Command-line mode.
2270 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002271 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 * event modi- position visual change action
2273 * fier cursor window
2274 * left press - yes end yes
2275 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002276 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 * left drag - yes start if moved no
2278 * left relse - yes start if moved no
2279 * middle press - yes if not active no put register
2280 * middle press - yes if active no yank and put
2281 * right press - yes start or extend yes
2282 * right press S yes no change yes "#" (2)
2283 * right drag - yes extend no
2284 * right relse - yes extend no
2285 *
2286 * Insert or Replace Mode:
2287 * event modi- position visual change action
2288 * fier cursor window
2289 * left press - yes (cannot be active) yes
2290 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2291 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2292 * left drag - yes start or extend (1) no CTRL-O (1)
2293 * left relse - yes start or extend (1) no CTRL-O (1)
2294 * middle press - no (cannot be active) no put register
2295 * right press - yes start or extend yes CTRL-O
2296 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2297 *
2298 * (1) only if mouse pointer moved since press
2299 * (2) only if click is in same buffer
2300 *
2301 * Return TRUE if start_arrow() should be called for edit mode.
2302 */
2303 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002304do_mouse(
2305 oparg_T *oap, /* operator argument, can be NULL */
2306 int c, /* K_LEFTMOUSE, etc */
2307 int dir, /* Direction to 'put' if necessary */
2308 long count,
2309 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
2311 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2312 static int got_click = FALSE; /* got a click some time back */
2313
2314 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2315 int is_click; /* If FALSE it's a drag or release event */
2316 int is_drag; /* If TRUE it's a drag event */
2317 int jump_flags = 0; /* flags for jump_to_mouse() */
2318 pos_T start_visual;
2319 int moved; /* Has cursor moved? */
2320 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002321 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 int c1, c2;
2324#if defined(FEAT_FOLDING)
2325 pos_T save_cursor;
2326#endif
2327 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 static pos_T orig_cursor;
2329 colnr_T leftcol, rightcol;
2330 pos_T end_visual;
2331 int diff;
2332 int old_active = VIsual_active;
2333 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 int regname;
2335
2336#if defined(FEAT_FOLDING)
2337 save_cursor = curwin->w_cursor;
2338#endif
2339
2340 /*
2341 * When GUI is active, always recognize mouse events, otherwise:
2342 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2343 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2344 * - For command line and insert mode 'mouse' is checked before calling
2345 * do_mouse().
2346 */
2347 if (do_always)
2348 do_always = FALSE;
2349 else
2350#ifdef FEAT_GUI
2351 if (!gui.in_use)
2352#endif
2353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 if (VIsual_active)
2355 {
2356 if (!mouse_has(MOUSE_VISUAL))
2357 return FALSE;
2358 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002359 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 return FALSE;
2361 }
2362
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002363 for (;;)
2364 {
2365 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2366 if (is_drag)
2367 {
2368 /* If the next character is the same mouse event then use that
2369 * one. Speeds up dragging the status line. */
2370 if (vpeekc() != NUL)
2371 {
2372 int nc;
2373 int save_mouse_row = mouse_row;
2374 int save_mouse_col = mouse_col;
2375
2376 /* Need to get the character, peeking doesn't get the actual
2377 * one. */
2378 nc = safe_vgetc();
2379 if (c == nc)
2380 continue;
2381 vungetc(nc);
2382 mouse_row = save_mouse_row;
2383 mouse_col = save_mouse_col;
2384 }
2385 }
2386 break;
2387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002389 if (c == K_MOUSEMOVE)
2390 {
2391 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002392#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002393 ui_may_remove_balloon();
2394 if (p_bevalterm && !VIsual_active)
2395 {
2396 profile_setlimit(p_bdlay, &bevalexpr_due);
2397 bevalexpr_due_set = TRUE;
2398 }
2399#endif
2400 return FALSE;
2401 }
2402
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403#ifdef FEAT_MOUSESHAPE
2404 /* May have stopped dragging the status or separator line. The pointer is
2405 * most likely still on the status or separator line. */
2406 if (!is_drag && drag_status_line)
2407 {
2408 drag_status_line = FALSE;
2409 update_mouseshape(SHAPE_IDX_STATUS);
2410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (!is_drag && drag_sep_line)
2412 {
2413 drag_sep_line = FALSE;
2414 update_mouseshape(SHAPE_IDX_VSEP);
2415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416#endif
2417
2418 /*
2419 * Ignore drag and release events if we didn't get a click.
2420 */
2421 if (is_click)
2422 got_click = TRUE;
2423 else
2424 {
2425 if (!got_click) /* didn't get click, ignore */
2426 return FALSE;
2427 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002430 if (in_tab_line)
2431 {
2432 in_tab_line = FALSE;
2433 return FALSE;
2434 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 /*
2439 * CTRL right mouse button does CTRL-T
2440 */
2441 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2442 {
2443 if (State & INSERT)
2444 stuffcharReadbuff(Ctrl_O);
2445 if (count > 1)
2446 stuffnumReadbuff(count);
2447 stuffcharReadbuff(Ctrl_T);
2448 got_click = FALSE; /* ignore drag&release now */
2449 return FALSE;
2450 }
2451
2452 /*
2453 * CTRL only works with left mouse button
2454 */
2455 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2456 return FALSE;
2457
2458 /*
2459 * When a modifier is down, ignore drag and release events, as well as
2460 * multiple clicks and the middle mouse button.
2461 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2462 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002463 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2464 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 && (!is_click
2466 || (mod_mask & MOD_MASK_MULTI_CLICK)
2467 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002468 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 && mouse_model_popup()
2470 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002471 && !((mod_mask & MOD_MASK_ALT)
2472 && !mouse_model_popup()
2473 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 )
2475 return FALSE;
2476
2477 /*
2478 * If the button press was used as the movement command for an operator
2479 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2480 * drag/release events.
2481 */
2482 if (!is_click && which_button == MOUSE_MIDDLE)
2483 return FALSE;
2484
2485 if (oap != NULL)
2486 regname = oap->regname;
2487 else
2488 regname = 0;
2489
2490 /*
2491 * Middle mouse button does a 'put' of the selected text
2492 */
2493 if (which_button == MOUSE_MIDDLE)
2494 {
2495 if (State == NORMAL)
2496 {
2497 /*
2498 * If an operator was pending, we don't know what the user wanted
2499 * to do. Go back to normal mode: Clear the operator and beep().
2500 */
2501 if (oap != NULL && oap->op_type != OP_NOP)
2502 {
2503 clearopbeep(oap);
2504 return FALSE;
2505 }
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 /*
2508 * If visual was active, yank the highlighted text and put it
2509 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002510 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 */
2512 if (VIsual_active)
2513 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002514 if (VIsual_select)
2515 {
2516 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002517 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002518 }
2519 else
2520 {
2521 stuffcharReadbuff('y');
2522 stuffcharReadbuff(K_MIDDLEMOUSE);
2523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 do_always = TRUE; /* ignore 'mouse' setting next time */
2525 return FALSE;
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 /*
2528 * The rest is below jump_to_mouse()
2529 */
2530 }
2531
2532 else if ((State & INSERT) == 0)
2533 return FALSE;
2534
2535 /*
2536 * Middle click in insert mode doesn't move the mouse, just insert the
2537 * contents of a register. '.' register is special, can't insert that
2538 * with do_put().
2539 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2540 * happens for the GUI).
2541 */
2542 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2543 {
2544 if (regname == '.')
2545 insert_reg(regname, TRUE);
2546 else
2547 {
2548#ifdef FEAT_CLIPBOARD
2549 if (clip_star.available && regname == 0)
2550 regname = '*';
2551#endif
2552 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2553 insert_reg(regname, TRUE);
2554 else
2555 {
2556 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2557
2558 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2559 AppendCharToRedobuff(Ctrl_R);
2560 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2561 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2562 }
2563 }
2564 return FALSE;
2565 }
2566 }
2567
2568 /* When dragging or button-up stay in the same window. */
2569 if (!is_click)
2570 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2571
2572 start_visual.lnum = 0;
2573
Bram Moolenaarf740b292006-02-16 22:11:02 +00002574 /* Check for clicking in the tab page line. */
2575 if (mouse_row == 0 && firstwin->w_winrow > 0)
2576 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002577 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002578 {
2579 if (in_tab_line)
2580 {
2581 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002582 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2583 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002584 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002585 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002586 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002587
Bram Moolenaarf740b292006-02-16 22:11:02 +00002588 /* click in a tab selects that tab page */
2589 if (is_click
2590# ifdef FEAT_CMDWIN
2591 && cmdwin_type == 0
2592# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002593 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002594 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002595 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002596 c1 = TabPageIdxs[mouse_col];
2597 if (c1 >= 0)
2598 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002599 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2600 {
2601 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002602 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002603 tabpage_new();
2604 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2605 }
2606 else
2607 {
2608 /* Go to specified tab page, or next one if not clicking
2609 * on a label. */
2610 goto_tabpage(c1);
2611
2612 /* It's like clicking on the status line of a window. */
2613 if (curwin != old_curwin)
2614 end_visual_mode();
2615 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002616 }
2617 else if (c1 < 0)
2618 {
2619 tabpage_T *tp;
2620
2621 /* Close the current or specified tab page. */
2622 if (c1 == -999)
2623 tp = curtab;
2624 else
2625 tp = find_tabpage(-c1);
2626 if (tp == curtab)
2627 {
2628 if (first_tabpage->tp_next != NULL)
2629 tabpage_close(FALSE);
2630 }
2631 else if (tp != NULL)
2632 tabpage_close_other(tp, FALSE);
2633 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002634 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002635 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002636 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002637 else if (is_drag && in_tab_line)
2638 {
2639 c1 = TabPageIdxs[mouse_col];
2640 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2641 return FALSE;
2642 }
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 /*
2645 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2646 * right button up -> pop-up menu
2647 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002648 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 */
2650 if (mouse_model_popup())
2651 {
2652 if (which_button == MOUSE_RIGHT
2653 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2654 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002655#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002657 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2658 || defined(FEAT_TERM_POPUP_MENU)
2659# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 if (gui.in_use)
2661 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002662# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2663 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2664 if (!is_click)
2665 /* Ignore right button release events, only shows the popup
2666 * menu on the button down event. */
2667 return FALSE;
2668# endif
2669# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2670 if (is_click || is_drag)
2671 /* Ignore right button down and drag mouse events. Windows
2672 * only shows the popup menu on the button up event. */
2673 return FALSE;
2674# endif
2675 }
2676# endif
2677# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2678 else
2679# endif
2680# if defined(FEAT_TERM_POPUP_MENU)
2681 if (!is_click)
2682 /* Ignore right button release events, only shows the popup
2683 * menu on the button down event. */
2684 return FALSE;
2685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002687 jump_flags = 0;
2688 if (STRCMP(p_mousem, "popup_setpos") == 0)
2689 {
2690 /* First set the cursor position before showing the popup
2691 * menu. */
2692 if (VIsual_active)
2693 {
2694 pos_T m_pos;
2695
2696 /*
2697 * set MOUSE_MAY_STOP_VIS if we are outside the
2698 * selection or the current window (might have false
2699 * negative here)
2700 */
2701 if (mouse_row < curwin->w_winrow
2702 || mouse_row
2703 > (curwin->w_winrow + curwin->w_height))
2704 jump_flags = MOUSE_MAY_STOP_VIS;
2705 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2706 jump_flags = MOUSE_MAY_STOP_VIS;
2707 else
2708 {
2709 if ((LT_POS(curwin->w_cursor, VIsual)
2710 && (LT_POS(m_pos, curwin->w_cursor)
2711 || LT_POS(VIsual, m_pos)))
2712 || (LT_POS(VIsual, curwin->w_cursor)
2713 && (LT_POS(m_pos, VIsual)
2714 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002716 jump_flags = MOUSE_MAY_STOP_VIS;
2717 }
2718 else if (VIsual_mode == Ctrl_V)
2719 {
2720 getvcols(curwin, &curwin->w_cursor, &VIsual,
2721 &leftcol, &rightcol);
2722 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2723 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 }
2726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002728 else
2729 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002731 if (jump_flags)
2732 {
2733 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2734 update_curbuf(VIsual_active ? INVERTED : VALID);
2735 setcursor();
2736 out_flush(); /* Update before showing popup menu */
2737 }
2738# ifdef FEAT_MENU
2739 show_popupmenu();
2740 got_click = FALSE; /* ignore release events */
2741# endif
2742 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#else
2744 return FALSE;
2745#endif
2746 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002747 if (which_button == MOUSE_LEFT
2748 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 {
2750 which_button = MOUSE_RIGHT;
2751 mod_mask &= ~MOD_MASK_SHIFT;
2752 }
2753 }
2754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 if ((State & (NORMAL | INSERT))
2756 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2757 {
2758 if (which_button == MOUSE_LEFT)
2759 {
2760 if (is_click)
2761 {
2762 /* stop Visual mode for a left click in a window, but not when
2763 * on a status line */
2764 if (VIsual_active)
2765 jump_flags |= MOUSE_MAY_STOP_VIS;
2766 }
2767 else if (mouse_has(MOUSE_VISUAL))
2768 jump_flags |= MOUSE_MAY_VIS;
2769 }
2770 else if (which_button == MOUSE_RIGHT)
2771 {
2772 if (is_click && VIsual_active)
2773 {
2774 /*
2775 * Remember the start and end of visual before moving the
2776 * cursor.
2777 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002778 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
2780 start_visual = curwin->w_cursor;
2781 end_visual = VIsual;
2782 }
2783 else
2784 {
2785 start_visual = VIsual;
2786 end_visual = curwin->w_cursor;
2787 }
2788 }
2789 jump_flags |= MOUSE_FOCUS;
2790 if (mouse_has(MOUSE_VISUAL))
2791 jump_flags |= MOUSE_MAY_VIS;
2792 }
2793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795 /*
2796 * If an operator is pending, ignore all drags and releases until the
2797 * next mouse click.
2798 */
2799 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2800 {
2801 got_click = FALSE;
2802 oap->motion_type = MCHAR;
2803 }
2804
2805 /* When releasing the button let jump_to_mouse() know. */
2806 if (!is_click && !is_drag)
2807 jump_flags |= MOUSE_RELEASED;
2808
2809 /*
2810 * JUMP!
2811 */
2812 jump_flags = jump_to_mouse(jump_flags,
2813 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002814
2815#ifdef FEAT_MENU
2816 /* A click in the window toolbar has no side effects. */
2817 if (jump_flags & MOUSE_WINBAR)
2818 return FALSE;
2819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 moved = (jump_flags & CURSOR_MOVED);
2821 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
2824#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002825 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2827 {
2828 int key = KEY2TERMCAP1(c);
2829
2830 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2831 || key == (int)KE_RIGHTRELEASE)
2832 netbeans_button_release(which_button);
2833 }
2834#endif
2835
2836 /* When jumping to another window, clear a pending operator. That's a bit
2837 * friendlier than beeping and not jumping to that window. */
2838 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2839 clearop(oap);
2840
2841#ifdef FEAT_FOLDING
2842 if (mod_mask == 0
2843 && !is_drag
2844 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2845 && which_button == MOUSE_LEFT)
2846 {
2847 /* open or close a fold at this line */
2848 if (jump_flags & MOUSE_FOLD_OPEN)
2849 openFold(curwin->w_cursor.lnum, 1L);
2850 else
2851 closeFold(curwin->w_cursor.lnum, 1L);
2852 /* don't move the cursor if still in the same window */
2853 if (curwin == old_curwin)
2854 curwin->w_cursor = save_cursor;
2855 }
2856#endif
2857
2858#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2859 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2860 {
2861 clip_modeless(which_button, is_click, is_drag);
2862 return FALSE;
2863 }
2864#endif
2865
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002867 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 if (VIsual_active && is_drag && p_so)
2869 {
2870 /* In the very first line, allow scrolling one line */
2871 if (mouse_row == 0)
2872 mouse_dragging = 2;
2873 else
2874 mouse_dragging = 1;
2875 }
2876
2877 /* When dragging the mouse above the window, scroll down. */
2878 if (is_drag && mouse_row < 0 && !in_status_line)
2879 {
2880 scroll_redraw(FALSE, 1L);
2881 mouse_row = 0;
2882 }
2883
2884 if (start_visual.lnum) /* right click in visual mode */
2885 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002886 /* When ALT is pressed make Visual mode blockwise. */
2887 if (mod_mask & MOD_MASK_ALT)
2888 VIsual_mode = Ctrl_V;
2889
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 /*
2891 * In Visual-block mode, divide the area in four, pick up the corner
2892 * that is in the quarter that the cursor is in.
2893 */
2894 if (VIsual_mode == Ctrl_V)
2895 {
2896 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2897 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2898 end_visual.col = leftcol;
2899 else
2900 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002901 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 end_visual.lnum = start_visual.lnum;
2904
2905 /* move VIsual to the right column */
2906 start_visual = curwin->w_cursor; /* save the cursor pos */
2907 curwin->w_cursor = end_visual;
2908 coladvance(end_visual.col);
2909 VIsual = curwin->w_cursor;
2910 curwin->w_cursor = start_visual; /* restore the cursor */
2911 }
2912 else
2913 {
2914 /*
2915 * If the click is before the start of visual, change the start.
2916 * If the click is after the end of visual, change the end. If
2917 * the click is inside the visual, change the closest side.
2918 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002919 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002921 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 VIsual = start_visual;
2923 else
2924 {
2925 /* In the same line, compare column number */
2926 if (end_visual.lnum == start_visual.lnum)
2927 {
2928 if (curwin->w_cursor.col - start_visual.col >
2929 end_visual.col - curwin->w_cursor.col)
2930 VIsual = start_visual;
2931 else
2932 VIsual = end_visual;
2933 }
2934
2935 /* In different lines, compare line number */
2936 else
2937 {
2938 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2939 (end_visual.lnum - curwin->w_cursor.lnum);
2940
2941 if (diff > 0) /* closest to end */
2942 VIsual = start_visual;
2943 else if (diff < 0) /* closest to start */
2944 VIsual = end_visual;
2945 else /* in the middle line */
2946 {
2947 if (curwin->w_cursor.col <
2948 (start_visual.col + end_visual.col) / 2)
2949 VIsual = end_visual;
2950 else
2951 VIsual = start_visual;
2952 }
2953 }
2954 }
2955 }
2956 }
2957 /*
2958 * If Visual mode started in insert mode, execute "CTRL-O"
2959 */
2960 else if ((State & INSERT) && VIsual_active)
2961 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 /*
2964 * Middle mouse click: Put text before cursor.
2965 */
2966 if (which_button == MOUSE_MIDDLE)
2967 {
2968#ifdef FEAT_CLIPBOARD
2969 if (clip_star.available && regname == 0)
2970 regname = '*';
2971#endif
2972 if (yank_register_mline(regname))
2973 {
2974 if (mouse_past_bottom)
2975 dir = FORWARD;
2976 }
2977 else if (mouse_past_eol)
2978 dir = FORWARD;
2979
2980 if (fixindent)
2981 {
2982 c1 = (dir == BACKWARD) ? '[' : ']';
2983 c2 = 'p';
2984 }
2985 else
2986 {
2987 c1 = (dir == FORWARD) ? 'p' : 'P';
2988 c2 = NUL;
2989 }
2990 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2991
2992 /*
2993 * Remember where the paste started, so in edit() Insstart can be set
2994 * to this position
2995 */
2996 if (restart_edit != 0)
2997 where_paste_started = curwin->w_cursor;
2998 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2999 }
3000
Bram Moolenaar4033c552017-09-16 20:54:51 +02003001#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 /*
3003 * Ctrl-Mouse click or double click in a quickfix window jumps to the
3004 * error under the mouse pointer.
3005 */
3006 else if (((mod_mask & MOD_MASK_CTRL)
3007 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3008 && bt_quickfix(curbuf))
3009 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003010 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003011 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003012 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003013 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 got_click = FALSE; /* ignore drag&release now */
3015 }
3016#endif
3017
3018 /*
3019 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3020 * under the mouse pointer.
3021 */
3022 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3023 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3024 {
3025 if (State & INSERT)
3026 stuffcharReadbuff(Ctrl_O);
3027 stuffcharReadbuff(Ctrl_RSB);
3028 got_click = FALSE; /* ignore drag&release now */
3029 }
3030
3031 /*
3032 * Shift-Mouse click searches for the next occurrence of the word under
3033 * the mouse pointer
3034 */
3035 else if ((mod_mask & MOD_MASK_SHIFT))
3036 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003037 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 stuffcharReadbuff(Ctrl_O);
3039 if (which_button == MOUSE_LEFT)
3040 stuffcharReadbuff('*');
3041 else /* MOUSE_RIGHT */
3042 stuffcharReadbuff('#');
3043 }
3044
3045 /* Handle double clicks, unless on status line */
3046 else if (in_status_line)
3047 {
3048#ifdef FEAT_MOUSESHAPE
3049 if ((is_drag || is_click) && !drag_status_line)
3050 {
3051 drag_status_line = TRUE;
3052 update_mouseshape(-1);
3053 }
3054#endif
3055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 else if (in_sep_line)
3057 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003058#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 if ((is_drag || is_click) && !drag_sep_line)
3060 {
3061 drag_sep_line = TRUE;
3062 update_mouseshape(-1);
3063 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003064#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3067 && mouse_has(MOUSE_VISUAL))
3068 {
3069 if (is_click || !VIsual_active)
3070 {
3071 if (VIsual_active)
3072 orig_cursor = VIsual;
3073 else
3074 {
3075 check_visual_highlight();
3076 VIsual = curwin->w_cursor;
3077 orig_cursor = VIsual;
3078 VIsual_active = TRUE;
3079 VIsual_reselect = TRUE;
3080 /* start Select mode if 'selectmode' contains "mouse" */
3081 may_start_select('o');
3082 setmouse();
3083 }
3084 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003085 {
3086 /* Double click with ALT pressed makes it blockwise. */
3087 if (mod_mask & MOD_MASK_ALT)
3088 VIsual_mode = Ctrl_V;
3089 else
3090 VIsual_mode = 'v';
3091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3093 VIsual_mode = 'V';
3094 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3095 VIsual_mode = Ctrl_V;
3096#ifdef FEAT_CLIPBOARD
3097 /* Make sure the clipboard gets updated. Needed because start and
3098 * end may still be the same, and the selection needs to be owned */
3099 clip_star.vmode = NUL;
3100#endif
3101 }
3102 /*
3103 * A double click selects a word or a block.
3104 */
3105 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3106 {
3107 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003108 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110 if (is_click)
3111 {
3112 /* If the character under the cursor (skipping white space) is
3113 * not a word character, try finding a match and select a (),
3114 * {}, [], #if/#endif, etc. block. */
3115 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003116 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 inc(&end_visual);
3118 if (oap != NULL)
3119 oap->motion_type = MCHAR;
3120 if (oap != NULL
3121 && VIsual_mode == 'v'
3122 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003123 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 && (pos = findmatch(oap, NUL)) != NULL)
3125 {
3126 curwin->w_cursor = *pos;
3127 if (oap->motion_type == MLINE)
3128 VIsual_mode = 'V';
3129 else if (*p_sel == 'e')
3130 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003131 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 ++VIsual.col;
3133 else
3134 ++curwin->w_cursor.col;
3135 }
3136 }
3137 }
3138
3139 if (pos == NULL && (is_click || is_drag))
3140 {
3141 /* When not found a match or when dragging: extend to include
3142 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003143 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 {
3145 find_start_of_word(&curwin->w_cursor);
3146 find_end_of_word(&VIsual);
3147 }
3148 else
3149 {
3150 find_start_of_word(&VIsual);
3151 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3152#ifdef FEAT_MBYTE
3153 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003154 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#else
3156 ++curwin->w_cursor.col;
3157#endif
3158 find_end_of_word(&curwin->w_cursor);
3159 }
3160 }
3161 curwin->w_set_curswant = TRUE;
3162 }
3163 if (is_click)
3164 redraw_curbuf_later(INVERTED); /* update the inversion */
3165 }
3166 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003167 {
3168 if (mod_mask & MOD_MASK_ALT)
3169 VIsual_mode = Ctrl_V;
3170 else
3171 VIsual_mode = 'v';
3172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
3174 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003175 if ((!VIsual_active && old_active && mode_displayed)
3176 || (VIsual_active && p_smd && msg_silent == 0
3177 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179
3180 return moved;
3181}
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183/*
3184 * Move "pos" back to the start of the word it's in.
3185 */
3186 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003187find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188{
3189 char_u *line;
3190 int cclass;
3191 int col;
3192
3193 line = ml_get(pos->lnum);
3194 cclass = get_mouse_class(line + pos->col);
3195
3196 while (pos->col > 0)
3197 {
3198 col = pos->col - 1;
3199#ifdef FEAT_MBYTE
3200 col -= (*mb_head_off)(line, line + col);
3201#endif
3202 if (get_mouse_class(line + col) != cclass)
3203 break;
3204 pos->col = col;
3205 }
3206}
3207
3208/*
3209 * Move "pos" forward to the end of the word it's in.
3210 * When 'selection' is "exclusive", the position is just after the word.
3211 */
3212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003213find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
3215 char_u *line;
3216 int cclass;
3217 int col;
3218
3219 line = ml_get(pos->lnum);
3220 if (*p_sel == 'e' && pos->col > 0)
3221 {
3222 --pos->col;
3223#ifdef FEAT_MBYTE
3224 pos->col -= (*mb_head_off)(line, line + pos->col);
3225#endif
3226 }
3227 cclass = get_mouse_class(line + pos->col);
3228 while (line[pos->col] != NUL)
3229 {
3230#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003231 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232#else
3233 col = pos->col + 1;
3234#endif
3235 if (get_mouse_class(line + col) != cclass)
3236 {
3237 if (*p_sel == 'e')
3238 pos->col = col;
3239 break;
3240 }
3241 pos->col = col;
3242 }
3243}
3244
3245/*
3246 * Get class of a character for selection: same class means same word.
3247 * 0: blank
3248 * 1: punctuation groups
3249 * 2: normal word character
3250 * >2: multi-byte word character.
3251 */
3252 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003253get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 int c;
3256
3257#ifdef FEAT_MBYTE
3258 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3259 return mb_get_class(p);
3260#endif
3261
3262 c = *p;
3263 if (c == ' ' || c == '\t')
3264 return 0;
3265
3266 if (vim_iswordc(c))
3267 return 2;
3268
3269 /*
3270 * There are a few special cases where we want certain combinations of
3271 * characters to be considered as a single word. These are things like
3272 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003273 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 */
3275 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3276 return 1;
3277 return c;
3278}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif /* FEAT_MOUSE */
3280
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281/*
3282 * Check if highlighting for visual mode is possible, give a warning message
3283 * if not.
3284 */
3285 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003286check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
3288 static int did_check = FALSE;
3289
3290 if (full_screen)
3291 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003292 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 MSG(_("Warning: terminal cannot highlight"));
3294 did_check = TRUE;
3295 }
3296}
3297
3298/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003299 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 * This function should ALWAYS be called to end Visual mode, except from
3301 * do_pending_operator().
3302 */
3303 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003304end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306#ifdef FEAT_CLIPBOARD
3307 /*
3308 * If we are using the clipboard, then remember what was selected in case
3309 * we need to paste it somewhere while we still own the selection.
3310 * Only do this when the clipboard is already owned. Don't want to grab
3311 * the selection when hitting ESC.
3312 */
3313 if (clip_star.available && clip_star.owned)
3314 clip_auto_select();
3315#endif
3316
3317 VIsual_active = FALSE;
3318#ifdef FEAT_MOUSE
3319 setmouse();
3320 mouse_dragging = 0;
3321#endif
3322
3323 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003324 curbuf->b_visual.vi_mode = VIsual_mode;
3325 curbuf->b_visual.vi_start = VIsual;
3326 curbuf->b_visual.vi_end = curwin->w_cursor;
3327 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#ifdef FEAT_EVAL
3329 curbuf->b_visual_mode_eval = VIsual_mode;
3330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#ifdef FEAT_VIRTUALEDIT
3332 if (!virtual_active())
3333 curwin->w_cursor.coladd = 0;
3334#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003335 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003337 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338}
3339
3340/*
3341 * Reset VIsual_active and VIsual_reselect.
3342 */
3343 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003344reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 if (VIsual_active)
3347 {
3348 end_visual_mode();
3349 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3350 }
3351 VIsual_reselect = FALSE;
3352}
3353
3354/*
3355 * Reset VIsual_active and VIsual_reselect if it's set.
3356 */
3357 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003358reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 if (VIsual_active)
3361 {
3362 end_visual_mode();
3363 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3364 VIsual_reselect = FALSE;
3365 }
3366}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368/*
3369 * Check for a balloon-eval special item to include when searching for an
3370 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3371 * Returns TRUE if the character at "*ptr" should be included.
3372 * "dir" is FORWARD or BACKWARD, the direction of searching.
3373 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3374 * "bnp" points to a counter for square brackets.
3375 */
3376 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003377find_is_eval_item(
3378 char_u *ptr,
3379 int *colp,
3380 int *bnp,
3381 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 /* Accept everything inside []. */
3384 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3385 ++*bnp;
3386 if (*bnp > 0)
3387 {
3388 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3389 --*bnp;
3390 return TRUE;
3391 }
3392
3393 /* skip over "s.var" */
3394 if (*ptr == '.')
3395 return TRUE;
3396
3397 /* two-character item: s->var */
3398 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3399 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3400 {
3401 *colp += dir;
3402 return TRUE;
3403 }
3404 return FALSE;
3405}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
3407/*
3408 * Find the identifier under or to the right of the cursor.
3409 * "find_type" can have one of three values:
3410 * FIND_IDENT: find an identifier (keyword)
3411 * FIND_STRING: find any non-white string
3412 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003413 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 *
3415 * There are three steps:
3416 * 1. Search forward for the start of an identifier/string. Doesn't move if
3417 * already on one.
3418 * 2. Search backward for the start of this identifier/string.
3419 * This doesn't match the real Vi but I like it a little better and it
3420 * shouldn't bother anyone.
3421 * 3. Search forward to the end of this identifier/string.
3422 * When FIND_IDENT isn't defined, we backup until a blank.
3423 *
3424 * Returns the length of the string, or zero if no string is found.
3425 * If a string is found, a pointer to the string is put in "*string". This
3426 * string is not always NUL terminated.
3427 */
3428 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003429find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3432 curwin->w_cursor.col, string, find_type);
3433}
3434
3435/*
3436 * Like find_ident_under_cursor(), but for any window and any position.
3437 * However: Uses 'iskeyword' from the current window!.
3438 */
3439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003440find_ident_at_pos(
3441 win_T *wp,
3442 linenr_T lnum,
3443 colnr_T startcol,
3444 char_u **string,
3445 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 char_u *ptr;
3448 int col = 0; /* init to shut up GCC */
3449 int i;
3450#ifdef FEAT_MBYTE
3451 int this_class = 0;
3452 int prev_class;
3453 int prevcol;
3454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457 /*
3458 * if i == 0: try to find an identifier
3459 * if i == 1: try to find any non-white string
3460 */
3461 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3462 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3463 {
3464 /*
3465 * 1. skip to start of identifier/string
3466 */
3467 col = startcol;
3468#ifdef FEAT_MBYTE
3469 if (has_mbyte)
3470 {
3471 while (ptr[col] != NUL)
3472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 /* Stop at a ']' to evaluate "a[x]". */
3474 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3475 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 this_class = mb_get_class(ptr + col);
3477 if (this_class != 0 && (i == 1 || this_class != 1))
3478 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003479 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481 }
3482 else
3483#endif
3484 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003485 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 )
3488 ++col;
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 /* When starting on a ']' count it, so that we include the '['. */
3491 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493 /*
3494 * 2. Back up to start of identifier/string.
3495 */
3496#ifdef FEAT_MBYTE
3497 if (has_mbyte)
3498 {
3499 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3501 this_class = mb_get_class((char_u *)"a");
3502 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003504 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3507 prev_class = mb_get_class(ptr + prevcol);
3508 if (this_class != prev_class
3509 && (i == 0
3510 || prev_class == 0
3511 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 && (!(find_type & FIND_EVAL)
3513 || prevcol == 0
3514 || !find_is_eval_item(ptr + prevcol, &prevcol,
3515 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 )
3517 break;
3518 col = prevcol;
3519 }
3520
3521 /* If we don't want just any old string, or we've found an
3522 * identifier, stop searching. */
3523 if (this_class > 2)
3524 this_class = 2;
3525 if (!(find_type & FIND_STRING) || this_class == 2)
3526 break;
3527 }
3528 else
3529#endif
3530 {
3531 while (col > 0
3532 && ((i == 0
3533 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003534 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 && (!(find_type & FIND_IDENT)
3536 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 || ((find_type & FIND_EVAL)
3538 && col > 1
3539 && find_is_eval_item(ptr + col - 1, &col,
3540 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 ))
3542 --col;
3543
3544 /* If we don't want just any old string, or we've found an
3545 * identifier, stop searching. */
3546 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3547 break;
3548 }
3549 }
3550
3551 if (ptr[col] == NUL || (i == 0 && (
3552#ifdef FEAT_MBYTE
3553 has_mbyte ? this_class != 2 :
3554#endif
3555 !vim_iswordc(ptr[col]))))
3556 {
3557 /*
3558 * didn't find an identifier or string
3559 */
3560 if (find_type & FIND_STRING)
3561 EMSG(_("E348: No string under cursor"));
3562 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003563 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 return 0;
3565 }
3566 ptr += col;
3567 *string = ptr;
3568
3569 /*
3570 * 3. Find the end if the identifier/string.
3571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 bn = 0;
3573 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 col = 0;
3575#ifdef FEAT_MBYTE
3576 if (has_mbyte)
3577 {
3578 /* Search for point of changing multibyte character class. */
3579 this_class = mb_get_class(ptr);
3580 while (ptr[col] != NUL
3581 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3582 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 || ((find_type & FIND_EVAL)
3584 && col <= (int)startcol
3585 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003587 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 }
3589 else
3590#endif
3591 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003592 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 || ((find_type & FIND_EVAL)
3594 && col <= (int)startcol
3595 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 )
3597 {
3598 ++col;
3599 }
3600
3601 return col;
3602}
3603
3604/*
3605 * Prepare for redo of a normal command.
3606 */
3607 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003608prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
3610 prep_redo(cap->oap->regname, cap->count0,
3611 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3612}
3613
3614/*
3615 * Prepare for redo of any command.
3616 * Note that only the last argument can be a multi-byte char.
3617 */
3618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003619prep_redo(
3620 int regname,
3621 long num,
3622 int cmd1,
3623 int cmd2,
3624 int cmd3,
3625 int cmd4,
3626 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
3628 ResetRedobuff();
3629 if (regname != 0) /* yank from specified buffer */
3630 {
3631 AppendCharToRedobuff('"');
3632 AppendCharToRedobuff(regname);
3633 }
3634 if (num)
3635 AppendNumberToRedobuff(num);
3636
3637 if (cmd1 != NUL)
3638 AppendCharToRedobuff(cmd1);
3639 if (cmd2 != NUL)
3640 AppendCharToRedobuff(cmd2);
3641 if (cmd3 != NUL)
3642 AppendCharToRedobuff(cmd3);
3643 if (cmd4 != NUL)
3644 AppendCharToRedobuff(cmd4);
3645 if (cmd5 != NUL)
3646 AppendCharToRedobuff(cmd5);
3647}
3648
3649/*
3650 * check for operator active and clear it
3651 *
3652 * return TRUE if operator was active
3653 */
3654 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003655checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
3657 if (oap->op_type == OP_NOP)
3658 return FALSE;
3659 clearopbeep(oap);
3660 return TRUE;
3661}
3662
3663/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003664 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003666 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 */
3668 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003669checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003671 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 return FALSE;
3673 clearopbeep(oap);
3674 return TRUE;
3675}
3676
3677 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003678clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679{
3680 oap->op_type = OP_NOP;
3681 oap->regname = 0;
3682 oap->motion_force = NUL;
3683 oap->use_reg_one = FALSE;
3684}
3685
3686 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003687clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 clearop(oap);
3690 beep_flush();
3691}
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693/*
3694 * Remove the shift modifier from a special key.
3695 */
3696 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003697unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
3699 switch (cap->cmdchar)
3700 {
3701 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3702 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3703 case K_S_UP: cap->cmdchar = K_UP; break;
3704 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3705 case K_S_HOME: cap->cmdchar = K_HOME; break;
3706 case K_S_END: cap->cmdchar = K_END; break;
3707 }
3708 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003711/*
3712 * If the mode is currently displayed clear the command line or update the
3713 * command displayed.
3714 */
3715 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003716may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003717{
3718 if (mode_displayed)
3719 clear_cmdline = TRUE; /* unshow visual mode later */
3720#ifdef FEAT_CMDL_INFO
3721 else
3722 clear_showcmd();
3723#endif
3724}
3725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3727/*
3728 * Routines for displaying a partly typed command
3729 */
3730
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003731#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732static char_u showcmd_buf[SHOWCMD_BUFLEN];
3733static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3734static int showcmd_is_clear = TRUE;
3735static int showcmd_visual = FALSE;
3736
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003737static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
3739 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003740clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 if (!p_sc)
3743 return;
3744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 if (VIsual_active && !char_avail())
3746 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003747 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 long lines;
3749 colnr_T leftcol, rightcol;
3750 linenr_T top, bot;
3751
3752 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003753 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 {
3755 top = VIsual.lnum;
3756 bot = curwin->w_cursor.lnum;
3757 }
3758 else
3759 {
3760 top = curwin->w_cursor.lnum;
3761 bot = VIsual.lnum;
3762 }
3763# ifdef FEAT_FOLDING
3764 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003765 (void)hasFolding(top, &top, NULL);
3766 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767# endif
3768 lines = bot - top + 1;
3769
3770 if (VIsual_mode == Ctrl_V)
3771 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003772# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003773 char_u *saved_sbr = p_sbr;
3774
3775 /* Make 'sbr' empty for a moment to get the correct size. */
3776 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003779# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003780 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3783 (long)(rightcol - leftcol + 1));
3784 }
3785 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3786 sprintf((char *)showcmd_buf, "%ld", lines);
3787 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003788 {
3789 char_u *s, *e;
3790 int l;
3791 int bytes = 0;
3792 int chars = 0;
3793
3794 if (cursor_bot)
3795 {
3796 s = ml_get_pos(&VIsual);
3797 e = ml_get_cursor();
3798 }
3799 else
3800 {
3801 s = ml_get_cursor();
3802 e = ml_get_pos(&VIsual);
3803 }
3804 while ((*p_sel != 'e') ? s <= e : s < e)
3805 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003806# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003807 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003808# else
3809 l = (*s == NUL) ? 0 : 1;
3810# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003811 if (l == 0)
3812 {
3813 ++bytes;
3814 ++chars;
3815 break; /* end of line */
3816 }
3817 bytes += l;
3818 ++chars;
3819 s += l;
3820 }
3821 if (bytes == chars)
3822 sprintf((char *)showcmd_buf, "%d", chars);
3823 else
3824 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3827 showcmd_visual = TRUE;
3828 }
3829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 {
3831 showcmd_buf[0] = NUL;
3832 showcmd_visual = FALSE;
3833
3834 /* Don't actually display something if there is nothing to clear. */
3835 if (showcmd_is_clear)
3836 return;
3837 }
3838
3839 display_showcmd();
3840}
3841
3842/*
3843 * Add 'c' to string of shown command chars.
3844 * Return TRUE if output has been written (and setcursor() has been called).
3845 */
3846 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003847add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848{
3849 char_u *p;
3850 int old_len;
3851 int extra_len;
3852 int overflow;
3853#if defined(FEAT_MOUSE)
3854 int i;
3855 static int ignore[] =
3856 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003857# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3859 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003860# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003861 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003862 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3864 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003865 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003867 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 0
3869 };
3870#endif
3871
Bram Moolenaar09df3122006-01-23 22:23:09 +00003872 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 return FALSE;
3874
3875 if (showcmd_visual)
3876 {
3877 showcmd_buf[0] = NUL;
3878 showcmd_visual = FALSE;
3879 }
3880
3881#if defined(FEAT_MOUSE)
3882 /* Ignore keys that are scrollbar updates and mouse clicks */
3883 if (IS_SPECIAL(c))
3884 for (i = 0; ignore[i] != 0; ++i)
3885 if (ignore[i] == c)
3886 return FALSE;
3887#endif
3888
3889 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003890 if (*p == ' ')
3891 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 old_len = (int)STRLEN(showcmd_buf);
3893 extra_len = (int)STRLEN(p);
3894 overflow = old_len + extra_len - SHOWCMD_COLS;
3895 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003896 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3897 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 STRCAT(showcmd_buf, p);
3899
3900 if (char_avail())
3901 return FALSE;
3902
3903 display_showcmd();
3904
3905 return TRUE;
3906}
3907
3908 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003909add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910{
3911 if (!add_to_showcmd(c))
3912 setcursor();
3913}
3914
3915/*
3916 * Delete 'len' characters from the end of the shown command.
3917 */
3918 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003919del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
3921 int old_len;
3922
3923 if (!p_sc)
3924 return;
3925
3926 old_len = (int)STRLEN(showcmd_buf);
3927 if (len > old_len)
3928 len = old_len;
3929 showcmd_buf[old_len - len] = NUL;
3930
3931 if (!char_avail())
3932 display_showcmd();
3933}
3934
3935/*
3936 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3937 * something and there is a partial mapping.
3938 */
3939 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003940push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
3942 if (p_sc)
3943 STRCPY(old_showcmd_buf, showcmd_buf);
3944}
3945
3946 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003947pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
3949 if (!p_sc)
3950 return;
3951
3952 STRCPY(showcmd_buf, old_showcmd_buf);
3953
3954 display_showcmd();
3955}
3956
3957 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003958display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
3960 int len;
3961
3962 cursor_off();
3963
3964 len = (int)STRLEN(showcmd_buf);
3965 if (len == 0)
3966 showcmd_is_clear = TRUE;
3967 else
3968 {
3969 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3970 showcmd_is_clear = FALSE;
3971 }
3972
3973 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003974 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3975 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 */
3977 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3978
3979 setcursor(); /* put cursor back where it belongs */
3980}
3981#endif
3982
3983#ifdef FEAT_SCROLLBIND
3984/*
3985 * When "check" is FALSE, prepare for commands that scroll the window.
3986 * When "check" is TRUE, take care of scroll-binding after the window has
3987 * scrolled. Called from normal_cmd() and edit().
3988 */
3989 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003990do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
3992 static win_T *old_curwin = NULL;
3993 static linenr_T old_topline = 0;
3994#ifdef FEAT_DIFF
3995 static int old_topfill = 0;
3996#endif
3997 static buf_T *old_buf = NULL;
3998 static colnr_T old_leftcol = 0;
3999
4000 if (check && curwin->w_p_scb)
4001 {
4002 /* If a ":syncbind" command was just used, don't scroll, only reset
4003 * the values. */
4004 if (did_syncbind)
4005 did_syncbind = FALSE;
4006 else if (curwin == old_curwin)
4007 {
4008 /*
4009 * Synchronize other windows, as necessary according to
4010 * 'scrollbind'. Don't do this after an ":edit" command, except
4011 * when 'diff' is set.
4012 */
4013 if ((curwin->w_buffer == old_buf
4014#ifdef FEAT_DIFF
4015 || curwin->w_p_diff
4016#endif
4017 )
4018 && (curwin->w_topline != old_topline
4019#ifdef FEAT_DIFF
4020 || curwin->w_topfill != old_topfill
4021#endif
4022 || curwin->w_leftcol != old_leftcol))
4023 {
4024 check_scrollbind(curwin->w_topline - old_topline,
4025 (long)(curwin->w_leftcol - old_leftcol));
4026 }
4027 }
4028 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4029 {
4030 /*
4031 * When switching between windows, make sure that the relative
4032 * vertical offset is valid for the new window. The relative
4033 * offset is invalid whenever another 'scrollbind' window has
4034 * scrolled to a point that would force the current window to
4035 * scroll past the beginning or end of its buffer. When the
4036 * resync is performed, some of the other 'scrollbind' windows may
4037 * need to jump so that the current window's relative position is
4038 * visible on-screen.
4039 */
4040 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4041 }
4042 curwin->w_scbind_pos = curwin->w_topline;
4043 }
4044
4045 old_curwin = curwin;
4046 old_topline = curwin->w_topline;
4047#ifdef FEAT_DIFF
4048 old_topfill = curwin->w_topfill;
4049#endif
4050 old_buf = curwin->w_buffer;
4051 old_leftcol = curwin->w_leftcol;
4052}
4053
4054/*
4055 * Synchronize any windows that have "scrollbind" set, based on the
4056 * number of rows by which the current window has changed
4057 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4058 */
4059 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004060check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
4062 int want_ver;
4063 int want_hor;
4064 win_T *old_curwin = curwin;
4065 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 int old_VIsual_select = VIsual_select;
4067 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 colnr_T tgt_leftcol = curwin->w_leftcol;
4069 long topline;
4070 long y;
4071
4072 /*
4073 * check 'scrollopt' string for vertical and horizontal scroll options
4074 */
4075 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4076#ifdef FEAT_DIFF
4077 want_ver |= old_curwin->w_p_diff;
4078#endif
4079 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4080
4081 /*
4082 * loop through the scrollbound windows and scroll accordingly
4083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004085 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
4087 curbuf = curwin->w_buffer;
4088 /* skip original window and windows with 'noscrollbind' */
4089 if (curwin != old_curwin && curwin->w_p_scb)
4090 {
4091 /*
4092 * do the vertical scroll
4093 */
4094 if (want_ver)
4095 {
4096#ifdef FEAT_DIFF
4097 if (old_curwin->w_p_diff && curwin->w_p_diff)
4098 {
4099 diff_set_topline(old_curwin, curwin);
4100 }
4101 else
4102#endif
4103 {
4104 curwin->w_scbind_pos += topline_diff;
4105 topline = curwin->w_scbind_pos;
4106 if (topline > curbuf->b_ml.ml_line_count)
4107 topline = curbuf->b_ml.ml_line_count;
4108 if (topline < 1)
4109 topline = 1;
4110
4111 y = topline - curwin->w_topline;
4112 if (y > 0)
4113 scrollup(y, FALSE);
4114 else
4115 scrolldown(-y, FALSE);
4116 }
4117
4118 redraw_later(VALID);
4119 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122
4123 /*
4124 * do the horizontal scroll
4125 */
4126 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4127 {
4128 curwin->w_leftcol = tgt_leftcol;
4129 leftcol_changed();
4130 }
4131 }
4132 }
4133
4134 /*
4135 * reset current-window
4136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 VIsual_select = old_VIsual_select;
4138 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 curwin = old_curwin;
4140 curbuf = old_curbuf;
4141}
4142#endif /* #ifdef FEAT_SCROLLBIND */
4143
4144/*
4145 * Command character that's ignored.
4146 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004147 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004150nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004152 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153}
4154
4155/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004156 * Command character that doesn't do anything, but unlike nv_ignore() does
4157 * start edit(). Used for "startinsert" executed while starting up.
4158 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004159 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004160nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004161{
4162}
4163
4164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * Command character doesn't exist.
4166 */
4167 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004168nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
4170 clearopbeep(cap->oap);
4171}
4172
4173/*
4174 * <Help> and <F1> commands.
4175 */
4176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004177nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
4179 if (!checkclearopq(cap->oap))
4180 ex_help(NULL);
4181}
4182
4183/*
4184 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4185 */
4186 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004187nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
Bram Moolenaard79e5502016-01-10 22:13:02 +01004189 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004190 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004191 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004192 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4193 op_addsub(cap->oap, cap->count1, cap->arg);
4194 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004195 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004196 else if (VIsual_active)
4197 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004198 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004199 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200}
4201
4202/*
4203 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4204 */
4205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004206nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004209 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004210 if (mod_mask & MOD_MASK_CTRL)
4211 {
4212 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4213 if (cap->arg == BACKWARD)
4214 goto_tabpage(-(int)cap->count1);
4215 else
4216 goto_tabpage((int)cap->count0);
4217 }
4218 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004219 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221}
4222
4223/*
4224 * Implementation of "gd" and "gD" command.
4225 */
4226 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004227nv_gd(
4228 oparg_T *oap,
4229 int nchar,
4230 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
4232 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 char_u *ptr;
4234
Bram Moolenaard9d30582005-05-18 22:10:28 +00004235 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004236 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4237 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004239#ifdef FEAT_FOLDING
4240 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4241 foldOpenCursor();
4242#endif
4243}
4244
4245/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004246 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4247 * otherwise.
4248 */
4249 static int
4250is_ident(char_u *line, int offset)
4251{
4252 int i;
4253 int incomment = FALSE;
4254 int instring = 0;
4255 int prev = 0;
4256
4257 for (i = 0; i < offset && line[i] != NUL; i++)
4258 {
4259 if (instring != 0)
4260 {
4261 if (prev != '\\' && line[i] == instring)
4262 instring = 0;
4263 }
4264 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4265 {
4266 instring = line[i];
4267 }
4268 else
4269 {
4270 if (incomment)
4271 {
4272 if (prev == '*' && line[i] == '/')
4273 incomment = FALSE;
4274 }
4275 else if (prev == '/' && line[i] == '*')
4276 {
4277 incomment = TRUE;
4278 }
4279 else if (prev == '/' && line[i] == '/')
4280 {
4281 return FALSE;
4282 }
4283 }
4284
4285 prev = line[i];
4286 }
4287
4288 return incomment == FALSE && instring == 0;
4289}
4290
4291/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004292 * Search for variable declaration of "ptr[len]".
4293 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4294 * current file ("gD").
4295 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004296 * Return FAIL when not found.
4297 */
4298 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004299find_decl(
4300 char_u *ptr,
4301 int len,
4302 int locally,
4303 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004304 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004305{
4306 char_u *pat;
4307 pos_T old_pos;
4308 pos_T par_pos;
4309 pos_T found_pos;
4310 int t;
4311 int save_p_ws;
4312 int save_p_scs;
4313 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004314 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004315 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004316 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004317
4318 if ((pat = alloc(len + 7)) == NULL)
4319 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004320
4321 /* Put "\V" before the pattern to avoid that the special meaning of "."
4322 * and "~" causes trouble. */
4323 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4324 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 old_pos = curwin->w_cursor;
4326 save_p_ws = p_ws;
4327 save_p_scs = p_scs;
4328 p_ws = FALSE; /* don't wrap around end of file now */
4329 p_scs = FALSE; /* don't switch ignorecase off now */
4330
4331 /*
4332 * With "gD" go to line 1.
4333 * With "gd" Search back for the start of the current function, then go
4334 * back until a blank line. If this fails go to line 1.
4335 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004336 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 {
4338 setpcmark(); /* Set in findpar() otherwise */
4339 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004340 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342 else
4343 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004344 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4346 --curwin->w_cursor.lnum;
4347 }
4348 curwin->w_cursor.col = 0;
4349
4350 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004351 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004352 for (;;)
4353 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004354 valid = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004355 t = searchit(curwin, curbuf, &curwin->w_cursor, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004356 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004357 if (curwin->w_cursor.lnum >= old_pos.lnum)
4358 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004359
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004360 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004361 {
4362 pos_T *pos;
4363
4364 /* Check that the block the match is in doesn't end before the
4365 * position where we started the search from. */
4366 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4367 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4368 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004369 {
4370 /* There can't be a useful match before the end of this block.
4371 * Skip to the end. */
4372 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004373 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004374 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004375 }
4376
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377 if (t == FAIL)
4378 {
4379 /* If we previously found a valid position, use it. */
4380 if (found_pos.lnum != 0)
4381 {
4382 curwin->w_cursor = found_pos;
4383 t = OK;
4384 }
4385 break;
4386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004388 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004389 {
4390 /* Ignore this line, continue at start of next line. */
4391 ++curwin->w_cursor.lnum;
4392 curwin->w_cursor.col = 0;
4393 continue;
4394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004396 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4397
4398 /* If the current position is not a valid identifier and a previous
4399 * match is present, favor that one instead. */
4400 if (!valid && found_pos.lnum != 0)
4401 {
4402 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004403 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004404 }
4405
4406 /* Global search: use first valid match found */
4407 if (valid && !locally)
4408 break;
4409 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004410 {
4411 /* If we previously found a valid position, use it. */
4412 if (found_pos.lnum != 0)
4413 curwin->w_cursor = found_pos;
4414 break;
4415 }
4416
Bram Moolenaar226630a2016-10-08 19:21:31 +02004417 /* For finding a local variable and the match is before the "{" or
4418 * inside a comment, continue searching. For K&R style function
4419 * declarations this skips the function header without types. */
4420 if (!valid)
4421 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004422 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004423 }
4424 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004425 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004426 /* Remove SEARCH_START from flags to avoid getting stuck at one
4427 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004428 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004430
4431 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004433 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 curwin->w_cursor = old_pos;
4435 }
4436 else
4437 {
4438 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 /* "n" searches forward now */
4440 reset_search_dir();
4441 }
4442
4443 vim_free(pat);
4444 p_ws = save_p_ws;
4445 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004446
4447 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448}
4449
4450/*
4451 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4452 * lines rather than lines in the file.
4453 * 'dist' must be positive.
4454 *
4455 * Return OK if able to move cursor, FAIL otherwise.
4456 */
4457 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004458nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
4460 int linelen = linetabsize(ml_get_curline());
4461 int retval = OK;
4462 int atend = FALSE;
4463 int n;
4464 int col_off1; /* margin offset for first screen line */
4465 int col_off2; /* margin offset for wrapped screen line */
4466 int width1; /* text width for first screen line */
4467 int width2; /* test width for wrapped screen line */
4468
4469 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004470 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 col_off1 = curwin_col_off();
4473 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004474 width1 = curwin->w_width - col_off1;
4475 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004476 if (width2 == 0)
4477 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 /*
4482 * Instead of sticking at the last character of the buffer line we
4483 * try to stick in the last column of the screen.
4484 */
4485 if (curwin->w_curswant == MAXCOL)
4486 {
4487 atend = TRUE;
4488 validate_virtcol();
4489 if (width1 <= 0)
4490 curwin->w_curswant = 0;
4491 else
4492 {
4493 curwin->w_curswant = width1 - 1;
4494 if (curwin->w_virtcol > curwin->w_curswant)
4495 curwin->w_curswant += ((curwin->w_virtcol
4496 - curwin->w_curswant - 1) / width2 + 1) * width2;
4497 }
4498 }
4499 else
4500 {
4501 if (linelen > width1)
4502 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4503 else
4504 n = width1;
4505 if (curwin->w_curswant > (colnr_T)n + 1)
4506 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4507 * width2;
4508 }
4509
4510 while (dist--)
4511 {
4512 if (dir == BACKWARD)
4513 {
4514 if ((long)curwin->w_curswant >= width2)
4515 /* move back within line */
4516 curwin->w_curswant -= width2;
4517 else
4518 {
4519 /* to previous line */
4520 if (curwin->w_cursor.lnum == 1)
4521 {
4522 retval = FAIL;
4523 break;
4524 }
4525 --curwin->w_cursor.lnum;
4526#ifdef FEAT_FOLDING
4527 /* Move to the start of a closed fold. Don't do that when
4528 * 'foldopen' contains "all": it will open in a moment. */
4529 if (!(fdo_flags & FDO_ALL))
4530 (void)hasFolding(curwin->w_cursor.lnum,
4531 &curwin->w_cursor.lnum, NULL);
4532#endif
4533 linelen = linetabsize(ml_get_curline());
4534 if (linelen > width1)
4535 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4536 + 1) * width2;
4537 }
4538 }
4539 else /* dir == FORWARD */
4540 {
4541 if (linelen > width1)
4542 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4543 else
4544 n = width1;
4545 if (curwin->w_curswant + width2 < (colnr_T)n)
4546 /* move forward within line */
4547 curwin->w_curswant += width2;
4548 else
4549 {
4550 /* to next line */
4551#ifdef FEAT_FOLDING
4552 /* Move to the end of a closed fold. */
4553 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4554 &curwin->w_cursor.lnum);
4555#endif
4556 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4557 {
4558 retval = FAIL;
4559 break;
4560 }
4561 curwin->w_cursor.lnum++;
4562 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004563 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565 }
4566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004569 if (virtual_active() && atend)
4570 coladvance(MAXCOL);
4571 else
4572 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
4574#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4575 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4576 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004577 colnr_T virtcol;
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 /*
4580 * Check for landing on a character that got split at the end of the
4581 * last line. We want to advance a screenline, not end up in the same
4582 * screenline or move two screenlines.
4583 */
4584 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004585 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004586# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004587 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4588 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004589# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004590
4591 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 && (curwin->w_curswant < (colnr_T)width1
4593 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4594 : ((curwin->w_curswant - width1) % width2
4595 > (colnr_T)width2 / 2)))
4596 --curwin->w_cursor.col;
4597 }
4598#endif
4599
4600 if (atend)
4601 curwin->w_curswant = MAXCOL; /* stick in the last column */
4602
4603 return retval;
4604}
4605
4606#ifdef FEAT_MOUSE
4607/*
4608 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4609 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004610 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4611 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 */
4613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004614nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004616 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004618 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
4620 int row, col;
4621
4622 row = mouse_row;
4623 col = mouse_col;
4624
4625 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004626 wp = mouse_find_win(&row, &col);
4627 if (wp == NULL)
4628 return;
4629 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 curbuf = curwin->w_buffer;
4631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004633 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004635# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004636 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004637 /* This window is a terminal window, send the mouse event there.
4638 * Set "typed" to FALSE to avoid an endless loop. */
4639 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004640 else
4641# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004642 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4643 {
4644 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4645 }
4646 else
4647 {
4648 cap->count1 = 3;
4649 cap->count0 = 3;
4650 nv_scroll_line(cap);
4651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004653# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 else
4655 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004656 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4657 if (!curwin->w_p_wrap)
4658 {
4659 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004660
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004661 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004662 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004663 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4664 if (val < 0)
4665 val = 0;
4666
4667 gui_do_horiz_scroll(val, TRUE);
4668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 curwin->w_redr_status = TRUE;
4673
4674 curwin = old_curwin;
4675 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676}
4677
4678/*
4679 * Mouse clicks and drags.
4680 */
4681 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004682nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683{
4684 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4685}
4686#endif
4687
4688/*
4689 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4690 * cap->arg must be TRUE for CTRL-E.
4691 */
4692 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004693nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
4695 if (!checkclearop(cap->oap))
4696 scroll_redraw(cap->arg, cap->count1);
4697}
4698
4699/*
4700 * Scroll "count" lines up or down, and redraw.
4701 */
4702 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004703scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 linenr_T prev_topline = curwin->w_topline;
4706#ifdef FEAT_DIFF
4707 int prev_topfill = curwin->w_topfill;
4708#endif
4709 linenr_T prev_lnum = curwin->w_cursor.lnum;
4710
4711 if (up)
4712 scrollup(count, TRUE);
4713 else
4714 scrolldown(count, TRUE);
4715 if (p_so)
4716 {
4717 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4718 * valid, otherwise the screen jumps back at the end of the file. */
4719 cursor_correct();
4720 check_cursor_moved(curwin);
4721 curwin->w_valid |= VALID_TOPLINE;
4722
4723 /* If moved back to where we were, at least move the cursor, otherwise
4724 * we get stuck at one position. Don't move the cursor up if the
4725 * first line of the buffer is already on the screen */
4726 while (curwin->w_topline == prev_topline
4727#ifdef FEAT_DIFF
4728 && curwin->w_topfill == prev_topfill
4729#endif
4730 )
4731 {
4732 if (up)
4733 {
4734 if (curwin->w_cursor.lnum > prev_lnum
4735 || cursor_down(1L, FALSE) == FAIL)
4736 break;
4737 }
4738 else
4739 {
4740 if (curwin->w_cursor.lnum < prev_lnum
4741 || prev_topline == 1L
4742 || cursor_up(1L, FALSE) == FAIL)
4743 break;
4744 }
4745 /* Mark w_topline as valid, otherwise the screen jumps back at the
4746 * end of the file. */
4747 check_cursor_moved(curwin);
4748 curwin->w_valid |= VALID_TOPLINE;
4749 }
4750 }
4751 if (curwin->w_cursor.lnum != prev_lnum)
4752 coladvance(curwin->w_curswant);
4753 redraw_later(VALID);
4754}
4755
4756/*
4757 * Commands that start with "z".
4758 */
4759 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004760nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761{
4762 long n;
4763 colnr_T col;
4764 int nchar = cap->nchar;
4765#ifdef FEAT_FOLDING
4766 long old_fdl = curwin->w_p_fdl;
4767 int old_fen = curwin->w_p_fen;
4768#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004769#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004770 int undo = FALSE;
4771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
4773 if (VIM_ISDIGIT(nchar))
4774 {
4775 /*
4776 * "z123{nchar}": edit the count before obtaining {nchar}
4777 */
4778 if (checkclearop(cap->oap))
4779 return;
4780 n = nchar - '0';
4781 for (;;)
4782 {
4783#ifdef USE_ON_FLY_SCROLL
4784 dont_scroll = TRUE; /* disallow scrolling here */
4785#endif
4786 ++no_mapping;
4787 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004788 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 --no_mapping;
4791 --allow_keys;
4792#ifdef FEAT_CMDL_INFO
4793 (void)add_to_showcmd(nchar);
4794#endif
4795 if (nchar == K_DEL || nchar == K_KDEL)
4796 n /= 10;
4797 else if (VIM_ISDIGIT(nchar))
4798 n = n * 10 + (nchar - '0');
4799 else if (nchar == CAR)
4800 {
4801#ifdef FEAT_GUI
4802 need_mouse_correct = TRUE;
4803#endif
4804 win_setheight((int)n);
4805 break;
4806 }
4807 else if (nchar == 'l'
4808 || nchar == 'h'
4809 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004810 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
4812 cap->count1 = n ? n * cap->count1 : cap->count1;
4813 goto dozet;
4814 }
4815 else
4816 {
4817 clearopbeep(cap->oap);
4818 break;
4819 }
4820 }
4821 cap->oap->op_type = OP_NOP;
4822 return;
4823 }
4824
4825dozet:
4826 if (
4827#ifdef FEAT_FOLDING
4828 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4829 * and "zC" only in Visual mode. "zj" and "zk" are motion
4830 * commands. */
4831 cap->nchar != 'f' && cap->nchar != 'F'
4832 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4833 && cap->nchar != 'j' && cap->nchar != 'k'
4834 &&
4835#endif
4836 checkclearop(cap->oap))
4837 return;
4838
4839 /*
4840 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4841 * If line number given, set cursor.
4842 */
4843 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4844 && cap->count0
4845 && cap->count0 != curwin->w_cursor.lnum)
4846 {
4847 setpcmark();
4848 if (cap->count0 > curbuf->b_ml.ml_line_count)
4849 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4850 else
4851 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004852 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854
4855 switch (nchar)
4856 {
4857 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4858 case '+':
4859 if (cap->count0 == 0)
4860 {
4861 /* No count given: put cursor at the line below screen */
4862 validate_botline(); /* make sure w_botline is valid */
4863 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4864 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4865 else
4866 curwin->w_cursor.lnum = curwin->w_botline;
4867 }
4868 /* FALLTHROUGH */
4869 case NL:
4870 case CAR:
4871 case K_KENTER:
4872 beginline(BL_WHITE | BL_FIX);
4873 /* FALLTHROUGH */
4874
4875 case 't': scroll_cursor_top(0, TRUE);
4876 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004877 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 break;
4879
4880 /* "z." and "zz": put cursor in middle of screen */
4881 case '.': beginline(BL_WHITE | BL_FIX);
4882 /* FALLTHROUGH */
4883
4884 case 'z': scroll_cursor_halfway(TRUE);
4885 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004886 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 break;
4888
4889 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4890 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4891 * when <count> is at bottom of window, and puts that one at
4892 * bottom of window. */
4893 if (cap->count0 != 0)
4894 {
4895 scroll_cursor_bot(0, TRUE);
4896 curwin->w_cursor.lnum = curwin->w_topline;
4897 }
4898 else if (curwin->w_topline == 1)
4899 curwin->w_cursor.lnum = 1;
4900 else
4901 curwin->w_cursor.lnum = curwin->w_topline - 1;
4902 /* FALLTHROUGH */
4903 case '-':
4904 beginline(BL_WHITE | BL_FIX);
4905 /* FALLTHROUGH */
4906
4907 case 'b': scroll_cursor_bot(0, TRUE);
4908 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004909 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 break;
4911
4912 /* "zH" - scroll screen right half-page */
4913 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004914 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 /* FALLTHROUGH */
4916
4917 /* "zh" - scroll screen to the right */
4918 case 'h':
4919 case K_LEFT:
4920 if (!curwin->w_p_wrap)
4921 {
4922 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4923 curwin->w_leftcol = 0;
4924 else
4925 curwin->w_leftcol -= (colnr_T)cap->count1;
4926 leftcol_changed();
4927 }
4928 break;
4929
4930 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004931 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 /* FALLTHROUGH */
4933
4934 /* "zl" - scroll screen to the left */
4935 case 'l':
4936 case K_RIGHT:
4937 if (!curwin->w_p_wrap)
4938 {
4939 /* scroll the window left */
4940 curwin->w_leftcol += (colnr_T)cap->count1;
4941 leftcol_changed();
4942 }
4943 break;
4944
4945 /* "zs" - scroll screen, cursor at the start */
4946 case 's': if (!curwin->w_p_wrap)
4947 {
4948#ifdef FEAT_FOLDING
4949 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4950 col = 0; /* like the cursor is in col 0 */
4951 else
4952#endif
4953 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4954 if ((long)col > p_siso)
4955 col -= p_siso;
4956 else
4957 col = 0;
4958 if (curwin->w_leftcol != col)
4959 {
4960 curwin->w_leftcol = col;
4961 redraw_later(NOT_VALID);
4962 }
4963 }
4964 break;
4965
4966 /* "ze" - scroll screen, cursor at the end */
4967 case 'e': if (!curwin->w_p_wrap)
4968 {
4969#ifdef FEAT_FOLDING
4970 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4971 col = 0; /* like the cursor is in col 0 */
4972 else
4973#endif
4974 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004975 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if ((long)col + p_siso < n)
4977 col = 0;
4978 else
4979 col = col + p_siso - n + 1;
4980 if (curwin->w_leftcol != col)
4981 {
4982 curwin->w_leftcol = col;
4983 redraw_later(NOT_VALID);
4984 }
4985 }
4986 break;
4987
4988#ifdef FEAT_FOLDING
4989 /* "zF": create fold command */
4990 /* "zf": create fold operator */
4991 case 'F':
4992 case 'f': if (foldManualAllowed(TRUE))
4993 {
4994 cap->nchar = 'f';
4995 nv_operator(cap);
4996 curwin->w_p_fen = TRUE;
4997
4998 /* "zF" is like "zfzf" */
4999 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
5000 {
5001 nv_operator(cap);
5002 finish_op = TRUE;
5003 }
5004 }
5005 else
5006 clearopbeep(cap->oap);
5007 break;
5008
5009 /* "zd": delete fold at cursor */
5010 /* "zD": delete fold at cursor recursively */
5011 case 'd':
5012 case 'D': if (foldManualAllowed(FALSE))
5013 {
5014 if (VIsual_active)
5015 nv_operator(cap);
5016 else
5017 deleteFold(curwin->w_cursor.lnum,
5018 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5019 }
5020 break;
5021
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005022 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 case 'E': if (foldmethodIsManual(curwin))
5024 {
5025 clearFolding(curwin);
5026 changed_window_setting();
5027 }
5028 else if (foldmethodIsMarker(curwin))
5029 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5030 TRUE, FALSE);
5031 else
5032 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5033 break;
5034
5035 /* "zn": fold none: reset 'foldenable' */
5036 case 'n': curwin->w_p_fen = FALSE;
5037 break;
5038
5039 /* "zN": fold Normal: set 'foldenable' */
5040 case 'N': curwin->w_p_fen = TRUE;
5041 break;
5042
5043 /* "zi": invert folding: toggle 'foldenable' */
5044 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5045 break;
5046
5047 /* "za": open closed fold or close open fold at cursor */
5048 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5049 openFold(curwin->w_cursor.lnum, cap->count1);
5050 else
5051 {
5052 closeFold(curwin->w_cursor.lnum, cap->count1);
5053 curwin->w_p_fen = TRUE;
5054 }
5055 break;
5056
5057 /* "zA": open fold at cursor recursively */
5058 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5059 openFoldRecurse(curwin->w_cursor.lnum);
5060 else
5061 {
5062 closeFoldRecurse(curwin->w_cursor.lnum);
5063 curwin->w_p_fen = TRUE;
5064 }
5065 break;
5066
5067 /* "zo": open fold at cursor or Visual area */
5068 case 'o': if (VIsual_active)
5069 nv_operator(cap);
5070 else
5071 openFold(curwin->w_cursor.lnum, cap->count1);
5072 break;
5073
5074 /* "zO": open fold recursively */
5075 case 'O': if (VIsual_active)
5076 nv_operator(cap);
5077 else
5078 openFoldRecurse(curwin->w_cursor.lnum);
5079 break;
5080
5081 /* "zc": close fold at cursor or Visual area */
5082 case 'c': if (VIsual_active)
5083 nv_operator(cap);
5084 else
5085 closeFold(curwin->w_cursor.lnum, cap->count1);
5086 curwin->w_p_fen = TRUE;
5087 break;
5088
5089 /* "zC": close fold recursively */
5090 case 'C': if (VIsual_active)
5091 nv_operator(cap);
5092 else
5093 closeFoldRecurse(curwin->w_cursor.lnum);
5094 curwin->w_p_fen = TRUE;
5095 break;
5096
5097 /* "zv": open folds at the cursor */
5098 case 'v': foldOpenCursor();
5099 break;
5100
5101 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5102 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005103 curwin->w_foldinvalid = TRUE; /* recompute folds */
5104 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 foldOpenCursor();
5106 break;
5107
5108 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5109 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005110 curwin->w_foldinvalid = TRUE; /* recompute folds */
5111 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 break;
5113
5114 /* "zm": fold more */
5115 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005116 {
5117 curwin->w_p_fdl -= cap->count1;
5118 if (curwin->w_p_fdl < 0)
5119 curwin->w_p_fdl = 0;
5120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 old_fdl = -1; /* force an update */
5122 curwin->w_p_fen = TRUE;
5123 break;
5124
5125 /* "zM": close all folds */
5126 case 'M': curwin->w_p_fdl = 0;
5127 old_fdl = -1; /* force an update */
5128 curwin->w_p_fen = TRUE;
5129 break;
5130
5131 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005132 case 'r': curwin->w_p_fdl += cap->count1;
5133 {
5134 int d = getDeepestNesting();
5135
5136 if (curwin->w_p_fdl >= d)
5137 curwin->w_p_fdl = d;
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 break;
5140
5141 /* "zR": open all folds */
5142 case 'R': curwin->w_p_fdl = getDeepestNesting();
5143 old_fdl = -1; /* force an update */
5144 break;
5145
5146 case 'j': /* "zj" move to next fold downwards */
5147 case 'k': /* "zk" move to next fold upwards */
5148 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5149 cap->count1) == FAIL)
5150 clearopbeep(cap->oap);
5151 break;
5152
5153#endif /* FEAT_FOLDING */
5154
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005155#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005156 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5157 ++no_mapping;
5158 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005159 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005160 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005161 --no_mapping;
5162 --allow_keys;
5163#ifdef FEAT_CMDL_INFO
5164 (void)add_to_showcmd(nchar);
5165#endif
5166 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5167 {
5168 clearopbeep(cap->oap);
5169 break;
5170 }
5171 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005172 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005173
Bram Moolenaarb765d632005-06-07 21:00:02 +00005174 case 'g': /* "zg": add good word to word list */
5175 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005176 case 'G': /* "zG": add good word to temp word list */
5177 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005178 {
5179 char_u *ptr = NULL;
5180 int len;
5181
5182 if (checkclearop(cap->oap))
5183 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005184 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5185 == FAIL)
5186 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005187 if (ptr == NULL)
5188 {
5189 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005190
Bram Moolenaar134bf072013-09-25 18:54:24 +02005191 /* Find bad word under the cursor. When 'spell' is
5192 * off this fails and find_ident_under_cursor() is
5193 * used below. */
5194 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005195 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005196 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005197 if (len != 0 && curwin->w_cursor.col <= pos.col)
5198 ptr = ml_get_pos(&curwin->w_cursor);
5199 curwin->w_cursor = pos;
5200 }
5201
Bram Moolenaarb765d632005-06-07 21:00:02 +00005202 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5203 FIND_IDENT)) == 0)
5204 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005205 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005206 (nchar == 'G' || nchar == 'W')
5207 ? 0 : (int)cap->count1,
5208 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005209 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005210 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211
Bram Moolenaar43abc522005-12-10 20:15:02 +00005212 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005213 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005214 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005215 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005216#endif
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 default: clearopbeep(cap->oap);
5219 }
5220
5221#ifdef FEAT_FOLDING
5222 /* Redraw when 'foldenable' changed */
5223 if (old_fen != curwin->w_p_fen)
5224 {
5225# ifdef FEAT_DIFF
5226 win_T *wp;
5227
5228 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5229 {
5230 /* Adjust 'foldenable' in diff-synced windows. */
5231 FOR_ALL_WINDOWS(wp)
5232 {
5233 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5234 {
5235 wp->w_p_fen = curwin->w_p_fen;
5236 changed_window_setting_win(wp);
5237 }
5238 }
5239 }
5240# endif
5241 changed_window_setting();
5242 }
5243
5244 /* Redraw when 'foldlevel' changed. */
5245 if (old_fdl != curwin->w_p_fdl)
5246 newFoldLevel();
5247#endif
5248}
5249
5250#ifdef FEAT_GUI
5251/*
5252 * Vertical scrollbar movement.
5253 */
5254 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005255nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256{
5257 if (cap->oap->op_type != OP_NOP)
5258 clearopbeep(cap->oap);
5259
5260 /* Even if an operator was pending, we still want to scroll */
5261 gui_do_scroll();
5262}
5263
5264/*
5265 * Horizontal scrollbar movement.
5266 */
5267 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005268nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269{
5270 if (cap->oap->op_type != OP_NOP)
5271 clearopbeep(cap->oap);
5272
5273 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005274 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275}
5276#endif
5277
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005278#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005279/*
5280 * Click in GUI tab.
5281 */
5282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005283nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005284{
5285 if (cap->oap->op_type != OP_NOP)
5286 clearopbeep(cap->oap);
5287
5288 /* Even if an operator was pending, we still want to jump tabs. */
5289 goto_tabpage(current_tab);
5290}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005291
5292/*
5293 * Selected item in tab line menu.
5294 */
5295 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005296nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005297{
5298 if (cap->oap->op_type != OP_NOP)
5299 clearopbeep(cap->oap);
5300
5301 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005302 handle_tabmenu();
5303}
5304
5305/*
5306 * Handle selecting an item of the GUI tab line menu.
5307 * Used in Normal and Insert mode.
5308 */
5309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005310handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005311{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005312 switch (current_tabmenu)
5313 {
5314 case TABLINE_MENU_CLOSE:
5315 if (current_tab == 0)
5316 do_cmdline_cmd((char_u *)"tabclose");
5317 else
5318 {
5319 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5320 current_tab);
5321 do_cmdline_cmd(IObuff);
5322 }
5323 break;
5324
5325 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005326 if (current_tab == 0)
5327 do_cmdline_cmd((char_u *)"$tabnew");
5328 else
5329 {
5330 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5331 current_tab - 1);
5332 do_cmdline_cmd(IObuff);
5333 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005334 break;
5335
5336 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005337 if (current_tab == 0)
5338 do_cmdline_cmd((char_u *)"browse $tabnew");
5339 else
5340 {
5341 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5342 current_tab - 1);
5343 do_cmdline_cmd(IObuff);
5344 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005345 break;
5346 }
5347}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005348#endif
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350/*
5351 * "Q" command.
5352 */
5353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005354nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 /*
5357 * Ignore 'Q' in Visual mode, just give a beep.
5358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005360 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005361 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 do_exmode(FALSE);
5363}
5364
5365/*
5366 * Handle a ":" command.
5367 */
5368 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005369nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
5371 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005372 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (VIsual_active)
5375 nv_operator(cap);
5376 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
5378 if (cap->oap->op_type != OP_NOP)
5379 {
5380 /* Using ":" as a movement is characterwise exclusive. */
5381 cap->oap->motion_type = MCHAR;
5382 cap->oap->inclusive = FALSE;
5383 }
5384 else if (cap->count0)
5385 {
5386 /* translate "count:" into ":.,.+(count - 1)" */
5387 stuffcharReadbuff('.');
5388 if (cap->count0 > 1)
5389 {
5390 stuffReadbuff((char_u *)",.+");
5391 stuffnumReadbuff((long)cap->count0 - 1L);
5392 }
5393 }
5394
5395 /* When typing, don't type below an old message */
5396 if (KeyTyped)
5397 compute_cmdrow();
5398
5399 old_p_im = p_im;
5400
5401 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005402 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5404
5405 /* If 'insertmode' changed, enter or exit Insert mode */
5406 if (p_im != old_p_im)
5407 {
5408 if (p_im)
5409 restart_edit = 'i';
5410 else
5411 restart_edit = 0;
5412 }
5413
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005414 if (cmd_result == FAIL)
5415 /* The Ex command failed, do not execute the operator. */
5416 clearop(cap->oap);
5417 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5419 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005420 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5421 || did_emsg
5422 ))
5423 /* The start of the operator has become invalid by the Ex command.
5424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 clearopbeep(cap->oap);
5426 }
5427}
5428
5429/*
5430 * Handle CTRL-G command.
5431 */
5432 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005433nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 if (VIsual_active) /* toggle Selection/Visual mode */
5436 {
5437 VIsual_select = !VIsual_select;
5438 showmode();
5439 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005440 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 /* print full name if count given or :cd used */
5442 fileinfo((int)cap->count0, FALSE, TRUE);
5443}
5444
5445/*
5446 * Handle CTRL-H <Backspace> command.
5447 */
5448 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005449nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 if (VIsual_active && VIsual_select)
5452 {
5453 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5454 v_visop(cap);
5455 }
5456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 nv_left(cap);
5458}
5459
5460/*
5461 * CTRL-L: clear screen and redraw.
5462 */
5463 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005464nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465{
5466 if (!checkclearop(cap->oap))
5467 {
5468#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5469 /*
5470 * Right now, the BeBox doesn't seem to have an easy way to detect
5471 * window resizing, so we cheat and make the user detect it
5472 * manually with CTRL-L instead
5473 */
5474 ui_get_shellsize();
5475#endif
5476#ifdef FEAT_SYN_HL
5477 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005478 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005479# ifdef FEAT_RELTIME
5480 {
5481 win_T *wp;
5482
5483 FOR_ALL_WINDOWS(wp)
5484 wp->w_s->b_syn_slow = FALSE;
5485 }
5486# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487#endif
5488 redraw_later(CLEAR);
5489 }
5490}
5491
5492/*
5493 * CTRL-O: In Select mode: switch to Visual mode for one command.
5494 * Otherwise: Go to older pcmark.
5495 */
5496 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005497nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 if (VIsual_active && VIsual_select)
5500 {
5501 VIsual_select = FALSE;
5502 showmode();
5503 restart_VIsual_select = 2; /* restart Select mode later */
5504 }
5505 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 cap->count1 = -cap->count1;
5508 nv_pcmark(cap);
5509 }
5510}
5511
5512/*
5513 * CTRL-^ command, short for ":e #"
5514 */
5515 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005516nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 if (!checkclearopq(cap->oap))
5519 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5520 GETF_SETMARK|GETF_ALT, FALSE);
5521}
5522
5523/*
5524 * "Z" commands.
5525 */
5526 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005527nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529 if (!checkclearopq(cap->oap))
5530 {
5531 switch (cap->nchar)
5532 {
5533 /* "ZZ": equivalent to ":x". */
5534 case 'Z': do_cmdline_cmd((char_u *)"x");
5535 break;
5536
5537 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5538 case 'Q': do_cmdline_cmd((char_u *)"q!");
5539 break;
5540
5541 default: clearopbeep(cap->oap);
5542 }
5543 }
5544}
5545
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546/*
5547 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5548 */
5549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005550do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551{
5552 oparg_T oa;
5553 cmdarg_T ca;
5554
5555 clear_oparg(&oa);
5556 vim_memset(&ca, 0, sizeof(ca));
5557 ca.oap = &oa;
5558 ca.cmdchar = c1;
5559 ca.nchar = c2;
5560 nv_ident(&ca);
5561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563/*
5564 * Handle the commands that use the word under the cursor.
5565 * [g] CTRL-] :ta to current identifier
5566 * [g] 'K' run program for current identifier
5567 * [g] '*' / to current identifier or string
5568 * [g] '#' ? to current identifier or string
5569 * g ']' :tselect for current identifier
5570 */
5571 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005572nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
5574 char_u *ptr = NULL;
5575 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005576 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005577 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 char_u *p;
5579 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005580 int kp_help; /* 'keywordprg' is ":he" */
5581 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 int n = 0; /* init for GCC */
5583 int cmdchar;
5584 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005585 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 char_u *aux_ptr;
5587 int isman;
5588 int isman_s;
5589
5590 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5591 {
5592 cmdchar = cap->nchar;
5593 g_cmd = TRUE;
5594 }
5595 else
5596 {
5597 cmdchar = cap->cmdchar;
5598 g_cmd = FALSE;
5599 }
5600
5601 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5602 cmdchar = '#';
5603
5604 /*
5605 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5606 */
5607 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5610 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if (checkclearopq(cap->oap))
5612 return;
5613 }
5614
5615 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5616 (cmdchar == '*' || cmdchar == '#')
5617 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5618 {
5619 clearop(cap->oap);
5620 return;
5621 }
5622
5623 /* Allocate buffer to put the command in. Inserting backslashes can
5624 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5625 * and some numbers. */
5626 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5627 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5628 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005629 if (kp_help && *skipwhite(ptr) == NUL)
5630 {
5631 EMSG(_(e_noident)); /* found white space only */
5632 return;
5633 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005634 kp_ex = (*kp == ':');
5635 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5636 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (buf == NULL)
5638 return;
5639 buf[0] = NUL;
5640
5641 switch (cmdchar)
5642 {
5643 case '*':
5644 case '#':
5645 /*
5646 * Put cursor at start of word, makes search skip the word
5647 * under the cursor.
5648 * Call setpcmark() first, so "*``" puts the cursor back where
5649 * it was.
5650 */
5651 setpcmark();
5652 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5653
5654 if (!g_cmd && vim_iswordp(ptr))
5655 STRCPY(buf, "\\<");
5656 no_smartcase = TRUE; /* don't use 'smartcase' now */
5657 break;
5658
5659 case 'K':
5660 if (kp_help)
5661 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005662 else if (kp_ex)
5663 {
5664 if (cap->count0 != 0)
5665 vim_snprintf((char *)buf, buflen, "%s %ld",
5666 kp, cap->count0);
5667 else
5668 STRCPY(buf, kp);
5669 STRCAT(buf, " ");
5670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 else
5672 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005673 /* An external command will probably use an argument starting
5674 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005675 while (*ptr == '-' && n > 0)
5676 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005677 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005678 --n;
5679 }
5680 if (n == 0)
5681 {
5682 EMSG(_(e_noident)); /* found dashes only */
5683 vim_free(buf);
5684 return;
5685 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005686
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 /* When a count is given, turn it into a range. Is this
5688 * really what we want? */
5689 isman = (STRCMP(kp, "man") == 0);
5690 isman_s = (STRCMP(kp, "man -s") == 0);
5691 if (cap->count0 != 0 && !(isman || isman_s))
5692 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5693
5694 STRCAT(buf, "! ");
5695 if (cap->count0 == 0 && isman_s)
5696 STRCAT(buf, "man");
5697 else
5698 STRCAT(buf, kp);
5699 STRCAT(buf, " ");
5700 if (cap->count0 != 0 && (isman || isman_s))
5701 {
5702 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5703 STRCAT(buf, " ");
5704 }
5705 }
5706 break;
5707
5708 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005709 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710#ifdef FEAT_CSCOPE
5711 if (p_cst)
5712 STRCPY(buf, "cstag ");
5713 else
5714#endif
5715 STRCPY(buf, "ts ");
5716 break;
5717
5718 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005719 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 if (curbuf->b_help)
5721 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005723 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005724 if (g_cmd)
5725 STRCPY(buf, "tj ");
5726 else
5727 sprintf((char *)buf, "%ldta ", cap->count0);
5728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 }
5730
5731 /*
5732 * Now grab the chars in the identifier
5733 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005734 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005736 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005737 if (kp_ex)
5738 /* Escape the argument properly for an Ex command */
5739 p = vim_strsave_fnameescape(ptr, FALSE);
5740 else
5741 /* Escape the argument properly for a shell command */
5742 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005743 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005744 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005746 vim_free(buf);
5747 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005749 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5750 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005751 {
5752 vim_free(buf);
5753 vim_free(p);
5754 return;
5755 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005756 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005757 STRCAT(buf, p);
5758 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005760 else
5761 {
5762 if (cmdchar == '*')
5763 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5764 else if (cmdchar == '#')
5765 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005766 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005767 {
5768 if (curbuf->b_help)
5769 /* ":help" handles unescaped argument */
5770 aux_ptr = (char_u *)"";
5771 else
5772 aux_ptr = (char_u *)"\\|\"\n[";
5773 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005774 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005775 aux_ptr = (char_u *)"\\|\"\n*?[";
5776
5777 p = buf + STRLEN(buf);
5778 while (n-- > 0)
5779 {
5780 /* put a backslash before \ and some others */
5781 if (vim_strchr(aux_ptr, *ptr) != NULL)
5782 *p++ = '\\';
5783#ifdef FEAT_MBYTE
5784 /* When current byte is a part of multibyte character, copy all
5785 * bytes of that character. */
5786 if (has_mbyte)
5787 {
5788 int i;
5789 int len = (*mb_ptr2len)(ptr) - 1;
5790
5791 for (i = 0; i < len && n >= 1; ++i, --n)
5792 *p++ = *ptr++;
5793 }
5794#endif
5795 *p++ = *ptr++;
5796 }
5797 *p = NUL;
5798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
5800 /*
5801 * Execute the command.
5802 */
5803 if (cmdchar == '*' || cmdchar == '#')
5804 {
5805 if (!g_cmd && (
5806#ifdef FEAT_MBYTE
5807 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5808#endif
5809 vim_iswordc(ptr[-1])))
5810 STRCAT(buf, "\\>");
5811#ifdef FEAT_CMDHIST
5812 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005813 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5815#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005816 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
5818 else
5819 do_cmdline_cmd(buf);
5820
5821 vim_free(buf);
5822}
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824/*
5825 * Get visually selected text, within one line only.
5826 * Returns FAIL if more than one line selected.
5827 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005828 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005829get_visual_text(
5830 cmdarg_T *cap,
5831 char_u **pp, /* return: start of selected text */
5832 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
5834 if (VIsual_mode != 'V')
5835 unadjust_for_sel();
5836 if (VIsual.lnum != curwin->w_cursor.lnum)
5837 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005838 if (cap != NULL)
5839 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 return FAIL;
5841 }
5842 if (VIsual_mode == 'V')
5843 {
5844 *pp = ml_get_curline();
5845 *lenp = (int)STRLEN(*pp);
5846 }
5847 else
5848 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005849 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 {
5851 *pp = ml_get_pos(&curwin->w_cursor);
5852 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5853 }
5854 else
5855 {
5856 *pp = ml_get_pos(&VIsual);
5857 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5858 }
5859#ifdef FEAT_MBYTE
5860 if (has_mbyte)
5861 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005862 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863#endif
5864 }
5865 reset_VIsual_and_resel();
5866 return OK;
5867}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
5869/*
5870 * CTRL-T: backwards in tag stack
5871 */
5872 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005873nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 if (!checkclearopq(cap->oap))
5876 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5877}
5878
5879/*
5880 * Handle scrolling command 'H', 'L' and 'M'.
5881 */
5882 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005883nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884{
5885 int used = 0;
5886 long n;
5887#ifdef FEAT_FOLDING
5888 linenr_T lnum;
5889#endif
5890 int half;
5891
5892 cap->oap->motion_type = MLINE;
5893 setpcmark();
5894
5895 if (cap->cmdchar == 'L')
5896 {
5897 validate_botline(); /* make sure curwin->w_botline is valid */
5898 curwin->w_cursor.lnum = curwin->w_botline - 1;
5899 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5900 curwin->w_cursor.lnum = 1;
5901 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005902 {
5903#ifdef FEAT_FOLDING
5904 if (hasAnyFolding(curwin))
5905 {
5906 /* Count a fold for one screen line. */
5907 for (n = cap->count1 - 1; n > 0
5908 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5909 {
5910 (void)hasFolding(curwin->w_cursor.lnum,
5911 &curwin->w_cursor.lnum, NULL);
5912 --curwin->w_cursor.lnum;
5913 }
5914 }
5915 else
5916#endif
5917 curwin->w_cursor.lnum -= cap->count1 - 1;
5918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 }
5920 else
5921 {
5922 if (cap->cmdchar == 'M')
5923 {
5924#ifdef FEAT_DIFF
5925 /* Don't count filler lines above the window. */
5926 used -= diff_check_fill(curwin, curwin->w_topline)
5927 - curwin->w_topfill;
5928#endif
5929 validate_botline(); /* make sure w_empty_rows is valid */
5930 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5931 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5932 {
5933#ifdef FEAT_DIFF
5934 /* Count half he number of filler lines to be "below this
5935 * line" and half to be "above the next line". */
5936 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5937 + n) / 2 >= half)
5938 {
5939 --n;
5940 break;
5941 }
5942#endif
5943 used += plines(curwin->w_topline + n);
5944 if (used >= half)
5945 break;
5946#ifdef FEAT_FOLDING
5947 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5948 n = lnum - curwin->w_topline;
5949#endif
5950 }
5951 if (n > 0 && used > curwin->w_height)
5952 --n;
5953 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005954 else /* (cap->cmdchar == 'H') */
5955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005957#ifdef FEAT_FOLDING
5958 if (hasAnyFolding(curwin))
5959 {
5960 /* Count a fold for one screen line. */
5961 lnum = curwin->w_topline;
5962 while (n-- > 0 && lnum < curwin->w_botline - 1)
5963 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005964 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005965 ++lnum;
5966 }
5967 n = lnum - curwin->w_topline;
5968 }
5969#endif
5970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 curwin->w_cursor.lnum = curwin->w_topline + n;
5972 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5973 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5974 }
5975
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005976 /* Correct for 'so', except when an operator is pending. */
5977 if (cap->oap->op_type == OP_NOP)
5978 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 beginline(BL_SOL | BL_FIX);
5980}
5981
5982/*
5983 * Cursor right commands.
5984 */
5985 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005986nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
5988 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005989 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005991 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5992 {
5993 /* <C-Right> and <S-Right> move a word or WORD right */
5994 if (mod_mask & MOD_MASK_CTRL)
5995 cap->arg = TRUE;
5996 nv_wordcmd(cap);
5997 return;
5998 }
5999
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 cap->oap->motion_type = MCHAR;
6001 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006002 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006004#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006006 * In virtual edit mode, there's no such thing as "past_line", as lines
6007 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 */
6009 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006010 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011#endif
6012
6013 for (n = cap->count1; n > 0; --n)
6014 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006015 if ((!past_line && oneright() == FAIL)
6016 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006017 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 {
6019 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006020 * <Space> wraps to next line if 'whichwrap' has 's'.
6021 * 'l' wraps to next line if 'whichwrap' has 'l'.
6022 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 */
6024 if ( ((cap->cmdchar == ' '
6025 && vim_strchr(p_ww, 's') != NULL)
6026 || (cap->cmdchar == 'l'
6027 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006028 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 && vim_strchr(p_ww, '>') != NULL))
6030 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6031 {
6032 /* When deleting we also count the NL as a character.
6033 * Set cap->oap->inclusive when last char in the line is
6034 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006035 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006037 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 cap->oap->inclusive = TRUE;
6039 else
6040 {
6041 ++curwin->w_cursor.lnum;
6042 curwin->w_cursor.col = 0;
6043#ifdef FEAT_VIRTUALEDIT
6044 curwin->w_cursor.coladd = 0;
6045#endif
6046 curwin->w_set_curswant = TRUE;
6047 cap->oap->inclusive = FALSE;
6048 }
6049 continue;
6050 }
6051 if (cap->oap->op_type == OP_NOP)
6052 {
6053 /* Only beep and flush if not moved at all */
6054 if (n == cap->count1)
6055 beep_flush();
6056 }
6057 else
6058 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006059 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 cap->oap->inclusive = TRUE;
6061 }
6062 break;
6063 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006064 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 {
6066 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006067#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (virtual_active())
6069 oneright();
6070 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006073#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if (has_mbyte)
6075 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006076 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 ++curwin->w_cursor.col;
6080 }
6081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 }
6083#ifdef FEAT_FOLDING
6084 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6085 && cap->oap->op_type == OP_NOP)
6086 foldOpenCursor();
6087#endif
6088}
6089
6090/*
6091 * Cursor left commands.
6092 *
6093 * Returns TRUE when operator end should not be adjusted.
6094 */
6095 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006096nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 long n;
6099
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006100 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6101 {
6102 /* <C-Left> and <S-Left> move a word or WORD left */
6103 if (mod_mask & MOD_MASK_CTRL)
6104 cap->arg = 1;
6105 nv_bck_word(cap);
6106 return;
6107 }
6108
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 cap->oap->motion_type = MCHAR;
6110 cap->oap->inclusive = FALSE;
6111 for (n = cap->count1; n > 0; --n)
6112 {
6113 if (oneleft() == FAIL)
6114 {
6115 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6116 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6117 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6118 */
6119 if ( (((cap->cmdchar == K_BS
6120 || cap->cmdchar == Ctrl_H)
6121 && vim_strchr(p_ww, 'b') != NULL)
6122 || (cap->cmdchar == 'h'
6123 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006124 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 && vim_strchr(p_ww, '<') != NULL))
6126 && curwin->w_cursor.lnum > 1)
6127 {
6128 --(curwin->w_cursor.lnum);
6129 coladvance((colnr_T)MAXCOL);
6130 curwin->w_set_curswant = TRUE;
6131
6132 /* When the NL before the first char has to be deleted we
6133 * put the cursor on the NUL after the previous line.
6134 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006135 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if ( (cap->oap->op_type == OP_DELETE
6137 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006138 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006140 char_u *cp = ml_get_cursor();
6141
6142 if (*cp != NUL)
6143 {
6144#ifdef FEAT_MBYTE
6145 if (has_mbyte)
6146 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6147 else
6148#endif
6149 ++curwin->w_cursor.col;
6150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 cap->retval |= CA_NO_ADJ_OP_END;
6152 }
6153 continue;
6154 }
6155 /* Only beep and flush if not moved at all */
6156 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6157 beep_flush();
6158 break;
6159 }
6160 }
6161#ifdef FEAT_FOLDING
6162 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6163 && cap->oap->op_type == OP_NOP)
6164 foldOpenCursor();
6165#endif
6166}
6167
6168/*
6169 * Cursor up commands.
6170 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6171 */
6172 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006173nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006175 if (mod_mask & MOD_MASK_SHIFT)
6176 {
6177 /* <S-Up> is page up */
6178 cap->arg = BACKWARD;
6179 nv_page(cap);
6180 }
6181 else
6182 {
6183 cap->oap->motion_type = MLINE;
6184 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6185 clearopbeep(cap->oap);
6186 else if (cap->arg)
6187 beginline(BL_WHITE | BL_FIX);
6188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189}
6190
6191/*
6192 * Cursor down commands.
6193 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6194 */
6195 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006196nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006198 if (mod_mask & MOD_MASK_SHIFT)
6199 {
6200 /* <S-Down> is page down */
6201 cap->arg = FORWARD;
6202 nv_page(cap);
6203 }
6204 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02006205#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 /* In a quickfix window a <CR> jumps to the error under the cursor. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006207 if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
Bram Moolenaar74a47162017-02-26 19:09:05 +01006208 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006209 if (curwin->w_llist_ref == NULL)
6210 do_cmdline_cmd((char_u *)".cc"); /* quickfix window */
6211 else
6212 do_cmdline_cmd((char_u *)".ll"); /* location list window */
Bram Moolenaar74a47162017-02-26 19:09:05 +01006213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 else
6215#endif
6216 {
6217#ifdef FEAT_CMDWIN
6218 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006219 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 cmdwin_result = CAR;
6221 else
6222#endif
6223 {
6224 cap->oap->motion_type = MLINE;
6225 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6226 clearopbeep(cap->oap);
6227 else if (cap->arg)
6228 beginline(BL_WHITE | BL_FIX);
6229 }
6230 }
6231}
6232
6233#ifdef FEAT_SEARCHPATH
6234/*
6235 * Grab the file name under the cursor and edit it.
6236 */
6237 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006238nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
6240 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006241 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006243 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
6245 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006246 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 return;
6248 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006249 if (curbuf_locked())
6250 {
6251 clearop(cap->oap);
6252 return;
6253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006255 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
6257 if (ptr != NULL)
6258 {
6259 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006260 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006261 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006263 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006264 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006265 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006266 {
6267 curwin->w_cursor.lnum = lnum;
6268 check_cursor_lnum();
6269 beginline(BL_SOL | BL_FIX);
6270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 vim_free(ptr);
6272 }
6273 else
6274 clearop(cap->oap);
6275}
6276#endif
6277
6278/*
6279 * <End> command: to end of current line or last line.
6280 */
6281 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006282nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006284 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006286 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 nv_goto(cap);
6288 cap->count1 = 1; /* to end of current line */
6289 }
6290 nv_dollar(cap);
6291}
6292
6293/*
6294 * Handle the "$" command.
6295 */
6296 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006297nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
6299 cap->oap->motion_type = MCHAR;
6300 cap->oap->inclusive = TRUE;
6301#ifdef FEAT_VIRTUALEDIT
6302 /* In virtual mode when off the edge of a line and an operator
6303 * is pending (whew!) keep the cursor where it is.
6304 * Otherwise, send it to the end of the line. */
6305 if (!virtual_active() || gchar_cursor() != NUL
6306 || cap->oap->op_type == OP_NOP)
6307#endif
6308 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6309 if (cursor_down((long)(cap->count1 - 1),
6310 cap->oap->op_type == OP_NOP) == FAIL)
6311 clearopbeep(cap->oap);
6312#ifdef FEAT_FOLDING
6313 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6314 foldOpenCursor();
6315#endif
6316}
6317
6318/*
6319 * Implementation of '?' and '/' commands.
6320 * If cap->arg is TRUE don't set PC mark.
6321 */
6322 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006323nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006326 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
6328 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6329 {
6330 /* Translate "g??" to "g?g?" */
6331 cap->cmdchar = 'g';
6332 cap->nchar = '?';
6333 nv_operator(cap);
6334 return;
6335 }
6336
Bram Moolenaardda933d2016-09-03 21:04:58 +02006337 /* When using 'incsearch' the cursor may be moved to set a different search
6338 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6340
6341 if (cap->searchbuf == NULL)
6342 {
6343 clearop(oap);
6344 return;
6345 }
6346
Bram Moolenaar46539112015-02-17 15:43:57 +01006347 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006348 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006349 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350}
6351
6352/*
6353 * Handle "N" and "n" commands.
6354 * cap->arg is SEARCH_REV for "N", 0 for "n".
6355 */
6356 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006357nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
Bram Moolenaar46539112015-02-17 15:43:57 +01006359 pos_T old = curwin->w_cursor;
6360 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6361
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006362 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006363 {
6364 /* Avoid getting stuck on the current cursor position, which can
6365 * happen when an offset is given and the cursor is on the last char
6366 * in the buffer: Repeat with count + 1. */
6367 cap->count1 += 1;
6368 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6369 cap->count1 -= 1;
6370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371}
6372
6373/*
6374 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6375 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006376 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006378 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006379normal_search(
6380 cmdarg_T *cap,
6381 int dir,
6382 char_u *pat,
6383 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 int i;
6386
6387 cap->oap->motion_type = MCHAR;
6388 cap->oap->inclusive = FALSE;
6389 cap->oap->use_reg_one = TRUE;
6390 curwin->w_set_curswant = TRUE;
6391
6392 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006393 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (i == 0)
6395 clearop(cap->oap);
6396 else
6397 {
6398 if (i == 2)
6399 cap->oap->motion_type = MLINE;
6400#ifdef FEAT_VIRTUALEDIT
6401 curwin->w_cursor.coladd = 0;
6402#endif
6403#ifdef FEAT_FOLDING
6404 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6405 foldOpenCursor();
6406#endif
6407 }
6408
6409 /* "/$" will put the cursor after the end of the line, may need to
6410 * correct that here */
6411 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006412 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413}
6414
6415/*
6416 * Character search commands.
6417 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6418 * ',' and FALSE for ';'.
6419 * cap->nchar is NUL for ',' and ';' (repeat the search)
6420 */
6421 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006422nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
6424 int t_cmd;
6425
6426 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6427 t_cmd = TRUE;
6428 else
6429 t_cmd = FALSE;
6430
6431 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6433 clearopbeep(cap->oap);
6434 else
6435 {
6436 curwin->w_set_curswant = TRUE;
6437#ifdef FEAT_VIRTUALEDIT
6438 /* Include a Tab for "tx" and for "dfx". */
6439 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6440 && (t_cmd || cap->oap->op_type != OP_NOP))
6441 {
6442 colnr_T scol, ecol;
6443
6444 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6445 curwin->w_cursor.coladd = ecol - scol;
6446 }
6447 else
6448 curwin->w_cursor.coladd = 0;
6449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451#ifdef FEAT_FOLDING
6452 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6453 foldOpenCursor();
6454#endif
6455 }
6456}
6457
6458/*
6459 * "[" and "]" commands.
6460 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6461 */
6462 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006463nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006465 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 pos_T prev_pos;
6467 pos_T *pos = NULL; /* init for GCC */
6468 pos_T old_pos; /* cursor position before command */
6469 int flag;
6470 long n;
6471 int findc;
6472 int c;
6473
6474 cap->oap->motion_type = MCHAR;
6475 cap->oap->inclusive = FALSE;
6476 old_pos = curwin->w_cursor;
6477#ifdef FEAT_VIRTUALEDIT
6478 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6479#endif
6480
6481#ifdef FEAT_SEARCHPATH
6482 /*
6483 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6484 */
6485 if (cap->nchar == 'f')
6486 nv_gotofile(cap);
6487 else
6488#endif
6489
6490#ifdef FEAT_FIND_ID
6491 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006492 * Find the occurrence(s) of the identifier or define under cursor
6493 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 *
6495 * search list jump
6496 * fwd bwd fwd bwd fwd bwd
6497 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6498 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6499 */
6500 if (vim_strchr((char_u *)
6501#ifdef EBCDIC
6502 "iI\005dD\067",
6503#else
6504 "iI\011dD\004",
6505#endif
6506 cap->nchar) != NULL)
6507 {
6508 char_u *ptr;
6509 int len;
6510
6511 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6512 clearop(cap->oap);
6513 else
6514 {
6515 find_pattern_in_path(ptr, 0, len, TRUE,
6516 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6517 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6518 cap->count1,
6519 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6520 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6521 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6522 (linenr_T)MAXLNUM);
6523 curwin->w_set_curswant = TRUE;
6524 }
6525 }
6526 else
6527#endif
6528
6529 /*
6530 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6531 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6532 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6533 * "[m" or "]m" search for prev/next start of (Java) method.
6534 * "[M" or "]M" search for prev/next end of (Java) method.
6535 */
6536 if ( (cap->cmdchar == '['
6537 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6538 || (cap->cmdchar == ']'
6539 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6540 {
6541 if (cap->nchar == '*')
6542 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 prev_pos.lnum = 0;
6544 if (cap->nchar == 'm' || cap->nchar == 'M')
6545 {
6546 if (cap->cmdchar == '[')
6547 findc = '{';
6548 else
6549 findc = '}';
6550 n = 9999;
6551 }
6552 else
6553 {
6554 findc = cap->nchar;
6555 n = cap->count1;
6556 }
6557 for ( ; n > 0; --n)
6558 {
6559 if ((pos = findmatchlimit(cap->oap, findc,
6560 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6561 {
6562 if (new_pos.lnum == 0) /* nothing found */
6563 {
6564 if (cap->nchar != 'm' && cap->nchar != 'M')
6565 clearopbeep(cap->oap);
6566 }
6567 else
6568 pos = &new_pos; /* use last one found */
6569 break;
6570 }
6571 prev_pos = new_pos;
6572 curwin->w_cursor = *pos;
6573 new_pos = *pos;
6574 }
6575 curwin->w_cursor = old_pos;
6576
6577 /*
6578 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6579 * brought us to the match for "[m" and "]M" when inside a method.
6580 * Try finding the '{' or '}' we want to be at.
6581 * Also repeat for the given count.
6582 */
6583 if (cap->nchar == 'm' || cap->nchar == 'M')
6584 {
6585 /* norm is TRUE for "]M" and "[m" */
6586 int norm = ((findc == '{') == (cap->nchar == 'm'));
6587
6588 n = cap->count1;
6589 /* found a match: we were inside a method */
6590 if (prev_pos.lnum != 0)
6591 {
6592 pos = &prev_pos;
6593 curwin->w_cursor = prev_pos;
6594 if (norm)
6595 --n;
6596 }
6597 else
6598 pos = NULL;
6599 while (n > 0)
6600 {
6601 for (;;)
6602 {
6603 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6604 {
6605 /* if not found anything, that's an error */
6606 if (pos == NULL)
6607 clearopbeep(cap->oap);
6608 n = 0;
6609 break;
6610 }
6611 c = gchar_cursor();
6612 if (c == '{' || c == '}')
6613 {
6614 /* Must have found end/start of class: use it.
6615 * Or found the place to be at. */
6616 if ((c == findc && norm) || (n == 1 && !norm))
6617 {
6618 new_pos = curwin->w_cursor;
6619 pos = &new_pos;
6620 n = 0;
6621 }
6622 /* if no match found at all, we started outside of the
6623 * class and we're inside now. Just go on. */
6624 else if (new_pos.lnum == 0)
6625 {
6626 new_pos = curwin->w_cursor;
6627 pos = &new_pos;
6628 }
6629 /* found start/end of other method: go to match */
6630 else if ((pos = findmatchlimit(cap->oap, findc,
6631 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6632 0)) == NULL)
6633 n = 0;
6634 else
6635 curwin->w_cursor = *pos;
6636 break;
6637 }
6638 }
6639 --n;
6640 }
6641 curwin->w_cursor = old_pos;
6642 if (pos == NULL && new_pos.lnum != 0)
6643 clearopbeep(cap->oap);
6644 }
6645 if (pos != NULL)
6646 {
6647 setpcmark();
6648 curwin->w_cursor = *pos;
6649 curwin->w_set_curswant = TRUE;
6650#ifdef FEAT_FOLDING
6651 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6652 && cap->oap->op_type == OP_NOP)
6653 foldOpenCursor();
6654#endif
6655 }
6656 }
6657
6658 /*
6659 * "[[", "[]", "]]" and "][": move to start or end of function
6660 */
6661 else if (cap->nchar == '[' || cap->nchar == ']')
6662 {
6663 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6664 flag = '{';
6665 else
6666 flag = '}'; /* "][" or "[]" */
6667
6668 curwin->w_set_curswant = TRUE;
6669 /*
6670 * Imitate strange Vi behaviour: When using "]]" with an operator
6671 * we also stop at '}'.
6672 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006673 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 (cap->oap->op_type != OP_NOP
6675 && cap->arg == FORWARD && flag == '{')))
6676 clearopbeep(cap->oap);
6677 else
6678 {
6679 if (cap->oap->op_type == OP_NOP)
6680 beginline(BL_WHITE | BL_FIX);
6681#ifdef FEAT_FOLDING
6682 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6683 foldOpenCursor();
6684#endif
6685 }
6686 }
6687
6688 /*
6689 * "[p", "[P", "]P" and "]p": put with indent adjustment
6690 */
6691 else if (cap->nchar == 'p' || cap->nchar == 'P')
6692 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006693 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006695 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6696 ? FORWARD : BACKWARD;
6697 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006698 int was_visual = VIsual_active;
6699 int line_count = curbuf->b_ml.ml_line_count;
6700 pos_T start, end;
6701
6702 if (VIsual_active)
6703 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006704 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006705 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006706 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006707 curwin->w_cursor = (dir == BACKWARD ? start : end);
6708 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006709# ifdef FEAT_CLIPBOARD
6710 adjust_clip_reg(&regname);
6711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006713
6714 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006715 if (was_visual)
6716 {
6717 VIsual = start;
6718 curwin->w_cursor = end;
6719 if (dir == BACKWARD)
6720 {
6721 /* adjust lines */
6722 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6723 curwin->w_cursor.lnum +=
6724 curbuf->b_ml.ml_line_count - line_count;
6725 }
6726
6727 VIsual_active = TRUE;
6728 if (VIsual_mode == 'V')
6729 {
6730 /* delete visually selected lines */
6731 cap->cmdchar = 'd';
6732 cap->nchar = NUL;
6733 cap->oap->regname = regname;
6734 nv_operator(cap);
6735 do_pending_operator(cap, 0, FALSE);
6736 }
6737 if (VIsual_active)
6738 {
6739 end_visual_mode();
6740 redraw_later(SOME_VALID);
6741 }
6742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744 }
6745
6746 /*
6747 * "['", "[`", "]'" and "]`": jump to next mark
6748 */
6749 else if (cap->nchar == '\'' || cap->nchar == '`')
6750 {
6751 pos = &curwin->w_cursor;
6752 for (n = cap->count1; n > 0; --n)
6753 {
6754 prev_pos = *pos;
6755 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6756 cap->nchar == '\'');
6757 if (pos == NULL)
6758 break;
6759 }
6760 if (pos == NULL)
6761 pos = &prev_pos;
6762 nv_cursormark(cap, cap->nchar == '\'', pos);
6763 }
6764
6765#ifdef FEAT_MOUSE
6766 /*
6767 * [ or ] followed by a middle mouse click: put selected text with
6768 * indent adjustment. Any other button just does as usual.
6769 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006770 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
6772 (void)do_mouse(cap->oap, cap->nchar,
6773 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6774 cap->count1, PUT_FIXINDENT);
6775 }
6776#endif /* FEAT_MOUSE */
6777
6778#ifdef FEAT_FOLDING
6779 /*
6780 * "[z" and "]z": move to start or end of open fold.
6781 */
6782 else if (cap->nchar == 'z')
6783 {
6784 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6785 cap->count1) == FAIL)
6786 clearopbeep(cap->oap);
6787 }
6788#endif
6789
6790#ifdef FEAT_DIFF
6791 /*
6792 * "[c" and "]c": move to next or previous diff-change.
6793 */
6794 else if (cap->nchar == 'c')
6795 {
6796 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6797 cap->count1) == FAIL)
6798 clearopbeep(cap->oap);
6799 }
6800#endif
6801
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006802#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006803 /*
6804 * "[s", "[S", "]s" and "]S": move to next spell error.
6805 */
6806 else if (cap->nchar == 's' || cap->nchar == 'S')
6807 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006808 setpcmark();
6809 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006810 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6811 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006812 {
6813 clearopbeep(cap->oap);
6814 break;
6815 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006816 else
6817 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006818# ifdef FEAT_FOLDING
6819 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6820 foldOpenCursor();
6821# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006822 }
6823#endif
6824
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 /* Not a valid cap->nchar. */
6826 else
6827 clearopbeep(cap->oap);
6828}
6829
6830/*
6831 * Handle Normal mode "%" command.
6832 */
6833 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006834nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
6836 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006837#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 linenr_T lnum = curwin->w_cursor.lnum;
6839#endif
6840
6841 cap->oap->inclusive = TRUE;
6842 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6843 {
6844 if (cap->count0 > 100)
6845 clearopbeep(cap->oap);
6846 else
6847 {
6848 cap->oap->motion_type = MLINE;
6849 setpcmark();
6850 /* Round up, so CTRL-G will give same value. Watch out for a
6851 * large line count, the line number must not go negative! */
6852 if (curbuf->b_ml.ml_line_count > 1000000)
6853 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6854 / 100L * cap->count0;
6855 else
6856 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6857 cap->count0 + 99L) / 100L;
6858 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6859 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6860 beginline(BL_SOL | BL_FIX);
6861 }
6862 }
6863 else /* "%" : go to matching paren */
6864 {
6865 cap->oap->motion_type = MCHAR;
6866 cap->oap->use_reg_one = TRUE;
6867 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6868 clearopbeep(cap->oap);
6869 else
6870 {
6871 setpcmark();
6872 curwin->w_cursor = *pos;
6873 curwin->w_set_curswant = TRUE;
6874#ifdef FEAT_VIRTUALEDIT
6875 curwin->w_cursor.coladd = 0;
6876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 }
6879 }
6880#ifdef FEAT_FOLDING
6881 if (cap->oap->op_type == OP_NOP
6882 && lnum != curwin->w_cursor.lnum
6883 && (fdo_flags & FDO_PERCENT)
6884 && KeyTyped)
6885 foldOpenCursor();
6886#endif
6887}
6888
6889/*
6890 * Handle "(" and ")" commands.
6891 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6892 */
6893 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006894nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
6896 cap->oap->motion_type = MCHAR;
6897 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006898 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6899 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 curwin->w_set_curswant = TRUE;
6901
6902 if (findsent(cap->arg, cap->count1) == FAIL)
6903 clearopbeep(cap->oap);
6904 else
6905 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006906 /* Don't leave the cursor on the NUL past end of line. */
6907 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908#ifdef FEAT_VIRTUALEDIT
6909 curwin->w_cursor.coladd = 0;
6910#endif
6911#ifdef FEAT_FOLDING
6912 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6913 foldOpenCursor();
6914#endif
6915 }
6916}
6917
6918/*
6919 * "m" command: Mark a position.
6920 */
6921 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006922nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924 if (!checkclearop(cap->oap))
6925 {
6926 if (setmark(cap->nchar) == FAIL)
6927 clearopbeep(cap->oap);
6928 }
6929}
6930
6931/*
6932 * "{" and "}" commands.
6933 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6934 */
6935 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006936nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 cap->oap->motion_type = MCHAR;
6939 cap->oap->inclusive = FALSE;
6940 cap->oap->use_reg_one = TRUE;
6941 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006942 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 clearopbeep(cap->oap);
6944 else
6945 {
6946#ifdef FEAT_VIRTUALEDIT
6947 curwin->w_cursor.coladd = 0;
6948#endif
6949#ifdef FEAT_FOLDING
6950 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6951 foldOpenCursor();
6952#endif
6953 }
6954}
6955
6956/*
6957 * "u" command: Undo or make lower case.
6958 */
6959 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006960nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006962 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {
6964 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6965 cap->cmdchar = 'g';
6966 cap->nchar = 'u';
6967 nv_operator(cap);
6968 }
6969 else
6970 nv_kundo(cap);
6971}
6972
6973/*
6974 * <Undo> command.
6975 */
6976 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006977nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979 if (!checkclearopq(cap->oap))
6980 {
6981 u_undo((int)cap->count1);
6982 curwin->w_set_curswant = TRUE;
6983 }
6984}
6985
6986/*
6987 * Handle the "r" command.
6988 */
6989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006990nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991{
6992 char_u *ptr;
6993 int had_ctrl_v;
6994 long n;
6995
6996 if (checkclearop(cap->oap))
6997 return;
6998
6999 /* get another character */
7000 if (cap->nchar == Ctrl_V)
7001 {
7002 had_ctrl_v = Ctrl_V;
7003 cap->nchar = get_literal();
7004 /* Don't redo a multibyte character with CTRL-V. */
7005 if (cap->nchar > DEL)
7006 had_ctrl_v = NUL;
7007 }
7008 else
7009 had_ctrl_v = NUL;
7010
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007011 /* Abort if the character is a special key. */
7012 if (IS_SPECIAL(cap->nchar))
7013 {
7014 clearopbeep(cap->oap);
7015 return;
7016 }
7017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 /* Visual mode "r" */
7019 if (VIsual_active)
7020 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007021 if (got_int)
7022 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007023 if (had_ctrl_v)
7024 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01007025 /* Use a special (negative) number to make a difference between a
7026 * literal CR or NL and a line break. */
7027 if (cap->nchar == CAR)
7028 cap->nchar = REPLACE_CR_NCHAR;
7029 else if (cap->nchar == NL)
7030 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01007031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 nv_operator(cap);
7033 return;
7034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
7036#ifdef FEAT_VIRTUALEDIT
7037 /* Break tabs, etc. */
7038 if (virtual_active())
7039 {
7040 if (u_save_cursor() == FAIL)
7041 return;
7042 if (gchar_cursor() == NUL)
7043 {
7044 /* Add extra space and put the cursor on the first one. */
7045 coladvance_force((colnr_T)(getviscol() + cap->count1));
7046 curwin->w_cursor.col -= cap->count1;
7047 }
7048 else if (gchar_cursor() == TAB)
7049 coladvance_force(getviscol());
7050 }
7051#endif
7052
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007053 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007055 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056#ifdef FEAT_MBYTE
7057 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7058#endif
7059 )
7060 {
7061 clearopbeep(cap->oap);
7062 return;
7063 }
7064
7065 /*
7066 * Replacing with a TAB is done by edit() when it is complicated because
7067 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7068 * Other characters are done below to avoid problems with things like
7069 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7070 */
7071 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7072 {
7073 stuffnumReadbuff(cap->count1);
7074 stuffcharReadbuff('R');
7075 stuffcharReadbuff('\t');
7076 stuffcharReadbuff(ESC);
7077 return;
7078 }
7079
7080 /* save line for undo */
7081 if (u_save_cursor() == FAIL)
7082 return;
7083
7084 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7085 {
7086 /*
7087 * Replace character(s) by a single newline.
7088 * Strange vi behaviour: Only one newline is inserted.
7089 * Delete the characters here.
7090 * Insert the newline with an insert command, takes care of
7091 * autoindent. The insert command depends on being on the last
7092 * character of a line or not.
7093 */
7094#ifdef FEAT_MBYTE
7095 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7096#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007097 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098#endif
7099 stuffcharReadbuff('\r');
7100 stuffcharReadbuff(ESC);
7101
7102 /* Give 'r' to edit(), to get the redo command right. */
7103 invoke_edit(cap, TRUE, 'r', FALSE);
7104 }
7105 else
7106 {
7107 prep_redo(cap->oap->regname, cap->count1,
7108 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7109
7110 curbuf->b_op_start = curwin->w_cursor;
7111#ifdef FEAT_MBYTE
7112 if (has_mbyte)
7113 {
7114 int old_State = State;
7115
7116 if (cap->ncharC1 != 0)
7117 AppendCharToRedobuff(cap->ncharC1);
7118 if (cap->ncharC2 != 0)
7119 AppendCharToRedobuff(cap->ncharC2);
7120
7121 /* This is slow, but it handles replacing a single-byte with a
7122 * multi-byte and the other way around. Also handles adding
7123 * composing characters for utf-8. */
7124 for (n = cap->count1; n > 0; --n)
7125 {
7126 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007127 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7128 {
7129 int c = ins_copychar(curwin->w_cursor.lnum
7130 + (cap->nchar == Ctrl_Y ? -1 : 1));
7131 if (c != NUL)
7132 ins_char(c);
7133 else
7134 /* will be decremented further down */
7135 ++curwin->w_cursor.col;
7136 }
7137 else
7138 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 State = old_State;
7140 if (cap->ncharC1 != 0)
7141 ins_char(cap->ncharC1);
7142 if (cap->ncharC2 != 0)
7143 ins_char(cap->ncharC2);
7144 }
7145 }
7146 else
7147#endif
7148 {
7149 /*
7150 * Replace the characters within one line.
7151 */
7152 for (n = cap->count1; n > 0; --n)
7153 {
7154 /*
7155 * Get ptr again, because u_save and/or showmatch() will have
7156 * released the line. At the same time we let know that the
7157 * line will be changed.
7158 */
7159 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007160 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7161 {
7162 int c = ins_copychar(curwin->w_cursor.lnum
7163 + (cap->nchar == Ctrl_Y ? -1 : 1));
7164 if (c != NUL)
7165 ptr[curwin->w_cursor.col] = c;
7166 }
7167 else
7168 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 if (p_sm && msg_silent == 0)
7170 showmatch(cap->nchar);
7171 ++curwin->w_cursor.col;
7172 }
7173#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007174 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007176 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaar009b2592004-10-24 19:18:58 +00007178 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007179 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007181 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 }
7183#endif
7184
7185 /* mark the buffer as changed and prepare for displaying */
7186 changed_bytes(curwin->w_cursor.lnum,
7187 (colnr_T)(curwin->w_cursor.col - cap->count1));
7188 }
7189 --curwin->w_cursor.col; /* cursor on the last replaced char */
7190#ifdef FEAT_MBYTE
7191 /* if the character on the left of the current cursor is a multi-byte
7192 * character, move two characters left */
7193 if (has_mbyte)
7194 mb_adjust_cursor();
7195#endif
7196 curbuf->b_op_end = curwin->w_cursor;
7197 curwin->w_set_curswant = TRUE;
7198 set_last_insert(cap->nchar);
7199 }
7200}
7201
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202/*
7203 * 'o': Exchange start and end of Visual area.
7204 * 'O': same, but in block mode exchange left and right corners.
7205 */
7206 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007207v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
7209 pos_T old_cursor;
7210 colnr_T left, right;
7211
7212 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7213 {
7214 old_cursor = curwin->w_cursor;
7215 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7216 curwin->w_cursor.lnum = VIsual.lnum;
7217 coladvance(left);
7218 VIsual = curwin->w_cursor;
7219
7220 curwin->w_cursor.lnum = old_cursor.lnum;
7221 curwin->w_curswant = right;
7222 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7223 * right one column */
7224 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7225 ++curwin->w_curswant;
7226 coladvance(curwin->w_curswant);
7227 if (curwin->w_cursor.col == old_cursor.col
7228#ifdef FEAT_VIRTUALEDIT
7229 && (!virtual_active()
7230 || curwin->w_cursor.coladd == old_cursor.coladd)
7231#endif
7232 )
7233 {
7234 curwin->w_cursor.lnum = VIsual.lnum;
7235 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7236 ++right;
7237 coladvance(right);
7238 VIsual = curwin->w_cursor;
7239
7240 curwin->w_cursor.lnum = old_cursor.lnum;
7241 coladvance(left);
7242 curwin->w_curswant = left;
7243 }
7244 }
7245 else
7246 {
7247 old_cursor = curwin->w_cursor;
7248 curwin->w_cursor = VIsual;
7249 VIsual = old_cursor;
7250 curwin->w_set_curswant = TRUE;
7251 }
7252}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253
7254/*
7255 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7256 */
7257 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007258nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 if (VIsual_active) /* "R" is replace lines */
7261 {
7262 cap->cmdchar = 'c';
7263 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007264 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 VIsual_mode = 'V';
7266 nv_operator(cap);
7267 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007268 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
7270 if (!curbuf->b_p_ma)
7271 EMSG(_(e_modifiable));
7272 else
7273 {
7274#ifdef FEAT_VIRTUALEDIT
7275 if (virtual_active())
7276 coladvance(getviscol());
7277#endif
7278 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7279 }
7280 }
7281}
7282
7283#ifdef FEAT_VREPLACE
7284/*
7285 * "gr".
7286 */
7287 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007288nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 if (VIsual_active)
7291 {
7292 cap->cmdchar = 'r';
7293 cap->nchar = cap->extra_char;
7294 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7295 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007296 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 {
7298 if (!curbuf->b_p_ma)
7299 EMSG(_(e_modifiable));
7300 else
7301 {
7302 if (cap->extra_char == Ctrl_V) /* get another character */
7303 cap->extra_char = get_literal();
7304 stuffcharReadbuff(cap->extra_char);
7305 stuffcharReadbuff(ESC);
7306# ifdef FEAT_VIRTUALEDIT
7307 if (virtual_active())
7308 coladvance(getviscol());
7309# endif
7310 invoke_edit(cap, TRUE, 'v', FALSE);
7311 }
7312 }
7313}
7314#endif
7315
7316/*
7317 * Swap case for "~" command, when it does not work like an operator.
7318 */
7319 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007320n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 long n;
7323 pos_T startpos;
7324 int did_change = 0;
7325#ifdef FEAT_NETBEANS_INTG
7326 pos_T pos;
7327 char_u *ptr;
7328 int count;
7329#endif
7330
7331 if (checkclearopq(cap->oap))
7332 return;
7333
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007334 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 clearopbeep(cap->oap);
7337 return;
7338 }
7339
7340 prep_redo_cmd(cap);
7341
7342 if (u_save_cursor() == FAIL)
7343 return;
7344
7345 startpos = curwin->w_cursor;
7346#ifdef FEAT_NETBEANS_INTG
7347 pos = startpos;
7348#endif
7349 for (n = cap->count1; n > 0; --n)
7350 {
7351 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7352 inc_cursor();
7353 if (gchar_cursor() == NUL)
7354 {
7355 if (vim_strchr(p_ww, '~') != NULL
7356 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7357 {
7358#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007359 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
7361 if (did_change)
7362 {
7363 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007364 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007365 netbeans_removed(curbuf, pos.lnum, pos.col,
7366 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007368 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 }
7370 pos.col = 0;
7371 pos.lnum++;
7372 }
7373#endif
7374 ++curwin->w_cursor.lnum;
7375 curwin->w_cursor.col = 0;
7376 if (n > 1)
7377 {
7378 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7379 break;
7380 u_clearline();
7381 }
7382 }
7383 else
7384 break;
7385 }
7386 }
7387#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007388 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 {
7390 ptr = ml_get(pos.lnum);
7391 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007392 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7393 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 }
7395#endif
7396
7397
7398 check_cursor();
7399 curwin->w_set_curswant = TRUE;
7400 if (did_change)
7401 {
7402 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7403 0L);
7404 curbuf->b_op_start = startpos;
7405 curbuf->b_op_end = curwin->w_cursor;
7406 if (curbuf->b_op_end.col > 0)
7407 --curbuf->b_op_end.col;
7408 }
7409}
7410
7411/*
7412 * Move cursor to mark.
7413 */
7414 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007415nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
7417 if (check_mark(pos) == FAIL)
7418 clearop(cap->oap);
7419 else
7420 {
7421 if (cap->cmdchar == '\''
7422 || cap->cmdchar == '`'
7423 || cap->cmdchar == '['
7424 || cap->cmdchar == ']')
7425 setpcmark();
7426 curwin->w_cursor = *pos;
7427 if (flag)
7428 beginline(BL_WHITE | BL_FIX);
7429 else
7430 check_cursor();
7431 }
7432 cap->oap->motion_type = flag ? MLINE : MCHAR;
7433 if (cap->cmdchar == '`')
7434 cap->oap->use_reg_one = TRUE;
7435 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7436 curwin->w_set_curswant = TRUE;
7437}
7438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439/*
7440 * Handle commands that are operators in Visual mode.
7441 */
7442 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007443v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
7445 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7446
7447 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007448 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 if (isupper(cap->cmdchar))
7450 {
7451 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007452 {
7453 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7457 curwin->w_curswant = MAXCOL;
7458 }
7459 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7460 nv_operator(cap);
7461}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462
7463/*
7464 * "s" and "S" commands.
7465 */
7466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007467nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007469#ifdef FEAT_TERMINAL
7470 /* When showing output of term_dumpdiff() swap the top and botom. */
7471 if (term_swap_diff() == OK)
7472 return;
7473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7475 {
7476 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007477 {
7478 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 cap->cmdchar = 'c';
7482 nv_operator(cap);
7483 }
7484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 nv_optrans(cap);
7486}
7487
7488/*
7489 * Abbreviated commands.
7490 */
7491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007492nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
7494 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7495 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7496
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 /* in Visual mode these commands are operators */
7498 if (VIsual_active)
7499 v_visop(cap);
7500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 nv_optrans(cap);
7502}
7503
7504/*
7505 * Translate a command into another command.
7506 */
7507 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007508nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7511 (char_u *)"d$", (char_u *)"c$",
7512 (char_u *)"cl", (char_u *)"cc",
7513 (char_u *)"yy", (char_u *)":s\r"};
7514 static char_u *str = (char_u *)"xXDCsSY&";
7515
7516 if (!checkclearopq(cap->oap))
7517 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007518 /* In Vi "2D" doesn't delete the next line. Can't translate it
7519 * either, because "2." should also not use the count. */
7520 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7521 {
7522 cap->oap->start = curwin->w_cursor;
7523 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007524#ifdef FEAT_EVAL
7525 set_op_var(OP_DELETE);
7526#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007527 cap->count1 = 1;
7528 nv_dollar(cap);
7529 finish_op = TRUE;
7530 ResetRedobuff();
7531 AppendCharToRedobuff('D');
7532 }
7533 else
7534 {
7535 if (cap->count0)
7536 stuffnumReadbuff(cap->count0);
7537 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 }
7540 cap->opcount = 0;
7541}
7542
7543/*
7544 * "'" and "`" commands. Also for "g'" and "g`".
7545 * cap->arg is TRUE for "'" and "g'".
7546 */
7547 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007548nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549{
7550 pos_T *pos;
7551 int c;
7552#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007553 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7555#endif
7556
7557 if (cap->cmdchar == 'g')
7558 c = cap->extra_char;
7559 else
7560 c = cap->nchar;
7561 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7562 if (pos == (pos_T *)-1) /* jumped to other file */
7563 {
7564 if (cap->arg)
7565 {
7566 check_cursor_lnum();
7567 beginline(BL_WHITE | BL_FIX);
7568 }
7569 else
7570 check_cursor();
7571 }
7572 else
7573 nv_cursormark(cap, cap->arg, pos);
7574
7575#ifdef FEAT_VIRTUALEDIT
7576 /* May need to clear the coladd that a mark includes. */
7577 if (!virtual_active())
7578 curwin->w_cursor.coladd = 0;
7579#endif
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007580 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581#ifdef FEAT_FOLDING
7582 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007583 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007584 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 && (fdo_flags & FDO_MARK)
7586 && old_KeyTyped)
7587 foldOpenCursor();
7588#endif
7589}
7590
7591/*
7592 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7593 */
7594 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007595nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597#ifdef FEAT_JUMPLIST
7598 pos_T *pos;
7599# ifdef FEAT_FOLDING
7600 linenr_T lnum = curwin->w_cursor.lnum;
7601 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7602# endif
7603
7604 if (!checkclearopq(cap->oap))
7605 {
7606 if (cap->cmdchar == 'g')
7607 pos = movechangelist((int)cap->count1);
7608 else
7609 pos = movemark((int)cap->count1);
7610 if (pos == (pos_T *)-1) /* jump to other file */
7611 {
7612 curwin->w_set_curswant = TRUE;
7613 check_cursor();
7614 }
7615 else if (pos != NULL) /* can jump */
7616 nv_cursormark(cap, FALSE, pos);
7617 else if (cap->cmdchar == 'g')
7618 {
7619 if (curbuf->b_changelistlen == 0)
7620 EMSG(_("E664: changelist is empty"));
7621 else if (cap->count1 < 0)
7622 EMSG(_("E662: At start of changelist"));
7623 else
7624 EMSG(_("E663: At end of changelist"));
7625 }
7626 else
7627 clearopbeep(cap->oap);
7628# ifdef FEAT_FOLDING
7629 if (cap->oap->op_type == OP_NOP
7630 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7631 && (fdo_flags & FDO_MARK)
7632 && old_KeyTyped)
7633 foldOpenCursor();
7634# endif
7635 }
7636#else
7637 clearopbeep(cap->oap);
7638#endif
7639}
7640
7641/*
7642 * Handle '"' command.
7643 */
7644 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007645nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
7647 if (checkclearop(cap->oap))
7648 return;
7649#ifdef FEAT_EVAL
7650 if (cap->nchar == '=')
7651 cap->nchar = get_expr_register();
7652#endif
7653 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7654 {
7655 cap->oap->regname = cap->nchar;
7656 cap->opcount = cap->count0; /* remember count before '"' */
7657#ifdef FEAT_EVAL
7658 set_reg_var(cap->oap->regname);
7659#endif
7660 }
7661 else
7662 clearopbeep(cap->oap);
7663}
7664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665/*
7666 * Handle "v", "V" and "CTRL-V" commands.
7667 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7668 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007669 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 */
7671 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007672nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007674 if (cap->cmdchar == Ctrl_Q)
7675 cap->cmdchar = Ctrl_V;
7676
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7678 * characterwise, linewise, or blockwise. */
7679 if (cap->oap->op_type != OP_NOP)
7680 {
7681 cap->oap->motion_force = cap->cmdchar;
7682 finish_op = FALSE; /* operator doesn't finish now but later */
7683 return;
7684 }
7685
7686 VIsual_select = cap->arg;
7687 if (VIsual_active) /* change Visual mode */
7688 {
7689 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7690 end_visual_mode();
7691 else /* toggle char/block mode */
7692 { /* or char/line mode */
7693 VIsual_mode = cap->cmdchar;
7694 showmode();
7695 }
7696 redraw_curbuf_later(INVERTED); /* update the inversion */
7697 }
7698 else /* start Visual mode */
7699 {
7700 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007701 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007703 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 VIsual = curwin->w_cursor;
7705
7706 VIsual_active = TRUE;
7707 VIsual_reselect = TRUE;
7708 if (!cap->arg)
7709 /* start Select mode when 'selectmode' contains "cmd" */
7710 may_start_select('c');
7711#ifdef FEAT_MOUSE
7712 setmouse();
7713#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007714 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 redraw_cmdline = TRUE; /* show visual mode later */
7716 /*
7717 * For V and ^V, we multiply the number of lines even if there
7718 * was only one -- webb
7719 */
7720 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7721 {
7722 curwin->w_cursor.lnum +=
7723 resel_VIsual_line_count * cap->count0 - 1;
7724 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7725 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7726 }
7727 VIsual_mode = resel_VIsual_mode;
7728 if (VIsual_mode == 'v')
7729 {
7730 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007731 {
7732 validate_virtcol();
7733 curwin->w_curswant = curwin->w_virtcol
7734 + resel_VIsual_vcol * cap->count0 - 1;
7735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007737 curwin->w_curswant = resel_VIsual_vcol;
7738 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007740 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {
7742 curwin->w_curswant = MAXCOL;
7743 coladvance((colnr_T)MAXCOL);
7744 }
7745 else if (VIsual_mode == Ctrl_V)
7746 {
7747 validate_virtcol();
7748 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007749 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 coladvance(curwin->w_curswant);
7751 }
7752 else
7753 curwin->w_set_curswant = TRUE;
7754 redraw_curbuf_later(INVERTED); /* show the inversion */
7755 }
7756 else
7757 {
7758 if (!cap->arg)
7759 /* start Select mode when 'selectmode' contains "cmd" */
7760 may_start_select('c');
7761 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007762 if (VIsual_mode != 'V' && *p_sel == 'e')
7763 ++cap->count1; /* include one more char */
7764 if (cap->count0 > 0 && --cap->count1 > 0)
7765 {
7766 /* With a count select that many characters or lines. */
7767 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7768 nv_right(cap);
7769 else if (VIsual_mode == 'V')
7770 nv_down(cap);
7771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
7773 }
7774}
7775
7776/*
7777 * Start selection for Shift-movement keys.
7778 */
7779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007780start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782 /* if 'selectmode' contains "key", start Select mode */
7783 may_start_select('k');
7784 n_start_visual_mode('v');
7785}
7786
7787/*
7788 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7789 */
7790 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007791may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 VIsual_select = (stuff_empty() && typebuf_typed()
7794 && (vim_strchr(p_slm, c) != NULL));
7795}
7796
7797/*
7798 * Start Visual mode "c".
7799 * Should set VIsual_select before calling this.
7800 */
7801 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007802n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007804#ifdef FEAT_CONCEAL
7805 /* Check for redraw before changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007806 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007807#endif
7808
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 VIsual_mode = c;
7810 VIsual_active = TRUE;
7811 VIsual_reselect = TRUE;
7812#ifdef FEAT_VIRTUALEDIT
7813 /* Corner case: the 0 position in a tab may change when going into
7814 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7815 */
7816 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007817 {
7818 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821#endif
7822 VIsual = curwin->w_cursor;
7823
7824#ifdef FEAT_FOLDING
7825 foldAdjustVisual();
7826#endif
7827
7828#ifdef FEAT_MOUSE
7829 setmouse();
7830#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007831#ifdef FEAT_CONCEAL
7832 /* Check for redraw after changing the state. */
Bram Moolenaar8e469272010-07-28 19:38:16 +02007833 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007834#endif
7835
Bram Moolenaar09df3122006-01-23 22:23:09 +00007836 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 redraw_cmdline = TRUE; /* show visual mode later */
7838#ifdef FEAT_CLIPBOARD
7839 /* Make sure the clipboard gets updated. Needed because start and
7840 * end may still be the same, and the selection needs to be owned */
7841 clip_star.vmode = NUL;
7842#endif
7843
7844 /* Only need to redraw this line, unless still need to redraw an old
7845 * Visual area (when 'lazyredraw' is set). */
7846 if (curwin->w_redr_type < INVERTED)
7847 {
7848 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7849 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7850 }
7851}
7852
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853
7854/*
7855 * CTRL-W: Window commands
7856 */
7857 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007858nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007860 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007861 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007862 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007863 cap->cmdchar = ':';
7864 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007865 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007866 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007867 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869}
7870
7871/*
7872 * CTRL-Z: Suspend
7873 */
7874 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007875nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
7877 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 if (VIsual_active)
7879 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 do_cmdline_cmd((char_u *)"st");
7881}
7882
7883/*
7884 * Commands starting with "g".
7885 */
7886 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007887nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888{
7889 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 int i;
7892 int flag = FALSE;
7893
7894 switch (cap->nchar)
7895 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007896 case Ctrl_A:
7897 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#ifdef MEM_PROFILE
7899 /*
7900 * "g^A": dump log of used memory.
7901 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007902 if (!VIsual_active && cap->nchar == Ctrl_A)
7903 vim_mem_profile_dump();
7904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007906 /*
7907 * "g^A/g^X": sequentially increment visually selected region
7908 */
7909 if (VIsual_active)
7910 {
7911 cap->arg = TRUE;
7912 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007913 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007914 nv_addsub(cap);
7915 }
7916 else
7917 clearopbeep(oap);
7918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
7920#ifdef FEAT_VREPLACE
7921 /*
7922 * "gR": Enter virtual replace mode.
7923 */
7924 case 'R':
7925 cap->arg = TRUE;
7926 nv_Replace(cap);
7927 break;
7928
7929 case 'r':
7930 nv_vreplace(cap);
7931 break;
7932#endif
7933
7934 case '&':
7935 do_cmdline_cmd((char_u *)"%s//~/&");
7936 break;
7937
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 /*
7939 * "gv": Reselect the previous Visual area. If Visual already active,
7940 * exchange previous and current Visual area.
7941 */
7942 case 'v':
7943 if (checkclearop(oap))
7944 break;
7945
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007946 if ( curbuf->b_visual.vi_start.lnum == 0
7947 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7948 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 beep_flush();
7950 else
7951 {
7952 /* set w_cursor to the start of the Visual area, tpos to the end */
7953 if (VIsual_active)
7954 {
7955 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007956 VIsual_mode = curbuf->b_visual.vi_mode;
7957 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958# ifdef FEAT_EVAL
7959 curbuf->b_visual_mode_eval = i;
7960# endif
7961 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007962 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7963 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007965 tpos = curbuf->b_visual.vi_end;
7966 curbuf->b_visual.vi_end = curwin->w_cursor;
7967 curwin->w_cursor = curbuf->b_visual.vi_start;
7968 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 }
7970 else
7971 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007972 VIsual_mode = curbuf->b_visual.vi_mode;
7973 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7974 tpos = curbuf->b_visual.vi_end;
7975 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 }
7977
7978 VIsual_active = TRUE;
7979 VIsual_reselect = TRUE;
7980
7981 /* Set Visual to the start and w_cursor to the end of the Visual
7982 * area. Make sure they are on an existing character. */
7983 check_cursor();
7984 VIsual = curwin->w_cursor;
7985 curwin->w_cursor = tpos;
7986 check_cursor();
7987 update_topline();
7988 /*
7989 * When called from normal "g" command: start Select mode when
7990 * 'selectmode' contains "cmd". When called for K_SELECT, always
7991 * start Select mode.
7992 */
7993 if (cap->arg)
7994 VIsual_select = TRUE;
7995 else
7996 may_start_select('c');
7997#ifdef FEAT_MOUSE
7998 setmouse();
7999#endif
8000#ifdef FEAT_CLIPBOARD
8001 /* Make sure the clipboard gets updated. Needed because start and
8002 * end are still the same, and the selection needs to be owned */
8003 clip_star.vmode = NUL;
8004#endif
8005 redraw_curbuf_later(INVERTED);
8006 showmode();
8007 }
8008 break;
8009 /*
8010 * "gV": Don't reselect the previous Visual area after a Select mode
8011 * mapping of menu.
8012 */
8013 case 'V':
8014 VIsual_reselect = FALSE;
8015 break;
8016
8017 /*
8018 * "gh": start Select mode.
8019 * "gH": start Select line mode.
8020 * "g^H": start Select block mode.
8021 */
8022 case K_BS:
8023 cap->nchar = Ctrl_H;
8024 /* FALLTHROUGH */
8025 case 'h':
8026 case 'H':
8027 case Ctrl_H:
8028# ifdef EBCDIC
8029 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8030 if (cap->nchar == Ctrl_H)
8031 cap->cmdchar = Ctrl_V;
8032 else
8033# endif
8034 cap->cmdchar = cap->nchar + ('v' - 'h');
8035 cap->arg = TRUE;
8036 nv_visual(cap);
8037 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008038
8039 /* "gn", "gN" visually select next/previous search match
8040 * "gn" selects next match
8041 * "gN" selects previous match
8042 */
8043 case 'N':
8044 case 'n':
8045 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008046 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048
8049 /*
8050 * "gj" and "gk" two new funny movement keys -- up and down
8051 * movement based on *screen* line rather than *file* line.
8052 */
8053 case 'j':
8054 case K_DOWN:
8055 /* with 'nowrap' it works just like the normal "j" command; also when
8056 * in a closed fold */
8057 if (!curwin->w_p_wrap
8058#ifdef FEAT_FOLDING
8059 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8060#endif
8061 )
8062 {
8063 oap->motion_type = MLINE;
8064 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8065 }
8066 else
8067 i = nv_screengo(oap, FORWARD, cap->count1);
8068 if (i == FAIL)
8069 clearopbeep(oap);
8070 break;
8071
8072 case 'k':
8073 case K_UP:
8074 /* with 'nowrap' it works just like the normal "k" command; also when
8075 * in a closed fold */
8076 if (!curwin->w_p_wrap
8077#ifdef FEAT_FOLDING
8078 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8079#endif
8080 )
8081 {
8082 oap->motion_type = MLINE;
8083 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8084 }
8085 else
8086 i = nv_screengo(oap, BACKWARD, cap->count1);
8087 if (i == FAIL)
8088 clearopbeep(oap);
8089 break;
8090
8091 /*
8092 * "gJ": join two lines without inserting a space.
8093 */
8094 case 'J':
8095 nv_join(cap);
8096 break;
8097
8098 /*
8099 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8100 * "gm": middle of "g0" and "g$".
8101 */
8102 case '^':
8103 flag = TRUE;
8104 /* FALLTHROUGH */
8105
8106 case '0':
8107 case 'm':
8108 case K_HOME:
8109 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 oap->motion_type = MCHAR;
8111 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008112 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008114 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 int width2 = width1 + curwin_col_off2();
8116
8117 validate_virtcol();
8118 i = 0;
8119 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8120 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8121 }
8122 else
8123 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008124 /* Go to the middle of the screen line. When 'number' or
8125 * 'relativenumber' is on and lines are wrapping the middle can be more
8126 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008128 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 + ((curwin->w_p_wrap && i > 0)
8130 ? curwin_col_off2() : 0)) / 2;
8131 coladvance((colnr_T)i);
8132 if (flag)
8133 {
8134 do
8135 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008136 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 }
8138 curwin->w_set_curswant = TRUE;
8139 break;
8140
8141 case '_':
8142 /* "g_": to the last non-blank character in the line or <count> lines
8143 * downward. */
8144 cap->oap->motion_type = MCHAR;
8145 cap->oap->inclusive = TRUE;
8146 curwin->w_curswant = MAXCOL;
8147 if (cursor_down((long)(cap->count1 - 1),
8148 cap->oap->op_type == OP_NOP) == FAIL)
8149 clearopbeep(cap->oap);
8150 else
8151 {
8152 char_u *ptr = ml_get_curline();
8153
8154 /* In Visual mode we may end up after the line. */
8155 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8156 --curwin->w_cursor.col;
8157
8158 /* Decrease the cursor column until it's on a non-blank. */
8159 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008160 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 --curwin->w_cursor.col;
8162 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008163 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 }
8165 break;
8166
8167 case '$':
8168 case K_END:
8169 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {
8171 int col_off = curwin_col_off();
8172
8173 oap->motion_type = MCHAR;
8174 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008175 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {
8177 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8178 if (cap->count1 == 1)
8179 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008180 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 int width2 = width1 + curwin_col_off2();
8182
8183 validate_virtcol();
8184 i = width1 - 1;
8185 if (curwin->w_virtcol >= (colnr_T)width1)
8186 i += ((curwin->w_virtcol - width1) / width2 + 1)
8187 * width2;
8188 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008189
8190 /* Make sure we stick in this column. */
8191 validate_virtcol();
8192 curwin->w_curswant = curwin->w_virtcol;
8193 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8195 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8196 {
8197 /*
8198 * Check for landing on a character that got split at
8199 * the end of the line. We do not want to advance to
8200 * the next screen line.
8201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 if (curwin->w_virtcol > (colnr_T)i)
8203 --curwin->w_cursor.col;
8204 }
8205#endif
8206 }
8207 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8208 clearopbeep(oap);
8209 }
8210 else
8211 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008212 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008214
8215 /* Make sure we stick in this column. */
8216 validate_virtcol();
8217 curwin->w_curswant = curwin->w_virtcol;
8218 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
8220 }
8221 break;
8222
8223 /*
8224 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8225 */
8226 case '*':
8227 case '#':
8228#if POUND != '#'
8229 case POUND: /* pound sign (sometimes equal to '#') */
8230#endif
8231 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8232 case ']': /* :tselect for current identifier */
8233 nv_ident(cap);
8234 break;
8235
8236 /*
8237 * ge and gE: go back to end of word
8238 */
8239 case 'e':
8240 case 'E':
8241 oap->motion_type = MCHAR;
8242 curwin->w_set_curswant = TRUE;
8243 oap->inclusive = TRUE;
8244 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8245 clearopbeep(oap);
8246 break;
8247
8248 /*
8249 * "g CTRL-G": display info about cursor position
8250 */
8251 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008252 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 break;
8254
8255 /*
8256 * "gi": start Insert at the last position.
8257 */
8258 case 'i':
8259 if (curbuf->b_last_insert.lnum != 0)
8260 {
8261 curwin->w_cursor = curbuf->b_last_insert;
8262 check_cursor_lnum();
8263 i = (int)STRLEN(ml_get_curline());
8264 if (curwin->w_cursor.col > (colnr_T)i)
8265 {
8266#ifdef FEAT_VIRTUALEDIT
8267 if (virtual_active())
8268 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8269#endif
8270 curwin->w_cursor.col = i;
8271 }
8272 }
8273 cap->cmdchar = 'i';
8274 nv_edit(cap);
8275 break;
8276
8277 /*
8278 * "gI": Start insert in column 1.
8279 */
8280 case 'I':
8281 beginline(0);
8282 if (!checkclearopq(oap))
8283 invoke_edit(cap, FALSE, 'g', FALSE);
8284 break;
8285
8286#ifdef FEAT_SEARCHPATH
8287 /*
8288 * "gf": goto file, edit file under cursor
8289 * "]f" and "[f": can also be used.
8290 */
8291 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008292 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 nv_gotofile(cap);
8294 break;
8295#endif
8296
8297 /* "g'm" and "g`m": jump to mark without setting pcmark */
8298 case '\'':
8299 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008300 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 case '`':
8302 nv_gomark(cap);
8303 break;
8304
8305 /*
8306 * "gs": Goto sleep.
8307 */
8308 case 's':
8309 do_sleep(cap->count1 * 1000L);
8310 break;
8311
8312 /*
8313 * "ga": Display the ascii value of the character under the
8314 * cursor. It is displayed in decimal, hex, and octal. -- webb
8315 */
8316 case 'a':
8317 do_ascii(NULL);
8318 break;
8319
8320#ifdef FEAT_MBYTE
8321 /*
8322 * "g8": Display the bytes used for the UTF-8 character under the
8323 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008324 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 */
8326 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008327 if (cap->count0 == 8)
8328 utf_find_illegal();
8329 else
8330 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 break;
8332#endif
8333
Bram Moolenaar60402d62017-04-20 18:54:50 +02008334 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008335 case '<':
8336 show_sb_text();
8337 break;
8338
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 /*
8340 * "gg": Goto the first line in file. With a count it goes to
8341 * that line number like for "G". -- webb
8342 */
8343 case 'g':
8344 cap->arg = FALSE;
8345 nv_goto(cap);
8346 break;
8347
8348 /*
8349 * Two-character operators:
8350 * "gq" Format text
8351 * "gw" Format text and keep cursor position
8352 * "g~" Toggle the case of the text.
8353 * "gu" Change text to lower case.
8354 * "gU" Change text to upper case.
8355 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008356 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 */
8358 case 'q':
8359 case 'w':
8360 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008361 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 case '~':
8363 case 'u':
8364 case 'U':
8365 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008366 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 nv_operator(cap);
8368 break;
8369
8370 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008371 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 * current function
8373 * "gD": idem, but in the current file.
8374 */
8375 case 'd':
8376 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008377 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 break;
8379
8380#ifdef FEAT_MOUSE
8381 /*
8382 * g<*Mouse> : <C-*mouse>
8383 */
8384 case K_MIDDLEMOUSE:
8385 case K_MIDDLEDRAG:
8386 case K_MIDDLERELEASE:
8387 case K_LEFTMOUSE:
8388 case K_LEFTDRAG:
8389 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008390 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 case K_RIGHTMOUSE:
8392 case K_RIGHTDRAG:
8393 case K_RIGHTRELEASE:
8394 case K_X1MOUSE:
8395 case K_X1DRAG:
8396 case K_X1RELEASE:
8397 case K_X2MOUSE:
8398 case K_X2DRAG:
8399 case K_X2RELEASE:
8400 mod_mask = MOD_MASK_CTRL;
8401 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8402 break;
8403#endif
8404
8405 case K_IGNORE:
8406 break;
8407
8408 /*
8409 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8410 */
8411 case 'p':
8412 case 'P':
8413 nv_put(cap);
8414 break;
8415
8416#ifdef FEAT_BYTEOFF
8417 /* "go": goto byte count from start of buffer */
8418 case 'o':
8419 goto_byte(cap->count0);
8420 break;
8421#endif
8422
8423 /* "gQ": improved Ex mode */
8424 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008425 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {
8427 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008428 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 break;
8430 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008431
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (!checkclearopq(oap))
8433 do_exmode(TRUE);
8434 break;
8435
8436#ifdef FEAT_JUMPLIST
8437 case ',':
8438 nv_pcmark(cap);
8439 break;
8440
8441 case ';':
8442 cap->count1 = -cap->count1;
8443 nv_pcmark(cap);
8444 break;
8445#endif
8446
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008447 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008448 if (!checkclearop(oap))
8449 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008450 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008451 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008452 if (!checkclearop(oap))
8453 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008454 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008455
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008456 case '+':
8457 case '-': /* "g+" and "g-": undo or redo along the timeline */
8458 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008459 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008460 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008461 break;
8462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 default:
8464 clearopbeep(oap);
8465 break;
8466 }
8467}
8468
8469/*
8470 * Handle "o" and "O" commands.
8471 */
8472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008473n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008475#ifdef FEAT_CONCEAL
8476 linenr_T oldline = curwin->w_cursor.lnum;
8477#endif
8478
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if (!checkclearopq(cap->oap))
8480 {
8481#ifdef FEAT_FOLDING
8482 if (cap->cmdchar == 'O')
8483 /* Open above the first line of a folded sequence of lines */
8484 (void)hasFolding(curwin->w_cursor.lnum,
8485 &curwin->w_cursor.lnum, NULL);
8486 else
8487 /* Open below the last line of a folded sequence of lines */
8488 (void)hasFolding(curwin->w_cursor.lnum,
8489 NULL, &curwin->w_cursor.lnum);
8490#endif
8491 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8492 (cap->cmdchar == 'O' ? 1 : 0)),
8493 (linenr_T)(curwin->w_cursor.lnum +
8494 (cap->cmdchar == 'o' ? 1 : 0))
8495 ) == OK
8496 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8497#ifdef FEAT_COMMENTS
8498 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8499#endif
8500 0, 0))
8501 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008502#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008503 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008504 update_single_line(curwin, oldline);
8505#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008506 /* When '#' is in 'cpoptions' ignore the count. */
8507 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8508 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008509#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008510 if (curwin->w_p_cul)
8511 /* force redraw of cursorline */
8512 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8515 }
8516 }
8517}
8518
8519/*
8520 * "." command: redo last change.
8521 */
8522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008523nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
8525 if (!checkclearopq(cap->oap))
8526 {
8527 /*
8528 * If "restart_edit" is TRUE, the last but one command is repeated
8529 * instead of the last command (inserting text). This is used for
8530 * CTRL-O <.> in insert mode.
8531 */
8532 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8533 clearopbeep(cap->oap);
8534 }
8535}
8536
8537/*
8538 * CTRL-R: undo undo
8539 */
8540 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008541nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542{
8543 if (!checkclearopq(cap->oap))
8544 {
8545 u_redo((int)cap->count1);
8546 curwin->w_set_curswant = TRUE;
8547 }
8548}
8549
8550/*
8551 * Handle "U" command.
8552 */
8553 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008554nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008557 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 {
8559 /* translate "gUU" to "gUgU" */
8560 cap->cmdchar = 'g';
8561 cap->nchar = 'U';
8562 nv_operator(cap);
8563 }
8564 else if (!checkclearopq(cap->oap))
8565 {
8566 u_undoline();
8567 curwin->w_set_curswant = TRUE;
8568 }
8569}
8570
8571/*
8572 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8573 * single character.
8574 */
8575 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008576nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008578 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 n_swapchar(cap);
8580 else
8581 nv_operator(cap);
8582}
8583
8584/*
8585 * Handle an operator command.
8586 * The actual work is done by do_pending_operator().
8587 */
8588 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008589nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591 int op_type;
8592
8593 op_type = get_op_type(cap->cmdchar, cap->nchar);
8594
8595 if (op_type == cap->oap->op_type) /* double operator works on lines */
8596 nv_lineop(cap);
8597 else if (!checkclearop(cap->oap))
8598 {
8599 cap->oap->start = curwin->w_cursor;
8600 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008601#ifdef FEAT_EVAL
8602 set_op_var(op_type);
8603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 }
8605}
8606
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008607#ifdef FEAT_EVAL
8608/*
8609 * Set v:operator to the characters for "optype".
8610 */
8611 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008612set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008613{
8614 char_u opchars[3];
8615
8616 if (optype == OP_NOP)
8617 set_vim_var_string(VV_OP, NULL, 0);
8618 else
8619 {
8620 opchars[0] = get_op_char(optype);
8621 opchars[1] = get_extra_op_char(optype);
8622 opchars[2] = NUL;
8623 set_vim_var_string(VV_OP, opchars, -1);
8624 }
8625}
8626#endif
8627
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628/*
8629 * Handle linewise operator "dd", "yy", etc.
8630 *
8631 * "_" is is a strange motion command that helps make operators more logical.
8632 * It is actually implemented, but not documented in the real Vi. This motion
8633 * command actually refers to "the current line". Commands like "dd" and "yy"
8634 * are really an alternate form of "d_" and "y_". It does accept a count, so
8635 * "d3_" works to delete 3 lines.
8636 */
8637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008638nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639{
8640 cap->oap->motion_type = MLINE;
8641 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8642 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008643 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8644 && cap->oap->motion_force != 'v'
8645 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 || cap->oap->op_type == OP_LSHIFT
8647 || cap->oap->op_type == OP_RSHIFT)
8648 beginline(BL_SOL | BL_FIX);
8649 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8650 beginline(BL_WHITE | BL_FIX);
8651}
8652
8653/*
8654 * <Home> command.
8655 */
8656 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008657nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008659 /* CTRL-HOME is like "gg" */
8660 if (mod_mask & MOD_MASK_CTRL)
8661 nv_goto(cap);
8662 else
8663 {
8664 cap->count0 = 1;
8665 nv_pipe(cap);
8666 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008667 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8668 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669}
8670
8671/*
8672 * "|" command.
8673 */
8674 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008675nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 cap->oap->motion_type = MCHAR;
8678 cap->oap->inclusive = FALSE;
8679 beginline(0);
8680 if (cap->count0 > 0)
8681 {
8682 coladvance((colnr_T)(cap->count0 - 1));
8683 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8684 }
8685 else
8686 curwin->w_curswant = 0;
8687 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008688 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 curwin->w_set_curswant = FALSE;
8690}
8691
8692/*
8693 * Handle back-word command "b" and "B".
8694 * cap->arg is 1 for "B"
8695 */
8696 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008697nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 cap->oap->motion_type = MCHAR;
8700 cap->oap->inclusive = FALSE;
8701 curwin->w_set_curswant = TRUE;
8702 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8703 clearopbeep(cap->oap);
8704#ifdef FEAT_FOLDING
8705 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8706 foldOpenCursor();
8707#endif
8708}
8709
8710/*
8711 * Handle word motion commands "e", "E", "w" and "W".
8712 * cap->arg is TRUE for "E" and "W".
8713 */
8714 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008715nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716{
8717 int n;
8718 int word_end;
8719 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008720 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
8722 /*
8723 * Set inclusive for the "E" and "e" command.
8724 */
8725 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8726 word_end = TRUE;
8727 else
8728 word_end = FALSE;
8729 cap->oap->inclusive = word_end;
8730
8731 /*
8732 * "cw" and "cW" are a special case.
8733 */
8734 if (!word_end && cap->oap->op_type == OP_CHANGE)
8735 {
8736 n = gchar_cursor();
8737 if (n != NUL) /* not an empty line */
8738 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008739 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 {
8741 /*
8742 * Reproduce a funny Vi behaviour: "cw" on a blank only
8743 * changes one character, not all blanks until the start of
8744 * the next word. Only do this when the 'w' flag is included
8745 * in 'cpoptions'.
8746 */
8747 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8748 {
8749 cap->oap->inclusive = TRUE;
8750 cap->oap->motion_type = MCHAR;
8751 return;
8752 }
8753 }
8754 else
8755 {
8756 /*
8757 * This is a little strange. To match what the real Vi does,
8758 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8759 * that we are not on a space or a TAB. This seems impolite
8760 * at first, but it's really more what we mean when we say
8761 * 'cw'.
8762 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008763 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 * will change only one character! This is done by setting
8765 * flag.
8766 */
8767 cap->oap->inclusive = TRUE;
8768 word_end = TRUE;
8769 flag = TRUE;
8770 }
8771 }
8772 }
8773
8774 cap->oap->motion_type = MCHAR;
8775 curwin->w_set_curswant = TRUE;
8776 if (word_end)
8777 n = end_word(cap->count1, cap->arg, flag, FALSE);
8778 else
8779 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8780
Bram Moolenaardfefb982008-04-01 10:06:39 +00008781 /* Don't leave the cursor on the NUL past the end of line. Unless we
8782 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008783 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008784 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 if (n == FAIL && cap->oap->op_type == OP_NOP)
8787 clearopbeep(cap->oap);
8788 else
8789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791#ifdef FEAT_FOLDING
8792 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8793 foldOpenCursor();
8794#endif
8795 }
8796}
8797
8798/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008799 * Used after a movement command: If the cursor ends up on the NUL after the
8800 * end of the line, may move it back to the last character and make the motion
8801 * inclusive.
8802 */
8803 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008804adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008805{
8806 /* The cursor cannot remain on the NUL when:
8807 * - the column is > 0
8808 * - not in Visual mode or 'selection' is "o"
8809 * - 'virtualedit' is not "all" and not "onemore".
8810 */
8811 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008812 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008813#ifdef FEAT_VIRTUALEDIT
8814 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8815#endif
8816 )
8817 {
8818 --curwin->w_cursor.col;
8819#ifdef FEAT_MBYTE
8820 /* prevent cursor from moving on the trail byte */
8821 if (has_mbyte)
8822 mb_adjust_cursor();
8823#endif
8824 oap->inclusive = TRUE;
8825 }
8826}
8827
8828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 * "0" and "^" commands.
8830 * cap->arg is the argument for beginline().
8831 */
8832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008833nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835 cap->oap->motion_type = MCHAR;
8836 cap->oap->inclusive = FALSE;
8837 beginline(cap->arg);
8838#ifdef FEAT_FOLDING
8839 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8840 foldOpenCursor();
8841#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008842 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8843 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844}
8845
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846/*
8847 * In exclusive Visual mode, may include the last character.
8848 */
8849 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008850adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
8852 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008853 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008855#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 if (has_mbyte)
8857 inc_cursor();
8858 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 ++curwin->w_cursor.col;
8861 cap->oap->inclusive = FALSE;
8862 }
8863}
8864
8865/*
8866 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8867 * Should check VIsual_mode before calling this.
8868 * Returns TRUE when backed up to the previous line.
8869 */
8870 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008871unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
8873 pos_T *pp;
8874
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008875 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008877 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 pp = &curwin->w_cursor;
8879 else
8880 pp = &VIsual;
8881#ifdef FEAT_VIRTUALEDIT
8882 if (pp->coladd > 0)
8883 --pp->coladd;
8884 else
8885#endif
8886 if (pp->col > 0)
8887 {
8888 --pp->col;
8889#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008890 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891#endif
8892 }
8893 else if (pp->lnum > 1)
8894 {
8895 --pp->lnum;
8896 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8897 return TRUE;
8898 }
8899 }
8900 return FALSE;
8901}
8902
8903/*
8904 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8905 */
8906 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008907nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 if (VIsual_active)
8910 VIsual_select = TRUE;
8911 else if (VIsual_reselect)
8912 {
8913 cap->nchar = 'v'; /* fake "gv" command */
8914 cap->arg = TRUE;
8915 nv_g_cmd(cap);
8916 }
8917}
8918
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919
8920/*
8921 * "G", "gg", CTRL-END, CTRL-HOME.
8922 * cap->arg is TRUE for "G".
8923 */
8924 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008925nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 linenr_T lnum;
8928
8929 if (cap->arg)
8930 lnum = curbuf->b_ml.ml_line_count;
8931 else
8932 lnum = 1L;
8933 cap->oap->motion_type = MLINE;
8934 setpcmark();
8935
8936 /* When a count is given, use it instead of the default lnum */
8937 if (cap->count0 != 0)
8938 lnum = cap->count0;
8939 if (lnum < 1L)
8940 lnum = 1L;
8941 else if (lnum > curbuf->b_ml.ml_line_count)
8942 lnum = curbuf->b_ml.ml_line_count;
8943 curwin->w_cursor.lnum = lnum;
8944 beginline(BL_SOL | BL_FIX);
8945#ifdef FEAT_FOLDING
8946 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8947 foldOpenCursor();
8948#endif
8949}
8950
8951/*
8952 * CTRL-\ in Normal mode.
8953 */
8954 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008955nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956{
8957 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8958 {
8959 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008960 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 clear_cmdline = TRUE; /* unshow mode later */
8962 restart_edit = 0;
8963#ifdef FEAT_CMDWIN
8964 if (cmdwin_type != 0)
8965 cmdwin_result = Ctrl_C;
8966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 if (VIsual_active)
8968 {
8969 end_visual_mode(); /* stop Visual */
8970 redraw_curbuf_later(INVERTED);
8971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8973 if (cap->nchar == Ctrl_G && p_im)
8974 restart_edit = 'a';
8975 }
8976 else
8977 clearopbeep(cap->oap);
8978}
8979
8980/*
8981 * ESC in Normal mode: beep, but don't flush buffers.
8982 * Don't even beep if we are canceling a command.
8983 */
8984 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008985nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
8987 int no_reason;
8988
8989 no_reason = (cap->oap->op_type == OP_NOP
8990 && cap->opcount == 0
8991 && cap->count0 == 0
8992 && cap->oap->regname == 0
8993 && !p_im);
8994
8995 if (cap->arg) /* TRUE for CTRL-C */
8996 {
8997 if (restart_edit == 0
8998#ifdef FEAT_CMDWIN
8999 && cmdwin_type == 0
9000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02009003 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004
9005 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9006 * set again below when halfway a mapping. */
9007 if (!p_im)
9008 restart_edit = 0;
9009#ifdef FEAT_CMDWIN
9010 if (cmdwin_type != 0)
9011 {
9012 cmdwin_result = K_IGNORE;
9013 got_int = FALSE; /* don't stop executing autocommands et al. */
9014 return;
9015 }
9016#endif
9017 }
9018
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 if (VIsual_active)
9020 {
9021 end_visual_mode(); /* stop Visual */
9022 check_cursor_col(); /* make sure cursor is not beyond EOL */
9023 curwin->w_set_curswant = TRUE;
9024 redraw_curbuf_later(INVERTED);
9025 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009026 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009027 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 clearop(cap->oap);
9029
9030 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9031 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009032 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 restart_edit = 'a';
9034}
9035
9036/*
9037 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009038 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 */
9040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009041nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
9043 /* <Insert> is equal to "i" */
9044 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9045 cap->cmdchar = 'i';
9046
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 /* in Visual mode "A" and "I" are an operator */
9048 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02009049 {
9050#ifdef FEAT_TERMINAL
9051 if (term_in_normal_mode())
9052 {
9053 end_visual_mode();
9054 clearop(cap->oap);
9055 term_enter_job_mode();
9056 return;
9057 }
9058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02009060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061
9062 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009063 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9064 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 {
9066#ifdef FEAT_TEXTOBJ
9067 nv_object(cap);
9068#else
9069 clearopbeep(cap->oap);
9070#endif
9071 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009072#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009073 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009074 {
9075 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009076 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009077 return;
9078 }
9079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 else if (!curbuf->b_p_ma && !p_im)
9081 {
9082 /* Only give this error when 'insertmode' is off. */
9083 EMSG(_(e_modifiable));
9084 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009085 if (cap->cmdchar == K_PS)
9086 /* drop the pasted text */
9087 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009089 else if (cap->cmdchar == K_PS && VIsual_active)
9090 {
9091 pos_T old_pos = curwin->w_cursor;
9092 pos_T old_visual = VIsual;
9093
9094 /* In Visual mode the selected text is deleted. */
9095 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9096 {
9097 shift_delete_registers();
9098 cap->oap->regname = '1';
9099 }
9100 else
9101 cap->oap->regname = '-';
9102 cap->cmdchar = 'd';
9103 cap->nchar = NUL;
9104 nv_operator(cap);
9105 do_pending_operator(cap, 0, FALSE);
9106 cap->cmdchar = K_PS;
9107
9108 /* When the last char in the line was deleted then append. Detect this
9109 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009110 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9111 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009112 inc_cursor();
9113
9114 /* Insert to replace the deleted text with the pasted text. */
9115 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 else if (!checkclearopq(cap->oap))
9118 {
9119 switch (cap->cmdchar)
9120 {
9121 case 'A': /* "A"ppend after the line */
9122 curwin->w_set_curswant = TRUE;
9123#ifdef FEAT_VIRTUALEDIT
9124 if (ve_flags == VE_ALL)
9125 {
9126 int save_State = State;
9127
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009128 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 * character past the end of the line */
9130 State = INSERT;
9131 coladvance((colnr_T)MAXCOL);
9132 State = save_State;
9133 }
9134 else
9135#endif
9136 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9137 break;
9138
9139 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009140 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9141 beginline(BL_WHITE);
9142 else
9143 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 break;
9145
Bram Moolenaara1891842017-02-04 21:34:31 +01009146 case K_PS:
9147 /* Bracketed paste works like "a"ppend, unless the cursor is in
9148 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009149 if (curwin->w_cursor.col == 0)
9150 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009151 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009152
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9154#ifdef FEAT_VIRTUALEDIT
9155 /* increment coladd when in virtual space, increment the
9156 * column otherwise, also to append after an unprintable char */
9157 if (virtual_active()
9158 && (curwin->w_cursor.coladd > 0
9159 || *ml_get_cursor() == NUL
9160 || *ml_get_cursor() == TAB))
9161 curwin->w_cursor.coladd++;
9162 else
9163#endif
9164 if (*ml_get_cursor() != NUL)
9165 inc_cursor();
9166 break;
9167 }
9168
9169#ifdef FEAT_VIRTUALEDIT
9170 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9171 {
9172 int save_State = State;
9173
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009174 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 * character past the end of the line */
9176 State = INSERT;
9177 coladvance(getviscol());
9178 State = save_State;
9179 }
9180#endif
9181
9182 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9183 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009184 else if (cap->cmdchar == K_PS)
9185 /* drop the pasted text */
9186 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187}
9188
9189/*
9190 * Invoke edit() and take care of "restart_edit" and the return value.
9191 */
9192 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009193invoke_edit(
9194 cmdarg_T *cap,
9195 int repl, /* "r" or "gr" command */
9196 int cmd,
9197 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
9199 int restart_edit_save = 0;
9200
9201 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9202 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9203 * it. */
9204 if (repl || !stuff_empty())
9205 restart_edit_save = restart_edit;
9206 else
9207 restart_edit_save = 0;
9208
9209 /* Always reset "restart_edit", this is not a restarted edit. */
9210 restart_edit = 0;
9211
9212 if (edit(cmd, startln, cap->count1))
9213 cap->retval |= CA_COMMAND_BUSY;
9214
9215 if (restart_edit == 0)
9216 restart_edit = restart_edit_save;
9217}
9218
9219#ifdef FEAT_TEXTOBJ
9220/*
9221 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9222 */
9223 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009224nv_object(
9225 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226{
9227 int flag;
9228 int include;
9229 char_u *mps_save;
9230
9231 if (cap->cmdchar == 'i')
9232 include = FALSE; /* "ix" = inner object: exclude white space */
9233 else
9234 include = TRUE; /* "ax" = an object: include white space */
9235
9236 /* Make sure (), [], {} and <> are in 'matchpairs' */
9237 mps_save = curbuf->b_p_mps;
9238 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9239
9240 switch (cap->nchar)
9241 {
9242 case 'w': /* "aw" = a word */
9243 flag = current_word(cap->oap, cap->count1, include, FALSE);
9244 break;
9245 case 'W': /* "aW" = a WORD */
9246 flag = current_word(cap->oap, cap->count1, include, TRUE);
9247 break;
9248 case 'b': /* "ab" = a braces block */
9249 case '(':
9250 case ')':
9251 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9252 break;
9253 case 'B': /* "aB" = a Brackets block */
9254 case '{':
9255 case '}':
9256 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9257 break;
9258 case '[': /* "a[" = a [] block */
9259 case ']':
9260 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9261 break;
9262 case '<': /* "a<" = a <> block */
9263 case '>':
9264 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9265 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009266 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009267 /* Do not adjust oap->end in do_pending_operator()
9268 * otherwise there are different results for 'dit'
9269 * (note leading whitespace in last line):
9270 * 1) <b> 2) <b>
9271 * foobar foobar
9272 * </b> </b>
9273 */
9274 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009275 flag = current_tagblock(cap->oap, cap->count1, include);
9276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 case 'p': /* "ap" = a paragraph */
9278 flag = current_par(cap->oap, cap->count1, include, 'p');
9279 break;
9280 case 's': /* "as" = a sentence */
9281 flag = current_sent(cap->oap, cap->count1, include);
9282 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009283 case '"': /* "a"" = a double quoted string */
9284 case '\'': /* "a'" = a single quoted string */
9285 case '`': /* "a`" = a backtick quoted string */
9286 flag = current_quote(cap->oap, cap->count1, include,
9287 cap->nchar);
9288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289#if 0 /* TODO */
9290 case 'S': /* "aS" = a section */
9291 case 'f': /* "af" = a filename */
9292 case 'u': /* "au" = a URL */
9293#endif
9294 default:
9295 flag = FAIL;
9296 break;
9297 }
9298
9299 curbuf->b_p_mps = mps_save;
9300 if (flag == FAIL)
9301 clearopbeep(cap->oap);
9302 adjust_cursor_col();
9303 curwin->w_set_curswant = TRUE;
9304}
9305#endif
9306
9307/*
9308 * "q" command: Start/stop recording.
9309 * "q:", "q/", "q?": edit command-line in command-line window.
9310 */
9311 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009312nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 if (cap->oap->op_type == OP_FORMAT)
9315 {
9316 /* "gqq" is the same as "gqgq": format line */
9317 cap->cmdchar = 'g';
9318 cap->nchar = 'q';
9319 nv_operator(cap);
9320 }
9321 else if (!checkclearop(cap->oap))
9322 {
9323#ifdef FEAT_CMDWIN
9324 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9325 {
9326 stuffcharReadbuff(cap->nchar);
9327 stuffcharReadbuff(K_CMDWIN);
9328 }
9329 else
9330#endif
9331 /* (stop) recording into a named register, unless executing a
9332 * register */
9333 if (!Exec_reg && do_record(cap->nchar) == FAIL)
9334 clearopbeep(cap->oap);
9335 }
9336}
9337
9338/*
9339 * Handle the "@r" command.
9340 */
9341 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009342nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343{
9344 if (checkclearop(cap->oap))
9345 return;
9346#ifdef FEAT_EVAL
9347 if (cap->nchar == '=')
9348 {
9349 if (get_expr_register() == NUL)
9350 return;
9351 }
9352#endif
9353 while (cap->count1-- && !got_int)
9354 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009355 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 {
9357 clearopbeep(cap->oap);
9358 break;
9359 }
9360 line_breakcheck();
9361 }
9362}
9363
9364/*
9365 * Handle the CTRL-U and CTRL-D commands.
9366 */
9367 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009368nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
9370 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9371 || (cap->cmdchar == Ctrl_D
9372 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9373 clearopbeep(cap->oap);
9374 else if (!checkclearop(cap->oap))
9375 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9376}
9377
9378/*
9379 * Handle "J" or "gJ" command.
9380 */
9381 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009382nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 if (VIsual_active) /* join the visual lines */
9385 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009386 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 {
9388 if (cap->count0 <= 1)
9389 cap->count0 = 2; /* default for join is two lines! */
9390 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9391 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009393 /* can't join when on the last line */
9394 if (cap->count0 <= 2)
9395 {
9396 clearopbeep(cap->oap);
9397 return;
9398 }
9399 cap->count0 = curbuf->b_ml.ml_line_count
9400 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009402
9403 prep_redo(cap->oap->regname, cap->count0,
9404 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9405 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 }
9407}
9408
9409/*
9410 * "P", "gP", "p" and "gp" commands.
9411 */
9412 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009413nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 int regname = 0;
9416 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009417 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009418 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 int dir;
9420 int flags = 0;
9421
9422 if (cap->oap->op_type != OP_NOP)
9423 {
9424#ifdef FEAT_DIFF
9425 /* "dp" is ":diffput" */
9426 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9427 {
9428 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009429 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 }
9431 else
9432#endif
9433 clearopbeep(cap->oap);
9434 }
9435 else
9436 {
9437 dir = (cap->cmdchar == 'P'
9438 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9439 ? BACKWARD : FORWARD;
9440 prep_redo_cmd(cap);
9441 if (cap->cmdchar == 'g')
9442 flags |= PUT_CURSEND;
9443
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 if (VIsual_active)
9445 {
9446 /* Putting in Visual mode: The put text replaces the selected
9447 * text. First delete the selected text, then put the new text.
9448 * Need to save and restore the registers that the delete
9449 * overwrites if the old contents is being put.
9450 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009451 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009453#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009455#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009456 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009457 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009458#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461
9462 )
9463 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009464 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 * put, save it first. */
9466 reg1 = get_register(regname, TRUE);
9467 }
9468
9469 /* Now delete the selected text. */
9470 cap->cmdchar = 'd';
9471 cap->nchar = NUL;
9472 cap->oap->regname = NUL;
9473 nv_operator(cap);
9474 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009475 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476
9477 /* delete PUT_LINE_BACKWARD; */
9478 cap->oap->regname = regname;
9479
9480 if (reg1 != NULL)
9481 {
9482 /* Delete probably changed the register we want to put, save
9483 * it first. Then put back what was there before the delete. */
9484 reg2 = get_register(regname, FALSE);
9485 put_register(regname, reg1);
9486 }
9487
9488 /* When deleted a linewise Visual area, put the register as
9489 * lines to avoid it joined with the next line. When deletion was
9490 * characterwise, split a line when putting lines. */
9491 if (VIsual_mode == 'V')
9492 flags |= PUT_LINE;
9493 else if (VIsual_mode == 'v')
9494 flags |= PUT_LINE_SPLIT;
9495 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9496 flags |= PUT_LINE_FORWARD;
9497 dir = BACKWARD;
9498 if ((VIsual_mode != 'V'
9499 && curwin->w_cursor.col < curbuf->b_op_start.col)
9500 || (VIsual_mode == 'V'
9501 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9502 /* cursor is at the end of the line or end of file, put
9503 * forward. */
9504 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009505 /* May have been reset in do_put(). */
9506 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 do_put(cap->oap->regname, dir, cap->count1, flags);
9509
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 /* If a register was saved, put it back now. */
9511 if (reg2 != NULL)
9512 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009513
9514 /* What to reselect with "gv"? Selecting the just put text seems to
9515 * be the most useful, since the original text was removed. */
9516 if (was_visual)
9517 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009518 curbuf->b_visual.vi_start = curbuf->b_op_start;
9519 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009520 /* need to adjust cursor position */
9521 if (*p_sel == 'e')
9522 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009523 }
9524
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009525 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009526 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009527 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009528 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009529 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009530
9531 /* If the cursor was in that line, move it to the end of the last
9532 * line. */
9533 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9534 {
9535 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9536 coladvance((colnr_T)MAXCOL);
9537 }
9538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 auto_format(FALSE, TRUE);
9540 }
9541}
9542
9543/*
9544 * "o" and "O" commands.
9545 */
9546 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009547nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549#ifdef FEAT_DIFF
9550 /* "do" is ":diffget" */
9551 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9552 {
9553 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009554 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 }
9556 else
9557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 if (VIsual_active) /* switch start and end of visual */
9559 v_swap_corners(cap->cmdchar);
9560 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 n_opencmd(cap);
9562}
9563
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564#ifdef FEAT_NETBEANS_INTG
9565 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009566nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567{
9568 netbeans_keycommand(cap->nchar);
9569}
9570#endif
9571
9572#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009574nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575{
9576 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9577}
9578#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009579
Bram Moolenaar3918c952005-03-15 22:34:55 +00009580/*
9581 * Trigger CursorHold event.
9582 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9583 * input buffer. "did_cursorhold" is set to avoid retriggering.
9584 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009585 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009586nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009587{
9588 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9589 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009590 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009591}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009592
9593/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009594 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009595 */
9596 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009597get_op_vcol(
9598 oparg_T *oap,
9599 colnr_T redo_VIsual_vcol,
9600 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009601{
9602 colnr_T start, end;
9603
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009604 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009605 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009606 return;
9607
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009608 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009609
9610#ifdef FEAT_MBYTE
9611 /* prevent from moving onto a trail byte */
9612 if (has_mbyte)
9613 mb_adjustpos(curwin->w_buffer, &oap->end);
9614#endif
9615
9616 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009617
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009618 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009619 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009620 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9621
9622 if (start < oap->start_vcol)
9623 oap->start_vcol = start;
9624 if (end > oap->end_vcol)
9625 {
9626 if (initial && *p_sel == 'e' && start >= 1
9627 && start - 1 >= oap->end_vcol)
9628 oap->end_vcol = start - 1;
9629 else
9630 oap->end_vcol = end;
9631 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009632 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009633
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009634 /* if '$' was used, get oap->end_vcol from longest line */
9635 if (curwin->w_curswant == MAXCOL)
9636 {
9637 curwin->w_cursor.col = MAXCOL;
9638 oap->end_vcol = 0;
9639 for (curwin->w_cursor.lnum = oap->start.lnum;
9640 curwin->w_cursor.lnum <= oap->end.lnum;
9641 ++curwin->w_cursor.lnum)
9642 {
9643 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9644 if (end > oap->end_vcol)
9645 oap->end_vcol = end;
9646 }
9647 }
9648 else if (redo_VIsual_busy)
9649 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9650 /*
9651 * Correct oap->end.col and oap->start.col to be the
9652 * upper-left and lower-right corner of the block area.
9653 *
9654 * (Actually, this does convert column positions into character
9655 * positions)
9656 */
9657 curwin->w_cursor.lnum = oap->end.lnum;
9658 coladvance(oap->end_vcol);
9659 oap->end = curwin->w_cursor;
9660
9661 curwin->w_cursor = oap->start;
9662 coladvance(oap->start_vcol);
9663 oap->start = curwin->w_cursor;
9664}