blob: b953b7dc4cb56891085c5a6c5eaac47a955394e3 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 /* A multi-byte character is never a command. */
481 if (cmdchar >= 0x100)
482 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
484 /* We use the absolute value of the character. Special keys have a
485 * negative value, but are sorted on their absolute value. */
486 if (cmdchar < 0)
487 cmdchar = -cmdchar;
488
489 /* If the character is in the first part: The character is the index into
490 * nv_cmd_idx[]. */
491 if (cmdchar <= nv_max_linear)
492 return nv_cmd_idx[cmdchar];
493
494 /* Perform a binary search. */
495 bot = nv_max_linear + 1;
496 top = NV_CMDS_SIZE - 1;
497 idx = -1;
498 while (bot <= top)
499 {
500 i = (top + bot) / 2;
501 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
502 if (c < 0)
503 c = -c;
504 if (cmdchar == c)
505 {
506 idx = nv_cmd_idx[i];
507 break;
508 }
509 if (cmdchar > c)
510 bot = i + 1;
511 else
512 top = i - 1;
513 }
514 return idx;
515}
516
517/*
518 * Execute a command in Normal mode.
519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100521normal_cmd(
522 oparg_T *oap,
523 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 cmdarg_T ca; /* command arguments */
526 int c;
527 int ctrl_w = FALSE; /* got CTRL-W command */
528 int old_col = curwin->w_curswant;
529#ifdef FEAT_CMDL_INFO
530 int need_flushbuf; /* need to call out_flush() */
531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 pos_T old_pos; /* cursor position before command */
533 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 static int old_mapped_len = 0;
535 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000536#ifdef FEAT_EVAL
537 int set_prevcount = FALSE;
538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
541 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000542
543 /* Use a count remembered from before entering an operator. After typing
544 * "3d" we return from normal_cmd() and come back here, the "3" is
545 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 ca.opcount = opcount;
547
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 /*
549 * If there is an operator pending, then the command we take this time
550 * will terminate it. Finish_op tells us to finish the operation before
551 * returning this time (unless the operation was cancelled).
552 */
553#ifdef CURSOR_SHAPE
554 c = finish_op;
555#endif
556 finish_op = (oap->op_type != OP_NOP);
557#ifdef CURSOR_SHAPE
558 if (finish_op != c)
559 {
560 ui_cursor_shape(); /* may show different cursor shape */
561# ifdef FEAT_MOUSESHAPE
562 update_mouseshape(-1);
563# endif
564 }
565#endif
566
Bram Moolenaara983fe92008-07-31 20:04:27 +0000567 /* When not finishing an operator and no register name typed, reset the
568 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000572#ifdef FEAT_EVAL
573 set_prevcount = TRUE;
574#endif
575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
Bram Moolenaara983fe92008-07-31 20:04:27 +0000577 /* Restore counts from before receiving K_CURSORHOLD. This means after
578 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
579 * "3 * 2". */
580 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
581 {
582 ca.opcount = oap->prev_opcount;
583 ca.count0 = oap->prev_count0;
584 oap->prev_opcount = 0;
585 oap->prev_count0 = 0;
586 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000587
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590 State = NORMAL_BUSY;
591#ifdef USE_ON_FLY_SCROLL
592 dont_scroll = FALSE; /* allow scrolling here */
593#endif
594
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100595#ifdef FEAT_EVAL
596 /* Set v:count here, when called from main() and not a stuffed
597 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100598 * when there is no count. Do set it for redo. */
599 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100600 set_vcount_ca(&ca, &set_prevcount);
601#endif
602
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 /*
604 * Get the command character from the user.
605 */
606 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100607 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609 /*
610 * If a mapping was started in Visual or Select mode, remember the length
611 * of the mapping. This is used below to not return to Insert mode for as
612 * long as the mapping is being executed.
613 */
614 if (restart_edit == 0)
615 old_mapped_len = 0;
616 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000617 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 old_mapped_len = typebuf_maplen();
619
620 if (c == NUL)
621 c = K_ZERO;
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 /*
624 * In Select mode, typed text replaces the selection.
625 */
626 if (VIsual_active
627 && VIsual_select
628 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
629 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000630 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
631 * 'insertmode' is set) fake a "d"elete command, Insert mode will
632 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000633 * Insert the typed character in the typeahead buffer, so that it can
634 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000635 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000636 if (restart_edit != 0)
637 c = 'd';
638 else
639 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000640 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200641 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644#ifdef FEAT_CMDL_INFO
645 need_flushbuf = add_to_showcmd(c);
646#endif
647
648getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
651 /*
652 * Handle a count before a command and compute ca.count0.
653 * Note that '0' is a command and not the start of a count, but it's
654 * part of a count after other digits.
655 */
656 while ( (c >= '1' && c <= '9')
657 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
658 {
659 if (c == K_DEL || c == K_KDEL)
660 {
661 ca.count0 /= 10;
662#ifdef FEAT_CMDL_INFO
663 del_from_showcmd(4); /* delete the digit and ~@% */
664#endif
665 }
666 else
667 ca.count0 = ca.count0 * 10 + (c - '0');
668 if (ca.count0 < 0) /* got too large! */
669 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000670#ifdef FEAT_EVAL
671 /* Set v:count here, when called from main() and not a stuffed
672 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100673 * right after the count. Do set it for redo. */
674 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100675 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 if (ctrl_w)
678 {
679 ++no_mapping;
680 ++allow_keys; /* no mapping for nchar, but keys */
681 }
682 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000683 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 --no_zero_mapping;
686 if (ctrl_w)
687 {
688 --no_mapping;
689 --allow_keys;
690 }
691#ifdef FEAT_CMDL_INFO
692 need_flushbuf |= add_to_showcmd(c);
693#endif
694 }
695
696 /*
697 * If we got CTRL-W there may be a/another count
698 */
699 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
700 {
701 ctrl_w = TRUE;
702 ca.opcount = ca.count0; /* remember first count */
703 ca.count0 = 0;
704 ++no_mapping;
705 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000706 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 --no_mapping;
709 --allow_keys;
710#ifdef FEAT_CMDL_INFO
711 need_flushbuf |= add_to_showcmd(c);
712#endif
713 goto getcount; /* jump back */
714 }
715 }
716
Bram Moolenaara983fe92008-07-31 20:04:27 +0000717 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000719 /* Save the count values so that ca.opcount and ca.count0 are exactly
720 * the same when coming back here after handling K_CURSORHOLD. */
721 oap->prev_opcount = ca.opcount;
722 oap->prev_count0 = ca.count0;
723 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100724 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000725 {
726 /*
727 * If we're in the middle of an operator (including after entering a
728 * yank buffer with '"') AND we had a count before the operator, then
729 * that count overrides the current value of ca.count0.
730 * What this means effectively, is that commands like "3dw" get turned
731 * into "d3w" which makes things fall into place pretty neatly.
732 * If you give a count before AND after the operator, they are
733 * multiplied.
734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (ca.count0)
736 ca.count0 *= ca.opcount;
737 else
738 ca.count0 = ca.opcount;
739 }
740
741 /*
742 * Always remember the count. It will be set to zero (on the next call,
743 * above) when there is no pending operator.
744 * When called from main(), save the count for use by the "count" built-in
745 * variable.
746 */
747 ca.opcount = ca.count0;
748 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
749
750#ifdef FEAT_EVAL
751 /*
752 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100753 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100755 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000756 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#endif
758
759 /*
760 * Find the command character in the table of commands.
761 * For CTRL-W we already got nchar when looking for a count.
762 */
763 if (ctrl_w)
764 {
765 ca.nchar = c;
766 ca.cmdchar = Ctrl_W;
767 }
768 else
769 ca.cmdchar = c;
770 idx = find_command(ca.cmdchar);
771 if (idx < 0)
772 {
773 /* Not a known command: beep. */
774 clearopbeep(oap);
775 goto normal_end;
776 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000777
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000778 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200780 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000782 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 goto normal_end;
784 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000785 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
786 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 /*
789 * In Visual/Select mode, a few keys are handled in a special way.
790 */
791 if (VIsual_active)
792 {
793 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
794 if (km_stopsel
795 && (nv_cmds[idx].cmd_flags & NV_STS)
796 && !(mod_mask & MOD_MASK_SHIFT))
797 {
798 end_visual_mode();
799 redraw_curbuf_later(INVERTED);
800 }
801
802 /* Keys that work different when 'keymodel' contains "startsel" */
803 if (km_startsel)
804 {
805 if (nv_cmds[idx].cmd_flags & NV_SS)
806 {
807 unshift_special(&ca);
808 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000809 if (idx < 0)
810 {
811 /* Just in case */
812 clearopbeep(oap);
813 goto normal_end;
814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
817 && (mod_mask & MOD_MASK_SHIFT))
818 {
819 mod_mask &= ~MOD_MASK_SHIFT;
820 }
821 }
822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
824#ifdef FEAT_RIGHTLEFT
825 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
826 && (nv_cmds[idx].cmd_flags & NV_RL))
827 {
828 /* Invert horizontal movements and operations. Only when typed by the
829 * user directly, not when the result of a mapping or "x" translated
830 * to "dl". */
831 switch (ca.cmdchar)
832 {
833 case 'l': ca.cmdchar = 'h'; break;
834 case K_RIGHT: ca.cmdchar = K_LEFT; break;
835 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
836 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
837 case 'h': ca.cmdchar = 'l'; break;
838 case K_LEFT: ca.cmdchar = K_RIGHT; break;
839 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
840 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
841 case '>': ca.cmdchar = '<'; break;
842 case '<': ca.cmdchar = '>'; break;
843 }
844 idx = find_command(ca.cmdchar);
845 }
846#endif
847
848 /*
849 * Get an additional character if we need one.
850 */
851 if ((nv_cmds[idx].cmd_flags & NV_NCH)
852 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
853 && oap->op_type == OP_NOP)
854 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
855 || (ca.cmdchar == 'q'
856 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200857 && reg_recording == 0
858 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100860 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
862 int *cp;
863 int repl = FALSE; /* get character for replace mode */
864 int lit = FALSE; /* get extra character literally */
865 int langmap_active = FALSE; /* using :lmap mappings */
866 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100867#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 int save_smd; /* saved value of p_smd */
869#endif
870
871 ++no_mapping;
872 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000873 /* Don't generate a CursorHold event here, most commands can't handle
874 * it, e.g., nv_replace(), nv_csearch(). */
875 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (ca.cmdchar == 'g')
877 {
878 /*
879 * For 'g' get the next character now, so that we can check for
880 * "gr", "g'" and "g`".
881 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000882 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_CMDL_INFO
885 need_flushbuf |= add_to_showcmd(ca.nchar);
886#endif
887 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100888 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
890 cp = &ca.extra_char; /* need to get a third character */
891 if (ca.nchar != 'r')
892 lit = TRUE; /* get it literally */
893 else
894 repl = TRUE; /* get it in replace mode */
895 }
896 else
897 cp = NULL; /* no third character needed */
898 }
899 else
900 {
901 if (ca.cmdchar == 'r') /* get it in replace mode */
902 repl = TRUE;
903 cp = &ca.nchar;
904 }
905 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
906
907 /*
908 * Get a second or third character.
909 */
910 if (cp != NULL)
911 {
912#ifdef CURSOR_SHAPE
913 if (repl)
914 {
915 State = REPLACE; /* pretend Replace mode */
916 ui_cursor_shape(); /* show different cursor shape */
917 }
918#endif
919 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
920 {
921 /* Allow mappings defined with ":lmap". */
922 --no_mapping;
923 --allow_keys;
924 if (repl)
925 State = LREPLACE;
926 else
927 State = LANGMAP;
928 langmap_active = TRUE;
929 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100930#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 save_smd = p_smd;
932 p_smd = FALSE; /* Don't let the IM code show the mode here */
933 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
934 im_set_active(TRUE);
935#endif
936
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000937 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939 if (langmap_active)
940 {
941 /* Undo the decrement done above */
942 ++no_mapping;
943 ++allow_keys;
944 State = NORMAL_BUSY;
945 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100946#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (lang)
948 {
949 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
950 im_save_status(&curbuf->b_p_iminsert);
951 im_set_active(FALSE);
952 }
953 p_smd = save_smd;
954#endif
955#ifdef CURSOR_SHAPE
956 State = NORMAL_BUSY;
957#endif
958#ifdef FEAT_CMDL_INFO
959 need_flushbuf |= add_to_showcmd(*cp);
960#endif
961
962 if (!lit)
963 {
964#ifdef FEAT_DIGRAPHS
965 /* Typing CTRL-K gets a digraph. */
966 if (*cp == Ctrl_K
967 && ((nv_cmds[idx].cmd_flags & NV_LANG)
968 || cp == &ca.extra_char)
969 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
970 {
971 c = get_digraph(FALSE);
972 if (c > 0)
973 {
974 *cp = c;
975# ifdef FEAT_CMDL_INFO
976 /* Guessing how to update showcmd here... */
977 del_from_showcmd(3);
978 need_flushbuf |= add_to_showcmd(*cp);
979# endif
980 }
981 }
982#endif
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 /* adjust chars > 127, except after "tTfFr" commands */
985 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef FEAT_RIGHTLEFT
987 /* adjust Hebrew mapped char */
988 if (p_hkmap && lang && KeyTyped)
989 *cp = hkmap(*cp);
990# ifdef FEAT_FKMAP
991 /* adjust Farsi mapped char */
992 if (p_fkmap && lang && KeyTyped)
993 *cp = fkmap(*cp);
994# endif
995#endif
996 }
997
998 /*
999 * When the next character is CTRL-\ a following CTRL-N means the
1000 * command is aborted and we go to Normal mode.
1001 */
1002 if (cp == &ca.extra_char
1003 && ca.nchar == Ctrl_BSL
1004 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
1005 {
1006 ca.cmdchar = Ctrl_BSL;
1007 ca.nchar = ca.extra_char;
1008 idx = find_command(ca.cmdchar);
1009 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +02001010 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +02001011 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 else if (*cp == Ctrl_BSL)
1013 {
1014 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
1015
1016 /* There is a busy wait here when typing "f<C-\>" and then
1017 * something different from CTRL-N. Can't be avoided. */
1018 while ((c = vpeekc()) <= 0 && towait > 0L)
1019 {
1020 do_sleep(towait > 50L ? 50L : towait);
1021 towait -= 50L;
1022 }
1023 if (c > 0)
1024 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001025 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (c != Ctrl_N && c != Ctrl_G)
1027 vungetc(c);
1028 else
1029 {
1030 ca.cmdchar = Ctrl_BSL;
1031 ca.nchar = c;
1032 idx = find_command(ca.cmdchar);
1033 }
1034 }
1035 }
1036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 /* When getting a text character and the next character is a
1038 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001039 * However, don't wait for it to arrive. Also, do enable mapping,
1040 * because if it's put back with vungetc() it's too late to apply
1041 * mapping. */
1042 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 while (enc_utf8 && lang && (c = vpeekc()) > 0
1044 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1045 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001046 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (!utf_iscomposing(c))
1048 {
1049 vungetc(c); /* it wasn't, put it back */
1050 break;
1051 }
1052 else if (ca.ncharC1 == 0)
1053 ca.ncharC1 = c;
1054 else
1055 ca.ncharC2 = c;
1056 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001057 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059 --no_mapping;
1060 --allow_keys;
1061 }
1062
1063#ifdef FEAT_CMDL_INFO
1064 /*
1065 * Flush the showcmd characters onto the screen so we can see them while
1066 * the command is being executed. Only do this when the shown command was
1067 * actually displayed, otherwise this will slow down a lot when executing
1068 * mappings.
1069 */
1070 if (need_flushbuf)
1071 out_flush();
1072#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001073 if (ca.cmdchar != K_IGNORE)
1074 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 State = NORMAL;
1077
1078 if (ca.nchar == ESC)
1079 {
1080 clearop(oap);
1081 if (restart_edit == 0 && goto_im())
1082 restart_edit = 'a';
1083 goto normal_end;
1084 }
1085
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001086 if (ca.cmdchar != K_IGNORE)
1087 {
1088 msg_didout = FALSE; /* don't scroll screen up for normal command */
1089 msg_col = 0;
1090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 old_pos = curwin->w_cursor; /* remember where cursor was */
1093
1094 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1095 * mode. */
1096 if (!VIsual_active && km_startsel)
1097 {
1098 if (nv_cmds[idx].cmd_flags & NV_SS)
1099 {
1100 start_selection();
1101 unshift_special(&ca);
1102 idx = find_command(ca.cmdchar);
1103 }
1104 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1105 && (mod_mask & MOD_MASK_SHIFT))
1106 {
1107 start_selection();
1108 mod_mask &= ~MOD_MASK_SHIFT;
1109 }
1110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
1112 /*
1113 * Execute the command!
1114 * Call the command function found in the commands table.
1115 */
1116 ca.arg = nv_cmds[idx].cmd_arg;
1117 (nv_cmds[idx].cmd_func)(&ca);
1118
1119 /*
1120 * If we didn't start or finish an operator, reset oap->regname, unless we
1121 * need it later.
1122 */
1123 if (!finish_op
1124 && !oap->op_type
1125 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1126 {
1127 clearop(oap);
1128#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001129 {
1130 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001131
Bram Moolenaar536681b2011-05-10 16:12:45 +02001132 /* Adjust the register according to 'clipboard', so that when
1133 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001134# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001135 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001136# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001137 set_reg_var(regname);
1138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#endif
1140 }
1141
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001142 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001143 * character or "z333<cr>". */
1144 if (old_mapped_len > 0)
1145 old_mapped_len = typebuf_maplen();
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 /*
1148 * If an operation is pending, handle it...
1149 */
1150 do_pending_operator(&ca, old_col, FALSE);
1151
1152 /*
1153 * Wait for a moment when a message is displayed that will be overwritten
1154 * by the mode message.
1155 * In Visual mode and with "^O" in Insert mode, a short message will be
1156 * overwritten by the mode message. Wait a bit, until a key is hit.
1157 * In Visual mode, it's more important to keep the Visual area updated
1158 * than keeping a message (e.g. from a /pat search).
1159 * Only do this if the command was typed, not from a mapping.
1160 * Don't wait when emsg_silent is non-zero.
1161 * Also wait a bit after an error message, e.g. for "^O:".
1162 * Don't redraw the screen, it would remove the message.
1163 */
1164 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001165 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 || (VIsual_active
1168 && old_pos.lnum == curwin->w_cursor.lnum
1169 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 )
1171 && (clear_cmdline
1172 || redraw_cmdline)
1173 && (msg_didout || (msg_didany && msg_scroll))
1174 && !msg_nowait
1175 && KeyTyped)
1176 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 && (msg_scroll
1179 || emsg_on_display)))
1180 && oap->regname == 0
1181 && !(ca.retval & CA_COMMAND_BUSY)
1182 && stuff_empty()
1183 && typebuf_typed()
1184 && emsg_silent == 0
1185 && !did_wait_return
1186 && oap->op_type == OP_NOP)
1187 {
1188 int save_State = State;
1189
1190 /* Draw the cursor with the right shape here */
1191 if (restart_edit != 0)
1192 State = INSERT;
1193
1194 /* If need to redraw, and there is a "keep_msg", redraw before the
1195 * delay */
1196 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1197 {
1198 char_u *kmsg;
1199
1200 kmsg = keep_msg;
1201 keep_msg = NULL;
1202 /* showmode() will clear keep_msg, but we want to use it anyway */
1203 update_screen(0);
1204 /* now reset it, otherwise it's put in the history again */
1205 keep_msg = kmsg;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001206 msg_attr((char *)kmsg, keep_msg_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 vim_free(kmsg);
1208 }
1209 setcursor();
1210 cursor_on();
1211 out_flush();
1212 if (msg_scroll || emsg_on_display)
1213 ui_delay(1000L, TRUE); /* wait at least one second */
1214 ui_delay(3000L, FALSE); /* wait up to three seconds */
1215 State = save_State;
1216
1217 msg_scroll = FALSE;
1218 emsg_on_display = FALSE;
1219 }
1220
1221 /*
1222 * Finish up after executing a Normal mode command.
1223 */
1224normal_end:
1225
1226 msg_nowait = FALSE;
1227
1228 /* Reset finish_op, in case it was set */
1229#ifdef CURSOR_SHAPE
1230 c = finish_op;
1231#endif
1232 finish_op = FALSE;
1233#ifdef CURSOR_SHAPE
1234 /* Redraw the cursor with another shape, if we were in Operator-pending
1235 * mode or did a replace command. */
1236 if (c || ca.cmdchar == 'r')
1237 {
1238 ui_cursor_shape(); /* may show different cursor shape */
1239# ifdef FEAT_MOUSESHAPE
1240 update_mouseshape(-1);
1241# endif
1242 }
1243#endif
1244
1245#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001246 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001247 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 clear_showcmd();
1249#endif
1250
1251 checkpcmark(); /* check if we moved since setting pcmark */
1252 vim_free(ca.searchbuf);
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (has_mbyte)
1255 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 if (curwin->w_p_scb && toplevel)
1258 {
1259 validate_cursor(); /* may need to update w_leftcol */
1260 do_check_scrollbind(TRUE);
1261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaar860cae12010-06-05 23:22:07 +02001263 if (curwin->w_p_crb && toplevel)
1264 {
1265 validate_cursor(); /* may need to update w_leftcol */
1266 do_check_cursorbind();
1267 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001268
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001269#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001270 /* don't go to Insert mode if a terminal has a running job */
1271 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001272 restart_edit = 0;
1273#endif
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 /*
1276 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1277 * if still inside a mapping that started in Visual mode).
1278 * May switch from Visual to Select mode after CTRL-O command.
1279 */
1280 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1282 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 && !(ca.retval & CA_COMMAND_BUSY)
1284 && stuff_empty()
1285 && oap->regname == 0)
1286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (restart_VIsual_select == 1)
1288 {
1289 VIsual_select = TRUE;
1290 showmode();
1291 restart_VIsual_select = 0;
1292 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001293 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 (void)edit(restart_edit, FALSE, 1L);
1295 }
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 if (restart_VIsual_select == 2)
1298 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300 /* Save count before an operator for next time. */
1301 opcount = ca.opcount;
1302}
1303
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001304#ifdef FEAT_EVAL
1305/*
1306 * Set v:count and v:count1 according to "cap".
1307 * Set v:prevcount only when "set_prevcount" is TRUE.
1308 */
1309 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001310set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001311{
1312 long count = cap->count0;
1313
1314 /* multiply with cap->opcount the same way as above */
1315 if (cap->opcount != 0)
1316 count = cap->opcount * (count == 0 ? 1 : count);
1317 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1318 *set_prevcount = FALSE; /* only set v:prevcount once */
1319}
1320#endif
1321
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322/*
Bram Moolenaar5823f842019-01-03 22:58:08 +01001323 * Handle an operator after Visual mode or when the movement is finished.
1324 * "gui_yank" is true when yanking text for the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 */
1326 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001327do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 oparg_T *oap = cap->oap;
1330 pos_T old_cursor;
1331 int empty_region_error;
1332 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001333#ifdef FEAT_LINEBREAK
1334 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 /* The visual area is remembered for redo */
1338 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1339 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001340 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001342 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345#if defined(FEAT_CLIPBOARD)
1346 /*
1347 * Yank the visual area into the GUI selection register before we operate
1348 * on it and lose it forever.
1349 * Don't do it if a specific register was specified, so that ""x"*P works.
1350 * This could call do_pending_operator() recursively, but that's OK
1351 * because gui_yank will be TRUE for the nested call.
1352 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001353 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 && oap->op_type != OP_NOP
1355 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 && VIsual_active
1357 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 && oap->regname == 0)
1359 clip_auto_select();
1360#endif
1361 old_cursor = curwin->w_cursor;
1362
1363 /*
1364 * If an operation is pending, handle it...
1365 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001366 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar5823f842019-01-03 22:58:08 +01001368 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
1369 // for the clipboard.
1370 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
1371
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001372#ifdef FEAT_LINEBREAK
1373 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001374 if (curwin->w_p_lbr)
1375 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001376 curwin->w_p_lbr = FALSE;
1377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 oap->is_VIsual = VIsual_active;
1379 if (oap->motion_force == 'V')
1380 oap->motion_type = MLINE;
1381 else if (oap->motion_force == 'v')
1382 {
1383 /* If the motion was linewise, "inclusive" will not have been set.
1384 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1385 if (oap->motion_type == MLINE)
1386 oap->inclusive = FALSE;
1387 /* If the motion already was characterwise, toggle "inclusive" */
1388 else if (oap->motion_type == MCHAR)
1389 oap->inclusive = !oap->inclusive;
1390 oap->motion_type = MCHAR;
1391 }
1392 else if (oap->motion_force == Ctrl_V)
1393 {
1394 /* Change line- or characterwise motion into Visual block mode. */
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01001395 if (!VIsual_active)
1396 {
1397 VIsual_active = TRUE;
1398 VIsual = oap->start;
1399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 VIsual_mode = Ctrl_V;
1401 VIsual_select = FALSE;
1402 VIsual_reselect = FALSE;
1403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404
Bram Moolenaar7afea822013-04-24 18:34:45 +02001405 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1406 /* Never redo "zf" (define fold). */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001407 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001408 && ((!VIsual_active || oap->motion_force)
1409 /* Also redo Operator-pending Visual mode mappings */
1410 || (VIsual_active && cap->cmdchar == ':'
1411 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001412 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#ifdef FEAT_FOLDING
1414 && oap->op_type != OP_FOLD
1415 && oap->op_type != OP_FOLDOPEN
1416 && oap->op_type != OP_FOLDOPENREC
1417 && oap->op_type != OP_FOLDCLOSE
1418 && oap->op_type != OP_FOLDCLOSEREC
1419 && oap->op_type != OP_FOLDDEL
1420 && oap->op_type != OP_FOLDDELREC
1421#endif
1422 )
1423 {
1424 prep_redo(oap->regname, cap->count0,
1425 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1426 oap->motion_force, cap->cmdchar, cap->nchar);
1427 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1428 {
1429 /*
1430 * If 'cpoptions' does not contain 'r', insert the search
1431 * pattern to really repeat the same command.
1432 */
1433 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001434 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 AppendToRedobuff(NL_STR);
1436 }
1437 else if (cap->cmdchar == ':')
1438 {
1439 /* do_cmdline() has stored the first typed line in
1440 * "repeat_cmdline". When several lines are typed repeating
1441 * won't be possible. */
1442 if (repeat_cmdline == NULL)
1443 ResetRedobuff();
1444 else
1445 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001446 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001448 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 }
1450 }
1451 }
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (redo_VIsual_busy)
1454 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001455 /* Redo of an operation on a Visual area. Use the same size from
1456 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 oap->start = curwin->w_cursor;
1458 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1459 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1460 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1461 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001462 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001464 if (VIsual_mode == 'v')
1465 {
1466 if (redo_VIsual_line_count <= 1)
1467 {
1468 validate_virtcol();
1469 curwin->w_curswant =
1470 curwin->w_virtcol + redo_VIsual_vcol - 1;
1471 }
1472 else
1473 curwin->w_curswant = redo_VIsual_vcol;
1474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001476 {
1477 curwin->w_curswant = MAXCOL;
1478 }
1479 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481 cap->count0 = redo_VIsual_count;
1482 if (redo_VIsual_count != 0)
1483 cap->count1 = redo_VIsual_count;
1484 else
1485 cap->count1 = 1;
1486 }
1487 else if (VIsual_active)
1488 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001489 if (!gui_yank)
1490 {
1491 /* Save the current VIsual area for '< and '> marks, and "gv" */
1492 curbuf->b_visual.vi_start = VIsual;
1493 curbuf->b_visual.vi_end = curwin->w_cursor;
1494 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001495 if (VIsual_mode_orig != NUL)
1496 {
1497 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1498 VIsual_mode_orig = NUL;
1499 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001500 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001502 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
1506 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001507 * characterwise selection.
1508 * Special case: gH<Del> deletes the last line. */
1509 if (VIsual_select && VIsual_mode == 'V'
1510 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001512 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514 VIsual.col = 0;
1515 curwin->w_cursor.col =
1516 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1517 }
1518 else
1519 {
1520 curwin->w_cursor.col = 0;
1521 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1522 }
1523 VIsual_mode = 'v';
1524 }
1525 /* If 'selection' is "exclusive", backup one character for
1526 * charwise selections. */
1527 else if (VIsual_mode == 'v')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001528 include_line_break = unadjust_for_sel();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
1530 oap->start = VIsual;
1531 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001534 oap->start.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
1538 /*
1539 * Set oap->start to the first position of the operated text, oap->end
1540 * to the end of the operated text. w_cursor is equal to oap->start.
1541 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001542 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544#ifdef FEAT_FOLDING
1545 /* Include folded lines completely. */
1546 if (!VIsual_active)
1547 {
1548 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1549 oap->start.col = 0;
1550 if (hasFolding(curwin->w_cursor.lnum, NULL,
1551 &curwin->w_cursor.lnum))
1552 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1553 }
1554#endif
1555 oap->end = curwin->w_cursor;
1556 curwin->w_cursor = oap->start;
1557
1558 /* w_virtcol may have been updated; if the cursor goes back to its
1559 * previous position w_virtcol becomes invalid and isn't updated
1560 * automatically. */
1561 curwin->w_valid &= ~VALID_VIRTCOL;
1562 }
1563 else
1564 {
1565#ifdef FEAT_FOLDING
1566 /* Include folded lines completely. */
1567 if (!VIsual_active && oap->motion_type == MLINE)
1568 {
1569 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1570 NULL))
1571 curwin->w_cursor.col = 0;
1572 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1573 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1574 }
1575#endif
1576 oap->end = oap->start;
1577 oap->start = curwin->w_cursor;
1578 }
1579
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001580 /* Just in case lines were deleted that make the position invalid. */
1581 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 /* Set "virtual_op" before resetting VIsual_active. */
1585 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (VIsual_active || redo_VIsual_busy)
1588 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001589 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 if (!redo_VIsual_busy && !gui_yank)
1592 {
1593 /*
1594 * Prepare to reselect and redo Visual: this is based on the
1595 * size of the Visual text
1596 */
1597 resel_VIsual_mode = VIsual_mode;
1598 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001599 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001601 {
1602 if (VIsual_mode != Ctrl_V)
1603 getvvcol(curwin, &(oap->end),
1604 NULL, NULL, &oap->end_vcol);
1605 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1606 {
1607 if (VIsual_mode != Ctrl_V)
1608 getvvcol(curwin, &(oap->start),
1609 &oap->start_vcol, NULL, NULL);
1610 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1611 }
1612 else
1613 resel_VIsual_vcol = oap->end_vcol;
1614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 resel_VIsual_line_count = oap->line_count;
1616 }
1617
1618 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001619 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 && oap->op_type != OP_COLON
1621#ifdef FEAT_FOLDING
1622 && oap->op_type != OP_FOLD
1623 && oap->op_type != OP_FOLDOPEN
1624 && oap->op_type != OP_FOLDOPENREC
1625 && oap->op_type != OP_FOLDCLOSE
1626 && oap->op_type != OP_FOLDCLOSEREC
1627 && oap->op_type != OP_FOLDDEL
1628 && oap->op_type != OP_FOLDDELREC
1629#endif
1630 && oap->motion_force == NUL
1631 )
1632 {
1633 /* Prepare for redoing. Only use the nchar field for "r",
1634 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001635 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1636 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001637 prep_redo(oap->regname, cap->count0,
1638 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1639 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001640 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001641 {
1642 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1643
1644 /* reverse what nv_replace() did */
1645 if (nchar == REPLACE_CR_NCHAR)
1646 nchar = CAR;
1647 else if (nchar == REPLACE_NL_NCHAR)
1648 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001649 prep_redo(oap->regname, 0L, NUL, 'v',
1650 get_op_char(oap->op_type),
1651 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001652 nchar);
1653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (!redo_VIsual_busy)
1655 {
1656 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001657 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 redo_VIsual_line_count = resel_VIsual_line_count;
1659 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001660 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 }
1662 }
1663
1664 /*
1665 * oap->inclusive defaults to TRUE.
1666 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1667 * FALSE. This makes "d}P" and "v}dP" work the same.
1668 */
1669 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1670 oap->inclusive = TRUE;
1671 if (VIsual_mode == 'V')
1672 oap->motion_type = MLINE;
1673 else
1674 {
1675 oap->motion_type = MCHAR;
1676 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001677 && (include_line_break || !virtual_op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 {
1679 oap->inclusive = FALSE;
1680 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001681 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001682 if (*p_sel != 'o'
1683 && !op_on_lines(oap->op_type)
1684 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001686 ++oap->end.lnum;
1687 oap->end.col = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001688 oap->end.coladd = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001689 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 }
1691 }
1692 }
1693
1694 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 /*
1697 * Switch Visual off now, so screen updating does
1698 * not show inverted text when the screen is redrawn.
1699 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1700 * no screen redraw, so it is done here to remove the inverted
1701 * part.
1702 */
1703 if (!gui_yank)
1704 {
1705 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001706#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 setmouse();
1708 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001709#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001710 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 if ((oap->op_type == OP_YANK
1712 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001713 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 || oap->op_type == OP_FILTER)
1715 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001716 {
1717#ifdef FEAT_LINEBREAK
1718 /* make sure redrawing is correct */
1719 curwin->w_p_lbr = lbr_saved;
1720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
1724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 /* Include the trailing byte of a multi-byte char. */
1727 if (has_mbyte && oap->inclusive)
1728 {
1729 int l;
1730
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001731 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (l > 1)
1733 oap->end.col += l - 1;
1734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 curwin->w_set_curswant = TRUE;
1736
1737 /*
1738 * oap->empty is set when start and end are the same. The inclusive
1739 * flag affects this too, unless yanking and the end is on a NUL.
1740 */
1741 oap->empty = (oap->motion_type == MCHAR
1742 && (!oap->inclusive
1743 || (oap->op_type == OP_YANK
1744 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001745 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001746 && !(virtual_op && oap->start.coladd != oap->end.coladd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 /*
1748 * For delete, change and yank, it's an error to operate on an
1749 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1750 */
1751 empty_region_error = (oap->empty
1752 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 /* Force a redraw when operating on an empty Visual region, when
1755 * 'modifiable is off or creating a fold. */
1756 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001757#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001761 {
1762#ifdef FEAT_LINEBREAK
1763 curwin->w_p_lbr = lbr_saved;
1764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
1768 /*
1769 * If the end of an operator is in column one while oap->motion_type
1770 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1771 * character in the previous line. If op_start is on or before the
1772 * first non-blank in the line, the operator becomes linewise
1773 * (strange, but that's the way vi does it).
1774 */
1775 if ( oap->motion_type == MCHAR
1776 && oap->inclusive == FALSE
1777 && !(cap->retval & CA_NO_ADJ_OP_END)
1778 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001780 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 && oap->line_count > 1)
1782 {
1783 oap->end_adjusted = TRUE; /* remember that we did this */
1784 --oap->line_count;
1785 --oap->end.lnum;
1786 if (inindent(0))
1787 oap->motion_type = MLINE;
1788 else
1789 {
1790 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1791 if (oap->end.col)
1792 {
1793 --oap->end.col;
1794 oap->inclusive = TRUE;
1795 }
1796 }
1797 }
1798 else
1799 oap->end_adjusted = FALSE;
1800
1801 switch (oap->op_type)
1802 {
1803 case OP_LSHIFT:
1804 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001805 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 auto_format(FALSE, TRUE);
1807 break;
1808
1809 case OP_JOIN_NS:
1810 case OP_JOIN:
1811 if (oap->line_count < 2)
1812 oap->line_count = 2;
1813 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1814 curbuf->b_ml.ml_line_count)
1815 beep_flush();
1816 else
1817 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001818 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001819 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 auto_format(FALSE, TRUE);
1821 }
1822 break;
1823
1824 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001827 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001828 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001829 CancelRedo();
1830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 else
1832 {
1833 (void)op_delete(oap);
1834 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1835 u_save_cursor(); /* cursor line wasn't saved yet */
1836 auto_format(FALSE, TRUE);
1837 }
1838 break;
1839
1840 case OP_YANK:
1841 if (empty_region_error)
1842 {
1843 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001844 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001845 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001846 CancelRedo();
1847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001850 {
1851#ifdef FEAT_LINEBREAK
1852 curwin->w_p_lbr = lbr_saved;
1853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 check_cursor_col();
1857 break;
1858
1859 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001862 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001863 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001864 CancelRedo();
1865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 else
1867 {
1868 /* This is a new edit command, not a restart. Need to
1869 * remember it to make 'insertmode' work with mappings for
1870 * Visual mode. But do this only once and not when typed and
1871 * 'insertmode' isn't set. */
1872 if (p_im || !KeyTyped)
1873 restart_edit_save = restart_edit;
1874 else
1875 restart_edit_save = 0;
1876 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001877#ifdef FEAT_LINEBREAK
1878 /* Restore linebreak, so that when the user edits it looks as
1879 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001880 if (curwin->w_p_lbr != lbr_saved)
1881 {
1882 curwin->w_p_lbr = lbr_saved;
1883 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1884 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 /* Reset finish_op now, don't want it set inside edit(). */
1887 finish_op = FALSE;
1888 if (op_change(oap)) /* will call edit() */
1889 cap->retval |= CA_COMMAND_BUSY;
1890 if (restart_edit == 0)
1891 restart_edit = restart_edit_save;
1892 }
1893 break;
1894
1895 case OP_FILTER:
1896 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1897 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1898 else
1899 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001900 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 case OP_INDENT:
1903 case OP_COLON:
1904
1905#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1906 /*
1907 * If 'equalprg' is empty, do the indenting internally.
1908 */
1909 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1910 {
1911# ifdef FEAT_LISP
1912 if (curbuf->b_p_lisp)
1913 {
1914 op_reindent(oap, get_lisp_indent);
1915 break;
1916 }
1917# endif
1918# ifdef FEAT_CINDENT
1919 op_reindent(oap,
1920# ifdef FEAT_EVAL
1921 *curbuf->b_p_inde != NUL ? get_expr_indent :
1922# endif
1923 get_c_indent);
1924 break;
1925# endif
1926 }
1927#endif
1928
1929 op_colon(oap);
1930 break;
1931
1932 case OP_TILDE:
1933 case OP_UPPER:
1934 case OP_LOWER:
1935 case OP_ROT13:
1936 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001937 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001938 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001939 CancelRedo();
1940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 else
1942 op_tilde(oap);
1943 check_cursor_col();
1944 break;
1945
1946 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001947#if defined(FEAT_EVAL)
1948 if (*curbuf->b_p_fex != NUL)
1949 op_formatexpr(oap); /* use expression */
1950 else
1951#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001952 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 op_colon(oap); /* use external command */
1954 else
1955 op_format(oap, FALSE); /* use internal function */
1956 break;
1957
1958 case OP_FORMAT2:
1959 op_format(oap, TRUE); /* use internal function */
1960 break;
1961
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001962 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001963#ifdef FEAT_LINEBREAK
1964 /* Restore linebreak, so that when the user edits it looks as
1965 * before. */
1966 curwin->w_p_lbr = lbr_saved;
1967#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001968 op_function(oap); /* call 'operatorfunc' */
1969 break;
1970
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 case OP_INSERT:
1972 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001975 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001976 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001977 CancelRedo();
1978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 else
1980 {
1981 /* This is a new edit command, not a restart. Need to
1982 * remember it to make 'insertmode' work with mappings for
1983 * Visual mode. But do this only once. */
1984 restart_edit_save = restart_edit;
1985 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001986#ifdef FEAT_LINEBREAK
1987 /* Restore linebreak, so that when the user edits it looks as
1988 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001989 if (curwin->w_p_lbr != lbr_saved)
1990 {
1991 curwin->w_p_lbr = lbr_saved;
1992 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1993 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001996#ifdef FEAT_LINEBREAK
1997 /* Reset linebreak, so that formatting works correctly. */
1998 curwin->w_p_lbr = FALSE;
1999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 /* TODO: when inserting in several lines, should format all
2002 * the lines. */
2003 auto_format(FALSE, TRUE);
2004
2005 if (restart_edit == 0)
2006 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01002007 else
2008 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 break;
2011
2012 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002015 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002016 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002017 CancelRedo();
2018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002020 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002021#ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002022 /* Restore linebreak, so that when the user edits it looks as
2023 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002024 if (curwin->w_p_lbr != lbr_saved)
2025 {
2026 curwin->w_p_lbr = lbr_saved;
2027 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2028 }
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 break;
2033
2034#ifdef FEAT_FOLDING
2035 case OP_FOLD:
2036 VIsual_reselect = FALSE; /* don't reselect now */
2037 foldCreate(oap->start.lnum, oap->end.lnum);
2038 break;
2039
2040 case OP_FOLDOPEN:
2041 case OP_FOLDOPENREC:
2042 case OP_FOLDCLOSE:
2043 case OP_FOLDCLOSEREC:
2044 VIsual_reselect = FALSE; /* don't reselect now */
2045 opFoldRange(oap->start.lnum, oap->end.lnum,
2046 oap->op_type == OP_FOLDOPEN
2047 || oap->op_type == OP_FOLDOPENREC,
2048 oap->op_type == OP_FOLDOPENREC
2049 || oap->op_type == OP_FOLDCLOSEREC,
2050 oap->is_VIsual);
2051 break;
2052
2053 case OP_FOLDDEL:
2054 case OP_FOLDDELREC:
2055 VIsual_reselect = FALSE; /* don't reselect now */
2056 deleteFold(oap->start.lnum, oap->end.lnum,
2057 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2058 break;
2059#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002060 case OP_NR_ADD:
2061 case OP_NR_SUB:
2062 if (empty_region_error)
2063 {
2064 vim_beep(BO_OPER);
2065 CancelRedo();
2066 }
2067 else
2068 {
2069 VIsual_active = TRUE;
2070#ifdef FEAT_LINEBREAK
2071 curwin->w_p_lbr = lbr_saved;
2072#endif
2073 op_addsub(oap, cap->count1, redo_VIsual_arg);
2074 VIsual_active = FALSE;
2075 }
2076 check_cursor_col();
2077 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 default:
2079 clearopbeep(oap);
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 virtual_op = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (!gui_yank)
2083 {
2084 /*
2085 * if 'sol' not set, go back to old column for some commands
2086 */
2087 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2088 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2089 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002090 {
2091#ifdef FEAT_LINEBREAK
2092 curwin->w_p_lbr = FALSE;
2093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097 else
2098 {
2099 curwin->w_cursor = old_cursor;
2100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 clearop(oap);
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01002103 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002105#ifdef FEAT_LINEBREAK
2106 curwin->w_p_lbr = lbr_saved;
2107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108}
2109
2110/*
2111 * Handle indent and format operators and visual mode ":".
2112 */
2113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002114op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (oap->is_VIsual)
2118 stuffReadbuff((char_u *)"'<,'>");
2119 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
2121 /*
2122 * Make the range look nice, so it can be repeated.
2123 */
2124 if (oap->start.lnum == curwin->w_cursor.lnum)
2125 stuffcharReadbuff('.');
2126 else
2127 stuffnumReadbuff((long)oap->start.lnum);
2128 if (oap->end.lnum != oap->start.lnum)
2129 {
2130 stuffcharReadbuff(',');
2131 if (oap->end.lnum == curwin->w_cursor.lnum)
2132 stuffcharReadbuff('.');
2133 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2134 stuffcharReadbuff('$');
2135 else if (oap->start.lnum == curwin->w_cursor.lnum)
2136 {
2137 stuffReadbuff((char_u *)".+");
2138 stuffnumReadbuff((long)oap->line_count - 1);
2139 }
2140 else
2141 stuffnumReadbuff((long)oap->end.lnum);
2142 }
2143 }
2144 if (oap->op_type != OP_COLON)
2145 stuffReadbuff((char_u *)"!");
2146 if (oap->op_type == OP_INDENT)
2147 {
2148#ifndef FEAT_CINDENT
2149 if (*get_equalprg() == NUL)
2150 stuffReadbuff((char_u *)"indent");
2151 else
2152#endif
2153 stuffReadbuff(get_equalprg());
2154 stuffReadbuff((char_u *)"\n");
2155 }
2156 else if (oap->op_type == OP_FORMAT)
2157 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002158 if (*curbuf->b_p_fp != NUL)
2159 stuffReadbuff(curbuf->b_p_fp);
2160 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002162 else
2163 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002164 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166
2167 /*
2168 * do_cmdline() does the rest
2169 */
2170}
2171
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002172/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002173 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002174 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002175 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002176op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002177{
2178#ifdef FEAT_EVAL
Bram Moolenaarffa96842018-06-12 22:05:14 +02002179 typval_T argv[2];
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002180 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002181
2182 if (*p_opfunc == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002183 emsg(_("E774: 'operatorfunc' is empty"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002184 else
2185 {
2186 /* Set '[ and '] marks to text to be operated on. */
2187 curbuf->b_op_start = oap->start;
2188 curbuf->b_op_end = oap->end;
2189 if (oap->motion_type != MLINE && !oap->inclusive)
2190 /* Exclude the end position. */
2191 decl(&curbuf->b_op_end);
2192
Bram Moolenaarffa96842018-06-12 22:05:14 +02002193 argv[0].v_type = VAR_STRING;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002194 if (oap->block_mode)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002195 argv[0].vval.v_string = (char_u *)"block";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002196 else if (oap->motion_type == MLINE)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002197 argv[0].vval.v_string = (char_u *)"line";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002198 else
Bram Moolenaarffa96842018-06-12 22:05:14 +02002199 argv[0].vval.v_string = (char_u *)"char";
2200 argv[1].v_type = VAR_UNKNOWN;
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002201
2202 /* Reset virtual_op so that 'virtualedit' can be changed in the
2203 * function. */
2204 virtual_op = MAYBE;
2205
Bram Moolenaarded27a12018-08-01 19:06:03 +02002206 (void)call_func_retnr(p_opfunc, 1, argv);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002207
2208 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002209 }
2210#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002211 emsg(_("E775: Eval feature not available"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002212#endif
2213}
2214
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#if defined(FEAT_MOUSE) || defined(PROTO)
2216/*
2217 * Do the appropriate action for the current mouse click in the current mode.
2218 * Not used for Command-line mode.
2219 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002220 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * event modi- position visual change action
2222 * fier cursor window
2223 * left press - yes end yes
2224 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002225 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 * left drag - yes start if moved no
2227 * left relse - yes start if moved no
2228 * middle press - yes if not active no put register
2229 * middle press - yes if active no yank and put
2230 * right press - yes start or extend yes
2231 * right press S yes no change yes "#" (2)
2232 * right drag - yes extend no
2233 * right relse - yes extend no
2234 *
2235 * Insert or Replace Mode:
2236 * event modi- position visual change action
2237 * fier cursor window
2238 * left press - yes (cannot be active) yes
2239 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2240 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2241 * left drag - yes start or extend (1) no CTRL-O (1)
2242 * left relse - yes start or extend (1) no CTRL-O (1)
2243 * middle press - no (cannot be active) no put register
2244 * right press - yes start or extend yes CTRL-O
2245 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2246 *
2247 * (1) only if mouse pointer moved since press
2248 * (2) only if click is in same buffer
2249 *
2250 * Return TRUE if start_arrow() should be called for edit mode.
2251 */
2252 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002253do_mouse(
2254 oparg_T *oap, /* operator argument, can be NULL */
2255 int c, /* K_LEFTMOUSE, etc */
2256 int dir, /* Direction to 'put' if necessary */
2257 long count,
2258 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2261 static int got_click = FALSE; /* got a click some time back */
2262
2263 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2264 int is_click; /* If FALSE it's a drag or release event */
2265 int is_drag; /* If TRUE it's a drag event */
2266 int jump_flags = 0; /* flags for jump_to_mouse() */
2267 pos_T start_visual;
2268 int moved; /* Has cursor moved? */
2269 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002270 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 int c1, c2;
2273#if defined(FEAT_FOLDING)
2274 pos_T save_cursor;
2275#endif
2276 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 static pos_T orig_cursor;
2278 colnr_T leftcol, rightcol;
2279 pos_T end_visual;
2280 int diff;
2281 int old_active = VIsual_active;
2282 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 int regname;
2284
2285#if defined(FEAT_FOLDING)
2286 save_cursor = curwin->w_cursor;
2287#endif
2288
2289 /*
2290 * When GUI is active, always recognize mouse events, otherwise:
2291 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2292 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2293 * - For command line and insert mode 'mouse' is checked before calling
2294 * do_mouse().
2295 */
2296 if (do_always)
2297 do_always = FALSE;
2298 else
2299#ifdef FEAT_GUI
2300 if (!gui.in_use)
2301#endif
2302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (VIsual_active)
2304 {
2305 if (!mouse_has(MOUSE_VISUAL))
2306 return FALSE;
2307 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002308 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 return FALSE;
2310 }
2311
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002312 for (;;)
2313 {
2314 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2315 if (is_drag)
2316 {
2317 /* If the next character is the same mouse event then use that
2318 * one. Speeds up dragging the status line. */
2319 if (vpeekc() != NUL)
2320 {
2321 int nc;
2322 int save_mouse_row = mouse_row;
2323 int save_mouse_col = mouse_col;
2324
2325 /* Need to get the character, peeking doesn't get the actual
2326 * one. */
2327 nc = safe_vgetc();
2328 if (c == nc)
2329 continue;
2330 vungetc(nc);
2331 mouse_row = save_mouse_row;
2332 mouse_col = save_mouse_col;
2333 }
2334 }
2335 break;
2336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002338 if (c == K_MOUSEMOVE)
2339 {
2340 /* Mouse moved without a button pressed. */
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002341#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002342 ui_may_remove_balloon();
2343 if (p_bevalterm && !VIsual_active)
2344 {
2345 profile_setlimit(p_bdlay, &bevalexpr_due);
2346 bevalexpr_due_set = TRUE;
2347 }
2348#endif
2349 return FALSE;
2350 }
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#ifdef FEAT_MOUSESHAPE
2353 /* May have stopped dragging the status or separator line. The pointer is
2354 * most likely still on the status or separator line. */
2355 if (!is_drag && drag_status_line)
2356 {
2357 drag_status_line = FALSE;
2358 update_mouseshape(SHAPE_IDX_STATUS);
2359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (!is_drag && drag_sep_line)
2361 {
2362 drag_sep_line = FALSE;
2363 update_mouseshape(SHAPE_IDX_VSEP);
2364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366
2367 /*
2368 * Ignore drag and release events if we didn't get a click.
2369 */
2370 if (is_click)
2371 got_click = TRUE;
2372 else
2373 {
2374 if (!got_click) /* didn't get click, ignore */
2375 return FALSE;
2376 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002379 if (in_tab_line)
2380 {
2381 in_tab_line = FALSE;
2382 return FALSE;
2383 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 }
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 /*
2388 * CTRL right mouse button does CTRL-T
2389 */
2390 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2391 {
2392 if (State & INSERT)
2393 stuffcharReadbuff(Ctrl_O);
2394 if (count > 1)
2395 stuffnumReadbuff(count);
2396 stuffcharReadbuff(Ctrl_T);
2397 got_click = FALSE; /* ignore drag&release now */
2398 return FALSE;
2399 }
2400
2401 /*
2402 * CTRL only works with left mouse button
2403 */
2404 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2405 return FALSE;
2406
2407 /*
2408 * When a modifier is down, ignore drag and release events, as well as
2409 * multiple clicks and the middle mouse button.
2410 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2411 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002412 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2413 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 && (!is_click
2415 || (mod_mask & MOD_MASK_MULTI_CLICK)
2416 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002417 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 && mouse_model_popup()
2419 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002420 && !((mod_mask & MOD_MASK_ALT)
2421 && !mouse_model_popup()
2422 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 )
2424 return FALSE;
2425
2426 /*
2427 * If the button press was used as the movement command for an operator
2428 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2429 * drag/release events.
2430 */
2431 if (!is_click && which_button == MOUSE_MIDDLE)
2432 return FALSE;
2433
2434 if (oap != NULL)
2435 regname = oap->regname;
2436 else
2437 regname = 0;
2438
2439 /*
2440 * Middle mouse button does a 'put' of the selected text
2441 */
2442 if (which_button == MOUSE_MIDDLE)
2443 {
2444 if (State == NORMAL)
2445 {
2446 /*
2447 * If an operator was pending, we don't know what the user wanted
2448 * to do. Go back to normal mode: Clear the operator and beep().
2449 */
2450 if (oap != NULL && oap->op_type != OP_NOP)
2451 {
2452 clearopbeep(oap);
2453 return FALSE;
2454 }
2455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 /*
2457 * If visual was active, yank the highlighted text and put it
2458 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002459 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 */
2461 if (VIsual_active)
2462 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002463 if (VIsual_select)
2464 {
2465 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002466 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002467 }
2468 else
2469 {
2470 stuffcharReadbuff('y');
2471 stuffcharReadbuff(K_MIDDLEMOUSE);
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 do_always = TRUE; /* ignore 'mouse' setting next time */
2474 return FALSE;
2475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 /*
2477 * The rest is below jump_to_mouse()
2478 */
2479 }
2480
2481 else if ((State & INSERT) == 0)
2482 return FALSE;
2483
2484 /*
2485 * Middle click in insert mode doesn't move the mouse, just insert the
2486 * contents of a register. '.' register is special, can't insert that
2487 * with do_put().
2488 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2489 * happens for the GUI).
2490 */
2491 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2492 {
2493 if (regname == '.')
2494 insert_reg(regname, TRUE);
2495 else
2496 {
2497#ifdef FEAT_CLIPBOARD
2498 if (clip_star.available && regname == 0)
2499 regname = '*';
2500#endif
2501 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2502 insert_reg(regname, TRUE);
2503 else
2504 {
2505 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2506
2507 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2508 AppendCharToRedobuff(Ctrl_R);
2509 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2510 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2511 }
2512 }
2513 return FALSE;
2514 }
2515 }
2516
2517 /* When dragging or button-up stay in the same window. */
2518 if (!is_click)
2519 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2520
2521 start_visual.lnum = 0;
2522
Bram Moolenaarf740b292006-02-16 22:11:02 +00002523 /* Check for clicking in the tab page line. */
2524 if (mouse_row == 0 && firstwin->w_winrow > 0)
2525 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002526 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002527 {
2528 if (in_tab_line)
2529 {
2530 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002531 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2532 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002533 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002534 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002535 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002536
Bram Moolenaarf740b292006-02-16 22:11:02 +00002537 /* click in a tab selects that tab page */
2538 if (is_click
2539# ifdef FEAT_CMDWIN
2540 && cmdwin_type == 0
2541# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002542 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002543 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002544 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545 c1 = TabPageIdxs[mouse_col];
2546 if (c1 >= 0)
2547 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002548 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2549 {
2550 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002551 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002552 tabpage_new();
2553 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2554 }
2555 else
2556 {
2557 /* Go to specified tab page, or next one if not clicking
2558 * on a label. */
2559 goto_tabpage(c1);
2560
2561 /* It's like clicking on the status line of a window. */
2562 if (curwin != old_curwin)
2563 end_visual_mode();
2564 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002565 }
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002566 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002567 {
2568 tabpage_T *tp;
2569
2570 /* Close the current or specified tab page. */
2571 if (c1 == -999)
2572 tp = curtab;
2573 else
2574 tp = find_tabpage(-c1);
2575 if (tp == curtab)
2576 {
2577 if (first_tabpage->tp_next != NULL)
2578 tabpage_close(FALSE);
2579 }
2580 else if (tp != NULL)
2581 tabpage_close_other(tp, FALSE);
2582 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002583 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002584 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002585 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002586 else if (is_drag && in_tab_line)
2587 {
2588 c1 = TabPageIdxs[mouse_col];
2589 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2590 return FALSE;
2591 }
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 /*
2594 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2595 * right button up -> pop-up menu
2596 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002597 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 */
2599 if (mouse_model_popup())
2600 {
2601 if (which_button == MOUSE_RIGHT
2602 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2603 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002604#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002606 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2607 || defined(FEAT_TERM_POPUP_MENU)
2608# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 if (gui.in_use)
2610 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002611# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2612 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2613 if (!is_click)
2614 /* Ignore right button release events, only shows the popup
2615 * menu on the button down event. */
2616 return FALSE;
2617# endif
2618# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2619 if (is_click || is_drag)
2620 /* Ignore right button down and drag mouse events. Windows
2621 * only shows the popup menu on the button up event. */
2622 return FALSE;
2623# endif
2624 }
2625# endif
2626# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2627 else
2628# endif
2629# if defined(FEAT_TERM_POPUP_MENU)
2630 if (!is_click)
2631 /* Ignore right button release events, only shows the popup
2632 * menu on the button down event. */
2633 return FALSE;
2634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002636 jump_flags = 0;
2637 if (STRCMP(p_mousem, "popup_setpos") == 0)
2638 {
2639 /* First set the cursor position before showing the popup
2640 * menu. */
2641 if (VIsual_active)
2642 {
2643 pos_T m_pos;
2644
2645 /*
2646 * set MOUSE_MAY_STOP_VIS if we are outside the
2647 * selection or the current window (might have false
2648 * negative here)
2649 */
2650 if (mouse_row < curwin->w_winrow
2651 || mouse_row
2652 > (curwin->w_winrow + curwin->w_height))
2653 jump_flags = MOUSE_MAY_STOP_VIS;
2654 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2655 jump_flags = MOUSE_MAY_STOP_VIS;
2656 else
2657 {
2658 if ((LT_POS(curwin->w_cursor, VIsual)
2659 && (LT_POS(m_pos, curwin->w_cursor)
2660 || LT_POS(VIsual, m_pos)))
2661 || (LT_POS(VIsual, curwin->w_cursor)
2662 && (LT_POS(m_pos, VIsual)
2663 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002665 jump_flags = MOUSE_MAY_STOP_VIS;
2666 }
2667 else if (VIsual_mode == Ctrl_V)
2668 {
2669 getvcols(curwin, &curwin->w_cursor, &VIsual,
2670 &leftcol, &rightcol);
2671 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2672 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002677 else
2678 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002680 if (jump_flags)
2681 {
2682 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2683 update_curbuf(VIsual_active ? INVERTED : VALID);
2684 setcursor();
2685 out_flush(); /* Update before showing popup menu */
2686 }
2687# ifdef FEAT_MENU
2688 show_popupmenu();
2689 got_click = FALSE; /* ignore release events */
2690# endif
2691 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692#else
2693 return FALSE;
2694#endif
2695 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002696 if (which_button == MOUSE_LEFT
2697 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
2699 which_button = MOUSE_RIGHT;
2700 mod_mask &= ~MOD_MASK_SHIFT;
2701 }
2702 }
2703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if ((State & (NORMAL | INSERT))
2705 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2706 {
2707 if (which_button == MOUSE_LEFT)
2708 {
2709 if (is_click)
2710 {
2711 /* stop Visual mode for a left click in a window, but not when
2712 * on a status line */
2713 if (VIsual_active)
2714 jump_flags |= MOUSE_MAY_STOP_VIS;
2715 }
2716 else if (mouse_has(MOUSE_VISUAL))
2717 jump_flags |= MOUSE_MAY_VIS;
2718 }
2719 else if (which_button == MOUSE_RIGHT)
2720 {
2721 if (is_click && VIsual_active)
2722 {
2723 /*
2724 * Remember the start and end of visual before moving the
2725 * cursor.
2726 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002727 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
2729 start_visual = curwin->w_cursor;
2730 end_visual = VIsual;
2731 }
2732 else
2733 {
2734 start_visual = VIsual;
2735 end_visual = curwin->w_cursor;
2736 }
2737 }
2738 jump_flags |= MOUSE_FOCUS;
2739 if (mouse_has(MOUSE_VISUAL))
2740 jump_flags |= MOUSE_MAY_VIS;
2741 }
2742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 /*
2745 * If an operator is pending, ignore all drags and releases until the
2746 * next mouse click.
2747 */
2748 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2749 {
2750 got_click = FALSE;
2751 oap->motion_type = MCHAR;
2752 }
2753
2754 /* When releasing the button let jump_to_mouse() know. */
2755 if (!is_click && !is_drag)
2756 jump_flags |= MOUSE_RELEASED;
2757
2758 /*
2759 * JUMP!
2760 */
2761 jump_flags = jump_to_mouse(jump_flags,
2762 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002763
2764#ifdef FEAT_MENU
2765 /* A click in the window toolbar has no side effects. */
2766 if (jump_flags & MOUSE_WINBAR)
2767 return FALSE;
2768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 moved = (jump_flags & CURSOR_MOVED);
2770 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002774 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2776 {
2777 int key = KEY2TERMCAP1(c);
2778
2779 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2780 || key == (int)KE_RIGHTRELEASE)
2781 netbeans_button_release(which_button);
2782 }
2783#endif
2784
2785 /* When jumping to another window, clear a pending operator. That's a bit
2786 * friendlier than beeping and not jumping to that window. */
2787 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2788 clearop(oap);
2789
2790#ifdef FEAT_FOLDING
2791 if (mod_mask == 0
2792 && !is_drag
2793 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2794 && which_button == MOUSE_LEFT)
2795 {
2796 /* open or close a fold at this line */
2797 if (jump_flags & MOUSE_FOLD_OPEN)
2798 openFold(curwin->w_cursor.lnum, 1L);
2799 else
2800 closeFold(curwin->w_cursor.lnum, 1L);
2801 /* don't move the cursor if still in the same window */
2802 if (curwin == old_curwin)
2803 curwin->w_cursor = save_cursor;
2804 }
2805#endif
2806
2807#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2808 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2809 {
2810 clip_modeless(which_button, is_click, is_drag);
2811 return FALSE;
2812 }
2813#endif
2814
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002816 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 if (VIsual_active && is_drag && p_so)
2818 {
2819 /* In the very first line, allow scrolling one line */
2820 if (mouse_row == 0)
2821 mouse_dragging = 2;
2822 else
2823 mouse_dragging = 1;
2824 }
2825
2826 /* When dragging the mouse above the window, scroll down. */
2827 if (is_drag && mouse_row < 0 && !in_status_line)
2828 {
2829 scroll_redraw(FALSE, 1L);
2830 mouse_row = 0;
2831 }
2832
2833 if (start_visual.lnum) /* right click in visual mode */
2834 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002835 /* When ALT is pressed make Visual mode blockwise. */
2836 if (mod_mask & MOD_MASK_ALT)
2837 VIsual_mode = Ctrl_V;
2838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 /*
2840 * In Visual-block mode, divide the area in four, pick up the corner
2841 * that is in the quarter that the cursor is in.
2842 */
2843 if (VIsual_mode == Ctrl_V)
2844 {
2845 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2846 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2847 end_visual.col = leftcol;
2848 else
2849 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002850 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 end_visual.lnum = start_visual.lnum;
2853
2854 /* move VIsual to the right column */
2855 start_visual = curwin->w_cursor; /* save the cursor pos */
2856 curwin->w_cursor = end_visual;
2857 coladvance(end_visual.col);
2858 VIsual = curwin->w_cursor;
2859 curwin->w_cursor = start_visual; /* restore the cursor */
2860 }
2861 else
2862 {
2863 /*
2864 * If the click is before the start of visual, change the start.
2865 * If the click is after the end of visual, change the end. If
2866 * the click is inside the visual, change the closest side.
2867 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002868 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002870 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 VIsual = start_visual;
2872 else
2873 {
2874 /* In the same line, compare column number */
2875 if (end_visual.lnum == start_visual.lnum)
2876 {
2877 if (curwin->w_cursor.col - start_visual.col >
2878 end_visual.col - curwin->w_cursor.col)
2879 VIsual = start_visual;
2880 else
2881 VIsual = end_visual;
2882 }
2883
2884 /* In different lines, compare line number */
2885 else
2886 {
2887 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2888 (end_visual.lnum - curwin->w_cursor.lnum);
2889
2890 if (diff > 0) /* closest to end */
2891 VIsual = start_visual;
2892 else if (diff < 0) /* closest to start */
2893 VIsual = end_visual;
2894 else /* in the middle line */
2895 {
2896 if (curwin->w_cursor.col <
2897 (start_visual.col + end_visual.col) / 2)
2898 VIsual = end_visual;
2899 else
2900 VIsual = start_visual;
2901 }
2902 }
2903 }
2904 }
2905 }
2906 /*
2907 * If Visual mode started in insert mode, execute "CTRL-O"
2908 */
2909 else if ((State & INSERT) && VIsual_active)
2910 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912 /*
2913 * Middle mouse click: Put text before cursor.
2914 */
2915 if (which_button == MOUSE_MIDDLE)
2916 {
2917#ifdef FEAT_CLIPBOARD
2918 if (clip_star.available && regname == 0)
2919 regname = '*';
2920#endif
2921 if (yank_register_mline(regname))
2922 {
2923 if (mouse_past_bottom)
2924 dir = FORWARD;
2925 }
2926 else if (mouse_past_eol)
2927 dir = FORWARD;
2928
2929 if (fixindent)
2930 {
2931 c1 = (dir == BACKWARD) ? '[' : ']';
2932 c2 = 'p';
2933 }
2934 else
2935 {
2936 c1 = (dir == FORWARD) ? 'p' : 'P';
2937 c2 = NUL;
2938 }
2939 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2940
2941 /*
2942 * Remember where the paste started, so in edit() Insstart can be set
2943 * to this position
2944 */
2945 if (restart_edit != 0)
2946 where_paste_started = curwin->w_cursor;
2947 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2948 }
2949
Bram Moolenaar4033c552017-09-16 20:54:51 +02002950#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 /*
2952 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2953 * error under the mouse pointer.
2954 */
2955 else if (((mod_mask & MOD_MASK_CTRL)
2956 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2957 && bt_quickfix(curbuf))
2958 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002959 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002960 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002961 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002962 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 got_click = FALSE; /* ignore drag&release now */
2964 }
2965#endif
2966
2967 /*
2968 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2969 * under the mouse pointer.
2970 */
2971 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2972 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2973 {
2974 if (State & INSERT)
2975 stuffcharReadbuff(Ctrl_O);
2976 stuffcharReadbuff(Ctrl_RSB);
2977 got_click = FALSE; /* ignore drag&release now */
2978 }
2979
2980 /*
2981 * Shift-Mouse click searches for the next occurrence of the word under
2982 * the mouse pointer
2983 */
2984 else if ((mod_mask & MOD_MASK_SHIFT))
2985 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002986 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 stuffcharReadbuff(Ctrl_O);
2988 if (which_button == MOUSE_LEFT)
2989 stuffcharReadbuff('*');
2990 else /* MOUSE_RIGHT */
2991 stuffcharReadbuff('#');
2992 }
2993
2994 /* Handle double clicks, unless on status line */
2995 else if (in_status_line)
2996 {
2997#ifdef FEAT_MOUSESHAPE
2998 if ((is_drag || is_click) && !drag_status_line)
2999 {
3000 drag_status_line = TRUE;
3001 update_mouseshape(-1);
3002 }
3003#endif
3004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 else if (in_sep_line)
3006 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003007#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 if ((is_drag || is_click) && !drag_sep_line)
3009 {
3010 drag_sep_line = TRUE;
3011 update_mouseshape(-1);
3012 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003013#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3016 && mouse_has(MOUSE_VISUAL))
3017 {
3018 if (is_click || !VIsual_active)
3019 {
3020 if (VIsual_active)
3021 orig_cursor = VIsual;
3022 else
3023 {
3024 check_visual_highlight();
3025 VIsual = curwin->w_cursor;
3026 orig_cursor = VIsual;
3027 VIsual_active = TRUE;
3028 VIsual_reselect = TRUE;
3029 /* start Select mode if 'selectmode' contains "mouse" */
3030 may_start_select('o');
3031 setmouse();
3032 }
3033 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003034 {
3035 /* Double click with ALT pressed makes it blockwise. */
3036 if (mod_mask & MOD_MASK_ALT)
3037 VIsual_mode = Ctrl_V;
3038 else
3039 VIsual_mode = 'v';
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3042 VIsual_mode = 'V';
3043 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3044 VIsual_mode = Ctrl_V;
3045#ifdef FEAT_CLIPBOARD
3046 /* Make sure the clipboard gets updated. Needed because start and
3047 * end may still be the same, and the selection needs to be owned */
3048 clip_star.vmode = NUL;
3049#endif
3050 }
3051 /*
3052 * A double click selects a word or a block.
3053 */
3054 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3055 {
3056 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003057 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
3059 if (is_click)
3060 {
3061 /* If the character under the cursor (skipping white space) is
3062 * not a word character, try finding a match and select a (),
3063 * {}, [], #if/#endif, etc. block. */
3064 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003065 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 inc(&end_visual);
3067 if (oap != NULL)
3068 oap->motion_type = MCHAR;
3069 if (oap != NULL
3070 && VIsual_mode == 'v'
3071 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003072 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 && (pos = findmatch(oap, NUL)) != NULL)
3074 {
3075 curwin->w_cursor = *pos;
3076 if (oap->motion_type == MLINE)
3077 VIsual_mode = 'V';
3078 else if (*p_sel == 'e')
3079 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003080 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 ++VIsual.col;
3082 else
3083 ++curwin->w_cursor.col;
3084 }
3085 }
3086 }
3087
3088 if (pos == NULL && (is_click || is_drag))
3089 {
3090 /* When not found a match or when dragging: extend to include
3091 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003092 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 {
3094 find_start_of_word(&curwin->w_cursor);
3095 find_end_of_word(&VIsual);
3096 }
3097 else
3098 {
3099 find_start_of_word(&VIsual);
3100 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003102 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 find_end_of_word(&curwin->w_cursor);
3104 }
3105 }
3106 curwin->w_set_curswant = TRUE;
3107 }
3108 if (is_click)
3109 redraw_curbuf_later(INVERTED); /* update the inversion */
3110 }
3111 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003112 {
3113 if (mod_mask & MOD_MASK_ALT)
3114 VIsual_mode = Ctrl_V;
3115 else
3116 VIsual_mode = 'v';
3117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003120 if ((!VIsual_active && old_active && mode_displayed)
3121 || (VIsual_active && p_smd && msg_silent == 0
3122 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124
3125 return moved;
3126}
3127
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128/*
3129 * Move "pos" back to the start of the word it's in.
3130 */
3131 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003132find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 char_u *line;
3135 int cclass;
3136 int col;
3137
3138 line = ml_get(pos->lnum);
3139 cclass = get_mouse_class(line + pos->col);
3140
3141 while (pos->col > 0)
3142 {
3143 col = pos->col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 col -= (*mb_head_off)(line, line + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (get_mouse_class(line + col) != cclass)
3146 break;
3147 pos->col = col;
3148 }
3149}
3150
3151/*
3152 * Move "pos" forward to the end of the word it's in.
3153 * When 'selection' is "exclusive", the position is just after the word.
3154 */
3155 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003156find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 char_u *line;
3159 int cclass;
3160 int col;
3161
3162 line = ml_get(pos->lnum);
3163 if (*p_sel == 'e' && pos->col > 0)
3164 {
3165 --pos->col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 pos->col -= (*mb_head_off)(line, line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
3168 cclass = get_mouse_class(line + pos->col);
3169 while (line[pos->col] != NUL)
3170 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003171 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (get_mouse_class(line + col) != cclass)
3173 {
3174 if (*p_sel == 'e')
3175 pos->col = col;
3176 break;
3177 }
3178 pos->col = col;
3179 }
3180}
3181
3182/*
3183 * Get class of a character for selection: same class means same word.
3184 * 0: blank
3185 * 1: punctuation groups
3186 * 2: normal word character
3187 * >2: multi-byte word character.
3188 */
3189 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003190get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 int c;
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3195 return mb_get_class(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 c = *p;
3198 if (c == ' ' || c == '\t')
3199 return 0;
3200
3201 if (vim_iswordc(c))
3202 return 2;
3203
3204 /*
3205 * There are a few special cases where we want certain combinations of
3206 * characters to be considered as a single word. These are things like
3207 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003208 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 */
3210 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3211 return 1;
3212 return c;
3213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214#endif /* FEAT_MOUSE */
3215
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216/*
3217 * Check if highlighting for visual mode is possible, give a warning message
3218 * if not.
3219 */
3220 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003221check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 static int did_check = FALSE;
3224
3225 if (full_screen)
3226 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003227 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003228 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 did_check = TRUE;
3230 }
3231}
3232
3233/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003234 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 * This function should ALWAYS be called to end Visual mode, except from
3236 * do_pending_operator().
3237 */
3238 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003239end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240{
3241#ifdef FEAT_CLIPBOARD
3242 /*
3243 * If we are using the clipboard, then remember what was selected in case
3244 * we need to paste it somewhere while we still own the selection.
3245 * Only do this when the clipboard is already owned. Don't want to grab
3246 * the selection when hitting ESC.
3247 */
3248 if (clip_star.available && clip_star.owned)
3249 clip_auto_select();
3250#endif
3251
3252 VIsual_active = FALSE;
3253#ifdef FEAT_MOUSE
3254 setmouse();
3255 mouse_dragging = 0;
3256#endif
3257
3258 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003259 curbuf->b_visual.vi_mode = VIsual_mode;
3260 curbuf->b_visual.vi_start = VIsual;
3261 curbuf->b_visual.vi_end = curwin->w_cursor;
3262 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#ifdef FEAT_EVAL
3264 curbuf->b_visual_mode_eval = VIsual_mode;
3265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 if (!virtual_active())
3267 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003268 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003270 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271}
3272
3273/*
3274 * Reset VIsual_active and VIsual_reselect.
3275 */
3276 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003277reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
3279 if (VIsual_active)
3280 {
3281 end_visual_mode();
3282 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3283 }
3284 VIsual_reselect = FALSE;
3285}
3286
3287/*
3288 * Reset VIsual_active and VIsual_reselect if it's set.
3289 */
3290 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003291reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 if (VIsual_active)
3294 {
3295 end_visual_mode();
3296 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3297 VIsual_reselect = FALSE;
3298 }
3299}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301/*
3302 * Check for a balloon-eval special item to include when searching for an
3303 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3304 * Returns TRUE if the character at "*ptr" should be included.
3305 * "dir" is FORWARD or BACKWARD, the direction of searching.
3306 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3307 * "bnp" points to a counter for square brackets.
3308 */
3309 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003310find_is_eval_item(
3311 char_u *ptr,
3312 int *colp,
3313 int *bnp,
3314 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 /* Accept everything inside []. */
3317 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3318 ++*bnp;
3319 if (*bnp > 0)
3320 {
3321 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3322 --*bnp;
3323 return TRUE;
3324 }
3325
3326 /* skip over "s.var" */
3327 if (*ptr == '.')
3328 return TRUE;
3329
3330 /* two-character item: s->var */
3331 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3332 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3333 {
3334 *colp += dir;
3335 return TRUE;
3336 }
3337 return FALSE;
3338}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
3340/*
3341 * Find the identifier under or to the right of the cursor.
3342 * "find_type" can have one of three values:
3343 * FIND_IDENT: find an identifier (keyword)
3344 * FIND_STRING: find any non-white string
3345 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003346 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 *
3348 * There are three steps:
3349 * 1. Search forward for the start of an identifier/string. Doesn't move if
3350 * already on one.
3351 * 2. Search backward for the start of this identifier/string.
3352 * This doesn't match the real Vi but I like it a little better and it
3353 * shouldn't bother anyone.
3354 * 3. Search forward to the end of this identifier/string.
3355 * When FIND_IDENT isn't defined, we backup until a blank.
3356 *
3357 * Returns the length of the string, or zero if no string is found.
3358 * If a string is found, a pointer to the string is put in "*string". This
3359 * string is not always NUL terminated.
3360 */
3361 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003362find_ident_under_cursor(char_u **string, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
3365 curwin->w_cursor.col, string, find_type);
3366}
3367
3368/*
3369 * Like find_ident_under_cursor(), but for any window and any position.
3370 * However: Uses 'iskeyword' from the current window!.
3371 */
3372 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003373find_ident_at_pos(
3374 win_T *wp,
3375 linenr_T lnum,
3376 colnr_T startcol,
3377 char_u **string,
3378 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 char_u *ptr;
3381 int col = 0; /* init to shut up GCC */
3382 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 int this_class = 0;
3384 int prev_class;
3385 int prevcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 int bn = 0; /* bracket nesting */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387
3388 /*
3389 * if i == 0: try to find an identifier
3390 * if i == 1: try to find any non-white string
3391 */
3392 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3393 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3394 {
3395 /*
3396 * 1. skip to start of identifier/string
3397 */
3398 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (has_mbyte)
3400 {
3401 while (ptr[col] != NUL)
3402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 /* Stop at a ']' to evaluate "a[x]". */
3404 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3405 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 this_class = mb_get_class(ptr + col);
3407 if (this_class != 0 && (i == 1 || this_class != 1))
3408 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003409 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411 }
3412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003414 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 )
3417 ++col;
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 /* When starting on a ']' count it, so that we include the '['. */
3420 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
3422 /*
3423 * 2. Back up to start of identifier/string.
3424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 if (has_mbyte)
3426 {
3427 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3429 this_class = mb_get_class((char_u *)"a");
3430 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003432 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
3434 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3435 prev_class = mb_get_class(ptr + prevcol);
3436 if (this_class != prev_class
3437 && (i == 0
3438 || prev_class == 0
3439 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 && (!(find_type & FIND_EVAL)
3441 || prevcol == 0
3442 || !find_is_eval_item(ptr + prevcol, &prevcol,
3443 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 )
3445 break;
3446 col = prevcol;
3447 }
3448
3449 /* If we don't want just any old string, or we've found an
3450 * identifier, stop searching. */
3451 if (this_class > 2)
3452 this_class = 2;
3453 if (!(find_type & FIND_STRING) || this_class == 2)
3454 break;
3455 }
3456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
3458 while (col > 0
3459 && ((i == 0
3460 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003461 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 && (!(find_type & FIND_IDENT)
3463 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 || ((find_type & FIND_EVAL)
3465 && col > 1
3466 && find_is_eval_item(ptr + col - 1, &col,
3467 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 ))
3469 --col;
3470
3471 /* If we don't want just any old string, or we've found an
3472 * identifier, stop searching. */
3473 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3474 break;
3475 }
3476 }
3477
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003478 if (ptr[col] == NUL || (i == 0
3479 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
3481 /*
3482 * didn't find an identifier or string
3483 */
3484 if (find_type & FIND_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003485 emsg(_("E348: No string under cursor"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003487 emsg(_(e_noident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return 0;
3489 }
3490 ptr += col;
3491 *string = ptr;
3492
3493 /*
3494 * 3. Find the end if the identifier/string.
3495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 bn = 0;
3497 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 if (has_mbyte)
3500 {
3501 /* Search for point of changing multibyte character class. */
3502 this_class = mb_get_class(ptr);
3503 while (ptr[col] != NUL
3504 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3505 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 || ((find_type & FIND_EVAL)
3507 && col <= (int)startcol
3508 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003510 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003514 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 || ((find_type & FIND_EVAL)
3516 && col <= (int)startcol
3517 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 )
3519 {
3520 ++col;
3521 }
3522
3523 return col;
3524}
3525
3526/*
3527 * Prepare for redo of a normal command.
3528 */
3529 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003530prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
3532 prep_redo(cap->oap->regname, cap->count0,
3533 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3534}
3535
3536/*
3537 * Prepare for redo of any command.
3538 * Note that only the last argument can be a multi-byte char.
3539 */
3540 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003541prep_redo(
3542 int regname,
3543 long num,
3544 int cmd1,
3545 int cmd2,
3546 int cmd3,
3547 int cmd4,
3548 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549{
3550 ResetRedobuff();
3551 if (regname != 0) /* yank from specified buffer */
3552 {
3553 AppendCharToRedobuff('"');
3554 AppendCharToRedobuff(regname);
3555 }
3556 if (num)
3557 AppendNumberToRedobuff(num);
3558
3559 if (cmd1 != NUL)
3560 AppendCharToRedobuff(cmd1);
3561 if (cmd2 != NUL)
3562 AppendCharToRedobuff(cmd2);
3563 if (cmd3 != NUL)
3564 AppendCharToRedobuff(cmd3);
3565 if (cmd4 != NUL)
3566 AppendCharToRedobuff(cmd4);
3567 if (cmd5 != NUL)
3568 AppendCharToRedobuff(cmd5);
3569}
3570
3571/*
3572 * check for operator active and clear it
3573 *
3574 * return TRUE if operator was active
3575 */
3576 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003577checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 if (oap->op_type == OP_NOP)
3580 return FALSE;
3581 clearopbeep(oap);
3582 return TRUE;
3583}
3584
3585/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003586 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003588 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 */
3590 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003591checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003593 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 return FALSE;
3595 clearopbeep(oap);
3596 return TRUE;
3597}
3598
3599 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003600clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601{
3602 oap->op_type = OP_NOP;
3603 oap->regname = 0;
3604 oap->motion_force = NUL;
3605 oap->use_reg_one = FALSE;
3606}
3607
3608 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003609clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
3611 clearop(oap);
3612 beep_flush();
3613}
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615/*
3616 * Remove the shift modifier from a special key.
3617 */
3618 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003619unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 switch (cap->cmdchar)
3622 {
3623 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3624 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3625 case K_S_UP: cap->cmdchar = K_UP; break;
3626 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3627 case K_S_HOME: cap->cmdchar = K_HOME; break;
3628 case K_S_END: cap->cmdchar = K_END; break;
3629 }
3630 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003633/*
3634 * If the mode is currently displayed clear the command line or update the
3635 * command displayed.
3636 */
3637 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003638may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003639{
3640 if (mode_displayed)
3641 clear_cmdline = TRUE; /* unshow visual mode later */
3642#ifdef FEAT_CMDL_INFO
3643 else
3644 clear_showcmd();
3645#endif
3646}
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3649/*
3650 * Routines for displaying a partly typed command
3651 */
3652
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003653#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654static char_u showcmd_buf[SHOWCMD_BUFLEN];
3655static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3656static int showcmd_is_clear = TRUE;
3657static int showcmd_visual = FALSE;
3658
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003659static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003662clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
3664 if (!p_sc)
3665 return;
3666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (VIsual_active && !char_avail())
3668 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003669 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 long lines;
3671 colnr_T leftcol, rightcol;
3672 linenr_T top, bot;
3673
3674 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003675 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
3677 top = VIsual.lnum;
3678 bot = curwin->w_cursor.lnum;
3679 }
3680 else
3681 {
3682 top = curwin->w_cursor.lnum;
3683 bot = VIsual.lnum;
3684 }
3685# ifdef FEAT_FOLDING
3686 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003687 (void)hasFolding(top, &top, NULL);
3688 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689# endif
3690 lines = bot - top + 1;
3691
3692 if (VIsual_mode == Ctrl_V)
3693 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003694# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003695 char_u *saved_sbr = p_sbr;
3696
3697 /* Make 'sbr' empty for a moment to get the correct size. */
3698 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003701# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003702 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3705 (long)(rightcol - leftcol + 1));
3706 }
3707 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3708 sprintf((char *)showcmd_buf, "%ld", lines);
3709 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003710 {
3711 char_u *s, *e;
3712 int l;
3713 int bytes = 0;
3714 int chars = 0;
3715
3716 if (cursor_bot)
3717 {
3718 s = ml_get_pos(&VIsual);
3719 e = ml_get_cursor();
3720 }
3721 else
3722 {
3723 s = ml_get_cursor();
3724 e = ml_get_pos(&VIsual);
3725 }
3726 while ((*p_sel != 'e') ? s <= e : s < e)
3727 {
3728 l = (*mb_ptr2len)(s);
3729 if (l == 0)
3730 {
3731 ++bytes;
3732 ++chars;
3733 break; /* end of line */
3734 }
3735 bytes += l;
3736 ++chars;
3737 s += l;
3738 }
3739 if (bytes == chars)
3740 sprintf((char *)showcmd_buf, "%d", chars);
3741 else
3742 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3745 showcmd_visual = TRUE;
3746 }
3747 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 {
3749 showcmd_buf[0] = NUL;
3750 showcmd_visual = FALSE;
3751
3752 /* Don't actually display something if there is nothing to clear. */
3753 if (showcmd_is_clear)
3754 return;
3755 }
3756
3757 display_showcmd();
3758}
3759
3760/*
3761 * Add 'c' to string of shown command chars.
3762 * Return TRUE if output has been written (and setcursor() has been called).
3763 */
3764 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003765add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
3767 char_u *p;
3768 int old_len;
3769 int extra_len;
3770 int overflow;
3771#if defined(FEAT_MOUSE)
3772 int i;
3773 static int ignore[] =
3774 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003775# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3777 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003778# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003779 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003780 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3782 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003783 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003785 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 0
3787 };
3788#endif
3789
Bram Moolenaar09df3122006-01-23 22:23:09 +00003790 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 return FALSE;
3792
3793 if (showcmd_visual)
3794 {
3795 showcmd_buf[0] = NUL;
3796 showcmd_visual = FALSE;
3797 }
3798
3799#if defined(FEAT_MOUSE)
3800 /* Ignore keys that are scrollbar updates and mouse clicks */
3801 if (IS_SPECIAL(c))
3802 for (i = 0; ignore[i] != 0; ++i)
3803 if (ignore[i] == c)
3804 return FALSE;
3805#endif
3806
3807 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003808 if (*p == ' ')
3809 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 old_len = (int)STRLEN(showcmd_buf);
3811 extra_len = (int)STRLEN(p);
3812 overflow = old_len + extra_len - SHOWCMD_COLS;
3813 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003814 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3815 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 STRCAT(showcmd_buf, p);
3817
3818 if (char_avail())
3819 return FALSE;
3820
3821 display_showcmd();
3822
3823 return TRUE;
3824}
3825
3826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003827add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 if (!add_to_showcmd(c))
3830 setcursor();
3831}
3832
3833/*
3834 * Delete 'len' characters from the end of the shown command.
3835 */
3836 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003837del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838{
3839 int old_len;
3840
3841 if (!p_sc)
3842 return;
3843
3844 old_len = (int)STRLEN(showcmd_buf);
3845 if (len > old_len)
3846 len = old_len;
3847 showcmd_buf[old_len - len] = NUL;
3848
3849 if (!char_avail())
3850 display_showcmd();
3851}
3852
3853/*
3854 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3855 * something and there is a partial mapping.
3856 */
3857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003858push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
3860 if (p_sc)
3861 STRCPY(old_showcmd_buf, showcmd_buf);
3862}
3863
3864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003865pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
3867 if (!p_sc)
3868 return;
3869
3870 STRCPY(showcmd_buf, old_showcmd_buf);
3871
3872 display_showcmd();
3873}
3874
3875 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003876display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 int len;
3879
3880 cursor_off();
3881
3882 len = (int)STRLEN(showcmd_buf);
3883 if (len == 0)
3884 showcmd_is_clear = TRUE;
3885 else
3886 {
3887 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3888 showcmd_is_clear = FALSE;
3889 }
3890
3891 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003892 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3893 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 */
3895 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3896
3897 setcursor(); /* put cursor back where it belongs */
3898}
3899#endif
3900
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901/*
3902 * When "check" is FALSE, prepare for commands that scroll the window.
3903 * When "check" is TRUE, take care of scroll-binding after the window has
3904 * scrolled. Called from normal_cmd() and edit().
3905 */
3906 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003907do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
3909 static win_T *old_curwin = NULL;
3910 static linenr_T old_topline = 0;
3911#ifdef FEAT_DIFF
3912 static int old_topfill = 0;
3913#endif
3914 static buf_T *old_buf = NULL;
3915 static colnr_T old_leftcol = 0;
3916
3917 if (check && curwin->w_p_scb)
3918 {
3919 /* If a ":syncbind" command was just used, don't scroll, only reset
3920 * the values. */
3921 if (did_syncbind)
3922 did_syncbind = FALSE;
3923 else if (curwin == old_curwin)
3924 {
3925 /*
3926 * Synchronize other windows, as necessary according to
3927 * 'scrollbind'. Don't do this after an ":edit" command, except
3928 * when 'diff' is set.
3929 */
3930 if ((curwin->w_buffer == old_buf
3931#ifdef FEAT_DIFF
3932 || curwin->w_p_diff
3933#endif
3934 )
3935 && (curwin->w_topline != old_topline
3936#ifdef FEAT_DIFF
3937 || curwin->w_topfill != old_topfill
3938#endif
3939 || curwin->w_leftcol != old_leftcol))
3940 {
3941 check_scrollbind(curwin->w_topline - old_topline,
3942 (long)(curwin->w_leftcol - old_leftcol));
3943 }
3944 }
3945 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3946 {
3947 /*
3948 * When switching between windows, make sure that the relative
3949 * vertical offset is valid for the new window. The relative
3950 * offset is invalid whenever another 'scrollbind' window has
3951 * scrolled to a point that would force the current window to
3952 * scroll past the beginning or end of its buffer. When the
3953 * resync is performed, some of the other 'scrollbind' windows may
3954 * need to jump so that the current window's relative position is
3955 * visible on-screen.
3956 */
3957 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3958 }
3959 curwin->w_scbind_pos = curwin->w_topline;
3960 }
3961
3962 old_curwin = curwin;
3963 old_topline = curwin->w_topline;
3964#ifdef FEAT_DIFF
3965 old_topfill = curwin->w_topfill;
3966#endif
3967 old_buf = curwin->w_buffer;
3968 old_leftcol = curwin->w_leftcol;
3969}
3970
3971/*
3972 * Synchronize any windows that have "scrollbind" set, based on the
3973 * number of rows by which the current window has changed
3974 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3975 */
3976 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003977check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 int want_ver;
3980 int want_hor;
3981 win_T *old_curwin = curwin;
3982 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 int old_VIsual_select = VIsual_select;
3984 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 colnr_T tgt_leftcol = curwin->w_leftcol;
3986 long topline;
3987 long y;
3988
3989 /*
3990 * check 'scrollopt' string for vertical and horizontal scroll options
3991 */
3992 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3993#ifdef FEAT_DIFF
3994 want_ver |= old_curwin->w_p_diff;
3995#endif
3996 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3997
3998 /*
3999 * loop through the scrollbound windows and scroll accordingly
4000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004002 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
4004 curbuf = curwin->w_buffer;
4005 /* skip original window and windows with 'noscrollbind' */
4006 if (curwin != old_curwin && curwin->w_p_scb)
4007 {
4008 /*
4009 * do the vertical scroll
4010 */
4011 if (want_ver)
4012 {
4013#ifdef FEAT_DIFF
4014 if (old_curwin->w_p_diff && curwin->w_p_diff)
4015 {
4016 diff_set_topline(old_curwin, curwin);
4017 }
4018 else
4019#endif
4020 {
4021 curwin->w_scbind_pos += topline_diff;
4022 topline = curwin->w_scbind_pos;
4023 if (topline > curbuf->b_ml.ml_line_count)
4024 topline = curbuf->b_ml.ml_line_count;
4025 if (topline < 1)
4026 topline = 1;
4027
4028 y = topline - curwin->w_topline;
4029 if (y > 0)
4030 scrollup(y, FALSE);
4031 else
4032 scrolldown(-y, FALSE);
4033 }
4034
4035 redraw_later(VALID);
4036 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039
4040 /*
4041 * do the horizontal scroll
4042 */
4043 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4044 {
4045 curwin->w_leftcol = tgt_leftcol;
4046 leftcol_changed();
4047 }
4048 }
4049 }
4050
4051 /*
4052 * reset current-window
4053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 VIsual_select = old_VIsual_select;
4055 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 curwin = old_curwin;
4057 curbuf = old_curbuf;
4058}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060/*
4061 * Command character that's ignored.
4062 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004063 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004066nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004068 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069}
4070
4071/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004072 * Command character that doesn't do anything, but unlike nv_ignore() does
4073 * start edit(). Used for "startinsert" executed while starting up.
4074 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004075 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004076nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004077{
4078}
4079
4080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 * Command character doesn't exist.
4082 */
4083 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004084nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086 clearopbeep(cap->oap);
4087}
4088
4089/*
4090 * <Help> and <F1> commands.
4091 */
4092 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004093nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 if (!checkclearopq(cap->oap))
4096 ex_help(NULL);
4097}
4098
4099/*
4100 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4101 */
4102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004103nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
Bram Moolenaarf2732452018-06-03 14:47:35 +02004105#ifdef FEAT_JOB_CHANNEL
4106 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4107 clearopbeep(cap->oap);
4108 else
4109#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01004110 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004111 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004112 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004113 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4114 op_addsub(cap->oap, cap->count1, cap->arg);
4115 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004116 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004117 else if (VIsual_active)
4118 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004119 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004120 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121}
4122
4123/*
4124 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4125 */
4126 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004127nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004130 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004131 if (mod_mask & MOD_MASK_CTRL)
4132 {
4133 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4134 if (cap->arg == BACKWARD)
4135 goto_tabpage(-(int)cap->count1);
4136 else
4137 goto_tabpage((int)cap->count0);
4138 }
4139 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004140 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142}
4143
4144/*
4145 * Implementation of "gd" and "gD" command.
4146 */
4147 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004148nv_gd(
4149 oparg_T *oap,
4150 int nchar,
4151 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 char_u *ptr;
4155
Bram Moolenaard9d30582005-05-18 22:10:28 +00004156 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004157 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4158 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004160#ifdef FEAT_FOLDING
4161 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4162 foldOpenCursor();
4163#endif
4164}
4165
4166/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004167 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4168 * otherwise.
4169 */
4170 static int
4171is_ident(char_u *line, int offset)
4172{
4173 int i;
4174 int incomment = FALSE;
4175 int instring = 0;
4176 int prev = 0;
4177
4178 for (i = 0; i < offset && line[i] != NUL; i++)
4179 {
4180 if (instring != 0)
4181 {
4182 if (prev != '\\' && line[i] == instring)
4183 instring = 0;
4184 }
4185 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4186 {
4187 instring = line[i];
4188 }
4189 else
4190 {
4191 if (incomment)
4192 {
4193 if (prev == '*' && line[i] == '/')
4194 incomment = FALSE;
4195 }
4196 else if (prev == '/' && line[i] == '*')
4197 {
4198 incomment = TRUE;
4199 }
4200 else if (prev == '/' && line[i] == '/')
4201 {
4202 return FALSE;
4203 }
4204 }
4205
4206 prev = line[i];
4207 }
4208
4209 return incomment == FALSE && instring == 0;
4210}
4211
4212/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004213 * Search for variable declaration of "ptr[len]".
4214 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4215 * current file ("gD").
4216 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004217 * Return FAIL when not found.
4218 */
4219 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004220find_decl(
4221 char_u *ptr,
4222 int len,
4223 int locally,
4224 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004225 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004226{
4227 char_u *pat;
4228 pos_T old_pos;
4229 pos_T par_pos;
4230 pos_T found_pos;
4231 int t;
4232 int save_p_ws;
4233 int save_p_scs;
4234 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004235 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004236 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004237 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004238
4239 if ((pat = alloc(len + 7)) == NULL)
4240 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004241
4242 /* Put "\V" before the pattern to avoid that the special meaning of "."
4243 * and "~" causes trouble. */
4244 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4245 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 old_pos = curwin->w_cursor;
4247 save_p_ws = p_ws;
4248 save_p_scs = p_scs;
4249 p_ws = FALSE; /* don't wrap around end of file now */
4250 p_scs = FALSE; /* don't switch ignorecase off now */
4251
4252 /*
4253 * With "gD" go to line 1.
4254 * With "gd" Search back for the start of the current function, then go
4255 * back until a blank line. If this fails go to line 1.
4256 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004257 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
4259 setpcmark(); /* Set in findpar() otherwise */
4260 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004261 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 else
4264 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004265 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4267 --curwin->w_cursor.lnum;
4268 }
4269 curwin->w_cursor.col = 0;
4270
4271 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004272 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004273 for (;;)
4274 {
Bram Moolenaar226630a2016-10-08 19:21:31 +02004275 valid = FALSE;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004276 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004277 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004278 if (curwin->w_cursor.lnum >= old_pos.lnum)
4279 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004280
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004281 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004282 {
4283 pos_T *pos;
4284
4285 /* Check that the block the match is in doesn't end before the
4286 * position where we started the search from. */
4287 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4288 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4289 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004290 {
4291 /* There can't be a useful match before the end of this block.
4292 * Skip to the end. */
4293 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004294 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004295 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004296 }
4297
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004298 if (t == FAIL)
4299 {
4300 /* If we previously found a valid position, use it. */
4301 if (found_pos.lnum != 0)
4302 {
4303 curwin->w_cursor = found_pos;
4304 t = OK;
4305 }
4306 break;
4307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004309 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004310 {
4311 /* Ignore this line, continue at start of next line. */
4312 ++curwin->w_cursor.lnum;
4313 curwin->w_cursor.col = 0;
4314 continue;
4315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004317 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4318
4319 /* If the current position is not a valid identifier and a previous
4320 * match is present, favor that one instead. */
4321 if (!valid && found_pos.lnum != 0)
4322 {
4323 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004324 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004325 }
4326
4327 /* Global search: use first valid match found */
4328 if (valid && !locally)
4329 break;
4330 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004331 {
4332 /* If we previously found a valid position, use it. */
4333 if (found_pos.lnum != 0)
4334 curwin->w_cursor = found_pos;
4335 break;
4336 }
4337
Bram Moolenaar226630a2016-10-08 19:21:31 +02004338 /* For finding a local variable and the match is before the "{" or
4339 * inside a comment, continue searching. For K&R style function
4340 * declarations this skips the function header without types. */
4341 if (!valid)
4342 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004343 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004344 }
4345 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004346 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004347 /* Remove SEARCH_START from flags to avoid getting stuck at one
4348 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004349 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004351
4352 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004354 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 curwin->w_cursor = old_pos;
4356 }
4357 else
4358 {
4359 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 /* "n" searches forward now */
4361 reset_search_dir();
4362 }
4363
4364 vim_free(pat);
4365 p_ws = save_p_ws;
4366 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004367
4368 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369}
4370
4371/*
4372 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4373 * lines rather than lines in the file.
4374 * 'dist' must be positive.
4375 *
4376 * Return OK if able to move cursor, FAIL otherwise.
4377 */
4378 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004379nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380{
4381 int linelen = linetabsize(ml_get_curline());
4382 int retval = OK;
4383 int atend = FALSE;
4384 int n;
4385 int col_off1; /* margin offset for first screen line */
4386 int col_off2; /* margin offset for wrapped screen line */
4387 int width1; /* text width for first screen line */
4388 int width2; /* test width for wrapped screen line */
4389
4390 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004391 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 col_off1 = curwin_col_off();
4394 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004395 width1 = curwin->w_width - col_off1;
4396 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004397 if (width2 == 0)
4398 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 /*
4403 * Instead of sticking at the last character of the buffer line we
4404 * try to stick in the last column of the screen.
4405 */
4406 if (curwin->w_curswant == MAXCOL)
4407 {
4408 atend = TRUE;
4409 validate_virtcol();
4410 if (width1 <= 0)
4411 curwin->w_curswant = 0;
4412 else
4413 {
4414 curwin->w_curswant = width1 - 1;
4415 if (curwin->w_virtcol > curwin->w_curswant)
4416 curwin->w_curswant += ((curwin->w_virtcol
4417 - curwin->w_curswant - 1) / width2 + 1) * width2;
4418 }
4419 }
4420 else
4421 {
4422 if (linelen > width1)
4423 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4424 else
4425 n = width1;
4426 if (curwin->w_curswant > (colnr_T)n + 1)
4427 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4428 * width2;
4429 }
4430
4431 while (dist--)
4432 {
4433 if (dir == BACKWARD)
4434 {
4435 if ((long)curwin->w_curswant >= width2)
4436 /* move back within line */
4437 curwin->w_curswant -= width2;
4438 else
4439 {
4440 /* to previous line */
4441 if (curwin->w_cursor.lnum == 1)
4442 {
4443 retval = FAIL;
4444 break;
4445 }
4446 --curwin->w_cursor.lnum;
4447#ifdef FEAT_FOLDING
4448 /* Move to the start of a closed fold. Don't do that when
4449 * 'foldopen' contains "all": it will open in a moment. */
4450 if (!(fdo_flags & FDO_ALL))
4451 (void)hasFolding(curwin->w_cursor.lnum,
4452 &curwin->w_cursor.lnum, NULL);
4453#endif
4454 linelen = linetabsize(ml_get_curline());
4455 if (linelen > width1)
4456 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4457 + 1) * width2;
4458 }
4459 }
4460 else /* dir == FORWARD */
4461 {
4462 if (linelen > width1)
4463 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4464 else
4465 n = width1;
4466 if (curwin->w_curswant + width2 < (colnr_T)n)
4467 /* move forward within line */
4468 curwin->w_curswant += width2;
4469 else
4470 {
4471 /* to next line */
4472#ifdef FEAT_FOLDING
4473 /* Move to the end of a closed fold. */
4474 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4475 &curwin->w_cursor.lnum);
4476#endif
4477 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4478 {
4479 retval = FAIL;
4480 break;
4481 }
4482 curwin->w_cursor.lnum++;
4483 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004484 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486 }
4487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004490 if (virtual_active() && atend)
4491 coladvance(MAXCOL);
4492 else
4493 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4496 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004497 colnr_T virtcol;
4498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 /*
4500 * Check for landing on a character that got split at the end of the
4501 * last line. We want to advance a screenline, not end up in the same
4502 * screenline or move two screenlines.
4503 */
4504 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004505 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004506#if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004507 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4508 virtcol -= vim_strsize(p_sbr);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004509#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004510
4511 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 && (curwin->w_curswant < (colnr_T)width1
4513 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4514 : ((curwin->w_curswant - width1) % width2
4515 > (colnr_T)width2 / 2)))
4516 --curwin->w_cursor.col;
4517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
4519 if (atend)
4520 curwin->w_curswant = MAXCOL; /* stick in the last column */
4521
4522 return retval;
4523}
4524
4525#ifdef FEAT_MOUSE
4526/*
4527 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4528 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004529 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4530 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 */
4532 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004533nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004535 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004537 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 {
4539 int row, col;
4540
4541 row = mouse_row;
4542 col = mouse_col;
4543
4544 /* find the window at the pointer coordinates */
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004545 wp = mouse_find_win(&row, &col);
4546 if (wp == NULL)
4547 return;
4548 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 curbuf = curwin->w_buffer;
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004552 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004554# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004555 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004556 /* This window is a terminal window, send the mouse event there.
4557 * Set "typed" to FALSE to avoid an endless loop. */
4558 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004559 else
4560# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004561 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4562 {
4563 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4564 }
4565 else
4566 {
4567 cap->count1 = 3;
4568 cap->count0 = 3;
4569 nv_scroll_line(cap);
4570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004572# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 else
4574 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004575 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4576 if (!curwin->w_p_wrap)
4577 {
4578 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004579
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004580 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004581 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004582 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4583 if (val < 0)
4584 val = 0;
4585
4586 gui_do_horiz_scroll(val, TRUE);
4587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004589# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 curwin->w_redr_status = TRUE;
4592
4593 curwin = old_curwin;
4594 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595}
4596
4597/*
4598 * Mouse clicks and drags.
4599 */
4600 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004601nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602{
4603 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4604}
4605#endif
4606
4607/*
4608 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4609 * cap->arg must be TRUE for CTRL-E.
4610 */
4611 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004612nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613{
4614 if (!checkclearop(cap->oap))
4615 scroll_redraw(cap->arg, cap->count1);
4616}
4617
4618/*
4619 * Scroll "count" lines up or down, and redraw.
4620 */
4621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004622scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 linenr_T prev_topline = curwin->w_topline;
4625#ifdef FEAT_DIFF
4626 int prev_topfill = curwin->w_topfill;
4627#endif
4628 linenr_T prev_lnum = curwin->w_cursor.lnum;
4629
4630 if (up)
4631 scrollup(count, TRUE);
4632 else
4633 scrolldown(count, TRUE);
4634 if (p_so)
4635 {
4636 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4637 * valid, otherwise the screen jumps back at the end of the file. */
4638 cursor_correct();
4639 check_cursor_moved(curwin);
4640 curwin->w_valid |= VALID_TOPLINE;
4641
4642 /* If moved back to where we were, at least move the cursor, otherwise
4643 * we get stuck at one position. Don't move the cursor up if the
4644 * first line of the buffer is already on the screen */
4645 while (curwin->w_topline == prev_topline
4646#ifdef FEAT_DIFF
4647 && curwin->w_topfill == prev_topfill
4648#endif
4649 )
4650 {
4651 if (up)
4652 {
4653 if (curwin->w_cursor.lnum > prev_lnum
4654 || cursor_down(1L, FALSE) == FAIL)
4655 break;
4656 }
4657 else
4658 {
4659 if (curwin->w_cursor.lnum < prev_lnum
4660 || prev_topline == 1L
4661 || cursor_up(1L, FALSE) == FAIL)
4662 break;
4663 }
4664 /* Mark w_topline as valid, otherwise the screen jumps back at the
4665 * end of the file. */
4666 check_cursor_moved(curwin);
4667 curwin->w_valid |= VALID_TOPLINE;
4668 }
4669 }
4670 if (curwin->w_cursor.lnum != prev_lnum)
4671 coladvance(curwin->w_curswant);
4672 redraw_later(VALID);
4673}
4674
4675/*
4676 * Commands that start with "z".
4677 */
4678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004679nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680{
4681 long n;
4682 colnr_T col;
4683 int nchar = cap->nchar;
4684#ifdef FEAT_FOLDING
4685 long old_fdl = curwin->w_p_fdl;
4686 int old_fen = curwin->w_p_fen;
4687#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004688#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004689 int undo = FALSE;
4690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
4692 if (VIM_ISDIGIT(nchar))
4693 {
4694 /*
4695 * "z123{nchar}": edit the count before obtaining {nchar}
4696 */
4697 if (checkclearop(cap->oap))
4698 return;
4699 n = nchar - '0';
4700 for (;;)
4701 {
4702#ifdef USE_ON_FLY_SCROLL
4703 dont_scroll = TRUE; /* disallow scrolling here */
4704#endif
4705 ++no_mapping;
4706 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004707 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 --no_mapping;
4710 --allow_keys;
4711#ifdef FEAT_CMDL_INFO
4712 (void)add_to_showcmd(nchar);
4713#endif
4714 if (nchar == K_DEL || nchar == K_KDEL)
4715 n /= 10;
4716 else if (VIM_ISDIGIT(nchar))
4717 n = n * 10 + (nchar - '0');
4718 else if (nchar == CAR)
4719 {
4720#ifdef FEAT_GUI
4721 need_mouse_correct = TRUE;
4722#endif
4723 win_setheight((int)n);
4724 break;
4725 }
4726 else if (nchar == 'l'
4727 || nchar == 'h'
4728 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004729 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 {
4731 cap->count1 = n ? n * cap->count1 : cap->count1;
4732 goto dozet;
4733 }
4734 else
4735 {
4736 clearopbeep(cap->oap);
4737 break;
4738 }
4739 }
4740 cap->oap->op_type = OP_NOP;
4741 return;
4742 }
4743
4744dozet:
4745 if (
4746#ifdef FEAT_FOLDING
4747 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4748 * and "zC" only in Visual mode. "zj" and "zk" are motion
4749 * commands. */
4750 cap->nchar != 'f' && cap->nchar != 'F'
4751 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4752 && cap->nchar != 'j' && cap->nchar != 'k'
4753 &&
4754#endif
4755 checkclearop(cap->oap))
4756 return;
4757
4758 /*
4759 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4760 * If line number given, set cursor.
4761 */
4762 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4763 && cap->count0
4764 && cap->count0 != curwin->w_cursor.lnum)
4765 {
4766 setpcmark();
4767 if (cap->count0 > curbuf->b_ml.ml_line_count)
4768 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4769 else
4770 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004771 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
4773
4774 switch (nchar)
4775 {
4776 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4777 case '+':
4778 if (cap->count0 == 0)
4779 {
4780 /* No count given: put cursor at the line below screen */
4781 validate_botline(); /* make sure w_botline is valid */
4782 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4783 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4784 else
4785 curwin->w_cursor.lnum = curwin->w_botline;
4786 }
4787 /* FALLTHROUGH */
4788 case NL:
4789 case CAR:
4790 case K_KENTER:
4791 beginline(BL_WHITE | BL_FIX);
4792 /* FALLTHROUGH */
4793
4794 case 't': scroll_cursor_top(0, TRUE);
4795 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004796 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 break;
4798
4799 /* "z." and "zz": put cursor in middle of screen */
4800 case '.': beginline(BL_WHITE | BL_FIX);
4801 /* FALLTHROUGH */
4802
4803 case 'z': scroll_cursor_halfway(TRUE);
4804 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004805 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 break;
4807
4808 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4809 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4810 * when <count> is at bottom of window, and puts that one at
4811 * bottom of window. */
4812 if (cap->count0 != 0)
4813 {
4814 scroll_cursor_bot(0, TRUE);
4815 curwin->w_cursor.lnum = curwin->w_topline;
4816 }
4817 else if (curwin->w_topline == 1)
4818 curwin->w_cursor.lnum = 1;
4819 else
4820 curwin->w_cursor.lnum = curwin->w_topline - 1;
4821 /* FALLTHROUGH */
4822 case '-':
4823 beginline(BL_WHITE | BL_FIX);
4824 /* FALLTHROUGH */
4825
4826 case 'b': scroll_cursor_bot(0, TRUE);
4827 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004828 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 break;
4830
4831 /* "zH" - scroll screen right half-page */
4832 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004833 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 /* FALLTHROUGH */
4835
4836 /* "zh" - scroll screen to the right */
4837 case 'h':
4838 case K_LEFT:
4839 if (!curwin->w_p_wrap)
4840 {
4841 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4842 curwin->w_leftcol = 0;
4843 else
4844 curwin->w_leftcol -= (colnr_T)cap->count1;
4845 leftcol_changed();
4846 }
4847 break;
4848
4849 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004850 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 /* FALLTHROUGH */
4852
4853 /* "zl" - scroll screen to the left */
4854 case 'l':
4855 case K_RIGHT:
4856 if (!curwin->w_p_wrap)
4857 {
4858 /* scroll the window left */
4859 curwin->w_leftcol += (colnr_T)cap->count1;
4860 leftcol_changed();
4861 }
4862 break;
4863
4864 /* "zs" - scroll screen, cursor at the start */
4865 case 's': if (!curwin->w_p_wrap)
4866 {
4867#ifdef FEAT_FOLDING
4868 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4869 col = 0; /* like the cursor is in col 0 */
4870 else
4871#endif
4872 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
4873 if ((long)col > p_siso)
4874 col -= p_siso;
4875 else
4876 col = 0;
4877 if (curwin->w_leftcol != col)
4878 {
4879 curwin->w_leftcol = col;
4880 redraw_later(NOT_VALID);
4881 }
4882 }
4883 break;
4884
4885 /* "ze" - scroll screen, cursor at the end */
4886 case 'e': if (!curwin->w_p_wrap)
4887 {
4888#ifdef FEAT_FOLDING
4889 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4890 col = 0; /* like the cursor is in col 0 */
4891 else
4892#endif
4893 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004894 n = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if ((long)col + p_siso < n)
4896 col = 0;
4897 else
4898 col = col + p_siso - n + 1;
4899 if (curwin->w_leftcol != col)
4900 {
4901 curwin->w_leftcol = col;
4902 redraw_later(NOT_VALID);
4903 }
4904 }
4905 break;
4906
4907#ifdef FEAT_FOLDING
4908 /* "zF": create fold command */
4909 /* "zf": create fold operator */
4910 case 'F':
4911 case 'f': if (foldManualAllowed(TRUE))
4912 {
4913 cap->nchar = 'f';
4914 nv_operator(cap);
4915 curwin->w_p_fen = TRUE;
4916
4917 /* "zF" is like "zfzf" */
4918 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4919 {
4920 nv_operator(cap);
4921 finish_op = TRUE;
4922 }
4923 }
4924 else
4925 clearopbeep(cap->oap);
4926 break;
4927
4928 /* "zd": delete fold at cursor */
4929 /* "zD": delete fold at cursor recursively */
4930 case 'd':
4931 case 'D': if (foldManualAllowed(FALSE))
4932 {
4933 if (VIsual_active)
4934 nv_operator(cap);
4935 else
4936 deleteFold(curwin->w_cursor.lnum,
4937 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4938 }
4939 break;
4940
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004941 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 case 'E': if (foldmethodIsManual(curwin))
4943 {
4944 clearFolding(curwin);
4945 changed_window_setting();
4946 }
4947 else if (foldmethodIsMarker(curwin))
4948 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4949 TRUE, FALSE);
4950 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004951 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 break;
4953
4954 /* "zn": fold none: reset 'foldenable' */
4955 case 'n': curwin->w_p_fen = FALSE;
4956 break;
4957
4958 /* "zN": fold Normal: set 'foldenable' */
4959 case 'N': curwin->w_p_fen = TRUE;
4960 break;
4961
4962 /* "zi": invert folding: toggle 'foldenable' */
4963 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4964 break;
4965
4966 /* "za": open closed fold or close open fold at cursor */
4967 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4968 openFold(curwin->w_cursor.lnum, cap->count1);
4969 else
4970 {
4971 closeFold(curwin->w_cursor.lnum, cap->count1);
4972 curwin->w_p_fen = TRUE;
4973 }
4974 break;
4975
4976 /* "zA": open fold at cursor recursively */
4977 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4978 openFoldRecurse(curwin->w_cursor.lnum);
4979 else
4980 {
4981 closeFoldRecurse(curwin->w_cursor.lnum);
4982 curwin->w_p_fen = TRUE;
4983 }
4984 break;
4985
4986 /* "zo": open fold at cursor or Visual area */
4987 case 'o': if (VIsual_active)
4988 nv_operator(cap);
4989 else
4990 openFold(curwin->w_cursor.lnum, cap->count1);
4991 break;
4992
4993 /* "zO": open fold recursively */
4994 case 'O': if (VIsual_active)
4995 nv_operator(cap);
4996 else
4997 openFoldRecurse(curwin->w_cursor.lnum);
4998 break;
4999
5000 /* "zc": close fold at cursor or Visual area */
5001 case 'c': if (VIsual_active)
5002 nv_operator(cap);
5003 else
5004 closeFold(curwin->w_cursor.lnum, cap->count1);
5005 curwin->w_p_fen = TRUE;
5006 break;
5007
5008 /* "zC": close fold recursively */
5009 case 'C': if (VIsual_active)
5010 nv_operator(cap);
5011 else
5012 closeFoldRecurse(curwin->w_cursor.lnum);
5013 curwin->w_p_fen = TRUE;
5014 break;
5015
5016 /* "zv": open folds at the cursor */
5017 case 'v': foldOpenCursor();
5018 break;
5019
5020 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5021 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005022 curwin->w_foldinvalid = TRUE; /* recompute folds */
5023 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 foldOpenCursor();
5025 break;
5026
5027 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5028 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005029 curwin->w_foldinvalid = TRUE; /* recompute folds */
5030 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 break;
5032
5033 /* "zm": fold more */
5034 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005035 {
5036 curwin->w_p_fdl -= cap->count1;
5037 if (curwin->w_p_fdl < 0)
5038 curwin->w_p_fdl = 0;
5039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 old_fdl = -1; /* force an update */
5041 curwin->w_p_fen = TRUE;
5042 break;
5043
5044 /* "zM": close all folds */
5045 case 'M': curwin->w_p_fdl = 0;
5046 old_fdl = -1; /* force an update */
5047 curwin->w_p_fen = TRUE;
5048 break;
5049
5050 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005051 case 'r': curwin->w_p_fdl += cap->count1;
5052 {
5053 int d = getDeepestNesting();
5054
5055 if (curwin->w_p_fdl >= d)
5056 curwin->w_p_fdl = d;
5057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 break;
5059
5060 /* "zR": open all folds */
5061 case 'R': curwin->w_p_fdl = getDeepestNesting();
5062 old_fdl = -1; /* force an update */
5063 break;
5064
5065 case 'j': /* "zj" move to next fold downwards */
5066 case 'k': /* "zk" move to next fold upwards */
5067 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5068 cap->count1) == FAIL)
5069 clearopbeep(cap->oap);
5070 break;
5071
5072#endif /* FEAT_FOLDING */
5073
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005074#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005075 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5076 ++no_mapping;
5077 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005078 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005079 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005080 --no_mapping;
5081 --allow_keys;
5082#ifdef FEAT_CMDL_INFO
5083 (void)add_to_showcmd(nchar);
5084#endif
5085 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5086 {
5087 clearopbeep(cap->oap);
5088 break;
5089 }
5090 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005091 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005092
Bram Moolenaarb765d632005-06-07 21:00:02 +00005093 case 'g': /* "zg": add good word to word list */
5094 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005095 case 'G': /* "zG": add good word to temp word list */
5096 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005097 {
5098 char_u *ptr = NULL;
5099 int len;
5100
5101 if (checkclearop(cap->oap))
5102 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005103 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5104 == FAIL)
5105 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005106 if (ptr == NULL)
5107 {
5108 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005109
Bram Moolenaar134bf072013-09-25 18:54:24 +02005110 /* Find bad word under the cursor. When 'spell' is
5111 * off this fails and find_ident_under_cursor() is
5112 * used below. */
5113 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005114 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005115 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005116 if (len != 0 && curwin->w_cursor.col <= pos.col)
5117 ptr = ml_get_pos(&curwin->w_cursor);
5118 curwin->w_cursor = pos;
5119 }
5120
Bram Moolenaarb765d632005-06-07 21:00:02 +00005121 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5122 FIND_IDENT)) == 0)
5123 return;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005124 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W',
Bram Moolenaard0131a82006-03-04 21:46:13 +00005125 (nchar == 'G' || nchar == 'W')
5126 ? 0 : (int)cap->count1,
5127 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005128 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005129 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005130
Bram Moolenaar43abc522005-12-10 20:15:02 +00005131 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005132 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005133 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005134 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005135#endif
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 default: clearopbeep(cap->oap);
5138 }
5139
5140#ifdef FEAT_FOLDING
5141 /* Redraw when 'foldenable' changed */
5142 if (old_fen != curwin->w_p_fen)
5143 {
5144# ifdef FEAT_DIFF
5145 win_T *wp;
5146
5147 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5148 {
5149 /* Adjust 'foldenable' in diff-synced windows. */
5150 FOR_ALL_WINDOWS(wp)
5151 {
5152 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5153 {
5154 wp->w_p_fen = curwin->w_p_fen;
5155 changed_window_setting_win(wp);
5156 }
5157 }
5158 }
5159# endif
5160 changed_window_setting();
5161 }
5162
5163 /* Redraw when 'foldlevel' changed. */
5164 if (old_fdl != curwin->w_p_fdl)
5165 newFoldLevel();
5166#endif
5167}
5168
5169#ifdef FEAT_GUI
5170/*
5171 * Vertical scrollbar movement.
5172 */
5173 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005174nv_ver_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
5176 if (cap->oap->op_type != OP_NOP)
5177 clearopbeep(cap->oap);
5178
5179 /* Even if an operator was pending, we still want to scroll */
5180 gui_do_scroll();
5181}
5182
5183/*
5184 * Horizontal scrollbar movement.
5185 */
5186 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005187nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
5189 if (cap->oap->op_type != OP_NOP)
5190 clearopbeep(cap->oap);
5191
5192 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005193 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194}
5195#endif
5196
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005197#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005198/*
5199 * Click in GUI tab.
5200 */
5201 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005203{
5204 if (cap->oap->op_type != OP_NOP)
5205 clearopbeep(cap->oap);
5206
5207 /* Even if an operator was pending, we still want to jump tabs. */
5208 goto_tabpage(current_tab);
5209}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005210
5211/*
5212 * Selected item in tab line menu.
5213 */
5214 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005215nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005216{
5217 if (cap->oap->op_type != OP_NOP)
5218 clearopbeep(cap->oap);
5219
5220 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005221 handle_tabmenu();
5222}
5223
5224/*
5225 * Handle selecting an item of the GUI tab line menu.
5226 * Used in Normal and Insert mode.
5227 */
5228 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005229handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005230{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005231 switch (current_tabmenu)
5232 {
5233 case TABLINE_MENU_CLOSE:
5234 if (current_tab == 0)
5235 do_cmdline_cmd((char_u *)"tabclose");
5236 else
5237 {
5238 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5239 current_tab);
5240 do_cmdline_cmd(IObuff);
5241 }
5242 break;
5243
5244 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005245 if (current_tab == 0)
5246 do_cmdline_cmd((char_u *)"$tabnew");
5247 else
5248 {
5249 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5250 current_tab - 1);
5251 do_cmdline_cmd(IObuff);
5252 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005253 break;
5254
5255 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005256 if (current_tab == 0)
5257 do_cmdline_cmd((char_u *)"browse $tabnew");
5258 else
5259 {
5260 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5261 current_tab - 1);
5262 do_cmdline_cmd(IObuff);
5263 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005264 break;
5265 }
5266}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005267#endif
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269/*
5270 * "Q" command.
5271 */
5272 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005273nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 /*
5276 * Ignore 'Q' in Visual mode, just give a beep.
5277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005279 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005280 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 do_exmode(FALSE);
5282}
5283
5284/*
5285 * Handle a ":" command.
5286 */
5287 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005288nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
5290 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005291 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 if (VIsual_active)
5294 nv_operator(cap);
5295 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
5297 if (cap->oap->op_type != OP_NOP)
5298 {
5299 /* Using ":" as a movement is characterwise exclusive. */
5300 cap->oap->motion_type = MCHAR;
5301 cap->oap->inclusive = FALSE;
5302 }
5303 else if (cap->count0)
5304 {
5305 /* translate "count:" into ":.,.+(count - 1)" */
5306 stuffcharReadbuff('.');
5307 if (cap->count0 > 1)
5308 {
5309 stuffReadbuff((char_u *)",.+");
5310 stuffnumReadbuff((long)cap->count0 - 1L);
5311 }
5312 }
5313
5314 /* When typing, don't type below an old message */
5315 if (KeyTyped)
5316 compute_cmdrow();
5317
5318 old_p_im = p_im;
5319
5320 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005321 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5323
5324 /* If 'insertmode' changed, enter or exit Insert mode */
5325 if (p_im != old_p_im)
5326 {
5327 if (p_im)
5328 restart_edit = 'i';
5329 else
5330 restart_edit = 0;
5331 }
5332
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005333 if (cmd_result == FAIL)
5334 /* The Ex command failed, do not execute the operator. */
5335 clearop(cap->oap);
5336 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5338 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005339 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5340 || did_emsg
5341 ))
5342 /* The start of the operator has become invalid by the Ex command.
5343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 clearopbeep(cap->oap);
5345 }
5346}
5347
5348/*
5349 * Handle CTRL-G command.
5350 */
5351 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005352nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (VIsual_active) /* toggle Selection/Visual mode */
5355 {
5356 VIsual_select = !VIsual_select;
5357 showmode();
5358 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005359 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 /* print full name if count given or :cd used */
5361 fileinfo((int)cap->count0, FALSE, TRUE);
5362}
5363
5364/*
5365 * Handle CTRL-H <Backspace> command.
5366 */
5367 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005368nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (VIsual_active && VIsual_select)
5371 {
5372 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5373 v_visop(cap);
5374 }
5375 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 nv_left(cap);
5377}
5378
5379/*
5380 * CTRL-L: clear screen and redraw.
5381 */
5382 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005383nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
5385 if (!checkclearop(cap->oap))
5386 {
5387#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5388 /*
5389 * Right now, the BeBox doesn't seem to have an easy way to detect
5390 * window resizing, so we cheat and make the user detect it
5391 * manually with CTRL-L instead
5392 */
5393 ui_get_shellsize();
5394#endif
5395#ifdef FEAT_SYN_HL
5396 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005397 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005398# ifdef FEAT_RELTIME
5399 {
5400 win_T *wp;
5401
5402 FOR_ALL_WINDOWS(wp)
5403 wp->w_s->b_syn_slow = FALSE;
5404 }
5405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406#endif
5407 redraw_later(CLEAR);
5408 }
5409}
5410
5411/*
5412 * CTRL-O: In Select mode: switch to Visual mode for one command.
5413 * Otherwise: Go to older pcmark.
5414 */
5415 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005416nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 if (VIsual_active && VIsual_select)
5419 {
5420 VIsual_select = FALSE;
5421 showmode();
5422 restart_VIsual_select = 2; /* restart Select mode later */
5423 }
5424 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
5426 cap->count1 = -cap->count1;
5427 nv_pcmark(cap);
5428 }
5429}
5430
5431/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01005432 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
5433 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 */
5435 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005436nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437{
5438 if (!checkclearopq(cap->oap))
5439 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5440 GETF_SETMARK|GETF_ALT, FALSE);
5441}
5442
5443/*
5444 * "Z" commands.
5445 */
5446 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005447nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 if (!checkclearopq(cap->oap))
5450 {
5451 switch (cap->nchar)
5452 {
5453 /* "ZZ": equivalent to ":x". */
5454 case 'Z': do_cmdline_cmd((char_u *)"x");
5455 break;
5456
5457 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5458 case 'Q': do_cmdline_cmd((char_u *)"q!");
5459 break;
5460
5461 default: clearopbeep(cap->oap);
5462 }
5463 }
5464}
5465
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466/*
5467 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5468 */
5469 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005470do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471{
5472 oparg_T oa;
5473 cmdarg_T ca;
5474
5475 clear_oparg(&oa);
5476 vim_memset(&ca, 0, sizeof(ca));
5477 ca.oap = &oa;
5478 ca.cmdchar = c1;
5479 ca.nchar = c2;
5480 nv_ident(&ca);
5481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483/*
5484 * Handle the commands that use the word under the cursor.
5485 * [g] CTRL-] :ta to current identifier
5486 * [g] 'K' run program for current identifier
5487 * [g] '*' / to current identifier or string
5488 * [g] '#' ? to current identifier or string
5489 * g ']' :tselect for current identifier
5490 */
5491 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005492nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 char_u *ptr = NULL;
5495 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005496 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005497 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 char_u *p;
5499 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005500 int kp_help; /* 'keywordprg' is ":he" */
5501 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 int n = 0; /* init for GCC */
5503 int cmdchar;
5504 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005505 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 char_u *aux_ptr;
5507 int isman;
5508 int isman_s;
5509
5510 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5511 {
5512 cmdchar = cap->nchar;
5513 g_cmd = TRUE;
5514 }
5515 else
5516 {
5517 cmdchar = cap->cmdchar;
5518 g_cmd = FALSE;
5519 }
5520
5521 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5522 cmdchar = '#';
5523
5524 /*
5525 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5526 */
5527 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5530 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 if (checkclearopq(cap->oap))
5532 return;
5533 }
5534
5535 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5536 (cmdchar == '*' || cmdchar == '#')
5537 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5538 {
5539 clearop(cap->oap);
5540 return;
5541 }
5542
5543 /* Allocate buffer to put the command in. Inserting backslashes can
5544 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5545 * and some numbers. */
5546 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5547 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5548 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005549 if (kp_help && *skipwhite(ptr) == NUL)
5550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005551 emsg(_(e_noident)); /* found white space only */
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005552 return;
5553 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005554 kp_ex = (*kp == ':');
5555 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5556 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 if (buf == NULL)
5558 return;
5559 buf[0] = NUL;
5560
5561 switch (cmdchar)
5562 {
5563 case '*':
5564 case '#':
5565 /*
5566 * Put cursor at start of word, makes search skip the word
5567 * under the cursor.
5568 * Call setpcmark() first, so "*``" puts the cursor back where
5569 * it was.
5570 */
5571 setpcmark();
5572 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5573
5574 if (!g_cmd && vim_iswordp(ptr))
5575 STRCPY(buf, "\\<");
5576 no_smartcase = TRUE; /* don't use 'smartcase' now */
5577 break;
5578
5579 case 'K':
5580 if (kp_help)
5581 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005582 else if (kp_ex)
5583 {
5584 if (cap->count0 != 0)
5585 vim_snprintf((char *)buf, buflen, "%s %ld",
5586 kp, cap->count0);
5587 else
5588 STRCPY(buf, kp);
5589 STRCAT(buf, " ");
5590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 else
5592 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005593 /* An external command will probably use an argument starting
5594 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005595 while (*ptr == '-' && n > 0)
5596 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005597 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005598 --n;
5599 }
5600 if (n == 0)
5601 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005602 emsg(_(e_noident)); /* found dashes only */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005603 vim_free(buf);
5604 return;
5605 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005606
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 /* When a count is given, turn it into a range. Is this
5608 * really what we want? */
5609 isman = (STRCMP(kp, "man") == 0);
5610 isman_s = (STRCMP(kp, "man -s") == 0);
5611 if (cap->count0 != 0 && !(isman || isman_s))
5612 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5613
5614 STRCAT(buf, "! ");
5615 if (cap->count0 == 0 && isman_s)
5616 STRCAT(buf, "man");
5617 else
5618 STRCAT(buf, kp);
5619 STRCAT(buf, " ");
5620 if (cap->count0 != 0 && (isman || isman_s))
5621 {
5622 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5623 STRCAT(buf, " ");
5624 }
5625 }
5626 break;
5627
5628 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005629 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630#ifdef FEAT_CSCOPE
5631 if (p_cst)
5632 STRCPY(buf, "cstag ");
5633 else
5634#endif
5635 STRCPY(buf, "ts ");
5636 break;
5637
5638 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005639 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 if (curbuf->b_help)
5641 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005643 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005644 if (g_cmd)
5645 STRCPY(buf, "tj ");
5646 else
5647 sprintf((char *)buf, "%ldta ", cap->count0);
5648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 }
5650
5651 /*
5652 * Now grab the chars in the identifier
5653 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005654 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005656 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005657 if (kp_ex)
5658 /* Escape the argument properly for an Ex command */
5659 p = vim_strsave_fnameescape(ptr, FALSE);
5660 else
5661 /* Escape the argument properly for a shell command */
5662 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005663 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005664 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005666 vim_free(buf);
5667 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005669 newbuf = (char_u *)vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
5670 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005671 {
5672 vim_free(buf);
5673 vim_free(p);
5674 return;
5675 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005676 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005677 STRCAT(buf, p);
5678 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005680 else
5681 {
5682 if (cmdchar == '*')
5683 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5684 else if (cmdchar == '#')
5685 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005686 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005687 {
5688 if (curbuf->b_help)
5689 /* ":help" handles unescaped argument */
5690 aux_ptr = (char_u *)"";
5691 else
5692 aux_ptr = (char_u *)"\\|\"\n[";
5693 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005694 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005695 aux_ptr = (char_u *)"\\|\"\n*?[";
5696
5697 p = buf + STRLEN(buf);
5698 while (n-- > 0)
5699 {
5700 /* put a backslash before \ and some others */
5701 if (vim_strchr(aux_ptr, *ptr) != NULL)
5702 *p++ = '\\';
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005703 /* When current byte is a part of multibyte character, copy all
5704 * bytes of that character. */
5705 if (has_mbyte)
5706 {
5707 int i;
5708 int len = (*mb_ptr2len)(ptr) - 1;
5709
5710 for (i = 0; i < len && n >= 1; ++i, --n)
5711 *p++ = *ptr++;
5712 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005713 *p++ = *ptr++;
5714 }
5715 *p = NUL;
5716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
5718 /*
5719 * Execute the command.
5720 */
5721 if (cmdchar == '*' || cmdchar == '#')
5722 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005723 if (!g_cmd && (has_mbyte
5724 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
5725 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 STRCAT(buf, "\\>");
5727#ifdef FEAT_CMDHIST
5728 /* put pattern in search history */
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005729 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
5731#endif
Bram Moolenaar46539112015-02-17 15:43:57 +01005732 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734 else
5735 do_cmdline_cmd(buf);
5736
5737 vim_free(buf);
5738}
5739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740/*
5741 * Get visually selected text, within one line only.
5742 * Returns FAIL if more than one line selected.
5743 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005744 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005745get_visual_text(
5746 cmdarg_T *cap,
5747 char_u **pp, /* return: start of selected text */
5748 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 if (VIsual_mode != 'V')
5751 unadjust_for_sel();
5752 if (VIsual.lnum != curwin->w_cursor.lnum)
5753 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005754 if (cap != NULL)
5755 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 return FAIL;
5757 }
5758 if (VIsual_mode == 'V')
5759 {
5760 *pp = ml_get_curline();
5761 *lenp = (int)STRLEN(*pp);
5762 }
5763 else
5764 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005765 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 {
5767 *pp = ml_get_pos(&curwin->w_cursor);
5768 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5769 }
5770 else
5771 {
5772 *pp = ml_get_pos(&VIsual);
5773 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (has_mbyte)
5776 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005777 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 }
5779 reset_VIsual_and_resel();
5780 return OK;
5781}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
5783/*
5784 * CTRL-T: backwards in tag stack
5785 */
5786 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005787nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788{
5789 if (!checkclearopq(cap->oap))
5790 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5791}
5792
5793/*
5794 * Handle scrolling command 'H', 'L' and 'M'.
5795 */
5796 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005797nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 int used = 0;
5800 long n;
5801#ifdef FEAT_FOLDING
5802 linenr_T lnum;
5803#endif
5804 int half;
5805
5806 cap->oap->motion_type = MLINE;
5807 setpcmark();
5808
5809 if (cap->cmdchar == 'L')
5810 {
5811 validate_botline(); /* make sure curwin->w_botline is valid */
5812 curwin->w_cursor.lnum = curwin->w_botline - 1;
5813 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5814 curwin->w_cursor.lnum = 1;
5815 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005816 {
5817#ifdef FEAT_FOLDING
5818 if (hasAnyFolding(curwin))
5819 {
5820 /* Count a fold for one screen line. */
5821 for (n = cap->count1 - 1; n > 0
5822 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5823 {
5824 (void)hasFolding(curwin->w_cursor.lnum,
5825 &curwin->w_cursor.lnum, NULL);
5826 --curwin->w_cursor.lnum;
5827 }
5828 }
5829 else
5830#endif
5831 curwin->w_cursor.lnum -= cap->count1 - 1;
5832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
5834 else
5835 {
5836 if (cap->cmdchar == 'M')
5837 {
5838#ifdef FEAT_DIFF
5839 /* Don't count filler lines above the window. */
5840 used -= diff_check_fill(curwin, curwin->w_topline)
5841 - curwin->w_topfill;
5842#endif
5843 validate_botline(); /* make sure w_empty_rows is valid */
5844 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5845 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5846 {
5847#ifdef FEAT_DIFF
5848 /* Count half he number of filler lines to be "below this
5849 * line" and half to be "above the next line". */
5850 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5851 + n) / 2 >= half)
5852 {
5853 --n;
5854 break;
5855 }
5856#endif
5857 used += plines(curwin->w_topline + n);
5858 if (used >= half)
5859 break;
5860#ifdef FEAT_FOLDING
5861 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5862 n = lnum - curwin->w_topline;
5863#endif
5864 }
5865 if (n > 0 && used > curwin->w_height)
5866 --n;
5867 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005868 else /* (cap->cmdchar == 'H') */
5869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005871#ifdef FEAT_FOLDING
5872 if (hasAnyFolding(curwin))
5873 {
5874 /* Count a fold for one screen line. */
5875 lnum = curwin->w_topline;
5876 while (n-- > 0 && lnum < curwin->w_botline - 1)
5877 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005878 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005879 ++lnum;
5880 }
5881 n = lnum - curwin->w_topline;
5882 }
5883#endif
5884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 curwin->w_cursor.lnum = curwin->w_topline + n;
5886 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5887 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5888 }
5889
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005890 /* Correct for 'so', except when an operator is pending. */
5891 if (cap->oap->op_type == OP_NOP)
5892 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 beginline(BL_SOL | BL_FIX);
5894}
5895
5896/*
5897 * Cursor right commands.
5898 */
5899 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005900nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
5902 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005903 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005905 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5906 {
5907 /* <C-Right> and <S-Right> move a word or WORD right */
5908 if (mod_mask & MOD_MASK_CTRL)
5909 cap->arg = TRUE;
5910 nv_wordcmd(cap);
5911 return;
5912 }
5913
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 cap->oap->motion_type = MCHAR;
5915 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005916 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005919 * In virtual edit mode, there's no such thing as "past_line", as lines
5920 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 */
5922 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005923 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
5925 for (n = cap->count1; n > 0; --n)
5926 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005927 if ((!past_line && oneright() == FAIL)
5928 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005929 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 {
5931 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005932 * <Space> wraps to next line if 'whichwrap' has 's'.
5933 * 'l' wraps to next line if 'whichwrap' has 'l'.
5934 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 */
5936 if ( ((cap->cmdchar == ' '
5937 && vim_strchr(p_ww, 's') != NULL)
5938 || (cap->cmdchar == 'l'
5939 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005940 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 && vim_strchr(p_ww, '>') != NULL))
5942 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5943 {
5944 /* When deleting we also count the NL as a character.
5945 * Set cap->oap->inclusive when last char in the line is
5946 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005947 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005949 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 cap->oap->inclusive = TRUE;
5951 else
5952 {
5953 ++curwin->w_cursor.lnum;
5954 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 curwin->w_set_curswant = TRUE;
5957 cap->oap->inclusive = FALSE;
5958 }
5959 continue;
5960 }
5961 if (cap->oap->op_type == OP_NOP)
5962 {
5963 /* Only beep and flush if not moved at all */
5964 if (n == cap->count1)
5965 beep_flush();
5966 }
5967 else
5968 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005969 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 cap->oap->inclusive = TRUE;
5971 }
5972 break;
5973 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005974 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {
5976 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 if (virtual_active())
5978 oneright();
5979 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 if (has_mbyte)
5982 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005983 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 ++curwin->w_cursor.col;
5986 }
5987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 }
5989#ifdef FEAT_FOLDING
5990 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
5991 && cap->oap->op_type == OP_NOP)
5992 foldOpenCursor();
5993#endif
5994}
5995
5996/*
5997 * Cursor left commands.
5998 *
5999 * Returns TRUE when operator end should not be adjusted.
6000 */
6001 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006002nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 long n;
6005
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006006 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6007 {
6008 /* <C-Left> and <S-Left> move a word or WORD left */
6009 if (mod_mask & MOD_MASK_CTRL)
6010 cap->arg = 1;
6011 nv_bck_word(cap);
6012 return;
6013 }
6014
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 cap->oap->motion_type = MCHAR;
6016 cap->oap->inclusive = FALSE;
6017 for (n = cap->count1; n > 0; --n)
6018 {
6019 if (oneleft() == FAIL)
6020 {
6021 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6022 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6023 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6024 */
6025 if ( (((cap->cmdchar == K_BS
6026 || cap->cmdchar == Ctrl_H)
6027 && vim_strchr(p_ww, 'b') != NULL)
6028 || (cap->cmdchar == 'h'
6029 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006030 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 && vim_strchr(p_ww, '<') != NULL))
6032 && curwin->w_cursor.lnum > 1)
6033 {
6034 --(curwin->w_cursor.lnum);
6035 coladvance((colnr_T)MAXCOL);
6036 curwin->w_set_curswant = TRUE;
6037
6038 /* When the NL before the first char has to be deleted we
6039 * put the cursor on the NUL after the previous line.
6040 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006041 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 if ( (cap->oap->op_type == OP_DELETE
6043 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006044 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006046 char_u *cp = ml_get_cursor();
6047
6048 if (*cp != NUL)
6049 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006050 if (has_mbyte)
6051 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6052 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006053 ++curwin->w_cursor.col;
6054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 cap->retval |= CA_NO_ADJ_OP_END;
6056 }
6057 continue;
6058 }
6059 /* Only beep and flush if not moved at all */
6060 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6061 beep_flush();
6062 break;
6063 }
6064 }
6065#ifdef FEAT_FOLDING
6066 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6067 && cap->oap->op_type == OP_NOP)
6068 foldOpenCursor();
6069#endif
6070}
6071
6072/*
6073 * Cursor up commands.
6074 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6075 */
6076 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006077nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006079 if (mod_mask & MOD_MASK_SHIFT)
6080 {
6081 /* <S-Up> is page up */
6082 cap->arg = BACKWARD;
6083 nv_page(cap);
6084 }
6085 else
6086 {
6087 cap->oap->motion_type = MLINE;
6088 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6089 clearopbeep(cap->oap);
6090 else if (cap->arg)
6091 beginline(BL_WHITE | BL_FIX);
6092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093}
6094
6095/*
6096 * Cursor down commands.
6097 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6098 */
6099 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006100nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006102 if (mod_mask & MOD_MASK_SHIFT)
6103 {
6104 /* <S-Down> is page down */
6105 cap->arg = FORWARD;
6106 nv_page(cap);
6107 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006108#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006109 /* Quickfix window only: view the result under the cursor. */
6110 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6111 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 {
6115#ifdef FEAT_CMDWIN
6116 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006117 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 cmdwin_result = CAR;
6119 else
6120#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006121#ifdef FEAT_JOB_CHANNEL
6122 /* In a prompt buffer a <CR> in the last line invokes the callback. */
6123 if (bt_prompt(curbuf) && cap->cmdchar == CAR
6124 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
6125 {
6126 invoke_prompt_callback();
6127 if (restart_edit == 0)
6128 restart_edit = 'a';
6129 }
6130 else
6131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
6133 cap->oap->motion_type = MLINE;
6134 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6135 clearopbeep(cap->oap);
6136 else if (cap->arg)
6137 beginline(BL_WHITE | BL_FIX);
6138 }
6139 }
6140}
6141
6142#ifdef FEAT_SEARCHPATH
6143/*
6144 * Grab the file name under the cursor and edit it.
6145 */
6146 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006147nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006150 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006152 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 {
6154 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006155 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 return;
6157 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006158 if (curbuf_locked())
6159 {
6160 clearop(cap->oap);
6161 return;
6162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006164 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166 if (ptr != NULL)
6167 {
6168 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006169 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006170 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006172 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006173 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006174 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006175 {
6176 curwin->w_cursor.lnum = lnum;
6177 check_cursor_lnum();
6178 beginline(BL_SOL | BL_FIX);
6179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 vim_free(ptr);
6181 }
6182 else
6183 clearop(cap->oap);
6184}
6185#endif
6186
6187/*
6188 * <End> command: to end of current line or last line.
6189 */
6190 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006191nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006193 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006195 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 nv_goto(cap);
6197 cap->count1 = 1; /* to end of current line */
6198 }
6199 nv_dollar(cap);
6200}
6201
6202/*
6203 * Handle the "$" command.
6204 */
6205 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006206nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 cap->oap->motion_type = MCHAR;
6209 cap->oap->inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 /* In virtual mode when off the edge of a line and an operator
6211 * is pending (whew!) keep the cursor where it is.
6212 * Otherwise, send it to the end of the line. */
6213 if (!virtual_active() || gchar_cursor() != NUL
6214 || cap->oap->op_type == OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6216 if (cursor_down((long)(cap->count1 - 1),
6217 cap->oap->op_type == OP_NOP) == FAIL)
6218 clearopbeep(cap->oap);
6219#ifdef FEAT_FOLDING
6220 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6221 foldOpenCursor();
6222#endif
6223}
6224
6225/*
6226 * Implementation of '?' and '/' commands.
6227 * If cap->arg is TRUE don't set PC mark.
6228 */
6229 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006230nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006233 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234
6235 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6236 {
6237 /* Translate "g??" to "g?g?" */
6238 cap->cmdchar = 'g';
6239 cap->nchar = '?';
6240 nv_operator(cap);
6241 return;
6242 }
6243
Bram Moolenaardda933d2016-09-03 21:04:58 +02006244 /* When using 'incsearch' the cursor may be moved to set a different search
6245 * start position. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0);
6247
6248 if (cap->searchbuf == NULL)
6249 {
6250 clearop(oap);
6251 return;
6252 }
6253
Bram Moolenaar46539112015-02-17 15:43:57 +01006254 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006255 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006256 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257}
6258
6259/*
6260 * Handle "N" and "n" commands.
6261 * cap->arg is SEARCH_REV for "N", 0 for "n".
6262 */
6263 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006264nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
Bram Moolenaar46539112015-02-17 15:43:57 +01006266 pos_T old = curwin->w_cursor;
6267 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6268
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006269 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006270 {
6271 /* Avoid getting stuck on the current cursor position, which can
6272 * happen when an offset is given and the cursor is on the last char
6273 * in the buffer: Repeat with count + 1. */
6274 cap->count1 += 1;
6275 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6276 cap->count1 -= 1;
6277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278}
6279
6280/*
6281 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6282 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006283 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006285 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006286normal_search(
6287 cmdarg_T *cap,
6288 int dir,
6289 char_u *pat,
6290 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 int i;
6293
6294 cap->oap->motion_type = MCHAR;
6295 cap->oap->inclusive = FALSE;
6296 cap->oap->use_reg_one = TRUE;
6297 curwin->w_set_curswant = TRUE;
6298
6299 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006300 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (i == 0)
6302 clearop(cap->oap);
6303 else
6304 {
6305 if (i == 2)
6306 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308#ifdef FEAT_FOLDING
6309 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6310 foldOpenCursor();
6311#endif
6312 }
6313
6314 /* "/$" will put the cursor after the end of the line, may need to
6315 * correct that here */
6316 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006317 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318}
6319
6320/*
6321 * Character search commands.
6322 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6323 * ',' and FALSE for ';'.
6324 * cap->nchar is NUL for ',' and ';' (repeat the search)
6325 */
6326 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006327nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328{
6329 int t_cmd;
6330
6331 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6332 t_cmd = TRUE;
6333 else
6334 t_cmd = FALSE;
6335
6336 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6338 clearopbeep(cap->oap);
6339 else
6340 {
6341 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 /* Include a Tab for "tx" and for "dfx". */
6343 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6344 && (t_cmd || cap->oap->op_type != OP_NOP))
6345 {
6346 colnr_T scol, ecol;
6347
6348 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6349 curwin->w_cursor.coladd = ecol - scol;
6350 }
6351 else
6352 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354#ifdef FEAT_FOLDING
6355 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6356 foldOpenCursor();
6357#endif
6358 }
6359}
6360
6361/*
6362 * "[" and "]" commands.
6363 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6364 */
6365 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006366nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006368 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 pos_T prev_pos;
6370 pos_T *pos = NULL; /* init for GCC */
6371 pos_T old_pos; /* cursor position before command */
6372 int flag;
6373 long n;
6374 int findc;
6375 int c;
6376
6377 cap->oap->motion_type = MCHAR;
6378 cap->oap->inclusive = FALSE;
6379 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01006380 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
6382#ifdef FEAT_SEARCHPATH
6383 /*
6384 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6385 */
6386 if (cap->nchar == 'f')
6387 nv_gotofile(cap);
6388 else
6389#endif
6390
6391#ifdef FEAT_FIND_ID
6392 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006393 * Find the occurrence(s) of the identifier or define under cursor
6394 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 *
6396 * search list jump
6397 * fwd bwd fwd bwd fwd bwd
6398 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6399 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6400 */
6401 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006402# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006404# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 cap->nchar) != NULL)
6408 {
6409 char_u *ptr;
6410 int len;
6411
6412 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6413 clearop(cap->oap);
6414 else
6415 {
6416 find_pattern_in_path(ptr, 0, len, TRUE,
6417 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6418 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6419 cap->count1,
6420 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6421 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6422 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6423 (linenr_T)MAXLNUM);
6424 curwin->w_set_curswant = TRUE;
6425 }
6426 }
6427 else
6428#endif
6429
6430 /*
6431 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6432 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6433 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6434 * "[m" or "]m" search for prev/next start of (Java) method.
6435 * "[M" or "]M" search for prev/next end of (Java) method.
6436 */
6437 if ( (cap->cmdchar == '['
6438 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6439 || (cap->cmdchar == ']'
6440 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6441 {
6442 if (cap->nchar == '*')
6443 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 prev_pos.lnum = 0;
6445 if (cap->nchar == 'm' || cap->nchar == 'M')
6446 {
6447 if (cap->cmdchar == '[')
6448 findc = '{';
6449 else
6450 findc = '}';
6451 n = 9999;
6452 }
6453 else
6454 {
6455 findc = cap->nchar;
6456 n = cap->count1;
6457 }
6458 for ( ; n > 0; --n)
6459 {
6460 if ((pos = findmatchlimit(cap->oap, findc,
6461 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6462 {
6463 if (new_pos.lnum == 0) /* nothing found */
6464 {
6465 if (cap->nchar != 'm' && cap->nchar != 'M')
6466 clearopbeep(cap->oap);
6467 }
6468 else
6469 pos = &new_pos; /* use last one found */
6470 break;
6471 }
6472 prev_pos = new_pos;
6473 curwin->w_cursor = *pos;
6474 new_pos = *pos;
6475 }
6476 curwin->w_cursor = old_pos;
6477
6478 /*
6479 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6480 * brought us to the match for "[m" and "]M" when inside a method.
6481 * Try finding the '{' or '}' we want to be at.
6482 * Also repeat for the given count.
6483 */
6484 if (cap->nchar == 'm' || cap->nchar == 'M')
6485 {
6486 /* norm is TRUE for "]M" and "[m" */
6487 int norm = ((findc == '{') == (cap->nchar == 'm'));
6488
6489 n = cap->count1;
6490 /* found a match: we were inside a method */
6491 if (prev_pos.lnum != 0)
6492 {
6493 pos = &prev_pos;
6494 curwin->w_cursor = prev_pos;
6495 if (norm)
6496 --n;
6497 }
6498 else
6499 pos = NULL;
6500 while (n > 0)
6501 {
6502 for (;;)
6503 {
6504 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6505 {
6506 /* if not found anything, that's an error */
6507 if (pos == NULL)
6508 clearopbeep(cap->oap);
6509 n = 0;
6510 break;
6511 }
6512 c = gchar_cursor();
6513 if (c == '{' || c == '}')
6514 {
6515 /* Must have found end/start of class: use it.
6516 * Or found the place to be at. */
6517 if ((c == findc && norm) || (n == 1 && !norm))
6518 {
6519 new_pos = curwin->w_cursor;
6520 pos = &new_pos;
6521 n = 0;
6522 }
6523 /* if no match found at all, we started outside of the
6524 * class and we're inside now. Just go on. */
6525 else if (new_pos.lnum == 0)
6526 {
6527 new_pos = curwin->w_cursor;
6528 pos = &new_pos;
6529 }
6530 /* found start/end of other method: go to match */
6531 else if ((pos = findmatchlimit(cap->oap, findc,
6532 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6533 0)) == NULL)
6534 n = 0;
6535 else
6536 curwin->w_cursor = *pos;
6537 break;
6538 }
6539 }
6540 --n;
6541 }
6542 curwin->w_cursor = old_pos;
6543 if (pos == NULL && new_pos.lnum != 0)
6544 clearopbeep(cap->oap);
6545 }
6546 if (pos != NULL)
6547 {
6548 setpcmark();
6549 curwin->w_cursor = *pos;
6550 curwin->w_set_curswant = TRUE;
6551#ifdef FEAT_FOLDING
6552 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6553 && cap->oap->op_type == OP_NOP)
6554 foldOpenCursor();
6555#endif
6556 }
6557 }
6558
6559 /*
6560 * "[[", "[]", "]]" and "][": move to start or end of function
6561 */
6562 else if (cap->nchar == '[' || cap->nchar == ']')
6563 {
6564 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6565 flag = '{';
6566 else
6567 flag = '}'; /* "][" or "[]" */
6568
6569 curwin->w_set_curswant = TRUE;
6570 /*
6571 * Imitate strange Vi behaviour: When using "]]" with an operator
6572 * we also stop at '}'.
6573 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006574 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 (cap->oap->op_type != OP_NOP
6576 && cap->arg == FORWARD && flag == '{')))
6577 clearopbeep(cap->oap);
6578 else
6579 {
6580 if (cap->oap->op_type == OP_NOP)
6581 beginline(BL_WHITE | BL_FIX);
6582#ifdef FEAT_FOLDING
6583 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6584 foldOpenCursor();
6585#endif
6586 }
6587 }
6588
6589 /*
6590 * "[p", "[P", "]P" and "]p": put with indent adjustment
6591 */
6592 else if (cap->nchar == 'p' || cap->nchar == 'P')
6593 {
Bram Moolenaar78f6f7e2007-07-10 12:03:33 +00006594 if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 {
Bram Moolenaar27bed202014-03-12 17:42:04 +01006596 int dir = (cap->cmdchar == ']' && cap->nchar == 'p')
6597 ? FORWARD : BACKWARD;
6598 int regname = cap->oap->regname;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006599 int was_visual = VIsual_active;
6600 int line_count = curbuf->b_ml.ml_line_count;
6601 pos_T start, end;
6602
6603 if (VIsual_active)
6604 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006605 start = LTOREQ_POS(VIsual, curwin->w_cursor)
Bram Moolenaar27bed202014-03-12 17:42:04 +01006606 ? VIsual : curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006607 end = EQUAL_POS(start,VIsual) ? curwin->w_cursor : VIsual;
Bram Moolenaar27bed202014-03-12 17:42:04 +01006608 curwin->w_cursor = (dir == BACKWARD ? start : end);
6609 }
Bram Moolenaar27bed202014-03-12 17:42:04 +01006610# ifdef FEAT_CLIPBOARD
6611 adjust_clip_reg(&regname);
6612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 prep_redo_cmd(cap);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006614
6615 do_put(regname, dir, cap->count1, PUT_FIXINDENT);
Bram Moolenaar27bed202014-03-12 17:42:04 +01006616 if (was_visual)
6617 {
6618 VIsual = start;
6619 curwin->w_cursor = end;
6620 if (dir == BACKWARD)
6621 {
6622 /* adjust lines */
6623 VIsual.lnum += curbuf->b_ml.ml_line_count - line_count;
6624 curwin->w_cursor.lnum +=
6625 curbuf->b_ml.ml_line_count - line_count;
6626 }
6627
6628 VIsual_active = TRUE;
6629 if (VIsual_mode == 'V')
6630 {
6631 /* delete visually selected lines */
6632 cap->cmdchar = 'd';
6633 cap->nchar = NUL;
6634 cap->oap->regname = regname;
6635 nv_operator(cap);
6636 do_pending_operator(cap, 0, FALSE);
6637 }
6638 if (VIsual_active)
6639 {
6640 end_visual_mode();
6641 redraw_later(SOME_VALID);
6642 }
6643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645 }
6646
6647 /*
6648 * "['", "[`", "]'" and "]`": jump to next mark
6649 */
6650 else if (cap->nchar == '\'' || cap->nchar == '`')
6651 {
6652 pos = &curwin->w_cursor;
6653 for (n = cap->count1; n > 0; --n)
6654 {
6655 prev_pos = *pos;
6656 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6657 cap->nchar == '\'');
6658 if (pos == NULL)
6659 break;
6660 }
6661 if (pos == NULL)
6662 pos = &prev_pos;
6663 nv_cursormark(cap, cap->nchar == '\'', pos);
6664 }
6665
6666#ifdef FEAT_MOUSE
6667 /*
6668 * [ or ] followed by a middle mouse click: put selected text with
6669 * indent adjustment. Any other button just does as usual.
6670 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006671 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {
6673 (void)do_mouse(cap->oap, cap->nchar,
6674 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6675 cap->count1, PUT_FIXINDENT);
6676 }
6677#endif /* FEAT_MOUSE */
6678
6679#ifdef FEAT_FOLDING
6680 /*
6681 * "[z" and "]z": move to start or end of open fold.
6682 */
6683 else if (cap->nchar == 'z')
6684 {
6685 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6686 cap->count1) == FAIL)
6687 clearopbeep(cap->oap);
6688 }
6689#endif
6690
6691#ifdef FEAT_DIFF
6692 /*
6693 * "[c" and "]c": move to next or previous diff-change.
6694 */
6695 else if (cap->nchar == 'c')
6696 {
6697 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6698 cap->count1) == FAIL)
6699 clearopbeep(cap->oap);
6700 }
6701#endif
6702
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006703#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006704 /*
6705 * "[s", "[S", "]s" and "]S": move to next spell error.
6706 */
6707 else if (cap->nchar == 's' || cap->nchar == 'S')
6708 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006709 setpcmark();
6710 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006711 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6712 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006713 {
6714 clearopbeep(cap->oap);
6715 break;
6716 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006717 else
6718 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006719# ifdef FEAT_FOLDING
6720 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6721 foldOpenCursor();
6722# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006723 }
6724#endif
6725
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 /* Not a valid cap->nchar. */
6727 else
6728 clearopbeep(cap->oap);
6729}
6730
6731/*
6732 * Handle Normal mode "%" command.
6733 */
6734 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006735nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
6737 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006738#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 linenr_T lnum = curwin->w_cursor.lnum;
6740#endif
6741
6742 cap->oap->inclusive = TRUE;
6743 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6744 {
6745 if (cap->count0 > 100)
6746 clearopbeep(cap->oap);
6747 else
6748 {
6749 cap->oap->motion_type = MLINE;
6750 setpcmark();
6751 /* Round up, so CTRL-G will give same value. Watch out for a
6752 * large line count, the line number must not go negative! */
6753 if (curbuf->b_ml.ml_line_count > 1000000)
6754 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6755 / 100L * cap->count0;
6756 else
6757 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6758 cap->count0 + 99L) / 100L;
6759 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6760 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6761 beginline(BL_SOL | BL_FIX);
6762 }
6763 }
6764 else /* "%" : go to matching paren */
6765 {
6766 cap->oap->motion_type = MCHAR;
6767 cap->oap->use_reg_one = TRUE;
6768 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6769 clearopbeep(cap->oap);
6770 else
6771 {
6772 setpcmark();
6773 curwin->w_cursor = *pos;
6774 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 }
6778 }
6779#ifdef FEAT_FOLDING
6780 if (cap->oap->op_type == OP_NOP
6781 && lnum != curwin->w_cursor.lnum
6782 && (fdo_flags & FDO_PERCENT)
6783 && KeyTyped)
6784 foldOpenCursor();
6785#endif
6786}
6787
6788/*
6789 * Handle "(" and ")" commands.
6790 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6791 */
6792 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006793nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 cap->oap->motion_type = MCHAR;
6796 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006797 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6798 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 curwin->w_set_curswant = TRUE;
6800
6801 if (findsent(cap->arg, cap->count1) == FAIL)
6802 clearopbeep(cap->oap);
6803 else
6804 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006805 /* Don't leave the cursor on the NUL past end of line. */
6806 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808#ifdef FEAT_FOLDING
6809 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6810 foldOpenCursor();
6811#endif
6812 }
6813}
6814
6815/*
6816 * "m" command: Mark a position.
6817 */
6818 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006819nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821 if (!checkclearop(cap->oap))
6822 {
6823 if (setmark(cap->nchar) == FAIL)
6824 clearopbeep(cap->oap);
6825 }
6826}
6827
6828/*
6829 * "{" and "}" commands.
6830 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6831 */
6832 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006833nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
6835 cap->oap->motion_type = MCHAR;
6836 cap->oap->inclusive = FALSE;
6837 cap->oap->use_reg_one = TRUE;
6838 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006839 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 clearopbeep(cap->oap);
6841 else
6842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844#ifdef FEAT_FOLDING
6845 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6846 foldOpenCursor();
6847#endif
6848 }
6849}
6850
6851/*
6852 * "u" command: Undo or make lower case.
6853 */
6854 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006855nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006857 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {
6859 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6860 cap->cmdchar = 'g';
6861 cap->nchar = 'u';
6862 nv_operator(cap);
6863 }
6864 else
6865 nv_kundo(cap);
6866}
6867
6868/*
6869 * <Undo> command.
6870 */
6871 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006872nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
6874 if (!checkclearopq(cap->oap))
6875 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02006876#ifdef FEAT_JOB_CHANNEL
6877 if (bt_prompt(curbuf))
6878 {
6879 clearopbeep(cap->oap);
6880 return;
6881 }
6882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 u_undo((int)cap->count1);
6884 curwin->w_set_curswant = TRUE;
6885 }
6886}
6887
6888/*
6889 * Handle the "r" command.
6890 */
6891 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006892nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 char_u *ptr;
6895 int had_ctrl_v;
6896 long n;
6897
6898 if (checkclearop(cap->oap))
6899 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006900#ifdef FEAT_JOB_CHANNEL
6901 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6902 {
6903 clearopbeep(cap->oap);
6904 return;
6905 }
6906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908 /* get another character */
6909 if (cap->nchar == Ctrl_V)
6910 {
6911 had_ctrl_v = Ctrl_V;
6912 cap->nchar = get_literal();
6913 /* Don't redo a multibyte character with CTRL-V. */
6914 if (cap->nchar > DEL)
6915 had_ctrl_v = NUL;
6916 }
6917 else
6918 had_ctrl_v = NUL;
6919
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006920 /* Abort if the character is a special key. */
6921 if (IS_SPECIAL(cap->nchar))
6922 {
6923 clearopbeep(cap->oap);
6924 return;
6925 }
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 /* Visual mode "r" */
6928 if (VIsual_active)
6929 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006930 if (got_int)
6931 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006932 if (had_ctrl_v)
6933 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006934 /* Use a special (negative) number to make a difference between a
6935 * literal CR or NL and a line break. */
6936 if (cap->nchar == CAR)
6937 cap->nchar = REPLACE_CR_NCHAR;
6938 else if (cap->nchar == NL)
6939 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01006940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 nv_operator(cap);
6942 return;
6943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 /* Break tabs, etc. */
6946 if (virtual_active())
6947 {
6948 if (u_save_cursor() == FAIL)
6949 return;
6950 if (gchar_cursor() == NUL)
6951 {
6952 /* Add extra space and put the cursor on the first one. */
6953 coladvance_force((colnr_T)(getviscol() + cap->count1));
6954 curwin->w_cursor.col -= cap->count1;
6955 }
6956 else if (gchar_cursor() == TAB)
6957 coladvance_force(getviscol());
6958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006960 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006962 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006963 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
6965 clearopbeep(cap->oap);
6966 return;
6967 }
6968
6969 /*
6970 * Replacing with a TAB is done by edit() when it is complicated because
6971 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6972 * Other characters are done below to avoid problems with things like
6973 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6974 */
6975 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6976 {
6977 stuffnumReadbuff(cap->count1);
6978 stuffcharReadbuff('R');
6979 stuffcharReadbuff('\t');
6980 stuffcharReadbuff(ESC);
6981 return;
6982 }
6983
6984 /* save line for undo */
6985 if (u_save_cursor() == FAIL)
6986 return;
6987
6988 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6989 {
6990 /*
6991 * Replace character(s) by a single newline.
6992 * Strange vi behaviour: Only one newline is inserted.
6993 * Delete the characters here.
6994 * Insert the newline with an insert command, takes care of
6995 * autoindent. The insert command depends on being on the last
6996 * character of a line or not.
6997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 (void)del_chars(cap->count1, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 stuffcharReadbuff('\r');
7000 stuffcharReadbuff(ESC);
7001
7002 /* Give 'r' to edit(), to get the redo command right. */
7003 invoke_edit(cap, TRUE, 'r', FALSE);
7004 }
7005 else
7006 {
7007 prep_redo(cap->oap->regname, cap->count1,
7008 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
7009
7010 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 if (has_mbyte)
7012 {
7013 int old_State = State;
7014
7015 if (cap->ncharC1 != 0)
7016 AppendCharToRedobuff(cap->ncharC1);
7017 if (cap->ncharC2 != 0)
7018 AppendCharToRedobuff(cap->ncharC2);
7019
7020 /* This is slow, but it handles replacing a single-byte with a
7021 * multi-byte and the other way around. Also handles adding
7022 * composing characters for utf-8. */
7023 for (n = cap->count1; n > 0; --n)
7024 {
7025 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02007026 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7027 {
7028 int c = ins_copychar(curwin->w_cursor.lnum
7029 + (cap->nchar == Ctrl_Y ? -1 : 1));
7030 if (c != NUL)
7031 ins_char(c);
7032 else
7033 /* will be decremented further down */
7034 ++curwin->w_cursor.col;
7035 }
7036 else
7037 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 State = old_State;
7039 if (cap->ncharC1 != 0)
7040 ins_char(cap->ncharC1);
7041 if (cap->ncharC2 != 0)
7042 ins_char(cap->ncharC2);
7043 }
7044 }
7045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {
7047 /*
7048 * Replace the characters within one line.
7049 */
7050 for (n = cap->count1; n > 0; --n)
7051 {
7052 /*
7053 * Get ptr again, because u_save and/or showmatch() will have
7054 * released the line. At the same time we let know that the
7055 * line will be changed.
7056 */
7057 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007058 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7059 {
7060 int c = ins_copychar(curwin->w_cursor.lnum
7061 + (cap->nchar == Ctrl_Y ? -1 : 1));
7062 if (c != NUL)
7063 ptr[curwin->w_cursor.col] = c;
7064 }
7065 else
7066 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 if (p_sm && msg_silent == 0)
7068 showmatch(cap->nchar);
7069 ++curwin->w_cursor.col;
7070 }
7071#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007072 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007074 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
Bram Moolenaar009b2592004-10-24 19:18:58 +00007076 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007077 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007079 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081#endif
7082
7083 /* mark the buffer as changed and prepare for displaying */
7084 changed_bytes(curwin->w_cursor.lnum,
7085 (colnr_T)(curwin->w_cursor.col - cap->count1));
7086 }
7087 --curwin->w_cursor.col; /* cursor on the last replaced char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 /* if the character on the left of the current cursor is a multi-byte
7089 * character, move two characters left */
7090 if (has_mbyte)
7091 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 curbuf->b_op_end = curwin->w_cursor;
7093 curwin->w_set_curswant = TRUE;
7094 set_last_insert(cap->nchar);
7095 }
7096}
7097
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098/*
7099 * 'o': Exchange start and end of Visual area.
7100 * 'O': same, but in block mode exchange left and right corners.
7101 */
7102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007103v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
7105 pos_T old_cursor;
7106 colnr_T left, right;
7107
7108 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7109 {
7110 old_cursor = curwin->w_cursor;
7111 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7112 curwin->w_cursor.lnum = VIsual.lnum;
7113 coladvance(left);
7114 VIsual = curwin->w_cursor;
7115
7116 curwin->w_cursor.lnum = old_cursor.lnum;
7117 curwin->w_curswant = right;
7118 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7119 * right one column */
7120 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7121 ++curwin->w_curswant;
7122 coladvance(curwin->w_curswant);
7123 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007125 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 {
7127 curwin->w_cursor.lnum = VIsual.lnum;
7128 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7129 ++right;
7130 coladvance(right);
7131 VIsual = curwin->w_cursor;
7132
7133 curwin->w_cursor.lnum = old_cursor.lnum;
7134 coladvance(left);
7135 curwin->w_curswant = left;
7136 }
7137 }
7138 else
7139 {
7140 old_cursor = curwin->w_cursor;
7141 curwin->w_cursor = VIsual;
7142 VIsual = old_cursor;
7143 curwin->w_set_curswant = TRUE;
7144 }
7145}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
7147/*
7148 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7149 */
7150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007151nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 if (VIsual_active) /* "R" is replace lines */
7154 {
7155 cap->cmdchar = 'c';
7156 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007157 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 VIsual_mode = 'V';
7159 nv_operator(cap);
7160 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007161 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {
7163 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007164 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 else
7166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 if (virtual_active())
7168 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7170 }
7171 }
7172}
7173
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174/*
7175 * "gr".
7176 */
7177 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007178nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (VIsual_active)
7181 {
7182 cap->cmdchar = 'r';
7183 cap->nchar = cap->extra_char;
7184 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7185 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007186 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007189 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 else
7191 {
7192 if (cap->extra_char == Ctrl_V) /* get another character */
7193 cap->extra_char = get_literal();
7194 stuffcharReadbuff(cap->extra_char);
7195 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 if (virtual_active())
7197 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 invoke_edit(cap, TRUE, 'v', FALSE);
7199 }
7200 }
7201}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
7203/*
7204 * Swap case for "~" command, when it does not work like an operator.
7205 */
7206 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007207n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208{
7209 long n;
7210 pos_T startpos;
7211 int did_change = 0;
7212#ifdef FEAT_NETBEANS_INTG
7213 pos_T pos;
7214 char_u *ptr;
7215 int count;
7216#endif
7217
7218 if (checkclearopq(cap->oap))
7219 return;
7220
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007221 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {
7223 clearopbeep(cap->oap);
7224 return;
7225 }
7226
7227 prep_redo_cmd(cap);
7228
7229 if (u_save_cursor() == FAIL)
7230 return;
7231
7232 startpos = curwin->w_cursor;
7233#ifdef FEAT_NETBEANS_INTG
7234 pos = startpos;
7235#endif
7236 for (n = cap->count1; n > 0; --n)
7237 {
7238 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7239 inc_cursor();
7240 if (gchar_cursor() == NUL)
7241 {
7242 if (vim_strchr(p_ww, '~') != NULL
7243 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7244 {
7245#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007246 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
7248 if (did_change)
7249 {
7250 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007251 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007252 netbeans_removed(curbuf, pos.lnum, pos.col,
7253 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007255 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257 pos.col = 0;
7258 pos.lnum++;
7259 }
7260#endif
7261 ++curwin->w_cursor.lnum;
7262 curwin->w_cursor.col = 0;
7263 if (n > 1)
7264 {
7265 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7266 break;
7267 u_clearline();
7268 }
7269 }
7270 else
7271 break;
7272 }
7273 }
7274#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007275 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 {
7277 ptr = ml_get(pos.lnum);
7278 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007279 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7280 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
7282#endif
7283
7284
7285 check_cursor();
7286 curwin->w_set_curswant = TRUE;
7287 if (did_change)
7288 {
7289 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7290 0L);
7291 curbuf->b_op_start = startpos;
7292 curbuf->b_op_end = curwin->w_cursor;
7293 if (curbuf->b_op_end.col > 0)
7294 --curbuf->b_op_end.col;
7295 }
7296}
7297
7298/*
7299 * Move cursor to mark.
7300 */
7301 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007302nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 if (check_mark(pos) == FAIL)
7305 clearop(cap->oap);
7306 else
7307 {
7308 if (cap->cmdchar == '\''
7309 || cap->cmdchar == '`'
7310 || cap->cmdchar == '['
7311 || cap->cmdchar == ']')
7312 setpcmark();
7313 curwin->w_cursor = *pos;
7314 if (flag)
7315 beginline(BL_WHITE | BL_FIX);
7316 else
7317 check_cursor();
7318 }
7319 cap->oap->motion_type = flag ? MLINE : MCHAR;
7320 if (cap->cmdchar == '`')
7321 cap->oap->use_reg_one = TRUE;
7322 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7323 curwin->w_set_curswant = TRUE;
7324}
7325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326/*
7327 * Handle commands that are operators in Visual mode.
7328 */
7329 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007330v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7333
7334 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007335 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 if (isupper(cap->cmdchar))
7337 {
7338 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007339 {
7340 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7344 curwin->w_curswant = MAXCOL;
7345 }
7346 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7347 nv_operator(cap);
7348}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349
7350/*
7351 * "s" and "S" commands.
7352 */
7353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007354nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007356#ifdef FEAT_TERMINAL
7357 /* When showing output of term_dumpdiff() swap the top and botom. */
7358 if (term_swap_diff() == OK)
7359 return;
7360#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02007361#ifdef FEAT_JOB_CHANNEL
7362 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7363 {
7364 clearopbeep(cap->oap);
7365 return;
7366 }
7367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7369 {
7370 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007371 {
7372 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 cap->cmdchar = 'c';
7376 nv_operator(cap);
7377 }
7378 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 nv_optrans(cap);
7380}
7381
7382/*
7383 * Abbreviated commands.
7384 */
7385 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007386nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
7388 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7389 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7390
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 /* in Visual mode these commands are operators */
7392 if (VIsual_active)
7393 v_visop(cap);
7394 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 nv_optrans(cap);
7396}
7397
7398/*
7399 * Translate a command into another command.
7400 */
7401 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007402nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
7404 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7405 (char_u *)"d$", (char_u *)"c$",
7406 (char_u *)"cl", (char_u *)"cc",
7407 (char_u *)"yy", (char_u *)":s\r"};
7408 static char_u *str = (char_u *)"xXDCsSY&";
7409
7410 if (!checkclearopq(cap->oap))
7411 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007412 /* In Vi "2D" doesn't delete the next line. Can't translate it
7413 * either, because "2." should also not use the count. */
7414 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7415 {
7416 cap->oap->start = curwin->w_cursor;
7417 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007418#ifdef FEAT_EVAL
7419 set_op_var(OP_DELETE);
7420#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007421 cap->count1 = 1;
7422 nv_dollar(cap);
7423 finish_op = TRUE;
7424 ResetRedobuff();
7425 AppendCharToRedobuff('D');
7426 }
7427 else
7428 {
7429 if (cap->count0)
7430 stuffnumReadbuff(cap->count0);
7431 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
7432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 }
7434 cap->opcount = 0;
7435}
7436
7437/*
7438 * "'" and "`" commands. Also for "g'" and "g`".
7439 * cap->arg is TRUE for "'" and "g'".
7440 */
7441 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007442nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
7444 pos_T *pos;
7445 int c;
7446#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007447 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7449#endif
7450
7451 if (cap->cmdchar == 'g')
7452 c = cap->extra_char;
7453 else
7454 c = cap->nchar;
7455 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7456 if (pos == (pos_T *)-1) /* jumped to other file */
7457 {
7458 if (cap->arg)
7459 {
7460 check_cursor_lnum();
7461 beginline(BL_WHITE | BL_FIX);
7462 }
7463 else
7464 check_cursor();
7465 }
7466 else
7467 nv_cursormark(cap, cap->arg, pos);
7468
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 /* May need to clear the coladd that a mark includes. */
7470 if (!virtual_active())
7471 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007472 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473#ifdef FEAT_FOLDING
7474 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007475 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007476 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 && (fdo_flags & FDO_MARK)
7478 && old_KeyTyped)
7479 foldOpenCursor();
7480#endif
7481}
7482
7483/*
7484 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7485 */
7486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007487nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488{
7489#ifdef FEAT_JUMPLIST
7490 pos_T *pos;
7491# ifdef FEAT_FOLDING
7492 linenr_T lnum = curwin->w_cursor.lnum;
7493 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7494# endif
7495
7496 if (!checkclearopq(cap->oap))
7497 {
7498 if (cap->cmdchar == 'g')
7499 pos = movechangelist((int)cap->count1);
7500 else
7501 pos = movemark((int)cap->count1);
7502 if (pos == (pos_T *)-1) /* jump to other file */
7503 {
7504 curwin->w_set_curswant = TRUE;
7505 check_cursor();
7506 }
7507 else if (pos != NULL) /* can jump */
7508 nv_cursormark(cap, FALSE, pos);
7509 else if (cap->cmdchar == 'g')
7510 {
7511 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007512 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007514 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007516 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 }
7518 else
7519 clearopbeep(cap->oap);
7520# ifdef FEAT_FOLDING
7521 if (cap->oap->op_type == OP_NOP
7522 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7523 && (fdo_flags & FDO_MARK)
7524 && old_KeyTyped)
7525 foldOpenCursor();
7526# endif
7527 }
7528#else
7529 clearopbeep(cap->oap);
7530#endif
7531}
7532
7533/*
7534 * Handle '"' command.
7535 */
7536 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007537nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
7539 if (checkclearop(cap->oap))
7540 return;
7541#ifdef FEAT_EVAL
7542 if (cap->nchar == '=')
7543 cap->nchar = get_expr_register();
7544#endif
7545 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7546 {
7547 cap->oap->regname = cap->nchar;
7548 cap->opcount = cap->count0; /* remember count before '"' */
7549#ifdef FEAT_EVAL
7550 set_reg_var(cap->oap->regname);
7551#endif
7552 }
7553 else
7554 clearopbeep(cap->oap);
7555}
7556
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557/*
7558 * Handle "v", "V" and "CTRL-V" commands.
7559 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7560 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007561 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 */
7563 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007564nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007566 if (cap->cmdchar == Ctrl_Q)
7567 cap->cmdchar = Ctrl_V;
7568
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7570 * characterwise, linewise, or blockwise. */
7571 if (cap->oap->op_type != OP_NOP)
7572 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007573 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 finish_op = FALSE; /* operator doesn't finish now but later */
7575 return;
7576 }
7577
7578 VIsual_select = cap->arg;
7579 if (VIsual_active) /* change Visual mode */
7580 {
7581 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7582 end_visual_mode();
7583 else /* toggle char/block mode */
7584 { /* or char/line mode */
7585 VIsual_mode = cap->cmdchar;
7586 showmode();
7587 }
7588 redraw_curbuf_later(INVERTED); /* update the inversion */
7589 }
7590 else /* start Visual mode */
7591 {
7592 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007593 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007595 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 VIsual = curwin->w_cursor;
7597
7598 VIsual_active = TRUE;
7599 VIsual_reselect = TRUE;
7600 if (!cap->arg)
7601 /* start Select mode when 'selectmode' contains "cmd" */
7602 may_start_select('c');
7603#ifdef FEAT_MOUSE
7604 setmouse();
7605#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007606 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 redraw_cmdline = TRUE; /* show visual mode later */
7608 /*
7609 * For V and ^V, we multiply the number of lines even if there
7610 * was only one -- webb
7611 */
7612 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7613 {
7614 curwin->w_cursor.lnum +=
7615 resel_VIsual_line_count * cap->count0 - 1;
7616 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7617 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7618 }
7619 VIsual_mode = resel_VIsual_mode;
7620 if (VIsual_mode == 'v')
7621 {
7622 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007623 {
7624 validate_virtcol();
7625 curwin->w_curswant = curwin->w_virtcol
7626 + resel_VIsual_vcol * cap->count0 - 1;
7627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007629 curwin->w_curswant = resel_VIsual_vcol;
7630 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007632 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 {
7634 curwin->w_curswant = MAXCOL;
7635 coladvance((colnr_T)MAXCOL);
7636 }
7637 else if (VIsual_mode == Ctrl_V)
7638 {
7639 validate_virtcol();
7640 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007641 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 coladvance(curwin->w_curswant);
7643 }
7644 else
7645 curwin->w_set_curswant = TRUE;
7646 redraw_curbuf_later(INVERTED); /* show the inversion */
7647 }
7648 else
7649 {
7650 if (!cap->arg)
7651 /* start Select mode when 'selectmode' contains "cmd" */
7652 may_start_select('c');
7653 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007654 if (VIsual_mode != 'V' && *p_sel == 'e')
7655 ++cap->count1; /* include one more char */
7656 if (cap->count0 > 0 && --cap->count1 > 0)
7657 {
7658 /* With a count select that many characters or lines. */
7659 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7660 nv_right(cap);
7661 else if (VIsual_mode == 'V')
7662 nv_down(cap);
7663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
7665 }
7666}
7667
7668/*
7669 * Start selection for Shift-movement keys.
7670 */
7671 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007672start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
7674 /* if 'selectmode' contains "key", start Select mode */
7675 may_start_select('k');
7676 n_start_visual_mode('v');
7677}
7678
7679/*
7680 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7681 */
7682 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007683may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 VIsual_select = (stuff_empty() && typebuf_typed()
7686 && (vim_strchr(p_slm, c) != NULL));
7687}
7688
7689/*
7690 * Start Visual mode "c".
7691 * Should set VIsual_select before calling this.
7692 */
7693 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007694n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007696#ifdef FEAT_CONCEAL
7697 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007698 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007699#endif
7700
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 VIsual_mode = c;
7702 VIsual_active = TRUE;
7703 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007704
7705 // Corner case: the 0 position in a tab may change when going into
7706 // virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007708 {
7709 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 VIsual = curwin->w_cursor;
7713
7714#ifdef FEAT_FOLDING
7715 foldAdjustVisual();
7716#endif
7717
7718#ifdef FEAT_MOUSE
7719 setmouse();
7720#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007721#ifdef FEAT_CONCEAL
7722 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007723 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007724#endif
7725
Bram Moolenaar09df3122006-01-23 22:23:09 +00007726 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 redraw_cmdline = TRUE; /* show visual mode later */
7728#ifdef FEAT_CLIPBOARD
7729 /* Make sure the clipboard gets updated. Needed because start and
7730 * end may still be the same, and the selection needs to be owned */
7731 clip_star.vmode = NUL;
7732#endif
7733
7734 /* Only need to redraw this line, unless still need to redraw an old
7735 * Visual area (when 'lazyredraw' is set). */
7736 if (curwin->w_redr_type < INVERTED)
7737 {
7738 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7739 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7740 }
7741}
7742
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743
7744/*
7745 * CTRL-W: Window commands
7746 */
7747 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007748nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007750 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007751 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007752 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007753 cap->cmdchar = ':';
7754 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007755 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007756 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007757 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759}
7760
7761/*
7762 * CTRL-Z: Suspend
7763 */
7764 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007765nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 if (VIsual_active)
7769 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 do_cmdline_cmd((char_u *)"st");
7771}
7772
7773/*
7774 * Commands starting with "g".
7775 */
7776 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007777nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 int i;
7782 int flag = FALSE;
7783
7784 switch (cap->nchar)
7785 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007786 case Ctrl_A:
7787 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788#ifdef MEM_PROFILE
7789 /*
7790 * "g^A": dump log of used memory.
7791 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007792 if (!VIsual_active && cap->nchar == Ctrl_A)
7793 vim_mem_profile_dump();
7794 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007796 /*
7797 * "g^A/g^X": sequentially increment visually selected region
7798 */
7799 if (VIsual_active)
7800 {
7801 cap->arg = TRUE;
7802 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007803 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007804 nv_addsub(cap);
7805 }
7806 else
7807 clearopbeep(oap);
7808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 /*
7811 * "gR": Enter virtual replace mode.
7812 */
7813 case 'R':
7814 cap->arg = TRUE;
7815 nv_Replace(cap);
7816 break;
7817
7818 case 'r':
7819 nv_vreplace(cap);
7820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821
7822 case '&':
7823 do_cmdline_cmd((char_u *)"%s//~/&");
7824 break;
7825
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 /*
7827 * "gv": Reselect the previous Visual area. If Visual already active,
7828 * exchange previous and current Visual area.
7829 */
7830 case 'v':
7831 if (checkclearop(oap))
7832 break;
7833
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007834 if ( curbuf->b_visual.vi_start.lnum == 0
7835 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7836 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 beep_flush();
7838 else
7839 {
7840 /* set w_cursor to the start of the Visual area, tpos to the end */
7841 if (VIsual_active)
7842 {
7843 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007844 VIsual_mode = curbuf->b_visual.vi_mode;
7845 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846# ifdef FEAT_EVAL
7847 curbuf->b_visual_mode_eval = i;
7848# endif
7849 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007850 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7851 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007853 tpos = curbuf->b_visual.vi_end;
7854 curbuf->b_visual.vi_end = curwin->w_cursor;
7855 curwin->w_cursor = curbuf->b_visual.vi_start;
7856 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858 else
7859 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007860 VIsual_mode = curbuf->b_visual.vi_mode;
7861 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7862 tpos = curbuf->b_visual.vi_end;
7863 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865
7866 VIsual_active = TRUE;
7867 VIsual_reselect = TRUE;
7868
7869 /* Set Visual to the start and w_cursor to the end of the Visual
7870 * area. Make sure they are on an existing character. */
7871 check_cursor();
7872 VIsual = curwin->w_cursor;
7873 curwin->w_cursor = tpos;
7874 check_cursor();
7875 update_topline();
7876 /*
7877 * When called from normal "g" command: start Select mode when
7878 * 'selectmode' contains "cmd". When called for K_SELECT, always
7879 * start Select mode.
7880 */
7881 if (cap->arg)
7882 VIsual_select = TRUE;
7883 else
7884 may_start_select('c');
7885#ifdef FEAT_MOUSE
7886 setmouse();
7887#endif
7888#ifdef FEAT_CLIPBOARD
7889 /* Make sure the clipboard gets updated. Needed because start and
7890 * end are still the same, and the selection needs to be owned */
7891 clip_star.vmode = NUL;
7892#endif
7893 redraw_curbuf_later(INVERTED);
7894 showmode();
7895 }
7896 break;
7897 /*
7898 * "gV": Don't reselect the previous Visual area after a Select mode
7899 * mapping of menu.
7900 */
7901 case 'V':
7902 VIsual_reselect = FALSE;
7903 break;
7904
7905 /*
7906 * "gh": start Select mode.
7907 * "gH": start Select line mode.
7908 * "g^H": start Select block mode.
7909 */
7910 case K_BS:
7911 cap->nchar = Ctrl_H;
7912 /* FALLTHROUGH */
7913 case 'h':
7914 case 'H':
7915 case Ctrl_H:
7916# ifdef EBCDIC
7917 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7918 if (cap->nchar == Ctrl_H)
7919 cap->cmdchar = Ctrl_V;
7920 else
7921# endif
7922 cap->cmdchar = cap->nchar + ('v' - 'h');
7923 cap->arg = TRUE;
7924 nv_visual(cap);
7925 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007926
7927 /* "gn", "gN" visually select next/previous search match
7928 * "gn" selects next match
7929 * "gN" selects previous match
7930 */
7931 case 'N':
7932 case 'n':
7933 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007934 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007935 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936
7937 /*
7938 * "gj" and "gk" two new funny movement keys -- up and down
7939 * movement based on *screen* line rather than *file* line.
7940 */
7941 case 'j':
7942 case K_DOWN:
7943 /* with 'nowrap' it works just like the normal "j" command; also when
7944 * in a closed fold */
7945 if (!curwin->w_p_wrap
7946#ifdef FEAT_FOLDING
7947 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7948#endif
7949 )
7950 {
7951 oap->motion_type = MLINE;
7952 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7953 }
7954 else
7955 i = nv_screengo(oap, FORWARD, cap->count1);
7956 if (i == FAIL)
7957 clearopbeep(oap);
7958 break;
7959
7960 case 'k':
7961 case K_UP:
7962 /* with 'nowrap' it works just like the normal "k" command; also when
7963 * in a closed fold */
7964 if (!curwin->w_p_wrap
7965#ifdef FEAT_FOLDING
7966 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7967#endif
7968 )
7969 {
7970 oap->motion_type = MLINE;
7971 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7972 }
7973 else
7974 i = nv_screengo(oap, BACKWARD, cap->count1);
7975 if (i == FAIL)
7976 clearopbeep(oap);
7977 break;
7978
7979 /*
7980 * "gJ": join two lines without inserting a space.
7981 */
7982 case 'J':
7983 nv_join(cap);
7984 break;
7985
7986 /*
7987 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7988 * "gm": middle of "g0" and "g$".
7989 */
7990 case '^':
7991 flag = TRUE;
7992 /* FALLTHROUGH */
7993
7994 case '0':
7995 case 'm':
7996 case K_HOME:
7997 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 oap->motion_type = MCHAR;
7999 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008000 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008002 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 int width2 = width1 + curwin_col_off2();
8004
8005 validate_virtcol();
8006 i = 0;
8007 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
8008 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
8009 }
8010 else
8011 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02008012 /* Go to the middle of the screen line. When 'number' or
8013 * 'relativenumber' is on and lines are wrapping the middle can be more
8014 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02008016 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 + ((curwin->w_p_wrap && i > 0)
8018 ? curwin_col_off2() : 0)) / 2;
8019 coladvance((colnr_T)i);
8020 if (flag)
8021 {
8022 do
8023 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01008024 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01008025 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
8027 curwin->w_set_curswant = TRUE;
8028 break;
8029
8030 case '_':
8031 /* "g_": to the last non-blank character in the line or <count> lines
8032 * downward. */
8033 cap->oap->motion_type = MCHAR;
8034 cap->oap->inclusive = TRUE;
8035 curwin->w_curswant = MAXCOL;
8036 if (cursor_down((long)(cap->count1 - 1),
8037 cap->oap->op_type == OP_NOP) == FAIL)
8038 clearopbeep(cap->oap);
8039 else
8040 {
8041 char_u *ptr = ml_get_curline();
8042
8043 /* In Visual mode we may end up after the line. */
8044 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8045 --curwin->w_cursor.col;
8046
8047 /* Decrease the cursor column until it's on a non-blank. */
8048 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008049 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 --curwin->w_cursor.col;
8051 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008052 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 }
8054 break;
8055
8056 case '$':
8057 case K_END:
8058 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {
8060 int col_off = curwin_col_off();
8061
8062 oap->motion_type = MCHAR;
8063 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008064 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 {
8066 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8067 if (cap->count1 == 1)
8068 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008069 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 int width2 = width1 + curwin_col_off2();
8071
8072 validate_virtcol();
8073 i = width1 - 1;
8074 if (curwin->w_virtcol >= (colnr_T)width1)
8075 i += ((curwin->w_virtcol - width1) / width2 + 1)
8076 * width2;
8077 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008078
8079 /* Make sure we stick in this column. */
8080 validate_virtcol();
8081 curwin->w_curswant = curwin->w_virtcol;
8082 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8084 {
8085 /*
8086 * Check for landing on a character that got split at
8087 * the end of the line. We do not want to advance to
8088 * the next screen line.
8089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 if (curwin->w_virtcol > (colnr_T)i)
8091 --curwin->w_cursor.col;
8092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
8094 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8095 clearopbeep(oap);
8096 }
8097 else
8098 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008099 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008101
8102 /* Make sure we stick in this column. */
8103 validate_virtcol();
8104 curwin->w_curswant = curwin->w_virtcol;
8105 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 }
8107 }
8108 break;
8109
8110 /*
8111 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8112 */
8113 case '*':
8114 case '#':
8115#if POUND != '#'
8116 case POUND: /* pound sign (sometimes equal to '#') */
8117#endif
8118 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8119 case ']': /* :tselect for current identifier */
8120 nv_ident(cap);
8121 break;
8122
8123 /*
8124 * ge and gE: go back to end of word
8125 */
8126 case 'e':
8127 case 'E':
8128 oap->motion_type = MCHAR;
8129 curwin->w_set_curswant = TRUE;
8130 oap->inclusive = TRUE;
8131 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8132 clearopbeep(oap);
8133 break;
8134
8135 /*
8136 * "g CTRL-G": display info about cursor position
8137 */
8138 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008139 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 break;
8141
8142 /*
8143 * "gi": start Insert at the last position.
8144 */
8145 case 'i':
8146 if (curbuf->b_last_insert.lnum != 0)
8147 {
8148 curwin->w_cursor = curbuf->b_last_insert;
8149 check_cursor_lnum();
8150 i = (int)STRLEN(ml_get_curline());
8151 if (curwin->w_cursor.col > (colnr_T)i)
8152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 if (virtual_active())
8154 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 curwin->w_cursor.col = i;
8156 }
8157 }
8158 cap->cmdchar = 'i';
8159 nv_edit(cap);
8160 break;
8161
8162 /*
8163 * "gI": Start insert in column 1.
8164 */
8165 case 'I':
8166 beginline(0);
8167 if (!checkclearopq(oap))
8168 invoke_edit(cap, FALSE, 'g', FALSE);
8169 break;
8170
8171#ifdef FEAT_SEARCHPATH
8172 /*
8173 * "gf": goto file, edit file under cursor
8174 * "]f" and "[f": can also be used.
8175 */
8176 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008177 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 nv_gotofile(cap);
8179 break;
8180#endif
8181
8182 /* "g'm" and "g`m": jump to mark without setting pcmark */
8183 case '\'':
8184 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008185 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 case '`':
8187 nv_gomark(cap);
8188 break;
8189
8190 /*
8191 * "gs": Goto sleep.
8192 */
8193 case 's':
8194 do_sleep(cap->count1 * 1000L);
8195 break;
8196
8197 /*
8198 * "ga": Display the ascii value of the character under the
8199 * cursor. It is displayed in decimal, hex, and octal. -- webb
8200 */
8201 case 'a':
8202 do_ascii(NULL);
8203 break;
8204
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 /*
8206 * "g8": Display the bytes used for the UTF-8 character under the
8207 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008208 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 */
8210 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008211 if (cap->count0 == 8)
8212 utf_find_illegal();
8213 else
8214 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216
Bram Moolenaar60402d62017-04-20 18:54:50 +02008217 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008218 case '<':
8219 show_sb_text();
8220 break;
8221
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 /*
8223 * "gg": Goto the first line in file. With a count it goes to
8224 * that line number like for "G". -- webb
8225 */
8226 case 'g':
8227 cap->arg = FALSE;
8228 nv_goto(cap);
8229 break;
8230
8231 /*
8232 * Two-character operators:
8233 * "gq" Format text
8234 * "gw" Format text and keep cursor position
8235 * "g~" Toggle the case of the text.
8236 * "gu" Change text to lower case.
8237 * "gU" Change text to upper case.
8238 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008239 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 */
8241 case 'q':
8242 case 'w':
8243 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008244 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 case '~':
8246 case 'u':
8247 case 'U':
8248 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008249 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 nv_operator(cap);
8251 break;
8252
8253 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008254 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 * current function
8256 * "gD": idem, but in the current file.
8257 */
8258 case 'd':
8259 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008260 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 break;
8262
8263#ifdef FEAT_MOUSE
8264 /*
8265 * g<*Mouse> : <C-*mouse>
8266 */
8267 case K_MIDDLEMOUSE:
8268 case K_MIDDLEDRAG:
8269 case K_MIDDLERELEASE:
8270 case K_LEFTMOUSE:
8271 case K_LEFTDRAG:
8272 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008273 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 case K_RIGHTMOUSE:
8275 case K_RIGHTDRAG:
8276 case K_RIGHTRELEASE:
8277 case K_X1MOUSE:
8278 case K_X1DRAG:
8279 case K_X1RELEASE:
8280 case K_X2MOUSE:
8281 case K_X2DRAG:
8282 case K_X2RELEASE:
8283 mod_mask = MOD_MASK_CTRL;
8284 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8285 break;
8286#endif
8287
8288 case K_IGNORE:
8289 break;
8290
8291 /*
8292 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8293 */
8294 case 'p':
8295 case 'P':
8296 nv_put(cap);
8297 break;
8298
8299#ifdef FEAT_BYTEOFF
8300 /* "go": goto byte count from start of buffer */
8301 case 'o':
8302 goto_byte(cap->count0);
8303 break;
8304#endif
8305
8306 /* "gQ": improved Ex mode */
8307 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008308 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {
8310 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008311 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 break;
8313 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008314
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 if (!checkclearopq(oap))
8316 do_exmode(TRUE);
8317 break;
8318
8319#ifdef FEAT_JUMPLIST
8320 case ',':
8321 nv_pcmark(cap);
8322 break;
8323
8324 case ';':
8325 cap->count1 = -cap->count1;
8326 nv_pcmark(cap);
8327 break;
8328#endif
8329
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008330 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008331 if (!checkclearop(oap))
8332 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008333 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008334 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008335 if (!checkclearop(oap))
8336 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008337 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008338
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008339 case '+':
8340 case '-': /* "g+" and "g-": undo or redo along the timeline */
8341 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008342 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008343 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008344 break;
8345
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 default:
8347 clearopbeep(oap);
8348 break;
8349 }
8350}
8351
8352/*
8353 * Handle "o" and "O" commands.
8354 */
8355 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008356n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008358#ifdef FEAT_CONCEAL
8359 linenr_T oldline = curwin->w_cursor.lnum;
8360#endif
8361
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 if (!checkclearopq(cap->oap))
8363 {
8364#ifdef FEAT_FOLDING
8365 if (cap->cmdchar == 'O')
8366 /* Open above the first line of a folded sequence of lines */
8367 (void)hasFolding(curwin->w_cursor.lnum,
8368 &curwin->w_cursor.lnum, NULL);
8369 else
8370 /* Open below the last line of a folded sequence of lines */
8371 (void)hasFolding(curwin->w_cursor.lnum,
8372 NULL, &curwin->w_cursor.lnum);
8373#endif
8374 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8375 (cap->cmdchar == 'O' ? 1 : 0)),
8376 (linenr_T)(curwin->w_cursor.lnum +
8377 (cap->cmdchar == 'o' ? 1 : 0))
8378 ) == OK
8379 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8380#ifdef FEAT_COMMENTS
8381 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8382#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02008383 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008385#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008386 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01008387 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008388#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008389#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008390 if (curwin->w_p_cul)
8391 /* force redraw of cursorline */
8392 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008393#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008394 /* When '#' is in 'cpoptions' ignore the count. */
8395 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8396 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8398 }
8399 }
8400}
8401
8402/*
8403 * "." command: redo last change.
8404 */
8405 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008406nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
8408 if (!checkclearopq(cap->oap))
8409 {
8410 /*
8411 * If "restart_edit" is TRUE, the last but one command is repeated
8412 * instead of the last command (inserting text). This is used for
8413 * CTRL-O <.> in insert mode.
8414 */
8415 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8416 clearopbeep(cap->oap);
8417 }
8418}
8419
8420/*
8421 * CTRL-R: undo undo
8422 */
8423 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008424nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425{
8426 if (!checkclearopq(cap->oap))
8427 {
8428 u_redo((int)cap->count1);
8429 curwin->w_set_curswant = TRUE;
8430 }
8431}
8432
8433/*
8434 * Handle "U" command.
8435 */
8436 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008437nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
8439 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008440 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {
8442 /* translate "gUU" to "gUgU" */
8443 cap->cmdchar = 'g';
8444 cap->nchar = 'U';
8445 nv_operator(cap);
8446 }
8447 else if (!checkclearopq(cap->oap))
8448 {
8449 u_undoline();
8450 curwin->w_set_curswant = TRUE;
8451 }
8452}
8453
8454/*
8455 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8456 * single character.
8457 */
8458 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008459nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008461 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02008462 {
8463#ifdef FEAT_JOB_CHANNEL
8464 if (bt_prompt(curbuf) && !prompt_curpos_editable())
8465 {
8466 clearopbeep(cap->oap);
8467 return;
8468 }
8469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 else
8473 nv_operator(cap);
8474}
8475
8476/*
8477 * Handle an operator command.
8478 * The actual work is done by do_pending_operator().
8479 */
8480 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008481nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
8483 int op_type;
8484
8485 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008486#ifdef FEAT_JOB_CHANNEL
8487 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
8488 {
8489 clearopbeep(cap->oap);
8490 return;
8491 }
8492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
8494 if (op_type == cap->oap->op_type) /* double operator works on lines */
8495 nv_lineop(cap);
8496 else if (!checkclearop(cap->oap))
8497 {
8498 cap->oap->start = curwin->w_cursor;
8499 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008500#ifdef FEAT_EVAL
8501 set_op_var(op_type);
8502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 }
8504}
8505
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008506#ifdef FEAT_EVAL
8507/*
8508 * Set v:operator to the characters for "optype".
8509 */
8510 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008511set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008512{
8513 char_u opchars[3];
8514
8515 if (optype == OP_NOP)
8516 set_vim_var_string(VV_OP, NULL, 0);
8517 else
8518 {
8519 opchars[0] = get_op_char(optype);
8520 opchars[1] = get_extra_op_char(optype);
8521 opchars[2] = NUL;
8522 set_vim_var_string(VV_OP, opchars, -1);
8523 }
8524}
8525#endif
8526
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527/*
8528 * Handle linewise operator "dd", "yy", etc.
8529 *
8530 * "_" is is a strange motion command that helps make operators more logical.
8531 * It is actually implemented, but not documented in the real Vi. This motion
8532 * command actually refers to "the current line". Commands like "dd" and "yy"
8533 * are really an alternate form of "d_" and "y_". It does accept a count, so
8534 * "d3_" works to delete 3 lines.
8535 */
8536 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008537nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 cap->oap->motion_type = MLINE;
8540 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8541 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008542 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8543 && cap->oap->motion_force != 'v'
8544 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 || cap->oap->op_type == OP_LSHIFT
8546 || cap->oap->op_type == OP_RSHIFT)
8547 beginline(BL_SOL | BL_FIX);
8548 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8549 beginline(BL_WHITE | BL_FIX);
8550}
8551
8552/*
8553 * <Home> command.
8554 */
8555 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008556nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008558 /* CTRL-HOME is like "gg" */
8559 if (mod_mask & MOD_MASK_CTRL)
8560 nv_goto(cap);
8561 else
8562 {
8563 cap->count0 = 1;
8564 nv_pipe(cap);
8565 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008566 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8567 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568}
8569
8570/*
8571 * "|" command.
8572 */
8573 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008574nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
8576 cap->oap->motion_type = MCHAR;
8577 cap->oap->inclusive = FALSE;
8578 beginline(0);
8579 if (cap->count0 > 0)
8580 {
8581 coladvance((colnr_T)(cap->count0 - 1));
8582 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8583 }
8584 else
8585 curwin->w_curswant = 0;
8586 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008587 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 curwin->w_set_curswant = FALSE;
8589}
8590
8591/*
8592 * Handle back-word command "b" and "B".
8593 * cap->arg is 1 for "B"
8594 */
8595 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008596nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 cap->oap->motion_type = MCHAR;
8599 cap->oap->inclusive = FALSE;
8600 curwin->w_set_curswant = TRUE;
8601 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8602 clearopbeep(cap->oap);
8603#ifdef FEAT_FOLDING
8604 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8605 foldOpenCursor();
8606#endif
8607}
8608
8609/*
8610 * Handle word motion commands "e", "E", "w" and "W".
8611 * cap->arg is TRUE for "E" and "W".
8612 */
8613 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008614nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615{
8616 int n;
8617 int word_end;
8618 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008619 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
8621 /*
8622 * Set inclusive for the "E" and "e" command.
8623 */
8624 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8625 word_end = TRUE;
8626 else
8627 word_end = FALSE;
8628 cap->oap->inclusive = word_end;
8629
8630 /*
8631 * "cw" and "cW" are a special case.
8632 */
8633 if (!word_end && cap->oap->op_type == OP_CHANGE)
8634 {
8635 n = gchar_cursor();
8636 if (n != NUL) /* not an empty line */
8637 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008638 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 {
8640 /*
8641 * Reproduce a funny Vi behaviour: "cw" on a blank only
8642 * changes one character, not all blanks until the start of
8643 * the next word. Only do this when the 'w' flag is included
8644 * in 'cpoptions'.
8645 */
8646 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8647 {
8648 cap->oap->inclusive = TRUE;
8649 cap->oap->motion_type = MCHAR;
8650 return;
8651 }
8652 }
8653 else
8654 {
8655 /*
8656 * This is a little strange. To match what the real Vi does,
8657 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8658 * that we are not on a space or a TAB. This seems impolite
8659 * at first, but it's really more what we mean when we say
8660 * 'cw'.
8661 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008662 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 * will change only one character! This is done by setting
8664 * flag.
8665 */
8666 cap->oap->inclusive = TRUE;
8667 word_end = TRUE;
8668 flag = TRUE;
8669 }
8670 }
8671 }
8672
8673 cap->oap->motion_type = MCHAR;
8674 curwin->w_set_curswant = TRUE;
8675 if (word_end)
8676 n = end_word(cap->count1, cap->arg, flag, FALSE);
8677 else
8678 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8679
Bram Moolenaardfefb982008-04-01 10:06:39 +00008680 /* Don't leave the cursor on the NUL past the end of line. Unless we
8681 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008682 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008683 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684
8685 if (n == FAIL && cap->oap->op_type == OP_NOP)
8686 clearopbeep(cap->oap);
8687 else
8688 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690#ifdef FEAT_FOLDING
8691 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8692 foldOpenCursor();
8693#endif
8694 }
8695}
8696
8697/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008698 * Used after a movement command: If the cursor ends up on the NUL after the
8699 * end of the line, may move it back to the last character and make the motion
8700 * inclusive.
8701 */
8702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008703adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008704{
8705 /* The cursor cannot remain on the NUL when:
8706 * - the column is > 0
8707 * - not in Visual mode or 'selection' is "o"
8708 * - 'virtualedit' is not "all" and not "onemore".
8709 */
8710 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008711 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008712 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008713 {
8714 --curwin->w_cursor.col;
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008715 /* prevent cursor from moving on the trail byte */
8716 if (has_mbyte)
8717 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008718 oap->inclusive = TRUE;
8719 }
8720}
8721
8722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 * "0" and "^" commands.
8724 * cap->arg is the argument for beginline().
8725 */
8726 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008727nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729 cap->oap->motion_type = MCHAR;
8730 cap->oap->inclusive = FALSE;
8731 beginline(cap->arg);
8732#ifdef FEAT_FOLDING
8733 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8734 foldOpenCursor();
8735#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008736 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8737 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738}
8739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740/*
8741 * In exclusive Visual mode, may include the last character.
8742 */
8743 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008744adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745{
8746 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008747 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 if (has_mbyte)
8750 inc_cursor();
8751 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 ++curwin->w_cursor.col;
8753 cap->oap->inclusive = FALSE;
8754 }
8755}
8756
8757/*
8758 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8759 * Should check VIsual_mode before calling this.
8760 * Returns TRUE when backed up to the previous line.
8761 */
8762 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008763unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764{
8765 pos_T *pp;
8766
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008767 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008769 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 pp = &curwin->w_cursor;
8771 else
8772 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 if (pp->coladd > 0)
8774 --pp->coladd;
8775 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 if (pp->col > 0)
8777 {
8778 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008779 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 }
8781 else if (pp->lnum > 1)
8782 {
8783 --pp->lnum;
8784 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8785 return TRUE;
8786 }
8787 }
8788 return FALSE;
8789}
8790
8791/*
8792 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8793 */
8794 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008795nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796{
8797 if (VIsual_active)
8798 VIsual_select = TRUE;
8799 else if (VIsual_reselect)
8800 {
8801 cap->nchar = 'v'; /* fake "gv" command */
8802 cap->arg = TRUE;
8803 nv_g_cmd(cap);
8804 }
8805}
8806
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
8808/*
8809 * "G", "gg", CTRL-END, CTRL-HOME.
8810 * cap->arg is TRUE for "G".
8811 */
8812 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008813nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814{
8815 linenr_T lnum;
8816
8817 if (cap->arg)
8818 lnum = curbuf->b_ml.ml_line_count;
8819 else
8820 lnum = 1L;
8821 cap->oap->motion_type = MLINE;
8822 setpcmark();
8823
8824 /* When a count is given, use it instead of the default lnum */
8825 if (cap->count0 != 0)
8826 lnum = cap->count0;
8827 if (lnum < 1L)
8828 lnum = 1L;
8829 else if (lnum > curbuf->b_ml.ml_line_count)
8830 lnum = curbuf->b_ml.ml_line_count;
8831 curwin->w_cursor.lnum = lnum;
8832 beginline(BL_SOL | BL_FIX);
8833#ifdef FEAT_FOLDING
8834 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8835 foldOpenCursor();
8836#endif
8837}
8838
8839/*
8840 * CTRL-\ in Normal mode.
8841 */
8842 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008843nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
8845 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8846 {
8847 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008848 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 clear_cmdline = TRUE; /* unshow mode later */
8850 restart_edit = 0;
8851#ifdef FEAT_CMDWIN
8852 if (cmdwin_type != 0)
8853 cmdwin_result = Ctrl_C;
8854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 if (VIsual_active)
8856 {
8857 end_visual_mode(); /* stop Visual */
8858 redraw_curbuf_later(INVERTED);
8859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8861 if (cap->nchar == Ctrl_G && p_im)
8862 restart_edit = 'a';
8863 }
8864 else
8865 clearopbeep(cap->oap);
8866}
8867
8868/*
8869 * ESC in Normal mode: beep, but don't flush buffers.
8870 * Don't even beep if we are canceling a command.
8871 */
8872 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008873nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
8875 int no_reason;
8876
8877 no_reason = (cap->oap->op_type == OP_NOP
8878 && cap->opcount == 0
8879 && cap->count0 == 0
8880 && cap->oap->regname == 0
8881 && !p_im);
8882
8883 if (cap->arg) /* TRUE for CTRL-C */
8884 {
8885 if (restart_edit == 0
8886#ifdef FEAT_CMDWIN
8887 && cmdwin_type == 0
8888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 && no_reason)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008891 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892
8893 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8894 * set again below when halfway a mapping. */
8895 if (!p_im)
8896 restart_edit = 0;
8897#ifdef FEAT_CMDWIN
8898 if (cmdwin_type != 0)
8899 {
8900 cmdwin_result = K_IGNORE;
8901 got_int = FALSE; /* don't stop executing autocommands et al. */
8902 return;
8903 }
8904#endif
8905 }
8906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 if (VIsual_active)
8908 {
8909 end_visual_mode(); /* stop Visual */
8910 check_cursor_col(); /* make sure cursor is not beyond EOL */
8911 curwin->w_set_curswant = TRUE;
8912 redraw_curbuf_later(INVERTED);
8913 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008914 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008915 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 clearop(cap->oap);
8917
8918 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8919 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01008920 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 restart_edit = 'a';
8922}
8923
8924/*
8925 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01008926 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 */
8928 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008929nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930{
8931 /* <Insert> is equal to "i" */
8932 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8933 cap->cmdchar = 'i';
8934
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 /* in Visual mode "A" and "I" are an operator */
8936 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02008937 {
8938#ifdef FEAT_TERMINAL
8939 if (term_in_normal_mode())
8940 {
8941 end_visual_mode();
8942 clearop(cap->oap);
8943 term_enter_job_mode();
8944 return;
8945 }
8946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02008948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
8950 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008951 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8952 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 {
8954#ifdef FEAT_TEXTOBJ
8955 nv_object(cap);
8956#else
8957 clearopbeep(cap->oap);
8958#endif
8959 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02008960#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02008961 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02008962 {
8963 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02008964 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02008965 return;
8966 }
8967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 else if (!curbuf->b_p_ma && !p_im)
8969 {
8970 /* Only give this error when 'insertmode' is off. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008971 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01008973 if (cap->cmdchar == K_PS)
8974 /* drop the pasted text */
8975 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 }
Bram Moolenaara1891842017-02-04 21:34:31 +01008977 else if (cap->cmdchar == K_PS && VIsual_active)
8978 {
8979 pos_T old_pos = curwin->w_cursor;
8980 pos_T old_visual = VIsual;
8981
8982 /* In Visual mode the selected text is deleted. */
8983 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
8984 {
8985 shift_delete_registers();
8986 cap->oap->regname = '1';
8987 }
8988 else
8989 cap->oap->regname = '-';
8990 cap->cmdchar = 'd';
8991 cap->nchar = NUL;
8992 nv_operator(cap);
8993 do_pending_operator(cap, 0, FALSE);
8994 cap->cmdchar = K_PS;
8995
8996 /* When the last char in the line was deleted then append. Detect this
8997 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008998 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
8999 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009000 inc_cursor();
9001
9002 /* Insert to replace the deleted text with the pasted text. */
9003 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 else if (!checkclearopq(cap->oap))
9006 {
9007 switch (cap->cmdchar)
9008 {
9009 case 'A': /* "A"ppend after the line */
9010 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 if (ve_flags == VE_ALL)
9012 {
9013 int save_State = State;
9014
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009015 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 * character past the end of the line */
9017 State = INSERT;
9018 coladvance((colnr_T)MAXCOL);
9019 State = save_State;
9020 }
9021 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9023 break;
9024
9025 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009026 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9027 beginline(BL_WHITE);
9028 else
9029 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 break;
9031
Bram Moolenaara1891842017-02-04 21:34:31 +01009032 case K_PS:
9033 /* Bracketed paste works like "a"ppend, unless the cursor is in
9034 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009035 if (curwin->w_cursor.col == 0)
9036 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009037 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009038
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 case 'a': /* "a"ppend is like "i"nsert on the next character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 /* increment coladd when in virtual space, increment the
9041 * column otherwise, also to append after an unprintable char */
9042 if (virtual_active()
9043 && (curwin->w_cursor.coladd > 0
9044 || *ml_get_cursor() == NUL
9045 || *ml_get_cursor() == TAB))
9046 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009047 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 inc_cursor();
9049 break;
9050 }
9051
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9053 {
9054 int save_State = State;
9055
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009056 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 * character past the end of the line */
9058 State = INSERT;
9059 coladvance(getviscol());
9060 State = save_State;
9061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062
9063 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9064 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009065 else if (cap->cmdchar == K_PS)
9066 /* drop the pasted text */
9067 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068}
9069
9070/*
9071 * Invoke edit() and take care of "restart_edit" and the return value.
9072 */
9073 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009074invoke_edit(
9075 cmdarg_T *cap,
9076 int repl, /* "r" or "gr" command */
9077 int cmd,
9078 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
9080 int restart_edit_save = 0;
9081
9082 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9083 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9084 * it. */
9085 if (repl || !stuff_empty())
9086 restart_edit_save = restart_edit;
9087 else
9088 restart_edit_save = 0;
9089
9090 /* Always reset "restart_edit", this is not a restarted edit. */
9091 restart_edit = 0;
9092
9093 if (edit(cmd, startln, cap->count1))
9094 cap->retval |= CA_COMMAND_BUSY;
9095
9096 if (restart_edit == 0)
9097 restart_edit = restart_edit_save;
9098}
9099
9100#ifdef FEAT_TEXTOBJ
9101/*
9102 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9103 */
9104 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009105nv_object(
9106 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
9108 int flag;
9109 int include;
9110 char_u *mps_save;
9111
9112 if (cap->cmdchar == 'i')
9113 include = FALSE; /* "ix" = inner object: exclude white space */
9114 else
9115 include = TRUE; /* "ax" = an object: include white space */
9116
9117 /* Make sure (), [], {} and <> are in 'matchpairs' */
9118 mps_save = curbuf->b_p_mps;
9119 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9120
9121 switch (cap->nchar)
9122 {
9123 case 'w': /* "aw" = a word */
9124 flag = current_word(cap->oap, cap->count1, include, FALSE);
9125 break;
9126 case 'W': /* "aW" = a WORD */
9127 flag = current_word(cap->oap, cap->count1, include, TRUE);
9128 break;
9129 case 'b': /* "ab" = a braces block */
9130 case '(':
9131 case ')':
9132 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9133 break;
9134 case 'B': /* "aB" = a Brackets block */
9135 case '{':
9136 case '}':
9137 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9138 break;
9139 case '[': /* "a[" = a [] block */
9140 case ']':
9141 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9142 break;
9143 case '<': /* "a<" = a <> block */
9144 case '>':
9145 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9146 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009147 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009148 /* Do not adjust oap->end in do_pending_operator()
9149 * otherwise there are different results for 'dit'
9150 * (note leading whitespace in last line):
9151 * 1) <b> 2) <b>
9152 * foobar foobar
9153 * </b> </b>
9154 */
9155 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009156 flag = current_tagblock(cap->oap, cap->count1, include);
9157 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 case 'p': /* "ap" = a paragraph */
9159 flag = current_par(cap->oap, cap->count1, include, 'p');
9160 break;
9161 case 's': /* "as" = a sentence */
9162 flag = current_sent(cap->oap, cap->count1, include);
9163 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009164 case '"': /* "a"" = a double quoted string */
9165 case '\'': /* "a'" = a single quoted string */
9166 case '`': /* "a`" = a backtick quoted string */
9167 flag = current_quote(cap->oap, cap->count1, include,
9168 cap->nchar);
9169 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170#if 0 /* TODO */
9171 case 'S': /* "aS" = a section */
9172 case 'f': /* "af" = a filename */
9173 case 'u': /* "au" = a URL */
9174#endif
9175 default:
9176 flag = FAIL;
9177 break;
9178 }
9179
9180 curbuf->b_p_mps = mps_save;
9181 if (flag == FAIL)
9182 clearopbeep(cap->oap);
9183 adjust_cursor_col();
9184 curwin->w_set_curswant = TRUE;
9185}
9186#endif
9187
9188/*
9189 * "q" command: Start/stop recording.
9190 * "q:", "q/", "q?": edit command-line in command-line window.
9191 */
9192 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009193nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
9195 if (cap->oap->op_type == OP_FORMAT)
9196 {
9197 /* "gqq" is the same as "gqgq": format line */
9198 cap->cmdchar = 'g';
9199 cap->nchar = 'q';
9200 nv_operator(cap);
9201 }
9202 else if (!checkclearop(cap->oap))
9203 {
9204#ifdef FEAT_CMDWIN
9205 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9206 {
9207 stuffcharReadbuff(cap->nchar);
9208 stuffcharReadbuff(K_CMDWIN);
9209 }
9210 else
9211#endif
9212 /* (stop) recording into a named register, unless executing a
9213 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009214 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 clearopbeep(cap->oap);
9216 }
9217}
9218
9219/*
9220 * Handle the "@r" command.
9221 */
9222 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009223nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224{
9225 if (checkclearop(cap->oap))
9226 return;
9227#ifdef FEAT_EVAL
9228 if (cap->nchar == '=')
9229 {
9230 if (get_expr_register() == NUL)
9231 return;
9232 }
9233#endif
9234 while (cap->count1-- && !got_int)
9235 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009236 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 {
9238 clearopbeep(cap->oap);
9239 break;
9240 }
9241 line_breakcheck();
9242 }
9243}
9244
9245/*
9246 * Handle the CTRL-U and CTRL-D commands.
9247 */
9248 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009249nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250{
9251 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9252 || (cap->cmdchar == Ctrl_D
9253 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9254 clearopbeep(cap->oap);
9255 else if (!checkclearop(cap->oap))
9256 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9257}
9258
9259/*
9260 * Handle "J" or "gJ" command.
9261 */
9262 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009263nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 if (VIsual_active) /* join the visual lines */
9266 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009267 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 {
9269 if (cap->count0 <= 1)
9270 cap->count0 = 2; /* default for join is two lines! */
9271 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9272 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009274 /* can't join when on the last line */
9275 if (cap->count0 <= 2)
9276 {
9277 clearopbeep(cap->oap);
9278 return;
9279 }
9280 cap->count0 = curbuf->b_ml.ml_line_count
9281 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009283
9284 prep_redo(cap->oap->regname, cap->count0,
9285 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9286 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 }
9288}
9289
9290/*
9291 * "P", "gP", "p" and "gp" commands.
9292 */
9293 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009294nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 int regname = 0;
9297 void *reg1 = NULL, *reg2 = NULL;
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009298 int empty = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009299 int was_visual = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 int dir;
9301 int flags = 0;
9302
9303 if (cap->oap->op_type != OP_NOP)
9304 {
9305#ifdef FEAT_DIFF
9306 /* "dp" is ":diffput" */
9307 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'p')
9308 {
9309 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009310 nv_diffgetput(TRUE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 }
9312 else
9313#endif
9314 clearopbeep(cap->oap);
9315 }
Bram Moolenaarf2732452018-06-03 14:47:35 +02009316#ifdef FEAT_JOB_CHANNEL
9317 else if (bt_prompt(curbuf) && !prompt_curpos_editable())
9318 {
9319 clearopbeep(cap->oap);
9320 }
9321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 else
9323 {
9324 dir = (cap->cmdchar == 'P'
9325 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
9326 ? BACKWARD : FORWARD;
9327 prep_redo_cmd(cap);
9328 if (cap->cmdchar == 'g')
9329 flags |= PUT_CURSEND;
9330
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 if (VIsual_active)
9332 {
9333 /* Putting in Visual mode: The put text replaces the selected
9334 * text. First delete the selected text, then put the new text.
9335 * Need to save and restore the registers that the delete
9336 * overwrites if the old contents is being put.
9337 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009338 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009340#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009342#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009343 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009344 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009345#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348
9349 )
9350 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009351 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 * put, save it first. */
9353 reg1 = get_register(regname, TRUE);
9354 }
9355
9356 /* Now delete the selected text. */
9357 cap->cmdchar = 'd';
9358 cap->nchar = NUL;
9359 cap->oap->regname = NUL;
9360 nv_operator(cap);
9361 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009362 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363
9364 /* delete PUT_LINE_BACKWARD; */
9365 cap->oap->regname = regname;
9366
9367 if (reg1 != NULL)
9368 {
9369 /* Delete probably changed the register we want to put, save
9370 * it first. Then put back what was there before the delete. */
9371 reg2 = get_register(regname, FALSE);
9372 put_register(regname, reg1);
9373 }
9374
9375 /* When deleted a linewise Visual area, put the register as
9376 * lines to avoid it joined with the next line. When deletion was
9377 * characterwise, split a line when putting lines. */
9378 if (VIsual_mode == 'V')
9379 flags |= PUT_LINE;
9380 else if (VIsual_mode == 'v')
9381 flags |= PUT_LINE_SPLIT;
9382 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9383 flags |= PUT_LINE_FORWARD;
9384 dir = BACKWARD;
9385 if ((VIsual_mode != 'V'
9386 && curwin->w_cursor.col < curbuf->b_op_start.col)
9387 || (VIsual_mode == 'V'
9388 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9389 /* cursor is at the end of the line or end of file, put
9390 * forward. */
9391 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009392 /* May have been reset in do_put(). */
9393 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 do_put(cap->oap->regname, dir, cap->count1, flags);
9396
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 /* If a register was saved, put it back now. */
9398 if (reg2 != NULL)
9399 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009400
9401 /* What to reselect with "gv"? Selecting the just put text seems to
9402 * be the most useful, since the original text was removed. */
9403 if (was_visual)
9404 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009405 curbuf->b_visual.vi_start = curbuf->b_op_start;
9406 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009407 /* need to adjust cursor position */
9408 if (*p_sel == 'e')
9409 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009410 }
9411
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009412 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009413 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009414 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009415 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009416 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009417
9418 /* If the cursor was in that line, move it to the end of the last
9419 * line. */
9420 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9421 {
9422 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9423 coladvance((colnr_T)MAXCOL);
9424 }
9425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 auto_format(FALSE, TRUE);
9427 }
9428}
9429
9430/*
9431 * "o" and "O" commands.
9432 */
9433 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009434nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435{
9436#ifdef FEAT_DIFF
9437 /* "do" is ":diffget" */
9438 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9439 {
9440 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009441 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 }
9443 else
9444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 if (VIsual_active) /* switch start and end of visual */
9446 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009447#ifdef FEAT_JOB_CHANNEL
9448 else if (bt_prompt(curbuf))
9449 {
9450 clearopbeep(cap->oap);
9451 }
9452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 n_opencmd(cap);
9455}
9456
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457#ifdef FEAT_NETBEANS_INTG
9458 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009459nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460{
9461 netbeans_keycommand(cap->nchar);
9462}
9463#endif
9464
9465#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009467nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9470}
9471#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009472
Bram Moolenaar3918c952005-03-15 22:34:55 +00009473/*
9474 * Trigger CursorHold event.
9475 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9476 * input buffer. "did_cursorhold" is set to avoid retriggering.
9477 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009478 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009479nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009480{
9481 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9482 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009483 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009484}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009485
9486/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009487 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009488 */
9489 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009490get_op_vcol(
9491 oparg_T *oap,
9492 colnr_T redo_VIsual_vcol,
9493 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009494{
9495 colnr_T start, end;
9496
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009497 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009498 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009499 return;
9500
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009501 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009502
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009503 /* prevent from moving onto a trail byte */
9504 if (has_mbyte)
9505 mb_adjustpos(curwin->w_buffer, &oap->end);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009506
9507 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009508
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009509 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009510 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009511 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9512
9513 if (start < oap->start_vcol)
9514 oap->start_vcol = start;
9515 if (end > oap->end_vcol)
9516 {
9517 if (initial && *p_sel == 'e' && start >= 1
9518 && start - 1 >= oap->end_vcol)
9519 oap->end_vcol = start - 1;
9520 else
9521 oap->end_vcol = end;
9522 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009523 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009524
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009525 /* if '$' was used, get oap->end_vcol from longest line */
9526 if (curwin->w_curswant == MAXCOL)
9527 {
9528 curwin->w_cursor.col = MAXCOL;
9529 oap->end_vcol = 0;
9530 for (curwin->w_cursor.lnum = oap->start.lnum;
9531 curwin->w_cursor.lnum <= oap->end.lnum;
9532 ++curwin->w_cursor.lnum)
9533 {
9534 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9535 if (end > oap->end_vcol)
9536 oap->end_vcol = end;
9537 }
9538 }
9539 else if (redo_VIsual_busy)
9540 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9541 /*
9542 * Correct oap->end.col and oap->start.col to be the
9543 * upper-left and lower-right corner of the block area.
9544 *
9545 * (Actually, this does convert column positions into character
9546 * positions)
9547 */
9548 curwin->w_cursor.lnum = oap->end.lnum;
9549 coladvance(oap->end_vcol);
9550 oap->end = curwin->w_cursor;
9551
9552 curwin->w_cursor = oap->start;
9553 coladvance(oap->start_vcol);
9554 oap->start = curwin->w_cursor;
9555}