blob: f7a3e84dc31e1a660ebac355140129465b61404e [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 Moolenaareae1b912019-05-09 15:12:55 +020030static int nv_compare(const void *s1, const void *s2);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010031static void op_colon(oparg_T *oap);
32static void op_function(oparg_T *oap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010033#if defined(FEAT_MOUSE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void find_start_of_word(pos_T *);
35static void find_end_of_word(pos_T *);
36static int get_mouse_class(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void prep_redo(int regname, long, int, int, int, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void clearop(oparg_T *oap);
40static void clearopbeep(oparg_T *oap);
41static void unshift_special(cmdarg_T *cap);
42static void may_clear_cmdline(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#ifdef FEAT_CMDL_INFO
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void del_from_showcmd(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045#endif
46
47/*
48 * nv_*(): functions called to handle Normal and Visual mode commands.
49 * n_*(): functions called to handle Normal mode commands.
50 * v_*(): functions called to handle Visual mode commands.
51 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010052static void nv_ignore(cmdarg_T *cap);
53static void nv_nop(cmdarg_T *cap);
54static void nv_error(cmdarg_T *cap);
55static void nv_help(cmdarg_T *cap);
56static void nv_addsub(cmdarg_T *cap);
57static void nv_page(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#ifdef FEAT_MOUSE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010059static void nv_mousescroll(cmdarg_T *cap);
60static void nv_mouse(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010062static void nv_scroll_line(cmdarg_T *cap);
63static void nv_zet(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_GUI
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010065static void nv_ver_scrollbar(cmdarg_T *cap);
66static void nv_hor_scrollbar(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +000068#ifdef FEAT_GUI_TABLINE
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010069static void nv_tabline(cmdarg_T *cap);
70static void nv_tabmenu(cmdarg_T *cap);
Bram Moolenaar32466aa2006-02-24 23:53:04 +000071#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010072static void nv_exmode(cmdarg_T *cap);
73static void nv_colon(cmdarg_T *cap);
74static void nv_ctrlg(cmdarg_T *cap);
75static void nv_ctrlh(cmdarg_T *cap);
76static void nv_clear(cmdarg_T *cap);
77static void nv_ctrlo(cmdarg_T *cap);
78static void nv_hat(cmdarg_T *cap);
79static void nv_Zet(cmdarg_T *cap);
80static void nv_ident(cmdarg_T *cap);
81static void nv_tagpop(cmdarg_T *cap);
82static void nv_scroll(cmdarg_T *cap);
83static void nv_right(cmdarg_T *cap);
84static void nv_left(cmdarg_T *cap);
85static void nv_up(cmdarg_T *cap);
86static void nv_down(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010087static void nv_end(cmdarg_T *cap);
88static void nv_dollar(cmdarg_T *cap);
89static void nv_search(cmdarg_T *cap);
90static void nv_next(cmdarg_T *cap);
91static int normal_search(cmdarg_T *cap, int dir, char_u *pat, int opt);
92static void nv_csearch(cmdarg_T *cap);
93static void nv_brackets(cmdarg_T *cap);
94static void nv_percent(cmdarg_T *cap);
95static void nv_brace(cmdarg_T *cap);
96static void nv_mark(cmdarg_T *cap);
97static void nv_findpar(cmdarg_T *cap);
98static void nv_undo(cmdarg_T *cap);
99static void nv_kundo(cmdarg_T *cap);
100static void nv_Replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100101static void nv_replace(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100102static void nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos);
103static void v_visop(cmdarg_T *cap);
104static void nv_subst(cmdarg_T *cap);
105static void nv_abbrev(cmdarg_T *cap);
106static void nv_optrans(cmdarg_T *cap);
107static void nv_gomark(cmdarg_T *cap);
108static void nv_pcmark(cmdarg_T *cap);
109static void nv_regname(cmdarg_T *cap);
110static void nv_visual(cmdarg_T *cap);
111static void n_start_visual_mode(int c);
112static void nv_window(cmdarg_T *cap);
113static void nv_suspend(cmdarg_T *cap);
114static void nv_g_cmd(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void nv_dot(cmdarg_T *cap);
116static void nv_redo(cmdarg_T *cap);
117static void nv_Undo(cmdarg_T *cap);
118static void nv_tilde(cmdarg_T *cap);
119static void nv_operator(cmdarg_T *cap);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000120#ifdef FEAT_EVAL
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void set_op_var(int optype);
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000122#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100123static void nv_lineop(cmdarg_T *cap);
124static void nv_home(cmdarg_T *cap);
125static void nv_pipe(cmdarg_T *cap);
126static void nv_bck_word(cmdarg_T *cap);
127static void nv_wordcmd(cmdarg_T *cap);
128static void nv_beginline(cmdarg_T *cap);
129static void adjust_cursor(oparg_T *oap);
130static void adjust_for_sel(cmdarg_T *cap);
131static int unadjust_for_sel(void);
132static void nv_select(cmdarg_T *cap);
133static void nv_goto(cmdarg_T *cap);
134static void nv_normal(cmdarg_T *cap);
135static void nv_esc(cmdarg_T *oap);
136static void nv_edit(cmdarg_T *cap);
137static void invoke_edit(cmdarg_T *cap, int repl, int cmd, int startln);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#ifdef FEAT_TEXTOBJ
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100139static void nv_object(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100141static void nv_record(cmdarg_T *cap);
142static void nv_at(cmdarg_T *cap);
143static void nv_halfpage(cmdarg_T *cap);
144static void nv_join(cmdarg_T *cap);
145static void nv_put(cmdarg_T *cap);
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200146static void nv_put_opt(cmdarg_T *cap, int fix_indent);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100147static void nv_open(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100149static void nv_nbcmd(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#endif
151#ifdef FEAT_DND
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100152static void nv_drop(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100154static void nv_cursorhold(cmdarg_T *cap);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100155static void get_op_vcol(oparg_T *oap, colnr_T col, int initial);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
Bram Moolenaar9fd01c62008-11-01 12:52:38 +0000157static char *e_noident = N_("E349: No identifier under cursor");
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * Function to be called for a Normal or Visual mode command.
161 * The argument is a cmdarg_T.
162 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100163typedef void (*nv_func_T)(cmdarg_T *cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
165/* Values for cmd_flags. */
166#define NV_NCH 0x01 /* may need to get a second char */
167#define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
168#define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
169#define NV_LANG 0x08 /* second char needs language adjustment */
170
171#define NV_SS 0x10 /* may start selection */
172#define NV_SSS 0x20 /* may start selection with shift modifier */
173#define NV_STS 0x40 /* may stop selection without shift modif. */
174#define NV_RL 0x80 /* 'rightleft' modifies command */
175#define NV_KEEPREG 0x100 /* don't clear regname */
176#define NV_NCW 0x200 /* not allowed in command-line window */
177
178/*
179 * Generally speaking, every Normal mode command should either clear any
180 * pending operator (with *clearop*()), or set the motion type variable
181 * oap->motion_type.
182 *
183 * When a cursor motion command is made, it is marked as being a character or
184 * line oriented motion. Then, if an operator is in effect, the operation
185 * becomes character or line oriented accordingly.
186 */
187
188/*
189 * This table contains one entry for every Normal or Visual mode command.
190 * The order doesn't matter, init_normal_cmds() will create a sorted index.
191 * It is faster when all keys from zero to '~' are present.
192 */
193static const struct nv_cmd
194{
195 int cmd_char; /* (first) command character */
196 nv_func_T cmd_func; /* function for this command */
197 short_u cmd_flags; /* NV_ flags */
198 short cmd_arg; /* value for ca.arg */
199} nv_cmds[] =
200{
201 {NUL, nv_error, 0, 0},
202 {Ctrl_A, nv_addsub, 0, 0},
203 {Ctrl_B, nv_page, NV_STS, BACKWARD},
204 {Ctrl_C, nv_esc, 0, TRUE},
205 {Ctrl_D, nv_halfpage, 0, 0},
206 {Ctrl_E, nv_scroll_line, 0, TRUE},
207 {Ctrl_F, nv_page, NV_STS, FORWARD},
208 {Ctrl_G, nv_ctrlg, 0, 0},
209 {Ctrl_H, nv_ctrlh, 0, 0},
210 {Ctrl_I, nv_pcmark, 0, 0},
211 {NL, nv_down, 0, FALSE},
212 {Ctrl_K, nv_error, 0, 0},
213 {Ctrl_L, nv_clear, 0, 0},
Bram Moolenaar2c519cf2019-03-21 21:45:34 +0100214 {CAR, nv_down, 0, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 {Ctrl_N, nv_down, NV_STS, FALSE},
216 {Ctrl_O, nv_ctrlo, 0, 0},
217 {Ctrl_P, nv_up, NV_STS, FALSE},
Bram Moolenaardf177f62005-02-22 08:39:57 +0000218 {Ctrl_Q, nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 {Ctrl_R, nv_redo, 0, 0},
220 {Ctrl_S, nv_ignore, 0, 0},
221 {Ctrl_T, nv_tagpop, NV_NCW, 0},
222 {Ctrl_U, nv_halfpage, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 {Ctrl_V, nv_visual, 0, FALSE},
224 {'V', nv_visual, 0, FALSE},
225 {'v', nv_visual, 0, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 {Ctrl_W, nv_window, 0, 0},
227 {Ctrl_X, nv_addsub, 0, 0},
228 {Ctrl_Y, nv_scroll_line, 0, FALSE},
229 {Ctrl_Z, nv_suspend, 0, 0},
230 {ESC, nv_esc, 0, FALSE},
231 {Ctrl_BSL, nv_normal, NV_NCH_ALW, 0},
232 {Ctrl_RSB, nv_ident, NV_NCW, 0},
233 {Ctrl_HAT, nv_hat, NV_NCW, 0},
234 {Ctrl__, nv_error, 0, 0},
235 {' ', nv_right, 0, 0},
236 {'!', nv_operator, 0, 0},
237 {'"', nv_regname, NV_NCH_NOP|NV_KEEPREG, 0},
238 {'#', nv_ident, 0, 0},
239 {'$', nv_dollar, 0, 0},
240 {'%', nv_percent, 0, 0},
241 {'&', nv_optrans, 0, 0},
242 {'\'', nv_gomark, NV_NCH_ALW, TRUE},
243 {'(', nv_brace, 0, BACKWARD},
244 {')', nv_brace, 0, FORWARD},
245 {'*', nv_ident, 0, 0},
246 {'+', nv_down, 0, TRUE},
247 {',', nv_csearch, 0, TRUE},
248 {'-', nv_up, 0, TRUE},
249 {'.', nv_dot, NV_KEEPREG, 0},
250 {'/', nv_search, 0, FALSE},
251 {'0', nv_beginline, 0, 0},
252 {'1', nv_ignore, 0, 0},
253 {'2', nv_ignore, 0, 0},
254 {'3', nv_ignore, 0, 0},
255 {'4', nv_ignore, 0, 0},
256 {'5', nv_ignore, 0, 0},
257 {'6', nv_ignore, 0, 0},
258 {'7', nv_ignore, 0, 0},
259 {'8', nv_ignore, 0, 0},
260 {'9', nv_ignore, 0, 0},
261 {':', nv_colon, 0, 0},
262 {';', nv_csearch, 0, FALSE},
263 {'<', nv_operator, NV_RL, 0},
264 {'=', nv_operator, 0, 0},
265 {'>', nv_operator, NV_RL, 0},
266 {'?', nv_search, 0, FALSE},
267 {'@', nv_at, NV_NCH_NOP, FALSE},
268 {'A', nv_edit, 0, 0},
269 {'B', nv_bck_word, 0, 1},
270 {'C', nv_abbrev, NV_KEEPREG, 0},
271 {'D', nv_abbrev, NV_KEEPREG, 0},
272 {'E', nv_wordcmd, 0, TRUE},
273 {'F', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
274 {'G', nv_goto, 0, TRUE},
275 {'H', nv_scroll, 0, 0},
276 {'I', nv_edit, 0, 0},
277 {'J', nv_join, 0, 0},
278 {'K', nv_ident, 0, 0},
279 {'L', nv_scroll, 0, 0},
280 {'M', nv_scroll, 0, 0},
281 {'N', nv_next, 0, SEARCH_REV},
282 {'O', nv_open, 0, 0},
283 {'P', nv_put, 0, 0},
284 {'Q', nv_exmode, NV_NCW, 0},
285 {'R', nv_Replace, 0, FALSE},
286 {'S', nv_subst, NV_KEEPREG, 0},
287 {'T', nv_csearch, NV_NCH_ALW|NV_LANG, BACKWARD},
288 {'U', nv_Undo, 0, 0},
289 {'W', nv_wordcmd, 0, TRUE},
290 {'X', nv_abbrev, NV_KEEPREG, 0},
291 {'Y', nv_abbrev, NV_KEEPREG, 0},
292 {'Z', nv_Zet, NV_NCH_NOP|NV_NCW, 0},
293 {'[', nv_brackets, NV_NCH_ALW, BACKWARD},
294 {'\\', nv_error, 0, 0},
295 {']', nv_brackets, NV_NCH_ALW, FORWARD},
296 {'^', nv_beginline, 0, BL_WHITE | BL_FIX},
297 {'_', nv_lineop, 0, 0},
298 {'`', nv_gomark, NV_NCH_ALW, FALSE},
299 {'a', nv_edit, NV_NCH, 0},
300 {'b', nv_bck_word, 0, 0},
301 {'c', nv_operator, 0, 0},
302 {'d', nv_operator, 0, 0},
303 {'e', nv_wordcmd, 0, FALSE},
304 {'f', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
305 {'g', nv_g_cmd, NV_NCH_ALW, FALSE},
306 {'h', nv_left, NV_RL, 0},
307 {'i', nv_edit, NV_NCH, 0},
308 {'j', nv_down, 0, FALSE},
309 {'k', nv_up, 0, FALSE},
310 {'l', nv_right, NV_RL, 0},
311 {'m', nv_mark, NV_NCH_NOP, 0},
312 {'n', nv_next, 0, 0},
313 {'o', nv_open, 0, 0},
314 {'p', nv_put, 0, 0},
315 {'q', nv_record, NV_NCH, 0},
316 {'r', nv_replace, NV_NCH_NOP|NV_LANG, 0},
317 {'s', nv_subst, NV_KEEPREG, 0},
318 {'t', nv_csearch, NV_NCH_ALW|NV_LANG, FORWARD},
319 {'u', nv_undo, 0, 0},
320 {'w', nv_wordcmd, 0, FALSE},
321 {'x', nv_abbrev, NV_KEEPREG, 0},
322 {'y', nv_operator, 0, 0},
323 {'z', nv_zet, NV_NCH_ALW, 0},
324 {'{', nv_findpar, 0, BACKWARD},
325 {'|', nv_pipe, 0, 0},
326 {'}', nv_findpar, 0, FORWARD},
327 {'~', nv_tilde, 0, 0},
328
329 /* pound sign */
330 {POUND, nv_ident, 0, 0},
331#ifdef FEAT_MOUSE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200332 {K_MOUSEUP, nv_mousescroll, 0, MSCR_UP},
333 {K_MOUSEDOWN, nv_mousescroll, 0, MSCR_DOWN},
334 {K_MOUSELEFT, nv_mousescroll, 0, MSCR_LEFT},
335 {K_MOUSERIGHT, nv_mousescroll, 0, MSCR_RIGHT},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 {K_LEFTMOUSE, nv_mouse, 0, 0},
337 {K_LEFTMOUSE_NM, nv_mouse, 0, 0},
338 {K_LEFTDRAG, nv_mouse, 0, 0},
339 {K_LEFTRELEASE, nv_mouse, 0, 0},
340 {K_LEFTRELEASE_NM, nv_mouse, 0, 0},
Bram Moolenaar51b0f372017-11-18 18:52:04 +0100341 {K_MOUSEMOVE, nv_mouse, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {K_MIDDLEMOUSE, nv_mouse, 0, 0},
343 {K_MIDDLEDRAG, nv_mouse, 0, 0},
344 {K_MIDDLERELEASE, nv_mouse, 0, 0},
345 {K_RIGHTMOUSE, nv_mouse, 0, 0},
346 {K_RIGHTDRAG, nv_mouse, 0, 0},
347 {K_RIGHTRELEASE, nv_mouse, 0, 0},
348 {K_X1MOUSE, nv_mouse, 0, 0},
349 {K_X1DRAG, nv_mouse, 0, 0},
350 {K_X1RELEASE, nv_mouse, 0, 0},
351 {K_X2MOUSE, nv_mouse, 0, 0},
352 {K_X2DRAG, nv_mouse, 0, 0},
353 {K_X2RELEASE, nv_mouse, 0, 0},
354#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000355 {K_IGNORE, nv_ignore, NV_KEEPREG, 0},
Bram Moolenaarebefac62005-12-28 22:39:57 +0000356 {K_NOP, nv_nop, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {K_INS, nv_edit, 0, 0},
358 {K_KINS, nv_edit, 0, 0},
359 {K_BS, nv_ctrlh, 0, 0},
360 {K_UP, nv_up, NV_SSS|NV_STS, FALSE},
361 {K_S_UP, nv_page, NV_SS, BACKWARD},
362 {K_DOWN, nv_down, NV_SSS|NV_STS, FALSE},
363 {K_S_DOWN, nv_page, NV_SS, FORWARD},
364 {K_LEFT, nv_left, NV_SSS|NV_STS|NV_RL, 0},
365 {K_S_LEFT, nv_bck_word, NV_SS|NV_RL, 0},
366 {K_C_LEFT, nv_bck_word, NV_SSS|NV_RL|NV_STS, 1},
367 {K_RIGHT, nv_right, NV_SSS|NV_STS|NV_RL, 0},
368 {K_S_RIGHT, nv_wordcmd, NV_SS|NV_RL, FALSE},
369 {K_C_RIGHT, nv_wordcmd, NV_SSS|NV_RL|NV_STS, TRUE},
370 {K_PAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
371 {K_KPAGEUP, nv_page, NV_SSS|NV_STS, BACKWARD},
372 {K_PAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
373 {K_KPAGEDOWN, nv_page, NV_SSS|NV_STS, FORWARD},
374 {K_END, nv_end, NV_SSS|NV_STS, FALSE},
375 {K_KEND, nv_end, NV_SSS|NV_STS, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {K_S_END, nv_end, NV_SS, FALSE},
377 {K_C_END, nv_end, NV_SSS|NV_STS, TRUE},
378 {K_HOME, nv_home, NV_SSS|NV_STS, 0},
379 {K_KHOME, nv_home, NV_SSS|NV_STS, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 {K_S_HOME, nv_home, NV_SS, 0},
381 {K_C_HOME, nv_goto, NV_SSS|NV_STS, FALSE},
382 {K_DEL, nv_abbrev, 0, 0},
383 {K_KDEL, nv_abbrev, 0, 0},
384 {K_UNDO, nv_kundo, 0, 0},
385 {K_HELP, nv_help, NV_NCW, 0},
386 {K_F1, nv_help, NV_NCW, 0},
387 {K_XF1, nv_help, NV_NCW, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {K_SELECT, nv_select, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#ifdef FEAT_GUI
390 {K_VER_SCROLLBAR, nv_ver_scrollbar, 0, 0},
391 {K_HOR_SCROLLBAR, nv_hor_scrollbar, 0, 0},
392#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000393#ifdef FEAT_GUI_TABLINE
394 {K_TABLINE, nv_tabline, 0, 0},
Bram Moolenaarba6c0522006-02-25 21:45:02 +0000395 {K_TABMENU, nv_tabmenu, 0, 0},
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397#ifdef FEAT_NETBEANS_INTG
398 {K_F21, nv_nbcmd, NV_NCH_ALW, 0},
399#endif
400#ifdef FEAT_DND
401 {K_DROP, nv_drop, NV_STS, 0},
402#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403 {K_CURSORHOLD, nv_cursorhold, NV_KEEPREG, 0},
Bram Moolenaarec2da362017-01-21 20:04:22 +0100404 {K_PS, nv_edit, 0, 0},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405};
406
407/* Number of commands in nv_cmds[]. */
408#define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
409
410/* Sorted index of commands in nv_cmds[]. */
411static short nv_cmd_idx[NV_CMDS_SIZE];
412
413/* The highest index for which
414 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
415static int nv_max_linear;
416
417/*
418 * Compare functions for qsort() below, that checks the command character
419 * through the index in nv_cmd_idx[].
420 */
421 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100422nv_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423{
424 int c1, c2;
425
426 /* The commands are sorted on absolute value. */
427 c1 = nv_cmds[*(const short *)s1].cmd_char;
428 c2 = nv_cmds[*(const short *)s2].cmd_char;
429 if (c1 < 0)
430 c1 = -c1;
431 if (c2 < 0)
432 c2 = -c2;
433 return c1 - c2;
434}
435
436/*
437 * Initialize the nv_cmd_idx[] table.
438 */
439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440init_normal_cmds(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
442 int i;
443
444 /* Fill the index table with a one to one relation. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000445 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 nv_cmd_idx[i] = i;
447
448 /* Sort the commands by the command character. */
449 qsort((void *)&nv_cmd_idx, (size_t)NV_CMDS_SIZE, sizeof(short), nv_compare);
450
451 /* Find the first entry that can't be indexed by the command character. */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000452 for (i = 0; i < (int)NV_CMDS_SIZE; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 if (i != nv_cmds[nv_cmd_idx[i]].cmd_char)
454 break;
455 nv_max_linear = i - 1;
456}
457
458/*
459 * Search for a command in the commands table.
460 * Returns -1 for invalid command.
461 */
462 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100463find_command(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464{
465 int i;
466 int idx;
467 int top, bot;
468 int c;
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 /* A multi-byte character is never a command. */
471 if (cmdchar >= 0x100)
472 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
474 /* We use the absolute value of the character. Special keys have a
475 * negative value, but are sorted on their absolute value. */
476 if (cmdchar < 0)
477 cmdchar = -cmdchar;
478
479 /* If the character is in the first part: The character is the index into
480 * nv_cmd_idx[]. */
481 if (cmdchar <= nv_max_linear)
482 return nv_cmd_idx[cmdchar];
483
484 /* Perform a binary search. */
485 bot = nv_max_linear + 1;
486 top = NV_CMDS_SIZE - 1;
487 idx = -1;
488 while (bot <= top)
489 {
490 i = (top + bot) / 2;
491 c = nv_cmds[nv_cmd_idx[i]].cmd_char;
492 if (c < 0)
493 c = -c;
494 if (cmdchar == c)
495 {
496 idx = nv_cmd_idx[i];
497 break;
498 }
499 if (cmdchar > c)
500 bot = i + 1;
501 else
502 top = i - 1;
503 }
504 return idx;
505}
506
507/*
508 * Execute a command in Normal mode.
509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 void
Bram Moolenaar9b578142016-01-30 19:39:49 +0100511normal_cmd(
512 oparg_T *oap,
513 int toplevel UNUSED) /* TRUE when called from main() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 cmdarg_T ca; /* command arguments */
516 int c;
517 int ctrl_w = FALSE; /* got CTRL-W command */
518 int old_col = curwin->w_curswant;
519#ifdef FEAT_CMDL_INFO
520 int need_flushbuf; /* need to call out_flush() */
521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 pos_T old_pos; /* cursor position before command */
523 int mapped_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 static int old_mapped_len = 0;
525 int idx;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000526#ifdef FEAT_EVAL
527 int set_prevcount = FALSE;
528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
530 vim_memset(&ca, 0, sizeof(ca)); /* also resets ca.retval */
531 ca.oap = oap;
Bram Moolenaara983fe92008-07-31 20:04:27 +0000532
533 /* Use a count remembered from before entering an operator. After typing
534 * "3d" we return from normal_cmd() and come back here, the "3" is
535 * remembered in "opcount". */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 ca.opcount = opcount;
537
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 /*
539 * If there is an operator pending, then the command we take this time
540 * will terminate it. Finish_op tells us to finish the operation before
541 * returning this time (unless the operation was cancelled).
542 */
543#ifdef CURSOR_SHAPE
544 c = finish_op;
545#endif
546 finish_op = (oap->op_type != OP_NOP);
547#ifdef CURSOR_SHAPE
548 if (finish_op != c)
549 {
550 ui_cursor_shape(); /* may show different cursor shape */
551# ifdef FEAT_MOUSESHAPE
552 update_mouseshape(-1);
553# endif
554 }
555#endif
556
Bram Moolenaara983fe92008-07-31 20:04:27 +0000557 /* When not finishing an operator and no register name typed, reset the
558 * count. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 if (!finish_op && !oap->regname)
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 ca.opcount = 0;
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000562#ifdef FEAT_EVAL
563 set_prevcount = TRUE;
564#endif
565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
Bram Moolenaara983fe92008-07-31 20:04:27 +0000567 /* Restore counts from before receiving K_CURSORHOLD. This means after
568 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
569 * "3 * 2". */
570 if (oap->prev_opcount > 0 || oap->prev_count0 > 0)
571 {
572 ca.opcount = oap->prev_opcount;
573 ca.count0 = oap->prev_count0;
574 oap->prev_opcount = 0;
575 oap->prev_count0 = 0;
576 }
Bram Moolenaara983fe92008-07-31 20:04:27 +0000577
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 mapped_len = typebuf_maplen();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579
580 State = NORMAL_BUSY;
581#ifdef USE_ON_FLY_SCROLL
582 dont_scroll = FALSE; /* allow scrolling here */
583#endif
584
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100585#ifdef FEAT_EVAL
586 /* Set v:count here, when called from main() and not a stuffed
587 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100588 * when there is no count. Do set it for redo. */
589 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100590 set_vcount_ca(&ca, &set_prevcount);
591#endif
592
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 /*
594 * Get the command character from the user.
595 */
596 c = safe_vgetc();
Bram Moolenaar25281632016-01-21 23:32:32 +0100597 LANGMAP_ADJUST(c, get_real_state() != SELECTMODE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599 /*
600 * If a mapping was started in Visual or Select mode, remember the length
601 * of the mapping. This is used below to not return to Insert mode for as
602 * long as the mapping is being executed.
603 */
604 if (restart_edit == 0)
605 old_mapped_len = 0;
606 else if (old_mapped_len
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000607 || (VIsual_active && mapped_len == 0 && typebuf_maplen() > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 old_mapped_len = typebuf_maplen();
609
610 if (c == NUL)
611 c = K_ZERO;
612
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 /*
614 * In Select mode, typed text replaces the selection.
615 */
616 if (VIsual_active
617 && VIsual_select
618 && (vim_isprintc(c) || c == NL || c == CAR || c == K_KENTER))
619 {
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000620 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
621 * 'insertmode' is set) fake a "d"elete command, Insert mode will
622 * restart automatically.
Bram Moolenaarcf8e7d12006-12-05 20:43:17 +0000623 * Insert the typed character in the typeahead buffer, so that it can
624 * be mapped in Insert mode. Required for ":lmap" to work. */
Bram Moolenaard8fc5c02006-04-29 21:55:22 +0000625 ins_char_typebuf(c);
Bram Moolenaar686f51e2005-05-20 21:19:57 +0000626 if (restart_edit != 0)
627 c = 'd';
628 else
629 c = 'c';
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000630 msg_nowait = TRUE; /* don't delay going to insert mode */
Bram Moolenaar9bad29d2013-05-21 12:46:02 +0200631 old_mapped_len = 0; /* do go to Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
634#ifdef FEAT_CMDL_INFO
635 need_flushbuf = add_to_showcmd(c);
636#endif
637
638getcount:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if (!(VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
641 /*
642 * Handle a count before a command and compute ca.count0.
643 * Note that '0' is a command and not the start of a count, but it's
644 * part of a count after other digits.
645 */
646 while ( (c >= '1' && c <= '9')
647 || (ca.count0 != 0 && (c == K_DEL || c == K_KDEL || c == '0')))
648 {
649 if (c == K_DEL || c == K_KDEL)
650 {
651 ca.count0 /= 10;
652#ifdef FEAT_CMDL_INFO
653 del_from_showcmd(4); /* delete the digit and ~@% */
654#endif
655 }
656 else
657 ca.count0 = ca.count0 * 10 + (c - '0');
658 if (ca.count0 < 0) /* got too large! */
659 ca.count0 = 999999999L;
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000660#ifdef FEAT_EVAL
661 /* Set v:count here, when called from main() and not a stuffed
662 * command, so that v:count can be used in an expression mapping
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100663 * right after the count. Do set it for redo. */
664 if (toplevel && readbuf1_empty())
Bram Moolenaarf82a2d22010-12-17 18:53:01 +0100665 set_vcount_ca(&ca, &set_prevcount);
Bram Moolenaarf13249a2007-10-14 15:16:27 +0000666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 if (ctrl_w)
668 {
669 ++no_mapping;
670 ++allow_keys; /* no mapping for nchar, but keys */
671 }
672 ++no_zero_mapping; /* don't map zero here */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000673 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 --no_zero_mapping;
676 if (ctrl_w)
677 {
678 --no_mapping;
679 --allow_keys;
680 }
681#ifdef FEAT_CMDL_INFO
682 need_flushbuf |= add_to_showcmd(c);
683#endif
684 }
685
686 /*
687 * If we got CTRL-W there may be a/another count
688 */
689 if (c == Ctrl_W && !ctrl_w && oap->op_type == OP_NOP)
690 {
691 ctrl_w = TRUE;
692 ca.opcount = ca.count0; /* remember first count */
693 ca.count0 = 0;
694 ++no_mapping;
695 ++allow_keys; /* no mapping for nchar, but keys */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000696 c = plain_vgetc(); /* get next character */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 LANGMAP_ADJUST(c, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 --no_mapping;
699 --allow_keys;
700#ifdef FEAT_CMDL_INFO
701 need_flushbuf |= add_to_showcmd(c);
702#endif
703 goto getcount; /* jump back */
704 }
705 }
706
Bram Moolenaara983fe92008-07-31 20:04:27 +0000707 if (c == K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
Bram Moolenaara983fe92008-07-31 20:04:27 +0000709 /* Save the count values so that ca.opcount and ca.count0 are exactly
710 * the same when coming back here after handling K_CURSORHOLD. */
711 oap->prev_opcount = ca.opcount;
712 oap->prev_count0 = ca.count0;
713 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100714 else if (ca.opcount != 0)
Bram Moolenaara983fe92008-07-31 20:04:27 +0000715 {
716 /*
717 * If we're in the middle of an operator (including after entering a
718 * yank buffer with '"') AND we had a count before the operator, then
719 * that count overrides the current value of ca.count0.
720 * What this means effectively, is that commands like "3dw" get turned
721 * into "d3w" which makes things fall into place pretty neatly.
722 * If you give a count before AND after the operator, they are
723 * multiplied.
724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 if (ca.count0)
726 ca.count0 *= ca.opcount;
727 else
728 ca.count0 = ca.opcount;
729 }
730
731 /*
732 * Always remember the count. It will be set to zero (on the next call,
733 * above) when there is no pending operator.
734 * When called from main(), save the count for use by the "count" built-in
735 * variable.
736 */
737 ca.opcount = ca.count0;
738 ca.count1 = (ca.count0 == 0 ? 1 : ca.count0);
739
740#ifdef FEAT_EVAL
741 /*
742 * Only set v:count when called from main() and not a stuffed command.
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100743 * Do set it for redo.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 */
Bram Moolenaar0a36fec2014-02-11 15:10:43 +0100745 if (toplevel && readbuf1_empty())
Bram Moolenaar8df74be2008-11-20 15:12:02 +0000746 set_vcount(ca.count0, ca.count1, set_prevcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747#endif
748
749 /*
750 * Find the command character in the table of commands.
751 * For CTRL-W we already got nchar when looking for a count.
752 */
753 if (ctrl_w)
754 {
755 ca.nchar = c;
756 ca.cmdchar = Ctrl_W;
757 }
758 else
759 ca.cmdchar = c;
760 idx = find_command(ca.cmdchar);
761 if (idx < 0)
762 {
763 /* Not a known command: beep. */
764 clearopbeep(oap);
765 goto normal_end;
766 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +0000767
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000768 if (text_locked() && (nv_cmds[idx].cmd_flags & NV_NCW))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {
Bram Moolenaard69bd9a2014-04-29 12:15:40 +0200770 /* This command is not allowed while editing a cmdline: beep. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 clearopbeep(oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000772 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 goto normal_end;
774 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000775 if ((nv_cmds[idx].cmd_flags & NV_NCW) && curbuf_locked())
776 goto normal_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 /*
779 * In Visual/Select mode, a few keys are handled in a special way.
780 */
781 if (VIsual_active)
782 {
783 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
784 if (km_stopsel
785 && (nv_cmds[idx].cmd_flags & NV_STS)
786 && !(mod_mask & MOD_MASK_SHIFT))
787 {
788 end_visual_mode();
789 redraw_curbuf_later(INVERTED);
790 }
791
792 /* Keys that work different when 'keymodel' contains "startsel" */
793 if (km_startsel)
794 {
795 if (nv_cmds[idx].cmd_flags & NV_SS)
796 {
797 unshift_special(&ca);
798 idx = find_command(ca.cmdchar);
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000799 if (idx < 0)
800 {
801 /* Just in case */
802 clearopbeep(oap);
803 goto normal_end;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
807 && (mod_mask & MOD_MASK_SHIFT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 mod_mask &= ~MOD_MASK_SHIFT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812#ifdef FEAT_RIGHTLEFT
813 if (curwin->w_p_rl && KeyTyped && !KeyStuffed
814 && (nv_cmds[idx].cmd_flags & NV_RL))
815 {
816 /* Invert horizontal movements and operations. Only when typed by the
817 * user directly, not when the result of a mapping or "x" translated
818 * to "dl". */
819 switch (ca.cmdchar)
820 {
821 case 'l': ca.cmdchar = 'h'; break;
822 case K_RIGHT: ca.cmdchar = K_LEFT; break;
823 case K_S_RIGHT: ca.cmdchar = K_S_LEFT; break;
824 case K_C_RIGHT: ca.cmdchar = K_C_LEFT; break;
825 case 'h': ca.cmdchar = 'l'; break;
826 case K_LEFT: ca.cmdchar = K_RIGHT; break;
827 case K_S_LEFT: ca.cmdchar = K_S_RIGHT; break;
828 case K_C_LEFT: ca.cmdchar = K_C_RIGHT; break;
829 case '>': ca.cmdchar = '<'; break;
830 case '<': ca.cmdchar = '>'; break;
831 }
832 idx = find_command(ca.cmdchar);
833 }
834#endif
835
836 /*
837 * Get an additional character if we need one.
838 */
839 if ((nv_cmds[idx].cmd_flags & NV_NCH)
840 && (((nv_cmds[idx].cmd_flags & NV_NCH_NOP) == NV_NCH_NOP
841 && oap->op_type == OP_NOP)
842 || (nv_cmds[idx].cmd_flags & NV_NCH_ALW) == NV_NCH_ALW
843 || (ca.cmdchar == 'q'
844 && oap->op_type == OP_NOP
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200845 && reg_recording == 0
846 && reg_executing == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 || ((ca.cmdchar == 'a' || ca.cmdchar == 'i')
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100848 && (oap->op_type != OP_NOP || VIsual_active))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
850 int *cp;
851 int repl = FALSE; /* get character for replace mode */
852 int lit = FALSE; /* get extra character literally */
853 int langmap_active = FALSE; /* using :lmap mappings */
854 int lang; /* getting a text character */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100855#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 int save_smd; /* saved value of p_smd */
857#endif
858
859 ++no_mapping;
860 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +0000861 /* Don't generate a CursorHold event here, most commands can't handle
862 * it, e.g., nv_replace(), nv_csearch(). */
863 did_cursorhold = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (ca.cmdchar == 'g')
865 {
866 /*
867 * For 'g' get the next character now, so that we can check for
868 * "gr", "g'" and "g`".
869 */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000870 ca.nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 LANGMAP_ADJUST(ca.nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#ifdef FEAT_CMDL_INFO
873 need_flushbuf |= add_to_showcmd(ca.nchar);
874#endif
875 if (ca.nchar == 'r' || ca.nchar == '\'' || ca.nchar == '`'
Bram Moolenaarba2d44f2013-11-28 19:27:30 +0100876 || ca.nchar == Ctrl_BSL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 {
878 cp = &ca.extra_char; /* need to get a third character */
879 if (ca.nchar != 'r')
880 lit = TRUE; /* get it literally */
881 else
882 repl = TRUE; /* get it in replace mode */
883 }
884 else
885 cp = NULL; /* no third character needed */
886 }
887 else
888 {
889 if (ca.cmdchar == 'r') /* get it in replace mode */
890 repl = TRUE;
891 cp = &ca.nchar;
892 }
893 lang = (repl || (nv_cmds[idx].cmd_flags & NV_LANG));
894
895 /*
896 * Get a second or third character.
897 */
898 if (cp != NULL)
899 {
900#ifdef CURSOR_SHAPE
901 if (repl)
902 {
903 State = REPLACE; /* pretend Replace mode */
904 ui_cursor_shape(); /* show different cursor shape */
905 }
906#endif
907 if (lang && curbuf->b_p_iminsert == B_IMODE_LMAP)
908 {
909 /* Allow mappings defined with ":lmap". */
910 --no_mapping;
911 --allow_keys;
912 if (repl)
913 State = LREPLACE;
914 else
915 State = LANGMAP;
916 langmap_active = TRUE;
917 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100918#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 save_smd = p_smd;
920 p_smd = FALSE; /* Don't let the IM code show the mode here */
921 if (lang && curbuf->b_p_iminsert == B_IMODE_IM)
922 im_set_active(TRUE);
923#endif
924
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000925 *cp = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
927 if (langmap_active)
928 {
929 /* Undo the decrement done above */
930 ++no_mapping;
931 ++allow_keys;
932 State = NORMAL_BUSY;
933 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100934#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 if (lang)
936 {
937 if (curbuf->b_p_iminsert != B_IMODE_LMAP)
938 im_save_status(&curbuf->b_p_iminsert);
939 im_set_active(FALSE);
940 }
941 p_smd = save_smd;
942#endif
943#ifdef CURSOR_SHAPE
944 State = NORMAL_BUSY;
945#endif
946#ifdef FEAT_CMDL_INFO
947 need_flushbuf |= add_to_showcmd(*cp);
948#endif
949
950 if (!lit)
951 {
952#ifdef FEAT_DIGRAPHS
953 /* Typing CTRL-K gets a digraph. */
954 if (*cp == Ctrl_K
955 && ((nv_cmds[idx].cmd_flags & NV_LANG)
956 || cp == &ca.extra_char)
957 && vim_strchr(p_cpo, CPO_DIGRAPH) == NULL)
958 {
959 c = get_digraph(FALSE);
960 if (c > 0)
961 {
962 *cp = c;
963# ifdef FEAT_CMDL_INFO
964 /* Guessing how to update showcmd here... */
965 del_from_showcmd(3);
966 need_flushbuf |= add_to_showcmd(*cp);
967# endif
968 }
969 }
970#endif
971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 /* adjust chars > 127, except after "tTfFr" commands */
973 LANGMAP_ADJUST(*cp, !lang);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974#ifdef FEAT_RIGHTLEFT
975 /* adjust Hebrew mapped char */
976 if (p_hkmap && lang && KeyTyped)
977 *cp = hkmap(*cp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#endif
979 }
980
981 /*
982 * When the next character is CTRL-\ a following CTRL-N means the
983 * command is aborted and we go to Normal mode.
984 */
985 if (cp == &ca.extra_char
986 && ca.nchar == Ctrl_BSL
987 && (ca.extra_char == Ctrl_N || ca.extra_char == Ctrl_G))
988 {
989 ca.cmdchar = Ctrl_BSL;
990 ca.nchar = ca.extra_char;
991 idx = find_command(ca.cmdchar);
992 }
Bram Moolenaar12a753a2012-10-23 05:08:53 +0200993 else if ((ca.nchar == 'n' || ca.nchar == 'N') && ca.cmdchar == 'g')
Bram Moolenaarf00dc262012-10-21 03:54:33 +0200994 ca.oap->op_type = get_op_type(*cp, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 else if (*cp == Ctrl_BSL)
996 {
997 long towait = (p_ttm >= 0 ? p_ttm : p_tm);
998
999 /* There is a busy wait here when typing "f<C-\>" and then
1000 * something different from CTRL-N. Can't be avoided. */
1001 while ((c = vpeekc()) <= 0 && towait > 0L)
1002 {
1003 do_sleep(towait > 50L ? 50L : towait);
1004 towait -= 50L;
1005 }
1006 if (c > 0)
1007 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001008 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 if (c != Ctrl_N && c != Ctrl_G)
1010 vungetc(c);
1011 else
1012 {
1013 ca.cmdchar = Ctrl_BSL;
1014 ca.nchar = c;
1015 idx = find_command(ca.cmdchar);
1016 }
1017 }
1018 }
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 /* When getting a text character and the next character is a
1021 * multi-byte character, it could be a composing character.
Bram Moolenaar4f880622014-07-23 12:31:20 +02001022 * However, don't wait for it to arrive. Also, do enable mapping,
1023 * because if it's put back with vungetc() it's too late to apply
1024 * mapping. */
1025 --no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 while (enc_utf8 && lang && (c = vpeekc()) > 0
1027 && (c >= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1028 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001029 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (!utf_iscomposing(c))
1031 {
1032 vungetc(c); /* it wasn't, put it back */
1033 break;
1034 }
1035 else if (ca.ncharC1 == 0)
1036 ca.ncharC1 = c;
1037 else
1038 ca.ncharC2 = c;
1039 }
Bram Moolenaar4f880622014-07-23 12:31:20 +02001040 ++no_mapping;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
1042 --no_mapping;
1043 --allow_keys;
1044 }
1045
1046#ifdef FEAT_CMDL_INFO
1047 /*
1048 * Flush the showcmd characters onto the screen so we can see them while
1049 * the command is being executed. Only do this when the shown command was
1050 * actually displayed, otherwise this will slow down a lot when executing
1051 * mappings.
1052 */
1053 if (need_flushbuf)
1054 out_flush();
1055#endif
Bram Moolenaard9205ca2008-10-02 20:55:54 +00001056 if (ca.cmdchar != K_IGNORE)
1057 did_cursorhold = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
1059 State = NORMAL;
1060
1061 if (ca.nchar == ESC)
1062 {
1063 clearop(oap);
1064 if (restart_edit == 0 && goto_im())
1065 restart_edit = 'a';
1066 goto normal_end;
1067 }
1068
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001069 if (ca.cmdchar != K_IGNORE)
1070 {
1071 msg_didout = FALSE; /* don't scroll screen up for normal command */
1072 msg_col = 0;
1073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 old_pos = curwin->w_cursor; /* remember where cursor was */
1076
1077 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1078 * mode. */
1079 if (!VIsual_active && km_startsel)
1080 {
1081 if (nv_cmds[idx].cmd_flags & NV_SS)
1082 {
1083 start_selection();
1084 unshift_special(&ca);
1085 idx = find_command(ca.cmdchar);
1086 }
1087 else if ((nv_cmds[idx].cmd_flags & NV_SSS)
1088 && (mod_mask & MOD_MASK_SHIFT))
1089 {
1090 start_selection();
1091 mod_mask &= ~MOD_MASK_SHIFT;
1092 }
1093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
1095 /*
1096 * Execute the command!
1097 * Call the command function found in the commands table.
1098 */
1099 ca.arg = nv_cmds[idx].cmd_arg;
1100 (nv_cmds[idx].cmd_func)(&ca);
1101
1102 /*
1103 * If we didn't start or finish an operator, reset oap->regname, unless we
1104 * need it later.
1105 */
1106 if (!finish_op
1107 && !oap->op_type
1108 && (idx < 0 || !(nv_cmds[idx].cmd_flags & NV_KEEPREG)))
1109 {
1110 clearop(oap);
1111#ifdef FEAT_EVAL
Bram Moolenaar536681b2011-05-10 16:12:45 +02001112 {
1113 int regname = 0;
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001114
Bram Moolenaar536681b2011-05-10 16:12:45 +02001115 /* Adjust the register according to 'clipboard', so that when
1116 * "unnamed" is present it becomes '*' or '+' instead of '"'. */
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001117# ifdef FEAT_CLIPBOARD
Bram Moolenaar536681b2011-05-10 16:12:45 +02001118 adjust_clip_reg(&regname);
Bram Moolenaare2bdce32011-05-10 17:29:33 +02001119# endif
Bram Moolenaar536681b2011-05-10 16:12:45 +02001120 set_reg_var(regname);
1121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122#endif
1123 }
1124
Bram Moolenaarc6039d82005-12-02 00:44:04 +00001125 /* Get the length of mapped chars again after typing a count, second
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001126 * character or "z333<cr>". */
1127 if (old_mapped_len > 0)
1128 old_mapped_len = typebuf_maplen();
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 /*
1131 * If an operation is pending, handle it...
1132 */
1133 do_pending_operator(&ca, old_col, FALSE);
1134
1135 /*
1136 * Wait for a moment when a message is displayed that will be overwritten
1137 * by the mode message.
1138 * In Visual mode and with "^O" in Insert mode, a short message will be
1139 * overwritten by the mode message. Wait a bit, until a key is hit.
1140 * In Visual mode, it's more important to keep the Visual area updated
1141 * than keeping a message (e.g. from a /pat search).
1142 * Only do this if the command was typed, not from a mapping.
1143 * Don't wait when emsg_silent is non-zero.
1144 * Also wait a bit after an error message, e.g. for "^O:".
1145 * Don't redraw the screen, it would remove the message.
1146 */
1147 if ( ((p_smd
Bram Moolenaar09df3122006-01-23 22:23:09 +00001148 && msg_silent == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 && (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 || (VIsual_active
1151 && old_pos.lnum == curwin->w_cursor.lnum
1152 && old_pos.col == curwin->w_cursor.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 )
1154 && (clear_cmdline
1155 || redraw_cmdline)
1156 && (msg_didout || (msg_didany && msg_scroll))
1157 && !msg_nowait
1158 && KeyTyped)
1159 || (restart_edit != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 && (msg_scroll
1162 || emsg_on_display)))
1163 && oap->regname == 0
1164 && !(ca.retval & CA_COMMAND_BUSY)
1165 && stuff_empty()
1166 && typebuf_typed()
1167 && emsg_silent == 0
1168 && !did_wait_return
1169 && oap->op_type == OP_NOP)
1170 {
1171 int save_State = State;
1172
1173 /* Draw the cursor with the right shape here */
1174 if (restart_edit != 0)
1175 State = INSERT;
1176
1177 /* If need to redraw, and there is a "keep_msg", redraw before the
1178 * delay */
1179 if (must_redraw && keep_msg != NULL && !emsg_on_display)
1180 {
1181 char_u *kmsg;
1182
1183 kmsg = keep_msg;
1184 keep_msg = NULL;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001185 // showmode() will clear keep_msg, but we want to use it anyway
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 update_screen(0);
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001187 // now reset it, otherwise it's put in the history again
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 keep_msg = kmsg;
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001189
1190 kmsg = vim_strsave(keep_msg);
1191 if (kmsg != NULL)
1192 {
1193 msg_attr((char *)kmsg, keep_msg_attr);
1194 vim_free(kmsg);
1195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 }
1197 setcursor();
1198 cursor_on();
1199 out_flush();
1200 if (msg_scroll || emsg_on_display)
1201 ui_delay(1000L, TRUE); /* wait at least one second */
1202 ui_delay(3000L, FALSE); /* wait up to three seconds */
1203 State = save_State;
1204
1205 msg_scroll = FALSE;
1206 emsg_on_display = FALSE;
1207 }
1208
1209 /*
1210 * Finish up after executing a Normal mode command.
1211 */
1212normal_end:
1213
1214 msg_nowait = FALSE;
1215
1216 /* Reset finish_op, in case it was set */
1217#ifdef CURSOR_SHAPE
1218 c = finish_op;
1219#endif
1220 finish_op = FALSE;
1221#ifdef CURSOR_SHAPE
1222 /* Redraw the cursor with another shape, if we were in Operator-pending
1223 * mode or did a replace command. */
1224 if (c || ca.cmdchar == 'r')
1225 {
1226 ui_cursor_shape(); /* may show different cursor shape */
1227# ifdef FEAT_MOUSESHAPE
1228 update_mouseshape(-1);
1229# endif
1230 }
1231#endif
1232
1233#ifdef FEAT_CMDL_INFO
Bram Moolenaara983fe92008-07-31 20:04:27 +00001234 if (oap->op_type == OP_NOP && oap->regname == 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001235 && ca.cmdchar != K_CURSORHOLD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 clear_showcmd();
1237#endif
1238
1239 checkpcmark(); /* check if we moved since setting pcmark */
1240 vim_free(ca.searchbuf);
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (has_mbyte)
1243 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (curwin->w_p_scb && toplevel)
1246 {
1247 validate_cursor(); /* may need to update w_leftcol */
1248 do_check_scrollbind(TRUE);
1249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
Bram Moolenaar860cae12010-06-05 23:22:07 +02001251 if (curwin->w_p_crb && toplevel)
1252 {
1253 validate_cursor(); /* may need to update w_leftcol */
1254 do_check_cursorbind();
1255 }
Bram Moolenaar860cae12010-06-05 23:22:07 +02001256
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001257#ifdef FEAT_TERMINAL
Bram Moolenaareef9add2017-09-16 15:38:04 +02001258 /* don't go to Insert mode if a terminal has a running job */
1259 if (term_job_running(curbuf->b_term))
Bram Moolenaar6fe15bb2017-08-16 21:09:18 +02001260 restart_edit = 0;
1261#endif
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1265 * if still inside a mapping that started in Visual mode).
1266 * May switch from Visual to Select mode after CTRL-O command.
1267 */
1268 if ( oap->op_type == OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 && ((restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
1270 || restart_VIsual_select == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 && !(ca.retval & CA_COMMAND_BUSY)
1272 && stuff_empty()
1273 && oap->regname == 0)
1274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (restart_VIsual_select == 1)
1276 {
1277 VIsual_select = TRUE;
1278 showmode();
1279 restart_VIsual_select = 0;
1280 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001281 if (restart_edit != 0 && !VIsual_active && old_mapped_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 (void)edit(restart_edit, FALSE, 1L);
1283 }
1284
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if (restart_VIsual_select == 2)
1286 restart_VIsual_select = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 /* Save count before an operator for next time. */
1289 opcount = ca.opcount;
1290}
1291
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001292#ifdef FEAT_EVAL
1293/*
1294 * Set v:count and v:count1 according to "cap".
1295 * Set v:prevcount only when "set_prevcount" is TRUE.
1296 */
1297 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001298set_vcount_ca(cmdarg_T *cap, int *set_prevcount)
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01001299{
1300 long count = cap->count0;
1301
1302 /* multiply with cap->opcount the same way as above */
1303 if (cap->opcount != 0)
1304 count = cap->opcount * (count == 0 ? 1 : count);
1305 set_vcount(count, count == 0 ? 1 : count, *set_prevcount);
1306 *set_prevcount = FALSE; /* only set v:prevcount once */
1307}
1308#endif
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310/*
Bram Moolenaar5823f842019-01-03 22:58:08 +01001311 * Handle an operator after Visual mode or when the movement is finished.
1312 * "gui_yank" is true when yanking text for the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 */
1314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01001315do_pending_operator(cmdarg_T *cap, int old_col, int gui_yank)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 oparg_T *oap = cap->oap;
1318 pos_T old_cursor;
1319 int empty_region_error;
1320 int restart_edit_save;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001321#ifdef FEAT_LINEBREAK
1322 int lbr_saved = curwin->w_p_lbr;
Bram Moolenaar404406a2014-10-09 13:24:43 +02001323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 /* The visual area is remembered for redo */
1326 static int redo_VIsual_mode = NUL; /* 'v', 'V', or Ctrl-V */
1327 static linenr_T redo_VIsual_line_count; /* number of lines */
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001328 static colnr_T redo_VIsual_vcol; /* number of cols or end column */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 static long redo_VIsual_count; /* count for Visual operator */
Bram Moolenaard79e5502016-01-10 22:13:02 +01001330 static int redo_VIsual_arg; /* extra argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 int include_line_break = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333#if defined(FEAT_CLIPBOARD)
1334 /*
1335 * Yank the visual area into the GUI selection register before we operate
1336 * on it and lose it forever.
1337 * Don't do it if a specific register was specified, so that ""x"*P works.
1338 * This could call do_pending_operator() recursively, but that's OK
1339 * because gui_yank will be TRUE for the nested call.
1340 */
Bram Moolenaarc0885aa2012-07-10 16:49:23 +02001341 if ((clip_star.available || clip_plus.available)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 && oap->op_type != OP_NOP
1343 && !gui_yank
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 && VIsual_active
1345 && !redo_VIsual_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 && oap->regname == 0)
1347 clip_auto_select();
1348#endif
1349 old_cursor = curwin->w_cursor;
1350
1351 /*
1352 * If an operation is pending, handle it...
1353 */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001354 if ((finish_op || VIsual_active) && oap->op_type != OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
Bram Moolenaar5823f842019-01-03 22:58:08 +01001356 // Yank can be redone when 'y' is in 'cpoptions', but not when yanking
1357 // for the clipboard.
1358 int redo_yank = vim_strchr(p_cpo, CPO_YANK) != NULL && !gui_yank;
1359
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001360#ifdef FEAT_LINEBREAK
1361 /* Avoid a problem with unwanted linebreaks in block mode. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001362 if (curwin->w_p_lbr)
1363 curwin->w_valid &= ~VALID_VIRTCOL;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001364 curwin->w_p_lbr = FALSE;
1365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 oap->is_VIsual = VIsual_active;
1367 if (oap->motion_force == 'V')
1368 oap->motion_type = MLINE;
1369 else if (oap->motion_force == 'v')
1370 {
1371 /* If the motion was linewise, "inclusive" will not have been set.
1372 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1373 if (oap->motion_type == MLINE)
1374 oap->inclusive = FALSE;
1375 /* If the motion already was characterwise, toggle "inclusive" */
1376 else if (oap->motion_type == MCHAR)
1377 oap->inclusive = !oap->inclusive;
1378 oap->motion_type = MCHAR;
1379 }
1380 else if (oap->motion_force == Ctrl_V)
1381 {
1382 /* Change line- or characterwise motion into Visual block mode. */
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01001383 if (!VIsual_active)
1384 {
1385 VIsual_active = TRUE;
1386 VIsual = oap->start;
1387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 VIsual_mode = Ctrl_V;
1389 VIsual_select = FALSE;
1390 VIsual_reselect = FALSE;
1391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
Bram Moolenaar7afea822013-04-24 18:34:45 +02001393 /* Only redo yank when 'y' flag is in 'cpoptions'. */
1394 /* Never redo "zf" (define fold). */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001395 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar7afea822013-04-24 18:34:45 +02001396 && ((!VIsual_active || oap->motion_force)
1397 /* Also redo Operator-pending Visual mode mappings */
1398 || (VIsual_active && cap->cmdchar == ':'
1399 && oap->op_type != OP_COLON))
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001400 && cap->cmdchar != 'D'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#ifdef FEAT_FOLDING
1402 && oap->op_type != OP_FOLD
1403 && oap->op_type != OP_FOLDOPEN
1404 && oap->op_type != OP_FOLDOPENREC
1405 && oap->op_type != OP_FOLDCLOSE
1406 && oap->op_type != OP_FOLDCLOSEREC
1407 && oap->op_type != OP_FOLDDEL
1408 && oap->op_type != OP_FOLDDELREC
1409#endif
1410 )
1411 {
1412 prep_redo(oap->regname, cap->count0,
1413 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1414 oap->motion_force, cap->cmdchar, cap->nchar);
1415 if (cap->cmdchar == '/' || cap->cmdchar == '?') /* was a search */
1416 {
1417 /*
1418 * If 'cpoptions' does not contain 'r', insert the search
1419 * pattern to really repeat the same command.
1420 */
1421 if (vim_strchr(p_cpo, CPO_REDO) == NULL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001422 AppendToRedobuffLit(cap->searchbuf, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 AppendToRedobuff(NL_STR);
1424 }
1425 else if (cap->cmdchar == ':')
1426 {
1427 /* do_cmdline() has stored the first typed line in
1428 * "repeat_cmdline". When several lines are typed repeating
1429 * won't be possible. */
1430 if (repeat_cmdline == NULL)
1431 ResetRedobuff();
1432 else
1433 {
Bram Moolenaarebefac62005-12-28 22:39:57 +00001434 AppendToRedobuffLit(repeat_cmdline, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 AppendToRedobuff(NL_STR);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001436 VIM_CLEAR(repeat_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
1438 }
1439 }
1440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (redo_VIsual_busy)
1442 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001443 /* Redo of an operation on a Visual area. Use the same size from
1444 * redo_VIsual_line_count and redo_VIsual_vcol. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 oap->start = curwin->w_cursor;
1446 curwin->w_cursor.lnum += redo_VIsual_line_count - 1;
1447 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1448 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1449 VIsual_mode = redo_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001450 if (redo_VIsual_vcol == MAXCOL || VIsual_mode == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 {
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001452 if (VIsual_mode == 'v')
1453 {
1454 if (redo_VIsual_line_count <= 1)
1455 {
1456 validate_virtcol();
1457 curwin->w_curswant =
1458 curwin->w_virtcol + redo_VIsual_vcol - 1;
1459 }
1460 else
1461 curwin->w_curswant = redo_VIsual_vcol;
1462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001464 {
1465 curwin->w_curswant = MAXCOL;
1466 }
1467 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 }
1469 cap->count0 = redo_VIsual_count;
1470 if (redo_VIsual_count != 0)
1471 cap->count1 = redo_VIsual_count;
1472 else
1473 cap->count1 = 1;
1474 }
1475 else if (VIsual_active)
1476 {
Bram Moolenaar6179c612006-10-10 11:26:53 +00001477 if (!gui_yank)
1478 {
1479 /* Save the current VIsual area for '< and '> marks, and "gv" */
1480 curbuf->b_visual.vi_start = VIsual;
1481 curbuf->b_visual.vi_end = curwin->w_cursor;
1482 curbuf->b_visual.vi_mode = VIsual_mode;
Bram Moolenaara390bb62013-03-13 19:02:41 +01001483 if (VIsual_mode_orig != NUL)
1484 {
1485 curbuf->b_visual.vi_mode = VIsual_mode_orig;
1486 VIsual_mode_orig = NUL;
1487 }
Bram Moolenaar6179c612006-10-10 11:26:53 +00001488 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489# ifdef FEAT_EVAL
Bram Moolenaar6179c612006-10-10 11:26:53 +00001490 curbuf->b_visual_mode_eval = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491# endif
Bram Moolenaar6179c612006-10-10 11:26:53 +00001492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494 /* In Select mode, a linewise selection is operated upon like a
Bram Moolenaard009e862015-06-09 20:20:03 +02001495 * characterwise selection.
1496 * Special case: gH<Del> deletes the last line. */
1497 if (VIsual_select && VIsual_mode == 'V'
1498 && cap->oap->op_type != OP_DELETE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001500 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
1502 VIsual.col = 0;
1503 curwin->w_cursor.col =
1504 (colnr_T)STRLEN(ml_get(curwin->w_cursor.lnum));
1505 }
1506 else
1507 {
1508 curwin->w_cursor.col = 0;
1509 VIsual.col = (colnr_T)STRLEN(ml_get(VIsual.lnum));
1510 }
1511 VIsual_mode = 'v';
1512 }
1513 /* If 'selection' is "exclusive", backup one character for
1514 * charwise selections. */
1515 else if (VIsual_mode == 'v')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001516 include_line_break = unadjust_for_sel();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
1518 oap->start = VIsual;
1519 if (VIsual_mode == 'V')
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 oap->start.col = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001522 oap->start.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02001523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
1526 /*
1527 * Set oap->start to the first position of the operated text, oap->end
1528 * to the end of the operated text. w_cursor is equal to oap->start.
1529 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001530 if (LT_POS(oap->start, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532#ifdef FEAT_FOLDING
1533 /* Include folded lines completely. */
1534 if (!VIsual_active)
1535 {
1536 if (hasFolding(oap->start.lnum, &oap->start.lnum, NULL))
1537 oap->start.col = 0;
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02001538 if ((curwin->w_cursor.col > 0 || oap->inclusive)
1539 && hasFolding(curwin->w_cursor.lnum, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 &curwin->w_cursor.lnum))
1541 curwin->w_cursor.col = (colnr_T)STRLEN(ml_get_curline());
1542 }
1543#endif
1544 oap->end = curwin->w_cursor;
1545 curwin->w_cursor = oap->start;
1546
1547 /* w_virtcol may have been updated; if the cursor goes back to its
1548 * previous position w_virtcol becomes invalid and isn't updated
1549 * automatically. */
1550 curwin->w_valid &= ~VALID_VIRTCOL;
1551 }
1552 else
1553 {
1554#ifdef FEAT_FOLDING
1555 /* Include folded lines completely. */
1556 if (!VIsual_active && oap->motion_type == MLINE)
1557 {
1558 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum,
1559 NULL))
1560 curwin->w_cursor.col = 0;
1561 if (hasFolding(oap->start.lnum, NULL, &oap->start.lnum))
1562 oap->start.col = (colnr_T)STRLEN(ml_get(oap->start.lnum));
1563 }
1564#endif
1565 oap->end = oap->start;
1566 oap->start = curwin->w_cursor;
1567 }
1568
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001569 /* Just in case lines were deleted that make the position invalid. */
1570 check_pos(curwin->w_buffer, &oap->end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 oap->line_count = oap->end.lnum - oap->start.lnum + 1;
1572
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 /* Set "virtual_op" before resetting VIsual_active. */
1574 virtual_op = virtual_active();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (VIsual_active || redo_VIsual_busy)
1577 {
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001578 get_op_vcol(oap, redo_VIsual_vcol, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 if (!redo_VIsual_busy && !gui_yank)
1581 {
1582 /*
1583 * Prepare to reselect and redo Visual: this is based on the
1584 * size of the Visual text
1585 */
1586 resel_VIsual_mode = VIsual_mode;
1587 if (curwin->w_curswant == MAXCOL)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001588 resel_VIsual_vcol = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001590 {
1591 if (VIsual_mode != Ctrl_V)
1592 getvvcol(curwin, &(oap->end),
1593 NULL, NULL, &oap->end_vcol);
1594 if (VIsual_mode == Ctrl_V || oap->line_count <= 1)
1595 {
1596 if (VIsual_mode != Ctrl_V)
1597 getvvcol(curwin, &(oap->start),
1598 &oap->start_vcol, NULL, NULL);
1599 resel_VIsual_vcol = oap->end_vcol - oap->start_vcol + 1;
1600 }
1601 else
1602 resel_VIsual_vcol = oap->end_vcol;
1603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 resel_VIsual_line_count = oap->line_count;
1605 }
1606
1607 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
Bram Moolenaar5823f842019-01-03 22:58:08 +01001608 if ((redo_yank || oap->op_type != OP_YANK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 && oap->op_type != OP_COLON
1610#ifdef FEAT_FOLDING
1611 && oap->op_type != OP_FOLD
1612 && oap->op_type != OP_FOLDOPEN
1613 && oap->op_type != OP_FOLDOPENREC
1614 && oap->op_type != OP_FOLDCLOSE
1615 && oap->op_type != OP_FOLDCLOSEREC
1616 && oap->op_type != OP_FOLDDEL
1617 && oap->op_type != OP_FOLDDELREC
1618#endif
1619 && oap->motion_force == NUL
1620 )
1621 {
1622 /* Prepare for redoing. Only use the nchar field for "r",
1623 * otherwise it might be the second char of the operator. */
Bram Moolenaar641e2862012-07-25 15:06:34 +02001624 if (cap->cmdchar == 'g' && (cap->nchar == 'n'
1625 || cap->nchar == 'N'))
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01001626 prep_redo(oap->regname, cap->count0,
1627 get_op_char(oap->op_type), get_extra_op_char(oap->op_type),
1628 oap->motion_force, cap->cmdchar, cap->nchar);
Bram Moolenaar7afea822013-04-24 18:34:45 +02001629 else if (cap->cmdchar != ':')
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001630 {
1631 int nchar = oap->op_type == OP_REPLACE ? cap->nchar : NUL;
1632
1633 /* reverse what nv_replace() did */
1634 if (nchar == REPLACE_CR_NCHAR)
1635 nchar = CAR;
1636 else if (nchar == REPLACE_NL_NCHAR)
1637 nchar = NL;
Bram Moolenaar641e2862012-07-25 15:06:34 +02001638 prep_redo(oap->regname, 0L, NUL, 'v',
1639 get_op_char(oap->op_type),
1640 get_extra_op_char(oap->op_type),
Bram Moolenaarf12519d2018-02-06 22:52:49 +01001641 nchar);
1642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (!redo_VIsual_busy)
1644 {
1645 redo_VIsual_mode = resel_VIsual_mode;
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02001646 redo_VIsual_vcol = resel_VIsual_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 redo_VIsual_line_count = resel_VIsual_line_count;
1648 redo_VIsual_count = cap->count0;
Bram Moolenaard79e5502016-01-10 22:13:02 +01001649 redo_VIsual_arg = cap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
1651 }
1652
1653 /*
1654 * oap->inclusive defaults to TRUE.
1655 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1656 * FALSE. This makes "d}P" and "v}dP" work the same.
1657 */
1658 if (oap->motion_force == NUL || oap->motion_type == MLINE)
1659 oap->inclusive = TRUE;
1660 if (VIsual_mode == 'V')
1661 oap->motion_type = MLINE;
1662 else
1663 {
1664 oap->motion_type = MCHAR;
1665 if (VIsual_mode != Ctrl_V && *ml_get_pos(&(oap->end)) == NUL
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001666 && (include_line_break || !virtual_op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
1668 oap->inclusive = FALSE;
1669 /* Try to include the newline, unless it's an operator
Bram Moolenaar44286ca2011-07-15 17:51:34 +02001670 * that works on lines only. */
Bram Moolenaard009e862015-06-09 20:20:03 +02001671 if (*p_sel != 'o'
1672 && !op_on_lines(oap->op_type)
1673 && oap->end.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 {
Bram Moolenaard009e862015-06-09 20:20:03 +02001675 ++oap->end.lnum;
1676 oap->end.col = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001677 oap->end.coladd = 0;
Bram Moolenaard009e862015-06-09 20:20:03 +02001678 ++oap->line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 }
1680 }
1681 }
1682
1683 redo_VIsual_busy = FALSE;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001684
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 /*
1686 * Switch Visual off now, so screen updating does
1687 * not show inverted text when the screen is redrawn.
1688 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1689 * no screen redraw, so it is done here to remove the inverted
1690 * part.
1691 */
1692 if (!gui_yank)
1693 {
1694 VIsual_active = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001695#ifdef FEAT_MOUSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 setmouse();
1697 mouse_dragging = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001698#endif
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02001699 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if ((oap->op_type == OP_YANK
1701 || oap->op_type == OP_COLON
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001702 || oap->op_type == OP_FUNCTION
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 || oap->op_type == OP_FILTER)
1704 && oap->motion_force == NUL)
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001705 {
1706#ifdef FEAT_LINEBREAK
1707 /* make sure redrawing is correct */
1708 curwin->w_p_lbr = lbr_saved;
1709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
1713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 /* Include the trailing byte of a multi-byte char. */
1716 if (has_mbyte && oap->inclusive)
1717 {
1718 int l;
1719
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001720 l = (*mb_ptr2len)(ml_get_pos(&oap->end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (l > 1)
1722 oap->end.col += l - 1;
1723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 curwin->w_set_curswant = TRUE;
1725
1726 /*
1727 * oap->empty is set when start and end are the same. The inclusive
1728 * flag affects this too, unless yanking and the end is on a NUL.
1729 */
1730 oap->empty = (oap->motion_type == MCHAR
1731 && (!oap->inclusive
1732 || (oap->op_type == OP_YANK
1733 && gchar_pos(&oap->end) == NUL))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001734 && EQUAL_POS(oap->start, oap->end)
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01001735 && !(virtual_op && oap->start.coladd != oap->end.coladd));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 /*
1737 * For delete, change and yank, it's an error to operate on an
1738 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1739 */
1740 empty_region_error = (oap->empty
1741 && vim_strchr(p_cpo, CPO_EMPTYREGION) != NULL);
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 /* Force a redraw when operating on an empty Visual region, when
1744 * 'modifiable is off or creating a fold. */
1745 if (oap->is_VIsual && (oap->empty || !curbuf->b_p_ma
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001746#ifdef FEAT_FOLDING
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 || oap->op_type == OP_FOLD
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001750 {
1751#ifdef FEAT_LINEBREAK
1752 curwin->w_p_lbr = lbr_saved;
1753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 redraw_curbuf_later(INVERTED);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756
1757 /*
1758 * If the end of an operator is in column one while oap->motion_type
1759 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1760 * character in the previous line. If op_start is on or before the
1761 * first non-blank in the line, the operator becomes linewise
1762 * (strange, but that's the way vi does it).
1763 */
1764 if ( oap->motion_type == MCHAR
1765 && oap->inclusive == FALSE
1766 && !(cap->retval & CA_NO_ADJ_OP_END)
1767 && oap->end.col == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 && (!oap->is_VIsual || *p_sel == 'o')
Bram Moolenaar34114692005-01-02 11:28:13 +00001769 && !oap->block_mode
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 && oap->line_count > 1)
1771 {
1772 oap->end_adjusted = TRUE; /* remember that we did this */
1773 --oap->line_count;
1774 --oap->end.lnum;
1775 if (inindent(0))
1776 oap->motion_type = MLINE;
1777 else
1778 {
1779 oap->end.col = (colnr_T)STRLEN(ml_get(oap->end.lnum));
1780 if (oap->end.col)
1781 {
1782 --oap->end.col;
1783 oap->inclusive = TRUE;
1784 }
1785 }
1786 }
1787 else
1788 oap->end_adjusted = FALSE;
1789
1790 switch (oap->op_type)
1791 {
1792 case OP_LSHIFT:
1793 case OP_RSHIFT:
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001794 op_shift(oap, TRUE, oap->is_VIsual ? (int)cap->count1 : 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 auto_format(FALSE, TRUE);
1796 break;
1797
1798 case OP_JOIN_NS:
1799 case OP_JOIN:
1800 if (oap->line_count < 2)
1801 oap->line_count = 2;
1802 if (curwin->w_cursor.lnum + oap->line_count - 1 >
1803 curbuf->b_ml.ml_line_count)
1804 beep_flush();
1805 else
1806 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001807 (void)do_join(oap->line_count, oap->op_type == OP_JOIN,
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02001808 TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 auto_format(FALSE, TRUE);
1810 }
1811 break;
1812
1813 case OP_DELETE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001816 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001817 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001818 CancelRedo();
1819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 else
1821 {
1822 (void)op_delete(oap);
1823 if (oap->motion_type == MLINE && has_format_option(FO_AUTO))
1824 u_save_cursor(); /* cursor line wasn't saved yet */
1825 auto_format(FALSE, TRUE);
1826 }
1827 break;
1828
1829 case OP_YANK:
1830 if (empty_region_error)
1831 {
1832 if (!gui_yank)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001833 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001834 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001835 CancelRedo();
1836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001839 {
1840#ifdef FEAT_LINEBREAK
1841 curwin->w_p_lbr = lbr_saved;
1842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 (void)op_yank(oap, FALSE, !gui_yank);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 check_cursor_col();
1846 break;
1847
1848 case OP_CHANGE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001851 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001852 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001853 CancelRedo();
1854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 else
1856 {
1857 /* This is a new edit command, not a restart. Need to
1858 * remember it to make 'insertmode' work with mappings for
1859 * Visual mode. But do this only once and not when typed and
1860 * 'insertmode' isn't set. */
1861 if (p_im || !KeyTyped)
1862 restart_edit_save = restart_edit;
1863 else
1864 restart_edit_save = 0;
1865 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001866#ifdef FEAT_LINEBREAK
1867 /* Restore linebreak, so that when the user edits it looks as
1868 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001869 if (curwin->w_p_lbr != lbr_saved)
1870 {
1871 curwin->w_p_lbr = lbr_saved;
1872 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1873 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 /* Reset finish_op now, don't want it set inside edit(). */
1876 finish_op = FALSE;
1877 if (op_change(oap)) /* will call edit() */
1878 cap->retval |= CA_COMMAND_BUSY;
1879 if (restart_edit == 0)
1880 restart_edit = restart_edit_save;
1881 }
1882 break;
1883
1884 case OP_FILTER:
1885 if (vim_strchr(p_cpo, CPO_FILTER) != NULL)
1886 AppendToRedobuff((char_u *)"!\r"); /* use any last used !cmd */
1887 else
1888 bangredo = TRUE; /* do_bang() will put cmd in redo buffer */
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001889 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891 case OP_INDENT:
1892 case OP_COLON:
1893
1894#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1895 /*
1896 * If 'equalprg' is empty, do the indenting internally.
1897 */
1898 if (oap->op_type == OP_INDENT && *get_equalprg() == NUL)
1899 {
1900# ifdef FEAT_LISP
1901 if (curbuf->b_p_lisp)
1902 {
1903 op_reindent(oap, get_lisp_indent);
1904 break;
1905 }
1906# endif
1907# ifdef FEAT_CINDENT
1908 op_reindent(oap,
1909# ifdef FEAT_EVAL
1910 *curbuf->b_p_inde != NUL ? get_expr_indent :
1911# endif
1912 get_c_indent);
1913 break;
1914# endif
1915 }
1916#endif
1917
1918 op_colon(oap);
1919 break;
1920
1921 case OP_TILDE:
1922 case OP_UPPER:
1923 case OP_LOWER:
1924 case OP_ROT13:
1925 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001926 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001927 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001928 CancelRedo();
1929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 else
1931 op_tilde(oap);
1932 check_cursor_col();
1933 break;
1934
1935 case OP_FORMAT:
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001936#if defined(FEAT_EVAL)
1937 if (*curbuf->b_p_fex != NUL)
1938 op_formatexpr(oap); /* use expression */
1939 else
1940#endif
Bram Moolenaar9be7c042017-01-14 14:28:30 +01001941 if (*p_fp != NUL || *curbuf->b_p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 op_colon(oap); /* use external command */
1943 else
1944 op_format(oap, FALSE); /* use internal function */
1945 break;
1946
1947 case OP_FORMAT2:
1948 op_format(oap, TRUE); /* use internal function */
1949 break;
1950
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001951 case OP_FUNCTION:
Bram Moolenaar4a08b0d2016-11-05 21:55:13 +01001952#ifdef FEAT_LINEBREAK
1953 /* Restore linebreak, so that when the user edits it looks as
1954 * before. */
1955 curwin->w_p_lbr = lbr_saved;
1956#endif
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00001957 op_function(oap); /* call 'operatorfunc' */
1958 break;
1959
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 case OP_INSERT:
1961 case OP_APPEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001964 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02001965 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01001966 CancelRedo();
1967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 else
1969 {
1970 /* This is a new edit command, not a restart. Need to
1971 * remember it to make 'insertmode' work with mappings for
1972 * Visual mode. But do this only once. */
1973 restart_edit_save = restart_edit;
1974 restart_edit = 0;
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001975#ifdef FEAT_LINEBREAK
1976 /* Restore linebreak, so that when the user edits it looks as
1977 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02001978 if (curwin->w_p_lbr != lbr_saved)
1979 {
1980 curwin->w_p_lbr = lbr_saved;
1981 get_op_vcol(oap, redo_VIsual_mode, FALSE);
1982 }
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 op_insert(oap, cap->count1);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01001985#ifdef FEAT_LINEBREAK
1986 /* Reset linebreak, so that formatting works correctly. */
1987 curwin->w_p_lbr = FALSE;
1988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990 /* TODO: when inserting in several lines, should format all
1991 * the lines. */
1992 auto_format(FALSE, TRUE);
1993
1994 if (restart_edit == 0)
1995 restart_edit = restart_edit_save;
Bram Moolenaar0b5c93a2017-02-01 15:03:30 +01001996 else
1997 cap->retval |= CA_COMMAND_BUSY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 break;
2000
2001 case OP_REPLACE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 VIsual_reselect = FALSE; /* don't reselect now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (empty_region_error)
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002004 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002005 vim_beep(BO_OPER);
Bram Moolenaarbe094a12012-02-05 01:18:48 +01002006 CancelRedo();
2007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 else
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002009 {
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002010#ifdef FEAT_LINEBREAK
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002011 /* Restore linebreak, so that when the user edits it looks as
2012 * before. */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02002013 if (curwin->w_p_lbr != lbr_saved)
2014 {
2015 curwin->w_p_lbr = lbr_saved;
2016 get_op_vcol(oap, redo_VIsual_mode, FALSE);
2017 }
Bram Moolenaar870ba5f2019-01-11 14:37:20 +01002018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 op_replace(oap, cap->nchar);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 break;
2022
2023#ifdef FEAT_FOLDING
2024 case OP_FOLD:
2025 VIsual_reselect = FALSE; /* don't reselect now */
2026 foldCreate(oap->start.lnum, oap->end.lnum);
2027 break;
2028
2029 case OP_FOLDOPEN:
2030 case OP_FOLDOPENREC:
2031 case OP_FOLDCLOSE:
2032 case OP_FOLDCLOSEREC:
2033 VIsual_reselect = FALSE; /* don't reselect now */
2034 opFoldRange(oap->start.lnum, oap->end.lnum,
2035 oap->op_type == OP_FOLDOPEN
2036 || oap->op_type == OP_FOLDOPENREC,
2037 oap->op_type == OP_FOLDOPENREC
2038 || oap->op_type == OP_FOLDCLOSEREC,
2039 oap->is_VIsual);
2040 break;
2041
2042 case OP_FOLDDEL:
2043 case OP_FOLDDELREC:
2044 VIsual_reselect = FALSE; /* don't reselect now */
2045 deleteFold(oap->start.lnum, oap->end.lnum,
2046 oap->op_type == OP_FOLDDELREC, oap->is_VIsual);
2047 break;
2048#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01002049 case OP_NR_ADD:
2050 case OP_NR_SUB:
2051 if (empty_region_error)
2052 {
2053 vim_beep(BO_OPER);
2054 CancelRedo();
2055 }
2056 else
2057 {
2058 VIsual_active = TRUE;
2059#ifdef FEAT_LINEBREAK
2060 curwin->w_p_lbr = lbr_saved;
2061#endif
2062 op_addsub(oap, cap->count1, redo_VIsual_arg);
2063 VIsual_active = FALSE;
2064 }
2065 check_cursor_col();
2066 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 default:
2068 clearopbeep(oap);
2069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 virtual_op = MAYBE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 if (!gui_yank)
2072 {
2073 /*
2074 * if 'sol' not set, go back to old column for some commands
2075 */
2076 if (!p_sol && oap->motion_type == MLINE && !oap->end_adjusted
2077 && (oap->op_type == OP_LSHIFT || oap->op_type == OP_RSHIFT
2078 || oap->op_type == OP_DELETE))
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002079 {
2080#ifdef FEAT_LINEBREAK
2081 curwin->w_p_lbr = FALSE;
2082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 coladvance(curwin->w_curswant = old_col);
Bram Moolenaarba3f58e2015-01-14 17:52:30 +01002084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 }
2086 else
2087 {
2088 curwin->w_cursor = old_cursor;
2089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 oap->block_mode = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 clearop(oap);
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01002092 motion_force = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
Bram Moolenaar404406a2014-10-09 13:24:43 +02002094#ifdef FEAT_LINEBREAK
2095 curwin->w_p_lbr = lbr_saved;
2096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097}
2098
2099/*
2100 * Handle indent and format operators and visual mode ":".
2101 */
2102 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002103op_colon(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
2105 stuffcharReadbuff(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if (oap->is_VIsual)
2107 stuffReadbuff((char_u *)"'<,'>");
2108 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 /*
2111 * Make the range look nice, so it can be repeated.
2112 */
2113 if (oap->start.lnum == curwin->w_cursor.lnum)
2114 stuffcharReadbuff('.');
2115 else
2116 stuffnumReadbuff((long)oap->start.lnum);
2117 if (oap->end.lnum != oap->start.lnum)
2118 {
2119 stuffcharReadbuff(',');
2120 if (oap->end.lnum == curwin->w_cursor.lnum)
2121 stuffcharReadbuff('.');
2122 else if (oap->end.lnum == curbuf->b_ml.ml_line_count)
2123 stuffcharReadbuff('$');
2124 else if (oap->start.lnum == curwin->w_cursor.lnum)
2125 {
2126 stuffReadbuff((char_u *)".+");
2127 stuffnumReadbuff((long)oap->line_count - 1);
2128 }
2129 else
2130 stuffnumReadbuff((long)oap->end.lnum);
2131 }
2132 }
2133 if (oap->op_type != OP_COLON)
2134 stuffReadbuff((char_u *)"!");
2135 if (oap->op_type == OP_INDENT)
2136 {
2137#ifndef FEAT_CINDENT
2138 if (*get_equalprg() == NUL)
2139 stuffReadbuff((char_u *)"indent");
2140 else
2141#endif
2142 stuffReadbuff(get_equalprg());
2143 stuffReadbuff((char_u *)"\n");
2144 }
2145 else if (oap->op_type == OP_FORMAT)
2146 {
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002147 if (*curbuf->b_p_fp != NUL)
2148 stuffReadbuff(curbuf->b_p_fp);
2149 else if (*p_fp != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 stuffReadbuff(p_fp);
Bram Moolenaar9be7c042017-01-14 14:28:30 +01002151 else
2152 stuffReadbuff((char_u *)"fmt");
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00002153 stuffReadbuff((char_u *)"\n']");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 }
2155
2156 /*
2157 * do_cmdline() does the rest
2158 */
2159}
2160
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002161/*
Bram Moolenaar12033fb2005-12-16 21:49:31 +00002162 * Handle the "g@" operator: call 'operatorfunc'.
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002163 */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002164 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002165op_function(oparg_T *oap UNUSED)
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002166{
2167#ifdef FEAT_EVAL
Bram Moolenaarffa96842018-06-12 22:05:14 +02002168 typval_T argv[2];
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002169 int save_virtual_op = virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002170
2171 if (*p_opfunc == NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002172 emsg(_("E774: 'operatorfunc' is empty"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002173 else
2174 {
2175 /* Set '[ and '] marks to text to be operated on. */
2176 curbuf->b_op_start = oap->start;
2177 curbuf->b_op_end = oap->end;
2178 if (oap->motion_type != MLINE && !oap->inclusive)
2179 /* Exclude the end position. */
2180 decl(&curbuf->b_op_end);
2181
Bram Moolenaarffa96842018-06-12 22:05:14 +02002182 argv[0].v_type = VAR_STRING;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002183 if (oap->block_mode)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002184 argv[0].vval.v_string = (char_u *)"block";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002185 else if (oap->motion_type == MLINE)
Bram Moolenaarffa96842018-06-12 22:05:14 +02002186 argv[0].vval.v_string = (char_u *)"line";
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002187 else
Bram Moolenaarffa96842018-06-12 22:05:14 +02002188 argv[0].vval.v_string = (char_u *)"char";
2189 argv[1].v_type = VAR_UNKNOWN;
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002190
2191 /* Reset virtual_op so that 'virtualedit' can be changed in the
2192 * function. */
2193 virtual_op = MAYBE;
2194
Bram Moolenaarded27a12018-08-01 19:06:03 +02002195 (void)call_func_retnr(p_opfunc, 1, argv);
Bram Moolenaar61d281a2012-03-28 12:59:57 +02002196
2197 virtual_op = save_virtual_op;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002198 }
2199#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002200 emsg(_("E775: Eval feature not available"));
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00002201#endif
2202}
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204#if defined(FEAT_MOUSE) || defined(PROTO)
2205/*
2206 * Do the appropriate action for the current mouse click in the current mode.
2207 * Not used for Command-line mode.
2208 *
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002209 * Normal and Visual Mode:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 * event modi- position visual change action
2211 * fier cursor window
2212 * left press - yes end yes
2213 * left press C yes end yes "^]" (2)
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002214 * left press S yes end (popup: extend) yes "*" (2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 * left drag - yes start if moved no
2216 * left relse - yes start if moved no
2217 * middle press - yes if not active no put register
2218 * middle press - yes if active no yank and put
2219 * right press - yes start or extend yes
2220 * right press S yes no change yes "#" (2)
2221 * right drag - yes extend no
2222 * right relse - yes extend no
2223 *
2224 * Insert or Replace Mode:
2225 * event modi- position visual change action
2226 * fier cursor window
2227 * left press - yes (cannot be active) yes
2228 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2229 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2230 * left drag - yes start or extend (1) no CTRL-O (1)
2231 * left relse - yes start or extend (1) no CTRL-O (1)
2232 * middle press - no (cannot be active) no put register
2233 * right press - yes start or extend yes CTRL-O
2234 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2235 *
2236 * (1) only if mouse pointer moved since press
2237 * (2) only if click is in same buffer
2238 *
2239 * Return TRUE if start_arrow() should be called for edit mode.
2240 */
2241 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002242do_mouse(
2243 oparg_T *oap, /* operator argument, can be NULL */
2244 int c, /* K_LEFTMOUSE, etc */
2245 int dir, /* Direction to 'put' if necessary */
2246 long count,
2247 int fixindent) /* PUT_FIXINDENT if fixing indent necessary */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248{
2249 static int do_always = FALSE; /* ignore 'mouse' setting next time */
2250 static int got_click = FALSE; /* got a click some time back */
2251
2252 int which_button; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2253 int is_click; /* If FALSE it's a drag or release event */
2254 int is_drag; /* If TRUE it's a drag event */
2255 int jump_flags = 0; /* flags for jump_to_mouse() */
2256 pos_T start_visual;
2257 int moved; /* Has cursor moved? */
2258 int in_status_line; /* mouse in status line */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002259 static int in_tab_line = FALSE; /* mouse clicked in tab line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 int in_sep_line; /* mouse in vertical separator line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 int c1, c2;
2262#if defined(FEAT_FOLDING)
2263 pos_T save_cursor;
2264#endif
2265 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 static pos_T orig_cursor;
2267 colnr_T leftcol, rightcol;
2268 pos_T end_visual;
2269 int diff;
2270 int old_active = VIsual_active;
2271 int old_mode = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 int regname;
2273
2274#if defined(FEAT_FOLDING)
2275 save_cursor = curwin->w_cursor;
2276#endif
2277
2278 /*
2279 * When GUI is active, always recognize mouse events, otherwise:
2280 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2281 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2282 * - For command line and insert mode 'mouse' is checked before calling
2283 * do_mouse().
2284 */
2285 if (do_always)
2286 do_always = FALSE;
2287 else
2288#ifdef FEAT_GUI
2289 if (!gui.in_use)
2290#endif
2291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (VIsual_active)
2293 {
2294 if (!mouse_has(MOUSE_VISUAL))
2295 return FALSE;
2296 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002297 else if (State == NORMAL && !mouse_has(MOUSE_NORMAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 return FALSE;
2299 }
2300
Bram Moolenaar2526ef22013-03-16 14:20:51 +01002301 for (;;)
2302 {
2303 which_button = get_mouse_button(KEY2TERMCAP1(c), &is_click, &is_drag);
2304 if (is_drag)
2305 {
2306 /* If the next character is the same mouse event then use that
2307 * one. Speeds up dragging the status line. */
2308 if (vpeekc() != NUL)
2309 {
2310 int nc;
2311 int save_mouse_row = mouse_row;
2312 int save_mouse_col = mouse_col;
2313
2314 /* Need to get the character, peeking doesn't get the actual
2315 * one. */
2316 nc = safe_vgetc();
2317 if (c == nc)
2318 continue;
2319 vungetc(nc);
2320 mouse_row = save_mouse_row;
2321 mouse_col = save_mouse_col;
2322 }
2323 }
2324 break;
2325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002327 if (c == K_MOUSEMOVE)
2328 {
Bram Moolenaar06bd8242019-05-08 22:55:16 +02002329 // Mouse moved without a button pressed.
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01002330#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002331 ui_may_remove_balloon();
Bram Moolenaar06bd8242019-05-08 22:55:16 +02002332 if (p_bevalterm)
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002333 {
2334 profile_setlimit(p_bdlay, &bevalexpr_due);
2335 bevalexpr_due_set = TRUE;
2336 }
2337#endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002338#ifdef FEAT_TEXT_PROP
2339 popup_handle_mouse_moved();
2340#endif
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002341 return FALSE;
2342 }
2343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344#ifdef FEAT_MOUSESHAPE
2345 /* May have stopped dragging the status or separator line. The pointer is
2346 * most likely still on the status or separator line. */
2347 if (!is_drag && drag_status_line)
2348 {
2349 drag_status_line = FALSE;
2350 update_mouseshape(SHAPE_IDX_STATUS);
2351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (!is_drag && drag_sep_line)
2353 {
2354 drag_sep_line = FALSE;
2355 update_mouseshape(SHAPE_IDX_VSEP);
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357#endif
2358
2359 /*
2360 * Ignore drag and release events if we didn't get a click.
2361 */
2362 if (is_click)
2363 got_click = TRUE;
2364 else
2365 {
2366 if (!got_click) /* didn't get click, ignore */
2367 return FALSE;
2368 if (!is_drag) /* release, reset got_click */
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002369 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 got_click = FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002371 if (in_tab_line)
2372 {
2373 in_tab_line = FALSE;
2374 return FALSE;
2375 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
2378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 /*
2380 * CTRL right mouse button does CTRL-T
2381 */
2382 if (is_click && (mod_mask & MOD_MASK_CTRL) && which_button == MOUSE_RIGHT)
2383 {
2384 if (State & INSERT)
2385 stuffcharReadbuff(Ctrl_O);
2386 if (count > 1)
2387 stuffnumReadbuff(count);
2388 stuffcharReadbuff(Ctrl_T);
2389 got_click = FALSE; /* ignore drag&release now */
2390 return FALSE;
2391 }
2392
2393 /*
2394 * CTRL only works with left mouse button
2395 */
2396 if ((mod_mask & MOD_MASK_CTRL) && which_button != MOUSE_LEFT)
2397 return FALSE;
2398
2399 /*
2400 * When a modifier is down, ignore drag and release events, as well as
2401 * multiple clicks and the middle mouse button.
2402 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2403 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002404 if ((mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT
2405 | MOD_MASK_META))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 && (!is_click
2407 || (mod_mask & MOD_MASK_MULTI_CLICK)
2408 || which_button == MOUSE_MIDDLE)
Bram Moolenaar64969662005-12-14 21:59:55 +00002409 && !((mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 && mouse_model_popup()
2411 && which_button == MOUSE_LEFT)
Bram Moolenaar64969662005-12-14 21:59:55 +00002412 && !((mod_mask & MOD_MASK_ALT)
2413 && !mouse_model_popup()
2414 && which_button == MOUSE_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 )
2416 return FALSE;
2417
2418 /*
2419 * If the button press was used as the movement command for an operator
2420 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2421 * drag/release events.
2422 */
2423 if (!is_click && which_button == MOUSE_MIDDLE)
2424 return FALSE;
2425
2426 if (oap != NULL)
2427 regname = oap->regname;
2428 else
2429 regname = 0;
2430
2431 /*
2432 * Middle mouse button does a 'put' of the selected text
2433 */
2434 if (which_button == MOUSE_MIDDLE)
2435 {
2436 if (State == NORMAL)
2437 {
2438 /*
2439 * If an operator was pending, we don't know what the user wanted
2440 * to do. Go back to normal mode: Clear the operator and beep().
2441 */
2442 if (oap != NULL && oap->op_type != OP_NOP)
2443 {
2444 clearopbeep(oap);
2445 return FALSE;
2446 }
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 /*
2449 * If visual was active, yank the highlighted text and put it
2450 * before the mouse pointer position.
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002451 * In Select mode replace the highlighted text with the clipboard.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 */
2453 if (VIsual_active)
2454 {
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002455 if (VIsual_select)
2456 {
2457 stuffcharReadbuff(Ctrl_G);
Bram Moolenaar2d8b2d82006-10-17 20:38:28 +00002458 stuffReadbuff((char_u *)"\"+p");
Bram Moolenaara350f4a2006-10-17 14:54:03 +00002459 }
2460 else
2461 {
2462 stuffcharReadbuff('y');
2463 stuffcharReadbuff(K_MIDDLEMOUSE);
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 do_always = TRUE; /* ignore 'mouse' setting next time */
2466 return FALSE;
2467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 /*
2469 * The rest is below jump_to_mouse()
2470 */
2471 }
2472
2473 else if ((State & INSERT) == 0)
2474 return FALSE;
2475
2476 /*
2477 * Middle click in insert mode doesn't move the mouse, just insert the
2478 * contents of a register. '.' register is special, can't insert that
2479 * with do_put().
2480 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2481 * happens for the GUI).
2482 */
2483 if ((State & INSERT) || !mouse_has(MOUSE_NORMAL))
2484 {
2485 if (regname == '.')
2486 insert_reg(regname, TRUE);
2487 else
2488 {
2489#ifdef FEAT_CLIPBOARD
2490 if (clip_star.available && regname == 0)
2491 regname = '*';
2492#endif
2493 if ((State & REPLACE_FLAG) && !yank_register_mline(regname))
2494 insert_reg(regname, TRUE);
2495 else
2496 {
2497 do_put(regname, BACKWARD, 1L, fixindent | PUT_CURSEND);
2498
2499 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2500 AppendCharToRedobuff(Ctrl_R);
2501 AppendCharToRedobuff(fixindent ? Ctrl_P : Ctrl_O);
2502 AppendCharToRedobuff(regname == 0 ? '"' : regname);
2503 }
2504 }
2505 return FALSE;
2506 }
2507 }
2508
2509 /* When dragging or button-up stay in the same window. */
2510 if (!is_click)
2511 jump_flags |= MOUSE_FOCUS | MOUSE_DID_MOVE;
2512
2513 start_visual.lnum = 0;
2514
Bram Moolenaarf740b292006-02-16 22:11:02 +00002515 /* Check for clicking in the tab page line. */
2516 if (mouse_row == 0 && firstwin->w_winrow > 0)
2517 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00002518 if (is_drag)
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002519 {
2520 if (in_tab_line)
2521 {
2522 c1 = TabPageIdxs[mouse_col];
Bram Moolenaar4a4b8212015-09-08 17:50:41 +02002523 tabpage_move(c1 <= 0 ? 9999 : c1 < tabpage_index(curtab)
2524 ? c1 - 1 : c1);
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002525 }
Bram Moolenaara7241f52008-06-24 20:39:31 +00002526 return FALSE;
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002527 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002528
Bram Moolenaarf740b292006-02-16 22:11:02 +00002529 /* click in a tab selects that tab page */
2530 if (is_click
2531# ifdef FEAT_CMDWIN
2532 && cmdwin_type == 0
2533# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 && mouse_col < Columns)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002535 {
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002536 in_tab_line = TRUE;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002537 c1 = TabPageIdxs[mouse_col];
2538 if (c1 >= 0)
2539 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002540 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2541 {
2542 /* double click opens new page */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002543 end_visual_mode();
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002544 tabpage_new();
2545 tabpage_move(c1 == 0 ? 9999 : c1 - 1);
2546 }
2547 else
2548 {
2549 /* Go to specified tab page, or next one if not clicking
2550 * on a label. */
2551 goto_tabpage(c1);
2552
2553 /* It's like clicking on the status line of a window. */
2554 if (curwin != old_curwin)
2555 end_visual_mode();
2556 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002557 }
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02002558 else
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002559 {
2560 tabpage_T *tp;
2561
2562 /* Close the current or specified tab page. */
2563 if (c1 == -999)
2564 tp = curtab;
2565 else
2566 tp = find_tabpage(-c1);
2567 if (tp == curtab)
2568 {
2569 if (first_tabpage->tp_next != NULL)
2570 tabpage_close(FALSE);
2571 }
2572 else if (tp != NULL)
2573 tabpage_close_other(tp, FALSE);
2574 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002575 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002576 return TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002577 }
Bram Moolenaar58f0a1f2010-07-17 16:30:42 +02002578 else if (is_drag && in_tab_line)
2579 {
2580 c1 = TabPageIdxs[mouse_col];
2581 tabpage_move(c1 <= 0 ? 9999 : c1 - 1);
2582 return FALSE;
2583 }
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 /*
2586 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2587 * right button up -> pop-up menu
2588 * shift-left button -> right button
Bram Moolenaar64969662005-12-14 21:59:55 +00002589 * alt-left button -> alt-right button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 */
2591 if (mouse_model_popup())
2592 {
2593 if (which_button == MOUSE_RIGHT
2594 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2595 {
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00002596#if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002598 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2599 || defined(FEAT_TERM_POPUP_MENU)
2600# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 if (gui.in_use)
2602 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002603# if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2604 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2605 if (!is_click)
2606 /* Ignore right button release events, only shows the popup
2607 * menu on the button down event. */
2608 return FALSE;
2609# endif
2610# if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2611 if (is_click || is_drag)
2612 /* Ignore right button down and drag mouse events. Windows
2613 * only shows the popup menu on the button up event. */
2614 return FALSE;
2615# endif
2616 }
2617# endif
2618# if defined(FEAT_GUI) && defined(FEAT_TERM_POPUP_MENU)
2619 else
2620# endif
2621# if defined(FEAT_TERM_POPUP_MENU)
2622 if (!is_click)
2623 /* Ignore right button release events, only shows the popup
2624 * menu on the button down event. */
2625 return FALSE;
2626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002628 jump_flags = 0;
2629 if (STRCMP(p_mousem, "popup_setpos") == 0)
2630 {
2631 /* First set the cursor position before showing the popup
2632 * menu. */
2633 if (VIsual_active)
2634 {
2635 pos_T m_pos;
2636
2637 /*
2638 * set MOUSE_MAY_STOP_VIS if we are outside the
2639 * selection or the current window (might have false
2640 * negative here)
2641 */
2642 if (mouse_row < curwin->w_winrow
2643 || mouse_row
2644 > (curwin->w_winrow + curwin->w_height))
2645 jump_flags = MOUSE_MAY_STOP_VIS;
2646 else if (get_fpos_of_mouse(&m_pos) != IN_BUFFER)
2647 jump_flags = MOUSE_MAY_STOP_VIS;
2648 else
2649 {
2650 if ((LT_POS(curwin->w_cursor, VIsual)
2651 && (LT_POS(m_pos, curwin->w_cursor)
2652 || LT_POS(VIsual, m_pos)))
2653 || (LT_POS(VIsual, curwin->w_cursor)
2654 && (LT_POS(m_pos, VIsual)
2655 || LT_POS(curwin->w_cursor, m_pos))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 {
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002657 jump_flags = MOUSE_MAY_STOP_VIS;
2658 }
2659 else if (VIsual_mode == Ctrl_V)
2660 {
2661 getvcols(curwin, &curwin->w_cursor, &VIsual,
2662 &leftcol, &rightcol);
2663 getvcol(curwin, &m_pos, NULL, &m_pos.col, NULL);
2664 if (m_pos.col < leftcol || m_pos.col > rightcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 }
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002669 else
2670 jump_flags = MOUSE_MAY_STOP_VIS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 }
Bram Moolenaaraef8c3d2018-03-03 18:59:16 +01002672 if (jump_flags)
2673 {
2674 jump_flags = jump_to_mouse(jump_flags, NULL, which_button);
2675 update_curbuf(VIsual_active ? INVERTED : VALID);
2676 setcursor();
2677 out_flush(); /* Update before showing popup menu */
2678 }
2679# ifdef FEAT_MENU
2680 show_popupmenu();
2681 got_click = FALSE; /* ignore release events */
2682# endif
2683 return (jump_flags & CURSOR_MOVED) != 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684#else
2685 return FALSE;
2686#endif
2687 }
Bram Moolenaar64969662005-12-14 21:59:55 +00002688 if (which_button == MOUSE_LEFT
2689 && (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_ALT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
2691 which_button = MOUSE_RIGHT;
2692 mod_mask &= ~MOD_MASK_SHIFT;
2693 }
2694 }
2695
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if ((State & (NORMAL | INSERT))
2697 && !(mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)))
2698 {
2699 if (which_button == MOUSE_LEFT)
2700 {
2701 if (is_click)
2702 {
2703 /* stop Visual mode for a left click in a window, but not when
2704 * on a status line */
2705 if (VIsual_active)
2706 jump_flags |= MOUSE_MAY_STOP_VIS;
2707 }
2708 else if (mouse_has(MOUSE_VISUAL))
2709 jump_flags |= MOUSE_MAY_VIS;
2710 }
2711 else if (which_button == MOUSE_RIGHT)
2712 {
2713 if (is_click && VIsual_active)
2714 {
2715 /*
2716 * Remember the start and end of visual before moving the
2717 * cursor.
2718 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002719 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 {
2721 start_visual = curwin->w_cursor;
2722 end_visual = VIsual;
2723 }
2724 else
2725 {
2726 start_visual = VIsual;
2727 end_visual = curwin->w_cursor;
2728 }
2729 }
2730 jump_flags |= MOUSE_FOCUS;
2731 if (mouse_has(MOUSE_VISUAL))
2732 jump_flags |= MOUSE_MAY_VIS;
2733 }
2734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736 /*
2737 * If an operator is pending, ignore all drags and releases until the
2738 * next mouse click.
2739 */
2740 if (!is_drag && oap != NULL && oap->op_type != OP_NOP)
2741 {
2742 got_click = FALSE;
2743 oap->motion_type = MCHAR;
2744 }
2745
2746 /* When releasing the button let jump_to_mouse() know. */
2747 if (!is_click && !is_drag)
2748 jump_flags |= MOUSE_RELEASED;
2749
2750 /*
2751 * JUMP!
2752 */
2753 jump_flags = jump_to_mouse(jump_flags,
2754 oap == NULL ? NULL : &(oap->inclusive), which_button);
Bram Moolenaareb163d72017-09-23 15:08:17 +02002755
2756#ifdef FEAT_MENU
2757 /* A click in the window toolbar has no side effects. */
2758 if (jump_flags & MOUSE_WINBAR)
2759 return FALSE;
2760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 moved = (jump_flags & CURSOR_MOVED);
2762 in_status_line = (jump_flags & IN_STATUS_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 in_sep_line = (jump_flags & IN_SEP_LINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
2765#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002766 if (isNetbeansBuffer(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 && !(jump_flags & (IN_STATUS_LINE | IN_SEP_LINE)))
2768 {
2769 int key = KEY2TERMCAP1(c);
2770
2771 if (key == (int)KE_LEFTRELEASE || key == (int)KE_MIDDLERELEASE
2772 || key == (int)KE_RIGHTRELEASE)
2773 netbeans_button_release(which_button);
2774 }
2775#endif
2776
2777 /* When jumping to another window, clear a pending operator. That's a bit
2778 * friendlier than beeping and not jumping to that window. */
2779 if (curwin != old_curwin && oap != NULL && oap->op_type != OP_NOP)
2780 clearop(oap);
2781
2782#ifdef FEAT_FOLDING
2783 if (mod_mask == 0
2784 && !is_drag
2785 && (jump_flags & (MOUSE_FOLD_CLOSE | MOUSE_FOLD_OPEN))
2786 && which_button == MOUSE_LEFT)
2787 {
2788 /* open or close a fold at this line */
2789 if (jump_flags & MOUSE_FOLD_OPEN)
2790 openFold(curwin->w_cursor.lnum, 1L);
2791 else
2792 closeFold(curwin->w_cursor.lnum, 1L);
2793 /* don't move the cursor if still in the same window */
2794 if (curwin == old_curwin)
2795 curwin->w_cursor = save_cursor;
2796 }
2797#endif
2798
2799#if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2800 if ((jump_flags & IN_OTHER_WIN) && !VIsual_active && clip_star.available)
2801 {
2802 clip_modeless(which_button, is_click, is_drag);
2803 return FALSE;
2804 }
2805#endif
2806
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 /* Set global flag that we are extending the Visual area with mouse
Bram Moolenaarf711faf2007-05-10 16:48:19 +00002808 * dragging; temporarily minimize 'scrolloff'. */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002809 if (VIsual_active && is_drag && get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 {
2811 /* In the very first line, allow scrolling one line */
2812 if (mouse_row == 0)
2813 mouse_dragging = 2;
2814 else
2815 mouse_dragging = 1;
2816 }
2817
2818 /* When dragging the mouse above the window, scroll down. */
2819 if (is_drag && mouse_row < 0 && !in_status_line)
2820 {
2821 scroll_redraw(FALSE, 1L);
2822 mouse_row = 0;
2823 }
2824
2825 if (start_visual.lnum) /* right click in visual mode */
2826 {
Bram Moolenaar64969662005-12-14 21:59:55 +00002827 /* When ALT is pressed make Visual mode blockwise. */
2828 if (mod_mask & MOD_MASK_ALT)
2829 VIsual_mode = Ctrl_V;
2830
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 /*
2832 * In Visual-block mode, divide the area in four, pick up the corner
2833 * that is in the quarter that the cursor is in.
2834 */
2835 if (VIsual_mode == Ctrl_V)
2836 {
2837 getvcols(curwin, &start_visual, &end_visual, &leftcol, &rightcol);
2838 if (curwin->w_curswant > (leftcol + rightcol) / 2)
2839 end_visual.col = leftcol;
2840 else
2841 end_visual.col = rightcol;
Bram Moolenaarcde88542015-08-11 19:14:00 +02002842 if (curwin->w_cursor.lnum >=
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 (start_visual.lnum + end_visual.lnum) / 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 end_visual.lnum = start_visual.lnum;
2845
2846 /* move VIsual to the right column */
2847 start_visual = curwin->w_cursor; /* save the cursor pos */
2848 curwin->w_cursor = end_visual;
2849 coladvance(end_visual.col);
2850 VIsual = curwin->w_cursor;
2851 curwin->w_cursor = start_visual; /* restore the cursor */
2852 }
2853 else
2854 {
2855 /*
2856 * If the click is before the start of visual, change the start.
2857 * If the click is after the end of visual, change the end. If
2858 * the click is inside the visual, change the closest side.
2859 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002860 if (LT_POS(curwin->w_cursor, start_visual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 VIsual = end_visual;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002862 else if (LT_POS(end_visual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 VIsual = start_visual;
2864 else
2865 {
2866 /* In the same line, compare column number */
2867 if (end_visual.lnum == start_visual.lnum)
2868 {
2869 if (curwin->w_cursor.col - start_visual.col >
2870 end_visual.col - curwin->w_cursor.col)
2871 VIsual = start_visual;
2872 else
2873 VIsual = end_visual;
2874 }
2875
2876 /* In different lines, compare line number */
2877 else
2878 {
2879 diff = (curwin->w_cursor.lnum - start_visual.lnum) -
2880 (end_visual.lnum - curwin->w_cursor.lnum);
2881
2882 if (diff > 0) /* closest to end */
2883 VIsual = start_visual;
2884 else if (diff < 0) /* closest to start */
2885 VIsual = end_visual;
2886 else /* in the middle line */
2887 {
2888 if (curwin->w_cursor.col <
2889 (start_visual.col + end_visual.col) / 2)
2890 VIsual = end_visual;
2891 else
2892 VIsual = start_visual;
2893 }
2894 }
2895 }
2896 }
2897 }
2898 /*
2899 * If Visual mode started in insert mode, execute "CTRL-O"
2900 */
2901 else if ((State & INSERT) && VIsual_active)
2902 stuffcharReadbuff(Ctrl_O);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904 /*
2905 * Middle mouse click: Put text before cursor.
2906 */
2907 if (which_button == MOUSE_MIDDLE)
2908 {
2909#ifdef FEAT_CLIPBOARD
2910 if (clip_star.available && regname == 0)
2911 regname = '*';
2912#endif
2913 if (yank_register_mline(regname))
2914 {
2915 if (mouse_past_bottom)
2916 dir = FORWARD;
2917 }
2918 else if (mouse_past_eol)
2919 dir = FORWARD;
2920
2921 if (fixindent)
2922 {
2923 c1 = (dir == BACKWARD) ? '[' : ']';
2924 c2 = 'p';
2925 }
2926 else
2927 {
2928 c1 = (dir == FORWARD) ? 'p' : 'P';
2929 c2 = NUL;
2930 }
2931 prep_redo(regname, count, NUL, c1, NUL, c2, NUL);
2932
2933 /*
2934 * Remember where the paste started, so in edit() Insstart can be set
2935 * to this position
2936 */
2937 if (restart_edit != 0)
2938 where_paste_started = curwin->w_cursor;
2939 do_put(regname, dir, count, fixindent | PUT_CURSEND);
2940 }
2941
Bram Moolenaar4033c552017-09-16 20:54:51 +02002942#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 /*
2944 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2945 * error under the mouse pointer.
2946 */
2947 else if (((mod_mask & MOD_MASK_CTRL)
2948 || (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
2949 && bt_quickfix(curbuf))
2950 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002951 if (curwin->w_llist_ref == NULL) /* quickfix window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002952 do_cmdline_cmd((char_u *)".cc");
Bram Moolenaar28c258f2006-01-25 22:02:51 +00002953 else /* location list window */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01002954 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 got_click = FALSE; /* ignore drag&release now */
2956 }
2957#endif
2958
2959 /*
2960 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2961 * under the mouse pointer.
2962 */
2963 else if ((mod_mask & MOD_MASK_CTRL) || (curbuf->b_help
2964 && (mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK))
2965 {
2966 if (State & INSERT)
2967 stuffcharReadbuff(Ctrl_O);
2968 stuffcharReadbuff(Ctrl_RSB);
2969 got_click = FALSE; /* ignore drag&release now */
2970 }
2971
2972 /*
2973 * Shift-Mouse click searches for the next occurrence of the word under
2974 * the mouse pointer
2975 */
2976 else if ((mod_mask & MOD_MASK_SHIFT))
2977 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01002978 if ((State & INSERT) || (VIsual_active && VIsual_select))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 stuffcharReadbuff(Ctrl_O);
2980 if (which_button == MOUSE_LEFT)
2981 stuffcharReadbuff('*');
2982 else /* MOUSE_RIGHT */
2983 stuffcharReadbuff('#');
2984 }
2985
2986 /* Handle double clicks, unless on status line */
2987 else if (in_status_line)
2988 {
2989#ifdef FEAT_MOUSESHAPE
2990 if ((is_drag || is_click) && !drag_status_line)
2991 {
2992 drag_status_line = TRUE;
2993 update_mouseshape(-1);
2994 }
2995#endif
2996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 else if (in_sep_line)
2998 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02002999#ifdef FEAT_MOUSESHAPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if ((is_drag || is_click) && !drag_sep_line)
3001 {
3002 drag_sep_line = TRUE;
3003 update_mouseshape(-1);
3004 }
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003005#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 else if ((mod_mask & MOD_MASK_MULTI_CLICK) && (State & (NORMAL | INSERT))
3008 && mouse_has(MOUSE_VISUAL))
3009 {
3010 if (is_click || !VIsual_active)
3011 {
3012 if (VIsual_active)
3013 orig_cursor = VIsual;
3014 else
3015 {
3016 check_visual_highlight();
3017 VIsual = curwin->w_cursor;
3018 orig_cursor = VIsual;
3019 VIsual_active = TRUE;
3020 VIsual_reselect = TRUE;
3021 /* start Select mode if 'selectmode' contains "mouse" */
3022 may_start_select('o');
3023 setmouse();
3024 }
3025 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
Bram Moolenaar64969662005-12-14 21:59:55 +00003026 {
3027 /* Double click with ALT pressed makes it blockwise. */
3028 if (mod_mask & MOD_MASK_ALT)
3029 VIsual_mode = Ctrl_V;
3030 else
3031 VIsual_mode = 'v';
3032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_3CLICK)
3034 VIsual_mode = 'V';
3035 else if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_4CLICK)
3036 VIsual_mode = Ctrl_V;
3037#ifdef FEAT_CLIPBOARD
3038 /* Make sure the clipboard gets updated. Needed because start and
3039 * end may still be the same, and the selection needs to be owned */
3040 clip_star.vmode = NUL;
3041#endif
3042 }
3043 /*
3044 * A double click selects a word or a block.
3045 */
3046 if ((mod_mask & MOD_MASK_MULTI_CLICK) == MOD_MASK_2CLICK)
3047 {
3048 pos_T *pos = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003049 int gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050
3051 if (is_click)
3052 {
3053 /* If the character under the cursor (skipping white space) is
3054 * not a word character, try finding a match and select a (),
3055 * {}, [], #if/#endif, etc. block. */
3056 end_visual = curwin->w_cursor;
Bram Moolenaar1c465442017-03-12 20:10:05 +01003057 while (gc = gchar_pos(&end_visual), VIM_ISWHITE(gc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 inc(&end_visual);
3059 if (oap != NULL)
3060 oap->motion_type = MCHAR;
3061 if (oap != NULL
3062 && VIsual_mode == 'v'
3063 && !vim_iswordc(gchar_pos(&end_visual))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003064 && EQUAL_POS(curwin->w_cursor, VIsual)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 && (pos = findmatch(oap, NUL)) != NULL)
3066 {
3067 curwin->w_cursor = *pos;
3068 if (oap->motion_type == MLINE)
3069 VIsual_mode = 'V';
3070 else if (*p_sel == 'e')
3071 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003072 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 ++VIsual.col;
3074 else
3075 ++curwin->w_cursor.col;
3076 }
3077 }
3078 }
3079
3080 if (pos == NULL && (is_click || is_drag))
3081 {
3082 /* When not found a match or when dragging: extend to include
3083 * a word. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003084 if (LT_POS(curwin->w_cursor, orig_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 {
3086 find_start_of_word(&curwin->w_cursor);
3087 find_end_of_word(&VIsual);
3088 }
3089 else
3090 {
3091 find_start_of_word(&VIsual);
3092 if (*p_sel == 'e' && *ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 curwin->w_cursor.col +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003094 (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 find_end_of_word(&curwin->w_cursor);
3096 }
3097 }
3098 curwin->w_set_curswant = TRUE;
3099 }
3100 if (is_click)
3101 redraw_curbuf_later(INVERTED); /* update the inversion */
3102 }
3103 else if (VIsual_active && !old_active)
Bram Moolenaar64969662005-12-14 21:59:55 +00003104 {
3105 if (mod_mask & MOD_MASK_ALT)
3106 VIsual_mode = Ctrl_V;
3107 else
3108 VIsual_mode = 'v';
3109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110
3111 /* If Visual mode changed show it later. */
Bram Moolenaar28c258f2006-01-25 22:02:51 +00003112 if ((!VIsual_active && old_active && mode_displayed)
3113 || (VIsual_active && p_smd && msg_silent == 0
3114 && (!old_active || VIsual_mode != old_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 redraw_cmdline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 return moved;
3118}
3119
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120/*
3121 * Move "pos" back to the start of the word it's in.
3122 */
3123 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003124find_start_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
3126 char_u *line;
3127 int cclass;
3128 int col;
3129
3130 line = ml_get(pos->lnum);
3131 cclass = get_mouse_class(line + pos->col);
3132
3133 while (pos->col > 0)
3134 {
3135 col = pos->col - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 col -= (*mb_head_off)(line, line + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (get_mouse_class(line + col) != cclass)
3138 break;
3139 pos->col = col;
3140 }
3141}
3142
3143/*
3144 * Move "pos" forward to the end of the word it's in.
3145 * When 'selection' is "exclusive", the position is just after the word.
3146 */
3147 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003148find_end_of_word(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
3150 char_u *line;
3151 int cclass;
3152 int col;
3153
3154 line = ml_get(pos->lnum);
3155 if (*p_sel == 'e' && pos->col > 0)
3156 {
3157 --pos->col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 pos->col -= (*mb_head_off)(line, line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
3160 cclass = get_mouse_class(line + pos->col);
3161 while (line[pos->col] != NUL)
3162 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003163 col = pos->col + (*mb_ptr2len)(line + pos->col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 if (get_mouse_class(line + col) != cclass)
3165 {
3166 if (*p_sel == 'e')
3167 pos->col = col;
3168 break;
3169 }
3170 pos->col = col;
3171 }
3172}
3173
3174/*
3175 * Get class of a character for selection: same class means same word.
3176 * 0: blank
3177 * 1: punctuation groups
3178 * 2: normal word character
3179 * >2: multi-byte word character.
3180 */
3181 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003182get_mouse_class(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 int c;
3185
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (has_mbyte && MB_BYTE2LEN(p[0]) > 1)
3187 return mb_get_class(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 c = *p;
3190 if (c == ' ' || c == '\t')
3191 return 0;
3192
3193 if (vim_iswordc(c))
3194 return 2;
3195
3196 /*
3197 * There are a few special cases where we want certain combinations of
3198 * characters to be considered as a single word. These are things like
3199 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02003200 * character is in its own class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 */
3202 if (c != NUL && vim_strchr((char_u *)"-+*/%<>&|^!=", c) != NULL)
3203 return 1;
3204 return c;
3205}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206#endif /* FEAT_MOUSE */
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208/*
3209 * Check if highlighting for visual mode is possible, give a warning message
3210 * if not.
3211 */
3212 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003213check_visual_highlight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
3215 static int did_check = FALSE;
3216
3217 if (full_screen)
3218 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003219 if (!did_check && HL_ATTR(HLF_V) == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003220 msg(_("Warning: terminal cannot highlight"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 did_check = TRUE;
3222 }
3223}
3224
3225/*
Bram Moolenaar66fa2712006-01-22 23:22:22 +00003226 * End Visual mode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 * This function should ALWAYS be called to end Visual mode, except from
3228 * do_pending_operator().
3229 */
3230 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003231end_visual_mode(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
3233#ifdef FEAT_CLIPBOARD
3234 /*
3235 * If we are using the clipboard, then remember what was selected in case
3236 * we need to paste it somewhere while we still own the selection.
3237 * Only do this when the clipboard is already owned. Don't want to grab
3238 * the selection when hitting ESC.
3239 */
3240 if (clip_star.available && clip_star.owned)
3241 clip_auto_select();
3242#endif
3243
3244 VIsual_active = FALSE;
3245#ifdef FEAT_MOUSE
3246 setmouse();
3247 mouse_dragging = 0;
3248#endif
3249
3250 /* Save the current VIsual area for '< and '> marks, and "gv" */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003251 curbuf->b_visual.vi_mode = VIsual_mode;
3252 curbuf->b_visual.vi_start = VIsual;
3253 curbuf->b_visual.vi_end = curwin->w_cursor;
3254 curbuf->b_visual.vi_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#ifdef FEAT_EVAL
3256 curbuf->b_visual_mode_eval = VIsual_mode;
3257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (!virtual_active())
3259 curwin->w_cursor.coladd = 0;
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003260 may_clear_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003262 adjust_cursor_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263}
3264
3265/*
3266 * Reset VIsual_active and VIsual_reselect.
3267 */
3268 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003269reset_VIsual_and_resel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 if (VIsual_active)
3272 {
3273 end_visual_mode();
3274 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3275 }
3276 VIsual_reselect = FALSE;
3277}
3278
3279/*
3280 * Reset VIsual_active and VIsual_reselect if it's set.
3281 */
3282 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003283reset_VIsual(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 if (VIsual_active)
3286 {
3287 end_visual_mode();
3288 redraw_curbuf_later(INVERTED); /* delete the inversion later */
3289 VIsual_reselect = FALSE;
3290 }
3291}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293/*
3294 * Check for a balloon-eval special item to include when searching for an
3295 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3296 * Returns TRUE if the character at "*ptr" should be included.
3297 * "dir" is FORWARD or BACKWARD, the direction of searching.
3298 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3299 * "bnp" points to a counter for square brackets.
3300 */
3301 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003302find_is_eval_item(
3303 char_u *ptr,
3304 int *colp,
3305 int *bnp,
3306 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 /* Accept everything inside []. */
3309 if ((*ptr == ']' && dir == BACKWARD) || (*ptr == '[' && dir == FORWARD))
3310 ++*bnp;
3311 if (*bnp > 0)
3312 {
3313 if ((*ptr == '[' && dir == BACKWARD) || (*ptr == ']' && dir == FORWARD))
3314 --*bnp;
3315 return TRUE;
3316 }
3317
3318 /* skip over "s.var" */
3319 if (*ptr == '.')
3320 return TRUE;
3321
3322 /* two-character item: s->var */
3323 if (ptr[dir == BACKWARD ? 0 : 1] == '>'
3324 && ptr[dir == BACKWARD ? -1 : 0] == '-')
3325 {
3326 *colp += dir;
3327 return TRUE;
3328 }
3329 return FALSE;
3330}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
3332/*
3333 * Find the identifier under or to the right of the cursor.
3334 * "find_type" can have one of three values:
3335 * FIND_IDENT: find an identifier (keyword)
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003336 * FIND_STRING: find any non-white text
3337 * FIND_IDENT + FIND_STRING: find any non-white text, identifier preferred.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003338 * FIND_EVAL: find text useful for C program debugging
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 *
3340 * There are three steps:
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003341 * 1. Search forward for the start of an identifier/text. Doesn't move if
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 * already on one.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003343 * 2. Search backward for the start of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 * This doesn't match the real Vi but I like it a little better and it
3345 * shouldn't bother anyone.
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003346 * 3. Search forward to the end of this identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 * When FIND_IDENT isn't defined, we backup until a blank.
3348 *
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003349 * Returns the length of the text, or zero if no text is found.
3350 * If text is found, a pointer to the text is put in "*text". This
3351 * points into the current buffer line and is not always NUL terminated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
3353 int
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003354find_ident_under_cursor(char_u **text, int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 return find_ident_at_pos(curwin, curwin->w_cursor.lnum,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003357 curwin->w_cursor.col, text, NULL, find_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358}
3359
3360/*
3361 * Like find_ident_under_cursor(), but for any window and any position.
3362 * However: Uses 'iskeyword' from the current window!.
3363 */
3364 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003365find_ident_at_pos(
3366 win_T *wp,
3367 linenr_T lnum,
3368 colnr_T startcol,
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003369 char_u **text,
3370 int *textcol, // column where "text" starts, can be NULL
Bram Moolenaar9b578142016-01-30 19:39:49 +01003371 int find_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 char_u *ptr;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003374 int col = 0; // init to shut up GCC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 int this_class = 0;
3377 int prev_class;
3378 int prevcol;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003379 int bn = 0; // bracket nesting
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381 /*
3382 * if i == 0: try to find an identifier
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003383 * if i == 1: try to find any non-white text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 */
3385 ptr = ml_get_buf(wp->w_buffer, lnum, FALSE);
3386 for (i = (find_type & FIND_IDENT) ? 0 : 1; i < 2; ++i)
3387 {
3388 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003389 * 1. skip to start of identifier/text
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 */
3391 col = startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 if (has_mbyte)
3393 {
3394 while (ptr[col] != NUL)
3395 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003396 // Stop at a ']' to evaluate "a[x]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3398 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 this_class = mb_get_class(ptr + col);
3400 if (this_class != 0 && (i == 1 || this_class != 1))
3401 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003402 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
3404 }
3405 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 while (ptr[col] != NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01003407 && (i == 0 ? !vim_iswordc(ptr[col]) : VIM_ISWHITE(ptr[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 && (!(find_type & FIND_EVAL) || ptr[col] != ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 )
3410 ++col;
3411
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003412 // When starting on a ']' count it, so that we include the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 bn = ptr[col] == ']';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
3415 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003416 * 2. Back up to start of identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (has_mbyte)
3419 {
3420 /* Remember class of character under cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if ((find_type & FIND_EVAL) && ptr[col] == ']')
3422 this_class = mb_get_class((char_u *)"a");
3423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 this_class = mb_get_class(ptr + col);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003425 while (col > 0 && this_class != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
3427 prevcol = col - 1 - (*mb_head_off)(ptr, ptr + col - 1);
3428 prev_class = mb_get_class(ptr + prevcol);
3429 if (this_class != prev_class
3430 && (i == 0
3431 || prev_class == 0
3432 || (find_type & FIND_IDENT))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 && (!(find_type & FIND_EVAL)
3434 || prevcol == 0
3435 || !find_is_eval_item(ptr + prevcol, &prevcol,
3436 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 )
3438 break;
3439 col = prevcol;
3440 }
3441
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003442 // If we don't want just any old text, or we've found an
3443 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (this_class > 2)
3445 this_class = 2;
3446 if (!(find_type & FIND_STRING) || this_class == 2)
3447 break;
3448 }
3449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
3451 while (col > 0
3452 && ((i == 0
3453 ? vim_iswordc(ptr[col - 1])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003454 : (!VIM_ISWHITE(ptr[col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 && (!(find_type & FIND_IDENT)
3456 || !vim_iswordc(ptr[col - 1]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 || ((find_type & FIND_EVAL)
3458 && col > 1
3459 && find_is_eval_item(ptr + col - 1, &col,
3460 &bn, BACKWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 ))
3462 --col;
3463
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003464 // If we don't want just any old text, or we've found an
3465 // identifier, stop searching.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (!(find_type & FIND_STRING) || vim_iswordc(ptr[col]))
3467 break;
3468 }
3469 }
3470
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003471 if (ptr[col] == NUL || (i == 0
3472 && (has_mbyte ? this_class != 2 : !vim_iswordc(ptr[col]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003474 // didn't find an identifier or text
Bram Moolenaar17627312019-06-02 19:53:44 +02003475 if ((find_type & FIND_NOERROR) == 0)
3476 {
3477 if (find_type & FIND_STRING)
3478 emsg(_("E348: No string under cursor"));
3479 else
3480 emsg(_(e_noident));
3481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 return 0;
3483 }
3484 ptr += col;
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003485 *text = ptr;
3486 if (textcol != NULL)
3487 *textcol = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 /*
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003490 * 3. Find the end if the identifier/text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 bn = 0;
3493 startcol -= col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (has_mbyte)
3496 {
Bram Moolenaar7ba343e2019-07-09 23:22:15 +02003497 // Search for point of changing multibyte character class.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 this_class = mb_get_class(ptr);
3499 while (ptr[col] != NUL
3500 && ((i == 0 ? mb_get_class(ptr + col) == this_class
3501 : mb_get_class(ptr + col) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 || ((find_type & FIND_EVAL)
3503 && col <= (int)startcol
3504 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 ))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003506 col += (*mb_ptr2len)(ptr + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 while ((i == 0 ? vim_iswordc(ptr[col])
Bram Moolenaar1c465442017-03-12 20:10:05 +01003510 : (ptr[col] != NUL && !VIM_ISWHITE(ptr[col])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 || ((find_type & FIND_EVAL)
3512 && col <= (int)startcol
3513 && find_is_eval_item(ptr + col, &col, &bn, FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 ++col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
3517 return col;
3518}
3519
3520/*
3521 * Prepare for redo of a normal command.
3522 */
3523 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003524prep_redo_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525{
3526 prep_redo(cap->oap->regname, cap->count0,
3527 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
3528}
3529
3530/*
3531 * Prepare for redo of any command.
3532 * Note that only the last argument can be a multi-byte char.
3533 */
3534 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003535prep_redo(
3536 int regname,
3537 long num,
3538 int cmd1,
3539 int cmd2,
3540 int cmd3,
3541 int cmd4,
3542 int cmd5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543{
3544 ResetRedobuff();
3545 if (regname != 0) /* yank from specified buffer */
3546 {
3547 AppendCharToRedobuff('"');
3548 AppendCharToRedobuff(regname);
3549 }
3550 if (num)
3551 AppendNumberToRedobuff(num);
3552
3553 if (cmd1 != NUL)
3554 AppendCharToRedobuff(cmd1);
3555 if (cmd2 != NUL)
3556 AppendCharToRedobuff(cmd2);
3557 if (cmd3 != NUL)
3558 AppendCharToRedobuff(cmd3);
3559 if (cmd4 != NUL)
3560 AppendCharToRedobuff(cmd4);
3561 if (cmd5 != NUL)
3562 AppendCharToRedobuff(cmd5);
3563}
3564
3565/*
3566 * check for operator active and clear it
3567 *
3568 * return TRUE if operator was active
3569 */
3570 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003571checkclearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
3573 if (oap->op_type == OP_NOP)
3574 return FALSE;
3575 clearopbeep(oap);
3576 return TRUE;
3577}
3578
3579/*
Bram Moolenaarc980de32007-05-06 11:59:04 +00003580 * Check for operator or Visual active. Clear active operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 *
Bram Moolenaarc980de32007-05-06 11:59:04 +00003582 * Return TRUE if operator or Visual was active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 */
3584 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003585checkclearopq(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003587 if (oap->op_type == OP_NOP && !VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 return FALSE;
3589 clearopbeep(oap);
3590 return TRUE;
3591}
3592
3593 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003594clearop(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
3596 oap->op_type = OP_NOP;
3597 oap->regname = 0;
3598 oap->motion_force = NUL;
3599 oap->use_reg_one = FALSE;
3600}
3601
3602 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003603clearopbeep(oparg_T *oap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 clearop(oap);
3606 beep_flush();
3607}
3608
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609/*
3610 * Remove the shift modifier from a special key.
3611 */
3612 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003613unshift_special(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614{
3615 switch (cap->cmdchar)
3616 {
3617 case K_S_RIGHT: cap->cmdchar = K_RIGHT; break;
3618 case K_S_LEFT: cap->cmdchar = K_LEFT; break;
3619 case K_S_UP: cap->cmdchar = K_UP; break;
3620 case K_S_DOWN: cap->cmdchar = K_DOWN; break;
3621 case K_S_HOME: cap->cmdchar = K_HOME; break;
3622 case K_S_END: cap->cmdchar = K_END; break;
3623 }
3624 cap->cmdchar = simplify_key(cap->cmdchar, &mod_mask);
3625}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003627/*
3628 * If the mode is currently displayed clear the command line or update the
3629 * command displayed.
3630 */
3631 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003632may_clear_cmdline(void)
Bram Moolenaar0bbcb5c2015-08-04 19:18:52 +02003633{
3634 if (mode_displayed)
3635 clear_cmdline = TRUE; /* unshow visual mode later */
3636#ifdef FEAT_CMDL_INFO
3637 else
3638 clear_showcmd();
3639#endif
3640}
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642#if defined(FEAT_CMDL_INFO) || defined(PROTO)
3643/*
3644 * Routines for displaying a partly typed command
3645 */
3646
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01003647#define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648static char_u showcmd_buf[SHOWCMD_BUFLEN];
3649static char_u old_showcmd_buf[SHOWCMD_BUFLEN]; /* For push_showcmd() */
3650static int showcmd_is_clear = TRUE;
3651static int showcmd_visual = FALSE;
3652
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003653static void display_showcmd(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003656clear_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 if (!p_sc)
3659 return;
3660
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 if (VIsual_active && !char_avail())
3662 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003663 int cursor_bot = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 long lines;
3665 colnr_T leftcol, rightcol;
3666 linenr_T top, bot;
3667
3668 /* Show the size of the Visual area. */
Bram Moolenaar81d00072009-04-29 15:41:40 +00003669 if (cursor_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
3671 top = VIsual.lnum;
3672 bot = curwin->w_cursor.lnum;
3673 }
3674 else
3675 {
3676 top = curwin->w_cursor.lnum;
3677 bot = VIsual.lnum;
3678 }
3679# ifdef FEAT_FOLDING
3680 /* Include closed folds as a whole. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02003681 (void)hasFolding(top, &top, NULL);
3682 (void)hasFolding(bot, NULL, &bot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683# endif
3684 lines = bot - top + 1;
3685
3686 if (VIsual_mode == Ctrl_V)
3687 {
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003688# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003689 char_u *saved_sbr = p_sbr;
3690
3691 /* Make 'sbr' empty for a moment to get the correct size. */
3692 p_sbr = empty_option;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 getvcols(curwin, &curwin->w_cursor, &VIsual, &leftcol, &rightcol);
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003695# ifdef FEAT_LINEBREAK
Bram Moolenaar81d00072009-04-29 15:41:40 +00003696 p_sbr = saved_sbr;
Bram Moolenaarfdf732e2010-07-18 14:20:35 +02003697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 sprintf((char *)showcmd_buf, "%ldx%ld", lines,
3699 (long)(rightcol - leftcol + 1));
3700 }
3701 else if (VIsual_mode == 'V' || VIsual.lnum != curwin->w_cursor.lnum)
3702 sprintf((char *)showcmd_buf, "%ld", lines);
3703 else
Bram Moolenaarf91787c2010-07-17 12:47:16 +02003704 {
3705 char_u *s, *e;
3706 int l;
3707 int bytes = 0;
3708 int chars = 0;
3709
3710 if (cursor_bot)
3711 {
3712 s = ml_get_pos(&VIsual);
3713 e = ml_get_cursor();
3714 }
3715 else
3716 {
3717 s = ml_get_cursor();
3718 e = ml_get_pos(&VIsual);
3719 }
3720 while ((*p_sel != 'e') ? s <= e : s < e)
3721 {
3722 l = (*mb_ptr2len)(s);
3723 if (l == 0)
3724 {
3725 ++bytes;
3726 ++chars;
3727 break; /* end of line */
3728 }
3729 bytes += l;
3730 ++chars;
3731 s += l;
3732 }
3733 if (bytes == chars)
3734 sprintf((char *)showcmd_buf, "%d", chars);
3735 else
3736 sprintf((char *)showcmd_buf, "%d-%d", chars, bytes);
3737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 showcmd_buf[SHOWCMD_COLS] = NUL; /* truncate */
3739 showcmd_visual = TRUE;
3740 }
3741 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
3743 showcmd_buf[0] = NUL;
3744 showcmd_visual = FALSE;
3745
3746 /* Don't actually display something if there is nothing to clear. */
3747 if (showcmd_is_clear)
3748 return;
3749 }
3750
3751 display_showcmd();
3752}
3753
3754/*
3755 * Add 'c' to string of shown command chars.
3756 * Return TRUE if output has been written (and setcursor() has been called).
3757 */
3758 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003759add_to_showcmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
3761 char_u *p;
3762 int old_len;
3763 int extra_len;
3764 int overflow;
3765#if defined(FEAT_MOUSE)
3766 int i;
3767 static int ignore[] =
3768 {
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003769# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 K_VER_SCROLLBAR, K_HOR_SCROLLBAR,
3771 K_LEFTMOUSE_NM, K_LEFTRELEASE_NM,
Bram Moolenaarcf0c5542006-02-09 23:48:02 +00003772# endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01003773 K_IGNORE, K_PS,
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003774 K_LEFTMOUSE, K_LEFTDRAG, K_LEFTRELEASE, K_MOUSEMOVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 K_MIDDLEMOUSE, K_MIDDLEDRAG, K_MIDDLERELEASE,
3776 K_RIGHTMOUSE, K_RIGHTDRAG, K_RIGHTRELEASE,
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003777 K_MOUSEDOWN, K_MOUSEUP, K_MOUSELEFT, K_MOUSERIGHT,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 K_X1MOUSE, K_X1DRAG, K_X1RELEASE, K_X2MOUSE, K_X2DRAG, K_X2RELEASE,
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003779 K_CURSORHOLD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 0
3781 };
3782#endif
3783
Bram Moolenaar09df3122006-01-23 22:23:09 +00003784 if (!p_sc || msg_silent != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 return FALSE;
3786
3787 if (showcmd_visual)
3788 {
3789 showcmd_buf[0] = NUL;
3790 showcmd_visual = FALSE;
3791 }
3792
3793#if defined(FEAT_MOUSE)
3794 /* Ignore keys that are scrollbar updates and mouse clicks */
3795 if (IS_SPECIAL(c))
3796 for (i = 0; ignore[i] != 0; ++i)
3797 if (ignore[i] == c)
3798 return FALSE;
3799#endif
3800
3801 p = transchar(c);
Bram Moolenaar7ba07412013-12-11 14:55:01 +01003802 if (*p == ' ')
3803 STRCPY(p, "<20>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 old_len = (int)STRLEN(showcmd_buf);
3805 extra_len = (int)STRLEN(p);
3806 overflow = old_len + extra_len - SHOWCMD_COLS;
3807 if (overflow > 0)
Bram Moolenaarb0db5692007-08-14 20:54:49 +00003808 mch_memmove(showcmd_buf, showcmd_buf + overflow,
3809 old_len - overflow + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 STRCAT(showcmd_buf, p);
3811
3812 if (char_avail())
3813 return FALSE;
3814
3815 display_showcmd();
3816
3817 return TRUE;
3818}
3819
3820 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003821add_to_showcmd_c(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
3823 if (!add_to_showcmd(c))
3824 setcursor();
3825}
3826
3827/*
3828 * Delete 'len' characters from the end of the shown command.
3829 */
3830 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003831del_from_showcmd(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832{
3833 int old_len;
3834
3835 if (!p_sc)
3836 return;
3837
3838 old_len = (int)STRLEN(showcmd_buf);
3839 if (len > old_len)
3840 len = old_len;
3841 showcmd_buf[old_len - len] = NUL;
3842
3843 if (!char_avail())
3844 display_showcmd();
3845}
3846
3847/*
3848 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3849 * something and there is a partial mapping.
3850 */
3851 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003852push_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853{
3854 if (p_sc)
3855 STRCPY(old_showcmd_buf, showcmd_buf);
3856}
3857
3858 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003859pop_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
3861 if (!p_sc)
3862 return;
3863
3864 STRCPY(showcmd_buf, old_showcmd_buf);
3865
3866 display_showcmd();
3867}
3868
3869 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003870display_showcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
3872 int len;
3873
3874 cursor_off();
3875
3876 len = (int)STRLEN(showcmd_buf);
3877 if (len == 0)
3878 showcmd_is_clear = TRUE;
3879 else
3880 {
3881 screen_puts(showcmd_buf, (int)Rows - 1, sc_col, 0);
3882 showcmd_is_clear = FALSE;
3883 }
3884
3885 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00003886 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3887 * spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 */
3889 screen_puts((char_u *)" " + len, (int)Rows - 1, sc_col + len, 0);
3890
3891 setcursor(); /* put cursor back where it belongs */
3892}
3893#endif
3894
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895/*
3896 * When "check" is FALSE, prepare for commands that scroll the window.
3897 * When "check" is TRUE, take care of scroll-binding after the window has
3898 * scrolled. Called from normal_cmd() and edit().
3899 */
3900 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003901do_check_scrollbind(int check)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902{
3903 static win_T *old_curwin = NULL;
3904 static linenr_T old_topline = 0;
3905#ifdef FEAT_DIFF
3906 static int old_topfill = 0;
3907#endif
3908 static buf_T *old_buf = NULL;
3909 static colnr_T old_leftcol = 0;
3910
3911 if (check && curwin->w_p_scb)
3912 {
3913 /* If a ":syncbind" command was just used, don't scroll, only reset
3914 * the values. */
3915 if (did_syncbind)
3916 did_syncbind = FALSE;
3917 else if (curwin == old_curwin)
3918 {
3919 /*
3920 * Synchronize other windows, as necessary according to
3921 * 'scrollbind'. Don't do this after an ":edit" command, except
3922 * when 'diff' is set.
3923 */
3924 if ((curwin->w_buffer == old_buf
3925#ifdef FEAT_DIFF
3926 || curwin->w_p_diff
3927#endif
3928 )
3929 && (curwin->w_topline != old_topline
3930#ifdef FEAT_DIFF
3931 || curwin->w_topfill != old_topfill
3932#endif
3933 || curwin->w_leftcol != old_leftcol))
3934 {
3935 check_scrollbind(curwin->w_topline - old_topline,
3936 (long)(curwin->w_leftcol - old_leftcol));
3937 }
3938 }
3939 else if (vim_strchr(p_sbo, 'j')) /* jump flag set in 'scrollopt' */
3940 {
3941 /*
3942 * When switching between windows, make sure that the relative
3943 * vertical offset is valid for the new window. The relative
3944 * offset is invalid whenever another 'scrollbind' window has
3945 * scrolled to a point that would force the current window to
3946 * scroll past the beginning or end of its buffer. When the
3947 * resync is performed, some of the other 'scrollbind' windows may
3948 * need to jump so that the current window's relative position is
3949 * visible on-screen.
3950 */
3951 check_scrollbind(curwin->w_topline - curwin->w_scbind_pos, 0L);
3952 }
3953 curwin->w_scbind_pos = curwin->w_topline;
3954 }
3955
3956 old_curwin = curwin;
3957 old_topline = curwin->w_topline;
3958#ifdef FEAT_DIFF
3959 old_topfill = curwin->w_topfill;
3960#endif
3961 old_buf = curwin->w_buffer;
3962 old_leftcol = curwin->w_leftcol;
3963}
3964
3965/*
3966 * Synchronize any windows that have "scrollbind" set, based on the
3967 * number of rows by which the current window has changed
3968 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3969 */
3970 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003971check_scrollbind(linenr_T topline_diff, long leftcol_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
3973 int want_ver;
3974 int want_hor;
3975 win_T *old_curwin = curwin;
3976 buf_T *old_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 int old_VIsual_select = VIsual_select;
3978 int old_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 colnr_T tgt_leftcol = curwin->w_leftcol;
3980 long topline;
3981 long y;
3982
3983 /*
3984 * check 'scrollopt' string for vertical and horizontal scroll options
3985 */
3986 want_ver = (vim_strchr(p_sbo, 'v') && topline_diff != 0);
3987#ifdef FEAT_DIFF
3988 want_ver |= old_curwin->w_p_diff;
3989#endif
3990 want_hor = (vim_strchr(p_sbo, 'h') && (leftcol_diff || topline_diff != 0));
3991
3992 /*
3993 * loop through the scrollbound windows and scroll accordingly
3994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 VIsual_select = VIsual_active = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02003996 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 {
3998 curbuf = curwin->w_buffer;
3999 /* skip original window and windows with 'noscrollbind' */
4000 if (curwin != old_curwin && curwin->w_p_scb)
4001 {
4002 /*
4003 * do the vertical scroll
4004 */
4005 if (want_ver)
4006 {
4007#ifdef FEAT_DIFF
4008 if (old_curwin->w_p_diff && curwin->w_p_diff)
4009 {
4010 diff_set_topline(old_curwin, curwin);
4011 }
4012 else
4013#endif
4014 {
4015 curwin->w_scbind_pos += topline_diff;
4016 topline = curwin->w_scbind_pos;
4017 if (topline > curbuf->b_ml.ml_line_count)
4018 topline = curbuf->b_ml.ml_line_count;
4019 if (topline < 1)
4020 topline = 1;
4021
4022 y = topline - curwin->w_topline;
4023 if (y > 0)
4024 scrollup(y, FALSE);
4025 else
4026 scrolldown(-y, FALSE);
4027 }
4028
4029 redraw_later(VALID);
4030 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 }
4033
4034 /*
4035 * do the horizontal scroll
4036 */
4037 if (want_hor && curwin->w_leftcol != tgt_leftcol)
4038 {
4039 curwin->w_leftcol = tgt_leftcol;
4040 leftcol_changed();
4041 }
4042 }
4043 }
4044
4045 /*
4046 * reset current-window
4047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 VIsual_select = old_VIsual_select;
4049 VIsual_active = old_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 curwin = old_curwin;
4051 curbuf = old_curbuf;
4052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054/*
4055 * Command character that's ignored.
4056 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02004057 * xon/xoff.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004060nv_ignore(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
Bram Moolenaarfc735152005-03-22 22:54:12 +00004062 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063}
4064
4065/*
Bram Moolenaarebefac62005-12-28 22:39:57 +00004066 * Command character that doesn't do anything, but unlike nv_ignore() does
4067 * start edit(). Used for "startinsert" executed while starting up.
4068 */
Bram Moolenaarebefac62005-12-28 22:39:57 +00004069 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004070nv_nop(cmdarg_T *cap UNUSED)
Bram Moolenaarebefac62005-12-28 22:39:57 +00004071{
4072}
4073
4074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * Command character doesn't exist.
4076 */
4077 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004078nv_error(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079{
4080 clearopbeep(cap->oap);
4081}
4082
4083/*
4084 * <Help> and <F1> commands.
4085 */
4086 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004087nv_help(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088{
4089 if (!checkclearopq(cap->oap))
4090 ex_help(NULL);
4091}
4092
4093/*
4094 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4095 */
4096 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004097nv_addsub(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
Bram Moolenaarf2732452018-06-03 14:47:35 +02004099#ifdef FEAT_JOB_CHANNEL
4100 if (bt_prompt(curbuf) && !prompt_curpos_editable())
4101 clearopbeep(cap->oap);
4102 else
4103#endif
Bram Moolenaard79e5502016-01-10 22:13:02 +01004104 if (!VIsual_active && cap->oap->op_type == OP_NOP)
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004105 {
Bram Moolenaaref2b5032016-01-12 22:20:58 +01004106 prep_redo_cmd(cap);
Bram Moolenaard79e5502016-01-10 22:13:02 +01004107 cap->oap->op_type = cap->cmdchar == Ctrl_A ? OP_NR_ADD : OP_NR_SUB;
4108 op_addsub(cap->oap, cap->count1, cap->arg);
4109 cap->oap->op_type = OP_NOP;
Bram Moolenaar9bb19302015-07-03 12:44:07 +02004110 }
Bram Moolenaard79e5502016-01-10 22:13:02 +01004111 else if (VIsual_active)
4112 nv_operator(cap);
Bram Moolenaar3a304b22015-06-25 13:57:36 +02004113 else
Bram Moolenaard79e5502016-01-10 22:13:02 +01004114 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115}
4116
4117/*
4118 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4119 */
4120 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004121nv_page(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122{
4123 if (!checkclearop(cap->oap))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004124 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004125 if (mod_mask & MOD_MASK_CTRL)
4126 {
4127 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4128 if (cap->arg == BACKWARD)
4129 goto_tabpage(-(int)cap->count1);
4130 else
4131 goto_tabpage((int)cap->count0);
4132 }
4133 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004134 (void)onepage(cap->arg, cap->count1);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136}
4137
4138/*
4139 * Implementation of "gd" and "gD" command.
4140 */
4141 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004142nv_gd(
4143 oparg_T *oap,
4144 int nchar,
4145 int thisblock) /* 1 for "1gd" and "1gD" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
4147 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 char_u *ptr;
4149
Bram Moolenaard9d30582005-05-18 22:10:28 +00004150 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
Bram Moolenaar1538fc32016-04-16 09:13:34 +02004151 || find_decl(ptr, len, nchar == 'd', thisblock, SEARCH_START)
4152 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 clearopbeep(oap);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004154#ifdef FEAT_FOLDING
4155 else if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP)
4156 foldOpenCursor();
4157#endif
4158}
4159
4160/*
Bram Moolenaar226630a2016-10-08 19:21:31 +02004161 * Return TRUE if line[offset] is not inside a C-style comment or string, FALSE
4162 * otherwise.
4163 */
4164 static int
4165is_ident(char_u *line, int offset)
4166{
4167 int i;
4168 int incomment = FALSE;
4169 int instring = 0;
4170 int prev = 0;
4171
4172 for (i = 0; i < offset && line[i] != NUL; i++)
4173 {
4174 if (instring != 0)
4175 {
4176 if (prev != '\\' && line[i] == instring)
4177 instring = 0;
4178 }
4179 else if ((line[i] == '"' || line[i] == '\'') && !incomment)
4180 {
4181 instring = line[i];
4182 }
4183 else
4184 {
4185 if (incomment)
4186 {
4187 if (prev == '*' && line[i] == '/')
4188 incomment = FALSE;
4189 }
4190 else if (prev == '/' && line[i] == '*')
4191 {
4192 incomment = TRUE;
4193 }
4194 else if (prev == '/' && line[i] == '/')
4195 {
4196 return FALSE;
4197 }
4198 }
4199
4200 prev = line[i];
4201 }
4202
4203 return incomment == FALSE && instring == 0;
4204}
4205
4206/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004207 * Search for variable declaration of "ptr[len]".
4208 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4209 * current file ("gD").
4210 * When "thisblock" is TRUE check the {} block scope.
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004211 * Return FAIL when not found.
4212 */
4213 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004214find_decl(
4215 char_u *ptr,
4216 int len,
4217 int locally,
4218 int thisblock,
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004219 int flags_arg) /* flags passed to searchit() */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004220{
4221 char_u *pat;
4222 pos_T old_pos;
4223 pos_T par_pos;
4224 pos_T found_pos;
4225 int t;
4226 int save_p_ws;
4227 int save_p_scs;
4228 int retval = OK;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004229 int incll;
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004230 int searchflags = flags_arg;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004231 int valid;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004232
4233 if ((pat = alloc(len + 7)) == NULL)
4234 return FAIL;
Bram Moolenaard9d30582005-05-18 22:10:28 +00004235
4236 /* Put "\V" before the pattern to avoid that the special meaning of "."
4237 * and "~" causes trouble. */
4238 sprintf((char *)pat, vim_iswordp(ptr) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4239 len, ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 old_pos = curwin->w_cursor;
4241 save_p_ws = p_ws;
4242 save_p_scs = p_scs;
4243 p_ws = FALSE; /* don't wrap around end of file now */
4244 p_scs = FALSE; /* don't switch ignorecase off now */
4245
4246 /*
4247 * With "gD" go to line 1.
4248 * With "gd" Search back for the start of the current function, then go
4249 * back until a blank line. If this fails go to line 1.
4250 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00004251 if (!locally || !findpar(&incll, BACKWARD, 1L, '{', FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
4253 setpcmark(); /* Set in findpar() otherwise */
4254 curwin->w_cursor.lnum = 1;
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004255 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257 else
4258 {
Bram Moolenaarbb15b652005-10-03 21:52:09 +00004259 par_pos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 while (curwin->w_cursor.lnum > 1 && *skipwhite(ml_get_curline()) != NUL)
4261 --curwin->w_cursor.lnum;
4262 }
4263 curwin->w_cursor.col = 0;
4264
4265 /* Search forward for the identifier, ignore comment lines. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004266 CLEAR_POS(&found_pos);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004267 for (;;)
4268 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004269 t = searchit(curwin, curbuf, &curwin->w_cursor, NULL, FORWARD,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004270 pat, 1L, searchflags, RE_LAST, (linenr_T)0, NULL, NULL);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004271 if (curwin->w_cursor.lnum >= old_pos.lnum)
4272 t = FAIL; /* match after start is failure too */
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004273
Bram Moolenaar0fd92892006-03-09 22:27:48 +00004274 if (thisblock && t != FAIL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004275 {
4276 pos_T *pos;
4277
4278 /* Check that the block the match is in doesn't end before the
4279 * position where we started the search from. */
4280 if ((pos = findmatchlimit(NULL, '}', FM_FORWARD,
4281 (int)(old_pos.lnum - curwin->w_cursor.lnum + 1))) != NULL
4282 && pos->lnum < old_pos.lnum)
Bram Moolenaar60402d62017-04-20 18:54:50 +02004283 {
4284 /* There can't be a useful match before the end of this block.
4285 * Skip to the end. */
4286 curwin->w_cursor = *pos;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004287 continue;
Bram Moolenaar60402d62017-04-20 18:54:50 +02004288 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004289 }
4290
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004291 if (t == FAIL)
4292 {
4293 /* If we previously found a valid position, use it. */
4294 if (found_pos.lnum != 0)
4295 {
4296 curwin->w_cursor = found_pos;
4297 t = OK;
4298 }
4299 break;
4300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#ifdef FEAT_COMMENTS
Bram Moolenaar81340392012-06-06 16:12:59 +02004302 if (get_leader_len(ml_get_curline(), NULL, FALSE, TRUE) > 0)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004303 {
4304 /* Ignore this line, continue at start of next line. */
4305 ++curwin->w_cursor.lnum;
4306 curwin->w_cursor.col = 0;
4307 continue;
4308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309#endif
Bram Moolenaar226630a2016-10-08 19:21:31 +02004310 valid = is_ident(ml_get_curline(), curwin->w_cursor.col);
4311
4312 /* If the current position is not a valid identifier and a previous
4313 * match is present, favor that one instead. */
4314 if (!valid && found_pos.lnum != 0)
4315 {
4316 curwin->w_cursor = found_pos;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004317 break;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004318 }
4319
4320 /* Global search: use first valid match found */
4321 if (valid && !locally)
4322 break;
4323 if (valid && curwin->w_cursor.lnum >= par_pos.lnum)
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004324 {
4325 /* If we previously found a valid position, use it. */
4326 if (found_pos.lnum != 0)
4327 curwin->w_cursor = found_pos;
4328 break;
4329 }
4330
Bram Moolenaar226630a2016-10-08 19:21:31 +02004331 /* For finding a local variable and the match is before the "{" or
4332 * inside a comment, continue searching. For K&R style function
4333 * declarations this skips the function header without types. */
4334 if (!valid)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004335 CLEAR_POS(&found_pos);
Bram Moolenaar226630a2016-10-08 19:21:31 +02004336 else
Bram Moolenaar226630a2016-10-08 19:21:31 +02004337 found_pos = curwin->w_cursor;
Bram Moolenaar226630a2016-10-08 19:21:31 +02004338 /* Remove SEARCH_START from flags to avoid getting stuck at one
4339 * position. */
Bram Moolenaar23c60f22016-06-15 22:03:48 +02004340 searchflags &= ~SEARCH_START;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004342
4343 if (t == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004345 retval = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 curwin->w_cursor = old_pos;
4347 }
4348 else
4349 {
4350 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 /* "n" searches forward now */
4352 reset_search_dir();
4353 }
4354
4355 vim_free(pat);
4356 p_ws = save_p_ws;
4357 p_scs = save_p_scs;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004358
4359 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360}
4361
4362/*
4363 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4364 * lines rather than lines in the file.
4365 * 'dist' must be positive.
4366 *
4367 * Return OK if able to move cursor, FAIL otherwise.
4368 */
4369 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004370nv_screengo(oparg_T *oap, int dir, long dist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
4372 int linelen = linetabsize(ml_get_curline());
4373 int retval = OK;
4374 int atend = FALSE;
4375 int n;
4376 int col_off1; /* margin offset for first screen line */
4377 int col_off2; /* margin offset for wrapped screen line */
4378 int width1; /* text width for first screen line */
4379 int width2; /* test width for wrapped screen line */
4380
4381 oap->motion_type = MCHAR;
Bram Moolenaar91b2bdb2013-07-14 13:32:15 +02004382 oap->inclusive = (curwin->w_curswant == MAXCOL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
4384 col_off1 = curwin_col_off();
4385 col_off2 = col_off1 - curwin_col_off2();
Bram Moolenaar02631462017-09-22 15:20:32 +02004386 width1 = curwin->w_width - col_off1;
4387 width2 = curwin->w_width - col_off2;
Bram Moolenaar7cc8ec42015-01-27 20:59:31 +01004388 if (width2 == 0)
4389 width2 = 1; /* avoid divide by zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 if (curwin->w_width != 0)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 /*
4394 * Instead of sticking at the last character of the buffer line we
4395 * try to stick in the last column of the screen.
4396 */
4397 if (curwin->w_curswant == MAXCOL)
4398 {
4399 atend = TRUE;
4400 validate_virtcol();
4401 if (width1 <= 0)
4402 curwin->w_curswant = 0;
4403 else
4404 {
4405 curwin->w_curswant = width1 - 1;
4406 if (curwin->w_virtcol > curwin->w_curswant)
4407 curwin->w_curswant += ((curwin->w_virtcol
4408 - curwin->w_curswant - 1) / width2 + 1) * width2;
4409 }
4410 }
4411 else
4412 {
4413 if (linelen > width1)
4414 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4415 else
4416 n = width1;
4417 if (curwin->w_curswant > (colnr_T)n + 1)
4418 curwin->w_curswant -= ((curwin->w_curswant - n) / width2 + 1)
4419 * width2;
4420 }
4421
4422 while (dist--)
4423 {
4424 if (dir == BACKWARD)
4425 {
4426 if ((long)curwin->w_curswant >= width2)
4427 /* move back within line */
4428 curwin->w_curswant -= width2;
4429 else
4430 {
4431 /* to previous line */
4432 if (curwin->w_cursor.lnum == 1)
4433 {
4434 retval = FAIL;
4435 break;
4436 }
4437 --curwin->w_cursor.lnum;
4438#ifdef FEAT_FOLDING
4439 /* Move to the start of a closed fold. Don't do that when
4440 * 'foldopen' contains "all": it will open in a moment. */
4441 if (!(fdo_flags & FDO_ALL))
4442 (void)hasFolding(curwin->w_cursor.lnum,
4443 &curwin->w_cursor.lnum, NULL);
4444#endif
4445 linelen = linetabsize(ml_get_curline());
4446 if (linelen > width1)
4447 curwin->w_curswant += (((linelen - width1 - 1) / width2)
4448 + 1) * width2;
4449 }
4450 }
4451 else /* dir == FORWARD */
4452 {
4453 if (linelen > width1)
4454 n = ((linelen - width1 - 1) / width2 + 1) * width2 + width1;
4455 else
4456 n = width1;
4457 if (curwin->w_curswant + width2 < (colnr_T)n)
4458 /* move forward within line */
4459 curwin->w_curswant += width2;
4460 else
4461 {
4462 /* to next line */
4463#ifdef FEAT_FOLDING
4464 /* Move to the end of a closed fold. */
4465 (void)hasFolding(curwin->w_cursor.lnum, NULL,
4466 &curwin->w_cursor.lnum);
4467#endif
4468 if (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
4469 {
4470 retval = FAIL;
4471 break;
4472 }
4473 curwin->w_cursor.lnum++;
4474 curwin->w_curswant %= width2;
Bram Moolenaar914968e2011-06-20 00:45:58 +02004475 linelen = linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 }
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaar6cd3aee2014-01-14 13:18:58 +01004481 if (virtual_active() && atend)
4482 coladvance(MAXCOL);
4483 else
4484 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
4487 {
Bram Moolenaar773b1582014-08-29 14:20:51 +02004488 colnr_T virtcol;
4489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 /*
4491 * Check for landing on a character that got split at the end of the
4492 * last line. We want to advance a screenline, not end up in the same
4493 * screenline or move two screenlines.
4494 */
4495 validate_virtcol();
Bram Moolenaar773b1582014-08-29 14:20:51 +02004496 virtcol = curwin->w_virtcol;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004497#if defined(FEAT_LINEBREAK)
Bram Moolenaar773b1582014-08-29 14:20:51 +02004498 if (virtcol > (colnr_T)width1 && *p_sbr != NUL)
4499 virtcol -= vim_strsize(p_sbr);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004500#endif
Bram Moolenaar773b1582014-08-29 14:20:51 +02004501
4502 if (virtcol > curwin->w_curswant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 && (curwin->w_curswant < (colnr_T)width1
4504 ? (curwin->w_curswant > (colnr_T)width1 / 2)
4505 : ((curwin->w_curswant - width1) % width2
4506 > (colnr_T)width2 / 2)))
4507 --curwin->w_cursor.col;
4508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 if (atend)
4511 curwin->w_curswant = MAXCOL; /* stick in the last column */
4512
4513 return retval;
4514}
4515
4516#ifdef FEAT_MOUSE
4517/*
4518 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4519 * when Shift or Ctrl is used.
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004520 * K_MOUSEUP (cap->arg == 1) or K_MOUSEDOWN (cap->arg == 0) or
4521 * K_MOUSELEFT (cap->arg == -1) or K_MOUSERIGHT (cap->arg == -2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
4523 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004524nv_mousescroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004526 win_T *old_curwin = curwin, *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01004528 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 int row, col;
4531
4532 row = mouse_row;
4533 col = mouse_col;
4534
4535 /* find the window at the pointer coordinates */
Bram Moolenaar68acb412019-06-26 03:40:36 +02004536 wp = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004537 if (wp == NULL)
4538 return;
Bram Moolenaar68acb412019-06-26 03:40:36 +02004539#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004540 if (WIN_IS_POPUP(wp) && !wp->w_has_scrollbar)
Bram Moolenaar68acb412019-06-26 03:40:36 +02004541 return;
4542#endif
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004543 curwin = wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 curbuf = curwin->w_buffer;
4545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004547 if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004549# ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02004550 if (term_use_loop())
Bram Moolenaar73675fb2017-11-20 21:49:19 +01004551 /* This window is a terminal window, send the mouse event there.
4552 * Set "typed" to FALSE to avoid an endless loop. */
4553 send_keys_to_term(curbuf->b_term, cap->cmdchar, FALSE);
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02004554 else
4555# endif
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004556 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
4557 {
4558 (void)onepage(cap->arg ? FORWARD : BACKWARD, 1L);
4559 }
4560 else
4561 {
Bram Moolenaar68acb412019-06-26 03:40:36 +02004562 // Don't scroll more than half the window height.
4563 if (curwin->w_height < 6)
4564 {
4565 cap->count1 = curwin->w_height / 2;
4566 if (cap->count1 == 0)
4567 cap->count1 = 1;
4568 }
4569 else
4570 cap->count1 = 3;
4571 cap->count0 = cap->count1;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004572 nv_scroll_line(cap);
4573 }
Bram Moolenaar68acb412019-06-26 03:40:36 +02004574#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004575 if (WIN_IS_POPUP(curwin))
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02004576 popup_set_firstline(curwin);
Bram Moolenaar68acb412019-06-26 03:40:36 +02004577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004579# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 else
4581 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004582 /* Horizontal scroll - only allowed when 'wrap' is disabled */
4583 if (!curwin->w_p_wrap)
4584 {
4585 int val, step = 6;
Bram Moolenaar5e109c42010-07-26 22:51:28 +02004586
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004587 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
Bram Moolenaar02631462017-09-22 15:20:32 +02004588 step = curwin->w_width;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004589 val = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step);
4590 if (val < 0)
4591 val = 0;
4592
4593 gui_do_horiz_scroll(val, TRUE);
4594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02004596# endif
Bram Moolenaarbbb5f8d2019-01-31 13:22:32 +01004597# ifdef FEAT_SYN_HL
4598 if (curwin != old_curwin && curwin->w_p_cul)
4599 redraw_for_cursorline(curwin);
4600# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 curwin->w_redr_status = TRUE;
4603
4604 curwin = old_curwin;
4605 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606}
4607
4608/*
4609 * Mouse clicks and drags.
4610 */
4611 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004612nv_mouse(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613{
4614 (void)do_mouse(cap->oap, cap->cmdchar, BACKWARD, cap->count1, 0);
4615}
4616#endif
4617
4618/*
4619 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4620 * cap->arg must be TRUE for CTRL-E.
4621 */
4622 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004623nv_scroll_line(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624{
4625 if (!checkclearop(cap->oap))
4626 scroll_redraw(cap->arg, cap->count1);
4627}
4628
4629/*
4630 * Scroll "count" lines up or down, and redraw.
4631 */
4632 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004633scroll_redraw(int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 linenr_T prev_topline = curwin->w_topline;
4636#ifdef FEAT_DIFF
4637 int prev_topfill = curwin->w_topfill;
4638#endif
4639 linenr_T prev_lnum = curwin->w_cursor.lnum;
4640
4641 if (up)
4642 scrollup(count, TRUE);
4643 else
4644 scrolldown(count, TRUE);
Bram Moolenaar375e3392019-01-31 18:26:10 +01004645 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4648 * valid, otherwise the screen jumps back at the end of the file. */
4649 cursor_correct();
4650 check_cursor_moved(curwin);
4651 curwin->w_valid |= VALID_TOPLINE;
4652
4653 /* If moved back to where we were, at least move the cursor, otherwise
4654 * we get stuck at one position. Don't move the cursor up if the
4655 * first line of the buffer is already on the screen */
4656 while (curwin->w_topline == prev_topline
4657#ifdef FEAT_DIFF
4658 && curwin->w_topfill == prev_topfill
4659#endif
4660 )
4661 {
4662 if (up)
4663 {
4664 if (curwin->w_cursor.lnum > prev_lnum
4665 || cursor_down(1L, FALSE) == FAIL)
4666 break;
4667 }
4668 else
4669 {
4670 if (curwin->w_cursor.lnum < prev_lnum
4671 || prev_topline == 1L
4672 || cursor_up(1L, FALSE) == FAIL)
4673 break;
4674 }
4675 /* Mark w_topline as valid, otherwise the screen jumps back at the
4676 * end of the file. */
4677 check_cursor_moved(curwin);
4678 curwin->w_valid |= VALID_TOPLINE;
4679 }
4680 }
4681 if (curwin->w_cursor.lnum != prev_lnum)
4682 coladvance(curwin->w_curswant);
4683 redraw_later(VALID);
4684}
4685
4686/*
4687 * Commands that start with "z".
4688 */
4689 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004690nv_zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692 long n;
4693 colnr_T col;
4694 int nchar = cap->nchar;
4695#ifdef FEAT_FOLDING
4696 long old_fdl = curwin->w_p_fdl;
4697 int old_fen = curwin->w_p_fen;
4698#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004699#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00004700 int undo = FALSE;
4701#endif
Bram Moolenaar375e3392019-01-31 18:26:10 +01004702 long siso = get_sidescrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703
4704 if (VIM_ISDIGIT(nchar))
4705 {
4706 /*
4707 * "z123{nchar}": edit the count before obtaining {nchar}
4708 */
4709 if (checkclearop(cap->oap))
4710 return;
4711 n = nchar - '0';
4712 for (;;)
4713 {
4714#ifdef USE_ON_FLY_SCROLL
4715 dont_scroll = TRUE; /* disallow scrolling here */
4716#endif
4717 ++no_mapping;
4718 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00004719 nchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 --no_mapping;
4722 --allow_keys;
4723#ifdef FEAT_CMDL_INFO
4724 (void)add_to_showcmd(nchar);
4725#endif
4726 if (nchar == K_DEL || nchar == K_KDEL)
4727 n /= 10;
4728 else if (VIM_ISDIGIT(nchar))
4729 n = n * 10 + (nchar - '0');
4730 else if (nchar == CAR)
4731 {
4732#ifdef FEAT_GUI
4733 need_mouse_correct = TRUE;
4734#endif
4735 win_setheight((int)n);
4736 break;
4737 }
4738 else if (nchar == 'l'
4739 || nchar == 'h'
4740 || nchar == K_LEFT
Bram Moolenaara88d9682005-03-25 21:45:43 +00004741 || nchar == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
4743 cap->count1 = n ? n * cap->count1 : cap->count1;
4744 goto dozet;
4745 }
4746 else
4747 {
4748 clearopbeep(cap->oap);
4749 break;
4750 }
4751 }
4752 cap->oap->op_type = OP_NOP;
4753 return;
4754 }
4755
4756dozet:
4757 if (
4758#ifdef FEAT_FOLDING
4759 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4760 * and "zC" only in Visual mode. "zj" and "zk" are motion
4761 * commands. */
4762 cap->nchar != 'f' && cap->nchar != 'F'
4763 && !(VIsual_active && vim_strchr((char_u *)"dcCoO", cap->nchar))
4764 && cap->nchar != 'j' && cap->nchar != 'k'
4765 &&
4766#endif
4767 checkclearop(cap->oap))
4768 return;
4769
4770 /*
4771 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4772 * If line number given, set cursor.
4773 */
4774 if ((vim_strchr((char_u *)"+\r\nt.z^-b", nchar) != NULL)
4775 && cap->count0
4776 && cap->count0 != curwin->w_cursor.lnum)
4777 {
4778 setpcmark();
4779 if (cap->count0 > curbuf->b_ml.ml_line_count)
4780 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4781 else
4782 curwin->w_cursor.lnum = cap->count0;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004783 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785
4786 switch (nchar)
4787 {
4788 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4789 case '+':
4790 if (cap->count0 == 0)
4791 {
4792 /* No count given: put cursor at the line below screen */
4793 validate_botline(); /* make sure w_botline is valid */
4794 if (curwin->w_botline > curbuf->b_ml.ml_line_count)
4795 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
4796 else
4797 curwin->w_cursor.lnum = curwin->w_botline;
4798 }
4799 /* FALLTHROUGH */
4800 case NL:
4801 case CAR:
4802 case K_KENTER:
4803 beginline(BL_WHITE | BL_FIX);
4804 /* FALLTHROUGH */
4805
4806 case 't': scroll_cursor_top(0, TRUE);
4807 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004808 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 break;
4810
4811 /* "z." and "zz": put cursor in middle of screen */
4812 case '.': beginline(BL_WHITE | BL_FIX);
4813 /* FALLTHROUGH */
4814
4815 case 'z': scroll_cursor_halfway(TRUE);
4816 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004817 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 break;
4819
4820 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4821 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4822 * when <count> is at bottom of window, and puts that one at
4823 * bottom of window. */
4824 if (cap->count0 != 0)
4825 {
4826 scroll_cursor_bot(0, TRUE);
4827 curwin->w_cursor.lnum = curwin->w_topline;
4828 }
4829 else if (curwin->w_topline == 1)
4830 curwin->w_cursor.lnum = 1;
4831 else
4832 curwin->w_cursor.lnum = curwin->w_topline - 1;
4833 /* FALLTHROUGH */
4834 case '-':
4835 beginline(BL_WHITE | BL_FIX);
4836 /* FALLTHROUGH */
4837
4838 case 'b': scroll_cursor_bot(0, TRUE);
4839 redraw_later(VALID);
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01004840 set_fraction(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 break;
4842
4843 /* "zH" - scroll screen right half-page */
4844 case 'H':
Bram Moolenaar02631462017-09-22 15:20:32 +02004845 cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 /* FALLTHROUGH */
4847
4848 /* "zh" - scroll screen to the right */
4849 case 'h':
4850 case K_LEFT:
4851 if (!curwin->w_p_wrap)
4852 {
4853 if ((colnr_T)cap->count1 > curwin->w_leftcol)
4854 curwin->w_leftcol = 0;
4855 else
4856 curwin->w_leftcol -= (colnr_T)cap->count1;
4857 leftcol_changed();
4858 }
4859 break;
4860
4861 /* "zL" - scroll screen left half-page */
Bram Moolenaar02631462017-09-22 15:20:32 +02004862 case 'L': cap->count1 *= curwin->w_width / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 /* FALLTHROUGH */
4864
4865 /* "zl" - scroll screen to the left */
4866 case 'l':
4867 case K_RIGHT:
4868 if (!curwin->w_p_wrap)
4869 {
4870 /* scroll the window left */
4871 curwin->w_leftcol += (colnr_T)cap->count1;
4872 leftcol_changed();
4873 }
4874 break;
4875
4876 /* "zs" - scroll screen, cursor at the start */
4877 case 's': if (!curwin->w_p_wrap)
4878 {
4879#ifdef FEAT_FOLDING
4880 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4881 col = 0; /* like the cursor is in col 0 */
4882 else
4883#endif
4884 getvcol(curwin, &curwin->w_cursor, &col, NULL, NULL);
Bram Moolenaar375e3392019-01-31 18:26:10 +01004885 if ((long)col > siso)
4886 col -= siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 else
4888 col = 0;
4889 if (curwin->w_leftcol != col)
4890 {
4891 curwin->w_leftcol = col;
4892 redraw_later(NOT_VALID);
4893 }
4894 }
4895 break;
4896
4897 /* "ze" - scroll screen, cursor at the end */
4898 case 'e': if (!curwin->w_p_wrap)
4899 {
4900#ifdef FEAT_FOLDING
4901 if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4902 col = 0; /* like the cursor is in col 0 */
4903 else
4904#endif
4905 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &col);
Bram Moolenaar02631462017-09-22 15:20:32 +02004906 n = curwin->w_width - curwin_col_off();
Bram Moolenaar375e3392019-01-31 18:26:10 +01004907 if ((long)col + siso < n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 col = 0;
4909 else
Bram Moolenaar375e3392019-01-31 18:26:10 +01004910 col = col + siso - n + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (curwin->w_leftcol != col)
4912 {
4913 curwin->w_leftcol = col;
4914 redraw_later(NOT_VALID);
4915 }
4916 }
4917 break;
4918
4919#ifdef FEAT_FOLDING
4920 /* "zF": create fold command */
4921 /* "zf": create fold operator */
4922 case 'F':
4923 case 'f': if (foldManualAllowed(TRUE))
4924 {
4925 cap->nchar = 'f';
4926 nv_operator(cap);
4927 curwin->w_p_fen = TRUE;
4928
4929 /* "zF" is like "zfzf" */
4930 if (nchar == 'F' && cap->oap->op_type == OP_FOLD)
4931 {
4932 nv_operator(cap);
4933 finish_op = TRUE;
4934 }
4935 }
4936 else
4937 clearopbeep(cap->oap);
4938 break;
4939
4940 /* "zd": delete fold at cursor */
4941 /* "zD": delete fold at cursor recursively */
4942 case 'd':
4943 case 'D': if (foldManualAllowed(FALSE))
4944 {
4945 if (VIsual_active)
4946 nv_operator(cap);
4947 else
4948 deleteFold(curwin->w_cursor.lnum,
4949 curwin->w_cursor.lnum, nchar == 'D', FALSE);
4950 }
4951 break;
4952
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004953 /* "zE": erase all folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 case 'E': if (foldmethodIsManual(curwin))
4955 {
4956 clearFolding(curwin);
4957 changed_window_setting();
4958 }
4959 else if (foldmethodIsMarker(curwin))
4960 deleteFold((linenr_T)1, curbuf->b_ml.ml_line_count,
4961 TRUE, FALSE);
4962 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004963 emsg(_("E352: Cannot erase folds with current 'foldmethod'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 break;
4965
4966 /* "zn": fold none: reset 'foldenable' */
4967 case 'n': curwin->w_p_fen = FALSE;
4968 break;
4969
4970 /* "zN": fold Normal: set 'foldenable' */
4971 case 'N': curwin->w_p_fen = TRUE;
4972 break;
4973
4974 /* "zi": invert folding: toggle 'foldenable' */
4975 case 'i': curwin->w_p_fen = !curwin->w_p_fen;
4976 break;
4977
4978 /* "za": open closed fold or close open fold at cursor */
4979 case 'a': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4980 openFold(curwin->w_cursor.lnum, cap->count1);
4981 else
4982 {
4983 closeFold(curwin->w_cursor.lnum, cap->count1);
4984 curwin->w_p_fen = TRUE;
4985 }
4986 break;
4987
4988 /* "zA": open fold at cursor recursively */
4989 case 'A': if (hasFolding(curwin->w_cursor.lnum, NULL, NULL))
4990 openFoldRecurse(curwin->w_cursor.lnum);
4991 else
4992 {
4993 closeFoldRecurse(curwin->w_cursor.lnum);
4994 curwin->w_p_fen = TRUE;
4995 }
4996 break;
4997
4998 /* "zo": open fold at cursor or Visual area */
4999 case 'o': if (VIsual_active)
5000 nv_operator(cap);
5001 else
5002 openFold(curwin->w_cursor.lnum, cap->count1);
5003 break;
5004
5005 /* "zO": open fold recursively */
5006 case 'O': if (VIsual_active)
5007 nv_operator(cap);
5008 else
5009 openFoldRecurse(curwin->w_cursor.lnum);
5010 break;
5011
5012 /* "zc": close fold at cursor or Visual area */
5013 case 'c': if (VIsual_active)
5014 nv_operator(cap);
5015 else
5016 closeFold(curwin->w_cursor.lnum, cap->count1);
5017 curwin->w_p_fen = TRUE;
5018 break;
5019
5020 /* "zC": close fold recursively */
5021 case 'C': if (VIsual_active)
5022 nv_operator(cap);
5023 else
5024 closeFoldRecurse(curwin->w_cursor.lnum);
5025 curwin->w_p_fen = TRUE;
5026 break;
5027
5028 /* "zv": open folds at the cursor */
5029 case 'v': foldOpenCursor();
5030 break;
5031
5032 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
5033 case 'x': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005034 curwin->w_foldinvalid = TRUE; /* recompute folds */
5035 newFoldLevel(); /* update right now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 foldOpenCursor();
5037 break;
5038
5039 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
5040 case 'X': curwin->w_p_fen = TRUE;
Bram Moolenaar38ab0e22010-05-13 17:35:59 +02005041 curwin->w_foldinvalid = TRUE; /* recompute folds */
5042 old_fdl = -1; /* force an update */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 break;
5044
5045 /* "zm": fold more */
5046 case 'm': if (curwin->w_p_fdl > 0)
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005047 {
5048 curwin->w_p_fdl -= cap->count1;
5049 if (curwin->w_p_fdl < 0)
5050 curwin->w_p_fdl = 0;
5051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 old_fdl = -1; /* force an update */
5053 curwin->w_p_fen = TRUE;
5054 break;
5055
5056 /* "zM": close all folds */
5057 case 'M': curwin->w_p_fdl = 0;
5058 old_fdl = -1; /* force an update */
5059 curwin->w_p_fen = TRUE;
5060 break;
5061
5062 /* "zr": reduce folding */
Bram Moolenaar7d2757a2015-03-31 17:46:22 +02005063 case 'r': curwin->w_p_fdl += cap->count1;
5064 {
5065 int d = getDeepestNesting();
5066
5067 if (curwin->w_p_fdl >= d)
5068 curwin->w_p_fdl = d;
5069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 break;
5071
5072 /* "zR": open all folds */
5073 case 'R': curwin->w_p_fdl = getDeepestNesting();
5074 old_fdl = -1; /* force an update */
5075 break;
5076
5077 case 'j': /* "zj" move to next fold downwards */
5078 case 'k': /* "zk" move to next fold upwards */
5079 if (foldMoveTo(TRUE, nchar == 'j' ? FORWARD : BACKWARD,
5080 cap->count1) == FAIL)
5081 clearopbeep(cap->oap);
5082 break;
5083
5084#endif /* FEAT_FOLDING */
5085
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005086#ifdef FEAT_SPELL
Bram Moolenaard0131a82006-03-04 21:46:13 +00005087 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
5088 ++no_mapping;
5089 ++allow_keys; /* no mapping for nchar, but allow key codes */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005090 nchar = plain_vgetc();
Bram Moolenaard0131a82006-03-04 21:46:13 +00005091 LANGMAP_ADJUST(nchar, TRUE);
Bram Moolenaard0131a82006-03-04 21:46:13 +00005092 --no_mapping;
5093 --allow_keys;
5094#ifdef FEAT_CMDL_INFO
5095 (void)add_to_showcmd(nchar);
5096#endif
5097 if (vim_strchr((char_u *)"gGwW", nchar) == NULL)
5098 {
5099 clearopbeep(cap->oap);
5100 break;
5101 }
5102 undo = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005103 /* FALLTHROUGH */
Bram Moolenaard0131a82006-03-04 21:46:13 +00005104
Bram Moolenaarb765d632005-06-07 21:00:02 +00005105 case 'g': /* "zg": add good word to word list */
5106 case 'w': /* "zw": add wrong word to word list */
Bram Moolenaar7887d882005-07-01 22:33:52 +00005107 case 'G': /* "zG": add good word to temp word list */
5108 case 'W': /* "zW": add wrong word to temp word list */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005109 {
5110 char_u *ptr = NULL;
5111 int len;
5112
5113 if (checkclearop(cap->oap))
5114 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005115 if (VIsual_active && get_visual_text(cap, &ptr, &len)
5116 == FAIL)
5117 return;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005118 if (ptr == NULL)
5119 {
5120 pos_T pos = curwin->w_cursor;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005121
Bram Moolenaar134bf072013-09-25 18:54:24 +02005122 /* Find bad word under the cursor. When 'spell' is
5123 * off this fails and find_ident_under_cursor() is
5124 * used below. */
5125 emsg_off++;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005126 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar134bf072013-09-25 18:54:24 +02005127 emsg_off--;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00005128 if (len != 0 && curwin->w_cursor.col <= pos.col)
5129 ptr = ml_get_pos(&curwin->w_cursor);
5130 curwin->w_cursor = pos;
5131 }
5132
Bram Moolenaarb765d632005-06-07 21:00:02 +00005133 if (ptr == NULL && (len = find_ident_under_cursor(&ptr,
5134 FIND_IDENT)) == 0)
5135 return;
Bram Moolenaar08cc3742019-08-11 22:51:14 +02005136 spell_add_word(ptr, len, nchar == 'w' || nchar == 'W'
5137 ? SPELL_ADD_BAD : SPELL_ADD_GOOD,
Bram Moolenaard0131a82006-03-04 21:46:13 +00005138 (nchar == 'G' || nchar == 'W')
5139 ? 0 : (int)cap->count1,
5140 undo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005141 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005142 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005143
Bram Moolenaar43abc522005-12-10 20:15:02 +00005144 case '=': /* "z=": suggestions for a badly spelled word */
Bram Moolenaar66fa2712006-01-22 23:22:22 +00005145 if (!checkclearop(cap->oap))
Bram Moolenaard12a1322005-08-21 22:08:24 +00005146 spell_suggest((int)cap->count0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005147 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005148#endif
5149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 default: clearopbeep(cap->oap);
5151 }
5152
5153#ifdef FEAT_FOLDING
5154 /* Redraw when 'foldenable' changed */
5155 if (old_fen != curwin->w_p_fen)
5156 {
5157# ifdef FEAT_DIFF
5158 win_T *wp;
5159
5160 if (foldmethodIsDiff(curwin) && curwin->w_p_scb)
5161 {
5162 /* Adjust 'foldenable' in diff-synced windows. */
5163 FOR_ALL_WINDOWS(wp)
5164 {
5165 if (wp != curwin && foldmethodIsDiff(wp) && wp->w_p_scb)
5166 {
5167 wp->w_p_fen = curwin->w_p_fen;
5168 changed_window_setting_win(wp);
5169 }
5170 }
5171 }
5172# endif
5173 changed_window_setting();
5174 }
5175
5176 /* Redraw when 'foldlevel' changed. */
5177 if (old_fdl != curwin->w_p_fdl)
5178 newFoldLevel();
5179#endif
5180}
5181
5182#ifdef FEAT_GUI
5183/*
5184 * Vertical scrollbar movement.
5185 */
5186 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005187nv_ver_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 */
5193 gui_do_scroll();
5194}
5195
5196/*
5197 * Horizontal scrollbar movement.
5198 */
5199 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005200nv_hor_scrollbar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201{
5202 if (cap->oap->op_type != OP_NOP)
5203 clearopbeep(cap->oap);
5204
5205 /* Even if an operator was pending, we still want to scroll */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02005206 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207}
5208#endif
5209
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005210#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005211/*
5212 * Click in GUI tab.
5213 */
5214 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005215nv_tabline(cmdarg_T *cap)
Bram Moolenaar32466aa2006-02-24 23:53:04 +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. */
5221 goto_tabpage(current_tab);
5222}
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005223
5224/*
5225 * Selected item in tab line menu.
5226 */
5227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005228nv_tabmenu(cmdarg_T *cap)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005229{
5230 if (cap->oap->op_type != OP_NOP)
5231 clearopbeep(cap->oap);
5232
5233 /* Even if an operator was pending, we still want to jump tabs. */
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005234 handle_tabmenu();
5235}
5236
5237/*
5238 * Handle selecting an item of the GUI tab line menu.
5239 * Used in Normal and Insert mode.
5240 */
5241 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005242handle_tabmenu(void)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00005243{
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005244 switch (current_tabmenu)
5245 {
5246 case TABLINE_MENU_CLOSE:
5247 if (current_tab == 0)
5248 do_cmdline_cmd((char_u *)"tabclose");
5249 else
5250 {
5251 vim_snprintf((char *)IObuff, IOSIZE, "tabclose %d",
5252 current_tab);
5253 do_cmdline_cmd(IObuff);
5254 }
5255 break;
5256
5257 case TABLINE_MENU_NEW:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005258 if (current_tab == 0)
5259 do_cmdline_cmd((char_u *)"$tabnew");
5260 else
5261 {
5262 vim_snprintf((char *)IObuff, IOSIZE, "%dtabnew",
5263 current_tab - 1);
5264 do_cmdline_cmd(IObuff);
5265 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005266 break;
5267
5268 case TABLINE_MENU_OPEN:
Bram Moolenaardfd76912015-02-27 15:03:58 +01005269 if (current_tab == 0)
5270 do_cmdline_cmd((char_u *)"browse $tabnew");
5271 else
5272 {
5273 vim_snprintf((char *)IObuff, IOSIZE, "browse %dtabnew",
5274 current_tab - 1);
5275 do_cmdline_cmd(IObuff);
5276 }
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005277 break;
5278 }
5279}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005280#endif
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282/*
5283 * "Q" command.
5284 */
5285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286nv_exmode(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 /*
5289 * Ignore 'Q' in Visual mode, just give a beep.
5290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 if (VIsual_active)
Bram Moolenaar165bc692015-07-21 17:53:25 +02005292 vim_beep(BO_EX);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005293 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 do_exmode(FALSE);
5295}
5296
5297/*
5298 * Handle a ":" command.
5299 */
5300 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005301nv_colon(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302{
5303 int old_p_im;
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005304 int cmd_result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 if (VIsual_active)
5307 nv_operator(cap);
5308 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 if (cap->oap->op_type != OP_NOP)
5311 {
5312 /* Using ":" as a movement is characterwise exclusive. */
5313 cap->oap->motion_type = MCHAR;
5314 cap->oap->inclusive = FALSE;
5315 }
5316 else if (cap->count0)
5317 {
5318 /* translate "count:" into ":.,.+(count - 1)" */
5319 stuffcharReadbuff('.');
5320 if (cap->count0 > 1)
5321 {
5322 stuffReadbuff((char_u *)",.+");
5323 stuffnumReadbuff((long)cap->count0 - 1L);
5324 }
5325 }
5326
5327 /* When typing, don't type below an old message */
5328 if (KeyTyped)
5329 compute_cmdrow();
5330
5331 old_p_im = p_im;
5332
5333 /* get a command line and execute it */
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005334 cmd_result = do_cmdline(NULL, getexline, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 cap->oap->op_type != OP_NOP ? DOCMD_KEEPLINE : 0);
5336
5337 /* If 'insertmode' changed, enter or exit Insert mode */
5338 if (p_im != old_p_im)
5339 {
5340 if (p_im)
5341 restart_edit = 'i';
5342 else
5343 restart_edit = 0;
5344 }
5345
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005346 if (cmd_result == FAIL)
5347 /* The Ex command failed, do not execute the operator. */
5348 clearop(cap->oap);
5349 else if (cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 && (cap->oap->start.lnum > curbuf->b_ml.ml_line_count
5351 || cap->oap->start.col >
Bram Moolenaard7fbfe12013-04-05 17:43:14 +02005352 (colnr_T)STRLEN(ml_get(cap->oap->start.lnum))
5353 || did_emsg
5354 ))
5355 /* The start of the operator has become invalid by the Ex command.
5356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 clearopbeep(cap->oap);
5358 }
5359}
5360
5361/*
5362 * Handle CTRL-G command.
5363 */
5364 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005365nv_ctrlg(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 if (VIsual_active) /* toggle Selection/Visual mode */
5368 {
5369 VIsual_select = !VIsual_select;
5370 showmode();
5371 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005372 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 /* print full name if count given or :cd used */
5374 fileinfo((int)cap->count0, FALSE, TRUE);
5375}
5376
5377/*
5378 * Handle CTRL-H <Backspace> command.
5379 */
5380 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005381nv_ctrlh(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 if (VIsual_active && VIsual_select)
5384 {
5385 cap->cmdchar = 'x'; /* BS key behaves like 'x' in Select mode */
5386 v_visop(cap);
5387 }
5388 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 nv_left(cap);
5390}
5391
5392/*
5393 * CTRL-L: clear screen and redraw.
5394 */
5395 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005396nv_clear(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 if (!checkclearop(cap->oap))
5399 {
5400#if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5401 /*
5402 * Right now, the BeBox doesn't seem to have an easy way to detect
5403 * window resizing, so we cheat and make the user detect it
5404 * manually with CTRL-L instead
5405 */
5406 ui_get_shellsize();
5407#endif
5408#ifdef FEAT_SYN_HL
5409 /* Clear all syntax states to force resyncing. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02005410 syn_stack_free_all(curwin->w_s);
Bram Moolenaar06f1ed22017-06-18 22:41:03 +02005411# ifdef FEAT_RELTIME
5412 {
5413 win_T *wp;
5414
5415 FOR_ALL_WINDOWS(wp)
5416 wp->w_s->b_syn_slow = FALSE;
5417 }
5418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419#endif
5420 redraw_later(CLEAR);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005421#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
5422# ifdef VIMDLL
5423 if (!gui.in_use)
5424# endif
5425 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01005426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
5428}
5429
5430/*
5431 * CTRL-O: In Select mode: switch to Visual mode for one command.
5432 * Otherwise: Go to older pcmark.
5433 */
5434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005435nv_ctrlo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 if (VIsual_active && VIsual_select)
5438 {
5439 VIsual_select = FALSE;
5440 showmode();
5441 restart_VIsual_select = 2; /* restart Select mode later */
5442 }
5443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
5445 cap->count1 = -cap->count1;
5446 nv_pcmark(cap);
5447 }
5448}
5449
5450/*
Bram Moolenaar1bbb6192018-11-10 16:02:01 +01005451 * CTRL-^ command, short for ":e #". Works even when the alternate buffer is
5452 * not named.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 */
5454 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005455nv_hat(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
5457 if (!checkclearopq(cap->oap))
5458 (void)buflist_getfile((int)cap->count0, (linenr_T)0,
5459 GETF_SETMARK|GETF_ALT, FALSE);
5460}
5461
5462/*
5463 * "Z" commands.
5464 */
5465 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005466nv_Zet(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 if (!checkclearopq(cap->oap))
5469 {
5470 switch (cap->nchar)
5471 {
5472 /* "ZZ": equivalent to ":x". */
5473 case 'Z': do_cmdline_cmd((char_u *)"x");
5474 break;
5475
5476 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5477 case 'Q': do_cmdline_cmd((char_u *)"q!");
5478 break;
5479
5480 default: clearopbeep(cap->oap);
5481 }
5482 }
5483}
5484
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485/*
5486 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5487 */
5488 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005489do_nv_ident(int c1, int c2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490{
5491 oparg_T oa;
5492 cmdarg_T ca;
5493
5494 clear_oparg(&oa);
5495 vim_memset(&ca, 0, sizeof(ca));
5496 ca.oap = &oa;
5497 ca.cmdchar = c1;
5498 ca.nchar = c2;
5499 nv_ident(&ca);
5500}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
5502/*
5503 * Handle the commands that use the word under the cursor.
5504 * [g] CTRL-] :ta to current identifier
5505 * [g] 'K' run program for current identifier
5506 * [g] '*' / to current identifier or string
5507 * [g] '#' ? to current identifier or string
5508 * g ']' :tselect for current identifier
5509 */
5510 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005511nv_ident(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
5513 char_u *ptr = NULL;
5514 char_u *buf;
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005515 unsigned buflen;
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005516 char_u *newbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 char_u *p;
5518 char_u *kp; /* value of 'keywordprg' */
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005519 int kp_help; /* 'keywordprg' is ":he" */
5520 int kp_ex; /* 'keywordprg' starts with ":" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 int n = 0; /* init for GCC */
5522 int cmdchar;
5523 int g_cmd; /* "g" command */
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005524 int tag_cmd = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 char_u *aux_ptr;
5526 int isman;
5527 int isman_s;
5528
5529 if (cap->cmdchar == 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5530 {
5531 cmdchar = cap->nchar;
5532 g_cmd = TRUE;
5533 }
5534 else
5535 {
5536 cmdchar = cap->cmdchar;
5537 g_cmd = FALSE;
5538 }
5539
5540 if (cmdchar == POUND) /* the pound sign, '#' for English keyboards */
5541 cmdchar = '#';
5542
5543 /*
5544 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5545 */
5546 if (cmdchar == ']' || cmdchar == Ctrl_RSB || cmdchar == 'K')
5547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 if (VIsual_active && get_visual_text(cap, &ptr, &n) == FAIL)
5549 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 if (checkclearopq(cap->oap))
5551 return;
5552 }
5553
5554 if (ptr == NULL && (n = find_ident_under_cursor(&ptr,
5555 (cmdchar == '*' || cmdchar == '#')
5556 ? FIND_IDENT|FIND_STRING : FIND_IDENT)) == 0)
5557 {
5558 clearop(cap->oap);
5559 return;
5560 }
5561
5562 /* Allocate buffer to put the command in. Inserting backslashes can
5563 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5564 * and some numbers. */
5565 kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp);
5566 kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0
5567 || STRCMP(kp, ":help") == 0);
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005568 if (kp_help && *skipwhite(ptr) == NUL)
5569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005570 emsg(_(e_noident)); /* found white space only */
Bram Moolenaara4f99f52017-08-26 16:25:32 +02005571 return;
5572 }
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005573 kp_ex = (*kp == ':');
5574 buflen = (unsigned)(n * 2 + 30 + STRLEN(kp));
5575 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 if (buf == NULL)
5577 return;
5578 buf[0] = NUL;
5579
5580 switch (cmdchar)
5581 {
5582 case '*':
5583 case '#':
5584 /*
5585 * Put cursor at start of word, makes search skip the word
5586 * under the cursor.
5587 * Call setpcmark() first, so "*``" puts the cursor back where
5588 * it was.
5589 */
5590 setpcmark();
5591 curwin->w_cursor.col = (colnr_T) (ptr - ml_get_curline());
5592
5593 if (!g_cmd && vim_iswordp(ptr))
5594 STRCPY(buf, "\\<");
5595 no_smartcase = TRUE; /* don't use 'smartcase' now */
5596 break;
5597
5598 case 'K':
5599 if (kp_help)
5600 STRCPY(buf, "he! ");
Bram Moolenaar2ff8b642016-05-24 10:46:45 +02005601 else if (kp_ex)
5602 {
5603 if (cap->count0 != 0)
5604 vim_snprintf((char *)buf, buflen, "%s %ld",
5605 kp, cap->count0);
5606 else
5607 STRCPY(buf, kp);
5608 STRCAT(buf, " ");
5609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 else
5611 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005612 /* An external command will probably use an argument starting
5613 * with "-" as an option. To avoid trouble we skip the "-". */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005614 while (*ptr == '-' && n > 0)
5615 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005616 ++ptr;
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005617 --n;
5618 }
5619 if (n == 0)
5620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005621 emsg(_(e_noident)); /* found dashes only */
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005622 vim_free(buf);
5623 return;
5624 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005625
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 /* When a count is given, turn it into a range. Is this
5627 * really what we want? */
5628 isman = (STRCMP(kp, "man") == 0);
5629 isman_s = (STRCMP(kp, "man -s") == 0);
5630 if (cap->count0 != 0 && !(isman || isman_s))
5631 sprintf((char *)buf, ".,.+%ld", cap->count0 - 1);
5632
5633 STRCAT(buf, "! ");
5634 if (cap->count0 == 0 && isman_s)
5635 STRCAT(buf, "man");
5636 else
5637 STRCAT(buf, kp);
5638 STRCAT(buf, " ");
5639 if (cap->count0 != 0 && (isman || isman_s))
5640 {
5641 sprintf((char *)buf + STRLEN(buf), "%ld", cap->count0);
5642 STRCAT(buf, " ");
5643 }
5644 }
5645 break;
5646
5647 case ']':
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005648 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649#ifdef FEAT_CSCOPE
5650 if (p_cst)
5651 STRCPY(buf, "cstag ");
5652 else
5653#endif
5654 STRCPY(buf, "ts ");
5655 break;
5656
5657 default:
Bram Moolenaar97e7a842010-03-17 13:07:08 +01005658 tag_cmd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 if (curbuf->b_help)
5660 STRCPY(buf, "he! ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 else
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005662 {
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005663 if (g_cmd)
5664 STRCPY(buf, "tj ");
5665 else
5666 sprintf((char *)buf, "%ldta ", cap->count0);
5667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
5669
5670 /*
5671 * Now grab the chars in the identifier
5672 */
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005673 if (cmdchar == 'K' && !kp_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005675 ptr = vim_strnsave(ptr, n);
Bram Moolenaar426f3752016-11-04 21:22:37 +01005676 if (kp_ex)
5677 /* Escape the argument properly for an Ex command */
5678 p = vim_strsave_fnameescape(ptr, FALSE);
5679 else
5680 /* Escape the argument properly for a shell command */
5681 p = vim_strsave_shellescape(ptr, TRUE, TRUE);
Bram Moolenaar9fd01c62008-11-01 12:52:38 +00005682 vim_free(ptr);
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005683 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 {
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005685 vim_free(buf);
5686 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005688 newbuf = vim_realloc(buf, STRLEN(buf) + STRLEN(p) + 1);
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005689 if (newbuf == NULL)
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005690 {
5691 vim_free(buf);
5692 vim_free(p);
5693 return;
5694 }
Bram Moolenaar0bc380a2010-07-10 13:52:13 +02005695 buf = newbuf;
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005696 STRCAT(buf, p);
5697 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005699 else
5700 {
5701 if (cmdchar == '*')
5702 aux_ptr = (char_u *)(p_magic ? "/.*~[^$\\" : "/^$\\");
5703 else if (cmdchar == '#')
5704 aux_ptr = (char_u *)(p_magic ? "/?.*~[^$\\" : "/?^$\\");
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005705 else if (tag_cmd)
Bram Moolenaar77a0aa42010-10-13 18:06:47 +02005706 {
5707 if (curbuf->b_help)
5708 /* ":help" handles unescaped argument */
5709 aux_ptr = (char_u *)"";
5710 else
5711 aux_ptr = (char_u *)"\\|\"\n[";
5712 }
Bram Moolenaar6d8027a2010-01-19 15:24:27 +01005713 else
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005714 aux_ptr = (char_u *)"\\|\"\n*?[";
5715
5716 p = buf + STRLEN(buf);
5717 while (n-- > 0)
5718 {
5719 /* put a backslash before \ and some others */
5720 if (vim_strchr(aux_ptr, *ptr) != NULL)
5721 *p++ = '\\';
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005722 /* When current byte is a part of multibyte character, copy all
5723 * bytes of that character. */
5724 if (has_mbyte)
5725 {
5726 int i;
5727 int len = (*mb_ptr2len)(ptr) - 1;
5728
5729 for (i = 0; i < len && n >= 1; ++i, --n)
5730 *p++ = *ptr++;
5731 }
Bram Moolenaar3094a9e2008-09-06 14:44:59 +00005732 *p++ = *ptr++;
5733 }
5734 *p = NUL;
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 /*
5738 * Execute the command.
5739 */
5740 if (cmdchar == '*' || cmdchar == '#')
5741 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005742 if (!g_cmd && (has_mbyte
5743 ? vim_iswordp(mb_prevptr(ml_get_curline(), ptr))
5744 : vim_iswordc(ptr[-1])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 STRCAT(buf, "\\>");
Bram Moolenaard7663c22019-08-06 21:59:57 +02005746
5747 // put pattern in search history
Bram Moolenaarc7be3f32009-12-24 14:01:12 +00005748 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 add_to_history(HIST_SEARCH, buf, TRUE, NUL);
Bram Moolenaard7663c22019-08-06 21:59:57 +02005750
Bram Moolenaar46539112015-02-17 15:43:57 +01005751 (void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 }
5753 else
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005754 {
5755 g_tag_at_cursor = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 do_cmdline_cmd(buf);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02005757 g_tag_at_cursor = FALSE;
5758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759
5760 vim_free(buf);
5761}
5762
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763/*
5764 * Get visually selected text, within one line only.
5765 * Returns FAIL if more than one line selected.
5766 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005767 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005768get_visual_text(
5769 cmdarg_T *cap,
5770 char_u **pp, /* return: start of selected text */
5771 int *lenp) /* return: length of selected text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
5773 if (VIsual_mode != 'V')
5774 unadjust_for_sel();
5775 if (VIsual.lnum != curwin->w_cursor.lnum)
5776 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005777 if (cap != NULL)
5778 clearopbeep(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 return FAIL;
5780 }
5781 if (VIsual_mode == 'V')
5782 {
5783 *pp = ml_get_curline();
5784 *lenp = (int)STRLEN(*pp);
5785 }
5786 else
5787 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005788 if (LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 *pp = ml_get_pos(&curwin->w_cursor);
5791 *lenp = VIsual.col - curwin->w_cursor.col + 1;
5792 }
5793 else
5794 {
5795 *pp = ml_get_pos(&VIsual);
5796 *lenp = curwin->w_cursor.col - VIsual.col + 1;
5797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 if (has_mbyte)
5799 /* Correct the length to include the whole last character. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005800 *lenp += (*mb_ptr2len)(*pp + (*lenp - 1)) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
5802 reset_VIsual_and_resel();
5803 return OK;
5804}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806/*
5807 * CTRL-T: backwards in tag stack
5808 */
5809 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005810nv_tagpop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 if (!checkclearopq(cap->oap))
5813 do_tag((char_u *)"", DT_POP, (int)cap->count1, FALSE, TRUE);
5814}
5815
5816/*
5817 * Handle scrolling command 'H', 'L' and 'M'.
5818 */
5819 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005820nv_scroll(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
5822 int used = 0;
5823 long n;
5824#ifdef FEAT_FOLDING
5825 linenr_T lnum;
5826#endif
5827 int half;
5828
5829 cap->oap->motion_type = MLINE;
5830 setpcmark();
5831
5832 if (cap->cmdchar == 'L')
5833 {
5834 validate_botline(); /* make sure curwin->w_botline is valid */
5835 curwin->w_cursor.lnum = curwin->w_botline - 1;
5836 if (cap->count1 - 1 >= curwin->w_cursor.lnum)
5837 curwin->w_cursor.lnum = 1;
5838 else
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005839 {
5840#ifdef FEAT_FOLDING
5841 if (hasAnyFolding(curwin))
5842 {
5843 /* Count a fold for one screen line. */
5844 for (n = cap->count1 - 1; n > 0
5845 && curwin->w_cursor.lnum > curwin->w_topline; --n)
5846 {
5847 (void)hasFolding(curwin->w_cursor.lnum,
5848 &curwin->w_cursor.lnum, NULL);
5849 --curwin->w_cursor.lnum;
5850 }
5851 }
5852 else
5853#endif
5854 curwin->w_cursor.lnum -= cap->count1 - 1;
5855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
5857 else
5858 {
5859 if (cap->cmdchar == 'M')
5860 {
5861#ifdef FEAT_DIFF
5862 /* Don't count filler lines above the window. */
5863 used -= diff_check_fill(curwin, curwin->w_topline)
5864 - curwin->w_topfill;
5865#endif
5866 validate_botline(); /* make sure w_empty_rows is valid */
5867 half = (curwin->w_height - curwin->w_empty_rows + 1) / 2;
5868 for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; ++n)
5869 {
5870#ifdef FEAT_DIFF
5871 /* Count half he number of filler lines to be "below this
5872 * line" and half to be "above the next line". */
5873 if (n > 0 && used + diff_check_fill(curwin, curwin->w_topline
5874 + n) / 2 >= half)
5875 {
5876 --n;
5877 break;
5878 }
5879#endif
5880 used += plines(curwin->w_topline + n);
5881 if (used >= half)
5882 break;
5883#ifdef FEAT_FOLDING
5884 if (hasFolding(curwin->w_topline + n, NULL, &lnum))
5885 n = lnum - curwin->w_topline;
5886#endif
5887 }
5888 if (n > 0 && used > curwin->w_height)
5889 --n;
5890 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005891 else /* (cap->cmdchar == 'H') */
5892 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 n = cap->count1 - 1;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005894#ifdef FEAT_FOLDING
5895 if (hasAnyFolding(curwin))
5896 {
5897 /* Count a fold for one screen line. */
5898 lnum = curwin->w_topline;
5899 while (n-- > 0 && lnum < curwin->w_botline - 1)
5900 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02005901 (void)hasFolding(lnum, NULL, &lnum);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005902 ++lnum;
5903 }
5904 n = lnum - curwin->w_topline;
5905 }
5906#endif
5907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 curwin->w_cursor.lnum = curwin->w_topline + n;
5909 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5910 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5911 }
5912
Bram Moolenaar44cc4cf2017-10-15 22:13:37 +02005913 /* Correct for 'so', except when an operator is pending. */
5914 if (cap->oap->op_type == OP_NOP)
5915 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 beginline(BL_SOL | BL_FIX);
5917}
5918
5919/*
5920 * Cursor right commands.
5921 */
5922 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005923nv_right(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 long n;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005926 int past_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005928 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
5929 {
5930 /* <C-Right> and <S-Right> move a word or WORD right */
5931 if (mod_mask & MOD_MASK_CTRL)
5932 cap->arg = TRUE;
5933 nv_wordcmd(cap);
5934 return;
5935 }
5936
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 cap->oap->motion_type = MCHAR;
5938 cap->oap->inclusive = FALSE;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005939 past_line = (VIsual_active && *p_sel != 'o');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 /*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005942 * In virtual edit mode, there's no such thing as "past_line", as lines
5943 * are (theoretically) infinitely long.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 */
5945 if (virtual_active())
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005946 past_line = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
5948 for (n = cap->count1; n > 0; --n)
5949 {
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005950 if ((!past_line && oneright() == FAIL)
5951 || (past_line && *ml_get_cursor() == NUL)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005952 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005955 * <Space> wraps to next line if 'whichwrap' has 's'.
5956 * 'l' wraps to next line if 'whichwrap' has 'l'.
5957 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 */
5959 if ( ((cap->cmdchar == ' '
5960 && vim_strchr(p_ww, 's') != NULL)
5961 || (cap->cmdchar == 'l'
5962 && vim_strchr(p_ww, 'l') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00005963 || (cap->cmdchar == K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 && vim_strchr(p_ww, '>') != NULL))
5965 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
5966 {
5967 /* When deleting we also count the NL as a character.
5968 * Set cap->oap->inclusive when last char in the line is
5969 * included, move to next line after that */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005970 if ( cap->oap->op_type != OP_NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 && !cap->oap->inclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005972 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 cap->oap->inclusive = TRUE;
5974 else
5975 {
5976 ++curwin->w_cursor.lnum;
5977 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 curwin->w_set_curswant = TRUE;
5980 cap->oap->inclusive = FALSE;
5981 }
5982 continue;
5983 }
5984 if (cap->oap->op_type == OP_NOP)
5985 {
5986 /* Only beep and flush if not moved at all */
5987 if (n == cap->count1)
5988 beep_flush();
5989 }
5990 else
5991 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005992 if (!LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 cap->oap->inclusive = TRUE;
5994 }
5995 break;
5996 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01005997 else if (past_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 {
5999 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (virtual_active())
6001 oneright();
6002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 if (has_mbyte)
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02006005 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 ++curwin->w_cursor.col;
6008 }
6009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 }
6011#ifdef FEAT_FOLDING
6012 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6013 && cap->oap->op_type == OP_NOP)
6014 foldOpenCursor();
6015#endif
6016}
6017
6018/*
6019 * Cursor left commands.
6020 *
6021 * Returns TRUE when operator end should not be adjusted.
6022 */
6023 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006024nv_left(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
6026 long n;
6027
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006028 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
6029 {
6030 /* <C-Left> and <S-Left> move a word or WORD left */
6031 if (mod_mask & MOD_MASK_CTRL)
6032 cap->arg = 1;
6033 nv_bck_word(cap);
6034 return;
6035 }
6036
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 cap->oap->motion_type = MCHAR;
6038 cap->oap->inclusive = FALSE;
6039 for (n = cap->count1; n > 0; --n)
6040 {
6041 if (oneleft() == FAIL)
6042 {
6043 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
6044 * 'h' wraps to previous line if 'whichwrap' has 'h'.
6045 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
6046 */
6047 if ( (((cap->cmdchar == K_BS
6048 || cap->cmdchar == Ctrl_H)
6049 && vim_strchr(p_ww, 'b') != NULL)
6050 || (cap->cmdchar == 'h'
6051 && vim_strchr(p_ww, 'h') != NULL)
Bram Moolenaara88d9682005-03-25 21:45:43 +00006052 || (cap->cmdchar == K_LEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 && vim_strchr(p_ww, '<') != NULL))
6054 && curwin->w_cursor.lnum > 1)
6055 {
6056 --(curwin->w_cursor.lnum);
6057 coladvance((colnr_T)MAXCOL);
6058 curwin->w_set_curswant = TRUE;
6059
6060 /* When the NL before the first char has to be deleted we
6061 * put the cursor on the NUL after the previous line.
6062 * This is a very special case, be careful!
Bram Moolenaarad8958b2008-01-02 15:26:04 +00006063 * Don't adjust op_end now, otherwise it won't work. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 if ( (cap->oap->op_type == OP_DELETE
6065 || cap->oap->op_type == OP_CHANGE)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006066 && !LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006068 char_u *cp = ml_get_cursor();
6069
6070 if (*cp != NUL)
6071 {
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006072 if (has_mbyte)
6073 curwin->w_cursor.col += (*mb_ptr2len)(cp);
6074 else
Bram Moolenaar7d311c52014-02-22 23:49:35 +01006075 ++curwin->w_cursor.col;
6076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 cap->retval |= CA_NO_ADJ_OP_END;
6078 }
6079 continue;
6080 }
6081 /* Only beep and flush if not moved at all */
6082 else if (cap->oap->op_type == OP_NOP && n == cap->count1)
6083 beep_flush();
6084 break;
6085 }
6086 }
6087#ifdef FEAT_FOLDING
6088 if (n != cap->count1 && (fdo_flags & FDO_HOR) && KeyTyped
6089 && cap->oap->op_type == OP_NOP)
6090 foldOpenCursor();
6091#endif
6092}
6093
6094/*
6095 * Cursor up commands.
6096 * cap->arg is TRUE for "-": Move cursor to first non-blank.
6097 */
6098 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006099nv_up(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006101 if (mod_mask & MOD_MASK_SHIFT)
6102 {
6103 /* <S-Up> is page up */
6104 cap->arg = BACKWARD;
6105 nv_page(cap);
6106 }
6107 else
6108 {
6109 cap->oap->motion_type = MLINE;
6110 if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6111 clearopbeep(cap->oap);
6112 else if (cap->arg)
6113 beginline(BL_WHITE | BL_FIX);
6114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115}
6116
6117/*
6118 * Cursor down commands.
6119 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6120 */
6121 static void
Bram Moolenaar1b010052016-09-12 12:24:11 +02006122nv_down(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006124 if (mod_mask & MOD_MASK_SHIFT)
6125 {
6126 /* <S-Down> is page down */
6127 cap->arg = FORWARD;
6128 nv_page(cap);
6129 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006130#if defined(FEAT_QUICKFIX)
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006131 /* Quickfix window only: view the result under the cursor. */
6132 else if (bt_quickfix(curbuf) && cap->cmdchar == CAR)
6133 qf_view_result(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134#endif
Bram Moolenaar0a08c632018-07-25 22:36:52 +02006135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
6137#ifdef FEAT_CMDWIN
6138 /* In the cmdline window a <CR> executes the command. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006139 if (cmdwin_type != 0 && cap->cmdchar == CAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 cmdwin_result = CAR;
6141 else
6142#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02006143#ifdef FEAT_JOB_CHANNEL
6144 /* In a prompt buffer a <CR> in the last line invokes the callback. */
6145 if (bt_prompt(curbuf) && cap->cmdchar == CAR
6146 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count)
6147 {
6148 invoke_prompt_callback();
6149 if (restart_edit == 0)
6150 restart_edit = 'a';
6151 }
6152 else
6153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 {
6155 cap->oap->motion_type = MLINE;
6156 if (cursor_down(cap->count1, cap->oap->op_type == OP_NOP) == FAIL)
6157 clearopbeep(cap->oap);
6158 else if (cap->arg)
6159 beginline(BL_WHITE | BL_FIX);
6160 }
6161 }
6162}
6163
6164#ifdef FEAT_SEARCHPATH
6165/*
6166 * Grab the file name under the cursor and edit it.
6167 */
6168 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006169nv_gotofile(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006172 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006174 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 {
6176 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006177 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 return;
6179 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006180 if (curbuf_locked())
6181 {
6182 clearop(cap->oap);
6183 return;
6184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006186 ptr = grab_file_name(cap->count1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 if (ptr != NULL)
6189 {
6190 /* do autowrite if necessary */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006191 if (curbufIsChanged() && curbuf->b_nwindows <= 1 && !buf_hide(curbuf))
Bram Moolenaarcde88542015-08-11 19:14:00 +02006192 (void)autowrite(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 setpcmark();
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006194 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LAST,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006195 buf_hide(curbuf) ? ECMD_HIDE : 0, curwin) == OK
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02006196 && cap->nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006197 {
6198 curwin->w_cursor.lnum = lnum;
6199 check_cursor_lnum();
6200 beginline(BL_SOL | BL_FIX);
6201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 vim_free(ptr);
6203 }
6204 else
6205 clearop(cap->oap);
6206}
6207#endif
6208
6209/*
6210 * <End> command: to end of current line or last line.
6211 */
6212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006213nv_end(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006215 if (cap->arg || (mod_mask & MOD_MASK_CTRL)) /* CTRL-END = goto last line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00006217 cap->arg = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 nv_goto(cap);
6219 cap->count1 = 1; /* to end of current line */
6220 }
6221 nv_dollar(cap);
6222}
6223
6224/*
6225 * Handle the "$" command.
6226 */
6227 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006228nv_dollar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 cap->oap->motion_type = MCHAR;
6231 cap->oap->inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 /* In virtual mode when off the edge of a line and an operator
6233 * is pending (whew!) keep the cursor where it is.
6234 * Otherwise, send it to the end of the line. */
6235 if (!virtual_active() || gchar_cursor() != NUL
6236 || cap->oap->op_type == OP_NOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 curwin->w_curswant = MAXCOL; /* so we stay at the end */
6238 if (cursor_down((long)(cap->count1 - 1),
6239 cap->oap->op_type == OP_NOP) == FAIL)
6240 clearopbeep(cap->oap);
6241#ifdef FEAT_FOLDING
6242 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6243 foldOpenCursor();
6244#endif
6245}
6246
6247/*
6248 * Implementation of '?' and '/' commands.
6249 * If cap->arg is TRUE don't set PC mark.
6250 */
6251 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006252nv_search(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 oparg_T *oap = cap->oap;
Bram Moolenaardda933d2016-09-03 21:04:58 +02006255 pos_T save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
6257 if (cap->cmdchar == '?' && cap->oap->op_type == OP_ROT13)
6258 {
6259 /* Translate "g??" to "g?g?" */
6260 cap->cmdchar = 'g';
6261 cap->nchar = '?';
6262 nv_operator(cap);
6263 return;
6264 }
6265
Bram Moolenaardda933d2016-09-03 21:04:58 +02006266 /* When using 'incsearch' the cursor may be moved to set a different search
6267 * start position. */
Bram Moolenaare96a2492019-06-25 04:12:16 +02006268 cap->searchbuf = getcmdline(cap->cmdchar, cap->count1, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270 if (cap->searchbuf == NULL)
6271 {
6272 clearop(oap);
6273 return;
6274 }
6275
Bram Moolenaar46539112015-02-17 15:43:57 +01006276 (void)normal_search(cap, cap->cmdchar, cap->searchbuf,
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006277 (cap->arg || !EQUAL_POS(save_cursor, curwin->w_cursor))
Bram Moolenaardda933d2016-09-03 21:04:58 +02006278 ? 0 : SEARCH_MARK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279}
6280
6281/*
6282 * Handle "N" and "n" commands.
6283 * cap->arg is SEARCH_REV for "N", 0 for "n".
6284 */
6285 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006286nv_next(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287{
Bram Moolenaar46539112015-02-17 15:43:57 +01006288 pos_T old = curwin->w_cursor;
6289 int i = normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6290
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01006291 if (i == 1 && EQUAL_POS(old, curwin->w_cursor))
Bram Moolenaar46539112015-02-17 15:43:57 +01006292 {
6293 /* Avoid getting stuck on the current cursor position, which can
6294 * happen when an offset is given and the cursor is on the last char
6295 * in the buffer: Repeat with count + 1. */
6296 cap->count1 += 1;
6297 (void)normal_search(cap, 0, NULL, SEARCH_MARK | cap->arg);
6298 cap->count1 -= 1;
6299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}
6301
6302/*
6303 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6304 * Uses only cap->count1 and cap->oap from "cap".
Bram Moolenaar46539112015-02-17 15:43:57 +01006305 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 */
Bram Moolenaar46539112015-02-17 15:43:57 +01006307 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006308normal_search(
6309 cmdarg_T *cap,
6310 int dir,
6311 char_u *pat,
6312 int opt) /* extra flags for do_search() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313{
6314 int i;
6315
6316 cap->oap->motion_type = MCHAR;
6317 cap->oap->inclusive = FALSE;
6318 cap->oap->use_reg_one = TRUE;
6319 curwin->w_set_curswant = TRUE;
6320
6321 i = do_search(cap->oap, dir, pat, cap->count1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02006322 opt | SEARCH_OPT | SEARCH_ECHO | SEARCH_MSG, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 if (i == 0)
6324 clearop(cap->oap);
6325 else
6326 {
6327 if (i == 2)
6328 cap->oap->motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330#ifdef FEAT_FOLDING
6331 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6332 foldOpenCursor();
6333#endif
6334 }
6335
6336 /* "/$" will put the cursor after the end of the line, may need to
6337 * correct that here */
6338 check_cursor();
Bram Moolenaar46539112015-02-17 15:43:57 +01006339 return i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340}
6341
6342/*
6343 * Character search commands.
6344 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6345 * ',' and FALSE for ';'.
6346 * cap->nchar is NUL for ',' and ';' (repeat the search)
6347 */
6348 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006349nv_csearch(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 int t_cmd;
6352
6353 if (cap->cmdchar == 't' || cap->cmdchar == 'T')
6354 t_cmd = TRUE;
6355 else
6356 t_cmd = FALSE;
6357
6358 cap->oap->motion_type = MCHAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == FAIL)
6360 clearopbeep(cap->oap);
6361 else
6362 {
6363 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 /* Include a Tab for "tx" and for "dfx". */
6365 if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
6366 && (t_cmd || cap->oap->op_type != OP_NOP))
6367 {
6368 colnr_T scol, ecol;
6369
6370 getvcol(curwin, &curwin->w_cursor, &scol, NULL, &ecol);
6371 curwin->w_cursor.coladd = ecol - scol;
6372 }
6373 else
6374 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376#ifdef FEAT_FOLDING
6377 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
6378 foldOpenCursor();
6379#endif
6380 }
6381}
6382
6383/*
6384 * "[" and "]" commands.
6385 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6386 */
6387 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006388nv_brackets(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01006390 pos_T new_pos = {0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 pos_T prev_pos;
6392 pos_T *pos = NULL; /* init for GCC */
6393 pos_T old_pos; /* cursor position before command */
6394 int flag;
6395 long n;
6396 int findc;
6397 int c;
6398
6399 cap->oap->motion_type = MCHAR;
6400 cap->oap->inclusive = FALSE;
6401 old_pos = curwin->w_cursor;
Bram Moolenaar32526b32019-01-19 17:43:09 +01006402 curwin->w_cursor.coladd = 0; // TODO: don't do this for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404#ifdef FEAT_SEARCHPATH
6405 /*
6406 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6407 */
6408 if (cap->nchar == 'f')
6409 nv_gotofile(cap);
6410 else
6411#endif
6412
6413#ifdef FEAT_FIND_ID
6414 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00006415 * Find the occurrence(s) of the identifier or define under cursor
6416 * in current and included files or jump to the first occurrence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 *
6418 * search list jump
6419 * fwd bwd fwd bwd fwd bwd
6420 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6421 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6422 */
6423 if (vim_strchr((char_u *)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006424# ifdef EBCDIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 "iI\005dD\067",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006426# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 "iI\011dD\004",
Bram Moolenaar32526b32019-01-19 17:43:09 +01006428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 cap->nchar) != NULL)
6430 {
6431 char_u *ptr;
6432 int len;
6433
6434 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
6435 clearop(cap->oap);
6436 else
6437 {
6438 find_pattern_in_path(ptr, 0, len, TRUE,
6439 cap->count0 == 0 ? !isupper(cap->nchar) : FALSE,
6440 ((cap->nchar & 0xf) == ('d' & 0xf)) ? FIND_DEFINE : FIND_ANY,
6441 cap->count1,
6442 isupper(cap->nchar) ? ACTION_SHOW_ALL :
6443 islower(cap->nchar) ? ACTION_SHOW : ACTION_GOTO,
6444 cap->cmdchar == ']' ? curwin->w_cursor.lnum + 1 : (linenr_T)1,
6445 (linenr_T)MAXLNUM);
6446 curwin->w_set_curswant = TRUE;
6447 }
6448 }
6449 else
6450#endif
6451
6452 /*
6453 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6454 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6455 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6456 * "[m" or "]m" search for prev/next start of (Java) method.
6457 * "[M" or "]M" search for prev/next end of (Java) method.
6458 */
6459 if ( (cap->cmdchar == '['
6460 && vim_strchr((char_u *)"{(*/#mM", cap->nchar) != NULL)
6461 || (cap->cmdchar == ']'
6462 && vim_strchr((char_u *)"})*/#mM", cap->nchar) != NULL))
6463 {
6464 if (cap->nchar == '*')
6465 cap->nchar = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 prev_pos.lnum = 0;
6467 if (cap->nchar == 'm' || cap->nchar == 'M')
6468 {
6469 if (cap->cmdchar == '[')
6470 findc = '{';
6471 else
6472 findc = '}';
6473 n = 9999;
6474 }
6475 else
6476 {
6477 findc = cap->nchar;
6478 n = cap->count1;
6479 }
6480 for ( ; n > 0; --n)
6481 {
6482 if ((pos = findmatchlimit(cap->oap, findc,
6483 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD, 0)) == NULL)
6484 {
6485 if (new_pos.lnum == 0) /* nothing found */
6486 {
6487 if (cap->nchar != 'm' && cap->nchar != 'M')
6488 clearopbeep(cap->oap);
6489 }
6490 else
6491 pos = &new_pos; /* use last one found */
6492 break;
6493 }
6494 prev_pos = new_pos;
6495 curwin->w_cursor = *pos;
6496 new_pos = *pos;
6497 }
6498 curwin->w_cursor = old_pos;
6499
6500 /*
6501 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6502 * brought us to the match for "[m" and "]M" when inside a method.
6503 * Try finding the '{' or '}' we want to be at.
6504 * Also repeat for the given count.
6505 */
6506 if (cap->nchar == 'm' || cap->nchar == 'M')
6507 {
6508 /* norm is TRUE for "]M" and "[m" */
6509 int norm = ((findc == '{') == (cap->nchar == 'm'));
6510
6511 n = cap->count1;
6512 /* found a match: we were inside a method */
6513 if (prev_pos.lnum != 0)
6514 {
6515 pos = &prev_pos;
6516 curwin->w_cursor = prev_pos;
6517 if (norm)
6518 --n;
6519 }
6520 else
6521 pos = NULL;
6522 while (n > 0)
6523 {
6524 for (;;)
6525 {
6526 if ((findc == '{' ? dec_cursor() : inc_cursor()) < 0)
6527 {
6528 /* if not found anything, that's an error */
6529 if (pos == NULL)
6530 clearopbeep(cap->oap);
6531 n = 0;
6532 break;
6533 }
6534 c = gchar_cursor();
6535 if (c == '{' || c == '}')
6536 {
6537 /* Must have found end/start of class: use it.
6538 * Or found the place to be at. */
6539 if ((c == findc && norm) || (n == 1 && !norm))
6540 {
6541 new_pos = curwin->w_cursor;
6542 pos = &new_pos;
6543 n = 0;
6544 }
6545 /* if no match found at all, we started outside of the
6546 * class and we're inside now. Just go on. */
6547 else if (new_pos.lnum == 0)
6548 {
6549 new_pos = curwin->w_cursor;
6550 pos = &new_pos;
6551 }
6552 /* found start/end of other method: go to match */
6553 else if ((pos = findmatchlimit(cap->oap, findc,
6554 (cap->cmdchar == '[') ? FM_BACKWARD : FM_FORWARD,
6555 0)) == NULL)
6556 n = 0;
6557 else
6558 curwin->w_cursor = *pos;
6559 break;
6560 }
6561 }
6562 --n;
6563 }
6564 curwin->w_cursor = old_pos;
6565 if (pos == NULL && new_pos.lnum != 0)
6566 clearopbeep(cap->oap);
6567 }
6568 if (pos != NULL)
6569 {
6570 setpcmark();
6571 curwin->w_cursor = *pos;
6572 curwin->w_set_curswant = TRUE;
6573#ifdef FEAT_FOLDING
6574 if ((fdo_flags & FDO_BLOCK) && KeyTyped
6575 && cap->oap->op_type == OP_NOP)
6576 foldOpenCursor();
6577#endif
6578 }
6579 }
6580
6581 /*
6582 * "[[", "[]", "]]" and "][": move to start or end of function
6583 */
6584 else if (cap->nchar == '[' || cap->nchar == ']')
6585 {
6586 if (cap->nchar == cap->cmdchar) /* "]]" or "[[" */
6587 flag = '{';
6588 else
6589 flag = '}'; /* "][" or "[]" */
6590
6591 curwin->w_set_curswant = TRUE;
6592 /*
6593 * Imitate strange Vi behaviour: When using "]]" with an operator
6594 * we also stop at '}'.
6595 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006596 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, flag,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 (cap->oap->op_type != OP_NOP
6598 && cap->arg == FORWARD && flag == '{')))
6599 clearopbeep(cap->oap);
6600 else
6601 {
6602 if (cap->oap->op_type == OP_NOP)
6603 beginline(BL_WHITE | BL_FIX);
6604#ifdef FEAT_FOLDING
6605 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6606 foldOpenCursor();
6607#endif
6608 }
6609 }
6610
6611 /*
6612 * "[p", "[P", "]P" and "]p": put with indent adjustment
6613 */
6614 else if (cap->nchar == 'p' || cap->nchar == 'P')
6615 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02006616 nv_put_opt(cap, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 }
6618
6619 /*
6620 * "['", "[`", "]'" and "]`": jump to next mark
6621 */
6622 else if (cap->nchar == '\'' || cap->nchar == '`')
6623 {
6624 pos = &curwin->w_cursor;
6625 for (n = cap->count1; n > 0; --n)
6626 {
6627 prev_pos = *pos;
6628 pos = getnextmark(pos, cap->cmdchar == '[' ? BACKWARD : FORWARD,
6629 cap->nchar == '\'');
6630 if (pos == NULL)
6631 break;
6632 }
6633 if (pos == NULL)
6634 pos = &prev_pos;
6635 nv_cursormark(cap, cap->nchar == '\'', pos);
6636 }
6637
6638#ifdef FEAT_MOUSE
6639 /*
6640 * [ or ] followed by a middle mouse click: put selected text with
6641 * indent adjustment. Any other button just does as usual.
6642 */
Bram Moolenaar5ea0ac72010-05-07 15:52:08 +02006643 else if (cap->nchar >= K_RIGHTRELEASE && cap->nchar <= K_LEFTMOUSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 {
6645 (void)do_mouse(cap->oap, cap->nchar,
6646 (cap->cmdchar == ']') ? FORWARD : BACKWARD,
6647 cap->count1, PUT_FIXINDENT);
6648 }
6649#endif /* FEAT_MOUSE */
6650
6651#ifdef FEAT_FOLDING
6652 /*
6653 * "[z" and "]z": move to start or end of open fold.
6654 */
6655 else if (cap->nchar == 'z')
6656 {
6657 if (foldMoveTo(FALSE, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6658 cap->count1) == FAIL)
6659 clearopbeep(cap->oap);
6660 }
6661#endif
6662
6663#ifdef FEAT_DIFF
6664 /*
6665 * "[c" and "]c": move to next or previous diff-change.
6666 */
6667 else if (cap->nchar == 'c')
6668 {
6669 if (diff_move_to(cap->cmdchar == ']' ? FORWARD : BACKWARD,
6670 cap->count1) == FAIL)
6671 clearopbeep(cap->oap);
6672 }
6673#endif
6674
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006675#ifdef FEAT_SPELL
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006676 /*
6677 * "[s", "[S", "]s" and "]S": move to next spell error.
6678 */
6679 else if (cap->nchar == 's' || cap->nchar == 'S')
6680 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006681 setpcmark();
6682 for (n = 0; n < cap->count1; ++n)
Bram Moolenaar95529562005-08-25 21:21:38 +00006683 if (spell_move_to(curwin, cap->cmdchar == ']' ? FORWARD : BACKWARD,
6684 cap->nchar == 's' ? TRUE : FALSE, FALSE, NULL) == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006685 {
6686 clearopbeep(cap->oap);
6687 break;
6688 }
Bram Moolenaarb73fa622017-12-21 20:27:47 +01006689 else
6690 curwin->w_set_curswant = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006691# ifdef FEAT_FOLDING
6692 if (cap->oap->op_type == OP_NOP && (fdo_flags & FDO_SEARCH) && KeyTyped)
6693 foldOpenCursor();
6694# endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006695 }
6696#endif
6697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 /* Not a valid cap->nchar. */
6699 else
6700 clearopbeep(cap->oap);
6701}
6702
6703/*
6704 * Handle Normal mode "%" command.
6705 */
6706 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006707nv_percent(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
6709 pos_T *pos;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02006710#if defined(FEAT_FOLDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 linenr_T lnum = curwin->w_cursor.lnum;
6712#endif
6713
6714 cap->oap->inclusive = TRUE;
6715 if (cap->count0) /* {cnt}% : goto {cnt} percentage in file */
6716 {
6717 if (cap->count0 > 100)
6718 clearopbeep(cap->oap);
6719 else
6720 {
6721 cap->oap->motion_type = MLINE;
6722 setpcmark();
6723 /* Round up, so CTRL-G will give same value. Watch out for a
6724 * large line count, the line number must not go negative! */
6725 if (curbuf->b_ml.ml_line_count > 1000000)
6726 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count + 99L)
6727 / 100L * cap->count0;
6728 else
6729 curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count *
6730 cap->count0 + 99L) / 100L;
6731 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6732 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6733 beginline(BL_SOL | BL_FIX);
6734 }
6735 }
6736 else /* "%" : go to matching paren */
6737 {
6738 cap->oap->motion_type = MCHAR;
6739 cap->oap->use_reg_one = TRUE;
6740 if ((pos = findmatch(cap->oap, NUL)) == NULL)
6741 clearopbeep(cap->oap);
6742 else
6743 {
6744 setpcmark();
6745 curwin->w_cursor = *pos;
6746 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
6750 }
6751#ifdef FEAT_FOLDING
6752 if (cap->oap->op_type == OP_NOP
6753 && lnum != curwin->w_cursor.lnum
6754 && (fdo_flags & FDO_PERCENT)
6755 && KeyTyped)
6756 foldOpenCursor();
6757#endif
6758}
6759
6760/*
6761 * Handle "(" and ")" commands.
6762 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6763 */
6764 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006765nv_brace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767 cap->oap->motion_type = MCHAR;
6768 cap->oap->use_reg_one = TRUE;
Bram Moolenaarebefac62005-12-28 22:39:57 +00006769 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6770 cap->oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 curwin->w_set_curswant = TRUE;
6772
6773 if (findsent(cap->arg, cap->count1) == FAIL)
6774 clearopbeep(cap->oap);
6775 else
6776 {
Bram Moolenaar1f14d572008-01-12 16:12:10 +00006777 /* Don't leave the cursor on the NUL past end of line. */
6778 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780#ifdef FEAT_FOLDING
6781 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6782 foldOpenCursor();
6783#endif
6784 }
6785}
6786
6787/*
6788 * "m" command: Mark a position.
6789 */
6790 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006791nv_mark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
6793 if (!checkclearop(cap->oap))
6794 {
6795 if (setmark(cap->nchar) == FAIL)
6796 clearopbeep(cap->oap);
6797 }
6798}
6799
6800/*
6801 * "{" and "}" commands.
6802 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6803 */
6804 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006805nv_findpar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 cap->oap->motion_type = MCHAR;
6808 cap->oap->inclusive = FALSE;
6809 cap->oap->use_reg_one = TRUE;
6810 curwin->w_set_curswant = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00006811 if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 clearopbeep(cap->oap);
6813 else
6814 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816#ifdef FEAT_FOLDING
6817 if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP)
6818 foldOpenCursor();
6819#endif
6820 }
6821}
6822
6823/*
6824 * "u" command: Undo or make lower case.
6825 */
6826 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006827nv_undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006829 if (cap->oap->op_type == OP_LOWER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 {
6831 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6832 cap->cmdchar = 'g';
6833 cap->nchar = 'u';
6834 nv_operator(cap);
6835 }
6836 else
6837 nv_kundo(cap);
6838}
6839
6840/*
6841 * <Undo> command.
6842 */
6843 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006844nv_kundo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 if (!checkclearopq(cap->oap))
6847 {
Bram Moolenaarf2732452018-06-03 14:47:35 +02006848#ifdef FEAT_JOB_CHANNEL
6849 if (bt_prompt(curbuf))
6850 {
6851 clearopbeep(cap->oap);
6852 return;
6853 }
6854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 u_undo((int)cap->count1);
6856 curwin->w_set_curswant = TRUE;
6857 }
6858}
6859
6860/*
6861 * Handle the "r" command.
6862 */
6863 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006864nv_replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 char_u *ptr;
6867 int had_ctrl_v;
6868 long n;
6869
6870 if (checkclearop(cap->oap))
6871 return;
Bram Moolenaarf2732452018-06-03 14:47:35 +02006872#ifdef FEAT_JOB_CHANNEL
6873 if (bt_prompt(curbuf) && !prompt_curpos_editable())
6874 {
6875 clearopbeep(cap->oap);
6876 return;
6877 }
6878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
6880 /* get another character */
6881 if (cap->nchar == Ctrl_V)
6882 {
6883 had_ctrl_v = Ctrl_V;
6884 cap->nchar = get_literal();
6885 /* Don't redo a multibyte character with CTRL-V. */
6886 if (cap->nchar > DEL)
6887 had_ctrl_v = NUL;
6888 }
6889 else
6890 had_ctrl_v = NUL;
6891
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006892 /* Abort if the character is a special key. */
6893 if (IS_SPECIAL(cap->nchar))
6894 {
6895 clearopbeep(cap->oap);
6896 return;
6897 }
6898
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 /* Visual mode "r" */
6900 if (VIsual_active)
6901 {
Bram Moolenaar57fb0da2009-02-04 10:46:25 +00006902 if (got_int)
6903 reset_VIsual();
Bram Moolenaard9820532013-11-04 01:41:17 +01006904 if (had_ctrl_v)
6905 {
Bram Moolenaarf12519d2018-02-06 22:52:49 +01006906 /* Use a special (negative) number to make a difference between a
6907 * literal CR or NL and a line break. */
6908 if (cap->nchar == CAR)
6909 cap->nchar = REPLACE_CR_NCHAR;
6910 else if (cap->nchar == NL)
6911 cap->nchar = REPLACE_NL_NCHAR;
Bram Moolenaard9820532013-11-04 01:41:17 +01006912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 nv_operator(cap);
6914 return;
6915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 /* Break tabs, etc. */
6918 if (virtual_active())
6919 {
6920 if (u_save_cursor() == FAIL)
6921 return;
6922 if (gchar_cursor() == NUL)
6923 {
6924 /* Add extra space and put the cursor on the first one. */
6925 coladvance_force((colnr_T)(getviscol() + cap->count1));
6926 curwin->w_cursor.col -= cap->count1;
6927 }
6928 else if (gchar_cursor() == TAB)
6929 coladvance_force(getviscol());
6930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006932 /* Abort if not enough characters to replace. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 ptr = ml_get_cursor();
Bram Moolenaarc2f5abc2007-08-08 19:42:05 +00006934 if (STRLEN(ptr) < (unsigned)cap->count1
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006935 || (has_mbyte && mb_charlen(ptr) < cap->count1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 {
6937 clearopbeep(cap->oap);
6938 return;
6939 }
6940
6941 /*
6942 * Replacing with a TAB is done by edit() when it is complicated because
6943 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6944 * Other characters are done below to avoid problems with things like
6945 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6946 */
6947 if (had_ctrl_v != Ctrl_V && cap->nchar == '\t' && (curbuf->b_p_et || p_sta))
6948 {
6949 stuffnumReadbuff(cap->count1);
6950 stuffcharReadbuff('R');
6951 stuffcharReadbuff('\t');
6952 stuffcharReadbuff(ESC);
6953 return;
6954 }
6955
6956 /* save line for undo */
6957 if (u_save_cursor() == FAIL)
6958 return;
6959
6960 if (had_ctrl_v != Ctrl_V && (cap->nchar == '\r' || cap->nchar == '\n'))
6961 {
6962 /*
6963 * Replace character(s) by a single newline.
6964 * Strange vi behaviour: Only one newline is inserted.
6965 * Delete the characters here.
6966 * Insert the newline with an insert command, takes care of
6967 * autoindent. The insert command depends on being on the last
6968 * character of a line or not.
6969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 (void)del_chars(cap->count1, FALSE); /* delete the characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 stuffcharReadbuff('\r');
6972 stuffcharReadbuff(ESC);
6973
6974 /* Give 'r' to edit(), to get the redo command right. */
6975 invoke_edit(cap, TRUE, 'r', FALSE);
6976 }
6977 else
6978 {
6979 prep_redo(cap->oap->regname, cap->count1,
6980 NUL, 'r', NUL, had_ctrl_v, cap->nchar);
6981
6982 curbuf->b_op_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (has_mbyte)
6984 {
6985 int old_State = State;
6986
6987 if (cap->ncharC1 != 0)
6988 AppendCharToRedobuff(cap->ncharC1);
6989 if (cap->ncharC2 != 0)
6990 AppendCharToRedobuff(cap->ncharC2);
6991
6992 /* This is slow, but it handles replacing a single-byte with a
6993 * multi-byte and the other way around. Also handles adding
6994 * composing characters for utf-8. */
6995 for (n = cap->count1; n > 0; --n)
6996 {
6997 State = REPLACE;
Bram Moolenaar8320da42012-04-30 18:18:47 +02006998 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
6999 {
7000 int c = ins_copychar(curwin->w_cursor.lnum
7001 + (cap->nchar == Ctrl_Y ? -1 : 1));
7002 if (c != NUL)
7003 ins_char(c);
7004 else
7005 /* will be decremented further down */
7006 ++curwin->w_cursor.col;
7007 }
7008 else
7009 ins_char(cap->nchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 State = old_State;
7011 if (cap->ncharC1 != 0)
7012 ins_char(cap->ncharC1);
7013 if (cap->ncharC2 != 0)
7014 ins_char(cap->ncharC2);
7015 }
7016 }
7017 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
7019 /*
7020 * Replace the characters within one line.
7021 */
7022 for (n = cap->count1; n > 0; --n)
7023 {
7024 /*
7025 * Get ptr again, because u_save and/or showmatch() will have
7026 * released the line. At the same time we let know that the
7027 * line will be changed.
7028 */
7029 ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE);
Bram Moolenaar8320da42012-04-30 18:18:47 +02007030 if (cap->nchar == Ctrl_E || cap->nchar == Ctrl_Y)
7031 {
7032 int c = ins_copychar(curwin->w_cursor.lnum
7033 + (cap->nchar == Ctrl_Y ? -1 : 1));
7034 if (c != NUL)
7035 ptr[curwin->w_cursor.col] = c;
7036 }
7037 else
7038 ptr[curwin->w_cursor.col] = cap->nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 if (p_sm && msg_silent == 0)
7040 showmatch(cap->nchar);
7041 ++curwin->w_cursor.col;
7042 }
7043#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007044 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 {
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007046 colnr_T start = (colnr_T)(curwin->w_cursor.col - cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
Bram Moolenaar009b2592004-10-24 19:18:58 +00007048 netbeans_removed(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007049 (long)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 netbeans_inserted(curbuf, curwin->w_cursor.lnum, start,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007051 &ptr[start], (int)cap->count1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 }
7053#endif
7054
7055 /* mark the buffer as changed and prepare for displaying */
7056 changed_bytes(curwin->w_cursor.lnum,
7057 (colnr_T)(curwin->w_cursor.col - cap->count1));
7058 }
7059 --curwin->w_cursor.col; /* cursor on the last replaced char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 /* if the character on the left of the current cursor is a multi-byte
7061 * character, move two characters left */
7062 if (has_mbyte)
7063 mb_adjust_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 curbuf->b_op_end = curwin->w_cursor;
7065 curwin->w_set_curswant = TRUE;
7066 set_last_insert(cap->nchar);
7067 }
7068}
7069
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070/*
7071 * 'o': Exchange start and end of Visual area.
7072 * 'O': same, but in block mode exchange left and right corners.
7073 */
7074 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007075v_swap_corners(int cmdchar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077 pos_T old_cursor;
7078 colnr_T left, right;
7079
7080 if (cmdchar == 'O' && VIsual_mode == Ctrl_V)
7081 {
7082 old_cursor = curwin->w_cursor;
7083 getvcols(curwin, &old_cursor, &VIsual, &left, &right);
7084 curwin->w_cursor.lnum = VIsual.lnum;
7085 coladvance(left);
7086 VIsual = curwin->w_cursor;
7087
7088 curwin->w_cursor.lnum = old_cursor.lnum;
7089 curwin->w_curswant = right;
7090 /* 'selection "exclusive" and cursor at right-bottom corner: move it
7091 * right one column */
7092 if (old_cursor.lnum >= VIsual.lnum && *p_sel == 'e')
7093 ++curwin->w_curswant;
7094 coladvance(curwin->w_curswant);
7095 if (curwin->w_cursor.col == old_cursor.col
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 && (!virtual_active()
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007097 || curwin->w_cursor.coladd == old_cursor.coladd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {
7099 curwin->w_cursor.lnum = VIsual.lnum;
7100 if (old_cursor.lnum <= VIsual.lnum && *p_sel == 'e')
7101 ++right;
7102 coladvance(right);
7103 VIsual = curwin->w_cursor;
7104
7105 curwin->w_cursor.lnum = old_cursor.lnum;
7106 coladvance(left);
7107 curwin->w_curswant = left;
7108 }
7109 }
7110 else
7111 {
7112 old_cursor = curwin->w_cursor;
7113 curwin->w_cursor = VIsual;
7114 VIsual = old_cursor;
7115 curwin->w_set_curswant = TRUE;
7116 }
7117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
7119/*
7120 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7121 */
7122 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007123nv_Replace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 if (VIsual_active) /* "R" is replace lines */
7126 {
7127 cap->cmdchar = 'c';
7128 cap->nchar = NUL;
Bram Moolenaara390bb62013-03-13 19:02:41 +01007129 VIsual_mode_orig = VIsual_mode; /* remember original area for gv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 VIsual_mode = 'V';
7131 nv_operator(cap);
7132 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007133 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
7135 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007136 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 else
7138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 if (virtual_active())
7140 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 invoke_edit(cap, FALSE, cap->arg ? 'V' : 'R', FALSE);
7142 }
7143 }
7144}
7145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146/*
7147 * "gr".
7148 */
7149 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007150nv_vreplace(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 if (VIsual_active)
7153 {
7154 cap->cmdchar = 'r';
7155 cap->nchar = cap->extra_char;
7156 nv_replace(cap); /* Do same as "r" in Visual mode for now */
7157 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007158 else if (!checkclearopq(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 {
7160 if (!curbuf->b_p_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007161 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 else
7163 {
7164 if (cap->extra_char == Ctrl_V) /* get another character */
7165 cap->extra_char = get_literal();
7166 stuffcharReadbuff(cap->extra_char);
7167 stuffcharReadbuff(ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 if (virtual_active())
7169 coladvance(getviscol());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 invoke_edit(cap, TRUE, 'v', FALSE);
7171 }
7172 }
7173}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
7175/*
7176 * Swap case for "~" command, when it does not work like an operator.
7177 */
7178 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007179n_swapchar(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180{
7181 long n;
7182 pos_T startpos;
7183 int did_change = 0;
7184#ifdef FEAT_NETBEANS_INTG
7185 pos_T pos;
7186 char_u *ptr;
7187 int count;
7188#endif
7189
7190 if (checkclearopq(cap->oap))
7191 return;
7192
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007193 if (LINEEMPTY(curwin->w_cursor.lnum) && vim_strchr(p_ww, '~') == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
7195 clearopbeep(cap->oap);
7196 return;
7197 }
7198
7199 prep_redo_cmd(cap);
7200
7201 if (u_save_cursor() == FAIL)
7202 return;
7203
7204 startpos = curwin->w_cursor;
7205#ifdef FEAT_NETBEANS_INTG
7206 pos = startpos;
7207#endif
7208 for (n = cap->count1; n > 0; --n)
7209 {
7210 did_change |= swapchar(cap->oap->op_type, &curwin->w_cursor);
7211 inc_cursor();
7212 if (gchar_cursor() == NUL)
7213 {
7214 if (vim_strchr(p_ww, '~') != NULL
7215 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
7216 {
7217#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007218 if (netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 {
7220 if (did_change)
7221 {
7222 ptr = ml_get(pos.lnum);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007223 count = (int)STRLEN(ptr) - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007224 netbeans_removed(curbuf, pos.lnum, pos.col,
7225 (long)count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 netbeans_inserted(curbuf, pos.lnum, pos.col,
Bram Moolenaar009b2592004-10-24 19:18:58 +00007227 &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 }
7229 pos.col = 0;
7230 pos.lnum++;
7231 }
7232#endif
7233 ++curwin->w_cursor.lnum;
7234 curwin->w_cursor.col = 0;
7235 if (n > 1)
7236 {
7237 if (u_savesub(curwin->w_cursor.lnum) == FAIL)
7238 break;
7239 u_clearline();
7240 }
7241 }
7242 else
7243 break;
7244 }
7245 }
7246#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02007247 if (did_change && netbeans_active())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
7249 ptr = ml_get(pos.lnum);
7250 count = curwin->w_cursor.col - pos.col;
Bram Moolenaar009b2592004-10-24 19:18:58 +00007251 netbeans_removed(curbuf, pos.lnum, pos.col, (long)count);
7252 netbeans_inserted(curbuf, pos.lnum, pos.col, &ptr[pos.col], count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 }
7254#endif
7255
7256
7257 check_cursor();
7258 curwin->w_set_curswant = TRUE;
7259 if (did_change)
7260 {
7261 changed_lines(startpos.lnum, startpos.col, curwin->w_cursor.lnum + 1,
7262 0L);
7263 curbuf->b_op_start = startpos;
7264 curbuf->b_op_end = curwin->w_cursor;
7265 if (curbuf->b_op_end.col > 0)
7266 --curbuf->b_op_end.col;
7267 }
7268}
7269
7270/*
7271 * Move cursor to mark.
7272 */
7273 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007274nv_cursormark(cmdarg_T *cap, int flag, pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276 if (check_mark(pos) == FAIL)
7277 clearop(cap->oap);
7278 else
7279 {
7280 if (cap->cmdchar == '\''
7281 || cap->cmdchar == '`'
7282 || cap->cmdchar == '['
7283 || cap->cmdchar == ']')
7284 setpcmark();
7285 curwin->w_cursor = *pos;
7286 if (flag)
7287 beginline(BL_WHITE | BL_FIX);
7288 else
7289 check_cursor();
7290 }
7291 cap->oap->motion_type = flag ? MLINE : MCHAR;
7292 if (cap->cmdchar == '`')
7293 cap->oap->use_reg_one = TRUE;
7294 cap->oap->inclusive = FALSE; /* ignored if not MCHAR */
7295 curwin->w_set_curswant = TRUE;
7296}
7297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298/*
7299 * Handle commands that are operators in Visual mode.
7300 */
7301 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007302v_visop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303{
7304 static char_u trans[] = "YyDdCcxdXdAAIIrr";
7305
7306 /* Uppercase means linewise, except in block mode, then "D" deletes till
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007307 * the end of the line, and "C" replaces till EOL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 if (isupper(cap->cmdchar))
7309 {
7310 if (VIsual_mode != Ctrl_V)
Bram Moolenaara390bb62013-03-13 19:02:41 +01007311 {
7312 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 else if (cap->cmdchar == 'C' || cap->cmdchar == 'D')
7316 curwin->w_curswant = MAXCOL;
7317 }
7318 cap->cmdchar = *(vim_strchr(trans, cap->cmdchar) + 1);
7319 nv_operator(cap);
7320}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
7322/*
7323 * "s" and "S" commands.
7324 */
7325 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007326nv_subst(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
Bram Moolenaard96ff162018-02-18 22:13:29 +01007328#ifdef FEAT_TERMINAL
7329 /* When showing output of term_dumpdiff() swap the top and botom. */
7330 if (term_swap_diff() == OK)
7331 return;
7332#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02007333#ifdef FEAT_JOB_CHANNEL
7334 if (bt_prompt(curbuf) && !prompt_curpos_editable())
7335 {
7336 clearopbeep(cap->oap);
7337 return;
7338 }
7339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 if (VIsual_active) /* "vs" and "vS" are the same as "vc" */
7341 {
7342 if (cap->cmdchar == 'S')
Bram Moolenaara390bb62013-03-13 19:02:41 +01007343 {
7344 VIsual_mode_orig = VIsual_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 VIsual_mode = 'V';
Bram Moolenaara390bb62013-03-13 19:02:41 +01007346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 cap->cmdchar = 'c';
7348 nv_operator(cap);
7349 }
7350 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 nv_optrans(cap);
7352}
7353
7354/*
7355 * Abbreviated commands.
7356 */
7357 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007358nv_abbrev(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
7360 if (cap->cmdchar == K_DEL || cap->cmdchar == K_KDEL)
7361 cap->cmdchar = 'x'; /* DEL key behaves like 'x' */
7362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 /* in Visual mode these commands are operators */
7364 if (VIsual_active)
7365 v_visop(cap);
7366 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 nv_optrans(cap);
7368}
7369
7370/*
7371 * Translate a command into another command.
7372 */
7373 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007374nv_optrans(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
7376 static char_u *(ar[8]) = {(char_u *)"dl", (char_u *)"dh",
7377 (char_u *)"d$", (char_u *)"c$",
7378 (char_u *)"cl", (char_u *)"cc",
7379 (char_u *)"yy", (char_u *)":s\r"};
7380 static char_u *str = (char_u *)"xXDCsSY&";
7381
7382 if (!checkclearopq(cap->oap))
7383 {
Bram Moolenaar7a9bd7c2019-09-17 22:42:55 +02007384 // In Vi "2D" doesn't delete the next line. Can't translate it
7385 // either, because "2." should also not use the count.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007386 if (cap->cmdchar == 'D' && vim_strchr(p_cpo, CPO_HASH) != NULL)
7387 {
7388 cap->oap->start = curwin->w_cursor;
7389 cap->oap->op_type = OP_DELETE;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00007390#ifdef FEAT_EVAL
7391 set_op_var(OP_DELETE);
7392#endif
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007393 cap->count1 = 1;
7394 nv_dollar(cap);
7395 finish_op = TRUE;
7396 ResetRedobuff();
7397 AppendCharToRedobuff('D');
7398 }
7399 else
7400 {
7401 if (cap->count0)
7402 stuffnumReadbuff(cap->count0);
Bram Moolenaar56ebbab2019-09-20 13:40:14 +02007403 stuffReadbuff(ar[(int)(vim_strchr(str, cap->cmdchar) - str)]);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406 cap->opcount = 0;
7407}
7408
7409/*
7410 * "'" and "`" commands. Also for "g'" and "g`".
7411 * cap->arg is TRUE for "'" and "g'".
7412 */
7413 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007414nv_gomark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 pos_T *pos;
7417 int c;
7418#ifdef FEAT_FOLDING
Bram Moolenaar8754deb2013-01-17 13:24:08 +01007419 pos_T old_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7421#endif
7422
7423 if (cap->cmdchar == 'g')
7424 c = cap->extra_char;
7425 else
7426 c = cap->nchar;
7427 pos = getmark(c, (cap->oap->op_type == OP_NOP));
7428 if (pos == (pos_T *)-1) /* jumped to other file */
7429 {
7430 if (cap->arg)
7431 {
7432 check_cursor_lnum();
7433 beginline(BL_WHITE | BL_FIX);
7434 }
7435 else
7436 check_cursor();
7437 }
7438 else
7439 nv_cursormark(cap, cap->arg, pos);
7440
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 /* May need to clear the coladd that a mark includes. */
7442 if (!virtual_active())
7443 curwin->w_cursor.coladd = 0;
Bram Moolenaar9aa15692017-08-19 15:05:32 +02007444 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445#ifdef FEAT_FOLDING
7446 if (cap->oap->op_type == OP_NOP
Bram Moolenaar15364d72013-01-24 21:00:20 +01007447 && pos != NULL
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007448 && (pos == (pos_T *)-1 || !EQUAL_POS(old_cursor, *pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 && (fdo_flags & FDO_MARK)
7450 && old_KeyTyped)
7451 foldOpenCursor();
7452#endif
7453}
7454
7455/*
7456 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7457 */
7458 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007459nv_pcmark(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460{
7461#ifdef FEAT_JUMPLIST
7462 pos_T *pos;
7463# ifdef FEAT_FOLDING
7464 linenr_T lnum = curwin->w_cursor.lnum;
7465 int old_KeyTyped = KeyTyped; /* getting file may reset it */
7466# endif
7467
7468 if (!checkclearopq(cap->oap))
7469 {
7470 if (cap->cmdchar == 'g')
7471 pos = movechangelist((int)cap->count1);
7472 else
7473 pos = movemark((int)cap->count1);
7474 if (pos == (pos_T *)-1) /* jump to other file */
7475 {
7476 curwin->w_set_curswant = TRUE;
7477 check_cursor();
7478 }
7479 else if (pos != NULL) /* can jump */
7480 nv_cursormark(cap, FALSE, pos);
7481 else if (cap->cmdchar == 'g')
7482 {
7483 if (curbuf->b_changelistlen == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007484 emsg(_("E664: changelist is empty"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 else if (cap->count1 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007486 emsg(_("E662: At start of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007488 emsg(_("E663: At end of changelist"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
7490 else
7491 clearopbeep(cap->oap);
7492# ifdef FEAT_FOLDING
7493 if (cap->oap->op_type == OP_NOP
7494 && (pos == (pos_T *)-1 || lnum != curwin->w_cursor.lnum)
7495 && (fdo_flags & FDO_MARK)
7496 && old_KeyTyped)
7497 foldOpenCursor();
7498# endif
7499 }
7500#else
7501 clearopbeep(cap->oap);
7502#endif
7503}
7504
7505/*
7506 * Handle '"' command.
7507 */
7508 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007509nv_regname(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510{
7511 if (checkclearop(cap->oap))
7512 return;
7513#ifdef FEAT_EVAL
7514 if (cap->nchar == '=')
7515 cap->nchar = get_expr_register();
7516#endif
7517 if (cap->nchar != NUL && valid_yank_reg(cap->nchar, FALSE))
7518 {
7519 cap->oap->regname = cap->nchar;
7520 cap->opcount = cap->count0; /* remember count before '"' */
7521#ifdef FEAT_EVAL
7522 set_reg_var(cap->oap->regname);
7523#endif
7524 }
7525 else
7526 clearopbeep(cap->oap);
7527}
7528
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529/*
7530 * Handle "v", "V" and "CTRL-V" commands.
7531 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7532 * is TRUE.
Bram Moolenaardf177f62005-02-22 08:39:57 +00007533 * Handle CTRL-Q just like CTRL-V.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 */
7535 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007536nv_visual(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007538 if (cap->cmdchar == Ctrl_Q)
7539 cap->cmdchar = Ctrl_V;
7540
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7542 * characterwise, linewise, or blockwise. */
7543 if (cap->oap->op_type != OP_NOP)
7544 {
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01007545 motion_force = cap->oap->motion_force = cap->cmdchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 finish_op = FALSE; /* operator doesn't finish now but later */
7547 return;
7548 }
7549
7550 VIsual_select = cap->arg;
7551 if (VIsual_active) /* change Visual mode */
7552 {
7553 if (VIsual_mode == cap->cmdchar) /* stop visual mode */
7554 end_visual_mode();
7555 else /* toggle char/block mode */
7556 { /* or char/line mode */
7557 VIsual_mode = cap->cmdchar;
7558 showmode();
7559 }
7560 redraw_curbuf_later(INVERTED); /* update the inversion */
7561 }
7562 else /* start Visual mode */
7563 {
7564 check_visual_highlight();
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007565 if (cap->count0 > 0 && resel_VIsual_mode != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007567 /* use previously selected part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 VIsual = curwin->w_cursor;
7569
7570 VIsual_active = TRUE;
7571 VIsual_reselect = TRUE;
7572 if (!cap->arg)
7573 /* start Select mode when 'selectmode' contains "cmd" */
7574 may_start_select('c');
7575#ifdef FEAT_MOUSE
7576 setmouse();
7577#endif
Bram Moolenaar09df3122006-01-23 22:23:09 +00007578 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 redraw_cmdline = TRUE; /* show visual mode later */
7580 /*
7581 * For V and ^V, we multiply the number of lines even if there
7582 * was only one -- webb
7583 */
7584 if (resel_VIsual_mode != 'v' || resel_VIsual_line_count > 1)
7585 {
7586 curwin->w_cursor.lnum +=
7587 resel_VIsual_line_count * cap->count0 - 1;
7588 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
7589 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7590 }
7591 VIsual_mode = resel_VIsual_mode;
7592 if (VIsual_mode == 'v')
7593 {
7594 if (resel_VIsual_line_count <= 1)
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007595 {
7596 validate_virtcol();
7597 curwin->w_curswant = curwin->w_virtcol
7598 + resel_VIsual_vcol * cap->count0 - 1;
7599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 else
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007601 curwin->w_curswant = resel_VIsual_vcol;
7602 coladvance(curwin->w_curswant);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 }
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007604 if (resel_VIsual_vcol == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 {
7606 curwin->w_curswant = MAXCOL;
7607 coladvance((colnr_T)MAXCOL);
7608 }
7609 else if (VIsual_mode == Ctrl_V)
7610 {
7611 validate_virtcol();
7612 curwin->w_curswant = curwin->w_virtcol
Bram Moolenaarca0c9fc2011-10-04 21:22:44 +02007613 + resel_VIsual_vcol * cap->count0 - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 coladvance(curwin->w_curswant);
7615 }
7616 else
7617 curwin->w_set_curswant = TRUE;
7618 redraw_curbuf_later(INVERTED); /* show the inversion */
7619 }
7620 else
7621 {
7622 if (!cap->arg)
7623 /* start Select mode when 'selectmode' contains "cmd" */
7624 may_start_select('c');
7625 n_start_visual_mode(cap->cmdchar);
Bram Moolenaar6057b9c2012-05-25 13:12:36 +02007626 if (VIsual_mode != 'V' && *p_sel == 'e')
7627 ++cap->count1; /* include one more char */
7628 if (cap->count0 > 0 && --cap->count1 > 0)
7629 {
7630 /* With a count select that many characters or lines. */
7631 if (VIsual_mode == 'v' || VIsual_mode == Ctrl_V)
7632 nv_right(cap);
7633 else if (VIsual_mode == 'V')
7634 nv_down(cap);
7635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 }
7637 }
7638}
7639
7640/*
7641 * Start selection for Shift-movement keys.
7642 */
7643 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007644start_selection(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645{
7646 /* if 'selectmode' contains "key", start Select mode */
7647 may_start_select('k');
7648 n_start_visual_mode('v');
7649}
7650
7651/*
7652 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7653 */
7654 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007655may_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
7657 VIsual_select = (stuff_empty() && typebuf_typed()
7658 && (vim_strchr(p_slm, c) != NULL));
7659}
7660
7661/*
7662 * Start Visual mode "c".
7663 * Should set VIsual_select before calling this.
7664 */
7665 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007666n_start_visual_mode(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007668#ifdef FEAT_CONCEAL
7669 /* Check for redraw before changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007670 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007671#endif
7672
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 VIsual_mode = c;
7674 VIsual_active = TRUE;
7675 VIsual_reselect = TRUE;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01007676
7677 // Corner case: the 0 position in a tab may change when going into
7678 // virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 if (c == Ctrl_V && (ve_flags & VE_BLOCK) && gchar_cursor() == TAB)
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007680 {
7681 validate_virtcol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 coladvance(curwin->w_virtcol);
Bram Moolenaar2dac2132012-08-15 13:31:00 +02007683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 VIsual = curwin->w_cursor;
7685
7686#ifdef FEAT_FOLDING
7687 foldAdjustVisual();
7688#endif
7689
7690#ifdef FEAT_MOUSE
7691 setmouse();
7692#endif
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007693#ifdef FEAT_CONCEAL
7694 /* Check for redraw after changing the state. */
Bram Moolenaarb9464822018-05-10 15:09:49 +02007695 conceal_check_cursor_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +02007696#endif
7697
Bram Moolenaar09df3122006-01-23 22:23:09 +00007698 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 redraw_cmdline = TRUE; /* show visual mode later */
7700#ifdef FEAT_CLIPBOARD
7701 /* Make sure the clipboard gets updated. Needed because start and
7702 * end may still be the same, and the selection needs to be owned */
7703 clip_star.vmode = NUL;
7704#endif
7705
7706 /* Only need to redraw this line, unless still need to redraw an old
7707 * Visual area (when 'lazyredraw' is set). */
7708 if (curwin->w_redr_type < INVERTED)
7709 {
7710 curwin->w_old_cursor_lnum = curwin->w_cursor.lnum;
7711 curwin->w_old_visual_lnum = curwin->w_cursor.lnum;
7712 }
7713}
7714
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715
7716/*
7717 * CTRL-W: Window commands
7718 */
7719 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007720nv_window(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
Bram Moolenaar938783d2017-07-16 20:13:26 +02007722 if (cap->nchar == ':')
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007723 {
Bram Moolenaar938783d2017-07-16 20:13:26 +02007724 /* "CTRL-W :" is the same as typing ":"; useful in a terminal window */
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007725 cap->cmdchar = ':';
7726 cap->nchar = NUL;
Bram Moolenaar938783d2017-07-16 20:13:26 +02007727 nv_colon(cap);
Bram Moolenaar2efb3232017-12-19 12:27:23 +01007728 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02007729 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 do_window(cap->nchar, cap->count0, NUL); /* everything is in window.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731}
7732
7733/*
7734 * CTRL-Z: Suspend
7735 */
7736 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007737nv_suspend(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739 clearop(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 if (VIsual_active)
7741 end_visual_mode(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 do_cmdline_cmd((char_u *)"st");
7743}
7744
7745/*
7746 * Commands starting with "g".
7747 */
7748 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007749nv_g_cmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750{
7751 oparg_T *oap = cap->oap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 pos_T tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 int i;
7754 int flag = FALSE;
7755
7756 switch (cap->nchar)
7757 {
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007758 case Ctrl_A:
7759 case Ctrl_X:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760#ifdef MEM_PROFILE
7761 /*
7762 * "g^A": dump log of used memory.
7763 */
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007764 if (!VIsual_active && cap->nchar == Ctrl_A)
7765 vim_mem_profile_dump();
7766 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767#endif
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007768 /*
7769 * "g^A/g^X": sequentially increment visually selected region
7770 */
7771 if (VIsual_active)
7772 {
7773 cap->arg = TRUE;
7774 cap->cmdchar = cap->nchar;
Bram Moolenaard79e5502016-01-10 22:13:02 +01007775 cap->nchar = NUL;
Bram Moolenaar3a304b22015-06-25 13:57:36 +02007776 nv_addsub(cap);
7777 }
7778 else
7779 clearopbeep(oap);
7780 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 /*
7783 * "gR": Enter virtual replace mode.
7784 */
7785 case 'R':
7786 cap->arg = TRUE;
7787 nv_Replace(cap);
7788 break;
7789
7790 case 'r':
7791 nv_vreplace(cap);
7792 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
7794 case '&':
7795 do_cmdline_cmd((char_u *)"%s//~/&");
7796 break;
7797
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 /*
7799 * "gv": Reselect the previous Visual area. If Visual already active,
7800 * exchange previous and current Visual area.
7801 */
7802 case 'v':
7803 if (checkclearop(oap))
7804 break;
7805
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007806 if ( curbuf->b_visual.vi_start.lnum == 0
7807 || curbuf->b_visual.vi_start.lnum > curbuf->b_ml.ml_line_count
7808 || curbuf->b_visual.vi_end.lnum == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 beep_flush();
7810 else
7811 {
7812 /* set w_cursor to the start of the Visual area, tpos to the end */
7813 if (VIsual_active)
7814 {
7815 i = VIsual_mode;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007816 VIsual_mode = curbuf->b_visual.vi_mode;
7817 curbuf->b_visual.vi_mode = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818# ifdef FEAT_EVAL
7819 curbuf->b_visual_mode_eval = i;
7820# endif
7821 i = curwin->w_curswant;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007822 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7823 curbuf->b_visual.vi_curswant = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007825 tpos = curbuf->b_visual.vi_end;
7826 curbuf->b_visual.vi_end = curwin->w_cursor;
7827 curwin->w_cursor = curbuf->b_visual.vi_start;
7828 curbuf->b_visual.vi_start = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830 else
7831 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00007832 VIsual_mode = curbuf->b_visual.vi_mode;
7833 curwin->w_curswant = curbuf->b_visual.vi_curswant;
7834 tpos = curbuf->b_visual.vi_end;
7835 curwin->w_cursor = curbuf->b_visual.vi_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
7837
7838 VIsual_active = TRUE;
7839 VIsual_reselect = TRUE;
7840
7841 /* Set Visual to the start and w_cursor to the end of the Visual
7842 * area. Make sure they are on an existing character. */
7843 check_cursor();
7844 VIsual = curwin->w_cursor;
7845 curwin->w_cursor = tpos;
7846 check_cursor();
7847 update_topline();
7848 /*
7849 * When called from normal "g" command: start Select mode when
7850 * 'selectmode' contains "cmd". When called for K_SELECT, always
7851 * start Select mode.
7852 */
7853 if (cap->arg)
7854 VIsual_select = TRUE;
7855 else
7856 may_start_select('c');
7857#ifdef FEAT_MOUSE
7858 setmouse();
7859#endif
7860#ifdef FEAT_CLIPBOARD
7861 /* Make sure the clipboard gets updated. Needed because start and
7862 * end are still the same, and the selection needs to be owned */
7863 clip_star.vmode = NUL;
7864#endif
7865 redraw_curbuf_later(INVERTED);
7866 showmode();
7867 }
7868 break;
7869 /*
7870 * "gV": Don't reselect the previous Visual area after a Select mode
7871 * mapping of menu.
7872 */
7873 case 'V':
7874 VIsual_reselect = FALSE;
7875 break;
7876
7877 /*
7878 * "gh": start Select mode.
7879 * "gH": start Select line mode.
7880 * "g^H": start Select block mode.
7881 */
7882 case K_BS:
7883 cap->nchar = Ctrl_H;
7884 /* FALLTHROUGH */
7885 case 'h':
7886 case 'H':
7887 case Ctrl_H:
7888# ifdef EBCDIC
7889 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7890 if (cap->nchar == Ctrl_H)
7891 cap->cmdchar = Ctrl_V;
7892 else
7893# endif
7894 cap->cmdchar = cap->nchar + ('v' - 'h');
7895 cap->arg = TRUE;
7896 nv_visual(cap);
7897 break;
Bram Moolenaar641e2862012-07-25 15:06:34 +02007898
7899 /* "gn", "gN" visually select next/previous search match
7900 * "gn" selects next match
7901 * "gN" selects previous match
7902 */
7903 case 'N':
7904 case 'n':
7905 if (!current_search(cap->count1, cap->nchar == 'n'))
Bram Moolenaarf00dc262012-10-21 03:54:33 +02007906 clearopbeep(oap);
Bram Moolenaar641e2862012-07-25 15:06:34 +02007907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908
7909 /*
7910 * "gj" and "gk" two new funny movement keys -- up and down
7911 * movement based on *screen* line rather than *file* line.
7912 */
7913 case 'j':
7914 case K_DOWN:
7915 /* with 'nowrap' it works just like the normal "j" command; also when
7916 * in a closed fold */
7917 if (!curwin->w_p_wrap
7918#ifdef FEAT_FOLDING
7919 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7920#endif
7921 )
7922 {
7923 oap->motion_type = MLINE;
7924 i = cursor_down(cap->count1, oap->op_type == OP_NOP);
7925 }
7926 else
7927 i = nv_screengo(oap, FORWARD, cap->count1);
7928 if (i == FAIL)
7929 clearopbeep(oap);
7930 break;
7931
7932 case 'k':
7933 case K_UP:
7934 /* with 'nowrap' it works just like the normal "k" command; also when
7935 * in a closed fold */
7936 if (!curwin->w_p_wrap
7937#ifdef FEAT_FOLDING
7938 || hasFolding(curwin->w_cursor.lnum, NULL, NULL)
7939#endif
7940 )
7941 {
7942 oap->motion_type = MLINE;
7943 i = cursor_up(cap->count1, oap->op_type == OP_NOP);
7944 }
7945 else
7946 i = nv_screengo(oap, BACKWARD, cap->count1);
7947 if (i == FAIL)
7948 clearopbeep(oap);
7949 break;
7950
7951 /*
7952 * "gJ": join two lines without inserting a space.
7953 */
7954 case 'J':
7955 nv_join(cap);
7956 break;
7957
7958 /*
7959 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7960 * "gm": middle of "g0" and "g$".
7961 */
7962 case '^':
7963 flag = TRUE;
7964 /* FALLTHROUGH */
7965
7966 case '0':
7967 case 'm':
7968 case K_HOME:
7969 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 oap->motion_type = MCHAR;
7971 oap->inclusive = FALSE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007972 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {
Bram Moolenaar02631462017-09-22 15:20:32 +02007974 int width1 = curwin->w_width - curwin_col_off();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 int width2 = width1 + curwin_col_off2();
7976
7977 validate_virtcol();
7978 i = 0;
7979 if (curwin->w_virtcol >= (colnr_T)width1 && width2 > 0)
7980 i = (curwin->w_virtcol - width1) / width2 * width2 + width1;
7981 }
7982 else
7983 i = curwin->w_leftcol;
Bram Moolenaar64486672010-05-16 15:46:46 +02007984 /* Go to the middle of the screen line. When 'number' or
7985 * 'relativenumber' is on and lines are wrapping the middle can be more
7986 * to the left. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 if (cap->nchar == 'm')
Bram Moolenaar02631462017-09-22 15:20:32 +02007988 i += (curwin->w_width - curwin_col_off()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 + ((curwin->w_p_wrap && i > 0)
7990 ? curwin_col_off2() : 0)) / 2;
7991 coladvance((colnr_T)i);
7992 if (flag)
7993 {
7994 do
7995 i = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01007996 while (VIM_ISWHITE(i) && oneright() == OK);
Bram Moolenaarf9514162018-11-22 03:08:29 +01007997 curwin->w_valid &= ~VALID_WCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 }
7999 curwin->w_set_curswant = TRUE;
8000 break;
8001
8002 case '_':
8003 /* "g_": to the last non-blank character in the line or <count> lines
8004 * downward. */
8005 cap->oap->motion_type = MCHAR;
8006 cap->oap->inclusive = TRUE;
8007 curwin->w_curswant = MAXCOL;
8008 if (cursor_down((long)(cap->count1 - 1),
8009 cap->oap->op_type == OP_NOP) == FAIL)
8010 clearopbeep(cap->oap);
8011 else
8012 {
8013 char_u *ptr = ml_get_curline();
8014
8015 /* In Visual mode we may end up after the line. */
8016 if (curwin->w_cursor.col > 0 && ptr[curwin->w_cursor.col] == NUL)
8017 --curwin->w_cursor.col;
8018
8019 /* Decrease the cursor column until it's on a non-blank. */
8020 while (curwin->w_cursor.col > 0
Bram Moolenaar1c465442017-03-12 20:10:05 +01008021 && VIM_ISWHITE(ptr[curwin->w_cursor.col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 --curwin->w_cursor.col;
8023 curwin->w_set_curswant = TRUE;
Bram Moolenaar5890b2c2010-01-12 15:42:37 +01008024 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 }
8026 break;
8027
8028 case '$':
8029 case K_END:
8030 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {
8032 int col_off = curwin_col_off();
8033
8034 oap->motion_type = MCHAR;
8035 oap->inclusive = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02008036 if (curwin->w_p_wrap && curwin->w_width != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {
8038 curwin->w_curswant = MAXCOL; /* so we stay at the end */
8039 if (cap->count1 == 1)
8040 {
Bram Moolenaar02631462017-09-22 15:20:32 +02008041 int width1 = curwin->w_width - col_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 int width2 = width1 + curwin_col_off2();
8043
8044 validate_virtcol();
8045 i = width1 - 1;
8046 if (curwin->w_virtcol >= (colnr_T)width1)
8047 i += ((curwin->w_virtcol - width1) / width2 + 1)
8048 * width2;
8049 coladvance((colnr_T)i);
Bram Moolenaarb69510e2013-07-09 17:08:29 +02008050
8051 /* Make sure we stick in this column. */
8052 validate_virtcol();
8053 curwin->w_curswant = curwin->w_virtcol;
8054 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 if (curwin->w_cursor.col > 0 && curwin->w_p_wrap)
8056 {
8057 /*
8058 * Check for landing on a character that got split at
8059 * the end of the line. We do not want to advance to
8060 * the next screen line.
8061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 if (curwin->w_virtcol > (colnr_T)i)
8063 --curwin->w_cursor.col;
8064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 }
8066 else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == FAIL)
8067 clearopbeep(oap);
8068 }
8069 else
8070 {
Bram Moolenaard5c82342019-07-27 18:44:57 +02008071 if (cap->count1 > 1)
8072 // if it fails, let the cursor still move to the last char
Bram Moolenaar9c272a92019-08-16 21:54:27 +02008073 (void)cursor_down(cap->count1 - 1, FALSE);
Bram Moolenaard5c82342019-07-27 18:44:57 +02008074
Bram Moolenaar02631462017-09-22 15:20:32 +02008075 i = curwin->w_leftcol + curwin->w_width - col_off - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 coladvance((colnr_T)i);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008077
Bram Moolenaard5c82342019-07-27 18:44:57 +02008078 // Make sure we stick in this column.
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008079 validate_virtcol();
8080 curwin->w_curswant = curwin->w_virtcol;
8081 curwin->w_set_curswant = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 }
8083 }
8084 break;
8085
8086 /*
8087 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
8088 */
8089 case '*':
8090 case '#':
8091#if POUND != '#'
8092 case POUND: /* pound sign (sometimes equal to '#') */
8093#endif
8094 case Ctrl_RSB: /* :tag or :tselect for current identifier */
8095 case ']': /* :tselect for current identifier */
8096 nv_ident(cap);
8097 break;
8098
8099 /*
8100 * ge and gE: go back to end of word
8101 */
8102 case 'e':
8103 case 'E':
8104 oap->motion_type = MCHAR;
8105 curwin->w_set_curswant = TRUE;
8106 oap->inclusive = TRUE;
8107 if (bckend_word(cap->count1, cap->nchar == 'E', FALSE) == FAIL)
8108 clearopbeep(oap);
8109 break;
8110
8111 /*
8112 * "g CTRL-G": display info about cursor position
8113 */
8114 case Ctrl_G:
Bram Moolenaared767a22016-01-03 22:49:16 +01008115 cursor_pos_info(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 break;
8117
8118 /*
8119 * "gi": start Insert at the last position.
8120 */
8121 case 'i':
8122 if (curbuf->b_last_insert.lnum != 0)
8123 {
8124 curwin->w_cursor = curbuf->b_last_insert;
8125 check_cursor_lnum();
8126 i = (int)STRLEN(ml_get_curline());
8127 if (curwin->w_cursor.col > (colnr_T)i)
8128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 if (virtual_active())
8130 curwin->w_cursor.coladd += curwin->w_cursor.col - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 curwin->w_cursor.col = i;
8132 }
8133 }
8134 cap->cmdchar = 'i';
8135 nv_edit(cap);
8136 break;
8137
8138 /*
8139 * "gI": Start insert in column 1.
8140 */
8141 case 'I':
8142 beginline(0);
8143 if (!checkclearopq(oap))
8144 invoke_edit(cap, FALSE, 'g', FALSE);
8145 break;
8146
8147#ifdef FEAT_SEARCHPATH
8148 /*
8149 * "gf": goto file, edit file under cursor
8150 * "]f" and "[f": can also be used.
8151 */
8152 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008153 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 nv_gotofile(cap);
8155 break;
8156#endif
8157
8158 /* "g'm" and "g`m": jump to mark without setting pcmark */
8159 case '\'':
8160 cap->arg = TRUE;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008161 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 case '`':
8163 nv_gomark(cap);
8164 break;
8165
8166 /*
8167 * "gs": Goto sleep.
8168 */
8169 case 's':
8170 do_sleep(cap->count1 * 1000L);
8171 break;
8172
8173 /*
8174 * "ga": Display the ascii value of the character under the
8175 * cursor. It is displayed in decimal, hex, and octal. -- webb
8176 */
8177 case 'a':
8178 do_ascii(NULL);
8179 break;
8180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 /*
8182 * "g8": Display the bytes used for the UTF-8 character under the
8183 * cursor. It is displayed in hex.
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008184 * "8g8" finds illegal byte sequence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 */
8186 case '8':
Bram Moolenaara83c3e02006-03-17 23:10:44 +00008187 if (cap->count0 == 8)
8188 utf_find_illegal();
8189 else
8190 show_utf8();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192
Bram Moolenaar60402d62017-04-20 18:54:50 +02008193 /* "g<": show scrollback text */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00008194 case '<':
8195 show_sb_text();
8196 break;
8197
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 /*
8199 * "gg": Goto the first line in file. With a count it goes to
8200 * that line number like for "G". -- webb
8201 */
8202 case 'g':
8203 cap->arg = FALSE;
8204 nv_goto(cap);
8205 break;
8206
8207 /*
8208 * Two-character operators:
8209 * "gq" Format text
8210 * "gw" Format text and keep cursor position
8211 * "g~" Toggle the case of the text.
8212 * "gu" Change text to lower case.
8213 * "gU" Change text to upper case.
8214 * "g?" rot13 encoding
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008215 * "g@" call 'operatorfunc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 */
8217 case 'q':
8218 case 'w':
8219 oap->cursor_start = curwin->w_cursor;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02008220 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 case '~':
8222 case 'u':
8223 case 'U':
8224 case '?':
Bram Moolenaar12033fb2005-12-16 21:49:31 +00008225 case '@':
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 nv_operator(cap);
8227 break;
8228
8229 /*
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008230 * "gd": Find first occurrence of pattern under the cursor in the
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 * current function
8232 * "gD": idem, but in the current file.
8233 */
8234 case 'd':
8235 case 'D':
Bram Moolenaarf75a9632005-09-13 21:20:47 +00008236 nv_gd(oap, cap->nchar, (int)cap->count0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 break;
8238
8239#ifdef FEAT_MOUSE
8240 /*
8241 * g<*Mouse> : <C-*mouse>
8242 */
8243 case K_MIDDLEMOUSE:
8244 case K_MIDDLEDRAG:
8245 case K_MIDDLERELEASE:
8246 case K_LEFTMOUSE:
8247 case K_LEFTDRAG:
8248 case K_LEFTRELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01008249 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 case K_RIGHTMOUSE:
8251 case K_RIGHTDRAG:
8252 case K_RIGHTRELEASE:
8253 case K_X1MOUSE:
8254 case K_X1DRAG:
8255 case K_X1RELEASE:
8256 case K_X2MOUSE:
8257 case K_X2DRAG:
8258 case K_X2RELEASE:
8259 mod_mask = MOD_MASK_CTRL;
8260 (void)do_mouse(oap, cap->nchar, BACKWARD, cap->count1, 0);
8261 break;
8262#endif
8263
8264 case K_IGNORE:
8265 break;
8266
8267 /*
8268 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8269 */
8270 case 'p':
8271 case 'P':
8272 nv_put(cap);
8273 break;
8274
8275#ifdef FEAT_BYTEOFF
8276 /* "go": goto byte count from start of buffer */
8277 case 'o':
8278 goto_byte(cap->count0);
8279 break;
8280#endif
8281
8282 /* "gQ": improved Ex mode */
8283 case 'Q':
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008284 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
8286 clearopbeep(cap->oap);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00008287 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 break;
8289 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00008290
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 if (!checkclearopq(oap))
8292 do_exmode(TRUE);
8293 break;
8294
8295#ifdef FEAT_JUMPLIST
8296 case ',':
8297 nv_pcmark(cap);
8298 break;
8299
8300 case ';':
8301 cap->count1 = -cap->count1;
8302 nv_pcmark(cap);
8303 break;
8304#endif
8305
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008306 case 't':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008307 if (!checkclearop(oap))
8308 goto_tabpage((int)cap->count0);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008309 break;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008310 case 'T':
Bram Moolenaar89f940f2012-06-29 13:56:06 +02008311 if (!checkclearop(oap))
8312 goto_tabpage(-(int)cap->count1);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00008313 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008314
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008315 case '+':
8316 case '-': /* "g+" and "g-": undo or redo along the timeline */
8317 if (!checkclearopq(oap))
Bram Moolenaard3667a22006-03-16 21:35:52 +00008318 undo_time(cap->nchar == '-' ? -cap->count1 : cap->count1,
Bram Moolenaar730cde92010-06-27 05:18:54 +02008319 FALSE, FALSE, FALSE);
Bram Moolenaar35a2e192006-03-13 22:07:11 +00008320 break;
8321
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 default:
8323 clearopbeep(oap);
8324 break;
8325 }
8326}
8327
8328/*
8329 * Handle "o" and "O" commands.
8330 */
8331 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008332n_opencmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333{
Bram Moolenaar860cae12010-06-05 23:22:07 +02008334#ifdef FEAT_CONCEAL
8335 linenr_T oldline = curwin->w_cursor.lnum;
8336#endif
8337
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 if (!checkclearopq(cap->oap))
8339 {
8340#ifdef FEAT_FOLDING
8341 if (cap->cmdchar == 'O')
8342 /* Open above the first line of a folded sequence of lines */
8343 (void)hasFolding(curwin->w_cursor.lnum,
8344 &curwin->w_cursor.lnum, NULL);
8345 else
8346 /* Open below the last line of a folded sequence of lines */
8347 (void)hasFolding(curwin->w_cursor.lnum,
8348 NULL, &curwin->w_cursor.lnum);
8349#endif
8350 if (u_save((linenr_T)(curwin->w_cursor.lnum -
8351 (cap->cmdchar == 'O' ? 1 : 0)),
8352 (linenr_T)(curwin->w_cursor.lnum +
8353 (cap->cmdchar == 'o' ? 1 : 0))
8354 ) == OK
8355 && open_line(cap->cmdchar == 'O' ? BACKWARD : FORWARD,
8356#ifdef FEAT_COMMENTS
8357 has_format_option(FO_OPEN_COMS) ? OPENLINE_DO_COM :
8358#endif
Bram Moolenaar24a2d722018-04-24 19:36:43 +02008359 0, 0) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02008361#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02008362 if (curwin->w_p_cole > 0 && oldline != curwin->w_cursor.lnum)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01008363 redrawWinline(curwin, oldline);
Bram Moolenaar860cae12010-06-05 23:22:07 +02008364#endif
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008365#ifdef FEAT_SYN_HL
Bram Moolenaard0d0fe02015-06-09 19:23:46 +02008366 if (curwin->w_p_cul)
8367 /* force redraw of cursorline */
8368 curwin->w_valid &= ~VALID_CROW;
Bram Moolenaard710e0d2015-06-10 12:16:47 +02008369#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008370 /* When '#' is in 'cpoptions' ignore the count. */
8371 if (vim_strchr(p_cpo, CPO_HASH) != NULL)
8372 cap->count1 = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 invoke_edit(cap, FALSE, cap->cmdchar, TRUE);
8374 }
8375 }
8376}
8377
8378/*
8379 * "." command: redo last change.
8380 */
8381 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008382nv_dot(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
8384 if (!checkclearopq(cap->oap))
8385 {
8386 /*
8387 * If "restart_edit" is TRUE, the last but one command is repeated
8388 * instead of the last command (inserting text). This is used for
8389 * CTRL-O <.> in insert mode.
8390 */
8391 if (start_redo(cap->count0, restart_edit != 0 && !arrow_used) == FAIL)
8392 clearopbeep(cap->oap);
8393 }
8394}
8395
8396/*
8397 * CTRL-R: undo undo
8398 */
8399 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008400nv_redo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401{
8402 if (!checkclearopq(cap->oap))
8403 {
8404 u_redo((int)cap->count1);
8405 curwin->w_set_curswant = TRUE;
8406 }
8407}
8408
8409/*
8410 * Handle "U" command.
8411 */
8412 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008413nv_Undo(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414{
8415 /* In Visual mode and typing "gUU" triggers an operator */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008416 if (cap->oap->op_type == OP_UPPER || VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {
8418 /* translate "gUU" to "gUgU" */
8419 cap->cmdchar = 'g';
8420 cap->nchar = 'U';
8421 nv_operator(cap);
8422 }
8423 else if (!checkclearopq(cap->oap))
8424 {
8425 u_undoline();
8426 curwin->w_set_curswant = TRUE;
8427 }
8428}
8429
8430/*
8431 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8432 * single character.
8433 */
8434 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008435nv_tilde(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436{
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008437 if (!p_to && !VIsual_active && cap->oap->op_type != OP_TILDE)
Bram Moolenaarf2732452018-06-03 14:47:35 +02008438 {
8439#ifdef FEAT_JOB_CHANNEL
8440 if (bt_prompt(curbuf) && !prompt_curpos_editable())
8441 {
8442 clearopbeep(cap->oap);
8443 return;
8444 }
8445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 n_swapchar(cap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 else
8449 nv_operator(cap);
8450}
8451
8452/*
8453 * Handle an operator command.
8454 * The actual work is done by do_pending_operator().
8455 */
8456 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008457nv_operator(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458{
8459 int op_type;
8460
8461 op_type = get_op_type(cap->cmdchar, cap->nchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008462#ifdef FEAT_JOB_CHANNEL
8463 if (bt_prompt(curbuf) && op_is_change(op_type) && !prompt_curpos_editable())
8464 {
8465 clearopbeep(cap->oap);
8466 return;
8467 }
8468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469
8470 if (op_type == cap->oap->op_type) /* double operator works on lines */
8471 nv_lineop(cap);
8472 else if (!checkclearop(cap->oap))
8473 {
8474 cap->oap->start = curwin->w_cursor;
8475 cap->oap->op_type = op_type;
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008476#ifdef FEAT_EVAL
8477 set_op_var(op_type);
8478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 }
8480}
8481
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008482#ifdef FEAT_EVAL
8483/*
8484 * Set v:operator to the characters for "optype".
8485 */
8486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008487set_op_var(int optype)
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +00008488{
8489 char_u opchars[3];
8490
8491 if (optype == OP_NOP)
8492 set_vim_var_string(VV_OP, NULL, 0);
8493 else
8494 {
8495 opchars[0] = get_op_char(optype);
8496 opchars[1] = get_extra_op_char(optype);
8497 opchars[2] = NUL;
8498 set_vim_var_string(VV_OP, opchars, -1);
8499 }
8500}
8501#endif
8502
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503/*
8504 * Handle linewise operator "dd", "yy", etc.
8505 *
8506 * "_" is is a strange motion command that helps make operators more logical.
8507 * It is actually implemented, but not documented in the real Vi. This motion
8508 * command actually refers to "the current line". Commands like "dd" and "yy"
8509 * are really an alternate form of "d_" and "y_". It does accept a count, so
8510 * "d3_" works to delete 3 lines.
8511 */
8512 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008513nv_lineop(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
8515 cap->oap->motion_type = MLINE;
8516 if (cursor_down(cap->count1 - 1L, cap->oap->op_type == OP_NOP) == FAIL)
8517 clearopbeep(cap->oap);
Bram Moolenaar83dadaf2012-12-12 17:33:32 +01008518 else if ( (cap->oap->op_type == OP_DELETE /* only with linewise motions */
8519 && cap->oap->motion_force != 'v'
8520 && cap->oap->motion_force != Ctrl_V)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 || cap->oap->op_type == OP_LSHIFT
8522 || cap->oap->op_type == OP_RSHIFT)
8523 beginline(BL_SOL | BL_FIX);
8524 else if (cap->oap->op_type != OP_YANK) /* 'Y' does not move cursor */
8525 beginline(BL_WHITE | BL_FIX);
8526}
8527
8528/*
8529 * <Home> command.
8530 */
8531 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008532nv_home(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00008534 /* CTRL-HOME is like "gg" */
8535 if (mod_mask & MOD_MASK_CTRL)
8536 nv_goto(cap);
8537 else
8538 {
8539 cap->count0 = 1;
8540 nv_pipe(cap);
8541 }
Bram Moolenaara88d9682005-03-25 21:45:43 +00008542 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8543 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544}
8545
8546/*
8547 * "|" command.
8548 */
8549 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008550nv_pipe(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
8552 cap->oap->motion_type = MCHAR;
8553 cap->oap->inclusive = FALSE;
8554 beginline(0);
8555 if (cap->count0 > 0)
8556 {
8557 coladvance((colnr_T)(cap->count0 - 1));
8558 curwin->w_curswant = (colnr_T)(cap->count0 - 1);
8559 }
8560 else
8561 curwin->w_curswant = 0;
8562 /* keep curswant at the column where we wanted to go, not where
Bram Moolenaarf82a2d22010-12-17 18:53:01 +01008563 * we ended; differs if line is too short */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 curwin->w_set_curswant = FALSE;
8565}
8566
8567/*
8568 * Handle back-word command "b" and "B".
8569 * cap->arg is 1 for "B"
8570 */
8571 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008572nv_bck_word(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 cap->oap->motion_type = MCHAR;
8575 cap->oap->inclusive = FALSE;
8576 curwin->w_set_curswant = TRUE;
8577 if (bck_word(cap->count1, cap->arg, FALSE) == FAIL)
8578 clearopbeep(cap->oap);
8579#ifdef FEAT_FOLDING
8580 else if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8581 foldOpenCursor();
8582#endif
8583}
8584
8585/*
8586 * Handle word motion commands "e", "E", "w" and "W".
8587 * cap->arg is TRUE for "E" and "W".
8588 */
8589 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008590nv_wordcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
8592 int n;
8593 int word_end;
8594 int flag = FALSE;
Bram Moolenaardfefb982008-04-01 10:06:39 +00008595 pos_T startpos = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596
8597 /*
8598 * Set inclusive for the "E" and "e" command.
8599 */
8600 if (cap->cmdchar == 'e' || cap->cmdchar == 'E')
8601 word_end = TRUE;
8602 else
8603 word_end = FALSE;
8604 cap->oap->inclusive = word_end;
8605
8606 /*
8607 * "cw" and "cW" are a special case.
8608 */
8609 if (!word_end && cap->oap->op_type == OP_CHANGE)
8610 {
8611 n = gchar_cursor();
8612 if (n != NUL) /* not an empty line */
8613 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008614 if (VIM_ISWHITE(n))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 {
8616 /*
8617 * Reproduce a funny Vi behaviour: "cw" on a blank only
8618 * changes one character, not all blanks until the start of
8619 * the next word. Only do this when the 'w' flag is included
8620 * in 'cpoptions'.
8621 */
8622 if (cap->count1 == 1 && vim_strchr(p_cpo, CPO_CW) != NULL)
8623 {
8624 cap->oap->inclusive = TRUE;
8625 cap->oap->motion_type = MCHAR;
8626 return;
8627 }
8628 }
8629 else
8630 {
8631 /*
8632 * This is a little strange. To match what the real Vi does,
8633 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8634 * that we are not on a space or a TAB. This seems impolite
8635 * at first, but it's really more what we mean when we say
8636 * 'cw'.
8637 * Another strangeness: When standing on the end of a word
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008638 * "ce" will change until the end of the next word, but "cw"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 * will change only one character! This is done by setting
8640 * flag.
8641 */
8642 cap->oap->inclusive = TRUE;
8643 word_end = TRUE;
8644 flag = TRUE;
8645 }
8646 }
8647 }
8648
8649 cap->oap->motion_type = MCHAR;
8650 curwin->w_set_curswant = TRUE;
8651 if (word_end)
8652 n = end_word(cap->count1, cap->arg, flag, FALSE);
8653 else
8654 n = fwd_word(cap->count1, cap->arg, cap->oap->op_type != OP_NOP);
8655
Bram Moolenaardfefb982008-04-01 10:06:39 +00008656 /* Don't leave the cursor on the NUL past the end of line. Unless we
8657 * didn't move it forward. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008658 if (LT_POS(startpos, curwin->w_cursor))
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008659 adjust_cursor(cap->oap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
8661 if (n == FAIL && cap->oap->op_type == OP_NOP)
8662 clearopbeep(cap->oap);
8663 else
8664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 adjust_for_sel(cap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666#ifdef FEAT_FOLDING
8667 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8668 foldOpenCursor();
8669#endif
8670 }
8671}
8672
8673/*
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008674 * Used after a movement command: If the cursor ends up on the NUL after the
8675 * end of the line, may move it back to the last character and make the motion
8676 * inclusive.
8677 */
8678 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008679adjust_cursor(oparg_T *oap)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008680{
8681 /* The cursor cannot remain on the NUL when:
8682 * - the column is > 0
8683 * - not in Visual mode or 'selection' is "o"
8684 * - 'virtualedit' is not "all" and not "onemore".
8685 */
8686 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008687 && (!VIsual_active || *p_sel == 'o')
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01008688 && !virtual_active() && (ve_flags & VE_ONEMORE) == 0)
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008689 {
8690 --curwin->w_cursor.col;
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008691 /* prevent cursor from moving on the trail byte */
8692 if (has_mbyte)
8693 mb_adjust_cursor();
Bram Moolenaar1f14d572008-01-12 16:12:10 +00008694 oap->inclusive = TRUE;
8695 }
8696}
8697
8698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 * "0" and "^" commands.
8700 * cap->arg is the argument for beginline().
8701 */
8702 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008703nv_beginline(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
8705 cap->oap->motion_type = MCHAR;
8706 cap->oap->inclusive = FALSE;
8707 beginline(cap->arg);
8708#ifdef FEAT_FOLDING
8709 if ((fdo_flags & FDO_HOR) && KeyTyped && cap->oap->op_type == OP_NOP)
8710 foldOpenCursor();
8711#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +00008712 ins_at_eol = FALSE; /* Don't move cursor past eol (only necessary in a
8713 one-character line). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714}
8715
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716/*
8717 * In exclusive Visual mode, may include the last character.
8718 */
8719 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008720adjust_for_sel(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722 if (VIsual_active && cap->oap->inclusive && *p_sel == 'e'
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008723 && gchar_cursor() != NUL && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 if (has_mbyte)
8726 inc_cursor();
8727 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 ++curwin->w_cursor.col;
8729 cap->oap->inclusive = FALSE;
8730 }
8731}
8732
8733/*
8734 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8735 * Should check VIsual_mode before calling this.
8736 * Returns TRUE when backed up to the previous line.
8737 */
8738 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01008739unadjust_for_sel(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740{
8741 pos_T *pp;
8742
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008743 if (*p_sel == 'e' && !EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01008745 if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 pp = &curwin->w_cursor;
8747 else
8748 pp = &VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 if (pp->coladd > 0)
8750 --pp->coladd;
8751 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 if (pp->col > 0)
8753 {
8754 --pp->col;
Bram Moolenaar03a807a2011-07-07 15:08:58 +02008755 mb_adjustpos(curbuf, pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 }
8757 else if (pp->lnum > 1)
8758 {
8759 --pp->lnum;
8760 pp->col = (colnr_T)STRLEN(ml_get(pp->lnum));
8761 return TRUE;
8762 }
8763 }
8764 return FALSE;
8765}
8766
8767/*
8768 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8769 */
8770 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008771nv_select(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772{
8773 if (VIsual_active)
8774 VIsual_select = TRUE;
8775 else if (VIsual_reselect)
8776 {
8777 cap->nchar = 'v'; /* fake "gv" command */
8778 cap->arg = TRUE;
8779 nv_g_cmd(cap);
8780 }
8781}
8782
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784/*
8785 * "G", "gg", CTRL-END, CTRL-HOME.
8786 * cap->arg is TRUE for "G".
8787 */
8788 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008789nv_goto(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
8791 linenr_T lnum;
8792
8793 if (cap->arg)
8794 lnum = curbuf->b_ml.ml_line_count;
8795 else
8796 lnum = 1L;
8797 cap->oap->motion_type = MLINE;
8798 setpcmark();
8799
8800 /* When a count is given, use it instead of the default lnum */
8801 if (cap->count0 != 0)
8802 lnum = cap->count0;
8803 if (lnum < 1L)
8804 lnum = 1L;
8805 else if (lnum > curbuf->b_ml.ml_line_count)
8806 lnum = curbuf->b_ml.ml_line_count;
8807 curwin->w_cursor.lnum = lnum;
8808 beginline(BL_SOL | BL_FIX);
8809#ifdef FEAT_FOLDING
8810 if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP)
8811 foldOpenCursor();
8812#endif
8813}
8814
8815/*
8816 * CTRL-\ in Normal mode.
8817 */
8818 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008819nv_normal(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 if (cap->nchar == Ctrl_N || cap->nchar == Ctrl_G)
8822 {
8823 clearop(cap->oap);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00008824 if (restart_edit != 0 && mode_displayed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 clear_cmdline = TRUE; /* unshow mode later */
8826 restart_edit = 0;
8827#ifdef FEAT_CMDWIN
8828 if (cmdwin_type != 0)
8829 cmdwin_result = Ctrl_C;
8830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 if (VIsual_active)
8832 {
8833 end_visual_mode(); /* stop Visual */
8834 redraw_curbuf_later(INVERTED);
8835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8837 if (cap->nchar == Ctrl_G && p_im)
8838 restart_edit = 'a';
8839 }
8840 else
8841 clearopbeep(cap->oap);
8842}
8843
8844/*
8845 * ESC in Normal mode: beep, but don't flush buffers.
8846 * Don't even beep if we are canceling a command.
8847 */
8848 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008849nv_esc(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850{
8851 int no_reason;
8852
8853 no_reason = (cap->oap->op_type == OP_NOP
8854 && cap->opcount == 0
8855 && cap->count0 == 0
8856 && cap->oap->regname == 0
8857 && !p_im);
8858
8859 if (cap->arg) /* TRUE for CTRL-C */
8860 {
8861 if (restart_edit == 0
8862#ifdef FEAT_CMDWIN
8863 && cmdwin_type == 0
8864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 && no_reason)
Bram Moolenaara84a3dd2019-03-25 22:21:24 +01008867 {
8868 if (anyBufIsChanged())
8869 msg(_("Type :qa! and press <Enter> to abandon all changes and exit Vim"));
8870 else
8871 msg(_("Type :qa and press <Enter> to exit Vim"));
8872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873
8874 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8875 * set again below when halfway a mapping. */
8876 if (!p_im)
8877 restart_edit = 0;
8878#ifdef FEAT_CMDWIN
8879 if (cmdwin_type != 0)
8880 {
8881 cmdwin_result = K_IGNORE;
8882 got_int = FALSE; /* don't stop executing autocommands et al. */
8883 return;
8884 }
8885#endif
8886 }
8887
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 if (VIsual_active)
8889 {
8890 end_visual_mode(); /* stop Visual */
8891 check_cursor_col(); /* make sure cursor is not beyond EOL */
8892 curwin->w_set_curswant = TRUE;
8893 redraw_curbuf_later(INVERTED);
8894 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008895 else if (no_reason)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008896 vim_beep(BO_ESC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 clearop(cap->oap);
8898
8899 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8900 * set return to Insert mode afterwards. */
Bram Moolenaare2c38102016-01-31 14:55:40 +01008901 if (restart_edit == 0 && goto_im() && ex_normal_busy == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 restart_edit = 'a';
8903}
8904
8905/*
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008906 * Move the cursor for the "A" command.
8907 */
8908 void
8909set_cursor_for_append_to_line(void)
8910{
8911 curwin->w_set_curswant = TRUE;
8912 if (ve_flags == VE_ALL)
8913 {
8914 int save_State = State;
8915
8916 /* Pretend Insert mode here to allow the cursor on the
8917 * character past the end of the line */
8918 State = INSERT;
8919 coladvance((colnr_T)MAXCOL);
8920 State = save_State;
8921 }
8922 else
8923 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
8924}
8925
8926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 * Handle "A", "a", "I", "i" and <Insert> commands.
Bram Moolenaarec2da362017-01-21 20:04:22 +01008928 * Also handle K_PS, start bracketed paste.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 */
8930 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01008931nv_edit(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932{
8933 /* <Insert> is equal to "i" */
8934 if (cap->cmdchar == K_INS || cap->cmdchar == K_KINS)
8935 cap->cmdchar = 'i';
8936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 /* in Visual mode "A" and "I" are an operator */
8938 if (VIsual_active && (cap->cmdchar == 'A' || cap->cmdchar == 'I'))
Bram Moolenaareef9add2017-09-16 15:38:04 +02008939 {
8940#ifdef FEAT_TERMINAL
8941 if (term_in_normal_mode())
8942 {
8943 end_visual_mode();
8944 clearop(cap->oap);
8945 term_enter_job_mode();
8946 return;
8947 }
8948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 v_visop(cap);
Bram Moolenaareef9add2017-09-16 15:38:04 +02008950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
8952 /* in Visual mode and after an operator "a" and "i" are for text objects */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008953 else if ((cap->cmdchar == 'a' || cap->cmdchar == 'i')
8954 && (cap->oap->op_type != OP_NOP || VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
8956#ifdef FEAT_TEXTOBJ
8957 nv_object(cap);
8958#else
8959 clearopbeep(cap->oap);
8960#endif
8961 }
Bram Moolenaar662d9382017-07-31 22:56:24 +02008962#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02008963 else if (term_in_normal_mode())
Bram Moolenaar662d9382017-07-31 22:56:24 +02008964 {
8965 clearop(cap->oap);
Bram Moolenaar6d819742017-08-06 14:57:49 +02008966 term_enter_job_mode();
Bram Moolenaar662d9382017-07-31 22:56:24 +02008967 return;
8968 }
8969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 else if (!curbuf->b_p_ma && !p_im)
8971 {
8972 /* Only give this error when 'insertmode' is off. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008973 emsg(_(e_modifiable));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 clearop(cap->oap);
Bram Moolenaarec2da362017-01-21 20:04:22 +01008975 if (cap->cmdchar == K_PS)
8976 /* drop the pasted text */
8977 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 }
Bram Moolenaara1891842017-02-04 21:34:31 +01008979 else if (cap->cmdchar == K_PS && VIsual_active)
8980 {
8981 pos_T old_pos = curwin->w_cursor;
8982 pos_T old_visual = VIsual;
8983
8984 /* In Visual mode the selected text is deleted. */
8985 if (VIsual_mode == 'V' || curwin->w_cursor.lnum != VIsual.lnum)
8986 {
8987 shift_delete_registers();
8988 cap->oap->regname = '1';
8989 }
8990 else
8991 cap->oap->regname = '-';
8992 cap->cmdchar = 'd';
8993 cap->nchar = NUL;
8994 nv_operator(cap);
8995 do_pending_operator(cap, 0, FALSE);
8996 cap->cmdchar = K_PS;
8997
8998 /* When the last char in the line was deleted then append. Detect this
8999 * by checking if the cursor moved to before the Visual area. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009000 if (*ml_get_cursor() != NUL && LT_POS(curwin->w_cursor, old_pos)
9001 && LT_POS(curwin->w_cursor, old_visual))
Bram Moolenaara1891842017-02-04 21:34:31 +01009002 inc_cursor();
9003
9004 /* Insert to replace the deleted text with the pasted text. */
9005 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 else if (!checkclearopq(cap->oap))
9008 {
9009 switch (cap->cmdchar)
9010 {
9011 case 'A': /* "A"ppend after the line */
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02009012 set_cursor_for_append_to_line();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 break;
9014
9015 case 'I': /* "I"nsert before the first non-blank */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00009016 if (vim_strchr(p_cpo, CPO_INSEND) == NULL)
9017 beginline(BL_WHITE);
9018 else
9019 beginline(BL_WHITE|BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 break;
9021
Bram Moolenaara1891842017-02-04 21:34:31 +01009022 case K_PS:
9023 /* Bracketed paste works like "a"ppend, unless the cursor is in
9024 * the first column, then it inserts. */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009025 if (curwin->w_cursor.col == 0)
9026 break;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02009027 /* FALLTHROUGH */
Bram Moolenaarfd8983b2017-02-02 22:21:29 +01009028
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 case 'a': /* "a"ppend is like "i"nsert on the next character. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 /* increment coladd when in virtual space, increment the
9031 * column otherwise, also to append after an unprintable char */
9032 if (virtual_active()
9033 && (curwin->w_cursor.coladd > 0
9034 || *ml_get_cursor() == NUL
9035 || *ml_get_cursor() == TAB))
9036 curwin->w_cursor.coladd++;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01009037 else if (*ml_get_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 inc_cursor();
9039 break;
9040 }
9041
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 if (curwin->w_cursor.coladd && cap->cmdchar != 'A')
9043 {
9044 int save_State = State;
9045
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02009046 /* Pretend Insert mode here to allow the cursor on the
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 * character past the end of the line */
9048 State = INSERT;
9049 coladvance(getviscol());
9050 State = save_State;
9051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052
9053 invoke_edit(cap, FALSE, cap->cmdchar, FALSE);
9054 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009055 else if (cap->cmdchar == K_PS)
9056 /* drop the pasted text */
9057 bracketed_paste(PASTE_INSERT, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
9060/*
9061 * Invoke edit() and take care of "restart_edit" and the return value.
9062 */
9063 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009064invoke_edit(
9065 cmdarg_T *cap,
9066 int repl, /* "r" or "gr" command */
9067 int cmd,
9068 int startln)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
9070 int restart_edit_save = 0;
9071
9072 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
9073 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
9074 * it. */
9075 if (repl || !stuff_empty())
9076 restart_edit_save = restart_edit;
9077 else
9078 restart_edit_save = 0;
9079
9080 /* Always reset "restart_edit", this is not a restarted edit. */
9081 restart_edit = 0;
9082
9083 if (edit(cmd, startln, cap->count1))
9084 cap->retval |= CA_COMMAND_BUSY;
9085
9086 if (restart_edit == 0)
9087 restart_edit = restart_edit_save;
9088}
9089
9090#ifdef FEAT_TEXTOBJ
9091/*
9092 * "a" or "i" while an operator is pending or in Visual mode: object motion.
9093 */
9094 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009095nv_object(
9096 cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
9098 int flag;
9099 int include;
9100 char_u *mps_save;
9101
9102 if (cap->cmdchar == 'i')
9103 include = FALSE; /* "ix" = inner object: exclude white space */
9104 else
9105 include = TRUE; /* "ax" = an object: include white space */
9106
9107 /* Make sure (), [], {} and <> are in 'matchpairs' */
9108 mps_save = curbuf->b_p_mps;
9109 curbuf->b_p_mps = (char_u *)"(:),{:},[:],<:>";
9110
9111 switch (cap->nchar)
9112 {
9113 case 'w': /* "aw" = a word */
9114 flag = current_word(cap->oap, cap->count1, include, FALSE);
9115 break;
9116 case 'W': /* "aW" = a WORD */
9117 flag = current_word(cap->oap, cap->count1, include, TRUE);
9118 break;
9119 case 'b': /* "ab" = a braces block */
9120 case '(':
9121 case ')':
9122 flag = current_block(cap->oap, cap->count1, include, '(', ')');
9123 break;
9124 case 'B': /* "aB" = a Brackets block */
9125 case '{':
9126 case '}':
9127 flag = current_block(cap->oap, cap->count1, include, '{', '}');
9128 break;
9129 case '[': /* "a[" = a [] block */
9130 case ']':
9131 flag = current_block(cap->oap, cap->count1, include, '[', ']');
9132 break;
9133 case '<': /* "a<" = a <> block */
9134 case '>':
9135 flag = current_block(cap->oap, cap->count1, include, '<', '>');
9136 break;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009137 case 't': /* "at" = a tag block (xml and html) */
Bram Moolenaarb6c27352015-03-05 19:57:49 +01009138 /* Do not adjust oap->end in do_pending_operator()
9139 * otherwise there are different results for 'dit'
9140 * (note leading whitespace in last line):
9141 * 1) <b> 2) <b>
9142 * foobar foobar
9143 * </b> </b>
9144 */
9145 cap->retval |= CA_NO_ADJ_OP_END;
Bram Moolenaarf8c07b22005-07-19 22:10:03 +00009146 flag = current_tagblock(cap->oap, cap->count1, include);
9147 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 case 'p': /* "ap" = a paragraph */
9149 flag = current_par(cap->oap, cap->count1, include, 'p');
9150 break;
9151 case 's': /* "as" = a sentence */
9152 flag = current_sent(cap->oap, cap->count1, include);
9153 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00009154 case '"': /* "a"" = a double quoted string */
9155 case '\'': /* "a'" = a single quoted string */
9156 case '`': /* "a`" = a backtick quoted string */
9157 flag = current_quote(cap->oap, cap->count1, include,
9158 cap->nchar);
9159 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160#if 0 /* TODO */
9161 case 'S': /* "aS" = a section */
9162 case 'f': /* "af" = a filename */
9163 case 'u': /* "au" = a URL */
9164#endif
9165 default:
9166 flag = FAIL;
9167 break;
9168 }
9169
9170 curbuf->b_p_mps = mps_save;
9171 if (flag == FAIL)
9172 clearopbeep(cap->oap);
9173 adjust_cursor_col();
9174 curwin->w_set_curswant = TRUE;
9175}
9176#endif
9177
9178/*
9179 * "q" command: Start/stop recording.
9180 * "q:", "q/", "q?": edit command-line in command-line window.
9181 */
9182 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009183nv_record(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184{
9185 if (cap->oap->op_type == OP_FORMAT)
9186 {
9187 /* "gqq" is the same as "gqgq": format line */
9188 cap->cmdchar = 'g';
9189 cap->nchar = 'q';
9190 nv_operator(cap);
9191 }
9192 else if (!checkclearop(cap->oap))
9193 {
9194#ifdef FEAT_CMDWIN
9195 if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?')
9196 {
9197 stuffcharReadbuff(cap->nchar);
9198 stuffcharReadbuff(K_CMDWIN);
9199 }
9200 else
9201#endif
9202 /* (stop) recording into a named register, unless executing a
9203 * register */
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009204 if (reg_executing == 0 && do_record(cap->nchar) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 clearopbeep(cap->oap);
9206 }
9207}
9208
9209/*
9210 * Handle the "@r" command.
9211 */
9212 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009213nv_at(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
9215 if (checkclearop(cap->oap))
9216 return;
9217#ifdef FEAT_EVAL
9218 if (cap->nchar == '=')
9219 {
9220 if (get_expr_register() == NUL)
9221 return;
9222 }
9223#endif
9224 while (cap->count1-- && !got_int)
9225 {
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009226 if (do_execreg(cap->nchar, FALSE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 {
9228 clearopbeep(cap->oap);
9229 break;
9230 }
9231 line_breakcheck();
9232 }
9233}
9234
9235/*
9236 * Handle the CTRL-U and CTRL-D commands.
9237 */
9238 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009239nv_halfpage(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240{
9241 if ((cap->cmdchar == Ctrl_U && curwin->w_cursor.lnum == 1)
9242 || (cap->cmdchar == Ctrl_D
9243 && curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count))
9244 clearopbeep(cap->oap);
9245 else if (!checkclearop(cap->oap))
9246 halfpage(cap->cmdchar == Ctrl_D, cap->count0);
9247}
9248
9249/*
9250 * Handle "J" or "gJ" command.
9251 */
9252 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009253nv_join(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (VIsual_active) /* join the visual lines */
9256 nv_operator(cap);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009257 else if (!checkclearop(cap->oap))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 {
9259 if (cap->count0 <= 1)
9260 cap->count0 = 2; /* default for join is two lines! */
9261 if (curwin->w_cursor.lnum + cap->count0 - 1 >
9262 curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 {
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009264 /* can't join when on the last line */
9265 if (cap->count0 <= 2)
9266 {
9267 clearopbeep(cap->oap);
9268 return;
9269 }
9270 cap->count0 = curbuf->b_ml.ml_line_count
9271 - curwin->w_cursor.lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 }
Bram Moolenaar41e0f2f2016-03-08 14:44:42 +01009273
9274 prep_redo(cap->oap->regname, cap->count0,
9275 NUL, cap->cmdchar, NUL, NUL, cap->nchar);
9276 (void)do_join(cap->count0, cap->nchar == NUL, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 }
9278}
9279
9280/*
9281 * "P", "gP", "p" and "gp" commands.
9282 */
9283 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009284nv_put(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02009286 nv_put_opt(cap, FALSE);
9287}
9288
9289/*
9290 * "P", "gP", "p" and "gp" commands.
9291 * "fix_indent" is TRUE for "[p", "[P", "]p" and "]P".
9292 */
9293 static void
9294nv_put_opt(cmdarg_T *cap, int fix_indent)
9295{
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 {
Bram Moolenaar0ab190c2019-05-23 23:27:36 +02009324 if (fix_indent)
9325 {
9326 dir = (cap->cmdchar == ']' && cap->nchar == 'p')
9327 ? FORWARD : BACKWARD;
9328 flags |= PUT_FIXINDENT;
9329 }
9330 else
9331 dir = (cap->cmdchar == 'P'
9332 || (cap->cmdchar == 'g' && cap->nchar == 'P'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 ? BACKWARD : FORWARD;
9334 prep_redo_cmd(cap);
9335 if (cap->cmdchar == 'g')
9336 flags |= PUT_CURSEND;
9337
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 if (VIsual_active)
9339 {
9340 /* Putting in Visual mode: The put text replaces the selected
9341 * text. First delete the selected text, then put the new text.
9342 * Need to save and restore the registers that the delete
9343 * overwrites if the old contents is being put.
9344 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009345 was_visual = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 regname = cap->oap->regname;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009347#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 adjust_clip_reg(&regname);
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009349#endif
Bram Moolenaar7d311c52014-02-22 23:49:35 +01009350 if (regname == 0 || regname == '"'
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009351 || VIM_ISDIGIT(regname) || regname == '-'
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009352#ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 || (clip_unnamed && (regname == '*' || regname == '+'))
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355
9356 )
9357 {
Bram Moolenaarba6e8582012-12-12 18:20:32 +01009358 /* The delete is going to overwrite the register we want to
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 * put, save it first. */
9360 reg1 = get_register(regname, TRUE);
9361 }
9362
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02009363 // Now delete the selected text. Avoid messages here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 cap->cmdchar = 'd';
9365 cap->nchar = NUL;
9366 cap->oap->regname = NUL;
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02009367 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 nv_operator(cap);
9369 do_pending_operator(cap, 0, FALSE);
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009370 empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02009371 --msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372
9373 /* delete PUT_LINE_BACKWARD; */
9374 cap->oap->regname = regname;
9375
9376 if (reg1 != NULL)
9377 {
9378 /* Delete probably changed the register we want to put, save
9379 * it first. Then put back what was there before the delete. */
9380 reg2 = get_register(regname, FALSE);
9381 put_register(regname, reg1);
9382 }
9383
9384 /* When deleted a linewise Visual area, put the register as
9385 * lines to avoid it joined with the next line. When deletion was
9386 * characterwise, split a line when putting lines. */
9387 if (VIsual_mode == 'V')
9388 flags |= PUT_LINE;
9389 else if (VIsual_mode == 'v')
9390 flags |= PUT_LINE_SPLIT;
9391 if (VIsual_mode == Ctrl_V && dir == FORWARD)
9392 flags |= PUT_LINE_FORWARD;
9393 dir = BACKWARD;
9394 if ((VIsual_mode != 'V'
9395 && curwin->w_cursor.col < curbuf->b_op_start.col)
9396 || (VIsual_mode == 'V'
9397 && curwin->w_cursor.lnum < curbuf->b_op_start.lnum))
9398 /* cursor is at the end of the line or end of file, put
9399 * forward. */
9400 dir = FORWARD;
Bram Moolenaarec11aef2013-09-22 15:23:44 +02009401 /* May have been reset in do_put(). */
9402 VIsual_active = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 do_put(cap->oap->regname, dir, cap->count1, flags);
9405
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 /* If a register was saved, put it back now. */
9407 if (reg2 != NULL)
9408 put_register(regname, reg2);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009409
9410 /* What to reselect with "gv"? Selecting the just put text seems to
9411 * be the most useful, since the original text was removed. */
9412 if (was_visual)
9413 {
Bram Moolenaara226a6d2006-02-26 23:59:20 +00009414 curbuf->b_visual.vi_start = curbuf->b_op_start;
9415 curbuf->b_visual.vi_end = curbuf->b_op_end;
Bram Moolenaard29c6fe2015-11-19 20:11:54 +01009416 /* need to adjust cursor position */
9417 if (*p_sel == 'e')
9418 inc(&curbuf->b_visual.vi_end);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009419 }
9420
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009421 /* When all lines were selected and deleted do_put() leaves an empty
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009422 * line that needs to be deleted now. */
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009423 if (empty && *ml_get(curbuf->b_ml.ml_line_count) == NUL)
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009424 {
Bram Moolenaarcf3630f2005-01-08 16:04:29 +00009425 ml_delete(curbuf->b_ml.ml_line_count, TRUE);
Bram Moolenaar9a4a8c42019-08-19 22:48:30 +02009426 deleted_lines(curbuf->b_ml.ml_line_count + 1, 1);
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00009427
9428 /* If the cursor was in that line, move it to the end of the last
9429 * line. */
9430 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
9431 {
9432 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9433 coladvance((colnr_T)MAXCOL);
9434 }
9435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 auto_format(FALSE, TRUE);
9437 }
9438}
9439
9440/*
9441 * "o" and "O" commands.
9442 */
9443 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009444nv_open(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445{
9446#ifdef FEAT_DIFF
9447 /* "do" is ":diffget" */
9448 if (cap->oap->op_type == OP_DELETE && cap->cmdchar == 'o')
9449 {
9450 clearop(cap->oap);
Bram Moolenaar6a643652014-10-31 13:54:25 +01009451 nv_diffgetput(FALSE, cap->opcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 }
9453 else
9454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 if (VIsual_active) /* switch start and end of visual */
9456 v_swap_corners(cap->cmdchar);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009457#ifdef FEAT_JOB_CHANNEL
9458 else if (bt_prompt(curbuf))
Bram Moolenaarf2732452018-06-03 14:47:35 +02009459 clearopbeep(cap->oap);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 n_opencmd(cap);
9463}
9464
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465#ifdef FEAT_NETBEANS_INTG
9466 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009467nv_nbcmd(cmdarg_T *cap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469 netbeans_keycommand(cap->nchar);
9470}
9471#endif
9472
9473#ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009475nv_drop(cmdarg_T *cap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
9477 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9478}
9479#endif
Bram Moolenaar3918c952005-03-15 22:34:55 +00009480
Bram Moolenaar3918c952005-03-15 22:34:55 +00009481/*
9482 * Trigger CursorHold event.
9483 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9484 * input buffer. "did_cursorhold" is set to avoid retriggering.
9485 */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009486 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009487nv_cursorhold(cmdarg_T *cap)
Bram Moolenaar3918c952005-03-15 22:34:55 +00009488{
9489 apply_autocmds(EVENT_CURSORHOLD, NULL, NULL, FALSE, curbuf);
9490 did_cursorhold = TRUE;
Bram Moolenaarfc735152005-03-22 22:54:12 +00009491 cap->retval |= CA_COMMAND_BUSY; /* don't call edit() now */
Bram Moolenaar3918c952005-03-15 22:34:55 +00009492}
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009493
9494/*
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009495 * Calculate start/end virtual columns for operating in block mode.
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009496 */
9497 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009498get_op_vcol(
9499 oparg_T *oap,
9500 colnr_T redo_VIsual_vcol,
9501 int initial) /* when TRUE adjust position for 'selectmode' */
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009502{
9503 colnr_T start, end;
9504
Bram Moolenaar89c17c02015-08-11 17:46:36 +02009505 if (VIsual_mode != Ctrl_V
Bram Moolenaar02631462017-09-22 15:20:32 +02009506 || (!initial && oap->end.col < curwin->w_width))
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009507 return;
9508
Bram Moolenaar10ad1d92015-09-25 19:35:02 +02009509 oap->block_mode = TRUE;
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009510
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009511 /* prevent from moving onto a trail byte */
9512 if (has_mbyte)
9513 mb_adjustpos(curwin->w_buffer, &oap->end);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009514
9515 getvvcol(curwin, &(oap->start), &oap->start_vcol, NULL, &oap->end_vcol);
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009516
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009517 if (!redo_VIsual_busy)
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009518 {
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009519 getvvcol(curwin, &(oap->end), &start, NULL, &end);
9520
9521 if (start < oap->start_vcol)
9522 oap->start_vcol = start;
9523 if (end > oap->end_vcol)
9524 {
9525 if (initial && *p_sel == 'e' && start >= 1
9526 && start - 1 >= oap->end_vcol)
9527 oap->end_vcol = start - 1;
9528 else
9529 oap->end_vcol = end;
9530 }
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009531 }
Bram Moolenaar31b259b2015-07-28 11:21:32 +02009532
Bram Moolenaar74db34c2015-06-25 13:30:46 +02009533 /* if '$' was used, get oap->end_vcol from longest line */
9534 if (curwin->w_curswant == MAXCOL)
9535 {
9536 curwin->w_cursor.col = MAXCOL;
9537 oap->end_vcol = 0;
9538 for (curwin->w_cursor.lnum = oap->start.lnum;
9539 curwin->w_cursor.lnum <= oap->end.lnum;
9540 ++curwin->w_cursor.lnum)
9541 {
9542 getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end);
9543 if (end > oap->end_vcol)
9544 oap->end_vcol = end;
9545 }
9546 }
9547 else if (redo_VIsual_busy)
9548 oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1;
9549 /*
9550 * Correct oap->end.col and oap->start.col to be the
9551 * upper-left and lower-right corner of the block area.
9552 *
9553 * (Actually, this does convert column positions into character
9554 * positions)
9555 */
9556 curwin->w_cursor.lnum = oap->end.lnum;
9557 coladvance(oap->end_vcol);
9558 oap->end = curwin->w_cursor;
9559
9560 curwin->w_cursor = oap->start;
9561 coladvance(oap->start_vcol);
9562 oap->start = curwin->w_cursor;
9563}