blob: 2688bf9f593f83d3e70f4fd4f4b4d33be7c4aeb9 [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);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010035static void op_colon(oparg_T *oap);
36static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010037#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void find_start_of_word(pos_T *);
39static void find_end_of_word(pos_T *);
40static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void prep_redo(int regname, long, int, int, int, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void clearop(oparg_T *oap);
44static void clearopbeep(oparg_T *oap);
45static void unshift_special(cmdarg_T *cap);
46static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
50
51/*
52 * nv_*(): functions called to handle Normal and Visual mode commands.
53 * n_*(): functions called to handle Normal mode commands.
54 * v_*(): functions called to handle Visual mode commands.
55 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010056static void nv_ignore(cmdarg_T *cap);
57static void nv_nop(cmdarg_T *cap);
58static void nv_error(cmdarg_T *cap);
59static void nv_help(cmdarg_T *cap);
60static void nv_addsub(cmdarg_T *cap);
61static void nv_page(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010063static void nv_mousescroll(cmdarg_T *cap);
64static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010066static void nv_scroll_line(cmdarg_T *cap);
67static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_ver_scrollbar(cmdarg_T *cap);
70static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000072#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010073static void nv_tabline(cmdarg_T *cap);
74static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000075#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010076static void nv_exmode(cmdarg_T *cap);
77static void nv_colon(cmdarg_T *cap);
78static void nv_ctrlg(cmdarg_T *cap);
79static void nv_ctrlh(cmdarg_T *cap);
80static void nv_clear(cmdarg_T *cap);
81static void nv_ctrlo(cmdarg_T *cap);
82static void nv_hat(cmdarg_T *cap);
83static void nv_Zet(cmdarg_T *cap);
84static void nv_ident(cmdarg_T *cap);
85static void nv_tagpop(cmdarg_T *cap);
86static void nv_scroll(cmdarg_T *cap);
87static void nv_right(cmdarg_T *cap);
88static void nv_left(cmdarg_T *cap);
89static void nv_up(cmdarg_T *cap);
90static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010091static void nv_end(cmdarg_T *cap);
92static void nv_dollar(cmdarg_T *cap);
93static void nv_search(cmdarg_T *cap);
94static void nv_next(cmdarg_T *cap);
95static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
96static void nv_csearch(cmdarg_T *cap);
97static void nv_brackets(cmdarg_T *cap);
98static void nv_percent(cmdarg_T *cap);
99static void nv_brace(cmdarg_T *cap);
100static void nv_mark(cmdarg_T *cap);
101static void nv_findpar(cmdarg_T *cap);
102static void nv_undo(cmdarg_T *cap);
103static void nv_kundo(cmdarg_T *cap);
104static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100105static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100106static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
107static void v_visop(cmdarg_T *cap);
108static void nv_subst(cmdarg_T *cap);
109static void nv_abbrev(cmdarg_T *cap);
110static void nv_optrans(cmdarg_T *cap);
111static void nv_gomark(cmdarg_T *cap);
112static void nv_pcmark(cmdarg_T *cap);
113static void nv_regname(cmdarg_T *cap);
114static void nv_visual(cmdarg_T *cap);
115static void n_start_visual_mode(int c);
116static void nv_window(cmdarg_T *cap);
117static void nv_suspend(cmdarg_T *cap);
118static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100119static void nv_dot(cmdarg_T *cap);
120static void nv_redo(cmdarg_T *cap);
121static void nv_Undo(cmdarg_T *cap);
122static void nv_tilde(cmdarg_T *cap);
123static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000124#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000126#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100127static void nv_lineop(cmdarg_T *cap);
128static void nv_home(cmdarg_T *cap);
129static void nv_pipe(cmdarg_T *cap);
130static void nv_bck_word(cmdarg_T *cap);
131static void nv_wordcmd(cmdarg_T *cap);
132static void nv_beginline(cmdarg_T *cap);
133static void adjust_cursor(oparg_T *oap);
134static void adjust_for_sel(cmdarg_T *cap);
135static int unadjust_for_sel(void);
136static void nv_select(cmdarg_T *cap);
137static void nv_goto(cmdarg_T *cap);
138static void nv_normal(cmdarg_T *cap);
139static void nv_esc(cmdarg_T *oap);
140static void nv_edit(cmdarg_T *cap);
141static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100143static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100145static void nv_record(cmdarg_T *cap);
146static void nv_at(cmdarg_T *cap);
147static void nv_halfpage(cmdarg_T *cap);
148static void nv_join(cmdarg_T *cap);
149static void nv_put(cmdarg_T *cap);
150static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100152static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
154#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100155static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100157static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100158static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000160static char *e_noident = N_("E349: No identifier under cursor");
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Function to be called for a Normal or Visual mode command.
164 * The argument is a cmdarg_T.
165 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100166typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168/* Values for cmd_flags. */
169#define NV_NCH 0x01 /* may need to get a second char */
170#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
171#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
172#define NV_LANG 0x08 /* second char needs language adjustment */
173
174#define NV_SS 0x10 /* may start selection */
175#define NV_SSS 0x20 /* may start selection with shift modifier */
176#define NV_STS 0x40 /* may stop selection without shift modif. */
177#define NV_RL 0x80 /* 'rightleft' modifies command */
178#define NV_KEEPREG 0x100 /* don't clear regname */
179#define NV_NCW 0x200 /* not allowed in command-line window */
180
181/*
182 * Generally speaking, every Normal mode command should either clear any
183 * pending operator (with *clearop*()), or set the motion type variable
184 * oap->motion_type.
185 *
186 * When a cursor motion command is made, it is marked as being a character or
187 * line oriented motion. Then, if an operator is in effect, the operation
188 * becomes character or line oriented accordingly.
189 */
190
191/*
192 * This table contains one entry for every Normal or Visual mode command.
193 * The order doesn't matter, init_normal_cmds() will create a sorted index.
194 * It is faster when all keys from zero to '~' are present.
195 */
196static const struct nv_cmd
197{
198 int cmd_char; /* (first) command character */
199 nv_func_T cmd_func; /* function for this command */
200 short_u cmd_flags; /* NV_ flags */
201 short cmd_arg; /* value for ca.arg */
202} nv_cmds[] =
203{
204 {NUL, nv_error, 0, 0},
205 {Ctrl_A, nv_addsub, 0, 0},
206 {Ctrl_B, nv_page, NV_STS, BACKWARD},
207 {Ctrl_C, nv_esc, 0, TRUE},
208 {Ctrl_D, nv_halfpage, 0, 0},
209 {Ctrl_E, nv_scroll_line, 0, TRUE},
210 {Ctrl_F, nv_page, NV_STS, FORWARD},
211 {Ctrl_G, nv_ctrlg, 0, 0},
212 {Ctrl_H, nv_ctrlh, 0, 0},
213 {Ctrl_I, nv_pcmark, 0, 0},
214 {NL, nv_down, 0, FALSE},
215 {Ctrl_K, nv_error, 0, 0},
216 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000217 {Ctrl_M, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 {Ctrl_N, nv_down, NV_STS, FALSE},
219 {Ctrl_O, nv_ctrlo, 0, 0},
220 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000221 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222 {Ctrl_R, nv_redo, 0, 0},
223 {Ctrl_S, nv_ignore, 0, 0},
224 {Ctrl_T, nv_tagpop, NV_NCW, 0},
225 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 {Ctrl_V, nv_visual, 0, FALSE},
227 {'V', nv_visual, 0, FALSE},
228 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 {Ctrl_W, nv_window, 0, 0},
230 {Ctrl_X, nv_addsub, 0, 0},
231 {Ctrl_Y, nv_scroll_line, 0, FALSE},
232 {Ctrl_Z, nv_suspend, 0, 0},
233 {ESC, nv_esc, 0, FALSE},
234 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
235 {Ctrl_RSB, nv_ident, NV_NCW, 0},
236 {Ctrl_HAT, nv_hat, NV_NCW, 0},
237 {Ctrl__, nv_error, 0, 0},
238 {' ', nv_right, 0, 0},
239 {'!', nv_operator, 0, 0},
240 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
241 {'#', nv_ident, 0, 0},
242 {'$', nv_dollar, 0, 0},
243 {'%', nv_percent, 0, 0},
244 {'&', nv_optrans, 0, 0},
245 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
246 {'(', nv_brace, 0, BACKWARD},
247 {')', nv_brace, 0, FORWARD},
248 {'*', nv_ident, 0, 0},
249 {'+', nv_down, 0, TRUE},
250 {',', nv_csearch, 0, TRUE},
251 {'-', nv_up, 0, TRUE},
252 {'.', nv_dot, NV_KEEPREG, 0},
253 {'/', nv_search, 0, FALSE},
254 {'0', nv_beginline, 0, 0},
255 {'1', nv_ignore, 0, 0},
256 {'2', nv_ignore, 0, 0},
257 {'3', nv_ignore, 0, 0},
258 {'4', nv_ignore, 0, 0},
259 {'5', nv_ignore, 0, 0},
260 {'6', nv_ignore, 0, 0},
261 {'7', nv_ignore, 0, 0},
262 {'8', nv_ignore, 0, 0},
263 {'9', nv_ignore, 0, 0},
264 {':', nv_colon, 0, 0},
265 {';', nv_csearch, 0, FALSE},
266 {'<', nv_operator, NV_RL, 0},
267 {'=', nv_operator, 0, 0},
268 {'>', nv_operator, NV_RL, 0},
269 {'?', nv_search, 0, FALSE},
270 {'@', nv_at, NV_NCH_NOP, FALSE},
271 {'A', nv_edit, 0, 0},
272 {'B', nv_bck_word, 0, 1},
273 {'C', nv_abbrev, NV_KEEPREG, 0},
274 {'D', nv_abbrev, NV_KEEPREG, 0},
275 {'E', nv_wordcmd, 0, TRUE},
276 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
277 {'G', nv_goto, 0, TRUE},
278 {'H', nv_scroll, 0, 0},
279 {'I', nv_edit, 0, 0},
280 {'J', nv_join, 0, 0},
281 {'K', nv_ident, 0, 0},
282 {'L', nv_scroll, 0, 0},
283 {'M', nv_scroll, 0, 0},
284 {'N', nv_next, 0, SEARCH_REV},
285 {'O', nv_open, 0, 0},
286 {'P', nv_put, 0, 0},
287 {'Q', nv_exmode, NV_NCW, 0},
288 {'R', nv_Replace, 0, FALSE},
289 {'S', nv_subst, NV_KEEPREG, 0},
290 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
291 {'U', nv_Undo, 0, 0},
292 {'W', nv_wordcmd, 0, TRUE},
293 {'X', nv_abbrev, NV_KEEPREG, 0},
294 {'Y', nv_abbrev, NV_KEEPREG, 0},
295 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
296 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
297 {'\\', nv_error, 0, 0},
298 {']', nv_brackets, NV_NCH_ALW, FORWARD},
299 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
300 {'_', nv_lineop, 0, 0},
301 {'`', nv_gomark, NV_NCH_ALW, FALSE},
302 {'a', nv_edit, NV_NCH, 0},
303 {'b', nv_bck_word, 0, 0},
304 {'c', nv_operator, 0, 0},
305 {'d', nv_operator, 0, 0},
306 {'e', nv_wordcmd, 0, FALSE},
307 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
308 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
309 {'h', nv_left, NV_RL, 0},
310 {'i', nv_edit, NV_NCH, 0},
311 {'j', nv_down, 0, FALSE},
312 {'k', nv_up, 0, FALSE},
313 {'l', nv_right, NV_RL, 0},
314 {'m', nv_mark, NV_NCH_NOP, 0},
315 {'n', nv_next, 0, 0},
316 {'o', nv_open, 0, 0},
317 {'p', nv_put, 0, 0},
318 {'q', nv_record, NV_NCH, 0},
319 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
320 {'s', nv_subst, NV_KEEPREG, 0},
321 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
322 {'u', nv_undo, 0, 0},
323 {'w', nv_wordcmd, 0, FALSE},
324 {'x', nv_abbrev, NV_KEEPREG, 0},
325 {'y', nv_operator, 0, 0},
326 {'z', nv_zet, NV_NCH_ALW, 0},
327 {'{', nv_findpar, 0, BACKWARD},
328 {'|', nv_pipe, 0, 0},
329 {'}', nv_findpar, 0, FORWARD},
330 {'~', nv_tilde, 0, 0},
331
332 /* pound sign */
333 {POUND, nv_ident, 0, 0},
334#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200335 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
336 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
337 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
338 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {K_LEFTMOUSE, nv_mouse, 0, 0},
340 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
341 {K_LEFTDRAG, nv_mouse, 0, 0},
342 {K_LEFTRELEASE, nv_mouse, 0, 0},
343 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100344 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
346 {K_MIDDLEDRAG, nv_mouse, 0, 0},
347 {K_MIDDLERELEASE, nv_mouse, 0, 0},
348 {K_RIGHTMOUSE, nv_mouse, 0, 0},
349 {K_RIGHTDRAG, nv_mouse, 0, 0},
350 {K_RIGHTRELEASE, nv_mouse, 0, 0},
351 {K_X1MOUSE, nv_mouse, 0, 0},
352 {K_X1DRAG, nv_mouse, 0, 0},
353 {K_X1RELEASE, nv_mouse, 0, 0},
354 {K_X2MOUSE, nv_mouse, 0, 0},
355 {K_X2DRAG, nv_mouse, 0, 0},
356 {K_X2RELEASE, nv_mouse, 0, 0},
357#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000358 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000359 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {K_INS, nv_edit, 0, 0},
361 {K_KINS, nv_edit, 0, 0},
362 {K_BS, nv_ctrlh, 0, 0},
363 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
364 {K_S_UP, nv_page, NV_SS, BACKWARD},
365 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
366 {K_S_DOWN, nv_page, NV_SS, FORWARD},
367 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
368 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
369 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
370 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
371 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
372 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
373 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
374 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
375 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
376 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
377 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
378 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {K_S_END, nv_end, NV_SS, FALSE},
380 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
381 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
382 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {K_S_HOME, nv_home, NV_SS, 0},
384 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
385 {K_DEL, nv_abbrev, 0, 0},
386 {K_KDEL, nv_abbrev, 0, 0},
387 {K_UNDO, nv_kundo, 0, 0},
388 {K_HELP, nv_help, NV_NCW, 0},
389 {K_F1, nv_help, NV_NCW, 0},
390 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#ifdef FEAT_GUI
393 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
394 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
395#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000396#ifdef FEAT_GUI_TABLINE
397 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000398 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#ifdef FEAT_FKMAP
Bram Moolenaaree2615a2016-07-02 18:25:34 +0200401 {K_F8, farsi_f8, 0, 0},
402 {K_F9, farsi_f9, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404#ifdef FEAT_NETBEANS_INTG
405 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
406#endif
407#ifdef FEAT_DND
408 {K_DROP, nv_drop, NV_STS, 0},
409#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000410 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100411 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412};
413
414/* Number of commands in nv_cmds[]. */
415#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
416
417/* Sorted index of commands in nv_cmds[]. */
418static short nv_cmd_idx[NV_CMDS_SIZE];
419
420/* The highest index for which
421 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
422static int nv_max_linear;
423
424/*
425 * Compare functions for qsort() below, that checks the command character
426 * through the index in nv_cmd_idx[].
427 */
428 static int
429#ifdef __BORLANDC__
430_RTLENTRYF
431#endif
Bram Moolenaar9b578142016-01-30 19:39:49 +0100432nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433{
434 int c1, c2;
435
436 /* The commands are sorted on absolute value. */
437 c1 = nv_cmds[*(const short *)s1].cmd_char;
438 c2 = nv_cmds[*(const short *)s2].cmd_char;
439 if (c1 < 0)
440 c1 = -c1;
441 if (c2 < 0)
442 c2 = -c2;
443 return c1 - c2;
444}
445
446/*
447 * Initialize the nv_cmd_idx[] table.
448 */
449 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100450init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
452 int i;
453
454 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000455 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 nv_cmd_idx[i] = i;
457
458 /* Sort the commands by the command character. */
459 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
460
461 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000462 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
464 break;
465 nv_max_linear = i - 1;
466}
467
468/*
469 * Search for a command in the commands table.
470 * Returns -1 for invalid command.
471 */
472 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100473find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
475 int i;
476 int idx;
477 int top, bot;
478 int c;
479
480#ifdef FEAT_MBYTE
481 /* A multi-byte character is never a command. */
482 if (cmdchar >= 0x100)
483 return -1;
484#endif
485
486 /* We use the absolute value of the character. Special keys have a
487 * negative value, but are sorted on their absolute value. */
488 if (cmdchar < 0)
489 cmdchar = -cmdchar;
490
491 /* If the character is in the first part: The character is the index into
492 * nv_cmd_idx[]. */
493 if (cmdchar <= nv_max_linear)
494 return nv_cmd_idx[cmdchar];
495
496 /* Perform a binary search. */
497 bot = nv_max_linear + 1;
498 top = NV_CMDS_SIZE - 1;
499 idx = -1;
500 while (bot <= top)
501 {
502 i = (top + bot) / 2;
503 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
504 if (c < 0)
505 c = -c;
506 if (cmdchar == c)
507 {
508 idx = nv_cmd_idx[i];
509 break;
510 }
511 if (cmdchar > c)
512 bot = i + 1;
513 else
514 top = i - 1;
515 }
516 return idx;
517}
518
519/*
520 * Execute a command in Normal mode.
521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100523normal_cmd(
524 oparg_T *oap,
525 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 cmdarg_T ca; /* command arguments */
528 int c;
529 int ctrl_w = FALSE; /* got CTRL-W command */
530 int old_col = curwin->w_curswant;
531#ifdef FEAT_CMDL_INFO
532 int need_flushbuf; /* need to call out_flush() */
533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 pos_T old_pos; /* cursor position before command */
535 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 static int old_mapped_len = 0;
537 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000538#ifdef FEAT_EVAL
539 int set_prevcount = FALSE;
540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541
542 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
543 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000544
545 /* Use a count remembered from before entering an operator. After typing
546 * "3d" we return from normal_cmd() and come back here, the "3" is
547 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 ca.opcount = opcount;
549
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 /*
551 * If there is an operator pending, then the command we take this time
552 * will terminate it. Finish_op tells us to finish the operation before
553 * returning this time (unless the operation was cancelled).
554 */
555#ifdef CURSOR_SHAPE
556 c = finish_op;
557#endif
558 finish_op = (oap->op_type != OP_NOP);
559#ifdef CURSOR_SHAPE
560 if (finish_op != c)
561 {
562 ui_cursor_shape(); /* may show different cursor shape */
563# ifdef FEAT_MOUSESHAPE
564 update_mouseshape(-1);
565# endif
566 }
567#endif
568
Bram Moolenaara983fe92008-07-31 20:04:27 +0000569 /* When not finishing an operator and no register name typed, reset the
570 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000574#ifdef FEAT_EVAL
575 set_prevcount = TRUE;
576#endif
577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578
Bram Moolenaara983fe92008-07-31 20:04:27 +0000579 /* Restore counts from before receiving K_CURSORHOLD. This means after
580 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
581 * "3 * 2". */
582 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
583 {
584 ca.opcount = oap->prev_opcount;
585 ca.count0 = oap->prev_count0;
586 oap->prev_opcount = 0;
587 oap->prev_count0 = 0;
588 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 State = NORMAL_BUSY;
593#ifdef USE_ON_FLY_SCROLL
594 dont_scroll = FALSE; /* allow scrolling here */
595#endif
596
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100597#ifdef FEAT_EVAL
598 /* Set v:count here, when called from main() and not a stuffed
599 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100600 * when there is no count. Do set it for redo. */
601 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100602 set_vcount_ca(&ca, &set_prevcount);
603#endif
604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 /*
606 * Get the command character from the user.
607 */
608 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100609 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 /*
612 * If a mapping was started in Visual or Select mode, remember the length
613 * of the mapping. This is used below to not return to Insert mode for as
614 * long as the mapping is being executed.
615 */
616 if (restart_edit == 0)
617 old_mapped_len = 0;
618 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000619 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 old_mapped_len = typebuf_maplen();
621
622 if (c == NUL)
623 c = K_ZERO;
624
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 /*
626 * In Select mode, typed text replaces the selection.
627 */
628 if (VIsual_active
629 && VIsual_select
630 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
631 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000632 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
633 * 'insertmode' is set) fake a "d"elete command, Insert mode will
634 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000635 * Insert the typed character in the typeahead buffer, so that it can
636 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000637 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000638 if (restart_edit != 0)
639 c = 'd';
640 else
641 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000642 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200643 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646#ifdef FEAT_CMDL_INFO
647 need_flushbuf = add_to_showcmd(c);
648#endif
649
650getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {
653 /*
654 * Handle a count before a command and compute ca.count0.
655 * Note that '0' is a command and not the start of a count, but it's
656 * part of a count after other digits.
657 */
658 while ( (c >= '1' && c <= '9')
659 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
660 {
661 if (c == K_DEL || c == K_KDEL)
662 {
663 ca.count0 /= 10;
664#ifdef FEAT_CMDL_INFO
665 del_from_showcmd(4); /* delete the digit and ~@% */
666#endif
667 }
668 else
669 ca.count0 = ca.count0 * 10 + (c - '0');
670 if (ca.count0 < 0) /* got too large! */
671 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000672#ifdef FEAT_EVAL
673 /* Set v:count here, when called from main() and not a stuffed
674 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100675 * right after the count. Do set it for redo. */
676 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100677 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 if (ctrl_w)
680 {
681 ++no_mapping;
682 ++allow_keys; /* no mapping for nchar, but keys */
683 }
684 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000685 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 --no_zero_mapping;
688 if (ctrl_w)
689 {
690 --no_mapping;
691 --allow_keys;
692 }
693#ifdef FEAT_CMDL_INFO
694 need_flushbuf |= add_to_showcmd(c);
695#endif
696 }
697
698 /*
699 * If we got CTRL-W there may be a/another count
700 */
701 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
702 {
703 ctrl_w = TRUE;
704 ca.opcount = ca.count0; /* remember first count */
705 ca.count0 = 0;
706 ++no_mapping;
707 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000708 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 --no_mapping;
711 --allow_keys;
712#ifdef FEAT_CMDL_INFO
713 need_flushbuf |= add_to_showcmd(c);
714#endif
715 goto getcount; /* jump back */
716 }
717 }
718
Bram Moolenaara983fe92008-07-31 20:04:27 +0000719 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000721 /* Save the count values so that ca.opcount and ca.count0 are exactly
722 * the same when coming back here after handling K_CURSORHOLD. */
723 oap->prev_opcount = ca.opcount;
724 oap->prev_count0 = ca.count0;
725 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100726 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000727 {
728 /*
729 * If we're in the middle of an operator (including after entering a
730 * yank buffer with '"') AND we had a count before the operator, then
731 * that count overrides the current value of ca.count0.
732 * What this means effectively, is that commands like "3dw" get turned
733 * into "d3w" which makes things fall into place pretty neatly.
734 * If you give a count before AND after the operator, they are
735 * multiplied.
736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (ca.count0)
738 ca.count0 *= ca.opcount;
739 else
740 ca.count0 = ca.opcount;
741 }
742
743 /*
744 * Always remember the count. It will be set to zero (on the next call,
745 * above) when there is no pending operator.
746 * When called from main(), save the count for use by the "count" built-in
747 * variable.
748 */
749 ca.opcount = ca.count0;
750 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
751
752#ifdef FEAT_EVAL
753 /*
754 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100755 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100757 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000758 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759#endif
760
761 /*
762 * Find the command character in the table of commands.
763 * For CTRL-W we already got nchar when looking for a count.
764 */
765 if (ctrl_w)
766 {
767 ca.nchar = c;
768 ca.cmdchar = Ctrl_W;
769 }
770 else
771 ca.cmdchar = c;
772 idx = find_command(ca.cmdchar);
773 if (idx < 0)
774 {
775 /* Not a known command: beep. */
776 clearopbeep(oap);
777 goto normal_end;
778 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000779
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000780 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200782 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000784 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 goto normal_end;
786 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000787 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
788 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /*
791 * In Visual/Select mode, a few keys are handled in a special way.
792 */
793 if (VIsual_active)
794 {
795 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
796 if (km_stopsel
797 && (nv_cmds[idx].cmd_flags & NV_STS)
798 && !(mod_mask & MOD_MASK_SHIFT))
799 {
800 end_visual_mode();
801 redraw_curbuf_later(INVERTED);
802 }
803
804 /* Keys that work different when 'keymodel' contains "startsel" */
805 if (km_startsel)
806 {
807 if (nv_cmds[idx].cmd_flags & NV_SS)
808 {
809 unshift_special(&ca);
810 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000811 if (idx < 0)
812 {
813 /* Just in case */
814 clearopbeep(oap);
815 goto normal_end;
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
818 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
819 && (mod_mask & MOD_MASK_SHIFT))
820 {
821 mod_mask &= ~MOD_MASK_SHIFT;
822 }
823 }
824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826#ifdef FEAT_RIGHTLEFT
827 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
828 && (nv_cmds[idx].cmd_flags & NV_RL))
829 {
830 /* Invert horizontal movements and operations. Only when typed by the
831 * user directly, not when the result of a mapping or "x" translated
832 * to "dl". */
833 switch (ca.cmdchar)
834 {
835 case 'l': ca.cmdchar = 'h'; break;
836 case K_RIGHT: ca.cmdchar = K_LEFT; break;
837 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
838 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
839 case 'h': ca.cmdchar = 'l'; break;
840 case K_LEFT: ca.cmdchar = K_RIGHT; break;
841 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
842 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
843 case '>': ca.cmdchar = '<'; break;
844 case '<': ca.cmdchar = '>'; break;
845 }
846 idx = find_command(ca.cmdchar);
847 }
848#endif
849
850 /*
851 * Get an additional character if we need one.
852 */
853 if ((nv_cmds[idx].cmd_flags & NV_NCH)
854 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
855 && oap->op_type == OP_NOP)
856 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
857 || (ca.cmdchar == 'q'
858 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200859 && reg_recording == 0
860 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100862 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {
864 int *cp;
865 int repl = FALSE; /* get character for replace mode */
866 int lit = FALSE; /* get extra character literally */
867 int langmap_active = FALSE; /* using :lmap mappings */
868 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100869#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 int save_smd; /* saved value of p_smd */
871#endif
872
873 ++no_mapping;
874 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000875 /* Don't generate a CursorHold event here, most commands can't handle
876 * it, e.g., nv_replace(), nv_csearch(). */
877 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (ca.cmdchar == 'g')
879 {
880 /*
881 * For 'g' get the next character now, so that we can check for
882 * "gr", "g'" and "g`".
883 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000884 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_CMDL_INFO
887 need_flushbuf |= add_to_showcmd(ca.nchar);
888#endif
889 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100890 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
892 cp = &ca.extra_char; /* need to get a third character */
893 if (ca.nchar != 'r')
894 lit = TRUE; /* get it literally */
895 else
896 repl = TRUE; /* get it in replace mode */
897 }
898 else
899 cp = NULL; /* no third character needed */
900 }
901 else
902 {
903 if (ca.cmdchar == 'r') /* get it in replace mode */
904 repl = TRUE;
905 cp = &ca.nchar;
906 }
907 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
908
909 /*
910 * Get a second or third character.
911 */
912 if (cp != NULL)
913 {
914#ifdef CURSOR_SHAPE
915 if (repl)
916 {
917 State = REPLACE; /* pretend Replace mode */
918 ui_cursor_shape(); /* show different cursor shape */
919 }
920#endif
921 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
922 {
923 /* Allow mappings defined with ":lmap". */
924 --no_mapping;
925 --allow_keys;
926 if (repl)
927 State = LREPLACE;
928 else
929 State = LANGMAP;
930 langmap_active = TRUE;
931 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100932#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 save_smd = p_smd;
934 p_smd = FALSE; /* Don't let the IM code show the mode here */
935 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
936 im_set_active(TRUE);
937#endif
938
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000939 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 if (langmap_active)
942 {
943 /* Undo the decrement done above */
944 ++no_mapping;
945 ++allow_keys;
946 State = NORMAL_BUSY;
947 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100948#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (lang)
950 {
951 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
952 im_save_status(&curbuf->b_p_iminsert);
953 im_set_active(FALSE);
954 }
955 p_smd = save_smd;
956#endif
957#ifdef CURSOR_SHAPE
958 State = NORMAL_BUSY;
959#endif
960#ifdef FEAT_CMDL_INFO
961 need_flushbuf |= add_to_showcmd(*cp);
962#endif
963
964 if (!lit)
965 {
966#ifdef FEAT_DIGRAPHS
967 /* Typing CTRL-K gets a digraph. */
968 if (*cp == Ctrl_K
969 && ((nv_cmds[idx].cmd_flags & NV_LANG)
970 || cp == &ca.extra_char)
971 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
972 {
973 c = get_digraph(FALSE);
974 if (c > 0)
975 {
976 *cp = c;
977# ifdef FEAT_CMDL_INFO
978 /* Guessing how to update showcmd here... */
979 del_from_showcmd(3);
980 need_flushbuf |= add_to_showcmd(*cp);
981# endif
982 }
983 }
984#endif
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 /* adjust chars > 127, except after "tTfFr" commands */
987 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#ifdef FEAT_RIGHTLEFT
989 /* adjust Hebrew mapped char */
990 if (p_hkmap && lang && KeyTyped)
991 *cp = hkmap(*cp);
992# ifdef FEAT_FKMAP
993 /* adjust Farsi mapped char */
994 if (p_fkmap && lang && KeyTyped)
995 *cp = fkmap(*cp);
996# endif
997#endif
998 }
999
1000 /*
1001 * When the next character is CTRL-\ a following CTRL-N means the
1002 * command is aborted and we go to Normal mode.
1003 */
1004 if (cp == &ca.extra_char
1005 && ca.nchar == Ctrl_BSL
1006 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1007 {
1008 ca.cmdchar = Ctrl_BSL;
1009 ca.nchar = ca.extra_char;
1010 idx = find_command(ca.cmdchar);
1011 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001012 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001013 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 else if (*cp == Ctrl_BSL)
1015 {
1016 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1017
1018 /* There is a busy wait here when typing "f<C-\>" and then
1019 * something different from CTRL-N. Can't be avoided. */
1020 while ((c = vpeekc()) <= 0 && towait > 0L)
1021 {
1022 do_sleep(towait > 50L ? 50L : towait);
1023 towait -= 50L;
1024 }
1025 if (c > 0)
1026 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001027 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (c != Ctrl_N && c != Ctrl_G)
1029 vungetc(c);
1030 else
1031 {
1032 ca.cmdchar = Ctrl_BSL;
1033 ca.nchar = c;
1034 idx = find_command(ca.cmdchar);
1035 }
1036 }
1037 }
1038
1039#ifdef FEAT_MBYTE
1040 /* When getting a text character and the next character is a
1041 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001042 * However, don't wait for it to arrive. Also, do enable mapping,
1043 * because if it's put back with vungetc() it's too late to apply
1044 * mapping. */
1045 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 while (enc_utf8 && lang && (c = vpeekc()) > 0
1047 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1048 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001049 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (!utf_iscomposing(c))
1051 {
1052 vungetc(c); /* it wasn't, put it back */
1053 break;
1054 }
1055 else if (ca.ncharC1 == 0)
1056 ca.ncharC1 = c;
1057 else
1058 ca.ncharC2 = c;
1059 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001060 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
1062 }
1063 --no_mapping;
1064 --allow_keys;
1065 }
1066
1067#ifdef FEAT_CMDL_INFO
1068 /*
1069 * Flush the showcmd characters onto the screen so we can see them while
1070 * the command is being executed. Only do this when the shown command was
1071 * actually displayed, otherwise this will slow down a lot when executing
1072 * mappings.
1073 */
1074 if (need_flushbuf)
1075 out_flush();
1076#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001077 if (ca.cmdchar != K_IGNORE)
1078 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
1080 State = NORMAL;
1081
1082 if (ca.nchar == ESC)
1083 {
1084 clearop(oap);
1085 if (restart_edit == 0 && goto_im())
1086 restart_edit = 'a';
1087 goto normal_end;
1088 }
1089
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001090 if (ca.cmdchar != K_IGNORE)
1091 {
1092 msg_didout = FALSE; /* don't scroll screen up for normal command */
1093 msg_col = 0;
1094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 old_pos = curwin->w_cursor; /* remember where cursor was */
1097
1098 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1099 * mode. */
1100 if (!VIsual_active && km_startsel)
1101 {
1102 if (nv_cmds[idx].cmd_flags & NV_SS)
1103 {
1104 start_selection();
1105 unshift_special(&ca);
1106 idx = find_command(ca.cmdchar);
1107 }
1108 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1109 && (mod_mask & MOD_MASK_SHIFT))
1110 {
1111 start_selection();
1112 mod_mask &= ~MOD_MASK_SHIFT;
1113 }
1114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
1116 /*
1117 * Execute the command!
1118 * Call the command function found in the commands table.
1119 */
1120 ca.arg = nv_cmds[idx].cmd_arg;
1121 (nv_cmds[idx].cmd_func)(&ca);
1122
1123 /*
1124 * If we didn't start or finish an operator, reset oap->regname, unless we
1125 * need it later.
1126 */
1127 if (!finish_op
1128 && !oap->op_type
1129 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1130 {
1131 clearop(oap);
1132#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001133 {
1134 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001135
Bram Moolenaar536681b2011-05-10 16:12:45 +02001136 /* Adjust the register according to 'clipboard', so that when
1137 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001138# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001139 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001140# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001141 set_reg_var(regname);
1142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#endif
1144 }
1145
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001146 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001147 * character or "z333<cr>". */
1148 if (old_mapped_len > 0)
1149 old_mapped_len = typebuf_maplen();
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 /*
1152 * If an operation is pending, handle it...
1153 */
1154 do_pending_operator(&ca, old_col, FALSE);
1155
1156 /*
1157 * Wait for a moment when a message is displayed that will be overwritten
1158 * by the mode message.
1159 * In Visual mode and with "^O" in Insert mode, a short message will be
1160 * overwritten by the mode message. Wait a bit, until a key is hit.
1161 * In Visual mode, it's more important to keep the Visual area updated
1162 * than keeping a message (e.g. from a /pat search).
1163 * Only do this if the command was typed, not from a mapping.
1164 * Don't wait when emsg_silent is non-zero.
1165 * Also wait a bit after an error message, e.g. for "^O:".
1166 * Don't redraw the screen, it would remove the message.
1167 */
1168 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001169 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 || (VIsual_active
1172 && old_pos.lnum == curwin->w_cursor.lnum
1173 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 )
1175 && (clear_cmdline
1176 || redraw_cmdline)
1177 && (msg_didout || (msg_didany && msg_scroll))
1178 && !msg_nowait
1179 && KeyTyped)
1180 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 && (msg_scroll
1183 || emsg_on_display)))
1184 && oap->regname == 0
1185 && !(ca.retval & CA_COMMAND_BUSY)
1186 && stuff_empty()
1187 && typebuf_typed()
1188 && emsg_silent == 0
1189 && !did_wait_return
1190 && oap->op_type == OP_NOP)
1191 {
1192 int save_State = State;
1193
1194 /* Draw the cursor with the right shape here */
1195 if (restart_edit != 0)
1196 State = INSERT;
1197
1198 /* If need to redraw, and there is a "keep_msg", redraw before the
1199 * delay */
1200 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1201 {
1202 char_u *kmsg;
1203
1204 kmsg = keep_msg;
1205 keep_msg = NULL;
1206 /* showmode() will clear keep_msg, but we want to use it anyway */
1207 update_screen(0);
1208 /* now reset it, otherwise it's put in the history again */
1209 keep_msg = kmsg;
1210 msg_attr(kmsg, keep_msg_attr);
1211 vim_free(kmsg);
1212 }
1213 setcursor();
1214 cursor_on();
1215 out_flush();
1216 if (msg_scroll || emsg_on_display)
1217 ui_delay(1000L, TRUE); /* wait at least one second */
1218 ui_delay(3000L, FALSE); /* wait up to three seconds */
1219 State = save_State;
1220
1221 msg_scroll = FALSE;
1222 emsg_on_display = FALSE;
1223 }
1224
1225 /*
1226 * Finish up after executing a Normal mode command.
1227 */
1228normal_end:
1229
1230 msg_nowait = FALSE;
1231
1232 /* Reset finish_op, in case it was set */
1233#ifdef CURSOR_SHAPE
1234 c = finish_op;
1235#endif
1236 finish_op = FALSE;
1237#ifdef CURSOR_SHAPE
1238 /* Redraw the cursor with another shape, if we were in Operator-pending
1239 * mode or did a replace command. */
1240 if (c || ca.cmdchar == 'r')
1241 {
1242 ui_cursor_shape(); /* may show different cursor shape */
1243# ifdef FEAT_MOUSESHAPE
1244 update_mouseshape(-1);
1245# endif
1246 }
1247#endif
1248
1249#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001250 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001251 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 clear_showcmd();
1253#endif
1254
1255 checkpcmark(); /* check if we moved since setting pcmark */
1256 vim_free(ca.searchbuf);
1257
1258#ifdef FEAT_MBYTE
1259 if (has_mbyte)
1260 mb_adjust_cursor();
1261#endif
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (curwin->w_p_scb && toplevel)
1264 {
1265 validate_cursor(); /* may need to update w_leftcol */
1266 do_check_scrollbind(TRUE);
1267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
Bram Moolenaar860cae12010-06-05 23:22:07 +02001269 if (curwin->w_p_crb && toplevel)
1270 {
1271 validate_cursor(); /* may need to update w_leftcol */
1272 do_check_cursorbind();
1273 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001274
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001275#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001276 /* don't go to Insert mode if a terminal has a running job */
1277 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001278 restart_edit = 0;
1279#endif
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 /*
1282 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1283 * if still inside a mapping that started in Visual mode).
1284 * May switch from Visual to Select mode after CTRL-O command.
1285 */
1286 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1288 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 && !(ca.retval & CA_COMMAND_BUSY)
1290 && stuff_empty()
1291 && oap->regname == 0)
1292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (restart_VIsual_select == 1)
1294 {
1295 VIsual_select = TRUE;
1296 showmode();
1297 restart_VIsual_select = 0;
1298 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001299 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 (void)edit(restart_edit, FALSE, 1L);
1301 }
1302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (restart_VIsual_select == 2)
1304 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306 /* Save count before an operator for next time. */
1307 opcount = ca.opcount;
1308}
1309
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001310#ifdef FEAT_EVAL
1311/*
1312 * Set v:count and v:count1 according to "cap".
1313 * Set v:prevcount only when "set_prevcount" is TRUE.
1314 */
1315 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001316set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001317{
1318 long count = cap->count0;
1319
1320 /* multiply with cap->opcount the same way as above */
1321 if (cap->opcount != 0)
1322 count = cap->opcount * (count == 0 ? 1 : count);
1323 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1324 *set_prevcount = FALSE; /* only set v:prevcount once */
1325}
1326#endif
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328/*
Bram Moolenaar5823f842019-01-03 22:58:08 +01001329 * Handle an operator after Visual mode or when the movement is finished.
1330 * "gui_yank" is true when yanking text for the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 */
1332 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001333do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 oparg_T *oap = cap->oap;
1336 pos_T old_cursor;
1337 int empty_region_error;
1338 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001339#ifdef FEAT_LINEBREAK
1340 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 /* The visual area is remembered for redo */
1344 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1345 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001346 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001348 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001349#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#endif
1352
1353#if defined(FEAT_CLIPBOARD)
1354 /*
1355 * Yank the visual area into the GUI selection register before we operate
1356 * on it and lose it forever.
1357 * Don't do it if a specific register was specified, so that ""x"*P works.
1358 * This could call do_pending_operator() recursively, but that's OK
1359 * because gui_yank will be TRUE for the nested call.
1360 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001361 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 && oap->op_type != OP_NOP
1363 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 && VIsual_active
1365 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 && oap->regname == 0)
1367 clip_auto_select();
1368#endif
1369 old_cursor = curwin->w_cursor;
1370
1371 /*
1372 * If an operation is pending, handle it...
1373 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001374 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
Bram Moolenaar5823f842019-01-03 22:58:08 +01001376 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
1377 // for the clipboard.
1378 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
1379
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001380#ifdef FEAT_LINEBREAK
1381 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001382 if (curwin->w_p_lbr)
1383 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001384 curwin->w_p_lbr = FALSE;
1385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 oap->is_VIsual = VIsual_active;
1387 if (oap->motion_force == 'V')
1388 oap->motion_type = MLINE;
1389 else if (oap->motion_force == 'v')
1390 {
1391 /* If the motion was linewise, "inclusive" will not have been set.
1392 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1393 if (oap->motion_type == MLINE)
1394 oap->inclusive = FALSE;
1395 /* If the motion already was characterwise, toggle "inclusive" */
1396 else if (oap->motion_type == MCHAR)
1397 oap->inclusive = !oap->inclusive;
1398 oap->motion_type = MCHAR;
1399 }
1400 else if (oap->motion_force == Ctrl_V)
1401 {
1402 /* Change line- or characterwise motion into Visual block mode. */
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01001403 if (!VIsual_active)
1404 {
1405 VIsual_active = TRUE;
1406 VIsual = oap->start;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 VIsual_mode = Ctrl_V;
1409 VIsual_select = FALSE;
1410 VIsual_reselect = FALSE;
1411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
Bram Moolenaar7afea822013-04-24 18:34:45 +02001413 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1414 /* Never redo "zf" (define fold). */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001415 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001416 && ((!VIsual_active || oap->motion_force)
1417 /* Also redo Operator-pending Visual mode mappings */
1418 || (VIsual_active && cap->cmdchar == ':'
1419 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001420 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421#ifdef FEAT_FOLDING
1422 && oap->op_type != OP_FOLD
1423 && oap->op_type != OP_FOLDOPEN
1424 && oap->op_type != OP_FOLDOPENREC
1425 && oap->op_type != OP_FOLDCLOSE
1426 && oap->op_type != OP_FOLDCLOSEREC
1427 && oap->op_type != OP_FOLDDEL
1428 && oap->op_type != OP_FOLDDELREC
1429#endif
1430 )
1431 {
1432 prep_redo(oap->regname, cap->count0,
1433 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1434 oap->motion_force, cap->cmdchar, cap->nchar);
1435 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1436 {
1437 /*
1438 * If 'cpoptions' does not contain 'r', insert the search
1439 * pattern to really repeat the same command.
1440 */
1441 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001442 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 AppendToRedobuff(NL_STR);
1444 }
1445 else if (cap->cmdchar == ':')
1446 {
1447 /* do_cmdline() has stored the first typed line in
1448 * "repeat_cmdline". When several lines are typed repeating
1449 * won't be possible. */
1450 if (repeat_cmdline == NULL)
1451 ResetRedobuff();
1452 else
1453 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001454 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001456 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
1458 }
1459 }
1460
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 if (redo_VIsual_busy)
1462 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001463 /* Redo of an operation on a Visual area. Use the same size from
1464 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 oap->start = curwin->w_cursor;
1466 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1467 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1468 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1469 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001470 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001472 if (VIsual_mode == 'v')
1473 {
1474 if (redo_VIsual_line_count <= 1)
1475 {
1476 validate_virtcol();
1477 curwin->w_curswant =
1478 curwin->w_virtcol + redo_VIsual_vcol - 1;
1479 }
1480 else
1481 curwin->w_curswant = redo_VIsual_vcol;
1482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001484 {
1485 curwin->w_curswant = MAXCOL;
1486 }
1487 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
1489 cap->count0 = redo_VIsual_count;
1490 if (redo_VIsual_count != 0)
1491 cap->count1 = redo_VIsual_count;
1492 else
1493 cap->count1 = 1;
1494 }
1495 else if (VIsual_active)
1496 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001497 if (!gui_yank)
1498 {
1499 /* Save the current VIsual area for '< and '> marks, and "gv" */
1500 curbuf->b_visual.vi_start = VIsual;
1501 curbuf->b_visual.vi_end = curwin->w_cursor;
1502 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001503 if (VIsual_mode_orig != NUL)
1504 {
1505 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1506 VIsual_mode_orig = NUL;
1507 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001508 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001510 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513
1514 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001515 * characterwise selection.
1516 * Special case: gH<Del> deletes the last line. */
1517 if (VIsual_select && VIsual_mode == 'V'
1518 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001520 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
1522 VIsual.col = 0;
1523 curwin->w_cursor.col =
1524 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1525 }
1526 else
1527 {
1528 curwin->w_cursor.col = 0;
1529 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1530 }
1531 VIsual_mode = 'v';
1532 }
1533 /* If 'selection' is "exclusive", backup one character for
1534 * charwise selections. */
1535 else if (VIsual_mode == 'v')
1536 {
1537# ifdef FEAT_VIRTUALEDIT
1538 include_line_break =
1539# endif
1540 unadjust_for_sel();
1541 }
1542
1543 oap->start = VIsual;
1544 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001547# ifdef FEAT_VIRTUALEDIT
1548 oap->start.coladd = 0;
1549# endif
1550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
1553 /*
1554 * Set oap->start to the first position of the operated text, oap->end
1555 * to the end of the operated text. w_cursor is equal to oap->start.
1556 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001557 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 {
1559#ifdef FEAT_FOLDING
1560 /* Include folded lines completely. */
1561 if (!VIsual_active)
1562 {
1563 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1564 oap->start.col = 0;
1565 if (hasFolding(curwin->w_cursor.lnum, NULL,
1566 &curwin->w_cursor.lnum))
1567 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1568 }
1569#endif
1570 oap->end = curwin->w_cursor;
1571 curwin->w_cursor = oap->start;
1572
1573 /* w_virtcol may have been updated; if the cursor goes back to its
1574 * previous position w_virtcol becomes invalid and isn't updated
1575 * automatically. */
1576 curwin->w_valid &= ~VALID_VIRTCOL;
1577 }
1578 else
1579 {
1580#ifdef FEAT_FOLDING
1581 /* Include folded lines completely. */
1582 if (!VIsual_active && oap->motion_type == MLINE)
1583 {
1584 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1585 NULL))
1586 curwin->w_cursor.col = 0;
1587 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1588 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1589 }
1590#endif
1591 oap->end = oap->start;
1592 oap->start = curwin->w_cursor;
1593 }
1594
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001595 /* Just in case lines were deleted that make the position invalid. */
1596 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1598
1599#ifdef FEAT_VIRTUALEDIT
1600 /* Set "virtual_op" before resetting VIsual_active. */
1601 virtual_op = virtual_active();
1602#endif
1603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (VIsual_active || redo_VIsual_busy)
1605 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001606 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608 if (!redo_VIsual_busy && !gui_yank)
1609 {
1610 /*
1611 * Prepare to reselect and redo Visual: this is based on the
1612 * size of the Visual text
1613 */
1614 resel_VIsual_mode = VIsual_mode;
1615 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001616 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001618 {
1619 if (VIsual_mode != Ctrl_V)
1620 getvvcol(curwin, &(oap->end),
1621 NULL, NULL, &oap->end_vcol);
1622 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1623 {
1624 if (VIsual_mode != Ctrl_V)
1625 getvvcol(curwin, &(oap->start),
1626 &oap->start_vcol, NULL, NULL);
1627 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1628 }
1629 else
1630 resel_VIsual_vcol = oap->end_vcol;
1631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 resel_VIsual_line_count = oap->line_count;
1633 }
1634
1635 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001636 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 && oap->op_type != OP_COLON
1638#ifdef FEAT_FOLDING
1639 && oap->op_type != OP_FOLD
1640 && oap->op_type != OP_FOLDOPEN
1641 && oap->op_type != OP_FOLDOPENREC
1642 && oap->op_type != OP_FOLDCLOSE
1643 && oap->op_type != OP_FOLDCLOSEREC
1644 && oap->op_type != OP_FOLDDEL
1645 && oap->op_type != OP_FOLDDELREC
1646#endif
1647 && oap->motion_force == NUL
1648 )
1649 {
1650 /* Prepare for redoing. Only use the nchar field for "r",
1651 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001652 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1653 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001654 prep_redo(oap->regname, cap->count0,
1655 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1656 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001657 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001658 {
1659 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1660
1661 /* reverse what nv_replace() did */
1662 if (nchar == REPLACE_CR_NCHAR)
1663 nchar = CAR;
1664 else if (nchar == REPLACE_NL_NCHAR)
1665 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001666 prep_redo(oap->regname, 0L, NUL, 'v',
1667 get_op_char(oap->op_type),
1668 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001669 nchar);
1670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (!redo_VIsual_busy)
1672 {
1673 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001674 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 redo_VIsual_line_count = resel_VIsual_line_count;
1676 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001677 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 }
1679 }
1680
1681 /*
1682 * oap->inclusive defaults to TRUE.
1683 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1684 * FALSE. This makes "d}P" and "v}dP" work the same.
1685 */
1686 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1687 oap->inclusive = TRUE;
1688 if (VIsual_mode == 'V')
1689 oap->motion_type = MLINE;
1690 else
1691 {
1692 oap->motion_type = MCHAR;
1693 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001694#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 && (include_line_break || !virtual_op)
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 )
1698 {
1699 oap->inclusive = FALSE;
1700 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001701 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001702 if (*p_sel != 'o'
1703 && !op_on_lines(oap->op_type)
1704 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001706 ++oap->end.lnum;
1707 oap->end.col = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001708#ifdef FEAT_VIRTUALEDIT
Bram Moolenaard009e862015-06-09 20:20:03 +02001709 oap->end.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001710#endif
Bram Moolenaard009e862015-06-09 20:20:03 +02001711 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
1713 }
1714 }
1715
1716 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 /*
1719 * Switch Visual off now, so screen updating does
1720 * not show inverted text when the screen is redrawn.
1721 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1722 * no screen redraw, so it is done here to remove the inverted
1723 * part.
1724 */
1725 if (!gui_yank)
1726 {
1727 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001728#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 setmouse();
1730 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001731#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001732 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if ((oap->op_type == OP_YANK
1734 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001735 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 || oap->op_type == OP_FILTER)
1737 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001738 {
1739#ifdef FEAT_LINEBREAK
1740 /* make sure redrawing is correct */
1741 curwin->w_p_lbr = lbr_saved;
1742#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
1746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748#ifdef FEAT_MBYTE
1749 /* Include the trailing byte of a multi-byte char. */
1750 if (has_mbyte && oap->inclusive)
1751 {
1752 int l;
1753
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001754 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (l > 1)
1756 oap->end.col += l - 1;
1757 }
1758#endif
1759 curwin->w_set_curswant = TRUE;
1760
1761 /*
1762 * oap->empty is set when start and end are the same. The inclusive
1763 * flag affects this too, unless yanking and the end is on a NUL.
1764 */
1765 oap->empty = (oap->motion_type == MCHAR
1766 && (!oap->inclusive
1767 || (oap->op_type == OP_YANK
1768 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001769 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#ifdef FEAT_VIRTUALEDIT
1771 && !(virtual_op && oap->start.coladd != oap->end.coladd)
1772#endif
1773 );
1774 /*
1775 * For delete, change and yank, it's an error to operate on an
1776 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1777 */
1778 empty_region_error = (oap->empty
1779 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 /* Force a redraw when operating on an empty Visual region, when
1782 * 'modifiable is off or creating a fold. */
1783 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001784#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001788 {
1789#ifdef FEAT_LINEBREAK
1790 curwin->w_p_lbr = lbr_saved;
1791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 /*
1796 * If the end of an operator is in column one while oap->motion_type
1797 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1798 * character in the previous line. If op_start is on or before the
1799 * first non-blank in the line, the operator becomes linewise
1800 * (strange, but that's the way vi does it).
1801 */
1802 if ( oap->motion_type == MCHAR
1803 && oap->inclusive == FALSE
1804 && !(cap->retval & CA_NO_ADJ_OP_END)
1805 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001807 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 && oap->line_count > 1)
1809 {
1810 oap->end_adjusted = TRUE; /* remember that we did this */
1811 --oap->line_count;
1812 --oap->end.lnum;
1813 if (inindent(0))
1814 oap->motion_type = MLINE;
1815 else
1816 {
1817 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1818 if (oap->end.col)
1819 {
1820 --oap->end.col;
1821 oap->inclusive = TRUE;
1822 }
1823 }
1824 }
1825 else
1826 oap->end_adjusted = FALSE;
1827
1828 switch (oap->op_type)
1829 {
1830 case OP_LSHIFT:
1831 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001832 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 auto_format(FALSE, TRUE);
1834 break;
1835
1836 case OP_JOIN_NS:
1837 case OP_JOIN:
1838 if (oap->line_count < 2)
1839 oap->line_count = 2;
1840 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1841 curbuf->b_ml.ml_line_count)
1842 beep_flush();
1843 else
1844 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001845 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001846 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 auto_format(FALSE, TRUE);
1848 }
1849 break;
1850
1851 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001854 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001855 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001856 CancelRedo();
1857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 else
1859 {
1860 (void)op_delete(oap);
1861 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1862 u_save_cursor(); /* cursor line wasn't saved yet */
1863 auto_format(FALSE, TRUE);
1864 }
1865 break;
1866
1867 case OP_YANK:
1868 if (empty_region_error)
1869 {
1870 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001871 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001872 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001873 CancelRedo();
1874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001877 {
1878#ifdef FEAT_LINEBREAK
1879 curwin->w_p_lbr = lbr_saved;
1880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 check_cursor_col();
1884 break;
1885
1886 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001889 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001890 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001891 CancelRedo();
1892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 else
1894 {
1895 /* This is a new edit command, not a restart. Need to
1896 * remember it to make 'insertmode' work with mappings for
1897 * Visual mode. But do this only once and not when typed and
1898 * 'insertmode' isn't set. */
1899 if (p_im || !KeyTyped)
1900 restart_edit_save = restart_edit;
1901 else
1902 restart_edit_save = 0;
1903 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001904#ifdef FEAT_LINEBREAK
1905 /* Restore linebreak, so that when the user edits it looks as
1906 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001907 if (curwin->w_p_lbr != lbr_saved)
1908 {
1909 curwin->w_p_lbr = lbr_saved;
1910 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1911 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 /* Reset finish_op now, don't want it set inside edit(). */
1914 finish_op = FALSE;
1915 if (op_change(oap)) /* will call edit() */
1916 cap->retval |= CA_COMMAND_BUSY;
1917 if (restart_edit == 0)
1918 restart_edit = restart_edit_save;
1919 }
1920 break;
1921
1922 case OP_FILTER:
1923 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1924 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1925 else
1926 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001927 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
1929 case OP_INDENT:
1930 case OP_COLON:
1931
1932#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1933 /*
1934 * If 'equalprg' is empty, do the indenting internally.
1935 */
1936 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1937 {
1938# ifdef FEAT_LISP
1939 if (curbuf->b_p_lisp)
1940 {
1941 op_reindent(oap, get_lisp_indent);
1942 break;
1943 }
1944# endif
1945# ifdef FEAT_CINDENT
1946 op_reindent(oap,
1947# ifdef FEAT_EVAL
1948 *curbuf->b_p_inde != NUL ? get_expr_indent :
1949# endif
1950 get_c_indent);
1951 break;
1952# endif
1953 }
1954#endif
1955
1956 op_colon(oap);
1957 break;
1958
1959 case OP_TILDE:
1960 case OP_UPPER:
1961 case OP_LOWER:
1962 case OP_ROT13:
1963 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001964 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001965 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001966 CancelRedo();
1967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 else
1969 op_tilde(oap);
1970 check_cursor_col();
1971 break;
1972
1973 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001974#if defined(FEAT_EVAL)
1975 if (*curbuf->b_p_fex != NUL)
1976 op_formatexpr(oap); /* use expression */
1977 else
1978#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001979 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 op_colon(oap); /* use external command */
1981 else
1982 op_format(oap, FALSE); /* use internal function */
1983 break;
1984
1985 case OP_FORMAT2:
1986 op_format(oap, TRUE); /* use internal function */
1987 break;
1988
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001989 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001990#ifdef FEAT_LINEBREAK
1991 /* Restore linebreak, so that when the user edits it looks as
1992 * before. */
1993 curwin->w_p_lbr = lbr_saved;
1994#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001995 op_function(oap); /* call 'operatorfunc' */
1996 break;
1997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 case OP_INSERT:
1999 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#ifdef FEAT_VISUALEXTRA
2002 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002003 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002004 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002005 CancelRedo();
2006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 else
2008 {
2009 /* This is a new edit command, not a restart. Need to
2010 * remember it to make 'insertmode' work with mappings for
2011 * Visual mode. But do this only once. */
2012 restart_edit_save = restart_edit;
2013 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002014#ifdef FEAT_LINEBREAK
2015 /* Restore linebreak, so that when the user edits it looks as
2016 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002017 if (curwin->w_p_lbr != lbr_saved)
2018 {
2019 curwin->w_p_lbr = lbr_saved;
2020 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2021 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002024#ifdef FEAT_LINEBREAK
2025 /* Reset linebreak, so that formatting works correctly. */
2026 curwin->w_p_lbr = FALSE;
2027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 /* TODO: when inserting in several lines, should format all
2030 * the lines. */
2031 auto_format(FALSE, TRUE);
2032
2033 if (restart_edit == 0)
2034 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002035 else
2036 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02002039 vim_beep(BO_OPER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040#endif
2041 break;
2042
2043 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#ifdef FEAT_VISUALEXTRA
2046 if (empty_region_error)
2047#endif
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002048 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002049 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002050 CancelRedo();
2051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#ifdef FEAT_VISUALEXTRA
2053 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002054 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002055# ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002056 /* Restore linebreak, so that when the user edits it looks as
2057 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002058 if (curwin->w_p_lbr != lbr_saved)
2059 {
2060 curwin->w_p_lbr = lbr_saved;
2061 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2062 }
2063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066#endif
2067 break;
2068
2069#ifdef FEAT_FOLDING
2070 case OP_FOLD:
2071 VIsual_reselect = FALSE; /* don't reselect now */
2072 foldCreate(oap->start.lnum, oap->end.lnum);
2073 break;
2074
2075 case OP_FOLDOPEN:
2076 case OP_FOLDOPENREC:
2077 case OP_FOLDCLOSE:
2078 case OP_FOLDCLOSEREC:
2079 VIsual_reselect = FALSE; /* don't reselect now */
2080 opFoldRange(oap->start.lnum, oap->end.lnum,
2081 oap->op_type == OP_FOLDOPEN
2082 || oap->op_type == OP_FOLDOPENREC,
2083 oap->op_type == OP_FOLDOPENREC
2084 || oap->op_type == OP_FOLDCLOSEREC,
2085 oap->is_VIsual);
2086 break;
2087
2088 case OP_FOLDDEL:
2089 case OP_FOLDDELREC:
2090 VIsual_reselect = FALSE; /* don't reselect now */
2091 deleteFold(oap->start.lnum, oap->end.lnum,
2092 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2093 break;
2094#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002095 case OP_NR_ADD:
2096 case OP_NR_SUB:
2097 if (empty_region_error)
2098 {
2099 vim_beep(BO_OPER);
2100 CancelRedo();
2101 }
2102 else
2103 {
2104 VIsual_active = TRUE;
2105#ifdef FEAT_LINEBREAK
2106 curwin->w_p_lbr = lbr_saved;
2107#endif
2108 op_addsub(oap, cap->count1, redo_VIsual_arg);
2109 VIsual_active = FALSE;
2110 }
2111 check_cursor_col();
2112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 default:
2114 clearopbeep(oap);
2115 }
2116#ifdef FEAT_VIRTUALEDIT
2117 virtual_op = MAYBE;
2118#endif
2119 if (!gui_yank)
2120 {
2121 /*
2122 * if 'sol' not set, go back to old column for some commands
2123 */
2124 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2125 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2126 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002127 {
2128#ifdef FEAT_LINEBREAK
2129 curwin->w_p_lbr = FALSE;
2130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 }
2134 else
2135 {
2136 curwin->w_cursor = old_cursor;
2137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 clearop(oap);
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01002140 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002142#ifdef FEAT_LINEBREAK
2143 curwin->w_p_lbr = lbr_saved;
2144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145}
2146
2147/*
2148 * Handle indent and format operators and visual mode ":".
2149 */
2150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002151op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 if (oap->is_VIsual)
2155 stuffReadbuff((char_u *)"'<,'>");
2156 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 /*
2159 * Make the range look nice, so it can be repeated.
2160 */
2161 if (oap->start.lnum == curwin->w_cursor.lnum)
2162 stuffcharReadbuff('.');
2163 else
2164 stuffnumReadbuff((long)oap->start.lnum);
2165 if (oap->end.lnum != oap->start.lnum)
2166 {
2167 stuffcharReadbuff(',');
2168 if (oap->end.lnum == curwin->w_cursor.lnum)
2169 stuffcharReadbuff('.');
2170 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2171 stuffcharReadbuff('$');
2172 else if (oap->start.lnum == curwin->w_cursor.lnum)
2173 {
2174 stuffReadbuff((char_u *)".+");
2175 stuffnumReadbuff((long)oap->line_count - 1);
2176 }
2177 else
2178 stuffnumReadbuff((long)oap->end.lnum);
2179 }
2180 }
2181 if (oap->op_type != OP_COLON)
2182 stuffReadbuff((char_u *)"!");
2183 if (oap->op_type == OP_INDENT)
2184 {
2185#ifndef FEAT_CINDENT
2186 if (*get_equalprg() == NUL)
2187 stuffReadbuff((char_u *)"indent");
2188 else
2189#endif
2190 stuffReadbuff(get_equalprg());
2191 stuffReadbuff((char_u *)"\n");
2192 }
2193 else if (oap->op_type == OP_FORMAT)
2194 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002195 if (*curbuf->b_p_fp != NUL)
2196 stuffReadbuff(curbuf->b_p_fp);
2197 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002199 else
2200 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002201 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 }
2203
2204 /*
2205 * do_cmdline() does the rest
2206 */
2207}
2208
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002209/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002210 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002211 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002213op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002214{
2215#ifdef FEAT_EVAL
Bram Moolenaarffa96842018-06-12 22:05:14 +02002216 typval_T argv[2];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002217# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002218 int save_virtual_op = virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002219# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002220
2221 if (*p_opfunc == NUL)
2222 EMSG(_("E774: 'operatorfunc' is empty"));
2223 else
2224 {
2225 /* Set '[ and '] marks to text to be operated on. */
2226 curbuf->b_op_start = oap->start;
2227 curbuf->b_op_end = oap->end;
2228 if (oap->motion_type != MLINE && !oap->inclusive)
2229 /* Exclude the end position. */
2230 decl(&curbuf->b_op_end);
2231
Bram Moolenaarffa96842018-06-12 22:05:14 +02002232 argv[0].v_type = VAR_STRING;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002233 if (oap->block_mode)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002234 argv[0].vval.v_string = (char_u *)"block";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002235 else if (oap->motion_type == MLINE)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002236 argv[0].vval.v_string = (char_u *)"line";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002237 else
Bram Moolenaarffa96842018-06-12 22:05:14 +02002238 argv[0].vval.v_string = (char_u *)"char";
2239 argv[1].v_type = VAR_UNKNOWN;
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002240
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002241# ifdef FEAT_VIRTUALEDIT
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002242 /* Reset virtual_op so that 'virtualedit' can be changed in the
2243 * function. */
2244 virtual_op = MAYBE;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002245# endif
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002246
Bram Moolenaarded27a12018-08-01 19:06:03 +02002247 (void)call_func_retnr(p_opfunc, 1, argv);
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 virtual_op = save_virtual_op;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01002251# endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002252 }
2253#else
2254 EMSG(_("E775: Eval feature not available"));
2255#endif
2256}
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258#if defined(FEAT_MOUSE) || defined(PROTO)
2259/*
2260 * Do the appropriate action for the current mouse click in the current mode.
2261 * Not used for Command-line mode.
2262 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002263 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 * event modi- position visual change action
2265 * fier cursor window
2266 * left press - yes end yes
2267 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002268 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 * left drag - yes start if moved no
2270 * left relse - yes start if moved no
2271 * middle press - yes if not active no put register
2272 * middle press - yes if active no yank and put
2273 * right press - yes start or extend yes
2274 * right press S yes no change yes "#" (2)
2275 * right drag - yes extend no
2276 * right relse - yes extend no
2277 *
2278 * Insert or Replace Mode:
2279 * event modi- position visual change action
2280 * fier cursor window
2281 * left press - yes (cannot be active) yes
2282 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2283 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2284 * left drag - yes start or extend (1) no CTRL-O (1)
2285 * left relse - yes start or extend (1) no CTRL-O (1)
2286 * middle press - no (cannot be active) no put register
2287 * right press - yes start or extend yes CTRL-O
2288 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2289 *
2290 * (1) only if mouse pointer moved since press
2291 * (2) only if click is in same buffer
2292 *
2293 * Return TRUE if start_arrow() should be called for edit mode.
2294 */
2295 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002296do_mouse(
2297 oparg_T *oap, /* operator argument, can be NULL */
2298 int c, /* K_LEFTMOUSE, etc */
2299 int dir, /* Direction to 'put' if necessary */
2300 long count,
2301 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302{
2303 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2304 static int got_click = FALSE; /* got a click some time back */
2305
2306 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2307 int is_click; /* If FALSE it's a drag or release event */
2308 int is_drag; /* If TRUE it's a drag event */
2309 int jump_flags = 0; /* flags for jump_to_mouse() */
2310 pos_T start_visual;
2311 int moved; /* Has cursor moved? */
2312 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002313 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 int c1, c2;
2316#if defined(FEAT_FOLDING)
2317 pos_T save_cursor;
2318#endif
2319 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 static pos_T orig_cursor;
2321 colnr_T leftcol, rightcol;
2322 pos_T end_visual;
2323 int diff;
2324 int old_active = VIsual_active;
2325 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 int regname;
2327
2328#if defined(FEAT_FOLDING)
2329 save_cursor = curwin->w_cursor;
2330#endif
2331
2332 /*
2333 * When GUI is active, always recognize mouse events, otherwise:
2334 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2335 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2336 * - For command line and insert mode 'mouse' is checked before calling
2337 * do_mouse().
2338 */
2339 if (do_always)
2340 do_always = FALSE;
2341 else
2342#ifdef FEAT_GUI
2343 if (!gui.in_use)
2344#endif
2345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (VIsual_active)
2347 {
2348 if (!mouse_has(MOUSE_VISUAL))
2349 return FALSE;
2350 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002351 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 return FALSE;
2353 }
2354
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002355 for (;;)
2356 {
2357 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2358 if (is_drag)
2359 {
2360 /* If the next character is the same mouse event then use that
2361 * one. Speeds up dragging the status line. */
2362 if (vpeekc() != NUL)
2363 {
2364 int nc;
2365 int save_mouse_row = mouse_row;
2366 int save_mouse_col = mouse_col;
2367
2368 /* Need to get the character, peeking doesn't get the actual
2369 * one. */
2370 nc = safe_vgetc();
2371 if (c == nc)
2372 continue;
2373 vungetc(nc);
2374 mouse_row = save_mouse_row;
2375 mouse_col = save_mouse_col;
2376 }
2377 }
2378 break;
2379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002381 if (c == K_MOUSEMOVE)
2382 {
2383 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002384#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002385 ui_may_remove_balloon();
2386 if (p_bevalterm && !VIsual_active)
2387 {
2388 profile_setlimit(p_bdlay, &bevalexpr_due);
2389 bevalexpr_due_set = TRUE;
2390 }
2391#endif
2392 return FALSE;
2393 }
2394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395#ifdef FEAT_MOUSESHAPE
2396 /* May have stopped dragging the status or separator line. The pointer is
2397 * most likely still on the status or separator line. */
2398 if (!is_drag && drag_status_line)
2399 {
2400 drag_status_line = FALSE;
2401 update_mouseshape(SHAPE_IDX_STATUS);
2402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (!is_drag && drag_sep_line)
2404 {
2405 drag_sep_line = FALSE;
2406 update_mouseshape(SHAPE_IDX_VSEP);
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#endif
2409
2410 /*
2411 * Ignore drag and release events if we didn't get a click.
2412 */
2413 if (is_click)
2414 got_click = TRUE;
2415 else
2416 {
2417 if (!got_click) /* didn't get click, ignore */
2418 return FALSE;
2419 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002422 if (in_tab_line)
2423 {
2424 in_tab_line = FALSE;
2425 return FALSE;
2426 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 /*
2431 * CTRL right mouse button does CTRL-T
2432 */
2433 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2434 {
2435 if (State & INSERT)
2436 stuffcharReadbuff(Ctrl_O);
2437 if (count > 1)
2438 stuffnumReadbuff(count);
2439 stuffcharReadbuff(Ctrl_T);
2440 got_click = FALSE; /* ignore drag&release now */
2441 return FALSE;
2442 }
2443
2444 /*
2445 * CTRL only works with left mouse button
2446 */
2447 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2448 return FALSE;
2449
2450 /*
2451 * When a modifier is down, ignore drag and release events, as well as
2452 * multiple clicks and the middle mouse button.
2453 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2454 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002455 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2456 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 && (!is_click
2458 || (mod_mask & MOD_MASK_MULTI_CLICK)
2459 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002460 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 && mouse_model_popup()
2462 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002463 && !((mod_mask & MOD_MASK_ALT)
2464 && !mouse_model_popup()
2465 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 )
2467 return FALSE;
2468
2469 /*
2470 * If the button press was used as the movement command for an operator
2471 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2472 * drag/release events.
2473 */
2474 if (!is_click && which_button == MOUSE_MIDDLE)
2475 return FALSE;
2476
2477 if (oap != NULL)
2478 regname = oap->regname;
2479 else
2480 regname = 0;
2481
2482 /*
2483 * Middle mouse button does a 'put' of the selected text
2484 */
2485 if (which_button == MOUSE_MIDDLE)
2486 {
2487 if (State == NORMAL)
2488 {
2489 /*
2490 * If an operator was pending, we don't know what the user wanted
2491 * to do. Go back to normal mode: Clear the operator and beep().
2492 */
2493 if (oap != NULL && oap->op_type != OP_NOP)
2494 {
2495 clearopbeep(oap);
2496 return FALSE;
2497 }
2498
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 /*
2500 * If visual was active, yank the highlighted text and put it
2501 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002502 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
2504 if (VIsual_active)
2505 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002506 if (VIsual_select)
2507 {
2508 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002509 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002510 }
2511 else
2512 {
2513 stuffcharReadbuff('y');
2514 stuffcharReadbuff(K_MIDDLEMOUSE);
2515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 do_always = TRUE; /* ignore 'mouse' setting next time */
2517 return FALSE;
2518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 /*
2520 * The rest is below jump_to_mouse()
2521 */
2522 }
2523
2524 else if ((State & INSERT) == 0)
2525 return FALSE;
2526
2527 /*
2528 * Middle click in insert mode doesn't move the mouse, just insert the
2529 * contents of a register. '.' register is special, can't insert that
2530 * with do_put().
2531 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2532 * happens for the GUI).
2533 */
2534 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2535 {
2536 if (regname == '.')
2537 insert_reg(regname, TRUE);
2538 else
2539 {
2540#ifdef FEAT_CLIPBOARD
2541 if (clip_star.available && regname == 0)
2542 regname = '*';
2543#endif
2544 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2545 insert_reg(regname, TRUE);
2546 else
2547 {
2548 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2549
2550 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2551 AppendCharToRedobuff(Ctrl_R);
2552 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2553 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2554 }
2555 }
2556 return FALSE;
2557 }
2558 }
2559
2560 /* When dragging or button-up stay in the same window. */
2561 if (!is_click)
2562 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2563
2564 start_visual.lnum = 0;
2565
Bram Moolenaarf740b292006-02-16 22:11:02 +00002566 /* Check for clicking in the tab page line. */
2567 if (mouse_row == 0 && firstwin->w_winrow > 0)
2568 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002569 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002570 {
2571 if (in_tab_line)
2572 {
2573 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002574 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2575 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002576 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002577 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002578 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002579
Bram Moolenaarf740b292006-02-16 22:11:02 +00002580 /* click in a tab selects that tab page */
2581 if (is_click
2582# ifdef FEAT_CMDWIN
2583 && cmdwin_type == 0
2584# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002585 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002586 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002587 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002588 c1 = TabPageIdxs[mouse_col];
2589 if (c1 >= 0)
2590 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002591 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2592 {
2593 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002594 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002595 tabpage_new();
2596 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2597 }
2598 else
2599 {
2600 /* Go to specified tab page, or next one if not clicking
2601 * on a label. */
2602 goto_tabpage(c1);
2603
2604 /* It's like clicking on the status line of a window. */
2605 if (curwin != old_curwin)
2606 end_visual_mode();
2607 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002608 }
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002609 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002610 {
2611 tabpage_T *tp;
2612
2613 /* Close the current or specified tab page. */
2614 if (c1 == -999)
2615 tp = curtab;
2616 else
2617 tp = find_tabpage(-c1);
2618 if (tp == curtab)
2619 {
2620 if (first_tabpage->tp_next != NULL)
2621 tabpage_close(FALSE);
2622 }
2623 else if (tp != NULL)
2624 tabpage_close_other(tp, FALSE);
2625 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002626 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002627 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002628 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002629 else if (is_drag && in_tab_line)
2630 {
2631 c1 = TabPageIdxs[mouse_col];
2632 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2633 return FALSE;
2634 }
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 /*
2637 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2638 * right button up -> pop-up menu
2639 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002640 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 */
2642 if (mouse_model_popup())
2643 {
2644 if (which_button == MOUSE_RIGHT
2645 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2646 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002647#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002649 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2650 || defined(FEAT_TERM_POPUP_MENU)
2651# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (gui.in_use)
2653 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002654# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2655 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2656 if (!is_click)
2657 /* Ignore right button release events, only shows the popup
2658 * menu on the button down event. */
2659 return FALSE;
2660# endif
2661# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2662 if (is_click || is_drag)
2663 /* Ignore right button down and drag mouse events. Windows
2664 * only shows the popup menu on the button up event. */
2665 return FALSE;
2666# endif
2667 }
2668# endif
2669# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2670 else
2671# endif
2672# if defined(FEAT_TERM_POPUP_MENU)
2673 if (!is_click)
2674 /* Ignore right button release events, only shows the popup
2675 * menu on the button down event. */
2676 return FALSE;
2677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002679 jump_flags = 0;
2680 if (STRCMP(p_mousem, "popup_setpos") == 0)
2681 {
2682 /* First set the cursor position before showing the popup
2683 * menu. */
2684 if (VIsual_active)
2685 {
2686 pos_T m_pos;
2687
2688 /*
2689 * set MOUSE_MAY_STOP_VIS if we are outside the
2690 * selection or the current window (might have false
2691 * negative here)
2692 */
2693 if (mouse_row < curwin->w_winrow
2694 || mouse_row
2695 > (curwin->w_winrow + curwin->w_height))
2696 jump_flags = MOUSE_MAY_STOP_VIS;
2697 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2698 jump_flags = MOUSE_MAY_STOP_VIS;
2699 else
2700 {
2701 if ((LT_POS(curwin->w_cursor, VIsual)
2702 && (LT_POS(m_pos, curwin->w_cursor)
2703 || LT_POS(VIsual, m_pos)))
2704 || (LT_POS(VIsual, curwin->w_cursor)
2705 && (LT_POS(m_pos, VIsual)
2706 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002708 jump_flags = MOUSE_MAY_STOP_VIS;
2709 }
2710 else if (VIsual_mode == Ctrl_V)
2711 {
2712 getvcols(curwin, &curwin->w_cursor, &VIsual,
2713 &leftcol, &rightcol);
2714 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2715 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
2718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002720 else
2721 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002723 if (jump_flags)
2724 {
2725 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2726 update_curbuf(VIsual_active ? INVERTED : VALID);
2727 setcursor();
2728 out_flush(); /* Update before showing popup menu */
2729 }
2730# ifdef FEAT_MENU
2731 show_popupmenu();
2732 got_click = FALSE; /* ignore release events */
2733# endif
2734 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735#else
2736 return FALSE;
2737#endif
2738 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002739 if (which_button == MOUSE_LEFT
2740 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
2742 which_button = MOUSE_RIGHT;
2743 mod_mask &= ~MOD_MASK_SHIFT;
2744 }
2745 }
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if ((State & (NORMAL | INSERT))
2748 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2749 {
2750 if (which_button == MOUSE_LEFT)
2751 {
2752 if (is_click)
2753 {
2754 /* stop Visual mode for a left click in a window, but not when
2755 * on a status line */
2756 if (VIsual_active)
2757 jump_flags |= MOUSE_MAY_STOP_VIS;
2758 }
2759 else if (mouse_has(MOUSE_VISUAL))
2760 jump_flags |= MOUSE_MAY_VIS;
2761 }
2762 else if (which_button == MOUSE_RIGHT)
2763 {
2764 if (is_click && VIsual_active)
2765 {
2766 /*
2767 * Remember the start and end of visual before moving the
2768 * cursor.
2769 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002770 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
2772 start_visual = curwin->w_cursor;
2773 end_visual = VIsual;
2774 }
2775 else
2776 {
2777 start_visual = VIsual;
2778 end_visual = curwin->w_cursor;
2779 }
2780 }
2781 jump_flags |= MOUSE_FOCUS;
2782 if (mouse_has(MOUSE_VISUAL))
2783 jump_flags |= MOUSE_MAY_VIS;
2784 }
2785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 /*
2788 * If an operator is pending, ignore all drags and releases until the
2789 * next mouse click.
2790 */
2791 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2792 {
2793 got_click = FALSE;
2794 oap->motion_type = MCHAR;
2795 }
2796
2797 /* When releasing the button let jump_to_mouse() know. */
2798 if (!is_click && !is_drag)
2799 jump_flags |= MOUSE_RELEASED;
2800
2801 /*
2802 * JUMP!
2803 */
2804 jump_flags = jump_to_mouse(jump_flags,
2805 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002806
2807#ifdef FEAT_MENU
2808 /* A click in the window toolbar has no side effects. */
2809 if (jump_flags & MOUSE_WINBAR)
2810 return FALSE;
2811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 moved = (jump_flags & CURSOR_MOVED);
2813 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
2816#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002817 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2819 {
2820 int key = KEY2TERMCAP1(c);
2821
2822 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2823 || key == (int)KE_RIGHTRELEASE)
2824 netbeans_button_release(which_button);
2825 }
2826#endif
2827
2828 /* When jumping to another window, clear a pending operator. That's a bit
2829 * friendlier than beeping and not jumping to that window. */
2830 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2831 clearop(oap);
2832
2833#ifdef FEAT_FOLDING
2834 if (mod_mask == 0
2835 && !is_drag
2836 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2837 && which_button == MOUSE_LEFT)
2838 {
2839 /* open or close a fold at this line */
2840 if (jump_flags & MOUSE_FOLD_OPEN)
2841 openFold(curwin->w_cursor.lnum, 1L);
2842 else
2843 closeFold(curwin->w_cursor.lnum, 1L);
2844 /* don't move the cursor if still in the same window */
2845 if (curwin == old_curwin)
2846 curwin->w_cursor = save_cursor;
2847 }
2848#endif
2849
2850#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2851 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2852 {
2853 clip_modeless(which_button, is_click, is_drag);
2854 return FALSE;
2855 }
2856#endif
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002859 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 if (VIsual_active && is_drag && p_so)
2861 {
2862 /* In the very first line, allow scrolling one line */
2863 if (mouse_row == 0)
2864 mouse_dragging = 2;
2865 else
2866 mouse_dragging = 1;
2867 }
2868
2869 /* When dragging the mouse above the window, scroll down. */
2870 if (is_drag && mouse_row < 0 && !in_status_line)
2871 {
2872 scroll_redraw(FALSE, 1L);
2873 mouse_row = 0;
2874 }
2875
2876 if (start_visual.lnum) /* right click in visual mode */
2877 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002878 /* When ALT is pressed make Visual mode blockwise. */
2879 if (mod_mask & MOD_MASK_ALT)
2880 VIsual_mode = Ctrl_V;
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 /*
2883 * In Visual-block mode, divide the area in four, pick up the corner
2884 * that is in the quarter that the cursor is in.
2885 */
2886 if (VIsual_mode == Ctrl_V)
2887 {
2888 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2889 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2890 end_visual.col = leftcol;
2891 else
2892 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002893 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 end_visual.lnum = start_visual.lnum;
2896
2897 /* move VIsual to the right column */
2898 start_visual = curwin->w_cursor; /* save the cursor pos */
2899 curwin->w_cursor = end_visual;
2900 coladvance(end_visual.col);
2901 VIsual = curwin->w_cursor;
2902 curwin->w_cursor = start_visual; /* restore the cursor */
2903 }
2904 else
2905 {
2906 /*
2907 * If the click is before the start of visual, change the start.
2908 * If the click is after the end of visual, change the end. If
2909 * the click is inside the visual, change the closest side.
2910 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002911 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002913 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 VIsual = start_visual;
2915 else
2916 {
2917 /* In the same line, compare column number */
2918 if (end_visual.lnum == start_visual.lnum)
2919 {
2920 if (curwin->w_cursor.col - start_visual.col >
2921 end_visual.col - curwin->w_cursor.col)
2922 VIsual = start_visual;
2923 else
2924 VIsual = end_visual;
2925 }
2926
2927 /* In different lines, compare line number */
2928 else
2929 {
2930 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2931 (end_visual.lnum - curwin->w_cursor.lnum);
2932
2933 if (diff > 0) /* closest to end */
2934 VIsual = start_visual;
2935 else if (diff < 0) /* closest to start */
2936 VIsual = end_visual;
2937 else /* in the middle line */
2938 {
2939 if (curwin->w_cursor.col <
2940 (start_visual.col + end_visual.col) / 2)
2941 VIsual = end_visual;
2942 else
2943 VIsual = start_visual;
2944 }
2945 }
2946 }
2947 }
2948 }
2949 /*
2950 * If Visual mode started in insert mode, execute "CTRL-O"
2951 */
2952 else if ((State & INSERT) && VIsual_active)
2953 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
2955 /*
2956 * Middle mouse click: Put text before cursor.
2957 */
2958 if (which_button == MOUSE_MIDDLE)
2959 {
2960#ifdef FEAT_CLIPBOARD
2961 if (clip_star.available && regname == 0)
2962 regname = '*';
2963#endif
2964 if (yank_register_mline(regname))
2965 {
2966 if (mouse_past_bottom)
2967 dir = FORWARD;
2968 }
2969 else if (mouse_past_eol)
2970 dir = FORWARD;
2971
2972 if (fixindent)
2973 {
2974 c1 = (dir == BACKWARD) ? '[' : ']';
2975 c2 = 'p';
2976 }
2977 else
2978 {
2979 c1 = (dir == FORWARD) ? 'p' : 'P';
2980 c2 = NUL;
2981 }
2982 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2983
2984 /*
2985 * Remember where the paste started, so in edit() Insstart can be set
2986 * to this position
2987 */
2988 if (restart_edit != 0)
2989 where_paste_started = curwin->w_cursor;
2990 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2991 }
2992
Bram Moolenaar4033c552017-09-16 20:54:51 +02002993#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 /*
2995 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2996 * error under the mouse pointer.
2997 */
2998 else if (((mod_mask & MOD_MASK_CTRL)
2999 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3000 && bt_quickfix(curbuf))
3001 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003002 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003003 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003004 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01003005 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 got_click = FALSE; /* ignore drag&release now */
3007 }
3008#endif
3009
3010 /*
3011 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
3012 * under the mouse pointer.
3013 */
3014 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
3015 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
3016 {
3017 if (State & INSERT)
3018 stuffcharReadbuff(Ctrl_O);
3019 stuffcharReadbuff(Ctrl_RSB);
3020 got_click = FALSE; /* ignore drag&release now */
3021 }
3022
3023 /*
3024 * Shift-Mouse click searches for the next occurrence of the word under
3025 * the mouse pointer
3026 */
3027 else if ((mod_mask & MOD_MASK_SHIFT))
3028 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003029 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 stuffcharReadbuff(Ctrl_O);
3031 if (which_button == MOUSE_LEFT)
3032 stuffcharReadbuff('*');
3033 else /* MOUSE_RIGHT */
3034 stuffcharReadbuff('#');
3035 }
3036
3037 /* Handle double clicks, unless on status line */
3038 else if (in_status_line)
3039 {
3040#ifdef FEAT_MOUSESHAPE
3041 if ((is_drag || is_click) && !drag_status_line)
3042 {
3043 drag_status_line = TRUE;
3044 update_mouseshape(-1);
3045 }
3046#endif
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 else if (in_sep_line)
3049 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003050#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 if ((is_drag || is_click) && !drag_sep_line)
3052 {
3053 drag_sep_line = TRUE;
3054 update_mouseshape(-1);
3055 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003056#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3059 && mouse_has(MOUSE_VISUAL))
3060 {
3061 if (is_click || !VIsual_active)
3062 {
3063 if (VIsual_active)
3064 orig_cursor = VIsual;
3065 else
3066 {
3067 check_visual_highlight();
3068 VIsual = curwin->w_cursor;
3069 orig_cursor = VIsual;
3070 VIsual_active = TRUE;
3071 VIsual_reselect = TRUE;
3072 /* start Select mode if 'selectmode' contains "mouse" */
3073 may_start_select('o');
3074 setmouse();
3075 }
3076 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003077 {
3078 /* Double click with ALT pressed makes it blockwise. */
3079 if (mod_mask & MOD_MASK_ALT)
3080 VIsual_mode = Ctrl_V;
3081 else
3082 VIsual_mode = 'v';
3083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3085 VIsual_mode = 'V';
3086 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3087 VIsual_mode = Ctrl_V;
3088#ifdef FEAT_CLIPBOARD
3089 /* Make sure the clipboard gets updated. Needed because start and
3090 * end may still be the same, and the selection needs to be owned */
3091 clip_star.vmode = NUL;
3092#endif
3093 }
3094 /*
3095 * A double click selects a word or a block.
3096 */
3097 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3098 {
3099 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003100 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
3102 if (is_click)
3103 {
3104 /* If the character under the cursor (skipping white space) is
3105 * not a word character, try finding a match and select a (),
3106 * {}, [], #if/#endif, etc. block. */
3107 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003108 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 inc(&end_visual);
3110 if (oap != NULL)
3111 oap->motion_type = MCHAR;
3112 if (oap != NULL
3113 && VIsual_mode == 'v'
3114 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003115 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 && (pos = findmatch(oap, NUL)) != NULL)
3117 {
3118 curwin->w_cursor = *pos;
3119 if (oap->motion_type == MLINE)
3120 VIsual_mode = 'V';
3121 else if (*p_sel == 'e')
3122 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003123 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 ++VIsual.col;
3125 else
3126 ++curwin->w_cursor.col;
3127 }
3128 }
3129 }
3130
3131 if (pos == NULL && (is_click || is_drag))
3132 {
3133 /* When not found a match or when dragging: extend to include
3134 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003135 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
3137 find_start_of_word(&curwin->w_cursor);
3138 find_end_of_word(&VIsual);
3139 }
3140 else
3141 {
3142 find_start_of_word(&VIsual);
3143 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
3144#ifdef FEAT_MBYTE
3145 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003146 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#else
3148 ++curwin->w_cursor.col;
3149#endif
3150 find_end_of_word(&curwin->w_cursor);
3151 }
3152 }
3153 curwin->w_set_curswant = TRUE;
3154 }
3155 if (is_click)
3156 redraw_curbuf_later(INVERTED); /* update the inversion */
3157 }
3158 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003159 {
3160 if (mod_mask & MOD_MASK_ALT)
3161 VIsual_mode = Ctrl_V;
3162 else
3163 VIsual_mode = 'v';
3164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
3166 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003167 if ((!VIsual_active && old_active && mode_displayed)
3168 || (VIsual_active && p_smd && msg_silent == 0
3169 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
3172 return moved;
3173}
3174
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175/*
3176 * Move "pos" back to the start of the word it's in.
3177 */
3178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003179find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 char_u *line;
3182 int cclass;
3183 int col;
3184
3185 line = ml_get(pos->lnum);
3186 cclass = get_mouse_class(line + pos->col);
3187
3188 while (pos->col > 0)
3189 {
3190 col = pos->col - 1;
3191#ifdef FEAT_MBYTE
3192 col -= (*mb_head_off)(line, line + col);
3193#endif
3194 if (get_mouse_class(line + col) != cclass)
3195 break;
3196 pos->col = col;
3197 }
3198}
3199
3200/*
3201 * Move "pos" forward to the end of the word it's in.
3202 * When 'selection' is "exclusive", the position is just after the word.
3203 */
3204 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003205find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
3207 char_u *line;
3208 int cclass;
3209 int col;
3210
3211 line = ml_get(pos->lnum);
3212 if (*p_sel == 'e' && pos->col > 0)
3213 {
3214 --pos->col;
3215#ifdef FEAT_MBYTE
3216 pos->col -= (*mb_head_off)(line, line + pos->col);
3217#endif
3218 }
3219 cclass = get_mouse_class(line + pos->col);
3220 while (line[pos->col] != NUL)
3221 {
3222#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003223 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224#else
3225 col = pos->col + 1;
3226#endif
3227 if (get_mouse_class(line + col) != cclass)
3228 {
3229 if (*p_sel == 'e')
3230 pos->col = col;
3231 break;
3232 }
3233 pos->col = col;
3234 }
3235}
3236
3237/*
3238 * Get class of a character for selection: same class means same word.
3239 * 0: blank
3240 * 1: punctuation groups
3241 * 2: normal word character
3242 * >2: multi-byte word character.
3243 */
3244 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003245get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 int c;
3248
3249#ifdef FEAT_MBYTE
3250 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3251 return mb_get_class(p);
3252#endif
3253
3254 c = *p;
3255 if (c == ' ' || c == '\t')
3256 return 0;
3257
3258 if (vim_iswordc(c))
3259 return 2;
3260
3261 /*
3262 * There are a few special cases where we want certain combinations of
3263 * characters to be considered as a single word. These are things like
3264 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003265 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 */
3267 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3268 return 1;
3269 return c;
3270}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271#endif /* FEAT_MOUSE */
3272
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273/*
3274 * Check if highlighting for visual mode is possible, give a warning message
3275 * if not.
3276 */
3277 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003278check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 static int did_check = FALSE;
3281
3282 if (full_screen)
3283 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003284 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 MSG(_("Warning: terminal cannot highlight"));
3286 did_check = TRUE;
3287 }
3288}
3289
3290/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003291 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 * This function should ALWAYS be called to end Visual mode, except from
3293 * do_pending_operator().
3294 */
3295 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003296end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298#ifdef FEAT_CLIPBOARD
3299 /*
3300 * If we are using the clipboard, then remember what was selected in case
3301 * we need to paste it somewhere while we still own the selection.
3302 * Only do this when the clipboard is already owned. Don't want to grab
3303 * the selection when hitting ESC.
3304 */
3305 if (clip_star.available && clip_star.owned)
3306 clip_auto_select();
3307#endif
3308
3309 VIsual_active = FALSE;
3310#ifdef FEAT_MOUSE
3311 setmouse();
3312 mouse_dragging = 0;
3313#endif
3314
3315 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003316 curbuf->b_visual.vi_mode = VIsual_mode;
3317 curbuf->b_visual.vi_start = VIsual;
3318 curbuf->b_visual.vi_end = curwin->w_cursor;
3319 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#ifdef FEAT_EVAL
3321 curbuf->b_visual_mode_eval = VIsual_mode;
3322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#ifdef FEAT_VIRTUALEDIT
3324 if (!virtual_active())
3325 curwin->w_cursor.coladd = 0;
3326#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003327 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003329 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330}
3331
3332/*
3333 * Reset VIsual_active and VIsual_reselect.
3334 */
3335 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003336reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 if (VIsual_active)
3339 {
3340 end_visual_mode();
3341 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3342 }
3343 VIsual_reselect = FALSE;
3344}
3345
3346/*
3347 * Reset VIsual_active and VIsual_reselect if it's set.
3348 */
3349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003350reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 if (VIsual_active)
3353 {
3354 end_visual_mode();
3355 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3356 VIsual_reselect = FALSE;
3357 }
3358}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360/*
3361 * Check for a balloon-eval special item to include when searching for an
3362 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3363 * Returns TRUE if the character at "*ptr" should be included.
3364 * "dir" is FORWARD or BACKWARD, the direction of searching.
3365 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3366 * "bnp" points to a counter for square brackets.
3367 */
3368 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003369find_is_eval_item(
3370 char_u *ptr,
3371 int *colp,
3372 int *bnp,
3373 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
3375 /* Accept everything inside []. */
3376 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3377 ++*bnp;
3378 if (*bnp > 0)
3379 {
3380 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3381 --*bnp;
3382 return TRUE;
3383 }
3384
3385 /* skip over "s.var" */
3386 if (*ptr == '.')
3387 return TRUE;
3388
3389 /* two-character item: s->var */
3390 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3391 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3392 {
3393 *colp += dir;
3394 return TRUE;
3395 }
3396 return FALSE;
3397}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
3399/*
3400 * Find the identifier under or to the right of the cursor.
3401 * "find_type" can have one of three values:
3402 * FIND_IDENT: find an identifier (keyword)
3403 * FIND_STRING: find any non-white string
3404 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003405 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 *
3407 * There are three steps:
3408 * 1. Search forward for the start of an identifier/string. Doesn't move if
3409 * already on one.
3410 * 2. Search backward for the start of this identifier/string.
3411 * This doesn't match the real Vi but I like it a little better and it
3412 * shouldn't bother anyone.
3413 * 3. Search forward to the end of this identifier/string.
3414 * When FIND_IDENT isn't defined, we backup until a blank.
3415 *
3416 * Returns the length of the string, or zero if no string is found.
3417 * If a string is found, a pointer to the string is put in "*string". This
3418 * string is not always NUL terminated.
3419 */
3420 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003421find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422{
3423 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3424 curwin->w_cursor.col, string, find_type);
3425}
3426
3427/*
3428 * Like find_ident_under_cursor(), but for any window and any position.
3429 * However: Uses 'iskeyword' from the current window!.
3430 */
3431 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003432find_ident_at_pos(
3433 win_T *wp,
3434 linenr_T lnum,
3435 colnr_T startcol,
3436 char_u **string,
3437 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 char_u *ptr;
3440 int col = 0; /* init to shut up GCC */
3441 int i;
3442#ifdef FEAT_MBYTE
3443 int this_class = 0;
3444 int prev_class;
3445 int prevcol;
3446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 /*
3450 * if i == 0: try to find an identifier
3451 * if i == 1: try to find any non-white string
3452 */
3453 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3454 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3455 {
3456 /*
3457 * 1. skip to start of identifier/string
3458 */
3459 col = startcol;
3460#ifdef FEAT_MBYTE
3461 if (has_mbyte)
3462 {
3463 while (ptr[col] != NUL)
3464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 /* Stop at a ']' to evaluate "a[x]". */
3466 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3467 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 this_class = mb_get_class(ptr + col);
3469 if (this_class != 0 && (i == 1 || this_class != 1))
3470 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003471 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473 }
3474 else
3475#endif
3476 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003477 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 )
3480 ++col;
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 /* When starting on a ']' count it, so that we include the '['. */
3483 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
3485 /*
3486 * 2. Back up to start of identifier/string.
3487 */
3488#ifdef FEAT_MBYTE
3489 if (has_mbyte)
3490 {
3491 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3493 this_class = mb_get_class((char_u *)"a");
3494 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003496 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3499 prev_class = mb_get_class(ptr + prevcol);
3500 if (this_class != prev_class
3501 && (i == 0
3502 || prev_class == 0
3503 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 && (!(find_type & FIND_EVAL)
3505 || prevcol == 0
3506 || !find_is_eval_item(ptr + prevcol, &prevcol,
3507 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 )
3509 break;
3510 col = prevcol;
3511 }
3512
3513 /* If we don't want just any old string, or we've found an
3514 * identifier, stop searching. */
3515 if (this_class > 2)
3516 this_class = 2;
3517 if (!(find_type & FIND_STRING) || this_class == 2)
3518 break;
3519 }
3520 else
3521#endif
3522 {
3523 while (col > 0
3524 && ((i == 0
3525 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003526 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 && (!(find_type & FIND_IDENT)
3528 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 || ((find_type & FIND_EVAL)
3530 && col > 1
3531 && find_is_eval_item(ptr + col - 1, &col,
3532 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 ))
3534 --col;
3535
3536 /* If we don't want just any old string, or we've found an
3537 * identifier, stop searching. */
3538 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3539 break;
3540 }
3541 }
3542
3543 if (ptr[col] == NUL || (i == 0 && (
3544#ifdef FEAT_MBYTE
3545 has_mbyte ? this_class != 2 :
3546#endif
3547 !vim_iswordc(ptr[col]))))
3548 {
3549 /*
3550 * didn't find an identifier or string
3551 */
3552 if (find_type & FIND_STRING)
3553 EMSG(_("E348: No string under cursor"));
3554 else
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00003555 EMSG(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 return 0;
3557 }
3558 ptr += col;
3559 *string = ptr;
3560
3561 /*
3562 * 3. Find the end if the identifier/string.
3563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 bn = 0;
3565 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 col = 0;
3567#ifdef FEAT_MBYTE
3568 if (has_mbyte)
3569 {
3570 /* Search for point of changing multibyte character class. */
3571 this_class = mb_get_class(ptr);
3572 while (ptr[col] != NUL
3573 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3574 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 || ((find_type & FIND_EVAL)
3576 && col <= (int)startcol
3577 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003579 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
3581 else
3582#endif
3583 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003584 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 || ((find_type & FIND_EVAL)
3586 && col <= (int)startcol
3587 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 )
3589 {
3590 ++col;
3591 }
3592
3593 return col;
3594}
3595
3596/*
3597 * Prepare for redo of a normal command.
3598 */
3599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003600prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601{
3602 prep_redo(cap->oap->regname, cap->count0,
3603 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3604}
3605
3606/*
3607 * Prepare for redo of any command.
3608 * Note that only the last argument can be a multi-byte char.
3609 */
3610 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003611prep_redo(
3612 int regname,
3613 long num,
3614 int cmd1,
3615 int cmd2,
3616 int cmd3,
3617 int cmd4,
3618 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619{
3620 ResetRedobuff();
3621 if (regname != 0) /* yank from specified buffer */
3622 {
3623 AppendCharToRedobuff('"');
3624 AppendCharToRedobuff(regname);
3625 }
3626 if (num)
3627 AppendNumberToRedobuff(num);
3628
3629 if (cmd1 != NUL)
3630 AppendCharToRedobuff(cmd1);
3631 if (cmd2 != NUL)
3632 AppendCharToRedobuff(cmd2);
3633 if (cmd3 != NUL)
3634 AppendCharToRedobuff(cmd3);
3635 if (cmd4 != NUL)
3636 AppendCharToRedobuff(cmd4);
3637 if (cmd5 != NUL)
3638 AppendCharToRedobuff(cmd5);
3639}
3640
3641/*
3642 * check for operator active and clear it
3643 *
3644 * return TRUE if operator was active
3645 */
3646 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003647checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648{
3649 if (oap->op_type == OP_NOP)
3650 return FALSE;
3651 clearopbeep(oap);
3652 return TRUE;
3653}
3654
3655/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003656 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003658 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
3660 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003661checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003663 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 return FALSE;
3665 clearopbeep(oap);
3666 return TRUE;
3667}
3668
3669 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003670clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 oap->op_type = OP_NOP;
3673 oap->regname = 0;
3674 oap->motion_force = NUL;
3675 oap->use_reg_one = FALSE;
3676}
3677
3678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003679clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 clearop(oap);
3682 beep_flush();
3683}
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685/*
3686 * Remove the shift modifier from a special key.
3687 */
3688 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003689unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691 switch (cap->cmdchar)
3692 {
3693 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3694 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3695 case K_S_UP: cap->cmdchar = K_UP; break;
3696 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3697 case K_S_HOME: cap->cmdchar = K_HOME; break;
3698 case K_S_END: cap->cmdchar = K_END; break;
3699 }
3700 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003703/*
3704 * If the mode is currently displayed clear the command line or update the
3705 * command displayed.
3706 */
3707 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003708may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003709{
3710 if (mode_displayed)
3711 clear_cmdline = TRUE; /* unshow visual mode later */
3712#ifdef FEAT_CMDL_INFO
3713 else
3714 clear_showcmd();
3715#endif
3716}
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3719/*
3720 * Routines for displaying a partly typed command
3721 */
3722
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003723#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724static char_u showcmd_buf[SHOWCMD_BUFLEN];
3725static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3726static int showcmd_is_clear = TRUE;
3727static int showcmd_visual = FALSE;
3728
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003729static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
3731 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003732clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733{
3734 if (!p_sc)
3735 return;
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (VIsual_active && !char_avail())
3738 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003739 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 long lines;
3741 colnr_T leftcol, rightcol;
3742 linenr_T top, bot;
3743
3744 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003745 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
3747 top = VIsual.lnum;
3748 bot = curwin->w_cursor.lnum;
3749 }
3750 else
3751 {
3752 top = curwin->w_cursor.lnum;
3753 bot = VIsual.lnum;
3754 }
3755# ifdef FEAT_FOLDING
3756 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003757 (void)hasFolding(top, &top, NULL);
3758 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759# endif
3760 lines = bot - top + 1;
3761
3762 if (VIsual_mode == Ctrl_V)
3763 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003764# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003765 char_u *saved_sbr = p_sbr;
3766
3767 /* Make 'sbr' empty for a moment to get the correct size. */
3768 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003771# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003772 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3775 (long)(rightcol - leftcol + 1));
3776 }
3777 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3778 sprintf((char *)showcmd_buf, "%ld", lines);
3779 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003780 {
3781 char_u *s, *e;
3782 int l;
3783 int bytes = 0;
3784 int chars = 0;
3785
3786 if (cursor_bot)
3787 {
3788 s = ml_get_pos(&VIsual);
3789 e = ml_get_cursor();
3790 }
3791 else
3792 {
3793 s = ml_get_cursor();
3794 e = ml_get_pos(&VIsual);
3795 }
3796 while ((*p_sel != 'e') ? s <= e : s < e)
3797 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003798# ifdef FEAT_MBYTE
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003799 l = (*mb_ptr2len)(s);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003800# else
3801 l = (*s == NUL) ? 0 : 1;
3802# endif
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003803 if (l == 0)
3804 {
3805 ++bytes;
3806 ++chars;
3807 break; /* end of line */
3808 }
3809 bytes += l;
3810 ++chars;
3811 s += l;
3812 }
3813 if (bytes == chars)
3814 sprintf((char *)showcmd_buf, "%d", chars);
3815 else
3816 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3819 showcmd_visual = TRUE;
3820 }
3821 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
3823 showcmd_buf[0] = NUL;
3824 showcmd_visual = FALSE;
3825
3826 /* Don't actually display something if there is nothing to clear. */
3827 if (showcmd_is_clear)
3828 return;
3829 }
3830
3831 display_showcmd();
3832}
3833
3834/*
3835 * Add 'c' to string of shown command chars.
3836 * Return TRUE if output has been written (and setcursor() has been called).
3837 */
3838 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003839add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840{
3841 char_u *p;
3842 int old_len;
3843 int extra_len;
3844 int overflow;
3845#if defined(FEAT_MOUSE)
3846 int i;
3847 static int ignore[] =
3848 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003849# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3851 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003852# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003853 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003854 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3856 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003857 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003859 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 0
3861 };
3862#endif
3863
Bram Moolenaar09df3122006-01-23 22:23:09 +00003864 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 return FALSE;
3866
3867 if (showcmd_visual)
3868 {
3869 showcmd_buf[0] = NUL;
3870 showcmd_visual = FALSE;
3871 }
3872
3873#if defined(FEAT_MOUSE)
3874 /* Ignore keys that are scrollbar updates and mouse clicks */
3875 if (IS_SPECIAL(c))
3876 for (i = 0; ignore[i] != 0; ++i)
3877 if (ignore[i] == c)
3878 return FALSE;
3879#endif
3880
3881 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003882 if (*p == ' ')
3883 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 old_len = (int)STRLEN(showcmd_buf);
3885 extra_len = (int)STRLEN(p);
3886 overflow = old_len + extra_len - SHOWCMD_COLS;
3887 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003888 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3889 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 STRCAT(showcmd_buf, p);
3891
3892 if (char_avail())
3893 return FALSE;
3894
3895 display_showcmd();
3896
3897 return TRUE;
3898}
3899
3900 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003901add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
3903 if (!add_to_showcmd(c))
3904 setcursor();
3905}
3906
3907/*
3908 * Delete 'len' characters from the end of the shown command.
3909 */
3910 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003911del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
3913 int old_len;
3914
3915 if (!p_sc)
3916 return;
3917
3918 old_len = (int)STRLEN(showcmd_buf);
3919 if (len > old_len)
3920 len = old_len;
3921 showcmd_buf[old_len - len] = NUL;
3922
3923 if (!char_avail())
3924 display_showcmd();
3925}
3926
3927/*
3928 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3929 * something and there is a partial mapping.
3930 */
3931 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003932push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
3934 if (p_sc)
3935 STRCPY(old_showcmd_buf, showcmd_buf);
3936}
3937
3938 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003939pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
3941 if (!p_sc)
3942 return;
3943
3944 STRCPY(showcmd_buf, old_showcmd_buf);
3945
3946 display_showcmd();
3947}
3948
3949 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003950display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951{
3952 int len;
3953
3954 cursor_off();
3955
3956 len = (int)STRLEN(showcmd_buf);
3957 if (len == 0)
3958 showcmd_is_clear = TRUE;
3959 else
3960 {
3961 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3962 showcmd_is_clear = FALSE;
3963 }
3964
3965 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003966 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3967 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 */
3969 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3970
3971 setcursor(); /* put cursor back where it belongs */
3972}
3973#endif
3974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975/*
3976 * When "check" is FALSE, prepare for commands that scroll the window.
3977 * When "check" is TRUE, take care of scroll-binding after the window has
3978 * scrolled. Called from normal_cmd() and edit().
3979 */
3980 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003981do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
3983 static win_T *old_curwin = NULL;
3984 static linenr_T old_topline = 0;
3985#ifdef FEAT_DIFF
3986 static int old_topfill = 0;
3987#endif
3988 static buf_T *old_buf = NULL;
3989 static colnr_T old_leftcol = 0;
3990
3991 if (check && curwin->w_p_scb)
3992 {
3993 /* If a ":syncbind" command was just used, don't scroll, only reset
3994 * the values. */
3995 if (did_syncbind)
3996 did_syncbind = FALSE;
3997 else if (curwin == old_curwin)
3998 {
3999 /*
4000 * Synchronize other windows, as necessary according to
4001 * 'scrollbind'. Don't do this after an ":edit" command, except
4002 * when 'diff' is set.
4003 */
4004 if ((curwin->w_buffer == old_buf
4005#ifdef FEAT_DIFF
4006 || curwin->w_p_diff
4007#endif
4008 )
4009 && (curwin->w_topline != old_topline
4010#ifdef FEAT_DIFF
4011 || curwin->w_topfill != old_topfill
4012#endif
4013 || curwin->w_leftcol != old_leftcol))
4014 {
4015 check_scrollbind(curwin->w_topline - old_topline,
4016 (long)(curwin->w_leftcol - old_leftcol));
4017 }
4018 }
4019 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
4020 {
4021 /*
4022 * When switching between windows, make sure that the relative
4023 * vertical offset is valid for the new window. The relative
4024 * offset is invalid whenever another 'scrollbind' window has
4025 * scrolled to a point that would force the current window to
4026 * scroll past the beginning or end of its buffer. When the
4027 * resync is performed, some of the other 'scrollbind' windows may
4028 * need to jump so that the current window's relative position is
4029 * visible on-screen.
4030 */
4031 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
4032 }
4033 curwin->w_scbind_pos = curwin->w_topline;
4034 }
4035
4036 old_curwin = curwin;
4037 old_topline = curwin->w_topline;
4038#ifdef FEAT_DIFF
4039 old_topfill = curwin->w_topfill;
4040#endif
4041 old_buf = curwin->w_buffer;
4042 old_leftcol = curwin->w_leftcol;
4043}
4044
4045/*
4046 * Synchronize any windows that have "scrollbind" set, based on the
4047 * number of rows by which the current window has changed
4048 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
4049 */
4050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004051check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052{
4053 int want_ver;
4054 int want_hor;
4055 win_T *old_curwin = curwin;
4056 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 int old_VIsual_select = VIsual_select;
4058 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 colnr_T tgt_leftcol = curwin->w_leftcol;
4060 long topline;
4061 long y;
4062
4063 /*
4064 * check 'scrollopt' string for vertical and horizontal scroll options
4065 */
4066 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
4067#ifdef FEAT_DIFF
4068 want_ver |= old_curwin->w_p_diff;
4069#endif
4070 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
4071
4072 /*
4073 * loop through the scrollbound windows and scroll accordingly
4074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004076 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 curbuf = curwin->w_buffer;
4079 /* skip original window and windows with 'noscrollbind' */
4080 if (curwin != old_curwin && curwin->w_p_scb)
4081 {
4082 /*
4083 * do the vertical scroll
4084 */
4085 if (want_ver)
4086 {
4087#ifdef FEAT_DIFF
4088 if (old_curwin->w_p_diff && curwin->w_p_diff)
4089 {
4090 diff_set_topline(old_curwin, curwin);
4091 }
4092 else
4093#endif
4094 {
4095 curwin->w_scbind_pos += topline_diff;
4096 topline = curwin->w_scbind_pos;
4097 if (topline > curbuf->b_ml.ml_line_count)
4098 topline = curbuf->b_ml.ml_line_count;
4099 if (topline < 1)
4100 topline = 1;
4101
4102 y = topline - curwin->w_topline;
4103 if (y > 0)
4104 scrollup(y, FALSE);
4105 else
4106 scrolldown(-y, FALSE);
4107 }
4108
4109 redraw_later(VALID);
4110 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113
4114 /*
4115 * do the horizontal scroll
4116 */
4117 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4118 {
4119 curwin->w_leftcol = tgt_leftcol;
4120 leftcol_changed();
4121 }
4122 }
4123 }
4124
4125 /*
4126 * reset current-window
4127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 VIsual_select = old_VIsual_select;
4129 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 curwin = old_curwin;
4131 curbuf = old_curbuf;
4132}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
4134/*
4135 * Command character that's ignored.
4136 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004137 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004140nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004142 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143}
4144
4145/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004146 * Command character that doesn't do anything, but unlike nv_ignore() does
4147 * start edit(). Used for "startinsert" executed while starting up.
4148 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004150nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004151{
4152}
4153
4154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * Command character doesn't exist.
4156 */
4157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004158nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
4160 clearopbeep(cap->oap);
4161}
4162
4163/*
4164 * <Help> and <F1> commands.
4165 */
4166 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004167nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
4169 if (!checkclearopq(cap->oap))
4170 ex_help(NULL);
4171}
4172
4173/*
4174 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4175 */
4176 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004177nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaarf2732452018-06-03 14:47:35 +02004179#ifdef FEAT_JOB_CHANNEL
4180 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4181 clearopbeep(cap->oap);
4182 else
4183#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01004184 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004185 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004186 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004187 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4188 op_addsub(cap->oap, cap->count1, cap->arg);
4189 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004190 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004191 else if (VIsual_active)
4192 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004193 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004194 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195}
4196
4197/*
4198 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4199 */
4200 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004201nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
4203 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004204 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004205 if (mod_mask & MOD_MASK_CTRL)
4206 {
4207 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4208 if (cap->arg == BACKWARD)
4209 goto_tabpage(-(int)cap->count1);
4210 else
4211 goto_tabpage((int)cap->count0);
4212 }
4213 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004214 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216}
4217
4218/*
4219 * Implementation of "gd" and "gD" command.
4220 */
4221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004222nv_gd(
4223 oparg_T *oap,
4224 int nchar,
4225 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226{
4227 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 char_u *ptr;
4229
Bram Moolenaard9d30582005-05-18 22:10:28 +00004230 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004231 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4232 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004234#ifdef FEAT_FOLDING
4235 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4236 foldOpenCursor();
4237#endif
4238}
4239
4240/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004241 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4242 * otherwise.
4243 */
4244 static int
4245is_ident(char_u *line, int offset)
4246{
4247 int i;
4248 int incomment = FALSE;
4249 int instring = 0;
4250 int prev = 0;
4251
4252 for (i = 0; i < offset && line[i] != NUL; i++)
4253 {
4254 if (instring != 0)
4255 {
4256 if (prev != '\\' && line[i] == instring)
4257 instring = 0;
4258 }
4259 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4260 {
4261 instring = line[i];
4262 }
4263 else
4264 {
4265 if (incomment)
4266 {
4267 if (prev == '*' && line[i] == '/')
4268 incomment = FALSE;
4269 }
4270 else if (prev == '/' && line[i] == '*')
4271 {
4272 incomment = TRUE;
4273 }
4274 else if (prev == '/' && line[i] == '/')
4275 {
4276 return FALSE;
4277 }
4278 }
4279
4280 prev = line[i];
4281 }
4282
4283 return incomment == FALSE && instring == 0;
4284}
4285
4286/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004287 * Search for variable declaration of "ptr[len]".
4288 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4289 * current file ("gD").
4290 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004291 * Return FAIL when not found.
4292 */
4293 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004294find_decl(
4295 char_u *ptr,
4296 int len,
4297 int locally,
4298 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004299 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004300{
4301 char_u *pat;
4302 pos_T old_pos;
4303 pos_T par_pos;
4304 pos_T found_pos;
4305 int t;
4306 int save_p_ws;
4307 int save_p_scs;
4308 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004309 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004310 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004311 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312
4313 if ((pat = alloc(len + 7)) == NULL)
4314 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004315
4316 /* Put "\V" before the pattern to avoid that the special meaning of "."
4317 * and "~" causes trouble. */
4318 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4319 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 old_pos = curwin->w_cursor;
4321 save_p_ws = p_ws;
4322 save_p_scs = p_scs;
4323 p_ws = FALSE; /* don't wrap around end of file now */
4324 p_scs = FALSE; /* don't switch ignorecase off now */
4325
4326 /*
4327 * With "gD" go to line 1.
4328 * With "gd" Search back for the start of the current function, then go
4329 * back until a blank line. If this fails go to line 1.
4330 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004331 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
4333 setpcmark(); /* Set in findpar() otherwise */
4334 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004335 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337 else
4338 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004339 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4341 --curwin->w_cursor.lnum;
4342 }
4343 curwin->w_cursor.col = 0;
4344
4345 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004346 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004347 for (;;)
4348 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004349 valid = FALSE;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004350 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004351 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004352 if (curwin->w_cursor.lnum >= old_pos.lnum)
4353 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004354
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004355 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004356 {
4357 pos_T *pos;
4358
4359 /* Check that the block the match is in doesn't end before the
4360 * position where we started the search from. */
4361 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4362 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4363 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004364 {
4365 /* There can't be a useful match before the end of this block.
4366 * Skip to the end. */
4367 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004368 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004369 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004370 }
4371
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004372 if (t == FAIL)
4373 {
4374 /* If we previously found a valid position, use it. */
4375 if (found_pos.lnum != 0)
4376 {
4377 curwin->w_cursor = found_pos;
4378 t = OK;
4379 }
4380 break;
4381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004383 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004384 {
4385 /* Ignore this line, continue at start of next line. */
4386 ++curwin->w_cursor.lnum;
4387 curwin->w_cursor.col = 0;
4388 continue;
4389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004391 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4392
4393 /* If the current position is not a valid identifier and a previous
4394 * match is present, favor that one instead. */
4395 if (!valid && found_pos.lnum != 0)
4396 {
4397 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004398 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004399 }
4400
4401 /* Global search: use first valid match found */
4402 if (valid && !locally)
4403 break;
4404 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004405 {
4406 /* If we previously found a valid position, use it. */
4407 if (found_pos.lnum != 0)
4408 curwin->w_cursor = found_pos;
4409 break;
4410 }
4411
Bram Moolenaar226630a2016-10-08 19:21:31 +02004412 /* For finding a local variable and the match is before the "{" or
4413 * inside a comment, continue searching. For K&R style function
4414 * declarations this skips the function header without types. */
4415 if (!valid)
4416 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004417 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004418 }
4419 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004420 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004421 /* Remove SEARCH_START from flags to avoid getting stuck at one
4422 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004423 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004425
4426 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004428 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 curwin->w_cursor = old_pos;
4430 }
4431 else
4432 {
4433 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 /* "n" searches forward now */
4435 reset_search_dir();
4436 }
4437
4438 vim_free(pat);
4439 p_ws = save_p_ws;
4440 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004441
4442 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443}
4444
4445/*
4446 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4447 * lines rather than lines in the file.
4448 * 'dist' must be positive.
4449 *
4450 * Return OK if able to move cursor, FAIL otherwise.
4451 */
4452 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004453nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454{
4455 int linelen = linetabsize(ml_get_curline());
4456 int retval = OK;
4457 int atend = FALSE;
4458 int n;
4459 int col_off1; /* margin offset for first screen line */
4460 int col_off2; /* margin offset for wrapped screen line */
4461 int width1; /* text width for first screen line */
4462 int width2; /* test width for wrapped screen line */
4463
4464 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004465 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 col_off1 = curwin_col_off();
4468 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004469 width1 = curwin->w_width - col_off1;
4470 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004471 if (width2 == 0)
4472 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004475 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 /*
4477 * Instead of sticking at the last character of the buffer line we
4478 * try to stick in the last column of the screen.
4479 */
4480 if (curwin->w_curswant == MAXCOL)
4481 {
4482 atend = TRUE;
4483 validate_virtcol();
4484 if (width1 <= 0)
4485 curwin->w_curswant = 0;
4486 else
4487 {
4488 curwin->w_curswant = width1 - 1;
4489 if (curwin->w_virtcol > curwin->w_curswant)
4490 curwin->w_curswant += ((curwin->w_virtcol
4491 - curwin->w_curswant - 1) / width2 + 1) * width2;
4492 }
4493 }
4494 else
4495 {
4496 if (linelen > width1)
4497 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4498 else
4499 n = width1;
4500 if (curwin->w_curswant > (colnr_T)n + 1)
4501 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4502 * width2;
4503 }
4504
4505 while (dist--)
4506 {
4507 if (dir == BACKWARD)
4508 {
4509 if ((long)curwin->w_curswant >= width2)
4510 /* move back within line */
4511 curwin->w_curswant -= width2;
4512 else
4513 {
4514 /* to previous line */
4515 if (curwin->w_cursor.lnum == 1)
4516 {
4517 retval = FAIL;
4518 break;
4519 }
4520 --curwin->w_cursor.lnum;
4521#ifdef FEAT_FOLDING
4522 /* Move to the start of a closed fold. Don't do that when
4523 * 'foldopen' contains "all": it will open in a moment. */
4524 if (!(fdo_flags & FDO_ALL))
4525 (void)hasFolding(curwin->w_cursor.lnum,
4526 &curwin->w_cursor.lnum, NULL);
4527#endif
4528 linelen = linetabsize(ml_get_curline());
4529 if (linelen > width1)
4530 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4531 + 1) * width2;
4532 }
4533 }
4534 else /* dir == FORWARD */
4535 {
4536 if (linelen > width1)
4537 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4538 else
4539 n = width1;
4540 if (curwin->w_curswant + width2 < (colnr_T)n)
4541 /* move forward within line */
4542 curwin->w_curswant += width2;
4543 else
4544 {
4545 /* to next line */
4546#ifdef FEAT_FOLDING
4547 /* Move to the end of a closed fold. */
4548 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4549 &curwin->w_cursor.lnum);
4550#endif
4551 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4552 {
4553 retval = FAIL;
4554 break;
4555 }
4556 curwin->w_cursor.lnum++;
4557 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004558 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560 }
4561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004564 if (virtual_active() && atend)
4565 coladvance(MAXCOL);
4566 else
4567 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4570 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4571 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004572 colnr_T virtcol;
4573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 /*
4575 * Check for landing on a character that got split at the end of the
4576 * last line. We want to advance a screenline, not end up in the same
4577 * screenline or move two screenlines.
4578 */
4579 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004580 virtcol = curwin->w_virtcol;
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004581# if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004582 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4583 virtcol -= vim_strsize(p_sbr);
Bram Moolenaar84d8cdd2014-08-30 13:32:06 +02004584# endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004585
4586 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 && (curwin->w_curswant < (colnr_T)width1
4588 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4589 : ((curwin->w_curswant - width1) % width2
4590 > (colnr_T)width2 / 2)))
4591 --curwin->w_cursor.col;
4592 }
4593#endif
4594
4595 if (atend)
4596 curwin->w_curswant = MAXCOL; /* stick in the last column */
4597
4598 return retval;
4599}
4600
4601#ifdef FEAT_MOUSE
4602/*
4603 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4604 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004605 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4606 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 */
4608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004609nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004611 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004613 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
4615 int row, col;
4616
4617 row = mouse_row;
4618 col = mouse_col;
4619
4620 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004621 wp = mouse_find_win(&row, &col);
4622 if (wp == NULL)
4623 return;
4624 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 curbuf = curwin->w_buffer;
4626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004628 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004630# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004631 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004632 /* This window is a terminal window, send the mouse event there.
4633 * Set "typed" to FALSE to avoid an endless loop. */
4634 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004635 else
4636# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004637 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4638 {
4639 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4640 }
4641 else
4642 {
4643 cap->count1 = 3;
4644 cap->count0 = 3;
4645 nv_scroll_line(cap);
4646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004648# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 else
4650 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004651 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4652 if (!curwin->w_p_wrap)
4653 {
4654 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004655
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004656 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004657 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004658 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4659 if (val < 0)
4660 val = 0;
4661
4662 gui_do_horiz_scroll(val, TRUE);
4663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 curwin->w_redr_status = TRUE;
4668
4669 curwin = old_curwin;
4670 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671}
4672
4673/*
4674 * Mouse clicks and drags.
4675 */
4676 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004677nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4680}
4681#endif
4682
4683/*
4684 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4685 * cap->arg must be TRUE for CTRL-E.
4686 */
4687 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004688nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 if (!checkclearop(cap->oap))
4691 scroll_redraw(cap->arg, cap->count1);
4692}
4693
4694/*
4695 * Scroll "count" lines up or down, and redraw.
4696 */
4697 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004698scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 linenr_T prev_topline = curwin->w_topline;
4701#ifdef FEAT_DIFF
4702 int prev_topfill = curwin->w_topfill;
4703#endif
4704 linenr_T prev_lnum = curwin->w_cursor.lnum;
4705
4706 if (up)
4707 scrollup(count, TRUE);
4708 else
4709 scrolldown(count, TRUE);
4710 if (p_so)
4711 {
4712 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4713 * valid, otherwise the screen jumps back at the end of the file. */
4714 cursor_correct();
4715 check_cursor_moved(curwin);
4716 curwin->w_valid |= VALID_TOPLINE;
4717
4718 /* If moved back to where we were, at least move the cursor, otherwise
4719 * we get stuck at one position. Don't move the cursor up if the
4720 * first line of the buffer is already on the screen */
4721 while (curwin->w_topline == prev_topline
4722#ifdef FEAT_DIFF
4723 && curwin->w_topfill == prev_topfill
4724#endif
4725 )
4726 {
4727 if (up)
4728 {
4729 if (curwin->w_cursor.lnum > prev_lnum
4730 || cursor_down(1L, FALSE) == FAIL)
4731 break;
4732 }
4733 else
4734 {
4735 if (curwin->w_cursor.lnum < prev_lnum
4736 || prev_topline == 1L
4737 || cursor_up(1L, FALSE) == FAIL)
4738 break;
4739 }
4740 /* Mark w_topline as valid, otherwise the screen jumps back at the
4741 * end of the file. */
4742 check_cursor_moved(curwin);
4743 curwin->w_valid |= VALID_TOPLINE;
4744 }
4745 }
4746 if (curwin->w_cursor.lnum != prev_lnum)
4747 coladvance(curwin->w_curswant);
4748 redraw_later(VALID);
4749}
4750
4751/*
4752 * Commands that start with "z".
4753 */
4754 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004755nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
4757 long n;
4758 colnr_T col;
4759 int nchar = cap->nchar;
4760#ifdef FEAT_FOLDING
4761 long old_fdl = curwin->w_p_fdl;
4762 int old_fen = curwin->w_p_fen;
4763#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004764#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004765 int undo = FALSE;
4766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767
4768 if (VIM_ISDIGIT(nchar))
4769 {
4770 /*
4771 * "z123{nchar}": edit the count before obtaining {nchar}
4772 */
4773 if (checkclearop(cap->oap))
4774 return;
4775 n = nchar - '0';
4776 for (;;)
4777 {
4778#ifdef USE_ON_FLY_SCROLL
4779 dont_scroll = TRUE; /* disallow scrolling here */
4780#endif
4781 ++no_mapping;
4782 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004783 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 --no_mapping;
4786 --allow_keys;
4787#ifdef FEAT_CMDL_INFO
4788 (void)add_to_showcmd(nchar);
4789#endif
4790 if (nchar == K_DEL || nchar == K_KDEL)
4791 n /= 10;
4792 else if (VIM_ISDIGIT(nchar))
4793 n = n * 10 + (nchar - '0');
4794 else if (nchar == CAR)
4795 {
4796#ifdef FEAT_GUI
4797 need_mouse_correct = TRUE;
4798#endif
4799 win_setheight((int)n);
4800 break;
4801 }
4802 else if (nchar == 'l'
4803 || nchar == 'h'
4804 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004805 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 {
4807 cap->count1 = n ? n * cap->count1 : cap->count1;
4808 goto dozet;
4809 }
4810 else
4811 {
4812 clearopbeep(cap->oap);
4813 break;
4814 }
4815 }
4816 cap->oap->op_type = OP_NOP;
4817 return;
4818 }
4819
4820dozet:
4821 if (
4822#ifdef FEAT_FOLDING
4823 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4824 * and "zC" only in Visual mode. "zj" and "zk" are motion
4825 * commands. */
4826 cap->nchar != 'f' && cap->nchar != 'F'
4827 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4828 && cap->nchar != 'j' && cap->nchar != 'k'
4829 &&
4830#endif
4831 checkclearop(cap->oap))
4832 return;
4833
4834 /*
4835 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4836 * If line number given, set cursor.
4837 */
4838 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4839 && cap->count0
4840 && cap->count0 != curwin->w_cursor.lnum)
4841 {
4842 setpcmark();
4843 if (cap->count0 > curbuf->b_ml.ml_line_count)
4844 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4845 else
4846 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004847 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849
4850 switch (nchar)
4851 {
4852 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4853 case '+':
4854 if (cap->count0 == 0)
4855 {
4856 /* No count given: put cursor at the line below screen */
4857 validate_botline(); /* make sure w_botline is valid */
4858 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4859 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4860 else
4861 curwin->w_cursor.lnum = curwin->w_botline;
4862 }
4863 /* FALLTHROUGH */
4864 case NL:
4865 case CAR:
4866 case K_KENTER:
4867 beginline(BL_WHITE | BL_FIX);
4868 /* FALLTHROUGH */
4869
4870 case 't': scroll_cursor_top(0, TRUE);
4871 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004872 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 break;
4874
4875 /* "z." and "zz": put cursor in middle of screen */
4876 case '.': beginline(BL_WHITE | BL_FIX);
4877 /* FALLTHROUGH */
4878
4879 case 'z': scroll_cursor_halfway(TRUE);
4880 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004881 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 break;
4883
4884 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4885 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4886 * when <count> is at bottom of window, and puts that one at
4887 * bottom of window. */
4888 if (cap->count0 != 0)
4889 {
4890 scroll_cursor_bot(0, TRUE);
4891 curwin->w_cursor.lnum = curwin->w_topline;
4892 }
4893 else if (curwin->w_topline == 1)
4894 curwin->w_cursor.lnum = 1;
4895 else
4896 curwin->w_cursor.lnum = curwin->w_topline - 1;
4897 /* FALLTHROUGH */
4898 case '-':
4899 beginline(BL_WHITE | BL_FIX);
4900 /* FALLTHROUGH */
4901
4902 case 'b': scroll_cursor_bot(0, TRUE);
4903 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004904 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 break;
4906
4907 /* "zH" - scroll screen right half-page */
4908 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004909 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 /* FALLTHROUGH */
4911
4912 /* "zh" - scroll screen to the right */
4913 case 'h':
4914 case K_LEFT:
4915 if (!curwin->w_p_wrap)
4916 {
4917 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4918 curwin->w_leftcol = 0;
4919 else
4920 curwin->w_leftcol -= (colnr_T)cap->count1;
4921 leftcol_changed();
4922 }
4923 break;
4924
4925 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004926 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 /* FALLTHROUGH */
4928
4929 /* "zl" - scroll screen to the left */
4930 case 'l':
4931 case K_RIGHT:
4932 if (!curwin->w_p_wrap)
4933 {
4934 /* scroll the window left */
4935 curwin->w_leftcol += (colnr_T)cap->count1;
4936 leftcol_changed();
4937 }
4938 break;
4939
4940 /* "zs" - scroll screen, cursor at the start */
4941 case 's': if (!curwin->w_p_wrap)
4942 {
4943#ifdef FEAT_FOLDING
4944 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4945 col = 0; /* like the cursor is in col 0 */
4946 else
4947#endif
4948 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4949 if ((long)col > p_siso)
4950 col -= p_siso;
4951 else
4952 col = 0;
4953 if (curwin->w_leftcol != col)
4954 {
4955 curwin->w_leftcol = col;
4956 redraw_later(NOT_VALID);
4957 }
4958 }
4959 break;
4960
4961 /* "ze" - scroll screen, cursor at the end */
4962 case 'e': if (!curwin->w_p_wrap)
4963 {
4964#ifdef FEAT_FOLDING
4965 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4966 col = 0; /* like the cursor is in col 0 */
4967 else
4968#endif
4969 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004970 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if ((long)col + p_siso < n)
4972 col = 0;
4973 else
4974 col = col + p_siso - n + 1;
4975 if (curwin->w_leftcol != col)
4976 {
4977 curwin->w_leftcol = col;
4978 redraw_later(NOT_VALID);
4979 }
4980 }
4981 break;
4982
4983#ifdef FEAT_FOLDING
4984 /* "zF": create fold command */
4985 /* "zf": create fold operator */
4986 case 'F':
4987 case 'f': if (foldManualAllowed(TRUE))
4988 {
4989 cap->nchar = 'f';
4990 nv_operator(cap);
4991 curwin->w_p_fen = TRUE;
4992
4993 /* "zF" is like "zfzf" */
4994 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4995 {
4996 nv_operator(cap);
4997 finish_op = TRUE;
4998 }
4999 }
5000 else
5001 clearopbeep(cap->oap);
5002 break;
5003
5004 /* "zd": delete fold at cursor */
5005 /* "zD": delete fold at cursor recursively */
5006 case 'd':
5007 case 'D': if (foldManualAllowed(FALSE))
5008 {
5009 if (VIsual_active)
5010 nv_operator(cap);
5011 else
5012 deleteFold(curwin->w_cursor.lnum,
5013 curwin->w_cursor.lnum, nchar == 'D', FALSE);
5014 }
5015 break;
5016
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005017 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 case 'E': if (foldmethodIsManual(curwin))
5019 {
5020 clearFolding(curwin);
5021 changed_window_setting();
5022 }
5023 else if (foldmethodIsMarker(curwin))
5024 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
5025 TRUE, FALSE);
5026 else
5027 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
5028 break;
5029
5030 /* "zn": fold none: reset 'foldenable' */
5031 case 'n': curwin->w_p_fen = FALSE;
5032 break;
5033
5034 /* "zN": fold Normal: set 'foldenable' */
5035 case 'N': curwin->w_p_fen = TRUE;
5036 break;
5037
5038 /* "zi": invert folding: toggle 'foldenable' */
5039 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
5040 break;
5041
5042 /* "za": open closed fold or close open fold at cursor */
5043 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5044 openFold(curwin->w_cursor.lnum, cap->count1);
5045 else
5046 {
5047 closeFold(curwin->w_cursor.lnum, cap->count1);
5048 curwin->w_p_fen = TRUE;
5049 }
5050 break;
5051
5052 /* "zA": open fold at cursor recursively */
5053 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
5054 openFoldRecurse(curwin->w_cursor.lnum);
5055 else
5056 {
5057 closeFoldRecurse(curwin->w_cursor.lnum);
5058 curwin->w_p_fen = TRUE;
5059 }
5060 break;
5061
5062 /* "zo": open fold at cursor or Visual area */
5063 case 'o': if (VIsual_active)
5064 nv_operator(cap);
5065 else
5066 openFold(curwin->w_cursor.lnum, cap->count1);
5067 break;
5068
5069 /* "zO": open fold recursively */
5070 case 'O': if (VIsual_active)
5071 nv_operator(cap);
5072 else
5073 openFoldRecurse(curwin->w_cursor.lnum);
5074 break;
5075
5076 /* "zc": close fold at cursor or Visual area */
5077 case 'c': if (VIsual_active)
5078 nv_operator(cap);
5079 else
5080 closeFold(curwin->w_cursor.lnum, cap->count1);
5081 curwin->w_p_fen = TRUE;
5082 break;
5083
5084 /* "zC": close fold recursively */
5085 case 'C': if (VIsual_active)
5086 nv_operator(cap);
5087 else
5088 closeFoldRecurse(curwin->w_cursor.lnum);
5089 curwin->w_p_fen = TRUE;
5090 break;
5091
5092 /* "zv": open folds at the cursor */
5093 case 'v': foldOpenCursor();
5094 break;
5095
5096 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5097 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005098 curwin->w_foldinvalid = TRUE; /* recompute folds */
5099 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 foldOpenCursor();
5101 break;
5102
5103 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5104 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005105 curwin->w_foldinvalid = TRUE; /* recompute folds */
5106 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 break;
5108
5109 /* "zm": fold more */
5110 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005111 {
5112 curwin->w_p_fdl -= cap->count1;
5113 if (curwin->w_p_fdl < 0)
5114 curwin->w_p_fdl = 0;
5115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 old_fdl = -1; /* force an update */
5117 curwin->w_p_fen = TRUE;
5118 break;
5119
5120 /* "zM": close all folds */
5121 case 'M': curwin->w_p_fdl = 0;
5122 old_fdl = -1; /* force an update */
5123 curwin->w_p_fen = TRUE;
5124 break;
5125
5126 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005127 case 'r': curwin->w_p_fdl += cap->count1;
5128 {
5129 int d = getDeepestNesting();
5130
5131 if (curwin->w_p_fdl >= d)
5132 curwin->w_p_fdl = d;
5133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 break;
5135
5136 /* "zR": open all folds */
5137 case 'R': curwin->w_p_fdl = getDeepestNesting();
5138 old_fdl = -1; /* force an update */
5139 break;
5140
5141 case 'j': /* "zj" move to next fold downwards */
5142 case 'k': /* "zk" move to next fold upwards */
5143 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5144 cap->count1) == FAIL)
5145 clearopbeep(cap->oap);
5146 break;
5147
5148#endif /* FEAT_FOLDING */
5149
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005150#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005151 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5152 ++no_mapping;
5153 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005154 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005155 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005156 --no_mapping;
5157 --allow_keys;
5158#ifdef FEAT_CMDL_INFO
5159 (void)add_to_showcmd(nchar);
5160#endif
5161 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5162 {
5163 clearopbeep(cap->oap);
5164 break;
5165 }
5166 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005167 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005168
Bram Moolenaarb765d632005-06-07 21:00:02 +00005169 case 'g': /* "zg": add good word to word list */
5170 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005171 case 'G': /* "zG": add good word to temp word list */
5172 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005173 {
5174 char_u *ptr = NULL;
5175 int len;
5176
5177 if (checkclearop(cap->oap))
5178 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005179 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5180 == FAIL)
5181 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005182 if (ptr == NULL)
5183 {
5184 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005185
Bram Moolenaar134bf072013-09-25 18:54:24 +02005186 /* Find bad word under the cursor. When 'spell' is
5187 * off this fails and find_ident_under_cursor() is
5188 * used below. */
5189 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005190 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005191 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005192 if (len != 0 && curwin->w_cursor.col <= pos.col)
5193 ptr = ml_get_pos(&curwin->w_cursor);
5194 curwin->w_cursor = pos;
5195 }
5196
Bram Moolenaarb765d632005-06-07 21:00:02 +00005197 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5198 FIND_IDENT)) == 0)
5199 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005200 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005201 (nchar == 'G' || nchar == 'W')
5202 ? 0 : (int)cap->count1,
5203 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005204 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005205 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206
Bram Moolenaar43abc522005-12-10 20:15:02 +00005207 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005208 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005209 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005211#endif
5212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 default: clearopbeep(cap->oap);
5214 }
5215
5216#ifdef FEAT_FOLDING
5217 /* Redraw when 'foldenable' changed */
5218 if (old_fen != curwin->w_p_fen)
5219 {
5220# ifdef FEAT_DIFF
5221 win_T *wp;
5222
5223 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5224 {
5225 /* Adjust 'foldenable' in diff-synced windows. */
5226 FOR_ALL_WINDOWS(wp)
5227 {
5228 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5229 {
5230 wp->w_p_fen = curwin->w_p_fen;
5231 changed_window_setting_win(wp);
5232 }
5233 }
5234 }
5235# endif
5236 changed_window_setting();
5237 }
5238
5239 /* Redraw when 'foldlevel' changed. */
5240 if (old_fdl != curwin->w_p_fdl)
5241 newFoldLevel();
5242#endif
5243}
5244
5245#ifdef FEAT_GUI
5246/*
5247 * Vertical scrollbar movement.
5248 */
5249 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005250nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
5252 if (cap->oap->op_type != OP_NOP)
5253 clearopbeep(cap->oap);
5254
5255 /* Even if an operator was pending, we still want to scroll */
5256 gui_do_scroll();
5257}
5258
5259/*
5260 * Horizontal scrollbar movement.
5261 */
5262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005263nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
5265 if (cap->oap->op_type != OP_NOP)
5266 clearopbeep(cap->oap);
5267
5268 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005269 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270}
5271#endif
5272
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005273#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005274/*
5275 * Click in GUI tab.
5276 */
5277 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005279{
5280 if (cap->oap->op_type != OP_NOP)
5281 clearopbeep(cap->oap);
5282
5283 /* Even if an operator was pending, we still want to jump tabs. */
5284 goto_tabpage(current_tab);
5285}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005286
5287/*
5288 * Selected item in tab line menu.
5289 */
5290 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005291nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005292{
5293 if (cap->oap->op_type != OP_NOP)
5294 clearopbeep(cap->oap);
5295
5296 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005297 handle_tabmenu();
5298}
5299
5300/*
5301 * Handle selecting an item of the GUI tab line menu.
5302 * Used in Normal and Insert mode.
5303 */
5304 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005305handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005306{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005307 switch (current_tabmenu)
5308 {
5309 case TABLINE_MENU_CLOSE:
5310 if (current_tab == 0)
5311 do_cmdline_cmd((char_u *)"tabclose");
5312 else
5313 {
5314 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5315 current_tab);
5316 do_cmdline_cmd(IObuff);
5317 }
5318 break;
5319
5320 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005321 if (current_tab == 0)
5322 do_cmdline_cmd((char_u *)"$tabnew");
5323 else
5324 {
5325 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5326 current_tab - 1);
5327 do_cmdline_cmd(IObuff);
5328 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005329 break;
5330
5331 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005332 if (current_tab == 0)
5333 do_cmdline_cmd((char_u *)"browse $tabnew");
5334 else
5335 {
5336 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5337 current_tab - 1);
5338 do_cmdline_cmd(IObuff);
5339 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005340 break;
5341 }
5342}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005343#endif
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345/*
5346 * "Q" command.
5347 */
5348 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005349nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 /*
5352 * Ignore 'Q' in Visual mode, just give a beep.
5353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005355 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005356 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 do_exmode(FALSE);
5358}
5359
5360/*
5361 * Handle a ":" command.
5362 */
5363 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005364nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005367 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 if (VIsual_active)
5370 nv_operator(cap);
5371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
5373 if (cap->oap->op_type != OP_NOP)
5374 {
5375 /* Using ":" as a movement is characterwise exclusive. */
5376 cap->oap->motion_type = MCHAR;
5377 cap->oap->inclusive = FALSE;
5378 }
5379 else if (cap->count0)
5380 {
5381 /* translate "count:" into ":.,.+(count - 1)" */
5382 stuffcharReadbuff('.');
5383 if (cap->count0 > 1)
5384 {
5385 stuffReadbuff((char_u *)",.+");
5386 stuffnumReadbuff((long)cap->count0 - 1L);
5387 }
5388 }
5389
5390 /* When typing, don't type below an old message */
5391 if (KeyTyped)
5392 compute_cmdrow();
5393
5394 old_p_im = p_im;
5395
5396 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005397 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5399
5400 /* If 'insertmode' changed, enter or exit Insert mode */
5401 if (p_im != old_p_im)
5402 {
5403 if (p_im)
5404 restart_edit = 'i';
5405 else
5406 restart_edit = 0;
5407 }
5408
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005409 if (cmd_result == FAIL)
5410 /* The Ex command failed, do not execute the operator. */
5411 clearop(cap->oap);
5412 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5414 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005415 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5416 || did_emsg
5417 ))
5418 /* The start of the operator has become invalid by the Ex command.
5419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 clearopbeep(cap->oap);
5421 }
5422}
5423
5424/*
5425 * Handle CTRL-G command.
5426 */
5427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005428nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 if (VIsual_active) /* toggle Selection/Visual mode */
5431 {
5432 VIsual_select = !VIsual_select;
5433 showmode();
5434 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005435 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 /* print full name if count given or :cd used */
5437 fileinfo((int)cap->count0, FALSE, TRUE);
5438}
5439
5440/*
5441 * Handle CTRL-H <Backspace> command.
5442 */
5443 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005444nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (VIsual_active && VIsual_select)
5447 {
5448 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5449 v_visop(cap);
5450 }
5451 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 nv_left(cap);
5453}
5454
5455/*
5456 * CTRL-L: clear screen and redraw.
5457 */
5458 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005459nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460{
5461 if (!checkclearop(cap->oap))
5462 {
5463#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5464 /*
5465 * Right now, the BeBox doesn't seem to have an easy way to detect
5466 * window resizing, so we cheat and make the user detect it
5467 * manually with CTRL-L instead
5468 */
5469 ui_get_shellsize();
5470#endif
5471#ifdef FEAT_SYN_HL
5472 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005473 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005474# ifdef FEAT_RELTIME
5475 {
5476 win_T *wp;
5477
5478 FOR_ALL_WINDOWS(wp)
5479 wp->w_s->b_syn_slow = FALSE;
5480 }
5481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482#endif
5483 redraw_later(CLEAR);
5484 }
5485}
5486
5487/*
5488 * CTRL-O: In Select mode: switch to Visual mode for one command.
5489 * Otherwise: Go to older pcmark.
5490 */
5491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005492nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (VIsual_active && VIsual_select)
5495 {
5496 VIsual_select = FALSE;
5497 showmode();
5498 restart_VIsual_select = 2; /* restart Select mode later */
5499 }
5500 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 {
5502 cap->count1 = -cap->count1;
5503 nv_pcmark(cap);
5504 }
5505}
5506
5507/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01005508 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
5509 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 */
5511 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005512nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 if (!checkclearopq(cap->oap))
5515 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5516 GETF_SETMARK|GETF_ALT, FALSE);
5517}
5518
5519/*
5520 * "Z" commands.
5521 */
5522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005523nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 if (!checkclearopq(cap->oap))
5526 {
5527 switch (cap->nchar)
5528 {
5529 /* "ZZ": equivalent to ":x". */
5530 case 'Z': do_cmdline_cmd((char_u *)"x");
5531 break;
5532
5533 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5534 case 'Q': do_cmdline_cmd((char_u *)"q!");
5535 break;
5536
5537 default: clearopbeep(cap->oap);
5538 }
5539 }
5540}
5541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542/*
5543 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5544 */
5545 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005546do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 oparg_T oa;
5549 cmdarg_T ca;
5550
5551 clear_oparg(&oa);
5552 vim_memset(&ca, 0, sizeof(ca));
5553 ca.oap = &oa;
5554 ca.cmdchar = c1;
5555 ca.nchar = c2;
5556 nv_ident(&ca);
5557}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
5559/*
5560 * Handle the commands that use the word under the cursor.
5561 * [g] CTRL-] :ta to current identifier
5562 * [g] 'K' run program for current identifier
5563 * [g] '*' / to current identifier or string
5564 * [g] '#' ? to current identifier or string
5565 * g ']' :tselect for current identifier
5566 */
5567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005568nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 char_u *ptr = NULL;
5571 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005572 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005573 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 char_u *p;
5575 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005576 int kp_help; /* 'keywordprg' is ":he" */
5577 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 int n = 0; /* init for GCC */
5579 int cmdchar;
5580 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005581 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 char_u *aux_ptr;
5583 int isman;
5584 int isman_s;
5585
5586 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5587 {
5588 cmdchar = cap->nchar;
5589 g_cmd = TRUE;
5590 }
5591 else
5592 {
5593 cmdchar = cap->cmdchar;
5594 g_cmd = FALSE;
5595 }
5596
5597 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5598 cmdchar = '#';
5599
5600 /*
5601 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5602 */
5603 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5606 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 if (checkclearopq(cap->oap))
5608 return;
5609 }
5610
5611 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5612 (cmdchar == '*' || cmdchar == '#')
5613 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5614 {
5615 clearop(cap->oap);
5616 return;
5617 }
5618
5619 /* Allocate buffer to put the command in. Inserting backslashes can
5620 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5621 * and some numbers. */
5622 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5623 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5624 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005625 if (kp_help && *skipwhite(ptr) == NUL)
5626 {
5627 EMSG(_(e_noident)); /* found white space only */
5628 return;
5629 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005630 kp_ex = (*kp == ':');
5631 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5632 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 if (buf == NULL)
5634 return;
5635 buf[0] = NUL;
5636
5637 switch (cmdchar)
5638 {
5639 case '*':
5640 case '#':
5641 /*
5642 * Put cursor at start of word, makes search skip the word
5643 * under the cursor.
5644 * Call setpcmark() first, so "*``" puts the cursor back where
5645 * it was.
5646 */
5647 setpcmark();
5648 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5649
5650 if (!g_cmd && vim_iswordp(ptr))
5651 STRCPY(buf, "\\<");
5652 no_smartcase = TRUE; /* don't use 'smartcase' now */
5653 break;
5654
5655 case 'K':
5656 if (kp_help)
5657 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005658 else if (kp_ex)
5659 {
5660 if (cap->count0 != 0)
5661 vim_snprintf((char *)buf, buflen, "%s %ld",
5662 kp, cap->count0);
5663 else
5664 STRCPY(buf, kp);
5665 STRCAT(buf, " ");
5666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 else
5668 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005669 /* An external command will probably use an argument starting
5670 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005671 while (*ptr == '-' && n > 0)
5672 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005673 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005674 --n;
5675 }
5676 if (n == 0)
5677 {
5678 EMSG(_(e_noident)); /* found dashes only */
5679 vim_free(buf);
5680 return;
5681 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005682
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 /* When a count is given, turn it into a range. Is this
5684 * really what we want? */
5685 isman = (STRCMP(kp, "man") == 0);
5686 isman_s = (STRCMP(kp, "man -s") == 0);
5687 if (cap->count0 != 0 && !(isman || isman_s))
5688 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5689
5690 STRCAT(buf, "! ");
5691 if (cap->count0 == 0 && isman_s)
5692 STRCAT(buf, "man");
5693 else
5694 STRCAT(buf, kp);
5695 STRCAT(buf, " ");
5696 if (cap->count0 != 0 && (isman || isman_s))
5697 {
5698 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5699 STRCAT(buf, " ");
5700 }
5701 }
5702 break;
5703
5704 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005705 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706#ifdef FEAT_CSCOPE
5707 if (p_cst)
5708 STRCPY(buf, "cstag ");
5709 else
5710#endif
5711 STRCPY(buf, "ts ");
5712 break;
5713
5714 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005715 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (curbuf->b_help)
5717 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005719 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005720 if (g_cmd)
5721 STRCPY(buf, "tj ");
5722 else
5723 sprintf((char *)buf, "%ldta ", cap->count0);
5724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726
5727 /*
5728 * Now grab the chars in the identifier
5729 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005730 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005732 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005733 if (kp_ex)
5734 /* Escape the argument properly for an Ex command */
5735 p = vim_strsave_fnameescape(ptr, FALSE);
5736 else
5737 /* Escape the argument properly for a shell command */
5738 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005739 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005740 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005742 vim_free(buf);
5743 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005745 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5746 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005747 {
5748 vim_free(buf);
5749 vim_free(p);
5750 return;
5751 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005752 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005753 STRCAT(buf, p);
5754 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005756 else
5757 {
5758 if (cmdchar == '*')
5759 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5760 else if (cmdchar == '#')
5761 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005762 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005763 {
5764 if (curbuf->b_help)
5765 /* ":help" handles unescaped argument */
5766 aux_ptr = (char_u *)"";
5767 else
5768 aux_ptr = (char_u *)"\\|\"\n[";
5769 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005770 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005771 aux_ptr = (char_u *)"\\|\"\n*?[";
5772
5773 p = buf + STRLEN(buf);
5774 while (n-- > 0)
5775 {
5776 /* put a backslash before \ and some others */
5777 if (vim_strchr(aux_ptr, *ptr) != NULL)
5778 *p++ = '\\';
5779#ifdef FEAT_MBYTE
5780 /* When current byte is a part of multibyte character, copy all
5781 * bytes of that character. */
5782 if (has_mbyte)
5783 {
5784 int i;
5785 int len = (*mb_ptr2len)(ptr) - 1;
5786
5787 for (i = 0; i < len && n >= 1; ++i, --n)
5788 *p++ = *ptr++;
5789 }
5790#endif
5791 *p++ = *ptr++;
5792 }
5793 *p = NUL;
5794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
5796 /*
5797 * Execute the command.
5798 */
5799 if (cmdchar == '*' || cmdchar == '#')
5800 {
5801 if (!g_cmd && (
5802#ifdef FEAT_MBYTE
5803 has_mbyte ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr)) :
5804#endif
5805 vim_iswordc(ptr[-1])))
5806 STRCAT(buf, "\\>");
5807#ifdef FEAT_CMDHIST
5808 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005809 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5811#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005812 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 }
5814 else
5815 do_cmdline_cmd(buf);
5816
5817 vim_free(buf);
5818}
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820/*
5821 * Get visually selected text, within one line only.
5822 * Returns FAIL if more than one line selected.
5823 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005824 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005825get_visual_text(
5826 cmdarg_T *cap,
5827 char_u **pp, /* return: start of selected text */
5828 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
5830 if (VIsual_mode != 'V')
5831 unadjust_for_sel();
5832 if (VIsual.lnum != curwin->w_cursor.lnum)
5833 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005834 if (cap != NULL)
5835 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 return FAIL;
5837 }
5838 if (VIsual_mode == 'V')
5839 {
5840 *pp = ml_get_curline();
5841 *lenp = (int)STRLEN(*pp);
5842 }
5843 else
5844 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005845 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
5847 *pp = ml_get_pos(&curwin->w_cursor);
5848 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5849 }
5850 else
5851 {
5852 *pp = ml_get_pos(&VIsual);
5853 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5854 }
5855#ifdef FEAT_MBYTE
5856 if (has_mbyte)
5857 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005858 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859#endif
5860 }
5861 reset_VIsual_and_resel();
5862 return OK;
5863}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
5865/*
5866 * CTRL-T: backwards in tag stack
5867 */
5868 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005869nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
5871 if (!checkclearopq(cap->oap))
5872 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5873}
5874
5875/*
5876 * Handle scrolling command 'H', 'L' and 'M'.
5877 */
5878 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005879nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 int used = 0;
5882 long n;
5883#ifdef FEAT_FOLDING
5884 linenr_T lnum;
5885#endif
5886 int half;
5887
5888 cap->oap->motion_type = MLINE;
5889 setpcmark();
5890
5891 if (cap->cmdchar == 'L')
5892 {
5893 validate_botline(); /* make sure curwin->w_botline is valid */
5894 curwin->w_cursor.lnum = curwin->w_botline - 1;
5895 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5896 curwin->w_cursor.lnum = 1;
5897 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005898 {
5899#ifdef FEAT_FOLDING
5900 if (hasAnyFolding(curwin))
5901 {
5902 /* Count a fold for one screen line. */
5903 for (n = cap->count1 - 1; n > 0
5904 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5905 {
5906 (void)hasFolding(curwin->w_cursor.lnum,
5907 &curwin->w_cursor.lnum, NULL);
5908 --curwin->w_cursor.lnum;
5909 }
5910 }
5911 else
5912#endif
5913 curwin->w_cursor.lnum -= cap->count1 - 1;
5914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
5916 else
5917 {
5918 if (cap->cmdchar == 'M')
5919 {
5920#ifdef FEAT_DIFF
5921 /* Don't count filler lines above the window. */
5922 used -= diff_check_fill(curwin, curwin->w_topline)
5923 - curwin->w_topfill;
5924#endif
5925 validate_botline(); /* make sure w_empty_rows is valid */
5926 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5927 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5928 {
5929#ifdef FEAT_DIFF
5930 /* Count half he number of filler lines to be "below this
5931 * line" and half to be "above the next line". */
5932 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5933 + n) / 2 >= half)
5934 {
5935 --n;
5936 break;
5937 }
5938#endif
5939 used += plines(curwin->w_topline + n);
5940 if (used >= half)
5941 break;
5942#ifdef FEAT_FOLDING
5943 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5944 n = lnum - curwin->w_topline;
5945#endif
5946 }
5947 if (n > 0 && used > curwin->w_height)
5948 --n;
5949 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005950 else /* (cap->cmdchar == 'H') */
5951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005953#ifdef FEAT_FOLDING
5954 if (hasAnyFolding(curwin))
5955 {
5956 /* Count a fold for one screen line. */
5957 lnum = curwin->w_topline;
5958 while (n-- > 0 && lnum < curwin->w_botline - 1)
5959 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005960 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005961 ++lnum;
5962 }
5963 n = lnum - curwin->w_topline;
5964 }
5965#endif
5966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 curwin->w_cursor.lnum = curwin->w_topline + n;
5968 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5969 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5970 }
5971
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005972 /* Correct for 'so', except when an operator is pending. */
5973 if (cap->oap->op_type == OP_NOP)
5974 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 beginline(BL_SOL | BL_FIX);
5976}
5977
5978/*
5979 * Cursor right commands.
5980 */
5981 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005982nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005985 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005987 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5988 {
5989 /* <C-Right> and <S-Right> move a word or WORD right */
5990 if (mod_mask & MOD_MASK_CTRL)
5991 cap->arg = TRUE;
5992 nv_wordcmd(cap);
5993 return;
5994 }
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 cap->oap->motion_type = MCHAR;
5997 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005998 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006000#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006002 * In virtual edit mode, there's no such thing as "past_line", as lines
6003 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 */
6005 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006006 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007#endif
6008
6009 for (n = cap->count1; n > 0; --n)
6010 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006011 if ((!past_line && oneright() == FAIL)
6012 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006013 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 {
6015 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006016 * <Space> wraps to next line if 'whichwrap' has 's'.
6017 * 'l' wraps to next line if 'whichwrap' has 'l'.
6018 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 */
6020 if ( ((cap->cmdchar == ' '
6021 && vim_strchr(p_ww, 's') != NULL)
6022 || (cap->cmdchar == 'l'
6023 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006024 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 && vim_strchr(p_ww, '>') != NULL))
6026 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
6027 {
6028 /* When deleting we also count the NL as a character.
6029 * Set cap->oap->inclusive when last char in the line is
6030 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006031 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006033 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 cap->oap->inclusive = TRUE;
6035 else
6036 {
6037 ++curwin->w_cursor.lnum;
6038 curwin->w_cursor.col = 0;
6039#ifdef FEAT_VIRTUALEDIT
6040 curwin->w_cursor.coladd = 0;
6041#endif
6042 curwin->w_set_curswant = TRUE;
6043 cap->oap->inclusive = FALSE;
6044 }
6045 continue;
6046 }
6047 if (cap->oap->op_type == OP_NOP)
6048 {
6049 /* Only beep and flush if not moved at all */
6050 if (n == cap->count1)
6051 beep_flush();
6052 }
6053 else
6054 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006055 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 cap->oap->inclusive = TRUE;
6057 }
6058 break;
6059 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006060 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 {
6062 curwin->w_set_curswant = TRUE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006063#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 if (virtual_active())
6065 oneright();
6066 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006069#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (has_mbyte)
6071 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006072 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 ++curwin->w_cursor.col;
6076 }
6077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 }
6079#ifdef FEAT_FOLDING
6080 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6081 && cap->oap->op_type == OP_NOP)
6082 foldOpenCursor();
6083#endif
6084}
6085
6086/*
6087 * Cursor left commands.
6088 *
6089 * Returns TRUE when operator end should not be adjusted.
6090 */
6091 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006092nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
6094 long n;
6095
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006096 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6097 {
6098 /* <C-Left> and <S-Left> move a word or WORD left */
6099 if (mod_mask & MOD_MASK_CTRL)
6100 cap->arg = 1;
6101 nv_bck_word(cap);
6102 return;
6103 }
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 cap->oap->motion_type = MCHAR;
6106 cap->oap->inclusive = FALSE;
6107 for (n = cap->count1; n > 0; --n)
6108 {
6109 if (oneleft() == FAIL)
6110 {
6111 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6112 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6113 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6114 */
6115 if ( (((cap->cmdchar == K_BS
6116 || cap->cmdchar == Ctrl_H)
6117 && vim_strchr(p_ww, 'b') != NULL)
6118 || (cap->cmdchar == 'h'
6119 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006120 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 && vim_strchr(p_ww, '<') != NULL))
6122 && curwin->w_cursor.lnum > 1)
6123 {
6124 --(curwin->w_cursor.lnum);
6125 coladvance((colnr_T)MAXCOL);
6126 curwin->w_set_curswant = TRUE;
6127
6128 /* When the NL before the first char has to be deleted we
6129 * put the cursor on the NUL after the previous line.
6130 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006131 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if ( (cap->oap->op_type == OP_DELETE
6133 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006134 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006136 char_u *cp = ml_get_cursor();
6137
6138 if (*cp != NUL)
6139 {
6140#ifdef FEAT_MBYTE
6141 if (has_mbyte)
6142 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6143 else
6144#endif
6145 ++curwin->w_cursor.col;
6146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 cap->retval |= CA_NO_ADJ_OP_END;
6148 }
6149 continue;
6150 }
6151 /* Only beep and flush if not moved at all */
6152 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6153 beep_flush();
6154 break;
6155 }
6156 }
6157#ifdef FEAT_FOLDING
6158 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6159 && cap->oap->op_type == OP_NOP)
6160 foldOpenCursor();
6161#endif
6162}
6163
6164/*
6165 * Cursor up commands.
6166 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6167 */
6168 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006169nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006171 if (mod_mask & MOD_MASK_SHIFT)
6172 {
6173 /* <S-Up> is page up */
6174 cap->arg = BACKWARD;
6175 nv_page(cap);
6176 }
6177 else
6178 {
6179 cap->oap->motion_type = MLINE;
6180 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6181 clearopbeep(cap->oap);
6182 else if (cap->arg)
6183 beginline(BL_WHITE | BL_FIX);
6184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185}
6186
6187/*
6188 * Cursor down commands.
6189 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6190 */
6191 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006192nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006194 if (mod_mask & MOD_MASK_SHIFT)
6195 {
6196 /* <S-Down> is page down */
6197 cap->arg = FORWARD;
6198 nv_page(cap);
6199 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006200#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006201 /* Quickfix window only: view the result under the cursor. */
6202 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6203 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
6207#ifdef FEAT_CMDWIN
6208 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006209 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 cmdwin_result = CAR;
6211 else
6212#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006213#ifdef FEAT_JOB_CHANNEL
6214 /* In a prompt buffer a <CR> in the last line invokes the callback. */
6215 if (bt_prompt(curbuf) && cap->cmdchar == CAR
6216 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
6217 {
6218 invoke_prompt_callback();
6219 if (restart_edit == 0)
6220 restart_edit = 'a';
6221 }
6222 else
6223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 {
6225 cap->oap->motion_type = MLINE;
6226 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6227 clearopbeep(cap->oap);
6228 else if (cap->arg)
6229 beginline(BL_WHITE | BL_FIX);
6230 }
6231 }
6232}
6233
6234#ifdef FEAT_SEARCHPATH
6235/*
6236 * Grab the file name under the cursor and edit it.
6237 */
6238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006239nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006242 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006244 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 {
6246 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006247 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 return;
6249 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006250 if (curbuf_locked())
6251 {
6252 clearop(cap->oap);
6253 return;
6254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006256 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257
6258 if (ptr != NULL)
6259 {
6260 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006261 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006262 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006264 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006265 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006266 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006267 {
6268 curwin->w_cursor.lnum = lnum;
6269 check_cursor_lnum();
6270 beginline(BL_SOL | BL_FIX);
6271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 vim_free(ptr);
6273 }
6274 else
6275 clearop(cap->oap);
6276}
6277#endif
6278
6279/*
6280 * <End> command: to end of current line or last line.
6281 */
6282 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006283nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006285 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006287 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 nv_goto(cap);
6289 cap->count1 = 1; /* to end of current line */
6290 }
6291 nv_dollar(cap);
6292}
6293
6294/*
6295 * Handle the "$" command.
6296 */
6297 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006298nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
6300 cap->oap->motion_type = MCHAR;
6301 cap->oap->inclusive = TRUE;
6302#ifdef FEAT_VIRTUALEDIT
6303 /* In virtual mode when off the edge of a line and an operator
6304 * is pending (whew!) keep the cursor where it is.
6305 * Otherwise, send it to the end of the line. */
6306 if (!virtual_active() || gchar_cursor() != NUL
6307 || cap->oap->op_type == OP_NOP)
6308#endif
6309 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6310 if (cursor_down((long)(cap->count1 - 1),
6311 cap->oap->op_type == OP_NOP) == FAIL)
6312 clearopbeep(cap->oap);
6313#ifdef FEAT_FOLDING
6314 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6315 foldOpenCursor();
6316#endif
6317}
6318
6319/*
6320 * Implementation of '?' and '/' commands.
6321 * If cap->arg is TRUE don't set PC mark.
6322 */
6323 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006324nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006327 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328
6329 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6330 {
6331 /* Translate "g??" to "g?g?" */
6332 cap->cmdchar = 'g';
6333 cap->nchar = '?';
6334 nv_operator(cap);
6335 return;
6336 }
6337
Bram Moolenaardda933d2016-09-03 21:04:58 +02006338 /* When using 'incsearch' the cursor may be moved to set a different search
6339 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6341
6342 if (cap->searchbuf == NULL)
6343 {
6344 clearop(oap);
6345 return;
6346 }
6347
Bram Moolenaar46539112015-02-17 15:43:57 +01006348 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006349 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006350 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351}
6352
6353/*
6354 * Handle "N" and "n" commands.
6355 * cap->arg is SEARCH_REV for "N", 0 for "n".
6356 */
6357 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006358nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359{
Bram Moolenaar46539112015-02-17 15:43:57 +01006360 pos_T old = curwin->w_cursor;
6361 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6362
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006363 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006364 {
6365 /* Avoid getting stuck on the current cursor position, which can
6366 * happen when an offset is given and the cursor is on the last char
6367 * in the buffer: Repeat with count + 1. */
6368 cap->count1 += 1;
6369 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6370 cap->count1 -= 1;
6371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372}
6373
6374/*
6375 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6376 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006377 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006379 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006380normal_search(
6381 cmdarg_T *cap,
6382 int dir,
6383 char_u *pat,
6384 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 int i;
6387
6388 cap->oap->motion_type = MCHAR;
6389 cap->oap->inclusive = FALSE;
6390 cap->oap->use_reg_one = TRUE;
6391 curwin->w_set_curswant = TRUE;
6392
6393 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006394 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 if (i == 0)
6396 clearop(cap->oap);
6397 else
6398 {
6399 if (i == 2)
6400 cap->oap->motion_type = MLINE;
6401#ifdef FEAT_VIRTUALEDIT
6402 curwin->w_cursor.coladd = 0;
6403#endif
6404#ifdef FEAT_FOLDING
6405 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6406 foldOpenCursor();
6407#endif
6408 }
6409
6410 /* "/$" will put the cursor after the end of the line, may need to
6411 * correct that here */
6412 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006413 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415
6416/*
6417 * Character search commands.
6418 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6419 * ',' and FALSE for ';'.
6420 * cap->nchar is NUL for ',' and ';' (repeat the search)
6421 */
6422 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006423nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
6425 int t_cmd;
6426
6427 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6428 t_cmd = TRUE;
6429 else
6430 t_cmd = FALSE;
6431
6432 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6434 clearopbeep(cap->oap);
6435 else
6436 {
6437 curwin->w_set_curswant = TRUE;
6438#ifdef FEAT_VIRTUALEDIT
6439 /* Include a Tab for "tx" and for "dfx". */
6440 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6441 && (t_cmd || cap->oap->op_type != OP_NOP))
6442 {
6443 colnr_T scol, ecol;
6444
6445 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6446 curwin->w_cursor.coladd = ecol - scol;
6447 }
6448 else
6449 curwin->w_cursor.coladd = 0;
6450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452#ifdef FEAT_FOLDING
6453 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6454 foldOpenCursor();
6455#endif
6456 }
6457}
6458
6459/*
6460 * "[" and "]" commands.
6461 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6462 */
6463 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006464nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
Bram Moolenaara9d52e32010-07-31 16:44:19 +02006466 pos_T new_pos = INIT_POS_T(0, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 pos_T prev_pos;
6468 pos_T *pos = NULL; /* init for GCC */
6469 pos_T old_pos; /* cursor position before command */
6470 int flag;
6471 long n;
6472 int findc;
6473 int c;
6474
6475 cap->oap->motion_type = MCHAR;
6476 cap->oap->inclusive = FALSE;
6477 old_pos = curwin->w_cursor;
6478#ifdef FEAT_VIRTUALEDIT
6479 curwin->w_cursor.coladd = 0; /* TODO: don't do this for an error. */
6480#endif
6481
6482#ifdef FEAT_SEARCHPATH
6483 /*
6484 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6485 */
6486 if (cap->nchar == 'f')
6487 nv_gotofile(cap);
6488 else
6489#endif
6490
6491#ifdef FEAT_FIND_ID
6492 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006493 * Find the occurrence(s) of the identifier or define under cursor
6494 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 *
6496 * search list jump
6497 * fwd bwd fwd bwd fwd bwd
6498 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6499 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6500 */
6501 if (vim_strchr((char_u *)
6502#ifdef EBCDIC
6503 "iI\005dD\067",
6504#else
6505 "iI\011dD\004",
6506#endif
6507 cap->nchar) != NULL)
6508 {
6509 char_u *ptr;
6510 int len;
6511
6512 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6513 clearop(cap->oap);
6514 else
6515 {
6516 find_pattern_in_path(ptr, 0, len, TRUE,
6517 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6518 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6519 cap->count1,
6520 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6521 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6522 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6523 (linenr_T)MAXLNUM);
6524 curwin->w_set_curswant = TRUE;
6525 }
6526 }
6527 else
6528#endif
6529
6530 /*
6531 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6532 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6533 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6534 * "[m" or "]m" search for prev/next start of (Java) method.
6535 * "[M" or "]M" search for prev/next end of (Java) method.
6536 */
6537 if ( (cap->cmdchar == '['
6538 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6539 || (cap->cmdchar == ']'
6540 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6541 {
6542 if (cap->nchar == '*')
6543 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 prev_pos.lnum = 0;
6545 if (cap->nchar == 'm' || cap->nchar == 'M')
6546 {
6547 if (cap->cmdchar == '[')
6548 findc = '{';
6549 else
6550 findc = '}';
6551 n = 9999;
6552 }
6553 else
6554 {
6555 findc = cap->nchar;
6556 n = cap->count1;
6557 }
6558 for ( ; n > 0; --n)
6559 {
6560 if ((pos = findmatchlimit(cap->oap, findc,
6561 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6562 {
6563 if (new_pos.lnum == 0) /* nothing found */
6564 {
6565 if (cap->nchar != 'm' && cap->nchar != 'M')
6566 clearopbeep(cap->oap);
6567 }
6568 else
6569 pos = &new_pos; /* use last one found */
6570 break;
6571 }
6572 prev_pos = new_pos;
6573 curwin->w_cursor = *pos;
6574 new_pos = *pos;
6575 }
6576 curwin->w_cursor = old_pos;
6577
6578 /*
6579 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6580 * brought us to the match for "[m" and "]M" when inside a method.
6581 * Try finding the '{' or '}' we want to be at.
6582 * Also repeat for the given count.
6583 */
6584 if (cap->nchar == 'm' || cap->nchar == 'M')
6585 {
6586 /* norm is TRUE for "]M" and "[m" */
6587 int norm = ((findc == '{') == (cap->nchar == 'm'));
6588
6589 n = cap->count1;
6590 /* found a match: we were inside a method */
6591 if (prev_pos.lnum != 0)
6592 {
6593 pos = &prev_pos;
6594 curwin->w_cursor = prev_pos;
6595 if (norm)
6596 --n;
6597 }
6598 else
6599 pos = NULL;
6600 while (n > 0)
6601 {
6602 for (;;)
6603 {
6604 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6605 {
6606 /* if not found anything, that's an error */
6607 if (pos == NULL)
6608 clearopbeep(cap->oap);
6609 n = 0;
6610 break;
6611 }
6612 c = gchar_cursor();
6613 if (c == '{' || c == '}')
6614 {
6615 /* Must have found end/start of class: use it.
6616 * Or found the place to be at. */
6617 if ((c == findc && norm) || (n == 1 && !norm))
6618 {
6619 new_pos = curwin->w_cursor;
6620 pos = &new_pos;
6621 n = 0;
6622 }
6623 /* if no match found at all, we started outside of the
6624 * class and we're inside now. Just go on. */
6625 else if (new_pos.lnum == 0)
6626 {
6627 new_pos = curwin->w_cursor;
6628 pos = &new_pos;
6629 }
6630 /* found start/end of other method: go to match */
6631 else if ((pos = findmatchlimit(cap->oap, findc,
6632 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6633 0)) == NULL)
6634 n = 0;
6635 else
6636 curwin->w_cursor = *pos;
6637 break;
6638 }
6639 }
6640 --n;
6641 }
6642 curwin->w_cursor = old_pos;
6643 if (pos == NULL && new_pos.lnum != 0)
6644 clearopbeep(cap->oap);
6645 }
6646 if (pos != NULL)
6647 {
6648 setpcmark();
6649 curwin->w_cursor = *pos;
6650 curwin->w_set_curswant = TRUE;
6651#ifdef FEAT_FOLDING
6652 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6653 && cap->oap->op_type == OP_NOP)
6654 foldOpenCursor();
6655#endif
6656 }
6657 }
6658
6659 /*
6660 * "[[", "[]", "]]" and "][": move to start or end of function
6661 */
6662 else if (cap->nchar == '[' || cap->nchar == ']')
6663 {
6664 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6665 flag = '{';
6666 else
6667 flag = '}'; /* "][" or "[]" */
6668
6669 curwin->w_set_curswant = TRUE;
6670 /*
6671 * Imitate strange Vi behaviour: When using "]]" with an operator
6672 * we also stop at '}'.
6673 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006674 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 (cap->oap->op_type != OP_NOP
6676 && cap->arg == FORWARD && flag == '{')))
6677 clearopbeep(cap->oap);
6678 else
6679 {
6680 if (cap->oap->op_type == OP_NOP)
6681 beginline(BL_WHITE | BL_FIX);
6682#ifdef FEAT_FOLDING
6683 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6684 foldOpenCursor();
6685#endif
6686 }
6687 }
6688
6689 /*
6690 * "[p", "[P", "]P" and "]p": put with indent adjustment
6691 */
6692 else if (cap->nchar == 'p' || cap->nchar == 'P')
6693 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006694 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006696 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6697 ? FORWARD : BACKWARD;
6698 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006699 int was_visual = VIsual_active;
6700 int line_count = curbuf->b_ml.ml_line_count;
6701 pos_T start, end;
6702
6703 if (VIsual_active)
6704 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006705 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006706 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006707 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006708 curwin->w_cursor = (dir == BACKWARD ? start : end);
6709 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006710# ifdef FEAT_CLIPBOARD
6711 adjust_clip_reg(&regname);
6712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006714
6715 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006716 if (was_visual)
6717 {
6718 VIsual = start;
6719 curwin->w_cursor = end;
6720 if (dir == BACKWARD)
6721 {
6722 /* adjust lines */
6723 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6724 curwin->w_cursor.lnum +=
6725 curbuf->b_ml.ml_line_count - line_count;
6726 }
6727
6728 VIsual_active = TRUE;
6729 if (VIsual_mode == 'V')
6730 {
6731 /* delete visually selected lines */
6732 cap->cmdchar = 'd';
6733 cap->nchar = NUL;
6734 cap->oap->regname = regname;
6735 nv_operator(cap);
6736 do_pending_operator(cap, 0, FALSE);
6737 }
6738 if (VIsual_active)
6739 {
6740 end_visual_mode();
6741 redraw_later(SOME_VALID);
6742 }
6743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 }
6745 }
6746
6747 /*
6748 * "['", "[`", "]'" and "]`": jump to next mark
6749 */
6750 else if (cap->nchar == '\'' || cap->nchar == '`')
6751 {
6752 pos = &curwin->w_cursor;
6753 for (n = cap->count1; n > 0; --n)
6754 {
6755 prev_pos = *pos;
6756 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6757 cap->nchar == '\'');
6758 if (pos == NULL)
6759 break;
6760 }
6761 if (pos == NULL)
6762 pos = &prev_pos;
6763 nv_cursormark(cap, cap->nchar == '\'', pos);
6764 }
6765
6766#ifdef FEAT_MOUSE
6767 /*
6768 * [ or ] followed by a middle mouse click: put selected text with
6769 * indent adjustment. Any other button just does as usual.
6770 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006771 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
6773 (void)do_mouse(cap->oap, cap->nchar,
6774 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6775 cap->count1, PUT_FIXINDENT);
6776 }
6777#endif /* FEAT_MOUSE */
6778
6779#ifdef FEAT_FOLDING
6780 /*
6781 * "[z" and "]z": move to start or end of open fold.
6782 */
6783 else if (cap->nchar == 'z')
6784 {
6785 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6786 cap->count1) == FAIL)
6787 clearopbeep(cap->oap);
6788 }
6789#endif
6790
6791#ifdef FEAT_DIFF
6792 /*
6793 * "[c" and "]c": move to next or previous diff-change.
6794 */
6795 else if (cap->nchar == 'c')
6796 {
6797 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6798 cap->count1) == FAIL)
6799 clearopbeep(cap->oap);
6800 }
6801#endif
6802
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006803#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006804 /*
6805 * "[s", "[S", "]s" and "]S": move to next spell error.
6806 */
6807 else if (cap->nchar == 's' || cap->nchar == 'S')
6808 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006809 setpcmark();
6810 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006811 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6812 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006813 {
6814 clearopbeep(cap->oap);
6815 break;
6816 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006817 else
6818 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006819# ifdef FEAT_FOLDING
6820 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6821 foldOpenCursor();
6822# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006823 }
6824#endif
6825
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 /* Not a valid cap->nchar. */
6827 else
6828 clearopbeep(cap->oap);
6829}
6830
6831/*
6832 * Handle Normal mode "%" command.
6833 */
6834 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006835nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
6837 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006838#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 linenr_T lnum = curwin->w_cursor.lnum;
6840#endif
6841
6842 cap->oap->inclusive = TRUE;
6843 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6844 {
6845 if (cap->count0 > 100)
6846 clearopbeep(cap->oap);
6847 else
6848 {
6849 cap->oap->motion_type = MLINE;
6850 setpcmark();
6851 /* Round up, so CTRL-G will give same value. Watch out for a
6852 * large line count, the line number must not go negative! */
6853 if (curbuf->b_ml.ml_line_count > 1000000)
6854 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6855 / 100L * cap->count0;
6856 else
6857 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6858 cap->count0 + 99L) / 100L;
6859 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6860 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6861 beginline(BL_SOL | BL_FIX);
6862 }
6863 }
6864 else /* "%" : go to matching paren */
6865 {
6866 cap->oap->motion_type = MCHAR;
6867 cap->oap->use_reg_one = TRUE;
6868 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6869 clearopbeep(cap->oap);
6870 else
6871 {
6872 setpcmark();
6873 curwin->w_cursor = *pos;
6874 curwin->w_set_curswant = TRUE;
6875#ifdef FEAT_VIRTUALEDIT
6876 curwin->w_cursor.coladd = 0;
6877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 }
6880 }
6881#ifdef FEAT_FOLDING
6882 if (cap->oap->op_type == OP_NOP
6883 && lnum != curwin->w_cursor.lnum
6884 && (fdo_flags & FDO_PERCENT)
6885 && KeyTyped)
6886 foldOpenCursor();
6887#endif
6888}
6889
6890/*
6891 * Handle "(" and ")" commands.
6892 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6893 */
6894 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006895nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
6897 cap->oap->motion_type = MCHAR;
6898 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006899 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6900 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 curwin->w_set_curswant = TRUE;
6902
6903 if (findsent(cap->arg, cap->count1) == FAIL)
6904 clearopbeep(cap->oap);
6905 else
6906 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006907 /* Don't leave the cursor on the NUL past end of line. */
6908 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909#ifdef FEAT_VIRTUALEDIT
6910 curwin->w_cursor.coladd = 0;
6911#endif
6912#ifdef FEAT_FOLDING
6913 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6914 foldOpenCursor();
6915#endif
6916 }
6917}
6918
6919/*
6920 * "m" command: Mark a position.
6921 */
6922 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006923nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 if (!checkclearop(cap->oap))
6926 {
6927 if (setmark(cap->nchar) == FAIL)
6928 clearopbeep(cap->oap);
6929 }
6930}
6931
6932/*
6933 * "{" and "}" commands.
6934 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6935 */
6936 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006937nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 cap->oap->motion_type = MCHAR;
6940 cap->oap->inclusive = FALSE;
6941 cap->oap->use_reg_one = TRUE;
6942 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006943 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 clearopbeep(cap->oap);
6945 else
6946 {
6947#ifdef FEAT_VIRTUALEDIT
6948 curwin->w_cursor.coladd = 0;
6949#endif
6950#ifdef FEAT_FOLDING
6951 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6952 foldOpenCursor();
6953#endif
6954 }
6955}
6956
6957/*
6958 * "u" command: Undo or make lower case.
6959 */
6960 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006961nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006963 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
6965 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6966 cap->cmdchar = 'g';
6967 cap->nchar = 'u';
6968 nv_operator(cap);
6969 }
6970 else
6971 nv_kundo(cap);
6972}
6973
6974/*
6975 * <Undo> command.
6976 */
6977 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006978nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
6980 if (!checkclearopq(cap->oap))
6981 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02006982#ifdef FEAT_JOB_CHANNEL
6983 if (bt_prompt(curbuf))
6984 {
6985 clearopbeep(cap->oap);
6986 return;
6987 }
6988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 u_undo((int)cap->count1);
6990 curwin->w_set_curswant = TRUE;
6991 }
6992}
6993
6994/*
6995 * Handle the "r" command.
6996 */
6997 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006998nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999{
7000 char_u *ptr;
7001 int had_ctrl_v;
7002 long n;
7003
7004 if (checkclearop(cap->oap))
7005 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02007006#ifdef FEAT_JOB_CHANNEL
7007 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7008 {
7009 clearopbeep(cap->oap);
7010 return;
7011 }
7012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013
7014 /* get another character */
7015 if (cap->nchar == Ctrl_V)
7016 {
7017 had_ctrl_v = Ctrl_V;
7018 cap->nchar = get_literal();
7019 /* Don't redo a multibyte character with CTRL-V. */
7020 if (cap->nchar > DEL)
7021 had_ctrl_v = NUL;
7022 }
7023 else
7024 had_ctrl_v = NUL;
7025
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007026 /* Abort if the character is a special key. */
7027 if (IS_SPECIAL(cap->nchar))
7028 {
7029 clearopbeep(cap->oap);
7030 return;
7031 }
7032
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 /* Visual mode "r" */
7034 if (VIsual_active)
7035 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00007036 if (got_int)
7037 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01007038 if (had_ctrl_v)
7039 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01007040 /* Use a special (negative) number to make a difference between a
7041 * literal CR or NL and a line break. */
7042 if (cap->nchar == CAR)
7043 cap->nchar = REPLACE_CR_NCHAR;
7044 else if (cap->nchar == NL)
7045 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01007046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 nv_operator(cap);
7048 return;
7049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
7051#ifdef FEAT_VIRTUALEDIT
7052 /* Break tabs, etc. */
7053 if (virtual_active())
7054 {
7055 if (u_save_cursor() == FAIL)
7056 return;
7057 if (gchar_cursor() == NUL)
7058 {
7059 /* Add extra space and put the cursor on the first one. */
7060 coladvance_force((colnr_T)(getviscol() + cap->count1));
7061 curwin->w_cursor.col -= cap->count1;
7062 }
7063 else if (gchar_cursor() == TAB)
7064 coladvance_force(getviscol());
7065 }
7066#endif
7067
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007068 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00007070 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071#ifdef FEAT_MBYTE
7072 || (has_mbyte && mb_charlen(ptr) < cap->count1)
7073#endif
7074 )
7075 {
7076 clearopbeep(cap->oap);
7077 return;
7078 }
7079
7080 /*
7081 * Replacing with a TAB is done by edit() when it is complicated because
7082 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
7083 * Other characters are done below to avoid problems with things like
7084 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
7085 */
7086 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
7087 {
7088 stuffnumReadbuff(cap->count1);
7089 stuffcharReadbuff('R');
7090 stuffcharReadbuff('\t');
7091 stuffcharReadbuff(ESC);
7092 return;
7093 }
7094
7095 /* save line for undo */
7096 if (u_save_cursor() == FAIL)
7097 return;
7098
7099 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
7100 {
7101 /*
7102 * Replace character(s) by a single newline.
7103 * Strange vi behaviour: Only one newline is inserted.
7104 * Delete the characters here.
7105 * Insert the newline with an insert command, takes care of
7106 * autoindent. The insert command depends on being on the last
7107 * character of a line or not.
7108 */
7109#ifdef FEAT_MBYTE
7110 (void)del_chars(cap->count1, FALSE); /* delete the characters */
7111#else
Bram Moolenaarda1b1a72005-12-18 21:59:16 +00007112 (void)del_bytes(cap->count1, FALSE, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113#endif
7114 stuffcharReadbuff('\r');
7115 stuffcharReadbuff(ESC);
7116
7117 /* Give 'r' to edit(), to get the redo command right. */
7118 invoke_edit(cap, TRUE, 'r', FALSE);
7119 }
7120 else
7121 {
7122 prep_redo(cap->oap->regname, cap->count1,
7123 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7124
7125 curbuf->b_op_start = curwin->w_cursor;
7126#ifdef FEAT_MBYTE
7127 if (has_mbyte)
7128 {
7129 int old_State = State;
7130
7131 if (cap->ncharC1 != 0)
7132 AppendCharToRedobuff(cap->ncharC1);
7133 if (cap->ncharC2 != 0)
7134 AppendCharToRedobuff(cap->ncharC2);
7135
7136 /* This is slow, but it handles replacing a single-byte with a
7137 * multi-byte and the other way around. Also handles adding
7138 * composing characters for utf-8. */
7139 for (n = cap->count1; n > 0; --n)
7140 {
7141 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007142 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7143 {
7144 int c = ins_copychar(curwin->w_cursor.lnum
7145 + (cap->nchar == Ctrl_Y ? -1 : 1));
7146 if (c != NUL)
7147 ins_char(c);
7148 else
7149 /* will be decremented further down */
7150 ++curwin->w_cursor.col;
7151 }
7152 else
7153 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 State = old_State;
7155 if (cap->ncharC1 != 0)
7156 ins_char(cap->ncharC1);
7157 if (cap->ncharC2 != 0)
7158 ins_char(cap->ncharC2);
7159 }
7160 }
7161 else
7162#endif
7163 {
7164 /*
7165 * Replace the characters within one line.
7166 */
7167 for (n = cap->count1; n > 0; --n)
7168 {
7169 /*
7170 * Get ptr again, because u_save and/or showmatch() will have
7171 * released the line. At the same time we let know that the
7172 * line will be changed.
7173 */
7174 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007175 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7176 {
7177 int c = ins_copychar(curwin->w_cursor.lnum
7178 + (cap->nchar == Ctrl_Y ? -1 : 1));
7179 if (c != NUL)
7180 ptr[curwin->w_cursor.col] = c;
7181 }
7182 else
7183 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 if (p_sm && msg_silent == 0)
7185 showmatch(cap->nchar);
7186 ++curwin->w_cursor.col;
7187 }
7188#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007189 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007191 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaar009b2592004-10-24 19:18:58 +00007193 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007194 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007196 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 }
7198#endif
7199
7200 /* mark the buffer as changed and prepare for displaying */
7201 changed_bytes(curwin->w_cursor.lnum,
7202 (colnr_T)(curwin->w_cursor.col - cap->count1));
7203 }
7204 --curwin->w_cursor.col; /* cursor on the last replaced char */
7205#ifdef FEAT_MBYTE
7206 /* if the character on the left of the current cursor is a multi-byte
7207 * character, move two characters left */
7208 if (has_mbyte)
7209 mb_adjust_cursor();
7210#endif
7211 curbuf->b_op_end = curwin->w_cursor;
7212 curwin->w_set_curswant = TRUE;
7213 set_last_insert(cap->nchar);
7214 }
7215}
7216
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217/*
7218 * 'o': Exchange start and end of Visual area.
7219 * 'O': same, but in block mode exchange left and right corners.
7220 */
7221 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007222v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 pos_T old_cursor;
7225 colnr_T left, right;
7226
7227 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7228 {
7229 old_cursor = curwin->w_cursor;
7230 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7231 curwin->w_cursor.lnum = VIsual.lnum;
7232 coladvance(left);
7233 VIsual = curwin->w_cursor;
7234
7235 curwin->w_cursor.lnum = old_cursor.lnum;
7236 curwin->w_curswant = right;
7237 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7238 * right one column */
7239 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7240 ++curwin->w_curswant;
7241 coladvance(curwin->w_curswant);
7242 if (curwin->w_cursor.col == old_cursor.col
7243#ifdef FEAT_VIRTUALEDIT
7244 && (!virtual_active()
7245 || curwin->w_cursor.coladd == old_cursor.coladd)
7246#endif
7247 )
7248 {
7249 curwin->w_cursor.lnum = VIsual.lnum;
7250 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7251 ++right;
7252 coladvance(right);
7253 VIsual = curwin->w_cursor;
7254
7255 curwin->w_cursor.lnum = old_cursor.lnum;
7256 coladvance(left);
7257 curwin->w_curswant = left;
7258 }
7259 }
7260 else
7261 {
7262 old_cursor = curwin->w_cursor;
7263 curwin->w_cursor = VIsual;
7264 VIsual = old_cursor;
7265 curwin->w_set_curswant = TRUE;
7266 }
7267}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269/*
7270 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7271 */
7272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007273nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 if (VIsual_active) /* "R" is replace lines */
7276 {
7277 cap->cmdchar = 'c';
7278 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007279 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 VIsual_mode = 'V';
7281 nv_operator(cap);
7282 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007283 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 {
7285 if (!curbuf->b_p_ma)
7286 EMSG(_(e_modifiable));
7287 else
7288 {
7289#ifdef FEAT_VIRTUALEDIT
7290 if (virtual_active())
7291 coladvance(getviscol());
7292#endif
7293 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7294 }
7295 }
7296}
7297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298/*
7299 * "gr".
7300 */
7301 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007302nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 if (VIsual_active)
7305 {
7306 cap->cmdchar = 'r';
7307 cap->nchar = cap->extra_char;
7308 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7309 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007310 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 {
7312 if (!curbuf->b_p_ma)
7313 EMSG(_(e_modifiable));
7314 else
7315 {
7316 if (cap->extra_char == Ctrl_V) /* get another character */
7317 cap->extra_char = get_literal();
7318 stuffcharReadbuff(cap->extra_char);
7319 stuffcharReadbuff(ESC);
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02007320#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 if (virtual_active())
7322 coladvance(getviscol());
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02007323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 invoke_edit(cap, TRUE, 'v', FALSE);
7325 }
7326 }
7327}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
7329/*
7330 * Swap case for "~" command, when it does not work like an operator.
7331 */
7332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007333n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
7335 long n;
7336 pos_T startpos;
7337 int did_change = 0;
7338#ifdef FEAT_NETBEANS_INTG
7339 pos_T pos;
7340 char_u *ptr;
7341 int count;
7342#endif
7343
7344 if (checkclearopq(cap->oap))
7345 return;
7346
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007347 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 {
7349 clearopbeep(cap->oap);
7350 return;
7351 }
7352
7353 prep_redo_cmd(cap);
7354
7355 if (u_save_cursor() == FAIL)
7356 return;
7357
7358 startpos = curwin->w_cursor;
7359#ifdef FEAT_NETBEANS_INTG
7360 pos = startpos;
7361#endif
7362 for (n = cap->count1; n > 0; --n)
7363 {
7364 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7365 inc_cursor();
7366 if (gchar_cursor() == NUL)
7367 {
7368 if (vim_strchr(p_ww, '~') != NULL
7369 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7370 {
7371#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007372 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 {
7374 if (did_change)
7375 {
7376 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007377 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007378 netbeans_removed(curbuf, pos.lnum, pos.col,
7379 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007381 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 }
7383 pos.col = 0;
7384 pos.lnum++;
7385 }
7386#endif
7387 ++curwin->w_cursor.lnum;
7388 curwin->w_cursor.col = 0;
7389 if (n > 1)
7390 {
7391 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7392 break;
7393 u_clearline();
7394 }
7395 }
7396 else
7397 break;
7398 }
7399 }
7400#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007401 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 {
7403 ptr = ml_get(pos.lnum);
7404 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007405 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7406 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 }
7408#endif
7409
7410
7411 check_cursor();
7412 curwin->w_set_curswant = TRUE;
7413 if (did_change)
7414 {
7415 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7416 0L);
7417 curbuf->b_op_start = startpos;
7418 curbuf->b_op_end = curwin->w_cursor;
7419 if (curbuf->b_op_end.col > 0)
7420 --curbuf->b_op_end.col;
7421 }
7422}
7423
7424/*
7425 * Move cursor to mark.
7426 */
7427 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007428nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
7430 if (check_mark(pos) == FAIL)
7431 clearop(cap->oap);
7432 else
7433 {
7434 if (cap->cmdchar == '\''
7435 || cap->cmdchar == '`'
7436 || cap->cmdchar == '['
7437 || cap->cmdchar == ']')
7438 setpcmark();
7439 curwin->w_cursor = *pos;
7440 if (flag)
7441 beginline(BL_WHITE | BL_FIX);
7442 else
7443 check_cursor();
7444 }
7445 cap->oap->motion_type = flag ? MLINE : MCHAR;
7446 if (cap->cmdchar == '`')
7447 cap->oap->use_reg_one = TRUE;
7448 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7449 curwin->w_set_curswant = TRUE;
7450}
7451
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452/*
7453 * Handle commands that are operators in Visual mode.
7454 */
7455 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007456v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457{
7458 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7459
7460 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007461 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 if (isupper(cap->cmdchar))
7463 {
7464 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007465 {
7466 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7470 curwin->w_curswant = MAXCOL;
7471 }
7472 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7473 nv_operator(cap);
7474}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476/*
7477 * "s" and "S" commands.
7478 */
7479 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007480nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007482#ifdef FEAT_TERMINAL
7483 /* When showing output of term_dumpdiff() swap the top and botom. */
7484 if (term_swap_diff() == OK)
7485 return;
7486#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02007487#ifdef FEAT_JOB_CHANNEL
7488 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7489 {
7490 clearopbeep(cap->oap);
7491 return;
7492 }
7493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7495 {
7496 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007497 {
7498 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 cap->cmdchar = 'c';
7502 nv_operator(cap);
7503 }
7504 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 nv_optrans(cap);
7506}
7507
7508/*
7509 * Abbreviated commands.
7510 */
7511 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007512nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513{
7514 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7515 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7516
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 /* in Visual mode these commands are operators */
7518 if (VIsual_active)
7519 v_visop(cap);
7520 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 nv_optrans(cap);
7522}
7523
7524/*
7525 * Translate a command into another command.
7526 */
7527 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007528nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7531 (char_u *)"d$", (char_u *)"c$",
7532 (char_u *)"cl", (char_u *)"cc",
7533 (char_u *)"yy", (char_u *)":s\r"};
7534 static char_u *str = (char_u *)"xXDCsSY&";
7535
7536 if (!checkclearopq(cap->oap))
7537 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007538 /* In Vi "2D" doesn't delete the next line. Can't translate it
7539 * either, because "2." should also not use the count. */
7540 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7541 {
7542 cap->oap->start = curwin->w_cursor;
7543 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007544#ifdef FEAT_EVAL
7545 set_op_var(OP_DELETE);
7546#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007547 cap->count1 = 1;
7548 nv_dollar(cap);
7549 finish_op = TRUE;
7550 ResetRedobuff();
7551 AppendCharToRedobuff('D');
7552 }
7553 else
7554 {
7555 if (cap->count0)
7556 stuffnumReadbuff(cap->count0);
7557 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 }
7560 cap->opcount = 0;
7561}
7562
7563/*
7564 * "'" and "`" commands. Also for "g'" and "g`".
7565 * cap->arg is TRUE for "'" and "g'".
7566 */
7567 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007568nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569{
7570 pos_T *pos;
7571 int c;
7572#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007573 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7575#endif
7576
7577 if (cap->cmdchar == 'g')
7578 c = cap->extra_char;
7579 else
7580 c = cap->nchar;
7581 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7582 if (pos == (pos_T *)-1) /* jumped to other file */
7583 {
7584 if (cap->arg)
7585 {
7586 check_cursor_lnum();
7587 beginline(BL_WHITE | BL_FIX);
7588 }
7589 else
7590 check_cursor();
7591 }
7592 else
7593 nv_cursormark(cap, cap->arg, pos);
7594
7595#ifdef FEAT_VIRTUALEDIT
7596 /* May need to clear the coladd that a mark includes. */
7597 if (!virtual_active())
7598 curwin->w_cursor.coladd = 0;
7599#endif
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007600 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601#ifdef FEAT_FOLDING
7602 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007603 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007604 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 && (fdo_flags & FDO_MARK)
7606 && old_KeyTyped)
7607 foldOpenCursor();
7608#endif
7609}
7610
7611/*
7612 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7613 */
7614 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007615nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
7617#ifdef FEAT_JUMPLIST
7618 pos_T *pos;
7619# ifdef FEAT_FOLDING
7620 linenr_T lnum = curwin->w_cursor.lnum;
7621 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7622# endif
7623
7624 if (!checkclearopq(cap->oap))
7625 {
7626 if (cap->cmdchar == 'g')
7627 pos = movechangelist((int)cap->count1);
7628 else
7629 pos = movemark((int)cap->count1);
7630 if (pos == (pos_T *)-1) /* jump to other file */
7631 {
7632 curwin->w_set_curswant = TRUE;
7633 check_cursor();
7634 }
7635 else if (pos != NULL) /* can jump */
7636 nv_cursormark(cap, FALSE, pos);
7637 else if (cap->cmdchar == 'g')
7638 {
7639 if (curbuf->b_changelistlen == 0)
7640 EMSG(_("E664: changelist is empty"));
7641 else if (cap->count1 < 0)
7642 EMSG(_("E662: At start of changelist"));
7643 else
7644 EMSG(_("E663: At end of changelist"));
7645 }
7646 else
7647 clearopbeep(cap->oap);
7648# ifdef FEAT_FOLDING
7649 if (cap->oap->op_type == OP_NOP
7650 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7651 && (fdo_flags & FDO_MARK)
7652 && old_KeyTyped)
7653 foldOpenCursor();
7654# endif
7655 }
7656#else
7657 clearopbeep(cap->oap);
7658#endif
7659}
7660
7661/*
7662 * Handle '"' command.
7663 */
7664 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007665nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
7667 if (checkclearop(cap->oap))
7668 return;
7669#ifdef FEAT_EVAL
7670 if (cap->nchar == '=')
7671 cap->nchar = get_expr_register();
7672#endif
7673 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7674 {
7675 cap->oap->regname = cap->nchar;
7676 cap->opcount = cap->count0; /* remember count before '"' */
7677#ifdef FEAT_EVAL
7678 set_reg_var(cap->oap->regname);
7679#endif
7680 }
7681 else
7682 clearopbeep(cap->oap);
7683}
7684
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685/*
7686 * Handle "v", "V" and "CTRL-V" commands.
7687 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7688 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007689 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 */
7691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007692nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007694 if (cap->cmdchar == Ctrl_Q)
7695 cap->cmdchar = Ctrl_V;
7696
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7698 * characterwise, linewise, or blockwise. */
7699 if (cap->oap->op_type != OP_NOP)
7700 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007701 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 finish_op = FALSE; /* operator doesn't finish now but later */
7703 return;
7704 }
7705
7706 VIsual_select = cap->arg;
7707 if (VIsual_active) /* change Visual mode */
7708 {
7709 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7710 end_visual_mode();
7711 else /* toggle char/block mode */
7712 { /* or char/line mode */
7713 VIsual_mode = cap->cmdchar;
7714 showmode();
7715 }
7716 redraw_curbuf_later(INVERTED); /* update the inversion */
7717 }
7718 else /* start Visual mode */
7719 {
7720 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007721 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007723 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 VIsual = curwin->w_cursor;
7725
7726 VIsual_active = TRUE;
7727 VIsual_reselect = TRUE;
7728 if (!cap->arg)
7729 /* start Select mode when 'selectmode' contains "cmd" */
7730 may_start_select('c');
7731#ifdef FEAT_MOUSE
7732 setmouse();
7733#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007734 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 redraw_cmdline = TRUE; /* show visual mode later */
7736 /*
7737 * For V and ^V, we multiply the number of lines even if there
7738 * was only one -- webb
7739 */
7740 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7741 {
7742 curwin->w_cursor.lnum +=
7743 resel_VIsual_line_count * cap->count0 - 1;
7744 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7745 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7746 }
7747 VIsual_mode = resel_VIsual_mode;
7748 if (VIsual_mode == 'v')
7749 {
7750 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007751 {
7752 validate_virtcol();
7753 curwin->w_curswant = curwin->w_virtcol
7754 + resel_VIsual_vcol * cap->count0 - 1;
7755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007757 curwin->w_curswant = resel_VIsual_vcol;
7758 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007760 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {
7762 curwin->w_curswant = MAXCOL;
7763 coladvance((colnr_T)MAXCOL);
7764 }
7765 else if (VIsual_mode == Ctrl_V)
7766 {
7767 validate_virtcol();
7768 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007769 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 coladvance(curwin->w_curswant);
7771 }
7772 else
7773 curwin->w_set_curswant = TRUE;
7774 redraw_curbuf_later(INVERTED); /* show the inversion */
7775 }
7776 else
7777 {
7778 if (!cap->arg)
7779 /* start Select mode when 'selectmode' contains "cmd" */
7780 may_start_select('c');
7781 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007782 if (VIsual_mode != 'V' && *p_sel == 'e')
7783 ++cap->count1; /* include one more char */
7784 if (cap->count0 > 0 && --cap->count1 > 0)
7785 {
7786 /* With a count select that many characters or lines. */
7787 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7788 nv_right(cap);
7789 else if (VIsual_mode == 'V')
7790 nv_down(cap);
7791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793 }
7794}
7795
7796/*
7797 * Start selection for Shift-movement keys.
7798 */
7799 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007800start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802 /* if 'selectmode' contains "key", start Select mode */
7803 may_start_select('k');
7804 n_start_visual_mode('v');
7805}
7806
7807/*
7808 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7809 */
7810 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007811may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 VIsual_select = (stuff_empty() && typebuf_typed()
7814 && (vim_strchr(p_slm, c) != NULL));
7815}
7816
7817/*
7818 * Start Visual mode "c".
7819 * Should set VIsual_select before calling this.
7820 */
7821 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007822n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007824#ifdef FEAT_CONCEAL
7825 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007826 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007827#endif
7828
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 VIsual_mode = c;
7830 VIsual_active = TRUE;
7831 VIsual_reselect = TRUE;
7832#ifdef FEAT_VIRTUALEDIT
7833 /* Corner case: the 0 position in a tab may change when going into
7834 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7835 */
7836 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007837 {
7838 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841#endif
7842 VIsual = curwin->w_cursor;
7843
7844#ifdef FEAT_FOLDING
7845 foldAdjustVisual();
7846#endif
7847
7848#ifdef FEAT_MOUSE
7849 setmouse();
7850#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007851#ifdef FEAT_CONCEAL
7852 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007853 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007854#endif
7855
Bram Moolenaar09df3122006-01-23 22:23:09 +00007856 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 redraw_cmdline = TRUE; /* show visual mode later */
7858#ifdef FEAT_CLIPBOARD
7859 /* Make sure the clipboard gets updated. Needed because start and
7860 * end may still be the same, and the selection needs to be owned */
7861 clip_star.vmode = NUL;
7862#endif
7863
7864 /* Only need to redraw this line, unless still need to redraw an old
7865 * Visual area (when 'lazyredraw' is set). */
7866 if (curwin->w_redr_type < INVERTED)
7867 {
7868 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7869 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7870 }
7871}
7872
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873
7874/*
7875 * CTRL-W: Window commands
7876 */
7877 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007878nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007880 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007881 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007882 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007883 cap->cmdchar = ':';
7884 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007885 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007886 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007887 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891/*
7892 * CTRL-Z: Suspend
7893 */
7894 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007895nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (VIsual_active)
7899 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 do_cmdline_cmd((char_u *)"st");
7901}
7902
7903/*
7904 * Commands starting with "g".
7905 */
7906 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007907nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
7909 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 int i;
7912 int flag = FALSE;
7913
7914 switch (cap->nchar)
7915 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007916 case Ctrl_A:
7917 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918#ifdef MEM_PROFILE
7919 /*
7920 * "g^A": dump log of used memory.
7921 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007922 if (!VIsual_active && cap->nchar == Ctrl_A)
7923 vim_mem_profile_dump();
7924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007926 /*
7927 * "g^A/g^X": sequentially increment visually selected region
7928 */
7929 if (VIsual_active)
7930 {
7931 cap->arg = TRUE;
7932 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007933 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007934 nv_addsub(cap);
7935 }
7936 else
7937 clearopbeep(oap);
7938 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 /*
7941 * "gR": Enter virtual replace mode.
7942 */
7943 case 'R':
7944 cap->arg = TRUE;
7945 nv_Replace(cap);
7946 break;
7947
7948 case 'r':
7949 nv_vreplace(cap);
7950 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951
7952 case '&':
7953 do_cmdline_cmd((char_u *)"%s//~/&");
7954 break;
7955
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 /*
7957 * "gv": Reselect the previous Visual area. If Visual already active,
7958 * exchange previous and current Visual area.
7959 */
7960 case 'v':
7961 if (checkclearop(oap))
7962 break;
7963
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007964 if ( curbuf->b_visual.vi_start.lnum == 0
7965 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7966 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 beep_flush();
7968 else
7969 {
7970 /* set w_cursor to the start of the Visual area, tpos to the end */
7971 if (VIsual_active)
7972 {
7973 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007974 VIsual_mode = curbuf->b_visual.vi_mode;
7975 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976# ifdef FEAT_EVAL
7977 curbuf->b_visual_mode_eval = i;
7978# endif
7979 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007980 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7981 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007983 tpos = curbuf->b_visual.vi_end;
7984 curbuf->b_visual.vi_end = curwin->w_cursor;
7985 curwin->w_cursor = curbuf->b_visual.vi_start;
7986 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 }
7988 else
7989 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007990 VIsual_mode = curbuf->b_visual.vi_mode;
7991 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7992 tpos = curbuf->b_visual.vi_end;
7993 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 }
7995
7996 VIsual_active = TRUE;
7997 VIsual_reselect = TRUE;
7998
7999 /* Set Visual to the start and w_cursor to the end of the Visual
8000 * area. Make sure they are on an existing character. */
8001 check_cursor();
8002 VIsual = curwin->w_cursor;
8003 curwin->w_cursor = tpos;
8004 check_cursor();
8005 update_topline();
8006 /*
8007 * When called from normal "g" command: start Select mode when
8008 * 'selectmode' contains "cmd". When called for K_SELECT, always
8009 * start Select mode.
8010 */
8011 if (cap->arg)
8012 VIsual_select = TRUE;
8013 else
8014 may_start_select('c');
8015#ifdef FEAT_MOUSE
8016 setmouse();
8017#endif
8018#ifdef FEAT_CLIPBOARD
8019 /* Make sure the clipboard gets updated. Needed because start and
8020 * end are still the same, and the selection needs to be owned */
8021 clip_star.vmode = NUL;
8022#endif
8023 redraw_curbuf_later(INVERTED);
8024 showmode();
8025 }
8026 break;
8027 /*
8028 * "gV": Don't reselect the previous Visual area after a Select mode
8029 * mapping of menu.
8030 */
8031 case 'V':
8032 VIsual_reselect = FALSE;
8033 break;
8034
8035 /*
8036 * "gh": start Select mode.
8037 * "gH": start Select line mode.
8038 * "g^H": start Select block mode.
8039 */
8040 case K_BS:
8041 cap->nchar = Ctrl_H;
8042 /* FALLTHROUGH */
8043 case 'h':
8044 case 'H':
8045 case Ctrl_H:
8046# ifdef EBCDIC
8047 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
8048 if (cap->nchar == Ctrl_H)
8049 cap->cmdchar = Ctrl_V;
8050 else
8051# endif
8052 cap->cmdchar = cap->nchar + ('v' - 'h');
8053 cap->arg = TRUE;
8054 nv_visual(cap);
8055 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02008056
8057 /* "gn", "gN" visually select next/previous search match
8058 * "gn" selects next match
8059 * "gN" selects previous match
8060 */
8061 case 'N':
8062 case 'n':
8063 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02008064 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02008065 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066
8067 /*
8068 * "gj" and "gk" two new funny movement keys -- up and down
8069 * movement based on *screen* line rather than *file* line.
8070 */
8071 case 'j':
8072 case K_DOWN:
8073 /* with 'nowrap' it works just like the normal "j" command; also when
8074 * in a closed fold */
8075 if (!curwin->w_p_wrap
8076#ifdef FEAT_FOLDING
8077 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8078#endif
8079 )
8080 {
8081 oap->motion_type = MLINE;
8082 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
8083 }
8084 else
8085 i = nv_screengo(oap, FORWARD, cap->count1);
8086 if (i == FAIL)
8087 clearopbeep(oap);
8088 break;
8089
8090 case 'k':
8091 case K_UP:
8092 /* with 'nowrap' it works just like the normal "k" command; also when
8093 * in a closed fold */
8094 if (!curwin->w_p_wrap
8095#ifdef FEAT_FOLDING
8096 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
8097#endif
8098 )
8099 {
8100 oap->motion_type = MLINE;
8101 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
8102 }
8103 else
8104 i = nv_screengo(oap, BACKWARD, cap->count1);
8105 if (i == FAIL)
8106 clearopbeep(oap);
8107 break;
8108
8109 /*
8110 * "gJ": join two lines without inserting a space.
8111 */
8112 case 'J':
8113 nv_join(cap);
8114 break;
8115
8116 /*
8117 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
8118 * "gm": middle of "g0" and "g$".
8119 */
8120 case '^':
8121 flag = TRUE;
8122 /* FALLTHROUGH */
8123
8124 case '0':
8125 case 'm':
8126 case K_HOME:
8127 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 oap->motion_type = MCHAR;
8129 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008130 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008132 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 int width2 = width1 + curwin_col_off2();
8134
8135 validate_virtcol();
8136 i = 0;
8137 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8138 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8139 }
8140 else
8141 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008142 /* Go to the middle of the screen line. When 'number' or
8143 * 'relativenumber' is on and lines are wrapping the middle can be more
8144 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008146 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 + ((curwin->w_p_wrap && i > 0)
8148 ? curwin_col_off2() : 0)) / 2;
8149 coladvance((colnr_T)i);
8150 if (flag)
8151 {
8152 do
8153 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008154 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008155 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 }
8157 curwin->w_set_curswant = TRUE;
8158 break;
8159
8160 case '_':
8161 /* "g_": to the last non-blank character in the line or <count> lines
8162 * downward. */
8163 cap->oap->motion_type = MCHAR;
8164 cap->oap->inclusive = TRUE;
8165 curwin->w_curswant = MAXCOL;
8166 if (cursor_down((long)(cap->count1 - 1),
8167 cap->oap->op_type == OP_NOP) == FAIL)
8168 clearopbeep(cap->oap);
8169 else
8170 {
8171 char_u *ptr = ml_get_curline();
8172
8173 /* In Visual mode we may end up after the line. */
8174 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8175 --curwin->w_cursor.col;
8176
8177 /* Decrease the cursor column until it's on a non-blank. */
8178 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008179 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 --curwin->w_cursor.col;
8181 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008182 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 }
8184 break;
8185
8186 case '$':
8187 case K_END:
8188 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {
8190 int col_off = curwin_col_off();
8191
8192 oap->motion_type = MCHAR;
8193 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008194 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {
8196 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8197 if (cap->count1 == 1)
8198 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008199 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 int width2 = width1 + curwin_col_off2();
8201
8202 validate_virtcol();
8203 i = width1 - 1;
8204 if (curwin->w_virtcol >= (colnr_T)width1)
8205 i += ((curwin->w_virtcol - width1) / width2 + 1)
8206 * width2;
8207 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008208
8209 /* Make sure we stick in this column. */
8210 validate_virtcol();
8211 curwin->w_curswant = curwin->w_virtcol;
8212 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213#if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
8214 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8215 {
8216 /*
8217 * Check for landing on a character that got split at
8218 * the end of the line. We do not want to advance to
8219 * the next screen line.
8220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if (curwin->w_virtcol > (colnr_T)i)
8222 --curwin->w_cursor.col;
8223 }
8224#endif
8225 }
8226 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8227 clearopbeep(oap);
8228 }
8229 else
8230 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008231 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008233
8234 /* Make sure we stick in this column. */
8235 validate_virtcol();
8236 curwin->w_curswant = curwin->w_virtcol;
8237 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 }
8239 }
8240 break;
8241
8242 /*
8243 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8244 */
8245 case '*':
8246 case '#':
8247#if POUND != '#'
8248 case POUND: /* pound sign (sometimes equal to '#') */
8249#endif
8250 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8251 case ']': /* :tselect for current identifier */
8252 nv_ident(cap);
8253 break;
8254
8255 /*
8256 * ge and gE: go back to end of word
8257 */
8258 case 'e':
8259 case 'E':
8260 oap->motion_type = MCHAR;
8261 curwin->w_set_curswant = TRUE;
8262 oap->inclusive = TRUE;
8263 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8264 clearopbeep(oap);
8265 break;
8266
8267 /*
8268 * "g CTRL-G": display info about cursor position
8269 */
8270 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008271 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 break;
8273
8274 /*
8275 * "gi": start Insert at the last position.
8276 */
8277 case 'i':
8278 if (curbuf->b_last_insert.lnum != 0)
8279 {
8280 curwin->w_cursor = curbuf->b_last_insert;
8281 check_cursor_lnum();
8282 i = (int)STRLEN(ml_get_curline());
8283 if (curwin->w_cursor.col > (colnr_T)i)
8284 {
8285#ifdef FEAT_VIRTUALEDIT
8286 if (virtual_active())
8287 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
8288#endif
8289 curwin->w_cursor.col = i;
8290 }
8291 }
8292 cap->cmdchar = 'i';
8293 nv_edit(cap);
8294 break;
8295
8296 /*
8297 * "gI": Start insert in column 1.
8298 */
8299 case 'I':
8300 beginline(0);
8301 if (!checkclearopq(oap))
8302 invoke_edit(cap, FALSE, 'g', FALSE);
8303 break;
8304
8305#ifdef FEAT_SEARCHPATH
8306 /*
8307 * "gf": goto file, edit file under cursor
8308 * "]f" and "[f": can also be used.
8309 */
8310 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008311 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 nv_gotofile(cap);
8313 break;
8314#endif
8315
8316 /* "g'm" and "g`m": jump to mark without setting pcmark */
8317 case '\'':
8318 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008319 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 case '`':
8321 nv_gomark(cap);
8322 break;
8323
8324 /*
8325 * "gs": Goto sleep.
8326 */
8327 case 's':
8328 do_sleep(cap->count1 * 1000L);
8329 break;
8330
8331 /*
8332 * "ga": Display the ascii value of the character under the
8333 * cursor. It is displayed in decimal, hex, and octal. -- webb
8334 */
8335 case 'a':
8336 do_ascii(NULL);
8337 break;
8338
8339#ifdef FEAT_MBYTE
8340 /*
8341 * "g8": Display the bytes used for the UTF-8 character under the
8342 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008343 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 */
8345 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008346 if (cap->count0 == 8)
8347 utf_find_illegal();
8348 else
8349 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 break;
8351#endif
8352
Bram Moolenaar60402d62017-04-20 18:54:50 +02008353 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008354 case '<':
8355 show_sb_text();
8356 break;
8357
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 /*
8359 * "gg": Goto the first line in file. With a count it goes to
8360 * that line number like for "G". -- webb
8361 */
8362 case 'g':
8363 cap->arg = FALSE;
8364 nv_goto(cap);
8365 break;
8366
8367 /*
8368 * Two-character operators:
8369 * "gq" Format text
8370 * "gw" Format text and keep cursor position
8371 * "g~" Toggle the case of the text.
8372 * "gu" Change text to lower case.
8373 * "gU" Change text to upper case.
8374 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008375 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 */
8377 case 'q':
8378 case 'w':
8379 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008380 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 case '~':
8382 case 'u':
8383 case 'U':
8384 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008385 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 nv_operator(cap);
8387 break;
8388
8389 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008390 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 * current function
8392 * "gD": idem, but in the current file.
8393 */
8394 case 'd':
8395 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008396 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 break;
8398
8399#ifdef FEAT_MOUSE
8400 /*
8401 * g<*Mouse> : <C-*mouse>
8402 */
8403 case K_MIDDLEMOUSE:
8404 case K_MIDDLEDRAG:
8405 case K_MIDDLERELEASE:
8406 case K_LEFTMOUSE:
8407 case K_LEFTDRAG:
8408 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008409 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 case K_RIGHTMOUSE:
8411 case K_RIGHTDRAG:
8412 case K_RIGHTRELEASE:
8413 case K_X1MOUSE:
8414 case K_X1DRAG:
8415 case K_X1RELEASE:
8416 case K_X2MOUSE:
8417 case K_X2DRAG:
8418 case K_X2RELEASE:
8419 mod_mask = MOD_MASK_CTRL;
8420 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8421 break;
8422#endif
8423
8424 case K_IGNORE:
8425 break;
8426
8427 /*
8428 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8429 */
8430 case 'p':
8431 case 'P':
8432 nv_put(cap);
8433 break;
8434
8435#ifdef FEAT_BYTEOFF
8436 /* "go": goto byte count from start of buffer */
8437 case 'o':
8438 goto_byte(cap->count0);
8439 break;
8440#endif
8441
8442 /* "gQ": improved Ex mode */
8443 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008444 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {
8446 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008447 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 break;
8449 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008450
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 if (!checkclearopq(oap))
8452 do_exmode(TRUE);
8453 break;
8454
8455#ifdef FEAT_JUMPLIST
8456 case ',':
8457 nv_pcmark(cap);
8458 break;
8459
8460 case ';':
8461 cap->count1 = -cap->count1;
8462 nv_pcmark(cap);
8463 break;
8464#endif
8465
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008466 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008467 if (!checkclearop(oap))
8468 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008469 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008470 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008471 if (!checkclearop(oap))
8472 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008473 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008474
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008475 case '+':
8476 case '-': /* "g+" and "g-": undo or redo along the timeline */
8477 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008478 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008479 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008480 break;
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 default:
8483 clearopbeep(oap);
8484 break;
8485 }
8486}
8487
8488/*
8489 * Handle "o" and "O" commands.
8490 */
8491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008492n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008494#ifdef FEAT_CONCEAL
8495 linenr_T oldline = curwin->w_cursor.lnum;
8496#endif
8497
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 if (!checkclearopq(cap->oap))
8499 {
8500#ifdef FEAT_FOLDING
8501 if (cap->cmdchar == 'O')
8502 /* Open above the first line of a folded sequence of lines */
8503 (void)hasFolding(curwin->w_cursor.lnum,
8504 &curwin->w_cursor.lnum, NULL);
8505 else
8506 /* Open below the last line of a folded sequence of lines */
8507 (void)hasFolding(curwin->w_cursor.lnum,
8508 NULL, &curwin->w_cursor.lnum);
8509#endif
8510 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8511 (cap->cmdchar == 'O' ? 1 : 0)),
8512 (linenr_T)(curwin->w_cursor.lnum +
8513 (cap->cmdchar == 'o' ? 1 : 0))
8514 ) == OK
8515 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8516#ifdef FEAT_COMMENTS
8517 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8518#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02008519 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008521#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008522 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar860cae12010-06-05 23:22:07 +02008523 update_single_line(curwin, oldline);
8524#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008525 /* When '#' is in 'cpoptions' ignore the count. */
8526 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8527 cap->count1 = 1;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008528#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008529 if (curwin->w_p_cul)
8530 /* force redraw of cursorline */
8531 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8534 }
8535 }
8536}
8537
8538/*
8539 * "." command: redo last change.
8540 */
8541 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008542nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 if (!checkclearopq(cap->oap))
8545 {
8546 /*
8547 * If "restart_edit" is TRUE, the last but one command is repeated
8548 * instead of the last command (inserting text). This is used for
8549 * CTRL-O <.> in insert mode.
8550 */
8551 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8552 clearopbeep(cap->oap);
8553 }
8554}
8555
8556/*
8557 * CTRL-R: undo undo
8558 */
8559 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008560nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562 if (!checkclearopq(cap->oap))
8563 {
8564 u_redo((int)cap->count1);
8565 curwin->w_set_curswant = TRUE;
8566 }
8567}
8568
8569/*
8570 * Handle "U" command.
8571 */
8572 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008573nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008576 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 {
8578 /* translate "gUU" to "gUgU" */
8579 cap->cmdchar = 'g';
8580 cap->nchar = 'U';
8581 nv_operator(cap);
8582 }
8583 else if (!checkclearopq(cap->oap))
8584 {
8585 u_undoline();
8586 curwin->w_set_curswant = TRUE;
8587 }
8588}
8589
8590/*
8591 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8592 * single character.
8593 */
8594 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008595nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008597 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02008598 {
8599#ifdef FEAT_JOB_CHANNEL
8600 if (bt_prompt(curbuf) && !prompt_curpos_editable())
8601 {
8602 clearopbeep(cap->oap);
8603 return;
8604 }
8605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 else
8609 nv_operator(cap);
8610}
8611
8612/*
8613 * Handle an operator command.
8614 * The actual work is done by do_pending_operator().
8615 */
8616 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008617nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618{
8619 int op_type;
8620
8621 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008622#ifdef FEAT_JOB_CHANNEL
8623 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
8624 {
8625 clearopbeep(cap->oap);
8626 return;
8627 }
8628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629
8630 if (op_type == cap->oap->op_type) /* double operator works on lines */
8631 nv_lineop(cap);
8632 else if (!checkclearop(cap->oap))
8633 {
8634 cap->oap->start = curwin->w_cursor;
8635 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008636#ifdef FEAT_EVAL
8637 set_op_var(op_type);
8638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 }
8640}
8641
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008642#ifdef FEAT_EVAL
8643/*
8644 * Set v:operator to the characters for "optype".
8645 */
8646 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008647set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008648{
8649 char_u opchars[3];
8650
8651 if (optype == OP_NOP)
8652 set_vim_var_string(VV_OP, NULL, 0);
8653 else
8654 {
8655 opchars[0] = get_op_char(optype);
8656 opchars[1] = get_extra_op_char(optype);
8657 opchars[2] = NUL;
8658 set_vim_var_string(VV_OP, opchars, -1);
8659 }
8660}
8661#endif
8662
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663/*
8664 * Handle linewise operator "dd", "yy", etc.
8665 *
8666 * "_" is is a strange motion command that helps make operators more logical.
8667 * It is actually implemented, but not documented in the real Vi. This motion
8668 * command actually refers to "the current line". Commands like "dd" and "yy"
8669 * are really an alternate form of "d_" and "y_". It does accept a count, so
8670 * "d3_" works to delete 3 lines.
8671 */
8672 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008673nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
8675 cap->oap->motion_type = MLINE;
8676 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8677 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008678 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8679 && cap->oap->motion_force != 'v'
8680 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 || cap->oap->op_type == OP_LSHIFT
8682 || cap->oap->op_type == OP_RSHIFT)
8683 beginline(BL_SOL | BL_FIX);
8684 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8685 beginline(BL_WHITE | BL_FIX);
8686}
8687
8688/*
8689 * <Home> command.
8690 */
8691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008692nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008694 /* CTRL-HOME is like "gg" */
8695 if (mod_mask & MOD_MASK_CTRL)
8696 nv_goto(cap);
8697 else
8698 {
8699 cap->count0 = 1;
8700 nv_pipe(cap);
8701 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008702 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8703 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
8706/*
8707 * "|" command.
8708 */
8709 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008710nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712 cap->oap->motion_type = MCHAR;
8713 cap->oap->inclusive = FALSE;
8714 beginline(0);
8715 if (cap->count0 > 0)
8716 {
8717 coladvance((colnr_T)(cap->count0 - 1));
8718 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8719 }
8720 else
8721 curwin->w_curswant = 0;
8722 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008723 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 curwin->w_set_curswant = FALSE;
8725}
8726
8727/*
8728 * Handle back-word command "b" and "B".
8729 * cap->arg is 1 for "B"
8730 */
8731 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008732nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
8734 cap->oap->motion_type = MCHAR;
8735 cap->oap->inclusive = FALSE;
8736 curwin->w_set_curswant = TRUE;
8737 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8738 clearopbeep(cap->oap);
8739#ifdef FEAT_FOLDING
8740 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8741 foldOpenCursor();
8742#endif
8743}
8744
8745/*
8746 * Handle word motion commands "e", "E", "w" and "W".
8747 * cap->arg is TRUE for "E" and "W".
8748 */
8749 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008750nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751{
8752 int n;
8753 int word_end;
8754 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008755 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756
8757 /*
8758 * Set inclusive for the "E" and "e" command.
8759 */
8760 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8761 word_end = TRUE;
8762 else
8763 word_end = FALSE;
8764 cap->oap->inclusive = word_end;
8765
8766 /*
8767 * "cw" and "cW" are a special case.
8768 */
8769 if (!word_end && cap->oap->op_type == OP_CHANGE)
8770 {
8771 n = gchar_cursor();
8772 if (n != NUL) /* not an empty line */
8773 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008774 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 {
8776 /*
8777 * Reproduce a funny Vi behaviour: "cw" on a blank only
8778 * changes one character, not all blanks until the start of
8779 * the next word. Only do this when the 'w' flag is included
8780 * in 'cpoptions'.
8781 */
8782 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8783 {
8784 cap->oap->inclusive = TRUE;
8785 cap->oap->motion_type = MCHAR;
8786 return;
8787 }
8788 }
8789 else
8790 {
8791 /*
8792 * This is a little strange. To match what the real Vi does,
8793 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8794 * that we are not on a space or a TAB. This seems impolite
8795 * at first, but it's really more what we mean when we say
8796 * 'cw'.
8797 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008798 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 * will change only one character! This is done by setting
8800 * flag.
8801 */
8802 cap->oap->inclusive = TRUE;
8803 word_end = TRUE;
8804 flag = TRUE;
8805 }
8806 }
8807 }
8808
8809 cap->oap->motion_type = MCHAR;
8810 curwin->w_set_curswant = TRUE;
8811 if (word_end)
8812 n = end_word(cap->count1, cap->arg, flag, FALSE);
8813 else
8814 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8815
Bram Moolenaardfefb982008-04-01 10:06:39 +00008816 /* Don't leave the cursor on the NUL past the end of line. Unless we
8817 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008818 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008819 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820
8821 if (n == FAIL && cap->oap->op_type == OP_NOP)
8822 clearopbeep(cap->oap);
8823 else
8824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826#ifdef FEAT_FOLDING
8827 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8828 foldOpenCursor();
8829#endif
8830 }
8831}
8832
8833/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008834 * Used after a movement command: If the cursor ends up on the NUL after the
8835 * end of the line, may move it back to the last character and make the motion
8836 * inclusive.
8837 */
8838 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008839adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008840{
8841 /* The cursor cannot remain on the NUL when:
8842 * - the column is > 0
8843 * - not in Visual mode or 'selection' is "o"
8844 * - 'virtualedit' is not "all" and not "onemore".
8845 */
8846 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008847 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008848#ifdef FEAT_VIRTUALEDIT
8849 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0
8850#endif
8851 )
8852 {
8853 --curwin->w_cursor.col;
8854#ifdef FEAT_MBYTE
8855 /* prevent cursor from moving on the trail byte */
8856 if (has_mbyte)
8857 mb_adjust_cursor();
8858#endif
8859 oap->inclusive = TRUE;
8860 }
8861}
8862
8863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 * "0" and "^" commands.
8865 * cap->arg is the argument for beginline().
8866 */
8867 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008868nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
8870 cap->oap->motion_type = MCHAR;
8871 cap->oap->inclusive = FALSE;
8872 beginline(cap->arg);
8873#ifdef FEAT_FOLDING
8874 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8875 foldOpenCursor();
8876#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008877 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8878 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879}
8880
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881/*
8882 * In exclusive Visual mode, may include the last character.
8883 */
8884 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008885adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886{
8887 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008888 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008890#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 if (has_mbyte)
8892 inc_cursor();
8893 else
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 ++curwin->w_cursor.col;
8896 cap->oap->inclusive = FALSE;
8897 }
8898}
8899
8900/*
8901 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8902 * Should check VIsual_mode before calling this.
8903 * Returns TRUE when backed up to the previous line.
8904 */
8905 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008906unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907{
8908 pos_T *pp;
8909
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008910 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008912 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 pp = &curwin->w_cursor;
8914 else
8915 pp = &VIsual;
8916#ifdef FEAT_VIRTUALEDIT
8917 if (pp->coladd > 0)
8918 --pp->coladd;
8919 else
8920#endif
8921 if (pp->col > 0)
8922 {
8923 --pp->col;
8924#ifdef FEAT_MBYTE
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008925 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926#endif
8927 }
8928 else if (pp->lnum > 1)
8929 {
8930 --pp->lnum;
8931 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8932 return TRUE;
8933 }
8934 }
8935 return FALSE;
8936}
8937
8938/*
8939 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8940 */
8941 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008942nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
8944 if (VIsual_active)
8945 VIsual_select = TRUE;
8946 else if (VIsual_reselect)
8947 {
8948 cap->nchar = 'v'; /* fake "gv" command */
8949 cap->arg = TRUE;
8950 nv_g_cmd(cap);
8951 }
8952}
8953
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954
8955/*
8956 * "G", "gg", CTRL-END, CTRL-HOME.
8957 * cap->arg is TRUE for "G".
8958 */
8959 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008960nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962 linenr_T lnum;
8963
8964 if (cap->arg)
8965 lnum = curbuf->b_ml.ml_line_count;
8966 else
8967 lnum = 1L;
8968 cap->oap->motion_type = MLINE;
8969 setpcmark();
8970
8971 /* When a count is given, use it instead of the default lnum */
8972 if (cap->count0 != 0)
8973 lnum = cap->count0;
8974 if (lnum < 1L)
8975 lnum = 1L;
8976 else if (lnum > curbuf->b_ml.ml_line_count)
8977 lnum = curbuf->b_ml.ml_line_count;
8978 curwin->w_cursor.lnum = lnum;
8979 beginline(BL_SOL | BL_FIX);
8980#ifdef FEAT_FOLDING
8981 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8982 foldOpenCursor();
8983#endif
8984}
8985
8986/*
8987 * CTRL-\ in Normal mode.
8988 */
8989 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008990nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
8992 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8993 {
8994 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008995 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 clear_cmdline = TRUE; /* unshow mode later */
8997 restart_edit = 0;
8998#ifdef FEAT_CMDWIN
8999 if (cmdwin_type != 0)
9000 cmdwin_result = Ctrl_C;
9001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 if (VIsual_active)
9003 {
9004 end_visual_mode(); /* stop Visual */
9005 redraw_curbuf_later(INVERTED);
9006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
9008 if (cap->nchar == Ctrl_G && p_im)
9009 restart_edit = 'a';
9010 }
9011 else
9012 clearopbeep(cap->oap);
9013}
9014
9015/*
9016 * ESC in Normal mode: beep, but don't flush buffers.
9017 * Don't even beep if we are canceling a command.
9018 */
9019 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009020nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021{
9022 int no_reason;
9023
9024 no_reason = (cap->oap->op_type == OP_NOP
9025 && cap->opcount == 0
9026 && cap->count0 == 0
9027 && cap->oap->regname == 0
9028 && !p_im);
9029
9030 if (cap->arg) /* TRUE for CTRL-C */
9031 {
9032 if (restart_edit == 0
9033#ifdef FEAT_CMDWIN
9034 && cmdwin_type == 0
9035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 && no_reason)
Bram Moolenaar28a81932017-06-04 15:33:48 +02009038 MSG(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039
9040 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
9041 * set again below when halfway a mapping. */
9042 if (!p_im)
9043 restart_edit = 0;
9044#ifdef FEAT_CMDWIN
9045 if (cmdwin_type != 0)
9046 {
9047 cmdwin_result = K_IGNORE;
9048 got_int = FALSE; /* don't stop executing autocommands et al. */
9049 return;
9050 }
9051#endif
9052 }
9053
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 if (VIsual_active)
9055 {
9056 end_visual_mode(); /* stop Visual */
9057 check_cursor_col(); /* make sure cursor is not beyond EOL */
9058 curwin->w_set_curswant = TRUE;
9059 redraw_curbuf_later(INVERTED);
9060 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009061 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02009062 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 clearop(cap->oap);
9064
9065 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
9066 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01009067 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 restart_edit = 'a';
9069}
9070
9071/*
9072 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01009073 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 */
9075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009076nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
9078 /* <Insert> is equal to "i" */
9079 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
9080 cap->cmdchar = 'i';
9081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 /* in Visual mode "A" and "I" are an operator */
9083 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02009084 {
9085#ifdef FEAT_TERMINAL
9086 if (term_in_normal_mode())
9087 {
9088 end_visual_mode();
9089 clearop(cap->oap);
9090 term_enter_job_mode();
9091 return;
9092 }
9093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02009095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096
9097 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009098 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
9099 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 {
9101#ifdef FEAT_TEXTOBJ
9102 nv_object(cap);
9103#else
9104 clearopbeep(cap->oap);
9105#endif
9106 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02009107#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02009108 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02009109 {
9110 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02009111 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02009112 return;
9113 }
9114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 else if (!curbuf->b_p_ma && !p_im)
9116 {
9117 /* Only give this error when 'insertmode' is off. */
9118 EMSG(_(e_modifiable));
9119 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01009120 if (cap->cmdchar == K_PS)
9121 /* drop the pasted text */
9122 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 }
Bram Moolenaara1891842017-02-04 21:34:31 +01009124 else if (cap->cmdchar == K_PS && VIsual_active)
9125 {
9126 pos_T old_pos = curwin->w_cursor;
9127 pos_T old_visual = VIsual;
9128
9129 /* In Visual mode the selected text is deleted. */
9130 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
9131 {
9132 shift_delete_registers();
9133 cap->oap->regname = '1';
9134 }
9135 else
9136 cap->oap->regname = '-';
9137 cap->cmdchar = 'd';
9138 cap->nchar = NUL;
9139 nv_operator(cap);
9140 do_pending_operator(cap, 0, FALSE);
9141 cap->cmdchar = K_PS;
9142
9143 /* When the last char in the line was deleted then append. Detect this
9144 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009145 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9146 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009147 inc_cursor();
9148
9149 /* Insert to replace the deleted text with the pasted text. */
9150 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 else if (!checkclearopq(cap->oap))
9153 {
9154 switch (cap->cmdchar)
9155 {
9156 case 'A': /* "A"ppend after the line */
9157 curwin->w_set_curswant = TRUE;
9158#ifdef FEAT_VIRTUALEDIT
9159 if (ve_flags == VE_ALL)
9160 {
9161 int save_State = State;
9162
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009163 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 * character past the end of the line */
9165 State = INSERT;
9166 coladvance((colnr_T)MAXCOL);
9167 State = save_State;
9168 }
9169 else
9170#endif
9171 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9172 break;
9173
9174 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009175 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9176 beginline(BL_WHITE);
9177 else
9178 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 break;
9180
Bram Moolenaara1891842017-02-04 21:34:31 +01009181 case K_PS:
9182 /* Bracketed paste works like "a"ppend, unless the cursor is in
9183 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009184 if (curwin->w_cursor.col == 0)
9185 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009186 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009187
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 case 'a': /* "a"ppend is like "i"nsert on the next character. */
9189#ifdef FEAT_VIRTUALEDIT
9190 /* increment coladd when in virtual space, increment the
9191 * column otherwise, also to append after an unprintable char */
9192 if (virtual_active()
9193 && (curwin->w_cursor.coladd > 0
9194 || *ml_get_cursor() == NUL
9195 || *ml_get_cursor() == TAB))
9196 curwin->w_cursor.coladd++;
9197 else
9198#endif
9199 if (*ml_get_cursor() != NUL)
9200 inc_cursor();
9201 break;
9202 }
9203
9204#ifdef FEAT_VIRTUALEDIT
9205 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9206 {
9207 int save_State = State;
9208
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009209 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 * character past the end of the line */
9211 State = INSERT;
9212 coladvance(getviscol());
9213 State = save_State;
9214 }
9215#endif
9216
9217 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9218 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009219 else if (cap->cmdchar == K_PS)
9220 /* drop the pasted text */
9221 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222}
9223
9224/*
9225 * Invoke edit() and take care of "restart_edit" and the return value.
9226 */
9227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009228invoke_edit(
9229 cmdarg_T *cap,
9230 int repl, /* "r" or "gr" command */
9231 int cmd,
9232 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
9234 int restart_edit_save = 0;
9235
9236 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9237 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9238 * it. */
9239 if (repl || !stuff_empty())
9240 restart_edit_save = restart_edit;
9241 else
9242 restart_edit_save = 0;
9243
9244 /* Always reset "restart_edit", this is not a restarted edit. */
9245 restart_edit = 0;
9246
9247 if (edit(cmd, startln, cap->count1))
9248 cap->retval |= CA_COMMAND_BUSY;
9249
9250 if (restart_edit == 0)
9251 restart_edit = restart_edit_save;
9252}
9253
9254#ifdef FEAT_TEXTOBJ
9255/*
9256 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9257 */
9258 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009259nv_object(
9260 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
9262 int flag;
9263 int include;
9264 char_u *mps_save;
9265
9266 if (cap->cmdchar == 'i')
9267 include = FALSE; /* "ix" = inner object: exclude white space */
9268 else
9269 include = TRUE; /* "ax" = an object: include white space */
9270
9271 /* Make sure (), [], {} and <> are in 'matchpairs' */
9272 mps_save = curbuf->b_p_mps;
9273 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9274
9275 switch (cap->nchar)
9276 {
9277 case 'w': /* "aw" = a word */
9278 flag = current_word(cap->oap, cap->count1, include, FALSE);
9279 break;
9280 case 'W': /* "aW" = a WORD */
9281 flag = current_word(cap->oap, cap->count1, include, TRUE);
9282 break;
9283 case 'b': /* "ab" = a braces block */
9284 case '(':
9285 case ')':
9286 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9287 break;
9288 case 'B': /* "aB" = a Brackets block */
9289 case '{':
9290 case '}':
9291 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9292 break;
9293 case '[': /* "a[" = a [] block */
9294 case ']':
9295 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9296 break;
9297 case '<': /* "a<" = a <> block */
9298 case '>':
9299 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9300 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009301 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009302 /* Do not adjust oap->end in do_pending_operator()
9303 * otherwise there are different results for 'dit'
9304 * (note leading whitespace in last line):
9305 * 1) <b> 2) <b>
9306 * foobar foobar
9307 * </b> </b>
9308 */
9309 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009310 flag = current_tagblock(cap->oap, cap->count1, include);
9311 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 case 'p': /* "ap" = a paragraph */
9313 flag = current_par(cap->oap, cap->count1, include, 'p');
9314 break;
9315 case 's': /* "as" = a sentence */
9316 flag = current_sent(cap->oap, cap->count1, include);
9317 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009318 case '"': /* "a"" = a double quoted string */
9319 case '\'': /* "a'" = a single quoted string */
9320 case '`': /* "a`" = a backtick quoted string */
9321 flag = current_quote(cap->oap, cap->count1, include,
9322 cap->nchar);
9323 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324#if 0 /* TODO */
9325 case 'S': /* "aS" = a section */
9326 case 'f': /* "af" = a filename */
9327 case 'u': /* "au" = a URL */
9328#endif
9329 default:
9330 flag = FAIL;
9331 break;
9332 }
9333
9334 curbuf->b_p_mps = mps_save;
9335 if (flag == FAIL)
9336 clearopbeep(cap->oap);
9337 adjust_cursor_col();
9338 curwin->w_set_curswant = TRUE;
9339}
9340#endif
9341
9342/*
9343 * "q" command: Start/stop recording.
9344 * "q:", "q/", "q?": edit command-line in command-line window.
9345 */
9346 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009347nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348{
9349 if (cap->oap->op_type == OP_FORMAT)
9350 {
9351 /* "gqq" is the same as "gqgq": format line */
9352 cap->cmdchar = 'g';
9353 cap->nchar = 'q';
9354 nv_operator(cap);
9355 }
9356 else if (!checkclearop(cap->oap))
9357 {
9358#ifdef FEAT_CMDWIN
9359 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9360 {
9361 stuffcharReadbuff(cap->nchar);
9362 stuffcharReadbuff(K_CMDWIN);
9363 }
9364 else
9365#endif
9366 /* (stop) recording into a named register, unless executing a
9367 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009368 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 clearopbeep(cap->oap);
9370 }
9371}
9372
9373/*
9374 * Handle the "@r" command.
9375 */
9376 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009377nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 if (checkclearop(cap->oap))
9380 return;
9381#ifdef FEAT_EVAL
9382 if (cap->nchar == '=')
9383 {
9384 if (get_expr_register() == NUL)
9385 return;
9386 }
9387#endif
9388 while (cap->count1-- && !got_int)
9389 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009390 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 {
9392 clearopbeep(cap->oap);
9393 break;
9394 }
9395 line_breakcheck();
9396 }
9397}
9398
9399/*
9400 * Handle the CTRL-U and CTRL-D commands.
9401 */
9402 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009403nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404{
9405 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9406 || (cap->cmdchar == Ctrl_D
9407 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9408 clearopbeep(cap->oap);
9409 else if (!checkclearop(cap->oap))
9410 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9411}
9412
9413/*
9414 * Handle "J" or "gJ" command.
9415 */
9416 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009417nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 if (VIsual_active) /* join the visual lines */
9420 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009421 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 {
9423 if (cap->count0 <= 1)
9424 cap->count0 = 2; /* default for join is two lines! */
9425 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9426 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009428 /* can't join when on the last line */
9429 if (cap->count0 <= 2)
9430 {
9431 clearopbeep(cap->oap);
9432 return;
9433 }
9434 cap->count0 = curbuf->b_ml.ml_line_count
9435 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009437
9438 prep_redo(cap->oap->regname, cap->count0,
9439 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9440 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 }
9442}
9443
9444/*
9445 * "P", "gP", "p" and "gp" commands.
9446 */
9447 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009448nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 int regname = 0;
9451 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009452 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009453 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 int dir;
9455 int flags = 0;
9456
9457 if (cap->oap->op_type != OP_NOP)
9458 {
9459#ifdef FEAT_DIFF
9460 /* "dp" is ":diffput" */
9461 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9462 {
9463 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009464 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 }
9466 else
9467#endif
9468 clearopbeep(cap->oap);
9469 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02009470#ifdef FEAT_JOB_CHANNEL
9471 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
9472 {
9473 clearopbeep(cap->oap);
9474 }
9475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 else
9477 {
9478 dir = (cap->cmdchar == 'P'
9479 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9480 ? BACKWARD : FORWARD;
9481 prep_redo_cmd(cap);
9482 if (cap->cmdchar == 'g')
9483 flags |= PUT_CURSEND;
9484
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (VIsual_active)
9486 {
9487 /* Putting in Visual mode: The put text replaces the selected
9488 * text. First delete the selected text, then put the new text.
9489 * Need to save and restore the registers that the delete
9490 * overwrites if the old contents is being put.
9491 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009492 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009494#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009496#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009497 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009498 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009499#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502
9503 )
9504 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009505 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 * put, save it first. */
9507 reg1 = get_register(regname, TRUE);
9508 }
9509
9510 /* Now delete the selected text. */
9511 cap->cmdchar = 'd';
9512 cap->nchar = NUL;
9513 cap->oap->regname = NUL;
9514 nv_operator(cap);
9515 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009516 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517
9518 /* delete PUT_LINE_BACKWARD; */
9519 cap->oap->regname = regname;
9520
9521 if (reg1 != NULL)
9522 {
9523 /* Delete probably changed the register we want to put, save
9524 * it first. Then put back what was there before the delete. */
9525 reg2 = get_register(regname, FALSE);
9526 put_register(regname, reg1);
9527 }
9528
9529 /* When deleted a linewise Visual area, put the register as
9530 * lines to avoid it joined with the next line. When deletion was
9531 * characterwise, split a line when putting lines. */
9532 if (VIsual_mode == 'V')
9533 flags |= PUT_LINE;
9534 else if (VIsual_mode == 'v')
9535 flags |= PUT_LINE_SPLIT;
9536 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9537 flags |= PUT_LINE_FORWARD;
9538 dir = BACKWARD;
9539 if ((VIsual_mode != 'V'
9540 && curwin->w_cursor.col < curbuf->b_op_start.col)
9541 || (VIsual_mode == 'V'
9542 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9543 /* cursor is at the end of the line or end of file, put
9544 * forward. */
9545 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009546 /* May have been reset in do_put(). */
9547 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 do_put(cap->oap->regname, dir, cap->count1, flags);
9550
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 /* If a register was saved, put it back now. */
9552 if (reg2 != NULL)
9553 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009554
9555 /* What to reselect with "gv"? Selecting the just put text seems to
9556 * be the most useful, since the original text was removed. */
9557 if (was_visual)
9558 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009559 curbuf->b_visual.vi_start = curbuf->b_op_start;
9560 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009561 /* need to adjust cursor position */
9562 if (*p_sel == 'e')
9563 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009564 }
9565
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009566 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009567 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009568 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009569 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009570 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009571
9572 /* If the cursor was in that line, move it to the end of the last
9573 * line. */
9574 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9575 {
9576 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9577 coladvance((colnr_T)MAXCOL);
9578 }
9579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 auto_format(FALSE, TRUE);
9581 }
9582}
9583
9584/*
9585 * "o" and "O" commands.
9586 */
9587 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009588nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
9590#ifdef FEAT_DIFF
9591 /* "do" is ":diffget" */
9592 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9593 {
9594 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009595 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 }
9597 else
9598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 if (VIsual_active) /* switch start and end of visual */
9600 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009601#ifdef FEAT_JOB_CHANNEL
9602 else if (bt_prompt(curbuf))
9603 {
9604 clearopbeep(cap->oap);
9605 }
9606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 n_opencmd(cap);
9609}
9610
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611#ifdef FEAT_NETBEANS_INTG
9612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009613nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
9615 netbeans_keycommand(cap->nchar);
9616}
9617#endif
9618
9619#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009621nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9624}
9625#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009626
Bram Moolenaar3918c952005-03-15 22:34:55 +00009627/*
9628 * Trigger CursorHold event.
9629 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9630 * input buffer. "did_cursorhold" is set to avoid retriggering.
9631 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009632 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009633nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009634{
9635 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9636 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009637 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009638}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009639
9640/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009641 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009642 */
9643 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009644get_op_vcol(
9645 oparg_T *oap,
9646 colnr_T redo_VIsual_vcol,
9647 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009648{
9649 colnr_T start, end;
9650
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009651 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009652 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009653 return;
9654
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009655 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009656
9657#ifdef FEAT_MBYTE
9658 /* prevent from moving onto a trail byte */
9659 if (has_mbyte)
9660 mb_adjustpos(curwin->w_buffer, &oap->end);
9661#endif
9662
9663 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009664
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009665 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009666 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009667 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9668
9669 if (start < oap->start_vcol)
9670 oap->start_vcol = start;
9671 if (end > oap->end_vcol)
9672 {
9673 if (initial && *p_sel == 'e' && start >= 1
9674 && start - 1 >= oap->end_vcol)
9675 oap->end_vcol = start - 1;
9676 else
9677 oap->end_vcol = end;
9678 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009679 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009680
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009681 /* if '$' was used, get oap->end_vcol from longest line */
9682 if (curwin->w_curswant == MAXCOL)
9683 {
9684 curwin->w_cursor.col = MAXCOL;
9685 oap->end_vcol = 0;
9686 for (curwin->w_cursor.lnum = oap->start.lnum;
9687 curwin->w_cursor.lnum <= oap->end.lnum;
9688 ++curwin->w_cursor.lnum)
9689 {
9690 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9691 if (end > oap->end_vcol)
9692 oap->end_vcol = end;
9693 }
9694 }
9695 else if (redo_VIsual_busy)
9696 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9697 /*
9698 * Correct oap->end.col and oap->start.col to be the
9699 * upper-left and lower-right corner of the block area.
9700 *
9701 * (Actually, this does convert column positions into character
9702 * positions)
9703 */
9704 curwin->w_cursor.lnum = oap->end.lnum;
9705 coladvance(oap->end_vcol);
9706 oap->end = curwin->w_cursor;
9707
9708 curwin->w_cursor = oap->start;
9709 coladvance(oap->start_vcol);
9710 oap->start = curwin->w_cursor;
9711}